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