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