Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.19
      1  1.19  jmcneill /* $NetBSD: cpu.c,v 1.19 2011/08/27 21:43:06 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29  1.19  jmcneill #include "opt_cpu.h"
     30  1.19  jmcneill 
     31   1.1  jmcneill #include <sys/cdefs.h>
     32  1.19  jmcneill __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.19 2011/08/27 21:43:06 jmcneill Exp $");
     33   1.1  jmcneill 
     34   1.1  jmcneill #include <sys/param.h>
     35   1.1  jmcneill #include <sys/conf.h>
     36   1.1  jmcneill #include <sys/proc.h>
     37   1.1  jmcneill #include <sys/systm.h>
     38   1.1  jmcneill #include <sys/device.h>
     39   1.1  jmcneill #include <sys/reboot.h>
     40   1.1  jmcneill #include <sys/lwp.h>
     41   1.1  jmcneill #include <sys/cpu.h>
     42   1.1  jmcneill #include <sys/mbuf.h>
     43  1.16  jmcneill #include <sys/msgbuf.h>
     44   1.1  jmcneill 
     45   1.1  jmcneill #include <dev/cons.h>
     46   1.1  jmcneill 
     47   1.1  jmcneill #include <machine/cpu.h>
     48   1.1  jmcneill #include <machine/mainbus.h>
     49   1.8  jmcneill #include <machine/pcb.h>
     50  1.10  jmcneill #include <machine/thunk.h>
     51   1.1  jmcneill 
     52   1.1  jmcneill #include <uvm/uvm_extern.h>
     53   1.1  jmcneill #include <uvm/uvm_page.h>
     54   1.1  jmcneill 
     55   1.1  jmcneill static int	cpu_match(device_t, cfdata_t, void *);
     56   1.1  jmcneill static void	cpu_attach(device_t, device_t, void *);
     57   1.1  jmcneill 
     58  1.13  jmcneill struct cpu_info cpu_info_primary = {
     59  1.13  jmcneill 	.ci_dev = 0,
     60  1.13  jmcneill 	.ci_self = &cpu_info_primary,
     61  1.13  jmcneill 	.ci_idepth = -1,
     62  1.13  jmcneill 	.ci_curlwp = &lwp0,
     63  1.13  jmcneill };
     64  1.13  jmcneill 
     65   1.1  jmcneill char cpu_model[48] = "virtual processor";
     66   1.1  jmcneill 
     67   1.1  jmcneill typedef struct cpu_softc {
     68   1.1  jmcneill 	device_t	sc_dev;
     69   1.1  jmcneill 	struct cpu_info	*sc_ci;
     70   1.1  jmcneill } cpu_softc_t;
     71   1.1  jmcneill 
     72  1.15  jmcneill static struct pcb lwp0pcb;
     73  1.16  jmcneill static void *msgbuf;
     74   1.9  jmcneill 
     75   1.1  jmcneill CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
     76   1.1  jmcneill 
     77   1.1  jmcneill static int
     78   1.1  jmcneill cpu_match(device_t parent, cfdata_t match, void *opaque)
     79   1.1  jmcneill {
     80   1.1  jmcneill 	struct thunkbus_attach_args *taa = opaque;
     81   1.1  jmcneill 
     82   1.1  jmcneill 	if (taa->taa_type != THUNKBUS_TYPE_CPU)
     83   1.1  jmcneill 		return 0;
     84   1.1  jmcneill 
     85   1.1  jmcneill 	return 1;
     86   1.1  jmcneill }
     87   1.1  jmcneill 
     88   1.1  jmcneill static void
     89   1.1  jmcneill cpu_attach(device_t parent, device_t self, void *opaque)
     90   1.1  jmcneill {
     91   1.1  jmcneill 	cpu_softc_t *sc = device_private(self);
     92   1.1  jmcneill 
     93   1.1  jmcneill 	aprint_naive("\n");
     94   1.1  jmcneill 	aprint_normal("\n");
     95   1.1  jmcneill 
     96   1.1  jmcneill 	sc->sc_dev = self;
     97   1.1  jmcneill 	sc->sc_ci = &cpu_info_primary;
     98   1.1  jmcneill }
     99   1.1  jmcneill 
    100   1.1  jmcneill void
    101   1.1  jmcneill cpu_configure(void)
    102   1.1  jmcneill {
    103   1.1  jmcneill 	if (config_rootfound("mainbus", NULL) == NULL)
    104   1.1  jmcneill 		panic("configure: mainbus not configured");
    105   1.1  jmcneill 
    106   1.1  jmcneill 	spl0();
    107   1.1  jmcneill }
    108   1.1  jmcneill 
    109   1.1  jmcneill void
    110   1.1  jmcneill cpu_reboot(int howto, char *bootstr)
    111   1.1  jmcneill {
    112  1.11  jmcneill 	extern void usermode_reboot(void);
    113  1.11  jmcneill 
    114   1.1  jmcneill 	splhigh();
    115   1.1  jmcneill 
    116   1.1  jmcneill 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
    117  1.10  jmcneill 		thunk_exit(0);
    118   1.1  jmcneill 
    119   1.1  jmcneill 	if (howto & RB_HALT) {
    120   1.1  jmcneill 		printf("\n");
    121   1.1  jmcneill 		printf("The operating system has halted.\n");
    122   1.1  jmcneill 		printf("Please press any key to reboot.\n\n");
    123   1.1  jmcneill 		cnpollc(1);
    124   1.1  jmcneill 		cngetc();
    125   1.1  jmcneill 		cnpollc(0);
    126   1.1  jmcneill 	}
    127   1.1  jmcneill 
    128   1.1  jmcneill 	printf("rebooting...\n");
    129   1.1  jmcneill 
    130  1.13  jmcneill #if defined(DIAGNOSTIC) || defined(DEBUG)
    131  1.13  jmcneill 	thunk_abort();
    132  1.13  jmcneill #endif
    133  1.13  jmcneill 
    134  1.11  jmcneill 	usermode_reboot();
    135   1.1  jmcneill 
    136   1.1  jmcneill 	/* NOTREACHED */
    137   1.1  jmcneill }
    138   1.1  jmcneill 
    139   1.1  jmcneill void
    140   1.1  jmcneill cpu_need_resched(struct cpu_info *ci, int flags)
    141   1.1  jmcneill {
    142   1.9  jmcneill 	ci->ci_want_resched |= flags;
    143   1.1  jmcneill }
    144   1.1  jmcneill 
    145   1.1  jmcneill void
    146   1.1  jmcneill cpu_need_proftick(struct lwp *l)
    147   1.1  jmcneill {
    148   1.1  jmcneill }
    149   1.1  jmcneill 
    150   1.1  jmcneill lwp_t *
    151   1.1  jmcneill cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
    152   1.1  jmcneill {
    153   1.1  jmcneill 	extern int errno;
    154   1.5     rmind 	struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
    155   1.5     rmind 	struct pcb *newpcb = lwp_getpcb(newlwp);
    156   1.1  jmcneill 	struct cpu_info *ci = curcpu();
    157   1.1  jmcneill 
    158   1.1  jmcneill #ifdef CPU_DEBUG
    159  1.17  jmcneill 	printf("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
    160   1.1  jmcneill 	    oldlwp ? oldlwp->l_name : "none",
    161  1.17  jmcneill 	    oldlwp ? oldlwp->l_proc->p_pid : -1,
    162  1.17  jmcneill 	    oldlwp ? oldlwp->l_lid : -1,
    163  1.17  jmcneill 	    newlwp ? newlwp->l_name : "none",
    164  1.17  jmcneill 	    newlwp ? newlwp->l_proc->p_pid : -1,
    165  1.17  jmcneill 	    newlwp ? newlwp->l_lid : -1);
    166   1.1  jmcneill 	if (oldpcb) {
    167   1.1  jmcneill 		printf("    oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
    168   1.1  jmcneill 		    "uc_stack.ss_size=%d\n",
    169   1.1  jmcneill 		    oldpcb->pcb_ucp.uc_link,
    170   1.1  jmcneill 		    oldpcb->pcb_ucp.uc_stack.ss_sp,
    171   1.1  jmcneill 		    (int)oldpcb->pcb_ucp.uc_stack.ss_size);
    172   1.1  jmcneill 	}
    173   1.1  jmcneill 	if (newpcb) {
    174   1.1  jmcneill 		printf("    newpcb uc_link=%p, uc_stack.ss_sp=%p, "
    175   1.1  jmcneill 		    "uc_stack.ss_size=%d\n",
    176   1.1  jmcneill 		    newpcb->pcb_ucp.uc_link,
    177   1.1  jmcneill 		    newpcb->pcb_ucp.uc_stack.ss_sp,
    178   1.1  jmcneill 		    (int)newpcb->pcb_ucp.uc_stack.ss_size);
    179   1.1  jmcneill 	}
    180   1.1  jmcneill #endif /* !CPU_DEBUG */
    181   1.1  jmcneill 
    182   1.1  jmcneill 	ci->ci_stash = oldlwp;
    183   1.1  jmcneill 	curlwp = newlwp;
    184   1.1  jmcneill 	if (oldpcb) {
    185  1.10  jmcneill 		if (thunk_swapcontext(&oldpcb->pcb_ucp, &newpcb->pcb_ucp))
    186   1.1  jmcneill 			panic("swapcontext failed: %d", errno);
    187   1.1  jmcneill 	} else {
    188  1.10  jmcneill 		if (thunk_setcontext(&newpcb->pcb_ucp))
    189   1.1  jmcneill 			panic("setcontext failed: %d", errno);
    190   1.1  jmcneill 	}
    191   1.1  jmcneill 
    192   1.1  jmcneill #ifdef CPU_DEBUG
    193   1.1  jmcneill 	printf("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
    194   1.1  jmcneill #endif
    195   1.1  jmcneill 	return ci->ci_stash;
    196   1.1  jmcneill }
    197   1.1  jmcneill 
    198   1.1  jmcneill void
    199   1.1  jmcneill cpu_dumpconf(void)
    200   1.1  jmcneill {
    201   1.1  jmcneill #ifdef CPU_DEBUG
    202   1.1  jmcneill 	printf("cpu_dumpconf\n");
    203   1.1  jmcneill #endif
    204   1.1  jmcneill }
    205   1.1  jmcneill 
    206   1.1  jmcneill void
    207   1.1  jmcneill cpu_signotify(struct lwp *l)
    208   1.1  jmcneill {
    209   1.1  jmcneill }
    210   1.1  jmcneill 
    211   1.1  jmcneill void
    212   1.1  jmcneill cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    213   1.1  jmcneill {
    214   1.1  jmcneill #ifdef CPU_DEBUG
    215   1.1  jmcneill 	printf("cpu_getmcontext\n");
    216   1.1  jmcneill #endif
    217   1.1  jmcneill }
    218   1.1  jmcneill 
    219   1.1  jmcneill int
    220   1.1  jmcneill cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    221   1.1  jmcneill {
    222   1.1  jmcneill #ifdef CPU_DEBUG
    223   1.1  jmcneill 	printf("cpu_setmcontext\n");
    224   1.1  jmcneill #endif
    225   1.1  jmcneill 	return 0;
    226   1.1  jmcneill }
    227   1.1  jmcneill 
    228   1.1  jmcneill void
    229   1.1  jmcneill cpu_idle(void)
    230   1.1  jmcneill {
    231   1.1  jmcneill 	struct cpu_info *ci = curcpu();
    232   1.1  jmcneill 
    233   1.1  jmcneill 	if (ci->ci_want_resched)
    234   1.1  jmcneill 		return;
    235   1.1  jmcneill 
    236   1.1  jmcneill #if notyet
    237  1.10  jmcneill 	thunk_usleep(10000);
    238   1.1  jmcneill #endif
    239   1.1  jmcneill }
    240   1.1  jmcneill 
    241   1.1  jmcneill void
    242   1.1  jmcneill cpu_lwp_free(struct lwp *l, int proc)
    243   1.1  jmcneill {
    244   1.1  jmcneill #ifdef CPU_DEBUG
    245   1.1  jmcneill 	printf("cpu_lwp_free\n");
    246   1.1  jmcneill #endif
    247   1.1  jmcneill }
    248   1.1  jmcneill 
    249   1.1  jmcneill void
    250   1.1  jmcneill cpu_lwp_free2(struct lwp *l)
    251   1.1  jmcneill {
    252   1.5     rmind 	struct pcb *pcb = lwp_getpcb(l);
    253   1.1  jmcneill 
    254   1.1  jmcneill #ifdef CPU_DEBUG
    255   1.1  jmcneill 	printf("cpu_lwp_free2\n");
    256   1.1  jmcneill #endif
    257   1.1  jmcneill 
    258   1.1  jmcneill 	if (pcb == NULL)
    259   1.1  jmcneill 		return;
    260   1.1  jmcneill 
    261   1.1  jmcneill 	if (pcb->pcb_needfree) {
    262   1.1  jmcneill 		free(pcb->pcb_ucp.uc_stack.ss_sp, M_TEMP);
    263   1.1  jmcneill 		pcb->pcb_ucp.uc_stack.ss_sp = NULL;
    264   1.1  jmcneill 		pcb->pcb_ucp.uc_stack.ss_size = 0;
    265   1.1  jmcneill 		pcb->pcb_needfree = false;
    266   1.1  jmcneill 	}
    267   1.1  jmcneill }
    268   1.1  jmcneill 
    269   1.1  jmcneill static void
    270   1.1  jmcneill cpu_lwp_trampoline(void (*func)(void *), void *arg)
    271   1.1  jmcneill {
    272  1.18   reinoud #ifdef CPU_DEBUG
    273  1.18   reinoud 	printf("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
    274  1.18   reinoud #endif
    275   1.1  jmcneill 	lwp_startup(curcpu()->ci_stash, curlwp);
    276   1.1  jmcneill 
    277   1.1  jmcneill 	func(arg);
    278   1.1  jmcneill }
    279   1.1  jmcneill 
    280   1.1  jmcneill void
    281   1.1  jmcneill cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    282   1.1  jmcneill     void (*func)(void *), void *arg)
    283   1.1  jmcneill {
    284   1.1  jmcneill 	extern int errno;
    285   1.5     rmind 	struct pcb *pcb = lwp_getpcb(l2);
    286   1.1  jmcneill 
    287   1.1  jmcneill #ifdef CPU_DEBUG
    288   1.1  jmcneill 	printf("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    289   1.1  jmcneill 	    l1 ? l1->l_name : "none", l1,
    290   1.1  jmcneill 	    l2 ? l2->l_name : "none", l2,
    291   1.1  jmcneill 	    stack, (int)stacksize);
    292   1.1  jmcneill #endif
    293   1.1  jmcneill 
    294   1.1  jmcneill 	/* XXXJDM */
    295   1.1  jmcneill 	if (stack == NULL) {
    296   1.1  jmcneill 		stack = malloc(PAGE_SIZE, M_TEMP, M_NOWAIT);
    297   1.1  jmcneill 		stacksize = PAGE_SIZE;
    298   1.1  jmcneill 		pcb->pcb_needfree = true;
    299   1.1  jmcneill 	} else
    300   1.1  jmcneill 		pcb->pcb_needfree = false;
    301   1.1  jmcneill 
    302  1.10  jmcneill 	if (thunk_getcontext(&pcb->pcb_ucp))
    303   1.1  jmcneill 		panic("getcontext failed: %d", errno);
    304   1.1  jmcneill 	pcb->pcb_ucp.uc_stack.ss_sp = stack;
    305   1.1  jmcneill 	pcb->pcb_ucp.uc_stack.ss_size = stacksize;
    306   1.1  jmcneill 	pcb->pcb_ucp.uc_link = NULL;
    307   1.1  jmcneill 	pcb->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU;
    308  1.10  jmcneill 	thunk_makecontext(&pcb->pcb_ucp, (void (*)(void))cpu_lwp_trampoline,
    309   1.1  jmcneill 	    2, func, arg);
    310   1.1  jmcneill }
    311   1.1  jmcneill 
    312   1.1  jmcneill void
    313   1.1  jmcneill cpu_initclocks(void)
    314   1.1  jmcneill {
    315   1.1  jmcneill }
    316   1.1  jmcneill 
    317   1.1  jmcneill void
    318   1.1  jmcneill cpu_startup(void)
    319   1.1  jmcneill {
    320  1.16  jmcneill 
    321  1.16  jmcneill 	msgbuf = thunk_malloc(PAGE_SIZE);
    322  1.16  jmcneill 	if (msgbuf == NULL)
    323  1.16  jmcneill 		panic("couldn't allocate msgbuf");
    324  1.16  jmcneill 	initmsgbuf(msgbuf, PAGE_SIZE);
    325  1.16  jmcneill 
    326  1.13  jmcneill 	banner();
    327  1.15  jmcneill 
    328  1.15  jmcneill 	memset(&lwp0pcb, 0, sizeof(lwp0pcb));
    329  1.15  jmcneill 	if (thunk_getcontext(&lwp0pcb.pcb_ucp))
    330  1.15  jmcneill 		panic("getcontext failed");
    331  1.15  jmcneill 	uvm_lwp_setuarea(&lwp0, (vaddr_t)&lwp0pcb);
    332   1.1  jmcneill }
    333   1.1  jmcneill 
    334   1.1  jmcneill void
    335   1.1  jmcneill cpu_rootconf(void)
    336   1.1  jmcneill {
    337   1.2     joerg 	device_t rdev;
    338   1.1  jmcneill 
    339  1.12  jmcneill 	rdev = device_find_by_xname("ld0");
    340  1.12  jmcneill 	if (rdev == NULL)
    341  1.12  jmcneill 		rdev = device_find_by_xname("md0");
    342   1.1  jmcneill 
    343  1.12  jmcneill 	aprint_normal("boot device: %s\n",
    344  1.12  jmcneill 	    rdev ? device_xname(rdev) : "<unknown>");
    345   1.1  jmcneill 	setroot(rdev, 0);
    346   1.1  jmcneill }
    347   1.1  jmcneill 
    348   1.1  jmcneill bool
    349   1.1  jmcneill cpu_intr_p(void)
    350   1.1  jmcneill {
    351  1.13  jmcneill 	int idepth;
    352  1.13  jmcneill 
    353  1.13  jmcneill 	kpreempt_disable();
    354  1.13  jmcneill 	idepth = curcpu()->ci_idepth;
    355  1.13  jmcneill 	kpreempt_enable();
    356  1.13  jmcneill 
    357  1.13  jmcneill 	return (idepth >= 0);
    358   1.1  jmcneill }
    359