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)
14 * The layout of the per-CPU GDT under Linux:
21 * 4 - unused <==== new cacheline
24 * ------- start of TLS (Thread-Local Storage) segments:
26 * 6 - TLS segment #1 [ glibc's TLS segment ]
27 * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
33 * ------- start of kernel segments:
35 * 12 - kernel code segment <==== new cacheline
36 * 13 - kernel data segment
37 * 14 - default user CS
38 * 15 - default user DS
41 * 18 - PNPBIOS support (16->32 gate)
42 * 19 - PNPBIOS support
43 * 20 - PNPBIOS support
44 * 21 - PNPBIOS support
45 * 22 - PNPBIOS support
46 * 23 - APM BIOS support
47 * 24 - APM BIOS support
48 * 25 - APM BIOS support
50 * 26 - ESPFIX small SS
51 * 27 - per-cpu [ offset to per-cpu data area ]
55 * 31 - TSS for double fault handler
57 #define GDT_ENTRY_TLS_MIN 6
58 #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
60 #define GDT_ENTRY_DEFAULT_USER_CS 14
61 #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS * 8 + 3)
63 #define GDT_ENTRY_DEFAULT_USER_DS 15
64 #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS * 8 + 3)
66 #define GDT_ENTRY_KERNEL_BASE 12
68 #define GDT_ENTRY_KERNEL_CS (GDT_ENTRY_KERNEL_BASE + 0)
69 #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS * 8)
71 #define GDT_ENTRY_KERNEL_DS (GDT_ENTRY_KERNEL_BASE + 1)
72 #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS * 8)
74 #define GDT_ENTRY_TSS (GDT_ENTRY_KERNEL_BASE + 4)
75 #define GDT_ENTRY_LDT (GDT_ENTRY_KERNEL_BASE + 5)
77 #define GDT_ENTRY_PNPBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 6)
78 #define GDT_ENTRY_APMBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 11)
80 #define GDT_ENTRY_ESPFIX_SS (GDT_ENTRY_KERNEL_BASE + 14)
81 #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS * 8)
83 #define GDT_ENTRY_PERCPU (GDT_ENTRY_KERNEL_BASE + 15)
85 #define __KERNEL_PERCPU (GDT_ENTRY_PERCPU * 8)
87 #define __KERNEL_PERCPU 0
90 #define GDT_ENTRY_DOUBLEFAULT_TSS 31
93 * The GDT has 32 entries
95 #define GDT_ENTRIES 32
97 /* The PnP BIOS entries in the GDT */
98 #define GDT_ENTRY_PNPBIOS_CS32 (GDT_ENTRY_PNPBIOS_BASE + 0)
99 #define GDT_ENTRY_PNPBIOS_CS16 (GDT_ENTRY_PNPBIOS_BASE + 1)
100 #define GDT_ENTRY_PNPBIOS_DS (GDT_ENTRY_PNPBIOS_BASE + 2)
101 #define GDT_ENTRY_PNPBIOS_TS1 (GDT_ENTRY_PNPBIOS_BASE + 3)
102 #define GDT_ENTRY_PNPBIOS_TS2 (GDT_ENTRY_PNPBIOS_BASE + 4)
104 /* The PnP BIOS selectors */
105 #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */
106 #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */
107 #define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */
108 #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */
109 #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */
111 /* Bottom two bits of selector give the ring privilege level */
112 #define SEGMENT_RPL_MASK 0x3
113 /* Bit 2 is table indicator (LDT/GDT) */
114 #define SEGMENT_TI_MASK 0x4
116 /* User mode is privilege level 3 */
118 /* LDT segment has TI set, GDT has it cleared */
119 #define SEGMENT_LDT 0x4
120 #define SEGMENT_GDT 0x0
123 * Matching rules for certain types of segments.
126 /* Matches only __KERNEL_CS, ignoring PnP / USER / APM segments */
127 #define SEGMENT_IS_KERNEL_CODE(x) (((x) & 0xfc) == GDT_ENTRY_KERNEL_CS * 8)
129 /* Matches __KERNEL_CS and __USER_CS (they must be 2 entries apart) */
130 #define SEGMENT_IS_FLAT_CODE(x) (((x) & 0xec) == GDT_ENTRY_KERNEL_CS * 8)
132 /* Matches PNP_CS32 and PNP_CS16 (they must be consecutive) */
133 #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == GDT_ENTRY_PNPBIOS_BASE * 8)
137 #include <asm/cache.h>
139 #define __KERNEL_CS 0x10
140 #define __KERNEL_DS 0x18
142 #define __KERNEL32_CS 0x08
145 * we cannot use the same code segment descriptor for user and kernel
146 * -- not even in the long flat mode, because of different DPL /kkeil
147 * The segment offset needs to contain a RPL. Grr. -AK
148 * GDT layout to get 64bit syscall right (sysret hardcodes gdt offsets)
151 #define __USER32_CS 0x23 /* 4*8+3 */
152 #define __USER_DS 0x2b /* 5*8+3 */
153 #define __USER_CS 0x33 /* 6*8+3 */
154 #define __USER32_DS __USER_DS
156 #define GDT_ENTRY_TSS 8 /* needs two entries */
157 #define GDT_ENTRY_LDT 10 /* needs two entries */
158 #define GDT_ENTRY_TLS_MIN 12
159 #define GDT_ENTRY_TLS_MAX 14
161 #define GDT_ENTRY_PER_CPU 15 /* Abused to load per CPU data from limit */
162 #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU * 8 + 3)
164 /* TLS indexes for 64bit - hardcoded in arch_prctl */
168 #define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
169 #define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
171 #define GDT_ENTRIES 16
175 #ifndef CONFIG_PARAVIRT
176 #define get_kernel_rpl() 0
179 /* User mode is privilege level 3 */
181 /* LDT segment has TI set, GDT has it cleared */
182 #define SEGMENT_LDT 0x4
183 #define SEGMENT_GDT 0x0
185 /* Bottom two bits of selector give the ring privilege level */
186 #define SEGMENT_RPL_MASK 0x3
187 /* Bit 2 is table indicator (LDT/GDT) */
188 #define SEGMENT_TI_MASK 0x4
190 #define IDT_ENTRIES 256
191 #define GDT_SIZE (GDT_ENTRIES * 8)
192 #define GDT_ENTRY_TLS_ENTRIES 3
193 #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)