cpu.c revision 1.74.8.1 1 /* $NetBSD: cpu.c,v 1.74.8.1 2018/05/21 04:36:02 pgoyette 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 #include "opt_hz.h"
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.74.8.1 2018/05/21 04:36:02 pgoyette Exp $");
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/reboot.h>
41 #include <sys/lwp.h>
42 #include <sys/cpu.h>
43 #include <sys/mbuf.h>
44 #include <sys/msgbuf.h>
45 #include <sys/kmem.h>
46 #include <sys/kernel.h>
47 #include <sys/mount.h>
48
49 #include <dev/cons.h>
50
51 #include <machine/cpu.h>
52 #include <machine/mainbus.h>
53 #include <machine/pcb.h>
54 #include <machine/machdep.h>
55 #include <machine/thunk.h>
56
57 #include <uvm/uvm_extern.h>
58 #include <uvm/uvm_page.h>
59
60 #if __GNUC_PREREQ__(4,4)
61 #define cpu_unreachable() __builtin_unreachable()
62 #else
63 #define cpu_unreachable() do { thunk_abort(); } while (0)
64 #endif
65
66 static int cpu_match(device_t, cfdata_t, void *);
67 static void cpu_attach(device_t, device_t, void *);
68
69 struct cpu_info cpu_info_primary = {
70 .ci_dev = 0,
71 .ci_self = &cpu_info_primary,
72 .ci_idepth = -1,
73 .ci_curlwp = &lwp0,
74 };
75
76 typedef struct cpu_softc {
77 device_t sc_dev;
78 struct cpu_info *sc_ci;
79
80 ucontext_t sc_ucp;
81 uint8_t sc_ucp_stack[PAGE_SIZE];
82 } cpu_softc_t;
83
84
85 /* statics */
86 static struct pcb lwp0pcb;
87 static void *um_msgbuf;
88
89
90 /* attachment */
91 CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
92
93 static int
94 cpu_match(device_t parent, cfdata_t match, void *opaque)
95 {
96 struct thunkbus_attach_args *taa = opaque;
97
98 if (taa->taa_type != THUNKBUS_TYPE_CPU)
99 return 0;
100
101 return 1;
102 }
103
104 static void
105 cpu_attach(device_t parent, device_t self, void *opaque)
106 {
107 cpu_softc_t *sc = device_private(self);
108
109 aprint_naive("\n");
110 aprint_normal("\n");
111
112 cpu_info_primary.ci_dev = self;
113 sc->sc_dev = self;
114 sc->sc_ci = &cpu_info_primary;
115
116 thunk_getcontext(&sc->sc_ucp);
117 sc->sc_ucp.uc_stack.ss_sp = sc->sc_ucp_stack;
118 sc->sc_ucp.uc_stack.ss_size = PAGE_SIZE - sizeof(register_t);
119 sc->sc_ucp.uc_flags = _UC_STACK | _UC_CPU | _UC_SIGMASK;
120 thunk_sigaddset(&sc->sc_ucp.uc_sigmask, SIGALRM);
121 thunk_sigaddset(&sc->sc_ucp.uc_sigmask, SIGIO);
122 thunk_sigaddset(&sc->sc_ucp.uc_sigmask, SIGINT);
123 thunk_sigaddset(&sc->sc_ucp.uc_sigmask, SIGTSTP);
124 }
125
126 void
127 cpu_configure(void)
128 {
129 cpu_setmodel("virtual processor");
130 if (config_rootfound("mainbus", NULL) == NULL)
131 panic("configure: mainbus not configured");
132
133
134 spl0();
135 }
136
137
138 /* main guts */
139 void
140 cpu_reboot(int howto, char *bootstr)
141 {
142 extern void usermode_reboot(void);
143
144 if (cold)
145 howto |= RB_HALT;
146
147 if ((howto & RB_NOSYNC) == 0)
148 vfs_shutdown();
149 else
150 suspendsched();
151
152 doshutdownhooks();
153 pmf_system_shutdown(boothowto);
154
155 if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
156 thunk_exit(0);
157
158 splhigh();
159
160 if (howto & RB_DUMP)
161 thunk_abort();
162
163 if (howto & RB_HALT) {
164 printf("\n");
165 printf("The operating system has halted.\n");
166 printf("Please press any key to reboot.\n\n");
167 cnpollc(1);
168 cngetc();
169 cnpollc(0);
170 }
171
172 printf("rebooting...\n");
173
174 usermode_reboot();
175
176 /* NOTREACHED */
177 cpu_unreachable();
178 }
179
180 void
181 cpu_need_resched(struct cpu_info *ci, int flags)
182 {
183 ci->ci_want_resched |= flags;
184 aston(ci);
185 }
186
187 void
188 cpu_need_proftick(struct lwp *l)
189 {
190 }
191
192 static
193 void
194 cpu_switchto_atomic(lwp_t *oldlwp, lwp_t *newlwp)
195 {
196 struct pcb *oldpcb;
197 struct pcb *newpcb;
198 struct cpu_info *ci;
199
200 oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
201 newpcb = lwp_getpcb(newlwp);
202 ci = curcpu();
203
204 ci->ci_stash = oldlwp;
205
206 if (oldpcb)
207 oldpcb->pcb_errno = thunk_geterrno();
208
209 thunk_seterrno(newpcb->pcb_errno);
210
211 curlwp = newlwp;
212 if (thunk_setcontext(&newpcb->pcb_ucp))
213 panic("setcontext failed");
214 /* not reached */
215 }
216
217 lwp_t *
218 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
219 {
220 struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
221 struct pcb *newpcb = lwp_getpcb(newlwp);
222 struct cpu_info *ci = curcpu();
223 cpu_softc_t *sc = device_private(ci->ci_dev);
224
225 #ifdef CPU_DEBUG
226 thunk_printf_debug("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
227 oldlwp ? oldlwp->l_name : "none",
228 oldlwp ? oldlwp->l_proc->p_pid : -1,
229 oldlwp ? oldlwp->l_lid : -1,
230 newlwp ? newlwp->l_name : "none",
231 newlwp ? newlwp->l_proc->p_pid : -1,
232 newlwp ? newlwp->l_lid : -1);
233 if (oldpcb) {
234 thunk_printf_debug(" oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
235 "uc_stack.ss_size=%d\n",
236 oldpcb->pcb_ucp.uc_link,
237 oldpcb->pcb_ucp.uc_stack.ss_sp,
238 (int)oldpcb->pcb_ucp.uc_stack.ss_size);
239 }
240 if (newpcb) {
241 thunk_printf_debug(" newpcb uc_link=%p, uc_stack.ss_sp=%p, "
242 "uc_stack.ss_size=%d\n",
243 newpcb->pcb_ucp.uc_link,
244 newpcb->pcb_ucp.uc_stack.ss_sp,
245 (int)newpcb->pcb_ucp.uc_stack.ss_size);
246 }
247 #endif /* !CPU_DEBUG */
248
249 /* create atomic switcher */
250 KASSERT(newlwp);
251 thunk_makecontext(&sc->sc_ucp, (void (*)(void)) cpu_switchto_atomic,
252 2, oldlwp, newlwp, NULL, NULL);
253
254 KASSERT(sc);
255 if (oldpcb) {
256 thunk_swapcontext(&oldpcb->pcb_ucp, &sc->sc_ucp);
257 /* returns here */
258 } else {
259 thunk_setcontext(&sc->sc_ucp);
260 /* never returns */
261 }
262
263 #ifdef CPU_DEBUG
264 thunk_printf_debug("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
265 #endif
266 return ci->ci_stash;
267 }
268
269 void
270 cpu_dumpconf(void)
271 {
272 #ifdef CPU_DEBUG
273 thunk_printf_debug("cpu_dumpconf\n");
274 #endif
275 }
276
277 void
278 cpu_signotify(struct lwp *l)
279 {
280 }
281
282 void
283 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
284 {
285 struct pcb *pcb = lwp_getpcb(l);
286 ucontext_t *ucp = &pcb->pcb_userret_ucp;
287
288 #ifdef CPU_DEBUG
289 thunk_printf_debug("cpu_getmcontext\n");
290 #endif
291 memcpy(mcp, &ucp->uc_mcontext, sizeof(mcontext_t));
292 return;
293 }
294
295 int
296 cpu_mcontext_validate(struct lwp *l, const mcontext_t *mcp)
297 {
298 /*
299 * can we check here? or should that be done in the target
300 * specific places?
301 */
302 return 0;
303 }
304
305 int
306 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
307 {
308 struct pcb *pcb = lwp_getpcb(l);
309 ucontext_t *ucp = &pcb->pcb_userret_ucp;
310
311 #ifdef CPU_DEBUG
312 thunk_printf_debug("cpu_setmcontext\n");
313 #endif
314 memcpy(&ucp->uc_mcontext, mcp, sizeof(mcontext_t));
315 return 0;
316 }
317
318 void
319 cpu_idle(void)
320 {
321 struct cpu_info *ci = curcpu();
322
323 if (ci->ci_want_resched)
324 return;
325
326 thunk_idle();
327 }
328
329 void
330 cpu_lwp_free(struct lwp *l, int proc)
331 {
332 #ifdef CPU_DEBUG
333 thunk_printf_debug("cpu_lwp_free (dummy)\n");
334 #endif
335 }
336
337 void
338 cpu_lwp_free2(struct lwp *l)
339 {
340 struct pcb *pcb = lwp_getpcb(l);
341
342 #ifdef CPU_DEBUG
343 thunk_printf_debug("cpu_lwp_free2\n");
344 #endif
345
346 if (pcb == NULL)
347 return;
348 /* XXX nothing to do? */
349 }
350
351 static void
352 cpu_lwp_trampoline(ucontext_t *ucp, void (*func)(void *), void *arg)
353 {
354 #ifdef CPU_DEBUG
355 thunk_printf_debug("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
356 #endif
357 /* init lwp */
358 lwp_startup(curcpu()->ci_stash, curlwp);
359
360 /* actual jump */
361 thunk_makecontext(ucp, (void (*)(void)) func, 1, arg, NULL, NULL, NULL);
362 thunk_setcontext(ucp);
363 }
364
365 void
366 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
367 void (*func)(void *), void *arg)
368 {
369 struct pcb *pcb1 = lwp_getpcb(l1);
370 struct pcb *pcb2 = lwp_getpcb(l2);
371
372 #ifdef CPU_DEBUG
373 thunk_printf_debug("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
374 l1 ? l1->l_name : "none", l1,
375 l2 ? l2->l_name : "none", l2,
376 stack, (int)stacksize);
377 #endif
378
379 if (stack)
380 panic("%s: stack passed, can't handle\n", __func__);
381
382 /* copy the PCB and its switchframes from parent */
383 memcpy(pcb2, pcb1, sizeof(struct pcb));
384
385 /* refresh context */
386 if (thunk_getcontext(&pcb2->pcb_ucp))
387 panic("getcontext failed");
388
389 /* recalculate the system stack top */
390 pcb2->sys_stack_top = pcb2->sys_stack + TRAPSTACKSIZE;
391
392 /* get l2 its own stack */
393 pcb2->pcb_ucp.uc_stack.ss_sp = pcb2->sys_stack;
394 pcb2->pcb_ucp.uc_stack.ss_size = pcb2->sys_stack_top - pcb2->sys_stack;
395 pcb2->pcb_ucp.uc_link = &pcb2->pcb_userret_ucp;
396
397 thunk_sigemptyset(&pcb2->pcb_ucp.uc_sigmask);
398 pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU | _UC_SIGMASK;
399 thunk_makecontext(&pcb2->pcb_ucp,
400 (void (*)(void)) cpu_lwp_trampoline,
401 3, &pcb2->pcb_ucp, func, arg, NULL);
402 }
403
404 void
405 cpu_initclocks(void)
406 {
407 extern timer_t clock_timerid;
408
409 thunk_timer_start(clock_timerid, HZ);
410 }
411
412 void
413 cpu_startup(void)
414 {
415 vaddr_t minaddr, maxaddr;
416 size_t msgbufsize = 32 * 1024;
417
418 /* get ourself a message buffer */
419 um_msgbuf = kmem_zalloc(msgbufsize, KM_SLEEP);
420 initmsgbuf(um_msgbuf, msgbufsize);
421
422 /* allocate a submap for physio, 1Mb enough? */
423 minaddr = 0;
424 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
425 1024 * 1024, 0, false, NULL);
426
427 /* say hi! */
428 banner();
429
430 /* init lwp0 */
431 memset(&lwp0pcb, 0, sizeof(lwp0pcb));
432 thunk_getcontext(&lwp0pcb.pcb_ucp);
433 thunk_sigemptyset(&lwp0pcb.pcb_ucp.uc_sigmask);
434 lwp0pcb.pcb_ucp.uc_flags = _UC_STACK | _UC_CPU | _UC_SIGMASK;
435
436 uvm_lwp_setuarea(&lwp0, (vaddr_t) &lwp0pcb);
437 memcpy(&lwp0pcb.pcb_userret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
438
439 /* set stack top */
440 lwp0pcb.sys_stack_top = lwp0pcb.sys_stack + TRAPSTACKSIZE;
441 }
442
443 void
444 cpu_rootconf(void)
445 {
446 extern char *usermode_root_device;
447 device_t rdev;
448
449 if (usermode_root_device != NULL) {
450 rdev = device_find_by_xname(usermode_root_device);
451 } else {
452 rdev = device_find_by_xname("ld0");
453 if (rdev == NULL)
454 rdev = device_find_by_xname("md0");
455 }
456
457 aprint_normal("boot device: %s\n",
458 rdev ? device_xname(rdev) : "<unknown>");
459 booted_device = rdev;
460 rootconf();
461 }
462
463 bool
464 cpu_intr_p(void)
465 {
466 int idepth;
467
468 kpreempt_disable();
469 idepth = curcpu()->ci_idepth;
470 kpreempt_enable();
471
472 return (idepth >= 0);
473 }
474