Home | History | Annotate | Line # | Download | only in freebsd
freebsd_sched.c revision 1.18
      1 /*	$NetBSD: freebsd_sched.c,v 1.18 2008/02/29 08:41:51 dogcow 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.18 2008/02/29 08:41:51 dogcow 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 /*
     68  * XXX: Needs adjustment to do a proper conversion.
     69  */
     70 static int
     71 sched_freebsd2native(int freebsd_policy,
     72     struct freebsd_sched_param *freebsd_params, int *native_policy,
     73     struct sched_param *native_params)
     74 {
     75 	int error;
     76 
     77 	error = 0;
     78 
     79 	switch (freebsd_policy) {
     80 	case FREEBSD_SCHED_OTHER:
     81 		*native_policy = SCHED_OTHER;
     82 		break;
     83 
     84 	case FREEBSD_SCHED_FIFO:
     85 		*native_policy = SCHED_FIFO;
     86 		break;
     87 
     88 	case FREEBSD_SCHED_RR:
     89 		*native_policy = SCHED_RR;
     90 		break;
     91 
     92 	default:
     93 		error = EINVAL;
     94 		break;
     95 	}
     96 
     97 	if (freebsd_params != NULL && native_params != NULL && !error) {
     98 		native_params = (struct sched_param *)freebsd_params;
     99 	}
    100 
    101 	return (error);
    102 }
    103 
    104 /*
    105  * XXX: Needs adjustment to do a proper conversion.
    106  */
    107 static int
    108 sched_native2freebsd(int native_policy, struct sched_param *native_params,
    109     int *freebsd_policy, struct freebsd_sched_param *freebsd_params)
    110 {
    111 	int error;
    112 
    113 	error = 0;
    114 
    115 	switch (native_policy) {
    116 	case SCHED_OTHER:
    117 		*freebsd_policy = FREEBSD_SCHED_OTHER;
    118 		break;
    119 
    120 	case SCHED_FIFO:
    121 		*freebsd_policy = FREEBSD_SCHED_FIFO;
    122 		break;
    123 
    124 	case SCHED_RR:
    125 		*freebsd_policy = FREEBSD_SCHED_RR;
    126 		break;
    127 
    128 	default:
    129 		error = EINVAL;
    130 		break;
    131 	}
    132 
    133 	if (native_params != NULL && freebsd_params != NULL && !error) {
    134 		freebsd_params = (struct freebsd_sched_param *)native_params;
    135 	}
    136 
    137 	return (error);
    138 }
    139 
    140 int
    141 freebsd_sys_sched_setparam(struct lwp *l, const struct freebsd_sys_sched_setparam_args *uap, register_t *retval)
    142 {
    143 	/* {
    144 		syscallarg(pid_t) pid;
    145 		syscallarg(const struct freebsd_sched_param *) sp;
    146 	} */
    147 	int error, policy;
    148 	struct freebsd_sched_param lp;
    149 	struct sched_param sp;
    150 
    151 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    152 		error = EINVAL;
    153 		goto out;
    154 	}
    155 
    156 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    157 	if (error)
    158 		goto out;
    159 
    160 	/* We need the current policy in FreeBSD terms. */
    161 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    162 	if (error)
    163 		goto out;
    164 	error = sched_native2freebsd(policy, NULL, &policy, NULL);
    165 	if (error)
    166 		goto out;
    167 
    168 	error = sched_freebsd2native(policy, &lp, &policy, &sp);
    169 	if (error)
    170 		goto out;
    171 
    172 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    173 	if (error)
    174 		goto out;
    175 
    176  out:
    177 	return error;
    178 }
    179 
    180 int
    181 freebsd_sys_sched_getparam(struct lwp *l, const struct freebsd_sys_sched_getparam_args *uap, register_t *retval)
    182 {
    183 	/* {
    184 		syscallarg(pid_t) pid;
    185 		syscallarg(struct freebsd_sched_param *) sp;
    186 	} */
    187 	struct freebsd_sched_param lp;
    188 	struct sched_param sp;
    189 	int error;
    190 
    191 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    192 		error = EINVAL;
    193 		goto out;
    194 	}
    195 
    196 	error = do_sched_getparam(SCARG(uap, pid), 0, NULL, &sp);
    197 	if (error)
    198 		goto out;
    199 
    200 	error = sched_native2freebsd(0, &sp, NULL, &lp);
    201 	if (error)
    202 		goto out;
    203 
    204 	error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
    205 	if (error)
    206 		goto out;
    207 
    208  out:
    209 	return (error);
    210 }
    211 
    212 int
    213 freebsd_sys_sched_setscheduler(struct lwp *l, const struct freebsd_sys_sched_setscheduler_args *uap, register_t *retval)
    214 {
    215 	/* {
    216 		syscallarg(pid_t) pid;
    217 		syscallarg(int) policy;
    218 		syscallarg(cont struct freebsd_sched_scheduler *) sp;
    219 	} */
    220 	int error, policy;
    221 	struct freebsd_sched_param lp;
    222 	struct sched_param sp;
    223 
    224 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    225  		error = EINVAL;
    226 		goto out;
    227 	}
    228 
    229 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    230 	if (error)
    231 		goto out;
    232 
    233 	error = sched_freebsd2native(SCARG(uap, policy), &lp, &policy, &sp);
    234 	if (error)
    235 		goto out;
    236 
    237 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    238 	if (error)
    239 		goto out;
    240 
    241  out:
    242 	return error;
    243 }
    244 
    245 int
    246 freebsd_sys_sched_getscheduler(struct lwp *l, const struct freebsd_sys_sched_getscheduler_args *uap, register_t *retval)
    247 {
    248 	/* {
    249 		syscallarg(pid_t) pid;
    250 	} */
    251 	int error, policy;
    252 
    253 	*retval = -1;
    254 
    255 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    256 	if (error)
    257 		goto out;
    258 
    259 	error = sched_native2freebsd(policy, NULL, &policy, NULL);
    260 	if (error)
    261 		goto out;
    262 
    263 	*retval = policy;
    264 
    265  out:
    266 	return error;
    267 }
    268 
    269 int
    270 freebsd_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    271 {
    272 
    273 	yield();
    274 	return 0;
    275 }
    276 
    277 int
    278 freebsd_sys_sched_get_priority_max(struct lwp *l, const struct freebsd_sys_sched_get_priority_max_args *uap, register_t *retval)
    279 {
    280 	/* {
    281 		syscallarg(int) policy;
    282 	} */
    283 
    284 	/*
    285 	 * We can't emulate anything put the default scheduling policy.
    286 	 */
    287 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) {
    288 		*retval = -1;
    289 		return EINVAL;
    290 	}
    291 
    292 	*retval = 0;
    293 	return 0;
    294 }
    295 
    296 int
    297 freebsd_sys_sched_get_priority_min(struct lwp *l, const struct freebsd_sys_sched_get_priority_min_args *uap, register_t *retval)
    298 {
    299 	/* {
    300 		syscallarg(int) policy;
    301 	} */
    302 
    303 	/*
    304 	 * We can't emulate anything put the default scheduling policy.
    305 	 */
    306 	if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) {
    307 		*retval = -1;
    308 		return EINVAL;
    309 	}
    310 
    311 	*retval = 0;
    312 	return 0;
    313 }
    314