xref: /linux/arch/x86/include/asm/segment.h (revision 72d64cc76941cde45e65e2a5b9fb81d527963645)
11965aae3SH. Peter Anvin #ifndef _ASM_X86_SEGMENT_H
21965aae3SH. Peter Anvin #define _ASM_X86_SEGMENT_H
3bb898558SAl Viro 
4014eea51SH. Peter Anvin #include <linux/const.h>
5014eea51SH. Peter Anvin 
6*72d64cc7SIngo Molnar /*
7*72d64cc7SIngo Molnar  * Constructor for a conventional segment GDT (or LDT) entry.
8*72d64cc7SIngo Molnar  * This is a macro so it can be used in initializers.
9*72d64cc7SIngo Molnar  */
10bb898558SAl Viro #define GDT_ENTRY(flags, base, limit)			\
11014eea51SH. Peter Anvin 	((((base)  & _AC(0xff000000,ULL)) << (56-24)) |	\
12014eea51SH. Peter Anvin 	 (((flags) & _AC(0x0000f0ff,ULL)) << 40) |	\
13014eea51SH. Peter Anvin 	 (((limit) & _AC(0x000f0000,ULL)) << (48-16)) |	\
14014eea51SH. Peter Anvin 	 (((base)  & _AC(0x00ffffff,ULL)) << 16) |	\
15014eea51SH. Peter Anvin 	 (((limit) & _AC(0x0000ffff,ULL))))
16bb898558SAl Viro 
17*72d64cc7SIngo Molnar /* Simple and small GDT entries for booting only: */
18bb898558SAl Viro 
19bb898558SAl Viro #define GDT_ENTRY_BOOT_CS	2
2084f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_DS	3
2184f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_TSS	4
22bb898558SAl Viro #define __BOOT_CS		(GDT_ENTRY_BOOT_CS*8)
23bb898558SAl Viro #define __BOOT_DS		(GDT_ENTRY_BOOT_DS*8)
24bb898558SAl Viro #define __BOOT_TSS		(GDT_ENTRY_BOOT_TSS*8)
25bb898558SAl Viro 
26*72d64cc7SIngo Molnar /*
27be9d1738SBorislav Petkov  * Bottom two bits of selector give the ring
28be9d1738SBorislav Petkov  * privilege level
29be9d1738SBorislav Petkov  */
30*72d64cc7SIngo Molnar #define SEGMENT_RPL_MASK	0x3
31*72d64cc7SIngo Molnar 
32*72d64cc7SIngo Molnar /* User mode is privilege level 3: */
33*72d64cc7SIngo Molnar #define USER_RPL		0x3
34*72d64cc7SIngo Molnar 
35*72d64cc7SIngo Molnar /* Bit 2 is Table Indicator (TI): selects between LDT or GDT */
36*72d64cc7SIngo Molnar #define SEGMENT_TI_MASK		0x4
37*72d64cc7SIngo Molnar /* LDT segment has TI set ... */
38*72d64cc7SIngo Molnar #define SEGMENT_LDT		0x4
39*72d64cc7SIngo Molnar /* ... GDT has it cleared */
40*72d64cc7SIngo Molnar #define SEGMENT_GDT		0x0
41be9d1738SBorislav Petkov 
42bb898558SAl Viro #ifdef CONFIG_X86_32
43bb898558SAl Viro /*
44bb898558SAl Viro  * The layout of the per-CPU GDT under Linux:
45bb898558SAl Viro  *
46*72d64cc7SIngo Molnar  *   0 - null								<=== cacheline #1
47bb898558SAl Viro  *   1 - reserved
48bb898558SAl Viro  *   2 - reserved
49bb898558SAl Viro  *   3 - reserved
50bb898558SAl Viro  *
51*72d64cc7SIngo Molnar  *   4 - unused								<=== cacheline #2
52bb898558SAl Viro  *   5 - unused
53bb898558SAl Viro  *
54bb898558SAl Viro  *  ------- start of TLS (Thread-Local Storage) segments:
55bb898558SAl Viro  *
56bb898558SAl Viro  *   6 - TLS segment #1			[ glibc's TLS segment ]
57bb898558SAl Viro  *   7 - TLS segment #2			[ Wine's %fs Win32 segment ]
58*72d64cc7SIngo Molnar  *   8 - TLS segment #3							<=== cacheline #3
59bb898558SAl Viro  *   9 - reserved
60bb898558SAl Viro  *  10 - reserved
61bb898558SAl Viro  *  11 - reserved
62bb898558SAl Viro  *
63bb898558SAl Viro  *  ------- start of kernel segments:
64bb898558SAl Viro  *
65*72d64cc7SIngo Molnar  *  12 - kernel code segment						<=== cacheline #4
66bb898558SAl Viro  *  13 - kernel data segment
67bb898558SAl Viro  *  14 - default user CS
68bb898558SAl Viro  *  15 - default user DS
69*72d64cc7SIngo Molnar  *  16 - TSS								<=== cacheline #5
70bb898558SAl Viro  *  17 - LDT
71bb898558SAl Viro  *  18 - PNPBIOS support (16->32 gate)
72bb898558SAl Viro  *  19 - PNPBIOS support
73*72d64cc7SIngo Molnar  *  20 - PNPBIOS support						<=== cacheline #6
74bb898558SAl Viro  *  21 - PNPBIOS support
75bb898558SAl Viro  *  22 - PNPBIOS support
76bb898558SAl Viro  *  23 - APM BIOS support
77*72d64cc7SIngo Molnar  *  24 - APM BIOS support						<=== cacheline #7
78bb898558SAl Viro  *  25 - APM BIOS support
79bb898558SAl Viro  *
80bb898558SAl Viro  *  26 - ESPFIX small SS
81bb898558SAl Viro  *  27 - per-cpu			[ offset to per-cpu data area ]
82*72d64cc7SIngo Molnar  *  28 - stack_canary-20		[ for stack protector ]		<=== cacheline #8
83bb898558SAl Viro  *  29 - unused
84bb898558SAl Viro  *  30 - unused
85bb898558SAl Viro  *  31 - TSS for double fault handler
86bb898558SAl Viro  */
87bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		6
88bb898558SAl Viro #define GDT_ENTRY_TLS_MAX 		(GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
89bb898558SAl Viro 
9084f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_CS		12
9184f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_DS		13
92bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	14
93bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	15
9484f53788SDenys Vlasenko #define GDT_ENTRY_TSS			16
9584f53788SDenys Vlasenko #define GDT_ENTRY_LDT			17
9684f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS32		18
9784f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS16		19
9884f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_DS		20
9984f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS1		21
10084f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS2		22
10184f53788SDenys Vlasenko #define GDT_ENTRY_APMBIOS_BASE		23
102bb898558SAl Viro 
10384f53788SDenys Vlasenko #define GDT_ENTRY_ESPFIX_SS		26
10484f53788SDenys Vlasenko #define GDT_ENTRY_PERCPU		27
10584f53788SDenys Vlasenko #define GDT_ENTRY_STACK_CANARY		28
106bb898558SAl Viro 
10784f53788SDenys Vlasenko #define GDT_ENTRY_DOUBLEFAULT_TSS	31
108bb898558SAl Viro 
109*72d64cc7SIngo Molnar /*
110*72d64cc7SIngo Molnar  * Number of entries in the GDT table:
111*72d64cc7SIngo Molnar  */
112*72d64cc7SIngo Molnar #define GDT_ENTRIES			32
113*72d64cc7SIngo Molnar 
114*72d64cc7SIngo Molnar /*
115*72d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
116*72d64cc7SIngo Molnar  */
117*72d64cc7SIngo Molnar 
11884f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
11984f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
12084f53788SDenys Vlasenko #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
12184f53788SDenys Vlasenko #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
122bb898558SAl Viro #define __ESPFIX_SS			(GDT_ENTRY_ESPFIX_SS*8)
123*72d64cc7SIngo Molnar 
124*72d64cc7SIngo Molnar /* segment for calling fn: */
125*72d64cc7SIngo Molnar #define PNP_CS32			(GDT_ENTRY_PNPBIOS_CS32*8)
126*72d64cc7SIngo Molnar /* code segment for BIOS: */
127*72d64cc7SIngo Molnar #define PNP_CS16			(GDT_ENTRY_PNPBIOS_CS16*8)
128*72d64cc7SIngo Molnar 
12984f53788SDenys Vlasenko /* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
13084f53788SDenys Vlasenko #define SEGMENT_IS_PNP_CODE(x)		(((x) & 0xf4) == PNP_CS32)
131*72d64cc7SIngo Molnar 
132*72d64cc7SIngo Molnar /* data segment for BIOS: */
133*72d64cc7SIngo Molnar #define PNP_DS				(GDT_ENTRY_PNPBIOS_DS*8)
134*72d64cc7SIngo Molnar /* transfer data segment: */
135*72d64cc7SIngo Molnar #define PNP_TS1				(GDT_ENTRY_PNPBIOS_TS1*8)
136*72d64cc7SIngo Molnar /* another data segment: */
137*72d64cc7SIngo Molnar #define PNP_TS2				(GDT_ENTRY_PNPBIOS_TS2*8)
138*72d64cc7SIngo Molnar 
139bb898558SAl Viro #ifdef CONFIG_SMP
140bb898558SAl Viro # define __KERNEL_PERCPU		(GDT_ENTRY_PERCPU*8)
141bb898558SAl Viro #else
142bb898558SAl Viro # define __KERNEL_PERCPU		0
143bb898558SAl Viro #endif
144*72d64cc7SIngo Molnar 
14560a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
14660a5317fSTejun Heo # define __KERNEL_STACK_CANARY		(GDT_ENTRY_STACK_CANARY*8)
14760a5317fSTejun Heo #else
14860a5317fSTejun Heo # define __KERNEL_STACK_CANARY		0
14960a5317fSTejun Heo #endif
15060a5317fSTejun Heo 
15184f53788SDenys Vlasenko #else /* 64-bit: */
152bb898558SAl Viro 
153bb898558SAl Viro #include <asm/cache.h>
154bb898558SAl Viro 
155bb898558SAl Viro #define GDT_ENTRY_KERNEL32_CS		1
156bb898558SAl Viro #define GDT_ENTRY_KERNEL_CS		2
157bb898558SAl Viro #define GDT_ENTRY_KERNEL_DS		3
158*72d64cc7SIngo Molnar 
159bb898558SAl Viro /*
160*72d64cc7SIngo Molnar  * We cannot use the same code segment descriptor for user and kernel mode,
161*72d64cc7SIngo Molnar  * not even in long flat mode, because of different DPL.
162*72d64cc7SIngo Molnar  *
163*72d64cc7SIngo Molnar  * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
164*72d64cc7SIngo Molnar  * selectors:
165*72d64cc7SIngo Molnar  *
16684f53788SDenys Vlasenko  *   if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
16784f53788SDenys Vlasenko  *   if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
168*72d64cc7SIngo Molnar  *
16984f53788SDenys Vlasenko  * ss = STAR.SYSRET_CS+8 (in either case)
170*72d64cc7SIngo Molnar  *
17184f53788SDenys Vlasenko  * thus USER_DS should be between 32-bit and 64-bit code selectors:
172bb898558SAl Viro  */
173bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER32_CS	4
174bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	5
175bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	6
176bb898558SAl Viro 
177*72d64cc7SIngo Molnar /* Needs two entries */
178*72d64cc7SIngo Molnar #define GDT_ENTRY_TSS			8
179*72d64cc7SIngo Molnar /* Needs two entries */
180*72d64cc7SIngo Molnar #define GDT_ENTRY_LDT			10
181*72d64cc7SIngo Molnar 
182bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		12
183bb898558SAl Viro #define GDT_ENTRY_TLS_MAX		14
184bb898558SAl Viro 
185*72d64cc7SIngo Molnar /* Abused to load per CPU data from limit */
186*72d64cc7SIngo Molnar #define GDT_ENTRY_PER_CPU		15
18784f53788SDenys Vlasenko 
188*72d64cc7SIngo Molnar /*
189*72d64cc7SIngo Molnar  * Number of entries in the GDT table:
190*72d64cc7SIngo Molnar  */
191*72d64cc7SIngo Molnar #define GDT_ENTRIES			16
192*72d64cc7SIngo Molnar 
193*72d64cc7SIngo Molnar /*
194*72d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
195*72d64cc7SIngo Molnar  *
196*72d64cc7SIngo Molnar  * Note, selectors also need to have a correct RPL,
197*72d64cc7SIngo Molnar  * expressed with the +3 value for user-space selectors:
198*72d64cc7SIngo Molnar  */
199*72d64cc7SIngo Molnar #define __KERNEL32_CS			(GDT_ENTRY_KERNEL32_CS*8)
20084f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
20184f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
20284f53788SDenys Vlasenko #define __USER32_CS			(GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
203*72d64cc7SIngo Molnar #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
20484f53788SDenys Vlasenko #define __USER32_DS			__USER_DS
205*72d64cc7SIngo Molnar #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
206bb898558SAl Viro #define __PER_CPU_SEG			(GDT_ENTRY_PER_CPU*8 + 3)
207bb898558SAl Viro 
208*72d64cc7SIngo Molnar /* TLS indexes for 64-bit - hardcoded in arch_prctl(): */
209bb898558SAl Viro #define FS_TLS				0
210bb898558SAl Viro #define GS_TLS				1
211bb898558SAl Viro 
212bb898558SAl Viro #define GS_TLS_SEL			((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
213bb898558SAl Viro #define FS_TLS_SEL			((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
214bb898558SAl Viro 
215bb898558SAl Viro #endif
216bb898558SAl Viro 
217bb898558SAl Viro #ifndef CONFIG_PARAVIRT
218bb898558SAl Viro # define get_kernel_rpl()		0
219bb898558SAl Viro #endif
220bb898558SAl Viro 
221bb898558SAl Viro #define IDT_ENTRIES			256
222bb898558SAl Viro #define NUM_EXCEPTION_VECTORS		32
223*72d64cc7SIngo Molnar 
224*72d64cc7SIngo Molnar /* Bitmask of exception vectors which push an error code on the stack: */
22584f4fc52SH. Peter Anvin #define EXCEPTION_ERRCODE_MASK		0x00027d00
226*72d64cc7SIngo Molnar 
227bb898558SAl Viro #define GDT_SIZE			(GDT_ENTRIES*8)
228bb898558SAl Viro #define GDT_ENTRY_TLS_ENTRIES		3
229bb898558SAl Viro #define TLS_SIZE			(GDT_ENTRY_TLS_ENTRIES* 8)
230bb898558SAl Viro 
231bb898558SAl Viro #ifdef __KERNEL__
232bb898558SAl Viro #ifndef __ASSEMBLY__
233*72d64cc7SIngo Molnar 
2349900aa2fSH. Peter Anvin extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][2+2+5];
23525c74b10SSeiji Aguchi #ifdef CONFIG_TRACING
23625c74b10SSeiji Aguchi # define trace_early_idt_handlers early_idt_handlers
23725c74b10SSeiji Aguchi #endif
238f05e798aSDavid Howells 
239f05e798aSDavid Howells /*
240f05e798aSDavid Howells  * Load a segment. Fall back on loading the zero
241f05e798aSDavid Howells  * segment if something goes wrong..
242f05e798aSDavid Howells  */
243f05e798aSDavid Howells #define loadsegment(seg, value)						\
244f05e798aSDavid Howells do {									\
245f05e798aSDavid Howells 	unsigned short __val = (value);					\
246f05e798aSDavid Howells 									\
247f05e798aSDavid Howells 	asm volatile("						\n"	\
248f05e798aSDavid Howells 		     "1:	movl %k0,%%" #seg "		\n"	\
249f05e798aSDavid Howells 									\
250f05e798aSDavid Howells 		     ".section .fixup,\"ax\"			\n"	\
251f05e798aSDavid Howells 		     "2:	xorl %k0,%k0			\n"	\
252f05e798aSDavid Howells 		     "		jmp 1b				\n"	\
253f05e798aSDavid Howells 		     ".previous					\n"	\
254f05e798aSDavid Howells 									\
255f05e798aSDavid Howells 		     _ASM_EXTABLE(1b, 2b)				\
256f05e798aSDavid Howells 									\
257f05e798aSDavid Howells 		     : "+r" (__val) : : "memory");			\
258f05e798aSDavid Howells } while (0)
259f05e798aSDavid Howells 
260f05e798aSDavid Howells /*
261*72d64cc7SIngo Molnar  * Save a segment register away:
262f05e798aSDavid Howells  */
263f05e798aSDavid Howells #define savesegment(seg, value)				\
264f05e798aSDavid Howells 	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
265f05e798aSDavid Howells 
266f05e798aSDavid Howells /*
267*72d64cc7SIngo Molnar  * x86-32 user GS accessors:
268f05e798aSDavid Howells  */
269f05e798aSDavid Howells #ifdef CONFIG_X86_32
270f05e798aSDavid Howells # ifdef CONFIG_X86_32_LAZY_GS
271f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)({ unsigned long v; savesegment(gs, v); v; })
272f05e798aSDavid Howells #  define set_user_gs(regs, v)		loadsegment(gs, (unsigned long)(v))
273f05e798aSDavid Howells #  define task_user_gs(tsk)		((tsk)->thread.gs)
274f05e798aSDavid Howells #  define lazy_save_gs(v)		savesegment(gs, (v))
275f05e798aSDavid Howells #  define lazy_load_gs(v)		loadsegment(gs, (v))
276f05e798aSDavid Howells # else	/* X86_32_LAZY_GS */
277f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)((regs)->gs)
278f05e798aSDavid Howells #  define set_user_gs(regs, v)		do { (regs)->gs = (v); } while (0)
279f05e798aSDavid Howells #  define task_user_gs(tsk)		(task_pt_regs(tsk)->gs)
280f05e798aSDavid Howells #  define lazy_save_gs(v)		do { } while (0)
281f05e798aSDavid Howells #  define lazy_load_gs(v)		do { } while (0)
282f05e798aSDavid Howells # endif	/* X86_32_LAZY_GS */
283f05e798aSDavid Howells #endif	/* X86_32 */
284f05e798aSDavid Howells 
285f05e798aSDavid Howells #endif /* !__ASSEMBLY__ */
286f05e798aSDavid Howells #endif /* __KERNEL__ */
287bb898558SAl Viro 
2881965aae3SH. Peter Anvin #endif /* _ASM_X86_SEGMENT_H */
289