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