1 /* $NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 __KERNEL_RCSID(0, "$NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $");
38 #include "opt_compat.h"
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>
48 #include <sys/systm.h>
52 #include <machine/../linux32/linux.h>
53 #include <machine/../linux32/linux32_proto.h>
55 #include <machine/../linux/linux.h>
56 #include <machine/../linux/linux_proto.h>
59 static void native_to_linux_timespec(struct l_timespec *,
61 static int linux_to_native_timespec(struct timespec *,
63 static int linux_to_native_clockid(clockid_t *, clockid_t);
66 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
68 ltp->tv_sec = ntp->tv_sec;
69 ltp->tv_nsec = ntp->tv_nsec;
73 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
75 if (ltp->tv_sec < 0 || ltp->tv_nsec > (l_long)999999999L)
77 ntp->tv_sec = ltp->tv_sec;
78 ntp->tv_nsec = ltp->tv_nsec;
84 linux_to_native_clockid(clockid_t *n, clockid_t l)
87 case LINUX_CLOCK_REALTIME:
90 case LINUX_CLOCK_MONOTONIC:
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:
106 linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
108 struct l_timespec lts;
110 clockid_t nwhich = 0; /* XXX: GCC */
113 error = linux_to_native_clockid(&nwhich, args->which);
116 error = kern_clock_gettime(td, nwhich, &tp);
119 native_to_linux_timespec(<s, &tp);
121 return (copyout(<s, args->tp, sizeof lts));
125 linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
128 struct l_timespec lts;
130 clockid_t nwhich = 0; /* XXX: GCC */
132 error = linux_to_native_clockid(&nwhich, args->which);
135 error = copyin(args->tp, <s, sizeof lts);
138 error = linux_to_native_timespec(&ts, <s);
142 return (kern_clock_settime(td, nwhich, &ts));
146 linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
149 struct l_timespec lts;
151 clockid_t nwhich = 0; /* XXX: GCC */
153 if (args->tp == NULL)
156 error = linux_to_native_clockid(&nwhich, args->which);
159 error = kern_clock_getres(td, nwhich, &ts);
162 native_to_linux_timespec(<s, &ts);
164 return (copyout(<s, args->tp, sizeof lts));
168 linux_nanosleep(struct thread *td, struct linux_nanosleep_args *args)
170 struct timespec *rmtp;
171 struct l_timespec lrqts, lrmts;
172 struct timespec rqts, rmts;
175 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
179 if (args->rmtp != NULL)
184 error = linux_to_native_timespec(&rqts, &lrqts);
187 error = kern_nanosleep(td, &rqts, rmtp);
191 if (args->rmtp != NULL) {
192 native_to_linux_timespec(&lrmts, rmtp);
193 error = copyout(&lrmts, args->rmtp, sizeof(lrmts));
202 linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
204 struct timespec *rmtp;
205 struct l_timespec lrqts, lrmts;
206 struct timespec rqts, rmts;
209 if (args->flags != 0)
210 return (EINVAL); /* XXX deal with TIMER_ABSTIME */
212 if (args->which != LINUX_CLOCK_REALTIME)
215 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
219 if (args->rmtp != NULL)
224 error = linux_to_native_timespec(&rqts, &lrqts);
227 error = kern_nanosleep(td, &rqts, rmtp);
231 if (args->rmtp != NULL) {
232 native_to_linux_timespec(&lrmts, rmtp);
233 error = copyout(&lrmts, args->rmtp, sizeof lrmts );