FreeBSD kernel kern code
kern_ntptime.c File Reference
#include <sys/cdefs.h>
#include "opt_ntp.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/eventhandler.h>
#include <sys/kernel.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/time.h>
#include <sys/timex.h>
#include <sys/timetc.h>
#include <sys/timepps.h>
#include <sys/syscallsubr.h>
#include <sys/sysctl.h>
Include dependency graph for kern_ntptime.c:

Go to the source code of this file.

Data Structures

struct  ntp_gettime_args
 
struct  ntp_adjtime_args
 
struct  adjtime_args
 

Macros

#define L_ADD(v, u)   ((v) += (u))
 
#define L_SUB(v, u)   ((v) -= (u))
 
#define L_ADDHI(v, a)   ((v) += (int64_t)(a) << 32)
 
#define L_NEG(v)   ((v) = -(v))
 
#define L_RSHIFT(v, n)
 
#define L_MPY(v, a)   ((v) *= (a))
 
#define L_CLR(v)   ((v) = 0)
 
#define L_ISNEG(v)   ((v) < 0)
 
#define L_LINT(v, a)   ((v) = (int64_t)(a) << 32)
 
#define L_GINT(v)   ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
 
#define SHIFT_PLL   4 /* PLL loop gain (shift) */
 
#define SHIFT_FLL   2 /* FLL loop gain (shift) */
 
#define NTP_LOCK()   mtx_lock_spin(&ntp_lock)
 
#define NTP_UNLOCK()   mtx_unlock_spin(&ntp_lock)
 
#define NTP_ASSERT_LOCKED()   mtx_assert(&ntp_lock, MA_OWNED)
 

Typedefs

typedef int64_t l_fp
 

Functions

 __FBSDID ("$FreeBSD$")
 
 MTX_SYSINIT (ntp, &ntp_lock, "ntp", MTX_SPIN)
 
static void hardupdate (long offset)
 
static void ntp_gettime1 (struct ntptimeval *ntvp)
 
static bool ntp_is_time_error (int tsl)
 
int sys_ntp_gettime (struct thread *td, struct ntp_gettime_args *uap)
 
static int ntp_sysctl (SYSCTL_HANDLER_ARGS)
 
 SYSCTL_NODE (_kern, OID_AUTO, ntp_pll, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "")
 
 SYSCTL_PROC (_kern_ntp_pll, OID_AUTO, gettime, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE, 0, sizeof(struct ntptimeval), ntp_sysctl, "S,ntptimeval", "")
 
int kern_ntp_adjtime (struct thread *td, struct timex *ntv, int *retvalp)
 
int sys_ntp_adjtime (struct thread *td, struct ntp_adjtime_args *uap)
 
void ntp_update_second (int64_t *adjustment, time_t *newsec)
 
int sys_adjtime (struct thread *td, struct adjtime_args *uap)
 
int kern_adjtime (struct thread *td, struct timeval *delta, struct timeval *olddelta)
 
static void periodic_resettodr (void *arg __unused)
 
static void shutdown_resettodr (void *arg __unused, int howto __unused)
 
static int sysctl_resettodr_period (SYSCTL_HANDLER_ARGS)
 
 SYSCTL_PROC (_machdep, OID_AUTO, rtc_save_period, CTLTYPE_INT|CTLFLAG_RWTUN|CTLFLAG_MPSAFE, &resettodr_period, 1800, sysctl_resettodr_period, "I", "Save system time to RTC with this period (in seconds)")
 
static void start_periodic_resettodr (void *arg __unused)
 
 SYSINIT (periodic_resettodr, SI_SUB_LAST, SI_ORDER_MIDDLE, start_periodic_resettodr, NULL)
 

Variables

static int time_state = TIME_OK
 
int time_status = STA_UNSYNC
 
static long time_tai
 
static long time_monitor
 
static long time_constant
 
static long time_precision = 1
 
static long time_maxerror = MAXPHASE / 1000
 
