xref: /freebsd/sys/compat/linux/linux_timer.c (revision f20058955c9d1041a5a09cd02a27babda7e34dc8)
1 /*-
2  * Copyright (c) 2014 Bjoern A. Zeeb
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249
7  * ("MRC2"), as part of the DARPA MRC research programme.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/errno.h>
35 #include <sys/signal.h>
36 #include <sys/syscallsubr.h>
37 #include <sys/systm.h>
38 #include <sys/time.h>
39 #include <sys/types.h>
40 
41 #ifdef COMPAT_LINUX32
42 #include <machine/../linux32/linux.h>
43 #include <machine/../linux32/linux32_proto.h>
44 #else
45 #include <machine/../linux/linux.h>
46 #include <machine/../linux/linux_proto.h>
47 #endif
48 #include <compat/linux/linux_timer.h>
49 
50 static int
51 linux_convert_l_sigevent(struct l_sigevent *l_sig, struct sigevent *sig)
52 {
53 
54 	CP(*l_sig, *sig, sigev_notify);
55 	switch (l_sig->sigev_notify) {
56 	case L_SIGEV_SIGNAL:
57 		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
58 			return (EINVAL);
59 		sig->sigev_notify = SIGEV_SIGNAL;
60 		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
61 		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
62 		break;
63 	case L_SIGEV_NONE:
64 		sig->sigev_notify = SIGEV_NONE;
65 		break;
66 	case L_SIGEV_THREAD:
67 #if 0
68 		/* Seems to not be used anywhere (anymore)? */
69 		sig->sigev_notify = SIGEV_THREAD;
70 		return (ENOSYS);
71 #else
72 		return (EINVAL);
73 #endif
74 	case L_SIGEV_THREAD_ID:
75 		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
76 			return (EINVAL);
77 		sig->sigev_notify = SIGEV_THREAD_ID;
78 		CP2(*l_sig, *sig, _l_sigev_un._tid, sigev_notify_thread_id);
79 		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
80 		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
81 		break;
82 	default:
83 		return (EINVAL);
84 	}
85 	return (0);
86 }
87 
88 int
89 linux_timer_create(struct thread *td, struct linux_timer_create_args *uap)
90 {
91 	struct l_sigevent l_ev;
92 	struct sigevent ev, *evp;
93 	clockid_t nwhich;
94 	int error, id;
95 
96 	if (uap->evp == NULL) {
97 		evp = NULL;
98 	} else {
99 		error = copyin(uap->evp, &l_ev, sizeof(l_ev));
100 		if (error != 0)
101 			return (error);
102 		error = linux_convert_l_sigevent(&l_ev, &ev);
103 		if (error != 0)
104 			return (error);
105 		evp = &ev;
106 	}
107 	error = linux_to_native_clockid(&nwhich, uap->clock_id);
108 	if (error != 0)
109 		return (error);
110 	error = kern_ktimer_create(td, nwhich, evp, &id, -1);
111 	if (error == 0) {
112 		error = copyout(&id, uap->timerid, sizeof(int));
113 		if (error != 0)
114 			kern_ktimer_delete(td, id);
115 	}
116 	return (error);
117 }
118 
119 int
120 linux_timer_settime(struct thread *td, struct linux_timer_settime_args *uap)
121 {
122 	struct l_itimerspec l_val, l_oval;
123 	struct itimerspec val, oval, *ovalp;
124 	int flags, error;
125 
126 	error = copyin(uap->new, &l_val, sizeof(l_val));
127 	if (error != 0)
128 		return (error);
129 	error = linux_to_native_itimerspec(&val, &l_val);
130 	if (error != 0)
131 		return (error);
132 	ovalp = uap->old != NULL ? &oval : NULL;
133 	error = linux_to_native_timerflags(&flags, uap->flags);
134 	if (error != 0)
135 		return (error);
136 	error = kern_ktimer_settime(td, uap->timerid, flags, &val, ovalp);
137 	if (error == 0 && uap->old != NULL) {
138 		error = native_to_linux_itimerspec(&l_val, &val);
139 		if (error == 0)
140 			error = copyout(&l_oval, uap->old, sizeof(l_oval));
141 	}
142 	return (error);
143 }
144 
145 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
146 int
147 linux_timer_settime64(struct thread *td, struct linux_timer_settime64_args *uap)
148 {
149 	struct l_itimerspec64 l_val, l_oval;
150 	struct itimerspec val, oval, *ovalp;
151 	int flags, error;
152 
153 	error = copyin(uap->new, &l_val, sizeof(l_val));
154 	if (error != 0)
155 		return (error);
156 	error = linux_to_native_itimerspec64(&val, &l_val);
157 	if (error != 0)
158 		return (error);
159 	ovalp = uap->old != NULL ? &oval : NULL;
160 	error = linux_to_native_timerflags(&flags, uap->flags);
161 	if (error != 0)
162 		return (error);
163 	error = kern_ktimer_settime(td, uap->timerid, flags, &val, ovalp);
164 	if (error == 0 && uap->old != NULL) {
165 		error = native_to_linux_itimerspec64(&l_val, &val);
166 		if (error == 0)
167 			error = copyout(&l_oval, uap->old, sizeof(l_oval));
168 	}
169 	return (error);
170 }
171 #endif
172 
173 int
174 linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *uap)
175 {
176 	struct l_itimerspec l_val;
177 	struct itimerspec val;
178 	int error;
179 
180 	error = kern_ktimer_gettime(td, uap->timerid, &val);
181 	if (error == 0)
182 		error = native_to_linux_itimerspec(&l_val, &val);
183 	if (error == 0)
184 		error = copyout(&l_val, uap->setting, sizeof(l_val));
185 	return (error);
186 }
187 
188 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
189 int
190 linux_timer_gettime64(struct thread *td, struct linux_timer_gettime64_args *uap)
191 {
192 	struct l_itimerspec64 l_val;
193 	struct itimerspec val;
194 	int error;
195 
196 	error = kern_ktimer_gettime(td, uap->timerid, &val);
197 	if (error == 0)
198 		error = native_to_linux_itimerspec64(&l_val, &val);
199 	if (error == 0)
200 		error = copyout(&l_val, uap->setting, sizeof(l_val));
201 	return (error);
202 }
203 #endif
204 
205 int
206 linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *uap)
207 {
208 
209 	return (kern_ktimer_getoverrun(td, uap->timerid));
210 }
211 
212 int
213 linux_timer_delete(struct thread *td, struct linux_timer_delete_args *uap)
214 {
215 
216 	return (kern_ktimer_delete(td, uap->timerid));
217 }
218