xref: /freebsd/sys/kern/p1003_1b.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
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
59  * not run-time supported.  I am also logging since some programs
60  * start to use this when 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_proc->p_comm, 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, 1);
106 
107 	return ret;
108 }
109 
110 /*
111  * MPSAFE
112  */
113 int
114 sched_setparam(struct thread *td, struct sched_setparam_args *uap)
115 {
116 	struct thread *targettd;
117 	struct proc *targetp;
118 	int e;
119 	struct sched_param sched_param;
120 
121 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
122 	if (e)
123 		return (e);
124 
125 	if (uap->pid == 0) {
126 		targetp = td->td_proc;
127 		targettd = td;
128 		PROC_LOCK(targetp);
129 	} else {
130 		targetp = pfind(uap->pid);
131 		if (targetp == NULL)
132 			return (ESRCH);
133 		targettd = FIRST_THREAD_IN_PROC(targetp);
134 	}
135 
136 	e = p_cansched(td, targetp);
137 	if (e == 0) {
138 		e = ksched_setparam(ksched, targettd,
139 			(const struct sched_param *)&sched_param);
140 	}
141 	PROC_UNLOCK(targetp);
142 	return (e);
143 }
144 
145 /*
146  * MPSAFE
147  */
148 int
149 sched_getparam(struct thread *td, struct sched_getparam_args *uap)
150 {
151 	int e;
152 	struct sched_param sched_param;
153 	struct thread *targettd;
154 	struct proc *targetp;
155 
156 	if (uap->pid == 0) {
157 		targetp = td->td_proc;
158 		targettd = td;
159 		PROC_LOCK(targetp);
160 	} else {
161 		targetp = pfind(uap->pid);
162 		if (targetp == NULL) {
163 			return (ESRCH);
164 		}
165 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
166 	}
167 
168 	e = p_cansee(td, targetp);
169 	if (e == 0) {
170 		e = ksched_getparam(ksched, targettd, &sched_param);
171 	}
172 	PROC_UNLOCK(targetp);
173 	if (e == 0)
174 		e = copyout(&sched_param, uap->param, sizeof(sched_param));
175 	return (e);
176 }
177 
178 /*
179  * MPSAFE
180  */
181 int
182 sched_setscheduler(struct thread *td, struct sched_setscheduler_args *uap)
183 {
184 	int e;
185 	struct sched_param sched_param;
186 	struct thread *targettd;
187 	struct proc *targetp;
188 
189 	/* Don't allow non root user to set a scheduler policy. */
190 	e = priv_check(td, PRIV_SCHED_SET);
191 	if (e)
192 		return (e);
193 
194 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
195 	if (e)
196 		return (e);
197 
198 	if (uap->pid == 0) {
199 		targetp = td->td_proc;
200 		targettd = td;
201 		PROC_LOCK(targetp);
202 	} else {
203 		targetp = pfind(uap->pid);
204 		if (targetp == NULL)
205 			return (ESRCH);
206 		targettd = FIRST_THREAD_IN_PROC(targetp);
207 	}
208 
209 	e = p_cansched(td, targetp);
210 	if (e == 0) {
211 		e = ksched_setscheduler(ksched, targettd,
212 			uap->policy, (const struct sched_param *)&sched_param);
213 	}
214 	PROC_UNLOCK(targetp);
215 	return (e);
216 }
217 
218 /*
219  * MPSAFE
220  */
221 int
222 sched_getscheduler(struct thread *td, struct sched_getscheduler_args *uap)
223 {
224 	int e, policy;
225 	struct thread *targettd;
226 	struct proc *targetp;
227 
228 	if (uap->pid == 0) {
229 		targetp = td->td_proc;
230 		targettd = td;
231 		PROC_LOCK(targetp);
232 	} else {
233 		targetp = pfind(uap->pid);
234 		if (targetp == NULL) {
235 			e = ESRCH;
236 			goto done2;
237 		}
238 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
239 	}
240 
241 	e = p_cansee(td, targetp);
242 	if (e == 0) {
243 		e = ksched_getscheduler(ksched, targettd, &policy);
244 		td->td_retval[0] = policy;
245 	}
246 	PROC_UNLOCK(targetp);
247 
248 done2:
249 	return (e);
250 }
251 
252 /*
253  * MPSAFE
254  */
255 int
256 sched_yield(struct thread *td, struct sched_yield_args *uap)
257 {
258 
259 	return (ksched_yield(ksched));
260 }
261 
262 /*
263  * MPSAFE
264  */
265 int
266 sched_get_priority_max(struct thread *td,
267     struct sched_get_priority_max_args *uap)
268 {
269 	int error, prio;
270 
271 	error = ksched_get_priority_max(ksched, uap->policy, &prio);
272 	td->td_retval[0] = prio;
273 	return (error);
274 }
275 
276 /*
277  * MPSAFE
278  */
279 int
280 sched_get_priority_min(struct thread *td,
281     struct sched_get_priority_min_args *uap)
282 {
283 	int error, prio;
284 
285 	error = ksched_get_priority_min(ksched, uap->policy, &prio);
286 	td->td_retval[0] = prio;
287 	return (error);
288 }
289 
290 /*
291  * MPSAFE
292  */
293 int
294 sched_rr_get_interval(struct thread *td,
295     struct sched_rr_get_interval_args *uap)
296 {
297 	struct timespec timespec;
298 	int error;
299 
300 	error = kern_sched_rr_get_interval(td, uap->pid, &timespec);
301 	if (error == 0)
302 		error = copyout(&timespec, uap->interval, sizeof(timespec));
303 	return (error);
304 }
305 
306 int
307 kern_sched_rr_get_interval(struct thread *td, pid_t pid,
308     struct timespec *ts)
309 {
310 	int e;
311 	struct thread *targettd;
312 	struct proc *targetp;
313 
314 	if (pid == 0) {
315 		targettd = td;
316 		targetp = td->td_proc;
317 		PROC_LOCK(targetp);
318 	} else {
319 		targetp = td->td_proc;
320 		PROC_LOCK(targetp);
321 		targettd = thread_find(targetp, pid);
322 		if (targettd == NULL) {
323 			PROC_UNLOCK(targetp);
324 			return (ESRCH);
325 		}
326 	}
327 
328 	e = p_cansee(td, targetp);
329 	if (e == 0)
330 		e = ksched_rr_get_interval(ksched, targettd, ts);
331 	PROC_UNLOCK(targetp);
332 	return (e);
333 }
334 
335 #endif
336 
337 static void
338 p31binit(void *notused)
339 {
340 	(void) sched_attach();
341 	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
342 }
343 
344 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
345