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> 60748Sdmick #include <sys/pci_cfgspace.h> 610Sstevel@tonic-gate #if defined(__amd64) 620Sstevel@tonic-gate #include <sys/bootsvcs.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * XX64 This stuff deals with switching stacks in case a trapping 660Sstevel@tonic-gate * thread wants to call back into boot -after- boot has lost track 670Sstevel@tonic-gate * of the mappings but before the kernel owns the console. 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * (A better way to hide this would be to add a 'this' pointer to 700Sstevel@tonic-gate * every boot syscall so that vmx could get at the resulting save 710Sstevel@tonic-gate * area.) 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate 740Sstevel@tonic-gate struct boot_syscalls *_vmx_sysp; 750Sstevel@tonic-gate static struct boot_syscalls __kbootsvcs; 760Sstevel@tonic-gate extern struct boot_syscalls *sysp; 770Sstevel@tonic-gate extern void _stack_safe_putchar(int c); 780Sstevel@tonic-gate #endif 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* 810Sstevel@tonic-gate * some globals for patching the result of cpuid 820Sstevel@tonic-gate * to solve problems w/ creative cpu vendors 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate 850Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_include; 860Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_exclude; 870Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_include; 880Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_exclude; 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * External Routines: 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate 940Sstevel@tonic-gate extern void init_tables(void); 950Sstevel@tonic-gate 960Sstevel@tonic-gate 970Sstevel@tonic-gate static uint32_t 980Sstevel@tonic-gate cpuid_getval(char *name) 990Sstevel@tonic-gate { 1000Sstevel@tonic-gate char prop[32]; 1010Sstevel@tonic-gate u_longlong_t ll; 1020Sstevel@tonic-gate extern struct bootops *bootops; 1030Sstevel@tonic-gate if ((BOP_GETPROPLEN(bootops, name) > sizeof (prop)) || 1040Sstevel@tonic-gate (BOP_GETPROP(bootops, name, prop) < 0) || 1050Sstevel@tonic-gate (kobj_getvalue(prop, &ll) == -1)) 1060Sstevel@tonic-gate return (0); 1070Sstevel@tonic-gate return ((uint32_t)ll); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * Setup routine called right before main(). Interposing this function 1120Sstevel@tonic-gate * before main() allows us to call it in a machine-independent fashion. 1130Sstevel@tonic-gate */ 1140Sstevel@tonic-gate void 1150Sstevel@tonic-gate mlsetup(struct regs *rp) 1160Sstevel@tonic-gate { 1170Sstevel@tonic-gate extern struct classfuncs sys_classfuncs; 1180Sstevel@tonic-gate extern struct chip cpu0_chip; 1190Sstevel@tonic-gate extern disp_t cpu0_disp; 1200Sstevel@tonic-gate extern char t0stack[]; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate #if defined(__amd64) 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate #if (BS_VERSION > 4) 1270Sstevel@tonic-gate /* 1280Sstevel@tonic-gate * When new boot_syscalls are added to the vector, this routine 1290Sstevel@tonic-gate * must be modified to copy them into the kernel's copy of the 1300Sstevel@tonic-gate * vector. 1310Sstevel@tonic-gate */ 1320Sstevel@tonic-gate #error mlsetup() must be updated for amd64 to support new boot_syscalls 1330Sstevel@tonic-gate #endif /* (BS_VERSION > 4) */ 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* 1360Sstevel@tonic-gate * XX64 This remaps vmx's putchar to use the kernel's version 1370Sstevel@tonic-gate * that switches stacks before diving into vmx 1380Sstevel@tonic-gate * See explanation/complaints in commentary above. 1390Sstevel@tonic-gate */ 1400Sstevel@tonic-gate _vmx_sysp = sysp; 1410Sstevel@tonic-gate sysp = &__kbootsvcs; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate sysp->bsvc_getchar = _vmx_sysp->bsvc_getchar; 1440Sstevel@tonic-gate sysp->bsvc_putchar = _stack_safe_putchar; 1450Sstevel@tonic-gate sysp->bsvc_ischar = _vmx_sysp->bsvc_ischar; 1460Sstevel@tonic-gate #endif 1470Sstevel@tonic-gate /* 1480Sstevel@tonic-gate * initialize cpu_self 1490Sstevel@tonic-gate */ 1500Sstevel@tonic-gate cpu[0]->cpu_self = cpu[0]; 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * check if we've got special bits to clear or set 1540Sstevel@tonic-gate * when checking cpu features 1550Sstevel@tonic-gate */ 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate cpuid_feature_ecx_include = 1580Sstevel@tonic-gate cpuid_getval("cpuid_feature_ecx_include"); 1590Sstevel@tonic-gate cpuid_feature_ecx_exclude = 1600Sstevel@tonic-gate cpuid_getval("cpuid_feature_ecx_exclude"); 1610Sstevel@tonic-gate cpuid_feature_edx_include = 1620Sstevel@tonic-gate cpuid_getval("cpuid_feature_edx_include"); 1630Sstevel@tonic-gate cpuid_feature_edx_exclude = 1640Sstevel@tonic-gate cpuid_getval("cpuid_feature_edx_exclude"); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* 1670Sstevel@tonic-gate * The first lightweight pass (pass0) through the cpuid data 1680Sstevel@tonic-gate * was done in locore before mlsetup was called. Do the next 1690Sstevel@tonic-gate * pass in C code. 1700Sstevel@tonic-gate * 1710Sstevel@tonic-gate * The x86_feature bits are set here on the basis of the capabilities 1720Sstevel@tonic-gate * of the boot CPU. Note that if we choose to support CPUs that have 1730Sstevel@tonic-gate * different feature sets (at which point we would almost certainly 1740Sstevel@tonic-gate * want to set the feature bits to correspond to the feature 1750Sstevel@tonic-gate * minimum) this value may be altered. 1760Sstevel@tonic-gate */ 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate x86_feature = cpuid_pass1(cpu[0]); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate init_tables(); 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate #if defined(__amd64) 1860Sstevel@tonic-gate /*CSTYLED*/ 1870Sstevel@tonic-gate { 1880Sstevel@tonic-gate /* 1890Sstevel@tonic-gate * setup %gs for the kernel 1900Sstevel@tonic-gate */ 191*770Skucharsk wrmsr(MSR_AMD_GSBASE, (uint64_t)&cpus[0]); 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 */ 202*770Skucharsk wrmsr(MSR_AMD_FSBASE, 0x200000000UL); 203*770Skucharsk wrmsr(MSR_AMD_KGSBASE, 0x200000000UL); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate #elif defined(__i386) 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * enable large page support right here. 2090Sstevel@tonic-gate */ 2100Sstevel@tonic-gate if (x86_feature & X86_LARGEPAGE) { 2110Sstevel@tonic-gate cr4_value |= CR4_PSE; 2120Sstevel@tonic-gate if (x86_feature & X86_PGE) 2130Sstevel@tonic-gate cr4_value |= CR4_PGE; 2140Sstevel@tonic-gate setup_121_andcall(enable_big_page_support, cr4_value); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * Some i386 processors do not implement the rdtsc instruction, 2190Sstevel@tonic-gate * or at least they do not implement it correctly. 2200Sstevel@tonic-gate * 2210Sstevel@tonic-gate * For those that do, patch in the rdtsc instructions in 2220Sstevel@tonic-gate * various parts of the kernel right now while the text is 2230Sstevel@tonic-gate * still writable. 2240Sstevel@tonic-gate */ 2250Sstevel@tonic-gate if (x86_feature & X86_TSC) 2260Sstevel@tonic-gate patch_tsc(); 2270Sstevel@tonic-gate #endif 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * initialize t0 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate t0.t_stk = (caddr_t)rp - MINFRAME; 2330Sstevel@tonic-gate t0.t_stkbase = t0stack; 2340Sstevel@tonic-gate t0.t_pri = maxclsyspri - 3; 2350Sstevel@tonic-gate t0.t_schedflag = TS_LOAD | TS_DONT_SWAP; 2360Sstevel@tonic-gate t0.t_procp = &p0; 2370Sstevel@tonic-gate t0.t_plockp = &p0lock.pl_lock; 2380Sstevel@tonic-gate t0.t_lwp = &lwp0; 2390Sstevel@tonic-gate t0.t_forw = &t0; 2400Sstevel@tonic-gate t0.t_back = &t0; 2410Sstevel@tonic-gate t0.t_next = &t0; 2420Sstevel@tonic-gate t0.t_prev = &t0; 2430Sstevel@tonic-gate t0.t_cpu = cpu[0]; 2440Sstevel@tonic-gate t0.t_disp_queue = &cpu0_disp; 2450Sstevel@tonic-gate t0.t_bind_cpu = PBIND_NONE; 2460Sstevel@tonic-gate t0.t_bind_pset = PS_NONE; 2470Sstevel@tonic-gate t0.t_cpupart = &cp_default; 2480Sstevel@tonic-gate t0.t_clfuncs = &sys_classfuncs.thread; 2490Sstevel@tonic-gate t0.t_copyops = NULL; 2500Sstevel@tonic-gate THREAD_ONPROC(&t0, CPU); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate lwp0.lwp_thread = &t0; 2530Sstevel@tonic-gate lwp0.lwp_regs = (void *) rp; 2540Sstevel@tonic-gate lwp0.lwp_procp = &p0; 2550Sstevel@tonic-gate t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate p0.p_exec = NULL; 2580Sstevel@tonic-gate p0.p_stat = SRUN; 2590Sstevel@tonic-gate p0.p_flag = SSYS; 2600Sstevel@tonic-gate p0.p_tlist = &t0; 2610Sstevel@tonic-gate p0.p_stksize = 2*PAGESIZE; 2620Sstevel@tonic-gate p0.p_stkpageszc = 0; 2630Sstevel@tonic-gate p0.p_as = &kas; 2640Sstevel@tonic-gate p0.p_lockp = &p0lock; 2650Sstevel@tonic-gate p0.p_brkpageszc = 0; 2660Sstevel@tonic-gate sigorset(&p0.p_ignore, &ignoredefault); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate CPU->cpu_thread = &t0; 2690Sstevel@tonic-gate bzero(&cpu0_disp, sizeof (disp_t)); 2700Sstevel@tonic-gate CPU->cpu_disp = &cpu0_disp; 2710Sstevel@tonic-gate CPU->cpu_disp->disp_cpu = CPU; 2720Sstevel@tonic-gate CPU->cpu_dispthread = &t0; 2730Sstevel@tonic-gate CPU->cpu_idle_thread = &t0; 2740Sstevel@tonic-gate CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE; 2750Sstevel@tonic-gate CPU->cpu_dispatch_pri = t0.t_pri; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate CPU->cpu_mask = 1; 2780Sstevel@tonic-gate CPU->cpu_id = 0; 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate CPU->cpu_tss = &ktss0; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate CPU->cpu_pri = 12; /* initial PIL for the boot CPU */ 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate CPU->cpu_ldt = ldt0_default; /* default LDT */ 2850Sstevel@tonic-gate CPU->cpu_gdt = gdt0; 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate /* 2880Sstevel@tonic-gate * This must be done _after_ init_tables(), called above, has set up 2890Sstevel@tonic-gate * ldt0_default_desc. 2900Sstevel@tonic-gate */ 2910Sstevel@tonic-gate #if defined(__amd64) 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * ldt0_default64 contains all invalid entries. We use that as p0's LDT 2940Sstevel@tonic-gate * because p0 should never have any reason to use the LDT. This will 2950Sstevel@tonic-gate * catch things early if such a scenario should ever occur. 2960Sstevel@tonic-gate */ 2970Sstevel@tonic-gate p0.p_ldt_desc = ldt0_default64_desc; 2980Sstevel@tonic-gate #else 2990Sstevel@tonic-gate p0.p_ldt_desc = ldt0_default_desc; 3000Sstevel@tonic-gate #endif /* __amd64 */ 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate /* 3030Sstevel@tonic-gate * Kernel IDT. 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate CPU->cpu_idt = idt0; 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * Initialize thread/cpu microstate accounting here 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate init_mstate(&t0, LMS_SYSTEM); 3110Sstevel@tonic-gate init_cpu_mstate(CPU, CMS_SYSTEM); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * Initialize lists of available and active CPUs. 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate cpu_list_init(CPU); 3170Sstevel@tonic-gate 318414Skchow cpu_vm_data_init(CPU); 319414Skchow 320748Sdmick /* lgrp_init() needs PCI config space access */ 321748Sdmick pci_cfgspace_init(); 322748Sdmick 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * Initialize the lgrp framework 3250Sstevel@tonic-gate */ 3260Sstevel@tonic-gate lgrp_init(); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * The lgroup code needs to at least know about a CPU's 3300Sstevel@tonic-gate * chip association, but it's too early to fully initialize 3310Sstevel@tonic-gate * cpu0_chip, since the device node for the boot CPU doesn't 3320Sstevel@tonic-gate * exist yet. Initialize enough of it to get by until formal 3330Sstevel@tonic-gate * initialization. 3340Sstevel@tonic-gate */ 3350Sstevel@tonic-gate CPU->cpu_rechoose = rechoose_interval; 3360Sstevel@tonic-gate CPU->cpu_chip = &cpu0_chip; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate rp->r_fp = 0; /* terminate kernel stack traces! */ 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate prom_init("kernel", (void *)NULL); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate if (boothowto & RB_HALT) { 3430Sstevel@tonic-gate prom_printf("unix: kernel halted by -h flag\n"); 3440Sstevel@tonic-gate prom_enter_mon(); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate if (workaround_errata(CPU) != 0) 3500Sstevel@tonic-gate panic("critical workaround(s) missing for boot cpu"); 3510Sstevel@tonic-gate } 352