cpu.c revision 1.63 1 /* $NetBSD: cpu.c,v 1.63 2012/01/12 13:28:54 reinoud 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.63 2012/01/12 13:28:54 reinoud 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 thunk_printf_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 thunk_printf_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 thunk_printf_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 thunk_printf_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 thunk_printf_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 struct pcb *pcb = lwp_getpcb(l);
228 ucontext_t *ucp = &pcb->pcb_userret_ucp;
229
230 #ifdef CPU_DEBUG
231 thunk_printf_debug("cpu_getmcontext\n");
232 #endif
233 memcpy(mcp, &ucp->uc_mcontext, sizeof(mcontext_t));
234 return;
235 }
236
237 int
238 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
239 {
240 struct pcb *pcb = lwp_getpcb(l);
241 ucontext_t *ucp = &pcb->pcb_userret_ucp;
242
243 #ifdef CPU_DEBUG
244 thunk_printf_debug("cpu_setmcontext\n");
245 #endif
246 memcpy(&ucp->uc_mcontext, mcp, sizeof(mcontext_t));
247 return 0;
248 }
249
250 void
251 cpu_idle(void)
252 {
253 struct cpu_info *ci = curcpu();
254
255 if (ci->ci_want_resched)
256 return;
257
258 thunk_idle();
259 }
260
261 void
262 cpu_lwp_free(struct lwp *l, int proc)
263 {
264 #ifdef CPU_DEBUG
265 thunk_printf_debug("cpu_lwp_free (dummy)\n");
266 #endif
267 }
268
269 void
270 cpu_lwp_free2(struct lwp *l)
271 {
272 struct pcb *pcb = lwp_getpcb(l);
273
274 #ifdef CPU_DEBUG
275 thunk_printf_debug("cpu_lwp_free2\n");
276 #endif
277
278 if (pcb == NULL)
279 return;
280
281 if (pcb->pcb_stack_userland) {
282 free(pcb->pcb_stack_userland, M_TEMP);
283 pcb->pcb_stack_userland = NULL;
284 pcb->pcb_ucp.uc_stack.ss_sp = NULL;
285 pcb->pcb_ucp.uc_stack.ss_size = 0;
286 }
287
288 if (pcb->pcb_stack_syscall) {
289 free(pcb->pcb_stack_syscall, M_TEMP);
290 pcb->pcb_syscall_ucp.uc_stack.ss_sp = NULL;
291 pcb->pcb_syscall_ucp.uc_stack.ss_size = 0;
292 }
293
294 if (pcb->pcb_stack_pagefault) {
295 free(pcb->pcb_stack_pagefault, M_TEMP);
296 pcb->pcb_pagefault_ucp.uc_stack.ss_sp = NULL;
297 pcb->pcb_pagefault_ucp.uc_stack.ss_size = 0;
298 }
299 }
300
301 static void
302 cpu_lwp_trampoline(ucontext_t *ucp, void (*func)(void *), void *arg)
303 {
304 #ifdef CPU_DEBUG
305 thunk_printf_debug("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
306 #endif
307 /* init lwp */
308 lwp_startup(curcpu()->ci_stash, curlwp);
309
310 /* actual jump */
311 thunk_makecontext(ucp, (void (*)(void)) func, 1, arg, NULL, NULL);
312 thunk_setcontext(ucp);
313 }
314
315 void
316 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
317 void (*func)(void *), void *arg)
318 {
319 struct pcb *pcb1 = lwp_getpcb(l1);
320 struct pcb *pcb2 = lwp_getpcb(l2);
321
322 #ifdef CPU_DEBUG
323 thunk_printf_debug("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
324 l1 ? l1->l_name : "none", l1,
325 l2 ? l2->l_name : "none", l2,
326 stack, (int)stacksize);
327 #endif
328
329 if (stack)
330 panic("%s: stack passed, can't handle\n", __func__);
331
332 /* copy the PCB and its switchframes from parent */
333 memcpy(pcb2, pcb1, sizeof(struct pcb));
334
335 stacksize = 2*PAGE_SIZE;
336 pcb2->pcb_stack_userland = malloc(stacksize, M_TEMP, M_WAITOK | M_ZERO);
337 pcb2->pcb_stack_syscall = malloc(stacksize, M_TEMP, M_WAITOK | M_ZERO);
338 pcb2->pcb_stack_pagefault = malloc(stacksize, M_TEMP, M_WAITOK | M_ZERO);
339
340 KASSERT(pcb2->pcb_stack_userland);
341 KASSERT(pcb2->pcb_stack_syscall);
342 KASSERT(pcb2->pcb_stack_pagefault);
343
344 if (thunk_getcontext(&pcb2->pcb_ucp))
345 panic("getcontext failed");
346
347 /* set up the ucontext for the userland switch */
348 pcb2->pcb_ucp.uc_stack.ss_sp = pcb2->pcb_stack_userland;
349 pcb2->pcb_ucp.uc_stack.ss_size = stacksize;
350 pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
351 pcb2->pcb_ucp.uc_link = &pcb2->pcb_userret_ucp;
352 thunk_makecontext(&pcb2->pcb_ucp,
353 (void (*)(void)) cpu_lwp_trampoline,
354 3, &pcb2->pcb_ucp, func, arg);
355
356 /* set up the ucontext for the syscall */
357 pcb2->pcb_syscall_ucp.uc_stack.ss_sp = pcb2->pcb_stack_syscall;
358 pcb2->pcb_syscall_ucp.uc_stack.ss_size = stacksize;
359 pcb2->pcb_syscall_ucp.uc_flags = _UC_STACK | _UC_CPU;
360 pcb2->pcb_syscall_ucp.uc_link = &pcb2->pcb_userret_ucp;
361 thunk_makecontext(&pcb2->pcb_syscall_ucp, (void (*)(void)) syscall,
362 0, NULL, NULL, NULL);
363
364 /* set up the ucontext for the pagefault */
365 pcb2->pcb_pagefault_ucp.uc_stack.ss_sp = pcb2->pcb_stack_pagefault;
366 pcb2->pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
367 pcb2->pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
368 pcb2->pcb_pagefault_ucp.uc_link = &pcb2->pcb_trapret_ucp;
369 thunk_makecontext(&pcb2->pcb_pagefault_ucp, (void (*)(void)) pagefault,
370 0, NULL, NULL, NULL);
371 }
372
373 void
374 cpu_initclocks(void)
375 {
376 extern timer_t clock_timerid;
377
378 thunk_timer_start(clock_timerid, HZ);
379 }
380
381 void
382 cpu_startup(void)
383 {
384 vaddr_t minaddr, maxaddr;
385 size_t stacksize, msgbufsize = 32 * 1024;
386
387 /* get ourself a message buffer */
388 um_msgbuf = kmem_zalloc(msgbufsize, KM_SLEEP);
389 if (um_msgbuf == NULL)
390 panic("couldn't allocate msgbuf");
391 initmsgbuf(um_msgbuf, msgbufsize);
392
393 /* allocate a submap for physio, 1Mb enough? */
394 minaddr = 0;
395 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
396 1024 * 1024, 0, false, NULL);
397
398 /* say hi! */
399 banner();
400
401 /* init lwp0 */
402 memset(&lwp0pcb, 0, sizeof(lwp0pcb));
403 if (thunk_getcontext(&lwp0pcb.pcb_ucp))
404 panic("getcontext failed");
405 uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
406
407 /* init trapframes (going nowhere!), maybe a panic func? */
408 memcpy(&lwp0pcb.pcb_syscall_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
409 memcpy(&lwp0pcb.pcb_userret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
410 memcpy(&lwp0pcb.pcb_pagefault_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
411 memcpy(&lwp0pcb.pcb_trapret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
412
413 /* set up the ucontext for the pagefault */
414 stacksize = 2*PAGE_SIZE;
415 lwp0pcb.pcb_stack_pagefault = malloc(stacksize, M_TEMP, M_WAITOK | M_ZERO);
416
417 lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_sp = lwp0pcb.pcb_stack_pagefault;
418 lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
419 lwp0pcb.pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
420 lwp0pcb.pcb_pagefault_ucp.uc_link = &lwp0pcb.pcb_userret_ucp;
421 thunk_makecontext(&lwp0pcb.pcb_pagefault_ucp, (void (*)(void)) pagefault,
422 0, NULL, NULL, NULL);
423 }
424
425 void
426 cpu_rootconf(void)
427 {
428 device_t rdev;
429
430 rdev = device_find_by_xname("ld0");
431 if (rdev == NULL)
432 rdev = device_find_by_xname("md0");
433
434 aprint_normal("boot device: %s\n",
435 rdev ? device_xname(rdev) : "<unknown>");
436 setroot(rdev, 0);
437 }
438
439 bool
440 cpu_intr_p(void)
441 {
442 int idepth;
443
444 kpreempt_disable();
445 idepth = curcpu()->ci_idepth;
446 kpreempt_enable();
447
448 return (idepth >= 0);
449 }
450