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