linux emulation - Major update
[dragonfly.git] / sys / emulation / linux / linux_time.c
blobe05fff273812ddbacabd40df58542b27964febde
1 /* $NetBSD: linux_time.c,v 1.14 2006/05/14 03:40:54 christos Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus.
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.
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.
38 #include "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/imgact_aout.h>
44 #include <sys/kernel.h>
45 #include <sys/kern_syscall.h>
46 #include <sys/lock.h>
47 #include <sys/mman.h>
48 #include <sys/poll.h>
49 #include <sys/proc.h>
50 #include <sys/priv.h>
51 #include <sys/nlookup.h>
52 #include <sys/blist.h>
53 #include <sys/resourcevar.h>
54 #include <sys/signalvar.h>
55 #include <sys/stat.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysproto.h>
58 #include <sys/time.h>
59 #include <sys/timers.h>
60 #include <sys/unistd.h>
61 #include <sys/vmmeter.h>
62 #include <sys/vnode.h>
63 #include <sys/wait.h>
65 #include <sys/signal2.h>
66 #include <sys/thread2.h>
67 #include <sys/mplock2.h>
69 #include <machine/frame.h>
70 #include <machine/limits.h>
71 #include <machine/psl.h>
72 #include <machine/sysarch.h>
73 #ifdef __i386__
74 #include <machine/segments.h>
75 #endif
77 #include <sys/sched.h>
79 #include <emulation/linux/linux_sysproto.h>
80 #include <arch_linux/linux.h>
81 #include <arch_linux/linux_proto.h>
82 #include "linux_mib.h"
83 #include "linux_util.h"
85 static void native_to_linux_timespec(struct l_timespec *,
86 struct timespec *);
87 static int linux_to_native_timespec(struct timespec *,
88 struct l_timespec *);
89 static int linux_to_native_clockid(clockid_t *, clockid_t);
91 static void
92 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
94 ltp->tv_sec = ntp->tv_sec;
95 ltp->tv_nsec = ntp->tv_nsec;
98 static int
99 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
101 if (ltp->tv_sec < 0 || ltp->tv_nsec > (l_long)999999999L)
102 return (EINVAL);
103 ntp->tv_sec = ltp->tv_sec;
104 ntp->tv_nsec = ltp->tv_nsec;
106 return (0);
109 static int
110 linux_to_native_clockid(clockid_t *n, clockid_t l)
112 switch (l) {
113 case LINUX_CLOCK_REALTIME:
114 *n = CLOCK_REALTIME;
115 break;
116 case LINUX_CLOCK_MONOTONIC:
117 *n = CLOCK_MONOTONIC;
118 break;
119 case LINUX_CLOCK_PROCESS_CPUTIME_ID:
120 case LINUX_CLOCK_THREAD_CPUTIME_ID:
121 case LINUX_CLOCK_REALTIME_HR:
122 case LINUX_CLOCK_MONOTONIC_HR:
123 default:
124 return (EINVAL);
125 break;
128 return (0);
132 sys_linux_clock_gettime(struct linux_clock_gettime_args *args)
134 struct l_timespec lts;
135 int error;
136 clockid_t nwhich = 0; /* XXX: GCC */
137 struct timespec tp;
139 error = linux_to_native_clockid(&nwhich, args->which);
140 if (error != 0)
141 return (error);
142 error = kern_clock_gettime(nwhich, &tp);
143 if (error != 0)
144 return (error);
145 native_to_linux_timespec(&lts, &tp);
147 return (copyout(&lts, args->tp, sizeof lts));
151 sys_linux_clock_settime(struct linux_clock_settime_args *args)
153 struct timespec ts;
154 struct l_timespec lts;
155 int error;
156 clockid_t nwhich = 0; /* XXX: GCC */
158 error = linux_to_native_clockid(&nwhich, args->which);
159 if (error != 0)
160 return (error);
161 error = copyin(args->tp, &lts, sizeof lts);
162 if (error != 0)
163 return (error);
164 error = linux_to_native_timespec(&ts, &lts);
165 if (error != 0)
166 return (error);
168 return (kern_clock_settime(nwhich, &ts));
172 sys_linux_clock_getres(struct linux_clock_getres_args *args)
174 struct timespec ts;
175 struct l_timespec lts;
176 int error;
177 clockid_t nwhich = 0; /* XXX: GCC */
179 if (args->tp == NULL)
180 return (0);
182 error = linux_to_native_clockid(&nwhich, args->which);
183 if (error != 0)
184 return (error);
185 error = kern_clock_getres(nwhich, &ts);
186 if (error != 0)
187 return (error);
188 native_to_linux_timespec(&lts, &ts);
190 return (copyout(&lts, args->tp, sizeof lts));
192 extern int nanosleep1(struct timespec *rqt, struct timespec *rmt);
195 sys_linux_clock_nanosleep(struct linux_clock_nanosleep_args *args)
197 struct timespec *rmtp;
198 struct l_timespec lrqts, lrmts;
199 struct timespec rqts, rmts;
200 int error;
202 if (args->flags != 0)
203 return (EINVAL); /* XXX deal with TIMER_ABSTIME */
205 if (args->which != LINUX_CLOCK_REALTIME)
206 return (EINVAL);
208 error = copyin(args->rqtp, &lrqts, sizeof lrqts);
209 if (error != 0)
210 return (error);
212 if (args->rmtp != NULL)
213 rmtp = &rmts;
214 else
215 rmtp = NULL;
217 error = linux_to_native_timespec(&rqts, &lrqts);
218 if (error != 0)
219 return (error);
220 error = nanosleep1(&rqts, rmtp);
221 if (error != 0)
222 return (error);
224 if (args->rmtp != NULL) {
225 native_to_linux_timespec(&lrmts, rmtp);
226 error = copyout(&lrmts, args->rmtp, sizeof lrmts );
227 if (error != 0)
228 return (error);
231 return (0);