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