Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.47
      1 /* $NetBSD: cpu.c,v 1.47 2011/11/27 21:38:17 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 "opt_cpu.h"
     30 #include "opt_hz.h"
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.47 2011/11/27 21:38:17 reinoud 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 
     46 #include <dev/cons.h>
     47 
     48 #include <machine/cpu.h>
     49 #include <machine/mainbus.h>
     50 #include <machine/pcb.h>
     51 #include <machine/machdep.h>
     52 #include <machine/thunk.h>
     53 
     54 #include <uvm/uvm_extern.h>
     55 #include <uvm/uvm_page.h>
     56 
     57 #if __GNUC_PREREQ__(4,4)
     58 #define cpu_unreachable()	__builtin_unreachable()
     59 #else
     60 #define cpu_unreachable()	do { thunk_abort(); } while (0)
     61 #endif
     62 
     63 static int	cpu_match(device_t, cfdata_t, void *);
     64 static void	cpu_attach(device_t, device_t, void *);
     65 
     66 struct cpu_info cpu_info_primary = {
     67 	.ci_dev = 0,
     68 	.ci_self = &cpu_info_primary,
     69 	.ci_idepth = -1,
     70 	.ci_curlwp = &lwp0,
     71 };
     72 
     73 char cpu_model[48] = "virtual processor";
     74 
     75 typedef struct cpu_softc {
     76 	device_t	sc_dev;
     77 	struct cpu_info	*sc_ci;
     78 } cpu_softc_t;
     79 
     80 static struct pcb lwp0pcb;
     81 static void *msgbuf;
     82 
     83 CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
     84 
     85 static int
     86 cpu_match(device_t parent, cfdata_t match, void *opaque)
     87 {
     88 	struct thunkbus_attach_args *taa = opaque;
     89 
     90 	if (taa->taa_type != THUNKBUS_TYPE_CPU)
     91 		return 0;
     92 
     93 	return 1;
     94 }
     95 
     96 static void
     97 cpu_attach(device_t parent, device_t self, void *opaque)
     98 {
     99 	cpu_softc_t *sc = device_private(self);
    100 
    101 	aprint_naive("\n");
    102 	aprint_normal("\n");
    103 
    104 	sc->sc_dev = self;
    105 	sc->sc_ci = &cpu_info_primary;
    106 }
    107 
    108 void
    109 cpu_configure(void)
    110 {
    111 	if (config_rootfound("mainbus", NULL) == NULL)
    112 		panic("configure: mainbus not configured");
    113 
    114 	spl0();
    115 }
    116 
    117 void
    118 cpu_reboot(int howto, char *bootstr)
    119 {
    120 	extern void usermode_reboot(void);
    121 
    122 	splhigh();
    123 
    124 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
    125 		thunk_exit(0);
    126 
    127 	if (howto & RB_DUMP)
    128 		thunk_abort();
    129 
    130 	if (howto & RB_HALT) {
    131 		printf("\n");
    132 		printf("The operating system has halted.\n");
    133 		printf("Please press any key to reboot.\n\n");
    134 		cnpollc(1);
    135 		cngetc();
    136 		cnpollc(0);
    137 	}
    138 
    139 	printf("rebooting...\n");
    140 
    141 	usermode_reboot();
    142 
    143 	/* NOTREACHED */
    144 	cpu_unreachable();
    145 }
    146 
    147 void
    148 cpu_need_resched(struct cpu_info *ci, int flags)
    149 {
    150 	ci->ci_want_resched |= flags;
    151 }
    152 
    153 void
    154 cpu_need_proftick(struct lwp *l)
    155 {
    156 }
    157 
    158 lwp_t *
    159 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
    160 {
    161 	struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
    162 	struct pcb *newpcb = lwp_getpcb(newlwp);
    163 	struct cpu_info *ci = curcpu();
    164 
    165 #ifdef CPU_DEBUG
    166 	dprintf_debug("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
    167 	    oldlwp ? oldlwp->l_name : "none",
    168 	    oldlwp ? oldlwp->l_proc->p_pid : -1,
    169 	    oldlwp ? oldlwp->l_lid : -1,
    170 	    newlwp ? newlwp->l_name : "none",
    171 	    newlwp ? newlwp->l_proc->p_pid : -1,
    172 	    newlwp ? newlwp->l_lid : -1);
    173 	if (oldpcb) {
    174 		dprintf_debug("    oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
    175 		    "uc_stack.ss_size=%d\n",
    176 		    oldpcb->pcb_ucp.uc_link,
    177 		    oldpcb->pcb_ucp.uc_stack.ss_sp,
    178 		    (int)oldpcb->pcb_ucp.uc_stack.ss_size);
    179 	}
    180 	if (newpcb) {
    181 		dprintf_debug("    newpcb uc_link=%p, uc_stack.ss_sp=%p, "
    182 		    "uc_stack.ss_size=%d\n",
    183 		    newpcb->pcb_ucp.uc_link,
    184 		    newpcb->pcb_ucp.uc_stack.ss_sp,
    185 		    (int)newpcb->pcb_ucp.uc_stack.ss_size);
    186 	}
    187 #endif /* !CPU_DEBUG */
    188 
    189 	ci->ci_stash = oldlwp;
    190 
    191 	if (oldpcb) {
    192 		oldpcb->pcb_errno = thunk_geterrno();
    193 		thunk_seterrno(newpcb->pcb_errno);
    194 		curlwp = newlwp;
    195 		if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
    196 			panic("swapcontext failed");
    197 	} else {
    198 		thunk_seterrno(newpcb->pcb_errno);
    199 		curlwp = newlwp;
    200 		if (thunk_setcontext(&newpcb->pcb_ucp))
    201 			panic("setcontext failed");
    202 	}
    203 
    204 #ifdef CPU_DEBUG
    205 	dprintf_debug("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
    206 #endif
    207 	return ci->ci_stash;
    208 }
    209 
    210 void
    211 cpu_dumpconf(void)
    212 {
    213 #ifdef CPU_DEBUG
    214 	dprintf_debug("cpu_dumpconf\n");
    215 #endif
    216 }
    217 
    218 void
    219 cpu_signotify(struct lwp *l)
    220 {
    221 }
    222 
    223 void
    224 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    225 {
    226 	panic("cpu_getmcontext");
    227 #ifdef CPU_DEBUG
    228 	dprintf_debug("cpu_getmcontext\n");
    229 #endif
    230 }
    231 
    232 int
    233 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    234 {
    235 	panic("cpu_setmcontext");
    236 #ifdef CPU_DEBUG
    237 	dprintf_debug("cpu_setmcontext\n");
    238 #endif
    239 	return 0;
    240 }
    241 
    242 void
    243 cpu_idle(void)
    244 {
    245 	struct cpu_info *ci = curcpu();
    246 
    247 	if (ci->ci_want_resched)
    248 		return;
    249 
    250 #if notyet
    251 	thunk_usleep(10000);
    252 #endif
    253 }
    254 
    255 void
    256 cpu_lwp_free(struct lwp *l, int proc)
    257 {
    258 #ifdef CPU_DEBUG
    259 	dprintf_debug("cpu_lwp_free (dummy)\n");
    260 #endif
    261 }
    262 
    263 void
    264 cpu_lwp_free2(struct lwp *l)
    265 {
    266 	struct pcb *pcb = lwp_getpcb(l);
    267 
    268 #ifdef CPU_DEBUG
    269 	dprintf_debug("cpu_lwp_free2\n");
    270 #endif
    271 
    272 	if (pcb == NULL)
    273 		return;
    274 
    275 	if (pcb->pcb_needfree) {
    276 		free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
    277 		pcb->pcb_ucp.uc_stack.ss_sp = NULL;
    278 		pcb->pcb_ucp.uc_stack.ss_size = 0;
    279 
    280 		free(pcb->pcb_syscall_ucp.uc_stack.ss_sp, M_TEMP);
    281 		pcb->pcb_syscall_ucp.uc_stack.ss_sp = NULL;
    282 		pcb->pcb_syscall_ucp.uc_stack.ss_size = 0;
    283 
    284 		pcb->pcb_needfree = false;
    285 	}
    286 }
    287 
    288 static void
    289 cpu_lwp_trampoline(ucontext_t *ucp, void (*func)(void *), void *arg)
    290 {
    291 #ifdef CPU_DEBUG
    292 	dprintf_debug("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
    293 #endif
    294 	/* init lwp */
    295 	lwp_startup(curcpu()->ci_stash, curlwp);
    296 
    297 	/* actual jump */
    298 	thunk_makecontext(ucp, (void (*)(void)) func, 1, arg, NULL, NULL);
    299 	thunk_setcontext(ucp);
    300 }
    301 
    302 void
    303 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    304     void (*func)(void *), void *arg)
    305 {
    306 	struct pcb *pcb1 = lwp_getpcb(l1);
    307 	struct pcb *pcb2 = lwp_getpcb(l2);
    308 	void *stack_ucp, *stack_syscall_ucp, *stack_pagefault_ucp;
    309 
    310 #ifdef CPU_DEBUG
    311 	dprintf_debug("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    312 	    l1 ? l1->l_name : "none", l1,
    313 	    l2 ? l2->l_name : "none", l2,
    314 	    stack, (int)stacksize);
    315 #endif
    316 
    317 	if (stack)
    318 		panic("%s: stack passed, can't handle\n", __func__);
    319 
    320 	/* copy the PCB and its switchframes from parent */
    321 	memcpy(pcb2, pcb1, sizeof(struct pcb));
    322 
    323 	stacksize = 16*PAGE_SIZE;
    324 	stack_ucp           = malloc(stacksize, M_TEMP, M_NOWAIT);
    325 	stack_syscall_ucp   = malloc(stacksize, M_TEMP, M_NOWAIT);
    326 	stack_pagefault_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
    327 	pcb2->pcb_needfree = true;
    328 
    329 	if (thunk_getcontext(&pcb2->pcb_ucp))
    330 		panic("getcontext failed");
    331 
    332 	/* set up the ucontext for the userland switch */
    333 	pcb2->pcb_ucp.uc_stack.ss_sp = stack_ucp;
    334 	pcb2->pcb_ucp.uc_stack.ss_size = stacksize;
    335 	pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
    336 	pcb2->pcb_ucp.uc_link = &pcb2->pcb_userret_ucp;
    337 	thunk_makecontext(&pcb2->pcb_ucp,
    338 	    (void (*)(void)) cpu_lwp_trampoline,
    339 	    3, &pcb2->pcb_ucp, func, arg);
    340 
    341 	/* set up the ucontext for the syscall */
    342 	pcb2->pcb_syscall_ucp.uc_stack.ss_sp = stack_syscall_ucp;
    343 	pcb2->pcb_syscall_ucp.uc_stack.ss_size = stacksize;
    344 	pcb2->pcb_syscall_ucp.uc_flags = _UC_STACK | _UC_CPU;
    345 	pcb2->pcb_syscall_ucp.uc_link = &pcb2->pcb_userret_ucp;
    346 	thunk_makecontext(&pcb2->pcb_syscall_ucp, (void (*)(void)) syscall,
    347 	    0, NULL, NULL, NULL);
    348 
    349 	/* set up the ucontext for the pagefault */
    350 	pcb2->pcb_pagefault_ucp.uc_stack.ss_sp = stack_pagefault_ucp;
    351 	pcb2->pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
    352 	pcb2->pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
    353 	pcb2->pcb_pagefault_ucp.uc_link = &pcb2->pcb_trapret_ucp;
    354 	thunk_makecontext(&pcb2->pcb_pagefault_ucp, (void (*)(void)) pagefault,
    355 	    0, NULL, NULL, NULL);
    356 }
    357 
    358 void
    359 cpu_initclocks(void)
    360 {
    361 	struct thunk_itimerval itimer;
    362 
    363 	itimer.it_interval.tv_sec = 0;
    364 	itimer.it_interval.tv_usec = 1000000 / HZ;
    365 	itimer.it_value = itimer.it_interval;
    366 	thunk_setitimer(ITIMER_REAL, &itimer, NULL);
    367 }
    368 
    369 void
    370 cpu_startup(void)
    371 {
    372 	size_t stacksize;
    373 	void *stack_pagefault_ucp;
    374 
    375 	msgbuf = thunk_malloc(PAGE_SIZE);
    376 	if (msgbuf == NULL)
    377 		panic("couldn't allocate msgbuf");
    378 	initmsgbuf(msgbuf, PAGE_SIZE);
    379 
    380 	banner();
    381 
    382 	memset(&lwp0pcb, 0, sizeof(lwp0pcb));
    383 	if (thunk_getcontext(&lwp0pcb.pcb_ucp))
    384 		panic("getcontext failed");
    385 	uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
    386 
    387 	/* init trapframes (going nowhere!), maybe a panic func? */
    388 	memcpy(&lwp0pcb.pcb_syscall_ucp,   &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
    389 	memcpy(&lwp0pcb.pcb_userret_ucp,   &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
    390 	memcpy(&lwp0pcb.pcb_pagefault_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
    391 	memcpy(&lwp0pcb.pcb_trapret_ucp,   &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
    392 
    393 	/* set up the ucontext for the pagefault */
    394 	stacksize = 16*PAGE_SIZE;
    395 	stack_pagefault_ucp = malloc(stacksize, M_TEMP, M_NOWAIT);
    396 
    397 	lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_sp = stack_pagefault_ucp;
    398 	lwp0pcb.pcb_pagefault_ucp.uc_stack.ss_size = stacksize;
    399 	lwp0pcb.pcb_pagefault_ucp.uc_flags = _UC_STACK | _UC_CPU;
    400 	lwp0pcb.pcb_pagefault_ucp.uc_link = &lwp0pcb.pcb_userret_ucp;
    401 	thunk_makecontext(&lwp0pcb.pcb_pagefault_ucp, (void (*)(void)) pagefault,
    402 	    0, NULL, NULL, NULL);
    403 }
    404 
    405 void
    406 cpu_rootconf(void)
    407 {
    408 	device_t rdev;
    409 
    410 	rdev = device_find_by_xname("ld0");
    411 	if (rdev == NULL)
    412 		rdev = device_find_by_xname("md0");
    413 
    414 	aprint_normal("boot device: %s\n",
    415 	    rdev ? device_xname(rdev) : "<unknown>");
    416 	setroot(rdev, 0);
    417 }
    418 
    419 bool
    420 cpu_intr_p(void)
    421 {
    422 	int idepth;
    423 
    424 	kpreempt_disable();
    425 	idepth = curcpu()->ci_idepth;
    426 	kpreempt_enable();
    427 
    428 	return (idepth >= 0);
    429 }
    430