Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.22
      1 /* $NetBSD: cpu.c,v 1.22 2011/08/28 21:31:41 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 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.22 2011/08/28 21:31:41 jmcneill Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/conf.h>
     36 #include <sys/proc.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/reboot.h>
     40 #include <sys/lwp.h>
     41 #include <sys/cpu.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/msgbuf.h>
     44 
     45 #include <dev/cons.h>
     46 
     47 #include <machine/cpu.h>
     48 #include <machine/mainbus.h>
     49 #include <machine/pcb.h>
     50 #include <machine/thunk.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 #include <uvm/uvm_page.h>
     54 
     55 static int	cpu_match(device_t, cfdata_t, void *);
     56 static void	cpu_attach(device_t, device_t, void *);
     57 
     58 struct cpu_info cpu_info_primary = {
     59 	.ci_dev = 0,
     60 	.ci_self = &cpu_info_primary,
     61 	.ci_idepth = -1,
     62 	.ci_curlwp = &lwp0,
     63 };
     64 
     65 char cpu_model[48] = "virtual processor";
     66 
     67 typedef struct cpu_softc {
     68 	device_t	sc_dev;
     69 	struct cpu_info	*sc_ci;
     70 } cpu_softc_t;
     71 
     72 static struct pcb lwp0pcb;
     73 static void *msgbuf;
     74 
     75 CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
     76 
     77 static int
     78 cpu_match(device_t parent, cfdata_t match, void *opaque)
     79 {
     80 	struct thunkbus_attach_args *taa = opaque;
     81 
     82 	if (taa->taa_type != THUNKBUS_TYPE_CPU)
     83 		return 0;
     84 
     85 	return 1;
     86 }
     87 
     88 static void
     89 cpu_attach(device_t parent, device_t self, void *opaque)
     90 {
     91 	cpu_softc_t *sc = device_private(self);
     92 
     93 	aprint_naive("\n");
     94 	aprint_normal("\n");
     95 
     96 	sc->sc_dev = self;
     97 	sc->sc_ci = &cpu_info_primary;
     98 }
     99 
    100 void
    101 cpu_configure(void)
    102 {
    103 	if (config_rootfound("mainbus", NULL) == NULL)
    104 		panic("configure: mainbus not configured");
    105 
    106 	spl0();
    107 }
    108 
    109 void
    110 cpu_reboot(int howto, char *bootstr)
    111 {
    112 	extern void usermode_reboot(void);
    113 
    114 	splhigh();
    115 
    116 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
    117 		thunk_exit(0);
    118 
    119 	if (howto & RB_DUMP)
    120 		thunk_abort();
    121 
    122 	if (howto & RB_HALT) {
    123 		printf("\n");
    124 		printf("The operating system has halted.\n");
    125 		printf("Please press any key to reboot.\n\n");
    126 		cnpollc(1);
    127 		cngetc();
    128 		cnpollc(0);
    129 	}
    130 
    131 	printf("rebooting...\n");
    132 
    133 	usermode_reboot();
    134 
    135 	/* NOTREACHED */
    136 }
    137 
    138 void
    139 cpu_need_resched(struct cpu_info *ci, int flags)
    140 {
    141 	ci->ci_want_resched |= flags;
    142 }
    143 
    144 void
    145 cpu_need_proftick(struct lwp *l)
    146 {
    147 }
    148 
    149 lwp_t *
    150 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
    151 {
    152 	extern int errno;
    153 	struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
    154 	struct pcb *newpcb = lwp_getpcb(newlwp);
    155 	struct cpu_info *ci = curcpu();
    156 
    157 #ifdef CPU_DEBUG
    158 	printf("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
    159 	    oldlwp ? oldlwp->l_name : "none",
    160 	    oldlwp ? oldlwp->l_proc->p_pid : -1,
    161 	    oldlwp ? oldlwp->l_lid : -1,
    162 	    newlwp ? newlwp->l_name : "none",
    163 	    newlwp ? newlwp->l_proc->p_pid : -1,
    164 	    newlwp ? newlwp->l_lid : -1);
    165 	if (oldpcb) {
    166 		printf("    oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
    167 		    "uc_stack.ss_size=%d\n",
    168 		    oldpcb->pcb_ucp.uc_link,
    169 		    oldpcb->pcb_ucp.uc_stack.ss_sp,
    170 		    (int)oldpcb->pcb_ucp.uc_stack.ss_size);
    171 	}
    172 	if (newpcb) {
    173 		printf("    newpcb uc_link=%p, uc_stack.ss_sp=%p, "
    174 		    "uc_stack.ss_size=%d\n",
    175 		    newpcb->pcb_ucp.uc_link,
    176 		    newpcb->pcb_ucp.uc_stack.ss_sp,
    177 		    (int)newpcb->pcb_ucp.uc_stack.ss_size);
    178 	}
    179 #endif /* !CPU_DEBUG */
    180 
    181 	ci->ci_stash = oldlwp;
    182 	curlwp = newlwp;
    183 	if (oldpcb) {
    184 		if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
    185 			panic("swapcontext failed: %d", errno);
    186 	} else {
    187 		if (thunk_setcontext(&newpcb->pcb_ucp))
    188 			panic("setcontext failed: %d", errno);
    189 	}
    190 
    191 #ifdef CPU_DEBUG
    192 	printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
    193 #endif
    194 	return ci->ci_stash;
    195 }
    196 
    197 void
    198 cpu_dumpconf(void)
    199 {
    200 #ifdef CPU_DEBUG
    201 	printf("cpu_dumpconf\n");
    202 #endif
    203 }
    204 
    205 void
    206 cpu_signotify(struct lwp *l)
    207 {
    208 }
    209 
    210 void
    211 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    212 {
    213 #ifdef CPU_DEBUG
    214 	printf("cpu_getmcontext\n");
    215 #endif
    216 }
    217 
    218 int
    219 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    220 {
    221 #ifdef CPU_DEBUG
    222 	printf("cpu_setmcontext\n");
    223 #endif
    224 	return 0;
    225 }
    226 
    227 void
    228 cpu_idle(void)
    229 {
    230 	struct cpu_info *ci = curcpu();
    231 
    232 	if (ci->ci_want_resched)
    233 		return;
    234 
    235 #if notyet
    236 	thunk_usleep(10000);
    237 #endif
    238 }
    239 
    240 void
    241 cpu_lwp_free(struct lwp *l, int proc)
    242 {
    243 #ifdef CPU_DEBUG
    244 	printf("cpu_lwp_free\n");
    245 #endif
    246 }
    247 
    248 void
    249 cpu_lwp_free2(struct lwp *l)
    250 {
    251 	struct pcb *pcb = lwp_getpcb(l);
    252 
    253 #ifdef CPU_DEBUG
    254 	printf("cpu_lwp_free2\n");
    255 #endif
    256 
    257 	if (pcb == NULL)
    258 		return;
    259 
    260 	if (pcb->pcb_needfree) {
    261 		free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
    262 		pcb->pcb_ucp.uc_stack.ss_sp = NULL;
    263 		pcb->pcb_ucp.uc_stack.ss_size = 0;
    264 		pcb->pcb_needfree = false;
    265 	}
    266 }
    267 
    268 static void
    269 cpu_lwp_trampoline(void (*func)(void *), void *arg)
    270 {
    271 	struct pcb *pcb;
    272 
    273 #ifdef CPU_DEBUG
    274 	printf("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
    275 #endif
    276 	lwp_startup(curcpu()->ci_stash, curlwp);
    277 
    278 	func(arg);
    279 
    280 printf("%s: setting ucontext on lwp %p\n", __func__, curlwp);
    281 pcb = lwp_getpcb(curlwp);
    282 printf("pcb %p\n", pcb);
    283 printf("\tpcb->pcb_ucp.uc_stack.ss_sp   = %p\n", pcb->pcb_ucp.uc_stack.ss_sp);
    284 printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n", (int) pcb->pcb_ucp.uc_stack.ss_size);
    285 
    286 	/* switch to userland */
    287 	thunk_setcontext(&pcb->pcb_ucp);
    288 
    289 	panic("%s: shouldn't return", __func__);
    290 }
    291 
    292 void
    293 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    294     void (*func)(void *), void *arg)
    295 {
    296 	extern int errno;
    297 	struct pcb *pcb = lwp_getpcb(l2);
    298 
    299 #ifdef CPU_DEBUG
    300 	printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    301 	    l1 ? l1->l_name : "none", l1,
    302 	    l2 ? l2->l_name : "none", l2,
    303 	    stack, (int)stacksize);
    304 #endif
    305 
    306 	/* XXXJDM */
    307 	if (stack == NULL) {
    308 		stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
    309 		stacksize = PAGE_SIZE;
    310 		pcb->pcb_needfree = true;
    311 	} else
    312 		pcb->pcb_needfree = false;
    313 
    314 	if (thunk_getcontext(&pcb->pcb_ucp))
    315 		panic("getcontext failed: %d", errno);
    316 	pcb->pcb_ucp.uc_stack.ss_sp = stack;
    317 	pcb->pcb_ucp.uc_stack.ss_size = stacksize;
    318 	pcb->pcb_ucp.uc_link = NULL;
    319 	pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
    320 	thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
    321 	    2, func, arg);
    322 }
    323 
    324 void
    325 cpu_initclocks(void)
    326 {
    327 }
    328 
    329 void
    330 cpu_startup(void)
    331 {
    332 
    333 	msgbuf = thunk_malloc(PAGE_SIZE);
    334 	if (msgbuf == NULL)
    335 		panic("couldn't allocate msgbuf");
    336 	initmsgbuf(msgbuf, PAGE_SIZE);
    337 
    338 	banner();
    339 
    340 	memset(&lwp0pcb, 0, sizeof(lwp0pcb));
    341 	if (thunk_getcontext(&lwp0pcb.pcb_ucp))
    342 		panic("getcontext failed");
    343 	uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
    344 }
    345 
    346 void
    347 cpu_rootconf(void)
    348 {
    349 	device_t rdev;
    350 
    351 	rdev = device_find_by_xname("ld0");
    352 	if (rdev == NULL)
    353 		rdev = device_find_by_xname("md0");
    354 
    355 	aprint_normal("boot device: %s\n",
    356 	    rdev ? device_xname(rdev) : "<unknown>");
    357 	setroot(rdev, 0);
    358 }
    359 
    360 bool
    361 cpu_intr_p(void)
    362 {
    363 	int idepth;
    364 
    365 	kpreempt_disable();
    366 	idepth = curcpu()->ci_idepth;
    367 	kpreempt_enable();
    368 
    369 	return (idepth >= 0);
    370 }
    371