1 /* $NetBSD: netbsd32_time.c,v 1.58 2022/10/26 23:23:52 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.58 2022/10/26 23:23:52 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 SCARG(&getargs, which) = which; 154 SCARG(&getargs, itv) = SCARG(uap, oitv); 155 if ((error = netbsd32___getitimer50(l, &getargs, retval)) != 0) 156 return error; 157 } 158 if (itv32 == 0) 159 return 0; 160 161 return dosetitimer(p, which, &aitv); 162 } 163 164 int 165 netbsd32___getitimer50(struct lwp *l, const struct netbsd32___getitimer50_args *uap, register_t *retval) 166 { 167 /* { 168 syscallarg(int) which; 169 syscallarg(netbsd32_itimervalp_t) itv; 170 } */ 171 struct proc *p = l->l_proc; 172 struct netbsd32_itimerval s32it; 173 struct itimerval aitv; 174 int error; 175 176 error = dogetitimer(p, SCARG(uap, which), &aitv); 177 if (error) 178 return error; 179 180 netbsd32_from_itimerval(&aitv, &s32it); 181 return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it)); 182 } 183 184 int 185 netbsd32___gettimeofday50(struct lwp *l, const struct netbsd32___gettimeofday50_args *uap, register_t *retval) 186 { 187 /* { 188 syscallarg(netbsd32_timevalp_t) tp; 189 syscallarg(netbsd32_timezonep_t) tzp; 190 } */ 191 struct timeval atv; 192 struct netbsd32_timeval tv32; 193 int error = 0; 194 struct netbsd32_timezone tzfake; 195 196 if (SCARG_P32(uap, tp)) { 197 microtime(&atv); 198 netbsd32_from_timeval(&atv, &tv32); 199 error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32)); 200 if (error) 201 return error; 202 } 203 if (SCARG_P32(uap, tzp)) { 204 /* 205 * NetBSD has no kernel notion of time zone, so we just 206 * fake up a timezone struct and return it if demanded. 207 */ 208 memset(&tzfake, 0, sizeof(tzfake)); 209 tzfake.tz_minuteswest = 0; 210 tzfake.tz_dsttime = 0; 211 error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake)); 212 } 213 return error; 214 } 215 216 int 217 netbsd32___settimeofday50(struct lwp *l, const struct netbsd32___settimeofday50_args *uap, register_t *retval) 218 { 219 /* { 220 syscallarg(const netbsd32_timevalp_t) tv; 221 syscallarg(const netbsd32_timezonep_t) tzp; 222 } */ 223 struct netbsd32_timeval atv32; 224 struct timeval atv; 225 struct timespec ats; 226 int error; 227 struct proc *p = l->l_proc; 228 229 /* Verify all parameters before changing time. */ 230 231 /* 232 * NetBSD has no kernel notion of time zone, and only an 233 * obsolete program would try to set it, so we log a warning. 234 */ 235 if (SCARG_P32(uap, tzp)) 236 printf("pid %d attempted to set the " 237 "(obsolete) kernel time zone\n", p->p_pid); 238 239 if (SCARG_P32(uap, tv) == 0) 240 return 0; 241 242 if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0) 243 return error; 244 245 netbsd32_to_timeval(&atv32, &atv); 246 247 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000) 248 return EINVAL; 249 250 TIMEVAL_TO_TIMESPEC(&atv, &ats); 251 return settime(p, &ats); 252 } 253 254 int 255 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval) 256 { 257 /* { 258 syscallarg(const netbsd32_timevalp_t) delta; 259 syscallarg(netbsd32_timevalp_t) olddelta; 260 } */ 261 struct netbsd32_timeval atv; 262 int error; 263 264 if ((error = kauth_authorize_system(l->l_cred, 265 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, 266 NULL)) != 0) 267 return error; 268 269 if (SCARG_P32(uap, olddelta)) { 270 memset(&atv, 0, sizeof(atv)); 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 memset(&its32, 0, sizeof(its32)); 483 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval); 484 netbsd32_from_timespec(&ovp->it_value, &its32.it_value); 485 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32)); 486 } 487 return 0; 488 } 489 490 int 491 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval) 492 { 493 /* { 494 syscallarg(netbsd32_timer_t) timerid; 495 syscallarg(netbsd32_itimerspecp_t) value; 496 } */ 497 int error; 498 struct itimerspec its; 499 struct netbsd32_itimerspec its32; 500 501 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc, 502 &its)) != 0) 503 return error; 504 505 memset(&its32, 0, sizeof(its32)); 506 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 507 netbsd32_from_timespec(&its.it_value, &its32.it_value); 508 509 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32)); 510 } 511 512 int 513 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval) 514 { 515 /* { 516 syscallarg(netbsd32_timer_t) timerid; 517 } */ 518 struct sys_timer_getoverrun_args ua; 519 520 NETBSD32TO64_UAP(timerid); 521 return sys_timer_getoverrun(l, (void *)&ua, retval); 522 } 523 524 int 525 netbsd32_timerfd_create(struct lwp *l, 526 const struct netbsd32_timerfd_create_args *uap, register_t *retval) 527 { 528 /* { 529 syscallarg(netbsd32_clockid_t) clock_id; 530 syscallarg(int) flags; 531 } */ 532 struct sys_timerfd_create_args ua; 533 534 NETBSD32TO64_UAP(clock_id); 535 NETBSD32TO64_UAP(flags); 536 return sys_timerfd_create(l, (void *)&ua, retval); 537 } 538 539 int 540 netbsd32_timerfd_settime(struct lwp *l, 541 const struct netbsd32_timerfd_settime_args *uap, register_t *retval) 542 { 543 /* { 544 syscallarg(int) fd; 545 syscallarg(int) flags; 546 syscallarg(const netbsd32_itimerspecp_t) new_value; 547 syscallarg(netbsd32_itimerspecp_t) old_value; 548 } */ 549 struct itimerspec its, oits, *oitsp = NULL; 550 struct netbsd32_itimerspec its32; 551 int error; 552 553 if ((error = copyin(SCARG_P32(uap, new_value), &its32, 554 sizeof(its32))) != 0) { 555 return error; 556 } 557 netbsd32_to_timespec(&its32.it_interval, &its.it_interval); 558 netbsd32_to_timespec(&its32.it_value, &its.it_value); 559 560 if (SCARG_P32(uap, old_value)) { 561 oitsp = &oits; 562 } 563 564 error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags), 565 &its, oitsp, retval); 566 if (error == 0 && oitsp != NULL) { 567 memset(&its32, 0, sizeof(its32)); 568 netbsd32_from_timespec(&oitsp->it_interval, &its32.it_interval); 569 netbsd32_from_timespec(&oitsp->it_value, &its32.it_value); 570 error = copyout(&its32, SCARG_P32(uap, old_value), 571 sizeof(its32)); 572 } 573 574 return error; 575 } 576 577 int 578 netbsd32_timerfd_gettime(struct lwp *l, 579 const struct netbsd32_timerfd_gettime_args *uap, register_t *retval) 580 { 581 /* { 582 syscallarg(int) fd; 583 syscallarg(int) flags; 584 syscallarg(netbsd32_itimerspecp_t) curr_value; 585 } */ 586 int error; 587 struct itimerspec its; 588 struct netbsd32_itimerspec its32; 589 590 error = do_timerfd_gettime(l, SCARG(uap, fd), &its, retval); 591 if (error == 0) { 592 memset(&its32, 0, sizeof(its32)); 593 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 594 netbsd32_from_timespec(&its.it_value, &its32.it_value); 595 error = copyout(&its32, SCARG_P32(uap, curr_value), 596 sizeof(its32)); 597 } 598 599 return error; 600 } 601 602 int 603 netbsd32_clock_getcpuclockid2(struct lwp *l, 604 const struct netbsd32_clock_getcpuclockid2_args *uap, 605 register_t *retval) 606 { 607 /* { 608 syscallarg(idtype_t) idtype; 609 syscallarg(id_t) id; 610 syscallarg(netbsd32_clockidp_t) clock_id; 611 } */ 612 pid_t pid; 613 lwpid_t lid; 614 clockid_t clock_id; 615 id_t id = SCARG(uap, id); 616 617 switch (SCARG(uap, idtype)) { 618 case P_PID: 619 pid = id == 0 ? l->l_proc->p_pid : (pid_t)id; 620 clock_id = CLOCK_PROCESS_CPUTIME_ID | pid; 621 break; 622 case P_LWPID: 623 lid = id == 0 ? l->l_lid : (lwpid_t)id; 624 clock_id = CLOCK_THREAD_CPUTIME_ID | lid; 625 break; 626 default: 627 return EINVAL; 628 } 629 return copyout(&clock_id, SCARG_P32(uap, clock_id), sizeof(clock_id)); 630 } 631