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