kern_cpu.c revision 1.36.2.2 1 /* $NetBSD: kern_cpu.c,v 1.36.2.2 2009/03/03 18:32:55 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c)2007 YAMAMOTO Takashi,
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.36.2.2 2009/03/03 18:32:55 skrll Exp $");
60 #include "opt_compat_netbsd.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/idle.h>
65 #include <sys/sched.h>
66 #include <sys/intr.h>
67 #include <sys/conf.h>
68 #include <sys/cpu.h>
69 #include <sys/cpuio.h>
70 #include <sys/proc.h>
71 #include <sys/percpu.h>
72 #include <sys/kernel.h>
73 #include <sys/kauth.h>
74 #include <sys/xcall.h>
75 #include <sys/pool.h>
76 #include <sys/kmem.h>
77 #include <sys/select.h>
78 #include <sys/namei.h>
79 #include <sys/callout.h>
80
81 #include <uvm/uvm_extern.h>
82
83 #ifdef COMPAT_50
84 #include <compat/sys/cpuio.h>
85 #endif
86
87 void cpuctlattach(int);
88
89 static void cpu_xc_online(struct cpu_info *);
90 static void cpu_xc_offline(struct cpu_info *);
91
92 dev_type_ioctl(cpuctl_ioctl);
93
94 const struct cdevsw cpuctl_cdevsw = {
95 nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
96 nullstop, notty, nopoll, nommap, nokqfilter,
97 D_OTHER | D_MPSAFE
98 };
99
100 kmutex_t cpu_lock;
101 int ncpu;
102 int ncpuonline;
103 bool mp_online;
104 struct cpuqueue cpu_queue = CIRCLEQ_HEAD_INITIALIZER(cpu_queue);
105
106 static struct cpu_info *cpu_infos[MAXCPUS];
107
108 int
109 mi_cpu_attach(struct cpu_info *ci)
110 {
111 int error;
112
113 ci->ci_index = ncpu;
114 cpu_infos[cpu_index(ci)] = ci;
115 CIRCLEQ_INSERT_TAIL(&cpu_queue, ci, ci_data.cpu_qchain);
116 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
117 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
118
119 sched_cpuattach(ci);
120
121 error = create_idle_lwp(ci);
122 if (error != 0) {
123 /* XXX revert sched_cpuattach */
124 return error;
125 }
126
127 if (ci == curcpu())
128 ci->ci_data.cpu_onproc = curlwp;
129 else
130 ci->ci_data.cpu_onproc = ci->ci_data.cpu_idlelwp;
131
132 percpu_init_cpu(ci);
133 softint_init(ci);
134 callout_init_cpu(ci);
135 xc_init_cpu(ci);
136 pool_cache_cpu_init(ci);
137 selsysinit(ci);
138 cache_cpu_init(ci);
139 TAILQ_INIT(&ci->ci_data.cpu_biodone);
140 ncpu++;
141 ncpuonline++;
142
143 return 0;
144 }
145
146 void
147 cpuctlattach(int dummy)
148 {
149
150 }
151
152 int
153 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
154 {
155 CPU_INFO_ITERATOR cii;
156 cpustate_t *cs;
157 struct cpu_info *ci;
158 int error, i;
159 u_int id;
160
161 error = 0;
162
163 mutex_enter(&cpu_lock);
164 switch (cmd) {
165 #ifdef IOC_CPU_OSETSTATE
166 cpustate_t csb;
167
168 case IOC_CPU_OSETSTATE: {
169 cpustate50_t *ocs = data;
170 cpustate50_to_cpustate(ocs, &csb);
171 cs = &csb;
172 error = 1;
173 /*FALLTHROUGH*/
174 }
175 #endif
176 case IOC_CPU_SETSTATE:
177 if (error == 0)
178 cs = data;
179 error = kauth_authorize_system(l->l_cred,
180 KAUTH_SYSTEM_CPU, KAUTH_REQ_SYSTEM_CPU_SETSTATE, cs, NULL,
181 NULL);
182 if (error != 0)
183 break;
184 if (cs->cs_id >= __arraycount(cpu_infos) ||
185 (ci = cpu_lookup(cs->cs_id)) == NULL) {
186 error = ESRCH;
187 break;
188 }
189 if (!cs->cs_intr) {
190 error = EOPNOTSUPP;
191 break;
192 }
193 error = cpu_setstate(ci, cs->cs_online);
194 break;
195
196 #ifdef IOC_CPU_OGETSTATE
197 case IOC_CPU_OGETSTATE: {
198 cpustate50_t *ocs = data;
199 cpustate50_to_cpustate(ocs, &csb);
200 cs = &csb;
201 error = 1;
202 /*FALLTHROUGH*/
203 }
204 #endif
205 case IOC_CPU_GETSTATE:
206 if (error == 0)
207 cs = data;
208 id = cs->cs_id;
209 memset(cs, 0, sizeof(*cs));
210 cs->cs_id = id;
211 if (cs->cs_id >= __arraycount(cpu_infos) ||
212 (ci = cpu_lookup(id)) == NULL) {
213 error = ESRCH;
214 break;
215 }
216 if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
217 cs->cs_online = false;
218 else
219 cs->cs_online = true;
220 cs->cs_intr = true;
221 cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
222 #ifdef IOC_CPU_OGETSTATE
223 if (cmd == IOC_CPU_OGETSTATE) {
224 cpustate50_t *ocs = data;
225 cpustate_to_cpustate50(cs, ocs);
226 error = 0;
227 }
228 #endif
229 break;
230
231 case IOC_CPU_MAPID:
232 i = 0;
233 for (CPU_INFO_FOREACH(cii, ci)) {
234 if (i++ == *(int *)data)
235 break;
236 }
237 if (ci == NULL)
238 error = ESRCH;
239 else
240 *(int *)data = cpu_index(ci);
241 break;
242
243 case IOC_CPU_GETCOUNT:
244 *(int *)data = ncpu;
245 break;
246
247 default:
248 error = ENOTTY;
249 break;
250 }
251 mutex_exit(&cpu_lock);
252
253 return error;
254 }
255
256 struct cpu_info *
257 cpu_lookup(u_int idx)
258 {
259 struct cpu_info *ci = cpu_infos[idx];
260
261 KASSERT(idx < __arraycount(cpu_infos));
262 KASSERT(ci == NULL || cpu_index(ci) == idx);
263
264 return ci;
265 }
266
267 static void
268 cpu_xc_offline(struct cpu_info *ci)
269 {
270 struct schedstate_percpu *spc, *mspc = NULL;
271 struct cpu_info *target_ci;
272 struct lwp *l;
273 CPU_INFO_ITERATOR cii;
274 int s;
275
276 /*
277 * Thread which sent unicast (separate context) is holding
278 * the cpu_lock for us.
279 */
280 spc = &ci->ci_schedstate;
281 s = splsched();
282 spc->spc_flags |= SPCF_OFFLINE;
283 splx(s);
284
285 /* Take the first available CPU for the migration */
286 for (CPU_INFO_FOREACH(cii, target_ci)) {
287 mspc = &target_ci->ci_schedstate;
288 if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
289 break;
290 }
291 KASSERT(target_ci != NULL);
292
293 /*
294 * Migrate all non-bound threads to the other CPU. Note that this
295 * runs from the xcall thread, thus handling of LSONPROC is not needed.
296 */
297 mutex_enter(proc_lock);
298 LIST_FOREACH(l, &alllwp, l_list) {
299 struct cpu_info *mci;
300
301 lwp_lock(l);
302 if (l->l_cpu != ci || (l->l_pflag & (LP_BOUND | LP_INTR))) {
303 lwp_unlock(l);
304 continue;
305 }
306 /* Normal case - no affinity */
307 if ((l->l_flag & LW_AFFINITY) == 0) {
308 lwp_migrate(l, target_ci);
309 continue;
310 }
311 /* Affinity is set, find an online CPU in the set */
312 KASSERT(l->l_affinity != NULL);
313 for (CPU_INFO_FOREACH(cii, mci)) {
314 mspc = &mci->ci_schedstate;
315 if ((mspc->spc_flags & SPCF_OFFLINE) == 0 &&
316 kcpuset_isset(cpu_index(mci), l->l_affinity))
317 break;
318 }
319 if (mci == NULL) {
320 lwp_unlock(l);
321 mutex_exit(proc_lock);
322 goto fail;
323 }
324 lwp_migrate(l, mci);
325 }
326 mutex_exit(proc_lock);
327
328 #ifdef __HAVE_MD_CPU_OFFLINE
329 cpu_offline_md();
330 #endif
331 return;
332 fail:
333 /* Just unset the SPCF_OFFLINE flag, caller will check */
334 s = splsched();
335 spc->spc_flags &= ~SPCF_OFFLINE;
336 splx(s);
337 }
338
339 static void
340 cpu_xc_online(struct cpu_info *ci)
341 {
342 struct schedstate_percpu *spc;
343 int s;
344
345 spc = &ci->ci_schedstate;
346 s = splsched();
347 spc->spc_flags &= ~SPCF_OFFLINE;
348 splx(s);
349 }
350
351 int
352 cpu_setstate(struct cpu_info *ci, bool online)
353 {
354 struct schedstate_percpu *spc;
355 CPU_INFO_ITERATOR cii;
356 struct cpu_info *ci2;
357 uint64_t where;
358 xcfunc_t func;
359 int nonline;
360
361 spc = &ci->ci_schedstate;
362
363 KASSERT(mutex_owned(&cpu_lock));
364
365 if (online) {
366 if ((spc->spc_flags & SPCF_OFFLINE) == 0)
367 return 0;
368 func = (xcfunc_t)cpu_xc_online;
369 ncpuonline++;
370 } else {
371 if ((spc->spc_flags & SPCF_OFFLINE) != 0)
372 return 0;
373 nonline = 0;
374 /*
375 * Ensure that at least one CPU within the processor set
376 * stays online. Revisit this later.
377 */
378 for (CPU_INFO_FOREACH(cii, ci2)) {
379 if ((ci2->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
380 continue;
381 if (ci2->ci_schedstate.spc_psid != spc->spc_psid)
382 continue;
383 nonline++;
384 }
385 if (nonline == 1)
386 return EBUSY;
387 func = (xcfunc_t)cpu_xc_offline;
388 ncpuonline--;
389 }
390
391 where = xc_unicast(0, func, ci, NULL, ci);
392 xc_wait(where);
393 if (online) {
394 KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
395 } else if ((spc->spc_flags & SPCF_OFFLINE) == 0) {
396 /* If was not set offline, then it is busy */
397 return EBUSY;
398 }
399
400 spc->spc_lastmod = time_second;
401 return 0;
402 }
403
404 bool
405 cpu_softintr_p(void)
406 {
407
408 return (curlwp->l_pflag & LP_INTR) != 0;
409 }
410