sbin/hammer: Directly access volume in volume list
[dragonfly.git] / sys / emulation / linux / linux_time.c
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 #include "opt_compat.h"
39
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>
64
65 #include <sys/signal2.h>
66 #include <sys/thread2.h>
67 #include <sys/mplock2.h>
68
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
76
77 #include <sys/sched.h>
78
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"
84
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);
90
91 static void
92 native_to_linux_timespec(struct l_timespec *ltp, struct timespec *ntp)
93 {
94         ltp->tv_sec = ntp->tv_sec;
95         ltp->tv_nsec = ntp->tv_nsec;
96 }
97
98 static int
99 linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
100 {
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;
105
106         return (0);
107 }
108
109 static int
110 linux_to_native_clockid(clockid_t *n, clockid_t l)
111 {
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;
126         }
127
128         return (0);
129 }
130
131 int
132 sys_linux_clock_gettime(struct linux_clock_gettime_args *args)
133 {
134         struct l_timespec lts;
135         int error;
136         clockid_t nwhich = 0;   /* XXX: GCC */
137         struct timespec tp;
138
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);
146
147         return (copyout(&lts, args->tp, sizeof lts));
148 }
149
150 int
151 sys_linux_clock_settime(struct linux_clock_settime_args *args)
152 {
153         struct timespec ts;
154         struct l_timespec lts;
155         int error;
156         clockid_t nwhich = 0;   /* XXX: GCC */
157
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);
167
168         return (kern_clock_settime(nwhich, &ts));
169 }
170
171 int
172 sys_linux_clock_getres(struct linux_clock_getres_args *args)
173 {
174         struct timespec ts;
175         struct l_timespec lts;
176         int error;
177         clockid_t nwhich = 0;   /* XXX: GCC */
178
179         if (args->tp == NULL)
180                 return (0);
181
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);
189
190         return (copyout(&lts, args->tp, sizeof lts));
191 }
192
193 int
194 sys_linux_clock_nanosleep(struct linux_clock_nanosleep_args *args)
195 {
196         struct timespec *rmtp;
197         struct l_timespec lrqts, lrmts;
198         struct timespec rqts, rmts;
199         int error;
200
201         if (args->flags != 0)
202                 return (EINVAL);        /* XXX deal with TIMER_ABSTIME */
203
204         if (args->which != LINUX_CLOCK_REALTIME)
205                 return (EINVAL);
206
207         error = copyin(args->rqtp, &lrqts, sizeof lrqts);
208         if (error != 0)
209                 return (error);
210
211         if (args->rmtp != NULL)
212                 rmtp = &rmts;
213         else
214                 rmtp = NULL;
215
216         error = linux_to_native_timespec(&rqts, &lrqts);
217         if (error != 0)
218                 return (error);
219         error = nanosleep1(&rqts, rmtp);
220         if (error != 0)
221                 return (error);
222
223         if (args->rmtp != NULL) {
224                 native_to_linux_timespec(&lrmts, rmtp);
225                 error = copyout(&lrmts, args->rmtp, sizeof lrmts );
226                 if (error != 0)
227                         return (error);
228         }
229
230         return (0);
231 }