xref: /dragonfly/sys/kern/kern_p1003_1b.c (revision c69bf40f)
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: src/sys/posix4/p1003_1b.c,v 1.5.2.2 2003/03/25 06:13:35 rwatson Exp $
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/sysent.h>
42 #include <sys/posix4.h>
43 #include <sys/proc.h>
44 #include <sys/syslog.h>
45 #include <sys/module.h>
46 #include <sys/sysproto.h>
47 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 
50 MALLOC_DEFINE(M_P31B, "p1003.1b", "Posix 1003.1B");
51 
52 /*
53  * p31b_proc: Return a proc struct corresponding to a pid to operate on.
54  *
55  * Enforce permission policy.
56  *
57  * The policy is the same as for sending signals except there
58  * is no notion of process groups.
59  *
60  * pid == 0 means my process.
61  *
62  * This is disabled until I've got a permission gate in again:
63  * only root can do this.
64  */
65 
66 #if 0
67 /*
68  * This is stolen from CANSIGNAL in kern_sig:
69  *
70  * Can process p, with pcred pc, do "write flavor" operations to process q?
71  */
72 #define CAN_AFFECT(p, cr, q) \
73 	((cr)->cr_uid == 0 || \
74 	    (cr)->cr_ruid == (q)->p_ucred->cr_ruid || \
75 	    (cr)->cr_uid == (q)->p_ucred->cr_ruid || \
76 	    (cr)->cr_ruid == (q)->p_ucred->cr_uid || \
77 	    (cr)->cr_uid == (q)->p_ucred->cr_uid)
78 #else
79 #define CAN_AFFECT(p, cr, q) ((cr)->cr_uid == 0)
80 #endif
81 
82 #if !defined(_KPOSIX_PRIORITY_SCHEDULING)
83 
84 int syscall_not_present(const char *s);
85 
86 /* The system calls return ENOSYS if an entry is called that is
87  * not run-time supported.  I am also logging since some programs
88  * start to use this when they shouldn't.  That will be removed if annoying.
89  */
90 int syscall_not_present(const char *s)
91 {
92 	struct proc *p = curproc;
93 	log(LOG_ERR, "cmd %s pid %d tried to use non-present %s\n",
94 			p->p_comm, p->p_pid, s);
95 
96 	/* a " return nosys(p, uap); " here causes a core dump.
97 	 */
98 
99 	return ENOSYS;
100 }
101 
102 /* Not configured but loadable via a module:
103  */
104 
105 static int sched_attach(void)
106 {
107 	return 0;
108 }
109 
110 #define SYSCALL_NOT_PRESENT_GEN(SC) \
111 int sys_##SC (struct SC##_args *uap) \
112 { \
113 	return syscall_not_present(#SC); \
114 }
115 
116 SYSCALL_NOT_PRESENT_GEN(sched_setparam)
117 SYSCALL_NOT_PRESENT_GEN(sched_getparam)
118 SYSCALL_NOT_PRESENT_GEN(sched_setscheduler)
119 SYSCALL_NOT_PRESENT_GEN(sched_getscheduler)
120 SYSCALL_NOT_PRESENT_GEN(sched_yield)
121 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_max)
122 SYSCALL_NOT_PRESENT_GEN(sched_get_priority_min)
123 SYSCALL_NOT_PRESENT_GEN(sched_rr_get_interval)
124 
125 #else
126 
127 /*
128  * p31b_proc: Look up a proc from a PID.  If proc is 0 it is
129  * my own proc.
130  *
131  * Returns a held process in *pp.
132  */
133 static
134 int
135 p31b_proc(pid_t pid, struct proc **pp)
136 {
137 	int ret = 0;
138 	struct proc *p = curproc;
139 	struct proc *other_proc;
140 
141 	if (pid == 0) {
142 		other_proc = p;
143 		if (other_proc)
144 			PHOLD(other_proc);
145 	} else {
146 		other_proc = pfind(pid);
147 		/* ref from pfind() */
148 	}
149 
150 	if (other_proc) {
151 		/* Enforce permission policy.
152 		 */
153 		if (CAN_AFFECT(p, p->p_ucred, other_proc)) {
154 			*pp = other_proc;
155 			lwkt_gettoken(&other_proc->p_token);
156 		} else {
157 			*pp = NULL;
158 			ret = EPERM;
159 			PRELE(other_proc);
160 		}
161 	} else {
162 		*pp = NULL;
163 		ret = ESRCH;
164 	}
165 	return ret;
166 }
167 
168 static
169 void
170 p31b_proc_done(struct proc *other_proc)
171 {
172 	if (other_proc) {
173 		lwkt_reltoken(&other_proc->p_token);
174 		PRELE(other_proc);
175 	}
176 }
177 
178 /* Configured in kernel version:
179  */
180 static struct ksched *ksched;
181 
182 static int sched_attach(void)
183 {
184 	int ret = ksched_attach(&ksched);
185 
186 	if (ret == 0)
187 		p31b_setcfg(CTL_P1003_1B_PRIORITY_SCHEDULING, 1);
188 
189 	return ret;
190 }
191 
192 int
193 sys_sched_setparam(struct sched_setparam_args *uap)
194 {
195 	struct proc *p;
196 	struct lwp *lp;
197 	struct sched_param sched_param;
198 	int e;
199 
200 	copyin(uap->param, &sched_param, sizeof(sched_param));
201 
202 	if ((e = p31b_proc(uap->pid, &p)) == 0) {
203 		lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
204 		if (lp) {
205 			LWPHOLD(lp);
206 			lwkt_gettoken(&lp->lwp_token);
207 			e = ksched_setparam(&uap->sysmsg_reg, ksched, lp,
208 				    (const struct sched_param *)&sched_param);
209 			lwkt_reltoken(&lp->lwp_token);
210 			LWPRELE(lp);
211 		} else {
212 			e = ESRCH;
213 		}
214 		p31b_proc_done(p);
215 	}
216 	return e;
217 }
218 
219 int
220 sys_sched_getparam(struct sched_getparam_args *uap)
221 {
222 	struct proc *targetp;
223 	struct lwp *lp;
224 	struct sched_param sched_param;
225 	int e;
226 
227 	if ((e = p31b_proc(uap->pid, &targetp)) == 0) {
228 		lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */
229 		if (lp) {
230 			LWPHOLD(lp);
231 			lwkt_gettoken(&lp->lwp_token);
232 			e = ksched_getparam(&uap->sysmsg_reg, ksched,
233 					    lp, &sched_param);
234 			lwkt_reltoken(&lp->lwp_token);
235 			LWPRELE(lp);
236 		} else {
237 			e = ESRCH;
238 		}
239 		p31b_proc_done(targetp);
240 	}
241 	if (e == 0)
242 		copyout(&sched_param, uap->param, sizeof(sched_param));
243 	return e;
244 }
245 
246 int
247 sys_sched_setscheduler(struct sched_setscheduler_args *uap)
248 {
249 	struct proc *p;
250 	struct lwp *lp;
251 	int e;
252 	struct sched_param sched_param;
253 
254 	copyin(uap->param, &sched_param, sizeof(sched_param));
255 
256 	if ((e = p31b_proc(uap->pid, &p)) == 0) {
257 		lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
258 		if (lp) {
259 			LWPHOLD(lp);
260 			lwkt_gettoken(&lp->lwp_token);
261 			e = ksched_setscheduler(&uap->sysmsg_reg, ksched,
262 						lp, uap->policy,
263 				    (const struct sched_param *)&sched_param);
264 			lwkt_reltoken(&lp->lwp_token);
265 			LWPRELE(lp);
266 		} else {
267 			e = ESRCH;
268 		}
269 		p31b_proc_done(p);
270 	}
271 	return e;
272 }
273 
274 int
275 sys_sched_getscheduler(struct sched_getscheduler_args *uap)
276 {
277 	struct proc *targetp;
278 	struct lwp *lp;
279 	int e;
280 
281 	if ((e = p31b_proc(uap->pid, &targetp)) == 0) {
282 		lp = FIRST_LWP_IN_PROC(targetp); /* XXX lwp */
283 		if (lp) {
284 			LWPHOLD(lp);
285 			lwkt_gettoken(&lp->lwp_token);
286 			e = ksched_getscheduler(&uap->sysmsg_reg, ksched, lp);
287 			lwkt_reltoken(&lp->lwp_token);
288 			LWPRELE(lp);
289 		} else {
290 			e = ESRCH;
291 		}
292 		p31b_proc_done(targetp);
293 	}
294 	return e;
295 }
296 
297 /*
298  * MPSAFE
299  */
300 int
301 sys_sched_yield(struct sched_yield_args *uap)
302 {
303 	return ksched_yield(&uap->sysmsg_reg, ksched);
304 }
305 
306 /*
307  * MPSAFE
308  */
309 int
310 sys_sched_get_priority_max(struct sched_get_priority_max_args *uap)
311 {
312 	return ksched_get_priority_max(&uap->sysmsg_reg, ksched, uap->policy);
313 }
314 
315 /*
316  * MPSAFE
317  */
318 int
319 sys_sched_get_priority_min(struct sched_get_priority_min_args *uap)
320 {
321 	return ksched_get_priority_min(&uap->sysmsg_reg, ksched, uap->policy);
322 }
323 
324 int
325 sys_sched_rr_get_interval(struct sched_rr_get_interval_args *uap)
326 {
327 	int e;
328 	struct proc *p;
329 	struct lwp *lp;
330 	struct timespec ts;
331 
332 	if ((e = p31b_proc(uap->pid, &p)) == 0) {
333 		lp = FIRST_LWP_IN_PROC(p); /* XXX lwp */
334 		if (lp) {
335 			LWPHOLD(lp);
336 			lwkt_gettoken(&lp->lwp_token);
337 			e = ksched_rr_get_interval(&uap->sysmsg_reg, ksched,
338 						   lp, &ts);
339 			if (e == 0)
340 				e = copyout(&ts, uap->interval, sizeof(ts));
341 			lwkt_reltoken(&lp->lwp_token);
342 			LWPRELE(lp);
343 		} else {
344 			e = ESRCH;
345 		}
346 		p31b_proc_done(p);
347 	}
348 	return e;
349 }
350 
351 #endif
352 
353 static void
354 p31binit(void *notused)
355 {
356 	sched_attach();
357 	p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, -1);
358 	p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
359 	p31b_setcfg(CTL_P1003_1B_PAGESIZE, PAGE_SIZE);
360 	p31b_setcfg(CTL_P1003_1B_SHARED_MEMORY_OBJECTS,
361 	    _POSIX_SHARED_MEMORY_OBJECTS);
362 }
363 
364 SYSINIT(p31b, SI_SUB_P1003_1B, SI_ORDER_FIRST, p31binit, NULL);
365