long time_esterror = MAXPHASE / 1000
 
static long time_reftime
 
static l_fp time_offset
 
static l_fp time_freq
 
static l_fp time_adj
 
static int64_t time_adjtime
 
static struct mtx ntp_lock
 
static struct callout resettodr_callout
 
static int resettodr_period = 1800
 

Macro Definition Documentation

◆ L_ADD

#define L_ADD (   v,
 
)    ((v) += (u))

Definition at line 62 of file kern_ntptime.c.

◆ L_ADDHI

#define L_ADDHI (   v,
 
)    ((v) += (int64_t)(a) << 32)

Definition at line 64 of file kern_ntptime.c.

◆ L_CLR

#define L_CLR (   v)    ((v) = 0)

Definition at line 74 of file kern_ntptime.c.

◆ L_GINT

#define L_GINT (   v)    ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)

Definition at line 77 of file kern_ntptime.c.

◆ L_ISNEG

#define L_ISNEG (   v)    ((v) < 0)

Definition at line 75 of file kern_ntptime.c.

◆ L_LINT

#define L_LINT (   v,
 
)    ((v) = (int64_t)(a) << 32)

Definition at line 76 of file kern_ntptime.c.

◆ L_MPY

#define L_MPY (   v,
 
)    ((v) *= (a))

Definition at line 73 of file kern_ntptime.c.

◆ L_NEG

#define L_NEG (   v)    ((v) = -(v))

Definition at line 65 of file kern_ntptime.c.

◆ L_RSHIFT

#define L_RSHIFT (   v,
 
)
Value:
do { \
if ((v) < 0) \
(v) = -(-(v) >> (n)); \
else \
(v) = (v) >> (n); \
} while (0)

Definition at line 66 of file kern_ntptime.c.

◆ L_SUB

#define L_SUB (   v,
 
)    ((v) -= (u))

Definition at line 63 of file kern_ntptime.c.

◆ NTP_ASSERT_LOCKED

#define NTP_ASSERT_LOCKED ( )    mtx_assert(&ntp_lock, MA_OWNED)

Definition at line 170 of file kern_ntptime.c.

◆ NTP_LOCK

#define NTP_LOCK ( )    mtx_lock_spin(&ntp_lock)

Definition at line 168 of file kern_ntptime.c.

◆ NTP_UNLOCK

#define NTP_UNLOCK ( )    mtx_unlock_spin(&ntp_lock)

Definition at line 169 of file kern_ntptime.c.

◆ SHIFT_FLL

#define SHIFT_FLL   2 /* FLL loop gain (shift) */

Definition at line 148 of file kern_ntptime.c.

◆ SHIFT_PLL

#define SHIFT_PLL   4 /* PLL loop gain (shift) */

Definition at line 147 of file kern_ntptime.c.

Typedef Documentation

◆ l_fp

typedef int64_t l_fp

Definition at line 61 of file kern_ntptime.c.

Function Documentation

◆ __FBSDID()

__FBSDID ( "$FreeBSD$"  )

◆ hardupdate()

static void hardupdate ( long  offset)
static

Definition at line 656 of file kern_ntptime.c.

References L_ADD, L_GINT, L_LINT, L_MPY, L_RSHIFT, NTP_ASSERT_LOCKED, SHIFT_FLL, SHIFT_PLL, time_constant, time_freq, time_monitor, time_offset, time_reftime, time_status, and time_uptime.

Referenced by kern_ntp_adjtime().

Here is the caller graph for this function:

◆ kern_adjtime()

int kern_adjtime ( struct thread *  td,
struct timeval *  delta,
struct timeval *  olddelta 
)

Definition at line 960 of file kern_ntptime.c.

References NTP_LOCK, NTP_UNLOCK, priv_check(), and time_adjtime.

Referenced by sys_adjtime().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kern_ntp_adjtime()

int kern_ntp_adjtime ( struct thread *  td,
struct timex *  ntv,
int *  retvalp 
)

