cpu.c revision 1.55 1 /* $NetBSD: cpu.c,v 1.55 2011/12/15 03:42:32 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 "opt_cpu.h"
30 #include "opt_hz.h"
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.55 2011/12/15 03:42:32 jmcneill Exp $");
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/reboot.h>
41 #include <sys/lwp.h>
42 #include <sys/cpu.h>
43 #include <sys/mbuf.h>
44 #include <sys/msgbuf.h>
45 #include <sys/kmem.h>
46
47 #include <dev/cons.h>
48
49 #include <machine/cpu.h>
50 #include <machine/mainbus.h>
51 #include <machine/pcb.h>
52 #include <machine/machdep.h>
53 #include <machine/thunk.h>
54
55 #include <uvm/uvm_extern.h>
56 #include <uvm/uvm_page.h>
57
58 #if __GNUC_PREREQ__(4,4)
59 #define cpu_unreachable() __builtin_unreachable()
60 #else
61 #define cpu_unreachable() do { thunk_abort(); } while (0)
62 #endif
63
64 static int cpu_match(device_t, cfdata_t, void *);
65 static void cpu_attach(device_t, device_t, void *);
66
67 struct cpu_info cpu_info_primary = {
68 .ci_dev = 0,
69 .ci_self = &cpu_info_primary,
70 .ci_idepth = -1,
71 .ci_curlwp = &lwp0,
72 };
73
74 char cpu_model[48] = "virtual processor";
75
76 typedef struct cpu_softc {
77 device_t sc_dev;
78 struct cpu_info *sc_ci;
79 } cpu_softc_t;
80
81 static struct pcb lwp0pcb;
82 static void *um_msgbuf;
83
84 CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
85
86 static int
87 cpu_match(device_t parent, cfdata_t match, void *opaque)
88 {
89 struct thunkbus_attach_args *taa = opaque;
90
91 if (taa->taa_type != THUNKBUS_TYPE_CPU)
92 return 0;
93
94 return 1;
95 }
96
97 static void
98 cpu_attach(device_t parent, device_t self, void *opaque)
99 {
100 cpu_softc_t *sc = device_private(self);
101
102 aprint_naive("\n");
103 aprint_normal("\n");
104
105 sc->sc_dev = self;
106 sc->sc_ci = &cpu_info_primary;
107 }
108
109 void
110 cpu_configure(void)
111 {
112 if (config_rootfound("mainbus", NULL) == NULL)
113 panic("configure: mainbus not configured");
114
115 spl0();
116 }
117
118 void
119 cpu_reboot(int howto, char *bootstr)
120 {
121 extern void usermode_reboot(void);
122
123 splhigh();
124
125 if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
126 thunk_exit(0);
127
128 if (howto & RB_DUMP)
129 thunk_abort();
130
131 if (howto & RB_HALT) {
132 printf("\n");
133 printf("The operating system has halted.\n");
134 printf("Please press any key to reboot.\n\n");
135 cnpollc(1);
136 cngetc();
137 cnpollc(0);
138 }
139
140 printf("rebooting...\n");
141
142 usermode_reboot();
143
144 /* NOTREACHED */
145 cpu_unreachable();
146 }
147
148 void
149 cpu_need_resched(struct cpu_info *ci, int flags)
150 {
151 ci->ci_want_resched |= flags;
152 }
153
154 void
155 cpu_need_proftick(struct lwp *l)
156 {
157 }
158
159 lwp_t *
160 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
161 {
162 struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
163 struct pcb *newpcb = lwp_getpcb(newlwp);
164 struct cpu_info *ci = curcpu();
165
166 #ifdef CPU_DEBUG
167 dprintf_debug("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
168 oldlwp ? oldlwp->l_name : "none",
169 oldlwp ? oldlwp->l_proc->p_pid : -1,
170 oldlwp ? oldlwp->l_lid : -1,
171 newlwp ? newlwp->l_name : "none",
172 newlwp ? newlwp->l_proc->p_pid : -1,
173 newlwp ? newlwp->l_lid : -1);
174 if (oldpcb) {
175 dprintf_debug(" oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
176 "uc_stack.ss_size=%d\n",
177 oldpcb->pcb_ucp.uc_link,
178 oldpcb->pcb_ucp.uc_stack.ss_sp,
179 (int)oldpcb->pcb_ucp.uc_stack.ss_size);
180 }
181 if (newpcb) {
182 dprintf_debug(" newpcb uc_link=%p, uc_stack.ss_sp=%p, "
183 "uc_stack.ss_size=%d\n",
184 newpcb->pcb_ucp.uc_link,
185 newpcb->pcb_ucp.uc_stack.ss_sp,
186 (int)newpcb->pcb_ucp.uc_stack.ss_size);
187 }
188 #endif /* !CPU_DEBUG */
189
190 ci->ci_stash = oldlwp;
191
192 if (oldpcb) {
193 oldpcb->pcb_errno = thunk_geterrno();
194 thunk_seterrno(newpcb->pcb_errno);
195 curlwp = newlwp;
196 if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
197 panic("swapcontext failed");
198 } else {
199 thunk_seterrno(newpcb->pcb_errno);
200 curlwp = newlwp;
201 if (thunk_setcontext(&newpcb->pcb_ucp))
202 panic("setcontext failed");
203 }
204
205 #ifdef CPU_DEBUG
206 dprintf_debug("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
207 #endif
208 return ci->ci_stash;
209 }
210
211 void
212 cpu_dumpconf(void)
213 {
214 #ifdef CPU_DEBUG
215 dprintf_debug("cpu_dumpconf\n");
216 #endif
217 }
218
219 void
220 cpu_signotify(struct lwp *l)
221 {
222 }
223
224 void
225 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
226 {
227 panic("cpu_getmcontext");
228 #ifdef CPU_DEBUG
229 dprintf_debug("cpu_getmcontext\n");
230 #endif
231 }
232
233 int
234 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
235 {
236 panic("cpu_setmcontext");
237 #ifdef CPU_DEBUG
238 dprintf_debug("cpu_setmcontext\n");
239 #endif
240 return 0;
241 }
242
243 void
244 cpu_idle(void)
245 {
246 struct cpu_info *ci = curcpu();
247
248 if (ci->ci_want_resched)
249 return;
250
251 thunk_idle();
252 }
253
254 void
255 cpu_lwp_free(struct lwp *l, int proc)
256 {
257 #ifdef CPU_DEBUG
258 dprintf_debug("cpu_lwp_free (dummy)\n");
259 #endif
260 }
261
262 void
263 cpu_lwp_free2(struct lwp *l)
264 {
265 struct pcb *pcb = lwp_getpcb(l);
266
267 #ifdef CPU_DEBUG
268 dprintf_debug("cpu_lwp_free2\n");
269 #endif
270
271 if (pcb == NULL)
272 return;
273
274 if (pcb->pcb_needfree) {
275 #if 0
276 free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
277 pcb->pcb_ucp.uc_stack.ss_sp = NULL;
278 pcb->pcb_ucp.uc_stack.ss_size = 0;
279 #endif
280
281 free(pcb->pcb_syscall_ucp.uc_stack.ss_sp, M_TEMP);
282 pcb->pcb_syscall_ucp.uc_stack.ss_sp = NULL;
283 pcb->pcb_syscall_ucp.uc_stack.ss_size = 0;
284
285 free(pcb->pcb_pagefault_ucp.uc_stack.ss_sp, M_TEMP);
286 pcb->pcb_pagefault_ucp.uc_stack.ss_sp = NULL;
287 pcb->pcb_pagefault_ucp.uc_stack.ss_size = 0;
288
289 pcb->pcb_needfree = false;
290 }
291 }
292
293 static void
294 cpu_lwp_trampoline(ucontext_t *ucp, void (*func)(void *), void *arg)
295 {
296 #ifdef CPU_DEBUG
297 dprintf_debug("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
298 #endif
299 /* init lwp */
300 lwp_startup(curcpu()->ci_stash, curlwp);
301
302 /* actual jump */
303 thunk_makecontext(ucp, (void (*)(void)) func, 1, arg, NULL, NULL);
304 thunk_setcontext(ucp);
305 }
306
307 void
308 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
309 void (*func)(void *), void *arg)
310 {
311 struct pcb *pcb1 = lwp_getpcb(l1);
312 struct pcb *pcb2 = lwp_getpcb(l2);
313 void *stack_ucp, *stack_syscall_ucp, *stack_pagefault_ucp;
314
315 #ifdef CPU_DEBUG
316 dprintf_debug("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
317 l1 ? l1->l_name : "none", l1,
318 l2 ? l2->l_name : "none", l2,
319 stack, (int)stacksize);
320 #endif
321
322 if (stack)
323 panic("%s: stack passed, can't handle\n", __func__);
324
325 /* copy the PCB and its switchframes from parent */
326 memcpy(pcb2, pcb1, sizeof(struct pcb));
327
328 stacksize = 2*PAGE_SIZE;
329 stack_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
330 stack_syscall_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
331 stack_pagefault_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
332 pcb2->pcb_needfree = true;
333
334 if (thunk_getcontext(&pcb2->pcb_ucp))
335 panic("getcontext failed");
336
337 /* set up the ucontext for the userland switch */
338 /* XXX BUG TODO when is this stack space freed? */
339 pcb2->pcb_ucp.uc_stack.ss_sp = stack_ucp;
340 pcb2->pcb_ucp.uc_stack.ss_size = stacksize;
341 pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
342 pcb2->pcb_ucp.uc_link = &pcb2->pcb_userret_ucp;
343 thunk_makecontext(&pcb2->pcb_ucp,
344 (void (*)(void)) cpu_lwp_trampoline,
345 3, &pcb2->pcb_ucp, func, arg);
346
347 /* set up the ucontext for the syscall */
348 pcb2->pcb_syscall_ucp.uc_stack.ss_sp = stack_syscall_ucp;
349 pcb2->pcb_syscall_ucp.uc_stack.ss_size = stacksize;
350 pcb2->pcb_syscall_ucp.uc_flags = _UC_STACK | _UC_CPU;
351 pcb2->pcb_syscall_ucp.uc_link = &pcb2->pcb_userret_ucp;
352 thunk_makecontext(&pcb2->pcb_syscall_ucp, (void (*)(void)) syscall,
353 0, NULL, NULL, NULL);
354
355 /* set up the ucontext for the pagefault */
356 pcb2->pcb_pagefault_ucp.uc_stack.ss_sp = stack_pagefault_ucp;
357 pcb2->pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
358 pcb2->pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
359 pcb2->pcb_pagefault_ucp.uc_link = &pcb2->pcb_trapret_ucp;
360 thunk_makecontext(&pcb2->pcb_pagefault_ucp, (void (*)(void)) pagefault,
361 0, NULL, NULL, NULL);
362 }
363
364 void
365 cpu_initclocks(void)
366 {
367 extern timer_t clock_timerid;
368
369 thunk_timer_start(clock_timerid, HZ);
370 }
371
372 void
373 cpu_startup(void)
374 {
375 size_t stacksize, msgbufsize = 32 * 1024;
376 void *stack_pagefault_ucp;
377
378 um_msgbuf = kmem_zalloc(msgbufsize, KM_SLEEP);
379 if (um_msgbuf == NULL)
380 panic("couldn't allocate msgbuf");
381 initmsgbuf(um_msgbuf, msgbufsize);
382
383 banner();
384
385 memset(&lwp0pcb, 0, sizeof(lwp0pcb));
386 if (thunk_getcontext(&lwp0pcb.pcb_ucp))
387 panic("getcontext failed");
388 uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
389
390 /* init trapframes (going nowhere!), maybe a panic func? */
391 memcpy(&lwp0pcb.pcb_syscall_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
392 memcpy(&lwp0pcb.pcb_userret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
393 memcpy(&lwp0pcb.pcb_pagefault_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
394 memcpy(&lwp0pcb.pcb_trapret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
395
396 /* set up the ucontext for the pagefault */
397 stacksize = 16*PAGE_SIZE;
398 stack_pagefault_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
399
400 lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_sp = stack_pagefault_ucp;
401 lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
402 lwp0pcb.pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
403 lwp0pcb.pcb_pagefault_ucp.uc_link = &lwp0pcb.pcb_userret_ucp;
404 thunk_makecontext(&lwp0pcb.pcb_pagefault_ucp, (void (*)(void)) pagefault,
405 0, NULL, NULL, NULL);
406 }
407
408 void
409 cpu_rootconf(void)
410 {
411 device_t rdev;
412
413 rdev = device_find_by_xname("ld0");
414 if (rdev == NULL)
415 rdev = device_find_by_xname("md0");
416
417 aprint_normal("boot device: %s\n",
418 rdev ? device_xname(rdev) : "<unknown>");
419 setroot(rdev, 0);
420 }
421
422 bool
423 cpu_intr_p(void)
424 {
425 int idepth;
426
427 kpreempt_disable();
428 idepth = curcpu()->ci_idepth;
429 kpreempt_enable();
430
431 return (idepth >= 0);
432 }
433