xref: /netbsd-src/sys/compat/netbsd32/netbsd32_time.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: netbsd32_time.c,v 1.55 2021/02/18 12:54:03 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_time.c,v 1.55 2021/02/18 12:54:03 simonb Exp $");
31 
32 #if defined(_KERNEL_OPT)
33 #include "opt_ntp.h"
34 #include "opt_compat_netbsd.h"
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/time.h>
41 #include <sys/timex.h>
42 #include <sys/timevar.h>
43 #include <sys/proc.h>
44 #include <sys/pool.h>
45 #include <sys/resourcevar.h>
46 #include <sys/dirent.h>
47 #include <sys/kauth.h>
48 #include <sys/compat_stub.h>
49 
50 #include <compat/netbsd32/netbsd32.h>
51 #include <compat/netbsd32/netbsd32_syscallargs.h>
52 #include <compat/netbsd32/netbsd32_conv.h>
53 
54 #ifdef NTP
55 
56 int
57 netbsd32___ntp_gettime50(struct lwp *l,
58     const struct netbsd32___ntp_gettime50_args *uap, register_t *retval)
59 {
60 	/* {
61 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
62 	} */
63 	struct netbsd32_ntptimeval ntv32;
64 	struct ntptimeval ntv;
65 	int error = 0;
66 
67 	if (vec_ntp_gettime == NULL)
68 		return EINVAL;
69 
70 	if (SCARG_P32(uap, ntvp)) {
71 		(*vec_ntp_gettime)(&ntv);
72 
73 		memset(&ntv32, 0, sizeof(ntv32));
74 		ntv32.time.tv_sec = ntv.time.tv_sec;
75 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
76 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
77 		ntv32.esterror = (netbsd32_long)ntv.esterror;
78 		ntv32.tai = (netbsd32_long)ntv.tai;
79 		ntv32.time_state = ntv.time_state;
80 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
81 	}
82 	if (!error) {
83 		*retval = (*vec_ntp_timestatus)();
84 	}
85 
86 	return error;
87 }
88 
89 int
90 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval)
91 {
92 	/* {
93 		syscallarg(netbsd32_timexp_t) tp;
94 	} */
95 	struct netbsd32_timex ntv32;
96 	struct timex ntv;
97 	int error = 0;
98 	int modes;
99 
100 	if (vec_ntp_adjtime1 == NULL)
101 		return EINVAL;
102 
103 	if ((error = copyin(SCARG_P32(uap, tp), &ntv32, sizeof(ntv32))))
104 		return error;
105 
106 	netbsd32_to_timex(&ntv32, &ntv);
107 
108 	/*
109 	 * Update selected clock variables - only the superuser can
110 	 * change anything. Note that there is no error checking here on
111 	 * the assumption the superuser should know what it is doing.
112 	 */
113 	modes = ntv.modes;
114 	if (modes != 0 && (error = kauth_authorize_system(l->l_cred,
115 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL, NULL,
116 	    NULL)))
117 		return error;
118 
119 	(*vec_ntp_adjtime1)(&ntv);
120 
121 	netbsd32_from_timex(&ntv, &ntv32);
122 	error = copyout(&ntv32, SCARG_P32(uap, tp), sizeof(ntv32));
123 	if (!error) {
124 		*retval = (*vec_ntp_timestatus)();
125 	}
126 	return error;
127 }
128 #endif /* NTP */
129 
130 int
131 netbsd32___setitimer50(struct lwp *l, const struct netbsd32___setitimer50_args *uap, register_t *retval)
132 {
133 	/* {
134 		syscallarg(int) which;
135 		syscallarg(const netbsd32_itimervalp_t) itv;
136 		syscallarg(netbsd32_itimervalp_t) oitv;
137 	} */
138 	struct proc *p = l->l_proc;
139 	struct netbsd32_itimerval s32it, *itv32;
140 	int which = SCARG(uap, which);
141 	struct netbsd32___getitimer50_args getargs;
142 	struct itimerval aitv;
143 	int error;
144 
145 	itv32 = SCARG_P32(uap, itv);
146 	if (itv32) {
147 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
148 			return error;
149 		netbsd32_to_itimerval(&s32it, &aitv);
150 	}
151 	if (SCARG_P32(uap, oitv) != 0) {
152 		SCARG(&getargs, which) = which;
153 		SCARG(&getargs, itv) = SCARG(uap, oitv);
154 		if ((error = netbsd32___getitimer50(l, &getargs, retval)) != 0)
155 			return error;
156 	}
157 	if (itv32 == 0)
158 		return 0;
159 
160 	return dosetitimer(p, which, &aitv);
161 }
162 
163 int
164 netbsd32___getitimer50(struct lwp *l, const struct netbsd32___getitimer50_args *uap, register_t *retval)
165 {
166 	/* {
167 		syscallarg(int) which;
168 		syscallarg(netbsd32_itimervalp_t) itv;
169 	} */
170 	struct proc *p = l->l_proc;
171 	struct netbsd32_itimerval s32it;
172 	struct itimerval aitv;
173 	int error;
174 
175 	error = dogetitimer(p, SCARG(uap, which), &aitv);
176 	if (error)
177 		return error;
178 
179 	netbsd32_from_itimerval(&aitv, &s32it);
180 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
181 }
182 
183 int
184 netbsd32___gettimeofday50(struct lwp *l, const struct netbsd32___gettimeofday50_args *uap, register_t *retval)
185 {
186 	/* {
187 		syscallarg(netbsd32_timevalp_t) tp;
188 		syscallarg(netbsd32_timezonep_t) tzp;
189 	} */
190 	struct timeval atv;
191 	struct netbsd32_timeval tv32;
192 	int error = 0;
193 	struct netbsd32_timezone tzfake;
194 
195 	if (SCARG_P32(uap, tp)) {
196 		microtime(&atv);
197 		netbsd32_from_timeval(&atv, &tv32);
198 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
199 		if (error)
200 			return error;
201 	}
202 	if (SCARG_P32(uap, tzp)) {
203 		/*
204 		 * NetBSD has no kernel notion of time zone, so we just
205 		 * fake up a timezone struct and return it if demanded.
206 		 */
207 		tzfake.tz_minuteswest = 0;
208 		tzfake.tz_dsttime = 0;
209 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
210 	}
211 	return error;
212 }
213 
214 int
215 netbsd32___settimeofday50(struct lwp *l, const struct netbsd32___settimeofday50_args *uap, register_t *retval)
216 {
217 	/* {
218 		syscallarg(const netbsd32_timevalp_t) tv;
219 		syscallarg(const netbsd32_timezonep_t) tzp;
220 	} */
221 	struct netbsd32_timeval atv32;
222 	struct timeval atv;
223 	struct timespec ats;
224 	int error;
225 	struct proc *p = l->l_proc;
226 
227 	/* Verify all parameters before changing time. */
228 
229 	/*
230 	 * NetBSD has no kernel notion of time zone, and only an
231 	 * obsolete program would try to set it, so we log a warning.
232 	 */
233 	if (SCARG_P32(uap, tzp))
234 		printf("pid %d attempted to set the "
235 		    "(obsolete) kernel time zone\n", p->p_pid);
236 
237 	if (SCARG_P32(uap, tv) == 0)
238 		return 0;
239 
240 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
241 		return error;
242 
243 	netbsd32_to_timeval(&atv32, &atv);
244 
245 	if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
246 		return EINVAL;
247 
248 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
249 	return settime(p, &ats);
250 }
251 
252 int
253 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval)
254 {
255 	/* {
256 		syscallarg(const netbsd32_timevalp_t) delta;
257 		syscallarg(netbsd32_timevalp_t) olddelta;
258 	} */
259 	struct netbsd32_timeval atv;
260 	int error;
261 
262 	extern int time_adjusted;     /* in kern_ntptime.c */
263 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
264 
265 	if ((error = kauth_authorize_system(l->l_cred,
266 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
267 	    NULL)) != 0)
268 		return error;
269 
270 	if (SCARG_P32(uap, olddelta)) {
271 		atv.tv_sec = time_adjtime / 1000000;
272 		atv.tv_usec = time_adjtime % 1000000;
273 		if (atv.tv_usec < 0) {
274 			atv.tv_usec += 1000000;
275 			atv.tv_sec--;
276 		}
277 		error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv));
278 		if (error)
279 			return error;
280 	}
281 
282 	if (SCARG_P32(uap, delta)) {
283 		error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv));
284 		if (error)
285 			return error;
286 
287 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
288 
289 		if (time_adjtime)
290 			/* We need to save the system time during shutdown */
291 			time_adjusted |= 1;
292 	}
293 
294 	return 0;
295 }
296 
297 int
298 netbsd32___clock_gettime50(struct lwp *l, const struct netbsd32___clock_gettime50_args *uap, register_t *retval)
299 {
300 	/* {
301 		syscallarg(netbsd32_clockid_t) clock_id;
302 		syscallarg(netbsd32_timespecp_t) tp;
303 	} */
304 	int error;
305 	struct timespec ats;
306 	struct netbsd32_timespec ts32;
307 
308 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
309 	if (error != 0)
310 		return error;
311 
312 	netbsd32_from_timespec(&ats, &ts32);
313 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
314 }
315 
316 int
317 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval)
318 {
319 	/* {
320 		syscallarg(netbsd32_clockid_t) clock_id;
321 		syscallarg(const netbsd32_timespecp_t) tp;
322 	} */
323 	struct netbsd32_timespec ts32;
324 	struct timespec ats;
325 	int error;
326 
327 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
328 		return error;
329 
330 	netbsd32_to_timespec(&ts32, &ats);
331 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
332 }
333 
334 int
335 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval)
336 {
337 	/* {
338 		syscallarg(netbsd32_clockid_t) clock_id;
339 		syscallarg(netbsd32_timespecp_t) tp;
340 	} */
341 	struct netbsd32_timespec ts32;
342 	struct timespec ts;
343 	int error = 0;
344 
345 	error = clock_getres1(SCARG(uap, clock_id), &ts);
346 	if (error != 0)
347 		return error;
348 
349 	if (SCARG_P32(uap, tp)) {
350 		netbsd32_from_timespec(&ts, &ts32);
351 		error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
352 	}
353 
354 	return error;
355 }
356 
357 int
358 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval)
359 {
360 	/* {
361 		syscallarg(const netbsd32_timespecp_t) rqtp;
362 		syscallarg(netbsd32_timespecp_t) rmtp;
363 	} */
364 	struct netbsd32_timespec ts32;
365 	struct timespec rqt, rmt;
366 	int error, error1;
367 
368 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
369 	if (error)
370 		return error;
371 	netbsd32_to_timespec(&ts32, &rqt);
372 
373 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
374 	    SCARG_P32(uap, rmtp) ? &rmt : NULL);
375 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
376 		return error;
377 
378 	netbsd32_from_timespec(&rmt, &ts32);
379 	error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32));
380 	return error1 ? error1 : error;
381 }
382 
383 int
384 netbsd32_clock_nanosleep(struct lwp *l, const struct netbsd32_clock_nanosleep_args *uap, register_t *retval)
385 {
386 	/* {
387 		syscallarg(clockid_t) clock_id;
388 		syscallarg(int) flags;
389 		syscallarg(const netbsd32_timespecp_t) rqtp;
390 		syscallarg(netbsd32_timespecp_t) rmtp;
391 	} */
392 	struct netbsd32_timespec ts32;
393 	struct timespec rqt, rmt;
394 	int error, error1;
395 
396 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
397 	if (error)
398 		goto out;
399 	netbsd32_to_timespec(&ts32, &rqt);
400 
401 	error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags),
402 	    &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL);
403 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
404 		goto out;
405 
406 	netbsd32_from_timespec(&rmt, &ts32);
407 	if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 &&
408 	    (error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32))) != 0)
409 		error = error1;
410 out:
411 	*retval = error;
412 	return 0;
413 }
414 
415 static int
416 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
417 {
418 	struct sigevent *evp = dst;
419 	struct netbsd32_sigevent ev32;
420 	int error;
421 
422 	error = copyin(src, &ev32, sizeof(ev32));
423 	if (error)
424 		return error;
425 
426 	netbsd32_to_sigevent(&ev32, evp);
427 	return 0;
428 }
429 
430 int
431 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval)
432 {
433 	/* {
434 		syscallarg(netbsd32_clockid_t) clock_id;
435 		syscallarg(netbsd32_sigeventp_t) evp;
436 		syscallarg(netbsd32_timerp_t) timerid;
437 	} */
438 
439 	return timer_create1(SCARG_P32(uap, timerid),
440 	    SCARG(uap, clock_id), SCARG_P32(uap, evp),
441 	    netbsd32_timer_create_fetch, l);
442 }
443 
444 int
445 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval)
446 {
447 	/* {
448 		syscallarg(netbsd32_timer_t) timerid;
449 	} */
450 	struct sys_timer_delete_args ua;
451 
452 	NETBSD32TO64_UAP(timerid);
453 	return sys_timer_delete(l, (void *)&ua, retval);
454 }
455 
456 int
457 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval)
458 {
459 	/* {
460 		syscallarg(netbsd32_timer_t) timerid;
461 		syscallarg(int) flags;
462 		syscallarg(const netbsd32_itimerspecp_t) value;
463 		syscallarg(netbsd32_itimerspecp_t) ovalue;
464 	} */
465 	int error;
466 	struct itimerspec value, ovalue, *ovp = NULL;
467 	struct netbsd32_itimerspec its32;
468 
469 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
470 		return error;
471 	netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
472 	netbsd32_to_timespec(&its32.it_value, &value.it_value);
473 
474 	if (SCARG_P32(uap, ovalue))
475 		ovp = &ovalue;
476 
477 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
478 	    SCARG(uap, flags), l->l_proc)) != 0)
479 		return error;
480 
481 	if (ovp) {
482 		netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
483 		netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
484 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
485 	}
486 	return 0;
487 }
488 
489 int
490 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval)
491 {
492 	/* {
493 		syscallarg(netbsd32_timer_t) timerid;
494 		syscallarg(netbsd32_itimerspecp_t) value;
495 	} */
496 	int error;
497 	struct itimerspec its;
498 	struct netbsd32_itimerspec its32;
499 
500 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
501 	    &its)) != 0)
502 		return error;
503 
504 	netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
505 	netbsd32_from_timespec(&its.it_value, &its32.it_value);
506 
507 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
508 }
509 
510 int
511 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval)
512 {
513 	/* {
514 		syscallarg(netbsd32_timer_t) timerid;
515 	} */
516 	struct sys_timer_getoverrun_args ua;
517 
518 	NETBSD32TO64_UAP(timerid);
519 	return sys_timer_getoverrun(l, (void *)&ua, retval);
520 }
521 
522 int
523 netbsd32_clock_getcpuclockid2(struct lwp *l,
524     const struct netbsd32_clock_getcpuclockid2_args *uap,
525     register_t *retval)
526 {
527 	/* {
528 		syscallarg(idtype_t) idtype;
529 		syscallarg(id_t) id;
530 		syscallarg(netbsd32_clockidp_t) clock_id;
531 	} */
532 	pid_t pid;
533 	lwpid_t lid;
534 	clockid_t clock_id;
535 	id_t id = SCARG(uap, id);
536 
537 	switch (SCARG(uap, idtype)) {
538 	case P_PID:
539 		pid = id == 0 ? l->l_proc->p_pid : (pid_t)id;
540 		clock_id = CLOCK_PROCESS_CPUTIME_ID | pid;
541 		break;
542 	case P_LWPID:
543 		lid = id == 0 ? l->l_lid : (lwpid_t)id;
544 		clock_id = CLOCK_THREAD_CPUTIME_ID | lid;
545 		break;
546 	default:
547 		return EINVAL;
548 	}
549 	return copyout(&clock_id, SCARG_P32(uap, clock_id), sizeof(clock_id));
550 }
551