xref: /netbsd-src/sys/compat/freebsd/freebsd_sched.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: freebsd_sched.c,v 1.5 2006/10/12 01:30:47 christos 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.5 2006/10/12 01:30:47 christos 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 #include <sys/kauth.h>
53 
54 #include <machine/cpu.h>
55 
56 #include <compat/freebsd/freebsd_syscallargs.h>
57 #include <compat/freebsd/freebsd_sched.h>
58 
59 int
60 freebsd_sys_yield(struct lwp *l __unused, void *v __unused,
61     register_t *retval __unused)
62 {
63 
64 	yield();
65 	return 0;
66 }
67 
68 int
69 freebsd_sys_sched_setparam(struct lwp *l, void *v, register_t *retval __unused)
70 {
71 	struct freebsd_sys_sched_setparam_args /* {
72 		syscallarg(pid_t) pid;
73 		syscallarg(const struct freebsd_sched_param *) sp;
74 	} */ *uap = v;
75 	int error;
76 	struct freebsd_sched_param lp;
77 	struct proc *p;
78 
79 	/*
80 	 * We only check for valid parameters and return afterwards.
81 	 */
82 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
83 		return EINVAL;
84 
85 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
86 	if (error)
87 		return error;
88 
89 	if (SCARG(uap, pid) != 0) {
90 		kauth_cred_t pc = l->l_cred;
91 
92 		if ((p = pfind(SCARG(uap, pid))) == NULL)
93 			return ESRCH;
94 		if (!(l->l_proc == p ||
95 		      kauth_cred_geteuid(pc) == 0 ||
96 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
97 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
98 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
99 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
100 			return EPERM;
101 	}
102 
103 	return 0;
104 }
105 
106 int
107 freebsd_sys_sched_getparam(struct lwp *l, void *v, register_t *retval __unused)
108 {
109 	struct freebsd_sys_sched_getparam_args /* {
110 		syscallarg(pid_t) pid;
111 		syscallarg(struct freebsd_sched_param *) sp;
112 	} */ *uap = v;
113 	struct proc *p;
114 	struct freebsd_sched_param lp;
115 
116 	/*
117 	 * We only check for valid parameters and return a dummy
118 	 * priority afterwards.
119 	 */
120 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
121 		return EINVAL;
122 
123 	if (SCARG(uap, pid) != 0) {
124 		kauth_cred_t pc = l->l_cred;
125 
126 		if ((p = pfind(SCARG(uap, pid))) == NULL)
127 			return ESRCH;
128 		if (!(l->l_proc == p ||
129 		      kauth_cred_geteuid(pc) == 0 ||
130 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
131 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
132 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
133 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
134 			return EPERM;
135 	}
136 
137 	lp.sched_priority = 0;
138 	return copyout(&lp, SCARG(uap, sp), sizeof(lp));
139 }
140 
141 int
142 freebsd_sys_sched_setscheduler(struct lwp *l, void *v,
143     register_t *retval __unused)
144 {
145 	struct freebsd_sys_sched_setscheduler_args /* {
146 		syscallarg(pid_t) pid;
147 		syscallarg(int) policy;
148 		syscallarg(cont struct freebsd_sched_scheduler *) sp;
149 	} */ *uap = v;
150 	int error;
151 	struct freebsd_sched_param lp;
152 	struct proc *p;
153 
154 	/*
155 	 * We only check for valid parameters and return afterwards.
156 	 */
157 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
158 		return EINVAL;
159 
160 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
161 	if (error)
162 		return error;
163 
164 	if (SCARG(uap, pid) != 0) {
165 		kauth_cred_t pc = l->l_cred;
166 
167 		if ((p = pfind(SCARG(uap, pid))) == NULL)
168 			return ESRCH;
169 		if (!(l->l_proc == p ||
170 		      kauth_cred_geteuid(pc) == 0 ||
171 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
172 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
173 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
174 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
175 			return EPERM;
176 	}
177 
178 	/*
179 	 * We can't emulate anything put the default scheduling policy.
180 	 */
181 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER || lp.sched_priority != 0)
182 		return EINVAL;
183 
184 	return 0;
185 }
186 
187 int
188 freebsd_sys_sched_getscheduler(l, v, retval)
189 	struct lwp *l;
190 	void *v;
191 	register_t *retval;
192 {
193 	struct freebsd_sys_sched_getscheduler_args /* {
194 		syscallarg(pid_t) pid;
195 	} */ *uap = v;
196 	struct proc *p;
197 
198 	*retval = -1;
199 
200 	/*
201 	 * We only check for valid parameters and return afterwards.
202 	 */
203 	if (SCARG(uap, pid) != 0) {
204 		kauth_cred_t pc = l->l_cred;
205 
206 		if ((p = pfind(SCARG(uap, pid))) == NULL)
207 			return ESRCH;
208 		if (!(l->l_proc == p ||
209 		      kauth_cred_geteuid(pc) == 0 ||
210 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
211 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
212 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
213 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
214 			return EPERM;
215 	}
216 
217 	/*
218 	 * We can't emulate anything put the default scheduling policy.
219 	 */
220 	*retval = FREEBSD_SCHED_OTHER;
221 	return 0;
222 }
223 
224 int
225 freebsd_sys_sched_yield(struct lwp *l __unused, void *v __unused,
226     register_t *retval __unused)
227 {
228 
229 	yield();
230 	return 0;
231 }
232 
233 int
234 freebsd_sys_sched_get_priority_max(struct lwp *l __unused, void *v,
235     register_t *retval)
236 {
237 	struct freebsd_sys_sched_get_priority_max_args /* {
238 		syscallarg(int) policy;
239 	} */ *uap = v;
240 
241 	/*
242 	 * We can't emulate anything put the default scheduling policy.
243 	 */
244 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) {
245 		*retval = -1;
246 		return EINVAL;
247 	}
248 
249 	*retval = 0;
250 	return 0;
251 }
252 
253 int
254 freebsd_sys_sched_get_priority_min(struct lwp *l __unused, void *v,
255     register_t *retval)
256 {
257 	struct freebsd_sys_sched_get_priority_min_args /* {
258 		syscallarg(int) policy;
259 	} */ *uap = v;
260 
261 	/*
262 	 * We can't emulate anything put the default scheduling policy.
263 	 */
264 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) {
265 		*retval = -1;
266 		return EINVAL;
267 	}
268 
269 	*retval = 0;
270 	return 0;
271 }
272