xref: /linux/arch/x86/include/asm/segment.h (revision a13f2ef168cb2a033a284eb841bcc481ffbc90cf)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_SEGMENT_H
31965aae3SH. Peter Anvin #define _ASM_X86_SEGMENT_H
4bb898558SAl Viro 
5014eea51SH. Peter Anvin #include <linux/const.h>
645e876f7SAndy Lutomirski #include <asm/alternative.h>
7014eea51SH. Peter Anvin 
872d64cc7SIngo Molnar /*
972d64cc7SIngo Molnar  * Constructor for a conventional segment GDT (or LDT) entry.
1072d64cc7SIngo Molnar  * This is a macro so it can be used in initializers.
1172d64cc7SIngo Molnar  */
12bb898558SAl Viro #define GDT_ENTRY(flags, base, limit)			\
13014eea51SH. Peter Anvin 	((((base)  & _AC(0xff000000,ULL)) << (56-24)) |	\
14014eea51SH. Peter Anvin 	 (((flags) & _AC(0x0000f0ff,ULL)) << 40) |	\
15014eea51SH. Peter Anvin 	 (((limit) & _AC(0x000f0000,ULL)) << (48-16)) |	\
16014eea51SH. Peter Anvin 	 (((base)  & _AC(0x00ffffff,ULL)) << 16) |	\
17014eea51SH. Peter Anvin 	 (((limit) & _AC(0x0000ffff,ULL))))
18bb898558SAl Viro 
1972d64cc7SIngo Molnar /* Simple and small GDT entries for booting only: */
20bb898558SAl Viro 
21bb898558SAl Viro #define GDT_ENTRY_BOOT_CS	2
2284f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_DS	3
2384f53788SDenys Vlasenko #define GDT_ENTRY_BOOT_TSS	4
24bb898558SAl Viro #define __BOOT_CS		(GDT_ENTRY_BOOT_CS*8)
25bb898558SAl Viro #define __BOOT_DS		(GDT_ENTRY_BOOT_DS*8)
26bb898558SAl Viro #define __BOOT_TSS		(GDT_ENTRY_BOOT_TSS*8)
27bb898558SAl Viro 
2872d64cc7SIngo Molnar /*
29be9d1738SBorislav Petkov  * Bottom two bits of selector give the ring
30be9d1738SBorislav Petkov  * privilege level
31be9d1738SBorislav Petkov  */
3272d64cc7SIngo Molnar #define SEGMENT_RPL_MASK	0x3
3372d64cc7SIngo Molnar 
3481ff2c37SJan Beulich /*
3581ff2c37SJan Beulich  * When running on Xen PV, the actual privilege level of the kernel is 1,
3681ff2c37SJan Beulich  * not 0. Testing the Requested Privilege Level in a segment selector to
3781ff2c37SJan Beulich  * determine whether the context is user mode or kernel mode with
3881ff2c37SJan Beulich  * SEGMENT_RPL_MASK is wrong because the PV kernel's privilege level
3981ff2c37SJan Beulich  * matches the 0x3 mask.
4081ff2c37SJan Beulich  *
4181ff2c37SJan Beulich  * Testing with USER_SEGMENT_RPL_MASK is valid for both native and Xen PV
4281ff2c37SJan Beulich  * kernels because privilege level 2 is never used.
4381ff2c37SJan Beulich  */
4481ff2c37SJan Beulich #define USER_SEGMENT_RPL_MASK	0x2
4581ff2c37SJan Beulich 
4672d64cc7SIngo Molnar /* User mode is privilege level 3: */
4772d64cc7SIngo Molnar #define USER_RPL		0x3
4872d64cc7SIngo Molnar 
4972d64cc7SIngo Molnar /* Bit 2 is Table Indicator (TI): selects between LDT or GDT */
5072d64cc7SIngo Molnar #define SEGMENT_TI_MASK		0x4
5172d64cc7SIngo Molnar /* LDT segment has TI set ... */
5272d64cc7SIngo Molnar #define SEGMENT_LDT		0x4
5372d64cc7SIngo Molnar /* ... GDT has it cleared */
5472d64cc7SIngo Molnar #define SEGMENT_GDT		0x0
55be9d1738SBorislav Petkov 
566b51311cSBorislav Petkov #define GDT_ENTRY_INVALID_SEG	0
576b51311cSBorislav Petkov 
58bb898558SAl Viro #ifdef CONFIG_X86_32
59bb898558SAl Viro /*
60bb898558SAl Viro  * The layout of the per-CPU GDT under Linux:
61bb898558SAl Viro  *
6272d64cc7SIngo Molnar  *   0 - null								<=== cacheline #1
63bb898558SAl Viro  *   1 - reserved
64bb898558SAl Viro  *   2 - reserved
65bb898558SAl Viro  *   3 - reserved
66bb898558SAl Viro  *
6772d64cc7SIngo Molnar  *   4 - unused								<=== cacheline #2
68bb898558SAl Viro  *   5 - unused
69bb898558SAl Viro  *
70bb898558SAl Viro  *  ------- start of TLS (Thread-Local Storage) segments:
71bb898558SAl Viro  *
72bb898558SAl Viro  *   6 - TLS segment #1			[ glibc's TLS segment ]
73bb898558SAl Viro  *   7 - TLS segment #2			[ Wine's %fs Win32 segment ]
7472d64cc7SIngo Molnar  *   8 - TLS segment #3							<=== cacheline #3
75bb898558SAl Viro  *   9 - reserved
76bb898558SAl Viro  *  10 - reserved
77bb898558SAl Viro  *  11 - reserved
78bb898558SAl Viro  *
79bb898558SAl Viro  *  ------- start of kernel segments:
80bb898558SAl Viro  *
8172d64cc7SIngo Molnar  *  12 - kernel code segment						<=== cacheline #4
82bb898558SAl Viro  *  13 - kernel data segment
83bb898558SAl Viro  *  14 - default user CS
84bb898558SAl Viro  *  15 - default user DS
8572d64cc7SIngo Molnar  *  16 - TSS								<=== cacheline #5
86bb898558SAl Viro  *  17 - LDT
87bb898558SAl Viro  *  18 - PNPBIOS support (16->32 gate)
88bb898558SAl Viro  *  19 - PNPBIOS support
8972d64cc7SIngo Molnar  *  20 - PNPBIOS support						<=== cacheline #6
90bb898558SAl Viro  *  21 - PNPBIOS support
91bb898558SAl Viro  *  22 - PNPBIOS support
92bb898558SAl Viro  *  23 - APM BIOS support
9372d64cc7SIngo Molnar  *  24 - APM BIOS support						<=== cacheline #7
94bb898558SAl Viro  *  25 - APM BIOS support
95bb898558SAl Viro  *
96bb898558SAl Viro  *  26 - ESPFIX small SS
97bb898558SAl Viro  *  27 - per-cpu			[ offset to per-cpu data area ]
9872d64cc7SIngo Molnar  *  28 - stack_canary-20		[ for stack protector ]		<=== cacheline #8
99bb898558SAl Viro  *  29 - unused
100bb898558SAl Viro  *  30 - unused
101bb898558SAl Viro  *  31 - TSS for double fault handler
102bb898558SAl Viro  */
103bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		6
104bb898558SAl Viro #define GDT_ENTRY_TLS_MAX 		(GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
105bb898558SAl Viro 
10684f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_CS		12
10784f53788SDenys Vlasenko #define GDT_ENTRY_KERNEL_DS		13
108bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	14
109bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	15
11084f53788SDenys Vlasenko #define GDT_ENTRY_TSS			16
11184f53788SDenys Vlasenko #define GDT_ENTRY_LDT			17
11284f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS32		18
11384f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_CS16		19
11484f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_DS		20
11584f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS1		21
11684f53788SDenys Vlasenko #define GDT_ENTRY_PNPBIOS_TS2		22
11784f53788SDenys Vlasenko #define GDT_ENTRY_APMBIOS_BASE		23
118bb898558SAl Viro 
11984f53788SDenys Vlasenko #define GDT_ENTRY_ESPFIX_SS		26
12084f53788SDenys Vlasenko #define GDT_ENTRY_PERCPU		27
12184f53788SDenys Vlasenko #define GDT_ENTRY_STACK_CANARY		28
122bb898558SAl Viro 
12384f53788SDenys Vlasenko #define GDT_ENTRY_DOUBLEFAULT_TSS	31
124bb898558SAl Viro 
12572d64cc7SIngo Molnar /*
12672d64cc7SIngo Molnar  * Number of entries in the GDT table:
12772d64cc7SIngo Molnar  */
12872d64cc7SIngo Molnar #define GDT_ENTRIES			32
12972d64cc7SIngo Molnar 
13072d64cc7SIngo Molnar /*
13172d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
13272d64cc7SIngo Molnar  */
13372d64cc7SIngo Molnar 
13484f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
13584f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
13684f53788SDenys Vlasenko #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
13784f53788SDenys Vlasenko #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
138bb898558SAl Viro #define __ESPFIX_SS			(GDT_ENTRY_ESPFIX_SS*8)
13972d64cc7SIngo Molnar 
14072d64cc7SIngo Molnar /* segment for calling fn: */
14172d64cc7SIngo Molnar #define PNP_CS32			(GDT_ENTRY_PNPBIOS_CS32*8)
14272d64cc7SIngo Molnar /* code segment for BIOS: */
14372d64cc7SIngo Molnar #define PNP_CS16			(GDT_ENTRY_PNPBIOS_CS16*8)
14472d64cc7SIngo Molnar 
14584f53788SDenys Vlasenko /* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
14684f53788SDenys Vlasenko #define SEGMENT_IS_PNP_CODE(x)		(((x) & 0xf4) == PNP_CS32)
14772d64cc7SIngo Molnar 
14872d64cc7SIngo Molnar /* data segment for BIOS: */
14972d64cc7SIngo Molnar #define PNP_DS				(GDT_ENTRY_PNPBIOS_DS*8)
15072d64cc7SIngo Molnar /* transfer data segment: */
15172d64cc7SIngo Molnar #define PNP_TS1				(GDT_ENTRY_PNPBIOS_TS1*8)
15272d64cc7SIngo Molnar /* another data segment: */
15372d64cc7SIngo Molnar #define PNP_TS2				(GDT_ENTRY_PNPBIOS_TS2*8)
15472d64cc7SIngo Molnar 
155bb898558SAl Viro #ifdef CONFIG_SMP
156bb898558SAl Viro # define __KERNEL_PERCPU		(GDT_ENTRY_PERCPU*8)
157bb898558SAl Viro #else
158bb898558SAl Viro # define __KERNEL_PERCPU		0
159bb898558SAl Viro #endif
16072d64cc7SIngo Molnar 
161050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR
16260a5317fSTejun Heo # define __KERNEL_STACK_CANARY		(GDT_ENTRY_STACK_CANARY*8)
16360a5317fSTejun Heo #else
16460a5317fSTejun Heo # define __KERNEL_STACK_CANARY		0
16560a5317fSTejun Heo #endif
16660a5317fSTejun Heo 
16784f53788SDenys Vlasenko #else /* 64-bit: */
168bb898558SAl Viro 
169bb898558SAl Viro #include <asm/cache.h>
170bb898558SAl Viro 
171bb898558SAl Viro #define GDT_ENTRY_KERNEL32_CS		1
172bb898558SAl Viro #define GDT_ENTRY_KERNEL_CS		2
173bb898558SAl Viro #define GDT_ENTRY_KERNEL_DS		3
17472d64cc7SIngo Molnar 
175bb898558SAl Viro /*
17672d64cc7SIngo Molnar  * We cannot use the same code segment descriptor for user and kernel mode,
17772d64cc7SIngo Molnar  * not even in long flat mode, because of different DPL.
17872d64cc7SIngo Molnar  *
17972d64cc7SIngo Molnar  * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
18072d64cc7SIngo Molnar  * selectors:
18172d64cc7SIngo Molnar  *
18284f53788SDenys Vlasenko  *   if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
18384f53788SDenys Vlasenko  *   if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
18472d64cc7SIngo Molnar  *
18584f53788SDenys Vlasenko  * ss = STAR.SYSRET_CS+8 (in either case)
18672d64cc7SIngo Molnar  *
18784f53788SDenys Vlasenko  * thus USER_DS should be between 32-bit and 64-bit code selectors:
188bb898558SAl Viro  */
189bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER32_CS	4
190bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	5
191bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	6
192bb898558SAl Viro 
19372d64cc7SIngo Molnar /* Needs two entries */
19472d64cc7SIngo Molnar #define GDT_ENTRY_TSS			8
19572d64cc7SIngo Molnar /* Needs two entries */
19672d64cc7SIngo Molnar #define GDT_ENTRY_LDT			10
19772d64cc7SIngo Molnar 
198bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		12
199bb898558SAl Viro #define GDT_ENTRY_TLS_MAX		14
200bb898558SAl Viro 
20122245bdfSIngo Molnar #define GDT_ENTRY_CPUNODE		15
20284f53788SDenys Vlasenko 
20372d64cc7SIngo Molnar /*
20472d64cc7SIngo Molnar  * Number of entries in the GDT table:
20572d64cc7SIngo Molnar  */
20672d64cc7SIngo Molnar #define GDT_ENTRIES			16
20772d64cc7SIngo Molnar 
20872d64cc7SIngo Molnar /*
20972d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
21072d64cc7SIngo Molnar  *
21172d64cc7SIngo Molnar  * Note, selectors also need to have a correct RPL,
21272d64cc7SIngo Molnar  * expressed with the +3 value for user-space selectors:
21372d64cc7SIngo Molnar  */
21472d64cc7SIngo Molnar #define __KERNEL32_CS			(GDT_ENTRY_KERNEL32_CS*8)
21584f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
21684f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
21784f53788SDenys Vlasenko #define __USER32_CS			(GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
21872d64cc7SIngo Molnar #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
21984f53788SDenys Vlasenko #define __USER32_DS			__USER_DS
22072d64cc7SIngo Molnar #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
22122245bdfSIngo Molnar #define __CPUNODE_SEG			(GDT_ENTRY_CPUNODE*8 + 3)
222bb898558SAl Viro 
223bb898558SAl Viro #endif
224bb898558SAl Viro 
22540181646SJuergen Gross #ifndef CONFIG_PARAVIRT_XXL
226bb898558SAl Viro # define get_kernel_rpl()		0
227bb898558SAl Viro #endif
228bb898558SAl Viro 
229bb898558SAl Viro #define IDT_ENTRIES			256
230bb898558SAl Viro #define NUM_EXCEPTION_VECTORS		32
23172d64cc7SIngo Molnar 
23272d64cc7SIngo Molnar /* Bitmask of exception vectors which push an error code on the stack: */
23384f4fc52SH. Peter Anvin #define EXCEPTION_ERRCODE_MASK		0x00027d00
23472d64cc7SIngo Molnar 
235bb898558SAl Viro #define GDT_SIZE			(GDT_ENTRIES*8)
236bb898558SAl Viro #define GDT_ENTRY_TLS_ENTRIES		3
237bb898558SAl Viro #define TLS_SIZE			(GDT_ENTRY_TLS_ENTRIES* 8)
238bb898558SAl Viro 
239ffebbaedSChang S. Bae #ifdef CONFIG_X86_64
240ffebbaedSChang S. Bae 
241ffebbaedSChang S. Bae /* Bit size and mask of CPU number stored in the per CPU data (and TSC_AUX) */
24222245bdfSIngo Molnar #define VDSO_CPUNODE_BITS		12
24322245bdfSIngo Molnar #define VDSO_CPUNODE_MASK		0xfff
244ffebbaedSChang S. Bae 
245ffebbaedSChang S. Bae #ifndef __ASSEMBLY__
246ffebbaedSChang S. Bae 
247ffebbaedSChang S. Bae /* Helper functions to store/load CPU and node numbers */
248ffebbaedSChang S. Bae 
24922245bdfSIngo Molnar static inline unsigned long vdso_encode_cpunode(int cpu, unsigned long node)
250ffebbaedSChang S. Bae {
25122245bdfSIngo Molnar 	return (node << VDSO_CPUNODE_BITS) | cpu;
252ffebbaedSChang S. Bae }
253ffebbaedSChang S. Bae 
25422245bdfSIngo Molnar static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
255ffebbaedSChang S. Bae {
256ffebbaedSChang S. Bae 	unsigned int p;
257ffebbaedSChang S. Bae 
258ffebbaedSChang S. Bae 	/*
25922245bdfSIngo Molnar 	 * Load CPU and node number from the GDT.  LSL is faster than RDTSCP
260ffebbaedSChang S. Bae 	 * and works on all CPUs.  This is volatile so that it orders
261ffebbaedSChang S. Bae 	 * correctly with respect to barrier() and to keep GCC from cleverly
262ffebbaedSChang S. Bae 	 * hoisting it out of the calling function.
263ffebbaedSChang S. Bae 	 *
264ffebbaedSChang S. Bae 	 * If RDPID is available, use it.
265ffebbaedSChang S. Bae 	 */
266ffebbaedSChang S. Bae 	alternative_io ("lsl %[seg],%[p]",
267ffebbaedSChang S. Bae 			".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
268ffebbaedSChang S. Bae 			X86_FEATURE_RDPID,
26922245bdfSIngo Molnar 			[p] "=a" (p), [seg] "r" (__CPUNODE_SEG));
270ffebbaedSChang S. Bae 
271ffebbaedSChang S. Bae 	if (cpu)
27222245bdfSIngo Molnar 		*cpu = (p & VDSO_CPUNODE_MASK);
273ffebbaedSChang S. Bae 	if (node)
27422245bdfSIngo Molnar 		*node = (p >> VDSO_CPUNODE_BITS);
275ffebbaedSChang S. Bae }
276ffebbaedSChang S. Bae 
277ffebbaedSChang S. Bae #endif /* !__ASSEMBLY__ */
278ffebbaedSChang S. Bae #endif /* CONFIG_X86_64 */
279ffebbaedSChang S. Bae 
280bb898558SAl Viro #ifdef __KERNEL__
281425be567SAndy Lutomirski 
282425be567SAndy Lutomirski /*
283425be567SAndy Lutomirski  * early_idt_handler_array is an array of entry points referenced in the
284425be567SAndy Lutomirski  * early IDT.  For simplicity, it's a real array with one entry point
285425be567SAndy Lutomirski  * every nine bytes.  That leaves room for an optional 'push $0' if the
286425be567SAndy Lutomirski  * vector has no error code (two bytes), a 'push $vector_number' (two
287425be567SAndy Lutomirski  * bytes), and a jump to the common entry code (up to five bytes).
288425be567SAndy Lutomirski  */
289425be567SAndy Lutomirski #define EARLY_IDT_HANDLER_SIZE 9
290425be567SAndy Lutomirski 
29142b3a4cbSJuergen Gross /*
29242b3a4cbSJuergen Gross  * xen_early_idt_handler_array is for Xen pv guests: for each entry in
29342b3a4cbSJuergen Gross  * early_idt_handler_array it contains a prequel in the form of
29442b3a4cbSJuergen Gross  * pop %rcx; pop %r11; jmp early_idt_handler_array[i]; summing up to
29542b3a4cbSJuergen Gross  * max 8 bytes.
29642b3a4cbSJuergen Gross  */
29742b3a4cbSJuergen Gross #define XEN_EARLY_IDT_HANDLER_SIZE 8
29842b3a4cbSJuergen Gross 
299bb898558SAl Viro #ifndef __ASSEMBLY__
30072d64cc7SIngo Molnar 
301425be567SAndy Lutomirski extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
30287e81786SThomas Gleixner extern void early_ignore_irq(void);
303f05e798aSDavid Howells 
304*a13f2ef1SJuergen Gross #ifdef CONFIG_XEN_PV
30542b3a4cbSJuergen Gross extern const char xen_early_idt_handler_array[NUM_EXCEPTION_VECTORS][XEN_EARLY_IDT_HANDLER_SIZE];
30642b3a4cbSJuergen Gross #endif
30742b3a4cbSJuergen Gross 
308f05e798aSDavid Howells /*
30945e876f7SAndy Lutomirski  * Load a segment. Fall back on loading the zero segment if something goes
31045e876f7SAndy Lutomirski  * wrong.  This variant assumes that loading zero fully clears the segment.
31145e876f7SAndy Lutomirski  * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any
31245e876f7SAndy Lutomirski  * failure to fully clear the cached descriptor is only observable for
31345e876f7SAndy Lutomirski  * FS and GS.
314f05e798aSDavid Howells  */
31545e876f7SAndy Lutomirski #define __loadsegment_simple(seg, value)				\
316f05e798aSDavid Howells do {									\
317f05e798aSDavid Howells 	unsigned short __val = (value);					\
318f05e798aSDavid Howells 									\
319f05e798aSDavid Howells 	asm volatile("						\n"	\
320f05e798aSDavid Howells 		     "1:	movl %k0,%%" #seg "		\n"	\
321f05e798aSDavid Howells 									\
322f05e798aSDavid Howells 		     ".section .fixup,\"ax\"			\n"	\
323f05e798aSDavid Howells 		     "2:	xorl %k0,%k0			\n"	\
324f05e798aSDavid Howells 		     "		jmp 1b				\n"	\
325f05e798aSDavid Howells 		     ".previous					\n"	\
326f05e798aSDavid Howells 									\
327f05e798aSDavid Howells 		     _ASM_EXTABLE(1b, 2b)				\
328f05e798aSDavid Howells 									\
329f05e798aSDavid Howells 		     : "+r" (__val) : : "memory");			\
330f05e798aSDavid Howells } while (0)
331f05e798aSDavid Howells 
33245e876f7SAndy Lutomirski #define __loadsegment_ss(value) __loadsegment_simple(ss, (value))
33345e876f7SAndy Lutomirski #define __loadsegment_ds(value) __loadsegment_simple(ds, (value))
33445e876f7SAndy Lutomirski #define __loadsegment_es(value) __loadsegment_simple(es, (value))
33545e876f7SAndy Lutomirski 
33645e876f7SAndy Lutomirski #ifdef CONFIG_X86_32
33745e876f7SAndy Lutomirski 
33845e876f7SAndy Lutomirski /*
33945e876f7SAndy Lutomirski  * On 32-bit systems, the hidden parts of FS and GS are unobservable if
34045e876f7SAndy Lutomirski  * the selector is NULL, so there's no funny business here.
34145e876f7SAndy Lutomirski  */
34245e876f7SAndy Lutomirski #define __loadsegment_fs(value) __loadsegment_simple(fs, (value))
34345e876f7SAndy Lutomirski #define __loadsegment_gs(value) __loadsegment_simple(gs, (value))
34445e876f7SAndy Lutomirski 
34545e876f7SAndy Lutomirski #else
34645e876f7SAndy Lutomirski 
34745e876f7SAndy Lutomirski static inline void __loadsegment_fs(unsigned short value)
34845e876f7SAndy Lutomirski {
34945e876f7SAndy Lutomirski 	asm volatile("						\n"
35045e876f7SAndy Lutomirski 		     "1:	movw %0, %%fs			\n"
35145e876f7SAndy Lutomirski 		     "2:					\n"
35245e876f7SAndy Lutomirski 
35345e876f7SAndy Lutomirski 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs)
35445e876f7SAndy Lutomirski 
35545e876f7SAndy Lutomirski 		     : : "rm" (value) : "memory");
35645e876f7SAndy Lutomirski }
35745e876f7SAndy Lutomirski 
35845e876f7SAndy Lutomirski /* __loadsegment_gs is intentionally undefined.  Use load_gs_index instead. */
35945e876f7SAndy Lutomirski 
36045e876f7SAndy Lutomirski #endif
36145e876f7SAndy Lutomirski 
36245e876f7SAndy Lutomirski #define loadsegment(seg, value) __loadsegment_ ## seg (value)
36345e876f7SAndy Lutomirski 
364f05e798aSDavid Howells /*
36572d64cc7SIngo Molnar  * Save a segment register away:
366f05e798aSDavid Howells  */
367f05e798aSDavid Howells #define savesegment(seg, value)				\
368f05e798aSDavid Howells 	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
369f05e798aSDavid Howells 
370f05e798aSDavid Howells /*
37172d64cc7SIngo Molnar  * x86-32 user GS accessors:
372f05e798aSDavid Howells  */
373f05e798aSDavid Howells #ifdef CONFIG_X86_32
374f05e798aSDavid Howells # ifdef CONFIG_X86_32_LAZY_GS
375f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)({ unsigned long v; savesegment(gs, v); v; })
376f05e798aSDavid Howells #  define set_user_gs(regs, v)		loadsegment(gs, (unsigned long)(v))
377f05e798aSDavid Howells #  define task_user_gs(tsk)		((tsk)->thread.gs)
378f05e798aSDavid Howells #  define lazy_save_gs(v)		savesegment(gs, (v))
379f05e798aSDavid Howells #  define lazy_load_gs(v)		loadsegment(gs, (v))
380f05e798aSDavid Howells # else	/* X86_32_LAZY_GS */
381f05e798aSDavid Howells #  define get_user_gs(regs)		(u16)((regs)->gs)
382f05e798aSDavid Howells #  define set_user_gs(regs, v)		do { (regs)->gs = (v); } while (0)
383f05e798aSDavid Howells #  define task_user_gs(tsk)		(task_pt_regs(tsk)->gs)
384f05e798aSDavid Howells #  define lazy_save_gs(v)		do { } while (0)
385f05e798aSDavid Howells #  define lazy_load_gs(v)		do { } while (0)
386f05e798aSDavid Howells # endif	/* X86_32_LAZY_GS */
387f05e798aSDavid Howells #endif	/* X86_32 */
388f05e798aSDavid Howells 
389f05e798aSDavid Howells #endif /* !__ASSEMBLY__ */
390f05e798aSDavid Howells #endif /* __KERNEL__ */
391bb898558SAl Viro 
3921965aae3SH. Peter Anvin #endif /* _ASM_X86_SEGMENT_H */
393