Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.31
      1 /* $NetBSD: cpu.c,v 1.31 2011/09/05 12:40:38 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.31 2011/09/05 12:40:38 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 #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 	int s;
    163 
    164 #ifdef CPU_DEBUG
    165 	printf("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
    166 	    oldlwp ? oldlwp->l_name : "none",
    167 	    oldlwp ? oldlwp->l_proc->p_pid : -1,
    168 	    oldlwp ? oldlwp->l_lid : -1,
    169 	    newlwp ? newlwp->l_name : "none",
    170 	    newlwp ? newlwp->l_proc->p_pid : -1,
    171 	    newlwp ? newlwp->l_lid : -1);
    172 	if (oldpcb) {
    173 		printf("    oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
    174 		    "uc_stack.ss_size=%d\n",
    175 		    oldpcb->pcb_ucp.uc_link,
    176 		    oldpcb->pcb_ucp.uc_stack.ss_sp,
    177 		    (int)oldpcb->pcb_ucp.uc_stack.ss_size);
    178 	}
    179 	if (newpcb) {
    180 		printf("    newpcb uc_link=%p, uc_stack.ss_sp=%p, "
    181 		    "uc_stack.ss_size=%d\n",
    182 		    newpcb->pcb_ucp.uc_link,
    183 		    newpcb->pcb_ucp.uc_stack.ss_sp,
    184 		    (int)newpcb->pcb_ucp.uc_stack.ss_size);
    185 	}
    186 #endif /* !CPU_DEBUG */
    187 
    188 	s = splsched();
    189 
    190 	ci->ci_stash = oldlwp;
    191 	curlwp = newlwp;
    192 
    193 	if (oldpcb) {
    194 		oldpcb->pcb_errno = thunk_geterrno();
    195 		if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
    196 			panic("swapcontext failed");
    197 	} else {
    198 		if (thunk_setcontext(&newpcb->pcb_ucp))
    199 			panic("setcontext failed");
    200 	}
    201 	thunk_seterrno(newpcb->pcb_errno);
    202 
    203 	splx(s);
    204 
    205 #ifdef CPU_DEBUG
    206 	printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
    207 #endif
    208 	return ci->ci_stash;
    209 }
    210 
    211 void
    212 cpu_dumpconf(void)
    213 {
    214 #ifdef CPU_DEBUG
    215 	printf("cpu_dumpconf\n");
    216 #endif
    217 }
    218 
    219 void
    220 cpu_signotify(struct lwp *l)
    221 {
    222 }
    223 
    224 void
    225 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    226 {
    227 #ifdef CPU_DEBUG
    228 	printf("cpu_getmcontext\n");
    229 #endif
    230 }
    231 
    232 int
    233 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    234 {
    235 #ifdef CPU_DEBUG
    236 	printf("cpu_setmcontext\n");
    237 #endif
    238 	return 0;
    239 }
    240 
    241 void
    242 cpu_idle(void)
    243 {
    244 	struct cpu_info *ci = curcpu();
    245 
    246 	if (ci->ci_want_resched)
    247 		return;
    248 
    249 #if notyet
    250 	thunk_usleep(10000);
    251 #endif
    252 }
    253 
    254 void
    255 cpu_lwp_free(struct lwp *l, int proc)
    256 {
    257 #ifdef CPU_DEBUG
    258 	printf("cpu_lwp_free\n");
    259 #endif
    260 }
    261 
    262 void
    263 cpu_lwp_free2(struct lwp *l)
    264 {
    265 	struct pcb *pcb = lwp_getpcb(l);
    266 
    267 #ifdef CPU_DEBUG
    268 	printf("cpu_lwp_free2\n");
    269 #endif
    270 
    271 	if (pcb == NULL)
    272 		return;
    273 
    274 	if (pcb->pcb_needfree) {
    275 		free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
    276 		pcb->pcb_ucp.uc_stack.ss_sp = NULL;
    277 		pcb->pcb_ucp.uc_stack.ss_size = 0;
    278 		pcb->pcb_needfree = false;
    279 	}
    280 }
    281 
    282 static void
    283 cpu_lwp_trampoline(void (*func)(void *), void *arg)
    284 {
    285 	struct pcb *pcb;
    286 
    287 #ifdef CPU_DEBUG
    288 	printf("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
    289 #endif
    290 	lwp_startup(curcpu()->ci_stash, curlwp);
    291 
    292 	func(arg);
    293 
    294 	pcb = lwp_getpcb(curlwp);
    295 
    296 	/* switch to userland */
    297 printf("switching to userland\n");
    298 	thunk_setcontext(&pcb->pcb_userland_ucp);
    299 
    300 	panic("%s: shouldn't return", __func__);
    301 }
    302 
    303 void
    304 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    305     void (*func)(void *), void *arg)
    306 {
    307 	struct pcb *pcb1 = lwp_getpcb(l1);
    308 	struct pcb *pcb2 = lwp_getpcb(l2);
    309 
    310 #ifdef CPU_DEBUG
    311 	printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    312 	    l1 ? l1->l_name : "none", l1,
    313 	    l2 ? l2->l_name : "none", l2,
    314 	    stack, (int)stacksize);
    315 #endif
    316 
    317 	/* copy the PCB and its switchframes from parent */
    318 	memcpy(pcb2, pcb1, sizeof(struct pcb));
    319 
    320 	/* XXXJDM */
    321 	if (stack == NULL) {
    322 		stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
    323 		stacksize = PAGE_SIZE;
    324 		pcb2->pcb_needfree = true;
    325 	} else
    326 		pcb2->pcb_needfree = false;
    327 
    328 	if (thunk_getcontext(&pcb2->pcb_ucp))
    329 		panic("getcontext failed");
    330 
    331 	pcb2->pcb_ucp.uc_stack.ss_sp = stack;
    332 	pcb2->pcb_ucp.uc_stack.ss_size = stacksize;
    333 	pcb2->pcb_ucp.uc_link = NULL;
    334 	pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
    335 	thunk_makecontext(&pcb2->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
    336 	    2, func, arg);
    337 }
    338 
    339 void
    340 cpu_initclocks(void)
    341 {
    342 }
    343 
    344 int syscall(lwp_t *l, struct trapframe *tr);
    345 void
    346 cpu_startup(void)
    347 {
    348 
    349 	msgbuf = thunk_malloc(PAGE_SIZE);
    350 	if (msgbuf == NULL)
    351 		panic("couldn't allocate msgbuf");
    352 	initmsgbuf(msgbuf, PAGE_SIZE);
    353 
    354 	banner();
    355 
    356 	memset(&lwp0pcb, 0, sizeof(lwp0pcb));
    357 	if (thunk_getcontext(&lwp0pcb.pcb_ucp))
    358 		panic("getcontext failed");
    359 	uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
    360 
    361 	/* init trapframe (going nowhere!), maybe a panic func? */
    362 	lwp0pcb.pcb_tf.tf_syscall = syscall;
    363 	memcpy(&lwp0pcb.pcb_userland_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
    364 //	thunk_makecontext_trapframe2go(&lwp0pcb.pcb_userland_ucp, NULL, NULL);
    365 }
    366 
    367 void
    368 cpu_rootconf(void)
    369 {
    370 	device_t rdev;
    371 
    372 	rdev = device_find_by_xname("ld0");
    373 	if (rdev == NULL)
    374 		rdev = device_find_by_xname("md0");
    375 
    376 	aprint_normal("boot device: %s\n",
    377 	    rdev ? device_xname(rdev) : "<unknown>");
    378 	setroot(rdev, 0);
    379 }
    380 
    381 bool
    382 cpu_intr_p(void)
    383 {
    384 	int idepth;
    385 
    386 	kpreempt_disable();
    387 	idepth = curcpu()->ci_idepth;
    388 	kpreempt_enable();
    389 
    390 	return (idepth >= 0);
    391 }
    392