xref: /netbsd-src/sys/compat/linux/common/linux_time.c (revision a04395531661c5e8d314125d5ae77d4cbedd5d73)
1 /*	$NetBSD: linux_time.c,v 1.40 2021/09/07 11:43:04 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.40 2021/09/07 11:43:04 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 	memset(ltp, 0, sizeof(*ltp));
121 	ltp->tv_sec = ntp->tv_sec;
122 	ltp->tv_nsec = ntp->tv_nsec;
123 }
124 
125 void
126 linux_to_native_timespec(struct timespec *ntp, struct linux_timespec *ltp)
127 {
128 	memset(ntp, 0, sizeof(*ntp));
129 	ntp->tv_sec = ltp->tv_sec;
130 	ntp->tv_nsec = ltp->tv_nsec;
131 }
132 
133 int
134 linux_sys_nanosleep(struct lwp *l, const struct linux_sys_nanosleep_args *uap,
135     register_t *retval)
136 {
137 	/* {
138 		syscallarg(struct linux_timespec *) rqtp;
139 		syscallarg(struct linux_timespec *) rmtp;
140 	} */
141 	struct timespec rqts, rmts;
142 	struct linux_timespec lrqts, lrmts;
143 	int error, error1;
144 
145 	error = copyin(SCARG(uap, rqtp), &lrqts, sizeof(lrqts));
146 	if (error != 0)
147 		return error;
148 	linux_to_native_timespec(&rqts, &lrqts);
149 
150 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqts,
151 	    SCARG(uap, rmtp) ? &rmts : NULL);
152 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
153 		return error;
154 
155 	native_to_linux_timespec(&lrmts, &rmts);
156 	error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof(lrmts));
157 	return error1 ? error1 : error;
158 }
159 
160 int
161 linux_to_native_clockid(clockid_t *n, clockid_t l)
162 {
163 	switch (l) {
164 	case LINUX_CLOCK_REALTIME:
165 		*n = CLOCK_REALTIME;
166 		break;
167 	case LINUX_CLOCK_MONOTONIC:
168 		*n = CLOCK_MONOTONIC;
169 		break;
170 	case LINUX_CLOCK_PROCESS_CPUTIME_ID:
171 	case LINUX_CLOCK_THREAD_CPUTIME_ID:
172 	case LINUX_CLOCK_REALTIME_HR:
173 	case LINUX_CLOCK_MONOTONIC_HR:
174 	default:
175 		return EINVAL;
176 	}
177 
178 	return 0;
179 }
180 
181 int
182 linux_sys_clock_gettime(struct lwp *l, const struct linux_sys_clock_gettime_args *uap, register_t *retval)
183 {
184 	/* {
185 		syscallarg(clockid_t) which;
186 		syscallarg(struct linux_timespec *)tp;
187 	} */
188 	int error;
189 	clockid_t id;
190 	struct timespec ts;
191 	struct linux_timespec lts;
192 
193 	error = linux_to_native_clockid(&id, SCARG(uap, which));
194 	if (error != 0)
195 		return error;
196 
197 	error = clock_gettime1(id, &ts);
198 	if (error != 0)
199 		return error;
200 
201 	native_to_linux_timespec(&lts, &ts);
202 	return copyout(&lts, SCARG(uap, tp), sizeof lts);
203 }
204 
205 int
206 linux_sys_clock_settime(struct lwp *l, const struct linux_sys_clock_settime_args *uap, register_t *retval)
207 {
208 	/* {
209 		syscallarg(clockid_t) which;
210 		syscallarg(struct linux_timespec *)tp;
211 	} */
212 	struct timespec ts;
213 	struct linux_timespec lts;
214 	clockid_t id;
215 	int error;
216 
217 	error = linux_to_native_clockid(&id, SCARG(uap, which));
218 	if (error != 0)
219 		return error;
220 
221 	error = copyin(SCARG(uap, tp), &lts, sizeof lts);
222 	if (error != 0)
223 		return error;
224 
225 	linux_to_native_timespec(&ts, &lts);
226 
227 	return clock_settime1(l->l_proc, id, &ts, true);
228 }
229 
230 int
231 linux_sys_clock_getres(struct lwp *l, const struct linux_sys_clock_getres_args *uap, register_t *retval)
232 {
233 	/* {
234 		syscallarg(clockid_t) which;
235 		syscallarg(struct linux_timespec *)tp;
236 	} */
237 	struct timespec ts;
238 	struct linux_timespec lts;
239 	int error;
240 	clockid_t nwhich = 0;	/* XXX: GCC */
241 
242 	error = linux_to_native_clockid(&nwhich, SCARG(uap, which));
243 	if (error != 0 || SCARG(uap, tp) == NULL)
244 		return error;
245 
246 	error = clock_getres1(nwhich, &ts);
247 	if (error != 0)
248 		return error;
249 
250 	native_to_linux_timespec(&lts, &ts);
251 	return copyout(&lts, SCARG(uap, tp), sizeof lts);
252 }
253 
254 int
255 linux_sys_clock_nanosleep(struct lwp *l, const struct linux_sys_clock_nanosleep_args *uap, register_t *retval)
256 {
257 	/* {
258 		syscallarg(clockid_t) which;
259 		syscallarg(int) flags;
260 		syscallarg(struct linux_timespec) *rqtp;
261 		syscallarg(struct linux_timespec) *rmtp;
262 	} */
263 	struct linux_timespec lrqts, lrmts;
264 	struct timespec rqts, rmts;
265 	int error, error1, flags;
266 	clockid_t nwhich;
267 
268 	flags = SCARG(uap, flags) != 0 ? TIMER_ABSTIME : 0;
269 
270 	error = linux_to_native_clockid(&nwhich, SCARG(uap, which));
271 	if (error != 0)
272 		return error;
273 
274 	error = copyin(SCARG(uap, rqtp), &lrqts, sizeof lrqts);
275 	if (error != 0)
276 		return error;
277 
278 	linux_to_native_timespec(&rqts, &lrqts);
279 
280 	error = nanosleep1(l, nwhich, flags, &rqts,
281 	    SCARG(uap, rmtp) ? &rmts : NULL);
282 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
283 		return error;
284 
285 	native_to_linux_timespec(&lrmts, &rmts);
286 	error1 = copyout(&lrmts, SCARG(uap, rmtp), sizeof lrmts);
287 	return error1 ? error1 : error;
288 }
289