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