Merge branches 'master' and 'suser_to_priv'
[dragonfly.git] / sys / kern / kern_usched.c
1 /*
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sergey Glushchenko <deen@smz.com.ua>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/kern/kern_usched.c,v 1.9 2007/07/02 17:06:55 dillon Exp $
35  */
36
37 #include <sys/errno.h>
38 #include <sys/globaldata.h>             /* curthread */
39 #include <sys/proc.h>
40 #include <sys/priv.h>
41 #include <sys/sysproto.h>               /* struct usched_set_args */
42 #include <sys/systm.h>                  /* strcmp() */
43 #include <sys/usched.h> 
44 #include <machine/smp.h>
45
46 static TAILQ_HEAD(, usched) usched_list = TAILQ_HEAD_INITIALIZER(usched_list);
47
48 cpumask_t usched_mastermask = -1;
49
50 /*
51  * Called from very low level boot code, i386/i386/machdep.c/init386().
52  * We cannot do anything fancy.  no malloc's, no nothing other then 
53  * static initialization.
54  */
55 struct usched *
56 usched_init(void)
57 {
58         const char *defsched;
59
60         defsched = kgetenv("kern.user_scheduler");
61
62         /*
63          * Add various userland schedulers to the system.
64          */
65         usched_ctl(&usched_bsd4, USCH_ADD);
66         usched_ctl(&usched_dummy, USCH_ADD);
67         if (defsched == NULL )
68                 return(&usched_bsd4);
69         if (strcmp(defsched, "bsd4") == 0)
70                 return(&usched_bsd4);
71         kprintf("WARNING: Running dummy userland scheduler\n");
72         return(&usched_dummy);
73 }
74
75 /*
76  * USCHED_CTL
77  *
78  * SYNOPSIS:
79  *      Add/remove usched to/from list.
80  *      
81  * ARGUMENTS:
82  *      usched - pointer to target scheduler
83  *      action - addition or removal ?
84  *
85  * RETURN VALUES:
86  *      0 - success
87  *      EINVAL - error
88  */
89 int
90 usched_ctl(struct usched *usched, int action)
91 {
92         struct usched *item;    /* temporaly for TAILQ processing */
93         int error = 0;
94
95         switch(action) {
96         case USCH_ADD:
97                 /*
98                  * Make sure it isn't already on the list
99                  */
100 #ifdef INVARIANTS
101                 TAILQ_FOREACH(item, &usched_list, entry) {
102                         KKASSERT(item != usched);
103                 }
104 #endif
105                 /*
106                  * Optional callback to the scheduler before we officially
107                  * add it to the list.
108                  */
109                 if (usched->usched_register)
110                         usched->usched_register();
111                 TAILQ_INSERT_TAIL(&usched_list, usched, entry);
112                 break;
113         case USCH_REM:
114                 /*
115                  * Do not allow the default scheduler to be removed
116                  */
117                 if (strcmp(usched->name, "bsd4") == 0) {
118                         error = EINVAL;
119                         break;
120                 }
121                 TAILQ_FOREACH(item, &usched_list, entry) {
122                         if (item == usched)
123                                 break;
124                 }
125                 if (item) {
126                         if (item->usched_unregister)
127                                 item->usched_unregister();
128                         TAILQ_REMOVE(&usched_list, item, entry);
129                 } else {
130                         error = EINVAL;
131                 }
132                 break;
133         default:
134                 error = EINVAL;
135                 break;
136         }
137         return (error);
138 }
139
140 /*
141  * USCHED_SET(syscall)
142  *
143  * SYNOPSIS:
144  *      Setting up a proc's usched.
145  *
146  * ARGUMENTS:
147  *      pid     -
148  *      cmd     -
149  *      data    - 
150  *      bytes   -
151  * RETURN VALUES:
152  *      0 - success
153  *      EINVAL - error
154  */
155 int
156 sys_usched_set(struct usched_set_args *uap)
157 {
158         struct proc *p = curthread->td_proc;
159         struct usched *item;    /* temporaly for TAILQ processing */
160         int error;
161         char buffer[NAME_LENGTH];
162         cpumask_t mask;
163         struct lwp *lp;
164         int cpuid;
165
166         if ((error = priv_check(curthread, PRIV_ROOT)) != 0)
167                 return (error);
168
169         if (uap->pid != 0 && uap->pid != curthread->td_proc->p_pid)
170                 return (EINVAL);
171
172         lp = curthread->td_lwp;
173         switch (uap->cmd) {
174         case USCHED_SET_SCHEDULER:
175                 if ((error = copyinstr(uap->data, buffer, sizeof(buffer),
176                         NULL)) != 0)
177                         return (error);
178                 TAILQ_FOREACH(item, &usched_list, entry) {
179                         if ((strcmp(item->name, buffer) == 0))
180                                 break;
181                 }
182
183                 /*
184                  * If the scheduler for a process is being changed, disassociate
185                  * the old scheduler before switching to the new one.  
186                  *
187                  * XXX we might have to add an additional ABI call to do a 'full
188                  * disassociation' and another ABI call to do a 'full
189                  * reassociation'
190                  */
191                 /* XXX lwp have to deal with multiple lwps here */
192                 if (p->p_nthreads != 1)
193                         return (EINVAL);
194                 if (item && item != p->p_usched) {
195                         /* XXX lwp */
196                         p->p_usched->release_curproc(ONLY_LWP_IN_PROC(p));
197                         p->p_usched = item;
198                 } else if (item == NULL) {
199                         error = EINVAL;
200                 }
201                 break;
202         case USCHED_SET_CPU:
203                 if (uap->bytes != sizeof(int))
204                         return (EINVAL);
205                 error = copyin(uap->data, &cpuid, sizeof(int));
206                 if (error)
207                         break;
208                 if (cpuid < 0 || cpuid >= ncpus) {
209                         error = EFBIG;
210                         break;
211                 }
212                 if ((smp_active_mask & (1 << cpuid)) == 0) {
213                         error = EINVAL;
214                         break;
215                 }
216                 lp->lwp_cpumask = 1 << cpuid;
217                 if (cpuid != mycpu->gd_cpuid)
218                         lwkt_migratecpu(cpuid);
219                 break;
220         case USCHED_ADD_CPU:
221                 if (uap->bytes != sizeof(int))
222                         return (EINVAL);
223                 error = copyin(uap->data, &cpuid, sizeof(int));
224                 if (error)
225                         break;
226                 if (cpuid < 0 || cpuid >= ncpus) {
227                         error = EFBIG;
228                         break;
229                 }
230                 if (!(smp_active_mask & (1 << cpuid))) {
231                         error = EINVAL;
232                         break;
233                 }
234                 lp->lwp_cpumask |= 1 << cpuid;
235                 break;
236         case USCHED_DEL_CPU:
237                 if (uap->bytes != sizeof(int))
238                         return (EINVAL);
239                 error = copyin(uap->data, &cpuid, sizeof(int));
240                 if (error)
241                         break;
242                 if (cpuid < 0 || cpuid >= ncpus) {
243                         error = EFBIG;
244                         break;
245                 }
246                 lp = curthread->td_lwp;
247                 mask = lp->lwp_cpumask & smp_active_mask & ~(1 << cpuid);
248                 if (mask == 0)
249                         error = EPERM;
250                 else {
251                         lp->lwp_cpumask &= ~(1 << cpuid);
252                         if ((lp->lwp_cpumask & mycpu->gd_cpumask) == 0) {
253                                 cpuid = bsfl(lp->lwp_cpumask & smp_active_mask);
254                                 lwkt_migratecpu(cpuid);
255                         }
256                 }
257         default:
258                 error = EINVAL;
259                 break;
260         }
261         return (error);
262 }
263