xref: /illumos-gate/usr/src/uts/i86pc/os/mlsetup.c (revision 1b09309c5ebed5c0bf14e2b396bf626c5aa30034)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2012 Gary Mills
23  *
24  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2011 by Delphix. All rights reserved.
26  * Copyright 2019 Joyent, Inc.
27  * Copyright 2020 Oxide Computer Company
28  */
29 /*
30  * Copyright (c) 2010, Intel Corporation.
31  * All rights reserved.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/sysmacros.h>
36 #include <sys/disp.h>
37 #include <sys/promif.h>
38 #include <sys/clock.h>
39 #include <sys/cpuvar.h>
40 #include <sys/stack.h>
41 #include <vm/as.h>
42 #include <vm/hat.h>
43 #include <sys/reboot.h>
44 #include <sys/avintr.h>
45 #include <sys/vtrace.h>
46 #include <sys/proc.h>
47 #include <sys/thread.h>
48 #include <sys/cpupart.h>
49 #include <sys/pset.h>
50 #include <sys/copyops.h>
51 #include <sys/pg.h>
52 #include <sys/disp.h>
53 #include <sys/debug.h>
54 #include <sys/sunddi.h>
55 #include <sys/x86_archext.h>
56 #include <sys/privregs.h>
57 #include <sys/machsystm.h>
58 #include <sys/ontrap.h>
59 #include <sys/bootconf.h>
60 #include <sys/boot_console.h>
61 #include <sys/kdi_machimpl.h>
62 #include <sys/archsystm.h>
63 #include <sys/promif.h>
64 #include <sys/pci_cfgspace.h>
65 #include <sys/apic.h>
66 #include <sys/apic_common.h>
67 #include <sys/bootvfs.h>
68 #include <sys/tsc.h>
69 #include <sys/smt.h>
70 #ifdef __xpv
71 #include <sys/hypervisor.h>
72 #else
73 #include <sys/xpv_support.h>
74 #endif
75 
76 /*
77  * some globals for patching the result of cpuid
78  * to solve problems w/ creative cpu vendors
79  */
80 
81 extern uint32_t cpuid_feature_ecx_include;
82 extern uint32_t cpuid_feature_ecx_exclude;
83 extern uint32_t cpuid_feature_edx_include;
84 extern uint32_t cpuid_feature_edx_exclude;
85 
86 nmi_action_t nmi_action = NMI_ACTION_UNSET;
87 
88 /*
89  * Set console mode
90  */
91 static void
92 set_console_mode(uint8_t val)
93 {
94 	struct bop_regs rp = {0};
95 
96 	rp.eax.byte.ah = 0x0;
97 	rp.eax.byte.al = val;
98 	rp.ebx.word.bx = 0x0;
99 
100 	BOP_DOINT(bootops, 0x10, &rp);
101 }
102 
103 
104 /*
105  * Setup routine called right before main(). Interposing this function
106  * before main() allows us to call it in a machine-independent fashion.
107  */
108 void
109 mlsetup(struct regs *rp)
110 {
111 	u_longlong_t prop_value;
112 	char prop_str[BP_MAX_STRLEN];
113 	extern struct classfuncs sys_classfuncs;
114 	extern disp_t cpu0_disp;
115 	extern char t0stack[];
116 	extern int post_fastreboot;
117 	extern uint64_t plat_dr_options;
118 
119 	ASSERT_STACK_ALIGNED();
120 
121 	/*
122 	 * initialize cpu_self
123 	 */
124 	cpu[0]->cpu_self = cpu[0];
125 
126 #if defined(__xpv)
127 	/*
128 	 * Point at the hypervisor's virtual cpu structure
129 	 */
130 	cpu[0]->cpu_m.mcpu_vcpu_info = &HYPERVISOR_shared_info->vcpu_info[0];
131 #endif
132 
133 	/*
134 	 * check if we've got special bits to clear or set
135 	 * when checking cpu features
136 	 */
137 
138 	if (bootprop_getval("cpuid_feature_ecx_include", &prop_value) != 0)
139 		cpuid_feature_ecx_include = 0;
140 	else
141 		cpuid_feature_ecx_include = (uint32_t)prop_value;
142 
143 	if (bootprop_getval("cpuid_feature_ecx_exclude", &prop_value) != 0)
144 		cpuid_feature_ecx_exclude = 0;
145 	else
146 		cpuid_feature_ecx_exclude = (uint32_t)prop_value;
147 
148 	if (bootprop_getval("cpuid_feature_edx_include", &prop_value) != 0)
149 		cpuid_feature_edx_include = 0;
150 	else
151 		cpuid_feature_edx_include = (uint32_t)prop_value;
152 
153 	if (bootprop_getval("cpuid_feature_edx_exclude", &prop_value) != 0)
154 		cpuid_feature_edx_exclude = 0;
155 	else
156 		cpuid_feature_edx_exclude = (uint32_t)prop_value;
157 
158 #if !defined(__xpv)
159 	if (bootprop_getstr("nmi", prop_str, sizeof (prop_str)) == 0) {
160 		if (strcmp(prop_str, "ignore") == 0) {
161 			nmi_action = NMI_ACTION_IGNORE;
162 		} else if (strcmp(prop_str, "panic") == 0) {
163 			nmi_action = NMI_ACTION_PANIC;
164 		} else if (strcmp(prop_str, "kmdb") == 0) {
165 			nmi_action = NMI_ACTION_KMDB;
166 		} else {
167 			prom_printf("unix: ignoring unknown nmi=%s\n",
168 			    prop_str);
169 		}
170 	}
171 
172 	/*
173 	 * Check to see if KPTI has been explicitly enabled or disabled.
174 	 * We have to check this before init_desctbls().
175 	 */
176 	if (bootprop_getval("kpti", &prop_value) == 0) {
177 		kpti_enable = (uint64_t)(prop_value == 1);
178 		prom_printf("unix: forcing kpti to %s due to boot argument\n",
179 		    (kpti_enable == 1) ? "ON" : "OFF");
180 	} else {
181 		kpti_enable = 1;
182 	}
183 
184 	if (bootprop_getval("pcid", &prop_value) == 0 && prop_value == 0) {
185 		prom_printf("unix: forcing pcid to OFF due to boot argument\n");
186 		x86_use_pcid = 0;
187 	} else if (kpti_enable != 1) {
188 		x86_use_pcid = 0;
189 	}
190 
191 	/*
192 	 * While we don't need to check this until later, we might as well do it
193 	 * here.
194 	 */
195 	if (bootprop_getstr("smt_enabled", prop_str, sizeof (prop_str)) == 0) {
196 		if (strcasecmp(prop_str, "false") == 0 ||
197 		    strcmp(prop_str, "0") == 0)
198 			smt_boot_disable = 1;
199 	}
200 
201 #endif
202 
203 	/*
204 	 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
205 	 */
206 	init_desctbls();
207 
208 	/*
209 	 * lgrp_init() and possibly cpuid_pass1() need PCI config
210 	 * space access
211 	 */
212 #if defined(__xpv)
213 	if (DOMAIN_IS_INITDOMAIN(xen_info))
214 		pci_cfgspace_init();
215 #else
216 	pci_cfgspace_init();
217 	/*
218 	 * Initialize the platform type from CPU 0 to ensure that
219 	 * determine_platform() is only ever called once.
220 	 */
221 	determine_platform();
222 #endif
223 
224 	/*
225 	 * The first lightweight pass (pass0) through the cpuid data
226 	 * was done in locore before mlsetup was called.  Do the next
227 	 * pass in C code.
228 	 *
229 	 * The x86_featureset is initialized here based on the capabilities
230 	 * of the boot CPU.  Note that if we choose to support CPUs that have
231 	 * different feature sets (at which point we would almost certainly
232 	 * want to set the feature bits to correspond to the feature
233 	 * minimum) this value may be altered.
234 	 */
235 	cpuid_pass1(cpu[0], x86_featureset);
236 
237 #if !defined(__xpv)
238 	if ((get_hwenv() & HW_XEN_HVM) != 0)
239 		xen_hvm_init();
240 
241 	/*
242 	 * Before we do anything with the TSCs, we need to work around
243 	 * Intel erratum BT81.  On some CPUs, warm reset does not
244 	 * clear the TSC.  If we are on such a CPU, we will clear TSC ourselves
245 	 * here.  Other CPUs will clear it when we boot them later, and the
246 	 * resulting skew will be handled by tsc_sync_master()/_slave();
247 	 * note that such skew already exists and has to be handled anyway.
248 	 *
249 	 * We do this only on metal.  This same problem can occur with a
250 	 * hypervisor that does not happen to virtualise a TSC that starts from
251 	 * zero, regardless of CPU type; however, we do not expect hypervisors
252 	 * that do not virtualise TSC that way to handle writes to TSC
253 	 * correctly, either.
254 	 */
255 	if (get_hwenv() == HW_NATIVE &&
256 	    cpuid_getvendor(CPU) == X86_VENDOR_Intel &&
257 	    cpuid_getfamily(CPU) == 6 &&
258 	    (cpuid_getmodel(CPU) == 0x2d || cpuid_getmodel(CPU) == 0x3e) &&
259 	    is_x86_feature(x86_featureset, X86FSET_TSC)) {
260 		(void) wrmsr(REG_TSC, 0UL);
261 	}
262 
263 	/*
264 	 * Patch the tsc_read routine with appropriate set of instructions,
265 	 * depending on the processor family and architecure, to read the
266 	 * time-stamp counter while ensuring no out-of-order execution.
267 	 * Patch it while the kernel text is still writable.
268 	 *
269 	 * The Xen hypervisor does not correctly report whether rdtscp is
270 	 * supported or not, so we must assume that it is not.
271 	 */
272 	if ((get_hwenv() & HW_XEN_HVM) == 0 &&
273 	    is_x86_feature(x86_featureset, X86FSET_TSCP)) {
274 		patch_tsc_read(TSC_TSCP);
275 	} else if (is_x86_feature(x86_featureset, X86FSET_LFENCE_SER)) {
276 		ASSERT(is_x86_feature(x86_featureset, X86FSET_SSE2));
277 		patch_tsc_read(TSC_RDTSC_LFENCE);
278 	}
279 
280 #endif	/* !__xpv */
281 
282 
283 #if !defined(__xpv)
284 	patch_memops(cpuid_getvendor(CPU));
285 #endif	/* !__xpv */
286 
287 #if !defined(__xpv)
288 	/* XXPV	what, if anything, should be dorked with here under xen? */
289 
290 	/*
291 	 * While we're thinking about the TSC, let's set up %cr4 so that
292 	 * userland can issue rdtsc, and initialize the TSC_AUX value
293 	 * (the cpuid) for the rdtscp instruction on appropriately
294 	 * capable hardware.
295 	 */
296 	if (is_x86_feature(x86_featureset, X86FSET_TSC))
297 		setcr4(getcr4() & ~CR4_TSD);
298 
299 	if (is_x86_feature(x86_featureset, X86FSET_TSCP))
300 		(void) wrmsr(MSR_AMD_TSCAUX, 0);
301 
302 	/*
303 	 * Let's get the other %cr4 stuff while we're here. Note, we defer
304 	 * enabling CR4_SMAP until startup_end(); however, that's importantly
305 	 * before we start other CPUs. That ensures that it will be synced out
306 	 * to other CPUs.
307 	 */
308 	if (is_x86_feature(x86_featureset, X86FSET_DE))
309 		setcr4(getcr4() | CR4_DE);
310 
311 	if (is_x86_feature(x86_featureset, X86FSET_SMEP))
312 		setcr4(getcr4() | CR4_SMEP);
313 #endif /* __xpv */
314 
315 	/*
316 	 * initialize t0
317 	 */
318 	t0.t_stk = (caddr_t)rp - MINFRAME;
319 	t0.t_stkbase = t0stack;
320 	t0.t_pri = maxclsyspri - 3;
321 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
322 	t0.t_procp = &p0;
323 	t0.t_plockp = &p0lock.pl_lock;
324 	t0.t_lwp = &lwp0;
325 	t0.t_forw = &t0;
326 	t0.t_back = &t0;
327 	t0.t_next = &t0;
328 	t0.t_prev = &t0;
329 	t0.t_cpu = cpu[0];
330 	t0.t_disp_queue = &cpu0_disp;
331 	t0.t_bind_cpu = PBIND_NONE;
332 	t0.t_bind_pset = PS_NONE;
333 	t0.t_bindflag = (uchar_t)default_binding_mode;
334 	t0.t_cpupart = &cp_default;
335 	t0.t_clfuncs = &sys_classfuncs.thread;
336 	t0.t_copyops = NULL;
337 	THREAD_ONPROC(&t0, CPU);
338 
339 	lwp0.lwp_thread = &t0;
340 	lwp0.lwp_regs = (void *)rp;
341 	lwp0.lwp_procp = &p0;
342 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
343 
344 	p0.p_exec = NULL;
345 	p0.p_stat = SRUN;
346 	p0.p_flag = SSYS;
347 	p0.p_tlist = &t0;
348 	p0.p_stksize = 2*PAGESIZE;
349 	p0.p_stkpageszc = 0;
350 	p0.p_as = &kas;
351 	p0.p_lockp = &p0lock;
352 	p0.p_brkpageszc = 0;
353 	p0.p_t1_lgrpid = LGRP_NONE;
354 	p0.p_tr_lgrpid = LGRP_NONE;
355 	psecflags_default(&p0.p_secflags);
356 
357 	sigorset(&p0.p_ignore, &ignoredefault);
358 
359 	CPU->cpu_thread = &t0;
360 	bzero(&cpu0_disp, sizeof (disp_t));
361 	CPU->cpu_disp = &cpu0_disp;
362 	CPU->cpu_disp->disp_cpu = CPU;
363 	CPU->cpu_dispthread = &t0;
364 	CPU->cpu_idle_thread = &t0;
365 	CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
366 	CPU->cpu_dispatch_pri = t0.t_pri;
367 
368 	CPU->cpu_id = 0;
369 
370 	CPU->cpu_pri = 12;		/* initial PIL for the boot CPU */
371 
372 	/*
373 	 * Initialize thread/cpu microstate accounting
374 	 */
375 	init_mstate(&t0, LMS_SYSTEM);
376 	init_cpu_mstate(CPU, CMS_SYSTEM);
377 
378 	/*
379 	 * Initialize lists of available and active CPUs.
380 	 */
381 	cpu_list_init(CPU);
382 
383 	pg_cpu_bootstrap(CPU);
384 
385 	/*
386 	 * Now that we have taken over the GDT, IDT and have initialized
387 	 * active CPU list it's time to inform kmdb if present.
388 	 */
389 	if (boothowto & RB_DEBUG)
390 		kdi_idt_sync();
391 
392 	if (BOP_GETPROPLEN(bootops, "efi-systab") < 0) {
393 		/*
394 		 * In BIOS system, explicitly set console to text mode (0x3)
395 		 * if this is a boot post Fast Reboot, and the console is set
396 		 * to CONS_SCREEN_TEXT.
397 		 */
398 		if (post_fastreboot &&
399 		    boot_console_type(NULL) == CONS_SCREEN_TEXT) {
400 			set_console_mode(0x3);
401 		}
402 	}
403 
404 	/*
405 	 * If requested (boot -d) drop into kmdb.
406 	 *
407 	 * This must be done after cpu_list_init() on the 64-bit kernel
408 	 * since taking a trap requires that we re-compute gsbase based
409 	 * on the cpu list.
410 	 */
411 	if (boothowto & RB_DEBUGENTER)
412 		kmdb_enter();
413 
414 	cpu_vm_data_init(CPU);
415 
416 	rp->r_fp = 0;	/* terminate kernel stack traces! */
417 
418 	prom_init("kernel", (void *)NULL);
419 
420 	/* User-set option overrides firmware value. */
421 	if (bootprop_getval(PLAT_DR_OPTIONS_NAME, &prop_value) == 0) {
422 		plat_dr_options = (uint64_t)prop_value;
423 	}
424 #if defined(__xpv)
425 	/* No support of DR operations on xpv */
426 	plat_dr_options = 0;
427 #else	/* __xpv */
428 	/* Flag PLAT_DR_FEATURE_ENABLED should only be set by DR driver. */
429 	plat_dr_options &= ~PLAT_DR_FEATURE_ENABLED;
430 #endif	/* __xpv */
431 
432 	/*
433 	 * Get value of "plat_dr_physmax" boot option.
434 	 * It overrides values calculated from MSCT or SRAT table.
435 	 */
436 	if (bootprop_getval(PLAT_DR_PHYSMAX_NAME, &prop_value) == 0) {
437 		plat_dr_physmax = ((uint64_t)prop_value) >> PAGESHIFT;
438 	}
439 
440 	/* Get value of boot_ncpus. */
441 	if (bootprop_getval(BOOT_NCPUS_NAME, &prop_value) != 0) {
442 		boot_ncpus = NCPU;
443 	} else {
444 		boot_ncpus = (int)prop_value;
445 		if (boot_ncpus <= 0 || boot_ncpus > NCPU)
446 			boot_ncpus = NCPU;
447 	}
448 
449 	/*
450 	 * Set max_ncpus and boot_max_ncpus to boot_ncpus if platform doesn't
451 	 * support CPU DR operations.
452 	 */
453 	if (plat_dr_support_cpu() == 0) {
454 		max_ncpus = boot_max_ncpus = boot_ncpus;
455 	} else {
456 		if (bootprop_getval(PLAT_MAX_NCPUS_NAME, &prop_value) != 0) {
457 			max_ncpus = NCPU;
458 		} else {
459 			max_ncpus = (int)prop_value;
460 			if (max_ncpus <= 0 || max_ncpus > NCPU) {
461 				max_ncpus = NCPU;
462 			}
463 			if (boot_ncpus > max_ncpus) {
464 				boot_ncpus = max_ncpus;
465 			}
466 		}
467 
468 		if (bootprop_getval(BOOT_MAX_NCPUS_NAME, &prop_value) != 0) {
469 			boot_max_ncpus = boot_ncpus;
470 		} else {
471 			boot_max_ncpus = (int)prop_value;
472 			if (boot_max_ncpus <= 0 || boot_max_ncpus > NCPU) {
473 				boot_max_ncpus = boot_ncpus;
474 			} else if (boot_max_ncpus > max_ncpus) {
475 				boot_max_ncpus = max_ncpus;
476 			}
477 		}
478 	}
479 
480 	/*
481 	 * Initialize the lgrp framework
482 	 */
483 	lgrp_init(LGRP_INIT_STAGE1);
484 
485 	if (boothowto & RB_HALT) {
486 		prom_printf("unix: kernel halted by -h flag\n");
487 		prom_enter_mon();
488 	}
489 
490 	ASSERT_STACK_ALIGNED();
491 
492 	/*
493 	 * Fill out cpu_ucode_info.  Update microcode if necessary.
494 	 */
495 	ucode_check(CPU);
496 	cpuid_pass_ucode(CPU, x86_featureset);
497 
498 	if (workaround_errata(CPU) != 0)
499 		panic("critical workaround(s) missing for boot cpu");
500 }
501 
502 
503 void
504 mach_modpath(char *path, const char *filename)
505 {
506 	/*
507 	 * Construct the directory path from the filename.
508 	 */
509 
510 	int len;
511 	char *p;
512 	const char isastr[] = "/amd64";
513 	size_t isalen = strlen(isastr);
514 
515 	len = strlen(SYSTEM_BOOT_PATH "/kernel");
516 	(void) strcpy(path, SYSTEM_BOOT_PATH "/kernel ");
517 	path += len + 1;
518 
519 	if ((p = strrchr(filename, '/')) == NULL)
520 		return;
521 
522 	while (p > filename && *(p - 1) == '/')
523 		p--;	/* remove trailing '/' characters */
524 	if (p == filename)
525 		p++;	/* so "/" -is- the modpath in this case */
526 
527 	/*
528 	 * Remove optional isa-dependent directory name - the module
529 	 * subsystem will put this back again (!)
530 	 */
531 	len = p - filename;
532 	if (len > isalen &&
533 	    strncmp(&filename[len - isalen], isastr, isalen) == 0)
534 		p -= isalen;
535 
536 	/*
537 	 * "/platform/mumblefrotz" + " " + MOD_DEFPATH
538 	 */
539 	len += (p - filename) + 1 + strlen(MOD_DEFPATH) + 1;
540 	(void) strncpy(path, filename, p - filename);
541 }
542