xref: /netbsd-src/sys/compat/freebsd/freebsd_sched.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: freebsd_sched.c,v 1.2 2003/01/18 07:33:16 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center; by Matthias Scheler.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * FreeBSD compatibility module. Try to deal with scheduler related syscalls.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: freebsd_sched.c,v 1.2 2003/01/18 07:33:16 thorpej Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/mount.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/syscallargs.h>
52 
53 #include <machine/cpu.h>
54 
55 #include <compat/freebsd/freebsd_syscallargs.h>
56 #include <compat/freebsd/freebsd_sched.h>
57 
58 int
59 freebsd_sys_yield(l, v, retval)
60 	struct lwp *l;
61 	void *v;
62 	register_t *retval;
63 {
64 
65 	yield();
66 	return 0;
67 }
68 
69 int
70 freebsd_sys_sched_setparam(l, v, retval)
71 	struct lwp *l;
72 	void *v;
73 	register_t *retval;
74 {
75 	struct freebsd_sys_sched_setparam_args /* {
76 		syscallarg(pid_t) pid;
77 		syscallarg(const struct freebsd_sched_param *) sp;
78 	} */ *uap = v;
79 	int error;
80 	struct freebsd_sched_param lp;
81 	struct proc *p;
82 
83 	/*
84 	 * We only check for valid parameters and return afterwards.
85 	 */
86 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
87 		return EINVAL;
88 
89 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
90 	if (error)
91 		return error;
92 
93 	if (SCARG(uap, pid) != 0) {
94 		struct pcred *pc = l->l_proc->p_cred;
95 
96 		if ((p = pfind(SCARG(uap, pid))) == NULL)
97 			return ESRCH;
98 		if (!(l->l_proc == p ||
99 		      pc->pc_ucred->cr_uid == 0 ||
100 		      pc->p_ruid == p->p_cred->p_ruid ||
101 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
102 		      pc->p_ruid == p->p_ucred->cr_uid ||
103 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
104 			return EPERM;
105 	}
106 
107 	return 0;
108 }
109 
110 int
111 freebsd_sys_sched_getparam(l, v, retval)
112 	struct lwp *l;
113 	void *v;
114 	register_t *retval;
115 {
116 	struct freebsd_sys_sched_getparam_args /* {
117 		syscallarg(pid_t) pid;
118 		syscallarg(struct freebsd_sched_param *) sp;
119 	} */ *uap = v;
120 	struct proc *p;
121 	struct freebsd_sched_param lp;
122 
123 	/*
124 	 * We only check for valid parameters and return a dummy
125 	 * priority afterwards.
126 	 */
127 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
128 		return EINVAL;
129 
130 	if (SCARG(uap, pid) != 0) {
131 		struct pcred *pc = l->l_proc->p_cred;
132 
133 		if ((p = pfind(SCARG(uap, pid))) == NULL)
134 			return ESRCH;
135 		if (!(l->l_proc == p ||
136 		      pc->pc_ucred->cr_uid == 0 ||
137 		      pc->p_ruid == p->p_cred->p_ruid ||
138 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
139 		      pc->p_ruid == p->p_ucred->cr_uid ||
140 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
141 			return EPERM;
142 	}
143 
144 	lp.sched_priority = 0;
145 	return copyout(&lp, SCARG(uap, sp), sizeof(lp));
146 }
147 
148 int
149 freebsd_sys_sched_setscheduler(l, v, retval)
150 	struct lwp *l;
151 	void *v;
152 	register_t *retval;
153 {
154 	struct freebsd_sys_sched_setscheduler_args /* {
155 		syscallarg(pid_t) pid;
156 		syscallarg(int) policy;
157 		syscallarg(cont struct freebsd_sched_scheduler *) sp;
158 	} */ *uap = v;
159 	int error;
160 	struct freebsd_sched_param lp;
161 	struct proc *p;
162 
163 	/*
164 	 * We only check for valid parameters and return afterwards.
165 	 */
166 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
167 		return EINVAL;
168 
169 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
170 	if (error)
171 		return error;
172 
173 	if (SCARG(uap, pid) != 0) {
174 		struct pcred *pc = l->l_proc->p_cred;
175 
176 		if ((p = pfind(SCARG(uap, pid))) == NULL)
177 			return ESRCH;
178 		if (!(l->l_proc == p ||
179 		      pc->pc_ucred->cr_uid == 0 ||
180 		      pc->p_ruid == p->p_cred->p_ruid ||
181 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
182 		      pc->p_ruid == p->p_ucred->cr_uid ||
183 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
184 			return EPERM;
185 	}
186 
187 	/*
188 	 * We can't emulate anything put the default scheduling policy.
189 	 */
190 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER || lp.sched_priority != 0)
191 		return EINVAL;
192 
193 	return 0;
194 }
195 
196 int
197 freebsd_sys_sched_getscheduler(l, v, retval)
198 	struct lwp *l;
199 	void *v;
200 	register_t *retval;
201 {
202 	struct freebsd_sys_sched_getscheduler_args /* {
203 		syscallarg(pid_t) pid;
204 	} */ *uap = v;
205 	struct proc *p;
206 
207 	*retval = -1;
208 
209 	/*
210 	 * We only check for valid parameters and return afterwards.
211 	 */
212 	if (SCARG(uap, pid) != 0) {
213 		struct pcred *pc = l->l_proc->p_cred;
214 
215 		if ((p = pfind(SCARG(uap, pid))) == NULL)
216 			return ESRCH;
217 		if (!(l->l_proc == p ||
218 		      pc->pc_ucred->cr_uid == 0 ||
219 		      pc->p_ruid == p->p_cred->p_ruid ||
220 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
221 		      pc->p_ruid == p->p_ucred->cr_uid ||
222 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
223 			return EPERM;
224 	}
225 
226 	/*
227 	 * We can't emulate anything put the default scheduling policy.
228 	 */
229 	*retval = FREEBSD_SCHED_OTHER;
230 	return 0;
231 }
232 
233 int
234 freebsd_sys_sched_yield(l, v, retval)
235 	struct lwp *l;
236 	void *v;
237 	register_t *retval;
238 {
239 
240 	yield();
241 	return 0;
242 }
243 
244 int
245 freebsd_sys_sched_get_priority_max(l, v, retval)
246 	struct lwp *l;
247 	void *v;
248 	register_t *retval;
249 {
250 	struct freebsd_sys_sched_get_priority_max_args /* {
251 		syscallarg(int) policy;
252 	} */ *uap = v;
253 
254 	/*
255 	 * We can't emulate anything put the default scheduling policy.
256 	 */
257 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) {
258 		*retval = -1;
259 		return EINVAL;
260 	}
261 
262 	*retval = 0;
263 	return 0;
264 }
265 
266 int
267 freebsd_sys_sched_get_priority_min(l, v, retval)
268 	struct lwp *l;
269 	void *v;
270 	register_t *retval;
271 {
272 	struct freebsd_sys_sched_get_priority_min_args /* {
273 		syscallarg(int) policy;
274 	} */ *uap = v;
275 
276 	/*
277 	 * We can't emulate anything put the default scheduling policy.
278 	 */
279 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) {
280 		*retval = -1;
281 		return EINVAL;
282 	}
283 
284 	*retval = 0;
285 	return 0;
286 }
287