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