xref: /illumos-gate/usr/src/uts/sun4/os/mlsetup.c (revision 12551037)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c56c1e58Sgirish  * Common Development and Distribution License (the "License").
6c56c1e58Sgirish  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220e751525SEric Saxe  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/systm.h>
287c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
297c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
307c478bd9Sstevel@tonic-gate #include <sys/disp.h>
317c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
327c478bd9Sstevel@tonic-gate #include <sys/promif.h>
337c478bd9Sstevel@tonic-gate #include <sys/prom_plat.h>
34986fd29aSsetje #include <sys/promimpl.h>
35986fd29aSsetje #include <sys/platform_module.h>
367c478bd9Sstevel@tonic-gate #include <sys/clock.h>
377c478bd9Sstevel@tonic-gate #include <sys/pte.h>
387c478bd9Sstevel@tonic-gate #include <sys/scb.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
407c478bd9Sstevel@tonic-gate #include <sys/stack.h>
417c478bd9Sstevel@tonic-gate #include <sys/intreg.h>
427c478bd9Sstevel@tonic-gate #include <sys/ivintr.h>
437c478bd9Sstevel@tonic-gate #include <vm/as.h>
447c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
457c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
477c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
487c478bd9Sstevel@tonic-gate #include <sys/trap.h>
497c478bd9Sstevel@tonic-gate #include <sys/machtrap.h>
507c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
517c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
527c478bd9Sstevel@tonic-gate #include <sys/proc.h>
537c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
547c478bd9Sstevel@tonic-gate #include <sys/pset.h>
557c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
567c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
577c478bd9Sstevel@tonic-gate #include <sys/panic.h>
587c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>	/* for bootops */
59fb2f18f8Sesaxe #include <sys/pg.h>
607c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
617c478bd9Sstevel@tonic-gate #include <sys/fpras.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <sys/prom_debug.h>
647c478bd9Sstevel@tonic-gate #include <sys/debug.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
677c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
687c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
69986fd29aSsetje 
70986fd29aSsetje #include <sys/kobj_impl.h>
71986fd29aSsetje #include <sys/kdi_machimpl.h>
72986fd29aSsetje 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * External Routines:
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate extern void map_wellknown_devices(void);
77c56c1e58Sgirish extern void hsvc_setup(void);
781ae08745Sheppo extern void mach_descrip_startup_init(void);
793b890a5bSjb extern void mach_soft_state_init(void);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int	dcache_size;
827c478bd9Sstevel@tonic-gate int	dcache_linesize;
837c478bd9Sstevel@tonic-gate int	icache_size;
847c478bd9Sstevel@tonic-gate int	icache_linesize;
857c478bd9Sstevel@tonic-gate int	ecache_size;
867c478bd9Sstevel@tonic-gate int	ecache_alignsize;
877c478bd9Sstevel@tonic-gate int	ecache_associativity;
887c478bd9Sstevel@tonic-gate int	ecache_setsize;			/* max possible e$ setsize */
897c478bd9Sstevel@tonic-gate int	cpu_setsize;			/* max e$ setsize of configured cpus */
907c478bd9Sstevel@tonic-gate int	dcache_line_mask;		/* spitfire only */
917c478bd9Sstevel@tonic-gate int	vac_size;			/* cache size in bytes */
927c478bd9Sstevel@tonic-gate uint_t	vac_mask;			/* VAC alignment consistency mask */
937c478bd9Sstevel@tonic-gate int	vac_shift;			/* log2(vac_size) for ppmapout() */
947c478bd9Sstevel@tonic-gate int	vac = 0;	/* virtual address cache type (none == 0) */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * fpRAS.  An individual sun4* machine class (or perhaps subclass,
987c478bd9Sstevel@tonic-gate  * eg sun4u/cheetah) must set fpras_implemented to indicate that it implements
997c478bd9Sstevel@tonic-gate  * the fpRAS feature.  The feature can be suppressed by setting fpras_disable
1007c478bd9Sstevel@tonic-gate  * or the mechanism can be disabled for individual copy operations with
1017c478bd9Sstevel@tonic-gate  * fpras_disableids.  All these are checked in post_startup() code so
1027c478bd9Sstevel@tonic-gate  * fpras_disable and fpras_disableids can be set in /etc/system.
1037c478bd9Sstevel@tonic-gate  * If/when fpRAS is implemented on non-sun4 architectures these
1047c478bd9Sstevel@tonic-gate  * definitions will need to move up to the common level.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate int	fpras_implemented;
1077c478bd9Sstevel@tonic-gate int	fpras_disable;
1087c478bd9Sstevel@tonic-gate int	fpras_disableids;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Static Routines:
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate static void kern_splr_preprom(void);
1147c478bd9Sstevel@tonic-gate static void kern_splx_postprom(void);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Setup routine called right before main(). Interposing this function
1187c478bd9Sstevel@tonic-gate  * before main() allows us to call it in a machine-independent fashion.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate void
mlsetup(struct regs * rp,kfpu_t * fp)122986fd29aSsetje mlsetup(struct regs *rp, kfpu_t *fp)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	extern char t0stack[];
1277c478bd9Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
1287c478bd9Sstevel@tonic-gate 	extern disp_t cpu0_disp;
1297c478bd9Sstevel@tonic-gate 	unsigned long long pa;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
1327c478bd9Sstevel@tonic-gate 	TRAP_TRACE_CTL *ctlp;
1337c478bd9Sstevel@tonic-gate #endif /* TRAPTRACE */
1347c478bd9Sstevel@tonic-gate 
135986fd29aSsetje 	/* drop into kmdb on boot -d */
136986fd29aSsetje 	if (boothowto & RB_DEBUGENTER)
137986fd29aSsetje 		kmdb_enter();
138986fd29aSsetje 
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * initialize cpu_self
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	cpu0.cpu_self = &cpu0;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * initialize t0
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	t0.t_stk = (caddr_t)rp - REGOFF;
1487c478bd9Sstevel@tonic-gate 	/* Can't use va_to_pa here - wait until prom_ initialized */
1497c478bd9Sstevel@tonic-gate 	t0.t_stkbase = t0stack;
1507c478bd9Sstevel@tonic-gate 	t0.t_pri = maxclsyspri - 3;
1517c478bd9Sstevel@tonic-gate 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
1527c478bd9Sstevel@tonic-gate 	t0.t_procp = &p0;
1537c478bd9Sstevel@tonic-gate 	t0.t_plockp = &p0lock.pl_lock;
1547c478bd9Sstevel@tonic-gate 	t0.t_lwp = &lwp0;
1557c478bd9Sstevel@tonic-gate 	t0.t_forw = &t0;
1567c478bd9Sstevel@tonic-gate 	t0.t_back = &t0;
1577c478bd9Sstevel@tonic-gate 	t0.t_next = &t0;
1587c478bd9Sstevel@tonic-gate 	t0.t_prev = &t0;
1597c478bd9Sstevel@tonic-gate 	t0.t_cpu = &cpu0;			/* loaded by _start */
1607c478bd9Sstevel@tonic-gate 	t0.t_disp_queue = &cpu0_disp;
1617c478bd9Sstevel@tonic-gate 	t0.t_bind_cpu = PBIND_NONE;
1627c478bd9Sstevel@tonic-gate 	t0.t_bind_pset = PS_NONE;
1630b70c467Sakolb 	t0.t_bindflag = (uchar_t)default_binding_mode;
1647c478bd9Sstevel@tonic-gate 	t0.t_cpupart = &cp_default;
1657c478bd9Sstevel@tonic-gate 	t0.t_clfuncs = &sys_classfuncs.thread;
1667c478bd9Sstevel@tonic-gate 	t0.t_copyops = NULL;
1677c478bd9Sstevel@tonic-gate 	THREAD_ONPROC(&t0, CPU);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	lwp0.lwp_thread = &t0;
1707c478bd9Sstevel@tonic-gate 	lwp0.lwp_procp = &p0;
1717c478bd9Sstevel@tonic-gate 	lwp0.lwp_regs = (void *)rp;
1727c478bd9Sstevel@tonic-gate 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(&lwp0);
1757c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu = fp;
1767c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu->fpu_q = mpcb->mpcb_fpu_q;
1777c478bd9Sstevel@tonic-gate 	mpcb->mpcb_thread = &t0;
1787c478bd9Sstevel@tonic-gate 	lwp0.lwp_fpu = (void *)mpcb->mpcb_fpu;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	p0.p_exec = NULL;
1817c478bd9Sstevel@tonic-gate 	p0.p_stat = SRUN;
1827c478bd9Sstevel@tonic-gate 	p0.p_flag = SSYS;
1837c478bd9Sstevel@tonic-gate 	p0.p_tlist = &t0;
1847c478bd9Sstevel@tonic-gate 	p0.p_stksize = 2*PAGESIZE;
1857c478bd9Sstevel@tonic-gate 	p0.p_stkpageszc = 0;
1867c478bd9Sstevel@tonic-gate 	p0.p_as = &kas;
1877c478bd9Sstevel@tonic-gate 	p0.p_lockp = &p0lock;
1887c478bd9Sstevel@tonic-gate 	p0.p_utraps = NULL;
1897c478bd9Sstevel@tonic-gate 	p0.p_brkpageszc = 0;
1902cb27123Saguzovsk 	p0.p_t1_lgrpid = LGRP_NONE;
1912cb27123Saguzovsk 	p0.p_tr_lgrpid = LGRP_NONE;
192d2a70789SRichard Lowe 	psecflags_default(&p0.p_secflags);
1937c478bd9Sstevel@tonic-gate 	sigorset(&p0.p_ignore, &ignoredefault);
1947c478bd9Sstevel@tonic-gate 
195d2a70789SRichard Lowe 
1967c478bd9Sstevel@tonic-gate 	CPU->cpu_thread = &t0;
1977c478bd9Sstevel@tonic-gate 	CPU->cpu_dispthread = &t0;
1987c478bd9Sstevel@tonic-gate 	bzero(&cpu0_disp, sizeof (disp_t));
1997c478bd9Sstevel@tonic-gate 	CPU->cpu_disp = &cpu0_disp;
2007c478bd9Sstevel@tonic-gate 	CPU->cpu_disp->disp_cpu = CPU;
2017c478bd9Sstevel@tonic-gate 	CPU->cpu_idle_thread = &t0;
2027c478bd9Sstevel@tonic-gate 	CPU->cpu_flags = CPU_RUNNING;
2037c478bd9Sstevel@tonic-gate 	CPU->cpu_id = getprocessorid();
2047c478bd9Sstevel@tonic-gate 	CPU->cpu_dispatch_pri = t0.t_pri;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
207ae115bc7Smrj 	 * Initialize thread/cpu microstate accounting
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	init_mstate(&t0, LMS_SYSTEM);
2107c478bd9Sstevel@tonic-gate 	init_cpu_mstate(CPU, CMS_SYSTEM);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Initialize lists of available and active CPUs.
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	cpu_list_init(CPU);
2167c478bd9Sstevel@tonic-gate 
217affbd3ccSkchow 	cpu_vm_data_init(CPU);
218affbd3ccSkchow 
2190e751525SEric Saxe 	pg_cpu_bootstrap(CPU);
2200e751525SEric Saxe 
2217c478bd9Sstevel@tonic-gate 	(void) prom_set_preprom(kern_splr_preprom);
2227c478bd9Sstevel@tonic-gate 	(void) prom_set_postprom(kern_splx_postprom);
2237c478bd9Sstevel@tonic-gate 	PRM_INFO("mlsetup: now ok to call prom_printf");
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(t0.t_stk);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * Claim the physical and virtual resources used by panicbuf,
2297c478bd9Sstevel@tonic-gate 	 * then map panicbuf.  This operation removes the phys and
2307c478bd9Sstevel@tonic-gate 	 * virtual addresses from the free lists.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	if (prom_claim_virt(PANICBUFSIZE, panicbuf) != panicbuf)
2337c478bd9Sstevel@tonic-gate 		prom_panic("Can't claim panicbuf virtual address");
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (prom_retain("panicbuf", PANICBUFSIZE, MMU_PAGESIZE, &pa) != 0)
2367c478bd9Sstevel@tonic-gate 		prom_panic("Can't allocate retained panicbuf physical address");
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (prom_map_phys(-1, PANICBUFSIZE, panicbuf, pa) != 0)
2397c478bd9Sstevel@tonic-gate 		prom_panic("Can't map panicbuf");
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	PRM_DEBUG(panicbuf);
2427c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pa);
2437c478bd9Sstevel@tonic-gate 
244c56c1e58Sgirish 	/*
245c56c1e58Sgirish 	 * Negotiate hypervisor services, if any
246c56c1e58Sgirish 	 */
247c56c1e58Sgirish 	hsvc_setup();
2483b890a5bSjb 	mach_soft_state_init();
249c56c1e58Sgirish 
2507c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * initialize the trap trace buffer for the boot cpu
2537c478bd9Sstevel@tonic-gate 	 * XXX todo, dynamically allocate this buffer too
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	ctlp = &trap_trace_ctl[CPU->cpu_id];
2567c478bd9Sstevel@tonic-gate 	ctlp->d.vaddr_base = trap_tr0;
2577c478bd9Sstevel@tonic-gate 	ctlp->d.offset = ctlp->d.last_offset = 0;
2587c478bd9Sstevel@tonic-gate 	ctlp->d.limit = TRAP_TSIZE;		/* XXX dynamic someday */
2597c478bd9Sstevel@tonic-gate 	ctlp->d.paddr_base = va_to_pa(trap_tr0);
260db6d2ee3Ssvemuri #endif /* TRAPTRACE */
2611ae08745Sheppo 
2621ae08745Sheppo 	/*
2631ae08745Sheppo 	 * Initialize the Machine Description kernel framework
2641ae08745Sheppo 	 */
2651ae08745Sheppo 
2661ae08745Sheppo 	mach_descrip_startup_init();
2671ae08745Sheppo 
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * initialize HV trap trace buffer for the boot cpu
2707c478bd9Sstevel@tonic-gate 	 */
271db6d2ee3Ssvemuri 	mach_htraptrace_setup(CPU->cpu_id);
272db6d2ee3Ssvemuri 	mach_htraptrace_configure(CPU->cpu_id);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * lgroup framework initialization. This must be done prior
2767c478bd9Sstevel@tonic-gate 	 * to devices being mapped.
2777c478bd9Sstevel@tonic-gate 	 */
278d5d7cf4eSJonathan Chew 	lgrp_init(LGRP_INIT_STAGE1);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	cpu_setup();
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (boothowto & RB_HALT) {
2837c478bd9Sstevel@tonic-gate 		prom_printf("unix: kernel halted by -h flag\n");
2847c478bd9Sstevel@tonic-gate 		prom_enter_mon();
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	setcputype();
2887c478bd9Sstevel@tonic-gate 	map_wellknown_devices();
2897c478bd9Sstevel@tonic-gate 	setcpudelay();
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * These routines are called immediately before and
2947c478bd9Sstevel@tonic-gate  * immediately after calling into the firmware.  The
2957c478bd9Sstevel@tonic-gate  * firmware is significantly confused by preemption -
2967c478bd9Sstevel@tonic-gate  * particularly on MP machines - but also on UP's too.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static int saved_spl;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static void
kern_splr_preprom(void)3027c478bd9Sstevel@tonic-gate kern_splr_preprom(void)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	saved_spl = spl7();
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static void
kern_splx_postprom(void)3087c478bd9Sstevel@tonic-gate kern_splx_postprom(void)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	splx(saved_spl);
3117c478bd9Sstevel@tonic-gate }
312986fd29aSsetje 
313986fd29aSsetje 
314986fd29aSsetje /*
315986fd29aSsetje  * WARNING
316986fd29aSsetje  * The code fom here to the end of mlsetup.c runs before krtld has
317986fd29aSsetje  * knitted unix and genunix together.  It can call routines in unix,
318986fd29aSsetje  * but calls into genunix will fail spectacularly.  More specifically,
319986fd29aSsetje  * calls to prom_*, bop_* and str* will work, everything else is
320986fd29aSsetje  * caveat emptor.
321986fd29aSsetje  *
322986fd29aSsetje  * Also note that while #ifdef sun4u is generally a bad idea, they
323986fd29aSsetje  * exist here to concentrate the dangerous code into a single file.
324986fd29aSsetje  */
325986fd29aSsetje 
326986fd29aSsetje static char *
getcpulist(void)327986fd29aSsetje getcpulist(void)
328986fd29aSsetje {
329986fd29aSsetje 	pnode_t node;
330986fd29aSsetje 	/* big enough for OBP_NAME and for a reasonably sized OBP_COMPATIBLE. */
331986fd29aSsetje 	static char cpubuf[5 * OBP_MAXDRVNAME];
332986fd29aSsetje 	int nlen, clen, i;
333986fd29aSsetje #ifdef	sun4u
334986fd29aSsetje 	char dname[OBP_MAXDRVNAME];
335986fd29aSsetje #endif
336986fd29aSsetje 
337986fd29aSsetje 	node = prom_findnode_bydevtype(prom_rootnode(), OBP_CPU);
338986fd29aSsetje 	if (node != OBP_NONODE && node != OBP_BADNODE) {
339986fd29aSsetje 		if ((nlen = prom_getproplen(node, OBP_NAME)) <= 0 ||
340986fd29aSsetje 		    nlen > sizeof (cpubuf) ||
341986fd29aSsetje 		    prom_getprop(node, OBP_NAME, cpubuf) <= 0)
342986fd29aSsetje 			prom_panic("no name in cpu node");
343986fd29aSsetje 
344986fd29aSsetje 		/* nlen includes the terminating null character */
345986fd29aSsetje #ifdef	sun4v
346986fd29aSsetje 		if ((clen = prom_getproplen(node, OBP_COMPATIBLE)) > 0) {
347986fd29aSsetje #else	/* sun4u */
348986fd29aSsetje 		/*
349986fd29aSsetje 		 * For the CMT case, need check the parent "core"
350986fd29aSsetje 		 * node for the compatible property.
351986fd29aSsetje 		 */
352986fd29aSsetje 		if ((clen = prom_getproplen(node, OBP_COMPATIBLE)) > 0 ||
353986fd29aSsetje 		    ((node = prom_parentnode(node)) != OBP_NONODE &&
354986fd29aSsetje 		    node != OBP_BADNODE &&
355986fd29aSsetje 		    (clen = prom_getproplen(node, OBP_COMPATIBLE)) > 0 &&
356986fd29aSsetje 		    prom_getprop(node, OBP_DEVICETYPE, dname) > 0 &&
357986fd29aSsetje 		    strcmp(dname, "core") == 0)) {
358986fd29aSsetje #endif
359986fd29aSsetje 			if ((clen + nlen) > sizeof (cpubuf))
360986fd29aSsetje 				prom_panic("cpu node \"compatible\" too long");
361986fd29aSsetje 			/* read in compatible, leaving space for ':' */
362986fd29aSsetje 			if (prom_getprop(node, OBP_COMPATIBLE,
363986fd29aSsetje 			    &cpubuf[nlen]) != clen)
364986fd29aSsetje 				prom_panic("cpu node \"compatible\" error");
365986fd29aSsetje 			clen += nlen;	/* total length */
366986fd29aSsetje 			/* convert all null characters to ':' */
367986fd29aSsetje 			clen--;	/* except the final one... */
368986fd29aSsetje 			for (i = 0; i < clen; i++)
369986fd29aSsetje 				if (cpubuf[i] == '\0')
370986fd29aSsetje 					cpubuf[i] = ':';
371986fd29aSsetje 		}
372986fd29aSsetje #ifdef	sun4u
373986fd29aSsetje 		/*
374986fd29aSsetje 		 * Some PROMs return SUNW,UltraSPARC when they actually have
375986fd29aSsetje 		 * SUNW,UltraSPARC-II cpus. SInce we're now filtering out all
376986fd29aSsetje 		 * SUNW,UltraSPARC systems during the boot phase, we can safely
377986fd29aSsetje 		 * point the auxv CPU value at SUNW,UltraSPARC-II.
378986fd29aSsetje 		 */
379986fd29aSsetje 		if (strcmp("SUNW,UltraSPARC", cpubuf) == 0)
380986fd29aSsetje 			(void) strcpy(cpubuf, "SUNW,UltraSPARC-II");
381986fd29aSsetje #endif
382986fd29aSsetje 		return (cpubuf);
383986fd29aSsetje 	} else
384986fd29aSsetje 		return (NULL);
385986fd29aSsetje }
386986fd29aSsetje 
387986fd29aSsetje /*
388986fd29aSsetje  * called immediately from _start to stich the
389986fd29aSsetje  * primary modules together
390986fd29aSsetje  */
391986fd29aSsetje void
392986fd29aSsetje kobj_start(void *cif)
393986fd29aSsetje {
394986fd29aSsetje 	Ehdr *ehdr;
395986fd29aSsetje 	Phdr *phdr;
396986fd29aSsetje 	uint32_t eadr, padr;
397986fd29aSsetje 	val_t bootaux[BA_NUM];
398986fd29aSsetje 	int i;
399986fd29aSsetje 
400986fd29aSsetje 	prom_init("kernel", cif);
401986fd29aSsetje 	bop_init();
402986fd29aSsetje #ifdef	DEBUG
403986fd29aSsetje 	if (bop_getproplen("stop-me") != -1)
404986fd29aSsetje 		prom_enter_mon();
405986fd29aSsetje #endif
406986fd29aSsetje 
407986fd29aSsetje 	if (bop_getprop("elfheader-address", (caddr_t)&eadr) == -1)
408986fd29aSsetje 		prom_panic("no ELF image");
409986fd29aSsetje 	ehdr = (Ehdr *)(uintptr_t)eadr;
410986fd29aSsetje 	for (i = 0; i < BA_NUM; i++)
411*12551037SToomas Soome 		bootaux[i].ba_val = 0;
412986fd29aSsetje 	bootaux[BA_PHNUM].ba_val = ehdr->e_phnum;
413986fd29aSsetje 	bootaux[BA_PHENT].ba_val = ehdr->e_phentsize;
414986fd29aSsetje 	bootaux[BA_LDNAME].ba_ptr = NULL;
415986fd29aSsetje 
416986fd29aSsetje 	padr = eadr + ehdr->e_phoff;
417986fd29aSsetje 	bootaux[BA_PHDR].ba_ptr = (void *)(uintptr_t)padr;
418986fd29aSsetje 	for (i = 0; i < ehdr->e_phnum; i++) {
419986fd29aSsetje 		phdr = (Phdr *)((uintptr_t)padr + i * ehdr->e_phentsize);
420986fd29aSsetje 		if (phdr->p_type == PT_DYNAMIC) {
421986fd29aSsetje 			bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr;
422986fd29aSsetje 			break;
423986fd29aSsetje 		}
424986fd29aSsetje 	}
425986fd29aSsetje 
426986fd29aSsetje 	bootaux[BA_LPAGESZ].ba_val = MMU_PAGESIZE4M;
427986fd29aSsetje 	bootaux[BA_PAGESZ].ba_val = MMU_PAGESIZE;
428986fd29aSsetje 	bootaux[BA_IFLUSH].ba_val = 1;
429986fd29aSsetje 	bootaux[BA_CPU].ba_ptr = getcpulist();
430986fd29aSsetje 	bootaux[BA_MMU].ba_ptr = NULL;
431986fd29aSsetje 
432986fd29aSsetje 	kobj_init(cif, NULL, bootops, bootaux);
433986fd29aSsetje 
434986fd29aSsetje 	/* kernel stitched together; we can now test #pragma's */
435986fd29aSsetje 	if (&plat_setprop_enter != NULL) {
436986fd29aSsetje 		prom_setprop_enter = &plat_setprop_enter;
437986fd29aSsetje 		prom_setprop_exit = &plat_setprop_exit;
438986fd29aSsetje 		ASSERT(prom_setprop_exit != NULL);
439986fd29aSsetje 	}
440986fd29aSsetje 
441986fd29aSsetje }
442986fd29aSsetje 
443986fd29aSsetje /*
444986fd29aSsetje  * Create modpath from kernel name.
445986fd29aSsetje  * If we booted:
446986fd29aSsetje  *  /platform/`uname -i`/kernel/sparcv9/unix
447986fd29aSsetje  *   or
448986fd29aSsetje  *  /platform/`uname -m`/kernel/sparcv9/unix
449986fd29aSsetje  *
450986fd29aSsetje  * then make the modpath:
451986fd29aSsetje  *  /platform/`uname -i`/kernel /platform/`uname -m`/kernel
452986fd29aSsetje  *
453986fd29aSsetje  * otherwise, make the modpath the dir the kernel was
454986fd29aSsetje  * loaded from, minus any sparcv9 extension
455986fd29aSsetje  *
456986fd29aSsetje  * note the sparcv9 dir is optional since a unix -> sparcv9/unix
457986fd29aSsetje  * symlink is available as a shortcut.
458986fd29aSsetje  */
459986fd29aSsetje void
460986fd29aSsetje mach_modpath(char *path, const char *fname)
461986fd29aSsetje {
462986fd29aSsetje 	char *p;
463986fd29aSsetje 	int len, compat;
464986fd29aSsetje 	const char prefix[] = "/platform/";
465986fd29aSsetje 	char platname[MAXPATHLEN];
466986fd29aSsetje #ifdef	sun4u
467986fd29aSsetje 	char defname[] = "sun4u";
468986fd29aSsetje #else
469986fd29aSsetje 	char defname[] = "sun4v";
470986fd29aSsetje #endif
471986fd29aSsetje 	const char suffix[] = "/kernel";
472986fd29aSsetje 	const char isastr[] = "/sparcv9";
473986fd29aSsetje 
474986fd29aSsetje 	/*
475986fd29aSsetje 	 * check for /platform
476986fd29aSsetje 	 */
477986fd29aSsetje 	p = (char *)fname;
478986fd29aSsetje 	if (strncmp(p, prefix, sizeof (prefix) - 1) != 0)
479986fd29aSsetje 		goto nopath;
480986fd29aSsetje 	p += sizeof (prefix) - 1;
481986fd29aSsetje 
482986fd29aSsetje 	/*
483986fd29aSsetje 	 * check for the default name or the platform name.
484986fd29aSsetje 	 * also see if we used the 'compatible' name
485986fd29aSsetje 	 * (platname == default)
486986fd29aSsetje 	 */
487986fd29aSsetje 	(void) bop_getprop("impl-arch-name", platname);
488986fd29aSsetje 	compat = strcmp(platname, defname) == 0;
489986fd29aSsetje 	len = strlen(platname);
490986fd29aSsetje 	if (strncmp(p, platname, len) == 0)
491986fd29aSsetje 		p += len;
492986fd29aSsetje 	else if (strncmp(p, defname, sizeof (defname) - 1) == 0)
493986fd29aSsetje 		p += sizeof (defname) - 1;
494986fd29aSsetje 	else
495986fd29aSsetje 		goto nopath;
496986fd29aSsetje 
497986fd29aSsetje 	/*
498986fd29aSsetje 	 * check for /kernel/sparcv9 or just /kernel
499986fd29aSsetje 	 */
500986fd29aSsetje 	if (strncmp(p, suffix, sizeof (suffix) - 1) != 0)
501986fd29aSsetje 		goto nopath;
502986fd29aSsetje 	p += sizeof (suffix) - 1;
503986fd29aSsetje 	if (strncmp(p, isastr, sizeof (isastr) - 1) == 0)
504986fd29aSsetje 		p += sizeof (isastr) - 1;
505986fd29aSsetje 
506986fd29aSsetje 	/*
507986fd29aSsetje 	 * check we're at the last component
508986fd29aSsetje 	 */
509986fd29aSsetje 	if (p != strrchr(fname, '/'))
510986fd29aSsetje 		goto nopath;
511986fd29aSsetje 
512986fd29aSsetje 	/*
513986fd29aSsetje 	 * everything is kosher; setup modpath
514986fd29aSsetje 	 */
515986fd29aSsetje 	(void) strcpy(path, "/platform/");
516986fd29aSsetje 	(void) strcat(path, platname);
517da346f3bSPramod Batni 	(void) strcat(path, "/kernel");
518986fd29aSsetje 	if (!compat) {
519da346f3bSPramod Batni 		(void) strcat(path, " /platform/");
520986fd29aSsetje 		(void) strcat(path, defname);
521da346f3bSPramod Batni 		(void) strcat(path, "/kernel");
522986fd29aSsetje 	}
523986fd29aSsetje 	return;
524986fd29aSsetje 
525986fd29aSsetje nopath:
526986fd29aSsetje 	/*
527986fd29aSsetje 	 * Construct the directory path from the filename.
528986fd29aSsetje 	 */
529986fd29aSsetje 	if ((p = strrchr(fname, '/')) == NULL)
530986fd29aSsetje 		return;
531986fd29aSsetje 
532986fd29aSsetje 	while (p > fname && *(p - 1) == '/')
533986fd29aSsetje 		p--;	/* remove trailing '/' characters */
534986fd29aSsetje 	if (p == fname)
535986fd29aSsetje 		p++;	/* so "/" -is- the modpath in this case */
536986fd29aSsetje 
537986fd29aSsetje 	/*
538986fd29aSsetje 	 * Remove optional isa-dependent directory name - the module
539986fd29aSsetje 	 * subsystem will put this back again (!)
540986fd29aSsetje 	 */
541986fd29aSsetje 	len = p - fname;
542986fd29aSsetje 	if (len > sizeof (isastr) - 1 &&
543986fd29aSsetje 	    strncmp(&fname[len - (sizeof (isastr) - 1)], isastr,
544986fd29aSsetje 	    sizeof (isastr) - 1) == 0)
545986fd29aSsetje 		p -= sizeof (isastr) - 1;
546986fd29aSsetje 	(void) strncpy(path, fname, p - fname);
547986fd29aSsetje }
548