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