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