netbsd32_time.c revision 1.18.4.1 1 1.18.4.1 rpaulo /* $NetBSD: netbsd32_time.c,v 1.18.4.1 2006/09/09 02:46:12 rpaulo 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.18.4.1 rpaulo __KERNEL_RCSID(0, "$NetBSD: netbsd32_time.c,v 1.18.4.1 2006/09/09 02:46:12 rpaulo Exp $");
33 1.1 mrg
34 1.2 mrg #if defined(_KERNEL_OPT)
35 1.1 mrg #include "opt_ntp.h"
36 1.18.4.1 rpaulo #include "opt_compat_netbsd.h"
37 1.1 mrg #endif
38 1.1 mrg
39 1.1 mrg #include <sys/param.h>
40 1.1 mrg #include <sys/systm.h>
41 1.1 mrg #include <sys/mount.h>
42 1.1 mrg #include <sys/time.h>
43 1.1 mrg #include <sys/timex.h>
44 1.13 cube #include <sys/timevar.h>
45 1.18.4.1 rpaulo #include <sys/timetc.h>
46 1.1 mrg #include <sys/proc.h>
47 1.5 thorpej #include <sys/pool.h>
48 1.1 mrg #include <sys/resourcevar.h>
49 1.12 christos #include <sys/dirent.h>
50 1.18.4.1 rpaulo #include <sys/kauth.h>
51 1.1 mrg
52 1.1 mrg #include <compat/netbsd32/netbsd32.h>
53 1.1 mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
54 1.1 mrg #include <compat/netbsd32/netbsd32_conv.h>
55 1.1 mrg
56 1.1 mrg #ifdef NTP
57 1.18.4.1 rpaulo
58 1.1 mrg int
59 1.5 thorpej netbsd32_ntp_gettime(l, v, retval)
60 1.5 thorpej struct lwp *l;
61 1.1 mrg void *v;
62 1.1 mrg register_t *retval;
63 1.1 mrg {
64 1.1 mrg struct netbsd32_ntp_gettime_args /* {
65 1.1 mrg syscallarg(netbsd32_ntptimevalp_t) ntvp;
66 1.1 mrg } */ *uap = v;
67 1.1 mrg struct netbsd32_ntptimeval ntv32;
68 1.1 mrg struct ntptimeval ntv;
69 1.1 mrg int error = 0;
70 1.1 mrg
71 1.1 mrg if (SCARG(uap, ntvp)) {
72 1.18.4.1 rpaulo ntp_gettime(&ntv);
73 1.1 mrg
74 1.18.4.1 rpaulo ntv32.time.tv_sec = ntv.time.tv_sec;
75 1.18.4.1 rpaulo ntv32.time.tv_nsec = ntv.time.tv_nsec;
76 1.1 mrg ntv32.maxerror = (netbsd32_long)ntv.maxerror;
77 1.1 mrg ntv32.esterror = (netbsd32_long)ntv.esterror;
78 1.18.4.1 rpaulo ntv32.tai = (netbsd32_long)ntv.tai;
79 1.18.4.1 rpaulo ntv32.time_state = ntv.time_state;
80 1.4 scw error = copyout((caddr_t)&ntv32,
81 1.4 scw (caddr_t)NETBSD32PTR64(SCARG(uap, ntvp)), sizeof(ntv32));
82 1.1 mrg }
83 1.1 mrg if (!error) {
84 1.18.4.1 rpaulo *retval = ntp_timestatus();
85 1.18.4.1 rpaulo }
86 1.1 mrg
87 1.18.4.1 rpaulo return (error);
88 1.18.4.1 rpaulo }
89 1.1 mrg
90 1.18.4.1 rpaulo #ifdef COMPAT_30
91 1.18.4.1 rpaulo int
92 1.18.4.1 rpaulo compat_30_netbsd32_ntp_gettime(l, v, retval)
93 1.18.4.1 rpaulo struct lwp *l;
94 1.18.4.1 rpaulo void *v;
95 1.18.4.1 rpaulo register_t *retval;
96 1.18.4.1 rpaulo {
97 1.18.4.1 rpaulo struct compat_30_netbsd32_ntp_gettime_args /* {
98 1.18.4.1 rpaulo syscallarg(netbsd32_ntptimevalp_t) ntvp;
99 1.18.4.1 rpaulo } */ *uap = v;
100 1.18.4.1 rpaulo struct netbsd32_ntptimeval30 ntv32;
101 1.18.4.1 rpaulo struct ntptimeval ntv;
102 1.18.4.1 rpaulo int error = 0;
103 1.1 mrg
104 1.18.4.1 rpaulo if (SCARG(uap, ntvp)) {
105 1.18.4.1 rpaulo ntp_gettime(&ntv);
106 1.1 mrg
107 1.18.4.1 rpaulo ntv32.time.tv_sec = ntv.time.tv_sec;
108 1.18.4.1 rpaulo ntv32.time.tv_usec = ntv.time.tv_nsec / 1000;
109 1.18.4.1 rpaulo ntv32.maxerror = (netbsd32_long)ntv.maxerror;
110 1.18.4.1 rpaulo ntv32.esterror = (netbsd32_long)ntv.esterror;
111 1.18.4.1 rpaulo error = copyout((caddr_t)&ntv32,
112 1.18.4.1 rpaulo (caddr_t)NETBSD32PTR64(SCARG(uap, ntvp)), sizeof(ntv32));
113 1.18.4.1 rpaulo }
114 1.18.4.1 rpaulo if (!error) {
115 1.18.4.1 rpaulo *retval = ntp_timestatus();
116 1.1 mrg }
117 1.18.4.1 rpaulo
118 1.1 mrg return (error);
119 1.1 mrg }
120 1.18.4.1 rpaulo #endif
121 1.1 mrg
122 1.1 mrg int
123 1.5 thorpej netbsd32_ntp_adjtime(l, v, retval)
124 1.5 thorpej struct lwp *l;
125 1.1 mrg void *v;
126 1.1 mrg register_t *retval;
127 1.1 mrg {
128 1.1 mrg struct netbsd32_ntp_adjtime_args /* {
129 1.1 mrg syscallarg(netbsd32_timexp_t) tp;
130 1.1 mrg } */ *uap = v;
131 1.1 mrg struct netbsd32_timex ntv32;
132 1.1 mrg struct timex ntv;
133 1.1 mrg int error = 0;
134 1.1 mrg int modes;
135 1.1 mrg
136 1.4 scw if ((error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
137 1.4 scw (caddr_t)&ntv32, sizeof(ntv32))))
138 1.1 mrg return (error);
139 1.18.4.1 rpaulo
140 1.1 mrg netbsd32_to_timex(&ntv32, &ntv);
141 1.1 mrg
142 1.1 mrg /*
143 1.1 mrg * Update selected clock variables - only the superuser can
144 1.1 mrg * change anything. Note that there is no error checking here on
145 1.1 mrg * the assumption the superuser should know what it is doing.
146 1.1 mrg */
147 1.1 mrg modes = ntv.modes;
148 1.18.4.1 rpaulo if (modes != 0 && (error = kauth_authorize_generic(l->l_cred,
149 1.18.4.1 rpaulo KAUTH_GENERIC_ISSUSER, &l->l_acflag)))
150 1.1 mrg return (error);
151 1.1 mrg
152 1.18.4.1 rpaulo ntp_adjtime1(&ntv);
153 1.1 mrg
154 1.1 mrg netbsd32_from_timex(&ntv, &ntv32);
155 1.4 scw error = copyout((caddr_t)&ntv32, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
156 1.1 mrg sizeof(ntv32));
157 1.1 mrg if (!error) {
158 1.18.4.1 rpaulo *retval = ntp_timestatus();
159 1.1 mrg }
160 1.1 mrg return error;
161 1.1 mrg }
162 1.18.4.1 rpaulo #else /* !NTP */
163 1.1 mrg int
164 1.5 thorpej netbsd32_ntp_gettime(l, v, retval)
165 1.5 thorpej struct lwp *l;
166 1.1 mrg void *v;
167 1.1 mrg register_t *retval;
168 1.1 mrg {
169 1.1 mrg
170 1.1 mrg return (ENOSYS);
171 1.1 mrg }
172 1.1 mrg
173 1.18.4.1 rpaulo #ifdef COMPAT_30
174 1.1 mrg int
175 1.18.4.1 rpaulo compat_30_netbsd32_ntp_gettime(l, v, retval)
176 1.5 thorpej struct lwp *l;
177 1.1 mrg void *v;
178 1.1 mrg register_t *retval;
179 1.1 mrg {
180 1.1 mrg
181 1.1 mrg return (ENOSYS);
182 1.1 mrg }
183 1.1 mrg #endif
184 1.1 mrg
185 1.1 mrg int
186 1.18.4.1 rpaulo netbsd32_ntp_adjtime(l, v, retval)
187 1.18.4.1 rpaulo struct lwp *l;
188 1.18.4.1 rpaulo void *v;
189 1.18.4.1 rpaulo register_t *retval;
190 1.18.4.1 rpaulo {
191 1.18.4.1 rpaulo
192 1.18.4.1 rpaulo return (ENOSYS);
193 1.18.4.1 rpaulo }
194 1.18.4.1 rpaulo #endif /* NTP */
195 1.18.4.1 rpaulo
196 1.18.4.1 rpaulo int
197 1.5 thorpej netbsd32_setitimer(l, v, retval)
198 1.5 thorpej struct lwp *l;
199 1.1 mrg void *v;
200 1.1 mrg register_t *retval;
201 1.1 mrg {
202 1.1 mrg struct netbsd32_setitimer_args /* {
203 1.1 mrg syscallarg(int) which;
204 1.1 mrg syscallarg(const netbsd32_itimervalp_t) itv;
205 1.1 mrg syscallarg(netbsd32_itimervalp_t) oitv;
206 1.1 mrg } */ *uap = v;
207 1.5 thorpej struct proc *p = l->l_proc;
208 1.10 cube struct netbsd32_itimerval s32it, *itv32;
209 1.1 mrg int which = SCARG(uap, which);
210 1.1 mrg struct netbsd32_getitimer_args getargs;
211 1.1 mrg struct itimerval aitv;
212 1.10 cube int error;
213 1.1 mrg
214 1.1 mrg if ((u_int)which > ITIMER_PROF)
215 1.1 mrg return (EINVAL);
216 1.10 cube itv32 = (struct netbsd32_itimerval *)NETBSD32PTR64(SCARG(uap, itv));
217 1.10 cube if (itv32) {
218 1.10 cube if ((error = copyin(itv32, &s32it, sizeof(s32it))))
219 1.10 cube return (error);
220 1.10 cube netbsd32_to_itimerval(&s32it, &aitv);
221 1.10 cube }
222 1.6 fvdl if (SCARG(uap, oitv) != 0) {
223 1.1 mrg SCARG(&getargs, which) = which;
224 1.1 mrg SCARG(&getargs, itv) = SCARG(uap, oitv);
225 1.5 thorpej if ((error = netbsd32_getitimer(l, &getargs, retval)) != 0)
226 1.1 mrg return (error);
227 1.1 mrg }
228 1.10 cube if (itv32 == 0)
229 1.10 cube return 0;
230 1.5 thorpej
231 1.10 cube return dosetitimer(p, which, &aitv);
232 1.1 mrg }
233 1.1 mrg
234 1.1 mrg int
235 1.5 thorpej netbsd32_getitimer(l, v, retval)
236 1.5 thorpej struct lwp *l;
237 1.1 mrg void *v;
238 1.1 mrg register_t *retval;
239 1.1 mrg {
240 1.1 mrg struct netbsd32_getitimer_args /* {
241 1.1 mrg syscallarg(int) which;
242 1.1 mrg syscallarg(netbsd32_itimervalp_t) itv;
243 1.1 mrg } */ *uap = v;
244 1.5 thorpej struct proc *p = l->l_proc;
245 1.1 mrg struct netbsd32_itimerval s32it;
246 1.1 mrg struct itimerval aitv;
247 1.10 cube int error;
248 1.1 mrg
249 1.10 cube error = dogetitimer(p, SCARG(uap, which), &aitv);
250 1.10 cube if (error)
251 1.10 cube return error;
252 1.5 thorpej
253 1.1 mrg netbsd32_from_itimerval(&aitv, &s32it);
254 1.4 scw return (copyout(&s32it, (caddr_t)NETBSD32PTR64(SCARG(uap, itv)),
255 1.4 scw sizeof(s32it)));
256 1.1 mrg }
257 1.1 mrg
258 1.1 mrg int
259 1.5 thorpej netbsd32_gettimeofday(l, v, retval)
260 1.5 thorpej struct lwp *l;
261 1.1 mrg void *v;
262 1.1 mrg register_t *retval;
263 1.1 mrg {
264 1.1 mrg struct netbsd32_gettimeofday_args /* {
265 1.1 mrg syscallarg(netbsd32_timevalp_t) tp;
266 1.1 mrg syscallarg(netbsd32_timezonep_t) tzp;
267 1.1 mrg } */ *uap = v;
268 1.1 mrg struct timeval atv;
269 1.1 mrg struct netbsd32_timeval tv32;
270 1.1 mrg int error = 0;
271 1.1 mrg struct netbsd32_timezone tzfake;
272 1.1 mrg
273 1.1 mrg if (SCARG(uap, tp)) {
274 1.1 mrg microtime(&atv);
275 1.1 mrg netbsd32_from_timeval(&atv, &tv32);
276 1.4 scw error = copyout(&tv32, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
277 1.4 scw sizeof(tv32));
278 1.1 mrg if (error)
279 1.1 mrg return (error);
280 1.1 mrg }
281 1.1 mrg if (SCARG(uap, tzp)) {
282 1.1 mrg /*
283 1.1 mrg * NetBSD has no kernel notion of time zone, so we just
284 1.1 mrg * fake up a timezone struct and return it if demanded.
285 1.1 mrg */
286 1.1 mrg tzfake.tz_minuteswest = 0;
287 1.1 mrg tzfake.tz_dsttime = 0;
288 1.4 scw error = copyout(&tzfake,
289 1.4 scw (caddr_t)NETBSD32PTR64(SCARG(uap, tzp)), sizeof(tzfake));
290 1.1 mrg }
291 1.1 mrg return (error);
292 1.1 mrg }
293 1.1 mrg
294 1.1 mrg int
295 1.5 thorpej netbsd32_settimeofday(l, v, retval)
296 1.5 thorpej struct lwp *l;
297 1.1 mrg void *v;
298 1.1 mrg register_t *retval;
299 1.1 mrg {
300 1.1 mrg struct netbsd32_settimeofday_args /* {
301 1.1 mrg syscallarg(const netbsd32_timevalp_t) tv;
302 1.1 mrg syscallarg(const netbsd32_timezonep_t) tzp;
303 1.1 mrg } */ *uap = v;
304 1.1 mrg struct netbsd32_timeval atv32;
305 1.1 mrg struct timeval atv;
306 1.16 christos struct timespec ats;
307 1.1 mrg int error;
308 1.5 thorpej struct proc *p = l->l_proc;
309 1.1 mrg
310 1.16 christos /* Verify all parameters before changing time. */
311 1.18.4.1 rpaulo if ((error = kauth_authorize_generic(l->l_cred,
312 1.18.4.1 rpaulo KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
313 1.16 christos return error;
314 1.16 christos
315 1.1 mrg /*
316 1.1 mrg * NetBSD has no kernel notion of time zone, and only an
317 1.1 mrg * obsolete program would try to set it, so we log a warning.
318 1.1 mrg */
319 1.1 mrg if (SCARG(uap, tzp))
320 1.1 mrg printf("pid %d attempted to set the "
321 1.8 perry "(obsolete) kernel time zone\n", p->p_pid);
322 1.16 christos
323 1.18 christos if (SCARG(uap, tv) == 0)
324 1.16 christos return 0;
325 1.16 christos
326 1.16 christos if ((error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tv)), &atv32,
327 1.16 christos sizeof(atv32))) != 0)
328 1.16 christos return error;
329 1.16 christos
330 1.16 christos netbsd32_to_timeval(&atv32, &atv);
331 1.16 christos TIMEVAL_TO_TIMESPEC(&atv, &ats);
332 1.16 christos return settime(p, &ats);
333 1.1 mrg }
334 1.1 mrg
335 1.1 mrg int
336 1.5 thorpej netbsd32_adjtime(l, v, retval)
337 1.5 thorpej struct lwp *l;
338 1.1 mrg void *v;
339 1.1 mrg register_t *retval;
340 1.1 mrg {
341 1.1 mrg struct netbsd32_adjtime_args /* {
342 1.1 mrg syscallarg(const netbsd32_timevalp_t) delta;
343 1.1 mrg syscallarg(netbsd32_timevalp_t) olddelta;
344 1.1 mrg } */ *uap = v;
345 1.1 mrg struct netbsd32_timeval atv;
346 1.18.4.1 rpaulo int error;
347 1.1 mrg
348 1.18.4.1 rpaulo if ((error = kauth_authorize_generic(l->l_cred,
349 1.18.4.1 rpaulo KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
350 1.1 mrg return (error);
351 1.1 mrg
352 1.18.4.1 rpaulo #ifdef __HAVE_TIMECOUNTER
353 1.18.4.1 rpaulo {
354 1.18.4.1 rpaulo extern int time_adjusted; /* in kern_ntptime.c */
355 1.18.4.1 rpaulo extern int64_t time_adjtime; /* in kern_ntptime.c */
356 1.18.4.1 rpaulo if (SCARG(uap, olddelta)) {
357 1.18.4.1 rpaulo atv.tv_sec = time_adjtime / 1000000;
358 1.18.4.1 rpaulo atv.tv_usec = time_adjtime % 1000000;
359 1.18.4.1 rpaulo if (atv.tv_usec < 0) {
360 1.18.4.1 rpaulo atv.tv_usec += 1000000;
361 1.18.4.1 rpaulo atv.tv_sec--;
362 1.18.4.1 rpaulo }
363 1.18.4.1 rpaulo (void) copyout(&atv,
364 1.18.4.1 rpaulo (caddr_t)NETBSD32PTR64(SCARG(uap, olddelta)),
365 1.18.4.1 rpaulo sizeof(atv));
366 1.18.4.1 rpaulo if (error)
367 1.18.4.1 rpaulo return (error);
368 1.18.4.1 rpaulo }
369 1.18.4.1 rpaulo
370 1.18.4.1 rpaulo if (SCARG(uap, delta)) {
371 1.18.4.1 rpaulo error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, delta)), &atv,
372 1.18.4.1 rpaulo sizeof(struct timeval));
373 1.18.4.1 rpaulo if (error)
374 1.18.4.1 rpaulo return (error);
375 1.18.4.1 rpaulo
376 1.18.4.1 rpaulo time_adjtime = (int64_t)atv.tv_sec * 1000000 +
377 1.18.4.1 rpaulo atv.tv_usec;
378 1.18.4.1 rpaulo
379 1.18.4.1 rpaulo if (time_adjtime)
380 1.18.4.1 rpaulo /* We need to save the system time during shutdown */
381 1.18.4.1 rpaulo time_adjusted |= 1;
382 1.18.4.1 rpaulo }
383 1.18.4.1 rpaulo }
384 1.18.4.1 rpaulo #else /* !__HAVE_TIMECOUNTER */
385 1.18.4.1 rpaulo {
386 1.18.4.1 rpaulo int32_t ndelta, ntickdelta, odelta;
387 1.18.4.1 rpaulo extern long bigadj, timedelta;
388 1.18.4.1 rpaulo extern int tickdelta;
389 1.18.4.1 rpaulo int s;
390 1.18.4.1 rpaulo error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, delta)), &atv,
391 1.18.4.1 rpaulo sizeof(struct timeval));
392 1.18.4.1 rpaulo if (error)
393 1.18.4.1 rpaulo return (error);
394 1.18.4.1 rpaulo /*
395 1.18.4.1 rpaulo * Compute the total correction and the rate at which to apply it.
396 1.18.4.1 rpaulo * Round the adjustment down to a whole multiple of the per-tick
397 1.18.4.1 rpaulo * delta, so that after some number of incremental changes in
398 1.18.4.1 rpaulo * hardclock(), tickdelta will become zero, lest the correction
399 1.18.4.1 rpaulo * overshoot and start taking us away from the desired final time.
400 1.18.4.1 rpaulo */
401 1.18.4.1 rpaulo ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
402 1.18.4.1 rpaulo if (ndelta > bigadj)
403 1.18.4.1 rpaulo ntickdelta = 10 * tickadj;
404 1.18.4.1 rpaulo else
405 1.18.4.1 rpaulo ntickdelta = tickadj;
406 1.18.4.1 rpaulo if (ndelta % ntickdelta)
407 1.18.4.1 rpaulo ndelta = ndelta / ntickdelta * ntickdelta;
408 1.1 mrg
409 1.18.4.1 rpaulo /*
410 1.18.4.1 rpaulo * To make hardclock()'s job easier, make the per-tick delta negative
411 1.18.4.1 rpaulo * if we want time to run slower; then hardclock can simply compute
412 1.18.4.1 rpaulo * tick + tickdelta, and subtract tickdelta from timedelta.
413 1.18.4.1 rpaulo */
414 1.18.4.1 rpaulo if (ndelta < 0)
415 1.18.4.1 rpaulo ntickdelta = -ntickdelta;
416 1.18.4.1 rpaulo s = splclock();
417 1.18.4.1 rpaulo odelta = timedelta;
418 1.18.4.1 rpaulo timedelta = ndelta;
419 1.18.4.1 rpaulo tickdelta = ntickdelta;
420 1.18.4.1 rpaulo splx(s);
421 1.18.4.1 rpaulo
422 1.18.4.1 rpaulo if (SCARG(uap, olddelta)) {
423 1.18.4.1 rpaulo atv.tv_sec = odelta / 1000000;
424 1.18.4.1 rpaulo atv.tv_usec = odelta % 1000000;
425 1.18.4.1 rpaulo (void) copyout(&atv,
426 1.18.4.1 rpaulo (caddr_t)NETBSD32PTR64(SCARG(uap, olddelta)), sizeof(atv));
427 1.18.4.1 rpaulo }
428 1.1 mrg }
429 1.18.4.1 rpaulo #endif /* !__HAVE_TIMECOUNTER */
430 1.1 mrg return (0);
431 1.1 mrg }
432 1.1 mrg
433 1.1 mrg int
434 1.5 thorpej netbsd32_clock_gettime(l, v, retval)
435 1.5 thorpej struct lwp *l;
436 1.1 mrg void *v;
437 1.1 mrg register_t *retval;
438 1.1 mrg {
439 1.1 mrg struct netbsd32_clock_gettime_args /* {
440 1.1 mrg syscallarg(netbsd32_clockid_t) clock_id;
441 1.1 mrg syscallarg(netbsd32_timespecp_t) tp;
442 1.1 mrg } */ *uap = v;
443 1.1 mrg clockid_t clock_id;
444 1.1 mrg struct timespec ats;
445 1.1 mrg struct netbsd32_timespec ts32;
446 1.1 mrg
447 1.1 mrg clock_id = SCARG(uap, clock_id);
448 1.1 mrg if (clock_id != CLOCK_REALTIME)
449 1.1 mrg return (EINVAL);
450 1.1 mrg
451 1.14 simonb nanotime(&ats);
452 1.1 mrg netbsd32_from_timespec(&ats, &ts32);
453 1.1 mrg
454 1.4 scw return copyout(&ts32, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
455 1.4 scw sizeof(ts32));
456 1.1 mrg }
457 1.1 mrg
458 1.1 mrg int
459 1.5 thorpej netbsd32_clock_settime(l, v, retval)
460 1.5 thorpej struct lwp *l;
461 1.1 mrg void *v;
462 1.1 mrg register_t *retval;
463 1.1 mrg {
464 1.1 mrg struct netbsd32_clock_settime_args /* {
465 1.1 mrg syscallarg(netbsd32_clockid_t) clock_id;
466 1.1 mrg syscallarg(const netbsd32_timespecp_t) tp;
467 1.1 mrg } */ *uap = v;
468 1.1 mrg struct netbsd32_timespec ts32;
469 1.1 mrg clockid_t clock_id;
470 1.1 mrg struct timespec ats;
471 1.1 mrg int error;
472 1.1 mrg
473 1.18.4.1 rpaulo if ((error = kauth_authorize_generic(l->l_cred,
474 1.18.4.1 rpaulo KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
475 1.1 mrg return (error);
476 1.1 mrg
477 1.1 mrg clock_id = SCARG(uap, clock_id);
478 1.1 mrg if (clock_id != CLOCK_REALTIME)
479 1.1 mrg return (EINVAL);
480 1.1 mrg
481 1.4 scw if ((error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tp)), &ts32,
482 1.4 scw sizeof(ts32))) != 0)
483 1.1 mrg return (error);
484 1.1 mrg
485 1.1 mrg netbsd32_to_timespec(&ts32, &ats);
486 1.18.4.1 rpaulo return settime(l->l_proc, &ats);
487 1.1 mrg }
488 1.1 mrg
489 1.1 mrg int
490 1.5 thorpej netbsd32_clock_getres(l, v, retval)
491 1.5 thorpej struct lwp *l;
492 1.1 mrg void *v;
493 1.1 mrg register_t *retval;
494 1.1 mrg {
495 1.1 mrg struct netbsd32_clock_getres_args /* {
496 1.1 mrg syscallarg(netbsd32_clockid_t) clock_id;
497 1.1 mrg syscallarg(netbsd32_timespecp_t) tp;
498 1.1 mrg } */ *uap = v;
499 1.1 mrg struct netbsd32_timespec ts32;
500 1.1 mrg clockid_t clock_id;
501 1.1 mrg struct timespec ts;
502 1.1 mrg int error = 0;
503 1.1 mrg
504 1.1 mrg clock_id = SCARG(uap, clock_id);
505 1.1 mrg if (clock_id != CLOCK_REALTIME)
506 1.1 mrg return (EINVAL);
507 1.1 mrg
508 1.1 mrg if (SCARG(uap, tp)) {
509 1.1 mrg ts.tv_sec = 0;
510 1.1 mrg ts.tv_nsec = 1000000000 / hz;
511 1.1 mrg
512 1.1 mrg netbsd32_from_timespec(&ts, &ts32);
513 1.4 scw error = copyout(&ts, (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
514 1.4 scw sizeof(ts));
515 1.1 mrg }
516 1.1 mrg
517 1.1 mrg return error;
518 1.1 mrg }
519 1.1 mrg
520 1.1 mrg int
521 1.5 thorpej netbsd32_nanosleep(l, v, retval)
522 1.5 thorpej struct lwp *l;
523 1.1 mrg void *v;
524 1.1 mrg register_t *retval;
525 1.1 mrg {
526 1.1 mrg struct netbsd32_nanosleep_args /* {
527 1.1 mrg syscallarg(const netbsd32_timespecp_t) rqtp;
528 1.1 mrg syscallarg(netbsd32_timespecp_t) rmtp;
529 1.1 mrg } */ *uap = v;
530 1.1 mrg static int nanowait;
531 1.1 mrg struct netbsd32_timespec ts32;
532 1.1 mrg struct timespec rqt;
533 1.1 mrg struct timespec rmt;
534 1.18.4.1 rpaulo struct timeval atv, utv, ctime;
535 1.18.4.1 rpaulo int error, timo;
536 1.1 mrg
537 1.4 scw error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, rqtp)), (caddr_t)&ts32,
538 1.4 scw sizeof(ts32));
539 1.1 mrg if (error)
540 1.1 mrg return (error);
541 1.1 mrg
542 1.1 mrg netbsd32_to_timespec(&ts32, &rqt);
543 1.7 atatat TIMESPEC_TO_TIMEVAL(&atv,&rqt);
544 1.1 mrg if (itimerfix(&atv))
545 1.1 mrg return (EINVAL);
546 1.1 mrg
547 1.18.4.1 rpaulo getmicrotime(&ctime);
548 1.18.4.1 rpaulo timeradd(&atv,&ctime,&atv);
549 1.1 mrg timo = hzto(&atv);
550 1.8 perry /*
551 1.1 mrg * Avoid inadvertantly sleeping forever
552 1.1 mrg */
553 1.1 mrg if (timo == 0)
554 1.1 mrg timo = 1;
555 1.1 mrg
556 1.1 mrg error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
557 1.1 mrg if (error == ERESTART)
558 1.1 mrg error = EINTR;
559 1.1 mrg if (error == EWOULDBLOCK)
560 1.1 mrg error = 0;
561 1.1 mrg
562 1.1 mrg if (SCARG(uap, rmtp)) {
563 1.9 christos int error1;
564 1.1 mrg
565 1.18.4.1 rpaulo getmicrotime(&utv);
566 1.1 mrg
567 1.1 mrg timersub(&atv, &utv, &utv);
568 1.1 mrg if (utv.tv_sec < 0)
569 1.1 mrg timerclear(&utv);
570 1.1 mrg
571 1.1 mrg TIMEVAL_TO_TIMESPEC(&utv,&rmt);
572 1.1 mrg netbsd32_from_timespec(&rmt, &ts32);
573 1.9 christos error1 = copyout(&ts32,
574 1.9 christos NETBSD32PTR64(SCARG(uap,rmtp)), sizeof(ts32));
575 1.9 christos if (error1)
576 1.9 christos return (error1);
577 1.1 mrg }
578 1.1 mrg
579 1.1 mrg return error;
580 1.1 mrg }
581 1.11 cube
582 1.11 cube static int
583 1.11 cube netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
584 1.11 cube {
585 1.11 cube struct sigevent *evp = dst;
586 1.11 cube struct netbsd32_sigevent ev32;
587 1.11 cube int error;
588 1.11 cube
589 1.11 cube error = copyin(src, &ev32, sizeof(ev32));
590 1.11 cube if (error)
591 1.11 cube return error;
592 1.11 cube
593 1.11 cube netbsd32_to_sigevent(&ev32, evp);
594 1.11 cube return 0;
595 1.11 cube }
596 1.11 cube
597 1.11 cube int
598 1.11 cube netbsd32_timer_create(struct lwp *l, void *v, register_t *retval)
599 1.11 cube {
600 1.11 cube struct netbsd32_timer_create_args /* {
601 1.11 cube syscallarg(netbsd32_clockid_t) clock_id;
602 1.11 cube syscallarg(netbsd32_sigeventp_t) evp;
603 1.11 cube syscallarg(netbsd32_timerp_t) timerid;
604 1.11 cube } */ *uap = v;
605 1.11 cube
606 1.11 cube return timer_create1(NETBSD32PTR64(SCARG(uap, timerid)),
607 1.11 cube SCARG(uap, clock_id), NETBSD32PTR64(SCARG(uap, evp)),
608 1.18.4.1 rpaulo netbsd32_timer_create_fetch, l);
609 1.11 cube }
610 1.11 cube
611 1.11 cube int
612 1.11 cube netbsd32_timer_delete(struct lwp *l, void *v, register_t *retval)
613 1.11 cube {
614 1.11 cube struct netbsd32_timer_delete_args /* {
615 1.11 cube syscallarg(netbsd32_timer_t) timerid;
616 1.11 cube } */ *uap = v;
617 1.11 cube struct sys_timer_delete_args ua;
618 1.11 cube
619 1.11 cube NETBSD32TO64_UAP(timerid);
620 1.11 cube return sys_timer_delete(l, (void *)&ua, retval);
621 1.11 cube }
622 1.11 cube
623 1.11 cube int
624 1.11 cube netbsd32_timer_settime(struct lwp *l, void *v, register_t *retval)
625 1.11 cube {
626 1.11 cube struct netbsd32_timer_settime_args /* {
627 1.11 cube syscallarg(netbsd32_timer_t) timerid;
628 1.11 cube syscallarg(int) flags;
629 1.11 cube syscallarg(const netbsd32_itimerspecp_t) value;
630 1.11 cube syscallarg(netbsd32_itimerspecp_t) ovalue;
631 1.11 cube } */ *uap = v;
632 1.11 cube int error;
633 1.11 cube struct itimerspec value, ovalue, *ovp = NULL;
634 1.11 cube struct netbsd32_itimerspec its32;
635 1.11 cube
636 1.11 cube if ((error = copyin(NETBSD32PTR64(SCARG(uap, value)), &its32,
637 1.11 cube sizeof(its32))) != 0)
638 1.11 cube return (error);
639 1.11 cube netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
640 1.11 cube netbsd32_to_timespec(&its32.it_value, &value.it_value);
641 1.11 cube
642 1.11 cube if (SCARG(uap, ovalue))
643 1.11 cube ovp = &ovalue;
644 1.11 cube
645 1.11 cube if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
646 1.11 cube SCARG(uap, flags), l->l_proc)) != 0)
647 1.11 cube return error;
648 1.11 cube
649 1.11 cube if (ovp) {
650 1.11 cube netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
651 1.11 cube netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
652 1.11 cube return copyout(&its32, NETBSD32PTR64(SCARG(uap, ovalue)),
653 1.11 cube sizeof(its32));
654 1.11 cube }
655 1.11 cube return 0;
656 1.11 cube }
657 1.11 cube
658 1.11 cube int
659 1.11 cube netbsd32_timer_gettime(struct lwp *l, void *v, register_t *retval)
660 1.11 cube {
661 1.11 cube struct netbsd32_timer_gettime_args /* {
662 1.11 cube syscallarg(netbsd32_timer_t) timerid;
663 1.11 cube syscallarg(netbsd32_itimerspecp_t) value;
664 1.11 cube } */ *uap = v;
665 1.11 cube int error;
666 1.11 cube struct itimerspec its;
667 1.11 cube struct netbsd32_itimerspec its32;
668 1.11 cube
669 1.11 cube if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
670 1.11 cube &its)) != 0)
671 1.11 cube return error;
672 1.11 cube
673 1.11 cube netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
674 1.11 cube netbsd32_from_timespec(&its.it_value, &its32.it_value);
675 1.11 cube
676 1.11 cube return copyout(&its32, (caddr_t)NETBSD32PTR64(SCARG(uap, value)),
677 1.11 cube sizeof(its32));
678 1.11 cube }
679 1.11 cube
680 1.11 cube int
681 1.11 cube netbsd32_timer_getoverrun(struct lwp *l, void *v, register_t *retval)
682 1.11 cube {
683 1.11 cube struct netbsd32_timer_getoverrun_args /* {
684 1.11 cube syscallarg(netbsd32_timer_t) timerid;
685 1.11 cube } */ *uap = v;
686 1.11 cube struct sys_timer_getoverrun_args ua;
687 1.11 cube
688 1.11 cube NETBSD32TO64_UAP(timerid);
689 1.11 cube return sys_timer_getoverrun(l, (void *)&ua, retval);
690 1.11 cube }
691