Home | History | Annotate | Line # | Download | only in dev
cpu.c revision 1.47.2.1
      1  1.47.2.1       mrg /* $NetBSD: cpu.c,v 1.47.2.1 2012/02/18 07:33:23 mrg 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.37  jmcneill #include "opt_hz.h"
     31      1.19  jmcneill 
     32       1.1  jmcneill #include <sys/cdefs.h>
     33  1.47.2.1       mrg __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.47.2.1 2012/02/18 07:33:23 mrg Exp $");
     34       1.1  jmcneill 
     35       1.1  jmcneill #include <sys/param.h>
     36       1.1  jmcneill #include <sys/conf.h>
     37       1.1  jmcneill #include <sys/proc.h>
     38       1.1  jmcneill #include <sys/systm.h>
     39       1.1  jmcneill #include <sys/device.h>
     40       1.1  jmcneill #include <sys/reboot.h>
     41       1.1  jmcneill #include <sys/lwp.h>
     42       1.1  jmcneill #include <sys/cpu.h>
     43       1.1  jmcneill #include <sys/mbuf.h>
     44      1.16  jmcneill #include <sys/msgbuf.h>
     45  1.47.2.1       mrg #include <sys/kmem.h>
     46  1.47.2.1       mrg #include <sys/kernel.h>
     47  1.47.2.1       mrg #include <sys/mount.h>
     48       1.1  jmcneill 
     49       1.1  jmcneill #include <dev/cons.h>
     50       1.1  jmcneill 
     51       1.1  jmcneill #include <machine/cpu.h>
     52       1.1  jmcneill #include <machine/mainbus.h>
     53       1.8  jmcneill #include <machine/pcb.h>
     54      1.40   reinoud #include <machine/machdep.h>
     55      1.10  jmcneill #include <machine/thunk.h>
     56       1.1  jmcneill 
     57       1.1  jmcneill #include <uvm/uvm_extern.h>
     58       1.1  jmcneill #include <uvm/uvm_page.h>
     59       1.1  jmcneill 
     60      1.27  jmcneill #if __GNUC_PREREQ__(4,4)
     61      1.27  jmcneill #define cpu_unreachable()	__builtin_unreachable()
     62      1.27  jmcneill #else
     63      1.27  jmcneill #define cpu_unreachable()	do { thunk_abort(); } while (0)
     64      1.27  jmcneill #endif
     65      1.27  jmcneill 
     66       1.1  jmcneill static int	cpu_match(device_t, cfdata_t, void *);
     67       1.1  jmcneill static void	cpu_attach(device_t, device_t, void *);
     68       1.1  jmcneill 
     69      1.13  jmcneill struct cpu_info cpu_info_primary = {
     70      1.13  jmcneill 	.ci_dev = 0,
     71      1.13  jmcneill 	.ci_self = &cpu_info_primary,
     72      1.13  jmcneill 	.ci_idepth = -1,
     73      1.13  jmcneill 	.ci_curlwp = &lwp0,
     74      1.13  jmcneill };
     75      1.13  jmcneill 
     76       1.1  jmcneill char cpu_model[48] = "virtual processor";
     77       1.1  jmcneill 
     78       1.1  jmcneill typedef struct cpu_softc {
     79       1.1  jmcneill 	device_t	sc_dev;
     80       1.1  jmcneill 	struct cpu_info	*sc_ci;
     81  1.47.2.1       mrg 
     82  1.47.2.1       mrg 	ucontext_t	sc_ucp;
     83  1.47.2.1       mrg 	uint8_t		sc_ucp_stack[PAGE_SIZE];
     84       1.1  jmcneill } cpu_softc_t;
     85       1.1  jmcneill 
     86  1.47.2.1       mrg 
     87  1.47.2.1       mrg /* statics */
     88      1.15  jmcneill static struct pcb lwp0pcb;
     89  1.47.2.1       mrg static void *um_msgbuf;
     90  1.47.2.1       mrg 
     91       1.9  jmcneill 
     92  1.47.2.1       mrg /* attachment */
     93       1.1  jmcneill CFATTACH_DECL_NEW(cpu, sizeof(cpu_softc_t), cpu_match, cpu_attach, NULL, NULL);
     94       1.1  jmcneill 
     95       1.1  jmcneill static int
     96       1.1  jmcneill cpu_match(device_t parent, cfdata_t match, void *opaque)
     97       1.1  jmcneill {
     98       1.1  jmcneill 	struct thunkbus_attach_args *taa = opaque;
     99       1.1  jmcneill 
    100       1.1  jmcneill 	if (taa->taa_type != THUNKBUS_TYPE_CPU)
    101       1.1  jmcneill 		return 0;
    102       1.1  jmcneill 
    103       1.1  jmcneill 	return 1;
    104       1.1  jmcneill }
    105       1.1  jmcneill 
    106       1.1  jmcneill static void
    107       1.1  jmcneill cpu_attach(device_t parent, device_t self, void *opaque)
    108       1.1  jmcneill {
    109       1.1  jmcneill 	cpu_softc_t *sc = device_private(self);
    110       1.1  jmcneill 
    111       1.1  jmcneill 	aprint_naive("\n");
    112       1.1  jmcneill 	aprint_normal("\n");
    113       1.1  jmcneill 
    114  1.47.2.1       mrg 	cpu_info_primary.ci_dev = self;
    115       1.1  jmcneill 	sc->sc_dev = self;
    116       1.1  jmcneill 	sc->sc_ci = &cpu_info_primary;
    117  1.47.2.1       mrg 
    118  1.47.2.1       mrg 	thunk_getcontext(&sc->sc_ucp);
    119  1.47.2.1       mrg 	sc->sc_ucp.uc_stack.ss_sp = sc->sc_ucp_stack;
    120  1.47.2.1       mrg 	sc->sc_ucp.uc_stack.ss_size = PAGE_SIZE - sizeof(register_t);
    121  1.47.2.1       mrg 	sc->sc_ucp.uc_flags = _UC_STACK | _UC_CPU | _UC_SIGMASK;
    122  1.47.2.1       mrg 	thunk_sigaddset(&sc->sc_ucp.uc_sigmask, SIGALRM);
    123  1.47.2.1       mrg 	thunk_sigaddset(&sc->sc_ucp.uc_sigmask, SIGIO);
    124       1.1  jmcneill }
    125       1.1  jmcneill 
    126       1.1  jmcneill void
    127       1.1  jmcneill cpu_configure(void)
    128       1.1  jmcneill {
    129       1.1  jmcneill 	if (config_rootfound("mainbus", NULL) == NULL)
    130       1.1  jmcneill 		panic("configure: mainbus not configured");
    131       1.1  jmcneill 
    132       1.1  jmcneill 	spl0();
    133       1.1  jmcneill }
    134       1.1  jmcneill 
    135  1.47.2.1       mrg 
    136  1.47.2.1       mrg /* main guts */
    137       1.1  jmcneill void
    138       1.1  jmcneill cpu_reboot(int howto, char *bootstr)
    139       1.1  jmcneill {
    140      1.11  jmcneill 	extern void usermode_reboot(void);
    141      1.11  jmcneill 
    142  1.47.2.1       mrg 	if (cold)
    143  1.47.2.1       mrg 		howto |= RB_HALT;
    144  1.47.2.1       mrg 
    145  1.47.2.1       mrg 	if ((howto & RB_NOSYNC) == 0)
    146  1.47.2.1       mrg 		vfs_shutdown();
    147  1.47.2.1       mrg 	else
    148  1.47.2.1       mrg 		suspendsched();
    149  1.47.2.1       mrg 
    150  1.47.2.1       mrg 	doshutdownhooks();
    151  1.47.2.1       mrg 	pmf_system_shutdown(boothowto);
    152       1.1  jmcneill 
    153       1.1  jmcneill 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
    154      1.10  jmcneill 		thunk_exit(0);
    155       1.1  jmcneill 
    156  1.47.2.1       mrg 	splhigh();
    157  1.47.2.1       mrg 
    158      1.22  jmcneill 	if (howto & RB_DUMP)
    159      1.22  jmcneill 		thunk_abort();
    160      1.22  jmcneill 
    161       1.1  jmcneill 	if (howto & RB_HALT) {
    162       1.1  jmcneill 		printf("\n");
    163       1.1  jmcneill 		printf("The operating system has halted.\n");
    164       1.1  jmcneill 		printf("Please press any key to reboot.\n\n");
    165       1.1  jmcneill 		cnpollc(1);
    166       1.1  jmcneill 		cngetc();
    167       1.1  jmcneill 		cnpollc(0);
    168       1.1  jmcneill 	}
    169       1.1  jmcneill 
    170       1.1  jmcneill 	printf("rebooting...\n");
    171       1.1  jmcneill 
    172      1.11  jmcneill 	usermode_reboot();
    173       1.1  jmcneill 
    174       1.1  jmcneill 	/* NOTREACHED */
    175      1.27  jmcneill 	cpu_unreachable();
    176       1.1  jmcneill }
    177       1.1  jmcneill 
    178       1.1  jmcneill void
    179       1.1  jmcneill cpu_need_resched(struct cpu_info *ci, int flags)
    180       1.1  jmcneill {
    181       1.9  jmcneill 	ci->ci_want_resched |= flags;
    182  1.47.2.1       mrg 	aston(ci);
    183       1.1  jmcneill }
    184       1.1  jmcneill 
    185       1.1  jmcneill void
    186       1.1  jmcneill cpu_need_proftick(struct lwp *l)
    187       1.1  jmcneill {
    188       1.1  jmcneill }
    189       1.1  jmcneill 
    190  1.47.2.1       mrg static
    191  1.47.2.1       mrg void
    192  1.47.2.1       mrg cpu_switchto_atomic(lwp_t *oldlwp, lwp_t *newlwp)
    193  1.47.2.1       mrg {
    194  1.47.2.1       mrg 	struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
    195  1.47.2.1       mrg 	struct pcb *newpcb = lwp_getpcb(newlwp);
    196  1.47.2.1       mrg 	struct cpu_info *ci = curcpu();
    197  1.47.2.1       mrg 
    198  1.47.2.1       mrg 	ci->ci_stash = oldlwp;
    199  1.47.2.1       mrg 
    200  1.47.2.1       mrg 	if (oldpcb)
    201  1.47.2.1       mrg 		oldpcb->pcb_errno = thunk_geterrno();
    202  1.47.2.1       mrg 
    203  1.47.2.1       mrg 	thunk_seterrno(newpcb->pcb_errno);
    204  1.47.2.1       mrg 
    205  1.47.2.1       mrg 	curlwp = newlwp;
    206  1.47.2.1       mrg 	if (thunk_setcontext(&newpcb->pcb_ucp))
    207  1.47.2.1       mrg 		panic("setcontext failed");
    208  1.47.2.1       mrg 	/* not reached */
    209  1.47.2.1       mrg }
    210  1.47.2.1       mrg 
    211       1.1  jmcneill lwp_t *
    212       1.1  jmcneill cpu_switchto(lwp_t *oldlwp, lwp_t *newlwp, bool returning)
    213       1.1  jmcneill {
    214       1.5     rmind 	struct pcb *oldpcb = oldlwp ? lwp_getpcb(oldlwp) : NULL;
    215       1.5     rmind 	struct pcb *newpcb = lwp_getpcb(newlwp);
    216       1.1  jmcneill 	struct cpu_info *ci = curcpu();
    217  1.47.2.1       mrg 	cpu_softc_t *sc = device_private(ci->ci_dev);
    218       1.1  jmcneill 
    219       1.1  jmcneill #ifdef CPU_DEBUG
    220  1.47.2.1       mrg 	thunk_printf_debug("cpu_switchto [%s,pid=%d,lid=%d] -> [%s,pid=%d,lid=%d]\n",
    221       1.1  jmcneill 	    oldlwp ? oldlwp->l_name : "none",
    222      1.17  jmcneill 	    oldlwp ? oldlwp->l_proc->p_pid : -1,
    223      1.17  jmcneill 	    oldlwp ? oldlwp->l_lid : -1,
    224      1.17  jmcneill 	    newlwp ? newlwp->l_name : "none",
    225      1.17  jmcneill 	    newlwp ? newlwp->l_proc->p_pid : -1,
    226      1.17  jmcneill 	    newlwp ? newlwp->l_lid : -1);
    227       1.1  jmcneill 	if (oldpcb) {
    228  1.47.2.1       mrg 		thunk_printf_debug("    oldpcb uc_link=%p, uc_stack.ss_sp=%p, "
    229       1.1  jmcneill 		    "uc_stack.ss_size=%d\n",
    230       1.1  jmcneill 		    oldpcb->pcb_ucp.uc_link,
    231       1.1  jmcneill 		    oldpcb->pcb_ucp.uc_stack.ss_sp,
    232       1.1  jmcneill 		    (int)oldpcb->pcb_ucp.uc_stack.ss_size);
    233       1.1  jmcneill 	}
    234       1.1  jmcneill 	if (newpcb) {
    235  1.47.2.1       mrg 		thunk_printf_debug("    newpcb uc_link=%p, uc_stack.ss_sp=%p, "
    236       1.1  jmcneill 		    "uc_stack.ss_size=%d\n",
    237       1.1  jmcneill 		    newpcb->pcb_ucp.uc_link,
    238       1.1  jmcneill 		    newpcb->pcb_ucp.uc_stack.ss_sp,
    239       1.1  jmcneill 		    (int)newpcb->pcb_ucp.uc_stack.ss_size);
    240       1.1  jmcneill 	}
    241       1.1  jmcneill #endif /* !CPU_DEBUG */
    242       1.1  jmcneill 
    243  1.47.2.1       mrg 	/* create atomic switcher */
    244  1.47.2.1       mrg 	thunk_makecontext(&sc->sc_ucp, (void (*)(void)) cpu_switchto_atomic,
    245  1.47.2.1       mrg 			2, oldlwp, newlwp, NULL);
    246      1.28   reinoud 
    247  1.47.2.1       mrg 	KASSERT(sc);
    248       1.1  jmcneill 	if (oldpcb) {
    249  1.47.2.1       mrg 		thunk_swapcontext(&oldpcb->pcb_ucp, &sc->sc_ucp);
    250  1.47.2.1       mrg 		/* returns here */
    251       1.1  jmcneill 	} else {
    252  1.47.2.1       mrg 		thunk_setcontext(&sc->sc_ucp);
    253  1.47.2.1       mrg 		/* never returns */
    254       1.1  jmcneill 	}
    255      1.30   reinoud 
    256       1.1  jmcneill #ifdef CPU_DEBUG
    257  1.47.2.1       mrg 	thunk_printf_debug("cpu_switchto: returning %p (was %p)\n", ci->ci_stash, oldlwp);
    258       1.1  jmcneill #endif
    259       1.1  jmcneill 	return ci->ci_stash;
    260       1.1  jmcneill }
    261       1.1  jmcneill 
    262       1.1  jmcneill void
    263       1.1  jmcneill cpu_dumpconf(void)
    264       1.1  jmcneill {
    265       1.1  jmcneill #ifdef CPU_DEBUG
    266  1.47.2.1       mrg 	thunk_printf_debug("cpu_dumpconf\n");
    267       1.1  jmcneill #endif
    268       1.1  jmcneill }
    269       1.1  jmcneill 
    270       1.1  jmcneill void
    271       1.1  jmcneill cpu_signotify(struct lwp *l)
    272       1.1  jmcneill {
    273       1.1  jmcneill }
    274       1.1  jmcneill 
    275       1.1  jmcneill void
    276       1.1  jmcneill cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    277       1.1  jmcneill {
    278  1.47.2.1       mrg 	struct pcb *pcb = lwp_getpcb(l);
    279  1.47.2.1       mrg 	ucontext_t *ucp = &pcb->pcb_userret_ucp;
    280  1.47.2.1       mrg 
    281       1.1  jmcneill #ifdef CPU_DEBUG
    282  1.47.2.1       mrg 	thunk_printf_debug("cpu_getmcontext\n");
    283       1.1  jmcneill #endif
    284  1.47.2.1       mrg 	memcpy(mcp, &ucp->uc_mcontext, sizeof(mcontext_t));
    285  1.47.2.1       mrg 	return;
    286       1.1  jmcneill }
    287       1.1  jmcneill 
    288       1.1  jmcneill int
    289       1.1  jmcneill cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    290       1.1  jmcneill {
    291  1.47.2.1       mrg 	struct pcb *pcb = lwp_getpcb(l);
    292  1.47.2.1       mrg 	ucontext_t *ucp = &pcb->pcb_userret_ucp;
    293  1.47.2.1       mrg 
    294       1.1  jmcneill #ifdef CPU_DEBUG
    295  1.47.2.1       mrg 	thunk_printf_debug("cpu_setmcontext\n");
    296       1.1  jmcneill #endif
    297  1.47.2.1       mrg 	memcpy(&ucp->uc_mcontext, mcp, sizeof(mcontext_t));
    298       1.1  jmcneill 	return 0;
    299       1.1  jmcneill }
    300       1.1  jmcneill 
    301       1.1  jmcneill void
    302       1.1  jmcneill cpu_idle(void)
    303       1.1  jmcneill {
    304       1.1  jmcneill 	struct cpu_info *ci = curcpu();
    305       1.1  jmcneill 
    306       1.1  jmcneill 	if (ci->ci_want_resched)
    307       1.1  jmcneill 		return;
    308       1.1  jmcneill 
    309  1.47.2.1       mrg 	thunk_idle();
    310       1.1  jmcneill }
    311       1.1  jmcneill 
    312       1.1  jmcneill void
    313       1.1  jmcneill cpu_lwp_free(struct lwp *l, int proc)
    314       1.1  jmcneill {
    315       1.1  jmcneill #ifdef CPU_DEBUG
    316  1.47.2.1       mrg 	thunk_printf_debug("cpu_lwp_free (dummy)\n");
    317       1.1  jmcneill #endif
    318       1.1  jmcneill }
    319       1.1  jmcneill 
    320       1.1  jmcneill void
    321       1.1  jmcneill cpu_lwp_free2(struct lwp *l)
    322       1.1  jmcneill {
    323       1.5     rmind 	struct pcb *pcb = lwp_getpcb(l);
    324       1.1  jmcneill 
    325       1.1  jmcneill #ifdef CPU_DEBUG
    326  1.47.2.1       mrg 	thunk_printf_debug("cpu_lwp_free2\n");
    327       1.1  jmcneill #endif
    328       1.1  jmcneill 
    329       1.1  jmcneill 	if (pcb == NULL)
    330       1.1  jmcneill 		return;
    331  1.47.2.1       mrg 	/* XXX nothing to do? */
    332       1.1  jmcneill }
    333       1.1  jmcneill 
    334       1.1  jmcneill static void
    335      1.44   reinoud cpu_lwp_trampoline(ucontext_t *ucp, void (*func)(void *), void *arg)
    336       1.1  jmcneill {
    337      1.18   reinoud #ifdef CPU_DEBUG
    338  1.47.2.1       mrg 	thunk_printf_debug("cpu_lwp_trampoline called with func %p, arg %p\n", (void *) func, arg);
    339      1.18   reinoud #endif
    340      1.44   reinoud 	/* init lwp */
    341       1.1  jmcneill 	lwp_startup(curcpu()->ci_stash, curlwp);
    342      1.44   reinoud 
    343      1.45   reinoud 	/* actual jump */
    344      1.44   reinoud 	thunk_makecontext(ucp, (void (*)(void)) func, 1, arg, NULL, NULL);
    345      1.45   reinoud 	thunk_setcontext(ucp);
    346       1.1  jmcneill }
    347       1.1  jmcneill 
    348       1.1  jmcneill void
    349       1.1  jmcneill cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
    350       1.1  jmcneill     void (*func)(void *), void *arg)
    351       1.1  jmcneill {
    352      1.23   reinoud 	struct pcb *pcb1 = lwp_getpcb(l1);
    353      1.23   reinoud 	struct pcb *pcb2 = lwp_getpcb(l2);
    354       1.1  jmcneill 
    355       1.1  jmcneill #ifdef CPU_DEBUG
    356  1.47.2.1       mrg 	thunk_printf_debug("cpu_lwp_fork [%s/%p] -> [%s/%p] stack=%p stacksize=%d\n",
    357       1.1  jmcneill 	    l1 ? l1->l_name : "none", l1,
    358       1.1  jmcneill 	    l2 ? l2->l_name : "none", l2,
    359       1.1  jmcneill 	    stack, (int)stacksize);
    360       1.1  jmcneill #endif
    361       1.1  jmcneill 
    362      1.45   reinoud 	if (stack)
    363      1.45   reinoud 		panic("%s: stack passed, can't handle\n", __func__);
    364      1.45   reinoud 
    365      1.23   reinoud 	/* copy the PCB and its switchframes from parent */
    366      1.23   reinoud 	memcpy(pcb2, pcb1, sizeof(struct pcb));
    367      1.23   reinoud 
    368  1.47.2.1       mrg 	/* refresh context */
    369      1.23   reinoud 	if (thunk_getcontext(&pcb2->pcb_ucp))
    370      1.26  jmcneill 		panic("getcontext failed");
    371      1.23   reinoud 
    372  1.47.2.1       mrg 	/* recalculate the system stack top */
    373  1.47.2.1       mrg 	pcb2->sys_stack_top = pcb2->sys_stack + TRAPSTACKSIZE;
    374  1.47.2.1       mrg 
    375  1.47.2.1       mrg 	/* get l2 its own stack */
    376  1.47.2.1       mrg 	pcb2->pcb_ucp.uc_stack.ss_sp = pcb2->sys_stack;
    377  1.47.2.1       mrg 	pcb2->pcb_ucp.uc_stack.ss_size = pcb2->sys_stack_top - pcb2->sys_stack;
    378      1.47   reinoud 	pcb2->pcb_ucp.uc_link = &pcb2->pcb_userret_ucp;
    379  1.47.2.1       mrg 
    380  1.47.2.1       mrg 	thunk_sigemptyset(&pcb2->pcb_ucp.uc_sigmask);
    381  1.47.2.1       mrg 	pcb2->pcb_ucp.uc_flags = _UC_STACK | _UC_CPU | _UC_SIGMASK;
    382      1.45   reinoud 	thunk_makecontext(&pcb2->pcb_ucp,
    383      1.45   reinoud 	    (void (*)(void)) cpu_lwp_trampoline,
    384      1.44   reinoud 	    3, &pcb2->pcb_ucp, func, arg);
    385       1.1  jmcneill }
    386       1.1  jmcneill 
    387       1.1  jmcneill void
    388       1.1  jmcneill cpu_initclocks(void)
    389       1.1  jmcneill {
    390  1.47.2.1       mrg 	extern timer_t clock_timerid;
    391      1.37  jmcneill 
    392  1.47.2.1       mrg 	thunk_timer_start(clock_timerid, HZ);
    393       1.1  jmcneill }
    394       1.1  jmcneill 
    395       1.1  jmcneill void
    396       1.1  jmcneill cpu_startup(void)
    397       1.1  jmcneill {
    398  1.47.2.1       mrg 	vaddr_t minaddr, maxaddr;
    399  1.47.2.1       mrg 	size_t msgbufsize = 32 * 1024;
    400      1.47   reinoud 
    401  1.47.2.1       mrg 	/* get ourself a message buffer */
    402  1.47.2.1       mrg 	um_msgbuf = kmem_zalloc(msgbufsize, KM_SLEEP);
    403  1.47.2.1       mrg 	if (um_msgbuf == NULL)
    404      1.16  jmcneill 		panic("couldn't allocate msgbuf");
    405  1.47.2.1       mrg 	initmsgbuf(um_msgbuf, msgbufsize);
    406      1.16  jmcneill 
    407  1.47.2.1       mrg 	/* allocate a submap for physio, 1Mb enough? */
    408  1.47.2.1       mrg 	minaddr = 0;
    409  1.47.2.1       mrg 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    410  1.47.2.1       mrg 				   1024 * 1024, 0, false, NULL);
    411  1.47.2.1       mrg 
    412  1.47.2.1       mrg 	/* say hi! */
    413      1.13  jmcneill 	banner();
    414      1.15  jmcneill 
    415  1.47.2.1       mrg 	/* init lwp0 */
    416      1.15  jmcneill 	memset(&lwp0pcb, 0, sizeof(lwp0pcb));
    417  1.47.2.1       mrg 	thunk_getcontext(&lwp0pcb.pcb_ucp);
    418  1.47.2.1       mrg 	thunk_sigemptyset(&lwp0pcb.pcb_ucp.uc_sigmask);
    419  1.47.2.1       mrg 	lwp0pcb.pcb_ucp.uc_flags = _UC_STACK | _UC_CPU | _UC_SIGMASK;
    420  1.47.2.1       mrg 
    421  1.47.2.1       mrg 	uvm_lwp_setuarea(&lwp0, (vaddr_t) &lwp0pcb);
    422  1.47.2.1       mrg 	memcpy(&lwp0pcb.pcb_userret_ucp, &lwp0pcb.pcb_ucp, sizeof(ucontext_t));
    423      1.23   reinoud 
    424  1.47.2.1       mrg 	/* set stack top */
    425  1.47.2.1       mrg 	lwp0pcb.sys_stack_top = lwp0pcb.sys_stack + TRAPSTACKSIZE;
    426       1.1  jmcneill }
    427       1.1  jmcneill 
    428       1.1  jmcneill void
    429       1.1  jmcneill cpu_rootconf(void)
    430       1.1  jmcneill {
    431  1.47.2.1       mrg 	extern char *usermode_root_device;
    432       1.2     joerg 	device_t rdev;
    433       1.1  jmcneill 
    434  1.47.2.1       mrg 	if (usermode_root_device != NULL) {
    435  1.47.2.1       mrg 		rdev = device_find_by_xname(usermode_root_device);
    436  1.47.2.1       mrg 	} else {
    437  1.47.2.1       mrg 		rdev = device_find_by_xname("ld0");
    438  1.47.2.1       mrg 		if (rdev == NULL)
    439  1.47.2.1       mrg 			rdev = device_find_by_xname("md0");
    440  1.47.2.1       mrg 	}
    441       1.1  jmcneill 
    442      1.12  jmcneill 	aprint_normal("boot device: %s\n",
    443      1.12  jmcneill 	    rdev ? device_xname(rdev) : "<unknown>");
    444       1.1  jmcneill 	setroot(rdev, 0);
    445       1.1  jmcneill }
    446       1.1  jmcneill 
    447       1.1  jmcneill bool
    448       1.1  jmcneill cpu_intr_p(void)
    449       1.1  jmcneill {
    450      1.13  jmcneill 	int idepth;
    451      1.13  jmcneill 
    452      1.13  jmcneill 	kpreempt_disable();
    453      1.13  jmcneill 	idepth = curcpu()->ci_idepth;
    454      1.13  jmcneill 	kpreempt_enable();
    455      1.13  jmcneill 
    456      1.13  jmcneill 	return (idepth >= 0);
    457       1.1  jmcneill }
    458