Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.10
      1 /* $NetBSD: cpu.c,v 1.10 2011/08/12 00:57:24 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 <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.10 2011/08/12 00:57:24 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/conf.h>
     34 #include <sys/proc.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/reboot.h>
     38 #include <sys/lwp.h>
     39 #include <sys/cpu.h>
     40 #include <sys/mbuf.h>
     41 
     42 #include <dev/cons.h>
     43 
     44 #include <machine/cpu.h>
     45 #include <machine/mainbus.h>
     46 #include <machine/pcb.h>
     47 #include <machine/thunk.h>
     48 
     49 #include <uvm/uvm_extern.h>
     50 #include <uvm/uvm_page.h>
     51 
     52 /* #define CPU_DEBUG */
     53 
     54 static int	cpu_match(device_t, cfdata_t, void *);
     55 static void	cpu_attach(device_t, device_t, void *);
     56 
     57 struct cpu_info cpu_info_primary;
     58 char cpu_model[48] = "virtual processor";
     59 
     60 typedef struct cpu_softc {
     61 	device_t	sc_dev;
     62 	struct cpu_info	*sc_ci;
     63 } cpu_softc_t;
     64 
     65 static ucontext_t lwp0pcb;
     66 
     67 CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
     68 
     69 static int
     70 cpu_match(device_t parent, cfdata_t match, void *opaque)
     71 {
     72 	struct thunkbus_attach_args *taa = opaque;
     73 
     74 	if (taa->taa_type != THUNKBUS_TYPE_CPU)
     75 		return 0;
     76 
     77 	return 1;
     78 }
     79 
     80 static void
     81 cpu_attach(device_t parent, device_t self, void *opaque)
     82 {
     83 	cpu_softc_t *sc = device_private(self);
     84 
     85 	aprint_naive("\n");
     86 	aprint_normal("\n");
     87 
     88 	sc->sc_dev = self;
     89 	sc->sc_ci = &cpu_info_primary;
     90 	sc->sc_ci->ci_dev = 0;
     91 	sc->sc_ci->ci_self = &cpu_info_primary;
     92 	sc->sc_ci->ci_curlwp = &lwp0;
     93 
     94 	if (thunk_getcontext(&lwp0pcb))
     95 		panic("getcontext failed");
     96 	uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
     97 }
     98 
     99 void
    100 cpu_configure(void)
    101 {
    102 	if (config_rootfound("mainbus", NULL) == NULL)
    103 		panic("configure: mainbus not configured");
    104 
    105 	spl0();
    106 }
    107 
    108 void
    109 cpu_reboot(int howto, char *bootstr)
    110 {
    111 	splhigh();
    112 
    113 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
    114 		thunk_exit(0);
    115 
    116 	if (howto & RB_HALT) {
    117 		printf("\n");
    118 		printf("The operating system has halted.\n");
    119 		printf("Please press any key to reboot.\n\n");
    120 		cnpollc(1);
    121 		cngetc();
    122 		cnpollc(0);
    123 	}
    124 
    125 	printf("rebooting...\n");
    126 
    127 	/*
    128 	 * XXXJDM If we've panic'd, make sure we dump a core
    129 	 */
    130 	thunk_abort();
    131 
    132 	/* NOTREACHED */
    133 }
    134 
    135 void
    136 cpu_need_resched(struct cpu_info *ci, int flags)
    137 {
    138 	ci->ci_want_resched |= flags;
    139 }
    140 
    141 void
    142 cpu_need_proftick(struct lwp *l)
    143 {
    144 }
    145 
    146 lwp_t *
    147 cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
    148 {
    149 	extern int errno;
    150 	struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
    151 	struct pcb *newpcb = lwp_getpcb(newlwp);
    152 	struct cpu_info *ci = curcpu();
    153 
    154 #ifdef CPU_DEBUG
    155 	printf("cpu_switchto [%s] -> [%s]\n",
    156 	    oldlwp ? oldlwp->l_name : "none",
    157 	    newlwp ? newlwp->l_name : "none");
    158 	if (oldpcb) {
    159 		printf("    oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
    160 		    "uc_stack.ss_size=%d\n",
    161 		    oldpcb->pcb_ucp.uc_link,
    162 		    oldpcb->pcb_ucp.uc_stack.ss_sp,
    163 		    (int)oldpcb->pcb_ucp.uc_stack.ss_size);
    164 	}
    165 	if (newpcb) {
    166 		printf("    newpcb uc_link=%p, uc_stack.ss_sp=%p, "
    167 		    "uc_stack.ss_size=%d\n",
    168 		    newpcb->pcb_ucp.uc_link,
    169 		    newpcb->pcb_ucp.uc_stack.ss_sp,
    170 		    (int)newpcb->pcb_ucp.uc_stack.ss_size);
    171 	}
    172 #endif /* !CPU_DEBUG */
    173 
    174 	ci->ci_stash = oldlwp;
    175 	curlwp = newlwp;
    176 	if (oldpcb) {
    177 		if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
    178 			panic("swapcontext failed: %d", errno);
    179 	} else {
    180 		if (thunk_setcontext(&newpcb->pcb_ucp))
    181 			panic("setcontext failed: %d", errno);
    182 	}
    183 
    184 #ifdef CPU_DEBUG
    185 	printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
    186 #endif
    187 	return ci->ci_stash;
    188 }
    189 
    190 void
    191 cpu_dumpconf(void)
    192 {
    193 #ifdef CPU_DEBUG
    194 	printf("cpu_dumpconf\n");
    195 #endif
    196 }
    197 
    198 void
    199 cpu_signotify(struct lwp *l)
    200 {
    201 #ifdef CPU_DEBUG
    202 	printf("cpu_signotify\n");
    203 #endif
    204 }
    205 
    206 void
    207 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    208 {
    209 #ifdef CPU_DEBUG
    210 	printf("cpu_getmcontext\n");
    211 #endif
    212 }
    213 
    214 int
    215 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    216 {
    217 #ifdef CPU_DEBUG
    218 	printf("cpu_setmcontext\n");
    219 #endif
    220 	return 0;
    221 }
    222 
    223 void
    224 cpu_idle(void)
    225 {
    226 	struct cpu_info *ci = curcpu();
    227 
    228 	if (ci->ci_want_resched)
    229 		return;
    230 
    231 #if notyet
    232 	thunk_usleep(10000);
    233 #endif
    234 }
    235 
    236 void
    237 cpu_lwp_free(struct lwp *l, int proc)
    238 {
    239 #ifdef CPU_DEBUG
    240 	printf("cpu_lwp_free\n");
    241 #endif
    242 }
    243 
    244 void
    245 cpu_lwp_free2(struct lwp *l)
    246 {
    247 	struct pcb *pcb = lwp_getpcb(l);
    248 
    249 #ifdef CPU_DEBUG
    250 	printf("cpu_lwp_free2\n");
    251 #endif
    252 
    253 	if (pcb == NULL)
    254 		return;
    255 
    256 	if (pcb->pcb_needfree) {
    257 		free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
    258 		pcb->pcb_ucp.uc_stack.ss_sp = NULL;
    259 		pcb->pcb_ucp.uc_stack.ss_size = 0;
    260 		pcb->pcb_needfree = false;
    261 	}
    262 }
    263 
    264 static void
    265 cpu_lwp_trampoline(void (*func)(void *), void *arg)
    266 {
    267 	lwp_startup(curcpu()->ci_stash, curlwp);
    268 
    269 	func(arg);
    270 }
    271 
    272 void
    273 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    274     void (*func)(void *), void *arg)
    275 {
    276 	extern int errno;
    277 	struct pcb *pcb = lwp_getpcb(l2);
    278 
    279 #ifdef CPU_DEBUG
    280 	printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    281 	    l1 ? l1->l_name : "none", l1,
    282 	    l2 ? l2->l_name : "none", l2,
    283 	    stack, (int)stacksize);
    284 #endif
    285 
    286 	/* XXXJDM */
    287 	if (stack == NULL) {
    288 		stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
    289 		stacksize = PAGE_SIZE;
    290 		pcb->pcb_needfree = true;
    291 	} else
    292 		pcb->pcb_needfree = false;
    293 
    294 	if (thunk_getcontext(&pcb->pcb_ucp))
    295 		panic("getcontext failed: %d", errno);
    296 	pcb->pcb_ucp.uc_stack.ss_sp = stack;
    297 	pcb->pcb_ucp.uc_stack.ss_size = stacksize;
    298 	pcb->pcb_ucp.uc_link = NULL;
    299 	pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
    300 	thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
    301 	    2, func, arg);
    302 }
    303 
    304 void
    305 cpu_initclocks(void)
    306 {
    307 }
    308 
    309 void
    310 cpu_startup(void)
    311 {
    312 	char pbuf[9];
    313 
    314 	printf("%s%s", copyright, version);
    315 	format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
    316 	printf("total memory = %s\n", pbuf);
    317 
    318 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    319 	printf("avail memory = %s\n", pbuf);
    320 }
    321 
    322 void
    323 cpu_rootconf(void)
    324 {
    325 	device_t rdev;
    326 
    327 	rdev = device_find_by_xname("md0");
    328 
    329 	setroot(rdev, 0);
    330 }
    331 
    332 bool
    333 cpu_intr_p(void)
    334 {
    335 	printf("cpu_intr_p\n");
    336 	return false;
    337 }
    338