xref: /freebsd/sys/compat/linux/linux_timer.c (revision bdd1243d)
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/proc.h>
35 #include <sys/signal.h>
36 #include <sys/syscallsubr.h>
37 #include <sys/time.h>
38 
39 #ifdef COMPAT_LINUX32
40 #include <machine/../linux32/linux.h>
41 #include <machine/../linux32/linux32_proto.h>
42 #else
43 #include <machine/../linux/linux.h>
44 #include <machine/../linux/linux_proto.h>
45 #endif
46 #include <compat/linux/linux_time.h>
47 
48 static int
49 linux_convert_l_sigevent(struct l_sigevent *l_sig, struct sigevent *sig)
50 {
51 
52 	CP(*l_sig, *sig, sigev_notify);
53 	switch (l_sig->sigev_notify) {
54 	case L_SIGEV_SIGNAL:
55 		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
56 			return (EINVAL);
57 		sig->sigev_notify = SIGEV_SIGNAL;
58 		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
59 		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
60 		break;
61 	case L_SIGEV_NONE:
62 		sig->sigev_notify = SIGEV_NONE;
63 		break;
64 	case L_SIGEV_THREAD:
65 #if 0
66 		/* Seems to not be used anywhere (anymore)? */
67 		sig->sigev_notify = SIGEV_THREAD;
68 		return (ENOSYS);
69 #else
70 		return (EINVAL);
71 #endif
72 	case L_SIGEV_THREAD_ID:
73 		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
74 			return (EINVAL);
75 		sig->sigev_notify = SIGEV_THREAD_ID;
76 		CP2(*l_sig, *sig, _l_sigev_un._tid, sigev_notify_thread_id);
77 		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
78 		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
79 		break;
80 	default:
81 		return (EINVAL);
82 	}
83 	return (0);
84 }
85 
86 int
87 linux_timer_create(struct thread *td, struct linux_timer_create_args *uap)
88 {
89 	struct l_sigevent l_ev;
90 	struct sigevent ev, *evp;
91 	clockid_t nwhich;
92 	int error, id;
93 
94 	if (uap->evp == NULL) {
95 		evp = NULL;
96 	} else {
97 		error = copyin(uap->evp, &l_ev, sizeof(l_ev));
98 		if (error != 0)
99 			return (error);
100 		error = linux_convert_l_sigevent(&l_ev, &ev);
101 		if (error != 0)
102 			return (error);
103 		evp = &ev;
104 	}
105 	error = linux_to_native_clockid(&nwhich, uap->clock_id);
106 	if (error != 0)
107 		return (error);
108 	error = kern_ktimer_create(td, nwhich, evp, &id, -1);
109 	if (error == 0) {
110 		error = copyout(&id, uap->timerid, sizeof(int));
111 		if (error != 0)
112 			kern_ktimer_delete(td, id);
113 	}
114 	return (error);
115 }
116 
117 int
118 linux_timer_settime(struct thread *td, struct linux_timer_settime_args *uap)
119 {
120 	struct l_itimerspec l_val, l_oval;
121 	struct itimerspec val, oval, *ovalp;
122 	int flags, error;
123 
124 	error = copyin(uap->new, &l_val, sizeof(l_val));
125 	if (error != 0)
126 		return (error);
127 	error = linux_to_native_itimerspec(&val, &l_val);
128 	if (error != 0)
129 		return (error);
130 	ovalp = uap->old != NULL ? &oval : NULL;
131 	error = linux_to_native_timerflags(&flags, uap->flags);
132 	if (error != 0)
133 		return (error);
134 	error = kern_ktimer_settime(td, uap->timerid, flags, &val, ovalp);
135 	if (error == 0 && uap->old != NULL) {
136 		error = native_to_linux_itimerspec(&l_val, &val);
137 		if (error == 0)
138 			error = copyout(&l_oval, uap->old, sizeof(l_oval));
139 	}
140 	return (error);
141 }
142 
143 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
144 int
145 linux_timer_settime64(struct thread *td, struct linux_timer_settime64_args *uap)
146 {
147 	struct l_itimerspec64 l_val, l_oval;
148 	struct itimerspec val, oval, *ovalp;
149 	int flags, error;
150 
151 	error = copyin(uap->new, &l_val, sizeof(l_val));
152 	if (error != 0)
153 		return (error);
154 	error = linux_to_native_itimerspec64(&val, &l_val);
155 	if (error != 0)
156 		return (error);
157 	ovalp = uap->old != NULL ? &oval : NULL;
158 	error = linux_to_native_timerflags(&flags, uap->flags);
159 	if (error != 0)
160 		return (error);
161 	error = kern_ktimer_settime(td, uap->timerid, flags, &val, ovalp);
162 	if (error == 0 && uap->old != NULL) {
163 		error = native_to_linux_itimerspec64(&l_val, &val);
164 		if (error == 0)
165 			error = copyout(&l_oval, uap->old, sizeof(l_oval));
166 	}
167 	return (error);
168 }
169 #endif
170 
171 int
172 linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *uap)
173 {
174 	struct l_itimerspec l_val;
175 	struct itimerspec val;
176 	int error;
177 
178 	error = kern_ktimer_gettime(td, uap->timerid, &val);
179 	if (error == 0)
180 		error = native_to_linux_itimerspec(&l_val, &val);
181 	if (error == 0)
182 		error = copyout(&l_val, uap->setting, sizeof(l_val));
183 	return (error);
184 }
185 
186 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
187 int
188 linux_timer_gettime64(struct thread *td, struct linux_timer_gettime64_args *uap)
189 {
190 	struct l_itimerspec64 l_val;
191 	struct itimerspec val;
192 	int error;
193 
194 	error = kern_ktimer_gettime(td, uap->timerid, &val);
195 	if (error == 0)
196 		error = native_to_linux_itimerspec64(&l_val, &val);
197 	if (error == 0)
198 		error = copyout(&l_val, uap->setting, sizeof(l_val));
199 	return (error);
200 }
201 #endif
202 
203 int
204 linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *uap)
205 {
206 
207 	return (kern_ktimer_getoverrun(td, uap->timerid));
208 }
209 
210 int
211 linux_timer_delete(struct thread *td, struct linux_timer_delete_args *uap)
212 {
213 
214 	return (kern_ktimer_delete(td, uap->timerid));
215 }
216