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