2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sergey Glushchenko <deen@smz.com.ua>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/kern/kern_usched.c,v 1.9 2007/07/02 17:06:55 dillon Exp $
37 #include <sys/errno.h>
38 #include <sys/globaldata.h> /* curthread */
40 #include <sys/sysproto.h> /* struct usched_set_args */
41 #include <sys/systm.h> /* strcmp() */
42 #include <sys/usched.h>
43 #include <machine/smp.h>
45 static TAILQ_HEAD(, usched) usched_list = TAILQ_HEAD_INITIALIZER(usched_list);
47 cpumask_t usched_mastermask = -1;
50 * Called from very low level boot code, i386/i386/machdep.c/init386().
51 * We cannot do anything fancy. no malloc's, no nothing other then
52 * static initialization.
59 defsched = kgetenv("kern.user_scheduler");
62 * Add various userland schedulers to the system.
64 usched_ctl(&usched_bsd4, USCH_ADD);
65 usched_ctl(&usched_dummy, USCH_ADD);
66 if (defsched == NULL )
68 if (strcmp(defsched, "bsd4") == 0)
70 kprintf("WARNING: Running dummy userland scheduler\n");
71 return(&usched_dummy);
78 * Add/remove usched to/from list.
81 * usched - pointer to target scheduler
82 * action - addition or removal ?
89 usched_ctl(struct usched *usched, int action)
91 struct usched *item; /* temporaly for TAILQ processing */
97 * Make sure it isn't already on the list
100 TAILQ_FOREACH(item, &usched_list, entry) {
101 KKASSERT(item != usched);
105 * Optional callback to the scheduler before we officially
106 * add it to the list.
108 if (usched->usched_register)
109 usched->usched_register();
110 TAILQ_INSERT_TAIL(&usched_list, usched, entry);
114 * Do not allow the default scheduler to be removed
116 if (strcmp(usched->name, "bsd4") == 0) {
120 TAILQ_FOREACH(item, &usched_list, entry) {
125 if (item->usched_unregister)
126 item->usched_unregister();
127 TAILQ_REMOVE(&usched_list, item, entry);
140 * USCHED_SET(syscall)
143 * Setting up a proc's usched.
155 sys_usched_set(struct usched_set_args *uap)
157 struct proc *p = curthread->td_proc;
158 struct usched *item; /* temporaly for TAILQ processing */
160 char buffer[NAME_LENGTH];
165 if ((error = suser(curthread)) != 0)
168 if (uap->pid != 0 && uap->pid != curthread->td_proc->p_pid)
171 lp = curthread->td_lwp;
173 case USCHED_SET_SCHEDULER:
174 if ((error = copyinstr(uap->data, buffer, sizeof(buffer),
177 TAILQ_FOREACH(item, &usched_list, entry) {
178 if ((strcmp(item->name, buffer) == 0))
183 * If the scheduler for a process is being changed, disassociate
184 * the old scheduler before switching to the new one.
186 * XXX we might have to add an additional ABI call to do a 'full
187 * disassociation' and another ABI call to do a 'full
190 /* XXX lwp have to deal with multiple lwps here */
191 if (p->p_nthreads != 1)
193 if (item && item != p->p_usched) {
195 p->p_usched->release_curproc(ONLY_LWP_IN_PROC(p));
197 } else if (item == NULL) {
202 if (uap->bytes != sizeof(int))
204 error = copyin(uap->data, &cpuid, sizeof(int));
207 if (cpuid < 0 || cpuid >= ncpus) {
211 if ((smp_active_mask & (1 << cpuid)) == 0) {
215 lp->lwp_cpumask = 1 << cpuid;
216 if (cpuid != mycpu->gd_cpuid)
217 lwkt_migratecpu(cpuid);
220 if (uap->bytes != sizeof(int))
222 error = copyin(uap->data, &cpuid, sizeof(int));
225 if (cpuid < 0 || cpuid >= ncpus) {
229 if (!(smp_active_mask & (1 << cpuid))) {
233 lp->lwp_cpumask |= 1 << cpuid;
236 if (uap->bytes != sizeof(int))
238 error = copyin(uap->data, &cpuid, sizeof(int));
241 if (cpuid < 0 || cpuid >= ncpus) {
245 lp = curthread->td_lwp;
246 mask = lp->lwp_cpumask & smp_active_mask & ~(1 << cpuid);
250 lp->lwp_cpumask &= ~(1 << cpuid);
251 if ((lp->lwp_cpumask & mycpu->gd_cpumask) == 0) {
252 cpuid = bsfl(lp->lwp_cpumask & smp_active_mask);
253 lwkt_migratecpu(cpuid);