1 /* $NetBSD: netbsd32_time.c,v 1.57 2021/09/20 01:00:55 thorpej 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.57 2021/09/20 01:00:55 thorpej 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 extern int time_adjusted; /* in kern_ntptime.c */ 265 extern int64_t time_adjtime; /* in kern_ntptime.c */ 266 267 if ((error = kauth_authorize_system(l->l_cred, 268 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, 269 NULL)) != 0) 270 return error; 271 272 if (SCARG_P32(uap, olddelta)) { 273 memset(&atv, 0, sizeof(atv)); 274 atv.tv_sec = time_adjtime / 1000000; 275 atv.tv_usec = time_adjtime % 1000000; 276 if (atv.tv_usec < 0) { 277 atv.tv_usec += 1000000; 278 atv.tv_sec--; 279 } 280 error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv)); 281 if (error) 282 return error; 283 } 284 285 if (SCARG_P32(uap, delta)) { 286 error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv)); 287 if (error) 288 return error; 289 290 time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec; 291 292 if (time_adjtime) 293 /* We need to save the system time during shutdown */ 294 time_adjusted |= 1; 295 } 296 297 return 0; 298 } 299 300 int 301 netbsd32___clock_gettime50(struct lwp *l, const struct netbsd32___clock_gettime50_args *uap, register_t *retval) 302 { 303 /* { 304 syscallarg(netbsd32_clockid_t) clock_id; 305 syscallarg(netbsd32_timespecp_t) tp; 306 } */ 307 int error; 308 struct timespec ats; 309 struct netbsd32_timespec ts32; 310 311 error = clock_gettime1(SCARG(uap, clock_id), &ats); 312 if (error != 0) 313 return error; 314 315 netbsd32_from_timespec(&ats, &ts32); 316 return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32)); 317 } 318 319 int 320 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval) 321 { 322 /* { 323 syscallarg(netbsd32_clockid_t) clock_id; 324 syscallarg(const netbsd32_timespecp_t) tp; 325 } */ 326 struct netbsd32_timespec ts32; 327 struct timespec ats; 328 int error; 329 330 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0) 331 return error; 332 333 netbsd32_to_timespec(&ts32, &ats); 334 return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true); 335 } 336 337 int 338 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval) 339 { 340 /* { 341 syscallarg(netbsd32_clockid_t) clock_id; 342 syscallarg(netbsd32_timespecp_t) tp; 343 } */ 344 struct netbsd32_timespec ts32; 345 struct timespec ts; 346 int error = 0; 347 348 error = clock_getres1(SCARG(uap, clock_id), &ts); 349 if (error != 0) 350 return error; 351 352 if (SCARG_P32(uap, tp)) { 353 netbsd32_from_timespec(&ts, &ts32); 354 error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32)); 355 } 356 357 return error; 358 } 359 360 int 361 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval) 362 { 363 /* { 364 syscallarg(const netbsd32_timespecp_t) rqtp; 365 syscallarg(netbsd32_timespecp_t) rmtp; 366 } */ 367 struct netbsd32_timespec ts32; 368 struct timespec rqt, rmt; 369 int error, error1; 370 371 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 372 if (error) 373 return error; 374 netbsd32_to_timespec(&ts32, &rqt); 375 376 error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt, 377 SCARG_P32(uap, rmtp) ? &rmt : NULL); 378 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 379 return error; 380 381 netbsd32_from_timespec(&rmt, &ts32); 382 error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32)); 383 return error1 ? error1 : error; 384 } 385 386 int 387 netbsd32_clock_nanosleep(struct lwp *l, const struct netbsd32_clock_nanosleep_args *uap, register_t *retval) 388 { 389 /* { 390 syscallarg(clockid_t) clock_id; 391 syscallarg(int) flags; 392 syscallarg(const netbsd32_timespecp_t) rqtp; 393 syscallarg(netbsd32_timespecp_t) rmtp; 394 } */ 395 struct netbsd32_timespec ts32; 396 struct timespec rqt, rmt; 397 int error, error1; 398 399 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 400 if (error) 401 goto out; 402 netbsd32_to_timespec(&ts32, &rqt); 403 404 error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), 405 &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL); 406 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 407 goto out; 408 409 netbsd32_from_timespec(&rmt, &ts32); 410 if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 && 411 (error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32))) != 0) 412 error = error1; 413 out: 414 *retval = error; 415 return 0; 416 } 417 418 static int 419 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size) 420 { 421 struct sigevent *evp = dst; 422 struct netbsd32_sigevent ev32; 423 int error; 424 425 error = copyin(src, &ev32, sizeof(ev32)); 426 if (error) 427 return error; 428 429 netbsd32_to_sigevent(&ev32, evp); 430 return 0; 431 } 432 433 int 434 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval) 435 { 436 /* { 437 syscallarg(netbsd32_clockid_t) clock_id; 438 syscallarg(netbsd32_sigeventp_t) evp; 439 syscallarg(netbsd32_timerp_t) timerid; 440 } */ 441 442 return timer_create1(SCARG_P32(uap, timerid), 443 SCARG(uap, clock_id), SCARG_P32(uap, evp), 444 netbsd32_timer_create_fetch, l); 445 } 446 447 int 448 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval) 449 { 450 /* { 451 syscallarg(netbsd32_timer_t) timerid; 452 } */ 453 struct sys_timer_delete_args ua; 454 455 NETBSD32TO64_UAP(timerid); 456 return sys_timer_delete(l, (void *)&ua, retval); 457 } 458 459 int 460 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval) 461 { 462 /* { 463 syscallarg(netbsd32_timer_t) timerid; 464 syscallarg(int) flags; 465 syscallarg(const netbsd32_itimerspecp_t) value; 466 syscallarg(netbsd32_itimerspecp_t) ovalue; 467 } */ 468 int error; 469 struct itimerspec value, ovalue, *ovp = NULL; 470 struct netbsd32_itimerspec its32; 471 472 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0) 473 return error; 474 netbsd32_to_timespec(&its32.it_interval, &value.it_interval); 475 netbsd32_to_timespec(&its32.it_value, &value.it_value); 476 477 if (SCARG_P32(uap, ovalue)) 478 ovp = &ovalue; 479 480 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp, 481 SCARG(uap, flags), l->l_proc)) != 0) 482 return error; 483 484 if (ovp) { 485 memset(&its32, 0, sizeof(its32)); 486 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval); 487 netbsd32_from_timespec(&ovp->it_value, &its32.it_value); 488 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32)); 489 } 490 return 0; 491 } 492 493 int 494 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval) 495 { 496 /* { 497 syscallarg(netbsd32_timer_t) timerid; 498 syscallarg(netbsd32_itimerspecp_t) value; 499 } */ 500 int error; 501 struct itimerspec its; 502 struct netbsd32_itimerspec its32; 503 504 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc, 505 &its)) != 0) 506 return error; 507 508 memset(&its32, 0, sizeof(its32)); 509 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 510 netbsd32_from_timespec(&its.it_value, &its32.it_value); 511 512 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32)); 513 } 514 515 int 516 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval) 517 { 518 /* { 519 syscallarg(netbsd32_timer_t) timerid; 520 } */ 521 struct sys_timer_getoverrun_args ua; 522 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 NETBSD32TO64_UAP(clock_id); 538 NETBSD32TO64_UAP(flags); 539 return sys_timerfd_create(l, (void *)&ua, retval); 540 } 541 542 int 543 netbsd32_timerfd_settime(struct lwp *l, 544 const struct netbsd32_timerfd_settime_args *uap, register_t *retval) 545 { 546 /* { 547 syscallarg(int) fd; 548 syscallarg(int) flags; 549 syscallarg(const netbsd32_itimerspecp_t) new_value; 550 syscallarg(netbsd32_itimerspecp_t) old_value; 551 } */ 552 struct itimerspec its, oits, *oitsp = NULL; 553 struct netbsd32_itimerspec its32; 554 int error; 555 556 if ((error = copyin(SCARG_P32(uap, new_value), &its32, 557 sizeof(its32))) != 0) { 558 return error; 559 } 560 netbsd32_to_timespec(&its32.it_interval, &its.it_interval); 561 netbsd32_to_timespec(&its32.it_value, &its.it_value); 562 563 if (SCARG_P32(uap, old_value)) { 564 oitsp = &oits; 565 } 566 567 error = do_timerfd_settime(l, SCARG(uap, fd), SCARG(uap, flags), 568 &its, oitsp, retval); 569 if (error == 0 && oitsp != NULL) { 570 memset(&its32, 0, sizeof(its32)); 571 netbsd32_from_timespec(&oitsp->it_interval, &its32.it_interval); 572 netbsd32_from_timespec(&oitsp->it_value, &its32.it_value); 573 error = copyout(&its32, SCARG_P32(uap, old_value), 574 sizeof(its32)); 575 } 576 577 return error; 578 } 579 580 int 581 netbsd32_timerfd_gettime(struct lwp *l, 582 const struct netbsd32_timerfd_gettime_args *uap, register_t *retval) 583 { 584 /* { 585 syscallarg(int) fd; 586 syscallarg(int) flags; 587 syscallarg(netbsd32_itimerspecp_t) curr_value; 588 } */ 589 int error; 590 struct itimerspec its; 591 struct netbsd32_itimerspec its32; 592 593 error = do_timerfd_gettime(l, SCARG(uap, fd), &its, retval); 594 if (error == 0) { 595 memset(&its32, 0, sizeof(its32)); 596 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 597 netbsd32_from_timespec(&its.it_value, &its32.it_value); 598 error = copyout(&its32, SCARG_P32(uap, curr_value), 599 sizeof(its32)); 600 } 601 602 return error; 603 } 604 605 int 606 netbsd32_clock_getcpuclockid2(struct lwp *l, 607 const struct netbsd32_clock_getcpuclockid2_args *uap, 608 register_t *retval) 609 { 610 /* { 611 syscallarg(idtype_t) idtype; 612 syscallarg(id_t) id; 613 syscallarg(netbsd32_clockidp_t) clock_id; 614 } */ 615 pid_t pid; 616 lwpid_t lid; 617 clockid_t clock_id; 618 id_t id = SCARG(uap, id); 619 620 switch (SCARG(uap, idtype)) { 621 case P_PID: 622 pid = id == 0 ? l->l_proc->p_pid : (pid_t)id; 623 clock_id = CLOCK_PROCESS_CPUTIME_ID | pid; 624 break; 625 case P_LWPID: 626 lid = id == 0 ? l->l_lid : (lwpid_t)id; 627 clock_id = CLOCK_THREAD_CPUTIME_ID | lid; 628 break; 629 default: 630 return EINVAL; 631 } 632 return copyout(&clock_id, SCARG_P32(uap, clock_id), sizeof(clock_id)); 633 } 634