cpu.c revision 1.1.6.2 1 1.1.6.2 yamt /* $NetBSD: cpu.c,v 1.1.6.2 2008/01/21 09:39:45 yamt Exp $ */
2 1.1.6.2 yamt
3 1.1.6.2 yamt /*-
4 1.1.6.2 yamt * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1.6.2 yamt * All rights reserved.
6 1.1.6.2 yamt *
7 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 yamt * modification, are permitted provided that the following conditions
9 1.1.6.2 yamt * are met:
10 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.6.2 yamt * 3. All advertising materials mentioning features or use of this software
16 1.1.6.2 yamt * must display the following acknowledgement:
17 1.1.6.2 yamt * This product includes software developed by Jared D. McNeill.
18 1.1.6.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.1.6.2 yamt * contributors may be used to endorse or promote products derived
20 1.1.6.2 yamt * from this software without specific prior written permission.
21 1.1.6.2 yamt *
22 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.1.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.1.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.1.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
33 1.1.6.2 yamt */
34 1.1.6.2 yamt
35 1.1.6.2 yamt #include <sys/cdefs.h>
36 1.1.6.2 yamt __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.1.6.2 2008/01/21 09:39:45 yamt Exp $");
37 1.1.6.2 yamt
38 1.1.6.2 yamt #include <sys/param.h>
39 1.1.6.2 yamt #include <sys/conf.h>
40 1.1.6.2 yamt #include <sys/proc.h>
41 1.1.6.2 yamt #include <sys/user.h>
42 1.1.6.2 yamt #include <sys/systm.h>
43 1.1.6.2 yamt #include <sys/device.h>
44 1.1.6.2 yamt #include <sys/reboot.h>
45 1.1.6.2 yamt #include <sys/lwp.h>
46 1.1.6.2 yamt #include <sys/cpu.h>
47 1.1.6.2 yamt #include <sys/mbuf.h>
48 1.1.6.2 yamt
49 1.1.6.2 yamt #include <dev/cons.h>
50 1.1.6.2 yamt
51 1.1.6.2 yamt #include <machine/cpu.h>
52 1.1.6.2 yamt #include <machine/mainbus.h>
53 1.1.6.2 yamt
54 1.1.6.2 yamt #include <uvm/uvm_extern.h>
55 1.1.6.2 yamt #include <uvm/uvm_page.h>
56 1.1.6.2 yamt
57 1.1.6.2 yamt /* #define CPU_DEBUG */
58 1.1.6.2 yamt
59 1.1.6.2 yamt static int cpu_match(device_t, cfdata_t, void *);
60 1.1.6.2 yamt static void cpu_attach(device_t, device_t, void *);
61 1.1.6.2 yamt
62 1.1.6.2 yamt struct cpu_info cpu_info_primary;
63 1.1.6.2 yamt char cpu_model[48] = "virtual processor";
64 1.1.6.2 yamt
65 1.1.6.2 yamt typedef struct cpu_softc {
66 1.1.6.2 yamt device_t sc_dev;
67 1.1.6.2 yamt struct cpu_info *sc_ci;
68 1.1.6.2 yamt } cpu_softc_t;
69 1.1.6.2 yamt
70 1.1.6.2 yamt CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
71 1.1.6.2 yamt
72 1.1.6.2 yamt static int
73 1.1.6.2 yamt cpu_match(device_t parent, cfdata_t match, void *opaque)
74 1.1.6.2 yamt {
75 1.1.6.2 yamt struct thunkbus_attach_args *taa = opaque;
76 1.1.6.2 yamt
77 1.1.6.2 yamt if (taa->taa_type != THUNKBUS_TYPE_CPU)
78 1.1.6.2 yamt return 0;
79 1.1.6.2 yamt
80 1.1.6.2 yamt return 1;
81 1.1.6.2 yamt }
82 1.1.6.2 yamt
83 1.1.6.2 yamt static void
84 1.1.6.2 yamt cpu_attach(device_t parent, device_t self, void *opaque)
85 1.1.6.2 yamt {
86 1.1.6.2 yamt cpu_softc_t *sc = device_private(self);
87 1.1.6.2 yamt
88 1.1.6.2 yamt aprint_naive("\n");
89 1.1.6.2 yamt aprint_normal("\n");
90 1.1.6.2 yamt
91 1.1.6.2 yamt sc->sc_dev = self;
92 1.1.6.2 yamt sc->sc_ci = &cpu_info_primary;
93 1.1.6.2 yamt sc->sc_ci->ci_dev = 0;
94 1.1.6.2 yamt sc->sc_ci->ci_self = &cpu_info_primary;
95 1.1.6.2 yamt #if notyet
96 1.1.6.2 yamt sc->sc_ci->ci_curlwp = &lwp0;
97 1.1.6.2 yamt #endif
98 1.1.6.2 yamt }
99 1.1.6.2 yamt
100 1.1.6.2 yamt void
101 1.1.6.2 yamt cpu_configure(void)
102 1.1.6.2 yamt {
103 1.1.6.2 yamt if (config_rootfound("mainbus", NULL) == NULL)
104 1.1.6.2 yamt panic("configure: mainbus not configured");
105 1.1.6.2 yamt
106 1.1.6.2 yamt spl0();
107 1.1.6.2 yamt }
108 1.1.6.2 yamt
109 1.1.6.2 yamt void
110 1.1.6.2 yamt cpu_reboot(int howto, char *bootstr)
111 1.1.6.2 yamt {
112 1.1.6.2 yamt extern void exit(int);
113 1.1.6.2 yamt extern void abort(void);
114 1.1.6.2 yamt
115 1.1.6.2 yamt splhigh();
116 1.1.6.2 yamt
117 1.1.6.2 yamt if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
118 1.1.6.2 yamt exit(0);
119 1.1.6.2 yamt
120 1.1.6.2 yamt if (howto & RB_HALT) {
121 1.1.6.2 yamt printf("\n");
122 1.1.6.2 yamt printf("The operating system has halted.\n");
123 1.1.6.2 yamt printf("Please press any key to reboot.\n\n");
124 1.1.6.2 yamt cnpollc(1);
125 1.1.6.2 yamt cngetc();
126 1.1.6.2 yamt cnpollc(0);
127 1.1.6.2 yamt }
128 1.1.6.2 yamt
129 1.1.6.2 yamt printf("rebooting...\n");
130 1.1.6.2 yamt
131 1.1.6.2 yamt /*
132 1.1.6.2 yamt * XXXJDM If we've panic'd, make sure we dump a core
133 1.1.6.2 yamt */
134 1.1.6.2 yamt abort();
135 1.1.6.2 yamt
136 1.1.6.2 yamt /* NOTREACHED */
137 1.1.6.2 yamt }
138 1.1.6.2 yamt
139 1.1.6.2 yamt void
140 1.1.6.2 yamt cpu_need_resched(struct cpu_info *ci, int flags)
141 1.1.6.2 yamt {
142 1.1.6.2 yamt ci->ci_want_resched = 1;
143 1.1.6.2 yamt }
144 1.1.6.2 yamt
145 1.1.6.2 yamt void
146 1.1.6.2 yamt cpu_need_proftick(struct lwp *l)
147 1.1.6.2 yamt {
148 1.1.6.2 yamt }
149 1.1.6.2 yamt
150 1.1.6.2 yamt lwp_t *
151 1.1.6.2 yamt cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
152 1.1.6.2 yamt {
153 1.1.6.2 yamt extern int errno;
154 1.1.6.2 yamt struct pcb *oldpcb = (struct pcb *)(oldlwp ? oldlwp->l_addr : NULL);
155 1.1.6.2 yamt struct pcb *newpcb = (struct pcb *)newlwp->l_addr;
156 1.1.6.2 yamt struct cpu_info *ci = curcpu();
157 1.1.6.2 yamt
158 1.1.6.2 yamt #ifdef CPU_DEBUG
159 1.1.6.2 yamt printf("cpu_switchto [%s] -> [%s]\n",
160 1.1.6.2 yamt oldlwp ? oldlwp->l_name : "none",
161 1.1.6.2 yamt newlwp ? newlwp->l_name : "none");
162 1.1.6.2 yamt if (oldpcb) {
163 1.1.6.2 yamt printf(" oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
164 1.1.6.2 yamt "uc_stack.ss_size=%d\n",
165 1.1.6.2 yamt oldpcb->pcb_ucp.uc_link,
166 1.1.6.2 yamt oldpcb->pcb_ucp.uc_stack.ss_sp,
167 1.1.6.2 yamt (int)oldpcb->pcb_ucp.uc_stack.ss_size);
168 1.1.6.2 yamt }
169 1.1.6.2 yamt if (newpcb) {
170 1.1.6.2 yamt printf(" newpcb uc_link=%p, uc_stack.ss_sp=%p, "
171 1.1.6.2 yamt "uc_stack.ss_size=%d\n",
172 1.1.6.2 yamt newpcb->pcb_ucp.uc_link,
173 1.1.6.2 yamt newpcb->pcb_ucp.uc_stack.ss_sp,
174 1.1.6.2 yamt (int)newpcb->pcb_ucp.uc_stack.ss_size);
175 1.1.6.2 yamt }
176 1.1.6.2 yamt #endif /* !CPU_DEBUG */
177 1.1.6.2 yamt
178 1.1.6.2 yamt ci->ci_stash = oldlwp;
179 1.1.6.2 yamt curlwp = newlwp;
180 1.1.6.2 yamt if (oldpcb) {
181 1.1.6.2 yamt if (swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
182 1.1.6.2 yamt panic("swapcontext failed: %d", errno);
183 1.1.6.2 yamt } else {
184 1.1.6.2 yamt if (setcontext(&newpcb->pcb_ucp))
185 1.1.6.2 yamt panic("setcontext failed: %d", errno);
186 1.1.6.2 yamt }
187 1.1.6.2 yamt
188 1.1.6.2 yamt #ifdef CPU_DEBUG
189 1.1.6.2 yamt printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
190 1.1.6.2 yamt #endif
191 1.1.6.2 yamt return ci->ci_stash;
192 1.1.6.2 yamt }
193 1.1.6.2 yamt
194 1.1.6.2 yamt void
195 1.1.6.2 yamt cpu_dumpconf(void)
196 1.1.6.2 yamt {
197 1.1.6.2 yamt #ifdef CPU_DEBUG
198 1.1.6.2 yamt printf("cpu_dumpconf\n");
199 1.1.6.2 yamt #endif
200 1.1.6.2 yamt }
201 1.1.6.2 yamt
202 1.1.6.2 yamt void
203 1.1.6.2 yamt cpu_signotify(struct lwp *l)
204 1.1.6.2 yamt {
205 1.1.6.2 yamt #ifdef CPU_DEBUG
206 1.1.6.2 yamt printf("cpu_signotify\n");
207 1.1.6.2 yamt #endif
208 1.1.6.2 yamt }
209 1.1.6.2 yamt
210 1.1.6.2 yamt void
211 1.1.6.2 yamt cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
212 1.1.6.2 yamt {
213 1.1.6.2 yamt #ifdef CPU_DEBUG
214 1.1.6.2 yamt printf("cpu_getmcontext\n");
215 1.1.6.2 yamt #endif
216 1.1.6.2 yamt }
217 1.1.6.2 yamt
218 1.1.6.2 yamt int
219 1.1.6.2 yamt cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
220 1.1.6.2 yamt {
221 1.1.6.2 yamt #ifdef CPU_DEBUG
222 1.1.6.2 yamt printf("cpu_setmcontext\n");
223 1.1.6.2 yamt #endif
224 1.1.6.2 yamt return 0;
225 1.1.6.2 yamt }
226 1.1.6.2 yamt
227 1.1.6.2 yamt void
228 1.1.6.2 yamt cpu_idle(void)
229 1.1.6.2 yamt {
230 1.1.6.2 yamt extern int usleep(useconds_t);
231 1.1.6.2 yamt struct cpu_info *ci = curcpu();
232 1.1.6.2 yamt
233 1.1.6.2 yamt if (ci->ci_want_resched)
234 1.1.6.2 yamt return;
235 1.1.6.2 yamt
236 1.1.6.2 yamt #if notyet
237 1.1.6.2 yamt usleep(10000);
238 1.1.6.2 yamt #endif
239 1.1.6.2 yamt }
240 1.1.6.2 yamt
241 1.1.6.2 yamt void
242 1.1.6.2 yamt cpu_lwp_free(struct lwp *l, int proc)
243 1.1.6.2 yamt {
244 1.1.6.2 yamt #ifdef CPU_DEBUG
245 1.1.6.2 yamt printf("cpu_lwp_free\n");
246 1.1.6.2 yamt #endif
247 1.1.6.2 yamt }
248 1.1.6.2 yamt
249 1.1.6.2 yamt void
250 1.1.6.2 yamt cpu_lwp_free2(struct lwp *l)
251 1.1.6.2 yamt {
252 1.1.6.2 yamt struct pcb *pcb = (struct pcb *)l->l_addr;
253 1.1.6.2 yamt
254 1.1.6.2 yamt #ifdef CPU_DEBUG
255 1.1.6.2 yamt printf("cpu_lwp_free2\n");
256 1.1.6.2 yamt #endif
257 1.1.6.2 yamt
258 1.1.6.2 yamt if (pcb == NULL)
259 1.1.6.2 yamt return;
260 1.1.6.2 yamt
261 1.1.6.2 yamt if (pcb->pcb_needfree) {
262 1.1.6.2 yamt free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
263 1.1.6.2 yamt pcb->pcb_ucp.uc_stack.ss_sp = NULL;
264 1.1.6.2 yamt pcb->pcb_ucp.uc_stack.ss_size = 0;
265 1.1.6.2 yamt pcb->pcb_needfree = false;
266 1.1.6.2 yamt }
267 1.1.6.2 yamt }
268 1.1.6.2 yamt
269 1.1.6.2 yamt static void
270 1.1.6.2 yamt cpu_lwp_trampoline(void (*func)(void *), void *arg)
271 1.1.6.2 yamt {
272 1.1.6.2 yamt lwp_startup(curcpu()->ci_stash, curlwp);
273 1.1.6.2 yamt
274 1.1.6.2 yamt func(arg);
275 1.1.6.2 yamt }
276 1.1.6.2 yamt
277 1.1.6.2 yamt void
278 1.1.6.2 yamt cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
279 1.1.6.2 yamt void (*func)(void *), void *arg)
280 1.1.6.2 yamt {
281 1.1.6.2 yamt extern int errno;
282 1.1.6.2 yamt struct pcb *pcb = (struct pcb *)l2->l_addr;
283 1.1.6.2 yamt
284 1.1.6.2 yamt #ifdef CPU_DEBUG
285 1.1.6.2 yamt printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
286 1.1.6.2 yamt l1 ? l1->l_name : "none", l1,
287 1.1.6.2 yamt l2 ? l2->l_name : "none", l2,
288 1.1.6.2 yamt stack, (int)stacksize);
289 1.1.6.2 yamt #endif
290 1.1.6.2 yamt
291 1.1.6.2 yamt /* XXXJDM */
292 1.1.6.2 yamt if (stack == NULL) {
293 1.1.6.2 yamt stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
294 1.1.6.2 yamt stacksize = PAGE_SIZE;
295 1.1.6.2 yamt pcb->pcb_needfree = true;
296 1.1.6.2 yamt } else
297 1.1.6.2 yamt pcb->pcb_needfree = false;
298 1.1.6.2 yamt
299 1.1.6.2 yamt if (getcontext(&pcb->pcb_ucp))
300 1.1.6.2 yamt panic("getcontext failed: %d", errno);
301 1.1.6.2 yamt pcb->pcb_ucp.uc_stack.ss_sp = stack;
302 1.1.6.2 yamt pcb->pcb_ucp.uc_stack.ss_size = stacksize;
303 1.1.6.2 yamt pcb->pcb_ucp.uc_link = NULL;
304 1.1.6.2 yamt pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
305 1.1.6.2 yamt makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
306 1.1.6.2 yamt 2, func, arg);
307 1.1.6.2 yamt }
308 1.1.6.2 yamt
309 1.1.6.2 yamt void
310 1.1.6.2 yamt cpu_initclocks(void)
311 1.1.6.2 yamt {
312 1.1.6.2 yamt }
313 1.1.6.2 yamt
314 1.1.6.2 yamt void
315 1.1.6.2 yamt cpu_startup(void)
316 1.1.6.2 yamt {
317 1.1.6.2 yamt extern int physmem;
318 1.1.6.2 yamt extern struct vm_map *mb_map;
319 1.1.6.2 yamt vaddr_t minaddr, maxaddr;
320 1.1.6.2 yamt char pbuf[9];
321 1.1.6.2 yamt
322 1.1.6.2 yamt printf("%s%s", copyright, version);
323 1.1.6.2 yamt format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
324 1.1.6.2 yamt printf("total memory = %s\n", pbuf);
325 1.1.6.2 yamt
326 1.1.6.2 yamt minaddr = 0;
327 1.1.6.2 yamt mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
328 1.1.6.2 yamt nmbclusters * mclbytes, VM_MAP_INTRSAFE, false, NULL);
329 1.1.6.2 yamt
330 1.1.6.2 yamt format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
331 1.1.6.2 yamt printf("avail memory = %s\n", pbuf);
332 1.1.6.2 yamt }
333 1.1.6.2 yamt
334 1.1.6.2 yamt void
335 1.1.6.2 yamt cpu_rootconf(void)
336 1.1.6.2 yamt {
337 1.1.6.2 yamt device_t curdev, rdev = NULL;
338 1.1.6.2 yamt
339 1.1.6.2 yamt TAILQ_FOREACH(curdev, &alldevs, dv_list)
340 1.1.6.2 yamt if (strcmp(device_xname(curdev), "md0") == 0) {
341 1.1.6.2 yamt rdev = curdev;
342 1.1.6.2 yamt break;
343 1.1.6.2 yamt }
344 1.1.6.2 yamt
345 1.1.6.2 yamt setroot(rdev, 0);
346 1.1.6.2 yamt }
347 1.1.6.2 yamt
348 1.1.6.2 yamt bool
349 1.1.6.2 yamt cpu_intr_p(void)
350 1.1.6.2 yamt {
351 1.1.6.2 yamt printf("cpu_intr_p\n");
352 1.1.6.2 yamt return false;
353 1.1.6.2 yamt }
354