kern_cpu.c revision 1.16 1 /* $NetBSD: kern_cpu.c,v 1.16 2007/12/22 03:26:34 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2007 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c)2007 YAMAMOTO Takashi,
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66
67 __KERNEL_RCSID(0, "$NetBSD: kern_cpu.c,v 1.16 2007/12/22 03:26:34 yamt Exp $");
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/idle.h>
72 #include <sys/sched.h>
73 #include <sys/intr.h>
74 #include <sys/conf.h>
75 #include <sys/cpu.h>
76 #include <sys/cpuio.h>
77 #include <sys/proc.h>
78 #include <sys/kernel.h>
79 #include <sys/kauth.h>
80 #include <sys/xcall.h>
81 #include <sys/pool.h>
82
83 #include <uvm/uvm_extern.h>
84
85 void cpuctlattach(int);
86
87 static void cpu_xc_online(struct cpu_info *);
88 static void cpu_xc_offline(struct cpu_info *);
89
90 dev_type_ioctl(cpuctl_ioctl);
91
92 const struct cdevsw cpuctl_cdevsw = {
93 nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
94 nullstop, notty, nopoll, nommap, nokqfilter,
95 D_OTHER | D_MPSAFE
96 };
97
98 kmutex_t cpu_lock;
99 int ncpu;
100 int ncpuonline;
101
102 static struct cpu_info *cpu_infos[MAXCPUS];
103
104 int
105 mi_cpu_attach(struct cpu_info *ci)
106 {
107 struct schedstate_percpu *spc = &ci->ci_schedstate;
108 int error;
109
110 ci->ci_index = ncpu;
111
112 mutex_init(&spc->spc_lwplock, MUTEX_DEFAULT, IPL_SCHED);
113 sched_cpuattach(ci);
114 uvm_cpu_attach(ci);
115
116 error = create_idle_lwp(ci);
117 if (error != 0) {
118 /* XXX revert sched_cpuattach */
119 return error;
120 }
121
122 if (ci == curcpu())
123 ci->ci_data.cpu_onproc = curlwp;
124 else
125 ci->ci_data.cpu_onproc = ci->ci_data.cpu_idlelwp;
126
127 softint_init(ci);
128 xc_init_cpu(ci);
129 pool_cache_cpu_init(ci);
130 TAILQ_INIT(&ci->ci_data.cpu_biodone);
131 ncpu++;
132 ncpuonline++;
133 cpu_infos[cpu_index(ci)] = ci;
134
135 return 0;
136 }
137
138 void
139 cpuctlattach(int dummy)
140 {
141
142 }
143
144 int
145 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
146 {
147 CPU_INFO_ITERATOR cii;
148 cpustate_t *cs;
149 struct cpu_info *ci;
150 int error, i;
151 u_int id;
152
153 error = 0;
154
155 mutex_enter(&cpu_lock);
156 switch (cmd) {
157 case IOC_CPU_SETSTATE:
158 error = kauth_authorize_generic(l->l_cred,
159 KAUTH_GENERIC_ISSUSER, NULL);
160 if (error != 0)
161 break;
162 cs = data;
163 if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
164 error = ESRCH;
165 break;
166 }
167 if (!cs->cs_intr) {
168 error = EOPNOTSUPP;
169 break;
170 }
171 error = cpu_setonline(ci, cs->cs_online);
172 break;
173
174 case IOC_CPU_GETSTATE:
175 cs = data;
176 id = cs->cs_id;
177 memset(cs, 0, sizeof(*cs));
178 cs->cs_id = id;
179 if ((ci = cpu_lookup(id)) == NULL) {
180 error = ESRCH;
181 break;
182 }
183 if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
184 cs->cs_online = false;
185 else
186 cs->cs_online = true;
187 cs->cs_intr = true;
188 cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
189 break;
190
191 case IOC_CPU_MAPID:
192 i = 0;
193 for (CPU_INFO_FOREACH(cii, ci)) {
194 if (i++ == *(int *)data)
195 break;
196 }
197 if (ci == NULL)
198 error = ESRCH;
199 else
200 *(int *)data = ci->ci_cpuid;
201 break;
202
203 case IOC_CPU_GETCOUNT:
204 *(int *)data = ncpu;
205 break;
206
207 default:
208 error = ENOTTY;
209 break;
210 }
211 mutex_exit(&cpu_lock);
212
213 return error;
214 }
215
216 struct cpu_info *
217 cpu_lookup(cpuid_t id)
218 {
219 CPU_INFO_ITERATOR cii;
220 struct cpu_info *ci;
221
222 for (CPU_INFO_FOREACH(cii, ci)) {
223 if (ci->ci_cpuid == id)
224 return ci;
225 }
226
227 return NULL;
228 }
229
230 struct cpu_info *
231 cpu_lookup_byindex(u_int idx)
232 {
233 struct cpu_info *ci = cpu_infos[idx];
234
235 KASSERT(idx < MAXCPUS);
236 KASSERT(ci == NULL || cpu_index(ci) == idx);
237
238 return ci;
239 }
240
241 static void
242 cpu_xc_offline(struct cpu_info *ci)
243 {
244 struct schedstate_percpu *spc, *mspc = NULL;
245 struct cpu_info *mci;
246 struct lwp *l;
247 CPU_INFO_ITERATOR cii;
248 int s;
249
250 spc = &ci->ci_schedstate;
251 s = splsched();
252 spc->spc_flags |= SPCF_OFFLINE;
253 splx(s);
254
255 /* Take the first available CPU for the migration */
256 for (CPU_INFO_FOREACH(cii, mci)) {
257 mspc = &mci->ci_schedstate;
258 if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
259 break;
260 }
261 KASSERT(mci != NULL);
262
263 /*
264 * Migrate all non-bound threads to the other CPU.
265 * Please note, that this runs from the xcall thread, thus handling
266 * of LSONPROC is not needed.
267 */
268 mutex_enter(&proclist_lock);
269
270 /*
271 * Note that threads on the runqueue might sleep after this, but
272 * sched_takecpu() would migrate such threads to the appropriate CPU.
273 */
274 LIST_FOREACH(l, &alllwp, l_list) {
275 lwp_lock(l);
276 if (l->l_cpu == ci && (l->l_stat == LSSLEEP ||
277 l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED)) {
278 KASSERT((l->l_flag & LW_RUNNING) == 0);
279 l->l_cpu = mci;
280 }
281 lwp_unlock(l);
282 }
283
284 /*
285 * Runqueues are locked with the global lock if pointers match,
286 * thus hold only one. Otherwise, double-lock the runqueues.
287 */
288 if (spc->spc_mutex == mspc->spc_mutex) {
289 spc_lock(ci);
290 } else if (ci < mci) {
291 spc_lock(ci);
292 spc_lock(mci);
293 } else {
294 spc_lock(mci);
295 spc_lock(ci);
296 }
297
298 /* Handle LSRUN and LSIDL cases */
299 LIST_FOREACH(l, &alllwp, l_list) {
300 if (l->l_cpu != ci || (l->l_flag & LW_BOUND))
301 continue;
302 if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
303 sched_dequeue(l);
304 l->l_cpu = mci;
305 lwp_setlock(l, mspc->spc_mutex);
306 sched_enqueue(l, false);
307 } else if (l->l_stat == LSRUN || l->l_stat == LSIDL) {
308 l->l_cpu = mci;
309 lwp_setlock(l, mspc->spc_mutex);
310 }
311 }
312 if (spc->spc_mutex == mspc->spc_mutex) {
313 spc_unlock(ci);
314 } else {
315 spc_unlock(ci);
316 spc_unlock(mci);
317 }
318
319 mutex_exit(&proclist_lock);
320 }
321
322 static void
323 cpu_xc_online(struct cpu_info *ci)
324 {
325 struct schedstate_percpu *spc;
326 int s;
327
328 spc = &ci->ci_schedstate;
329 s = splsched();
330 spc->spc_flags &= ~SPCF_OFFLINE;
331 splx(s);
332 }
333
334 int
335 cpu_setonline(struct cpu_info *ci, bool online)
336 {
337 struct schedstate_percpu *spc;
338 CPU_INFO_ITERATOR cii;
339 struct cpu_info *ci2;
340 uint64_t where;
341 xcfunc_t func;
342 int nonline;
343
344 spc = &ci->ci_schedstate;
345
346 KASSERT(mutex_owned(&cpu_lock));
347
348 if (online) {
349 if ((spc->spc_flags & SPCF_OFFLINE) == 0)
350 return 0;
351 func = (xcfunc_t)cpu_xc_online;
352 ncpuonline++;
353 } else {
354 if ((spc->spc_flags & SPCF_OFFLINE) != 0)
355 return 0;
356 nonline = 0;
357 for (CPU_INFO_FOREACH(cii, ci2)) {
358 nonline += ((ci2->ci_schedstate.spc_flags &
359 SPCF_OFFLINE) == 0);
360 }
361 if (nonline == 1)
362 return EBUSY;
363 func = (xcfunc_t)cpu_xc_offline;
364 ncpuonline--;
365 }
366
367 where = xc_unicast(0, func, ci, NULL, ci);
368 xc_wait(where);
369 if (online) {
370 KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
371 } else {
372 KASSERT(spc->spc_flags & SPCF_OFFLINE);
373 }
374 spc->spc_lastmod = time_second;
375
376 return 0;
377 }
378