cpu.c revision 1.18 1 /* $NetBSD: cpu.c,v 1.18 2011/08/27 21:15:07 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 <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.18 2011/08/27 21:15:07 reinoud Exp $");
31
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/proc.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/reboot.h>
38 #include <sys/lwp.h>
39 #include <sys/cpu.h>
40 #include <sys/mbuf.h>
41 #include <sys/msgbuf.h>
42
43 #include <dev/cons.h>
44
45 #include <machine/cpu.h>
46 #include <machine/mainbus.h>
47 #include <machine/pcb.h>
48 #include <machine/thunk.h>
49
50 #include <uvm/uvm_extern.h>
51 #include <uvm/uvm_page.h>
52
53 #define CPU_DEBUG
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_HALT) {
120 printf("\n");
121 printf("The operating system has halted.\n");
122 printf("Please press any key to reboot.\n\n");
123 cnpollc(1);
124 cngetc();
125 cnpollc(0);
126 }
127
128 printf("rebooting...\n");
129
130 #if defined(DIAGNOSTIC) || defined(DEBUG)
131 thunk_abort();
132 #endif
133
134 usermode_reboot();
135
136 /* NOTREACHED */
137 }
138
139 void
140 cpu_need_resched(struct cpu_info *ci, int flags)
141 {
142 ci->ci_want_resched |= flags;
143 }
144
145 void
146 cpu_need_proftick(struct lwp *l)
147 {
148 }
149
150 lwp_t *
151 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
152 {
153 extern int errno;
154 struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
155 struct pcb *newpcb = lwp_getpcb(newlwp);
156 struct cpu_info *ci = curcpu();
157
158 #ifdef CPU_DEBUG
159 printf("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
160 oldlwp ? oldlwp->l_name : "none",
161 oldlwp ? oldlwp->l_proc->p_pid : -1,
162 oldlwp ? oldlwp->l_lid : -1,
163 newlwp ? newlwp->l_name : "none",
164 newlwp ? newlwp->l_proc->p_pid : -1,
165 newlwp ? newlwp->l_lid : -1);
166 if (oldpcb) {
167 printf(" oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
168 "uc_stack.ss_size=%d\n",
169 oldpcb->pcb_ucp.uc_link,
170 oldpcb->pcb_ucp.uc_stack.ss_sp,
171 (int)oldpcb->pcb_ucp.uc_stack.ss_size);
172 }
173 if (newpcb) {
174 printf(" newpcb uc_link=%p, uc_stack.ss_sp=%p, "
175 "uc_stack.ss_size=%d\n",
176 newpcb->pcb_ucp.uc_link,
177 newpcb->pcb_ucp.uc_stack.ss_sp,
178 (int)newpcb->pcb_ucp.uc_stack.ss_size);
179 }
180 #endif /* !CPU_DEBUG */
181
182 ci->ci_stash = oldlwp;
183 curlwp = newlwp;
184 if (oldpcb) {
185 if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
186 panic("swapcontext failed: %d", errno);
187 } else {
188 if (thunk_setcontext(&newpcb->pcb_ucp))
189 panic("setcontext failed: %d", errno);
190 }
191
192 #ifdef CPU_DEBUG
193 printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
194 #endif
195 return ci->ci_stash;
196 }
197
198 void
199 cpu_dumpconf(void)
200 {
201 #ifdef CPU_DEBUG
202 printf("cpu_dumpconf\n");
203 #endif
204 }
205
206 void
207 cpu_signotify(struct lwp *l)
208 {
209 }
210
211 void
212 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
213 {
214 #ifdef CPU_DEBUG
215 printf("cpu_getmcontext\n");
216 #endif
217 }
218
219 int
220 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
221 {
222 #ifdef CPU_DEBUG
223 printf("cpu_setmcontext\n");
224 #endif
225 return 0;
226 }
227
228 void
229 cpu_idle(void)
230 {
231 struct cpu_info *ci = curcpu();
232
233 if (ci->ci_want_resched)
234 return;
235
236 #if notyet
237 thunk_usleep(10000);
238 #endif
239 }
240
241 void
242 cpu_lwp_free(struct lwp *l, int proc)
243 {
244 #ifdef CPU_DEBUG
245 printf("cpu_lwp_free\n");
246 #endif
247 }
248
249 void
250 cpu_lwp_free2(struct lwp *l)
251 {
252 struct pcb *pcb = lwp_getpcb(l);
253
254 #ifdef CPU_DEBUG
255 printf("cpu_lwp_free2\n");
256 #endif
257
258 if (pcb == NULL)
259 return;
260
261 if (pcb->pcb_needfree) {
262 free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
263 pcb->pcb_ucp.uc_stack.ss_sp = NULL;
264 pcb->pcb_ucp.uc_stack.ss_size = 0;
265 pcb->pcb_needfree = false;
266 }
267 }
268
269 static void
270 cpu_lwp_trampoline(void (*func)(void *), void *arg)
271 {
272 #ifdef CPU_DEBUG
273 printf("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
274 #endif
275 lwp_startup(curcpu()->ci_stash, curlwp);
276
277 func(arg);
278 }
279
280 void
281 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
282 void (*func)(void *), void *arg)
283 {
284 extern int errno;
285 struct pcb *pcb = lwp_getpcb(l2);
286
287 #ifdef CPU_DEBUG
288 printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
289 l1 ? l1->l_name : "none", l1,
290 l2 ? l2->l_name : "none", l2,
291 stack, (int)stacksize);
292 #endif
293
294 /* XXXJDM */
295 if (stack == NULL) {
296 stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
297 stacksize = PAGE_SIZE;
298 pcb->pcb_needfree = true;
299 } else
300 pcb->pcb_needfree = false;
301
302 if (thunk_getcontext(&pcb->pcb_ucp))
303 panic("getcontext failed: %d", errno);
304 pcb->pcb_ucp.uc_stack.ss_sp = stack;
305 pcb->pcb_ucp.uc_stack.ss_size = stacksize;
306 pcb->pcb_ucp.uc_link = NULL;
307 pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
308 thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
309 2, func, arg);
310 }
311
312 void
313 cpu_initclocks(void)
314 {
315 }
316
317 void
318 cpu_startup(void)
319 {
320
321 msgbuf = thunk_malloc(PAGE_SIZE);
322 if (msgbuf == NULL)
323 panic("couldn't allocate msgbuf");
324 initmsgbuf(msgbuf, PAGE_SIZE);
325
326 banner();
327
328 memset(&lwp0pcb, 0, sizeof(lwp0pcb));
329 if (thunk_getcontext(&lwp0pcb.pcb_ucp))
330 panic("getcontext failed");
331 uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
332 }
333
334 void
335 cpu_rootconf(void)
336 {
337 device_t rdev;
338
339 rdev = device_find_by_xname("ld0");
340 if (rdev == NULL)
341 rdev = device_find_by_xname("md0");
342
343 aprint_normal("boot device: %s\n",
344 rdev ? device_xname(rdev) : "<unknown>");
345 setroot(rdev, 0);
346 }
347
348 bool
349 cpu_intr_p(void)
350 {
351 int idepth;
352
353 kpreempt_disable();
354 idepth = curcpu()->ci_idepth;
355 kpreempt_enable();
356
357 return (idepth >= 0);
358 }
359