cpu.c revision 1.11 1 /* $NetBSD: cpu.c,v 1.11 2011/08/12 11:37:04 jmcneill 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.11 2011/08/12 11:37:04 jmcneill 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
42 #include <dev/cons.h>
43
44 #include <machine/cpu.h>
45 #include <machine/mainbus.h>
46 #include <machine/pcb.h>
47 #include <machine/thunk.h>
48
49 #include <uvm/uvm_extern.h>
50 #include <uvm/uvm_page.h>
51
52 /* #define CPU_DEBUG */
53
54 static int cpu_match(device_t, cfdata_t, void *);
55 static void cpu_attach(device_t, device_t, void *);
56
57 struct cpu_info cpu_info_primary;
58 char cpu_model[48] = "virtual processor";
59
60 typedef struct cpu_softc {
61 device_t sc_dev;
62 struct cpu_info *sc_ci;
63 } cpu_softc_t;
64
65 static ucontext_t lwp0pcb;
66
67 CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
68
69 static int
70 cpu_match(device_t parent, cfdata_t match, void *opaque)
71 {
72 struct thunkbus_attach_args *taa = opaque;
73
74 if (taa->taa_type != THUNKBUS_TYPE_CPU)
75 return 0;
76
77 return 1;
78 }
79
80 static void
81 cpu_attach(device_t parent, device_t self, void *opaque)
82 {
83 cpu_softc_t *sc = device_private(self);
84
85 aprint_naive("\n");
86 aprint_normal("\n");
87
88 sc->sc_dev = self;
89 sc->sc_ci = &cpu_info_primary;
90 sc->sc_ci->ci_dev = 0;
91 sc->sc_ci->ci_self = &cpu_info_primary;
92 sc->sc_ci->ci_curlwp = &lwp0;
93
94 if (thunk_getcontext(&lwp0pcb))
95 panic("getcontext failed");
96 uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
97 }
98
99 void
100 cpu_configure(void)
101 {
102 if (config_rootfound("mainbus", NULL) == NULL)
103 panic("configure: mainbus not configured");
104
105 spl0();
106 }
107
108 void
109 cpu_reboot(int howto, char *bootstr)
110 {
111 extern void usermode_reboot(void);
112
113 splhigh();
114
115 if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
116 thunk_exit(0);
117
118 if (howto & RB_HALT) {
119 printf("\n");
120 printf("The operating system has halted.\n");
121 printf("Please press any key to reboot.\n\n");
122 cnpollc(1);
123 cngetc();
124 cnpollc(0);
125 }
126
127 printf("rebooting...\n");
128
129 usermode_reboot();
130
131 /* NOTREACHED */
132 }
133
134 void
135 cpu_need_resched(struct cpu_info *ci, int flags)
136 {
137 ci->ci_want_resched |= flags;
138 }
139
140 void
141 cpu_need_proftick(struct lwp *l)
142 {
143 }
144
145 lwp_t *
146 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
147 {
148 extern int errno;
149 struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
150 struct pcb *newpcb = lwp_getpcb(newlwp);
151 struct cpu_info *ci = curcpu();
152
153 #ifdef CPU_DEBUG
154 printf("cpu_switchto [%s] -> [%s]\n",
155 oldlwp ? oldlwp->l_name : "none",
156 newlwp ? newlwp->l_name : "none");
157 if (oldpcb) {
158 printf(" oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
159 "uc_stack.ss_size=%d\n",
160 oldpcb->pcb_ucp.uc_link,
161 oldpcb->pcb_ucp.uc_stack.ss_sp,
162 (int)oldpcb->pcb_ucp.uc_stack.ss_size);
163 }
164 if (newpcb) {
165 printf(" newpcb uc_link=%p, uc_stack.ss_sp=%p, "
166 "uc_stack.ss_size=%d\n",
167 newpcb->pcb_ucp.uc_link,
168 newpcb->pcb_ucp.uc_stack.ss_sp,
169 (int)newpcb->pcb_ucp.uc_stack.ss_size);
170 }
171 #endif /* !CPU_DEBUG */
172
173 ci->ci_stash = oldlwp;
174 curlwp = newlwp;
175 if (oldpcb) {
176 if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
177 panic("swapcontext failed: %d", errno);
178 } else {
179 if (thunk_setcontext(&newpcb->pcb_ucp))
180 panic("setcontext failed: %d", errno);
181 }
182
183 #ifdef CPU_DEBUG
184 printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
185 #endif
186 return ci->ci_stash;
187 }
188
189 void
190 cpu_dumpconf(void)
191 {
192 #ifdef CPU_DEBUG
193 printf("cpu_dumpconf\n");
194 #endif
195 }
196
197 void
198 cpu_signotify(struct lwp *l)
199 {
200 #ifdef CPU_DEBUG
201 printf("cpu_signotify\n");
202 #endif
203 }
204
205 void
206 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
207 {
208 #ifdef CPU_DEBUG
209 printf("cpu_getmcontext\n");
210 #endif
211 }
212
213 int
214 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
215 {
216 #ifdef CPU_DEBUG
217 printf("cpu_setmcontext\n");
218 #endif
219 return 0;
220 }
221
222 void
223 cpu_idle(void)
224 {
225 struct cpu_info *ci = curcpu();
226
227 if (ci->ci_want_resched)
228 return;
229
230 #if notyet
231 thunk_usleep(10000);
232 #endif
233 }
234
235 void
236 cpu_lwp_free(struct lwp *l, int proc)
237 {
238 #ifdef CPU_DEBUG
239 printf("cpu_lwp_free\n");
240 #endif
241 }
242
243 void
244 cpu_lwp_free2(struct lwp *l)
245 {
246 struct pcb *pcb = lwp_getpcb(l);
247
248 #ifdef CPU_DEBUG
249 printf("cpu_lwp_free2\n");
250 #endif
251
252 if (pcb == NULL)
253 return;
254
255 if (pcb->pcb_needfree) {
256 free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
257 pcb->pcb_ucp.uc_stack.ss_sp = NULL;
258 pcb->pcb_ucp.uc_stack.ss_size = 0;
259 pcb->pcb_needfree = false;
260 }
261 }
262
263 static void
264 cpu_lwp_trampoline(void (*func)(void *), void *arg)
265 {
266 lwp_startup(curcpu()->ci_stash, curlwp);
267
268 func(arg);
269 }
270
271 void
272 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
273 void (*func)(void *), void *arg)
274 {
275 extern int errno;
276 struct pcb *pcb = lwp_getpcb(l2);
277
278 #ifdef CPU_DEBUG
279 printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
280 l1 ? l1->l_name : "none", l1,
281 l2 ? l2->l_name : "none", l2,
282 stack, (int)stacksize);
283 #endif
284
285 /* XXXJDM */
286 if (stack == NULL) {
287 stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
288 stacksize = PAGE_SIZE;
289 pcb->pcb_needfree = true;
290 } else
291 pcb->pcb_needfree = false;
292
293 if (thunk_getcontext(&pcb->pcb_ucp))
294 panic("getcontext failed: %d", errno);
295 pcb->pcb_ucp.uc_stack.ss_sp = stack;
296 pcb->pcb_ucp.uc_stack.ss_size = stacksize;
297 pcb->pcb_ucp.uc_link = NULL;
298 pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
299 thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
300 2, func, arg);
301 }
302
303 void
304 cpu_initclocks(void)
305 {
306 }
307
308 void
309 cpu_startup(void)
310 {
311 char pbuf[9];
312
313 printf("%s%s", copyright, version);
314 format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
315 printf("total memory = %s\n", pbuf);
316
317 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
318 printf("avail memory = %s\n", pbuf);
319 }
320
321 void
322 cpu_rootconf(void)
323 {
324 device_t rdev;
325
326 rdev = device_find_by_xname("md0");
327
328 setroot(rdev, 0);
329 }
330
331 bool
332 cpu_intr_p(void)
333 {
334 printf("cpu_intr_p\n");
335 return false;
336 }
337