Definition at line 341 of file kern_ntptime.c.

References hardupdate(), L_GINT, L_LINT, ntp_is_time_error(), NTP_LOCK, NTP_UNLOCK, priv_check(), time_constant, time_esterror, time_freq, time_maxerror, time_offset, time_precision, time_state, time_status, and time_tai.

Referenced by sys_ntp_adjtime().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MTX_SYSINIT()

MTX_SYSINIT ( ntp  ,
ntp_lock,
"ntp"  ,
MTX_SPIN   
)

◆ ntp_gettime1()

static void ntp_gettime1 ( struct ntptimeval *  ntvp)
static

Definition at line 252 of file kern_ntptime.c.

References nanotime(), NTP_ASSERT_LOCKED, ntp_is_time_error(), time_esterror, time_maxerror, time_state, time_status, and time_tai.

Referenced by ntp_sysctl(), and sys_ntp_gettime().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ntp_is_time_error()

static bool ntp_is_time_error ( int  tsl)
static

Definition at line 215 of file kern_ntptime.c.

Referenced by kern_ntp_adjtime(), ntp_gettime1(), periodic_resettodr(), and shutdown_resettodr().

Here is the caller graph for this function:

◆ ntp_sysctl()

static int ntp_sysctl ( SYSCTL_HANDLER_ARGS  )
static

Definition at line 298 of file kern_ntptime.c.

References ntp_gettime1(), NTP_LOCK, NTP_UNLOCK, and sysctl_handle_opaque().

Here is the call graph for this function:

◆ ntp_update_second()

void ntp_update_second ( int64_t *  adjustment,
time_t *  newsec 
)

Definition at line 503 of file kern_ntptime.c.

References L_ADD, L_LINT, L_RSHIFT, L_SUB, NTP_LOCK, NTP_UNLOCK, SHIFT_PLL, time_adj, time_adjtime, time_constant, time_freq, time_maxerror, time_offset, time_state, time_status, and time_tai.

Referenced by tc_windup().

Here is the caller graph for this function:

◆ periodic_resettodr()

static void periodic_resettodr ( void *arg  __unused)
static

Definition at line 993 of file kern_ntptime.c.

References callout_schedule(), hz, ntp_is_time_error(), resettodr(), resettodr_callout, resettodr_period, and time_status.

Referenced by start_periodic_resettodr(), and sysctl_resettodr_period().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown_resettodr()

static void shutdown_resettodr ( void *arg  __unused,
int howto  __unused 
)
static

Definition at line 1007 of file kern_ntptime.c.

References ntp_is_time_error(), resettodr(), resettodr_callout, resettodr_period, and time_status.

Referenced by start_periodic_resettodr().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_periodic_resettodr()

static void start_periodic_resettodr ( void *arg  __unused)
static

Definition at line 1040 of file kern_ntptime.c.

References callout_init(), hz, periodic_resettodr(), resettodr_callout, resettodr_period, and shutdown_resettodr().

Here is the call graph for this function:

◆ sys_adjtime()

int sys_adjtime ( struct thread *  td,
struct adjtime_args uap 
)

Definition at line 941 of file kern_ntptime.c.

References adjtime_args::delta, kern_adjtime(), and adjtime_args::olddelta.

Here is the call graph for this function:

◆ sys_ntp_adjtime()

int sys_ntp_adjtime ( struct thread *  td,
struct ntp_adjtime_args uap 
)

Definition at line 477 of file kern_ntptime.c.

References kern_ntp_adjtime(), and ntp_adjtime_args::tp.

Here is the call graph for this function:

◆ sys_ntp_gettime()

int sys_ntp_gettime ( struct thread *  td,
struct ntp_gettime_args uap 
)

Definition at line 283 of file kern_ntptime.c.

References ntp_gettime1(), NTP_LOCK, NTP_UNLOCK, and ntp_gettime_args::ntvp.

Here is the call graph for this function:

◆ SYSCTL_NODE()

