1 /* $NetBSD: linux_time.c,v 1.39 2017/07/29 02:31:22 riastradh 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.39 2017/07/29 02:31:22 riastradh 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 * Linux keeps track of a system timezone in the kernel. It is readen 61 * by gettimeofday and set by settimeofday. This emulates this behavior 62 * See linux/kernel/time.c 63 */ 64 struct timezone linux_sys_tz; 65 66 int 67 linux_sys_gettimeofday(struct lwp *l, const struct linux_sys_gettimeofday_args *uap, register_t *retval) 68 { 69 /* { 70 syscallarg(struct timeval50 *) tz; 71 syscallarg(struct timezone *) tzp; 72 } */ 73 int error = 0; 74 75 if (SCARG(uap, tp)) { 76 error = compat_50_sys_gettimeofday(l, (const void *)uap, retval); 77 if (error) 78 return (error); 79 } 80 81 if (SCARG(uap, tzp)) { 82 error = copyout(&linux_sys_tz, SCARG(uap, tzp), sizeof(linux_sys_tz)); 83 if (error) 84 return (error); 85 } 86 87 return (0); 88 } 89 90 int 91 linux_sys_settimeofday(struct lwp *l, const struct linux_sys_settimeofday_args *uap, register_t *retval) 92 { 93 /* { 94 syscallarg(struct timeval50 *) tp; 95 syscallarg(struct timezone *) tzp; 96 } */ 97 int error = 0; 98 99 if (SCARG(uap, tp)) { 100 error = compat_50_sys_settimeofday(l, (const void *)uap, retval); 101 if (error) 102 return (error); 103 } 104 105 if (SCARG(uap, tzp)) { 106 if (kauth_authorize_generic(kauth_cred_get(), 107 KAUTH_GENERIC_ISSUSER, NULL) != 0) 108 return (EPERM); 109 error = copyin(SCARG(uap, tzp), &linux_sys_tz, sizeof(linux_sys_tz)); 110 if (error) 111 return (error); 112 } 113 114 return (0); 115 } 116 117 void 118 native_to_linux_timespec(struct linux_timespec *ltp, struct timespec *ntp) 119 { 120 ltp->tv_sec = ntp->tv_sec; 121 ltp->tv_nsec = ntp->tv_nsec; 122 } 123 124 void 125 linux_to_native_timespec(struct timespec *ntp, struct linux_timespec *ltp) 126 { 127 ntp->tv_sec = ltp->tv_sec; 128 ntp->tv_nsec = ltp->tv_nsec; 129 } 130 131 int 132 linux_sys_nanosleep(struct lwp *l, const struct linux_sys_nanosleep_args *uap, 133 register_t *retval) 134 { 135 /* { 136 syscallarg(struct linux_timespec *) rqtp; 137 syscallarg(struct linux_timespec *) rmtp; 138 } */ 139 struct timespec rqts, rmts; 140 struct linux_timespec lrqts, lrmts; 141 int error, error1; 142 143 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof(lrqts)); 144 if (error != 0) 145 return error; 146 linux_to_native_timespec(&rqts, &lrqts); 147 148 error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqts, 149 SCARG(uap, rmtp) ? &rmts : NULL); 150 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 151 return error; 152 153 native_to_linux_timespec(&lrmts, &rmts); 154 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof(lrmts)); 155 return error1 ? error1 : error; 156 } 157 158 int 159 linux_to_native_clockid(clockid_t *n, clockid_t l) 160 { 161 switch (l) { 162 case LINUX_CLOCK_REALTIME: 163 *n = CLOCK_REALTIME; 164 break; 165 case LINUX_CLOCK_MONOTONIC: 166 *n = CLOCK_MONOTONIC; 167 break; 168 case LINUX_CLOCK_PROCESS_CPUTIME_ID: 169 case LINUX_CLOCK_THREAD_CPUTIME_ID: 170 case LINUX_CLOCK_REALTIME_HR: 171 case LINUX_CLOCK_MONOTONIC_HR: 172 default: 173 return EINVAL; 174 } 175 176 return 0; 177 } 178 179 int 180 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval) 181 { 182 /* { 183 syscallarg(clockid_t) which; 184 syscallarg(struct linux_timespec *)tp; 185 } */ 186 int error; 187 clockid_t id; 188 struct timespec ts; 189 struct linux_timespec lts; 190 191 error = linux_to_native_clockid(&id, SCARG(uap, which)); 192 if (error != 0) 193 return error; 194 195 error = clock_gettime1(id, &ts); 196 if (error != 0) 197 return error; 198 199 native_to_linux_timespec(<s, &ts); 200 return copyout(<s, SCARG(uap, tp), sizeof lts); 201 } 202 203 int 204 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval) 205 { 206 /* { 207 syscallarg(clockid_t) which; 208 syscallarg(struct linux_timespec *)tp; 209 } */ 210 struct timespec ts; 211 struct linux_timespec lts; 212 clockid_t id; 213 int error; 214 215 error = linux_to_native_clockid(&id, SCARG(uap, which)); 216 if (error != 0) 217 return error; 218 219 error = copyin(SCARG(uap, tp), <s, sizeof lts); 220 if (error != 0) 221 return error; 222 223 linux_to_native_timespec(&ts, <s); 224 225 return clock_settime1(l->l_proc, id, &ts, true); 226 } 227 228 int 229 linux_sys_clock_getres(struct lwp *l, const struct linux_sys_clock_getres_args *uap, register_t *retval) 230 { 231 /* { 232 syscallarg(clockid_t) which; 233 syscallarg(struct linux_timespec *)tp; 234 } */ 235 struct timespec ts; 236 struct linux_timespec lts; 237 int error; 238 clockid_t nwhich = 0; /* XXX: GCC */ 239 240 error = linux_to_native_clockid(&nwhich, SCARG(uap, which)); 241 if (error != 0 || SCARG(uap, tp) == NULL) 242 return error; 243 244 error = clock_getres1(nwhich, &ts); 245 if (error != 0) 246 return error; 247 248 native_to_linux_timespec(<s, &ts); 249 return copyout(<s, SCARG(uap, tp), sizeof lts); 250 } 251 252 int 253 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval) 254 { 255 /* { 256 syscallarg(clockid_t) which; 257 syscallarg(int) flags; 258 syscallarg(struct linux_timespec) *rqtp; 259 syscallarg(struct linux_timespec) *rmtp; 260 } */ 261 struct linux_timespec lrqts, lrmts; 262 struct timespec rqts, rmts; 263 int error, error1, flags; 264 clockid_t nwhich; 265 266 flags = SCARG(uap, flags) != 0 ? TIMER_ABSTIME : 0; 267 268 error = linux_to_native_clockid(&nwhich, SCARG(uap, which)); 269 if (error != 0) 270 return error; 271 272 error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts); 273 if (error != 0) 274 return error; 275 276 linux_to_native_timespec(&rqts, &lrqts); 277 278 error = nanosleep1(l, nwhich, flags, &rqts, 279 SCARG(uap, rmtp) ? &rmts : NULL); 280 if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 281 return error; 282 283 native_to_linux_timespec(&lrmts, &rmts); 284 error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts); 285 return error1 ? error1 : error; 286 } 287