1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21965aae3SH. Peter Anvin #ifndef _ASM_X86_SEGMENT_H 31965aae3SH. Peter Anvin #define _ASM_X86_SEGMENT_H 4bb898558SAl Viro 5014eea51SH. Peter Anvin #include <linux/const.h> 645e876f7SAndy Lutomirski #include <asm/alternative.h> 7014eea51SH. Peter Anvin 872d64cc7SIngo Molnar /* 972d64cc7SIngo Molnar * Constructor for a conventional segment GDT (or LDT) entry. 1072d64cc7SIngo Molnar * This is a macro so it can be used in initializers. 1172d64cc7SIngo Molnar */ 12bb898558SAl Viro #define GDT_ENTRY(flags, base, limit) \ 13014eea51SH. Peter Anvin ((((base) & _AC(0xff000000,ULL)) << (56-24)) | \ 14014eea51SH. Peter Anvin (((flags) & _AC(0x0000f0ff,ULL)) << 40) | \ 15014eea51SH. Peter Anvin (((limit) & _AC(0x000f0000,ULL)) << (48-16)) | \ 16014eea51SH. Peter Anvin (((base) & _AC(0x00ffffff,ULL)) << 16) | \ 17014eea51SH. Peter Anvin (((limit) & _AC(0x0000ffff,ULL)))) 18bb898558SAl Viro 1972d64cc7SIngo Molnar /* Simple and small GDT entries for booting only: */ 20bb898558SAl Viro 21bb898558SAl Viro #define GDT_ENTRY_BOOT_CS 2 2284f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_DS 3 2384f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_TSS 4 24bb898558SAl Viro #define __BOOT_CS (GDT_ENTRY_BOOT_CS*8) 25bb898558SAl Viro #define __BOOT_DS (GDT_ENTRY_BOOT_DS*8) 26bb898558SAl Viro #define __BOOT_TSS (GDT_ENTRY_BOOT_TSS*8) 27bb898558SAl Viro 2872d64cc7SIngo Molnar /* 29be9d1738SBorislav Petkov * Bottom two bits of selector give the ring 30be9d1738SBorislav Petkov * privilege level 31be9d1738SBorislav Petkov */ 3272d64cc7SIngo Molnar #define SEGMENT_RPL_MASK 0x3 3372d64cc7SIngo Molnar 3472d64cc7SIngo Molnar /* User mode is privilege level 3: */ 3572d64cc7SIngo Molnar #define USER_RPL 0x3 3672d64cc7SIngo Molnar 3772d64cc7SIngo Molnar /* Bit 2 is Table Indicator (TI): selects between LDT or GDT */ 3872d64cc7SIngo Molnar #define SEGMENT_TI_MASK 0x4 3972d64cc7SIngo Molnar /* LDT segment has TI set ... */ 4072d64cc7SIngo Molnar #define SEGMENT_LDT 0x4 4172d64cc7SIngo Molnar /* ... GDT has it cleared */ 4272d64cc7SIngo Molnar #define SEGMENT_GDT 0x0 43be9d1738SBorislav Petkov 446b51311cSBorislav Petkov #define GDT_ENTRY_INVALID_SEG 0 456b51311cSBorislav Petkov 46bb898558SAl Viro #ifdef CONFIG_X86_32 47bb898558SAl Viro /* 48bb898558SAl Viro * The layout of the per-CPU GDT under Linux: 49bb898558SAl Viro * 5072d64cc7SIngo Molnar * 0 - null <=== cacheline #1 51bb898558SAl Viro * 1 - reserved 52bb898558SAl Viro * 2 - reserved 53bb898558SAl Viro * 3 - reserved 54bb898558SAl Viro * 5572d64cc7SIngo Molnar * 4 - unused <=== cacheline #2 56bb898558SAl Viro * 5 - unused 57bb898558SAl Viro * 58bb898558SAl Viro * ------- start of TLS (Thread-Local Storage) segments: 59bb898558SAl Viro * 60bb898558SAl Viro * 6 - TLS segment #1 [ glibc's TLS segment ] 61bb898558SAl Viro * 7 - TLS segment #2 [ Wine's %fs Win32 segment ] 6272d64cc7SIngo Molnar * 8 - TLS segment #3 <=== cacheline #3 63bb898558SAl Viro * 9 - reserved 64bb898558SAl Viro * 10 - reserved 65bb898558SAl Viro * 11 - reserved 66bb898558SAl Viro * 67bb898558SAl Viro * ------- start of kernel segments: 68bb898558SAl Viro * 6972d64cc7SIngo Molnar * 12 - kernel code segment <=== cacheline #4 70bb898558SAl Viro * 13 - kernel data segment 71bb898558SAl Viro * 14 - default user CS 72bb898558SAl Viro * 15 - default user DS 7372d64cc7SIngo Molnar * 16 - TSS <=== cacheline #5 74bb898558SAl Viro * 17 - LDT 75bb898558SAl Viro * 18 - PNPBIOS support (16->32 gate) 76bb898558SAl Viro * 19 - PNPBIOS support 7772d64cc7SIngo Molnar * 20 - PNPBIOS support <=== cacheline #6 78bb898558SAl Viro * 21 - PNPBIOS support 79bb898558SAl Viro * 22 - PNPBIOS support 80bb898558SAl Viro * 23 - APM BIOS support 8172d64cc7SIngo Molnar * 24 - APM BIOS support <=== cacheline #7 82bb898558SAl Viro * 25 - APM BIOS support 83bb898558SAl Viro * 84bb898558SAl Viro * 26 - ESPFIX small SS 85bb898558SAl Viro * 27 - per-cpu [ offset to per-cpu data area ] 8672d64cc7SIngo Molnar * 28 - stack_canary-20 [ for stack protector ] <=== cacheline #8 87bb898558SAl Viro * 29 - unused 88bb898558SAl Viro * 30 - unused 89bb898558SAl Viro * 31 - TSS for double fault handler 90bb898558SAl Viro */ 91bb898558SAl Viro #define GDT_ENTRY_TLS_MIN 6 92bb898558SAl Viro #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1) 93bb898558SAl Viro 9484f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_CS 12 9584f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_DS 13 96bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS 14 97bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS 15 9884f53788SDenys Vlasenko #define GDT_ENTRY_TSS 16 9984f53788SDenys Vlasenko #define GDT_ENTRY_LDT 17 10084f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS32 18 10184f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS16 19 10284f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_DS 20 10384f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS1 21 10484f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS2 22 10584f53788SDenys Vlasenko #define GDT_ENTRY_APMBIOS_BASE 23 106bb898558SAl Viro 10784f53788SDenys Vlasenko #define GDT_ENTRY_ESPFIX_SS 26 10884f53788SDenys Vlasenko #define GDT_ENTRY_PERCPU 27 10984f53788SDenys Vlasenko #define GDT_ENTRY_STACK_CANARY 28 110bb898558SAl Viro 11184f53788SDenys Vlasenko #define GDT_ENTRY_DOUBLEFAULT_TSS 31 112bb898558SAl Viro 11372d64cc7SIngo Molnar /* 11472d64cc7SIngo Molnar * Number of entries in the GDT table: 11572d64cc7SIngo Molnar */ 11672d64cc7SIngo Molnar #define GDT_ENTRIES 32 11772d64cc7SIngo Molnar 11872d64cc7SIngo Molnar /* 11972d64cc7SIngo Molnar * Segment selector values corresponding to the above entries: 12072d64cc7SIngo Molnar */ 12172d64cc7SIngo Molnar 12284f53788SDenys Vlasenko #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8) 12384f53788SDenys Vlasenko #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8) 12484f53788SDenys Vlasenko #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3) 12584f53788SDenys Vlasenko #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3) 126bb898558SAl Viro #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8) 12772d64cc7SIngo Molnar 12872d64cc7SIngo Molnar /* segment for calling fn: */ 12972d64cc7SIngo Molnar #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32*8) 13072d64cc7SIngo Molnar /* code segment for BIOS: */ 13172d64cc7SIngo Molnar #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16*8) 13272d64cc7SIngo Molnar 13384f53788SDenys Vlasenko /* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */ 13484f53788SDenys Vlasenko #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == PNP_CS32) 13572d64cc7SIngo Molnar 13672d64cc7SIngo Molnar /* data segment for BIOS: */ 13772d64cc7SIngo Molnar #define PNP_DS (GDT_ENTRY_PNPBIOS_DS*8) 13872d64cc7SIngo Molnar /* transfer data segment: */ 13972d64cc7SIngo Molnar #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1*8) 14072d64cc7SIngo Molnar /* another data segment: */ 14172d64cc7SIngo Molnar #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2*8) 14272d64cc7SIngo Molnar 143bb898558SAl Viro #ifdef CONFIG_SMP 144bb898558SAl Viro # define __KERNEL_PERCPU (GDT_ENTRY_PERCPU*8) 145bb898558SAl Viro #else 146bb898558SAl Viro # define __KERNEL_PERCPU 0 147bb898558SAl Viro #endif 14872d64cc7SIngo Molnar 14960a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR 15060a5317fSTejun Heo # define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8) 15160a5317fSTejun Heo #else 15260a5317fSTejun Heo # define __KERNEL_STACK_CANARY 0 15360a5317fSTejun Heo #endif 15460a5317fSTejun Heo 15584f53788SDenys Vlasenko #else /* 64-bit: */ 156bb898558SAl Viro 157bb898558SAl Viro #include <asm/cache.h> 158bb898558SAl Viro 159bb898558SAl Viro #define GDT_ENTRY_KERNEL32_CS 1 160bb898558SAl Viro #define GDT_ENTRY_KERNEL_CS 2 161bb898558SAl Viro #define GDT_ENTRY_KERNEL_DS 3 16272d64cc7SIngo Molnar 163bb898558SAl Viro /* 16472d64cc7SIngo Molnar * We cannot use the same code segment descriptor for user and kernel mode, 16572d64cc7SIngo Molnar * not even in long flat mode, because of different DPL. 16672d64cc7SIngo Molnar * 16772d64cc7SIngo Molnar * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes 16872d64cc7SIngo Molnar * selectors: 16972d64cc7SIngo Molnar * 17084f53788SDenys Vlasenko * if returning to 32-bit userspace: cs = STAR.SYSRET_CS, 17184f53788SDenys Vlasenko * if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16, 17272d64cc7SIngo Molnar * 17384f53788SDenys Vlasenko * ss = STAR.SYSRET_CS+8 (in either case) 17472d64cc7SIngo Molnar * 17584f53788SDenys Vlasenko * thus USER_DS should be between 32-bit and 64-bit code selectors: 176bb898558SAl Viro */ 177bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER32_CS 4 178bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS 5 179bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS 6 180bb898558SAl Viro 18172d64cc7SIngo Molnar /* Needs two entries */ 18272d64cc7SIngo Molnar #define GDT_ENTRY_TSS 8 18372d64cc7SIngo Molnar /* Needs two entries */ 18472d64cc7SIngo Molnar #define GDT_ENTRY_LDT 10 18572d64cc7SIngo Molnar 186bb898558SAl Viro #define GDT_ENTRY_TLS_MIN 12 187bb898558SAl Viro #define GDT_ENTRY_TLS_MAX 14 188bb898558SAl Viro 18972d64cc7SIngo Molnar /* Abused to load per CPU data from limit */ 19072d64cc7SIngo Molnar #define GDT_ENTRY_PER_CPU 15 19184f53788SDenys Vlasenko 19272d64cc7SIngo Molnar /* 19372d64cc7SIngo Molnar * Number of entries in the GDT table: 19472d64cc7SIngo Molnar */ 19572d64cc7SIngo Molnar #define GDT_ENTRIES 16 19672d64cc7SIngo Molnar 19772d64cc7SIngo Molnar /* 19872d64cc7SIngo Molnar * Segment selector values corresponding to the above entries: 19972d64cc7SIngo Molnar * 20072d64cc7SIngo Molnar * Note, selectors also need to have a correct RPL, 20172d64cc7SIngo Molnar * expressed with the +3 value for user-space selectors: 20272d64cc7SIngo Molnar */ 20372d64cc7SIngo Molnar #define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS*8) 20484f53788SDenys Vlasenko #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8) 20584f53788SDenys Vlasenko #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8) 20684f53788SDenys Vlasenko #define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8 + 3) 20772d64cc7SIngo Molnar #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3) 20884f53788SDenys Vlasenko #define __USER32_DS __USER_DS 20972d64cc7SIngo Molnar #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3) 210bb898558SAl Viro #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU*8 + 3) 211bb898558SAl Viro 212bb898558SAl Viro #endif 213bb898558SAl Viro 214bb898558SAl Viro #ifndef CONFIG_PARAVIRT 215bb898558SAl Viro # define get_kernel_rpl() 0 216bb898558SAl Viro #endif 217bb898558SAl Viro 218bb898558SAl Viro #define IDT_ENTRIES 256 219bb898558SAl Viro #define NUM_EXCEPTION_VECTORS 32 22072d64cc7SIngo Molnar 22172d64cc7SIngo Molnar /* Bitmask of exception vectors which push an error code on the stack: */ 22284f4fc52SH. Peter Anvin #define EXCEPTION_ERRCODE_MASK 0x00027d00 22372d64cc7SIngo Molnar 224bb898558SAl Viro #define GDT_SIZE (GDT_ENTRIES*8) 225bb898558SAl Viro #define GDT_ENTRY_TLS_ENTRIES 3 226bb898558SAl Viro #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES* 8) 227bb898558SAl Viro 228bb898558SAl Viro #ifdef __KERNEL__ 229425be567SAndy Lutomirski 230425be567SAndy Lutomirski /* 231425be567SAndy Lutomirski * early_idt_handler_array is an array of entry points referenced in the 232425be567SAndy Lutomirski * early IDT. For simplicity, it's a real array with one entry point 233425be567SAndy Lutomirski * every nine bytes. That leaves room for an optional 'push $0' if the 234425be567SAndy Lutomirski * vector has no error code (two bytes), a 'push $vector_number' (two 235425be567SAndy Lutomirski * bytes), and a jump to the common entry code (up to five bytes). 236425be567SAndy Lutomirski */ 237425be567SAndy Lutomirski #define EARLY_IDT_HANDLER_SIZE 9 238425be567SAndy Lutomirski 239*42b3a4cbSJuergen Gross /* 240*42b3a4cbSJuergen Gross * xen_early_idt_handler_array is for Xen pv guests: for each entry in 241*42b3a4cbSJuergen Gross * early_idt_handler_array it contains a prequel in the form of 242*42b3a4cbSJuergen Gross * pop %rcx; pop %r11; jmp early_idt_handler_array[i]; summing up to 243*42b3a4cbSJuergen Gross * max 8 bytes. 244*42b3a4cbSJuergen Gross */ 245*42b3a4cbSJuergen Gross #define XEN_EARLY_IDT_HANDLER_SIZE 8 246*42b3a4cbSJuergen Gross 247bb898558SAl Viro #ifndef __ASSEMBLY__ 24872d64cc7SIngo Molnar 249425be567SAndy Lutomirski extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE]; 25087e81786SThomas Gleixner extern void early_ignore_irq(void); 251f05e798aSDavid Howells 252*42b3a4cbSJuergen Gross #if defined(CONFIG_X86_64) && defined(CONFIG_XEN_PV) 253*42b3a4cbSJuergen Gross extern const char xen_early_idt_handler_array[NUM_EXCEPTION_VECTORS][XEN_EARLY_IDT_HANDLER_SIZE]; 254*42b3a4cbSJuergen Gross #endif 255*42b3a4cbSJuergen Gross 256f05e798aSDavid Howells /* 25745e876f7SAndy Lutomirski * Load a segment. Fall back on loading the zero segment if something goes 25845e876f7SAndy Lutomirski * wrong. This variant assumes that loading zero fully clears the segment. 25945e876f7SAndy Lutomirski * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any 26045e876f7SAndy Lutomirski * failure to fully clear the cached descriptor is only observable for 26145e876f7SAndy Lutomirski * FS and GS. 262f05e798aSDavid Howells */ 26345e876f7SAndy Lutomirski #define __loadsegment_simple(seg, value) \ 264f05e798aSDavid Howells do { \ 265f05e798aSDavid Howells unsigned short __val = (value); \ 266f05e798aSDavid Howells \ 267f05e798aSDavid Howells asm volatile(" \n" \ 268f05e798aSDavid Howells "1: movl %k0,%%" #seg " \n" \ 269f05e798aSDavid Howells \ 270f05e798aSDavid Howells ".section .fixup,\"ax\" \n" \ 271f05e798aSDavid Howells "2: xorl %k0,%k0 \n" \ 272f05e798aSDavid Howells " jmp 1b \n" \ 273f05e798aSDavid Howells ".previous \n" \ 274f05e798aSDavid Howells \ 275f05e798aSDavid Howells _ASM_EXTABLE(1b, 2b) \ 276f05e798aSDavid Howells \ 277f05e798aSDavid Howells : "+r" (__val) : : "memory"); \ 278f05e798aSDavid Howells } while (0) 279f05e798aSDavid Howells 28045e876f7SAndy Lutomirski #define __loadsegment_ss(value) __loadsegment_simple(ss, (value)) 28145e876f7SAndy Lutomirski #define __loadsegment_ds(value) __loadsegment_simple(ds, (value)) 28245e876f7SAndy Lutomirski #define __loadsegment_es(value) __loadsegment_simple(es, (value)) 28345e876f7SAndy Lutomirski 28445e876f7SAndy Lutomirski #ifdef CONFIG_X86_32 28545e876f7SAndy Lutomirski 28645e876f7SAndy Lutomirski /* 28745e876f7SAndy Lutomirski * On 32-bit systems, the hidden parts of FS and GS are unobservable if 28845e876f7SAndy Lutomirski * the selector is NULL, so there's no funny business here. 28945e876f7SAndy Lutomirski */ 29045e876f7SAndy Lutomirski #define __loadsegment_fs(value) __loadsegment_simple(fs, (value)) 29145e876f7SAndy Lutomirski #define __loadsegment_gs(value) __loadsegment_simple(gs, (value)) 29245e876f7SAndy Lutomirski 29345e876f7SAndy Lutomirski #else 29445e876f7SAndy Lutomirski 29545e876f7SAndy Lutomirski static inline void __loadsegment_fs(unsigned short value) 29645e876f7SAndy Lutomirski { 29745e876f7SAndy Lutomirski asm volatile(" \n" 29845e876f7SAndy Lutomirski "1: movw %0, %%fs \n" 29945e876f7SAndy Lutomirski "2: \n" 30045e876f7SAndy Lutomirski 30145e876f7SAndy Lutomirski _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs) 30245e876f7SAndy Lutomirski 30345e876f7SAndy Lutomirski : : "rm" (value) : "memory"); 30445e876f7SAndy Lutomirski } 30545e876f7SAndy Lutomirski 30645e876f7SAndy Lutomirski /* __loadsegment_gs is intentionally undefined. Use load_gs_index instead. */ 30745e876f7SAndy Lutomirski 30845e876f7SAndy Lutomirski #endif 30945e876f7SAndy Lutomirski 31045e876f7SAndy Lutomirski #define loadsegment(seg, value) __loadsegment_ ## seg (value) 31145e876f7SAndy Lutomirski 312f05e798aSDavid Howells /* 31372d64cc7SIngo Molnar * Save a segment register away: 314f05e798aSDavid Howells */ 315f05e798aSDavid Howells #define savesegment(seg, value) \ 316f05e798aSDavid Howells asm("mov %%" #seg ",%0":"=r" (value) : : "memory") 317f05e798aSDavid Howells 318f05e798aSDavid Howells /* 31972d64cc7SIngo Molnar * x86-32 user GS accessors: 320f05e798aSDavid Howells */ 321f05e798aSDavid Howells #ifdef CONFIG_X86_32 322f05e798aSDavid Howells # ifdef CONFIG_X86_32_LAZY_GS 323f05e798aSDavid Howells # define get_user_gs(regs) (u16)({ unsigned long v; savesegment(gs, v); v; }) 324f05e798aSDavid Howells # define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v)) 325f05e798aSDavid Howells # define task_user_gs(tsk) ((tsk)->thread.gs) 326f05e798aSDavid Howells # define lazy_save_gs(v) savesegment(gs, (v)) 327f05e798aSDavid Howells # define lazy_load_gs(v) loadsegment(gs, (v)) 328f05e798aSDavid Howells # else /* X86_32_LAZY_GS */ 329f05e798aSDavid Howells # define get_user_gs(regs) (u16)((regs)->gs) 330f05e798aSDavid Howells # define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0) 331f05e798aSDavid Howells # define task_user_gs(tsk) (task_pt_regs(tsk)->gs) 332f05e798aSDavid Howells # define lazy_save_gs(v) do { } while (0) 333f05e798aSDavid Howells # define lazy_load_gs(v) do { } while (0) 334f05e798aSDavid Howells # endif /* X86_32_LAZY_GS */ 335f05e798aSDavid Howells #endif /* X86_32 */ 336f05e798aSDavid Howells 337f05e798aSDavid Howells #endif /* !__ASSEMBLY__ */ 338f05e798aSDavid Howells #endif /* __KERNEL__ */ 339bb898558SAl Viro 3401965aae3SH. Peter Anvin #endif /* _ASM_X86_SEGMENT_H */ 341