linux_timer.c revision 271743
1271743Sbz/*-
2271743Sbz * Copyright (c) 2014 Bjoern A. Zeeb
3271743Sbz * All rights reserved.
4271743Sbz *
5271743Sbz * This software was developed by SRI International and the University of
6271743Sbz * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249
7271743Sbz * ("MRC2"), as part of the DARPA MRC research programme.
8271743Sbz *
9271743Sbz * Redistribution and use in source and binary forms, with or without
10271743Sbz * modification, are permitted provided that the following conditions
11271743Sbz * are met:
12271743Sbz * 1. Redistributions of source code must retain the above copyright
13271743Sbz *    notice, this list of conditions and the following disclaimer.
14271743Sbz * 2. Redistributions in binary form must reproduce the above copyright
15271743Sbz *    notice, this list of conditions and the following disclaimer in the
16271743Sbz *    documentation and/or other materials provided with the distribution.
17271743Sbz *
18271743Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19271743Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20271743Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21271743Sbz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22271743Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23271743Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24271743Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25271743Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26271743Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27271743Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28271743Sbz * SUCH DAMAGE.
29271743Sbz */
30271743Sbz#include <sys/cdefs.h>
31271743Sbz__FBSDID("$FreeBSD: head/sys/compat/linux/linux_timer.c 271743 2014-09-18 08:36:45Z bz $");
32271743Sbz
33271743Sbz#include "opt_compat.h"
34271743Sbz
35271743Sbz#include <sys/param.h>
36271743Sbz#include <sys/errno.h>
37271743Sbz#include <sys/signal.h>
38271743Sbz#include <sys/syscallsubr.h>
39271743Sbz#include <sys/systm.h>
40271743Sbz#include <sys/time.h>
41271743Sbz#include <sys/types.h>
42271743Sbz
43271743Sbz#ifdef COMPAT_LINUX32
44271743Sbz#include <machine/../linux32/linux.h>
45271743Sbz#include <machine/../linux32/linux32_proto.h>
46271743Sbz#else
47271743Sbz#include <machine/../linux/linux.h>
48271743Sbz#include <machine/../linux/linux_proto.h>
49271743Sbz#endif
50271743Sbz#include <compat/linux/linux_timer.h>
51271743Sbz
52271743Sbzstatic int
53271743Sbzlinux_convert_l_clockid(clockid_t *clock_id)
54271743Sbz{
55271743Sbz
56271743Sbz	switch (*clock_id) {
57271743Sbz	case LINUX_CLOCK_REALTIME:
58271743Sbz		*clock_id = CLOCK_REALTIME;
59271743Sbz		break;
60271743Sbz	case LINUX_CLOCK_MONOTONIC:
61271743Sbz		*clock_id = CLOCK_MONOTONIC;
62271743Sbz		break;
63271743Sbz	default:
64271743Sbz		return (EINVAL);
65271743Sbz	}
66271743Sbz
67271743Sbz	return (0);
68271743Sbz}
69271743Sbz
70271743Sbzstatic int
71271743Sbzlinux_convert_l_sigevent(struct l_sigevent *l_sig, struct sigevent *sig)
72271743Sbz{
73271743Sbz
74271743Sbz	CP(*l_sig, *sig, sigev_notify);
75271743Sbz	switch (l_sig->sigev_notify) {
76271743Sbz	case L_SIGEV_SIGNAL:
77271743Sbz		sig->sigev_notify = SIGEV_SIGNAL;
78271743Sbz		CP(*l_sig, *sig, sigev_signo);
79271743Sbz		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
80271743Sbz		break;
81271743Sbz	case L_SIGEV_NONE:
82271743Sbz		sig->sigev_notify = SIGEV_NONE;
83271743Sbz		break;
84271743Sbz	case L_SIGEV_THREAD:
85271743Sbz#if 0
86271743Sbz		/* Seems to not be used anywhere (anymore)? */
87271743Sbz		sig->sigev_notify = SIGEV_THREAD;
88271743Sbz		return (ENOSYS);
89271743Sbz#else
90271743Sbz		return (EINVAL);
91271743Sbz#endif
92271743Sbz	case L_SIGEV_THREAD_ID:
93271743Sbz		sig->sigev_notify = SIGEV_THREAD_ID;
94271743Sbz		CP2(*l_sig, *sig, _l_sigev_un._tid, sigev_notify_thread_id);
95271743Sbz		CP(*l_sig, *sig, sigev_signo);
96271743Sbz		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
97271743Sbz		break;
98271743Sbz	default:
99271743Sbz		return (EINVAL);
100271743Sbz	}
101271743Sbz	return (0);
102271743Sbz}
103271743Sbz
104271743Sbzint
105271743Sbzlinux_timer_create(struct thread *td, struct linux_timer_create_args *uap)
106271743Sbz{
107271743Sbz	struct l_sigevent l_ev;
108271743Sbz	struct sigevent ev, *evp;
109271743Sbz	int error, id;
110271743Sbz
111271743Sbz	if (uap->evp == NULL) {
112271743Sbz		evp = NULL;
113271743Sbz	} else {
114271743Sbz		error = copyin(uap->evp, &l_ev, sizeof(l_ev));
115271743Sbz		if (error != 0)
116271743Sbz			return (error);
117271743Sbz		error = linux_convert_l_sigevent(&l_ev, &ev);
118271743Sbz		if (error != 0)
119271743Sbz			return (error);
120271743Sbz		evp = &ev;
121271743Sbz	}
122271743Sbz	error = linux_convert_l_clockid(&uap->clock_id);
123271743Sbz	if (error != 0)
124271743Sbz		return (error);
125271743Sbz	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
126271743Sbz	if (error == 0) {
127271743Sbz		error = copyout(&id, uap->timerid, sizeof(int));
128271743Sbz		if (error != 0)
129271743Sbz			kern_ktimer_delete(td, id);
130271743Sbz	}
131271743Sbz	return (error);
132271743Sbz}
133271743Sbz
134271743Sbzint
135271743Sbzlinux_timer_settime(struct thread *td, struct linux_timer_settime_args *uap)
136271743Sbz{
137271743Sbz	struct l_itimerspec l_val, l_oval;
138271743Sbz	struct itimerspec val, oval, *ovalp;
139271743Sbz	int error;
140271743Sbz
141271743Sbz	error = copyin(uap->new, &l_val, sizeof(l_val));
142271743Sbz	if (error != 0)
143271743Sbz		return (error);
144271743Sbz	ITS_CP(l_val, val);
145271743Sbz	ovalp = uap->old != NULL ? &oval : NULL;
146271743Sbz	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
147271743Sbz	if (error == 0 && uap->old != NULL) {
148271743Sbz		ITS_CP(oval, l_oval);
149271743Sbz		error = copyout(&l_oval, uap->old, sizeof(l_oval));
150271743Sbz	}
151271743Sbz	return (error);
152271743Sbz}
153271743Sbz
154271743Sbzint
155271743Sbzlinux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *uap)
156271743Sbz{
157271743Sbz	struct l_itimerspec l_val;
158271743Sbz	struct itimerspec val;
159271743Sbz	int error;
160271743Sbz
161271743Sbz	error = kern_ktimer_gettime(td, uap->timerid, &val);
162271743Sbz	if (error == 0) {
163271743Sbz		ITS_CP(val, l_val);
164271743Sbz		error = copyout(&l_val, uap->setting, sizeof(l_val));
165271743Sbz	}
166271743Sbz	return (error);
167271743Sbz}
168271743Sbz
169271743Sbzint
170271743Sbzlinux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *uap)
171271743Sbz{
172271743Sbz
173271743Sbz	return (kern_ktimer_getoverrun(td, uap->timerid));
174271743Sbz}
175271743Sbz
176271743Sbzint
177271743Sbzlinux_timer_delete(struct thread *td, struct linux_timer_delete_args *uap)
178271743Sbz{
179271743Sbz
180271743Sbz	return (kern_ktimer_delete(td, uap->timerid));
181271743Sbz}
182271743Sbz
183