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