netbsd32_time.c revision 1.9 1 1.9 christos /* $NetBSD: netbsd32_time.c,v 1.9 2005/05/31 00:41:09 christos Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1998, 2001 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.3 lukem
31 1.3 lukem #include <sys/cdefs.h>
32 1.9 christos __KERNEL_RCSID(0, "$NetBSD: netbsd32_time.c,v 1.9 2005/05/31 00:41:09 christos Exp $");
33 1.1 mrg
34 1.2 mrg #if defined(_KERNEL_OPT)
35 1.1 mrg #include "opt_ntp.h"
36 1.1 mrg #endif
37 1.1 mrg
38 1.1 mrg #include <sys/param.h>
39 1.1 mrg #include <sys/systm.h>
40 1.1 mrg #include <sys/mount.h>
41 1.1 mrg #include <sys/time.h>
42 1.1 mrg #include <sys/timex.h>
43 1.1 mrg #include <sys/proc.h>
44 1.5 thorpej #include <sys/pool.h>
45 1.1 mrg #include <sys/resourcevar.h>
46 1.1 mrg
47 1.1 mrg #include <compat/netbsd32/netbsd32.h>
48 1.1 mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
49 1.1 mrg #include <compat/netbsd32/netbsd32_conv.h>
50 1.1 mrg
51 1.1 mrg #ifdef NTP
52 1.1 mrg int
53 1.5 thorpej netbsd32_ntp_gettime(l, v, retval)
54 1.5 thorpej struct lwp *l;
55 1.1 mrg void *v;
56 1.1 mrg register_t *retval;
57 1.1 mrg {
58 1.1 mrg struct netbsd32_ntp_gettime_args /* {
59 1.1 mrg syscallarg(netbsd32_ntptimevalp_t) ntvp;
60 1.1 mrg } */ *uap = v;
61 1.1 mrg struct netbsd32_ntptimeval ntv32;
62 1.1 mrg struct timeval atv;
63 1.1 mrg struct ntptimeval ntv;
64 1.1 mrg int error = 0;
65 1.1 mrg int s;
66 1.1 mrg
67 1.1 mrg /* The following are NTP variables */
68 1.1 mrg extern long time_maxerror;
69 1.1 mrg extern long time_esterror;
70 1.1 mrg extern int time_status;
71 1.1 mrg extern int time_state; /* clock state */
72 1.1 mrg extern int time_status; /* clock status bits */
73 1.1 mrg
74 1.1 mrg if (SCARG(uap, ntvp)) {
75 1.1 mrg s = splclock();
76 1.1 mrg #ifdef EXT_CLOCK
77 1.1 mrg /*
78 1.1 mrg * The microtime() external clock routine returns a
79 1.1 mrg * status code. If less than zero, we declare an error
80 1.1 mrg * in the clock status word and return the kernel
81 1.1 mrg * (software) time variable. While there are other
82 1.1 mrg * places that call microtime(), this is the only place
83 1.1 mrg * that matters from an application point of view.
84 1.1 mrg */
85 1.1 mrg if (microtime(&atv) < 0) {
86 1.1 mrg time_status |= STA_CLOCKERR;
87 1.1 mrg ntv.time = time;
88 1.1 mrg } else
89 1.1 mrg time_status &= ~STA_CLOCKERR;
90 1.1 mrg #else /* EXT_CLOCK */
91 1.1 mrg microtime(&atv);
92 1.1 mrg #endif /* EXT_CLOCK */
93 1.1 mrg ntv.time = atv;
94 1.1 mrg ntv.maxerror = time_maxerror;
95 1.1 mrg ntv.esterror = time_esterror;
96 1.1 mrg (void) splx(s);
97 1.1 mrg
98 1.1 mrg netbsd32_from_timeval(&ntv.time, &ntv32.time);
99 1.1 mrg ntv32.maxerror = (netbsd32_long)ntv.maxerror;
100 1.1 mrg ntv32.esterror = (netbsd32_long)ntv.esterror;
101 1.4 scw error = copyout((caddr_t)&ntv32,
102 1.4 scw (caddr_t)NETBSD32PTR64(SCARG(uap, ntvp)), sizeof(ntv32));
103 1.1 mrg }
104 1.1 mrg if (!error) {
105 1.1 mrg
106 1.1 mrg /*
107 1.1 mrg * Status word error decode. If any of these conditions
108 1.1 mrg * occur, an error is returned, instead of the status
109 1.1 mrg * word. Most applications will care only about the fact
110 1.1 mrg * the system clock may not be trusted, not about the
111 1.1 mrg * details.
112 1.1 mrg *
113 1.1 mrg * Hardware or software error
114 1.1 mrg */
115 1.1 mrg if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
116 1.1 mrg
117 1.1 mrg /*
118 1.1 mrg * PPS signal lost when either time or frequency
119 1.1 mrg * synchronization requested
120 1.1 mrg */
121 1.1 mrg (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
122 1.1 mrg !(time_status & STA_PPSSIGNAL)) ||
123 1.1 mrg
124 1.1 mrg /*
125 1.1 mrg * PPS jitter exceeded when time synchronization
126 1.1 mrg * requested
127 1.1 mrg */
128 1.1 mrg (time_status & STA_PPSTIME &&
129 1.1 mrg time_status & STA_PPSJITTER) ||
130 1.1 mrg
131 1.1 mrg /*
132 1.1 mrg * PPS wander exceeded or calibration error when
133 1.1 mrg * frequency synchronization requested
134 1.1 mrg */
135 1.1 mrg (time_status & STA_PPSFREQ &&
136 1.1 mrg time_status & (STA_PPSWANDER | STA_PPSERROR)))
137 1.1 mrg *retval = TIME_ERROR;
138 1.1 mrg else
139 1.1 mrg *retval = time_state;
140 1.1 mrg }
141 1.1 mrg return (error);
142 1.1 mrg }
143 1.1 mrg
144 1.1 mrg int
145 1.5 thorpej netbsd32_ntp_adjtime(l, v, retval)
146 1.5 thorpej struct lwp *l;
147 1.1 mrg void *v;
148 1.1 mrg register_t *retval;
149 1.1 mrg {
150 1.1 mrg struct netbsd32_ntp_adjtime_args /* {
151 1.1 mrg syscallarg(netbsd32_timexp_t) tp;
152 1.1 mrg } */ *uap = v;
153 1.1 mrg struct netbsd32_timex ntv32;
154 1.1 mrg struct timex ntv;
155 1.1 mrg int error = 0;
156 1.1 mrg int modes;
157 1.1 mrg int s;
158 1.5 thorpej struct proc *p = l->l_proc;
159 1.1 mrg extern long time_freq; /* frequency offset (scaled ppm) */
160 1.1 mrg extern long time_maxerror;
161 1.1 mrg extern long time_esterror;
162 1.1 mrg extern int time_state; /* clock state */
163 1.1 mrg extern int time_status; /* clock status bits */
164 1.1 mrg extern long time_constant; /* pll time constant */
165 1.1 mrg extern long time_offset; /* time offset (us) */
166 1.1 mrg extern long time_tolerance; /* frequency tolerance (scaled ppm) */
167 1.1 mrg extern long time_precision; /* clock precision (us) */
168 1.1 mrg
169 1.4 scw if ((error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
170 1.4 scw (caddr_t)&ntv32, sizeof(ntv32))))
171 1.1 mrg return (error);
172 1.1 mrg netbsd32_to_timex(&ntv32, &ntv);
173 1.1 mrg
174 1.1 mrg /*
175 1.1 mrg * Update selected clock variables - only the superuser can
176 1.1 mrg * change anything. Note that there is no error checking here on
177 1.1 mrg * the assumption the superuser should know what it is doing.
178 1.1 mrg */
179 1.1 mrg modes = ntv.modes;
180 1.1 mrg if (modes != 0 && (error = suser(p->p_ucred, &p->p_acflag)))
181 1.1 mrg return (error);
182 1.1 mrg
183 1.1 mrg s = splclock();
184 1.1 mrg if (modes & MOD_FREQUENCY)
185 1.1 mrg #ifdef PPS_SYNC
186 1.1 mrg time_freq = ntv.freq - pps_freq;
187 1.1 mrg #else /* PPS_SYNC */
188 1.1 mrg time_freq = ntv.freq;
189 1.1 mrg #endif /* PPS_SYNC */
190 1.1 mrg if (modes & MOD_MAXERROR)
191 1.1 mrg time_maxerror = ntv.maxerror;
192 1.1 mrg if (modes & MOD_ESTERROR)
193 1.1 mrg time_esterror = ntv.esterror;
194 1.1 mrg if (modes & MOD_STATUS) {
195 1.1 mrg time_status &= STA_RONLY;
196 1.1 mrg time_status |= ntv.status & ~STA_RONLY;
197 1.1 mrg }
198 1.1 mrg if (modes & MOD_TIMECONST)
199 1.1 mrg time_constant = ntv.constant;
200 1.1 mrg if (modes & MOD_OFFSET)
201 1.1 mrg hardupdate(ntv.offset);
202 1.1 mrg
203 1.1 mrg /*
204 1.1 mrg * Retrieve all clock variables
205 1.1 mrg */
206 1.1 mrg if (time_offset < 0)
207 1.1 mrg ntv.offset = -(-time_offset >> SHIFT_UPDATE);
208 1.1 mrg else
209 1.1 mrg ntv.offset = time_offset >> SHIFT_UPDATE;
210 1.1 mrg #ifdef PPS_SYNC
211 1.1 mrg ntv.freq = time_freq + pps_freq;
212 1.1 mrg #else /* PPS_SYNC */
213 1.1 mrg ntv.freq = time_freq;
214 1.1 mrg #endif /* PPS_SYNC */
215 1.1 mrg ntv.maxerror = time_maxerror;
216 1.1 mrg ntv.esterror = time_esterror;
217 1.1 mrg ntv.status = time_status;
218 1.1 mrg ntv.constant = time_constant;
219 1.1 mrg ntv.precision = time_precision;
220 1.1 mrg ntv.tolerance = time_tolerance;
221 1.1 mrg #ifdef PPS_SYNC
222 1.1 mrg ntv.shift = pps_shift;
223 1.1 mrg ntv.ppsfreq = pps_freq;
224 1.1 mrg ntv.jitter = pps_jitter >> PPS_AVG;
225 1.1 mrg ntv.stabil = pps_stabil;
226 1.1 mrg ntv.calcnt = pps_calcnt;
227 1.1 mrg ntv.errcnt = pps_errcnt;
228 1.1 mrg ntv.jitcnt = pps_jitcnt;
229 1.1 mrg ntv.stbcnt = pps_stbcnt;
230 1.1 mrg #endif /* PPS_SYNC */
231 1.1 mrg (void)splx(s);
232 1.1 mrg
233 1.1 mrg netbsd32_from_timex(&ntv, &ntv32);
234 1.4 scw error = copyout((caddr_t)&ntv32, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
235 1.1 mrg sizeof(ntv32));
236 1.1 mrg if (!error) {
237 1.1 mrg
238 1.1 mrg /*
239 1.1 mrg * Status word error decode. See comments in
240 1.1 mrg * ntp_gettime() routine.
241 1.1 mrg */
242 1.1 mrg if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||
243 1.1 mrg (time_status & (STA_PPSFREQ | STA_PPSTIME) &&
244 1.1 mrg !(time_status & STA_PPSSIGNAL)) ||
245 1.1 mrg (time_status & STA_PPSTIME &&
246 1.1 mrg time_status & STA_PPSJITTER) ||
247 1.1 mrg (time_status & STA_PPSFREQ &&
248 1.1 mrg time_status & (STA_PPSWANDER | STA_PPSERROR)))
249 1.1 mrg *retval = TIME_ERROR;
250 1.1 mrg else
251 1.1 mrg *retval = time_state;
252 1.1 mrg }
253 1.1 mrg return error;
254 1.1 mrg }
255 1.1 mrg #else
256 1.1 mrg int
257 1.5 thorpej netbsd32_ntp_gettime(l, v, retval)
258 1.5 thorpej struct lwp *l;
259 1.1 mrg void *v;
260 1.1 mrg register_t *retval;
261 1.1 mrg {
262 1.1 mrg
263 1.1 mrg return (ENOSYS);
264 1.1 mrg }
265 1.1 mrg
266 1.1 mrg int
267 1.5 thorpej netbsd32_ntp_adjtime(l, v, retval)
268 1.5 thorpej struct lwp *l;
269 1.1 mrg void *v;
270 1.1 mrg register_t *retval;
271 1.1 mrg {
272 1.1 mrg
273 1.1 mrg return (ENOSYS);
274 1.1 mrg }
275 1.1 mrg #endif
276 1.1 mrg
277 1.1 mrg int
278 1.5 thorpej netbsd32_setitimer(l, v, retval)
279 1.5 thorpej struct lwp *l;
280 1.1 mrg void *v;
281 1.1 mrg register_t *retval;
282 1.1 mrg {
283 1.1 mrg struct netbsd32_setitimer_args /* {
284 1.1 mrg syscallarg(int) which;
285 1.1 mrg syscallarg(const netbsd32_itimervalp_t) itv;
286 1.1 mrg syscallarg(netbsd32_itimervalp_t) oitv;
287 1.1 mrg } */ *uap = v;
288 1.5 thorpej struct proc *p = l->l_proc;
289 1.1 mrg struct netbsd32_itimerval s32it, *itvp;
290 1.1 mrg int which = SCARG(uap, which);
291 1.1 mrg struct netbsd32_getitimer_args getargs;
292 1.1 mrg struct itimerval aitv;
293 1.1 mrg int s, error;
294 1.5 thorpej struct ptimer *pt;
295 1.1 mrg
296 1.1 mrg if ((u_int)which > ITIMER_PROF)
297 1.1 mrg return (EINVAL);
298 1.4 scw itvp = (struct netbsd32_itimerval *)NETBSD32PTR64(SCARG(uap, itv));
299 1.1 mrg if (itvp && (error = copyin(itvp, &s32it, sizeof(s32it))))
300 1.1 mrg return (error);
301 1.1 mrg netbsd32_to_itimerval(&s32it, &aitv);
302 1.6 fvdl if (SCARG(uap, oitv) != 0) {
303 1.1 mrg SCARG(&getargs, which) = which;
304 1.1 mrg SCARG(&getargs, itv) = SCARG(uap, oitv);
305 1.5 thorpej if ((error = netbsd32_getitimer(l, &getargs, retval)) != 0)
306 1.1 mrg return (error);
307 1.1 mrg }
308 1.1 mrg if (itvp == 0)
309 1.1 mrg return (0);
310 1.1 mrg if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
311 1.1 mrg return (EINVAL);
312 1.5 thorpej
313 1.5 thorpej /* XXX there should be a way to share code with kern_time */
314 1.5 thorpej /* XXX just copied some from there */
315 1.8 perry /*
316 1.5 thorpej * Don't bother allocating data structures if the process just
317 1.5 thorpej * wants to clear the timer.
318 1.5 thorpej */
319 1.8 perry if (!timerisset(&aitv.it_value) &&
320 1.5 thorpej ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)))
321 1.5 thorpej return (0);
322 1.5 thorpej
323 1.5 thorpej if (p->p_timers == NULL)
324 1.5 thorpej timers_alloc(p);
325 1.5 thorpej if (p->p_timers->pts_timers[which] == NULL) {
326 1.5 thorpej pt = pool_get(&ptimer_pool, PR_WAITOK);
327 1.5 thorpej callout_init(&pt->pt_ch);
328 1.5 thorpej pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
329 1.5 thorpej pt->pt_overruns = 0;
330 1.5 thorpej pt->pt_proc = p;
331 1.5 thorpej pt->pt_type = which;
332 1.5 thorpej switch (which) {
333 1.5 thorpej case ITIMER_REAL:
334 1.5 thorpej pt->pt_ev.sigev_signo = SIGALRM;
335 1.5 thorpej break;
336 1.5 thorpej case ITIMER_VIRTUAL:
337 1.5 thorpej pt->pt_ev.sigev_signo = SIGVTALRM;
338 1.5 thorpej break;
339 1.5 thorpej case ITIMER_PROF:
340 1.5 thorpej pt->pt_ev.sigev_signo = SIGPROF;
341 1.5 thorpej break;
342 1.5 thorpej }
343 1.5 thorpej } else
344 1.5 thorpej pt = p->p_timers->pts_timers[which];
345 1.5 thorpej
346 1.5 thorpej pt->pt_time = aitv;
347 1.5 thorpej p->p_timers->pts_timers[which] = pt;
348 1.1 mrg if (which == ITIMER_REAL) {
349 1.5 thorpej s = splclock();
350 1.5 thorpej callout_stop(&pt->pt_ch);
351 1.5 thorpej if (timerisset(&pt->pt_time.it_value)) {
352 1.8 perry timeradd(&pt->pt_time.it_value, &time,
353 1.5 thorpej &pt->pt_time.it_value);
354 1.1 mrg /*
355 1.1 mrg * Don't need to check hzto() return value, here.
356 1.1 mrg * callout_reset() does it for us.
357 1.1 mrg */
358 1.8 perry callout_reset(&pt->pt_ch, hzto(&pt->pt_time.it_value),
359 1.5 thorpej realtimerexpire, pt);
360 1.1 mrg }
361 1.5 thorpej splx(s);
362 1.5 thorpej }
363 1.1 mrg return (0);
364 1.1 mrg }
365 1.1 mrg
366 1.1 mrg int
367 1.5 thorpej netbsd32_getitimer(l, v, retval)
368 1.5 thorpej struct lwp *l;
369 1.1 mrg void *v;
370 1.1 mrg register_t *retval;
371 1.1 mrg {
372 1.1 mrg struct netbsd32_getitimer_args /* {
373 1.1 mrg syscallarg(int) which;
374 1.1 mrg syscallarg(netbsd32_itimervalp_t) itv;
375 1.1 mrg } */ *uap = v;
376 1.5 thorpej struct proc *p = l->l_proc;
377 1.1 mrg int which = SCARG(uap, which);
378 1.1 mrg struct netbsd32_itimerval s32it;
379 1.1 mrg struct itimerval aitv;
380 1.1 mrg int s;
381 1.1 mrg
382 1.1 mrg if ((u_int)which > ITIMER_PROF)
383 1.1 mrg return (EINVAL);
384 1.5 thorpej
385 1.5 thorpej /* XXX same as setitimer */
386 1.5 thorpej if ((p->p_timers == NULL) || (p->p_timers->pts_timers[which] == NULL)) {
387 1.5 thorpej timerclear(&aitv.it_value);
388 1.5 thorpej timerclear(&aitv.it_interval);
389 1.5 thorpej } else {
390 1.5 thorpej s = splclock();
391 1.5 thorpej if (which == ITIMER_REAL) {
392 1.5 thorpej /*
393 1.5 thorpej * Convert from absolute to relative time in
394 1.5 thorpej * .it_value part of real time timer. If time
395 1.5 thorpej * for real time timer has passed return 0,
396 1.5 thorpej * else return difference between current time
397 1.8 perry * and time for the timer to go off.
398 1.5 thorpej */
399 1.5 thorpej aitv = p->p_timers->pts_timers[ITIMER_REAL]->pt_time;
400 1.5 thorpej if (timerisset(&aitv.it_value)) {
401 1.5 thorpej if (timercmp(&aitv.it_value, &time, <))
402 1.5 thorpej timerclear(&aitv.it_value);
403 1.5 thorpej else
404 1.5 thorpej timersub(&aitv.it_value, &time, &aitv.it_value);
405 1.5 thorpej }
406 1.5 thorpej } else
407 1.5 thorpej aitv = p->p_timers->pts_timers[which]->pt_time;
408 1.5 thorpej splx(s);
409 1.5 thorpej }
410 1.1 mrg netbsd32_from_itimerval(&aitv, &s32it);
411 1.4 scw return (copyout(&s32it, (caddr_t)NETBSD32PTR64(SCARG(uap, itv)),
412 1.4 scw sizeof(s32it)));
413 1.1 mrg }
414 1.1 mrg
415 1.1 mrg int
416 1.5 thorpej netbsd32_gettimeofday(l, v, retval)
417 1.5 thorpej struct lwp *l;
418 1.1 mrg void *v;
419 1.1 mrg register_t *retval;
420 1.1 mrg {
421 1.1 mrg struct netbsd32_gettimeofday_args /* {
422 1.1 mrg syscallarg(netbsd32_timevalp_t) tp;
423 1.1 mrg syscallarg(netbsd32_timezonep_t) tzp;
424 1.1 mrg } */ *uap = v;
425 1.1 mrg struct timeval atv;
426 1.1 mrg struct netbsd32_timeval tv32;
427 1.1 mrg int error = 0;
428 1.1 mrg struct netbsd32_timezone tzfake;
429 1.1 mrg
430 1.1 mrg if (SCARG(uap, tp)) {
431 1.1 mrg microtime(&atv);
432 1.1 mrg netbsd32_from_timeval(&atv, &tv32);
433 1.4 scw error = copyout(&tv32, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
434 1.4 scw sizeof(tv32));
435 1.1 mrg if (error)
436 1.1 mrg return (error);
437 1.1 mrg }
438 1.1 mrg if (SCARG(uap, tzp)) {
439 1.1 mrg /*
440 1.1 mrg * NetBSD has no kernel notion of time zone, so we just
441 1.1 mrg * fake up a timezone struct and return it if demanded.
442 1.1 mrg */
443 1.1 mrg tzfake.tz_minuteswest = 0;
444 1.1 mrg tzfake.tz_dsttime = 0;
445 1.4 scw error = copyout(&tzfake,
446 1.4 scw (caddr_t)NETBSD32PTR64(SCARG(uap, tzp)), sizeof(tzfake));
447 1.1 mrg }
448 1.1 mrg return (error);
449 1.1 mrg }
450 1.1 mrg
451 1.1 mrg int
452 1.5 thorpej netbsd32_settimeofday(l, v, retval)
453 1.5 thorpej struct lwp *l;
454 1.1 mrg void *v;
455 1.1 mrg register_t *retval;
456 1.1 mrg {
457 1.1 mrg struct netbsd32_settimeofday_args /* {
458 1.1 mrg syscallarg(const netbsd32_timevalp_t) tv;
459 1.1 mrg syscallarg(const netbsd32_timezonep_t) tzp;
460 1.1 mrg } */ *uap = v;
461 1.1 mrg struct netbsd32_timeval atv32;
462 1.1 mrg struct timeval atv;
463 1.1 mrg int error;
464 1.5 thorpej struct proc *p = l->l_proc;
465 1.1 mrg
466 1.1 mrg if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
467 1.1 mrg return (error);
468 1.1 mrg /* Verify all parameters before changing time. */
469 1.4 scw if (SCARG(uap, tv) &&
470 1.4 scw (error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tv)), &atv32,
471 1.4 scw sizeof(atv32))))
472 1.1 mrg return (error);
473 1.1 mrg netbsd32_to_timeval(&atv32, &atv);
474 1.1 mrg if (SCARG(uap, tv))
475 1.1 mrg if ((error = settime(&atv)))
476 1.1 mrg return (error);
477 1.1 mrg /* don't bother copying the tz in, we don't use it. */
478 1.1 mrg /*
479 1.1 mrg * NetBSD has no kernel notion of time zone, and only an
480 1.1 mrg * obsolete program would try to set it, so we log a warning.
481 1.1 mrg */
482 1.1 mrg if (SCARG(uap, tzp))
483 1.1 mrg printf("pid %d attempted to set the "
484 1.8 perry "(obsolete) kernel time zone\n", p->p_pid);
485 1.1 mrg return (0);
486 1.1 mrg }
487 1.1 mrg
488 1.1 mrg int
489 1.5 thorpej netbsd32_adjtime(l, v, retval)
490 1.5 thorpej struct lwp *l;
491 1.1 mrg void *v;
492 1.1 mrg register_t *retval;
493 1.1 mrg {
494 1.1 mrg struct netbsd32_adjtime_args /* {
495 1.1 mrg syscallarg(const netbsd32_timevalp_t) delta;
496 1.1 mrg syscallarg(netbsd32_timevalp_t) olddelta;
497 1.1 mrg } */ *uap = v;
498 1.1 mrg struct netbsd32_timeval atv;
499 1.1 mrg int32_t ndelta, ntickdelta, odelta;
500 1.1 mrg int s, error;
501 1.5 thorpej struct proc *p = l->l_proc;
502 1.1 mrg extern long bigadj, timedelta;
503 1.1 mrg extern int tickdelta;
504 1.1 mrg
505 1.1 mrg if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
506 1.1 mrg return (error);
507 1.1 mrg
508 1.4 scw error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, delta)), &atv,
509 1.4 scw sizeof(struct timeval));
510 1.1 mrg if (error)
511 1.1 mrg return (error);
512 1.1 mrg /*
513 1.1 mrg * Compute the total correction and the rate at which to apply it.
514 1.1 mrg * Round the adjustment down to a whole multiple of the per-tick
515 1.1 mrg * delta, so that after some number of incremental changes in
516 1.1 mrg * hardclock(), tickdelta will become zero, lest the correction
517 1.1 mrg * overshoot and start taking us away from the desired final time.
518 1.1 mrg */
519 1.1 mrg ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
520 1.1 mrg if (ndelta > bigadj)
521 1.1 mrg ntickdelta = 10 * tickadj;
522 1.1 mrg else
523 1.1 mrg ntickdelta = tickadj;
524 1.1 mrg if (ndelta % ntickdelta)
525 1.1 mrg ndelta = ndelta / ntickdelta * ntickdelta;
526 1.1 mrg
527 1.1 mrg /*
528 1.1 mrg * To make hardclock()'s job easier, make the per-tick delta negative
529 1.1 mrg * if we want time to run slower; then hardclock can simply compute
530 1.1 mrg * tick + tickdelta, and subtract tickdelta from timedelta.
531 1.1 mrg */
532 1.1 mrg if (ndelta < 0)
533 1.1 mrg ntickdelta = -ntickdelta;
534 1.1 mrg s = splclock();
535 1.1 mrg odelta = timedelta;
536 1.1 mrg timedelta = ndelta;
537 1.1 mrg tickdelta = ntickdelta;
538 1.1 mrg splx(s);
539 1.1 mrg
540 1.1 mrg if (SCARG(uap, olddelta)) {
541 1.1 mrg atv.tv_sec = odelta / 1000000;
542 1.1 mrg atv.tv_usec = odelta % 1000000;
543 1.4 scw (void) copyout(&atv,
544 1.4 scw (caddr_t)NETBSD32PTR64(SCARG(uap, olddelta)), sizeof(atv));
545 1.1 mrg }
546 1.1 mrg return (0);
547 1.1 mrg }
548 1.1 mrg
549 1.1 mrg int
550 1.5 thorpej netbsd32_clock_gettime(l, v, retval)
551 1.5 thorpej struct lwp *l;
552 1.1 mrg void *v;
553 1.1 mrg register_t *retval;
554 1.1 mrg {
555 1.1 mrg struct netbsd32_clock_gettime_args /* {
556 1.1 mrg syscallarg(netbsd32_clockid_t) clock_id;
557 1.1 mrg syscallarg(netbsd32_timespecp_t) tp;
558 1.1 mrg } */ *uap = v;
559 1.1 mrg clockid_t clock_id;
560 1.1 mrg struct timeval atv;
561 1.1 mrg struct timespec ats;
562 1.1 mrg struct netbsd32_timespec ts32;
563 1.1 mrg
564 1.1 mrg clock_id = SCARG(uap, clock_id);
565 1.1 mrg if (clock_id != CLOCK_REALTIME)
566 1.1 mrg return (EINVAL);
567 1.1 mrg
568 1.1 mrg microtime(&atv);
569 1.1 mrg TIMEVAL_TO_TIMESPEC(&atv,&ats);
570 1.1 mrg netbsd32_from_timespec(&ats, &ts32);
571 1.1 mrg
572 1.4 scw return copyout(&ts32, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
573 1.4 scw sizeof(ts32));
574 1.1 mrg }
575 1.1 mrg
576 1.1 mrg int
577 1.5 thorpej netbsd32_clock_settime(l, v, retval)
578 1.5 thorpej struct lwp *l;
579 1.1 mrg void *v;
580 1.1 mrg register_t *retval;
581 1.1 mrg {
582 1.1 mrg struct netbsd32_clock_settime_args /* {
583 1.1 mrg syscallarg(netbsd32_clockid_t) clock_id;
584 1.1 mrg syscallarg(const netbsd32_timespecp_t) tp;
585 1.1 mrg } */ *uap = v;
586 1.1 mrg struct netbsd32_timespec ts32;
587 1.1 mrg clockid_t clock_id;
588 1.1 mrg struct timeval atv;
589 1.1 mrg struct timespec ats;
590 1.1 mrg int error;
591 1.5 thorpej struct proc *p = l->l_proc;
592 1.1 mrg
593 1.1 mrg if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
594 1.1 mrg return (error);
595 1.1 mrg
596 1.1 mrg clock_id = SCARG(uap, clock_id);
597 1.1 mrg if (clock_id != CLOCK_REALTIME)
598 1.1 mrg return (EINVAL);
599 1.1 mrg
600 1.4 scw if ((error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tp)), &ts32,
601 1.4 scw sizeof(ts32))) != 0)
602 1.1 mrg return (error);
603 1.1 mrg
604 1.1 mrg netbsd32_to_timespec(&ts32, &ats);
605 1.1 mrg TIMESPEC_TO_TIMEVAL(&atv,&ats);
606 1.1 mrg if ((error = settime(&atv)))
607 1.1 mrg return (error);
608 1.1 mrg
609 1.1 mrg return 0;
610 1.1 mrg }
611 1.1 mrg
612 1.1 mrg int
613 1.5 thorpej netbsd32_clock_getres(l, v, retval)
614 1.5 thorpej struct lwp *l;
615 1.1 mrg void *v;
616 1.1 mrg register_t *retval;
617 1.1 mrg {
618 1.1 mrg struct netbsd32_clock_getres_args /* {
619 1.1 mrg syscallarg(netbsd32_clockid_t) clock_id;
620 1.1 mrg syscallarg(netbsd32_timespecp_t) tp;
621 1.1 mrg } */ *uap = v;
622 1.1 mrg struct netbsd32_timespec ts32;
623 1.1 mrg clockid_t clock_id;
624 1.1 mrg struct timespec ts;
625 1.1 mrg int error = 0;
626 1.1 mrg
627 1.1 mrg clock_id = SCARG(uap, clock_id);
628 1.1 mrg if (clock_id != CLOCK_REALTIME)
629 1.1 mrg return (EINVAL);
630 1.1 mrg
631 1.1 mrg if (SCARG(uap, tp)) {
632 1.1 mrg ts.tv_sec = 0;
633 1.1 mrg ts.tv_nsec = 1000000000 / hz;
634 1.1 mrg
635 1.1 mrg netbsd32_from_timespec(&ts, &ts32);
636 1.4 scw error = copyout(&ts, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
637 1.4 scw sizeof(ts));
638 1.1 mrg }
639 1.1 mrg
640 1.1 mrg return error;
641 1.1 mrg }
642 1.1 mrg
643 1.1 mrg int
644 1.5 thorpej netbsd32_nanosleep(l, v, retval)
645 1.5 thorpej struct lwp *l;
646 1.1 mrg void *v;
647 1.1 mrg register_t *retval;
648 1.1 mrg {
649 1.1 mrg struct netbsd32_nanosleep_args /* {
650 1.1 mrg syscallarg(const netbsd32_timespecp_t) rqtp;
651 1.1 mrg syscallarg(netbsd32_timespecp_t) rmtp;
652 1.1 mrg } */ *uap = v;
653 1.1 mrg static int nanowait;
654 1.1 mrg struct netbsd32_timespec ts32;
655 1.1 mrg struct timespec rqt;
656 1.1 mrg struct timespec rmt;
657 1.1 mrg struct timeval atv, utv;
658 1.1 mrg int error, s, timo;
659 1.1 mrg
660 1.4 scw error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, rqtp)), (caddr_t)&ts32,
661 1.4 scw sizeof(ts32));
662 1.1 mrg if (error)
663 1.1 mrg return (error);
664 1.1 mrg
665 1.1 mrg netbsd32_to_timespec(&ts32, &rqt);
666 1.7 atatat TIMESPEC_TO_TIMEVAL(&atv,&rqt);
667 1.1 mrg if (itimerfix(&atv))
668 1.1 mrg return (EINVAL);
669 1.1 mrg
670 1.1 mrg s = splclock();
671 1.1 mrg timeradd(&atv,&time,&atv);
672 1.1 mrg timo = hzto(&atv);
673 1.8 perry /*
674 1.1 mrg * Avoid inadvertantly sleeping forever
675 1.1 mrg */
676 1.1 mrg if (timo == 0)
677 1.1 mrg timo = 1;
678 1.1 mrg splx(s);
679 1.1 mrg
680 1.1 mrg error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
681 1.1 mrg if (error == ERESTART)
682 1.1 mrg error = EINTR;
683 1.1 mrg if (error == EWOULDBLOCK)
684 1.1 mrg error = 0;
685 1.1 mrg
686 1.1 mrg if (SCARG(uap, rmtp)) {
687 1.9 christos int error1;
688 1.1 mrg
689 1.1 mrg s = splclock();
690 1.1 mrg utv = time;
691 1.1 mrg splx(s);
692 1.1 mrg
693 1.1 mrg timersub(&atv, &utv, &utv);
694 1.1 mrg if (utv.tv_sec < 0)
695 1.1 mrg timerclear(&utv);
696 1.1 mrg
697 1.1 mrg TIMEVAL_TO_TIMESPEC(&utv,&rmt);
698 1.1 mrg netbsd32_from_timespec(&rmt, &ts32);
699 1.9 christos error1 = copyout(&ts32,
700 1.9 christos NETBSD32PTR64(SCARG(uap,rmtp)), sizeof(ts32));
701 1.9 christos if (error1)
702 1.9 christos return (error1);
703 1.1 mrg }
704 1.1 mrg
705 1.1 mrg return error;
706 1.1 mrg }
707