xref: /freebsd/sys/kern/p1003_1b.c (revision a93e83c8d724de66295929dda3d506f4163ee9d2)
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/priv.h>
48 #include <sys/proc.h>
49 #include <sys/posix4.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysent.h>
53 #include <sys/syslog.h>
54 #include <sys/sysproto.h>
55 
56 MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B");
57 
58 /* The system calls return ENOSYS if an entry is called that is not run-time
59  * supported.  I am also logging since some programs start to use this when
60  * they shouldn't.  That will be removed if annoying.
61  */
62 int
63 syscall_not_present(struct thread *td, const char *s, struct nosys_args *uap)
64 {
65 	log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
66 			td->td_name, td->td_proc->p_pid, s);
67 
68 	/* a " return nosys(p, uap); " here causes a core dump.
69 	 */
70 
71 	return ENOSYS;
72 }
73 
74 #if !defined(_KPOSIX_PRIORITY_SCHEDULING)
75 
76 /* Not configured but loadable via a module:
77  */
78 
79 static int
80 sched_attach(void)
81 {
82 	return 0;
83 }
84 
85 SYSCALL_NOT_PRESENT_GEN(sched_setparam)
86 SYSCALL_NOT_PRESENT_GEN(sched_getparam)
87 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
88 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
89 SYSCALL_NOT_PRESENT_GEN(sched_yield)
90 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
91 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
92 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
93 #else
94 
95 /* Configured in kernel version:
96  */
97 static struct ksched *ksched;
98 
99 static int
100 sched_attach(void)
101 {
102 	int ret = ksched_attach(&ksched);
103 
104 	if (ret == 0)
105 		p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 200112L);
106 
107 	return ret;
108 }
109 
110 int
111 sys_sched_setparam(struct thread *td, struct sched_setparam_args *uap)
112 {
113 	struct thread *targettd;
114 	struct proc *targetp;
115 	int e;
116 	struct sched_param sched_param;
117 
118 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
119 	if (e)
120 		return (e);
121 
122 	if (uap->pid == 0) {
123 		targetp = td->td_proc;
124 		targettd = td;
125 		PROC_LOCK(targetp);
126 	} else {
127 		targetp = pfind(uap->pid);
128 		if (targetp == NULL)
129 			return (ESRCH);
130 		targettd = FIRST_THREAD_IN_PROC(targetp);
131 	}
132 
133 	e = kern_sched_setparam(td, targettd, &sched_param);
134 	PROC_UNLOCK(targetp);
135 	return (e);
136 }
137 
138 int
139 kern_sched_setparam(struct thread *td, struct thread *targettd,
140     struct sched_param *param)
141 {
142 	struct proc *targetp;
143 	int error;
144 
145 	targetp = targettd->td_proc;
146 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
147 
148 	error = p_cansched(td, targetp);
149 	if (error == 0)
150 		error = ksched_setparam(ksched, targettd,
151 		    (const struct sched_param *)param);
152 	return (error);
153 }
154 
155 int
156 sys_sched_getparam(struct thread *td, struct sched_getparam_args *uap)
157 {
158 	int e;
159 	struct sched_param sched_param;
160 	struct thread *targettd;
161 	struct proc *targetp;
162 
163 	if (uap->pid == 0) {
164 		targetp = td->td_proc;
165 		targettd = td;
166 		PROC_LOCK(targetp);
167 	} else {
168 		targetp = pfind(uap->pid);
169 		if (targetp == NULL) {
170 			return (ESRCH);
171 		}
172 		targettd = FIRST_THREAD_IN_PROC(targetp);
173 	}
174 
175 	e = kern_sched_getparam(td, targettd, &sched_param);
176 	PROC_UNLOCK(targetp);
177 	if (e == 0)
178 		e = copyout(&sched_param, uap->param, sizeof(sched_param));
179 	return (e);
180 }
181 
182 int
183 kern_sched_getparam(struct thread *td, struct thread *targettd,
184     struct sched_param *param)
185 {
186 	struct proc *targetp;
187 	int error;
188 
189 	targetp = targettd->td_proc;
190 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
191 
192 	error = p_cansee(td, targetp);
193 	if (error == 0)
194 		error = ksched_getparam(ksched, targettd, param);
195 	return (error);
196 }
197 
198 int
199 sys_sched_setscheduler(struct thread *td, struct sched_setscheduler_args *uap)
200 {
201 	int e;
202 	struct sched_param sched_param;
203 	struct thread *targettd;
204 	struct proc *targetp;
205 
206 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
207 	if (e)
208 		return (e);
209 
210 	if (uap->pid == 0) {
211 		targetp = td->td_proc;
212 		targettd = td;
213 		PROC_LOCK(targetp);
214 	} else {
215 		targetp = pfind(uap->pid);
216 		if (targetp == NULL)
217 			return (ESRCH);
218 		targettd = FIRST_THREAD_IN_PROC(targetp);
219 	}
220 
221 	e = kern_sched_setscheduler(td, targettd, uap->policy,
222 	    &sched_param);
223 	PROC_UNLOCK(targetp);
224 	return (e);
225 }
226 
227 int
228 kern_sched_setscheduler(struct thread *td, struct thread *targettd,
229     int policy, struct sched_param *param)
230 {
231 	struct proc *targetp;
232 	int error;
233 
234 	targetp = targettd->td_proc;
235 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
236 
237 	/* Don't allow non root user to set a scheduler policy. */
238 	error = priv_check(td, PRIV_SCHED_SET);
239 	if (error)
240 		return (error);
241 
242 	error = p_cansched(td, targetp);
243 	if (error == 0)
244 		error = ksched_setscheduler(ksched, targettd, policy,
245 		    (const struct sched_param *)param);
246 	return (error);
247 }
248 
249 int
250 sys_sched_getscheduler(struct thread *td, struct sched_getscheduler_args *uap)
251 {
252 	int e, policy;
253 	struct thread *targettd;
254 	struct proc *targetp;
255 
256 	if (uap->pid == 0) {
257 		targetp = td->td_proc;
258 		targettd = td;
259 		PROC_LOCK(targetp);
260 	} else {
261 		targetp = pfind(uap->pid);
262 		if (targetp == NULL)
263 			return (ESRCH);
264 		targettd = FIRST_THREAD_IN_PROC(targetp);
265 	}
266 
267 	e = kern_sched_getscheduler(td, targettd, &policy);
268 	PROC_UNLOCK(targetp);
269 	if (e == 0)
270 		td->td_retval[0] = policy;
271 
272 	return (e);
273 }
274 
275 int
276 kern_sched_getscheduler(struct thread *td, struct thread *targettd,
277     int *policy)
278 {
279 	struct proc *targetp;
280 	int error;
281 
282 	targetp = targettd->td_proc;
283 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
284 
285 	error = p_cansee(td, targetp);
286 	if (error == 0)
287 		error = ksched_getscheduler(ksched, targettd, policy);
288 	return (error);
289 }
290 
291 int
292 sys_sched_yield(struct thread *td, struct sched_yield_args *uap)
293 {
294 
295 	sched_relinquish(curthread);
296 	return 0;
297 }
298 
299 int
300 sys_sched_get_priority_max(struct thread *td,
301     struct sched_get_priority_max_args *uap)
302 {
303 	int error, prio;
304 
305 	error = ksched_get_priority_max(ksched, uap->policy, &prio);
306 	td->td_retval[0] = prio;
307 	return (error);
308 }
309 
310 int
311 sys_sched_get_priority_min(struct thread *td,
312     struct sched_get_priority_min_args *uap)
313 {
314 	int error, prio;
315 
316 	error = ksched_get_priority_min(ksched, uap->policy, &prio);
317 	td->td_retval[0] = prio;
318 	return (error);
319 }
320 
321 int
322 sys_sched_rr_get_interval(struct thread *td,
323     struct sched_rr_get_interval_args *uap)
324 {
325 	struct timespec timespec;
326 	int error;
327 
328 	error = kern_sched_rr_get_interval(td, uap->pid, &timespec);
329 	if (error == 0)
330 		error = copyout(&timespec, uap->interval, sizeof(timespec));
331 	return (error);
332 }
333 
334 int
335 kern_sched_rr_get_interval(struct thread *td, pid_t pid,
336     struct timespec *ts)
337 {
338 	int e;
339 	struct thread *targettd;
340 	struct proc *targetp;
341 
342 	if (pid == 0) {
343 		targettd = td;
344 		targetp = td->td_proc;
345 		PROC_LOCK(targetp);
346 	} else {
347 		targetp = pfind(pid);
348 		if (targetp == NULL)
349 			return (ESRCH);
350 		targettd = FIRST_THREAD_IN_PROC(targetp);
351 	}
352 
353 	e = kern_sched_rr_get_interval_td(td, targettd, ts);
354 	PROC_UNLOCK(targetp);
355 	return (e);
356 }
357 
358 int
359 kern_sched_rr_get_interval_td(struct thread *td, struct thread *targettd,
360     struct timespec *ts)
361 {
362 	struct proc *p;
363 	int error;
364 
365 	p = targettd->td_proc;
366 	PROC_LOCK_ASSERT(p, MA_OWNED);
367 
368 	error = p_cansee(td, p);
369 	if (error == 0)
370 		error = ksched_rr_get_interval(ksched, targettd, ts);
371 	return (error);
372 }
373 #endif
374 
375 static void
376 p31binit(void *notused)
377 {
378 	(void) sched_attach();
379 	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
380 }
381 
382 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
383