xref: /illumos-gate/usr/src/uts/i86pc/os/mlsetup.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
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 /*
22fb2f18f8Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
29*ae115bc7Smrj #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <sys/disp.h>
317c478bd9Sstevel@tonic-gate #include <sys/promif.h>
327c478bd9Sstevel@tonic-gate #include <sys/clock.h>
337c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
347c478bd9Sstevel@tonic-gate #include <sys/stack.h>
357c478bd9Sstevel@tonic-gate #include <vm/as.h>
367c478bd9Sstevel@tonic-gate #include <vm/hat.h>
377c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
387c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
397c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/thread.h>
427c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
437c478bd9Sstevel@tonic-gate #include <sys/pset.h>
447c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
45fb2f18f8Sesaxe #include <sys/pg.h>
467c478bd9Sstevel@tonic-gate #include <sys/disp.h>
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
497c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
507c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
517c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
527c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
537c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
54*ae115bc7Smrj #include <sys/kdi_machimpl.h>
557c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
567c478bd9Sstevel@tonic-gate #include <sys/promif.h>
577c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
587c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
597c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
60c88420b3Sdmick #include <sys/pci_cfgspace.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * some globals for patching the result of cpuid
647c478bd9Sstevel@tonic-gate  * to solve problems w/ creative cpu vendors
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_include;
687c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_exclude;
697c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_include;
707c478bd9Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_exclude;
717c478bd9Sstevel@tonic-gate 
724961a633Sdmick /*
734961a633Sdmick  * Dummy spl priority masks
744961a633Sdmick  */
75*ae115bc7Smrj static unsigned char dummy_cpu_pri[MAXIPL + 1] = {
764961a633Sdmick 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
774961a633Sdmick 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf
784961a633Sdmick };
794961a633Sdmick 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static uint32_t
8241791439Sandrei bootprop_getval(char *name)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	char prop[32];
857c478bd9Sstevel@tonic-gate 	u_longlong_t ll;
867c478bd9Sstevel@tonic-gate 	extern struct bootops *bootops;
877c478bd9Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, name) > sizeof (prop)) ||
887c478bd9Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, name, prop) < 0) ||
897c478bd9Sstevel@tonic-gate 	    (kobj_getvalue(prop, &ll) == -1))
907c478bd9Sstevel@tonic-gate 		return (0);
917c478bd9Sstevel@tonic-gate 	return ((uint32_t)ll);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Setup routine called right before main(). Interposing this function
967c478bd9Sstevel@tonic-gate  * before main() allows us to call it in a machine-independent fashion.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate void
997c478bd9Sstevel@tonic-gate mlsetup(struct regs *rp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
1027c478bd9Sstevel@tonic-gate 	extern disp_t cpu0_disp;
1037c478bd9Sstevel@tonic-gate 	extern char t0stack[];
10441791439Sandrei 	int boot_ncpus;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * initialize cpu_self
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	cpu[0]->cpu_self = cpu[0];
1127c478bd9Sstevel@tonic-gate 
1134961a633Sdmick 	/*
1144961a633Sdmick 	 * Set up dummy cpu_pri_data values till psm spl code is
1154961a633Sdmick 	 * installed.  This allows splx() to work on amd64.
1164961a633Sdmick 	 */
1174961a633Sdmick 
1184961a633Sdmick 	cpu[0]->cpu_pri_data = dummy_cpu_pri;
1194961a633Sdmick 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * check if we've got special bits to clear or set
1227c478bd9Sstevel@tonic-gate 	 * when checking cpu features
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	cpuid_feature_ecx_include =
12641791439Sandrei 	    bootprop_getval("cpuid_feature_ecx_include");
1277c478bd9Sstevel@tonic-gate 	cpuid_feature_ecx_exclude =
12841791439Sandrei 	    bootprop_getval("cpuid_feature_ecx_exclude");
1297c478bd9Sstevel@tonic-gate 	cpuid_feature_edx_include =
13041791439Sandrei 	    bootprop_getval("cpuid_feature_edx_include");
1317c478bd9Sstevel@tonic-gate 	cpuid_feature_edx_exclude =
13241791439Sandrei 	    bootprop_getval("cpuid_feature_edx_exclude");
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * The first lightweight pass (pass0) through the cpuid data
1367c478bd9Sstevel@tonic-gate 	 * was done in locore before mlsetup was called.  Do the next
1377c478bd9Sstevel@tonic-gate 	 * pass in C code.
1387c478bd9Sstevel@tonic-gate 	 *
1397c478bd9Sstevel@tonic-gate 	 * The x86_feature bits are set here on the basis of the capabilities
1407c478bd9Sstevel@tonic-gate 	 * of the boot CPU.  Note that if we choose to support CPUs that have
1417c478bd9Sstevel@tonic-gate 	 * different feature sets (at which point we would almost certainly
1427c478bd9Sstevel@tonic-gate 	 * want to set the feature bits to correspond to the feature
1437c478bd9Sstevel@tonic-gate 	 * minimum) this value may be altered.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	x86_feature = cpuid_pass1(cpu[0]);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
1497c478bd9Sstevel@tonic-gate 	 */
150*ae115bc7Smrj 	init_desctbls();
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
153*ae115bc7Smrj #if defined(__i386)
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * Some i386 processors do not implement the rdtsc instruction,
1567c478bd9Sstevel@tonic-gate 	 * or at least they do not implement it correctly.
1577c478bd9Sstevel@tonic-gate 	 *
1587c478bd9Sstevel@tonic-gate 	 * For those that do, patch in the rdtsc instructions in
1597c478bd9Sstevel@tonic-gate 	 * various parts of the kernel right now while the text is
1607c478bd9Sstevel@tonic-gate 	 * still writable.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	if (x86_feature & X86_TSC)
1637c478bd9Sstevel@tonic-gate 		patch_tsc();
164*ae115bc7Smrj #endif	/* __i386 */
165*ae115bc7Smrj 
166*ae115bc7Smrj 	/*
167*ae115bc7Smrj 	 * While we're thinking about the TSC, let's set up %cr4 so that
168*ae115bc7Smrj 	 * userland can issue rdtsc, and initialize the TSC_AUX value
169*ae115bc7Smrj 	 * (the cpuid) for the rdtscp instruction on appropriately
170*ae115bc7Smrj 	 * capable hardware.
171*ae115bc7Smrj 	 */
172*ae115bc7Smrj 	if (x86_feature & X86_TSC)
173*ae115bc7Smrj 		setcr4(getcr4() & ~CR4_TSD);
174*ae115bc7Smrj 
175*ae115bc7Smrj 	if (x86_feature & X86_TSCP)
176*ae115bc7Smrj 		(void) wrmsr(MSR_AMD_TSCAUX, 0);
177*ae115bc7Smrj 
178*ae115bc7Smrj 	if (x86_feature & X86_DE)
179*ae115bc7Smrj 		setcr4(getcr4() | CR4_DE);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * initialize t0
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	t0.t_stk = (caddr_t)rp - MINFRAME;
1857c478bd9Sstevel@tonic-gate 	t0.t_stkbase = t0stack;
1867c478bd9Sstevel@tonic-gate 	t0.t_pri = maxclsyspri - 3;
1877c478bd9Sstevel@tonic-gate 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
1887c478bd9Sstevel@tonic-gate 	t0.t_procp = &p0;
1897c478bd9Sstevel@tonic-gate 	t0.t_plockp = &p0lock.pl_lock;
1907c478bd9Sstevel@tonic-gate 	t0.t_lwp = &lwp0;
1917c478bd9Sstevel@tonic-gate 	t0.t_forw = &t0;
1927c478bd9Sstevel@tonic-gate 	t0.t_back = &t0;
1937c478bd9Sstevel@tonic-gate 	t0.t_next = &t0;
1947c478bd9Sstevel@tonic-gate 	t0.t_prev = &t0;
1957c478bd9Sstevel@tonic-gate 	t0.t_cpu = cpu[0];
1967c478bd9Sstevel@tonic-gate 	t0.t_disp_queue = &cpu0_disp;
1977c478bd9Sstevel@tonic-gate 	t0.t_bind_cpu = PBIND_NONE;
1987c478bd9Sstevel@tonic-gate 	t0.t_bind_pset = PS_NONE;
1997c478bd9Sstevel@tonic-gate 	t0.t_cpupart = &cp_default;
2007c478bd9Sstevel@tonic-gate 	t0.t_clfuncs = &sys_classfuncs.thread;
2017c478bd9Sstevel@tonic-gate 	t0.t_copyops = NULL;
2027c478bd9Sstevel@tonic-gate 	THREAD_ONPROC(&t0, CPU);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	lwp0.lwp_thread = &t0;
205*ae115bc7Smrj 	lwp0.lwp_regs = (void *)rp;
2067c478bd9Sstevel@tonic-gate 	lwp0.lwp_procp = &p0;
2077c478bd9Sstevel@tonic-gate 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	p0.p_exec = NULL;
2107c478bd9Sstevel@tonic-gate 	p0.p_stat = SRUN;
2117c478bd9Sstevel@tonic-gate 	p0.p_flag = SSYS;
2127c478bd9Sstevel@tonic-gate 	p0.p_tlist = &t0;
2137c478bd9Sstevel@tonic-gate 	p0.p_stksize = 2*PAGESIZE;
2147c478bd9Sstevel@tonic-gate 	p0.p_stkpageszc = 0;
2157c478bd9Sstevel@tonic-gate 	p0.p_as = &kas;
2167c478bd9Sstevel@tonic-gate 	p0.p_lockp = &p0lock;
2177c478bd9Sstevel@tonic-gate 	p0.p_brkpageszc = 0;
2187c478bd9Sstevel@tonic-gate 	sigorset(&p0.p_ignore, &ignoredefault);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	CPU->cpu_thread = &t0;
2217c478bd9Sstevel@tonic-gate 	bzero(&cpu0_disp, sizeof (disp_t));
2227c478bd9Sstevel@tonic-gate 	CPU->cpu_disp = &cpu0_disp;
2237c478bd9Sstevel@tonic-gate 	CPU->cpu_disp->disp_cpu = CPU;
2247c478bd9Sstevel@tonic-gate 	CPU->cpu_dispthread = &t0;
2257c478bd9Sstevel@tonic-gate 	CPU->cpu_idle_thread = &t0;
2267c478bd9Sstevel@tonic-gate 	CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
2277c478bd9Sstevel@tonic-gate 	CPU->cpu_dispatch_pri = t0.t_pri;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	CPU->cpu_id = 0;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	CPU->cpu_pri = 12;		/* initial PIL for the boot CPU */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2340baeff3dSrab 	 * The kernel doesn't use LDTs unless a process explicitly requests one.
2357c478bd9Sstevel@tonic-gate 	 */
2360baeff3dSrab 	p0.p_ldt_desc = zero_sdesc;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
239*ae115bc7Smrj 	 * Initialize thread/cpu microstate accounting
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	init_mstate(&t0, LMS_SYSTEM);
2427c478bd9Sstevel@tonic-gate 	init_cpu_mstate(CPU, CMS_SYSTEM);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * Initialize lists of available and active CPUs.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	cpu_list_init(CPU);
2487c478bd9Sstevel@tonic-gate 
249*ae115bc7Smrj 	/*
250*ae115bc7Smrj 	 * Now that we have taken over the GDT, IDT and have initialized
251*ae115bc7Smrj 	 * active CPU list it's time to inform kmdb if present.
252*ae115bc7Smrj 	 */
253*ae115bc7Smrj 	if (boothowto & RB_DEBUG)
254*ae115bc7Smrj 		kdi_idt_sync();
255*ae115bc7Smrj 
256*ae115bc7Smrj 	/*
257*ae115bc7Smrj 	 * If requested (boot -d) drop into kmdb.
258*ae115bc7Smrj 	 *
259*ae115bc7Smrj 	 * This must be done after cpu_list_init() on the 64-bit kernel
260*ae115bc7Smrj 	 * since taking a trap requires that we re-compute gsbase based
261*ae115bc7Smrj 	 * on the cpu list.
262*ae115bc7Smrj 	 */
263*ae115bc7Smrj 	if (boothowto & RB_DEBUGENTER)
264*ae115bc7Smrj 		kmdb_enter();
265*ae115bc7Smrj 
266affbd3ccSkchow 	cpu_vm_data_init(CPU);
267affbd3ccSkchow 
268c88420b3Sdmick 	/* lgrp_init() needs PCI config space access */
269c88420b3Sdmick 	pci_cfgspace_init();
270c88420b3Sdmick 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Initialize the lgrp framework
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	lgrp_init();
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	rp->r_fp = 0;	/* terminate kernel stack traces! */
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	prom_init("kernel", (void *)NULL);
2797c478bd9Sstevel@tonic-gate 
28041791439Sandrei 	boot_ncpus = bootprop_getval("boot-ncpus");
28141791439Sandrei 
28241791439Sandrei 	if (boot_ncpus <= 0 || boot_ncpus > NCPU)
283*ae115bc7Smrj 		boot_ncpus = NCPU;
28441791439Sandrei 
28541791439Sandrei 	max_ncpus = boot_max_ncpus = boot_ncpus;
28641791439Sandrei 
2877c478bd9Sstevel@tonic-gate 	if (boothowto & RB_HALT) {
2887c478bd9Sstevel@tonic-gate 		prom_printf("unix: kernel halted by -h flag\n");
2897c478bd9Sstevel@tonic-gate 		prom_enter_mon();
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (workaround_errata(CPU) != 0)
2957c478bd9Sstevel@tonic-gate 		panic("critical workaround(s) missing for boot cpu");
2967c478bd9Sstevel@tonic-gate }
297