linux_timer.c revision 283476
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 283476 2015-05-24 17:49:09Z dchagin $");
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:
60271743Sbz		sig->sigev_notify = SIGEV_SIGNAL;
61283476Sdchagin		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
62271743Sbz		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
63271743Sbz		break;
64271743Sbz	case L_SIGEV_NONE:
65271743Sbz		sig->sigev_notify = SIGEV_NONE;
66271743Sbz		break;
67271743Sbz	case L_SIGEV_THREAD:
68271743Sbz#if 0
69271743Sbz		/* Seems to not be used anywhere (anymore)? */
70271743Sbz		sig->sigev_notify = SIGEV_THREAD;
71271743Sbz		return (ENOSYS);
72271743Sbz#else
73271743Sbz		return (EINVAL);
74271743Sbz#endif
75271743Sbz	case L_SIGEV_THREAD_ID:
76271743Sbz		sig->sigev_notify = SIGEV_THREAD_ID;
77271743Sbz		CP2(*l_sig, *sig, _l_sigev_un._tid, sigev_notify_thread_id);
78283476Sdchagin		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
79271743Sbz		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
80271743Sbz		break;
81271743Sbz	default:
82271743Sbz		return (EINVAL);
83271743Sbz	}
84271743Sbz	return (0);
85271743Sbz}
86271743Sbz
87271743Sbzint
88271743Sbzlinux_timer_create(struct thread *td, struct linux_timer_create_args *uap)
89271743Sbz{
90271743Sbz	struct l_sigevent l_ev;
91271743Sbz	struct sigevent ev, *evp;
92283464Sdchagin	clockid_t nwhich;
93271743Sbz	int error, id;
94271743Sbz
95271743Sbz	if (uap->evp == NULL) {
96271743Sbz		evp = NULL;
97271743Sbz	} else {
98271743Sbz		error = copyin(uap->evp, &l_ev, sizeof(l_ev));
99271743Sbz		if (error != 0)
100271743Sbz			return (error);
101271743Sbz		error = linux_convert_l_sigevent(&l_ev, &ev);
102271743Sbz		if (error != 0)
103271743Sbz			return (error);
104271743Sbz		evp = &ev;
105271743Sbz	}
106283464Sdchagin	error = linux_to_native_clockid(&nwhich, uap->clock_id);
107271743Sbz	if (error != 0)
108271743Sbz		return (error);
109283464Sdchagin	error = kern_ktimer_create(td, nwhich, evp, &id, -1);
110271743Sbz	if (error == 0) {
111271743Sbz		error = copyout(&id, uap->timerid, sizeof(int));
112271743Sbz		if (error != 0)
113271743Sbz			kern_ktimer_delete(td, id);
114271743Sbz	}
115271743Sbz	return (error);
116271743Sbz}
117271743Sbz
118271743Sbzint
119271743Sbzlinux_timer_settime(struct thread *td, struct linux_timer_settime_args *uap)
120271743Sbz{
121271743Sbz	struct l_itimerspec l_val, l_oval;
122271743Sbz	struct itimerspec val, oval, *ovalp;
123271743Sbz	int error;
124271743Sbz
125271743Sbz	error = copyin(uap->new, &l_val, sizeof(l_val));
126271743Sbz	if (error != 0)
127271743Sbz		return (error);
128271743Sbz	ITS_CP(l_val, val);
129271743Sbz	ovalp = uap->old != NULL ? &oval : NULL;
130271743Sbz	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
131271743Sbz	if (error == 0 && uap->old != NULL) {
132271743Sbz		ITS_CP(oval, l_oval);
133271743Sbz		error = copyout(&l_oval, uap->old, sizeof(l_oval));
134271743Sbz	}
135271743Sbz	return (error);
136271743Sbz}
137271743Sbz
138271743Sbzint
139271743Sbzlinux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *uap)
140271743Sbz{
141271743Sbz	struct l_itimerspec l_val;
142271743Sbz	struct itimerspec val;
143271743Sbz	int error;
144271743Sbz
145271743Sbz	error = kern_ktimer_gettime(td, uap->timerid, &val);
146271743Sbz	if (error == 0) {
147271743Sbz		ITS_CP(val, l_val);
148271743Sbz		error = copyout(&l_val, uap->setting, sizeof(l_val));
149271743Sbz	}
150271743Sbz	return (error);
151271743Sbz}
152271743Sbz
153271743Sbzint
154271743Sbzlinux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *uap)
155271743Sbz{
156271743Sbz
157271743Sbz	return (kern_ktimer_getoverrun(td, uap->timerid));
158271743Sbz}
159271743Sbz
160271743Sbzint
161271743Sbzlinux_timer_delete(struct thread *td, struct linux_timer_delete_args *uap)
162271743Sbz{
163271743Sbz
164271743Sbz	return (kern_ktimer_delete(td, uap->timerid));
165271743Sbz}
166