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.
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.
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]
22 * Copyright (c) 2012 Gary Mills
24 * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 * Copyright 2016 Joyent, Inc.
29 * Copyright (c) 2010, Intel Corporation.
30 * All rights reserved.
33 #include <sys/types.h>
34 #include <sys/sysmacros.h>
36 #include <sys/promif.h>
37 #include <sys/clock.h>
38 #include <sys/cpuvar.h>
39 #include <sys/stack.h>
42 #include <sys/reboot.h>
43 #include <sys/avintr.h>
44 #include <sys/vtrace.h>
46 #include <sys/thread.h>
47 #include <sys/cpupart.h>
49 #include <sys/copyops.h>
52 #include <sys/debug.h>
53 #include <sys/sunddi.h>
54 #include <sys/x86_archext.h>
55 #include <sys/privregs.h>
56 #include <sys/machsystm.h>
57 #include <sys/ontrap.h>
58 #include <sys/bootconf.h>
59 #include <sys/boot_console.h>
60 #include <sys/kdi_machimpl.h>
61 #include <sys/archsystm.h>
62 #include <sys/promif.h>
63 #include <sys/pci_cfgspace.h>
64 #include <sys/bootvfs.h>
67 #include <sys/hypervisor.h>
69 #include <sys/xpv_support.h>
73 * some globals for patching the result of cpuid
74 * to solve problems w/ creative cpu vendors
77 extern uint32_t cpuid_feature_ecx_include
;
78 extern uint32_t cpuid_feature_ecx_exclude
;
79 extern uint32_t cpuid_feature_edx_include
;
80 extern uint32_t cpuid_feature_edx_exclude
;
86 set_console_mode(uint8_t val
)
88 struct bop_regs rp
= {0};
94 BOP_DOINT(bootops
, 0x10, &rp
);
99 * Setup routine called right before main(). Interposing this function
100 * before main() allows us to call it in a machine-independent fashion.
103 mlsetup(struct regs
*rp
)
105 u_longlong_t prop_value
;
106 extern struct classfuncs sys_classfuncs
;
107 extern disp_t cpu0_disp
;
108 extern char t0stack
[];
109 extern int post_fastreboot
;
110 extern uint64_t plat_dr_options
;
112 ASSERT_STACK_ALIGNED();
115 * initialize cpu_self
117 cpu
[0]->cpu_self
= cpu
[0];
121 * Point at the hypervisor's virtual cpu structure
123 cpu
[0]->cpu_m
.mcpu_vcpu_info
= &HYPERVISOR_shared_info
->vcpu_info
[0];
127 * check if we've got special bits to clear or set
128 * when checking cpu features
131 if (bootprop_getval("cpuid_feature_ecx_include", &prop_value
) != 0)
132 cpuid_feature_ecx_include
= 0;
134 cpuid_feature_ecx_include
= (uint32_t)prop_value
;
136 if (bootprop_getval("cpuid_feature_ecx_exclude", &prop_value
) != 0)
137 cpuid_feature_ecx_exclude
= 0;
139 cpuid_feature_ecx_exclude
= (uint32_t)prop_value
;
141 if (bootprop_getval("cpuid_feature_edx_include", &prop_value
) != 0)
142 cpuid_feature_edx_include
= 0;
144 cpuid_feature_edx_include
= (uint32_t)prop_value
;
146 if (bootprop_getval("cpuid_feature_edx_exclude", &prop_value
) != 0)
147 cpuid_feature_edx_exclude
= 0;
149 cpuid_feature_edx_exclude
= (uint32_t)prop_value
;
152 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
157 * lgrp_init() and possibly cpuid_pass1() need PCI config
161 if (DOMAIN_IS_INITDOMAIN(xen_info
))
166 * Initialize the platform type from CPU 0 to ensure that
167 * determine_platform() is only ever called once.
169 determine_platform();
173 * The first lightweight pass (pass0) through the cpuid data
174 * was done in locore before mlsetup was called. Do the next
177 * The x86_featureset is initialized here based on the capabilities
178 * of the boot CPU. Note that if we choose to support CPUs that have
179 * different feature sets (at which point we would almost certainly
180 * want to set the feature bits to correspond to the feature
181 * minimum) this value may be altered.
183 cpuid_pass1(cpu
[0], x86_featureset
);
186 if ((get_hwenv() & HW_XEN_HVM
) != 0)
190 * Before we do anything with the TSCs, we need to work around
191 * Intel erratum BT81. On some CPUs, warm reset does not
192 * clear the TSC. If we are on such a CPU, we will clear TSC ourselves
193 * here. Other CPUs will clear it when we boot them later, and the
194 * resulting skew will be handled by tsc_sync_master()/_slave();
195 * note that such skew already exists and has to be handled anyway.
197 * We do this only on metal. This same problem can occur with a
198 * hypervisor that does not happen to virtualise a TSC that starts from
199 * zero, regardless of CPU type; however, we do not expect hypervisors
200 * that do not virtualise TSC that way to handle writes to TSC
203 if (get_hwenv() == HW_NATIVE
&&
204 cpuid_getvendor(CPU
) == X86_VENDOR_Intel
&&
205 cpuid_getfamily(CPU
) == 6 &&
206 (cpuid_getmodel(CPU
) == 0x2d || cpuid_getmodel(CPU
) == 0x3e) &&
207 is_x86_feature(x86_featureset
, X86FSET_TSC
)) {
208 (void) wrmsr(REG_TSC
, 0UL);
212 * Patch the tsc_read routine with appropriate set of instructions,
213 * depending on the processor family and architecure, to read the
214 * time-stamp counter while ensuring no out-of-order execution.
215 * Patch it while the kernel text is still writable.
217 * Note: tsc_read is not patched for intel processors whose family
218 * is >6 and for amd whose family >f (in case they don't support rdtscp
219 * instruction, unlikely). By default tsc_read will use cpuid for
220 * serialization in such cases. The following code needs to be
221 * revisited if intel processors of family >= f retains the
222 * instruction serialization nature of mfence instruction.
223 * Note: tsc_read is not patched for x86 processors which do
224 * not support "mfence". By default tsc_read will use cpuid for
225 * serialization in such cases.
227 * The Xen hypervisor does not correctly report whether rdtscp is
228 * supported or not, so we must assume that it is not.
230 if ((get_hwenv() & HW_XEN_HVM
) == 0 &&
231 is_x86_feature(x86_featureset
, X86FSET_TSCP
))
232 patch_tsc_read(TSC_TSCP
);
233 else if (cpuid_getvendor(CPU
) == X86_VENDOR_AMD
&&
234 cpuid_getfamily(CPU
) <= 0xf &&
235 is_x86_feature(x86_featureset
, X86FSET_SSE2
))
236 patch_tsc_read(TSC_RDTSC_MFENCE
);
237 else if (cpuid_getvendor(CPU
) == X86_VENDOR_Intel
&&
238 cpuid_getfamily(CPU
) <= 6 &&
239 is_x86_feature(x86_featureset
, X86FSET_SSE2
))
240 patch_tsc_read(TSC_RDTSC_LFENCE
);
244 #if defined(__i386) && !defined(__xpv)
246 * Some i386 processors do not implement the rdtsc instruction,
247 * or at least they do not implement it correctly. Patch them to
250 if (!is_x86_feature(x86_featureset
, X86FSET_TSC
))
251 patch_tsc_read(TSC_NONE
);
252 #endif /* __i386 && !__xpv */
254 #if defined(__amd64) && !defined(__xpv)
255 patch_memops(cpuid_getvendor(CPU
));
256 #endif /* __amd64 && !__xpv */
259 /* XXPV what, if anything, should be dorked with here under xen? */
262 * While we're thinking about the TSC, let's set up %cr4 so that
263 * userland can issue rdtsc, and initialize the TSC_AUX value
264 * (the cpuid) for the rdtscp instruction on appropriately
267 if (is_x86_feature(x86_featureset
, X86FSET_TSC
))
268 setcr4(getcr4() & ~CR4_TSD
);
270 if (is_x86_feature(x86_featureset
, X86FSET_TSCP
))
271 (void) wrmsr(MSR_AMD_TSCAUX
, 0);
274 * Let's get the other %cr4 stuff while we're here. Note, we defer
275 * enabling CR4_SMAP until startup_end(); however, that's importantly
276 * before we start other CPUs. That ensures that it will be synced out
279 if (is_x86_feature(x86_featureset
, X86FSET_DE
))
280 setcr4(getcr4() | CR4_DE
);
282 if (is_x86_feature(x86_featureset
, X86FSET_SMEP
))
283 setcr4(getcr4() | CR4_SMEP
);
289 t0
.t_stk
= (caddr_t
)rp
- MINFRAME
;
290 t0
.t_stkbase
= t0stack
;
291 t0
.t_pri
= maxclsyspri
- 3;
292 t0
.t_schedflag
= TS_LOAD
| TS_DONT_SWAP
;
294 t0
.t_plockp
= &p0lock
.pl_lock
;
301 t0
.t_disp_queue
= &cpu0_disp
;
302 t0
.t_bind_cpu
= PBIND_NONE
;
303 t0
.t_bind_pset
= PS_NONE
;
304 t0
.t_bindflag
= (uchar_t
)default_binding_mode
;
305 t0
.t_cpupart
= &cp_default
;
306 t0
.t_clfuncs
= &sys_classfuncs
.thread
;
308 THREAD_ONPROC(&t0
, CPU
);
310 lwp0
.lwp_thread
= &t0
;
311 lwp0
.lwp_regs
= (void *)rp
;
312 lwp0
.lwp_procp
= &p0
;
313 t0
.t_tid
= p0
.p_lwpcnt
= p0
.p_lwprcnt
= p0
.p_lwpid
= 1;
319 p0
.p_stksize
= 2*PAGESIZE
;
322 p0
.p_lockp
= &p0lock
;
324 p0
.p_t1_lgrpid
= LGRP_NONE
;
325 p0
.p_tr_lgrpid
= LGRP_NONE
;
326 psecflags_default(&p0
.p_secflags
);
328 sigorset(&p0
.p_ignore
, &ignoredefault
);
330 CPU
->cpu_thread
= &t0
;
331 bzero(&cpu0_disp
, sizeof (disp_t
));
332 CPU
->cpu_disp
= &cpu0_disp
;
333 CPU
->cpu_disp
->disp_cpu
= CPU
;
334 CPU
->cpu_dispthread
= &t0
;
335 CPU
->cpu_idle_thread
= &t0
;
336 CPU
->cpu_flags
= CPU_READY
| CPU_RUNNING
| CPU_EXISTS
| CPU_ENABLE
;
337 CPU
->cpu_dispatch_pri
= t0
.t_pri
;
341 CPU
->cpu_pri
= 12; /* initial PIL for the boot CPU */
344 * The kernel doesn't use LDTs unless a process explicitly requests one.
346 p0
.p_ldt_desc
= null_sdesc
;
349 * Initialize thread/cpu microstate accounting
351 init_mstate(&t0
, LMS_SYSTEM
);
352 init_cpu_mstate(CPU
, CMS_SYSTEM
);
355 * Initialize lists of available and active CPUs.
359 pg_cpu_bootstrap(CPU
);
362 * Now that we have taken over the GDT, IDT and have initialized
363 * active CPU list it's time to inform kmdb if present.
365 if (boothowto
& RB_DEBUG
)
369 * Explicitly set console to text mode (0x3) if this is a boot
370 * post Fast Reboot, and the console is set to CONS_SCREEN_TEXT.
372 if (post_fastreboot
&& boot_console_type(NULL
) == CONS_SCREEN_TEXT
)
373 set_console_mode(0x3);
376 * If requested (boot -d) drop into kmdb.
378 * This must be done after cpu_list_init() on the 64-bit kernel
379 * since taking a trap requires that we re-compute gsbase based
382 if (boothowto
& RB_DEBUGENTER
)
385 cpu_vm_data_init(CPU
);
387 rp
->r_fp
= 0; /* terminate kernel stack traces! */
389 prom_init("kernel", (void *)NULL
);
391 /* User-set option overrides firmware value. */
392 if (bootprop_getval(PLAT_DR_OPTIONS_NAME
, &prop_value
) == 0) {
393 plat_dr_options
= (uint64_t)prop_value
;
396 /* No support of DR operations on xpv */
399 /* Flag PLAT_DR_FEATURE_ENABLED should only be set by DR driver. */
400 plat_dr_options
&= ~PLAT_DR_FEATURE_ENABLED
;
402 /* Only enable CPU/memory DR on 64 bits kernel. */
403 plat_dr_options
&= ~PLAT_DR_FEATURE_MEMORY
;
404 plat_dr_options
&= ~PLAT_DR_FEATURE_CPU
;
409 * Get value of "plat_dr_physmax" boot option.
410 * It overrides values calculated from MSCT or SRAT table.
412 if (bootprop_getval(PLAT_DR_PHYSMAX_NAME
, &prop_value
) == 0) {
413 plat_dr_physmax
= ((uint64_t)prop_value
) >> PAGESHIFT
;
416 /* Get value of boot_ncpus. */
417 if (bootprop_getval(BOOT_NCPUS_NAME
, &prop_value
) != 0) {
420 boot_ncpus
= (int)prop_value
;
421 if (boot_ncpus
<= 0 || boot_ncpus
> NCPU
)
426 * Set max_ncpus and boot_max_ncpus to boot_ncpus if platform doesn't
427 * support CPU DR operations.
429 if (plat_dr_support_cpu() == 0) {
430 max_ncpus
= boot_max_ncpus
= boot_ncpus
;
432 if (bootprop_getval(PLAT_MAX_NCPUS_NAME
, &prop_value
) != 0) {
435 max_ncpus
= (int)prop_value
;
436 if (max_ncpus
<= 0 || max_ncpus
> NCPU
) {
439 if (boot_ncpus
> max_ncpus
) {
440 boot_ncpus
= max_ncpus
;
444 if (bootprop_getval(BOOT_MAX_NCPUS_NAME
, &prop_value
) != 0) {
445 boot_max_ncpus
= boot_ncpus
;
447 boot_max_ncpus
= (int)prop_value
;
448 if (boot_max_ncpus
<= 0 || boot_max_ncpus
> NCPU
) {
449 boot_max_ncpus
= boot_ncpus
;
450 } else if (boot_max_ncpus
> max_ncpus
) {
451 boot_max_ncpus
= max_ncpus
;
457 * Initialize the lgrp framework
459 lgrp_init(LGRP_INIT_STAGE1
);
461 if (boothowto
& RB_HALT
) {
462 prom_printf("unix: kernel halted by -h flag\n");
466 ASSERT_STACK_ALIGNED();
469 * Fill out cpu_ucode_info. Update microcode if necessary.
473 if (workaround_errata(CPU
) != 0)
474 panic("critical workaround(s) missing for boot cpu");
479 mach_modpath(char *path
, const char *filename
)
482 * Construct the directory path from the filename.
487 const char isastr
[] = "/amd64";
488 size_t isalen
= strlen(isastr
);
490 len
= strlen(SYSTEM_BOOT_PATH
"/kernel");
491 (void) strcpy(path
, SYSTEM_BOOT_PATH
"/kernel ");
494 if ((p
= strrchr(filename
, '/')) == NULL
)
497 while (p
> filename
&& *(p
- 1) == '/')
498 p
--; /* remove trailing '/' characters */
500 p
++; /* so "/" -is- the modpath in this case */
503 * Remove optional isa-dependent directory name - the module
504 * subsystem will put this back again (!)
508 strncmp(&filename
[len
- isalen
], isastr
, isalen
) == 0)
512 * "/platform/mumblefrotz" + " " + MOD_DEFPATH
514 len
+= (p
- filename
) + 1 + strlen(MOD_DEFPATH
) + 1;
515 (void) strncpy(path
, filename
, p
- filename
);