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$");
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
52271743Sbz
53271743Sbzstatic int
54271743Sbzlinux_convert_l_sigevent(struct l_sigevent *l_sig, struct sigevent *sig)
55271743Sbz{
56271743Sbz
57271743Sbz	CP(*l_sig, *sig, sigev_notify);
58271743Sbz	switch (l_sig->sigev_notify) {
59271743Sbz	case L_SIGEV_SIGNAL:
60292744Sdchagin		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
61292744Sdchagin			return (EINVAL);
62271743Sbz		sig->sigev_notify = SIGEV_SIGNAL;
63283476Sdchagin		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
64271743Sbz		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
65271743Sbz		break;
66271743Sbz	case L_SIGEV_NONE:
67271743Sbz		sig->sigev_notify = SIGEV_NONE;
68271743Sbz		break;
69271743Sbz	case L_SIGEV_THREAD:
70271743Sbz#if 0
71271743Sbz		/* Seems to not be used anywhere (anymore)? */
72271743Sbz		sig->sigev_notify = SIGEV_THREAD;
73271743Sbz		return (ENOSYS);
74271743Sbz#else
75271743Sbz		return (EINVAL);
76271743Sbz#endif
77271743Sbz	case L_SIGEV_THREAD_ID:
78292744Sdchagin		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
79292744Sdchagin			return (EINVAL);
80271743Sbz		sig->sigev_notify = SIGEV_THREAD_ID;
81271743Sbz		CP2(*l_sig, *sig, _l_sigev_un._tid, sigev_notify_thread_id);
82283476Sdchagin		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
83271743Sbz		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
84271743Sbz		break;
85271743Sbz	default:
86271743Sbz		return (EINVAL);
87271743Sbz	}
88271743Sbz	return (0);
89271743Sbz}
90271743Sbz
91271743Sbzint
92271743Sbzlinux_timer_create(struct thread *td, struct linux_timer_create_args *uap)
93271743Sbz{
94271743Sbz	struct l_sigevent l_ev;
95271743Sbz	struct sigevent ev, *evp;
96283464Sdchagin	clockid_t nwhich;
97271743Sbz	int error, id;
98271743Sbz
99271743Sbz	if (uap->evp == NULL) {
100271743Sbz		evp = NULL;
101271743Sbz	} else {
102271743Sbz		error = copyin(uap->evp, &l_ev, sizeof(l_ev));
103271743Sbz		if (error != 0)
104271743Sbz			return (error);
105271743Sbz		error = linux_convert_l_sigevent(&l_ev, &ev);
106271743Sbz		if (error != 0)
107271743Sbz			return (error);
108271743Sbz		evp = &ev;
109271743Sbz	}
110283464Sdchagin	error = linux_to_native_clockid(&nwhich, uap->clock_id);
111271743Sbz	if (error != 0)
112271743Sbz		return (error);
113283464Sdchagin	error = kern_ktimer_create(td, nwhich, evp, &id, -1);
114271743Sbz	if (error == 0) {
115271743Sbz		error = copyout(&id, uap->timerid, sizeof(int));
116271743Sbz		if (error != 0)
117271743Sbz			kern_ktimer_delete(td, id);
118271743Sbz	}
119271743Sbz	return (error);
120271743Sbz}
121271743Sbz
122271743Sbzint
123271743Sbzlinux_timer_settime(struct thread *td, struct linux_timer_settime_args *uap)
124271743Sbz{
125271743Sbz	struct l_itimerspec l_val, l_oval;
126271743Sbz	struct itimerspec val, oval, *ovalp;
127271743Sbz	int error;
128271743Sbz
129271743Sbz	error = copyin(uap->new, &l_val, sizeof(l_val));
130271743Sbz	if (error != 0)
131271743Sbz		return (error);
132271743Sbz	ITS_CP(l_val, val);
133271743Sbz	ovalp = uap->old != NULL ? &oval : NULL;
134271743Sbz	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
135271743Sbz	if (error == 0 && uap->old != NULL) {
136271743Sbz		ITS_CP(oval, l_oval);
137271743Sbz		error = copyout(&l_oval, uap->old, sizeof(l_oval));
138271743Sbz	}
139271743Sbz	return (error);
140271743Sbz}
141271743Sbz
142271743Sbzint
143271743Sbzlinux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *uap)
144271743Sbz{
145271743Sbz	struct l_itimerspec l_val;
146271743Sbz	struct itimerspec val;
147271743Sbz	int error;
148271743Sbz
149271743Sbz	error = kern_ktimer_gettime(td, uap->timerid, &val);
150271743Sbz	if (error == 0) {
151271743Sbz		ITS_CP(val, l_val);
152271743Sbz		error = copyout(&l_val, uap->setting, sizeof(l_val));
153271743Sbz	}
154271743Sbz	return (error);
155271743Sbz}
156271743Sbz
157271743Sbzint
158271743Sbzlinux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *uap)
159271743Sbz{
160271743Sbz
161271743Sbz	return (kern_ktimer_getoverrun(td, uap->timerid));
162271743Sbz}
163271743Sbz
164271743Sbzint
165271743Sbzlinux_timer_delete(struct thread *td, struct linux_timer_delete_args *uap)
166271743Sbz{
167271743Sbz
168271743Sbz	return (kern_ktimer_delete(td, uap->timerid));
169271743Sbz}
170