linux_timer.c revision 293578
1275988Sngie/*-
2240116Smarcel * Copyright (c) 2014 Bjoern A. Zeeb
3240116Smarcel * All rights reserved.
4240116Smarcel *
5240116Smarcel * This software was developed by SRI International and the University of
6240116Smarcel * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249
7240116Smarcel * ("MRC2"), as part of the DARPA MRC research programme.
8240116Smarcel *
9240116Smarcel * Redistribution and use in source and binary forms, with or without
10240116Smarcel * modification, are permitted provided that the following conditions
11240116Smarcel * are met:
12240116Smarcel * 1. Redistributions of source code must retain the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer.
14240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
15240116Smarcel *    notice, this list of conditions and the following disclaimer in the
16240116Smarcel *    documentation and/or other materials provided with the distribution.
17240116Smarcel *
18240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19240116Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20240116Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21240116Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22240116Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24275988Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25240116Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26275988Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27275988Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28240116Smarcel * SUCH DAMAGE.
29240116Smarcel */
30240116Smarcel#include <sys/cdefs.h>
31240116Smarcel__FBSDID("$FreeBSD: stable/10/sys/compat/linux/linux_timer.c 293578 2016-01-09 17:31:20Z dchagin $");
32240116Smarcel
33240116Smarcel#include "opt_compat.h"
34240116Smarcel
35240116Smarcel#include <sys/param.h>
36240116Smarcel#include <sys/errno.h>
37240116Smarcel#include <sys/signal.h>
38240116Smarcel#include <sys/syscallsubr.h>
39240116Smarcel#include <sys/systm.h>
40240116Smarcel#include <sys/time.h>
41240116Smarcel#include <sys/types.h>
42240116Smarcel
43240116Smarcel#ifdef COMPAT_LINUX32
44240116Smarcel#include <machine/../linux32/linux.h>
45240116Smarcel#include <machine/../linux32/linux32_proto.h>
46240116Smarcel#else
47240116Smarcel#include <machine/../linux/linux.h>
48240116Smarcel#include <machine/../linux/linux_proto.h>
49240116Smarcel#endif
50240116Smarcel#include <compat/linux/linux_timer.h>
51240116Smarcel
52240116Smarcel
53240116Smarcelstatic int
54240116Smarcellinux_convert_l_sigevent(struct l_sigevent *l_sig, struct sigevent *sig)
55240116Smarcel{
56240116Smarcel
57240116Smarcel	CP(*l_sig, *sig, sigev_notify);
58240116Smarcel	switch (l_sig->sigev_notify) {
59240116Smarcel	case L_SIGEV_SIGNAL:
60240116Smarcel		sig->sigev_notify = SIGEV_SIGNAL;
61240116Smarcel		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
62240116Smarcel		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
63240116Smarcel		break;
64240116Smarcel	case L_SIGEV_NONE:
65240116Smarcel		sig->sigev_notify = SIGEV_NONE;
66240116Smarcel		break;
67240116Smarcel	case L_SIGEV_THREAD:
68240116Smarcel#if 0
69275988Sngie		/* Seems to not be used anywhere (anymore)? */
70		sig->sigev_notify = SIGEV_THREAD;
71		return (ENOSYS);
72#else
73		return (EINVAL);
74#endif
75	case L_SIGEV_THREAD_ID:
76		sig->sigev_notify = SIGEV_THREAD_ID;
77		CP2(*l_sig, *sig, _l_sigev_un._tid, sigev_notify_thread_id);
78		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
79		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
80		break;
81	default:
82		return (EINVAL);
83	}
84	return (0);
85}
86
87int
88linux_timer_create(struct thread *td, struct linux_timer_create_args *uap)
89{
90	struct l_sigevent l_ev;
91	struct sigevent ev, *evp;
92	clockid_t nwhich;
93	int error, id;
94
95	if (uap->evp == NULL) {
96		evp = NULL;
97	} else {
98		error = copyin(uap->evp, &l_ev, sizeof(l_ev));
99		if (error != 0)
100			return (error);
101		error = linux_convert_l_sigevent(&l_ev, &ev);
102		if (error != 0)
103			return (error);
104		evp = &ev;
105	}
106	error = linux_to_native_clockid(&nwhich, uap->clock_id);
107	if (error != 0)
108		return (error);
109	error = kern_ktimer_create(td, nwhich, evp, &id, -1);
110	if (error == 0) {
111		error = copyout(&id, uap->timerid, sizeof(int));
112		if (error != 0)
113			kern_ktimer_delete(td, id);
114	}
115	return (error);
116}
117
118int
119linux_timer_settime(struct thread *td, struct linux_timer_settime_args *uap)
120{
121	struct l_itimerspec l_val, l_oval;
122	struct itimerspec val, oval, *ovalp;
123	int error;
124
125	error = copyin(uap->new, &l_val, sizeof(l_val));
126	if (error != 0)
127		return (error);
128	ITS_CP(l_val, val);
129	ovalp = uap->old != NULL ? &oval : NULL;
130	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
131	if (error == 0 && uap->old != NULL) {
132		ITS_CP(oval, l_oval);
133		error = copyout(&l_oval, uap->old, sizeof(l_oval));
134	}
135	return (error);
136}
137
138int
139linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *uap)
140{
141	struct l_itimerspec l_val;
142	struct itimerspec val;
143	int error;
144
145	error = kern_ktimer_gettime(td, uap->timerid, &val);
146	if (error == 0) {
147		ITS_CP(val, l_val);
148		error = copyout(&l_val, uap->setting, sizeof(l_val));
149	}
150	return (error);
151}
152
153int
154linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *uap)
155{
156
157	return (kern_ktimer_getoverrun(td, uap->timerid));
158}
159
160int
161linux_timer_delete(struct thread *td, struct linux_timer_delete_args *uap)
162{
163
164	return (kern_ktimer_delete(td, uap->timerid));
165}
166