xref: /linux/arch/x86/include/asm/segment.h (revision 87e81786b13b267c4355e0d23e33c7e4c08fa63f)
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>
545e876f7SAndy Lutomirski #include <asm/alternative.h>
6014eea51SH. Peter Anvin 
772d64cc7SIngo Molnar /*
872d64cc7SIngo Molnar  * Constructor for a conventional segment GDT (or LDT) entry.
972d64cc7SIngo Molnar  * This is a macro so it can be used in initializers.
1072d64cc7SIngo Molnar  */
11bb898558SAl Viro #define GDT_ENTRY(flags, base, limit)			\
12014eea51SH. Peter Anvin 	((((base)  & _AC(0xff000000,ULL)) << (56-24)) |	\
13014eea51SH. Peter Anvin 	 (((flags) & _AC(0x0000f0ff,ULL)) << 40) |	\
14014eea51SH. Peter Anvin 	 (((limit) & _AC(0x000f0000,ULL)) << (48-16)) |	\
15014eea51SH. Peter Anvin 	 (((base)  & _AC(0x00ffffff,ULL)) << 16) |	\
16014eea51SH. Peter Anvin 	 (((limit) & _AC(0x0000ffff,ULL))))
17bb898558SAl Viro 
1872d64cc7SIngo Molnar /* Simple and small GDT entries for booting only: */
19bb898558SAl Viro 
20bb898558SAl Viro #define GDT_ENTRY_BOOT_CS	2
2184f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_DS	3
2284f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_TSS	4
23bb898558SAl Viro #define __BOOT_CS		(GDT_ENTRY_BOOT_CS*8)
24bb898558SAl Viro #define __BOOT_DS		(GDT_ENTRY_BOOT_DS*8)
25bb898558SAl Viro #define __BOOT_TSS		(GDT_ENTRY_BOOT_TSS*8)
26bb898558SAl Viro 
2772d64cc7SIngo Molnar /*
28be9d1738SBorislav Petkov  * Bottom two bits of selector give the ring
29be9d1738SBorislav Petkov  * privilege level
30be9d1738SBorislav Petkov  */
3172d64cc7SIngo Molnar #define SEGMENT_RPL_MASK	0x3
3272d64cc7SIngo Molnar 
3372d64cc7SIngo Molnar /* User mode is privilege level 3: */
3472d64cc7SIngo Molnar #define USER_RPL		0x3
3572d64cc7SIngo Molnar 
3672d64cc7SIngo Molnar /* Bit 2 is Table Indicator (TI): selects between LDT or GDT */
3772d64cc7SIngo Molnar #define SEGMENT_TI_MASK		0x4
3872d64cc7SIngo Molnar /* LDT segment has TI set ... */
3972d64cc7SIngo Molnar #define SEGMENT_LDT		0x4
4072d64cc7SIngo Molnar /* ... GDT has it cleared */
4172d64cc7SIngo Molnar #define SEGMENT_GDT		0x0
42be9d1738SBorislav Petkov 
436b51311cSBorislav Petkov #define GDT_ENTRY_INVALID_SEG	0
446b51311cSBorislav Petkov 
45bb898558SAl Viro #ifdef CONFIG_X86_32
46bb898558SAl Viro /*
47bb898558SAl Viro  * The layout of the per-CPU GDT under Linux:
48bb898558SAl Viro  *
4972d64cc7SIngo Molnar  *   0 - null								<=== cacheline #1
50bb898558SAl Viro  *   1 - reserved
51bb898558SAl Viro  *   2 - reserved
52bb898558SAl Viro  *   3 - reserved
53bb898558SAl Viro  *
5472d64cc7SIngo Molnar  *   4 - unused								<=== cacheline #2
55bb898558SAl Viro  *   5 - unused
56bb898558SAl Viro  *
57bb898558SAl Viro  *  ------- start of TLS (Thread-Local Storage) segments:
58bb898558SAl Viro  *
59bb898558SAl Viro  *   6 - TLS segment #1			[ glibc's TLS segment ]
60bb898558SAl Viro  *   7 - TLS segment #2			[ Wine's %fs Win32 segment ]
6172d64cc7SIngo Molnar  *   8 - TLS segment #3							<=== cacheline #3
62bb898558SAl Viro  *   9 - reserved
63bb898558SAl Viro  *  10 - reserved
64bb898558SAl Viro  *  11 - reserved
65bb898558SAl Viro  *
66bb898558SAl Viro  *  ------- start of kernel segments:
67bb898558SAl Viro  *
6872d64cc7SIngo Molnar  *  12 - kernel code segment						<=== cacheline #4
69bb898558SAl Viro  *  13 - kernel data segment
70bb898558SAl Viro  *  14 - default user CS
71bb898558SAl Viro  *  15 - default user DS
7272d64cc7SIngo Molnar  *  16 - TSS								<=== cacheline #5
73bb898558SAl Viro  *  17 - LDT
74bb898558SAl Viro  *  18 - PNPBIOS support (16->32 gate)
75bb898558SAl Viro  *  19 - PNPBIOS support
7672d64cc7SIngo Molnar  *  20 - PNPBIOS support						<=== cacheline #6
77bb898558SAl Viro  *  21 - PNPBIOS support
78bb898558SAl Viro  *  22 - PNPBIOS support
79bb898558SAl Viro  *  23 - APM BIOS support
8072d64cc7SIngo Molnar  *  24 - APM BIOS support						<=== cacheline #7
81bb898558SAl Viro  *  25 - APM BIOS support
82bb898558SAl Viro  *
83bb898558SAl Viro  *  26 - ESPFIX small SS
84bb898558SAl Viro  *  27 - per-cpu			[ offset to per-cpu data area ]
8572d64cc7SIngo Molnar  *  28 - stack_canary-20		[ for stack protector ]		<=== cacheline #8
86bb898558SAl Viro  *  29 - unused
87bb898558SAl Viro  *  30 - unused
88bb898558SAl Viro  *  31 - TSS for double fault handler
89bb898558SAl Viro  */
90bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		6
91bb898558SAl Viro #define GDT_ENTRY_TLS_MAX 		(GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
92bb898558SAl Viro 
9384f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_CS		12
9484f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_DS		13
95bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	14
96bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	15
9784f53788SDenys Vlasenko #define GDT_ENTRY_TSS			16
9884f53788SDenys Vlasenko #define GDT_ENTRY_LDT			17
9984f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS32		18
10084f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS16		19
10184f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_DS		20
10284f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS1		21
10384f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS2		22
10484f53788SDenys Vlasenko #define GDT_ENTRY_APMBIOS_BASE		23
105bb898558SAl Viro 
10684f53788SDenys Vlasenko #define GDT_ENTRY_ESPFIX_SS		26
10784f53788SDenys Vlasenko #define GDT_ENTRY_PERCPU		27
10884f53788SDenys Vlasenko #define GDT_ENTRY_STACK_CANARY		28
109bb898558SAl Viro 
11084f53788SDenys Vlasenko #define GDT_ENTRY_DOUBLEFAULT_TSS	31
111bb898558SAl Viro 
11272d64cc7SIngo Molnar /*
11372d64cc7SIngo Molnar  * Number of entries in the GDT table:
11472d64cc7SIngo Molnar  */
11572d64cc7SIngo Molnar #define GDT_ENTRIES			32
11672d64cc7SIngo Molnar 
11772d64cc7SIngo Molnar /*
11872d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
11972d64cc7SIngo Molnar  */
12072d64cc7SIngo Molnar 
12184f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
12284f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
12384f53788SDenys Vlasenko #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
12484f53788SDenys Vlasenko #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
125bb898558SAl Viro #define __ESPFIX_SS			(GDT_ENTRY_ESPFIX_SS*8)
12672d64cc7SIngo Molnar 
12772d64cc7SIngo Molnar /* segment for calling fn: */
12872d64cc7SIngo Molnar #define PNP_CS32			(GDT_ENTRY_PNPBIOS_CS32*8)
12972d64cc7SIngo Molnar /* code segment for BIOS: */
13072d64cc7SIngo Molnar #define PNP_CS16			(GDT_ENTRY_PNPBIOS_CS16*8)
13172d64cc7SIngo Molnar 
13284f53788SDenys Vlasenko /* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
13384f53788SDenys Vlasenko #define SEGMENT_IS_PNP_CODE(x)		(((x) & 0xf4) == PNP_CS32)
13472d64cc7SIngo Molnar 
13572d64cc7SIngo Molnar /* data segment for BIOS: */
13672d64cc7SIngo Molnar #define PNP_DS				(GDT_ENTRY_PNPBIOS_DS*8)
13772d64cc7SIngo Molnar /* transfer data segment: */
13872d64cc7SIngo Molnar #define PNP_TS1				(GDT_ENTRY_PNPBIOS_TS1*8)
13972d64cc7SIngo Molnar /* another data segment: */
14072d64cc7SIngo Molnar #define PNP_TS2				(GDT_ENTRY_PNPBIOS_TS2*8)
14172d64cc7SIngo Molnar 
142bb898558SAl Viro #ifdef CONFIG_SMP
143bb898558SAl Viro # define __KERNEL_PERCPU		(GDT_ENTRY_PERCPU*8)
144bb898558SAl Viro #else
145bb898558SAl Viro # define __KERNEL_PERCPU		0
146bb898558SAl Viro #endif
14772d64cc7SIngo Molnar 
14860a5317fSTejun Heo #ifdef CONFIG_CC_STACKPROTECTOR
14960a5317fSTejun Heo # define __KERNEL_STACK_CANARY		(GDT_ENTRY_STACK_CANARY*8)
15060a5317fSTejun Heo #else
15160a5317fSTejun Heo # define __KERNEL_STACK_CANARY		0
15260a5317fSTejun Heo #endif
15360a5317fSTejun Heo 
15484f53788SDenys Vlasenko #else /* 64-bit: */
155bb898558SAl Viro 
156bb898558SAl Viro #include <asm/cache.h>
157bb898558SAl Viro 
158bb898558SAl Viro #define GDT_ENTRY_KERNEL32_CS		1
159bb898558SAl Viro #define GDT_ENTRY_KERNEL_CS		2
160bb898558SAl Viro #define GDT_ENTRY_KERNEL_DS		3
16172d64cc7SIngo Molnar 
162bb898558SAl Viro /*
16372d64cc7SIngo Molnar  * We cannot use the same code segment descriptor for user and kernel mode,
16472d64cc7SIngo Molnar  * not even in long flat mode, because of different DPL.
16572d64cc7SIngo Molnar  *
16672d64cc7SIngo Molnar  * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
16772d64cc7SIngo Molnar  * selectors:
16872d64cc7SIngo Molnar  *
16984f53788SDenys Vlasenko  *   if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
17084f53788SDenys Vlasenko  *   if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
17172d64cc7SIngo Molnar  *
17284f53788SDenys Vlasenko  * ss = STAR.SYSRET_CS+8 (in either case)
17372d64cc7SIngo Molnar  *
17484f53788SDenys Vlasenko  * thus USER_DS should be between 32-bit and 64-bit code selectors:
175bb898558SAl Viro  */
176bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER32_CS	4
177bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	5
178bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	6
179bb898558SAl Viro 
18072d64cc7SIngo Molnar /* Needs two entries */
18172d64cc7SIngo Molnar #define GDT_ENTRY_TSS			8
18272d64cc7SIngo Molnar /* Needs two entries */
18372d64cc7SIngo Molnar #define GDT_ENTRY_LDT			10
18472d64cc7SIngo Molnar 
185bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		12
186bb898558SAl Viro #define GDT_ENTRY_TLS_MAX		14
187bb898558SAl Viro 
18872d64cc7SIngo Molnar /* Abused to load per CPU data from limit */
18972d64cc7SIngo Molnar #define GDT_ENTRY_PER_CPU		15
19084f53788SDenys Vlasenko 
19172d64cc7SIngo Molnar /*
19272d64cc7SIngo Molnar  * Number of entries in the GDT table:
19372d64cc7SIngo Molnar  */
19472d64cc7SIngo Molnar #define GDT_ENTRIES			16
19572d64cc7SIngo Molnar 
19672d64cc7SIngo Molnar /*
19772d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
19872d64cc7SIngo Molnar  *
19972d64cc7SIngo Molnar  * Note, selectors also need to have a correct RPL,
20072d64cc7SIngo Molnar  * expressed with the +3 value for user-space selectors:
20172d64cc7SIngo Molnar  */
20272d64cc7SIngo Molnar #define __KERNEL32_CS			(GDT_ENTRY_KERNEL32_CS*8)
20384f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
20484f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
20584f53788SDenys Vlasenko #define __USER32_CS			(GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
20672d64cc7SIngo Molnar #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
20784f53788SDenys Vlasenko #define __USER32_DS			__USER_DS
20872d64cc7SIngo Molnar #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
209bb898558SAl Viro #define __PER_CPU_SEG			(GDT_ENTRY_PER_CPU*8 + 3)
210bb898558SAl Viro 
211bb898558SAl Viro #endif
212bb898558SAl Viro 
213bb898558SAl Viro #ifndef CONFIG_PARAVIRT
214bb898558SAl Viro # define get_kernel_rpl()		0
215bb898558SAl Viro #endif
216bb898558SAl Viro 
217bb898558SAl Viro #define IDT_ENTRIES			256
218bb898558SAl Viro #define NUM_EXCEPTION_VECTORS		32
21972d64cc7SIngo Molnar 
22072d64cc7SIngo Molnar /* Bitmask of exception vectors which push an error code on the stack: */
22184f4fc52SH. Peter Anvin #define EXCEPTION_ERRCODE_MASK		0x00027d00
22272d64cc7SIngo Molnar 
223bb898558SAl Viro #define GDT_SIZE			(GDT_ENTRIES*8)
224bb898558SAl Viro #define GDT_ENTRY_TLS_ENTRIES		3
225bb898558SAl Viro #define TLS_SIZE			(GDT_ENTRY_TLS_ENTRIES* 8)
226bb898558SAl Viro 
227bb898558SAl Viro #ifdef __KERNEL__
228425be567SAndy Lutomirski 
229425be567SAndy Lutomirski /*
230425be567SAndy Lutomirski  * early_idt_handler_array is an array of entry points referenced in the
231425be567SAndy Lutomirski  * early IDT.  For simplicity, it's a real array with one entry point
232425be567SAndy Lutomirski  * every nine bytes.  That leaves room for an optional 'push $0' if the
233425be567SAndy Lutomirski  * vector has no error code (two bytes), a 'push $vector_number' (two
234425be567SAndy Lutomirski  * bytes), and a jump to the common entry code (up to five bytes).
235425be567SAndy Lutomirski  */
236425be567SAndy Lutomirski #define EARLY_IDT_HANDLER_SIZE 9
237425be567SAndy Lutomirski 
238bb898558SAl Viro #ifndef __ASSEMBLY__
23972d64cc7SIngo Molnar 
240425be567SAndy Lutomirski extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
241*87e81786SThomas Gleixner extern void early_ignore_irq(void);
242f05e798aSDavid Howells 
243f05e798aSDavid Howells /*
24445e876f7SAndy Lutomirski  * Load a segment. Fall back on loading the zero segment if something goes
24545e876f7SAndy Lutomirski  * wrong.  This variant assumes that loading zero fully clears the segment.
24645e876f7SAndy Lutomirski  * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any
24745e876f7SAndy Lutomirski  * failure to fully clear the cached descriptor is only observable for
24845e876f7SAndy Lutomirski  * FS and GS.
249f05e798aSDavid Howells  */
25045e876f7SAndy Lutomirski #define __loadsegment_simple(seg, value)				\
251f05e798aSDavid Howells do {									\
252f05e798aSDavid Howells 	unsigned short __val = (value);					\
253f05e798aSDavid Howells 									\
254f05e798aSDavid Howells 	asm volatile("						\n"	\
255f05e798aSDavid Howells 		     "1:	movl %k0,%%" #seg "		\n"	\
256f05e798aSDavid Howells 									\
257f05e798aSDavid Howells 		     ".section .fixup,\"ax\"			\n"	\
258f05e798aSDavid Howells 		     "2:	xorl %k0,%k0			\n"	\
259f05e798aSDavid Howells 		     "		jmp 1b				\n"	\
260f05e798aSDavid Howells 		     ".previous					\n"	\
261f05e798aSDavid Howells 									\
262f05e798aSDavid Howells 		     _ASM_EXTABLE(1b, 2b)				\
263f05e798aSDavid Howells 									\
264f05e798aSDavid Howells 		     : "+r" (__val) : : "memory");			\
265f05e798aSDavid Howells } while (0)
266f05e798aSDavid Howells 
26745e876f7SAndy Lutomirski #define __loadsegment_ss(value) __loadsegment_simple(ss, (value))
26845e876f7SAndy Lutomirski #define __loadsegment_ds(value) __loadsegment_simple(ds, (value))
26945e876f7SAndy Lutomirski #define __loadsegment_es(value) __loadsegment_simple(es, (value))
27045e876f7SAndy Lutomirski 
27145e876f7SAndy Lutomirski #ifdef CONFIG_X86_32
27245e876f7SAndy Lutomirski 
27345e876f7SAndy Lutomirski /*
27445e876f7SAndy Lutomirski  * On 32-bit systems, the hidden parts of FS and GS are unobservable if
27545e876f7SAndy Lutomirski  * the selector is NULL, so there's no funny business here.
27645e876f7SAndy Lutomirski  */
27745e876f7SAndy Lutomirski #define __loadsegment_fs(value) __loadsegment_simple(fs, (value))
27845e876f7SAndy Lutomirski #define __loadsegment_gs(value) __loadsegment_simple(gs, (value))
27945e876f7SAndy Lutomirski 
28045e876f7SAndy Lutomirski #else
28145e876f7SAndy Lutomirski 
28245e876f7SAndy Lutomirski static inline void __loadsegment_fs(unsigned short value)
28345e876f7SAndy Lutomirski {
28445e876f7SAndy Lutomirski 	asm volatile("						\n"
28545e876f7SAndy Lutomirski 		     "1:	movw %0, %%fs			\n"
28645e876f7SAndy Lutomirski 		     "2:					\n"
28745e876f7SAndy Lutomirski 
28845e876f7SAndy Lutomirski 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs)
28945e876f7SAndy Lutomirski 
29045e876f7SAndy Lutomirski 		     : : "rm" (value) : "memory");
29145e876f7SAndy Lutomirski }
29245e876f7SAndy Lutomirski 
29345e876f7SAndy Lutomirski /* __loadsegment_gs is intentionally undefined.  Use load_gs_index instead. */
29445e876f7SAndy Lutomirski 
29545e876f7SAndy Lutomirski #endif
29645e876f7SAndy Lutomirski 
29745e876f7SAndy Lutomirski #define loadsegment(seg, value) __loadsegment_ ## seg (value)
29845e876f7SAndy Lutomirski 
299f05e798aSDavid Howells /*
30072d64cc7SIngo Molnar  * Save a segment register away:
301f05e798aSDavid Howells  */
302f05e798aSDavid Howells #define savesegment(seg, value)				\
303f05e798aSDavid Howells 	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
304f05e798aSDavid Howells 
305f05e798aSDavid Howells /*
30672d64cc7SIngo Molnar  * x86-32 user GS accessors:
307f05e798aSDavid Howells  */
308f05e798aSDavid Howells #ifdef CONFIG_X86_32
309f05e798aSDavid Howells # ifdef CONFIG_X86_32_LAZY_GS
310f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)({ unsigned long v; savesegment(gs, v); v; })
311f05e798aSDavid Howells #  define set_user_gs(regs, v)		loadsegment(gs, (unsigned long)(v))
312f05e798aSDavid Howells #  define task_user_gs(tsk)		((tsk)->thread.gs)
313f05e798aSDavid Howells #  define lazy_save_gs(v)		savesegment(gs, (v))
314f05e798aSDavid Howells #  define lazy_load_gs(v)		loadsegment(gs, (v))
315f05e798aSDavid Howells # else	/* X86_32_LAZY_GS */
316f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)((regs)->gs)
317f05e798aSDavid Howells #  define set_user_gs(regs, v)		do { (regs)->gs = (v); } while (0)
318f05e798aSDavid Howells #  define task_user_gs(tsk)		(task_pt_regs(tsk)->gs)
319f05e798aSDavid Howells #  define lazy_save_gs(v)		do { } while (0)
320f05e798aSDavid Howells #  define lazy_load_gs(v)		do { } while (0)
321f05e798aSDavid Howells # endif	/* X86_32_LAZY_GS */
322f05e798aSDavid Howells #endif	/* X86_32 */
323f05e798aSDavid Howells 
324f05e798aSDavid Howells #endif /* !__ASSEMBLY__ */
325f05e798aSDavid Howells #endif /* __KERNEL__ */
326bb898558SAl Viro 
3271965aae3SH. Peter Anvin #endif /* _ASM_X86_SEGMENT_H */
328