kern_cpu.c revision 1.12 1 /* $NetBSD: kern_cpu.c,v 1.12 2007/11/05 03:36:14 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.12 2007/11/05 03:36:14 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/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 int
103 mi_cpu_attach(struct cpu_info *ci)
104 {
105 struct schedstate_percpu *spc = &ci->ci_schedstate;
106 int error;
107
108 ci->ci_index = ncpu;
109
110 mutex_init(&spc->spc_lwplock, MUTEX_SPIN, IPL_SCHED);
111 sched_cpuattach(ci);
112 uvm_cpu_attach(ci);
113
114 error = create_idle_lwp(ci);
115 if (error != 0) {
116 /* XXX revert sched_cpuattach */
117 return error;
118 }
119
120 softint_init(ci);
121 xc_init_cpu(ci);
122 TAILQ_INIT(&ci->ci_data.cpu_biodone);
123 ncpu++;
124 ncpuonline++;
125
126 return 0;
127 }
128
129 void
130 cpuctlattach(int dummy)
131 {
132
133 }
134
135 int
136 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
137 {
138 CPU_INFO_ITERATOR cii;
139 cpustate_t *cs;
140 struct cpu_info *ci;
141 int error, i;
142 u_int id;
143
144 error = 0;
145
146 mutex_enter(&cpu_lock);
147 switch (cmd) {
148 case IOC_CPU_SETSTATE:
149 error = kauth_authorize_generic(l->l_cred,
150 KAUTH_GENERIC_ISSUSER, NULL);
151 if (error != 0)
152 break;
153 cs = data;
154 if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
155 error = ESRCH;
156 break;
157 }
158 if (!cs->cs_intr) {
159 error = EOPNOTSUPP;
160 break;
161 }
162 error = cpu_setonline(ci, cs->cs_online);
163 break;
164
165 case IOC_CPU_GETSTATE:
166 cs = data;
167 id = cs->cs_id;
168 memset(cs, 0, sizeof(*cs));
169 cs->cs_id = id;
170 if ((ci = cpu_lookup(id)) == NULL) {
171 error = ESRCH;
172 break;
173 }
174 if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
175 cs->cs_online = false;
176 else
177 cs->cs_online = true;
178 cs->cs_intr = true;
179 cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
180 break;
181
182 case IOC_CPU_MAPID:
183 i = 0;
184 for (CPU_INFO_FOREACH(cii, ci)) {
185 if (i++ == *(int *)data)
186 break;
187 }
188 if (ci == NULL)
189 error = ESRCH;
190 else
191 *(int *)data = ci->ci_cpuid;
192 break;
193
194 case IOC_CPU_GETCOUNT:
195 *(int *)data = ncpu;
196 break;
197
198 default:
199 error = ENOTTY;
200 break;
201 }
202 mutex_exit(&cpu_lock);
203
204 return error;
205 }
206
207 struct cpu_info *
208 cpu_lookup(cpuid_t id)
209 {
210 CPU_INFO_ITERATOR cii;
211 struct cpu_info *ci;
212
213 for (CPU_INFO_FOREACH(cii, ci)) {
214 if (ci->ci_cpuid == id)
215 return ci;
216 }
217
218 return NULL;
219 }
220
221 static void
222 cpu_xc_offline(struct cpu_info *ci)
223 {
224 struct schedstate_percpu *spc, *mspc = NULL;
225 struct cpu_info *mci;
226 struct lwp *l;
227 CPU_INFO_ITERATOR cii;
228 int s;
229
230 spc = &ci->ci_schedstate;
231 s = splsched();
232 spc->spc_flags |= SPCF_OFFLINE;
233 splx(s);
234
235 /* Take the first available CPU for the migration */
236 for (CPU_INFO_FOREACH(cii, mci)) {
237 mspc = &mci->ci_schedstate;
238 if ((mspc->spc_flags & SPCF_OFFLINE) == 0)
239 break;
240 }
241 KASSERT(mci != NULL);
242
243 /*
244 * Migrate all non-bound threads to the other CPU.
245 * Please note, that this runs from the xcall thread, thus handling
246 * of LSONPROC is not needed.
247 */
248 mutex_enter(&proclist_lock);
249
250 /*
251 * Note that threads on the runqueue might sleep after this, but
252 * sched_takecpu() would migrate such threads to the appropriate CPU.
253 */
254 LIST_FOREACH(l, &alllwp, l_list) {
255 lwp_lock(l);
256 if (l->l_cpu == ci && (l->l_stat == LSSLEEP ||
257 l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED)) {
258 KASSERT((l->l_flag & LW_RUNNING) == 0);
259 l->l_cpu = mci;
260 }
261 lwp_unlock(l);
262 }
263
264 /*
265 * Runqueues are locked with the global lock if pointers match,
266 * thus hold only one. Otherwise, double-lock the runqueues.
267 */
268 if (spc->spc_mutex == mspc->spc_mutex) {
269 spc_lock(ci);
270 } else if (ci < mci) {
271 spc_lock(ci);
272 spc_lock(mci);
273 } else {
274 spc_lock(mci);
275 spc_lock(ci);
276 }
277
278 /* Handle LSRUN and LSIDL cases */
279 LIST_FOREACH(l, &alllwp, l_list) {
280 if (l->l_cpu != ci || (l->l_flag & LW_BOUND))
281 continue;
282 if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
283 sched_dequeue(l);
284 l->l_cpu = mci;
285 lwp_setlock(l, mspc->spc_mutex);
286 sched_enqueue(l, false);
287 } else if (l->l_stat == LSRUN || l->l_stat == LSIDL) {
288 l->l_cpu = mci;
289 lwp_setlock(l, mspc->spc_mutex);
290 }
291 }
292 if (spc->spc_mutex == mspc->spc_mutex) {
293 spc_unlock(ci);
294 } else {
295 spc_unlock(ci);
296 spc_unlock(mci);
297 }
298
299 mutex_exit(&proclist_lock);
300 }
301
302 static void
303 cpu_xc_online(struct cpu_info *ci)
304 {
305 struct schedstate_percpu *spc;
306 int s;
307
308 spc = &ci->ci_schedstate;
309 s = splsched();
310 spc->spc_flags &= ~SPCF_OFFLINE;
311 splx(s);
312 }
313
314 int
315 cpu_setonline(struct cpu_info *ci, bool online)
316 {
317 struct schedstate_percpu *spc;
318 CPU_INFO_ITERATOR cii;
319 struct cpu_info *ci2;
320 uint64_t where;
321 xcfunc_t func;
322 int nonline;
323
324 spc = &ci->ci_schedstate;
325
326 KASSERT(mutex_owned(&cpu_lock));
327
328 if (online) {
329 if ((spc->spc_flags & SPCF_OFFLINE) == 0)
330 return 0;
331 func = (xcfunc_t)cpu_xc_online;
332 ncpuonline++;
333 } else {
334 if ((spc->spc_flags & SPCF_OFFLINE) != 0)
335 return 0;
336 nonline = 0;
337 for (CPU_INFO_FOREACH(cii, ci2)) {
338 nonline += ((ci2->ci_schedstate.spc_flags &
339 SPCF_OFFLINE) == 0);
340 }
341 if (nonline == 1)
342 return EBUSY;
343 func = (xcfunc_t)cpu_xc_offline;
344 ncpuonline--;
345 }
346
347 where = xc_unicast(0, func, ci, NULL, ci);
348 xc_wait(where);
349 if (online) {
350 KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
351 } else {
352 KASSERT(spc->spc_flags & SPCF_OFFLINE);
353 }
354 spc->spc_lastmod = time_second;
355
356 return 0;
357 }
358