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