xref: /freebsd/sys/kern/p1003_1b.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
1 /*
2  * Copyright (c) 1996, 1997, 1998
3  *	HD Associates, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by HD Associates, Inc
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /* p1003_1b: Real Time common code.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_posix.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/syslog.h>
51 #include <sys/sysproto.h>
52 
53 #include <posix4/posix4.h>
54 
55 MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B");
56 
57 /* The system calls return ENOSYS if an entry is called that is
58  * not run-time supported.  I am also logging since some programs
59  * start to use this when they shouldn't.  That will be removed if annoying.
60  */
61 int
62 syscall_not_present(struct thread *td, const char *s, struct nosys_args *uap)
63 {
64 	log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
65 			td->td_proc->p_comm, td->td_proc->p_pid, s);
66 
67 	/* a " return nosys(p, uap); " here causes a core dump.
68 	 */
69 
70 	return ENOSYS;
71 }
72 
73 #if !defined(_KPOSIX_PRIORITY_SCHEDULING)
74 
75 /* Not configured but loadable via a module:
76  */
77 
78 static int sched_attach(void)
79 {
80 	return 0;
81 }
82 
83 SYSCALL_NOT_PRESENT_GEN(sched_setparam)
84 SYSCALL_NOT_PRESENT_GEN(sched_getparam)
85 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
86 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
87 SYSCALL_NOT_PRESENT_GEN(sched_yield)
88 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
89 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
90 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
91 
92 #else
93 
94 /* Configured in kernel version:
95  */
96 static struct ksched *ksched;
97 
98 static int sched_attach(void)
99 {
100 	int ret = ksched_attach(&ksched);
101 
102 	if (ret == 0)
103 		p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1);
104 
105 	return ret;
106 }
107 
108 /*
109  * MPSAFE
110  */
111 int sched_setparam(struct thread *td,
112 	struct sched_setparam_args *uap)
113 {
114 	struct thread *targettd;
115 	struct proc *targetp;
116 	int e;
117 	struct sched_param sched_param;
118 
119 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
120 	if (e)
121 		return (e);
122 
123 	mtx_lock(&Giant);
124 	if (uap->pid == 0) {
125 		targetp = td->td_proc;
126 		targettd = td;
127 		PROC_LOCK(targetp);
128 	} else {
129 		targetp = pfind(uap->pid);
130 		if (targetp == NULL) {
131 			e = ESRCH;
132 			goto done2;
133 		}
134 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
135 	}
136 
137 	e = p_cansched(td, targetp);
138 	PROC_UNLOCK(targetp);
139 	if (e == 0) {
140 		e = ksched_setparam(&td->td_retval[0], ksched, targettd,
141 			(const struct sched_param *)&sched_param);
142 	}
143 done2:
144 	mtx_unlock(&Giant);
145 	return (e);
146 }
147 
148 /*
149  * MPSAFE
150  */
151 int sched_getparam(struct thread *td,
152 	struct sched_getparam_args *uap)
153 {
154 	int e;
155 	struct sched_param sched_param;
156 	struct thread *targettd;
157 	struct proc *targetp;
158 
159 	mtx_lock(&Giant);
160 	if (uap->pid == 0) {
161 		targetp = td->td_proc;
162 		targettd = td;
163 		PROC_LOCK(targetp);
164 	} else {
165 		targetp = pfind(uap->pid);
166 		if (targetp == NULL) {
167 			e = ESRCH;
168 			goto done2;
169 		}
170 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
171 	}
172 
173 	e = p_cansee(td, targetp);
174 	PROC_UNLOCK(targetp);
175 	if (e)
176 		goto done2;
177 
178 	e = ksched_getparam(&td->td_retval[0], ksched, targettd, &sched_param);
179 	if (e == 0)
180 		e = copyout(&sched_param, uap->param, sizeof(sched_param));
181 done2:
182 	mtx_unlock(&Giant);
183 	return (e);
184 }
185 
186 /*
187  * MPSAFE
188  */
189 int sched_setscheduler(struct thread *td,
190 	struct sched_setscheduler_args *uap)
191 {
192 	int e;
193 	struct sched_param sched_param;
194 	struct thread *targettd;
195 	struct proc *targetp;
196 
197 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
198 	if (e)
199 		return (e);
200 
201 	mtx_lock(&Giant);
202 	if (uap->pid == 0) {
203 		targetp = td->td_proc;
204 		targettd = td;
205 		PROC_LOCK(targetp);
206 	} else {
207 		targetp = pfind(uap->pid);
208 		if (targetp == NULL) {
209 			e = ESRCH;
210 			goto done2;
211 		}
212 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
213 	}
214 
215 	e = p_cansched(td, targetp);
216 	PROC_UNLOCK(targetp);
217 	if (e == 0) {
218 		e = ksched_setscheduler(&td->td_retval[0], ksched, targettd,
219 			uap->policy, (const struct sched_param *)&sched_param);
220 	}
221 done2:
222 	mtx_unlock(&Giant);
223 	return (e);
224 }
225 
226 /*
227  * MPSAFE
228  */
229 int sched_getscheduler(struct thread *td,
230 	struct sched_getscheduler_args *uap)
231 {
232 	int e;
233 	struct thread *targettd;
234 	struct proc *targetp;
235 
236 	mtx_lock(&Giant);
237 	if (uap->pid == 0) {
238 		targetp = td->td_proc;
239 		targettd = td;
240 		PROC_LOCK(targetp);
241 	} else {
242 		targetp = pfind(uap->pid);
243 		if (targetp == NULL) {
244 			e = ESRCH;
245 			goto done2;
246 		}
247 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
248 	}
249 
250 	e = p_cansee(td, targetp);
251 	PROC_UNLOCK(targetp);
252 	if (e == 0)
253 		e = ksched_getscheduler(&td->td_retval[0], ksched, targettd);
254 
255 done2:
256 	mtx_unlock(&Giant);
257 	return (e);
258 }
259 
260 /*
261  * MPSAFE
262  */
263 int sched_yield(struct thread *td,
264 	struct sched_yield_args *uap)
265 {
266 	int error;
267 
268 	mtx_lock(&Giant);
269 	error = ksched_yield(&td->td_retval[0], ksched);
270 	mtx_unlock(&Giant);
271 	return (error);
272 }
273 
274 /*
275  * MPSAFE
276  */
277 int sched_get_priority_max(struct thread *td,
278 	struct sched_get_priority_max_args *uap)
279 {
280 	int error;
281 
282 	mtx_lock(&Giant);
283 	error = ksched_get_priority_max(&td->td_retval[0], ksched, uap->policy);
284 	mtx_unlock(&Giant);
285 	return (error);
286 }
287 
288 /*
289  * MPSAFE
290  */
291 int sched_get_priority_min(struct thread *td,
292 	struct sched_get_priority_min_args *uap)
293 {
294 	int error;
295 
296 	mtx_lock(&Giant);
297 	error = ksched_get_priority_min(&td->td_retval[0], ksched, uap->policy);
298 	mtx_unlock(&Giant);
299 	return (error);
300 }
301 
302 /*
303  * MPSAFE
304  */
305 int sched_rr_get_interval(struct thread *td,
306 	struct sched_rr_get_interval_args *uap)
307 {
308 	int e;
309 	struct thread *targettd;
310 	struct timespec timespec;
311 	struct proc *targetp;
312 
313 	mtx_lock(&Giant);
314 	if (uap->pid == 0) {
315 		targettd = td;
316 		targetp = td->td_proc;
317 		PROC_LOCK(targetp);
318 	} else {
319 		targetp = pfind(uap->pid);
320 		if (targetp == NULL) {
321 			e = ESRCH;
322 			goto done2;
323 		}
324 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
325 	}
326 
327 	e = p_cansee(td, targetp);
328 	PROC_UNLOCK(targetp);
329 	if (e == 0) {
330 		e = ksched_rr_get_interval(&td->td_retval[0], ksched, targettd,
331 			&timespec);
332 		if (e == 0)
333 			e = copyout(&timespec, uap->interval,
334 			    sizeof(timespec));
335 	}
336 done2:
337 	mtx_unlock(&Giant);
338 	return (e);
339 }
340 
341 #endif
342 
343 static void p31binit(void *notused)
344 {
345 	(void) sched_attach();
346 	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
347 }
348 
349 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
350