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