1/* $NetBSD: linux_time.c,v 1.34 2010/07/12 12:01:53 njoly Exp $ */ 2 3/*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Emmanuel Dreyfus. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.34 2010/07/12 12:01:53 njoly Exp $"); 34 35#include <sys/param.h> 36#include <sys/ucred.h> 37#include <sys/kauth.h> 38#include <sys/mount.h> 39#include <sys/signal.h> 40#include <sys/stdint.h> 41#include <sys/time.h> 42#include <sys/systm.h> 43#include <sys/sched.h> 44#include <sys/syscallargs.h> 45#include <sys/lwp.h> 46#include <sys/proc.h> 47 48#include <compat/linux/common/linux_types.h> 49#include <compat/linux/common/linux_signal.h> 50#include <compat/linux/common/linux_machdep.h> 51#include <compat/linux/common/linux_sched.h> 52#include <compat/linux/common/linux_ipc.h> 53#include <compat/linux/common/linux_sem.h> 54 55#include <compat/linux/linux_syscallargs.h> 56 57#include <compat/common/compat_util.h> 58 59/* 60 * This is not implemented for alpha yet 61 */ 62#if defined (__i386__) || defined (__m68k__) || \ 63 defined (__powerpc__) || defined (__mips__) || \ 64 defined(__arm__) || defined(__amd64__) 65 66/* 67 * Linux keeps track of a system timezone in the kernel. It is readen 68 * by gettimeofday and set by settimeofday. This emulates this behavior 69 * See linux/kernel/time.c 70 */ 71struct timezone linux_sys_tz; 72 73int 74linux_sys_gettimeofday(struct lwp *l, const struct linux_sys_gettimeofday_args *uap, register_t *retval) 75{ 76 /* { 77 syscallarg(struct timeval50 *) tz; 78 syscallarg(struct timezone *) tzp; 79 } */ 80 int error = 0; 81 82 if (SCARG(uap, tp)) { 83 error = compat_50_sys_gettimeofday(l, (const void *)uap, retval); 84 if (error) 85 return (error); 86 } 87 88 if (SCARG(uap, tzp)) { 89 error = copyout(&linux_sys_tz, SCARG(uap, tzp), sizeof(linux_sys_tz)); 90 if (error) 91 return (error); 92 } 93 94 return (0); 95} 96 97int 98linux_sys_settimeofday(struct lwp *l, const struct linux_sys_settimeofday_args *uap, register_t *retval) 99{ 100 /* { 101 syscallarg(struct timeval50 *) tp; 102 syscallarg(struct timezone *) tzp; 103 } */ 104 int error = 0; 105 106 if (SCARG(uap, tp)) { 107 error = compat_50_sys_settimeofday(l, (const void *)uap, retval); 108 if (error) 109 return (error); 110 } 111 112 /* 113 * If user is not the superuser, we returned 114 * after the sys_settimeofday() call. 115 */ 116 if (SCARG(uap, tzp)) { 117 error = copyin(SCARG(uap, tzp), &linux_sys_tz, sizeof(linux_sys_tz)); 118 if (error) 119 return (error); 120 } 121 122 return (0); 123} 124 125#endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */ 126 127void 128native_to_linux_timespec(struct linux_timespec *ltp, struct timespec *ntp) 129{ 130 ltp->tv_sec = ntp->tv_sec; 131 ltp->tv_nsec = ntp->tv_nsec; 132} 133 134void 135linux_to_native_timespec(struct timespec *ntp, struct linux_timespec *ltp) 136{ 137 ntp->tv_sec = ltp->tv_sec; 138 ntp->tv_nsec = ltp->tv_nsec; 139} 140 141int 142linux_sys_nanosleep(struct lwp *l, const struct linux_sys_nanosleep_args *uap, 143 register_t *retval) 144{ 145 /* { 146 syscallarg(struct linux_timespec *) rqtp; 147 syscallarg(struct linux_timespec *) rmtp; 148 } */ 149 struct timespec rqts, rmts; 150 struct linux_timespec lrqts, lrmts; 151 int error, error1; 152 153 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof(lrqts)); 154 if (error != 0) 155 return error; 156 linux_to_native_timespec(&rqts, &lrqts); 157 158 error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : NULL); 159 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 160 return error; 161 162 native_to_linux_timespec(&lrmts, &rmts); 163 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof(lrmts)); 164 return error1 ? error1 : error; 165} 166 167int 168linux_to_native_clockid(clockid_t *n, clockid_t l) 169{ 170 switch (l) { 171 case LINUX_CLOCK_REALTIME: 172 *n = CLOCK_REALTIME; 173 break; 174 case LINUX_CLOCK_MONOTONIC: 175 *n = CLOCK_MONOTONIC; 176 break; 177 case LINUX_CLOCK_PROCESS_CPUTIME_ID: 178 case LINUX_CLOCK_THREAD_CPUTIME_ID: 179 case LINUX_CLOCK_REALTIME_HR: 180 case LINUX_CLOCK_MONOTONIC_HR: 181 default: 182 return EINVAL; 183 } 184 185 return 0; 186} 187 188int 189linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval) 190{ 191 /* { 192 syscallarg(clockid_t) which; 193 syscallarg(struct linux_timespec *)tp; 194 } */ 195 int error; 196 clockid_t id; 197 struct timespec ts; 198 struct linux_timespec lts; 199 200 error = linux_to_native_clockid(&id, SCARG(uap, which)); 201 if (error != 0) 202 return error; 203 204 error = clock_gettime1(id, &ts); 205 if (error != 0) 206 return error; 207 208 native_to_linux_timespec(<s, &ts); 209 return copyout(<s, SCARG(uap, tp), sizeof lts); 210} 211 212int 213linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval) 214{ 215 /* { 216 syscallarg(clockid_t) which; 217 syscallarg(struct linux_timespec *)tp; 218 } */ 219 struct timespec ts; 220 struct linux_timespec lts; 221 clockid_t id; 222 int error; 223 224 error = linux_to_native_clockid(&id, SCARG(uap, which)); 225 if (error != 0) 226 return error; 227 228 error = copyin(SCARG(uap, tp), <s, sizeof lts); 229 if (error != 0) 230 return error; 231 232 linux_to_native_timespec(&ts, <s); 233 234 return clock_settime1(l->l_proc, id, &ts, true); 235} 236 237int 238linux_sys_clock_getres(struct lwp *l, const struct linux_sys_clock_getres_args *uap, register_t *retval) 239{ 240 /* { 241 syscallarg(clockid_t) which; 242 syscallarg(struct linux_timespec *)tp; 243 } */ 244 struct timespec ts; 245 struct linux_timespec lts; 246 int error; 247 clockid_t nwhich = 0; /* XXX: GCC */ 248 249 error = linux_to_native_clockid(&nwhich, SCARG(uap, which)); 250 if (error != 0 || SCARG(uap, tp) == NULL) 251 return error; 252 253 error = clock_getres1(nwhich, &ts); 254 if (error != 0) 255 return error; 256 257 native_to_linux_timespec(<s, &ts); 258 return copyout(<s, SCARG(uap, tp), sizeof lts); 259} 260 261int 262linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval) 263{ 264 /* { 265 syscallarg(clockid_t) which; 266 syscallarg(int) flags; 267 syscallarg(struct linux_timespec) *rqtp; 268 syscallarg(struct linux_timespec) *rmtp; 269 } */ 270 struct linux_timespec lrqts, lrmts; 271 struct timespec rqts, rmts; 272 int error, error1; 273 clockid_t nwhich; 274 275 if (SCARG(uap, flags) != 0) 276 return EINVAL; /* XXX deal with TIMER_ABSTIME */ 277 278 error = linux_to_native_clockid(&nwhich, SCARG(uap, which)); 279 if (error != 0) 280 return error; 281 282 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts); 283 if (error != 0) 284 return error; 285 286 linux_to_native_timespec(&rqts, &lrqts); 287 288 error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : NULL); 289 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 290 return error; 291 292 native_to_linux_timespec(&lrmts, &rmts); 293 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts); 294 return error1 ? error1 : error; 295} 296