xref: /illumos-gate/usr/src/uts/i86pc/os/mlsetup.c (revision 9db7147e806ba9dd646607e04abfa51ec79c6ffd)
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
541791439Sandrei  * Common Development and Distribution License (the "License").
641791439Sandrei  * 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>
27ae115bc7Smrj #include <sys/sysmacros.h>
287c478bd9Sstevel@tonic-gate #include <sys/disp.h>
297c478bd9Sstevel@tonic-gate #include <sys/promif.h>
307c478bd9Sstevel@tonic-gate #include <sys/clock.h>
317c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
327c478bd9Sstevel@tonic-gate #include <sys/stack.h>
337c478bd9Sstevel@tonic-gate #include <vm/as.h>
347c478bd9Sstevel@tonic-gate #include <vm/hat.h>
357c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
367c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
377c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
387c478bd9Sstevel@tonic-gate #include <sys/proc.h>
397c478bd9Sstevel@tonic-gate #include <sys/thread.h>
407c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
417c478bd9Sstevel@tonic-gate #include <sys/pset.h>
427c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
43fb2f18f8Sesaxe #include <sys/pg.h>
447c478bd9Sstevel@tonic-gate #include <sys/disp.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
487c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
497c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
507c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
517c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
52*9db7147eSSherry Moore #include <sys/boot_console.h>
53ae115bc7Smrj #include <sys/kdi_machimpl.h>
547c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
557c478bd9Sstevel@tonic-gate #include <sys/promif.h>
56c88420b3Sdmick #include <sys/pci_cfgspace.h>
57843e1988Sjohnlev #ifdef __xpv
58843e1988Sjohnlev #include <sys/hypervisor.h>
59349b53ddSStuart Maybee #else
60349b53ddSStuart Maybee #include <sys/xpv_support.h>
61843e1988Sjohnlev #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * some globals for patching the result of cpuid
657c478bd9Sstevel@tonic-gate  * to solve problems w/ creative cpu vendors
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_include;
697c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_exclude;
707c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_include;
717c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_exclude;
727c478bd9Sstevel@tonic-gate 
734961a633Sdmick /*
744961a633Sdmick  * Dummy spl priority masks
754961a633Sdmick  */
76ae115bc7Smrj static unsigned char dummy_cpu_pri[MAXIPL + 1] = {
774961a633Sdmick 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
784961a633Sdmick 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf
794961a633Sdmick };
804961a633Sdmick 
81*9db7147eSSherry Moore /*
82*9db7147eSSherry Moore  * Set console mode
83*9db7147eSSherry Moore  */
84*9db7147eSSherry Moore static void
85*9db7147eSSherry Moore set_console_mode(uint8_t val)
86*9db7147eSSherry Moore {
87*9db7147eSSherry Moore 	struct bop_regs rp = {0};
88*9db7147eSSherry Moore 
89*9db7147eSSherry Moore 	rp.eax.byte.ah = 0x0;
90*9db7147eSSherry Moore 	rp.eax.byte.al = val;
91*9db7147eSSherry Moore 	rp.ebx.word.bx = 0x0;
92*9db7147eSSherry Moore 
93*9db7147eSSherry Moore 	BOP_DOINT(bootops, 0x10, &rp);
94*9db7147eSSherry Moore }
95*9db7147eSSherry Moore 
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Setup routine called right before main(). Interposing this function
997c478bd9Sstevel@tonic-gate  * before main() allows us to call it in a machine-independent fashion.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate void
1027c478bd9Sstevel@tonic-gate mlsetup(struct regs *rp)
1037c478bd9Sstevel@tonic-gate {
1042baa66a0SJonathan Chew 	u_longlong_t prop_value;
1057c478bd9Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
1067c478bd9Sstevel@tonic-gate 	extern disp_t cpu0_disp;
1077c478bd9Sstevel@tonic-gate 	extern char t0stack[];
108*9db7147eSSherry Moore 	extern int post_fastreboot;
109*9db7147eSSherry Moore 	extern int console;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/*
1147c478bd9Sstevel@tonic-gate 	 * initialize cpu_self
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	cpu[0]->cpu_self = cpu[0];
1177c478bd9Sstevel@tonic-gate 
118843e1988Sjohnlev #if defined(__xpv)
119843e1988Sjohnlev 	/*
120843e1988Sjohnlev 	 * Point at the hypervisor's virtual cpu structure
121843e1988Sjohnlev 	 */
122843e1988Sjohnlev 	cpu[0]->cpu_m.mcpu_vcpu_info = &HYPERVISOR_shared_info->vcpu_info[0];
123843e1988Sjohnlev #endif
124843e1988Sjohnlev 
1254961a633Sdmick 	/*
1264961a633Sdmick 	 * Set up dummy cpu_pri_data values till psm spl code is
1274961a633Sdmick 	 * installed.  This allows splx() to work on amd64.
1284961a633Sdmick 	 */
1294961a633Sdmick 
1304961a633Sdmick 	cpu[0]->cpu_pri_data = dummy_cpu_pri;
1314961a633Sdmick 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * check if we've got special bits to clear or set
1347c478bd9Sstevel@tonic-gate 	 * when checking cpu features
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 
1372baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_ecx_include", &prop_value) != 0)
1382baa66a0SJonathan Chew 		cpuid_feature_ecx_include = 0;
1392baa66a0SJonathan Chew 	else
1402baa66a0SJonathan Chew 		cpuid_feature_ecx_include = (uint32_t)prop_value;
1412baa66a0SJonathan Chew 
1422baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_ecx_exclude", &prop_value) != 0)
1432baa66a0SJonathan Chew 		cpuid_feature_ecx_exclude = 0;
1442baa66a0SJonathan Chew 	else
1452baa66a0SJonathan Chew 		cpuid_feature_ecx_exclude = (uint32_t)prop_value;
1462baa66a0SJonathan Chew 
1472baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_edx_include", &prop_value) != 0)
1482baa66a0SJonathan Chew 		cpuid_feature_edx_include = 0;
1492baa66a0SJonathan Chew 	else
1502baa66a0SJonathan Chew 		cpuid_feature_edx_include = (uint32_t)prop_value;
1512baa66a0SJonathan Chew 
1522baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_edx_exclude", &prop_value) != 0)
1532baa66a0SJonathan Chew 		cpuid_feature_edx_exclude = 0;
1542baa66a0SJonathan Chew 	else
1552baa66a0SJonathan Chew 		cpuid_feature_edx_exclude = (uint32_t)prop_value;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * The first lightweight pass (pass0) through the cpuid data
1597c478bd9Sstevel@tonic-gate 	 * was done in locore before mlsetup was called.  Do the next
1607c478bd9Sstevel@tonic-gate 	 * pass in C code.
1617c478bd9Sstevel@tonic-gate 	 *
1627c478bd9Sstevel@tonic-gate 	 * The x86_feature bits are set here on the basis of the capabilities
1637c478bd9Sstevel@tonic-gate 	 * of the boot CPU.  Note that if we choose to support CPUs that have
1647c478bd9Sstevel@tonic-gate 	 * different feature sets (at which point we would almost certainly
1657c478bd9Sstevel@tonic-gate 	 * want to set the feature bits to correspond to the feature
1667c478bd9Sstevel@tonic-gate 	 * minimum) this value may be altered.
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	x86_feature = cpuid_pass1(cpu[0]);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
1727c478bd9Sstevel@tonic-gate 	 */
173ae115bc7Smrj 	init_desctbls();
1747c478bd9Sstevel@tonic-gate 
175247dbb3dSsudheer #if !defined(__xpv)
176247dbb3dSsudheer 
177349b53ddSStuart Maybee 	if (get_hwenv() == HW_XEN_HVM)
178349b53ddSStuart Maybee 		xen_hvm_init();
179349b53ddSStuart Maybee 
180247dbb3dSsudheer 	/*
181247dbb3dSsudheer 	 * Patch the tsc_read routine with appropriate set of instructions,
182247dbb3dSsudheer 	 * depending on the processor family and architecure, to read the
183247dbb3dSsudheer 	 * time-stamp counter while ensuring no out-of-order execution.
184247dbb3dSsudheer 	 * Patch it while the kernel text is still writable.
185247dbb3dSsudheer 	 *
186247dbb3dSsudheer 	 * Note: tsc_read is not patched for intel processors whose family
187247dbb3dSsudheer 	 * is >6 and for amd whose family >f (in case they don't support rdtscp
188247dbb3dSsudheer 	 * instruction, unlikely). By default tsc_read will use cpuid for
189247dbb3dSsudheer 	 * serialization in such cases. The following code needs to be
190247dbb3dSsudheer 	 * revisited if intel processors of family >= f retains the
191247dbb3dSsudheer 	 * instruction serialization nature of mfence instruction.
1922b0bcb26Ssudheer 	 * Note: tsc_read is not patched for x86 processors which do
1932b0bcb26Ssudheer 	 * not support "mfence". By default tsc_read will use cpuid for
1942b0bcb26Ssudheer 	 * serialization in such cases.
195551bc2a6Smrj 	 *
196551bc2a6Smrj 	 * The Xen hypervisor does not correctly report whether rdtscp is
197551bc2a6Smrj 	 * supported or not, so we must assume that it is not.
198247dbb3dSsudheer 	 */
199b9bfdccdSStuart Maybee 	if (get_hwenv() != HW_XEN_HVM && (x86_feature & X86_TSCP))
200247dbb3dSsudheer 		patch_tsc_read(X86_HAVE_TSCP);
201247dbb3dSsudheer 	else if (cpuid_getvendor(CPU) == X86_VENDOR_AMD &&
2022b0bcb26Ssudheer 	    cpuid_getfamily(CPU) <= 0xf && (x86_feature & X86_SSE2) != 0)
203247dbb3dSsudheer 		patch_tsc_read(X86_TSC_MFENCE);
204247dbb3dSsudheer 	else if (cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
2052b0bcb26Ssudheer 	    cpuid_getfamily(CPU) <= 6 && (x86_feature & X86_SSE2) != 0)
20615363b27Ssudheer 		patch_tsc_read(X86_TSC_LFENCE);
207247dbb3dSsudheer 
208247dbb3dSsudheer #endif	/* !__xpv */
2097c478bd9Sstevel@tonic-gate 
210843e1988Sjohnlev #if defined(__i386) && !defined(__xpv)
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * Some i386 processors do not implement the rdtsc instruction,
213247dbb3dSsudheer 	 * or at least they do not implement it correctly. Patch them to
214247dbb3dSsudheer 	 * return 0.
2157c478bd9Sstevel@tonic-gate 	 */
216247dbb3dSsudheer 	if ((x86_feature & X86_TSC) == 0)
217247dbb3dSsudheer 		patch_tsc_read(X86_NO_TSC);
218843e1988Sjohnlev #endif	/* __i386 && !__xpv */
219843e1988Sjohnlev 
22022cc0e45SBill Holler #if defined(__amd64) && !defined(__xpv)
22122cc0e45SBill Holler 	patch_memops(cpuid_getvendor(CPU));
22222cc0e45SBill Holler #endif	/* __amd64 && !__xpv */
22322cc0e45SBill Holler 
224843e1988Sjohnlev #if !defined(__xpv)
225843e1988Sjohnlev 	/* XXPV	what, if anything, should be dorked with here under xen? */
226ae115bc7Smrj 
227ae115bc7Smrj 	/*
228ae115bc7Smrj 	 * While we're thinking about the TSC, let's set up %cr4 so that
229ae115bc7Smrj 	 * userland can issue rdtsc, and initialize the TSC_AUX value
230ae115bc7Smrj 	 * (the cpuid) for the rdtscp instruction on appropriately
231ae115bc7Smrj 	 * capable hardware.
232ae115bc7Smrj 	 */
233ae115bc7Smrj 	if (x86_feature & X86_TSC)
234ae115bc7Smrj 		setcr4(getcr4() & ~CR4_TSD);
235ae115bc7Smrj 
236ae115bc7Smrj 	if (x86_feature & X86_TSCP)
237ae115bc7Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, 0);
238ae115bc7Smrj 
239ae115bc7Smrj 	if (x86_feature & X86_DE)
240ae115bc7Smrj 		setcr4(getcr4() | CR4_DE);
241843e1988Sjohnlev #endif /* __xpv */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * initialize t0
2457c478bd9Sstevel@tonic-gate 	 */
2467c478bd9Sstevel@tonic-gate 	t0.t_stk = (caddr_t)rp - MINFRAME;
2477c478bd9Sstevel@tonic-gate 	t0.t_stkbase = t0stack;
2487c478bd9Sstevel@tonic-gate 	t0.t_pri = maxclsyspri - 3;
2497c478bd9Sstevel@tonic-gate 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
2507c478bd9Sstevel@tonic-gate 	t0.t_procp = &p0;
2517c478bd9Sstevel@tonic-gate 	t0.t_plockp = &p0lock.pl_lock;
2527c478bd9Sstevel@tonic-gate 	t0.t_lwp = &lwp0;
2537c478bd9Sstevel@tonic-gate 	t0.t_forw = &t0;
2547c478bd9Sstevel@tonic-gate 	t0.t_back = &t0;
2557c478bd9Sstevel@tonic-gate 	t0.t_next = &t0;
2567c478bd9Sstevel@tonic-gate 	t0.t_prev = &t0;
2577c478bd9Sstevel@tonic-gate 	t0.t_cpu = cpu[0];
2587c478bd9Sstevel@tonic-gate 	t0.t_disp_queue = &cpu0_disp;
2597c478bd9Sstevel@tonic-gate 	t0.t_bind_cpu = PBIND_NONE;
2607c478bd9Sstevel@tonic-gate 	t0.t_bind_pset = PS_NONE;
2610b70c467Sakolb 	t0.t_bindflag = (uchar_t)default_binding_mode;
2627c478bd9Sstevel@tonic-gate 	t0.t_cpupart = &cp_default;
2637c478bd9Sstevel@tonic-gate 	t0.t_clfuncs = &sys_classfuncs.thread;
2647c478bd9Sstevel@tonic-gate 	t0.t_copyops = NULL;
2657c478bd9Sstevel@tonic-gate 	THREAD_ONPROC(&t0, CPU);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	lwp0.lwp_thread = &t0;
268ae115bc7Smrj 	lwp0.lwp_regs = (void *)rp;
2697c478bd9Sstevel@tonic-gate 	lwp0.lwp_procp = &p0;
2707c478bd9Sstevel@tonic-gate 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	p0.p_exec = NULL;
2737c478bd9Sstevel@tonic-gate 	p0.p_stat = SRUN;
2747c478bd9Sstevel@tonic-gate 	p0.p_flag = SSYS;
2757c478bd9Sstevel@tonic-gate 	p0.p_tlist = &t0;
2767c478bd9Sstevel@tonic-gate 	p0.p_stksize = 2*PAGESIZE;
2777c478bd9Sstevel@tonic-gate 	p0.p_stkpageszc = 0;
2787c478bd9Sstevel@tonic-gate 	p0.p_as = &kas;
2797c478bd9Sstevel@tonic-gate 	p0.p_lockp = &p0lock;
2807c478bd9Sstevel@tonic-gate 	p0.p_brkpageszc = 0;
2812cb27123Saguzovsk 	p0.p_t1_lgrpid = LGRP_NONE;
2822cb27123Saguzovsk 	p0.p_tr_lgrpid = LGRP_NONE;
2837c478bd9Sstevel@tonic-gate 	sigorset(&p0.p_ignore, &ignoredefault);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	CPU->cpu_thread = &t0;
2867c478bd9Sstevel@tonic-gate 	bzero(&cpu0_disp, sizeof (disp_t));
2877c478bd9Sstevel@tonic-gate 	CPU->cpu_disp = &cpu0_disp;
2887c478bd9Sstevel@tonic-gate 	CPU->cpu_disp->disp_cpu = CPU;
2897c478bd9Sstevel@tonic-gate 	CPU->cpu_dispthread = &t0;
2907c478bd9Sstevel@tonic-gate 	CPU->cpu_idle_thread = &t0;
2917c478bd9Sstevel@tonic-gate 	CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
2927c478bd9Sstevel@tonic-gate 	CPU->cpu_dispatch_pri = t0.t_pri;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	CPU->cpu_id = 0;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	CPU->cpu_pri = 12;		/* initial PIL for the boot CPU */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/*
2990baeff3dSrab 	 * The kernel doesn't use LDTs unless a process explicitly requests one.
3007c478bd9Sstevel@tonic-gate 	 */
301843e1988Sjohnlev 	p0.p_ldt_desc = null_sdesc;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
304ae115bc7Smrj 	 * Initialize thread/cpu microstate accounting
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 	init_mstate(&t0, LMS_SYSTEM);
3077c478bd9Sstevel@tonic-gate 	init_cpu_mstate(CPU, CMS_SYSTEM);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * Initialize lists of available and active CPUs.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	cpu_list_init(CPU);
3130e751525SEric Saxe 
3140e751525SEric Saxe 	pg_cpu_bootstrap(CPU);
3157c478bd9Sstevel@tonic-gate 
316ae115bc7Smrj 	/*
317ae115bc7Smrj 	 * Now that we have taken over the GDT, IDT and have initialized
318ae115bc7Smrj 	 * active CPU list it's time to inform kmdb if present.
319ae115bc7Smrj 	 */
320ae115bc7Smrj 	if (boothowto & RB_DEBUG)
321ae115bc7Smrj 		kdi_idt_sync();
322ae115bc7Smrj 
323*9db7147eSSherry Moore 	/*
324*9db7147eSSherry Moore 	 * Explicitly set console to text mode (0x3) if this is a boot
325*9db7147eSSherry Moore 	 * post Fast Reboot, and the console is set to CONS_SCREEN_TEXT.
326*9db7147eSSherry Moore 	 */
327*9db7147eSSherry Moore 	if (post_fastreboot && console == CONS_SCREEN_TEXT)
328*9db7147eSSherry Moore 		set_console_mode(0x3);
329*9db7147eSSherry Moore 
330ae115bc7Smrj 	/*
331ae115bc7Smrj 	 * If requested (boot -d) drop into kmdb.
332ae115bc7Smrj 	 *
333ae115bc7Smrj 	 * This must be done after cpu_list_init() on the 64-bit kernel
334ae115bc7Smrj 	 * since taking a trap requires that we re-compute gsbase based
335ae115bc7Smrj 	 * on the cpu list.
336ae115bc7Smrj 	 */
337ae115bc7Smrj 	if (boothowto & RB_DEBUGENTER)
338ae115bc7Smrj 		kmdb_enter();
339ae115bc7Smrj 
340affbd3ccSkchow 	cpu_vm_data_init(CPU);
341affbd3ccSkchow 
342c88420b3Sdmick 	/* lgrp_init() needs PCI config space access */
343843e1988Sjohnlev #if defined(__xpv)
344843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info))
345843e1988Sjohnlev 		pci_cfgspace_init();
346843e1988Sjohnlev #else
347c88420b3Sdmick 	pci_cfgspace_init();
348843e1988Sjohnlev #endif
349c88420b3Sdmick 
3507c478bd9Sstevel@tonic-gate 	rp->r_fp = 0;	/* terminate kernel stack traces! */
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	prom_init("kernel", (void *)NULL);
3537c478bd9Sstevel@tonic-gate 
3542baa66a0SJonathan Chew 	if (bootprop_getval("boot-ncpus", &prop_value) != 0)
355ae115bc7Smrj 		boot_ncpus = NCPU;
3562baa66a0SJonathan Chew 	else {
3572baa66a0SJonathan Chew 		boot_ncpus = (int)prop_value;
3582baa66a0SJonathan Chew 		if (boot_ncpus <= 0 || boot_ncpus > NCPU)
3592baa66a0SJonathan Chew 			boot_ncpus = NCPU;
3602baa66a0SJonathan Chew 	}
36141791439Sandrei 
36241791439Sandrei 	max_ncpus = boot_max_ncpus = boot_ncpus;
36341791439Sandrei 
3642e2c009bSjjc 	/*
3652e2c009bSjjc 	 * Initialize the lgrp framework
3662e2c009bSjjc 	 */
3672e2c009bSjjc 	lgrp_init();
3682e2c009bSjjc 
3697c478bd9Sstevel@tonic-gate 	if (boothowto & RB_HALT) {
3707c478bd9Sstevel@tonic-gate 		prom_printf("unix: kernel halted by -h flag\n");
3717c478bd9Sstevel@tonic-gate 		prom_enter_mon();
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
3757c478bd9Sstevel@tonic-gate 
3762449e17fSsherrym 	/*
3772449e17fSsherrym 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
3782449e17fSsherrym 	 */
3792449e17fSsherrym 	ucode_check(CPU);
3802449e17fSsherrym 
3817c478bd9Sstevel@tonic-gate 	if (workaround_errata(CPU) != 0)
3827c478bd9Sstevel@tonic-gate 		panic("critical workaround(s) missing for boot cpu");
3837c478bd9Sstevel@tonic-gate }
384986fd29aSsetje 
385986fd29aSsetje 
386986fd29aSsetje void
387986fd29aSsetje mach_modpath(char *path, const char *filename)
388986fd29aSsetje {
389986fd29aSsetje 	/*
390986fd29aSsetje 	 * Construct the directory path from the filename.
391986fd29aSsetje 	 */
392986fd29aSsetje 
393986fd29aSsetje 	int len;
394986fd29aSsetje 	char *p;
395986fd29aSsetje 	const char isastr[] = "/amd64";
396986fd29aSsetje 	size_t isalen = strlen(isastr);
397986fd29aSsetje 
398986fd29aSsetje 	if ((p = strrchr(filename, '/')) == NULL)
399986fd29aSsetje 		return;
400986fd29aSsetje 
401986fd29aSsetje 	while (p > filename && *(p - 1) == '/')
402986fd29aSsetje 		p--;	/* remove trailing '/' characters */
403986fd29aSsetje 	if (p == filename)
404986fd29aSsetje 		p++;	/* so "/" -is- the modpath in this case */
405986fd29aSsetje 
406986fd29aSsetje 	/*
407986fd29aSsetje 	 * Remove optional isa-dependent directory name - the module
408986fd29aSsetje 	 * subsystem will put this back again (!)
409986fd29aSsetje 	 */
410986fd29aSsetje 	len = p - filename;
411986fd29aSsetje 	if (len > isalen &&
412986fd29aSsetje 	    strncmp(&filename[len - isalen], isastr, isalen) == 0)
413986fd29aSsetje 		p -= isalen;
414986fd29aSsetje 
415986fd29aSsetje 	/*
416986fd29aSsetje 	 * "/platform/mumblefrotz" + " " + MOD_DEFPATH
417986fd29aSsetje 	 */
418986fd29aSsetje 	len += (p - filename) + 1 + strlen(MOD_DEFPATH) + 1;
419986fd29aSsetje 	(void) strncpy(path, filename, p - filename);
420986fd29aSsetje }
421