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