1 /* $NetBSD: linux32_time.c,v 1.38 2021/09/07 11:43:04 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Emmanuel Dreyfus 17 * 4. The name of the author may not be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS'' 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 36 __KERNEL_RCSID(0, "$NetBSD: linux32_time.c,v 1.38 2021/09/07 11:43:04 riastradh Exp $"); 37 38 #include <sys/types.h> 39 #include <sys/param.h> 40 #include <sys/fstypes.h> 41 #include <sys/signal.h> 42 #include <sys/dirent.h> 43 #include <sys/kauth.h> 44 #include <sys/kernel.h> 45 #include <sys/fcntl.h> 46 #include <sys/namei.h> 47 #include <sys/select.h> 48 #include <sys/proc.h> 49 #include <sys/resourcevar.h> 50 #include <sys/ucred.h> 51 #include <sys/swap.h> 52 #include <sys/vfs_syscalls.h> 53 54 #include <machine/types.h> 55 56 #include <sys/syscallargs.h> 57 58 #include <compat/netbsd32/netbsd32.h> 59 #include <compat/netbsd32/netbsd32_conv.h> 60 #include <compat/netbsd32/netbsd32_syscallargs.h> 61 62 #include <compat/linux/common/linux_types.h> 63 #include <compat/linux/common/linux_signal.h> 64 #include <compat/linux/common/linux_machdep.h> 65 #include <compat/linux/common/linux_misc.h> 66 #include <compat/linux/common/linux_oldolduname.h> 67 #include <compat/linux/common/linux_sched.h> 68 #include <compat/linux/common/linux_ipc.h> 69 #include <compat/linux/common/linux_sem.h> 70 #include <compat/linux/linux_syscallargs.h> 71 72 #include <compat/linux32/common/linux32_types.h> 73 #include <compat/linux32/common/linux32_signal.h> 74 #include <compat/linux32/common/linux32_machdep.h> 75 #include <compat/linux32/common/linux32_sysctl.h> 76 #include <compat/linux32/common/linux32_socketcall.h> 77 #include <compat/linux32/common/linux32_sched.h> 78 #include <compat/linux32/linux32_syscallargs.h> 79 80 extern struct timezone linux_sys_tz; 81 82 void native_to_linux32_timespec(struct linux32_timespec *, struct timespec *); 83 void linux32_to_native_timespec(struct timespec *, struct linux32_timespec *); 84 85 int 86 linux32_sys_gettimeofday(struct lwp *l, const struct linux32_sys_gettimeofday_args *uap, register_t *retval) 87 { 88 /* { 89 syscallarg(netbsd32_timeval50p_t) tp; 90 syscallarg(netbsd32_timezonep_t) tzp; 91 } */ 92 struct timeval tv; 93 struct netbsd32_timeval50 tv32; 94 int error; 95 96 if (SCARG_P32(uap, tp) != NULL) { 97 microtime(&tv); 98 netbsd32_from_timeval50(&tv, &tv32); 99 if ((error = copyout(&tv32, SCARG_P32(uap, tp), 100 sizeof(tv32))) != 0) 101 return error; 102 } 103 104 /* timezone size does not change */ 105 if (SCARG_P32(uap, tzp) != NULL) { 106 if ((error = copyout(&linux_sys_tz, SCARG_P32(uap, tzp), 107 sizeof(linux_sys_tz))) != 0) 108 return error; 109 } 110 111 return 0; 112 } 113 114 int 115 linux32_sys_settimeofday(struct lwp *l, const struct linux32_sys_settimeofday_args *uap, register_t *retval) 116 { 117 /* { 118 syscallarg(netbsd32_timeval50p_t) tp; 119 syscallarg(netbsd32_timezonep_t) tzp; 120 } */ 121 struct linux_sys_settimeofday_args ua; 122 123 NETBSD32TOP_UAP(tp, struct timeval50); 124 NETBSD32TOP_UAP(tzp, struct timezone); 125 126 return linux_sys_settimeofday(l, &ua, retval); 127 } 128 129 int 130 linux32_sys_time(struct lwp *l, const struct linux32_sys_time_args *uap, register_t *retval) 131 { 132 /* { 133 syscallarg(linux32_timep_t) t; 134 } */ 135 struct timeval atv; 136 linux32_time_t tt; 137 int error; 138 139 microtime(&atv); 140 141 tt = (linux32_time_t)atv.tv_sec; 142 143 if (SCARG_P32(uap, t) && (error = copyout(&tt, 144 SCARG_P32(uap, t), sizeof(tt)))) 145 return error; 146 147 retval[0] = tt; 148 149 return 0; 150 } 151 152 153 #define CONVTCK(r) (r.tv_sec * hz + r.tv_usec / (1000000 / hz)) 154 155 int 156 linux32_sys_times(struct lwp *l, const struct linux32_sys_times_args *uap, register_t *retval) 157 { 158 /* { 159 syscallarg(linux32_tmsp_t) tms; 160 } */ 161 struct proc *p = l->l_proc; 162 struct timeval t; 163 int error; 164 165 if (SCARG_P32(uap, tms)) { 166 struct linux32_tms ltms32; 167 struct rusage ru; 168 169 memset(<ms32, 0, sizeof(ltms32)); 170 171 mutex_enter(p->p_lock); 172 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL); 173 ltms32.ltms32_utime = CONVTCK(ru.ru_utime); 174 ltms32.ltms32_stime = CONVTCK(ru.ru_stime); 175 ltms32.ltms32_cutime = CONVTCK(p->p_stats->p_cru.ru_utime); 176 ltms32.ltms32_cstime = CONVTCK(p->p_stats->p_cru.ru_stime); 177 mutex_exit(p->p_lock); 178 179 error = copyout(<ms32, SCARG_P32(uap, tms), sizeof(ltms32)); 180 if (error) 181 return error; 182 } 183 184 getmicrouptime(&t); 185 186 retval[0] = ((linux32_clock_t)(CONVTCK(t))); 187 return 0; 188 } 189 190 #undef CONVTCK 191 192 int 193 linux32_sys_stime(struct lwp *l, const struct linux32_sys_stime_args *uap, register_t *retval) 194 { 195 /* { 196 syscallarg(linux32_timep_t) t; 197 } */ 198 struct timespec ts; 199 linux32_time_t tt32; 200 int error; 201 202 if ((error = copyin(SCARG_P32(uap, t), &tt32, sizeof tt32)) != 0) 203 return error; 204 205 ts.tv_sec = (long)tt32; 206 ts.tv_nsec = 0; 207 208 return settime(l->l_proc, &ts); 209 } 210 211 int 212 linux32_sys_utime(struct lwp *l, const struct linux32_sys_utime_args *uap, register_t *retval) 213 { 214 /* { 215 syscallarg(const netbsd32_charp) path; 216 syscallarg(linux32_utimbufp_t) times; 217 } */ 218 struct timeval tv[2], *tvp; 219 struct linux32_utimbuf lut; 220 int error; 221 222 if (SCARG_P32(uap, times) != NULL) { 223 if ((error = copyin(SCARG_P32(uap, times), &lut, sizeof lut))) 224 return error; 225 226 tv[0].tv_sec = (long)lut.l_actime; 227 tv[0].tv_usec = 0; 228 tv[1].tv_sec = (long)lut.l_modtime; 229 tv[1].tv_usec = 0; 230 tvp = tv; 231 } else { 232 tvp = NULL; 233 } 234 235 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW, 236 tvp, UIO_SYSSPACE); 237 } 238 239 void 240 native_to_linux32_timespec(struct linux32_timespec *ltp, struct timespec *ntp) 241 { 242 243 memset(ltp, 0, sizeof(*ltp)); 244 ltp->tv_sec = ntp->tv_sec; 245 ltp->tv_nsec = ntp->tv_nsec; 246 } 247 248 void 249 linux32_to_native_timespec(struct timespec *ntp, struct linux32_timespec *ltp) 250 { 251 252 memset(ntp, 0, sizeof(*ntp)); 253 ntp->tv_sec = ltp->tv_sec; 254 ntp->tv_nsec = ltp->tv_nsec; 255 } 256 257 int 258 linux32_sys_nanosleep(struct lwp *l, 259 const struct linux32_sys_nanosleep_args *uap, register_t *retval) 260 { 261 /* { 262 syscallarg(linux32_timespecp_t) rqtp; 263 syscallarg(linux32_timespecp_t) rmtp; 264 } */ 265 struct timespec rqts, rmts; 266 struct linux32_timespec lrqts, lrmts; 267 int error, error1; 268 269 error = copyin(SCARG_P32(uap, rqtp), &lrqts, sizeof(lrqts)); 270 if (error != 0) 271 return error; 272 linux32_to_native_timespec(&rqts, &lrqts); 273 274 error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqts, 275 SCARG_P32(uap, rmtp) ? &rmts : NULL); 276 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 277 return error; 278 279 native_to_linux32_timespec(&lrmts, &rmts); 280 error1 = copyout(&lrmts, SCARG_P32(uap, rmtp), sizeof(lrmts)); 281 return error1 ? error1 : error; 282 } 283 284 int 285 linux32_sys_clock_settime(struct lwp *l, 286 const struct linux32_sys_clock_settime_args *uap, register_t *retval) 287 { 288 /* { 289 syscallarg(clockid_t) which; 290 syscallarg(linux32_timespecp_t) tp; 291 } */ 292 int error; 293 struct timespec ts; 294 struct linux32_timespec lts; 295 clockid_t id; 296 297 error = linux_to_native_clockid(&id, SCARG(uap, which)); 298 if (error != 0) 299 return error; 300 301 if ((error = copyin(SCARG_P32(uap, tp), <s, sizeof lts))) 302 return error; 303 304 linux32_to_native_timespec(&ts, <s); 305 return clock_settime1(l->l_proc, id, &ts, true); 306 } 307 308 int 309 linux32_sys_clock_gettime(struct lwp *l, 310 const struct linux32_sys_clock_gettime_args *uap, register_t *retval) 311 { 312 /* { 313 syscallarg(clockid_t) which; 314 syscallarg(linux32_timespecp_t) tp; 315 } */ 316 int error; 317 clockid_t id; 318 struct timespec ts; 319 struct linux32_timespec lts; 320 321 error = linux_to_native_clockid(&id, SCARG(uap, which)); 322 if (error != 0) 323 return error; 324 325 error = clock_gettime1(id, &ts); 326 if (error != 0) 327 return error; 328 329 native_to_linux32_timespec(<s, &ts); 330 return copyout(<s, SCARG_P32(uap, tp), sizeof lts); 331 } 332 333 int 334 linux32_sys_clock_getres(struct lwp *l, 335 const struct linux32_sys_clock_getres_args *uap, register_t *retval) 336 { 337 /* { 338 syscallarg(clockid_t) which; 339 syscallarg(linux32_timespecp_t) tp; 340 } */ 341 int error; 342 clockid_t id; 343 struct timespec ts; 344 struct linux32_timespec lts; 345 346 error = linux_to_native_clockid(&id, SCARG(uap, which)); 347 if (error != 0 || SCARG_P32(uap, tp) == NULL) 348 return error; 349 350 error = clock_getres1(id, &ts); 351 if (error != 0) 352 return error; 353 354 native_to_linux32_timespec(<s, &ts); 355 return copyout(<s, SCARG_P32(uap, tp), sizeof lts); 356 } 357 358 int 359 linux32_sys_clock_nanosleep(struct lwp *l, 360 const struct linux32_sys_clock_nanosleep_args *uap, register_t *retval) 361 { 362 /* { 363 syscallarg(clockid_t) which; 364 syscallarg(int) flags; 365 syscallarg(linux32_timespecp_t) rqtp; 366 syscallarg(linux32_timespecp_t) rmtp; 367 } */ 368 struct linux32_timespec lrqts, lrmts; 369 struct timespec rqts, rmts; 370 int error, error1, flags; 371 clockid_t id; 372 373 flags = SCARG(uap, flags) != 0 ? TIMER_ABSTIME : 0; 374 375 error = linux_to_native_clockid(&id, SCARG(uap, which)); 376 if (error != 0) 377 return error; 378 379 error = copyin(SCARG_P32(uap, rqtp), &lrqts, sizeof lrqts); 380 if (error != 0) 381 return error; 382 linux32_to_native_timespec(&rqts, &lrqts); 383 384 error = nanosleep1(l, id, flags, &rqts, 385 SCARG_P32(uap, rmtp) ? &rmts : NULL); 386 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 387 return error; 388 389 native_to_linux32_timespec(&lrmts, &rmts); 390 error1 = copyout(&lrmts, SCARG_P32(uap, rmtp), sizeof lrmts); 391 return error1 ? error1 : error; 392 } 393