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