kern_cpu.c revision 1.11 1 /* $NetBSD: kern_cpu.c,v 1.11 2007/11/04 11:43:07 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.11 2007/11/04 11:43:07 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 /* Double-lock the runqueues */
265 if (ci < mci) {
266 spc_lock(ci);
267 spc_lock(mci);
268 } else {
269 spc_lock(mci);
270 spc_lock(ci);
271 }
272
273 /* Handle LSRUN and LSIDL cases */
274 LIST_FOREACH(l, &alllwp, l_list) {
275 if (l->l_cpu != ci || (l->l_flag & LW_BOUND))
276 continue;
277 if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
278 sched_dequeue(l);
279 l->l_cpu = mci;
280 lwp_setlock(l, mspc->spc_mutex);
281 sched_enqueue(l, false);
282 } else if (l->l_stat == LSRUN || l->l_stat == LSIDL) {
283 l->l_cpu = mci;
284 lwp_setlock(l, mspc->spc_mutex);
285 }
286 }
287 spc_unlock(ci);
288 spc_unlock(mci);
289
290 mutex_exit(&proclist_lock);
291 }
292
293 static void
294 cpu_xc_online(struct cpu_info *ci)
295 {
296 struct schedstate_percpu *spc;
297 int s;
298
299 spc = &ci->ci_schedstate;
300 s = splsched();
301 spc->spc_flags &= ~SPCF_OFFLINE;
302 splx(s);
303 }
304
305 int
306 cpu_setonline(struct cpu_info *ci, bool online)
307 {
308 struct schedstate_percpu *spc;
309 CPU_INFO_ITERATOR cii;
310 struct cpu_info *ci2;
311 uint64_t where;
312 xcfunc_t func;
313 int nonline;
314
315 spc = &ci->ci_schedstate;
316
317 KASSERT(mutex_owned(&cpu_lock));
318
319 if (online) {
320 if ((spc->spc_flags & SPCF_OFFLINE) == 0)
321 return 0;
322 func = (xcfunc_t)cpu_xc_online;
323 ncpuonline++;
324 } else {
325 if ((spc->spc_flags & SPCF_OFFLINE) != 0)
326 return 0;
327 nonline = 0;
328 for (CPU_INFO_FOREACH(cii, ci2)) {
329 nonline += ((ci2->ci_schedstate.spc_flags &
330 SPCF_OFFLINE) == 0);
331 }
332 if (nonline == 1)
333 return EBUSY;
334 func = (xcfunc_t)cpu_xc_offline;
335 ncpuonline--;
336 }
337
338 where = xc_unicast(0, func, ci, NULL, ci);
339 xc_wait(where);
340 if (online) {
341 KASSERT((spc->spc_flags & SPCF_OFFLINE) == 0);
342 } else {
343 KASSERT(spc->spc_flags & SPCF_OFFLINE);
344 }
345 spc->spc_lastmod = time_second;
346
347 return 0;
348 }
349