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