xref: /xnu-8792.81.2/bsd/sys/timex.h (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*-
2*19c3b8c2SApple OSS Distributions  ***********************************************************************
3*19c3b8c2SApple OSS Distributions  *								       *
4*19c3b8c2SApple OSS Distributions  * Copyright (c) David L. Mills 1993-2001			       *
5*19c3b8c2SApple OSS Distributions  * Copyright (c) Poul-Henning Kamp 2000-2001                           *
6*19c3b8c2SApple OSS Distributions  *								       *
7*19c3b8c2SApple OSS Distributions  * Permission to use, copy, modify, and distribute this software and   *
8*19c3b8c2SApple OSS Distributions  * its documentation for any purpose and without fee is hereby	       *
9*19c3b8c2SApple OSS Distributions  * granted, provided that the above copyright notice appears in all    *
10*19c3b8c2SApple OSS Distributions  * copies and that both the copyright notice and this permission       *
11*19c3b8c2SApple OSS Distributions  * notice appear in supporting documentation, and that the name        *
12*19c3b8c2SApple OSS Distributions  * University of Delaware not be used in advertising or publicity      *
13*19c3b8c2SApple OSS Distributions  * pertaining to distribution of the software without specific,	       *
14*19c3b8c2SApple OSS Distributions  * written prior permission. The University of Delaware makes no       *
15*19c3b8c2SApple OSS Distributions  * representations about the suitability this software for any	       *
16*19c3b8c2SApple OSS Distributions  * purpose. It is provided "as is" without express or implied	       *
17*19c3b8c2SApple OSS Distributions  * warranty.							       *
18*19c3b8c2SApple OSS Distributions  *								       *
19*19c3b8c2SApple OSS Distributions  ***********************************************************************
20*19c3b8c2SApple OSS Distributions  *
21*19c3b8c2SApple OSS Distributions  * $FreeBSD$
22*19c3b8c2SApple OSS Distributions  *
23*19c3b8c2SApple OSS Distributions  * This header file defines the Network Time Protocol (NTP) interfaces
24*19c3b8c2SApple OSS Distributions  * for user and daemon application programs.
25*19c3b8c2SApple OSS Distributions  *
26*19c3b8c2SApple OSS Distributions  * This file was originally created 17 Sep 93 by David L. Mills, Professor
27*19c3b8c2SApple OSS Distributions  * of University of Delaware, building on work which had already been ongoing
28*19c3b8c2SApple OSS Distributions  * for a decade and a half at that point in time.
29*19c3b8c2SApple OSS Distributions  *
30*19c3b8c2SApple OSS Distributions  * In 2000 the APIs got a upgrade from microseconds to nanoseconds,
31*19c3b8c2SApple OSS Distributions  * a joint work between Poul-Henning Kamp and David L. Mills.
32*19c3b8c2SApple OSS Distributions  *
33*19c3b8c2SApple OSS Distributions  */
34*19c3b8c2SApple OSS Distributions 
35*19c3b8c2SApple OSS Distributions /*
36*19c3b8c2SApple OSS Distributions  * Copyright (c) 2017 Apple Computer, Inc. All rights reserved.
37*19c3b8c2SApple OSS Distributions  *
38*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39*19c3b8c2SApple OSS Distributions  *
40*19c3b8c2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
41*19c3b8c2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
42*19c3b8c2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
43*19c3b8c2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
44*19c3b8c2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
45*19c3b8c2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
46*19c3b8c2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
47*19c3b8c2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
48*19c3b8c2SApple OSS Distributions  *
49*19c3b8c2SApple OSS Distributions  * Please obtain a copy of the License at
50*19c3b8c2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
51*19c3b8c2SApple OSS Distributions  *
52*19c3b8c2SApple OSS Distributions  * The Original Code and all software distributed under the License are
53*19c3b8c2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
54*19c3b8c2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
55*19c3b8c2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
56*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
57*19c3b8c2SApple OSS Distributions  * Please see the License for the specific language governing rights and
58*19c3b8c2SApple OSS Distributions  * limitations under the License.
59*19c3b8c2SApple OSS Distributions  *
60*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
61*19c3b8c2SApple OSS Distributions  */
62*19c3b8c2SApple OSS Distributions 
63*19c3b8c2SApple OSS Distributions #ifndef _SYS_TIMEX_H_
64*19c3b8c2SApple OSS Distributions #define _SYS_TIMEX_H_ 1
65*19c3b8c2SApple OSS Distributions 
66*19c3b8c2SApple OSS Distributions #include <sys/time.h>
67*19c3b8c2SApple OSS Distributions 
68*19c3b8c2SApple OSS Distributions #define NTP_API         4               /* NTP API version */
69*19c3b8c2SApple OSS Distributions 
70*19c3b8c2SApple OSS Distributions /*
71*19c3b8c2SApple OSS Distributions  * The following defines establish the performance envelope of the
72*19c3b8c2SApple OSS Distributions  * kernel discipline loop. Phase or frequency errors greater than
73*19c3b8c2SApple OSS Distributions  * NAXPHASE or MAXFREQ are clamped to these maxima. For update intervals
74*19c3b8c2SApple OSS Distributions  * less than MINSEC, the loop always operates in PLL mode; while, for
75*19c3b8c2SApple OSS Distributions  * update intervals greater than MAXSEC, the loop always operates in FLL
76*19c3b8c2SApple OSS Distributions  * mode. Between these two limits the operating mode is selected by the
77*19c3b8c2SApple OSS Distributions  * STA_FLL bit in the status word.
78*19c3b8c2SApple OSS Distributions  */
79*19c3b8c2SApple OSS Distributions 
80*19c3b8c2SApple OSS Distributions #define MAXPHASE        500000000L      /* max phase error (ns) */
81*19c3b8c2SApple OSS Distributions #define MAXFREQ         500000L         /* max freq error (ns/s) */
82*19c3b8c2SApple OSS Distributions #define MINSEC          256             /* min FLL update interval (s) */
83*19c3b8c2SApple OSS Distributions #define MAXSEC          2048            /* max PLL update interval (s) */
84*19c3b8c2SApple OSS Distributions #define NANOSECOND      1000000000L     /* nanoseconds in one second */
85*19c3b8c2SApple OSS Distributions #define SCALE_PPM       (65536 / 1000)  /* crude ns/s to scaled PPM */
86*19c3b8c2SApple OSS Distributions #define MAXTC           10              /* max time constant */
87*19c3b8c2SApple OSS Distributions 
88*19c3b8c2SApple OSS Distributions /* Codes for PPS (pulse-per-second) signals or leap seconds are not used but kept
89*19c3b8c2SApple OSS Distributions  * unchanged and commented for future compatibility.
90*19c3b8c2SApple OSS Distributions  */
91*19c3b8c2SApple OSS Distributions 
92*19c3b8c2SApple OSS Distributions /*
93*19c3b8c2SApple OSS Distributions  * Control mode codes (timex.modes)
94*19c3b8c2SApple OSS Distributions  */
95*19c3b8c2SApple OSS Distributions #define MOD_OFFSET      0x0001          /* set time offset */
96*19c3b8c2SApple OSS Distributions #define MOD_FREQUENCY   0x0002          /* set frequency offset */
97*19c3b8c2SApple OSS Distributions #define MOD_MAXERROR    0x0004          /* set maximum time error */
98*19c3b8c2SApple OSS Distributions #define MOD_ESTERROR    0x0008          /* set estimated time error */
99*19c3b8c2SApple OSS Distributions #define MOD_STATUS      0x0010          /* set clock status bits */
100*19c3b8c2SApple OSS Distributions #define MOD_TIMECONST   0x0020          /* set PLL time constant */
101*19c3b8c2SApple OSS Distributions #define MOD_PPSMAX      0x0040          /* set PPS maximum averaging time */
102*19c3b8c2SApple OSS Distributions #define MOD_TAI         0x0080          /* set TAI offset */
103*19c3b8c2SApple OSS Distributions #define MOD_MICRO       0x1000          /* select microsecond resolution */
104*19c3b8c2SApple OSS Distributions #define MOD_NANO        0x2000          /* select nanosecond resolution */
105*19c3b8c2SApple OSS Distributions #define MOD_CLKB        0x4000          /* select clock B */
106*19c3b8c2SApple OSS Distributions #define MOD_CLKA        0x8000          /* select clock A */
107*19c3b8c2SApple OSS Distributions 
108*19c3b8c2SApple OSS Distributions /*
109*19c3b8c2SApple OSS Distributions  * Status codes (timex.status)
110*19c3b8c2SApple OSS Distributions  */
111*19c3b8c2SApple OSS Distributions #define STA_PLL         0x0001          /* enable PLL updates (rw) */
112*19c3b8c2SApple OSS Distributions #define STA_PPSFREQ     0x0002          /* enable PPS freq discipline (rw) */
113*19c3b8c2SApple OSS Distributions #define STA_PPSTIME     0x0004          /* enable PPS time discipline (rw) */
114*19c3b8c2SApple OSS Distributions #define STA_FLL         0x0008          /* enable FLL mode (rw) */
115*19c3b8c2SApple OSS Distributions #define STA_INS         0x0010          /* insert leap (rw) */
116*19c3b8c2SApple OSS Distributions #define STA_DEL         0x0020          /* delete leap (rw) */
117*19c3b8c2SApple OSS Distributions #define STA_UNSYNC      0x0040          /* clock unsynchronized (rw) */
118*19c3b8c2SApple OSS Distributions #define STA_FREQHOLD    0x0080          /* hold frequency (rw) */
119*19c3b8c2SApple OSS Distributions #define STA_PPSSIGNAL   0x0100          /* PPS signal present (ro) */
120*19c3b8c2SApple OSS Distributions #define STA_PPSJITTER   0x0200          /* PPS signal jitter exceeded (ro) */
121*19c3b8c2SApple OSS Distributions #define STA_PPSWANDER   0x0400          /* PPS signal wander exceeded (ro) */
122*19c3b8c2SApple OSS Distributions #define STA_PPSERROR    0x0800          /* PPS signal calibration error (ro) */
123*19c3b8c2SApple OSS Distributions #define STA_CLOCKERR    0x1000          /* clock hardware fault (ro) */
124*19c3b8c2SApple OSS Distributions #define STA_NANO        0x2000          /* resolution (0 = us, 1 = ns) (ro) */
125*19c3b8c2SApple OSS Distributions #define STA_MODE        0x4000          /* mode (0 = PLL, 1 = FLL) (ro) */
126*19c3b8c2SApple OSS Distributions #define STA_CLK         0x8000          /* clock source (0 = A, 1 = B) (ro) */
127*19c3b8c2SApple OSS Distributions 
128*19c3b8c2SApple OSS Distributions #define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
129*19c3b8c2SApple OSS Distributions     STA_PPSERROR | STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK)
130*19c3b8c2SApple OSS Distributions 
131*19c3b8c2SApple OSS Distributions #define STA_SUPPORTED (STA_PLL | STA_FLL | STA_UNSYNC | STA_FREQHOLD | \
132*19c3b8c2SApple OSS Distributions     STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK)
133*19c3b8c2SApple OSS Distributions 
134*19c3b8c2SApple OSS Distributions /*
135*19c3b8c2SApple OSS Distributions  * Clock states (ntptimeval.time_state)
136*19c3b8c2SApple OSS Distributions  */
137*19c3b8c2SApple OSS Distributions #define TIME_OK         0               /* no leap second warning */
138*19c3b8c2SApple OSS Distributions #define TIME_INS        1               /* insert leap second warning */
139*19c3b8c2SApple OSS Distributions #define TIME_DEL        2               /* delete leap second warning */
140*19c3b8c2SApple OSS Distributions #define TIME_OOP        3               /* leap second in progress */
141*19c3b8c2SApple OSS Distributions #define TIME_WAIT       4               /* leap second has occurred */
142*19c3b8c2SApple OSS Distributions #define TIME_ERROR      5               /* error (see status word) */
143*19c3b8c2SApple OSS Distributions 
144*19c3b8c2SApple OSS Distributions /*
145*19c3b8c2SApple OSS Distributions  * NTP user interface -- ntp_gettime - used to read kernel clock values
146*19c3b8c2SApple OSS Distributions  */
147*19c3b8c2SApple OSS Distributions struct ntptimeval {
148*19c3b8c2SApple OSS Distributions 	struct timespec time;           /* current time (ns) (ro) */
149*19c3b8c2SApple OSS Distributions 	long maxerror;                  /* maximum error (us) (ro) */
150*19c3b8c2SApple OSS Distributions 	long esterror;                  /* estimated error (us) (ro) */
151*19c3b8c2SApple OSS Distributions 	long tai;                       /* TAI offset */
152*19c3b8c2SApple OSS Distributions 	int time_state;                 /* time status */
153*19c3b8c2SApple OSS Distributions };
154*19c3b8c2SApple OSS Distributions 
155*19c3b8c2SApple OSS Distributions /*
156*19c3b8c2SApple OSS Distributions  * NTP daemon interface -- ntp_adjtime -- used to discipline CPU clock
157*19c3b8c2SApple OSS Distributions  * oscillator and control/determine status.
158*19c3b8c2SApple OSS Distributions  *
159*19c3b8c2SApple OSS Distributions  * Note: The offset, precision and jitter members are in microseconds if
160*19c3b8c2SApple OSS Distributions  * STA_NANO is zero and nanoseconds if not.
161*19c3b8c2SApple OSS Distributions  */
162*19c3b8c2SApple OSS Distributions struct timex {
163*19c3b8c2SApple OSS Distributions 	unsigned int modes;             /* clock mode bits (wo) */
164*19c3b8c2SApple OSS Distributions 	long    offset;                 /* time offset (ns/us) (rw) */
165*19c3b8c2SApple OSS Distributions 	long    freq;                   /* frequency offset (scaled PPM) (rw) */
166*19c3b8c2SApple OSS Distributions 	long    maxerror;               /* maximum error (us) (rw) */
167*19c3b8c2SApple OSS Distributions 	long    esterror;               /* estimated error (us) (rw) */
168*19c3b8c2SApple OSS Distributions 	int     status;                 /* clock status bits (rw) */
169*19c3b8c2SApple OSS Distributions 	long    constant;               /* poll interval (log2 s) (rw) */
170*19c3b8c2SApple OSS Distributions 	long    precision;              /* clock precision (ns/us) (ro) */
171*19c3b8c2SApple OSS Distributions 	long    tolerance;              /* clock frequency tolerance (scaled
172*19c3b8c2SApple OSS Distributions 	                                 * PPM) (ro) */
173*19c3b8c2SApple OSS Distributions 	/*
174*19c3b8c2SApple OSS Distributions 	 * The following read-only structure members are used by
175*19c3b8c2SApple OSS Distributions 	 * the PPS signal discipline that is currently not supported.
176*19c3b8c2SApple OSS Distributions 	 * They are included for compatibility.
177*19c3b8c2SApple OSS Distributions 	 */
178*19c3b8c2SApple OSS Distributions 	long    ppsfreq;                /* PPS frequency (scaled PPM) (ro) */
179*19c3b8c2SApple OSS Distributions 	long    jitter;                 /* PPS jitter (ns/us) (ro) */
180*19c3b8c2SApple OSS Distributions 	int     shift;                  /* interval duration (s) (shift) (ro) */
181*19c3b8c2SApple OSS Distributions 	long    stabil;                 /* PPS stability (scaled PPM) (ro) */
182*19c3b8c2SApple OSS Distributions 	long    jitcnt;                 /* jitter limit exceeded (ro) */
183*19c3b8c2SApple OSS Distributions 	long    calcnt;                 /* calibration intervals (ro) */
184*19c3b8c2SApple OSS Distributions 	long    errcnt;                 /* calibration errors (ro) */
185*19c3b8c2SApple OSS Distributions 	long    stbcnt;                 /* stability limit exceeded (ro) */
186*19c3b8c2SApple OSS Distributions };
187*19c3b8c2SApple OSS Distributions 
188*19c3b8c2SApple OSS Distributions #ifdef KERNEL
189*19c3b8c2SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
190*19c3b8c2SApple OSS Distributions #include <sys/_types/_user32_timex.h>
191*19c3b8c2SApple OSS Distributions #include <sys/_types/_user64_timex.h>
192*19c3b8c2SApple OSS Distributions #include <sys/_types/_user32_ntptimeval.h>
193*19c3b8c2SApple OSS Distributions #include <sys/_types/_user64_ntptimeval.h>
194*19c3b8c2SApple OSS Distributions #include <kern/clock.h>
195*19c3b8c2SApple OSS Distributions 
196*19c3b8c2SApple OSS Distributions int64_t ntp_get_freq(void);
197*19c3b8c2SApple OSS Distributions void    ntp_update_second(int64_t *adjustment, clock_sec_t secs);
198*19c3b8c2SApple OSS Distributions void    ntp_init(void);
199*19c3b8c2SApple OSS Distributions #endif
200*19c3b8c2SApple OSS Distributions #else /* !_KERNEL */
201*19c3b8c2SApple OSS Distributions #include <sys/cdefs.h>
202*19c3b8c2SApple OSS Distributions 
203*19c3b8c2SApple OSS Distributions __BEGIN_DECLS
204*19c3b8c2SApple OSS Distributions #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
205*19c3b8c2SApple OSS Distributions int     ntp_adjtime(struct timex *);
206*19c3b8c2SApple OSS Distributions int     ntp_gettime(struct ntptimeval *);
207*19c3b8c2SApple OSS Distributions #endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
208*19c3b8c2SApple OSS Distributions __END_DECLS
209*19c3b8c2SApple OSS Distributions #endif /* KERNEL */
210*19c3b8c2SApple OSS Distributions 
211*19c3b8c2SApple OSS Distributions 
212*19c3b8c2SApple OSS Distributions #endif /* !_SYS_TIMEX_H_ */
213