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