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 52006Sandrei * Common Development and Distribution License (the "License"). 62006Sandrei * 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 /* 223434Sesaxe * Copyright 2007 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> 293446Smrj #include <sys/sysmacros.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> 453434Sesaxe #include <sys/pg.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> 543446Smrj #include <sys/kdi_machimpl.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> 615084Sjohnlev #ifdef __xpv 625084Sjohnlev #include <sys/hypervisor.h> 635084Sjohnlev #endif 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* 660Sstevel@tonic-gate * some globals for patching the result of cpuid 670Sstevel@tonic-gate * to solve problems w/ creative cpu vendors 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_include; 710Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_exclude; 720Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_include; 730Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_exclude; 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 76783Sdmick * Dummy spl priority masks 77783Sdmick */ 783446Smrj static unsigned char dummy_cpu_pri[MAXIPL + 1] = { 79783Sdmick 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 80783Sdmick 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf 81783Sdmick }; 82783Sdmick 830Sstevel@tonic-gate 840Sstevel@tonic-gate static uint32_t 852006Sandrei bootprop_getval(char *name) 860Sstevel@tonic-gate { 870Sstevel@tonic-gate char prop[32]; 880Sstevel@tonic-gate u_longlong_t ll; 890Sstevel@tonic-gate extern struct bootops *bootops; 900Sstevel@tonic-gate if ((BOP_GETPROPLEN(bootops, name) > sizeof (prop)) || 910Sstevel@tonic-gate (BOP_GETPROP(bootops, name, prop) < 0) || 920Sstevel@tonic-gate (kobj_getvalue(prop, &ll) == -1)) 930Sstevel@tonic-gate return (0); 940Sstevel@tonic-gate return ((uint32_t)ll); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * Setup routine called right before main(). Interposing this function 990Sstevel@tonic-gate * before main() allows us to call it in a machine-independent fashion. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate void 1020Sstevel@tonic-gate mlsetup(struct regs *rp) 1030Sstevel@tonic-gate { 1040Sstevel@tonic-gate extern struct classfuncs sys_classfuncs; 1050Sstevel@tonic-gate extern disp_t cpu0_disp; 1060Sstevel@tonic-gate extern char t0stack[]; 1072006Sandrei int boot_ncpus; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * initialize cpu_self 1130Sstevel@tonic-gate */ 1140Sstevel@tonic-gate cpu[0]->cpu_self = cpu[0]; 1150Sstevel@tonic-gate 1165084Sjohnlev #if defined(__xpv) 1175084Sjohnlev /* 1185084Sjohnlev * Point at the hypervisor's virtual cpu structure 1195084Sjohnlev */ 1205084Sjohnlev cpu[0]->cpu_m.mcpu_vcpu_info = &HYPERVISOR_shared_info->vcpu_info[0]; 1215084Sjohnlev #endif 1225084Sjohnlev 1230Sstevel@tonic-gate /* 124783Sdmick * Set up dummy cpu_pri_data values till psm spl code is 125783Sdmick * installed. This allows splx() to work on amd64. 126783Sdmick */ 127783Sdmick 128783Sdmick cpu[0]->cpu_pri_data = dummy_cpu_pri; 129783Sdmick 130783Sdmick /* 1310Sstevel@tonic-gate * check if we've got special bits to clear or set 1320Sstevel@tonic-gate * when checking cpu features 1330Sstevel@tonic-gate */ 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate cpuid_feature_ecx_include = 1362006Sandrei bootprop_getval("cpuid_feature_ecx_include"); 1370Sstevel@tonic-gate cpuid_feature_ecx_exclude = 1382006Sandrei bootprop_getval("cpuid_feature_ecx_exclude"); 1390Sstevel@tonic-gate cpuid_feature_edx_include = 1402006Sandrei bootprop_getval("cpuid_feature_edx_include"); 1410Sstevel@tonic-gate cpuid_feature_edx_exclude = 1422006Sandrei bootprop_getval("cpuid_feature_edx_exclude"); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* 1450Sstevel@tonic-gate * The first lightweight pass (pass0) through the cpuid data 1460Sstevel@tonic-gate * was done in locore before mlsetup was called. Do the next 1470Sstevel@tonic-gate * pass in C code. 1480Sstevel@tonic-gate * 1490Sstevel@tonic-gate * The x86_feature bits are set here on the basis of the capabilities 1500Sstevel@tonic-gate * of the boot CPU. Note that if we choose to support CPUs that have 1510Sstevel@tonic-gate * different feature sets (at which point we would almost certainly 1520Sstevel@tonic-gate * want to set the feature bits to correspond to the feature 1530Sstevel@tonic-gate * minimum) this value may be altered. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate x86_feature = cpuid_pass1(cpu[0]); 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1580Sstevel@tonic-gate * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss. 1590Sstevel@tonic-gate */ 1603446Smrj init_desctbls(); 1610Sstevel@tonic-gate 1625322Ssudheer #if !defined(__xpv) 1635322Ssudheer 1645322Ssudheer /* 1655322Ssudheer * Patch the tsc_read routine with appropriate set of instructions, 1665322Ssudheer * depending on the processor family and architecure, to read the 1675322Ssudheer * time-stamp counter while ensuring no out-of-order execution. 1685322Ssudheer * Patch it while the kernel text is still writable. 1695322Ssudheer * 1705322Ssudheer * Note: tsc_read is not patched for intel processors whose family 1715322Ssudheer * is >6 and for amd whose family >f (in case they don't support rdtscp 1725322Ssudheer * instruction, unlikely). By default tsc_read will use cpuid for 1735322Ssudheer * serialization in such cases. The following code needs to be 1745322Ssudheer * revisited if intel processors of family >= f retains the 1755322Ssudheer * instruction serialization nature of mfence instruction. 176*5338Ssudheer * Note: tsc_read is not patched for x86 processors which do 177*5338Ssudheer * not support "mfence". By default tsc_read will use cpuid for 178*5338Ssudheer * serialization in such cases. 1795322Ssudheer */ 1805322Ssudheer if (x86_feature & X86_TSCP) 1815322Ssudheer patch_tsc_read(X86_HAVE_TSCP); 1825322Ssudheer else if (cpuid_getvendor(CPU) == X86_VENDOR_AMD && 183*5338Ssudheer cpuid_getfamily(CPU) <= 0xf && (x86_feature & X86_SSE2) != 0) 1845322Ssudheer patch_tsc_read(X86_TSC_MFENCE); 1855322Ssudheer else if (cpuid_getvendor(CPU) == X86_VENDOR_Intel && 186*5338Ssudheer cpuid_getfamily(CPU) <= 6 && (x86_feature & X86_SSE2) != 0) 1875322Ssudheer patch_tsc_read(X86_TSC_MFENCE); 1885322Ssudheer 1895322Ssudheer #endif /* !__xpv */ 1900Sstevel@tonic-gate 1915084Sjohnlev #if defined(__i386) && !defined(__xpv) 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * Some i386 processors do not implement the rdtsc instruction, 1945322Ssudheer * or at least they do not implement it correctly. Patch them to 1955322Ssudheer * return 0. 1960Sstevel@tonic-gate */ 1975322Ssudheer if ((x86_feature & X86_TSC) == 0) 1985322Ssudheer patch_tsc_read(X86_NO_TSC); 1995084Sjohnlev #endif /* __i386 && !__xpv */ 2005084Sjohnlev 2015084Sjohnlev #if !defined(__xpv) 2025084Sjohnlev /* XXPV what, if anything, should be dorked with here under xen? */ 2033446Smrj 2043446Smrj /* 2053446Smrj * While we're thinking about the TSC, let's set up %cr4 so that 2063446Smrj * userland can issue rdtsc, and initialize the TSC_AUX value 2073446Smrj * (the cpuid) for the rdtscp instruction on appropriately 2083446Smrj * capable hardware. 2093446Smrj */ 2103446Smrj if (x86_feature & X86_TSC) 2113446Smrj setcr4(getcr4() & ~CR4_TSD); 2123446Smrj 2133446Smrj if (x86_feature & X86_TSCP) 2143446Smrj (void) wrmsr(MSR_AMD_TSCAUX, 0); 2153446Smrj 2163446Smrj if (x86_feature & X86_DE) 2173446Smrj setcr4(getcr4() | CR4_DE); 2185084Sjohnlev #endif /* __xpv */ 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * initialize t0 2220Sstevel@tonic-gate */ 2230Sstevel@tonic-gate t0.t_stk = (caddr_t)rp - MINFRAME; 2240Sstevel@tonic-gate t0.t_stkbase = t0stack; 2250Sstevel@tonic-gate t0.t_pri = maxclsyspri - 3; 2260Sstevel@tonic-gate t0.t_schedflag = TS_LOAD | TS_DONT_SWAP; 2270Sstevel@tonic-gate t0.t_procp = &p0; 2280Sstevel@tonic-gate t0.t_plockp = &p0lock.pl_lock; 2290Sstevel@tonic-gate t0.t_lwp = &lwp0; 2300Sstevel@tonic-gate t0.t_forw = &t0; 2310Sstevel@tonic-gate t0.t_back = &t0; 2320Sstevel@tonic-gate t0.t_next = &t0; 2330Sstevel@tonic-gate t0.t_prev = &t0; 2340Sstevel@tonic-gate t0.t_cpu = cpu[0]; 2350Sstevel@tonic-gate t0.t_disp_queue = &cpu0_disp; 2360Sstevel@tonic-gate t0.t_bind_cpu = PBIND_NONE; 2370Sstevel@tonic-gate t0.t_bind_pset = PS_NONE; 2380Sstevel@tonic-gate t0.t_cpupart = &cp_default; 2390Sstevel@tonic-gate t0.t_clfuncs = &sys_classfuncs.thread; 2400Sstevel@tonic-gate t0.t_copyops = NULL; 2410Sstevel@tonic-gate THREAD_ONPROC(&t0, CPU); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate lwp0.lwp_thread = &t0; 2443446Smrj lwp0.lwp_regs = (void *)rp; 2450Sstevel@tonic-gate lwp0.lwp_procp = &p0; 2460Sstevel@tonic-gate t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate p0.p_exec = NULL; 2490Sstevel@tonic-gate p0.p_stat = SRUN; 2500Sstevel@tonic-gate p0.p_flag = SSYS; 2510Sstevel@tonic-gate p0.p_tlist = &t0; 2520Sstevel@tonic-gate p0.p_stksize = 2*PAGESIZE; 2530Sstevel@tonic-gate p0.p_stkpageszc = 0; 2540Sstevel@tonic-gate p0.p_as = &kas; 2550Sstevel@tonic-gate p0.p_lockp = &p0lock; 2560Sstevel@tonic-gate p0.p_brkpageszc = 0; 2574426Saguzovsk p0.p_t1_lgrpid = LGRP_NONE; 2584426Saguzovsk p0.p_tr_lgrpid = LGRP_NONE; 2590Sstevel@tonic-gate sigorset(&p0.p_ignore, &ignoredefault); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate CPU->cpu_thread = &t0; 2620Sstevel@tonic-gate bzero(&cpu0_disp, sizeof (disp_t)); 2630Sstevel@tonic-gate CPU->cpu_disp = &cpu0_disp; 2640Sstevel@tonic-gate CPU->cpu_disp->disp_cpu = CPU; 2650Sstevel@tonic-gate CPU->cpu_dispthread = &t0; 2660Sstevel@tonic-gate CPU->cpu_idle_thread = &t0; 2670Sstevel@tonic-gate CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE; 2680Sstevel@tonic-gate CPU->cpu_dispatch_pri = t0.t_pri; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate CPU->cpu_id = 0; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate CPU->cpu_pri = 12; /* initial PIL for the boot CPU */ 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate /* 2751217Srab * The kernel doesn't use LDTs unless a process explicitly requests one. 2760Sstevel@tonic-gate */ 2775084Sjohnlev p0.p_ldt_desc = null_sdesc; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate /* 2803446Smrj * Initialize thread/cpu microstate accounting 2810Sstevel@tonic-gate */ 2820Sstevel@tonic-gate init_mstate(&t0, LMS_SYSTEM); 2830Sstevel@tonic-gate init_cpu_mstate(CPU, CMS_SYSTEM); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Initialize lists of available and active CPUs. 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate cpu_list_init(CPU); 2890Sstevel@tonic-gate 2903446Smrj /* 2913446Smrj * Now that we have taken over the GDT, IDT and have initialized 2923446Smrj * active CPU list it's time to inform kmdb if present. 2933446Smrj */ 2943446Smrj if (boothowto & RB_DEBUG) 2953446Smrj kdi_idt_sync(); 2963446Smrj 2973446Smrj /* 2983446Smrj * If requested (boot -d) drop into kmdb. 2993446Smrj * 3003446Smrj * This must be done after cpu_list_init() on the 64-bit kernel 3013446Smrj * since taking a trap requires that we re-compute gsbase based 3023446Smrj * on the cpu list. 3033446Smrj */ 3043446Smrj if (boothowto & RB_DEBUGENTER) 3053446Smrj kmdb_enter(); 3063446Smrj 307414Skchow cpu_vm_data_init(CPU); 308414Skchow 309748Sdmick /* lgrp_init() needs PCI config space access */ 3105084Sjohnlev #if defined(__xpv) 3115084Sjohnlev if (DOMAIN_IS_INITDOMAIN(xen_info)) 3125084Sjohnlev pci_cfgspace_init(); 3135084Sjohnlev #else 314748Sdmick pci_cfgspace_init(); 3155084Sjohnlev #endif 316748Sdmick 3170Sstevel@tonic-gate /* 3180Sstevel@tonic-gate * Initialize the lgrp framework 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate lgrp_init(); 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate rp->r_fp = 0; /* terminate kernel stack traces! */ 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate prom_init("kernel", (void *)NULL); 3250Sstevel@tonic-gate 3262006Sandrei boot_ncpus = bootprop_getval("boot-ncpus"); 3272006Sandrei 3282006Sandrei if (boot_ncpus <= 0 || boot_ncpus > NCPU) 3293446Smrj boot_ncpus = NCPU; 3302006Sandrei 3312006Sandrei max_ncpus = boot_max_ncpus = boot_ncpus; 3322006Sandrei 3330Sstevel@tonic-gate if (boothowto & RB_HALT) { 3340Sstevel@tonic-gate prom_printf("unix: kernel halted by -h flag\n"); 3350Sstevel@tonic-gate prom_enter_mon(); 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 3390Sstevel@tonic-gate 3405084Sjohnlev #if !defined(__xpv) 3414581Ssherrym /* 3424581Ssherrym * Fill out cpu_ucode_info. Update microcode if necessary. 3434581Ssherrym */ 3444581Ssherrym ucode_check(CPU); 3455084Sjohnlev #endif 3464581Ssherrym 3470Sstevel@tonic-gate if (workaround_errata(CPU) != 0) 3480Sstevel@tonic-gate panic("critical workaround(s) missing for boot cpu"); 3490Sstevel@tonic-gate } 350