1 /* $NetBSD: linux_time.c,v 1.29 2009/07/21 18:42:56 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.29 2009/07/21 18:42:56 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/timetc.h> 43 #include <sys/systm.h> 44 #include <sys/sched.h> 45 #include <sys/syscallargs.h> 46 #include <sys/lwp.h> 47 #include <sys/proc.h> 48 49 #include <compat/linux/common/linux_types.h> 50 #include <compat/linux/common/linux_signal.h> 51 #include <compat/linux/common/linux_machdep.h> 52 #include <compat/linux/common/linux_sched.h> 53 #include <compat/linux/common/linux_ipc.h> 54 #include <compat/linux/common/linux_sem.h> 55 56 #include <compat/linux/linux_syscallargs.h> 57 58 #include <compat/common/compat_util.h> 59 60 void native_to_linux_timespec(struct linux_timespec *, struct timespec *); 61 void linux_to_native_timespec(struct timespec *, struct linux_timespec *); 62 /* 63 * This is not implemented for alpha yet 64 */ 65 #if defined (__i386__) || defined (__m68k__) || \ 66 defined (__powerpc__) || defined (__mips__) || \ 67 defined(__arm__) || defined(__amd64__) 68 69 /* 70 * Linux keeps track of a system timezone in the kernel. It is readen 71 * by gettimeofday and set by settimeofday. This emulates this behavior 72 * See linux/kernel/time.c 73 */ 74 struct timezone linux_sys_tz; 75 76 int 77 linux_sys_gettimeofday(struct lwp *l, const struct linux_sys_gettimeofday_args *uap, register_t *retval) 78 { 79 /* { 80 syscallarg(struct timeval50 *) tz; 81 syscallarg(struct timezone *) tzp; 82 } */ 83 int error = 0; 84 85 if (SCARG(uap, tp)) { 86 error = compat_50_sys_gettimeofday(l, (const void *)uap, retval); 87 if (error) 88 return (error); 89 } 90 91 if (SCARG(uap, tzp)) { 92 error = copyout(&linux_sys_tz, SCARG(uap, tzp), sizeof(linux_sys_tz)); 93 if (error) 94 return (error); 95 } 96 97 return (0); 98 } 99 100 int 101 linux_sys_settimeofday(struct lwp *l, const struct linux_sys_settimeofday_args *uap, register_t *retval) 102 { 103 /* { 104 syscallarg(struct timeval50 *) tp; 105 syscallarg(struct timezone *) tzp; 106 } */ 107 int error = 0; 108 109 if (SCARG(uap, tp)) { 110 error = compat_50_sys_settimeofday(l, (const void *)uap, retval); 111 if (error) 112 return (error); 113 } 114 115 /* 116 * If user is not the superuser, we returned 117 * after the sys_settimeofday() call. 118 */ 119 if (SCARG(uap, tzp)) { 120 error = copyin(SCARG(uap, tzp), &linux_sys_tz, sizeof(linux_sys_tz)); 121 if (error) 122 return (error); 123 } 124 125 return (0); 126 } 127 128 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */ 129 130 void 131 native_to_linux_timespec(struct linux_timespec *ltp, struct timespec *ntp) 132 { 133 ltp->tv_sec = ntp->tv_sec; 134 ltp->tv_nsec = ntp->tv_nsec; 135 } 136 137 void 138 linux_to_native_timespec(struct timespec *ntp, struct linux_timespec *ltp) 139 { 140 ntp->tv_sec = ltp->tv_sec; 141 ntp->tv_nsec = ltp->tv_nsec; 142 } 143 144 int 145 linux_sys_nanosleep(struct lwp *l, const struct linux_sys_nanosleep_args *uap, 146 register_t *retval) 147 { 148 /* { 149 syscallarg(struct linux_timespec *) rqtp; 150 syscallarg(struct linux_timespec *) rmtp; 151 } */ 152 struct timespec rqts, rmts; 153 struct linux_timespec lrqts, lrmts; 154 int error, error1; 155 156 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof(lrqts)); 157 if (error != 0) 158 return error; 159 linux_to_native_timespec(&rqts, &lrqts); 160 161 error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : NULL); 162 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 163 return error; 164 165 native_to_linux_timespec(&lrmts, &rmts); 166 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof(lrmts)); 167 return error1 ? error1 : error; 168 } 169 170 int 171 linux_to_native_clockid(clockid_t *n, clockid_t l) 172 { 173 switch (l) { 174 case LINUX_CLOCK_REALTIME: 175 *n = CLOCK_REALTIME; 176 break; 177 case LINUX_CLOCK_MONOTONIC: 178 *n = CLOCK_MONOTONIC; 179 break; 180 case LINUX_CLOCK_PROCESS_CPUTIME_ID: 181 case LINUX_CLOCK_THREAD_CPUTIME_ID: 182 case LINUX_CLOCK_REALTIME_HR: 183 case LINUX_CLOCK_MONOTONIC_HR: 184 default: 185 return EINVAL; 186 } 187 188 return 0; 189 } 190 191 int 192 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval) 193 { 194 /* { 195 syscallarg(clockid_t) which; 196 syscallarg(struct linux_timespec *)tp; 197 } */ 198 struct timespec ts; 199 struct linux_timespec lts; 200 201 switch (SCARG(uap, which)) { 202 case LINUX_CLOCK_REALTIME: 203 nanotime(&ts); 204 break; 205 case LINUX_CLOCK_MONOTONIC: 206 nanouptime(&ts); 207 break; 208 default: 209 return EINVAL; 210 } 211 212 native_to_linux_timespec(<s, &ts); 213 return copyout(<s, SCARG(uap, tp), sizeof lts); 214 } 215 216 int 217 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval) 218 { 219 /* { 220 syscallarg(clockid_t) which; 221 syscallarg(struct linux_timespec *)tp; 222 } */ 223 struct timespec ts; 224 struct linux_timespec lts; 225 int error; 226 227 switch (SCARG(uap, which)) { 228 case LINUX_CLOCK_REALTIME: 229 break; 230 default: 231 return EINVAL; 232 } 233 234 error = copyin(SCARG(uap, tp), <s, sizeof lts); 235 if (error != 0) 236 return error; 237 238 linux_to_native_timespec(&ts, <s); 239 240 return settime(l->l_proc, &ts); 241 } 242 243 int 244 linux_sys_clock_getres(struct lwp *l, const struct linux_sys_clock_getres_args *uap, register_t *retval) 245 { 246 /* { 247 syscallarg(clockid_t) which; 248 syscallarg(struct linux_timespec *)tp; 249 } */ 250 struct timespec ts; 251 struct linux_timespec lts; 252 int error; 253 clockid_t nwhich = 0; /* XXX: GCC */ 254 255 error = linux_to_native_clockid(&nwhich, SCARG(uap, which)); 256 if (error != 0 || SCARG(uap, tp) == NULL) 257 return error; 258 259 ts.tv_sec = 0; 260 ts.tv_nsec = 1000000000 / tc_getfrequency(); 261 native_to_linux_timespec(<s, &ts); 262 return copyout(<s, SCARG(uap, tp), sizeof lts); 263 } 264 265 int 266 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval) 267 { 268 /* { 269 syscallarg(clockid_t) which; 270 syscallarg(int) flags; 271 syscallarg(struct linux_timespec) *rqtp; 272 syscallarg(struct linux_timespec) *rmtp; 273 } */ 274 struct linux_timespec lrqts, lrmts; 275 struct timespec rqts, rmts; 276 int error, error1; 277 278 if (SCARG(uap, flags) != 0) 279 return EINVAL; /* XXX deal with TIMER_ABSTIME */ 280 281 if (SCARG(uap, which) != LINUX_CLOCK_REALTIME) 282 return EINVAL; 283 284 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts); 285 if (error != 0) 286 return error; 287 288 linux_to_native_timespec(&rqts, &lrqts); 289 290 error = nanosleep1(l, &rqts, SCARG(uap, rmtp) ? &rmts : 0); 291 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 292 return error; 293 294 native_to_linux_timespec(&lrmts, &rmts); 295 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts); 296 return error1 ? error1 : error; 297 } 298