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