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