xref: /freebsd/sys/kern/p1003_1b.c (revision 7dfd9569a2f0637fb9a48157b1c1bfe5709faee3)
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/proc.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysctl.h>
50 #include <sys/sysent.h>
51 #include <sys/syslog.h>
52 #include <sys/sysproto.h>
53 
54 #include <posix4/posix4.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 sched_attach(void)
80 {
81 	return 0;
82 }
83 
84 SYSCALL_NOT_PRESENT_GEN(sched_setparam)
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 
93 #else
94 
95 /* Configured in kernel version:
96  */
97 static struct ksched *ksched;
98 
99 static int sched_attach(void)
100 {
101 	int ret = ksched_attach(&ksched);
102 
103 	if (ret == 0)
104 		p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1);
105 
106 	return ret;
107 }
108 
109 /*
110  * MPSAFE
111  */
112 int sched_setparam(struct thread *td,
113 	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 			e = ESRCH;
132 			goto done2;
133 		}
134 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
135 	}
136 
137 	e = p_cansched(td, targetp);
138 	if (e == 0) {
139 		e = ksched_setparam(&td->td_retval[0], ksched, targettd,
140 			(const struct sched_param *)&sched_param);
141 	}
142 	PROC_UNLOCK(targetp);
143 done2:
144 	return (e);
145 }
146 
147 /*
148  * MPSAFE
149  */
150 int sched_getparam(struct thread *td,
151 	struct sched_getparam_args *uap)
152 {
153 	int e;
154 	struct sched_param sched_param;
155 	struct thread *targettd;
156 	struct proc *targetp;
157 
158 	if (uap->pid == 0) {
159 		targetp = td->td_proc;
160 		targettd = td;
161 		PROC_LOCK(targetp);
162 	} else {
163 		targetp = pfind(uap->pid);
164 		if (targetp == NULL) {
165 			e = ESRCH;
166 			goto done2;
167 		}
168 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
169 	}
170 
171 	e = p_cansee(td, targetp);
172 	if (e == 0) {
173 		e = ksched_getparam(&td->td_retval[0], ksched, targettd,
174 			 &sched_param);
175 	}
176 	PROC_UNLOCK(targetp);
177 	if (e == 0)
178 		e = copyout(&sched_param, uap->param, sizeof(sched_param));
179 done2:
180 	return (e);
181 }
182 
183 /*
184  * MPSAFE
185  */
186 int sched_setscheduler(struct thread *td,
187 	struct sched_setscheduler_args *uap)
188 {
189 	int e;
190 	struct sched_param sched_param;
191 	struct thread *targettd;
192 	struct proc *targetp;
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 			e = ESRCH;
206 			goto done2;
207 		}
208 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
209 	}
210 
211 	e = p_cansched(td, targetp);
212 	if (e == 0) {
213 		e = ksched_setscheduler(&td->td_retval[0], ksched, targettd,
214 			uap->policy, (const struct sched_param *)&sched_param);
215 	}
216 	PROC_UNLOCK(targetp);
217 done2:
218 	return (e);
219 }
220 
221 /*
222  * MPSAFE
223  */
224 int sched_getscheduler(struct thread *td,
225 	struct sched_getscheduler_args *uap)
226 {
227 	int e;
228 	struct thread *targettd;
229 	struct proc *targetp;
230 
231 	if (uap->pid == 0) {
232 		targetp = td->td_proc;
233 		targettd = td;
234 		PROC_LOCK(targetp);
235 	} else {
236 		targetp = pfind(uap->pid);
237 		if (targetp == NULL) {
238 			e = ESRCH;
239 			goto done2;
240 		}
241 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
242 	}
243 
244 	e = p_cansee(td, targetp);
245 	if (e == 0)
246 		e = ksched_getscheduler(&td->td_retval[0], ksched, targettd);
247 	PROC_UNLOCK(targetp);
248 
249 done2:
250 	return (e);
251 }
252 
253 /*
254  * MPSAFE
255  */
256 int sched_yield(struct thread *td,
257 	struct sched_yield_args *uap)
258 {
259 	int error;
260 
261 	error = ksched_yield(&td->td_retval[0], ksched);
262 	return (error);
263 }
264 
265 /*
266  * MPSAFE
267  */
268 int sched_get_priority_max(struct thread *td,
269 	struct sched_get_priority_max_args *uap)
270 {
271 	int error;
272 
273 	error = ksched_get_priority_max(&td->td_retval[0], ksched, uap->policy);
274 	return (error);
275 }
276 
277 /*
278  * MPSAFE
279  */
280 int sched_get_priority_min(struct thread *td,
281 	struct sched_get_priority_min_args *uap)
282 {
283 	int error;
284 
285 	error = ksched_get_priority_min(&td->td_retval[0], ksched, uap->policy);
286 	return (error);
287 }
288 
289 /*
290  * MPSAFE
291  */
292 int sched_rr_get_interval(struct thread *td,
293 	struct sched_rr_get_interval_args *uap)
294 {
295 	struct timespec timespec;
296 	int error;
297 
298 	error = kern_sched_rr_get_interval(td, uap->pid, &timespec);
299 	if (error == 0)
300 		error = copyout(&timespec, uap->interval, sizeof(timespec));
301 	return (error);
302 }
303 
304 int kern_sched_rr_get_interval(struct thread *td, pid_t pid,
305     struct timespec *ts)
306 {
307 	int e;
308 	struct thread *targettd;
309 	struct proc *targetp;
310 
311 	if (pid == 0) {
312 		targettd = td;
313 		targetp = td->td_proc;
314 		PROC_LOCK(targetp);
315 	} else {
316 		targetp = pfind(pid);
317 		if (targetp == NULL)
318 			return (ESRCH);
319 
320 		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
321 	}
322 
323 	e = p_cansee(td, targetp);
324 	if (e == 0)
325 		e = ksched_rr_get_interval(&td->td_retval[0], ksched, targettd,
326 			ts);
327 	PROC_UNLOCK(targetp);
328 	return (e);
329 }
330 
331 #endif
332 
333 static void p31binit(void *notused)
334 {
335 	(void) sched_attach();
336 	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
337 }
338 
339 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
340