kern_cpu.c revision 1.21.6.3 1 /* $NetBSD: kern_cpu.c,v 1.21.6.3 2008/06/02 13:24:07 mjf 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
60 __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.21.6.3 2008/06/02 13:24:07 mjf Exp $");
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 void cpuctlattach(int);
84
85 static void cpu_xc_online(struct cpu_info *);
86 static void cpu_xc_offline(struct cpu_info *);
87
88 dev_type_ioctl(cpuctl_ioctl);
89
90 const struct cdevsw cpuctl_cdevsw = {
91 nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
92 nullstop, notty, nopoll, nommap, nokqfilter,
93 D_OTHER | D_MPSAFE
94 };
95
96 kmutex_t cpu_lock;
97 int ncpu;
98 int ncpuonline;
99 bool mp_online;
100 struct cpuqueue cpu_queue = CIRCLEQ_HEAD_INITIALIZER(cpu_queue);
101
102 static struct cpu_info *cpu_infos[MAXCPUS];
103
104 int
105 mi_cpu_attach(struct cpu_info *ci)
106 {
107 int error;
108
109 ci->ci_index = ncpu;
110 cpu_infos[cpu_index(ci)] = ci;
111 CIRCLEQ_INSERT_TAIL(&cpu_queue, ci, ci_data.cpu_qchain);
112 TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
113 __cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
114
115 sched_cpuattach(ci);
116 uvm_cpu_attach(ci);
117
118 error = create_idle_lwp(ci);
119 if (error != 0) {
120 /* XXX revert sched_cpuattach */
121 return error;
122 }
123
124 if (ci == curcpu())
125 ci->ci_data.cpu_onproc = curlwp;
126 else
127 ci->ci_data.cpu_onproc = ci->ci_data.cpu_idlelwp;
128
129 percpu_init_cpu(ci);
130 softint_init(ci);
131 callout_init_cpu(ci);
132 xc_init_cpu(ci);
133 pool_cache_cpu_init(ci);
134 selsysinit(ci);
135 cache_cpu_init(ci);
136 TAILQ_INIT(&ci->ci_data.cpu_biodone);
137 ncpu++;
138 ncpuonline++;
139
140 return 0;
141 }
142
143 void
144 cpuctlattach(int dummy)
145 {
146 int maj = cdevsw_lookup_major(&cpuctl_cdevsw);
147 device_register_name(makedev(maj, 0), NULL, true, DEV_OTHER, "cpuctl");
148 }
149
150 int
151 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
152 {
153 CPU_INFO_ITERATOR cii;
154 cpustate_t *cs;
155 struct cpu_info *ci;
156 int error, i;
157 u_int id;
158
159 error = 0;
160
161 mutex_enter(&cpu_lock);
162 switch (cmd) {
163 case IOC_CPU_SETSTATE:
164 cs = data;
165 error = kauth_authorize_system(l->l_cred,
166 KAUTH_SYSTEM_CPU, KAUTH_REQ_SYSTEM_CPU_SETSTATE, cs, NULL,
167 NULL);
168 if (error != 0)
169 break;
170 if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
171 error = ESRCH;
172 break;
173 }
174 if (!cs->cs_intr) {
175 error = EOPNOTSUPP;
176 break;
177 }
178 error = cpu_setonline(ci, cs->cs_online);
179 break;
180
181 case IOC_CPU_GETSTATE:
182 cs = data;
183 id = cs->cs_id;
184 memset(cs, 0, sizeof(*cs));
185 cs->cs_id = id;
186 if ((ci = cpu_lookup(id)) == NULL) {
187 error = ESRCH;
188 break;
189 }
190 if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
191 cs->cs_online = false;
192 else
193 cs->cs_online = true;
194 cs->cs_intr = true;
195 cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
196 break;
197
198 case IOC_CPU_MAPID:
199 i = 0;
200 for (CPU_INFO_FOREACH(cii, ci)) {
201 if (i++ == *(int *)data)
202 break;
203 }
204 if (ci == NULL)
205 error = ESRCH;
206 else
207 *(int *)data = ci->ci_cpuid;
208 break;
209
210 case IOC_CPU_GETCOUNT:
211 *(int *)data = ncpu;
212 break;
213
214 default:
215 error = ENOTTY;
216 break;
217 }
218 mutex_exit(&cpu_lock);
219
220 return error;
221 }
222
223 struct cpu_info *
224 cpu_lookup(cpuid_t id)
225 {
226 CPU_INFO_ITERATOR cii;
227 struct cpu_info *ci;
228
229 for (CPU_INFO_FOREACH(cii, ci)) {
230 if (ci->ci_cpuid == id)
231 return ci;
232 }
233
234 return NULL;
235 }
236
237 struct cpu_info *
238 cpu_lookup_byindex(u_int idx)
239 {
240 struct cpu_info *ci = cpu_infos[idx];
241
242 KASSERT(idx < MAXCPUS);
243 KASSERT(ci == NULL || cpu_index(ci) == idx);
244
245 return ci;
246 }
247
248 static void
249 cpu_xc_offline(struct cpu_info *ci)
250 {
251 struct schedstate_percpu *spc, *mspc = NULL;
252 struct cpu_info *mci;
253 struct lwp *l;
254 CPU_INFO_ITERATOR cii;
255 int s;
256
257 spc = &ci->ci_schedstate;
258 s = splsched();
259 spc->spc_flags |= SPCF_OFFLINE;
260 splx(s);
261
262 /* Take the first available CPU for the migration */
263 for (CPU_INFO_FOREACH(cii, mci)) {
264 mspc = &mci->ci_schedstate;
265 if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
266 break;
267 }
268 KASSERT(mci != NULL);
269
270 /*
271 * Migrate all non-bound threads to the other CPU.
272 *
273 * Please note, that this runs from the xcall thread, thus handling
274 * of LSONPROC is not needed. Threads which change the state will
275 * be handled by sched_takecpu().
276 */
277 mutex_enter(proc_lock);
278 spc_dlock(ci, mci);
279 LIST_FOREACH(l, &alllwp, l_list) {
280 lwp_lock(l);
281 if (l->l_cpu != ci || (l->l_pflag & LP_BOUND) != 0) {
282 lwp_unlock(l);
283 continue;
284 }
285 if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
286 sched_dequeue(l);
287 l->l_cpu = mci;
288 lwp_setlock(l, mspc->spc_mutex);
289 sched_enqueue(l, false);
290 lwp_unlock(l);
291 } else {
292 lwp_migrate(l, mci);
293 }
294 }
295 spc_dunlock(ci, mci);
296 mutex_exit(proc_lock);
297
298 #ifdef __HAVE_MD_CPU_OFFLINE
299 cpu_offline_md();
300 #endif
301 }
302
303 static void
304 cpu_xc_online(struct cpu_info *ci)
305 {
306 struct schedstate_percpu *spc;
307 int s;
308
309 spc = &ci->ci_schedstate;
310 s = splsched();
311 spc->spc_flags &= ~SPCF_OFFLINE;
312 splx(s);
313 }
314
315 int
316 cpu_setonline(struct cpu_info *ci, bool online)
317 {
318 struct schedstate_percpu *spc;
319 CPU_INFO_ITERATOR cii;
320 struct cpu_info *ci2;
321 uint64_t where;
322 xcfunc_t func;
323 int nonline;
324
325 spc = &ci->ci_schedstate;
326
327 KASSERT(mutex_owned(&cpu_lock));
328
329 if (online) {
330 if ((spc->spc_flags & SPCF_OFFLINE) == 0)
331 return 0;
332 func = (xcfunc_t)cpu_xc_online;
333 ncpuonline++;
334 } else {
335 if ((spc->spc_flags & SPCF_OFFLINE) != 0)
336 return 0;
337 nonline = 0;
338 for (CPU_INFO_FOREACH(cii, ci2)) {
339 nonline += ((ci2->ci_schedstate.spc_flags &
340 SPCF_OFFLINE) == 0);
341 }
342 if (nonline == 1)
343 return EBUSY;
344 func = (xcfunc_t)cpu_xc_offline;
345 ncpuonline--;
346 }
347
348 where = xc_unicast(0, func, ci, NULL, ci);
349 xc_wait(where);
350 if (online) {
351 KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
352 } else {
353 KASSERT(spc->spc_flags & SPCF_OFFLINE);
354 }
355 spc->spc_lastmod = time_second;
356
357 return 0;
358 }
359