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