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