1 #ifndef _ASM_X86_SEGMENT_H_
2 #define _ASM_X86_SEGMENT_H_
4 /* Simple and small GDT entries for booting only */
6 #define GDT_ENTRY_BOOT_CS 2
7 #define __BOOT_CS (GDT_ENTRY_BOOT_CS * 8)
9 #define GDT_ENTRY_BOOT_DS (GDT_ENTRY_BOOT_CS + 1)
10 #define __BOOT_DS (GDT_ENTRY_BOOT_DS * 8)
12 #define GDT_ENTRY_BOOT_TSS (GDT_ENTRY_BOOT_CS + 2)
13 #define __BOOT_TSS (GDT_ENTRY_BOOT_TSS * 8)
17 * The layout of the per-CPU GDT under Linux:
24 * 4 - unused <==== new cacheline
27 * ------- start of TLS (Thread-Local Storage) segments:
29 * 6 - TLS segment #1 [ glibc's TLS segment ]
30 * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
36 * ------- start of kernel segments:
38 * 12 - kernel code segment <==== new cacheline
39 * 13 - kernel data segment
40 * 14 - default user CS
41 * 15 - default user DS
44 * 18 - PNPBIOS support (16->32 gate)
45 * 19 - PNPBIOS support
46 * 20 - PNPBIOS support
47 * 21 - PNPBIOS support
48 * 22 - PNPBIOS support
49 * 23 - APM BIOS support
50 * 24 - APM BIOS support
51 * 25 - APM BIOS support
53 * 26 - ESPFIX small SS
54 * 27 - per-cpu [ offset to per-cpu data area ]
58 * 31 - TSS for double fault handler
60 #define GDT_ENTRY_TLS_MIN 6
61 #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
63 #define GDT_ENTRY_DEFAULT_USER_CS 14
64 #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS * 8 + 3)
66 #define GDT_ENTRY_DEFAULT_USER_DS 15
67 #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS * 8 + 3)
69 #define GDT_ENTRY_KERNEL_BASE 12
71 #define GDT_ENTRY_KERNEL_CS (GDT_ENTRY_KERNEL_BASE + 0)
72 #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS * 8)
74 #define GDT_ENTRY_KERNEL_DS (GDT_ENTRY_KERNEL_BASE + 1)
75 #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS * 8)
77 #define GDT_ENTRY_TSS (GDT_ENTRY_KERNEL_BASE + 4)
78 #define GDT_ENTRY_LDT (GDT_ENTRY_KERNEL_BASE + 5)
80 #define GDT_ENTRY_PNPBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 6)
81 #define GDT_ENTRY_APMBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 11)
83 #define GDT_ENTRY_ESPFIX_SS (GDT_ENTRY_KERNEL_BASE + 14)
84 #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS * 8)
86 #define GDT_ENTRY_PERCPU (GDT_ENTRY_KERNEL_BASE + 15)
88 #define __KERNEL_PERCPU (GDT_ENTRY_PERCPU * 8)
90 #define __KERNEL_PERCPU 0
93 #define GDT_ENTRY_DOUBLEFAULT_TSS 31
96 * The GDT has 32 entries
98 #define GDT_ENTRIES 32
100 /* The PnP BIOS entries in the GDT */
101 #define GDT_ENTRY_PNPBIOS_CS32 (GDT_ENTRY_PNPBIOS_BASE + 0)
102 #define GDT_ENTRY_PNPBIOS_CS16 (GDT_ENTRY_PNPBIOS_BASE + 1)
103 #define GDT_ENTRY_PNPBIOS_DS (GDT_ENTRY_PNPBIOS_BASE + 2)
104 #define GDT_ENTRY_PNPBIOS_TS1 (GDT_ENTRY_PNPBIOS_BASE + 3)
105 #define GDT_ENTRY_PNPBIOS_TS2 (GDT_ENTRY_PNPBIOS_BASE + 4)
107 /* The PnP BIOS selectors */
108 #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */
109 #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */
110 #define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */
111 #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */
112 #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */
114 /* Bottom two bits of selector give the ring privilege level */
115 #define SEGMENT_RPL_MASK 0x3
116 /* Bit 2 is table indicator (LDT/GDT) */
117 #define SEGMENT_TI_MASK 0x4
119 /* User mode is privilege level 3 */
121 /* LDT segment has TI set, GDT has it cleared */
122 #define SEGMENT_LDT 0x4
123 #define SEGMENT_GDT 0x0
126 * Matching rules for certain types of segments.
129 /* Matches only __KERNEL_CS, ignoring PnP / USER / APM segments */
130 #define SEGMENT_IS_KERNEL_CODE(x) (((x) & 0xfc) == GDT_ENTRY_KERNEL_CS * 8)
132 /* Matches __KERNEL_CS and __USER_CS (they must be 2 entries apart) */
133 #define SEGMENT_IS_FLAT_CODE(x) (((x) & 0xec) == GDT_ENTRY_KERNEL_CS * 8)
135 /* Matches PNP_CS32 and PNP_CS16 (they must be consecutive) */
136 #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == GDT_ENTRY_PNPBIOS_BASE * 8)
140 #include <asm/cache.h>
142 #define __KERNEL_CS 0x10
143 #define __KERNEL_DS 0x18
145 #define __KERNEL32_CS 0x08
148 * we cannot use the same code segment descriptor for user and kernel
149 * -- not even in the long flat mode, because of different DPL /kkeil
150 * The segment offset needs to contain a RPL. Grr. -AK
151 * GDT layout to get 64bit syscall right (sysret hardcodes gdt offsets)
154 #define __USER32_CS 0x23 /* 4*8+3 */
155 #define __USER_DS 0x2b /* 5*8+3 */
156 #define __USER_CS 0x33 /* 6*8+3 */
157 #define __USER32_DS __USER_DS
159 #define GDT_ENTRY_TSS 8 /* needs two entries */
160 #define GDT_ENTRY_LDT 10 /* needs two entries */
161 #define GDT_ENTRY_TLS_MIN 12
162 #define GDT_ENTRY_TLS_MAX 14
164 #define GDT_ENTRY_PER_CPU 15 /* Abused to load per CPU data from limit */
165 #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU * 8 + 3)
167 /* TLS indexes for 64bit - hardcoded in arch_prctl */
171 #define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
172 #define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
174 #define GDT_ENTRIES 16
178 #ifndef CONFIG_PARAVIRT
179 #define get_kernel_rpl() 0
182 /* User mode is privilege level 3 */
184 /* LDT segment has TI set, GDT has it cleared */
185 #define SEGMENT_LDT 0x4
186 #define SEGMENT_GDT 0x0
188 /* Bottom two bits of selector give the ring privilege level */
189 #define SEGMENT_RPL_MASK 0x3
190 /* Bit 2 is table indicator (LDT/GDT) */
191 #define SEGMENT_TI_MASK 0x4
193 #define IDT_ENTRIES 256
194 #define GDT_SIZE (GDT_ENTRIES * 8)
195 #define GDT_ENTRY_TLS_ENTRIES 3
196 #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)
200 extern const char early_idt_handlers[IDT_ENTRIES][10];