cpu.c revision 1.15 1 /* $NetBSD: cpu.c,v 1.15 2011/08/13 14:06:54 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.15 2011/08/13 14:06:54 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/proc.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/reboot.h>
38 #include <sys/lwp.h>
39 #include <sys/cpu.h>
40 #include <sys/mbuf.h>
41
42 #include <dev/cons.h>
43
44 #include <machine/cpu.h>
45 #include <machine/mainbus.h>
46 #include <machine/pcb.h>
47 #include <machine/thunk.h>
48
49 #include <uvm/uvm_extern.h>
50 #include <uvm/uvm_page.h>
51
52 /* #define CPU_DEBUG */
53
54 static int cpu_match(device_t, cfdata_t, void *);
55 static void cpu_attach(device_t, device_t, void *);
56
57 struct cpu_info cpu_info_primary = {
58 .ci_dev = 0,
59 .ci_self = &cpu_info_primary,
60 .ci_idepth = -1,
61 .ci_curlwp = &lwp0,
62 };
63
64 char cpu_model[48] = "virtual processor";
65
66 typedef struct cpu_softc {
67 device_t sc_dev;
68 struct cpu_info *sc_ci;
69 } cpu_softc_t;
70
71 static struct pcb lwp0pcb;
72
73 CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
74
75 static int
76 cpu_match(device_t parent, cfdata_t match, void *opaque)
77 {
78 struct thunkbus_attach_args *taa = opaque;
79
80 if (taa->taa_type != THUNKBUS_TYPE_CPU)
81 return 0;
82
83 return 1;
84 }
85
86 static void
87 cpu_attach(device_t parent, device_t self, void *opaque)
88 {
89 cpu_softc_t *sc = device_private(self);
90
91 aprint_naive("\n");
92 aprint_normal("\n");
93
94 sc->sc_dev = self;
95 sc->sc_ci = &cpu_info_primary;
96 }
97
98 void
99 cpu_configure(void)
100 {
101 if (config_rootfound("mainbus", NULL) == NULL)
102 panic("configure: mainbus not configured");
103
104 spl0();
105 }
106
107 void
108 cpu_reboot(int howto, char *bootstr)
109 {
110 extern void usermode_reboot(void);
111
112 splhigh();
113
114 if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
115 thunk_exit(0);
116
117 if (howto & RB_HALT) {
118 printf("\n");
119 printf("The operating system has halted.\n");
120 printf("Please press any key to reboot.\n\n");
121 cnpollc(1);
122 cngetc();
123 cnpollc(0);
124 }
125
126 printf("rebooting...\n");
127
128 #if defined(DIAGNOSTIC) || defined(DEBUG)
129 thunk_abort();
130 #endif
131
132 usermode_reboot();
133
134 /* NOTREACHED */
135 }
136
137 void
138 cpu_need_resched(struct cpu_info *ci, int flags)
139 {
140 ci->ci_want_resched |= flags;
141 }
142
143 void
144 cpu_need_proftick(struct lwp *l)
145 {
146 }
147
148 lwp_t *
149 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
150 {
151 extern int errno;
152 struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
153 struct pcb *newpcb = lwp_getpcb(newlwp);
154 struct cpu_info *ci = curcpu();
155
156 #ifdef CPU_DEBUG
157 printf("cpu_switchto [%s] -> [%s]\n",
158 oldlwp ? oldlwp->l_name : "none",
159 newlwp ? newlwp->l_name : "none");
160 if (oldpcb) {
161 printf(" oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
162 "uc_stack.ss_size=%d\n",
163 oldpcb->pcb_ucp.uc_link,
164 oldpcb->pcb_ucp.uc_stack.ss_sp,
165 (int)oldpcb->pcb_ucp.uc_stack.ss_size);
166 }
167 if (newpcb) {
168 printf(" newpcb uc_link=%p, uc_stack.ss_sp=%p, "
169 "uc_stack.ss_size=%d\n",
170 newpcb->pcb_ucp.uc_link,
171 newpcb->pcb_ucp.uc_stack.ss_sp,
172 (int)newpcb->pcb_ucp.uc_stack.ss_size);
173 }
174 #endif /* !CPU_DEBUG */
175
176 ci->ci_stash = oldlwp;
177 curlwp = newlwp;
178 if (oldpcb) {
179 if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
180 panic("swapcontext failed: %d", errno);
181 } else {
182 if (thunk_setcontext(&newpcb->pcb_ucp))
183 panic("setcontext failed: %d", errno);
184 }
185
186 #ifdef CPU_DEBUG
187 printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
188 #endif
189 return ci->ci_stash;
190 }
191
192 void
193 cpu_dumpconf(void)
194 {
195 #ifdef CPU_DEBUG
196 printf("cpu_dumpconf\n");
197 #endif
198 }
199
200 void
201 cpu_signotify(struct lwp *l)
202 {
203 #ifdef CPU_DEBUG
204 printf("cpu_signotify\n");
205 #endif
206 }
207
208 void
209 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
210 {
211 #ifdef CPU_DEBUG
212 printf("cpu_getmcontext\n");
213 #endif
214 }
215
216 int
217 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
218 {
219 #ifdef CPU_DEBUG
220 printf("cpu_setmcontext\n");
221 #endif
222 return 0;
223 }
224
225 void
226 cpu_idle(void)
227 {
228 struct cpu_info *ci = curcpu();
229
230 if (ci->ci_want_resched)
231 return;
232
233 #if notyet
234 thunk_usleep(10000);
235 #endif
236 }
237
238 void
239 cpu_lwp_free(struct lwp *l, int proc)
240 {
241 #ifdef CPU_DEBUG
242 printf("cpu_lwp_free\n");
243 #endif
244 }
245
246 void
247 cpu_lwp_free2(struct lwp *l)
248 {
249 struct pcb *pcb = lwp_getpcb(l);
250
251 #ifdef CPU_DEBUG
252 printf("cpu_lwp_free2\n");
253 #endif
254
255 if (pcb == NULL)
256 return;
257
258 if (pcb->pcb_needfree) {
259 free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
260 pcb->pcb_ucp.uc_stack.ss_sp = NULL;
261 pcb->pcb_ucp.uc_stack.ss_size = 0;
262 pcb->pcb_needfree = false;
263 }
264 }
265
266 static void
267 cpu_lwp_trampoline(void (*func)(void *), void *arg)
268 {
269 lwp_startup(curcpu()->ci_stash, curlwp);
270
271 func(arg);
272 }
273
274 void
275 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
276 void (*func)(void *), void *arg)
277 {
278 extern int errno;
279 struct pcb *pcb = lwp_getpcb(l2);
280
281 #ifdef CPU_DEBUG
282 printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
283 l1 ? l1->l_name : "none", l1,
284 l2 ? l2->l_name : "none", l2,
285 stack, (int)stacksize);
286 #endif
287
288 /* XXXJDM */
289 if (stack == NULL) {
290 stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
291 stacksize = PAGE_SIZE;
292 pcb->pcb_needfree = true;
293 } else
294 pcb->pcb_needfree = false;
295
296 if (thunk_getcontext(&pcb->pcb_ucp))
297 panic("getcontext failed: %d", errno);
298 pcb->pcb_ucp.uc_stack.ss_sp = stack;
299 pcb->pcb_ucp.uc_stack.ss_size = stacksize;
300 pcb->pcb_ucp.uc_link = NULL;
301 pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
302 thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
303 2, func, arg);
304 }
305
306 void
307 cpu_initclocks(void)
308 {
309 }
310
311 void
312 cpu_startup(void)
313 {
314 banner();
315
316 memset(&lwp0pcb, 0, sizeof(lwp0pcb));
317 if (thunk_getcontext(&lwp0pcb.pcb_ucp))
318 panic("getcontext failed");
319 uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
320 }
321
322 void
323 cpu_rootconf(void)
324 {
325 device_t rdev;
326
327 rdev = device_find_by_xname("ld0");
328 if (rdev == NULL)
329 rdev = device_find_by_xname("md0");
330
331 aprint_normal("boot device: %s\n",
332 rdev ? device_xname(rdev) : "<unknown>");
333 setroot(rdev, 0);
334 }
335
336 bool
337 cpu_intr_p(void)
338 {
339 int idepth;
340
341 kpreempt_disable();
342 idepth = curcpu()->ci_idepth;
343 kpreempt_enable();
344
345 return (idepth >= 0);
346 }
347