xref: /freebsd/sys/kern/p1003_1b.c (revision e84b7987bc37292885343916731d8b1c41eee44a)
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 int sched_setparam(struct proc *p,
106 	struct sched_setparam_args *uap)
107 {
108 	struct proc *targetp;
109 	int e;
110 
111 	struct sched_param sched_param;
112 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
113 	if (e)
114 		return (e);
115 
116 	if (uap->pid == 0) {
117 		targetp = p;
118 		PROC_LOCK(targetp);
119 	} else {
120 		targetp = pfind(uap->pid);
121 		if (targetp == NULL)
122 			return (ESRCH);
123 	}
124 
125 	e = p_can(p, targetp, P_CAN_SCHED, NULL);
126 	PROC_UNLOCK(targetp);
127 	if (e)
128 		return (e);
129 
130 	e = ksched_setparam(&p->p_retval[0], ksched, targetp,
131 		(const struct sched_param *)&sched_param);
132 	return (e);
133 }
134 
135 int sched_getparam(struct proc *p,
136 	struct sched_getparam_args *uap)
137 {
138 	int e;
139 	struct sched_param sched_param;
140 	struct proc *targetp;
141 
142 	if (uap->pid == 0) {
143 		targetp = p;
144 		PROC_LOCK(targetp);
145 	} else {
146 		targetp = pfind(uap->pid);
147 		if (targetp == NULL)
148 			return (ESRCH);
149 	}
150 
151 	e = p_can(p, targetp, P_CAN_SEE, NULL);
152 	PROC_UNLOCK(targetp);
153 	if (e)
154 		return (e);
155 
156 	e = ksched_getparam(&p->p_retval[0], ksched, targetp, &sched_param);
157 	if (e)
158 		return (e);
159 
160 	e = copyout(&sched_param, uap->param, sizeof(sched_param));
161 	return (e);
162 }
163 int sched_setscheduler(struct proc *p,
164 	struct sched_setscheduler_args *uap)
165 {
166 	int e;
167 	struct sched_param sched_param;
168 	struct proc *targetp;
169 
170 	e = copyin(uap->param, &sched_param, sizeof(sched_param));
171 	if (e)
172 		return (e);
173 
174 	if (uap->pid == 0) {
175 		targetp = p;
176 		PROC_LOCK(targetp);
177 	} else {
178 		targetp = pfind(uap->pid);
179 		if (targetp == NULL)
180 			return (ESRCH);
181 	}
182 
183 	e = p_can(p, targetp, P_CAN_SCHED, NULL);
184 	PROC_UNLOCK(targetp);
185 	if (e)
186 		return (e);
187 
188 	e = ksched_setscheduler(&p->p_retval[0], ksched, targetp, uap->policy,
189 		(const struct sched_param *)&sched_param);
190 
191 	return (e);
192 }
193 int sched_getscheduler(struct proc *p,
194 	struct sched_getscheduler_args *uap)
195 {
196 	int e;
197 	struct proc *targetp;
198 
199 	if (uap->pid == 0) {
200 		targetp = p;
201 		PROC_LOCK(targetp);
202 	} else {
203 		targetp = pfind(uap->pid);
204 		if (targetp == NULL)
205 			return (ESRCH);
206 	}
207 
208 	e = p_can(p, targetp, P_CAN_SEE, NULL);
209 	PROC_UNLOCK(targetp);
210 	if (e)
211 		return (e);
212 
213 	e = ksched_getscheduler(&p->p_retval[0], ksched, targetp);
214 
215 	return (e);
216 }
217 int sched_yield(struct proc *p,
218 	struct sched_yield_args *uap)
219 {
220 	return ksched_yield(&p->p_retval[0], ksched);
221 }
222 int sched_get_priority_max(struct proc *p,
223 	struct sched_get_priority_max_args *uap)
224 {
225 	return ksched_get_priority_max(&p->p_retval[0],
226 	ksched, uap->policy);
227 }
228 int sched_get_priority_min(struct proc *p,
229 	struct sched_get_priority_min_args *uap)
230 {
231 	return ksched_get_priority_min(&p->p_retval[0],
232 	ksched, uap->policy);
233 }
234 int sched_rr_get_interval(struct proc *p,
235 	struct sched_rr_get_interval_args *uap)
236 {
237 	int e;
238 	struct proc *targetp;
239 
240 	if (uap->pid == 0) {
241 		targetp = p;
242 		PROC_LOCK(targetp);
243 	} else {
244 		targetp = pfind(uap->pid);
245 		if (targetp == NULL)
246 			return (ESRCH);
247 	}
248 
249 	e = p_can(p, targetp, P_CAN_SEE, NULL);
250 	PROC_UNLOCK(targetp);
251 	if (e)
252 		return (e);
253 
254 	e = ksched_rr_get_interval(&p->p_retval[0], ksched, targetp,
255 	    uap->interval);
256 
257 	return (e);
258 }
259 
260 #endif
261 
262 static void p31binit(void *notused)
263 {
264 	(void) sched_attach();
265 	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
266 }
267 
268 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
269