xref: /onnv-gate/usr/src/uts/i86pc/os/mlsetup.c (revision 2006:11a559c797d5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2006Sandrei  * Common Development and Distribution License (the "License").
6*2006Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221217Srab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/disp.h>
300Sstevel@tonic-gate #include <sys/promif.h>
310Sstevel@tonic-gate #include <sys/clock.h>
320Sstevel@tonic-gate #include <sys/cpuvar.h>
330Sstevel@tonic-gate #include <sys/stack.h>
340Sstevel@tonic-gate #include <vm/as.h>
350Sstevel@tonic-gate #include <vm/hat.h>
360Sstevel@tonic-gate #include <sys/reboot.h>
370Sstevel@tonic-gate #include <sys/avintr.h>
380Sstevel@tonic-gate #include <sys/vtrace.h>
390Sstevel@tonic-gate #include <sys/proc.h>
400Sstevel@tonic-gate #include <sys/thread.h>
410Sstevel@tonic-gate #include <sys/cpupart.h>
420Sstevel@tonic-gate #include <sys/pset.h>
430Sstevel@tonic-gate #include <sys/copyops.h>
440Sstevel@tonic-gate #include <sys/chip.h>
450Sstevel@tonic-gate #include <sys/disp.h>
460Sstevel@tonic-gate #include <sys/debug.h>
470Sstevel@tonic-gate #include <sys/sunddi.h>
480Sstevel@tonic-gate #include <sys/x86_archext.h>
490Sstevel@tonic-gate #include <sys/privregs.h>
500Sstevel@tonic-gate #include <sys/machsystm.h>
510Sstevel@tonic-gate #include <sys/ontrap.h>
520Sstevel@tonic-gate #include <sys/bootconf.h>
530Sstevel@tonic-gate #include <sys/kdi.h>
540Sstevel@tonic-gate #include <sys/archsystm.h>
550Sstevel@tonic-gate #include <sys/promif.h>
560Sstevel@tonic-gate #include <sys/bootconf.h>
570Sstevel@tonic-gate #include <sys/kobj.h>
580Sstevel@tonic-gate #include <sys/kobj_lex.h>
59748Sdmick #include <sys/pci_cfgspace.h>
600Sstevel@tonic-gate #if defined(__amd64)
610Sstevel@tonic-gate #include <sys/bootsvcs.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * XX64	This stuff deals with switching stacks in case a trapping
650Sstevel@tonic-gate  *	thread wants to call back into boot -after- boot has lost track
660Sstevel@tonic-gate  *	of the mappings but before the kernel owns the console.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *	(A better way to hide this would be to add a 'this' pointer to
690Sstevel@tonic-gate  *	every boot syscall so that vmx could get at the resulting save
700Sstevel@tonic-gate  *	area.)
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate struct boot_syscalls *_vmx_sysp;
740Sstevel@tonic-gate static struct boot_syscalls __kbootsvcs;
750Sstevel@tonic-gate extern struct boot_syscalls *sysp;
760Sstevel@tonic-gate extern void _stack_safe_putchar(int c);
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * some globals for patching the result of cpuid
810Sstevel@tonic-gate  * to solve problems w/ creative cpu vendors
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_include;
850Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_exclude;
860Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_include;
870Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_exclude;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
90783Sdmick  * Dummy spl priority masks
91783Sdmick  */
92783Sdmick static unsigned char	dummy_cpu_pri[MAXIPL + 1] = {
93783Sdmick 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf,
94783Sdmick 	0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf
95783Sdmick };
96783Sdmick 
97783Sdmick /*
980Sstevel@tonic-gate  * External Routines:
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate extern void init_tables(void);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static uint32_t
105*2006Sandrei bootprop_getval(char *name)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	char prop[32];
1080Sstevel@tonic-gate 	u_longlong_t ll;
1090Sstevel@tonic-gate 	extern struct bootops *bootops;
1100Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, name) > sizeof (prop)) ||
1110Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, name, prop) < 0) ||
1120Sstevel@tonic-gate 	    (kobj_getvalue(prop, &ll) == -1))
1130Sstevel@tonic-gate 		return (0);
1140Sstevel@tonic-gate 	return ((uint32_t)ll);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * Setup routine called right before main(). Interposing this function
1190Sstevel@tonic-gate  * before main() allows us to call it in a machine-independent fashion.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate void
1220Sstevel@tonic-gate mlsetup(struct regs *rp)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
1250Sstevel@tonic-gate 	extern struct chip cpu0_chip;
1260Sstevel@tonic-gate 	extern disp_t cpu0_disp;
1270Sstevel@tonic-gate 	extern char t0stack[];
128*2006Sandrei 	int boot_ncpus;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #if defined(__amd64)
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #if (BS_VERSION > 4)
1350Sstevel@tonic-gate 	/*
1360Sstevel@tonic-gate 	 * When new boot_syscalls are added to the vector, this routine
1370Sstevel@tonic-gate 	 * must be modified to copy them into the kernel's copy of the
1380Sstevel@tonic-gate 	 * vector.
1390Sstevel@tonic-gate 	 */
1400Sstevel@tonic-gate #error mlsetup() must be updated for amd64 to support new boot_syscalls
1410Sstevel@tonic-gate #endif	/* (BS_VERSION > 4) */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/*
1440Sstevel@tonic-gate 	 * XX64	This remaps vmx's putchar to use the kernel's version
1450Sstevel@tonic-gate 	 *	that switches stacks before diving into vmx
1460Sstevel@tonic-gate 	 *	See explanation/complaints in commentary above.
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	_vmx_sysp = sysp;
1490Sstevel@tonic-gate 	sysp = &__kbootsvcs;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	sysp->bsvc_getchar = _vmx_sysp->bsvc_getchar;
1520Sstevel@tonic-gate 	sysp->bsvc_putchar = _stack_safe_putchar;
1530Sstevel@tonic-gate 	sysp->bsvc_ischar = _vmx_sysp->bsvc_ischar;
1540Sstevel@tonic-gate #endif
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * initialize cpu_self
1570Sstevel@tonic-gate 	 */
1580Sstevel@tonic-gate 	cpu[0]->cpu_self = cpu[0];
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
161783Sdmick 	 * Set up dummy cpu_pri_data values till psm spl code is
162783Sdmick 	 * installed.  This allows splx() to work on amd64.
163783Sdmick 	 */
164783Sdmick 
165783Sdmick 	cpu[0]->cpu_pri_data = dummy_cpu_pri;
166783Sdmick 
167783Sdmick 	/*
1680Sstevel@tonic-gate 	 * check if we've got special bits to clear or set
1690Sstevel@tonic-gate 	 * when checking cpu features
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	cpuid_feature_ecx_include =
173*2006Sandrei 	    bootprop_getval("cpuid_feature_ecx_include");
1740Sstevel@tonic-gate 	cpuid_feature_ecx_exclude =
175*2006Sandrei 	    bootprop_getval("cpuid_feature_ecx_exclude");
1760Sstevel@tonic-gate 	cpuid_feature_edx_include =
177*2006Sandrei 	    bootprop_getval("cpuid_feature_edx_include");
1780Sstevel@tonic-gate 	cpuid_feature_edx_exclude =
179*2006Sandrei 	    bootprop_getval("cpuid_feature_edx_exclude");
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * The first lightweight pass (pass0) through the cpuid data
1830Sstevel@tonic-gate 	 * was done in locore before mlsetup was called.  Do the next
1840Sstevel@tonic-gate 	 * pass in C code.
1850Sstevel@tonic-gate 	 *
1860Sstevel@tonic-gate 	 * The x86_feature bits are set here on the basis of the capabilities
1870Sstevel@tonic-gate 	 * of the boot CPU.  Note that if we choose to support CPUs that have
1880Sstevel@tonic-gate 	 * different feature sets (at which point we would almost certainly
1890Sstevel@tonic-gate 	 * want to set the feature bits to correspond to the feature
1900Sstevel@tonic-gate 	 * minimum) this value may be altered.
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	x86_feature = cpuid_pass1(cpu[0]);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 	init_tables();
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #if defined(__amd64)
2010Sstevel@tonic-gate 	/*CSTYLED*/
2020Sstevel@tonic-gate 	{
2030Sstevel@tonic-gate 		/*
2040Sstevel@tonic-gate 		 * setup %gs for the kernel
2050Sstevel@tonic-gate 		 */
206770Skucharsk 		wrmsr(MSR_AMD_GSBASE, (uint64_t)&cpus[0]);
2070Sstevel@tonic-gate 		/*
2080Sstevel@tonic-gate 		 * XX64 We should never dereference off "other gsbase" or
2090Sstevel@tonic-gate 		 * "fsbase".  So, we should arrange to point FSBASE and
2100Sstevel@tonic-gate 		 * KGSBASE somewhere truly awful e.g. point it at the last
2110Sstevel@tonic-gate 		 * valid address below the hole so that any attempts to index
2120Sstevel@tonic-gate 		 * off them cause an exception.
2130Sstevel@tonic-gate 		 *
2140Sstevel@tonic-gate 		 * For now, point it at 8G -- at least it should be unmapped
2150Sstevel@tonic-gate 		 * until some 64-bit processes run.
2160Sstevel@tonic-gate 		 */
217770Skucharsk 		wrmsr(MSR_AMD_FSBASE, 0x200000000UL);
218770Skucharsk 		wrmsr(MSR_AMD_KGSBASE, 0x200000000UL);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate #elif defined(__i386)
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * enable large page support right here.
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	if (x86_feature & X86_LARGEPAGE) {
2260Sstevel@tonic-gate 		cr4_value |= CR4_PSE;
2270Sstevel@tonic-gate 		if (x86_feature & X86_PGE)
2280Sstevel@tonic-gate 			cr4_value |= CR4_PGE;
2290Sstevel@tonic-gate 		setup_121_andcall(enable_big_page_support, cr4_value);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/*
2330Sstevel@tonic-gate 	 * Some i386 processors do not implement the rdtsc instruction,
2340Sstevel@tonic-gate 	 * or at least they do not implement it correctly.
2350Sstevel@tonic-gate 	 *
2360Sstevel@tonic-gate 	 * For those that do, patch in the rdtsc instructions in
2370Sstevel@tonic-gate 	 * various parts of the kernel right now while the text is
2380Sstevel@tonic-gate 	 * still writable.
2390Sstevel@tonic-gate 	 */
2400Sstevel@tonic-gate 	if (x86_feature & X86_TSC)
2410Sstevel@tonic-gate 		patch_tsc();
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * initialize t0
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	t0.t_stk = (caddr_t)rp - MINFRAME;
2480Sstevel@tonic-gate 	t0.t_stkbase = t0stack;
2490Sstevel@tonic-gate 	t0.t_pri = maxclsyspri - 3;
2500Sstevel@tonic-gate 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
2510Sstevel@tonic-gate 	t0.t_procp = &p0;
2520Sstevel@tonic-gate 	t0.t_plockp = &p0lock.pl_lock;
2530Sstevel@tonic-gate 	t0.t_lwp = &lwp0;
2540Sstevel@tonic-gate 	t0.t_forw = &t0;
2550Sstevel@tonic-gate 	t0.t_back = &t0;
2560Sstevel@tonic-gate 	t0.t_next = &t0;
2570Sstevel@tonic-gate 	t0.t_prev = &t0;
2580Sstevel@tonic-gate 	t0.t_cpu = cpu[0];
2590Sstevel@tonic-gate 	t0.t_disp_queue = &cpu0_disp;
2600Sstevel@tonic-gate 	t0.t_bind_cpu = PBIND_NONE;
2610Sstevel@tonic-gate 	t0.t_bind_pset = PS_NONE;
2620Sstevel@tonic-gate 	t0.t_cpupart = &cp_default;
2630Sstevel@tonic-gate 	t0.t_clfuncs = &sys_classfuncs.thread;
2640Sstevel@tonic-gate 	t0.t_copyops = NULL;
2650Sstevel@tonic-gate 	THREAD_ONPROC(&t0, CPU);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	lwp0.lwp_thread = &t0;
2680Sstevel@tonic-gate 	lwp0.lwp_regs = (void *) rp;
2690Sstevel@tonic-gate 	lwp0.lwp_procp = &p0;
2700Sstevel@tonic-gate 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	p0.p_exec = NULL;
2730Sstevel@tonic-gate 	p0.p_stat = SRUN;
2740Sstevel@tonic-gate 	p0.p_flag = SSYS;
2750Sstevel@tonic-gate 	p0.p_tlist = &t0;
2760Sstevel@tonic-gate 	p0.p_stksize = 2*PAGESIZE;
2770Sstevel@tonic-gate 	p0.p_stkpageszc = 0;
2780Sstevel@tonic-gate 	p0.p_as = &kas;
2790Sstevel@tonic-gate 	p0.p_lockp = &p0lock;
2800Sstevel@tonic-gate 	p0.p_brkpageszc = 0;
2810Sstevel@tonic-gate 	sigorset(&p0.p_ignore, &ignoredefault);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	CPU->cpu_thread = &t0;
2840Sstevel@tonic-gate 	bzero(&cpu0_disp, sizeof (disp_t));
2850Sstevel@tonic-gate 	CPU->cpu_disp = &cpu0_disp;
2860Sstevel@tonic-gate 	CPU->cpu_disp->disp_cpu = CPU;
2870Sstevel@tonic-gate 	CPU->cpu_dispthread = &t0;
2880Sstevel@tonic-gate 	CPU->cpu_idle_thread = &t0;
2890Sstevel@tonic-gate 	CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
2900Sstevel@tonic-gate 	CPU->cpu_dispatch_pri = t0.t_pri;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	CPU->cpu_id = 0;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	CPU->cpu_tss = &ktss0;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	CPU->cpu_pri = 12;		/* initial PIL for the boot CPU */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	CPU->cpu_gdt = gdt0;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/*
3011217Srab 	 * The kernel doesn't use LDTs unless a process explicitly requests one.
3020Sstevel@tonic-gate 	 */
3031217Srab 	p0.p_ldt_desc = zero_sdesc;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	/*
3060Sstevel@tonic-gate 	 * Kernel IDT.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate 	CPU->cpu_idt = idt0;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	/*
3110Sstevel@tonic-gate 	 * Initialize thread/cpu microstate accounting here
3120Sstevel@tonic-gate 	 */
3130Sstevel@tonic-gate 	init_mstate(&t0, LMS_SYSTEM);
3140Sstevel@tonic-gate 	init_cpu_mstate(CPU, CMS_SYSTEM);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/*
3170Sstevel@tonic-gate 	 * Initialize lists of available and active CPUs.
3180Sstevel@tonic-gate 	 */
3190Sstevel@tonic-gate 	cpu_list_init(CPU);
3200Sstevel@tonic-gate 
321414Skchow 	cpu_vm_data_init(CPU);
322414Skchow 
323748Sdmick 	/* lgrp_init() needs PCI config space access */
324748Sdmick 	pci_cfgspace_init();
325748Sdmick 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * Initialize the lgrp framework
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	lgrp_init();
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/*
3320Sstevel@tonic-gate 	 * The lgroup code needs to at least know about a CPU's
3330Sstevel@tonic-gate 	 * chip association, but it's too early to fully initialize
3340Sstevel@tonic-gate 	 * cpu0_chip, since the device node for the boot CPU doesn't
3350Sstevel@tonic-gate 	 * exist yet. Initialize enough of it to get by until formal
3360Sstevel@tonic-gate 	 * initialization.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	CPU->cpu_rechoose = rechoose_interval;
3390Sstevel@tonic-gate 	CPU->cpu_chip = &cpu0_chip;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	rp->r_fp = 0;	/* terminate kernel stack traces! */
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	prom_init("kernel", (void *)NULL);
3440Sstevel@tonic-gate 
345*2006Sandrei 	boot_ncpus = bootprop_getval("boot-ncpus");
346*2006Sandrei 
347*2006Sandrei 	/*
348*2006Sandrei 	 * To avoid wasting kernel memory, we're temporarily limiting the
349*2006Sandrei 	 * total number of processors to 32 by default until we get the
350*2006Sandrei 	 * capability to scan ACPI tables early on boot to detect how many
351*2006Sandrei 	 * processors are actually present.  However, 64-bit systems
352*2006Sandrei 	 * can be booted with up to 64 processors by setting "boot-ncpus"
353*2006Sandrei 	 * boot property to 64.
354*2006Sandrei 	 */
355*2006Sandrei 	if (boot_ncpus <= 0 || boot_ncpus > NCPU)
356*2006Sandrei 		boot_ncpus = 32;
357*2006Sandrei 
358*2006Sandrei 	max_ncpus = boot_max_ncpus = boot_ncpus;
359*2006Sandrei 
3600Sstevel@tonic-gate 	if (boothowto & RB_HALT) {
3610Sstevel@tonic-gate 		prom_printf("unix: kernel halted by -h flag\n");
3620Sstevel@tonic-gate 		prom_enter_mon();
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (workaround_errata(CPU) != 0)
3680Sstevel@tonic-gate 		panic("critical workaround(s) missing for boot cpu");
3690Sstevel@tonic-gate }
370