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