linux_time.c revision 161304
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: head/sys/compat/linux/linux_time.c 161304 2006-08-15 12:20:59Z netchild $");
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
66static void native_to_linux_timespec(struct l_timespec *,
67				     struct timespec *);
68static void linux_to_native_timespec(struct timespec *,
69				     struct l_timespec *);
70static int linux_to_native_clockid(clockid_t *, clockid_t);
71
72static void
73native_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
79static void
80linux_to_native_timespec(struct timespec *ntp, struct l_timespec *ltp)
81{
82	ntp->tv_sec = ltp->tv_sec;
83	ntp->tv_nsec = ltp->tv_nsec;
84}
85
86static int
87linux_to_native_clockid(clockid_t *n, clockid_t l)
88{
89	switch (l) {
90	case LINUX_CLOCK_REALTIME:
91		*n = CLOCK_REALTIME;
92		break;
93	case LINUX_CLOCK_MONOTONIC:
94		*n = CLOCK_MONOTONIC;
95		break;
96	case LINUX_CLOCK_PROCESS_CPUTIME_ID:
97	case LINUX_CLOCK_THREAD_CPUTIME_ID:
98	case LINUX_CLOCK_REALTIME_HR:
99	case LINUX_CLOCK_MONOTONIC_HR:
100		return EINVAL;
101	}
102
103	return 0;
104}
105
106int
107linux_clock_gettime(struct thread *td, struct linux_clock_gettime_args *args)
108{
109	struct l_timespec lts;
110	int error;
111	clockid_t nwhich = 0;	/* XXX: GCC */
112	struct timespec tp;
113
114	error = linux_to_native_clockid(&nwhich, args->which);
115	if (error != 0)
116		return error;
117
118	error = kern_clock_gettime(td, nwhich, &tp);
119	if (error != 0)
120		return error;
121
122	native_to_linux_timespec(&lts, &tp);
123
124	return copyout(&lts, args->tp, sizeof lts);
125}
126
127int
128linux_clock_settime(struct thread *td, struct linux_clock_settime_args *args)
129{
130	struct timespec ts;
131	struct l_timespec lts;
132	int error;
133	clockid_t nwhich = 0;	/* XXX: GCC */
134
135	error = linux_to_native_clockid(&nwhich, args->which);
136	if (error != 0)
137		return error;
138
139	error = copyin(args->tp, &lts, sizeof lts);
140	if (error != 0)
141		return error;
142
143	linux_to_native_timespec(&ts, &lts);
144
145	return kern_clock_settime(td, nwhich, &ts);
146}
147
148int
149linux_clock_getres(struct thread *td, struct linux_clock_getres_args *args)
150{
151	struct timespec ts;
152	struct l_timespec lts;
153	int error;
154	clockid_t nwhich = 0;	/* XXX: GCC */
155
156	if (args->tp == NULL)
157	  	return (0);
158
159	error = linux_to_native_clockid(&nwhich, args->which);
160	if (error != 0)
161		return error;
162
163	error = kern_clock_getres(td, nwhich, &ts);
164	if (error != 0)
165		return error;
166
167	native_to_linux_timespec(&lts, &ts);
168
169	return copyout(&lts, args->tp, sizeof lts);
170}
171
172int
173linux_clock_nanosleep(struct thread *td, struct linux_clock_nanosleep_args *args)
174{
175	struct timespec *rmtp;
176	struct l_timespec lrqts, lrmts;
177	struct timespec rqts, rmts;
178	int error;
179
180	if (args->flags != 0)
181		return EINVAL;		/* XXX deal with TIMER_ABSTIME */
182
183	if (args->which != LINUX_CLOCK_REALTIME)
184		return EINVAL;
185
186	error = copyin(args->rqtp, &lrqts, sizeof lrqts);
187	if (error != 0)
188		return error;
189
190	if (args->rmtp != NULL)
191	   	rmtp = &rmts;
192	else
193	   	rmtp = NULL;
194
195	linux_to_native_timespec(&rqts, &lrqts);
196
197	error = kern_nanosleep(td, &rqts, rmtp);
198	if (error != 0)
199		return error;
200	if (args->rmtp != NULL) {
201	   	native_to_linux_timespec(&lrmts, rmtp);
202	   	error = copyout(&lrmts, args->rmtp, sizeof lrmts );
203		if (error != 0)
204		   	return error;
205	}
206
207	return 0;
208}
209