xref: /freebsd/sys/compat/linux/linux_time.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*	$NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos 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 __FBSDID("$FreeBSD$");
34 #if 0
35 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $");
36 #endif
37 
38 #include "opt_compat.h"
39 
40 #include <sys/param.h>
41 #include <sys/ucred.h>
42 #include <sys/mount.h>
43 #include <sys/signal.h>
44 #include <sys/stdint.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysproto.h>
47 #include <sys/time.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 
51 #ifdef COMPAT_LINUX32
52 #include <machine/../linux32/linux.h>
53 #include <machine/../linux32/linux32_proto.h>
54 #else
55 #include <machine/../linux/linux.h>
56 #include <machine/../linux/linux_proto.h>
57 #endif
58 
59 static void native_to_linux_timespec(struct l_timespec *,
60 				     struct timespec *);
61 static int linux_to_native_timespec(struct timespec *,
62 				     struct l_timespec *);
63 static int linux_to_native_clockid(clockid_t *, clockid_t);
64 
65 static void
66 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
67 {
68 	ltp->tv_sec = ntp->tv_sec;
69 	ltp->tv_nsec = ntp->tv_nsec;
70 }
71 
72 static int
73 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
74 {
75 	if (ltp->tv_sec < 0 || ltp->tv_nsec > (l_long)999999999L)
76 		return (EINVAL);
77 	ntp->tv_sec = ltp->tv_sec;
78 	ntp->tv_nsec = ltp->tv_nsec;
79 
80 	return (0);
81 }
82 
83 static int
84 linux_to_native_clockid(clockid_t *n, clockid_t l)
85 {
86 	switch (l) {
87 	case LINUX_CLOCK_REALTIME:
88 		*n = CLOCK_REALTIME;
89 		break;
90 	case LINUX_CLOCK_MONOTONIC:
91 		*n = CLOCK_MONOTONIC;
92 		break;
93 	case LINUX_CLOCK_PROCESS_CPUTIME_ID:
94 	case LINUX_CLOCK_THREAD_CPUTIME_ID:
95 	case LINUX_CLOCK_REALTIME_HR:
96 	case LINUX_CLOCK_MONOTONIC_HR:
97 	default:
98 		return (EINVAL);
99 		break;
100 	}
101 
102 	return (0);
103 }
104 
105 int
106 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
107 {
108 	struct l_timespec lts;
109 	int error;
110 	clockid_t nwhich = 0;	/* XXX: GCC */
111 	struct timespec tp;
112 
113 	error = linux_to_native_clockid(&nwhich, args->which);
114 	if (error != 0)
115 		return (error);
116 	error = kern_clock_gettime(td, nwhich, &tp);
117 	if (error != 0)
118 		return (error);
119 	native_to_linux_timespec(&lts, &tp);
120 
121 	return (copyout(&lts, args->tp, sizeof lts));
122 }
123 
124 int
125 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
126 {
127 	struct timespec ts;
128 	struct l_timespec lts;
129 	int error;
130 	clockid_t nwhich = 0;	/* XXX: GCC */
131 
132 	error = linux_to_native_clockid(&nwhich, args->which);
133 	if (error != 0)
134 		return (error);
135 	error = copyin(args->tp, &lts, sizeof lts);
136 	if (error != 0)
137 		return (error);
138 	error = linux_to_native_timespec(&ts, &lts);
139 	if (error != 0)
140 		return (error);
141 
142 	return (kern_clock_settime(td, nwhich, &ts));
143 }
144 
145 int
146 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
147 {
148 	struct timespec ts;
149 	struct l_timespec lts;
150 	int error;
151 	clockid_t nwhich = 0;	/* XXX: GCC */
152 
153 	if (args->tp == NULL)
154 	  	return (0);
155 
156 	error = linux_to_native_clockid(&nwhich, args->which);
157 	if (error != 0)
158 		return (error);
159 	error = kern_clock_getres(td, nwhich, &ts);
160 	if (error != 0)
161 		return (error);
162 	native_to_linux_timespec(&lts, &ts);
163 
164 	return (copyout(&lts, args->tp, sizeof lts));
165 }
166 
167 int
168 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
169 {
170 	struct timespec *rmtp;
171 	struct l_timespec lrqts, lrmts;
172 	struct timespec rqts, rmts;
173 	int error;
174 
175 	error = copyin(args->rqtp, &lrqts, sizeof lrqts);
176 	if (error != 0)
177 		return (error);
178 
179 	if (args->rmtp != NULL)
180 	   	rmtp = &rmts;
181 	else
182 	   	rmtp = NULL;
183 
184 	error = linux_to_native_timespec(&rqts, &lrqts);
185 	if (error != 0)
186 		return (error);
187 	error = kern_nanosleep(td, &rqts, rmtp);
188 	if (error != 0)
189 		return (error);
190 
191 	if (args->rmtp != NULL) {
192 	   	native_to_linux_timespec(&lrmts, rmtp);
193 	   	error = copyout(&lrmts, args->rmtp, sizeof(lrmts));
194 		if (error != 0)
195 		   	return (error);
196 	}
197 
198 	return (0);
199 }
200 
201 int
202 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
203 {
204 	struct timespec *rmtp;
205 	struct l_timespec lrqts, lrmts;
206 	struct timespec rqts, rmts;
207 	int error;
208 
209 	if (args->flags != 0)
210 		return (EINVAL);	/* XXX deal with TIMER_ABSTIME */
211 
212 	if (args->which != LINUX_CLOCK_REALTIME)
213 		return (EINVAL);
214 
215 	error = copyin(args->rqtp, &lrqts, sizeof lrqts);
216 	if (error != 0)
217 		return (error);
218 
219 	if (args->rmtp != NULL)
220 	   	rmtp = &rmts;
221 	else
222 	   	rmtp = NULL;
223 
224 	error = linux_to_native_timespec(&rqts, &lrqts);
225 	if (error != 0)
226 		return (error);
227 	error = kern_nanosleep(td, &rqts, rmtp);
228 	if (error != 0)
229 		return (error);
230 
231 	if (args->rmtp != NULL) {
232 	   	native_to_linux_timespec(&lrmts, rmtp);
233 	   	error = copyout(&lrmts, args->rmtp, sizeof lrmts );
234 		if (error != 0)
235 		   	return (error);
236 	}
237 
238 	return (0);
239 }
240