xref: /onnv-gate/usr/src/uts/i86pc/os/mlsetup.c (revision 414:c62c3f13a640)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/disp.h>
310Sstevel@tonic-gate #include <sys/promif.h>
320Sstevel@tonic-gate #include <sys/clock.h>
330Sstevel@tonic-gate #include <sys/cpuvar.h>
340Sstevel@tonic-gate #include <sys/stack.h>
350Sstevel@tonic-gate #include <vm/as.h>
360Sstevel@tonic-gate #include <vm/hat.h>
370Sstevel@tonic-gate #include <sys/reboot.h>
380Sstevel@tonic-gate #include <sys/avintr.h>
390Sstevel@tonic-gate #include <sys/vtrace.h>
400Sstevel@tonic-gate #include <sys/proc.h>
410Sstevel@tonic-gate #include <sys/thread.h>
420Sstevel@tonic-gate #include <sys/cpupart.h>
430Sstevel@tonic-gate #include <sys/pset.h>
440Sstevel@tonic-gate #include <sys/copyops.h>
450Sstevel@tonic-gate #include <sys/chip.h>
460Sstevel@tonic-gate #include <sys/disp.h>
470Sstevel@tonic-gate #include <sys/debug.h>
480Sstevel@tonic-gate #include <sys/sunddi.h>
490Sstevel@tonic-gate #include <sys/x86_archext.h>
500Sstevel@tonic-gate #include <sys/privregs.h>
510Sstevel@tonic-gate #include <sys/machsystm.h>
520Sstevel@tonic-gate #include <sys/ontrap.h>
530Sstevel@tonic-gate #include <sys/bootconf.h>
540Sstevel@tonic-gate #include <sys/kdi.h>
550Sstevel@tonic-gate #include <sys/archsystm.h>
560Sstevel@tonic-gate #include <sys/promif.h>
570Sstevel@tonic-gate #include <sys/bootconf.h>
580Sstevel@tonic-gate #include <sys/kobj.h>
590Sstevel@tonic-gate #include <sys/kobj_lex.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 /*
900Sstevel@tonic-gate  * External Routines:
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate extern void init_tables(void);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static uint32_t
970Sstevel@tonic-gate cpuid_getval(char *name)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	char prop[32];
1000Sstevel@tonic-gate 	u_longlong_t ll;
1010Sstevel@tonic-gate 	extern struct bootops *bootops;
1020Sstevel@tonic-gate 	if ((BOP_GETPROPLEN(bootops, name) > sizeof (prop)) ||
1030Sstevel@tonic-gate 	    (BOP_GETPROP(bootops, name, prop) < 0) ||
1040Sstevel@tonic-gate 	    (kobj_getvalue(prop, &ll) == -1))
1050Sstevel@tonic-gate 		return (0);
1060Sstevel@tonic-gate 	return ((uint32_t)ll);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Setup routine called right before main(). Interposing this function
1110Sstevel@tonic-gate  * before main() allows us to call it in a machine-independent fashion.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate void
1140Sstevel@tonic-gate mlsetup(struct regs *rp)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
1170Sstevel@tonic-gate 	extern struct chip cpu0_chip;
1180Sstevel@tonic-gate 	extern disp_t cpu0_disp;
1190Sstevel@tonic-gate 	extern char t0stack[];
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #if defined(__amd64)
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #if (BS_VERSION > 4)
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * When new boot_syscalls are added to the vector, this routine
1280Sstevel@tonic-gate 	 * must be modified to copy them into the kernel's copy of the
1290Sstevel@tonic-gate 	 * vector.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate #error mlsetup() must be updated for amd64 to support new boot_syscalls
1320Sstevel@tonic-gate #endif	/* (BS_VERSION > 4) */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * XX64	This remaps vmx's putchar to use the kernel's version
1360Sstevel@tonic-gate 	 *	that switches stacks before diving into vmx
1370Sstevel@tonic-gate 	 *	See explanation/complaints in commentary above.
1380Sstevel@tonic-gate 	 */
1390Sstevel@tonic-gate 	_vmx_sysp = sysp;
1400Sstevel@tonic-gate 	sysp = &__kbootsvcs;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	sysp->bsvc_getchar = _vmx_sysp->bsvc_getchar;
1430Sstevel@tonic-gate 	sysp->bsvc_putchar = _stack_safe_putchar;
1440Sstevel@tonic-gate 	sysp->bsvc_ischar = _vmx_sysp->bsvc_ischar;
1450Sstevel@tonic-gate #endif
1460Sstevel@tonic-gate 	/*
1470Sstevel@tonic-gate 	 * initialize cpu_self
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 	cpu[0]->cpu_self = cpu[0];
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/*
1520Sstevel@tonic-gate 	 * check if we've got special bits to clear or set
1530Sstevel@tonic-gate 	 * when checking cpu features
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	cpuid_feature_ecx_include =
1570Sstevel@tonic-gate 	    cpuid_getval("cpuid_feature_ecx_include");
1580Sstevel@tonic-gate 	cpuid_feature_ecx_exclude =
1590Sstevel@tonic-gate 	    cpuid_getval("cpuid_feature_ecx_exclude");
1600Sstevel@tonic-gate 	cpuid_feature_edx_include =
1610Sstevel@tonic-gate 	    cpuid_getval("cpuid_feature_edx_include");
1620Sstevel@tonic-gate 	cpuid_feature_edx_exclude =
1630Sstevel@tonic-gate 	    cpuid_getval("cpuid_feature_edx_exclude");
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * The first lightweight pass (pass0) through the cpuid data
1670Sstevel@tonic-gate 	 * was done in locore before mlsetup was called.  Do the next
1680Sstevel@tonic-gate 	 * pass in C code.
1690Sstevel@tonic-gate 	 *
1700Sstevel@tonic-gate 	 * The x86_feature bits are set here on the basis of the capabilities
1710Sstevel@tonic-gate 	 * of the boot CPU.  Note that if we choose to support CPUs that have
1720Sstevel@tonic-gate 	 * different feature sets (at which point we would almost certainly
1730Sstevel@tonic-gate 	 * want to set the feature bits to correspond to the feature
1740Sstevel@tonic-gate 	 * minimum) this value may be altered.
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	x86_feature = cpuid_pass1(cpu[0]);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss.
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	init_tables();
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate #if defined(__amd64)
1850Sstevel@tonic-gate 	/*CSTYLED*/
1860Sstevel@tonic-gate 	{
1870Sstevel@tonic-gate 		/*
1880Sstevel@tonic-gate 		 * setup %gs for the kernel
1890Sstevel@tonic-gate 		 */
1900Sstevel@tonic-gate 		uint64_t addr64 = (uint64_t)&cpus[0];
1910Sstevel@tonic-gate 		wrmsr(MSR_AMD_GSBASE, &addr64);
1920Sstevel@tonic-gate 		/*
1930Sstevel@tonic-gate 		 * XX64 We should never dereference off "other gsbase" or
1940Sstevel@tonic-gate 		 * "fsbase".  So, we should arrange to point FSBASE and
1950Sstevel@tonic-gate 		 * KGSBASE somewhere truly awful e.g. point it at the last
1960Sstevel@tonic-gate 		 * valid address below the hole so that any attempts to index
1970Sstevel@tonic-gate 		 * off them cause an exception.
1980Sstevel@tonic-gate 		 *
1990Sstevel@tonic-gate 		 * For now, point it at 8G -- at least it should be unmapped
2000Sstevel@tonic-gate 		 * until some 64-bit processes run.
2010Sstevel@tonic-gate 		 */
2020Sstevel@tonic-gate 		addr64 = 0x200000000ul;
2030Sstevel@tonic-gate 		wrmsr(MSR_AMD_FSBASE, &addr64);
2040Sstevel@tonic-gate 		wrmsr(MSR_AMD_KGSBASE, &addr64);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate #elif defined(__i386)
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * enable large page support right here.
2100Sstevel@tonic-gate 	 */
2110Sstevel@tonic-gate 	if (x86_feature & X86_LARGEPAGE) {
2120Sstevel@tonic-gate 		cr4_value |= CR4_PSE;
2130Sstevel@tonic-gate 		if (x86_feature & X86_PGE)
2140Sstevel@tonic-gate 			cr4_value |= CR4_PGE;
2150Sstevel@tonic-gate 		setup_121_andcall(enable_big_page_support, cr4_value);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Some i386 processors do not implement the rdtsc instruction,
2200Sstevel@tonic-gate 	 * or at least they do not implement it correctly.
2210Sstevel@tonic-gate 	 *
2220Sstevel@tonic-gate 	 * For those that do, patch in the rdtsc instructions in
2230Sstevel@tonic-gate 	 * various parts of the kernel right now while the text is
2240Sstevel@tonic-gate 	 * still writable.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	if (x86_feature & X86_TSC)
2270Sstevel@tonic-gate 		patch_tsc();
2280Sstevel@tonic-gate #endif
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * initialize t0
2320Sstevel@tonic-gate 	 */
2330Sstevel@tonic-gate 	t0.t_stk = (caddr_t)rp - MINFRAME;
2340Sstevel@tonic-gate 	t0.t_stkbase = t0stack;
2350Sstevel@tonic-gate 	t0.t_pri = maxclsyspri - 3;
2360Sstevel@tonic-gate 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
2370Sstevel@tonic-gate 	t0.t_procp = &p0;
2380Sstevel@tonic-gate 	t0.t_plockp = &p0lock.pl_lock;
2390Sstevel@tonic-gate 	t0.t_lwp = &lwp0;
2400Sstevel@tonic-gate 	t0.t_forw = &t0;
2410Sstevel@tonic-gate 	t0.t_back = &t0;
2420Sstevel@tonic-gate 	t0.t_next = &t0;
2430Sstevel@tonic-gate 	t0.t_prev = &t0;
2440Sstevel@tonic-gate 	t0.t_cpu = cpu[0];
2450Sstevel@tonic-gate 	t0.t_disp_queue = &cpu0_disp;
2460Sstevel@tonic-gate 	t0.t_bind_cpu = PBIND_NONE;
2470Sstevel@tonic-gate 	t0.t_bind_pset = PS_NONE;
2480Sstevel@tonic-gate 	t0.t_cpupart = &cp_default;
2490Sstevel@tonic-gate 	t0.t_clfuncs = &sys_classfuncs.thread;
2500Sstevel@tonic-gate 	t0.t_copyops = NULL;
2510Sstevel@tonic-gate 	THREAD_ONPROC(&t0, CPU);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	lwp0.lwp_thread = &t0;
2540Sstevel@tonic-gate 	lwp0.lwp_regs = (void *) rp;
2550Sstevel@tonic-gate 	lwp0.lwp_procp = &p0;
2560Sstevel@tonic-gate 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	p0.p_exec = NULL;
2590Sstevel@tonic-gate 	p0.p_stat = SRUN;
2600Sstevel@tonic-gate 	p0.p_flag = SSYS;
2610Sstevel@tonic-gate 	p0.p_tlist = &t0;
2620Sstevel@tonic-gate 	p0.p_stksize = 2*PAGESIZE;
2630Sstevel@tonic-gate 	p0.p_stkpageszc = 0;
2640Sstevel@tonic-gate 	p0.p_as = &kas;
2650Sstevel@tonic-gate 	p0.p_lockp = &p0lock;
2660Sstevel@tonic-gate 	p0.p_brkpageszc = 0;
2670Sstevel@tonic-gate 	sigorset(&p0.p_ignore, &ignoredefault);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	CPU->cpu_thread = &t0;
2700Sstevel@tonic-gate 	bzero(&cpu0_disp, sizeof (disp_t));
2710Sstevel@tonic-gate 	CPU->cpu_disp = &cpu0_disp;
2720Sstevel@tonic-gate 	CPU->cpu_disp->disp_cpu = CPU;
2730Sstevel@tonic-gate 	CPU->cpu_dispthread = &t0;
2740Sstevel@tonic-gate 	CPU->cpu_idle_thread = &t0;
2750Sstevel@tonic-gate 	CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE;
2760Sstevel@tonic-gate 	CPU->cpu_dispatch_pri = t0.t_pri;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	CPU->cpu_mask = 1;
2790Sstevel@tonic-gate 	CPU->cpu_id = 0;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	CPU->cpu_tss = &ktss0;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	CPU->cpu_pri = 12;		/* initial PIL for the boot CPU */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	CPU->cpu_ldt = ldt0_default;	/* default LDT */
2860Sstevel@tonic-gate 	CPU->cpu_gdt = gdt0;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * This must be done _after_ init_tables(), called above, has set up
2900Sstevel@tonic-gate 	 * ldt0_default_desc.
2910Sstevel@tonic-gate 	 */
2920Sstevel@tonic-gate #if defined(__amd64)
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * ldt0_default64 contains all invalid entries. We use that as p0's LDT
2950Sstevel@tonic-gate 	 * because p0 should never have any reason to use the LDT. This will
2960Sstevel@tonic-gate 	 * catch things early if such a scenario should ever occur.
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	p0.p_ldt_desc = ldt0_default64_desc;
2990Sstevel@tonic-gate #else
3000Sstevel@tonic-gate 	p0.p_ldt_desc = ldt0_default_desc;
3010Sstevel@tonic-gate #endif /* __amd64 */
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/*
3040Sstevel@tonic-gate 	 * Kernel IDT.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 	CPU->cpu_idt = idt0;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/*
3090Sstevel@tonic-gate 	 * Initialize thread/cpu microstate accounting here
3100Sstevel@tonic-gate 	 */
3110Sstevel@tonic-gate 	init_mstate(&t0, LMS_SYSTEM);
3120Sstevel@tonic-gate 	init_cpu_mstate(CPU, CMS_SYSTEM);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * Initialize lists of available and active CPUs.
3160Sstevel@tonic-gate 	 */
3170Sstevel@tonic-gate 	cpu_list_init(CPU);
3180Sstevel@tonic-gate 
319*414Skchow 	cpu_vm_data_init(CPU);
320*414Skchow 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * Initialize the lgrp framework
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	lgrp_init();
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * The lgroup code needs to at least know about a CPU's
3280Sstevel@tonic-gate 	 * chip association, but it's too early to fully initialize
3290Sstevel@tonic-gate 	 * cpu0_chip, since the device node for the boot CPU doesn't
3300Sstevel@tonic-gate 	 * exist yet. Initialize enough of it to get by until formal
3310Sstevel@tonic-gate 	 * initialization.
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	CPU->cpu_rechoose = rechoose_interval;
3340Sstevel@tonic-gate 	CPU->cpu_chip = &cpu0_chip;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	rp->r_fp = 0;	/* terminate kernel stack traces! */
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	prom_init("kernel", (void *)NULL);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	if (boothowto & RB_HALT) {
3410Sstevel@tonic-gate 		prom_printf("unix: kernel halted by -h flag\n");
3420Sstevel@tonic-gate 		prom_enter_mon();
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (workaround_errata(CPU) != 0)
3480Sstevel@tonic-gate 		panic("critical workaround(s) missing for boot cpu");
3490Sstevel@tonic-gate }
350