10Sstevel@tonic-gate /* 2*3446Smrj * CDDL HEADER START 3*3446Smrj * 4*3446Smrj * The contents of this file are subject to the terms of the 5*3446Smrj * Common Development and Distribution License (the "License"). 6*3446Smrj * You may not use this file except in compliance with the License. 7*3446Smrj * 8*3446Smrj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*3446Smrj * or http://www.opensolaris.org/os/licensing. 10*3446Smrj * See the License for the specific language governing permissions 11*3446Smrj * and limitations under the License. 12*3446Smrj * 13*3446Smrj * When distributing Covered Code, include this CDDL HEADER in each 14*3446Smrj * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*3446Smrj * If applicable, add the following below this CDDL HEADER, with the 16*3446Smrj * fields enclosed by brackets "[]" replaced with your own identifying 17*3446Smrj * information: Portions Copyright [yyyy] [name of copyright owner] 18*3446Smrj * 19*3446Smrj * CDDL HEADER END 20*3446Smrj */ 21*3446Smrj 22*3446Smrj /* 23*3446Smrj * Copyright 2007 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 /* 300Sstevel@tonic-gate * Copyright (c) 1992 Terrence R. Lambert. 310Sstevel@tonic-gate * Copyright (c) 1990 The Regents of the University of California. 320Sstevel@tonic-gate * All rights reserved. 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 350Sstevel@tonic-gate * William Jolitz. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 380Sstevel@tonic-gate * modification, are permitted provided that the following conditions 390Sstevel@tonic-gate * are met: 400Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 410Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 420Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 430Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 440Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 450Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 460Sstevel@tonic-gate * must display the following acknowledgement: 470Sstevel@tonic-gate * This product includes software developed by the University of 480Sstevel@tonic-gate * California, Berkeley and its contributors. 490Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 500Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 510Sstevel@tonic-gate * without specific prior written permission. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 540Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 550Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 560Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 570Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 580Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 590Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 600Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 610Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 620Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 630Sstevel@tonic-gate * SUCH DAMAGE. 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/types.h> 69*3446Smrj #include <sys/sysmacros.h> 700Sstevel@tonic-gate #include <sys/tss.h> 710Sstevel@tonic-gate #include <sys/segments.h> 720Sstevel@tonic-gate #include <sys/trap.h> 730Sstevel@tonic-gate #include <sys/cpuvar.h> 74*3446Smrj #include <sys/bootconf.h> 750Sstevel@tonic-gate #include <sys/x86_archext.h> 76*3446Smrj #include <sys/controlregs.h> 770Sstevel@tonic-gate #include <sys/archsystm.h> 780Sstevel@tonic-gate #include <sys/machsystm.h> 790Sstevel@tonic-gate #include <sys/kobj.h> 800Sstevel@tonic-gate #include <sys/cmn_err.h> 810Sstevel@tonic-gate #include <sys/reboot.h> 820Sstevel@tonic-gate #include <sys/kdi.h> 83*3446Smrj #include <sys/mach_mmu.h> 841217Srab #include <sys/systm.h> 85*3446Smrj #include <sys/promif.h> 86*3446Smrj #include <sys/bootinfo.h> 87*3446Smrj #include <vm/kboot_mmu.h> 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * cpu0 and default tables and structures. 910Sstevel@tonic-gate */ 92*3446Smrj user_desc_t *gdt0; 930Sstevel@tonic-gate desctbr_t gdt0_default_r; 940Sstevel@tonic-gate 950Sstevel@tonic-gate #pragma align 16(idt0) 960Sstevel@tonic-gate gate_desc_t idt0[NIDT]; /* interrupt descriptor table */ 97*3446Smrj #if defined(__i386) 980Sstevel@tonic-gate desctbr_t idt0_default_r; /* describes idt0 in IDTR format */ 99*3446Smrj #endif 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate #pragma align 16(ktss0) 1020Sstevel@tonic-gate struct tss ktss0; /* kernel task state structure */ 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate #if defined(__i386) 1050Sstevel@tonic-gate #pragma align 16(dftss0) 1060Sstevel@tonic-gate struct tss dftss0; /* #DF double-fault exception */ 1070Sstevel@tonic-gate #endif /* __i386 */ 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate user_desc_t zero_udesc; /* base zero user desc native procs */ 1101217Srab system_desc_t zero_sdesc; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate #if defined(__amd64) 1130Sstevel@tonic-gate user_desc_t zero_u32desc; /* 32-bit compatibility procs */ 1140Sstevel@tonic-gate #endif /* __amd64 */ 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate #pragma align 16(dblfault_stack0) 1170Sstevel@tonic-gate char dblfault_stack0[DEFAULTSTKSZ]; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate extern void fast_null(void); 1200Sstevel@tonic-gate extern hrtime_t get_hrtime(void); 1210Sstevel@tonic-gate extern hrtime_t gethrvtime(void); 1220Sstevel@tonic-gate extern hrtime_t get_hrestime(void); 1230Sstevel@tonic-gate extern uint64_t getlgrp(void); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate void (*(fasttable[]))(void) = { 1260Sstevel@tonic-gate fast_null, /* T_FNULL routine */ 1270Sstevel@tonic-gate fast_null, /* T_FGETFP routine (initially null) */ 1280Sstevel@tonic-gate fast_null, /* T_FSETFP routine (initially null) */ 1290Sstevel@tonic-gate (void (*)())get_hrtime, /* T_GETHRTIME */ 1300Sstevel@tonic-gate (void (*)())gethrvtime, /* T_GETHRVTIME */ 1310Sstevel@tonic-gate (void (*)())get_hrestime, /* T_GETHRESTIME */ 1320Sstevel@tonic-gate (void (*)())getlgrp /* T_GETLGRP */ 1330Sstevel@tonic-gate }; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* 1362712Snn35248 * Structure containing pre-computed descriptors to allow us to temporarily 1372712Snn35248 * interpose on a standard handler. 1382712Snn35248 */ 1392712Snn35248 struct interposing_handler { 1402712Snn35248 int ih_inum; 1412712Snn35248 gate_desc_t ih_interp_desc; 1422712Snn35248 gate_desc_t ih_default_desc; 1432712Snn35248 }; 1442712Snn35248 1452712Snn35248 /* 1462712Snn35248 * The brand infrastructure interposes on two handlers, and we use one as a 1472712Snn35248 * NULL signpost. 1482712Snn35248 */ 1492712Snn35248 static struct interposing_handler brand_tbl[3]; 1502712Snn35248 1512712Snn35248 /* 1520Sstevel@tonic-gate * software prototypes for default local descriptor table 1530Sstevel@tonic-gate */ 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * Routines for loading segment descriptors in format the hardware 1570Sstevel@tonic-gate * can understand. 1580Sstevel@tonic-gate */ 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate #if defined(__amd64) 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * In long mode we have the new L or long mode attribute bit 1640Sstevel@tonic-gate * for code segments. Only the conforming bit in type is used along 1650Sstevel@tonic-gate * with descriptor priority and present bits. Default operand size must 1660Sstevel@tonic-gate * be zero when in long mode. In 32-bit compatibility mode all fields 1670Sstevel@tonic-gate * are treated as in legacy mode. For data segments while in long mode 1680Sstevel@tonic-gate * only the present bit is loaded. 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate void 1710Sstevel@tonic-gate set_usegd(user_desc_t *dp, uint_t lmode, void *base, size_t size, 1720Sstevel@tonic-gate uint_t type, uint_t dpl, uint_t gran, uint_t defopsz) 1730Sstevel@tonic-gate { 1740Sstevel@tonic-gate ASSERT(lmode == SDP_SHORT || lmode == SDP_LONG); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate /* 1770Sstevel@tonic-gate * 64-bit long mode. 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate if (lmode == SDP_LONG) 1800Sstevel@tonic-gate dp->usd_def32 = 0; /* 32-bit operands only */ 1810Sstevel@tonic-gate else 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * 32-bit compatibility mode. 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate dp->usd_def32 = defopsz; /* 0 = 16, 1 = 32-bit ops */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate dp->usd_long = lmode; /* 64-bit mode */ 1880Sstevel@tonic-gate dp->usd_type = type; 1890Sstevel@tonic-gate dp->usd_dpl = dpl; 1900Sstevel@tonic-gate dp->usd_p = 1; 1910Sstevel@tonic-gate dp->usd_gran = gran; /* 0 = bytes, 1 = pages */ 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate dp->usd_lobase = (uintptr_t)base; 1940Sstevel@tonic-gate dp->usd_midbase = (uintptr_t)base >> 16; 1950Sstevel@tonic-gate dp->usd_hibase = (uintptr_t)base >> (16 + 8); 1960Sstevel@tonic-gate dp->usd_lolimit = size; 1970Sstevel@tonic-gate dp->usd_hilimit = (uintptr_t)size >> 16; 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate #elif defined(__i386) 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* 2030Sstevel@tonic-gate * Install user segment descriptor for code and data. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate void 2060Sstevel@tonic-gate set_usegd(user_desc_t *dp, void *base, size_t size, uint_t type, 2070Sstevel@tonic-gate uint_t dpl, uint_t gran, uint_t defopsz) 2080Sstevel@tonic-gate { 2090Sstevel@tonic-gate dp->usd_lolimit = size; 2100Sstevel@tonic-gate dp->usd_hilimit = (uintptr_t)size >> 16; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate dp->usd_lobase = (uintptr_t)base; 2130Sstevel@tonic-gate dp->usd_midbase = (uintptr_t)base >> 16; 2140Sstevel@tonic-gate dp->usd_hibase = (uintptr_t)base >> (16 + 8); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate dp->usd_type = type; 2170Sstevel@tonic-gate dp->usd_dpl = dpl; 2180Sstevel@tonic-gate dp->usd_p = 1; 2190Sstevel@tonic-gate dp->usd_def32 = defopsz; /* 0 = 16, 1 = 32 bit operands */ 2200Sstevel@tonic-gate dp->usd_gran = gran; /* 0 = bytes, 1 = pages */ 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate #endif /* __i386 */ 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* 2260Sstevel@tonic-gate * Install system segment descriptor for LDT and TSS segments. 2270Sstevel@tonic-gate */ 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate #if defined(__amd64) 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate void 2320Sstevel@tonic-gate set_syssegd(system_desc_t *dp, void *base, size_t size, uint_t type, 2330Sstevel@tonic-gate uint_t dpl) 2340Sstevel@tonic-gate { 2350Sstevel@tonic-gate dp->ssd_lolimit = size; 2360Sstevel@tonic-gate dp->ssd_hilimit = (uintptr_t)size >> 16; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate dp->ssd_lobase = (uintptr_t)base; 2390Sstevel@tonic-gate dp->ssd_midbase = (uintptr_t)base >> 16; 2400Sstevel@tonic-gate dp->ssd_hibase = (uintptr_t)base >> (16 + 8); 2410Sstevel@tonic-gate dp->ssd_hi64base = (uintptr_t)base >> (16 + 8 + 8); 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate dp->ssd_type = type; 2440Sstevel@tonic-gate dp->ssd_zero1 = 0; /* must be zero */ 2450Sstevel@tonic-gate dp->ssd_zero2 = 0; 2460Sstevel@tonic-gate dp->ssd_dpl = dpl; 2470Sstevel@tonic-gate dp->ssd_p = 1; 2480Sstevel@tonic-gate dp->ssd_gran = 0; /* force byte units */ 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate #elif defined(__i386) 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate void 2540Sstevel@tonic-gate set_syssegd(system_desc_t *dp, void *base, size_t size, uint_t type, 2550Sstevel@tonic-gate uint_t dpl) 2560Sstevel@tonic-gate { 2570Sstevel@tonic-gate dp->ssd_lolimit = size; 2580Sstevel@tonic-gate dp->ssd_hilimit = (uintptr_t)size >> 16; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate dp->ssd_lobase = (uintptr_t)base; 2610Sstevel@tonic-gate dp->ssd_midbase = (uintptr_t)base >> 16; 2620Sstevel@tonic-gate dp->ssd_hibase = (uintptr_t)base >> (16 + 8); 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate dp->ssd_type = type; 2650Sstevel@tonic-gate dp->ssd_zero = 0; /* must be zero */ 2660Sstevel@tonic-gate dp->ssd_dpl = dpl; 2670Sstevel@tonic-gate dp->ssd_p = 1; 2680Sstevel@tonic-gate dp->ssd_gran = 0; /* force byte units */ 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate #endif /* __i386 */ 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate /* 2740Sstevel@tonic-gate * Install gate segment descriptor for interrupt, trap, call and task gates. 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate #if defined(__amd64) 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate void 280*3446Smrj set_gatesegd(gate_desc_t *dp, void (*func)(void), selector_t sel, 2810Sstevel@tonic-gate uint_t type, uint_t dpl) 2820Sstevel@tonic-gate { 2830Sstevel@tonic-gate dp->sgd_looffset = (uintptr_t)func; 2840Sstevel@tonic-gate dp->sgd_hioffset = (uintptr_t)func >> 16; 2850Sstevel@tonic-gate dp->sgd_hi64offset = (uintptr_t)func >> (16 + 16); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate dp->sgd_selector = (uint16_t)sel; 288*3446Smrj 289*3446Smrj /* 290*3446Smrj * For 64 bit native we use the IST stack mechanism 291*3446Smrj * for double faults. All other traps use the CPL = 0 292*3446Smrj * (tss_rsp0) stack. 293*3446Smrj */ 294*3446Smrj if (type == T_DBLFLT) 295*3446Smrj dp->sgd_ist = 1; 296*3446Smrj else 297*3446Smrj dp->sgd_ist = 0; 298*3446Smrj 2990Sstevel@tonic-gate dp->sgd_type = type; 3000Sstevel@tonic-gate dp->sgd_dpl = dpl; 3010Sstevel@tonic-gate dp->sgd_p = 1; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate #elif defined(__i386) 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate void 3070Sstevel@tonic-gate set_gatesegd(gate_desc_t *dp, void (*func)(void), selector_t sel, 308*3446Smrj uint_t type, uint_t dpl) 3090Sstevel@tonic-gate { 3100Sstevel@tonic-gate dp->sgd_looffset = (uintptr_t)func; 3110Sstevel@tonic-gate dp->sgd_hioffset = (uintptr_t)func >> 16; 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate dp->sgd_selector = (uint16_t)sel; 314*3446Smrj dp->sgd_stkcpy = 0; /* always zero bytes */ 3150Sstevel@tonic-gate dp->sgd_type = type; 3160Sstevel@tonic-gate dp->sgd_dpl = dpl; 3170Sstevel@tonic-gate dp->sgd_p = 1; 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 320*3446Smrj #endif /* __i386 */ 321*3446Smrj 322*3446Smrj #if defined(__amd64) 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Build kernel GDT. 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate static void 329*3446Smrj init_gdt_common(user_desc_t *gdt) 3300Sstevel@tonic-gate { 331*3446Smrj int i; 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate /* 3340Sstevel@tonic-gate * 64-bit kernel code segment. 3350Sstevel@tonic-gate */ 336*3446Smrj set_usegd(&gdt[GDT_KCODE], SDP_LONG, NULL, 0, SDT_MEMERA, SEL_KPL, 3370Sstevel@tonic-gate SDP_PAGES, SDP_OP32); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* 3400Sstevel@tonic-gate * 64-bit kernel data segment. The limit attribute is ignored in 64-bit 3410Sstevel@tonic-gate * mode, but we set it here to 0xFFFF so that we can use the SYSRET 3420Sstevel@tonic-gate * instruction to return from system calls back to 32-bit applications. 3430Sstevel@tonic-gate * SYSRET doesn't update the base, limit, or attributes of %ss or %ds 3440Sstevel@tonic-gate * descriptors. We therefore must ensure that the kernel uses something, 3450Sstevel@tonic-gate * though it will be ignored by hardware, that is compatible with 32-bit 3460Sstevel@tonic-gate * apps. For the same reason we must set the default op size of this 3470Sstevel@tonic-gate * descriptor to 32-bit operands. 3480Sstevel@tonic-gate */ 349*3446Smrj set_usegd(&gdt[GDT_KDATA], SDP_LONG, NULL, -1, SDT_MEMRWA, 3500Sstevel@tonic-gate SEL_KPL, SDP_PAGES, SDP_OP32); 351*3446Smrj gdt[GDT_KDATA].usd_def32 = 1; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate /* 3540Sstevel@tonic-gate * 64-bit user code segment. 3550Sstevel@tonic-gate */ 356*3446Smrj set_usegd(&gdt[GDT_UCODE], SDP_LONG, NULL, 0, SDT_MEMERA, SEL_UPL, 3570Sstevel@tonic-gate SDP_PAGES, SDP_OP32); 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * 32-bit user code segment. 3610Sstevel@tonic-gate */ 362*3446Smrj set_usegd(&gdt[GDT_U32CODE], SDP_SHORT, NULL, -1, SDT_MEMERA, 3630Sstevel@tonic-gate SEL_UPL, SDP_PAGES, SDP_OP32); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * 32 and 64 bit data segments can actually share the same descriptor. 3670Sstevel@tonic-gate * In long mode only the present bit is checked but all other fields 3680Sstevel@tonic-gate * are loaded. But in compatibility mode all fields are interpreted 3690Sstevel@tonic-gate * as in legacy mode so they must be set correctly for a 32-bit data 3700Sstevel@tonic-gate * segment. 3710Sstevel@tonic-gate */ 372*3446Smrj set_usegd(&gdt[GDT_UDATA], SDP_SHORT, NULL, -1, SDT_MEMRWA, SEL_UPL, 3730Sstevel@tonic-gate SDP_PAGES, SDP_OP32); 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate /* 3761217Srab * The 64-bit kernel has no default LDT. By default, the LDT descriptor 3771217Srab * in the GDT is 0. 3780Sstevel@tonic-gate */ 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* 3810Sstevel@tonic-gate * Kernel TSS 3820Sstevel@tonic-gate */ 383*3446Smrj set_syssegd((system_desc_t *)&gdt[GDT_KTSS], &ktss0, 3840Sstevel@tonic-gate sizeof (ktss0) - 1, SDT_SYSTSS, SEL_KPL); 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* 3870Sstevel@tonic-gate * Initialize fs and gs descriptors for 32 bit processes. 3880Sstevel@tonic-gate * Only attributes and limits are initialized, the effective 3890Sstevel@tonic-gate * base address is programmed via fsbase/gsbase. 3900Sstevel@tonic-gate */ 391*3446Smrj set_usegd(&gdt[GDT_LWPFS], SDP_SHORT, NULL, -1, SDT_MEMRWA, 3920Sstevel@tonic-gate SEL_UPL, SDP_PAGES, SDP_OP32); 393*3446Smrj set_usegd(&gdt[GDT_LWPGS], SDP_SHORT, NULL, -1, SDT_MEMRWA, 3940Sstevel@tonic-gate SEL_UPL, SDP_PAGES, SDP_OP32); 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /* 3972712Snn35248 * Initialize the descriptors set aside for brand usage. 3982712Snn35248 * Only attributes and limits are initialized. 3992712Snn35248 */ 4002712Snn35248 for (i = GDT_BRANDMIN; i <= GDT_BRANDMAX; i++) 401*3446Smrj set_usegd(&gdt0[i], SDP_SHORT, NULL, -1, SDT_MEMRWA, 4022712Snn35248 SEL_UPL, SDP_PAGES, SDP_OP32); 4032712Snn35248 4042712Snn35248 /* 4050Sstevel@tonic-gate * Initialize convenient zero base user descriptors for clearing 4060Sstevel@tonic-gate * lwp private %fs and %gs descriptors in GDT. See setregs() for 4070Sstevel@tonic-gate * an example. 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate set_usegd(&zero_udesc, SDP_LONG, 0, 0, SDT_MEMRWA, SEL_UPL, 4100Sstevel@tonic-gate SDP_BYTES, SDP_OP32); 4110Sstevel@tonic-gate set_usegd(&zero_u32desc, SDP_SHORT, 0, -1, SDT_MEMRWA, SEL_UPL, 4120Sstevel@tonic-gate SDP_PAGES, SDP_OP32); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 415*3446Smrj static user_desc_t * 4160Sstevel@tonic-gate init_gdt(void) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate desctbr_t r_bgdt, r_gdt; 4190Sstevel@tonic-gate user_desc_t *bgdt; 420*3446Smrj 421*3446Smrj #if !defined(__lint) 422*3446Smrj /* 423*3446Smrj * Our gdt is never larger than a single page. 424*3446Smrj */ 425*3446Smrj ASSERT((sizeof (*gdt0) * NGDT) <= PAGESIZE); 426*3446Smrj #endif 427*3446Smrj gdt0 = (user_desc_t *)BOP_ALLOC(bootops, (caddr_t)GDT_VA, 428*3446Smrj PAGESIZE, PAGESIZE); 429*3446Smrj if (gdt0 == NULL) 430*3446Smrj panic("init_gdt: BOP_ALLOC failed"); 431*3446Smrj bzero(gdt0, PAGESIZE); 432*3446Smrj 433*3446Smrj init_gdt_common(gdt0); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* 436*3446Smrj * Copy in from boot's gdt to our gdt. 437*3446Smrj * Entry 0 is the null descriptor by definition. 4380Sstevel@tonic-gate */ 4390Sstevel@tonic-gate rd_gdtr(&r_bgdt); 4400Sstevel@tonic-gate bgdt = (user_desc_t *)r_bgdt.dtr_base; 4410Sstevel@tonic-gate if (bgdt == NULL) 4420Sstevel@tonic-gate panic("null boot gdt"); 4430Sstevel@tonic-gate 444*3446Smrj gdt0[GDT_B32DATA] = bgdt[GDT_B32DATA]; 445*3446Smrj gdt0[GDT_B32CODE] = bgdt[GDT_B32CODE]; 446*3446Smrj gdt0[GDT_B16CODE] = bgdt[GDT_B16CODE]; 447*3446Smrj gdt0[GDT_B16DATA] = bgdt[GDT_B16DATA]; 448*3446Smrj gdt0[GDT_B64CODE] = bgdt[GDT_B64CODE]; 449*3446Smrj 450*3446Smrj /* 451*3446Smrj * Install our new GDT 452*3446Smrj */ 453*3446Smrj r_gdt.dtr_limit = (sizeof (*gdt0) * NGDT) - 1; 454*3446Smrj r_gdt.dtr_base = (uintptr_t)gdt0; 455*3446Smrj wr_gdtr(&r_gdt); 456*3446Smrj 457*3446Smrj /* 458*3446Smrj * Reload the segment registers to use the new GDT 459*3446Smrj */ 460*3446Smrj load_segment_registers(KCS_SEL, KFS_SEL, KGS_SEL, KDS_SEL); 461*3446Smrj 462*3446Smrj /* 463*3446Smrj * setup %gs for kernel 464*3446Smrj */ 465*3446Smrj wrmsr(MSR_AMD_GSBASE, (uint64_t)&cpus[0]); 466*3446Smrj 467*3446Smrj /* 468*3446Smrj * XX64 We should never dereference off "other gsbase" or 469*3446Smrj * "fsbase". So, we should arrange to point FSBASE and 470*3446Smrj * KGSBASE somewhere truly awful e.g. point it at the last 471*3446Smrj * valid address below the hole so that any attempts to index 472*3446Smrj * off them cause an exception. 473*3446Smrj * 474*3446Smrj * For now, point it at 8G -- at least it should be unmapped 475*3446Smrj * until some 64-bit processes run. 476*3446Smrj */ 477*3446Smrj wrmsr(MSR_AMD_FSBASE, 0x200000000ul); 478*3446Smrj wrmsr(MSR_AMD_KGSBASE, 0x200000000ul); 479*3446Smrj return (gdt0); 480*3446Smrj } 481*3446Smrj 482*3446Smrj #elif defined(__i386) 483*3446Smrj 484*3446Smrj static void 485*3446Smrj init_gdt_common(user_desc_t *gdt) 486*3446Smrj { 487*3446Smrj int i; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* 4900Sstevel@tonic-gate * Text and data for both kernel and user span entire 32 bit 4910Sstevel@tonic-gate * address space. 4920Sstevel@tonic-gate */ 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate /* 4950Sstevel@tonic-gate * kernel code segment. 4960Sstevel@tonic-gate */ 497*3446Smrj set_usegd(&gdt[GDT_KCODE], NULL, -1, SDT_MEMERA, SEL_KPL, SDP_PAGES, 4980Sstevel@tonic-gate SDP_OP32); 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * kernel data segment. 5020Sstevel@tonic-gate */ 503*3446Smrj set_usegd(&gdt[GDT_KDATA], NULL, -1, SDT_MEMRWA, SEL_KPL, SDP_PAGES, 5040Sstevel@tonic-gate SDP_OP32); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * user code segment. 5080Sstevel@tonic-gate */ 509*3446Smrj set_usegd(&gdt[GDT_UCODE], NULL, -1, SDT_MEMERA, SEL_UPL, SDP_PAGES, 5100Sstevel@tonic-gate SDP_OP32); 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* 5130Sstevel@tonic-gate * user data segment. 5140Sstevel@tonic-gate */ 515*3446Smrj set_usegd(&gdt[GDT_UDATA], NULL, -1, SDT_MEMRWA, SEL_UPL, SDP_PAGES, 5160Sstevel@tonic-gate SDP_OP32); 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * TSS for T_DBLFLT (double fault) handler 5200Sstevel@tonic-gate */ 521*3446Smrj set_syssegd((system_desc_t *)&gdt[GDT_DBFLT], &dftss0, 5220Sstevel@tonic-gate sizeof (dftss0) - 1, SDT_SYSTSS, SEL_KPL); 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * TSS for kernel 5260Sstevel@tonic-gate */ 527*3446Smrj set_syssegd((system_desc_t *)&gdt[GDT_KTSS], &ktss0, 5280Sstevel@tonic-gate sizeof (ktss0) - 1, SDT_SYSTSS, SEL_KPL); 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * %gs selector for kernel 5320Sstevel@tonic-gate */ 533*3446Smrj set_usegd(&gdt[GDT_GS], &cpus[0], sizeof (struct cpu) -1, SDT_MEMRWA, 5340Sstevel@tonic-gate SEL_KPL, SDP_BYTES, SDP_OP32); 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * Initialize lwp private descriptors. 5380Sstevel@tonic-gate * Only attributes and limits are initialized, the effective 5390Sstevel@tonic-gate * base address is programmed via fsbase/gsbase. 5400Sstevel@tonic-gate */ 541*3446Smrj set_usegd(&gdt[GDT_LWPFS], NULL, (size_t)-1, SDT_MEMRWA, SEL_UPL, 5420Sstevel@tonic-gate SDP_PAGES, SDP_OP32); 543*3446Smrj set_usegd(&gdt[GDT_LWPGS], NULL, (size_t)-1, SDT_MEMRWA, SEL_UPL, 5440Sstevel@tonic-gate SDP_PAGES, SDP_OP32); 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate /* 5472712Snn35248 * Initialize the descriptors set aside for brand usage. 5482712Snn35248 * Only attributes and limits are initialized. 5492712Snn35248 */ 5502712Snn35248 for (i = GDT_BRANDMIN; i <= GDT_BRANDMAX; i++) 5512712Snn35248 set_usegd(&gdt0[i], NULL, (size_t)-1, SDT_MEMRWA, SEL_UPL, 5522712Snn35248 SDP_PAGES, SDP_OP32); 553*3446Smrj /* 554*3446Smrj * Initialize convenient zero base user descriptor for clearing 555*3446Smrj * lwp private %fs and %gs descriptors in GDT. See setregs() for 556*3446Smrj * an example. 557*3446Smrj */ 558*3446Smrj set_usegd(&zero_udesc, NULL, -1, SDT_MEMRWA, SEL_UPL, 559*3446Smrj SDP_BYTES, SDP_OP32); 560*3446Smrj } 561*3446Smrj 562*3446Smrj static user_desc_t * 563*3446Smrj init_gdt(void) 564*3446Smrj { 565*3446Smrj desctbr_t r_bgdt, r_gdt; 566*3446Smrj user_desc_t *bgdt; 567*3446Smrj 568*3446Smrj #if !defined(__lint) 569*3446Smrj /* 570*3446Smrj * Our gdt is never larger than a single page. 571*3446Smrj */ 572*3446Smrj ASSERT((sizeof (*gdt0) * NGDT) <= PAGESIZE); 573*3446Smrj #endif 574*3446Smrj /* 575*3446Smrj * XXX this allocation belongs in our caller, not here. 576*3446Smrj */ 577*3446Smrj gdt0 = (user_desc_t *)BOP_ALLOC(bootops, (caddr_t)GDT_VA, 578*3446Smrj PAGESIZE, PAGESIZE); 579*3446Smrj if (gdt0 == NULL) 580*3446Smrj panic("init_gdt: BOP_ALLOC failed"); 581*3446Smrj bzero(gdt0, PAGESIZE); 582*3446Smrj 583*3446Smrj init_gdt_common(gdt0); 584*3446Smrj 585*3446Smrj /* 586*3446Smrj * Copy in from boot's gdt to our gdt entries. 587*3446Smrj * Entry 0 is null descriptor by definition. 588*3446Smrj */ 589*3446Smrj rd_gdtr(&r_bgdt); 590*3446Smrj bgdt = (user_desc_t *)r_bgdt.dtr_base; 591*3446Smrj if (bgdt == NULL) 592*3446Smrj panic("null boot gdt"); 593*3446Smrj 594*3446Smrj gdt0[GDT_B32DATA] = bgdt[GDT_B32DATA]; 595*3446Smrj gdt0[GDT_B32CODE] = bgdt[GDT_B32CODE]; 596*3446Smrj gdt0[GDT_B16CODE] = bgdt[GDT_B16CODE]; 597*3446Smrj gdt0[GDT_B16DATA] = bgdt[GDT_B16DATA]; 5982712Snn35248 5992712Snn35248 /* 6000Sstevel@tonic-gate * Install our new GDT 6010Sstevel@tonic-gate */ 602*3446Smrj r_gdt.dtr_limit = (sizeof (*gdt0) * NGDT) - 1; 6030Sstevel@tonic-gate r_gdt.dtr_base = (uintptr_t)gdt0; 6040Sstevel@tonic-gate wr_gdtr(&r_gdt); 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate /* 607*3446Smrj * Reload the segment registers to use the new GDT 6080Sstevel@tonic-gate */ 609*3446Smrj load_segment_registers( 610*3446Smrj KCS_SEL, KDS_SEL, KDS_SEL, KFS_SEL, KGS_SEL, KDS_SEL); 611*3446Smrj 612*3446Smrj return (gdt0); 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate #endif /* __i386 */ 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * Build kernel IDT. 6190Sstevel@tonic-gate * 620*3446Smrj * Note that for amd64 we pretty much require every gate to be an interrupt 621*3446Smrj * gate which blocks interrupts atomically on entry; that's because of our 622*3446Smrj * dependency on using 'swapgs' every time we come into the kernel to find 623*3446Smrj * the cpu structure. If we get interrupted just before doing that, %cs could 624*3446Smrj * be in kernel mode (so that the trap prolog doesn't do a swapgs), but 625*3446Smrj * %gsbase is really still pointing at something in userland. Bad things will 626*3446Smrj * ensue. We also use interrupt gates for i386 as well even though this is not 627*3446Smrj * required for some traps. 6280Sstevel@tonic-gate * 6290Sstevel@tonic-gate * Perhaps they should have invented a trap gate that does an atomic swapgs? 6300Sstevel@tonic-gate */ 6310Sstevel@tonic-gate static void 632*3446Smrj init_idt_common(gate_desc_t *idt) 633*3446Smrj { 634*3446Smrj set_gatesegd(&idt[T_ZERODIV], &div0trap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 635*3446Smrj set_gatesegd(&idt[T_SGLSTP], &dbgtrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 636*3446Smrj set_gatesegd(&idt[T_NMIFLT], &nmiint, KCS_SEL, SDT_SYSIGT, SEL_KPL); 637*3446Smrj set_gatesegd(&idt[T_BPTFLT], &brktrap, KCS_SEL, SDT_SYSIGT, SEL_UPL); 638*3446Smrj set_gatesegd(&idt[T_OVFLW], &ovflotrap, KCS_SEL, SDT_SYSIGT, SEL_UPL); 639*3446Smrj set_gatesegd(&idt[T_BOUNDFLT], &boundstrap, KCS_SEL, SDT_SYSIGT, 640*3446Smrj SEL_KPL); 641*3446Smrj set_gatesegd(&idt[T_ILLINST], &invoptrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 642*3446Smrj set_gatesegd(&idt[T_NOEXTFLT], &ndptrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 643*3446Smrj 644*3446Smrj /* 645*3446Smrj * double fault handler. 646*3446Smrj */ 647*3446Smrj #if defined(__amd64) 648*3446Smrj set_gatesegd(&idt[T_DBLFLT], &syserrtrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 649*3446Smrj #elif defined(__i386) 650*3446Smrj /* 651*3446Smrj * task gate required. 652*3446Smrj */ 653*3446Smrj set_gatesegd(&idt[T_DBLFLT], NULL, DFTSS_SEL, SDT_SYSTASKGT, SEL_KPL); 654*3446Smrj 655*3446Smrj #endif /* __i386 */ 656*3446Smrj 657*3446Smrj /* 658*3446Smrj * T_EXTOVRFLT coprocessor-segment-overrun not supported. 659*3446Smrj */ 660*3446Smrj 661*3446Smrj set_gatesegd(&idt[T_TSSFLT], &invtsstrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 662*3446Smrj set_gatesegd(&idt[T_SEGFLT], &segnptrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 663*3446Smrj set_gatesegd(&idt[T_STKFLT], &stktrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 664*3446Smrj set_gatesegd(&idt[T_GPFLT], &gptrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 665*3446Smrj set_gatesegd(&idt[T_PGFLT], &pftrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 666*3446Smrj set_gatesegd(&idt[T_EXTERRFLT], &ndperr, KCS_SEL, SDT_SYSIGT, SEL_KPL); 667*3446Smrj set_gatesegd(&idt[T_ALIGNMENT], &achktrap, KCS_SEL, SDT_SYSIGT, 668*3446Smrj SEL_KPL); 669*3446Smrj set_gatesegd(&idt[T_MCE], &mcetrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 670*3446Smrj set_gatesegd(&idt[T_SIMDFPE], &xmtrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 671*3446Smrj 672*3446Smrj /* 673*3446Smrj * install "int80" handler at, well, 0x80. 674*3446Smrj */ 675*3446Smrj set_gatesegd(&idt0[T_INT80], &sys_int80, KCS_SEL, SDT_SYSIGT, SEL_UPL); 676*3446Smrj 677*3446Smrj /* 678*3446Smrj * install fast trap handler at 210. 679*3446Smrj */ 680*3446Smrj set_gatesegd(&idt[T_FASTTRAP], &fasttrap, KCS_SEL, SDT_SYSIGT, SEL_UPL); 681*3446Smrj 682*3446Smrj /* 683*3446Smrj * System call handler. 684*3446Smrj */ 685*3446Smrj #if defined(__amd64) 686*3446Smrj set_gatesegd(&idt[T_SYSCALLINT], &sys_syscall_int, KCS_SEL, SDT_SYSIGT, 687*3446Smrj SEL_UPL); 688*3446Smrj 689*3446Smrj #elif defined(__i386) 690*3446Smrj set_gatesegd(&idt[T_SYSCALLINT], &sys_call, KCS_SEL, SDT_SYSIGT, 691*3446Smrj SEL_UPL); 692*3446Smrj #endif /* __i386 */ 693*3446Smrj 694*3446Smrj /* 695*3446Smrj * Install the DTrace interrupt handler for the pid provider. 696*3446Smrj */ 697*3446Smrj set_gatesegd(&idt[T_DTRACE_RET], &dtrace_ret, KCS_SEL, 698*3446Smrj SDT_SYSIGT, SEL_UPL); 699*3446Smrj 700*3446Smrj /* 701*3446Smrj * Prepare interposing descriptors for the branded "int80" 702*3446Smrj * and syscall handlers and cache copies of the default 703*3446Smrj * descriptors. 704*3446Smrj */ 705*3446Smrj brand_tbl[0].ih_inum = T_INT80; 706*3446Smrj brand_tbl[0].ih_default_desc = idt0[T_INT80]; 707*3446Smrj set_gatesegd(&(brand_tbl[0].ih_interp_desc), &brand_sys_int80, KCS_SEL, 708*3446Smrj SDT_SYSIGT, SEL_UPL); 709*3446Smrj 710*3446Smrj brand_tbl[1].ih_inum = T_SYSCALLINT; 711*3446Smrj brand_tbl[1].ih_default_desc = idt0[T_SYSCALLINT]; 712*3446Smrj 713*3446Smrj #if defined(__amd64) 714*3446Smrj set_gatesegd(&(brand_tbl[1].ih_interp_desc), &brand_sys_syscall_int, 715*3446Smrj KCS_SEL, SDT_SYSIGT, SEL_UPL); 716*3446Smrj #elif defined(__i386) 717*3446Smrj set_gatesegd(&(brand_tbl[1].ih_interp_desc), &brand_sys_call, 718*3446Smrj KCS_SEL, SDT_SYSIGT, SEL_UPL); 719*3446Smrj #endif /* __i386 */ 720*3446Smrj 721*3446Smrj brand_tbl[2].ih_inum = 0; 722*3446Smrj } 723*3446Smrj 724*3446Smrj static void 725*3446Smrj init_idt(gate_desc_t *idt) 7260Sstevel@tonic-gate { 7270Sstevel@tonic-gate char ivctname[80]; 7280Sstevel@tonic-gate void (*ivctptr)(void); 7290Sstevel@tonic-gate int i; 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * Initialize entire table with 'reserved' trap and then overwrite 7330Sstevel@tonic-gate * specific entries. T_EXTOVRFLT (9) is unsupported and reserved 7340Sstevel@tonic-gate * since it can only be generated on a 386 processor. 15 is also 7350Sstevel@tonic-gate * unsupported and reserved. 7360Sstevel@tonic-gate */ 7370Sstevel@tonic-gate for (i = 0; i < NIDT; i++) 738*3446Smrj set_gatesegd(&idt[i], &resvtrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate /* 7410Sstevel@tonic-gate * 20-31 reserved 7420Sstevel@tonic-gate */ 7430Sstevel@tonic-gate for (i = 20; i < 32; i++) 744*3446Smrj set_gatesegd(&idt[i], &invaltrap, KCS_SEL, SDT_SYSIGT, SEL_KPL); 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * interrupts 32 - 255 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate for (i = 32; i < 256; i++) { 7500Sstevel@tonic-gate (void) snprintf(ivctname, sizeof (ivctname), "ivct%d", i); 7510Sstevel@tonic-gate ivctptr = (void (*)(void))kobj_getsymvalue(ivctname, 0); 7520Sstevel@tonic-gate if (ivctptr == NULL) 7530Sstevel@tonic-gate panic("kobj_getsymvalue(%s) failed", ivctname); 7540Sstevel@tonic-gate 755*3446Smrj set_gatesegd(&idt[i], ivctptr, KCS_SEL, SDT_SYSIGT, SEL_KPL); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate /* 759*3446Smrj * Now install the common ones. Note that it will overlay some 760*3446Smrj * entries installed above like T_SYSCALLINT, T_FASTTRAP etc. 7610Sstevel@tonic-gate */ 762*3446Smrj init_idt_common(idt); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* 7661217Srab * The kernel does not deal with LDTs unless a user explicitly creates 7671217Srab * one. Under normal circumstances, the LDTR contains 0. Any process attempting 7681217Srab * to reference the LDT will therefore cause a #gp. System calls made via the 7691217Srab * obsolete lcall mechanism are emulated by the #gp fault handler. 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate static void 7720Sstevel@tonic-gate init_ldt(void) 7730Sstevel@tonic-gate { 7741217Srab wr_ldtr(0); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate #if defined(__amd64) 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate static void 7800Sstevel@tonic-gate init_tss(void) 7810Sstevel@tonic-gate { 7820Sstevel@tonic-gate /* 7830Sstevel@tonic-gate * tss_rsp0 is dynamically filled in by resume() on each context switch. 7840Sstevel@tonic-gate * All exceptions but #DF will run on the thread stack. 7850Sstevel@tonic-gate * Set up the double fault stack here. 7860Sstevel@tonic-gate */ 7870Sstevel@tonic-gate ktss0.tss_ist1 = 7880Sstevel@tonic-gate (uint64_t)&dblfault_stack0[sizeof (dblfault_stack0)]; 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate /* 7910Sstevel@tonic-gate * Set I/O bit map offset equal to size of TSS segment limit 7920Sstevel@tonic-gate * for no I/O permission map. This will force all user I/O 7930Sstevel@tonic-gate * instructions to generate #gp fault. 7940Sstevel@tonic-gate */ 7950Sstevel@tonic-gate ktss0.tss_bitmapbase = sizeof (ktss0); 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate /* 7980Sstevel@tonic-gate * Point %tr to descriptor for ktss0 in gdt. 7990Sstevel@tonic-gate */ 8000Sstevel@tonic-gate wr_tsr(KTSS_SEL); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate #elif defined(__i386) 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate static void 8060Sstevel@tonic-gate init_tss(void) 8070Sstevel@tonic-gate { 8080Sstevel@tonic-gate /* 8090Sstevel@tonic-gate * ktss0.tss_esp dynamically filled in by resume() on each 8100Sstevel@tonic-gate * context switch. 8110Sstevel@tonic-gate */ 8120Sstevel@tonic-gate ktss0.tss_ss0 = KDS_SEL; 8130Sstevel@tonic-gate ktss0.tss_eip = (uint32_t)_start; 8140Sstevel@tonic-gate ktss0.tss_ds = ktss0.tss_es = ktss0.tss_ss = KDS_SEL; 8150Sstevel@tonic-gate ktss0.tss_cs = KCS_SEL; 8160Sstevel@tonic-gate ktss0.tss_fs = KFS_SEL; 8170Sstevel@tonic-gate ktss0.tss_gs = KGS_SEL; 8180Sstevel@tonic-gate ktss0.tss_ldt = ULDT_SEL; 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate /* 8210Sstevel@tonic-gate * Initialize double fault tss. 8220Sstevel@tonic-gate */ 8230Sstevel@tonic-gate dftss0.tss_esp0 = (uint32_t)&dblfault_stack0[sizeof (dblfault_stack0)]; 8240Sstevel@tonic-gate dftss0.tss_ss0 = KDS_SEL; 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate /* 8270Sstevel@tonic-gate * tss_cr3 will get initialized in hat_kern_setup() once our page 8280Sstevel@tonic-gate * tables have been setup. 8290Sstevel@tonic-gate */ 8300Sstevel@tonic-gate dftss0.tss_eip = (uint32_t)syserrtrap; 8310Sstevel@tonic-gate dftss0.tss_esp = (uint32_t)&dblfault_stack0[sizeof (dblfault_stack0)]; 8320Sstevel@tonic-gate dftss0.tss_cs = KCS_SEL; 8330Sstevel@tonic-gate dftss0.tss_ds = KDS_SEL; 8340Sstevel@tonic-gate dftss0.tss_es = KDS_SEL; 8350Sstevel@tonic-gate dftss0.tss_ss = KDS_SEL; 8360Sstevel@tonic-gate dftss0.tss_fs = KFS_SEL; 8370Sstevel@tonic-gate dftss0.tss_gs = KGS_SEL; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate /* 8400Sstevel@tonic-gate * Set I/O bit map offset equal to size of TSS segment limit 8410Sstevel@tonic-gate * for no I/O permission map. This will force all user I/O 8420Sstevel@tonic-gate * instructions to generate #gp fault. 8430Sstevel@tonic-gate */ 8440Sstevel@tonic-gate ktss0.tss_bitmapbase = sizeof (ktss0); 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate /* 8470Sstevel@tonic-gate * Point %tr to descriptor for ktss0 in gdt. 8480Sstevel@tonic-gate */ 8490Sstevel@tonic-gate wr_tsr(KTSS_SEL); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate #endif /* __i386 */ 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate void 855*3446Smrj init_desctbls(void) 8560Sstevel@tonic-gate { 857*3446Smrj user_desc_t *gdt; 858*3446Smrj desctbr_t idtr; 859*3446Smrj 860*3446Smrj /* 861*3446Smrj * Setup and install our GDT. 862*3446Smrj */ 863*3446Smrj gdt = init_gdt(); 864*3446Smrj ASSERT(IS_P2ALIGNED((uintptr_t)gdt, PAGESIZE)); 865*3446Smrj CPU->cpu_m.mcpu_gdt = gdt; 866*3446Smrj 867*3446Smrj /* 868*3446Smrj * Setup and install our IDT. 869*3446Smrj */ 870*3446Smrj init_idt(&idt0[0]); 871*3446Smrj 872*3446Smrj idtr.dtr_base = (uintptr_t)idt0; 873*3446Smrj idtr.dtr_limit = sizeof (idt0) - 1; 874*3446Smrj wr_idtr(&idtr); 875*3446Smrj CPU->cpu_m.mcpu_idt = idt0; 876*3446Smrj 877*3446Smrj #if defined(__i386) 878*3446Smrj /* 879*3446Smrj * We maintain a description of idt0 in convenient IDTR format 880*3446Smrj * for #pf's on some older pentium processors. See pentium_pftrap(). 881*3446Smrj */ 882*3446Smrj idt0_default_r = idtr; 883*3446Smrj #endif /* __i386 */ 884*3446Smrj 8850Sstevel@tonic-gate init_tss(); 886*3446Smrj CPU->cpu_tss = &ktss0; 8870Sstevel@tonic-gate init_ldt(); 8880Sstevel@tonic-gate } 8892712Snn35248 8902712Snn35248 /* 891*3446Smrj * In the early kernel, we need to set up a simple GDT to run on. 892*3446Smrj */ 893*3446Smrj void 894*3446Smrj init_boot_gdt(user_desc_t *bgdt) 895*3446Smrj { 896*3446Smrj #if defined(__amd64) 897*3446Smrj set_usegd(&bgdt[GDT_B32DATA], SDP_LONG, NULL, -1, SDT_MEMRWA, SEL_KPL, 898*3446Smrj SDP_PAGES, SDP_OP32); 899*3446Smrj set_usegd(&bgdt[GDT_B64CODE], SDP_LONG, NULL, -1, SDT_MEMERA, SEL_KPL, 900*3446Smrj SDP_PAGES, SDP_OP32); 901*3446Smrj #elif defined(__i386) 902*3446Smrj set_usegd(&bgdt[GDT_B32DATA], NULL, -1, SDT_MEMRWA, SEL_KPL, 903*3446Smrj SDP_PAGES, SDP_OP32); 904*3446Smrj set_usegd(&bgdt[GDT_B32CODE], NULL, -1, SDT_MEMERA, SEL_KPL, 905*3446Smrj SDP_PAGES, SDP_OP32); 906*3446Smrj #endif /* __i386 */ 907*3446Smrj } 908*3446Smrj 909*3446Smrj /* 9102712Snn35248 * Enable interpositioning on the system call path by rewriting the 9112712Snn35248 * sys{call|enter} MSRs and the syscall-related entries in the IDT to use 9122712Snn35248 * the branded entry points. 9132712Snn35248 */ 9142712Snn35248 void 9152712Snn35248 brand_interpositioning_enable(void) 9162712Snn35248 { 9172712Snn35248 int i; 9182712Snn35248 9192712Snn35248 for (i = 0; brand_tbl[i].ih_inum; i++) 9202712Snn35248 CPU->cpu_idt[brand_tbl[i].ih_inum] = 9212712Snn35248 brand_tbl[i].ih_interp_desc; 9222712Snn35248 9232712Snn35248 #if defined(__amd64) 9242712Snn35248 wrmsr(MSR_AMD_LSTAR, (uintptr_t)brand_sys_syscall); 9252712Snn35248 wrmsr(MSR_AMD_CSTAR, (uintptr_t)brand_sys_syscall32); 9262712Snn35248 #endif 9272712Snn35248 9282712Snn35248 if (x86_feature & X86_SEP) 9292712Snn35248 wrmsr(MSR_INTC_SEP_EIP, (uintptr_t)brand_sys_sysenter); 9302712Snn35248 } 9312712Snn35248 9322712Snn35248 /* 9332712Snn35248 * Disable interpositioning on the system call path by rewriting the 9342712Snn35248 * sys{call|enter} MSRs and the syscall-related entries in the IDT to use 9352712Snn35248 * the standard entry points, which bypass the interpositioning hooks. 9362712Snn35248 */ 9372712Snn35248 void 9382712Snn35248 brand_interpositioning_disable(void) 9392712Snn35248 { 9402712Snn35248 int i; 9412712Snn35248 9422712Snn35248 for (i = 0; brand_tbl[i].ih_inum; i++) 9432712Snn35248 CPU->cpu_idt[brand_tbl[i].ih_inum] = 9442712Snn35248 brand_tbl[i].ih_default_desc; 9452712Snn35248 9462712Snn35248 #if defined(__amd64) 9472712Snn35248 wrmsr(MSR_AMD_LSTAR, (uintptr_t)sys_syscall); 9482712Snn35248 wrmsr(MSR_AMD_CSTAR, (uintptr_t)sys_syscall32); 9492712Snn35248 #endif 9502712Snn35248 9512712Snn35248 if (x86_feature & X86_SEP) 9522712Snn35248 wrmsr(MSR_INTC_SEP_EIP, (uintptr_t)sys_sysenter); 9532712Snn35248 } 954