p1003_1b.c revision 106969
11638Srgrimes/*
21638Srgrimes * Copyright (c) 1996, 1997, 1998
31638Srgrimes *	HD Associates, Inc.  All rights reserved.
41638Srgrimes *
51638Srgrimes * Redistribution and use in source and binary forms, with or without
61638Srgrimes * modification, are permitted provided that the following conditions
71638Srgrimes * are met:
81638Srgrimes * 1. Redistributions of source code must retain the above copyright
91638Srgrimes *    notice, this list of conditions and the following disclaimer.
101638Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111638Srgrimes *    notice, this list of conditions and the following disclaimer in the
121638Srgrimes *    documentation and/or other materials provided with the distribution.
131638Srgrimes * 3. All advertising materials mentioning features or use of this software
141638Srgrimes *    must display the following acknowledgement:
151638Srgrimes *	This product includes software developed by HD Associates, Inc
161638Srgrimes * 4. Neither the name of the author nor the names of any co-contributors
171638Srgrimes *    may be used to endorse or promote products derived from this software
181638Srgrimes *    without specific prior written permission.
191638Srgrimes *
201638Srgrimes * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES AND CONTRIBUTORS ``AS IS'' AND
211638Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221638Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231638Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES OR CONTRIBUTORS BE LIABLE
241638Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251638Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261638Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271638Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281638Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291638Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301638Srgrimes * SUCH DAMAGE.
311638Srgrimes *
321638Srgrimes * $FreeBSD: head/sys/kern/p1003_1b.c 106969 2002-11-15 22:55:06Z alfred $
331638Srgrimes */
341638Srgrimes
351638Srgrimes/* p1003_1b: Real Time common code.
361638Srgrimes */
371638Srgrimes
381638Srgrimes#include "opt_posix.h"
391638Srgrimes
401638Srgrimes#include <sys/param.h>
411638Srgrimes#include <sys/systm.h>
421638Srgrimes#include <sys/kernel.h>
431638Srgrimes#include <sys/lock.h>
441638Srgrimes#include <sys/module.h>
451638Srgrimes#include <sys/mutex.h>
461638Srgrimes#include <sys/proc.h>
471638Srgrimes#include <sys/sysctl.h>
481638Srgrimes#include <sys/sysent.h>
491638Srgrimes#include <sys/syslog.h>
501638Srgrimes#include <sys/sysproto.h>
511638Srgrimes
521638Srgrimes#include <posix4/posix4.h>
531638Srgrimes
541638SrgrimesMALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B");
551638Srgrimes
561638Srgrimes/* The system calls return ENOSYS if an entry is called that is
571638Srgrimes * not run-time supported.  I am also logging since some programs
581638Srgrimes * start to use this when they shouldn't.  That will be removed if annoying.
591638Srgrimes */
601638Srgrimesint
611638Srgrimessyscall_not_present(struct thread *td, const char *s, struct nosys_args *uap)
621638Srgrimes{
631638Srgrimes	log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
641638Srgrimes			td->td_proc->p_comm, td->td_proc->p_pid, s);
651638Srgrimes
661638Srgrimes	/* a " return nosys(p, uap); " here causes a core dump.
671638Srgrimes	 */
681638Srgrimes
691638Srgrimes	return ENOSYS;
701638Srgrimes}
711638Srgrimes
721638Srgrimes#if !defined(_KPOSIX_PRIORITY_SCHEDULING)
731638Srgrimes
741638Srgrimes/* Not configured but loadable via a module:
751638Srgrimes */
761638Srgrimes
771638Srgrimesstatic int sched_attach(void)
781638Srgrimes{
791638Srgrimes	return 0;
801638Srgrimes}
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 proc *targetp;
310
311	mtx_lock(&Giant);
312	if (uap->pid == 0) {
313		targettd = td;
314		targetp = td->td_proc;
315		PROC_LOCK(targetp);
316	} else {
317		targetp = pfind(uap->pid);
318		if (targetp == NULL) {
319			e = ESRCH;
320			goto done2;
321		}
322		targettd = FIRST_THREAD_IN_PROC(targetp); /* XXXKSE */
323	}
324
325	e = p_cansee(td, targetp);
326	PROC_UNLOCK(targetp);
327	if (e == 0) {
328		e = ksched_rr_get_interval(&td->td_retval[0], ksched, targettd,
329			uap->interval);
330	}
331done2:
332	mtx_unlock(&Giant);
333	return (e);
334}
335
336#endif
337
338static void p31binit(void *notused)
339{
340	(void) sched_attach();
341	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
342}
343
344SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
345