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