kern_cpu.c revision 1.6 1 /* $NetBSD: kern_cpu.c,v 1.6 2007/08/18 00:21:10 ad 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.6 2007/08/18 00:21:10 ad 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/conf.h>
74 #include <sys/cpu.h>
75 #include <sys/cpuio.h>
76 #include <sys/proc.h>
77 #include <sys/kernel.h>
78 #include <sys/kauth.h>
79
80 #include <uvm/uvm_extern.h>
81
82 void cpuctlattach(int);
83
84 dev_type_ioctl(cpuctl_ioctl);
85
86 const struct cdevsw cpuctl_cdevsw = {
87 nullopen, nullclose, nullread, nullwrite, cpuctl_ioctl,
88 nullstop, notty, nopoll, nommap, nokqfilter,
89 D_OTHER | D_MPSAFE
90 };
91
92 kmutex_t cpu_lock;
93
94 int
95 mi_cpu_attach(struct cpu_info *ci)
96 {
97 struct schedstate_percpu *spc = &ci->ci_schedstate;
98 int error;
99
100 ci->ci_index = ncpu;
101
102 mutex_init(&spc->spc_lwplock, MUTEX_SPIN, IPL_SCHED);
103 sched_cpuattach(ci);
104 uvm_cpu_attach(ci);
105
106 error = create_idle_lwp(ci);
107 if (error != 0) {
108 /* XXX revert sched_cpuattach */
109 return error;
110 }
111
112 ncpu++;
113
114 return 0;
115 }
116
117 void
118 cpuctlattach(int dummy)
119 {
120
121 }
122
123 int
124 cpuctl_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
125 {
126 CPU_INFO_ITERATOR cii;
127 cpustate_t *cs;
128 struct cpu_info *ci;
129 int error, i;
130 u_int id;
131
132 error = 0;
133
134 mutex_enter(&cpu_lock);
135 switch (cmd) {
136 case IOC_CPU_SETSTATE:
137 error = kauth_authorize_generic(l->l_cred,
138 KAUTH_GENERIC_ISSUSER, NULL);
139 if (error != 0)
140 break;
141 cs = data;
142 if ((ci = cpu_lookup(cs->cs_id)) == NULL) {
143 error = ESRCH;
144 break;
145 }
146 if (!cs->cs_intr) {
147 error = EOPNOTSUPP;
148 break;
149 }
150 error = cpu_setonline(ci, cs->cs_online);
151 break;
152
153 case IOC_CPU_GETSTATE:
154 cs = data;
155 id = cs->cs_id;
156 memset(cs, sizeof(*cs), 0);
157 cs->cs_id = id;
158 if ((ci = cpu_lookup(id)) == NULL) {
159 error = ESRCH;
160 break;
161 }
162 if ((ci->ci_schedstate.spc_flags & SPCF_OFFLINE) != 0)
163 cs->cs_online = false;
164 else
165 cs->cs_online = true;
166 cs->cs_intr = true;
167 cs->cs_lastmod = ci->ci_schedstate.spc_lastmod;
168 break;
169
170 case IOC_CPU_MAPID:
171 i = 0;
172 for (CPU_INFO_FOREACH(cii, ci)) {
173 if (i++ == *(int *)data)
174 break;
175 }
176 if (ci == NULL)
177 error = ESRCH;
178 else
179 *(int *)data = ci->ci_cpuid;
180 break;
181
182 case IOC_CPU_GETCOUNT:
183 *(int *)data = ncpu;
184 break;
185
186 default:
187 error = ENOTTY;
188 break;
189 }
190 mutex_exit(&cpu_lock);
191
192 return error;
193 }
194
195 struct cpu_info *
196 cpu_lookup(cpuid_t id)
197 {
198 CPU_INFO_ITERATOR cii;
199 struct cpu_info *ci;
200
201 for (CPU_INFO_FOREACH(cii, ci)) {
202 if (ci->ci_cpuid == id)
203 return ci;
204 }
205
206 return NULL;
207 }
208
209 int
210 cpu_setonline(struct cpu_info *ci, bool online)
211 {
212 struct schedstate_percpu *spc;
213 CPU_INFO_ITERATOR cii;
214 struct cpu_info *ci2;
215 int nonline;
216
217 spc = &ci->ci_schedstate;
218
219 KASSERT(mutex_owned(&cpu_lock));
220
221 if (online) {
222 if ((spc->spc_flags & SPCF_OFFLINE) == 0)
223 return 0;
224 spc_lock(ci);
225 spc->spc_flags &= ~SPCF_OFFLINE;
226 cpu_need_resched(ci, true);
227 spc_unlock(ci);
228 spc->spc_lastmod = time_second;
229 } else {
230 if ((spc->spc_flags & SPCF_OFFLINE) != 0)
231 return 0;
232 nonline = 0;
233 for (CPU_INFO_FOREACH(cii, ci2)) {
234 nonline += ((ci2->ci_schedstate.spc_flags &
235 SPCF_OFFLINE) == 0);
236 }
237 if (nonline == 1)
238 return EBUSY;
239 spc_lock(ci);
240 spc->spc_flags |= SPCF_OFFLINE;
241 cpu_need_resched(ci, true);
242 spc_unlock(ci);
243 do {
244 kpause("cpu", false, 1, NULL);
245 #ifdef MULTIPROCESOR
246 } while (ci->ci_curlwp != ci->ci_data.cpu_idlelwp);
247 #else
248 } while (0);
249 #endif
250 spc->spc_lastmod = time_second;
251 }
252
253 return 0;
254 }
255