xref: /netbsd/sys/compat/freebsd/freebsd_sched.c (revision d50c96bb)
1 /*	$NetBSD: freebsd_sched.c,v 1.23 2021/09/07 11:43:03 riastradh 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * FreeBSD compatibility module. Try to deal with scheduler related syscalls.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: freebsd_sched.c,v 1.23 2021/09/07 11:43:03 riastradh Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/systm.h>
44 #include <sys/syscallargs.h>
45 #include <sys/kauth.h>
46 
47 #include <sys/cpu.h>
48 
49 #include <compat/freebsd/freebsd_syscallargs.h>
50 #include <compat/freebsd/freebsd_sched.h>
51 
52 int
freebsd_sys_yield(struct lwp * l,const void * v,register_t * retval)53 freebsd_sys_yield(struct lwp *l, const void *v, register_t *retval)
54 {
55 
56 	yield();
57 	return 0;
58 }
59 
60 /*
61  * XXX: Needs adjustment to do a proper conversion.
62  */
63 static int
sched_freebsd2native(int freebsd_policy,const struct freebsd_sched_param * freebsd_params,int * native_policy,struct sched_param * native_params)64 sched_freebsd2native(int freebsd_policy,
65     const struct freebsd_sched_param *freebsd_params, int *native_policy,
66     struct sched_param *native_params)
67 {
68 	int p;
69 
70 	switch (freebsd_policy) {
71 	case FREEBSD_SCHED_OTHER:
72 		p = SCHED_OTHER;
73 		break;
74 
75 	case FREEBSD_SCHED_FIFO:
76 		p = SCHED_FIFO;
77 		break;
78 
79 	case FREEBSD_SCHED_RR:
80 		p = SCHED_RR;
81 		break;
82 
83 	default:
84 		return EINVAL;
85 	}
86 
87 	if (native_policy != NULL)
88 		*native_policy = p;
89 
90 	if (freebsd_params != NULL && native_params != NULL) {
91 		/* XXX: Needs adjustment to do a proper conversion. */
92 		native_params->sched_priority = freebsd_params->sched_priority;
93 	}
94 	return 0;
95 }
96 
97 static int
sched_native2freebsd(int native_policy,const struct sched_param * native_params,int * freebsd_policy,struct freebsd_sched_param * freebsd_params)98 sched_native2freebsd(int native_policy, const struct sched_param *native_params,
99     int *freebsd_policy, struct freebsd_sched_param *freebsd_params)
100 {
101 	int p;
102 
103 	switch (native_policy) {
104 	case SCHED_OTHER:
105 		p = FREEBSD_SCHED_OTHER;
106 		break;
107 
108 	case SCHED_FIFO:
109 		p = FREEBSD_SCHED_FIFO;
110 		break;
111 
112 	case SCHED_RR:
113 		p = FREEBSD_SCHED_RR;
114 		break;
115 
116 	default:
117 		return EINVAL;
118 	}
119 
120 	if (freebsd_policy != NULL)
121 		*freebsd_policy = p;
122 
123 	if (native_params != NULL && freebsd_params != NULL) {
124 		/* XXX: Needs adjustment to do a proper conversion. */
125 		memset(freebsd_params, 0, sizeof(*freebsd_params));
126 		freebsd_params->sched_priority = native_params->sched_priority;
127 	}
128 
129 	return 0;
130 }
131 
132 int
freebsd_sys_sched_setparam(struct lwp * l,const struct freebsd_sys_sched_setparam_args * uap,register_t * retval)133 freebsd_sys_sched_setparam(struct lwp *l, const struct freebsd_sys_sched_setparam_args *uap, register_t *retval)
134 {
135 	/* {
136 		syscallarg(pid_t) pid;
137 		syscallarg(const struct freebsd_sched_param *) sp;
138 	} */
139 	int error, policy;
140 	struct freebsd_sched_param lp;
141 	struct sched_param sp;
142 
143 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
144 		error = EINVAL;
145 		goto out;
146 	}
147 
148 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
149 	if (error)
150 		goto out;
151 
152 	/* We need the current policy in FreeBSD terms. */
153 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
154 	if (error)
155 		goto out;
156 	error = sched_native2freebsd(policy, NULL, &policy, NULL);
157 	if (error)
158 		goto out;
159 
160 	error = sched_freebsd2native(policy, &lp, &policy, &sp);
161 	if (error)
162 		goto out;
163 
164 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
165 	if (error)
166 		goto out;
167 
168  out:
169 	return error;
170 }
171 
172 int
freebsd_sys_sched_getparam(struct lwp * l,const struct freebsd_sys_sched_getparam_args * uap,register_t * retval)173 freebsd_sys_sched_getparam(struct lwp *l, const struct freebsd_sys_sched_getparam_args *uap, register_t *retval)
174 {
175 	/* {
176 		syscallarg(pid_t) pid;
177 		syscallarg(struct freebsd_sched_param *) sp;
178 	} */
179 	struct freebsd_sched_param lp;
180 	struct sched_param sp;
181 	int error;
182 
183 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
184 		error = EINVAL;
185 		goto out;
186 	}
187 
188 	error = do_sched_getparam(SCARG(uap, pid), 0, NULL, &sp);
189 	if (error)
190 		goto out;
191 
192 	error = sched_native2freebsd(0, &sp, NULL, &lp);
193 	if (error)
194 		goto out;
195 
196 	error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
197 	if (error)
198 		goto out;
199 
200  out:
201 	return error;
202 }
203 
204 int
freebsd_sys_sched_setscheduler(struct lwp * l,const struct freebsd_sys_sched_setscheduler_args * uap,register_t * retval)205 freebsd_sys_sched_setscheduler(struct lwp *l, const struct freebsd_sys_sched_setscheduler_args *uap, register_t *retval)
206 {
207 	/* {
208 		syscallarg(pid_t) pid;
209 		syscallarg(int) policy;
210 		syscallarg(cont struct freebsd_sched_scheduler *) sp;
211 	} */
212 	int error, policy;
213 	struct freebsd_sched_param lp;
214 	struct sched_param sp;
215 
216 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
217  		error = EINVAL;
218 		goto out;
219 	}
220 
221 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
222 	if (error)
223 		goto out;
224 
225 	error = sched_freebsd2native(SCARG(uap, policy), &lp, &policy, &sp);
226 	if (error)
227 		goto out;
228 
229 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
230 	if (error)
231 		goto out;
232 
233  out:
234 	return error;
235 }
236 
237 int
freebsd_sys_sched_getscheduler(struct lwp * l,const struct freebsd_sys_sched_getscheduler_args * uap,register_t * retval)238 freebsd_sys_sched_getscheduler(struct lwp *l, const struct freebsd_sys_sched_getscheduler_args *uap, register_t *retval)
239 {
240 	/* {
241 		syscallarg(pid_t) pid;
242 	} */
243 	int error, policy;
244 
245 	*retval = -1;
246 
247 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
248 	if (error)
249 		goto out;
250 
251 	error = sched_native2freebsd(policy, NULL, &policy, NULL);
252 	if (error)
253 		goto out;
254 
255 	*retval = policy;
256 
257  out:
258 	return error;
259 }
260 
261 int
freebsd_sys_sched_yield(struct lwp * l,const void * v,register_t * retval)262 freebsd_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
263 {
264 
265 	yield();
266 	return 0;
267 }
268 
269 int
freebsd_sys_sched_get_priority_max(struct lwp * l,const struct freebsd_sys_sched_get_priority_max_args * uap,register_t * retval)270 freebsd_sys_sched_get_priority_max(struct lwp *l, const struct freebsd_sys_sched_get_priority_max_args *uap, register_t *retval)
271 {
272 	/* {
273 		syscallarg(int) policy;
274 	} */
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 
288 int
freebsd_sys_sched_get_priority_min(struct lwp * l,const struct freebsd_sys_sched_get_priority_min_args * uap,register_t * retval)289 freebsd_sys_sched_get_priority_min(struct lwp *l, const struct freebsd_sys_sched_get_priority_min_args *uap, register_t *retval)
290 {
291 	/* {
292 		syscallarg(int) policy;
293 	} */
294 
295 	/*
296 	 * We can't emulate anything put the default scheduling policy.
297 	 */
298 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) {
299 		*retval = -1;
300 		return EINVAL;
301 	}
302 
303 	*retval = 0;
304 	return 0;
305 }
306