xref: /freebsd/sys/compat/linux/linux_timer.c (revision 270e01d4686ea99ac9760a2cebb84d848352e28b)
10a041f3bSBjoern A. Zeeb /*-
20a041f3bSBjoern A. Zeeb  * Copyright (c) 2014 Bjoern A. Zeeb
30a041f3bSBjoern A. Zeeb  * All rights reserved.
40a041f3bSBjoern A. Zeeb  *
50a041f3bSBjoern A. Zeeb  * This software was developed by SRI International and the University of
60a041f3bSBjoern A. Zeeb  * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249
70a041f3bSBjoern A. Zeeb  * ("MRC2"), as part of the DARPA MRC research programme.
80a041f3bSBjoern A. Zeeb  *
90a041f3bSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
100a041f3bSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
110a041f3bSBjoern A. Zeeb  * are met:
120a041f3bSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
130a041f3bSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
140a041f3bSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
150a041f3bSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
160a041f3bSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
170a041f3bSBjoern A. Zeeb  *
180a041f3bSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
190a041f3bSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
200a041f3bSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
210a041f3bSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
220a041f3bSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
230a041f3bSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
240a041f3bSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
250a041f3bSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
260a041f3bSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
270a041f3bSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
280a041f3bSBjoern A. Zeeb  * SUCH DAMAGE.
290a041f3bSBjoern A. Zeeb  */
30*270e01d4SDmitry Chagin 
310a041f3bSBjoern A. Zeeb #include <sys/cdefs.h>
320a041f3bSBjoern A. Zeeb #include <sys/param.h>
33d8e53d94SDmitry Chagin #include <sys/proc.h>
340a041f3bSBjoern A. Zeeb #include <sys/signal.h>
350a041f3bSBjoern A. Zeeb #include <sys/syscallsubr.h>
360a041f3bSBjoern A. Zeeb #include <sys/time.h>
370a041f3bSBjoern A. Zeeb 
380a041f3bSBjoern A. Zeeb #ifdef COMPAT_LINUX32
390a041f3bSBjoern A. Zeeb #include <machine/../linux32/linux.h>
400a041f3bSBjoern A. Zeeb #include <machine/../linux32/linux32_proto.h>
410a041f3bSBjoern A. Zeeb #else
420a041f3bSBjoern A. Zeeb #include <machine/../linux/linux.h>
430a041f3bSBjoern A. Zeeb #include <machine/../linux/linux_proto.h>
440a041f3bSBjoern A. Zeeb #endif
45c8a79231SDmitry Chagin #include <compat/linux/linux_time.h>
460a041f3bSBjoern A. Zeeb 
470a041f3bSBjoern A. Zeeb static int
480a041f3bSBjoern A. Zeeb linux_convert_l_sigevent(struct l_sigevent *l_sig, struct sigevent *sig)
490a041f3bSBjoern A. Zeeb {
500a041f3bSBjoern A. Zeeb 
510a041f3bSBjoern A. Zeeb 	CP(*l_sig, *sig, sigev_notify);
520a041f3bSBjoern A. Zeeb 	switch (l_sig->sigev_notify) {
530a041f3bSBjoern A. Zeeb 	case L_SIGEV_SIGNAL:
54bfb5568aSDmitry Chagin 		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
55bfb5568aSDmitry Chagin 			return (EINVAL);
560a041f3bSBjoern A. Zeeb 		sig->sigev_notify = SIGEV_SIGNAL;
575885e5abSDmitry Chagin 		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
580a041f3bSBjoern A. Zeeb 		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
590a041f3bSBjoern A. Zeeb 		break;
600a041f3bSBjoern A. Zeeb 	case L_SIGEV_NONE:
610a041f3bSBjoern A. Zeeb 		sig->sigev_notify = SIGEV_NONE;
620a041f3bSBjoern A. Zeeb 		break;
630a041f3bSBjoern A. Zeeb 	case L_SIGEV_THREAD:
640a041f3bSBjoern A. Zeeb #if 0
650a041f3bSBjoern A. Zeeb 		/* Seems to not be used anywhere (anymore)? */
660a041f3bSBjoern A. Zeeb 		sig->sigev_notify = SIGEV_THREAD;
670a041f3bSBjoern A. Zeeb 		return (ENOSYS);
680a041f3bSBjoern A. Zeeb #else
690a041f3bSBjoern A. Zeeb 		return (EINVAL);
700a041f3bSBjoern A. Zeeb #endif
710a041f3bSBjoern A. Zeeb 	case L_SIGEV_THREAD_ID:
72bfb5568aSDmitry Chagin 		if (!LINUX_SIG_VALID(l_sig->sigev_signo))
73bfb5568aSDmitry Chagin 			return (EINVAL);
740a041f3bSBjoern A. Zeeb 		sig->sigev_notify = SIGEV_THREAD_ID;
750a041f3bSBjoern A. Zeeb 		CP2(*l_sig, *sig, _l_sigev_un._tid, sigev_notify_thread_id);
765885e5abSDmitry Chagin 		sig->sigev_signo = linux_to_bsd_signal(l_sig->sigev_signo);
770a041f3bSBjoern A. Zeeb 		PTRIN_CP(*l_sig, *sig, sigev_value.sival_ptr);
780a041f3bSBjoern A. Zeeb 		break;
790a041f3bSBjoern A. Zeeb 	default:
800a041f3bSBjoern A. Zeeb 		return (EINVAL);
810a041f3bSBjoern A. Zeeb 	}
820a041f3bSBjoern A. Zeeb 	return (0);
830a041f3bSBjoern A. Zeeb }
840a041f3bSBjoern A. Zeeb 
850a041f3bSBjoern A. Zeeb int
860a041f3bSBjoern A. Zeeb linux_timer_create(struct thread *td, struct linux_timer_create_args *uap)
870a041f3bSBjoern A. Zeeb {
880a041f3bSBjoern A. Zeeb 	struct l_sigevent l_ev;
890a041f3bSBjoern A. Zeeb 	struct sigevent ev, *evp;
9016ac71bcSDmitry Chagin 	clockid_t nwhich;
910a041f3bSBjoern A. Zeeb 	int error, id;
920a041f3bSBjoern A. Zeeb 
930a041f3bSBjoern A. Zeeb 	if (uap->evp == NULL) {
940a041f3bSBjoern A. Zeeb 		evp = NULL;
950a041f3bSBjoern A. Zeeb 	} else {
960a041f3bSBjoern A. Zeeb 		error = copyin(uap->evp, &l_ev, sizeof(l_ev));
970a041f3bSBjoern A. Zeeb 		if (error != 0)
980a041f3bSBjoern A. Zeeb 			return (error);
990a041f3bSBjoern A. Zeeb 		error = linux_convert_l_sigevent(&l_ev, &ev);
1000a041f3bSBjoern A. Zeeb 		if (error != 0)
1010a041f3bSBjoern A. Zeeb 			return (error);
1020a041f3bSBjoern A. Zeeb 		evp = &ev;
1030a041f3bSBjoern A. Zeeb 	}
10416ac71bcSDmitry Chagin 	error = linux_to_native_clockid(&nwhich, uap->clock_id);
1050a041f3bSBjoern A. Zeeb 	if (error != 0)
1060a041f3bSBjoern A. Zeeb 		return (error);
10716ac71bcSDmitry Chagin 	error = kern_ktimer_create(td, nwhich, evp, &id, -1);
1080a041f3bSBjoern A. Zeeb 	if (error == 0) {
1090a041f3bSBjoern A. Zeeb 		error = copyout(&id, uap->timerid, sizeof(int));
1100a041f3bSBjoern A. Zeeb 		if (error != 0)
1110a041f3bSBjoern A. Zeeb 			kern_ktimer_delete(td, id);
1120a041f3bSBjoern A. Zeeb 	}
1130a041f3bSBjoern A. Zeeb 	return (error);
1140a041f3bSBjoern A. Zeeb }
1150a041f3bSBjoern A. Zeeb 
1160a041f3bSBjoern A. Zeeb int
1170a041f3bSBjoern A. Zeeb linux_timer_settime(struct thread *td, struct linux_timer_settime_args *uap)
1180a041f3bSBjoern A. Zeeb {
1190a041f3bSBjoern A. Zeeb 	struct l_itimerspec l_val, l_oval;
1200a041f3bSBjoern A. Zeeb 	struct itimerspec val, oval, *ovalp;
121a1fd2911SDmitry Chagin 	int flags, error;
1220a041f3bSBjoern A. Zeeb 
1230a041f3bSBjoern A. Zeeb 	error = copyin(uap->new, &l_val, sizeof(l_val));
1240a041f3bSBjoern A. Zeeb 	if (error != 0)
1250a041f3bSBjoern A. Zeeb 		return (error);
126a1fd2911SDmitry Chagin 	error = linux_to_native_itimerspec(&val, &l_val);
127a1fd2911SDmitry Chagin 	if (error != 0)
128a1fd2911SDmitry Chagin 		return (error);
1290a041f3bSBjoern A. Zeeb 	ovalp = uap->old != NULL ? &oval : NULL;
130a1fd2911SDmitry Chagin 	error = linux_to_native_timerflags(&flags, uap->flags);
131a1fd2911SDmitry Chagin 	if (error != 0)
132a1fd2911SDmitry Chagin 		return (error);
133a1fd2911SDmitry Chagin 	error = kern_ktimer_settime(td, uap->timerid, flags, &val, ovalp);
1340a041f3bSBjoern A. Zeeb 	if (error == 0 && uap->old != NULL) {
135a1fd2911SDmitry Chagin 		error = native_to_linux_itimerspec(&l_val, &val);
136a1fd2911SDmitry Chagin 		if (error == 0)
1370a041f3bSBjoern A. Zeeb 			error = copyout(&l_oval, uap->old, sizeof(l_oval));
1380a041f3bSBjoern A. Zeeb 	}
1390a041f3bSBjoern A. Zeeb 	return (error);
1400a041f3bSBjoern A. Zeeb }
1410a041f3bSBjoern A. Zeeb 
142a1fd2911SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
143a1fd2911SDmitry Chagin int
144a1fd2911SDmitry Chagin linux_timer_settime64(struct thread *td, struct linux_timer_settime64_args *uap)
145a1fd2911SDmitry Chagin {
146a1fd2911SDmitry Chagin 	struct l_itimerspec64 l_val, l_oval;
147a1fd2911SDmitry Chagin 	struct itimerspec val, oval, *ovalp;
148a1fd2911SDmitry Chagin 	int flags, error;
149a1fd2911SDmitry Chagin 
150a1fd2911SDmitry Chagin 	error = copyin(uap->new, &l_val, sizeof(l_val));
151a1fd2911SDmitry Chagin 	if (error != 0)
152a1fd2911SDmitry Chagin 		return (error);
153a1fd2911SDmitry Chagin 	error = linux_to_native_itimerspec64(&val, &l_val);
154a1fd2911SDmitry Chagin 	if (error != 0)
155a1fd2911SDmitry Chagin 		return (error);
156a1fd2911SDmitry Chagin 	ovalp = uap->old != NULL ? &oval : NULL;
157a1fd2911SDmitry Chagin 	error = linux_to_native_timerflags(&flags, uap->flags);
158a1fd2911SDmitry Chagin 	if (error != 0)
159a1fd2911SDmitry Chagin 		return (error);
160a1fd2911SDmitry Chagin 	error = kern_ktimer_settime(td, uap->timerid, flags, &val, ovalp);
161a1fd2911SDmitry Chagin 	if (error == 0 && uap->old != NULL) {
162a1fd2911SDmitry Chagin 		error = native_to_linux_itimerspec64(&l_val, &val);
163a1fd2911SDmitry Chagin 		if (error == 0)
164a1fd2911SDmitry Chagin 			error = copyout(&l_oval, uap->old, sizeof(l_oval));
165a1fd2911SDmitry Chagin 	}
166a1fd2911SDmitry Chagin 	return (error);
167a1fd2911SDmitry Chagin }
168a1fd2911SDmitry Chagin #endif
169a1fd2911SDmitry Chagin 
1700a041f3bSBjoern A. Zeeb int
1710a041f3bSBjoern A. Zeeb linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *uap)
1720a041f3bSBjoern A. Zeeb {
1730a041f3bSBjoern A. Zeeb 	struct l_itimerspec l_val;
1740a041f3bSBjoern A. Zeeb 	struct itimerspec val;
1750a041f3bSBjoern A. Zeeb 	int error;
1760a041f3bSBjoern A. Zeeb 
1770a041f3bSBjoern A. Zeeb 	error = kern_ktimer_gettime(td, uap->timerid, &val);
178783c1bd8SDmitry Chagin 	if (error == 0)
179783c1bd8SDmitry Chagin 		error = native_to_linux_itimerspec(&l_val, &val);
180783c1bd8SDmitry Chagin 	if (error == 0)
1810a041f3bSBjoern A. Zeeb 		error = copyout(&l_val, uap->setting, sizeof(l_val));
1820a041f3bSBjoern A. Zeeb 	return (error);
1830a041f3bSBjoern A. Zeeb }
1840a041f3bSBjoern A. Zeeb 
185783c1bd8SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
186783c1bd8SDmitry Chagin int
187783c1bd8SDmitry Chagin linux_timer_gettime64(struct thread *td, struct linux_timer_gettime64_args *uap)
188783c1bd8SDmitry Chagin {
189783c1bd8SDmitry Chagin 	struct l_itimerspec64 l_val;
190783c1bd8SDmitry Chagin 	struct itimerspec val;
191783c1bd8SDmitry Chagin 	int error;
192783c1bd8SDmitry Chagin 
193783c1bd8SDmitry Chagin 	error = kern_ktimer_gettime(td, uap->timerid, &val);
194783c1bd8SDmitry Chagin 	if (error == 0)
195783c1bd8SDmitry Chagin 		error = native_to_linux_itimerspec64(&l_val, &val);
196783c1bd8SDmitry Chagin 	if (error == 0)
197783c1bd8SDmitry Chagin 		error = copyout(&l_val, uap->setting, sizeof(l_val));
198783c1bd8SDmitry Chagin 	return (error);
199783c1bd8SDmitry Chagin }
200783c1bd8SDmitry Chagin #endif
201783c1bd8SDmitry Chagin 
2020a041f3bSBjoern A. Zeeb int
2030a041f3bSBjoern A. Zeeb linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *uap)
2040a041f3bSBjoern A. Zeeb {
2050a041f3bSBjoern A. Zeeb 
2060a041f3bSBjoern A. Zeeb 	return (kern_ktimer_getoverrun(td, uap->timerid));
2070a041f3bSBjoern A. Zeeb }
2080a041f3bSBjoern A. Zeeb 
2090a041f3bSBjoern A. Zeeb int
2100a041f3bSBjoern A. Zeeb linux_timer_delete(struct thread *td, struct linux_timer_delete_args *uap)
2110a041f3bSBjoern A. Zeeb {
2120a041f3bSBjoern A. Zeeb 
2130a041f3bSBjoern A. Zeeb 	return (kern_ktimer_delete(td, uap->timerid));
2140a041f3bSBjoern A. Zeeb }
215