kern_cpu.c revision 1.18 1 /* $NetBSD: kern_cpu.c,v 1.18 2008/01/15 03:37:10 rmind 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.18 2008/01/15 03:37:10 rmind 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/percpu.h>
79 #include <sys/kernel.h>
80 #include <sys/kauth.h>
81 #include <sys/xcall.h>
82 #include <sys/pool.h>
83
84 #include <uvm/uvm_extern.h>
85
86 void cpuctlattach(int);
87
88 static void cpu_xc_online(struct cpu_info *);
89 static void cpu_xc_offline(struct cpu_info *);
90
91 dev_type_ioctl(cpuctl_ioctl);
92
93 const struct cdevsw cpuctl_cdevsw = {
94 nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
95 nullstop, notty, nopoll, nommap, nokqfilter,
96 D_OTHER | D_MPSAFE
97 };
98
99 kmutex_t cpu_lock;
100 int ncpu;
101 int ncpuonline;
102 bool mp_online;
103
104 static struct cpu_info *cpu_infos[MAXCPUS];
105
106 int
107 mi_cpu_attach(struct cpu_info *ci)
108 {
109 struct schedstate_percpu *spc = &ci->ci_schedstate;
110 int error;
111
112 ci->ci_index = ncpu;
113
114 mutex_init(&spc->spc_lwplock, MUTEX_DEFAULT, IPL_SCHED);
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 xc_init_cpu(ci);
132 pool_cache_cpu_init(ci);
133 TAILQ_INIT(&ci->ci_data.cpu_biodone);
134 ncpu++;
135 ncpuonline++;
136 cpu_infos[cpu_index(ci)] = ci;
137
138 return 0;
139 }
140
141 void
142 cpuctlattach(int dummy)
143 {
144
145 }
146
147 int
148 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
149 {
150 CPU_INFO_ITERATOR cii;
151 cpustate_t *cs;
152 struct cpu_info *ci;
153 int error, i;
154 u_int id;
155
156 error = 0;
157
158 mutex_enter(&cpu_lock);
159 switch (cmd) {
160 case IOC_CPU_SETSTATE:
161 error = kauth_authorize_generic(l->l_cred,
162 KAUTH_GENERIC_ISSUSER, NULL);
163 if (error != 0)
164 break;
165 cs = data;
166 if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
167 error = ESRCH;
168 break;
169 }
170 if (!cs->cs_intr) {
171 error = EOPNOTSUPP;
172 break;
173 }
174 error = cpu_setonline(ci, cs->cs_online);
175 break;
176
177 case IOC_CPU_GETSTATE:
178 cs = data;
179 id = cs->cs_id;
180 memset(cs, 0, sizeof(*cs));
181 cs->cs_id = id;
182 if ((ci = cpu_lookup(id)) == NULL) {
183 error = ESRCH;
184 break;
185 }
186 if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
187 cs->cs_online = false;
188 else
189 cs->cs_online = true;
190 cs->cs_intr = true;
191 cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
192 break;
193
194 case IOC_CPU_MAPID:
195 i = 0;
196 for (CPU_INFO_FOREACH(cii, ci)) {
197 if (i++ == *(int *)data)
198 break;
199 }
200 if (ci == NULL)
201 error = ESRCH;
202 else
203 *(int *)data = ci->ci_cpuid;
204 break;
205
206 case IOC_CPU_GETCOUNT:
207 *(int *)data = ncpu;
208 break;
209
210 default:
211 error = ENOTTY;
212 break;
213 }
214 mutex_exit(&cpu_lock);
215
216 return error;
217 }
218
219 struct cpu_info *
220 cpu_lookup(cpuid_t id)
221 {
222 CPU_INFO_ITERATOR cii;
223 struct cpu_info *ci;
224
225 for (CPU_INFO_FOREACH(cii, ci)) {
226 if (ci->ci_cpuid == id)
227 return ci;
228 }
229
230 return NULL;
231 }
232
233 struct cpu_info *
234 cpu_lookup_byindex(u_int idx)
235 {
236 struct cpu_info *ci = cpu_infos[idx];
237
238 KASSERT(idx < MAXCPUS);
239 KASSERT(ci == NULL || cpu_index(ci) == idx);
240
241 return ci;
242 }
243
244 static void
245 cpu_xc_offline(struct cpu_info *ci)
246 {
247 struct schedstate_percpu *spc, *mspc = NULL;
248 struct cpu_info *mci;
249 struct lwp *l;
250 CPU_INFO_ITERATOR cii;
251 int s;
252
253 spc = &ci->ci_schedstate;
254 s = splsched();
255 spc->spc_flags |= SPCF_OFFLINE;
256 splx(s);
257
258 /* Take the first available CPU for the migration */
259 for (CPU_INFO_FOREACH(cii, mci)) {
260 mspc = &mci->ci_schedstate;
261 if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
262 break;
263 }
264 KASSERT(mci != NULL);
265
266 /*
267 * Migrate all non-bound threads to the other CPU.
268 * Please note, that this runs from the xcall thread, thus handling
269 * of LSONPROC is not needed.
270 */
271 mutex_enter(&proclist_lock);
272
273 /*
274 * Note that threads on the runqueue might sleep after this, but
275 * sched_takecpu() would migrate such threads to the appropriate CPU.
276 */
277 LIST_FOREACH(l, &alllwp, l_list) {
278 lwp_lock(l);
279 if (l->l_cpu == ci && (l->l_stat == LSSLEEP ||
280 l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED)) {
281 KASSERT((l->l_flag & LW_RUNNING) == 0);
282 l->l_cpu = mci;
283 }
284 lwp_unlock(l);
285 }
286
287 /* Double-lock the run-queues */
288 spc_dlock(ci, mci);
289
290 /* Handle LSRUN and LSIDL cases */
291 LIST_FOREACH(l, &alllwp, l_list) {
292 if (l->l_cpu != ci || (l->l_flag & LW_BOUND))
293 continue;
294 if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
295 sched_dequeue(l);
296 l->l_cpu = mci;
297 lwp_setlock(l, mspc->spc_mutex);
298 sched_enqueue(l, false);
299 } else if (l->l_stat == LSRUN || l->l_stat == LSIDL) {
300 l->l_cpu = mci;
301 lwp_setlock(l, mspc->spc_mutex);
302 }
303 }
304 spc_dunlock(ci, mci);
305 mutex_exit(&proclist_lock);
306 }
307
308 static void
309 cpu_xc_online(struct cpu_info *ci)
310 {
311 struct schedstate_percpu *spc;
312 int s;
313
314 spc = &ci->ci_schedstate;
315 s = splsched();
316 spc->spc_flags &= ~SPCF_OFFLINE;
317 splx(s);
318 }
319
320 int
321 cpu_setonline(struct cpu_info *ci, bool online)
322 {
323 struct schedstate_percpu *spc;
324 CPU_INFO_ITERATOR cii;
325 struct cpu_info *ci2;
326 uint64_t where;
327 xcfunc_t func;
328 int nonline;
329
330 spc = &ci->ci_schedstate;
331
332 KASSERT(mutex_owned(&cpu_lock));
333
334 if (online) {
335 if ((spc->spc_flags & SPCF_OFFLINE) == 0)
336 return 0;
337 func = (xcfunc_t)cpu_xc_online;
338 ncpuonline++;
339 } else {
340 if ((spc->spc_flags & SPCF_OFFLINE) != 0)
341 return 0;
342 nonline = 0;
343 for (CPU_INFO_FOREACH(cii, ci2)) {
344 nonline += ((ci2->ci_schedstate.spc_flags &
345 SPCF_OFFLINE) == 0);
346 }
347 if (nonline == 1)
348 return EBUSY;
349 func = (xcfunc_t)cpu_xc_offline;
350 ncpuonline--;
351 }
352
353 where = xc_unicast(0, func, ci, NULL, ci);
354 xc_wait(where);
355 if (online) {
356 KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
357 } else {
358 KASSERT(spc->spc_flags & SPCF_OFFLINE);
359 }
360 spc->spc_lastmod = time_second;
361
362 return 0;
363 }
364