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 /* 228906SEric.Saxe@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <sys/types.h> 273446Smrj #include <sys/sysmacros.h> 280Sstevel@tonic-gate #include <sys/disp.h> 290Sstevel@tonic-gate #include <sys/promif.h> 300Sstevel@tonic-gate #include <sys/clock.h> 310Sstevel@tonic-gate #include <sys/cpuvar.h> 320Sstevel@tonic-gate #include <sys/stack.h> 330Sstevel@tonic-gate #include <vm/as.h> 340Sstevel@tonic-gate #include <vm/hat.h> 350Sstevel@tonic-gate #include <sys/reboot.h> 360Sstevel@tonic-gate #include <sys/avintr.h> 370Sstevel@tonic-gate #include <sys/vtrace.h> 380Sstevel@tonic-gate #include <sys/proc.h> 390Sstevel@tonic-gate #include <sys/thread.h> 400Sstevel@tonic-gate #include <sys/cpupart.h> 410Sstevel@tonic-gate #include <sys/pset.h> 420Sstevel@tonic-gate #include <sys/copyops.h> 433434Sesaxe #include <sys/pg.h> 440Sstevel@tonic-gate #include <sys/disp.h> 450Sstevel@tonic-gate #include <sys/debug.h> 460Sstevel@tonic-gate #include <sys/sunddi.h> 470Sstevel@tonic-gate #include <sys/x86_archext.h> 480Sstevel@tonic-gate #include <sys/privregs.h> 490Sstevel@tonic-gate #include <sys/machsystm.h> 500Sstevel@tonic-gate #include <sys/ontrap.h> 510Sstevel@tonic-gate #include <sys/bootconf.h> 523446Smrj #include <sys/kdi_machimpl.h> 530Sstevel@tonic-gate #include <sys/archsystm.h> 540Sstevel@tonic-gate #include <sys/promif.h> 550Sstevel@tonic-gate #include <sys/bootconf.h> 56748Sdmick #include <sys/pci_cfgspace.h> 575084Sjohnlev #ifdef __xpv 585084Sjohnlev #include <sys/hypervisor.h> 595084Sjohnlev #endif 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* 620Sstevel@tonic-gate * some globals for patching the result of cpuid 630Sstevel@tonic-gate * to solve problems w/ creative cpu vendors 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate 660Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_include; 670Sstevel@tonic-gate extern uint32_t cpuid_feature_ecx_exclude; 680Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_include; 690Sstevel@tonic-gate extern uint32_t cpuid_feature_edx_exclude; 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 72783Sdmick * Dummy spl priority masks 73783Sdmick */ 743446Smrj static unsigned char dummy_cpu_pri[MAXIPL + 1] = { 75783Sdmick 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 76783Sdmick 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf 77783Sdmick }; 78783Sdmick 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* 810Sstevel@tonic-gate * Setup routine called right before main(). Interposing this function 820Sstevel@tonic-gate * before main() allows us to call it in a machine-independent fashion. 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate void 850Sstevel@tonic-gate mlsetup(struct regs *rp) 860Sstevel@tonic-gate { 87*9053Sjonathan.chew@sun.com u_longlong_t prop_value; 880Sstevel@tonic-gate extern struct classfuncs sys_classfuncs; 890Sstevel@tonic-gate extern disp_t cpu0_disp; 900Sstevel@tonic-gate extern char t0stack[]; 910Sstevel@tonic-gate 920Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* 950Sstevel@tonic-gate * initialize cpu_self 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate cpu[0]->cpu_self = cpu[0]; 980Sstevel@tonic-gate 995084Sjohnlev #if defined(__xpv) 1005084Sjohnlev /* 1015084Sjohnlev * Point at the hypervisor's virtual cpu structure 1025084Sjohnlev */ 1035084Sjohnlev cpu[0]->cpu_m.mcpu_vcpu_info = &HYPERVISOR_shared_info->vcpu_info[0]; 1045084Sjohnlev #endif 1055084Sjohnlev 1060Sstevel@tonic-gate /* 107783Sdmick * Set up dummy cpu_pri_data values till psm spl code is 108783Sdmick * installed. This allows splx() to work on amd64. 109783Sdmick */ 110783Sdmick 111783Sdmick cpu[0]->cpu_pri_data = dummy_cpu_pri; 112783Sdmick 113783Sdmick /* 1140Sstevel@tonic-gate * check if we've got special bits to clear or set 1150Sstevel@tonic-gate * when checking cpu features 1160Sstevel@tonic-gate */ 1170Sstevel@tonic-gate 118*9053Sjonathan.chew@sun.com if (bootprop_getval("cpuid_feature_ecx_include", &prop_value) != 0) 119*9053Sjonathan.chew@sun.com cpuid_feature_ecx_include = 0; 120*9053Sjonathan.chew@sun.com else 121*9053Sjonathan.chew@sun.com cpuid_feature_ecx_include = (uint32_t)prop_value; 122*9053Sjonathan.chew@sun.com 123*9053Sjonathan.chew@sun.com if (bootprop_getval("cpuid_feature_ecx_exclude", &prop_value) != 0) 124*9053Sjonathan.chew@sun.com cpuid_feature_ecx_exclude = 0; 125*9053Sjonathan.chew@sun.com else 126*9053Sjonathan.chew@sun.com cpuid_feature_ecx_exclude = (uint32_t)prop_value; 127*9053Sjonathan.chew@sun.com 128*9053Sjonathan.chew@sun.com if (bootprop_getval("cpuid_feature_edx_include", &prop_value) != 0) 129*9053Sjonathan.chew@sun.com cpuid_feature_edx_include = 0; 130*9053Sjonathan.chew@sun.com else 131*9053Sjonathan.chew@sun.com cpuid_feature_edx_include = (uint32_t)prop_value; 132*9053Sjonathan.chew@sun.com 133*9053Sjonathan.chew@sun.com if (bootprop_getval("cpuid_feature_edx_exclude", &prop_value) != 0) 134*9053Sjonathan.chew@sun.com cpuid_feature_edx_exclude = 0; 135*9053Sjonathan.chew@sun.com else 136*9053Sjonathan.chew@sun.com cpuid_feature_edx_exclude = (uint32_t)prop_value; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* 1390Sstevel@tonic-gate * The first lightweight pass (pass0) through the cpuid data 1400Sstevel@tonic-gate * was done in locore before mlsetup was called. Do the next 1410Sstevel@tonic-gate * pass in C code. 1420Sstevel@tonic-gate * 1430Sstevel@tonic-gate * The x86_feature bits are set here on the basis of the capabilities 1440Sstevel@tonic-gate * of the boot CPU. Note that if we choose to support CPUs that have 1450Sstevel@tonic-gate * different feature sets (at which point we would almost certainly 1460Sstevel@tonic-gate * want to set the feature bits to correspond to the feature 1470Sstevel@tonic-gate * minimum) this value may be altered. 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate x86_feature = cpuid_pass1(cpu[0]); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* 1520Sstevel@tonic-gate * Initialize idt0, gdt0, ldt0_default, ktss0 and dftss. 1530Sstevel@tonic-gate */ 1543446Smrj init_desctbls(); 1550Sstevel@tonic-gate 1565322Ssudheer #if !defined(__xpv) 1575322Ssudheer 1585322Ssudheer /* 1595322Ssudheer * Patch the tsc_read routine with appropriate set of instructions, 1605322Ssudheer * depending on the processor family and architecure, to read the 1615322Ssudheer * time-stamp counter while ensuring no out-of-order execution. 1625322Ssudheer * Patch it while the kernel text is still writable. 1635322Ssudheer * 1645322Ssudheer * Note: tsc_read is not patched for intel processors whose family 1655322Ssudheer * is >6 and for amd whose family >f (in case they don't support rdtscp 1665322Ssudheer * instruction, unlikely). By default tsc_read will use cpuid for 1675322Ssudheer * serialization in such cases. The following code needs to be 1685322Ssudheer * revisited if intel processors of family >= f retains the 1695322Ssudheer * instruction serialization nature of mfence instruction. 1705338Ssudheer * Note: tsc_read is not patched for x86 processors which do 1715338Ssudheer * not support "mfence". By default tsc_read will use cpuid for 1725338Ssudheer * serialization in such cases. 1735741Smrj * 1745741Smrj * The Xen hypervisor does not correctly report whether rdtscp is 1755741Smrj * supported or not, so we must assume that it is not. 1765322Ssudheer */ 1779000SStuart.Maybee@Sun.COM if (get_hwenv() != HW_XEN_HVM && (x86_feature & X86_TSCP)) 1785322Ssudheer patch_tsc_read(X86_HAVE_TSCP); 1795322Ssudheer else if (cpuid_getvendor(CPU) == X86_VENDOR_AMD && 1805338Ssudheer cpuid_getfamily(CPU) <= 0xf && (x86_feature & X86_SSE2) != 0) 1815322Ssudheer patch_tsc_read(X86_TSC_MFENCE); 1825322Ssudheer else if (cpuid_getvendor(CPU) == X86_VENDOR_Intel && 1835338Ssudheer cpuid_getfamily(CPU) <= 6 && (x86_feature & X86_SSE2) != 0) 1846642Ssudheer patch_tsc_read(X86_TSC_LFENCE); 1855322Ssudheer 1865322Ssudheer #endif /* !__xpv */ 1870Sstevel@tonic-gate 1885084Sjohnlev #if defined(__i386) && !defined(__xpv) 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Some i386 processors do not implement the rdtsc instruction, 1915322Ssudheer * or at least they do not implement it correctly. Patch them to 1925322Ssudheer * return 0. 1930Sstevel@tonic-gate */ 1945322Ssudheer if ((x86_feature & X86_TSC) == 0) 1955322Ssudheer patch_tsc_read(X86_NO_TSC); 1965084Sjohnlev #endif /* __i386 && !__xpv */ 1975084Sjohnlev 1988377SBill.Holler@Sun.COM #if defined(__amd64) && !defined(__xpv) 1998377SBill.Holler@Sun.COM patch_memops(cpuid_getvendor(CPU)); 2008377SBill.Holler@Sun.COM #endif /* __amd64 && !__xpv */ 2018377SBill.Holler@Sun.COM 2025084Sjohnlev #if !defined(__xpv) 2035084Sjohnlev /* XXPV what, if anything, should be dorked with here under xen? */ 2043446Smrj 2053446Smrj /* 2063446Smrj * While we're thinking about the TSC, let's set up %cr4 so that 2073446Smrj * userland can issue rdtsc, and initialize the TSC_AUX value 2083446Smrj * (the cpuid) for the rdtscp instruction on appropriately 2093446Smrj * capable hardware. 2103446Smrj */ 2113446Smrj if (x86_feature & X86_TSC) 2123446Smrj setcr4(getcr4() & ~CR4_TSD); 2133446Smrj 2143446Smrj if (x86_feature & X86_TSCP) 2153446Smrj (void) wrmsr(MSR_AMD_TSCAUX, 0); 2163446Smrj 2173446Smrj if (x86_feature & X86_DE) 2183446Smrj setcr4(getcr4() | CR4_DE); 2195084Sjohnlev #endif /* __xpv */ 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate /* 2220Sstevel@tonic-gate * initialize t0 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate t0.t_stk = (caddr_t)rp - MINFRAME; 2250Sstevel@tonic-gate t0.t_stkbase = t0stack; 2260Sstevel@tonic-gate t0.t_pri = maxclsyspri - 3; 2270Sstevel@tonic-gate t0.t_schedflag = TS_LOAD | TS_DONT_SWAP; 2280Sstevel@tonic-gate t0.t_procp = &p0; 2290Sstevel@tonic-gate t0.t_plockp = &p0lock.pl_lock; 2300Sstevel@tonic-gate t0.t_lwp = &lwp0; 2310Sstevel@tonic-gate t0.t_forw = &t0; 2320Sstevel@tonic-gate t0.t_back = &t0; 2330Sstevel@tonic-gate t0.t_next = &t0; 2340Sstevel@tonic-gate t0.t_prev = &t0; 2350Sstevel@tonic-gate t0.t_cpu = cpu[0]; 2360Sstevel@tonic-gate t0.t_disp_queue = &cpu0_disp; 2370Sstevel@tonic-gate t0.t_bind_cpu = PBIND_NONE; 2380Sstevel@tonic-gate t0.t_bind_pset = PS_NONE; 2396298Sakolb t0.t_bindflag = (uchar_t)default_binding_mode; 2400Sstevel@tonic-gate t0.t_cpupart = &cp_default; 2410Sstevel@tonic-gate t0.t_clfuncs = &sys_classfuncs.thread; 2420Sstevel@tonic-gate t0.t_copyops = NULL; 2430Sstevel@tonic-gate THREAD_ONPROC(&t0, CPU); 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate lwp0.lwp_thread = &t0; 2463446Smrj lwp0.lwp_regs = (void *)rp; 2470Sstevel@tonic-gate lwp0.lwp_procp = &p0; 2480Sstevel@tonic-gate t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate p0.p_exec = NULL; 2510Sstevel@tonic-gate p0.p_stat = SRUN; 2520Sstevel@tonic-gate p0.p_flag = SSYS; 2530Sstevel@tonic-gate p0.p_tlist = &t0; 2540Sstevel@tonic-gate p0.p_stksize = 2*PAGESIZE; 2550Sstevel@tonic-gate p0.p_stkpageszc = 0; 2560Sstevel@tonic-gate p0.p_as = &kas; 2570Sstevel@tonic-gate p0.p_lockp = &p0lock; 2580Sstevel@tonic-gate p0.p_brkpageszc = 0; 2594426Saguzovsk p0.p_t1_lgrpid = LGRP_NONE; 2604426Saguzovsk p0.p_tr_lgrpid = LGRP_NONE; 2610Sstevel@tonic-gate sigorset(&p0.p_ignore, &ignoredefault); 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate CPU->cpu_thread = &t0; 2640Sstevel@tonic-gate bzero(&cpu0_disp, sizeof (disp_t)); 2650Sstevel@tonic-gate CPU->cpu_disp = &cpu0_disp; 2660Sstevel@tonic-gate CPU->cpu_disp->disp_cpu = CPU; 2670Sstevel@tonic-gate CPU->cpu_dispthread = &t0; 2680Sstevel@tonic-gate CPU->cpu_idle_thread = &t0; 2690Sstevel@tonic-gate CPU->cpu_flags = CPU_READY | CPU_RUNNING | CPU_EXISTS | CPU_ENABLE; 2700Sstevel@tonic-gate CPU->cpu_dispatch_pri = t0.t_pri; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate CPU->cpu_id = 0; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate CPU->cpu_pri = 12; /* initial PIL for the boot CPU */ 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2771217Srab * The kernel doesn't use LDTs unless a process explicitly requests one. 2780Sstevel@tonic-gate */ 2795084Sjohnlev p0.p_ldt_desc = null_sdesc; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* 2823446Smrj * Initialize thread/cpu microstate accounting 2830Sstevel@tonic-gate */ 2840Sstevel@tonic-gate init_mstate(&t0, LMS_SYSTEM); 2850Sstevel@tonic-gate init_cpu_mstate(CPU, CMS_SYSTEM); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate /* 2880Sstevel@tonic-gate * Initialize lists of available and active CPUs. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate cpu_list_init(CPU); 2910Sstevel@tonic-gate 2928906SEric.Saxe@Sun.COM pg_cpu_bootstrap(CPU); 2938906SEric.Saxe@Sun.COM 2943446Smrj /* 2953446Smrj * Now that we have taken over the GDT, IDT and have initialized 2963446Smrj * active CPU list it's time to inform kmdb if present. 2973446Smrj */ 2983446Smrj if (boothowto & RB_DEBUG) 2993446Smrj kdi_idt_sync(); 3003446Smrj 3013446Smrj /* 3023446Smrj * If requested (boot -d) drop into kmdb. 3033446Smrj * 3043446Smrj * This must be done after cpu_list_init() on the 64-bit kernel 3053446Smrj * since taking a trap requires that we re-compute gsbase based 3063446Smrj * on the cpu list. 3073446Smrj */ 3083446Smrj if (boothowto & RB_DEBUGENTER) 3093446Smrj kmdb_enter(); 3103446Smrj 311414Skchow cpu_vm_data_init(CPU); 312414Skchow 313748Sdmick /* lgrp_init() needs PCI config space access */ 3145084Sjohnlev #if defined(__xpv) 3155084Sjohnlev if (DOMAIN_IS_INITDOMAIN(xen_info)) 3165084Sjohnlev pci_cfgspace_init(); 3175084Sjohnlev #else 318748Sdmick pci_cfgspace_init(); 3195084Sjohnlev #endif 320748Sdmick 3210Sstevel@tonic-gate rp->r_fp = 0; /* terminate kernel stack traces! */ 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate prom_init("kernel", (void *)NULL); 3240Sstevel@tonic-gate 325*9053Sjonathan.chew@sun.com if (bootprop_getval("boot-ncpus", &prop_value) != 0) 3263446Smrj boot_ncpus = NCPU; 327*9053Sjonathan.chew@sun.com else { 328*9053Sjonathan.chew@sun.com boot_ncpus = (int)prop_value; 329*9053Sjonathan.chew@sun.com if (boot_ncpus <= 0 || boot_ncpus > NCPU) 330*9053Sjonathan.chew@sun.com boot_ncpus = NCPU; 331*9053Sjonathan.chew@sun.com } 3322006Sandrei 3332006Sandrei max_ncpus = boot_max_ncpus = boot_ncpus; 3342006Sandrei 3356445Sjjc /* 3366445Sjjc * Initialize the lgrp framework 3376445Sjjc */ 3386445Sjjc lgrp_init(); 3396445Sjjc 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 3474581Ssherrym /* 3484581Ssherrym * Fill out cpu_ucode_info. Update microcode if necessary. 3494581Ssherrym */ 3504581Ssherrym ucode_check(CPU); 3514581Ssherrym 3520Sstevel@tonic-gate if (workaround_errata(CPU) != 0) 3530Sstevel@tonic-gate panic("critical workaround(s) missing for boot cpu"); 3540Sstevel@tonic-gate } 3555648Ssetje 3565648Ssetje 3575648Ssetje void 3585648Ssetje mach_modpath(char *path, const char *filename) 3595648Ssetje { 3605648Ssetje /* 3615648Ssetje * Construct the directory path from the filename. 3625648Ssetje */ 3635648Ssetje 3645648Ssetje int len; 3655648Ssetje char *p; 3665648Ssetje const char isastr[] = "/amd64"; 3675648Ssetje size_t isalen = strlen(isastr); 3685648Ssetje 3695648Ssetje if ((p = strrchr(filename, '/')) == NULL) 3705648Ssetje return; 3715648Ssetje 3725648Ssetje while (p > filename && *(p - 1) == '/') 3735648Ssetje p--; /* remove trailing '/' characters */ 3745648Ssetje if (p == filename) 3755648Ssetje p++; /* so "/" -is- the modpath in this case */ 3765648Ssetje 3775648Ssetje /* 3785648Ssetje * Remove optional isa-dependent directory name - the module 3795648Ssetje * subsystem will put this back again (!) 3805648Ssetje */ 3815648Ssetje len = p - filename; 3825648Ssetje if (len > isalen && 3835648Ssetje strncmp(&filename[len - isalen], isastr, isalen) == 0) 3845648Ssetje p -= isalen; 3855648Ssetje 3865648Ssetje /* 3875648Ssetje * "/platform/mumblefrotz" + " " + MOD_DEFPATH 3885648Ssetje */ 3895648Ssetje len += (p - filename) + 1 + strlen(MOD_DEFPATH) + 1; 3905648Ssetje (void) strncpy(path, filename, p - filename); 3915648Ssetje } 392