Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.26
      1 /* $NetBSD: cpu.c,v 1.26 2011/09/03 15:00:28 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.26 2011/09/03 15:00:28 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 	__builtin_unreachable();
    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 	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");
    186 	} else {
    187 		if (thunk_setcontext(&newpcb->pcb_ucp))
    188 			panic("setcontext failed");
    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 	pcb = lwp_getpcb(curlwp);
    281 
    282 	/* switch to userland */
    283 printf("switching to userland\n");
    284 	thunk_setcontext(&pcb->pcb_userland_ucp);
    285 
    286 	panic("%s: shouldn't return", __func__);
    287 }
    288 
    289 void
    290 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    291     void (*func)(void *), void *arg)
    292 {
    293 	struct pcb *pcb1 = lwp_getpcb(l1);
    294 	struct pcb *pcb2 = lwp_getpcb(l2);
    295 
    296 #ifdef CPU_DEBUG
    297 	printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    298 	    l1 ? l1->l_name : "none", l1,
    299 	    l2 ? l2->l_name : "none", l2,
    300 	    stack, (int)stacksize);
    301 #endif
    302 
    303 	/* copy the PCB and its switchframes from parent */
    304 	memcpy(pcb2, pcb1, sizeof(struct pcb));
    305 
    306 	/* XXXJDM */
    307 	if (stack == NULL) {
    308 		stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
    309 		stacksize = PAGE_SIZE;
    310 		pcb2->pcb_needfree = true;
    311 	} else
    312 		pcb2->pcb_needfree = false;
    313 
    314 	if (thunk_getcontext(&pcb2->pcb_ucp))
    315 		panic("getcontext failed");
    316 
    317 	pcb2->pcb_ucp.uc_stack.ss_sp = stack;
    318 	pcb2->pcb_ucp.uc_stack.ss_size = stacksize;
    319 	pcb2->pcb_ucp.uc_link = NULL;
    320 	pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
    321 	thunk_makecontext(&pcb2->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
    322 	    2, func, arg);
    323 }
    324 
    325 void
    326 cpu_initclocks(void)
    327 {
    328 }
    329 
    330 int syscall(lwp_t *l, struct trapframe *tr);
    331 void
    332 cpu_startup(void)
    333 {
    334 
    335 	msgbuf = thunk_malloc(PAGE_SIZE);
    336 	if (msgbuf == NULL)
    337 		panic("couldn't allocate msgbuf");
    338 	initmsgbuf(msgbuf, PAGE_SIZE);
    339 
    340 	banner();
    341 
    342 	memset(&lwp0pcb, 0, sizeof(lwp0pcb));
    343 	if (thunk_getcontext(&lwp0pcb.pcb_ucp))
    344 		panic("getcontext failed");
    345 	uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
    346 
    347 	/* init trapframe (going nowhere!), maybe a panic func? */
    348 	lwp0pcb.pcb_tf.tf_syscall = syscall;
    349 	memcpy(&lwp0pcb.pcb_userland_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
    350 //	thunk_makecontext_trapframe2go(&lwp0pcb.pcb_userland_ucp, NULL, NULL);
    351 }
    352 
    353 void
    354 cpu_rootconf(void)
    355 {
    356 	device_t rdev;
    357 
    358 	rdev = device_find_by_xname("ld0");
    359 	if (rdev == NULL)
    360 		rdev = device_find_by_xname("md0");
    361 
    362 	aprint_normal("boot device: %s\n",
    363 	    rdev ? device_xname(rdev) : "<unknown>");
    364 	setroot(rdev, 0);
    365 }
    366 
    367 bool
    368 cpu_intr_p(void)
    369 {
    370 	int idepth;
    371 
    372 	kpreempt_disable();
    373 	idepth = curcpu()->ci_idepth;
    374 	kpreempt_enable();
    375 
    376 	return (idepth >= 0);
    377 }
    378