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.
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>
51 #include <sys/nlookup.h>
52 #include <sys/blist.h>
53 #include <sys/resourcevar.h>
54 #include <sys/signalvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysproto.h>
59 #include <sys/timers.h>
60 #include <sys/unistd.h>
61 #include <sys/vmmeter.h>
62 #include <sys/vnode.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>
74 #include <machine/segments.h>
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
*,
87 static int linux_to_native_timespec(struct timespec
*,
89 static int linux_to_native_clockid(clockid_t
*, clockid_t
);
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
;
99 linux_to_native_timespec(struct timespec
*ntp
, struct l_timespec
*ltp
)
101 if (ltp
->tv_sec
< 0 || ltp
->tv_nsec
> (l_long
)999999999L)
103 ntp
->tv_sec
= ltp
->tv_sec
;
104 ntp
->tv_nsec
= ltp
->tv_nsec
;
110 linux_to_native_clockid(clockid_t
*n
, clockid_t l
)
113 case LINUX_CLOCK_REALTIME
:
116 case LINUX_CLOCK_MONOTONIC
:
117 *n
= CLOCK_MONOTONIC
;
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
:
132 sys_linux_clock_gettime(struct linux_clock_gettime_args
*args
)
134 struct l_timespec lts
;
136 clockid_t nwhich
= 0; /* XXX: GCC */
139 error
= linux_to_native_clockid(&nwhich
, args
->which
);
142 error
= kern_clock_gettime(nwhich
, &tp
);
145 native_to_linux_timespec(<s
, &tp
);
147 return (copyout(<s
, args
->tp
, sizeof lts
));
151 sys_linux_clock_settime(struct linux_clock_settime_args
*args
)
154 struct l_timespec lts
;
156 clockid_t nwhich
= 0; /* XXX: GCC */
158 error
= linux_to_native_clockid(&nwhich
, args
->which
);
161 error
= copyin(args
->tp
, <s
, sizeof lts
);
164 error
= linux_to_native_timespec(&ts
, <s
);
168 return (kern_clock_settime(nwhich
, &ts
));
172 sys_linux_clock_getres(struct linux_clock_getres_args
*args
)
175 struct l_timespec lts
;
177 clockid_t nwhich
= 0; /* XXX: GCC */
179 if (args
->tp
== NULL
)
182 error
= linux_to_native_clockid(&nwhich
, args
->which
);
185 error
= kern_clock_getres(nwhich
, &ts
);
188 native_to_linux_timespec(<s
, &ts
);
190 return (copyout(<s
, 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
;
202 if (args
->flags
!= 0)
203 return (EINVAL
); /* XXX deal with TIMER_ABSTIME */
205 if (args
->which
!= LINUX_CLOCK_REALTIME
)
208 error
= copyin(args
->rqtp
, &lrqts
, sizeof lrqts
);
212 if (args
->rmtp
!= NULL
)
217 error
= linux_to_native_timespec(&rqts
, &lrqts
);
220 error
= nanosleep1(&rqts
, rmtp
);
224 if (args
->rmtp
!= NULL
) {
225 native_to_linux_timespec(&lrmts
, rmtp
);
226 error
= copyout(&lrmts
, args
->rmtp
, sizeof lrmts
);