Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.21
      1 /* $NetBSD: cpu.c,v 1.21 2011/08/28 19:41:34 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 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.21 2011/08/28 19:41:34 reinoud 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_HALT) {
    120 		printf("\n");
    121 		printf("The operating system has halted.\n");
    122 		printf("Please press any key to reboot.\n\n");
    123 		cnpollc(1);
    124 		cngetc();
    125 		cnpollc(0);
    126 	}
    127 
    128 	printf("rebooting...\n");
    129 
    130 #if defined(DIAGNOSTIC) || defined(DEBUG)
    131 	thunk_abort();
    132 #endif
    133 
    134 	usermode_reboot();
    135 
    136 	/* NOTREACHED */
    137 }
    138 
    139 void
    140 cpu_need_resched(struct cpu_info *ci, int flags)
    141 {
    142 	ci->ci_want_resched |= flags;
    143 }
    144 
    145 void
    146 cpu_need_proftick(struct lwp *l)
    147 {
    148 }
    149 
    150 lwp_t *
    151 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
    152 {
    153 	extern int errno;
    154 	struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
    155 	struct pcb *newpcb = lwp_getpcb(newlwp);
    156 	struct cpu_info *ci = curcpu();
    157 
    158 #ifdef CPU_DEBUG
    159 	printf("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
    160 	    oldlwp ? oldlwp->l_name : "none",
    161 	    oldlwp ? oldlwp->l_proc->p_pid : -1,
    162 	    oldlwp ? oldlwp->l_lid : -1,
    163 	    newlwp ? newlwp->l_name : "none",
    164 	    newlwp ? newlwp->l_proc->p_pid : -1,
    165 	    newlwp ? newlwp->l_lid : -1);
    166 	if (oldpcb) {
    167 		printf("    oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
    168 		    "uc_stack.ss_size=%d\n",
    169 		    oldpcb->pcb_ucp.uc_link,
    170 		    oldpcb->pcb_ucp.uc_stack.ss_sp,
    171 		    (int)oldpcb->pcb_ucp.uc_stack.ss_size);
    172 	}
    173 	if (newpcb) {
    174 		printf("    newpcb uc_link=%p, uc_stack.ss_sp=%p, "
    175 		    "uc_stack.ss_size=%d\n",
    176 		    newpcb->pcb_ucp.uc_link,
    177 		    newpcb->pcb_ucp.uc_stack.ss_sp,
    178 		    (int)newpcb->pcb_ucp.uc_stack.ss_size);
    179 	}
    180 #endif /* !CPU_DEBUG */
    181 
    182 	ci->ci_stash = oldlwp;
    183 	curlwp = newlwp;
    184 	if (oldpcb) {
    185 		if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
    186 			panic("swapcontext failed: %d", errno);
    187 	} else {
    188 		if (thunk_setcontext(&newpcb->pcb_ucp))
    189 			panic("setcontext failed: %d", errno);
    190 	}
    191 
    192 #ifdef CPU_DEBUG
    193 	printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
    194 #endif
    195 	return ci->ci_stash;
    196 }
    197 
    198 void
    199 cpu_dumpconf(void)
    200 {
    201 #ifdef CPU_DEBUG
    202 	printf("cpu_dumpconf\n");
    203 #endif
    204 }
    205 
    206 void
    207 cpu_signotify(struct lwp *l)
    208 {
    209 }
    210 
    211 void
    212 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    213 {
    214 #ifdef CPU_DEBUG
    215 	printf("cpu_getmcontext\n");
    216 #endif
    217 }
    218 
    219 int
    220 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    221 {
    222 #ifdef CPU_DEBUG
    223 	printf("cpu_setmcontext\n");
    224 #endif
    225 	return 0;
    226 }
    227 
    228 void
    229 cpu_idle(void)
    230 {
    231 	struct cpu_info *ci = curcpu();
    232 
    233 	if (ci->ci_want_resched)
    234 		return;
    235 
    236 #if notyet
    237 	thunk_usleep(10000);
    238 #endif
    239 }
    240 
    241 void
    242 cpu_lwp_free(struct lwp *l, int proc)
    243 {
    244 #ifdef CPU_DEBUG
    245 	printf("cpu_lwp_free\n");
    246 #endif
    247 }
    248 
    249 void
    250 cpu_lwp_free2(struct lwp *l)
    251 {
    252 	struct pcb *pcb = lwp_getpcb(l);
    253 
    254 #ifdef CPU_DEBUG
    255 	printf("cpu_lwp_free2\n");
    256 #endif
    257 
    258 	if (pcb == NULL)
    259 		return;
    260 
    261 	if (pcb->pcb_needfree) {
    262 		free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
    263 		pcb->pcb_ucp.uc_stack.ss_sp = NULL;
    264 		pcb->pcb_ucp.uc_stack.ss_size = 0;
    265 		pcb->pcb_needfree = false;
    266 	}
    267 }
    268 
    269 static void
    270 cpu_lwp_trampoline(void (*func)(void *), void *arg)
    271 {
    272 	struct pcb *pcb;
    273 
    274 #ifdef CPU_DEBUG
    275 	printf("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
    276 #endif
    277 	lwp_startup(curcpu()->ci_stash, curlwp);
    278 
    279 	func(arg);
    280 
    281 printf("%s: setting ucontext on lwp %p\n", __func__, curlwp);
    282 pcb = lwp_getpcb(curlwp);
    283 printf("pcb %p\n", pcb);
    284 printf("\tpcb->pcb_ucp.uc_stack.ss_sp   = %p\n", pcb->pcb_ucp.uc_stack.ss_sp);
    285 printf("\tpcb->pcb_ucp.uc_stack.ss_size = %d\n", (int) pcb->pcb_ucp.uc_stack.ss_size);
    286 
    287 	/* switch to userland */
    288 	thunk_setcontext(&pcb->pcb_ucp);
    289 
    290 	panic("%s: shouldn't return", __func__);
    291 }
    292 
    293 void
    294 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    295     void (*func)(void *), void *arg)
    296 {
    297 	extern int errno;
    298 	struct pcb *pcb = lwp_getpcb(l2);
    299 
    300 #ifdef CPU_DEBUG
    301 	printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    302 	    l1 ? l1->l_name : "none", l1,
    303 	    l2 ? l2->l_name : "none", l2,
    304 	    stack, (int)stacksize);
    305 #endif
    306 
    307 	/* XXXJDM */
    308 	if (stack == NULL) {
    309 		stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
    310 		stacksize = PAGE_SIZE;
    311 		pcb->pcb_needfree = true;
    312 	} else
    313 		pcb->pcb_needfree = false;
    314 
    315 	if (thunk_getcontext(&pcb->pcb_ucp))
    316 		panic("getcontext failed: %d", errno);
    317 	pcb->pcb_ucp.uc_stack.ss_sp = stack;
    318 	pcb->pcb_ucp.uc_stack.ss_size = stacksize;
    319 	pcb->pcb_ucp.uc_link = NULL;
    320 	pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
    321 	thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
    322 	    2, func, arg);
    323 }
    324 
    325 void
    326 cpu_initclocks(void)
    327 {
    328 }
    329 
    330 void
    331 cpu_startup(void)
    332 {
    333 
    334 	msgbuf = thunk_malloc(PAGE_SIZE);
    335 	if (msgbuf == NULL)
    336 		panic("couldn't allocate msgbuf");
    337 	initmsgbuf(msgbuf, PAGE_SIZE);
    338 
    339 	banner();
    340 
    341 	memset(&lwp0pcb, 0, sizeof(lwp0pcb));
    342 	if (thunk_getcontext(&lwp0pcb.pcb_ucp))
    343 		panic("getcontext failed");
    344 	uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
    345 }
    346 
    347 void
    348 cpu_rootconf(void)
    349 {
    350 	device_t rdev;
    351 
    352 	rdev = device_find_by_xname("ld0");
    353 	if (rdev == NULL)
    354 		rdev = device_find_by_xname("md0");
    355 
    356 	aprint_normal("boot device: %s\n",
    357 	    rdev ? device_xname(rdev) : "<unknown>");
    358 	setroot(rdev, 0);
    359 }
    360 
    361 bool
    362 cpu_intr_p(void)
    363 {
    364 	int idepth;
    365 
    366 	kpreempt_disable();
    367 	idepth = curcpu()->ci_idepth;
    368 	kpreempt_enable();
    369 
    370 	return (idepth >= 0);
    371 }
    372