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