cpu.c revision 1.57 1 /* $NetBSD: cpu.c,v 1.57 2011/12/27 14:55:31 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.57 2011/12/27 14:55:31 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 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 struct pcb *pcb = lwp_getpcb(l);
228 ucontext_t *ucp = &pcb->pcb_userret_ucp;
229
230 #ifdef CPU_DEBUG
231 dprintf_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 dprintf_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 dprintf_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 dprintf_debug("cpu_lwp_free2\n");
276 #endif
277
278 if (pcb == NULL)
279 return;
280
281 if (pcb->pcb_needfree) {
282 #if 0
283 free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
284 pcb->pcb_ucp.uc_stack.ss_sp = NULL;
285 pcb->pcb_ucp.uc_stack.ss_size = 0;
286 #endif
287
288 free(pcb->pcb_syscall_ucp.uc_stack.ss_sp, M_TEMP);
289 pcb->pcb_syscall_ucp.uc_stack.ss_sp = NULL;
290 pcb->pcb_syscall_ucp.uc_stack.ss_size = 0;
291
292 free(pcb->pcb_pagefault_ucp.uc_stack.ss_sp, M_TEMP);
293 pcb->pcb_pagefault_ucp.uc_stack.ss_sp = NULL;
294 pcb->pcb_pagefault_ucp.uc_stack.ss_size = 0;
295
296 pcb->pcb_needfree = false;
297 }
298 }
299
300 static void
301 cpu_lwp_trampoline(ucontext_t *ucp, void (*func)(void *), void *arg)
302 {
303 #ifdef CPU_DEBUG
304 dprintf_debug("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
305 #endif
306 /* init lwp */
307 lwp_startup(curcpu()->ci_stash, curlwp);
308
309 /* actual jump */
310 thunk_makecontext(ucp, (void (*)(void)) func, 1, arg, NULL, NULL);
311 thunk_setcontext(ucp);
312 }
313
314 void
315 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
316 void (*func)(void *), void *arg)
317 {
318 struct pcb *pcb1 = lwp_getpcb(l1);
319 struct pcb *pcb2 = lwp_getpcb(l2);
320 void *stack_ucp, *stack_syscall_ucp, *stack_pagefault_ucp;
321
322 #ifdef CPU_DEBUG
323 dprintf_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 stack_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
337 stack_syscall_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
338 stack_pagefault_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
339 pcb2->pcb_needfree = true;
340
341 if (thunk_getcontext(&pcb2->pcb_ucp))
342 panic("getcontext failed");
343
344 /* set up the ucontext for the userland switch */
345 /* XXX BUG TODO when is this stack space freed? */
346 pcb2->pcb_ucp.uc_stack.ss_sp = stack_ucp;
347 pcb2->pcb_ucp.uc_stack.ss_size = stacksize;
348 pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
349 pcb2->pcb_ucp.uc_link = &pcb2->pcb_userret_ucp;
350 thunk_makecontext(&pcb2->pcb_ucp,
351 (void (*)(void)) cpu_lwp_trampoline,
352 3, &pcb2->pcb_ucp, func, arg);
353
354 /* set up the ucontext for the syscall */
355 pcb2->pcb_syscall_ucp.uc_stack.ss_sp = stack_syscall_ucp;
356 pcb2->pcb_syscall_ucp.uc_stack.ss_size = stacksize;
357 pcb2->pcb_syscall_ucp.uc_flags = _UC_STACK | _UC_CPU;
358 pcb2->pcb_syscall_ucp.uc_link = &pcb2->pcb_userret_ucp;
359 thunk_makecontext(&pcb2->pcb_syscall_ucp, (void (*)(void)) syscall,
360 0, NULL, NULL, NULL);
361
362 /* set up the ucontext for the pagefault */
363 pcb2->pcb_pagefault_ucp.uc_stack.ss_sp = stack_pagefault_ucp;
364 pcb2->pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
365 pcb2->pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
366 pcb2->pcb_pagefault_ucp.uc_link = &pcb2->pcb_trapret_ucp;
367 thunk_makecontext(&pcb2->pcb_pagefault_ucp, (void (*)(void)) pagefault,
368 0, NULL, NULL, NULL);
369 }
370
371 void
372 cpu_initclocks(void)
373 {
374 extern timer_t clock_timerid;
375
376 thunk_timer_start(clock_timerid, HZ);
377 }
378
379 void
380 cpu_startup(void)
381 {
382 vaddr_t minaddr, maxaddr;
383 size_t stacksize, msgbufsize = 32 * 1024;
384 void *stack_pagefault_ucp;
385
386 /* get ourself a message buffer */
387 um_msgbuf = kmem_zalloc(msgbufsize, KM_SLEEP);
388 if (um_msgbuf == NULL)
389 panic("couldn't allocate msgbuf");
390 initmsgbuf(um_msgbuf, msgbufsize);
391
392 /* allocate a submap for physio, 1Mb enough? */
393 minaddr = 0;
394 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
395 1024 * 1024, 0, false, NULL);
396
397 /* say hi! */
398 banner();
399
400 /* init lwp0 */
401 memset(&lwp0pcb, 0, sizeof(lwp0pcb));
402 if (thunk_getcontext(&lwp0pcb.pcb_ucp))
403 panic("getcontext failed");
404 uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
405
406 /* init trapframes (going nowhere!), maybe a panic func? */
407 memcpy(&lwp0pcb.pcb_syscall_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
408 memcpy(&lwp0pcb.pcb_userret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
409 memcpy(&lwp0pcb.pcb_pagefault_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
410 memcpy(&lwp0pcb.pcb_trapret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
411
412 /* set up the ucontext for the pagefault */
413 stacksize = 16*PAGE_SIZE;
414 stack_pagefault_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
415
416 lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_sp = stack_pagefault_ucp;
417 lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
418 lwp0pcb.pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
419 lwp0pcb.pcb_pagefault_ucp.uc_link = &lwp0pcb.pcb_userret_ucp;
420 thunk_makecontext(&lwp0pcb.pcb_pagefault_ucp, (void (*)(void)) pagefault,
421 0, NULL, NULL, NULL);
422 }
423
424 void
425 cpu_rootconf(void)
426 {
427 device_t rdev;
428
429 rdev = device_find_by_xname("ld0");
430 if (rdev == NULL)
431 rdev = device_find_by_xname("md0");
432
433 aprint_normal("boot device: %s\n",
434 rdev ? device_xname(rdev) : "<unknown>");
435 setroot(rdev, 0);
436 }
437
438 bool
439 cpu_intr_p(void)
440 {
441 int idepth;
442
443 kpreempt_disable();
444 idepth = curcpu()->ci_idepth;
445 kpreempt_enable();
446
447 return (idepth >= 0);
448 }
449