xref: /linux/arch/x86/include/asm/segment.h (revision 6b51311c976593fb7311322b1647a912cd456ec4)
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 
672d64cc7SIngo Molnar /*
772d64cc7SIngo Molnar  * Constructor for a conventional segment GDT (or LDT) entry.
872d64cc7SIngo Molnar  * This is a macro so it can be used in initializers.
972d64cc7SIngo 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 
1772d64cc7SIngo 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 
2672d64cc7SIngo Molnar /*
27be9d1738SBorislav Petkov  * Bottom two bits of selector give the ring
28be9d1738SBorislav Petkov  * privilege level
29be9d1738SBorislav Petkov  */
3072d64cc7SIngo Molnar #define SEGMENT_RPL_MASK	0x3
3172d64cc7SIngo Molnar 
3272d64cc7SIngo Molnar /* User mode is privilege level 3: */
3372d64cc7SIngo Molnar #define USER_RPL		0x3
3472d64cc7SIngo Molnar 
3572d64cc7SIngo Molnar /* Bit 2 is Table Indicator (TI): selects between LDT or GDT */
3672d64cc7SIngo Molnar #define SEGMENT_TI_MASK		0x4
3772d64cc7SIngo Molnar /* LDT segment has TI set ... */
3872d64cc7SIngo Molnar #define SEGMENT_LDT		0x4
3972d64cc7SIngo Molnar /* ... GDT has it cleared */
4072d64cc7SIngo Molnar #define SEGMENT_GDT		0x0
41be9d1738SBorislav Petkov 
42*6b51311cSBorislav Petkov #define GDT_ENTRY_INVALID_SEG	0
43*6b51311cSBorislav Petkov 
44bb898558SAl Viro #ifdef CONFIG_X86_32
45bb898558SAl Viro /*
46bb898558SAl Viro  * The layout of the per-CPU GDT under Linux:
47bb898558SAl Viro  *
4872d64cc7SIngo Molnar  *   0 - null								<=== cacheline #1
49bb898558SAl Viro  *   1 - reserved
50bb898558SAl Viro  *   2 - reserved
51bb898558SAl Viro  *   3 - reserved
52bb898558SAl Viro  *
5372d64cc7SIngo Molnar  *   4 - unused								<=== cacheline #2
54bb898558SAl Viro  *   5 - unused
55bb898558SAl Viro  *
56bb898558SAl Viro  *  ------- start of TLS (Thread-Local Storage) segments:
57bb898558SAl Viro  *
58bb898558SAl Viro  *   6 - TLS segment #1			[ glibc's TLS segment ]
59bb898558SAl Viro  *   7 - TLS segment #2			[ Wine's %fs Win32 segment ]
6072d64cc7SIngo Molnar  *   8 - TLS segment #3							<=== cacheline #3
61bb898558SAl Viro  *   9 - reserved
62bb898558SAl Viro  *  10 - reserved
63bb898558SAl Viro  *  11 - reserved
64bb898558SAl Viro  *
65bb898558SAl Viro  *  ------- start of kernel segments:
66bb898558SAl Viro  *
6772d64cc7SIngo Molnar  *  12 - kernel code segment						<=== cacheline #4
68bb898558SAl Viro  *  13 - kernel data segment
69bb898558SAl Viro  *  14 - default user CS
70bb898558SAl Viro  *  15 - default user DS
7172d64cc7SIngo Molnar  *  16 - TSS								<=== cacheline #5
72bb898558SAl Viro  *  17 - LDT
73bb898558SAl Viro  *  18 - PNPBIOS support (16->32 gate)
74bb898558SAl Viro  *  19 - PNPBIOS support
7572d64cc7SIngo Molnar  *  20 - PNPBIOS support						<=== cacheline #6
76bb898558SAl Viro  *  21 - PNPBIOS support
77bb898558SAl Viro  *  22 - PNPBIOS support
78bb898558SAl Viro  *  23 - APM BIOS support
7972d64cc7SIngo Molnar  *  24 - APM BIOS support						<=== cacheline #7
80bb898558SAl Viro  *  25 - APM BIOS support
81bb898558SAl Viro  *
82bb898558SAl Viro  *  26 - ESPFIX small SS
83bb898558SAl Viro  *  27 - per-cpu			[ offset to per-cpu data area ]
8472d64cc7SIngo Molnar  *  28 - stack_canary-20		[ for stack protector ]		<=== cacheline #8
85bb898558SAl Viro  *  29 - unused
86bb898558SAl Viro  *  30 - unused
87bb898558SAl Viro  *  31 - TSS for double fault handler
88bb898558SAl Viro  */
89bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		6
90bb898558SAl Viro #define GDT_ENTRY_TLS_MAX 		(GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
91bb898558SAl Viro 
9284f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_CS		12
9384f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_DS		13
94bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	14
95bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	15
9684f53788SDenys Vlasenko #define GDT_ENTRY_TSS			16
9784f53788SDenys Vlasenko #define GDT_ENTRY_LDT			17
9884f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS32		18
9984f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS16		19
10084f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_DS		20
10184f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS1		21
10284f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS2		22
10384f53788SDenys Vlasenko #define GDT_ENTRY_APMBIOS_BASE		23
104bb898558SAl Viro 
10584f53788SDenys Vlasenko #define GDT_ENTRY_ESPFIX_SS		26
10684f53788SDenys Vlasenko #define GDT_ENTRY_PERCPU		27
10784f53788SDenys Vlasenko #define GDT_ENTRY_STACK_CANARY		28
108bb898558SAl Viro 
10984f53788SDenys Vlasenko #define GDT_ENTRY_DOUBLEFAULT_TSS	31
110bb898558SAl Viro 
11172d64cc7SIngo Molnar /*
11272d64cc7SIngo Molnar  * Number of entries in the GDT table:
11372d64cc7SIngo Molnar  */
11472d64cc7SIngo Molnar #define GDT_ENTRIES			32
11572d64cc7SIngo Molnar 
11672d64cc7SIngo Molnar /*
11772d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
11872d64cc7SIngo Molnar  */
11972d64cc7SIngo Molnar 
12084f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
12184f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
12284f53788SDenys Vlasenko #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
12384f53788SDenys Vlasenko #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
124bb898558SAl Viro #define __ESPFIX_SS			(GDT_ENTRY_ESPFIX_SS*8)
12572d64cc7SIngo Molnar 
12672d64cc7SIngo Molnar /* segment for calling fn: */
12772d64cc7SIngo Molnar #define PNP_CS32			(GDT_ENTRY_PNPBIOS_CS32*8)
12872d64cc7SIngo Molnar /* code segment for BIOS: */
12972d64cc7SIngo Molnar #define PNP_CS16			(GDT_ENTRY_PNPBIOS_CS16*8)
13072d64cc7SIngo Molnar 
13184f53788SDenys Vlasenko /* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
13284f53788SDenys Vlasenko #define SEGMENT_IS_PNP_CODE(x)		(((x) & 0xf4) == PNP_CS32)
13372d64cc7SIngo Molnar 
13472d64cc7SIngo Molnar /* data segment for BIOS: */
13572d64cc7SIngo Molnar #define PNP_DS				(GDT_ENTRY_PNPBIOS_DS*8)
13672d64cc7SIngo Molnar /* transfer data segment: */
13772d64cc7SIngo Molnar #define PNP_TS1				(GDT_ENTRY_PNPBIOS_TS1*8)
13872d64cc7SIngo Molnar /* another data segment: */
13972d64cc7SIngo Molnar #define PNP_TS2				(GDT_ENTRY_PNPBIOS_TS2*8)
14072d64cc7SIngo Molnar 
141bb898558SAl Viro #ifdef CONFIG_SMP
142bb898558SAl Viro # define __KERNEL_PERCPU		(GDT_ENTRY_PERCPU*8)
143bb898558SAl Viro #else
144bb898558SAl Viro # define __KERNEL_PERCPU		0
145bb898558SAl Viro #endif
14672d64cc7SIngo Molnar 
14760a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
14860a5317fSTejun Heo # define __KERNEL_STACK_CANARY		(GDT_ENTRY_STACK_CANARY*8)
14960a5317fSTejun Heo #else
15060a5317fSTejun Heo # define __KERNEL_STACK_CANARY		0
15160a5317fSTejun Heo #endif
15260a5317fSTejun Heo 
15384f53788SDenys Vlasenko #else /* 64-bit: */
154bb898558SAl Viro 
155bb898558SAl Viro #include <asm/cache.h>
156bb898558SAl Viro 
157bb898558SAl Viro #define GDT_ENTRY_KERNEL32_CS		1
158bb898558SAl Viro #define GDT_ENTRY_KERNEL_CS		2
159bb898558SAl Viro #define GDT_ENTRY_KERNEL_DS		3
16072d64cc7SIngo Molnar 
161bb898558SAl Viro /*
16272d64cc7SIngo Molnar  * We cannot use the same code segment descriptor for user and kernel mode,
16372d64cc7SIngo Molnar  * not even in long flat mode, because of different DPL.
16472d64cc7SIngo Molnar  *
16572d64cc7SIngo Molnar  * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
16672d64cc7SIngo Molnar  * selectors:
16772d64cc7SIngo Molnar  *
16884f53788SDenys Vlasenko  *   if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
16984f53788SDenys Vlasenko  *   if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
17072d64cc7SIngo Molnar  *
17184f53788SDenys Vlasenko  * ss = STAR.SYSRET_CS+8 (in either case)
17272d64cc7SIngo Molnar  *
17384f53788SDenys Vlasenko  * thus USER_DS should be between 32-bit and 64-bit code selectors:
174bb898558SAl Viro  */
175bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER32_CS	4
176bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	5
177bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	6
178bb898558SAl Viro 
17972d64cc7SIngo Molnar /* Needs two entries */
18072d64cc7SIngo Molnar #define GDT_ENTRY_TSS			8
18172d64cc7SIngo Molnar /* Needs two entries */
18272d64cc7SIngo Molnar #define GDT_ENTRY_LDT			10
18372d64cc7SIngo Molnar 
184bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		12
185bb898558SAl Viro #define GDT_ENTRY_TLS_MAX		14
186bb898558SAl Viro 
18772d64cc7SIngo Molnar /* Abused to load per CPU data from limit */
18872d64cc7SIngo Molnar #define GDT_ENTRY_PER_CPU		15
18984f53788SDenys Vlasenko 
19072d64cc7SIngo Molnar /*
19172d64cc7SIngo Molnar  * Number of entries in the GDT table:
19272d64cc7SIngo Molnar  */
19372d64cc7SIngo Molnar #define GDT_ENTRIES			16
19472d64cc7SIngo Molnar 
19572d64cc7SIngo Molnar /*
19672d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
19772d64cc7SIngo Molnar  *
19872d64cc7SIngo Molnar  * Note, selectors also need to have a correct RPL,
19972d64cc7SIngo Molnar  * expressed with the +3 value for user-space selectors:
20072d64cc7SIngo Molnar  */
20172d64cc7SIngo Molnar #define __KERNEL32_CS			(GDT_ENTRY_KERNEL32_CS*8)
20284f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
20384f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
20484f53788SDenys Vlasenko #define __USER32_CS			(GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
20572d64cc7SIngo Molnar #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
20684f53788SDenys Vlasenko #define __USER32_DS			__USER_DS
20772d64cc7SIngo Molnar #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
208bb898558SAl Viro #define __PER_CPU_SEG			(GDT_ENTRY_PER_CPU*8 + 3)
209bb898558SAl Viro 
21072d64cc7SIngo Molnar /* TLS indexes for 64-bit - hardcoded in arch_prctl(): */
211bb898558SAl Viro #define FS_TLS				0
212bb898558SAl Viro #define GS_TLS				1
213bb898558SAl Viro 
214bb898558SAl Viro #define GS_TLS_SEL			((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
215bb898558SAl Viro #define FS_TLS_SEL			((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
216bb898558SAl Viro 
217bb898558SAl Viro #endif
218bb898558SAl Viro 
219bb898558SAl Viro #ifndef CONFIG_PARAVIRT
220bb898558SAl Viro # define get_kernel_rpl()		0
221bb898558SAl Viro #endif
222bb898558SAl Viro 
223bb898558SAl Viro #define IDT_ENTRIES			256
224bb898558SAl Viro #define NUM_EXCEPTION_VECTORS		32
22572d64cc7SIngo Molnar 
22672d64cc7SIngo Molnar /* Bitmask of exception vectors which push an error code on the stack: */
22784f4fc52SH. Peter Anvin #define EXCEPTION_ERRCODE_MASK		0x00027d00
22872d64cc7SIngo Molnar 
229bb898558SAl Viro #define GDT_SIZE			(GDT_ENTRIES*8)
230bb898558SAl Viro #define GDT_ENTRY_TLS_ENTRIES		3
231bb898558SAl Viro #define TLS_SIZE			(GDT_ENTRY_TLS_ENTRIES* 8)
232bb898558SAl Viro 
233bb898558SAl Viro #ifdef __KERNEL__
234bb898558SAl Viro #ifndef __ASSEMBLY__
23572d64cc7SIngo Molnar 
2369900aa2fSH. Peter Anvin extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][2+2+5];
23725c74b10SSeiji Aguchi #ifdef CONFIG_TRACING
23825c74b10SSeiji Aguchi # define trace_early_idt_handlers early_idt_handlers
23925c74b10SSeiji Aguchi #endif
240f05e798aSDavid Howells 
241f05e798aSDavid Howells /*
242f05e798aSDavid Howells  * Load a segment. Fall back on loading the zero
243f05e798aSDavid Howells  * segment if something goes wrong..
244f05e798aSDavid Howells  */
245f05e798aSDavid Howells #define loadsegment(seg, value)						\
246f05e798aSDavid Howells do {									\
247f05e798aSDavid Howells 	unsigned short __val = (value);					\
248f05e798aSDavid Howells 									\
249f05e798aSDavid Howells 	asm volatile("						\n"	\
250f05e798aSDavid Howells 		     "1:	movl %k0,%%" #seg "		\n"	\
251f05e798aSDavid Howells 									\
252f05e798aSDavid Howells 		     ".section .fixup,\"ax\"			\n"	\
253f05e798aSDavid Howells 		     "2:	xorl %k0,%k0			\n"	\
254f05e798aSDavid Howells 		     "		jmp 1b				\n"	\
255f05e798aSDavid Howells 		     ".previous					\n"	\
256f05e798aSDavid Howells 									\
257f05e798aSDavid Howells 		     _ASM_EXTABLE(1b, 2b)				\
258f05e798aSDavid Howells 									\
259f05e798aSDavid Howells 		     : "+r" (__val) : : "memory");			\
260f05e798aSDavid Howells } while (0)
261f05e798aSDavid Howells 
262f05e798aSDavid Howells /*
26372d64cc7SIngo Molnar  * Save a segment register away:
264f05e798aSDavid Howells  */
265f05e798aSDavid Howells #define savesegment(seg, value)				\
266f05e798aSDavid Howells 	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
267f05e798aSDavid Howells 
268f05e798aSDavid Howells /*
26972d64cc7SIngo Molnar  * x86-32 user GS accessors:
270f05e798aSDavid Howells  */
271f05e798aSDavid Howells #ifdef CONFIG_X86_32
272f05e798aSDavid Howells # ifdef CONFIG_X86_32_LAZY_GS
273f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)({ unsigned long v; savesegment(gs, v); v; })
274f05e798aSDavid Howells #  define set_user_gs(regs, v)		loadsegment(gs, (unsigned long)(v))
275f05e798aSDavid Howells #  define task_user_gs(tsk)		((tsk)->thread.gs)
276f05e798aSDavid Howells #  define lazy_save_gs(v)		savesegment(gs, (v))
277f05e798aSDavid Howells #  define lazy_load_gs(v)		loadsegment(gs, (v))
278f05e798aSDavid Howells # else	/* X86_32_LAZY_GS */
279f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)((regs)->gs)
280f05e798aSDavid Howells #  define set_user_gs(regs, v)		do { (regs)->gs = (v); } while (0)
281f05e798aSDavid Howells #  define task_user_gs(tsk)		(task_pt_regs(tsk)->gs)
282f05e798aSDavid Howells #  define lazy_save_gs(v)		do { } while (0)
283f05e798aSDavid Howells #  define lazy_load_gs(v)		do { } while (0)
284f05e798aSDavid Howells # endif	/* X86_32_LAZY_GS */
285f05e798aSDavid Howells #endif	/* X86_32 */
286f05e798aSDavid Howells 
287f05e798aSDavid Howells #endif /* !__ASSEMBLY__ */
288f05e798aSDavid Howells #endif /* __KERNEL__ */
289bb898558SAl Viro 
2901965aae3SH. Peter Anvin #endif /* _ASM_X86_SEGMENT_H */
291