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