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