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