SYSCTL_NODE ( _kern  ,
OID_AUTO  ,
ntp_pll  ,
CTLFLAG_RW|  CTLFLAG_MPSAFE,
,
""   
)

◆ SYSCTL_PROC() [1/2]

SYSCTL_PROC ( _kern_ntp_pll  ,
OID_AUTO  ,
gettime  ,
CTLTYPE_OPAQUE|CTLFLAG_RD|  CTLFLAG_MPSAFE,
,
sizeof(struct ntptimeval)  ,
ntp_sysctl  ,
S,
ntptimeval"  ,
""   
)

◆ SYSCTL_PROC() [2/2]

SYSCTL_PROC ( _machdep  ,
OID_AUTO  ,
rtc_save_period  ,
CTLTYPE_INT|CTLFLAG_RWTUN|  CTLFLAG_MPSAFE,
resettodr_period,
1800  ,
sysctl_resettodr_period  ,
"I"  ,
"Save system time to RTC with this period (in seconds)"   
)

◆ sysctl_resettodr_period()

static int sysctl_resettodr_period ( SYSCTL_HANDLER_ARGS  )
static

Definition at line 1017 of file kern_ntptime.c.

References hz, periodic_resettodr(), resettodr_callout, resettodr_period, and sysctl_handle_int().

Here is the call graph for this function:

◆ SYSINIT()

SYSINIT ( periodic_resettodr  ,
SI_SUB_LAST  ,
SI_ORDER_MIDDLE  ,
start_periodic_resettodr  ,
NULL   
)

Variable Documentation

◆ ntp_lock

struct mtx ntp_lock
static

Definition at line 165 of file kern_ntptime.c.

◆ resettodr_callout

struct callout resettodr_callout
static

◆ resettodr_period

int resettodr_period = 1800
static

◆ time_adj

l_fp time_adj
static

Definition at line 161 of file kern_ntptime.c.

Referenced by ntp_update_second().

◆ time_adjtime

int64_t time_adjtime
static

Definition at line 163 of file kern_ntptime.c.

Referenced by kern_adjtime(), and ntp_update_second().

◆ time_constant

long time_constant
static

Definition at line 154 of file kern_ntptime.c.

Referenced by hardupdate(), kern_ntp_adjtime(), and ntp_update_second().

◆ time_esterror

long time_esterror = MAXPHASE / 1000

Definition at line 157 of file kern_ntptime.c.

Referenced by kern_ntp_adjtime(), ntp_gettime1(), and sysclock_getsnapshot().

◆ time_freq

l_fp time_freq
static

Definition at line 160 of file kern_ntptime.c.

Referenced by hardupdate(), kern_ntp_adjtime(), and ntp_update_second().

◆ time_maxerror

long time_maxerror = MAXPHASE / 1000
static

Definition at line 156 of file kern_ntptime.c.

Referenced by kern_ntp_adjtime(), ntp_gettime1(), and ntp_update_second().

◆ time_monitor

long time_monitor
static

Definition at line 153 of file kern_ntptime.c.

Referenced by hardupdate().

◆ time_offset

l_fp time_offset
static

Definition at line 159 of file kern_ntptime.c.

Referenced by hardupdate(), kern_ntp_adjtime(), and ntp_update_second().

◆ time_precision

long time_precision = 1
static

Definition at line 155 of file kern_ntptime.c.

Referenced by kern_ntp_adjtime().

◆ time_reftime

long time_reftime
static

Definition at line 158 of file kern_ntptime.c.

Referenced by hardupdate().

◆ time_state

int time_state = TIME_OK
static

Definition at line 150 of file kern_ntptime.c.

Referenced by kern_ntp_adjtime(), ntp_gettime1(), and ntp_update_second().

◆ time_status

◆ time_tai

long time_tai
static

Definition at line 152 of file kern_ntptime.c.

Referenced by kern_ntp_adjtime(), ntp_gettime1(), and ntp_update_second().