1 #ifndef ASM_X86__SEGMENT_H
2 #define ASM_X86__SEGMENT_H
4 /* Constructor for a conventional segment GDT (or LDT) entry */
5 /* This is a macro so it can be used in initializers */
6 #define GDT_ENTRY(flags, base, limit) \
7 ((((base) & 0xff000000ULL) << (56-24)) | \
8 (((flags) & 0x0000f0ffULL) << 40) | \
9 (((limit) & 0x000f0000ULL) << (48-16)) | \
10 (((base) & 0x00ffffffULL) << 16) | \
11 (((limit) & 0x0000ffffULL)))
13 /* Simple and small GDT entries for booting only */
15 #define GDT_ENTRY_BOOT_CS 2
16 #define __BOOT_CS (GDT_ENTRY_BOOT_CS * 8)
18 #define GDT_ENTRY_BOOT_DS (GDT_ENTRY_BOOT_CS + 1)
19 #define __BOOT_DS (GDT_ENTRY_BOOT_DS * 8)
21 #define GDT_ENTRY_BOOT_TSS (GDT_ENTRY_BOOT_CS + 2)
22 #define __BOOT_TSS (GDT_ENTRY_BOOT_TSS * 8)
26 * The layout of the per-CPU GDT under Linux:
33 * 4 - unused <==== new cacheline
36 * ------- start of TLS (Thread-Local Storage) segments:
38 * 6 - TLS segment #1 [ glibc's TLS segment ]
39 * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
45 * ------- start of kernel segments:
47 * 12 - kernel code segment <==== new cacheline
48 * 13 - kernel data segment
49 * 14 - default user CS
50 * 15 - default user DS
53 * 18 - PNPBIOS support (16->32 gate)
54 * 19 - PNPBIOS support
55 * 20 - PNPBIOS support
56 * 21 - PNPBIOS support
57 * 22 - PNPBIOS support
58 * 23 - APM BIOS support
59 * 24 - APM BIOS support
60 * 25 - APM BIOS support
62 * 26 - ESPFIX small SS
63 * 27 - per-cpu [ offset to per-cpu data area ]
67 * 31 - TSS for double fault handler
69 #define GDT_ENTRY_TLS_MIN 6
70 #define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
72 #define GDT_ENTRY_DEFAULT_USER_CS 14
74 #define GDT_ENTRY_DEFAULT_USER_DS 15
76 #define GDT_ENTRY_KERNEL_BASE 12
78 #define GDT_ENTRY_KERNEL_CS (GDT_ENTRY_KERNEL_BASE + 0)
80 #define GDT_ENTRY_KERNEL_DS (GDT_ENTRY_KERNEL_BASE + 1)
82 #define GDT_ENTRY_TSS (GDT_ENTRY_KERNEL_BASE + 4)
83 #define GDT_ENTRY_LDT (GDT_ENTRY_KERNEL_BASE + 5)
85 #define GDT_ENTRY_PNPBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 6)
86 #define GDT_ENTRY_APMBIOS_BASE (GDT_ENTRY_KERNEL_BASE + 11)
88 #define GDT_ENTRY_ESPFIX_SS (GDT_ENTRY_KERNEL_BASE + 14)
89 #define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS * 8)
91 #define GDT_ENTRY_PERCPU (GDT_ENTRY_KERNEL_BASE + 15)
93 #define __KERNEL_PERCPU (GDT_ENTRY_PERCPU * 8)
95 #define __KERNEL_PERCPU 0
98 #define GDT_ENTRY_DOUBLEFAULT_TSS 31
101 * The GDT has 32 entries
103 #define GDT_ENTRIES 32
105 /* The PnP BIOS entries in the GDT */
106 #define GDT_ENTRY_PNPBIOS_CS32 (GDT_ENTRY_PNPBIOS_BASE + 0)
107 #define GDT_ENTRY_PNPBIOS_CS16 (GDT_ENTRY_PNPBIOS_BASE + 1)
108 #define GDT_ENTRY_PNPBIOS_DS (GDT_ENTRY_PNPBIOS_BASE + 2)
109 #define GDT_ENTRY_PNPBIOS_TS1 (GDT_ENTRY_PNPBIOS_BASE + 3)
110 #define GDT_ENTRY_PNPBIOS_TS2 (GDT_ENTRY_PNPBIOS_BASE + 4)
112 /* The PnP BIOS selectors */
113 #define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */
114 #define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */
115 #define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */
116 #define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */
117 #define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */
119 /* Bottom two bits of selector give the ring privilege level */
120 #define SEGMENT_RPL_MASK 0x3
121 /* Bit 2 is table indicator (LDT/GDT) */
122 #define SEGMENT_TI_MASK 0x4
124 /* User mode is privilege level 3 */
126 /* LDT segment has TI set, GDT has it cleared */
127 #define SEGMENT_LDT 0x4
128 #define SEGMENT_GDT 0x0
131 * Matching rules for certain types of segments.
134 /* Matches only __KERNEL_CS, ignoring PnP / USER / APM segments */
135 #define SEGMENT_IS_KERNEL_CODE(x) (((x) & 0xfc) == GDT_ENTRY_KERNEL_CS * 8)
137 /* Matches __KERNEL_CS and __USER_CS (they must be 2 entries apart) */
138 #define SEGMENT_IS_FLAT_CODE(x) (((x) & 0xec) == GDT_ENTRY_KERNEL_CS * 8)
140 /* Matches PNP_CS32 and PNP_CS16 (they must be consecutive) */
141 #define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == GDT_ENTRY_PNPBIOS_BASE * 8)
145 #include <asm/cache.h>
147 #define GDT_ENTRY_KERNEL32_CS 1
148 #define GDT_ENTRY_KERNEL_CS 2
149 #define GDT_ENTRY_KERNEL_DS 3
151 #define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS * 8)
154 * we cannot use the same code segment descriptor for user and kernel
155 * -- not even in the long flat mode, because of different DPL /kkeil
156 * The segment offset needs to contain a RPL. Grr. -AK
157 * GDT layout to get 64bit syscall right (sysret hardcodes gdt offsets)
159 #define GDT_ENTRY_DEFAULT_USER32_CS 4
160 #define GDT_ENTRY_DEFAULT_USER_DS 5
161 #define GDT_ENTRY_DEFAULT_USER_CS 6
162 #define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS * 8 + 3)
163 #define __USER32_DS __USER_DS
165 #define GDT_ENTRY_TSS 8 /* needs two entries */
166 #define GDT_ENTRY_LDT 10 /* needs two entries */
167 #define GDT_ENTRY_TLS_MIN 12
168 #define GDT_ENTRY_TLS_MAX 14
170 #define GDT_ENTRY_PER_CPU 15 /* Abused to load per CPU data from limit */
171 #define __PER_CPU_SEG (GDT_ENTRY_PER_CPU * 8 + 3)
173 /* TLS indexes for 64bit - hardcoded in arch_prctl */
177 #define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
178 #define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
180 #define GDT_ENTRIES 16
184 #define __KERNEL_CS (GDT_ENTRY_KERNEL_CS * 8)
185 #define __KERNEL_DS (GDT_ENTRY_KERNEL_DS * 8)
186 #define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS* 8 + 3)
187 #define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS* 8 + 3)
188 #ifndef CONFIG_PARAVIRT
189 #define get_kernel_rpl() 0
192 /* User mode is privilege level 3 */
194 /* LDT segment has TI set, GDT has it cleared */
195 #define SEGMENT_LDT 0x4
196 #define SEGMENT_GDT 0x0
198 /* Bottom two bits of selector give the ring privilege level */
199 #define SEGMENT_RPL_MASK 0x3
200 /* Bit 2 is table indicator (LDT/GDT) */
201 #define SEGMENT_TI_MASK 0x4
203 #define IDT_ENTRIES 256
204 #define NUM_EXCEPTION_VECTORS 32
205 #define GDT_SIZE (GDT_ENTRIES * 8)
206 #define GDT_ENTRY_TLS_ENTRIES 3
207 #define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)
211 extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][10];
215 #endif /* ASM_X86__SEGMENT_H */