1 /* $NetBSD: netbsd32_time.c,v 1.59 2026/09/20 13:45:00 riastradh 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.59 2026/09/20 13:45:00 riastradh 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/timerfd.h> 42 #include <sys/timex.h> 43 #include <sys/timevar.h> 44 #include <sys/proc.h> 45 #include <sys/pool.h> 46 #include <sys/resourcevar.h> 47 #include <sys/dirent.h> 48 #include <sys/kauth.h> 49 #include <sys/compat_stub.h> 50 51 #include <compat/netbsd32/netbsd32.h> 52 #include <compat/netbsd32/netbsd32_syscallargs.h> 53 #include <compat/netbsd32/netbsd32_conv.h> 54 55 #ifdef NTP 56 57 int 58 netbsd32___ntp_gettime50(struct lwp *l, 59 const struct netbsd32___ntp_gettime50_args *uap, register_t *retval) 60 { 61 /* { 62 syscallarg(netbsd32_ntptimevalp_t) ntvp; 63 } */ 64 struct netbsd32_ntptimeval ntv32; 65 struct ntptimeval ntv; 66 int error = 0; 67 68 if (vec_ntp_gettime == NULL) 69 return EINVAL; 70 71 if (SCARG_P32(uap, ntvp)) { 72 (*vec_ntp_gettime)(&ntv); 73 74 memset(&ntv32, 0, sizeof(ntv32)); 75 ntv32.time.tv_sec = ntv.time.tv_sec; 76 ntv32.time.tv_nsec = ntv.time.tv_nsec; 77 ntv32.maxerror = (netbsd32_long)ntv.maxerror; 78 ntv32.esterror = (netbsd32_long)ntv.esterror; 79 ntv32.tai = (netbsd32_long)ntv.tai; 80 ntv32.time_state = ntv.time_state; 81 error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32)); 82 } 83 if (!error) { 84 *retval = (*vec_ntp_timestatus)(); 85 } 86 87 return error; 88 } 89 90 int 91 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval) 92 { 93 /* { 94 syscallarg(netbsd32_timexp_t) tp; 95 } */ 96 struct netbsd32_timex ntv32; 97 struct timex ntv; 98 int error = 0; 99 int modes; 100 101 if (vec_ntp_adjtime1 == NULL) 102 return EINVAL; 103 104 if ((error = copyin(SCARG_P32(uap, tp), &ntv32, sizeof(ntv32)))) 105 return error; 106 107 netbsd32_to_timex(&ntv32, &ntv); 108 109 /* 110 * Update selected clock variables - only the superuser can 111 * change anything. Note that there is no error checking here on 112 * the assumption the superuser should know what it is doing. 113 */ 114 modes = ntv.modes; 115 if (modes != 0 && (error = kauth_authorize_system(l->l_cred, 116 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL, NULL, 117 NULL))) 118 return error; 119 120 (*vec_ntp_adjtime1)(&ntv); 121 122 netbsd32_from_timex(&ntv, &ntv32); 123 error = copyout(&ntv32, SCARG_P32(uap, tp), sizeof(ntv32)); 124 if (!error) { 125 *retval = (*vec_ntp_timestatus)(); 126 } 127 return error; 128 } 129 #endif /* NTP */ 130 131 int 132 netbsd32___setitimer50(struct lwp *l, const struct netbsd32___setitimer50_args *uap, register_t *retval) 133 { 134 /* { 135 syscallarg(int) which; 136 syscallarg(const netbsd32_itimervalp_t) itv; 137 syscallarg(netbsd32_itimervalp_t) oitv; 138 } */ 139 struct proc *p = l->l_proc; 140 struct netbsd32_itimerval s32it, *itv32; 141 int which = SCARG(uap, which); 142 struct netbsd32___getitimer50_args getargs; 143 struct itimerval aitv; 144 int error; 145 146 itv32 = SCARG_P32(uap, itv); 147 if (itv32) { 148 if ((error = copyin(itv32, &s32it, sizeof(s32it)))) 149 return error; 150 netbsd32_to_itimerval(&s32it, &aitv); 151 } 152 if (SCARG_P32(uap, oitv) != 0) { 153 memset(&getargs, 0, sizeof(getargs)); 154 SCARG(&getargs, which) = which; 155 SCARG(&getargs, itv) = SCARG(uap, oitv); 156 if ((error = netbsd32___getitimer50(l, &getargs, retval)) != 0) 157 return error; 158 } 159 if (itv32 == 0) 160 return 0; 161 162 return dosetitimer(p, which, &aitv); 163 } 164 165 int 166 netbsd32___getitimer50(struct lwp *l, const struct netbsd32___getitimer50_args *uap, register_t *retval) 167 { 168 /* { 169 syscallarg(int) which; 170 syscallarg(netbsd32_itimervalp_t) itv; 171 } */ 172 struct proc *p = l->l_proc; 173 struct netbsd32_itimerval s32it; 174 struct itimerval aitv; 175 int error; 176 177 error = dogetitimer(p, SCARG(uap, which), &aitv); 178 if (error) 179 return error; 180 181 netbsd32_from_itimerval(&aitv, &s32it); 182 return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it)); 183 } 184 185 int 186 netbsd32___gettimeofday50(struct lwp *l, const struct netbsd32___gettimeofday50_args *uap, register_t *retval) 187 { 188 /* { 189 syscallarg(netbsd32_timevalp_t) tp; 190 syscallarg(netbsd32_timezonep_t) tzp; 191 } */ 192 struct timeval atv; 193 struct netbsd32_timeval tv32; 194 int error = 0; 195 struct netbsd32_timezone tzfake; 196 197 if (SCARG_P32(uap, tp)) { 198 microtime(&atv); 199 netbsd32_from_timeval(&atv, &tv32); 200 error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32)); 201 if (error) 202 return error; 203 } 204 if (SCARG_P32(uap, tzp)) { 205 /* 206 * NetBSD has no kernel notion of time zone, so we just 207 * fake up a timezone struct and return it if demanded. 208 */ 209 memset(&tzfake, 0, sizeof(tzfake)); 210 tzfake.tz_minuteswest = 0; 211 tzfake.tz_dsttime = 0; 212 error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake)); 213 } 214 return error; 215 } 216 217 int 218 netbsd32___settimeofday50(struct lwp *l, const struct netbsd32___settimeofday50_args *uap, register_t *retval) 219 { 220 /* { 221 syscallarg(const netbsd32_timevalp_t) tv; 222 syscallarg(const netbsd32_timezonep_t) tzp; 223 } */ 224 struct netbsd32_timeval atv32; 225 struct timeval atv; 226 struct timespec ats; 227 int error; 228 struct proc *p = l->l_proc; 229 230 /* Verify all parameters before changing time. */ 231 232 /* 233 * NetBSD has no kernel notion of time zone, and only an 234 * obsolete program would try to set it, so we log a warning. 235 */ 236 if (SCARG_P32(uap, tzp)) 237 printf("pid %d attempted to set the " 238 "(obsolete) kernel time zone\n", p->p_pid); 239 240 if (SCARG_P32(uap, tv) == 0) 241 return 0; 242 243 if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0) 244 return error; 245 246 netbsd32_to_timeval(&atv32, &atv); 247 248 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) 249 return EINVAL; 250 251 TIMEVAL_TO_TIMESPEC(&atv, &ats); 252 return settime(p, &ats); 253 } 254 255 int 256 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval) 257 { 258 /* { 259 syscallarg(const netbsd32_timevalp_t) delta; 260 syscallarg(netbsd32_timevalp_t) olddelta; 261 } */ 262 struct netbsd32_timeval atv; 263 int error; 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 memset(&atv, 0, sizeof(atv)); 272 atv.tv_sec = time_adjtime / 1000000; 273 atv.tv_usec = time_adjtime % 1000000; 274 if (atv.tv_usec < 0) { 275 atv.tv_usec += 1000000; 276 atv.tv_sec--; 277 } 278 error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv)); 279 if (error) 280 return error; 281 } 282 283 if (SCARG_P32(uap, delta)) { 284 error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv)); 285 if (error) 286 return error; 287 288 time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec; 289 290 if (time_adjtime) 291 /* We need to save the system time during shutdown */ 292 time_adjusted |= 1; 293 } 294 295 return 0; 296 } 297 298 int 299 netbsd32___clock_gettime50(struct lwp *l, const struct netbsd32___clock_gettime50_args *uap, register_t *retval) 300 { 301 /* { 302 syscallarg(netbsd32_clockid_t) clock_id; 303 syscallarg(netbsd32_timespecp_t) tp; 304 } */ 305 int error; 306 struct timespec ats; 307 struct netbsd32_timespec ts32; 308 309 error = clock_gettime1(SCARG(uap, clock_id), &ats); 310 if (error != 0) 311 return error; 312 313 netbsd32_from_timespec(&ats, &ts32); 314 return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32)); 315 } 316 317 int 318 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval) 319 { 320 /* { 321 syscallarg(netbsd32_clockid_t) clock_id; 322 syscallarg(const netbsd32_timespecp_t) tp; 323 } */ 324 struct netbsd32_timespec ts32; 325 struct timespec ats; 326 int error; 327 328 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0) 329 return error; 330 331 netbsd32_to_timespec(&ts32, &ats); 332 return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true); 333 } 334 335 int 336 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval) 337 { 338 /* { 339 syscallarg(netbsd32_clockid_t) clock_id; 340 syscallarg(netbsd32_timespecp_t) tp; 341 } */ 342 struct netbsd32_timespec ts32; 343 struct timespec ts; 344 int error = 0; 345 346 error = clock_getres1(SCARG(uap, clock_id), &ts); 347 if (error != 0) 348 return error; 349 350 if (SCARG_P32(uap, tp)) { 351 netbsd32_from_timespec(&ts, &ts32); 352 error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32)); 353 } 354 355 return error; 356 } 357 358 int 359 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval) 360 { 361 /* { 362 syscallarg(const netbsd32_timespecp_t) rqtp; 363 syscallarg(netbsd32_timespecp_t) rmtp; 364 } */ 365 struct netbsd32_timespec ts32; 366 struct timespec rqt, rmt; 367 int error, error1; 368 369 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 370 if (error) 371 return error; 372 netbsd32_to_timespec(&ts32, &rqt); 373 374 error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt, 375 SCARG_P32(uap, rmtp) ? &rmt : NULL); 376 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 377 return error; 378 379 netbsd32_from_timespec(&rmt, &ts32); 380 error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32)); 381 return error1 ? error1 : error; 382 } 383 384 int 385 netbsd32_clock_nanosleep(struct lwp *l, const struct netbsd32_clock_nanosleep_args *uap, register_t *retval) 386 { 387 /* { 388 syscallarg(clockid_t) clock_id; 389 syscallarg(int) flags; 390 syscallarg(const netbsd32_timespecp_t) rqtp; 391 syscallarg(netbsd32_timespecp_t) rmtp; 392 } */ 393 struct netbsd32_timespec ts32; 394 struct timespec rqt, rmt; 395 int error, error1; 396 397 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 398 if (error) 399 goto out; 400 netbsd32_to_timespec(&ts32, &rqt); 401 402 error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), 403 &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL); 404 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 405 goto out; 406 407 netbsd32_from_timespec(&rmt, &ts32); 408 if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 && 409 (error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32))) != 0) 410 error = error1; 411 out: 412 *retval = error; 413 return 0; 414 } 415 416 static int 417 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size) 418 { 419 struct sigevent *evp = dst; 420 struct netbsd32_sigevent ev32; 421 int error; 422 423 error = copyin(src, &ev32, sizeof(ev32)); 424 if (error) 425 return error; 426 427 netbsd32_to_sigevent(&ev32, evp); 428 return 0; 429 } 430 431 int 432 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval) 433 { 434 /* { 435 syscallarg(netbsd32_clockid_t) clock_id; 436 syscallarg(netbsd32_sigeventp_t) evp; 437 syscallarg(netbsd32_timerp_t) timerid; 438 } */ 439 440 return timer_create1(SCARG_P32(uap, timerid), 441 SCARG(uap, clock_id), SCARG_P32(uap, evp), 442 netbsd32_timer_create_fetch, l); 443 } 444 445 int 446 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval) 447 { 448 /* { 449 syscallarg(netbsd32_timer_t) timerid; 450 } */ 451 struct sys_timer_delete_args ua; 452 453 memset(&ua, 0, sizeof(ua)); 454 NETBSD32TO64_UAP(timerid); 455 return sys_timer_delete(l, (void *)&ua, retval); 456 } 457 458 int 459 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval) 460 { 461 /* { 462 syscallarg(netbsd32_timer_t) timerid; 463 syscallarg(int) flags; 464 syscallarg(const netbsd32_itimerspecp_t) value; 465 syscallarg(netbsd32_itimerspecp_t) ovalue; 466 } */ 467 int error; 468 struct itimerspec value, ovalue, *ovp = NULL; 469 struct netbsd32_itimerspec its32; 470 471 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0) 472 return error; 473 netbsd32_to_timespec(&its32.it_interval, &value.it_interval); 474 netbsd32_to_timespec(&its32.it_value, &value.it_value); 475 476 if (SCARG_P32(uap, ovalue)) 477 ovp = &ovalue; 478 479 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp, 480 SCARG(uap, flags), l->l_proc)) != 0) 481 return error; 482 483 if (ovp) { 484 memset(&its32, 0, sizeof(its32)); 485 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval); 486 netbsd32_from_timespec(&ovp->it_value, &its32.it_value); 487 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32)); 488 } 489 return 0; 490 } 491 492 int 493 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval) 494 { 495 /* { 496 syscallarg(netbsd32_timer_t) timerid; 497 syscallarg(netbsd32_itimerspecp_t) value; 498 } */ 499 int error; 500 struct itimerspec its; 501 struct netbsd32_itimerspec its32; 502 503 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc, 504 &its)) != 0) 505 return error; 506 507 memset(&its32, 0, sizeof(its32)); 508 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 509 netbsd32_from_timespec(&its.it_value, &its32.it_value); 510 511 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32)); 512 } 513 514 int 515 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval) 516 { 517 /* { 518 syscallarg(netbsd32_timer_t) timerid; 519 } */ 520 struct sys_timer_getoverrun_args ua; 521 522 memset(&ua, 0, sizeof(ua)); 523 NETBSD32TO64_UAP(timerid); 524 return sys_timer_getoverrun(l, (void *)&ua, retval); 525 } 526 527 int 528 netbsd32_timerfd_create(struct lwp *l, 529 const struct netbsd32_timerfd_create_args *uap, register_t *retval) 530 { 531 /* { 532 syscallarg(netbsd32_clockid_t) clock_id; 533 syscallarg(int) flags; 534 } */ 535 struct sys_timerfd_create_args ua; 536 537 memset(&ua, 0, sizeof(ua)); 538 NETBSD32TO64_UAP(clock_id); 539 NETBSD32TO64_UAP(flags); 540 return sys_timerfd_create(l, (void *)&ua, retval); 541 } 542 543 int 544 netbsd32_timerfd_settime(struct lwp *l, 545 const struct netbsd32_timerfd_settime_args *uap, register_t *retval) 546 { 547 /* { 548 syscallarg(int) fd; 549 syscallarg(int) flags; 550 syscallarg(const netbsd32_itimerspecp_t) new_value; 551 syscallarg(netbsd32_itimerspecp_t) old_value; 552 } */ 553 struct itimerspec its, oits, *oitsp = NULL; 554 struct netbsd32_itimerspec its32; 555 int error; 556 557 if ((error = copyin(SCARG_P32(uap, new_value), &its32, 558 sizeof(its32))) != 0) { 559 return error; 560 } 561 netbsd32_to_timespec(&its32.it_interval, &its.it_interval); 562 netbsd32_to_timespec(&its32.it_value, &its.it_value); 563 564 if (SCARG_P32(uap, old_value)) { 565 oitsp = &oits; 566 } 567 568 error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags), 569 &its, oitsp, retval); 570 if (error == 0 && oitsp != NULL) { 571 memset(&its32, 0, sizeof(its32)); 572 netbsd32_from_timespec(&oitsp->it_interval, &its32.it_interval); 573 netbsd32_from_timespec(&oitsp->it_value, &its32.it_value); 574 error = copyout(&its32, SCARG_P32(uap, old_value), 575 sizeof(its32)); 576 } 577 578 return error; 579 } 580 581 int 582 netbsd32_timerfd_gettime(struct lwp *l, 583 const struct netbsd32_timerfd_gettime_args *uap, register_t *retval) 584 { 585 /* { 586 syscallarg(int) fd; 587 syscallarg(int) flags; 588 syscallarg(netbsd32_itimerspecp_t) curr_value; 589 } */ 590 int error; 591 struct itimerspec its; 592 struct netbsd32_itimerspec its32; 593 594 error = do_timerfd_gettime(l, SCARG(uap, fd), &its, retval); 595 if (error == 0) { 596 memset(&its32, 0, sizeof(its32)); 597 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 598 netbsd32_from_timespec(&its.it_value, &its32.it_value); 599 error = copyout(&its32, SCARG_P32(uap, curr_value), 600 sizeof(its32)); 601 } 602 603 return error; 604 } 605 606 int 607 netbsd32_clock_getcpuclockid2(struct lwp *l, 608 const struct netbsd32_clock_getcpuclockid2_args *uap, 609 register_t *retval) 610 { 611 /* { 612 syscallarg(idtype_t) idtype; 613 syscallarg(id_t) id; 614 syscallarg(netbsd32_clockidp_t) clock_id; 615 } */ 616 pid_t pid; 617 lwpid_t lid; 618 clockid_t clock_id; 619 id_t id = SCARG(uap, id); 620 621 switch (SCARG(uap, idtype)) { 622 case P_PID: 623 pid = id == 0 ? l->l_proc->p_pid : (pid_t)id; 624 clock_id = CLOCK_PROCESS_CPUTIME_ID | pid; 625 break; 626 case P_LWPID: 627 lid = id == 0 ? l->l_lid : (lwpid_t)id; 628 clock_id = CLOCK_THREAD_CPUTIME_ID | lid; 629 break; 630 default: 631 return EINVAL; 632 } 633 return copyout(&clock_id, SCARG_P32(uap, clock_id), sizeof(clock_id)); 634 } 635