xref: /illumos-gate/usr/src/uts/i86pc/os/mlsetup.c (revision df5a0c57c8c46976f94e3d1ffa07b420f2b24501)
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 /*
220d928757SGary Mills  * Copyright (c) 2012 Gary Mills
230d928757SGary Mills  *
247417cfdeSKuriakose Kuruvilla  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
25cfe84b82SMatt Amdur  * Copyright (c) 2011 by Delphix. All rights reserved.
26c3377ee9SJohn Levon  * Copyright 2019 Joyent, Inc.
27*df5a0c57SLuqman Aden  * Copyright 2024 Oxide Computer Company
287c478bd9Sstevel@tonic-gate  */
29a3114836SGerry Liu /*
30a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
31a3114836SGerry Liu  * All rights reserved.
32a3114836SGerry Liu  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
35ae115bc7Smrj #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/disp.h>
377c478bd9Sstevel@tonic-gate #include <sys/promif.h>
387c478bd9Sstevel@tonic-gate #include <sys/clock.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/stack.h>
417c478bd9Sstevel@tonic-gate #include <vm/as.h>
427c478bd9Sstevel@tonic-gate #include <vm/hat.h>
437c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
447c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
457c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
467c478bd9Sstevel@tonic-gate #include <sys/proc.h>
477c478bd9Sstevel@tonic-gate #include <sys/thread.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
497c478bd9Sstevel@tonic-gate #include <sys/pset.h>
507c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
51fb2f18f8Sesaxe #include <sys/pg.h>
527c478bd9Sstevel@tonic-gate #include <sys/disp.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
557c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
567c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
577c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
587c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
597c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
609db7147eSSherry Moore #include <sys/boot_console.h>
61ae115bc7Smrj #include <sys/kdi_machimpl.h>
627c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
637c478bd9Sstevel@tonic-gate #include <sys/promif.h>
64c88420b3Sdmick #include <sys/pci_cfgspace.h>
650c26abfeSJohn Levon #include <sys/apic.h>
660c26abfeSJohn Levon #include <sys/apic_common.h>
670181461bSKeith M Wesolowski #include <sys/bootvfs.h>
682428aad8SPatrick Mooney #include <sys/tsc.h>
69c3377ee9SJohn Levon #include <sys/smt.h>
70843e1988Sjohnlev #ifdef __xpv
71843e1988Sjohnlev #include <sys/hypervisor.h>
72349b53ddSStuart Maybee #else
73349b53ddSStuart Maybee #include <sys/xpv_support.h>
74843e1988Sjohnlev #endif
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * some globals for patching the result of cpuid
787c478bd9Sstevel@tonic-gate  * to solve problems w/ creative cpu vendors
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_include;
827c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_exclude;
837c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_include;
847c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_exclude;
857c478bd9Sstevel@tonic-gate 
860c26abfeSJohn Levon nmi_action_t nmi_action = NMI_ACTION_UNSET;
870c26abfeSJohn Levon 
887c478bd9Sstevel@tonic-gate /*
899db7147eSSherry Moore  * Set console mode
909db7147eSSherry Moore  */
919db7147eSSherry Moore static void
set_console_mode(uint8_t val)929db7147eSSherry Moore set_console_mode(uint8_t val)
939db7147eSSherry Moore {
949db7147eSSherry Moore 	struct bop_regs rp = {0};
959db7147eSSherry Moore 
969db7147eSSherry Moore 	rp.eax.byte.ah = 0x0;
979db7147eSSherry Moore 	rp.eax.byte.al = val;
989db7147eSSherry Moore 	rp.ebx.word.bx = 0x0;
999db7147eSSherry Moore 
1009db7147eSSherry Moore 	BOP_DOINT(bootops, 0x10, &rp);
1019db7147eSSherry Moore }
1029db7147eSSherry Moore 
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Setup routine called right before main(). Interposing this function
1067c478bd9Sstevel@tonic-gate  * before main() allows us to call it in a machine-independent fashion.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate void
mlsetup(struct regs * rp)1097c478bd9Sstevel@tonic-gate mlsetup(struct regs *rp)
1107c478bd9Sstevel@tonic-gate {
1112baa66a0SJonathan Chew 	u_longlong_t prop_value;
1120c26abfeSJohn Levon 	char prop_str[BP_MAX_STRLEN];
1137c478bd9Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
1147c478bd9Sstevel@tonic-gate 	extern disp_t cpu0_disp;
1157c478bd9Sstevel@tonic-gate 	extern char t0stack[];
1169db7147eSSherry Moore 	extern int post_fastreboot;
117a3114836SGerry Liu 	extern uint64_t plat_dr_options;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * initialize cpu_self
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	cpu[0]->cpu_self = cpu[0];
1257c478bd9Sstevel@tonic-gate 
126843e1988Sjohnlev #if defined(__xpv)
127843e1988Sjohnlev 	/*
128843e1988Sjohnlev 	 * Point at the hypervisor's virtual cpu structure
129843e1988Sjohnlev 	 */
130843e1988Sjohnlev 	cpu[0]->cpu_m.mcpu_vcpu_info = &HYPERVISOR_shared_info->vcpu_info[0];
131843e1988Sjohnlev #endif
132843e1988Sjohnlev 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * check if we've got special bits to clear or set
1357c478bd9Sstevel@tonic-gate 	 * when checking cpu features
1367c478bd9Sstevel@tonic-gate 	 */
1377c478bd9Sstevel@tonic-gate 
1382baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_ecx_include", &prop_value) != 0)
1392baa66a0SJonathan Chew 		cpuid_feature_ecx_include = 0;
1402baa66a0SJonathan Chew 	else
1412baa66a0SJonathan Chew 		cpuid_feature_ecx_include = (uint32_t)prop_value;
1422baa66a0SJonathan Chew 
1432baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_ecx_exclude", &prop_value) != 0)
1442baa66a0SJonathan Chew 		cpuid_feature_ecx_exclude = 0;
1452baa66a0SJonathan Chew 	else
1462baa66a0SJonathan Chew 		cpuid_feature_ecx_exclude = (uint32_t)prop_value;
1472baa66a0SJonathan Chew 
1482baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_edx_include", &prop_value) != 0)
1492baa66a0SJonathan Chew 		cpuid_feature_edx_include = 0;
1502baa66a0SJonathan Chew 	else
1512baa66a0SJonathan Chew 		cpuid_feature_edx_include = (uint32_t)prop_value;
1522baa66a0SJonathan Chew 
1532baa66a0SJonathan Chew 	if (bootprop_getval("cpuid_feature_edx_exclude", &prop_value) != 0)
1542baa66a0SJonathan Chew 		cpuid_feature_edx_exclude = 0;
1552baa66a0SJonathan Chew 	else
1562baa66a0SJonathan Chew 		cpuid_feature_edx_exclude = (uint32_t)prop_value;
1577c478bd9Sstevel@tonic-gate 
15874ecdb51SJohn Levon #if !defined(__xpv)
1590c26abfeSJohn Levon 	if (bootprop_getstr("nmi", prop_str, sizeof (prop_str)) == 0) {
1600c26abfeSJohn Levon 		if (strcmp(prop_str, "ignore") == 0) {
1610c26abfeSJohn Levon 			nmi_action = NMI_ACTION_IGNORE;
1620c26abfeSJohn Levon 		} else if (strcmp(prop_str, "panic") == 0) {
1630c26abfeSJohn Levon 			nmi_action = NMI_ACTION_PANIC;
1640c26abfeSJohn Levon 		} else if (strcmp(prop_str, "kmdb") == 0) {
1650c26abfeSJohn Levon 			nmi_action = NMI_ACTION_KMDB;
1660c26abfeSJohn Levon 		} else {
1670c26abfeSJohn Levon 			prom_printf("unix: ignoring unknown nmi=%s\n",
1680c26abfeSJohn Levon 			    prop_str);
1690c26abfeSJohn Levon 		}
1700c26abfeSJohn Levon 	}
1710c26abfeSJohn Levon 
17274ecdb51SJohn Levon 	/*
17374ecdb51SJohn Levon 	 * Check to see if KPTI has been explicitly enabled or disabled.
17474ecdb51SJohn Levon 	 * We have to check this before init_desctbls().
17574ecdb51SJohn Levon 	 */
17674ecdb51SJohn Levon 	if (bootprop_getval("kpti", &prop_value) == 0) {
17774ecdb51SJohn Levon 		kpti_enable = (uint64_t)(prop_value == 1);
17874ecdb51SJohn Levon 		prom_printf("unix: forcing kpti to %s due to boot argument\n",
17974ecdb51SJohn Levon 		    (kpti_enable == 1) ? "ON" : "OFF");
18074ecdb51SJohn Levon 	} else {
18174ecdb51SJohn Levon 		kpti_enable = 1;
18274ecdb51SJohn Levon 	}
18374ecdb51SJohn Levon 
18474ecdb51SJohn Levon 	if (bootprop_getval("pcid", &prop_value) == 0 && prop_value == 0) {
18574ecdb51SJohn Levon 		prom_printf("unix: forcing pcid to OFF due to boot argument\n");
18674ecdb51SJohn Levon 		x86_use_pcid = 0;
18774ecdb51SJohn Levon 	} else if (kpti_enable != 1) {
18874ecdb51SJohn Levon 		x86_use_pcid = 0;
18974ecdb51SJohn Levon 	}
190c3377ee9SJohn Levon 
191c3377ee9SJohn Levon 	/*
192c3377ee9SJohn Levon 	 * While we don't need to check this until later, we might as well do it
193c3377ee9SJohn Levon 	 * here.
194c3377ee9SJohn Levon 	 */
195c3377ee9SJohn Levon 	if (bootprop_getstr("smt_enabled", prop_str, sizeof (prop_str)) == 0) {
196c3377ee9SJohn Levon 		if (strcasecmp(prop_str, "false") == 0 ||
197c3377ee9SJohn Levon 		    strcmp(prop_str, "0") == 0)
198c3377ee9SJohn Levon 			smt_boot_disable = 1;
199c3377ee9SJohn Levon 	}
200c3377ee9SJohn Levon 
20174ecdb51SJohn Levon #endif
20274ecdb51SJohn Levon 
2037c478bd9Sstevel@tonic-gate 	/*
20445e032f7SDan Mick 	 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
20545e032f7SDan Mick 	 */
20645e032f7SDan Mick 	init_desctbls();
20745e032f7SDan Mick 
20845e032f7SDan Mick 	/*
209*df5a0c57SLuqman Aden 	 * initialize t0
210*df5a0c57SLuqman Aden 	 */
211*df5a0c57SLuqman Aden 	t0.t_stk = (caddr_t)rp - MINFRAME;
212*df5a0c57SLuqman Aden 	t0.t_stkbase = t0stack;
213*df5a0c57SLuqman Aden 	t0.t_pri = maxclsyspri - 3;
214*df5a0c57SLuqman Aden 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
215*df5a0c57SLuqman Aden 	t0.t_procp = &p0;
216*df5a0c57SLuqman Aden 	t0.t_plockp = &p0lock.pl_lock;
217*df5a0c57SLuqman Aden 	t0.t_lwp = &lwp0;
218*df5a0c57SLuqman Aden 	t0.t_forw = &t0;
219*df5a0c57SLuqman Aden 	t0.t_back = &t0;
220*df5a0c57SLuqman Aden 	t0.t_next = &t0;
221*df5a0c57SLuqman Aden 	t0.t_prev = &t0;
222*df5a0c57SLuqman Aden 	t0.t_cpu = cpu[0];
223*df5a0c57SLuqman Aden 	t0.t_disp_queue = &cpu0_disp;
224*df5a0c57SLuqman Aden 	t0.t_bind_cpu = PBIND_NONE;
225*df5a0c57SLuqman Aden 	t0.t_bind_pset = PS_NONE;
226*df5a0c57SLuqman Aden 	t0.t_bindflag = (uchar_t)default_binding_mode;
227*df5a0c57SLuqman Aden 	t0.t_cpupart = &cp_default;
228*df5a0c57SLuqman Aden 	t0.t_clfuncs = &sys_classfuncs.thread;
229*df5a0c57SLuqman Aden 	t0.t_copyops = NULL;
230*df5a0c57SLuqman Aden 	THREAD_ONPROC(&t0, CPU);
231*df5a0c57SLuqman Aden 
232*df5a0c57SLuqman Aden 	lwp0.lwp_thread = &t0;
233*df5a0c57SLuqman Aden 	lwp0.lwp_regs = (void *)rp;
234*df5a0c57SLuqman Aden 	lwp0.lwp_procp = &p0;
235*df5a0c57SLuqman Aden 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
236*df5a0c57SLuqman Aden 
237*df5a0c57SLuqman Aden 	p0.p_exec = NULL;
238*df5a0c57SLuqman Aden 	p0.p_stat = SRUN;
239*df5a0c57SLuqman Aden 	p0.p_flag = SSYS;
240*df5a0c57SLuqman Aden 	p0.p_tlist = &t0;
241*df5a0c57SLuqman Aden 	p0.p_stksize = 2*PAGESIZE;
242*df5a0c57SLuqman Aden 	p0.p_stkpageszc = 0;
243*df5a0c57SLuqman Aden 	p0.p_as = &kas;
244*df5a0c57SLuqman Aden 	p0.p_lockp = &p0lock;
245*df5a0c57SLuqman Aden 	p0.p_brkpageszc = 0;
246*df5a0c57SLuqman Aden 	p0.p_t1_lgrpid = LGRP_NONE;
247*df5a0c57SLuqman Aden 	p0.p_tr_lgrpid = LGRP_NONE;
248*df5a0c57SLuqman Aden 	psecflags_default(&p0.p_secflags);
249*df5a0c57SLuqman Aden 
250*df5a0c57SLuqman Aden 	sigorset(&p0.p_ignore, &ignoredefault);
251*df5a0c57SLuqman Aden 
252*df5a0c57SLuqman Aden 	CPU->cpu_thread = &t0;
253*df5a0c57SLuqman Aden 	bzero(&cpu0_disp, sizeof (disp_t));
254*df5a0c57SLuqman Aden 	CPU->cpu_disp = &cpu0_disp;
255*df5a0c57SLuqman Aden 	CPU->cpu_disp->disp_cpu = CPU;
256*df5a0c57SLuqman Aden 	CPU->cpu_dispthread = &t0;
257*df5a0c57SLuqman Aden 	CPU->cpu_idle_thread = &t0;
258*df5a0c57SLuqman Aden 	CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
259*df5a0c57SLuqman Aden 	CPU->cpu_dispatch_pri = t0.t_pri;
260*df5a0c57SLuqman Aden 
261*df5a0c57SLuqman Aden 	CPU->cpu_id = 0;
262*df5a0c57SLuqman Aden 
263*df5a0c57SLuqman Aden 	CPU->cpu_pri = 12;		/* initial PIL for the boot CPU */
264*df5a0c57SLuqman Aden 
265*df5a0c57SLuqman Aden 	/*
266ab5bb018SKeith M Wesolowski 	 * Ensure that we have set the necessary feature bits before setting up
267ab5bb018SKeith M Wesolowski 	 * PCI config space access.
268ab5bb018SKeith M Wesolowski 	 */
269ab5bb018SKeith M Wesolowski 	cpuid_execpass(cpu[0], CPUID_PASS_PRELUDE, x86_featureset);
270ab5bb018SKeith M Wesolowski 
271ab5bb018SKeith M Wesolowski 	/*
27245e032f7SDan Mick 	 * lgrp_init() and possibly cpuid_pass1() need PCI config
27345e032f7SDan Mick 	 * space access
27445e032f7SDan Mick 	 */
27545e032f7SDan Mick #if defined(__xpv)
27645e032f7SDan Mick 	if (DOMAIN_IS_INITDOMAIN(xen_info))
27745e032f7SDan Mick 		pci_cfgspace_init();
27845e032f7SDan Mick #else
27945e032f7SDan Mick 	pci_cfgspace_init();
280cfe84b82SMatt Amdur 	/*
281cfe84b82SMatt Amdur 	 * Initialize the platform type from CPU 0 to ensure that
282cfe84b82SMatt Amdur 	 * determine_platform() is only ever called once.
283cfe84b82SMatt Amdur 	 */
284cfe84b82SMatt Amdur 	determine_platform();
28545e032f7SDan Mick #endif
28645e032f7SDan Mick 
28745e032f7SDan Mick 	/*
288*df5a0c57SLuqman Aden 	 * While the BIOS may have already applied some microcode updates, we
289*df5a0c57SLuqman Aden 	 * may have more recent updates available that we'd like to apply.  The
290*df5a0c57SLuqman Aden 	 * application of said microcode may end up resulting in architecturally
291*df5a0c57SLuqman Aden 	 * visible changes (e.g., changed MSR or CPUID bits) which we'd like to
292*df5a0c57SLuqman Aden 	 * have in place before we start querying the CPU for its capabilities.
293*df5a0c57SLuqman Aden 	 * So we first run the IDENT pass to determine the specific CPU vendor,
294*df5a0c57SLuqman Aden 	 * model, rev etc., fill out cpu_ucode_info and update the microcode, if
295*df5a0c57SLuqman Aden 	 * necessary.
296*df5a0c57SLuqman Aden 	 */
297*df5a0c57SLuqman Aden 	cpuid_execpass(cpu[0], CPUID_PASS_IDENT, NULL);
298*df5a0c57SLuqman Aden 	ucode_init();
299*df5a0c57SLuqman Aden 	ucode_check_boot();
300*df5a0c57SLuqman Aden 
301*df5a0c57SLuqman Aden 	/*
302*df5a0c57SLuqman Aden 	 * Now we're ready to run the BASIC cpuid pass.
3037c478bd9Sstevel@tonic-gate 	 *
304ab5bb018SKeith M Wesolowski 	 * The x86_featureset is initialized here based on the capabilities of
305ab5bb018SKeith M Wesolowski 	 * the boot CPU.  Note that if we choose to support CPUs that have
306ab5bb018SKeith M Wesolowski 	 * different feature sets (at which point we would almost certainly want
307ab5bb018SKeith M Wesolowski 	 * to set the feature bits to correspond to the feature minimum) this
308ab5bb018SKeith M Wesolowski 	 * value may be altered.
3097c478bd9Sstevel@tonic-gate 	 */
310ab5bb018SKeith M Wesolowski 	cpuid_execpass(cpu[0], CPUID_PASS_BASIC, x86_featureset);
3117c478bd9Sstevel@tonic-gate 
312247dbb3dSsudheer #if !defined(__xpv)
31379ec9da8SYuri Pankov 	if ((get_hwenv() & HW_XEN_HVM) != 0)
314349b53ddSStuart Maybee 		xen_hvm_init();
315349b53ddSStuart Maybee 
316247dbb3dSsudheer 	/*
3174948216cSKeith M Wesolowski 	 * Before we do anything with the TSCs, we need to work around
3184948216cSKeith M Wesolowski 	 * Intel erratum BT81.  On some CPUs, warm reset does not
3194948216cSKeith M Wesolowski 	 * clear the TSC.  If we are on such a CPU, we will clear TSC ourselves
3204948216cSKeith M Wesolowski 	 * here.  Other CPUs will clear it when we boot them later, and the
3214948216cSKeith M Wesolowski 	 * resulting skew will be handled by tsc_sync_master()/_slave();
3224948216cSKeith M Wesolowski 	 * note that such skew already exists and has to be handled anyway.
3234948216cSKeith M Wesolowski 	 *
3244948216cSKeith M Wesolowski 	 * We do this only on metal.  This same problem can occur with a
3254948216cSKeith M Wesolowski 	 * hypervisor that does not happen to virtualise a TSC that starts from
3264948216cSKeith M Wesolowski 	 * zero, regardless of CPU type; however, we do not expect hypervisors
3274948216cSKeith M Wesolowski 	 * that do not virtualise TSC that way to handle writes to TSC
3284948216cSKeith M Wesolowski 	 * correctly, either.
3294948216cSKeith M Wesolowski 	 */
3304948216cSKeith M Wesolowski 	if (get_hwenv() == HW_NATIVE &&
3314948216cSKeith M Wesolowski 	    cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
3324948216cSKeith M Wesolowski 	    cpuid_getfamily(CPU) == 6 &&
3334948216cSKeith M Wesolowski 	    (cpuid_getmodel(CPU) == 0x2d || cpuid_getmodel(CPU) == 0x3e) &&
3344948216cSKeith M Wesolowski 	    is_x86_feature(x86_featureset, X86FSET_TSC)) {
3354948216cSKeith M Wesolowski 		(void) wrmsr(REG_TSC, 0UL);
3364948216cSKeith M Wesolowski 	}
3374948216cSKeith M Wesolowski 
3384948216cSKeith M Wesolowski 	/*
339247dbb3dSsudheer 	 * Patch the tsc_read routine with appropriate set of instructions,
340247dbb3dSsudheer 	 * depending on the processor family and architecure, to read the
341247dbb3dSsudheer 	 * time-stamp counter while ensuring no out-of-order execution.
342247dbb3dSsudheer 	 * Patch it while the kernel text is still writable.
343247dbb3dSsudheer 	 *
344551bc2a6Smrj 	 * The Xen hypervisor does not correctly report whether rdtscp is
345551bc2a6Smrj 	 * supported or not, so we must assume that it is not.
346247dbb3dSsudheer 	 */
34779ec9da8SYuri Pankov 	if ((get_hwenv() & HW_XEN_HVM) == 0 &&
348beed421eSPatrick Mooney 	    is_x86_feature(x86_featureset, X86FSET_TSCP)) {
3492428aad8SPatrick Mooney 		patch_tsc_read(TSC_TSCP);
350beed421eSPatrick Mooney 	} else if (is_x86_feature(x86_featureset, X86FSET_LFENCE_SER)) {
351beed421eSPatrick Mooney 		ASSERT(is_x86_feature(x86_featureset, X86FSET_SSE2));
3522428aad8SPatrick Mooney 		patch_tsc_read(TSC_RDTSC_LFENCE);
353beed421eSPatrick Mooney 	}
354247dbb3dSsudheer 
355247dbb3dSsudheer #endif	/* !__xpv */
3567c478bd9Sstevel@tonic-gate 
357843e1988Sjohnlev 
35886ef0a63SRichard Lowe #if !defined(__xpv)
35922cc0e45SBill Holler 	patch_memops(cpuid_getvendor(CPU));
36086ef0a63SRichard Lowe #endif	/* !__xpv */
36122cc0e45SBill Holler 
362843e1988Sjohnlev #if !defined(__xpv)
363843e1988Sjohnlev 	/* XXPV	what, if anything, should be dorked with here under xen? */
364ae115bc7Smrj 
365ae115bc7Smrj 	/*
366ae115bc7Smrj 	 * While we're thinking about the TSC, let's set up %cr4 so that
367ae115bc7Smrj 	 * userland can issue rdtsc, and initialize the TSC_AUX value
368ae115bc7Smrj 	 * (the cpuid) for the rdtscp instruction on appropriately
369ae115bc7Smrj 	 * capable hardware.
370ae115bc7Smrj 	 */
3717417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_TSC))
372ae115bc7Smrj 		setcr4(getcr4() & ~CR4_TSD);
373ae115bc7Smrj 
3747417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_TSCP))
375ae115bc7Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, 0);
376ae115bc7Smrj 
377799823bbSRobert Mustacchi 	/*
3783ce2fcdcSRobert Mustacchi 	 * Let's get the other %cr4 stuff while we're here. Note, we defer
3793ce2fcdcSRobert Mustacchi 	 * enabling CR4_SMAP until startup_end(); however, that's importantly
3803ce2fcdcSRobert Mustacchi 	 * before we start other CPUs. That ensures that it will be synced out
3813ce2fcdcSRobert Mustacchi 	 * to other CPUs.
382799823bbSRobert Mustacchi 	 */
3837417cfdeSKuriakose Kuruvilla 	if (is_x86_feature(x86_featureset, X86FSET_DE))
384ae115bc7Smrj 		setcr4(getcr4() | CR4_DE);
385799823bbSRobert Mustacchi 
386799823bbSRobert Mustacchi 	if (is_x86_feature(x86_featureset, X86FSET_SMEP))
387799823bbSRobert Mustacchi 		setcr4(getcr4() | CR4_SMEP);
388843e1988Sjohnlev #endif /* __xpv */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
392ae115bc7Smrj 	 * Initialize thread/cpu microstate accounting
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	init_mstate(&t0, LMS_SYSTEM);
3957c478bd9Sstevel@tonic-gate 	init_cpu_mstate(CPU, CMS_SYSTEM);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * Initialize lists of available and active CPUs.
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	cpu_list_init(CPU);
4017c478bd9Sstevel@tonic-gate 
4020e751525SEric Saxe 	pg_cpu_bootstrap(CPU);
4030e751525SEric Saxe 
404ae115bc7Smrj 	/*
405ae115bc7Smrj 	 * Now that we have taken over the GDT, IDT and have initialized
406ae115bc7Smrj 	 * active CPU list it's time to inform kmdb if present.
407ae115bc7Smrj 	 */
408ae115bc7Smrj 	if (boothowto & RB_DEBUG)
409ae115bc7Smrj 		kdi_idt_sync();
410ae115bc7Smrj 
411c66b891eSToomas Soome 	if (BOP_GETPROPLEN(bootops, "efi-systab") < 0) {
412ae115bc7Smrj 		/*
413c66b891eSToomas Soome 		 * In BIOS system, explicitly set console to text mode (0x3)
414c66b891eSToomas Soome 		 * if this is a boot post Fast Reboot, and the console is set
415c66b891eSToomas Soome 		 * to CONS_SCREEN_TEXT.
4169db7147eSSherry Moore 		 */
417c66b891eSToomas Soome 		if (post_fastreboot &&
418c66b891eSToomas Soome 		    boot_console_type(NULL) == CONS_SCREEN_TEXT) {
4199db7147eSSherry Moore 			set_console_mode(0x3);
420c66b891eSToomas Soome 		}
421c66b891eSToomas Soome 	}
4229db7147eSSherry Moore 
4239db7147eSSherry Moore 	/*
424ae115bc7Smrj 	 * If requested (boot -d) drop into kmdb.
425ae115bc7Smrj 	 *
426ae115bc7Smrj 	 * This must be done after cpu_list_init() on the 64-bit kernel
427ae115bc7Smrj 	 * since taking a trap requires that we re-compute gsbase based
428ae115bc7Smrj 	 * on the cpu list.
429ae115bc7Smrj 	 */
430ae115bc7Smrj 	if (boothowto & RB_DEBUGENTER)
431ae115bc7Smrj 		kmdb_enter();
432ae115bc7Smrj 
433affbd3ccSkchow 	cpu_vm_data_init(CPU);
434affbd3ccSkchow 
4357c478bd9Sstevel@tonic-gate 	rp->r_fp = 0;	/* terminate kernel stack traces! */
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	prom_init("kernel", (void *)NULL);
4387c478bd9Sstevel@tonic-gate 
439a3114836SGerry Liu 	/* User-set option overrides firmware value. */
440a3114836SGerry Liu 	if (bootprop_getval(PLAT_DR_OPTIONS_NAME, &prop_value) == 0) {
441a3114836SGerry Liu 		plat_dr_options = (uint64_t)prop_value;
442a3114836SGerry Liu 	}
443a3114836SGerry Liu #if defined(__xpv)
444a3114836SGerry Liu 	/* No support of DR operations on xpv */
445a3114836SGerry Liu 	plat_dr_options = 0;
446a3114836SGerry Liu #else	/* __xpv */
447a3114836SGerry Liu 	/* Flag PLAT_DR_FEATURE_ENABLED should only be set by DR driver. */
448a3114836SGerry Liu 	plat_dr_options &= ~PLAT_DR_FEATURE_ENABLED;
449a3114836SGerry Liu #endif	/* __xpv */
450a3114836SGerry Liu 
451a3114836SGerry Liu 	/*
452a3114836SGerry Liu 	 * Get value of "plat_dr_physmax" boot option.
453a3114836SGerry Liu 	 * It overrides values calculated from MSCT or SRAT table.
454a3114836SGerry Liu 	 */
455a3114836SGerry Liu 	if (bootprop_getval(PLAT_DR_PHYSMAX_NAME, &prop_value) == 0) {
456a3114836SGerry Liu 		plat_dr_physmax = ((uint64_t)prop_value) >> PAGESHIFT;
457a3114836SGerry Liu 	}
458a3114836SGerry Liu 
459a3114836SGerry Liu 	/* Get value of boot_ncpus. */
460a3114836SGerry Liu 	if (bootprop_getval(BOOT_NCPUS_NAME, &prop_value) != 0) {
4612baa66a0SJonathan Chew 		boot_ncpus = NCPU;
462a3114836SGerry Liu 	} else {
4632baa66a0SJonathan Chew 		boot_ncpus = (int)prop_value;
46441791439Sandrei 		if (boot_ncpus <= 0 || boot_ncpus > NCPU)
465ae115bc7Smrj 			boot_ncpus = NCPU;
4662baa66a0SJonathan Chew 	}
46741791439Sandrei 
468a3114836SGerry Liu 	/*
469a3114836SGerry Liu 	 * Set max_ncpus and boot_max_ncpus to boot_ncpus if platform doesn't
470a3114836SGerry Liu 	 * support CPU DR operations.
471a3114836SGerry Liu 	 */
472a3114836SGerry Liu 	if (plat_dr_support_cpu() == 0) {
47341791439Sandrei 		max_ncpus = boot_max_ncpus = boot_ncpus;
474a3114836SGerry Liu 	} else {
475a3114836SGerry Liu 		if (bootprop_getval(PLAT_MAX_NCPUS_NAME, &prop_value) != 0) {
476a3114836SGerry Liu 			max_ncpus = NCPU;
477a3114836SGerry Liu 		} else {
478a3114836SGerry Liu 			max_ncpus = (int)prop_value;
479a3114836SGerry Liu 			if (max_ncpus <= 0 || max_ncpus > NCPU) {
480a3114836SGerry Liu 				max_ncpus = NCPU;
481a3114836SGerry Liu 			}
482a3114836SGerry Liu 			if (boot_ncpus > max_ncpus) {
483a3114836SGerry Liu 				boot_ncpus = max_ncpus;
484a3114836SGerry Liu 			}
485a3114836SGerry Liu 		}
486a3114836SGerry Liu 
487a3114836SGerry Liu 		if (bootprop_getval(BOOT_MAX_NCPUS_NAME, &prop_value) != 0) {
488a3114836SGerry Liu 			boot_max_ncpus = boot_ncpus;
489a3114836SGerry Liu 		} else {
490a3114836SGerry Liu 			boot_max_ncpus = (int)prop_value;
491a3114836SGerry Liu 			if (boot_max_ncpus <= 0 || boot_max_ncpus > NCPU) {
492a3114836SGerry Liu 				boot_max_ncpus = boot_ncpus;
493a3114836SGerry Liu 			} else if (boot_max_ncpus > max_ncpus) {
494a3114836SGerry Liu 				boot_max_ncpus = max_ncpus;
495a3114836SGerry Liu 			}
496a3114836SGerry Liu 		}
497a3114836SGerry Liu 	}
49841791439Sandrei 
4992e2c009bSjjc 	/*
5002e2c009bSjjc 	 * Initialize the lgrp framework
5012e2c009bSjjc 	 */
502d5d7cf4eSJonathan Chew 	lgrp_init(LGRP_INIT_STAGE1);
5032e2c009bSjjc 
5047c478bd9Sstevel@tonic-gate 	if (boothowto & RB_HALT) {
5057c478bd9Sstevel@tonic-gate 		prom_printf("unix: kernel halted by -h flag\n");
5067c478bd9Sstevel@tonic-gate 		prom_enter_mon();
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (workaround_errata(CPU) != 0)
5127c478bd9Sstevel@tonic-gate 		panic("critical workaround(s) missing for boot cpu");
5137c478bd9Sstevel@tonic-gate }
514986fd29aSsetje 
515986fd29aSsetje 
516986fd29aSsetje void
mach_modpath(char * path,const char * filename)517986fd29aSsetje mach_modpath(char *path, const char *filename)
518986fd29aSsetje {
519986fd29aSsetje 	/*
520986fd29aSsetje 	 * Construct the directory path from the filename.
521986fd29aSsetje 	 */
522986fd29aSsetje 
523986fd29aSsetje 	int len;
524986fd29aSsetje 	char *p;
525986fd29aSsetje 	const char isastr[] = "/amd64";
526986fd29aSsetje 	size_t isalen = strlen(isastr);
527986fd29aSsetje 
5280181461bSKeith M Wesolowski 	len = strlen(SYSTEM_BOOT_PATH "/kernel");
5290181461bSKeith M Wesolowski 	(void) strcpy(path, SYSTEM_BOOT_PATH "/kernel ");
5300181461bSKeith M Wesolowski 	path += len + 1;
5310181461bSKeith M Wesolowski 
532986fd29aSsetje 	if ((p = strrchr(filename, '/')) == NULL)
533986fd29aSsetje 		return;
534986fd29aSsetje 
535986fd29aSsetje 	while (p > filename && *(p - 1) == '/')
536986fd29aSsetje 		p--;	/* remove trailing '/' characters */
537986fd29aSsetje 	if (p == filename)
538986fd29aSsetje 		p++;	/* so "/" -is- the modpath in this case */
539986fd29aSsetje 
540986fd29aSsetje 	/*
541986fd29aSsetje 	 * Remove optional isa-dependent directory name - the module
542986fd29aSsetje 	 * subsystem will put this back again (!)
543986fd29aSsetje 	 */
544986fd29aSsetje 	len = p - filename;
545986fd29aSsetje 	if (len > isalen &&
546986fd29aSsetje 	    strncmp(&filename[len - isalen], isastr, isalen) == 0)
547986fd29aSsetje 		p -= isalen;
548986fd29aSsetje 
549986fd29aSsetje 	/*
550986fd29aSsetje 	 * "/platform/mumblefrotz" + " " + MOD_DEFPATH
551986fd29aSsetje 	 */
552986fd29aSsetje 	len += (p - filename) + 1 + strlen(MOD_DEFPATH) + 1;
553986fd29aSsetje 	(void) strncpy(path, filename, p - filename);
554986fd29aSsetje }
555