H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_SEGMENT_H |
| 2 | #define _ASM_X86_SEGMENT_H |
Glauber de Oliveira Costa | cef1895 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 3 | |
H. Peter Anvin | 014eea5 | 2011-02-14 18:33:55 -0800 | [diff] [blame] | 4 | #include <linux/const.h> |
| 5 | |
H. Peter Anvin | 4fdf08b | 2008-07-17 11:29:24 -0700 | [diff] [blame] | 6 | /* Constructor for a conventional segment GDT (or LDT) entry */ |
| 7 | /* This is a macro so it can be used in initializers */ |
| 8 | #define GDT_ENTRY(flags, base, limit) \ |
H. Peter Anvin | 014eea5 | 2011-02-14 18:33:55 -0800 | [diff] [blame] | 9 | ((((base) & _AC(0xff000000,ULL)) << (56-24)) | \ |
| 10 | (((flags) & _AC(0x0000f0ff,ULL)) << 40) | \ |
| 11 | (((limit) & _AC(0x000f0000,ULL)) << (48-16)) | \ |
| 12 | (((base) & _AC(0x00ffffff,ULL)) << 16) | \ |
| 13 | (((limit) & _AC(0x0000ffff,ULL)))) |
H. Peter Anvin | 4fdf08b | 2008-07-17 11:29:24 -0700 | [diff] [blame] | 14 | |
H. Peter Anvin | 2a6648e | 2008-01-30 13:33:01 +0100 | [diff] [blame] | 15 | /* Simple and small GDT entries for booting only */ |
| 16 | |
| 17 | #define GDT_ENTRY_BOOT_CS 2 |
| 18 | #define __BOOT_CS (GDT_ENTRY_BOOT_CS * 8) |
| 19 | |
| 20 | #define GDT_ENTRY_BOOT_DS (GDT_ENTRY_BOOT_CS + 1) |
| 21 | #define __BOOT_DS (GDT_ENTRY_BOOT_DS * 8) |
| 22 | |
H. Peter Anvin | 8808951 | 2008-01-30 13:33:02 +0100 | [diff] [blame] | 23 | #define GDT_ENTRY_BOOT_TSS (GDT_ENTRY_BOOT_CS + 2) |
| 24 | #define __BOOT_TSS (GDT_ENTRY_BOOT_TSS * 8) |
| 25 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_X86_32 |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 27 | /* |
| 28 | * The layout of the per-CPU GDT under Linux: |
| 29 | * |
| 30 | * 0 - null |
| 31 | * 1 - reserved |
| 32 | * 2 - reserved |
| 33 | * 3 - reserved |
| 34 | * |
| 35 | * 4 - unused <==== new cacheline |
| 36 | * 5 - unused |
| 37 | * |
| 38 | * ------- start of TLS (Thread-Local Storage) segments: |
| 39 | * |
| 40 | * 6 - TLS segment #1 [ glibc's TLS segment ] |
| 41 | * 7 - TLS segment #2 [ Wine's %fs Win32 segment ] |
| 42 | * 8 - TLS segment #3 |
| 43 | * 9 - reserved |
| 44 | * 10 - reserved |
| 45 | * 11 - reserved |
| 46 | * |
| 47 | * ------- start of kernel segments: |
| 48 | * |
| 49 | * 12 - kernel code segment <==== new cacheline |
| 50 | * 13 - kernel data segment |
| 51 | * 14 - default user CS |
| 52 | * 15 - default user DS |
| 53 | * 16 - TSS |
| 54 | * 17 - LDT |
| 55 | * 18 - PNPBIOS support (16->32 gate) |
| 56 | * 19 - PNPBIOS support |
| 57 | * 20 - PNPBIOS support |
| 58 | * 21 - PNPBIOS support |
| 59 | * 22 - PNPBIOS support |
| 60 | * 23 - APM BIOS support |
| 61 | * 24 - APM BIOS support |
| 62 | * 25 - APM BIOS support |
| 63 | * |
| 64 | * 26 - ESPFIX small SS |
| 65 | * 27 - per-cpu [ offset to per-cpu data area ] |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 66 | * 28 - stack_canary-20 [ for stack protector ] |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 67 | * 29 - unused |
| 68 | * 30 - unused |
| 69 | * 31 - TSS for double fault handler |
| 70 | */ |
| 71 | #define GDT_ENTRY_TLS_MIN 6 |
| 72 | #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1) |
| 73 | |
| 74 | #define GDT_ENTRY_DEFAULT_USER_CS 14 |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 75 | |
| 76 | #define GDT_ENTRY_DEFAULT_USER_DS 15 |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 77 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 78 | #define GDT_ENTRY_KERNEL_BASE (12) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 79 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 80 | #define GDT_ENTRY_KERNEL_CS (GDT_ENTRY_KERNEL_BASE+0) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 81 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 82 | #define GDT_ENTRY_KERNEL_DS (GDT_ENTRY_KERNEL_BASE+1) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 83 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 84 | #define GDT_ENTRY_TSS (GDT_ENTRY_KERNEL_BASE+4) |
| 85 | #define GDT_ENTRY_LDT (GDT_ENTRY_KERNEL_BASE+5) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 86 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 87 | #define GDT_ENTRY_PNPBIOS_BASE (GDT_ENTRY_KERNEL_BASE+6) |
| 88 | #define GDT_ENTRY_APMBIOS_BASE (GDT_ENTRY_KERNEL_BASE+11) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 89 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 90 | #define GDT_ENTRY_ESPFIX_SS (GDT_ENTRY_KERNEL_BASE+14) |
| 91 | #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 92 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 93 | #define GDT_ENTRY_PERCPU (GDT_ENTRY_KERNEL_BASE+15) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 94 | #ifdef CONFIG_SMP |
| 95 | #define __KERNEL_PERCPU (GDT_ENTRY_PERCPU * 8) |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 96 | #else |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 97 | #define __KERNEL_PERCPU 0 |
| 98 | #endif |
| 99 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 100 | #define GDT_ENTRY_STACK_CANARY (GDT_ENTRY_KERNEL_BASE+16) |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 101 | #ifdef CONFIG_CC_STACKPROTECTOR |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 102 | #define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8) |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 103 | #else |
| 104 | #define __KERNEL_STACK_CANARY 0 |
| 105 | #endif |
| 106 | |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 107 | #define GDT_ENTRY_DOUBLEFAULT_TSS 31 |
| 108 | |
| 109 | /* |
| 110 | * The GDT has 32 entries |
| 111 | */ |
| 112 | #define GDT_ENTRIES 32 |
| 113 | |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 114 | /* The PnP BIOS entries in the GDT */ |
| 115 | #define GDT_ENTRY_PNPBIOS_CS32 (GDT_ENTRY_PNPBIOS_BASE + 0) |
| 116 | #define GDT_ENTRY_PNPBIOS_CS16 (GDT_ENTRY_PNPBIOS_BASE + 1) |
| 117 | #define GDT_ENTRY_PNPBIOS_DS (GDT_ENTRY_PNPBIOS_BASE + 2) |
| 118 | #define GDT_ENTRY_PNPBIOS_TS1 (GDT_ENTRY_PNPBIOS_BASE + 3) |
| 119 | #define GDT_ENTRY_PNPBIOS_TS2 (GDT_ENTRY_PNPBIOS_BASE + 4) |
| 120 | |
| 121 | /* The PnP BIOS selectors */ |
| 122 | #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */ |
| 123 | #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */ |
| 124 | #define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */ |
| 125 | #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */ |
| 126 | #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */ |
| 127 | |
| 128 | /* Bottom two bits of selector give the ring privilege level */ |
| 129 | #define SEGMENT_RPL_MASK 0x3 |
| 130 | /* Bit 2 is table indicator (LDT/GDT) */ |
| 131 | #define SEGMENT_TI_MASK 0x4 |
| 132 | |
| 133 | /* User mode is privilege level 3 */ |
| 134 | #define USER_RPL 0x3 |
| 135 | /* LDT segment has TI set, GDT has it cleared */ |
| 136 | #define SEGMENT_LDT 0x4 |
| 137 | #define SEGMENT_GDT 0x0 |
| 138 | |
| 139 | /* |
| 140 | * Matching rules for certain types of segments. |
| 141 | */ |
| 142 | |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 143 | /* Matches PNP_CS32 and PNP_CS16 (they must be consecutive) */ |
| 144 | #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == GDT_ENTRY_PNPBIOS_BASE * 8) |
| 145 | |
| 146 | |
| 147 | #else |
| 148 | #include <asm/cache.h> |
| 149 | |
Glauber Costa | a939098 | 2008-05-28 16:19:53 -0700 | [diff] [blame] | 150 | #define GDT_ENTRY_KERNEL32_CS 1 |
| 151 | #define GDT_ENTRY_KERNEL_CS 2 |
| 152 | #define GDT_ENTRY_KERNEL_DS 3 |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 153 | |
Glauber Costa | a939098 | 2008-05-28 16:19:53 -0700 | [diff] [blame] | 154 | #define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS * 8) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * we cannot use the same code segment descriptor for user and kernel |
| 158 | * -- not even in the long flat mode, because of different DPL /kkeil |
| 159 | * The segment offset needs to contain a RPL. Grr. -AK |
| 160 | * GDT layout to get 64bit syscall right (sysret hardcodes gdt offsets) |
| 161 | */ |
Glauber Costa | a939098 | 2008-05-28 16:19:53 -0700 | [diff] [blame] | 162 | #define GDT_ENTRY_DEFAULT_USER32_CS 4 |
| 163 | #define GDT_ENTRY_DEFAULT_USER_DS 5 |
| 164 | #define GDT_ENTRY_DEFAULT_USER_CS 6 |
Borislav Petkov | dd2897b | 2011-06-03 22:07:22 +0200 | [diff] [blame] | 165 | #define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8+3) |
Glauber de Oliveira Costa | b23be39 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 166 | #define __USER32_DS __USER_DS |
| 167 | |
| 168 | #define GDT_ENTRY_TSS 8 /* needs two entries */ |
| 169 | #define GDT_ENTRY_LDT 10 /* needs two entries */ |
| 170 | #define GDT_ENTRY_TLS_MIN 12 |
| 171 | #define GDT_ENTRY_TLS_MAX 14 |
| 172 | |
| 173 | #define GDT_ENTRY_PER_CPU 15 /* Abused to load per CPU data from limit */ |
| 174 | #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU * 8 + 3) |
| 175 | |
| 176 | /* TLS indexes for 64bit - hardcoded in arch_prctl */ |
| 177 | #define FS_TLS 0 |
| 178 | #define GS_TLS 1 |
| 179 | |
| 180 | #define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3) |
| 181 | #define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3) |
| 182 | |
| 183 | #define GDT_ENTRIES 16 |
| 184 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 185 | #endif |
Glauber de Oliveira Costa | cef1895 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 186 | |
Jan Beulich | 3234282 | 2010-10-19 14:52:26 +0100 | [diff] [blame] | 187 | #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8) |
| 188 | #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8) |
| 189 | #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8+3) |
| 190 | #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8+3) |
Glauber de Oliveira Costa | cef1895 | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 191 | #ifndef CONFIG_PARAVIRT |
| 192 | #define get_kernel_rpl() 0 |
| 193 | #endif |
| 194 | |
Glauber de Oliveira Costa | 15ee09b | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 195 | /* User mode is privilege level 3 */ |
| 196 | #define USER_RPL 0x3 |
| 197 | /* LDT segment has TI set, GDT has it cleared */ |
| 198 | #define SEGMENT_LDT 0x4 |
| 199 | #define SEGMENT_GDT 0x0 |
| 200 | |
| 201 | /* Bottom two bits of selector give the ring privilege level */ |
| 202 | #define SEGMENT_RPL_MASK 0x3 |
| 203 | /* Bit 2 is table indicator (LDT/GDT) */ |
| 204 | #define SEGMENT_TI_MASK 0x4 |
| 205 | |
| 206 | #define IDT_ENTRIES 256 |
Andi Kleen | 5524ea3 | 2008-03-11 02:23:20 +0100 | [diff] [blame] | 207 | #define NUM_EXCEPTION_VECTORS 32 |
Glauber de Oliveira Costa | 15ee09b | 2008-01-30 13:31:09 +0100 | [diff] [blame] | 208 | #define GDT_SIZE (GDT_ENTRIES * 8) |
| 209 | #define GDT_ENTRY_TLS_ENTRIES 3 |
| 210 | #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8) |
| 211 | |
Roland McGrath | 8866cd9 | 2008-01-30 13:33:06 +0100 | [diff] [blame] | 212 | #ifdef __KERNEL__ |
| 213 | #ifndef __ASSEMBLY__ |
Andi Kleen | 5524ea3 | 2008-03-11 02:23:20 +0100 | [diff] [blame] | 214 | extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][10]; |
Roland McGrath | 8866cd9 | 2008-01-30 13:33:06 +0100 | [diff] [blame] | 215 | #endif |
| 216 | #endif |
| 217 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 218 | #endif /* _ASM_X86_SEGMENT_H */ |