xref: /linux/arch/x86/include/asm/segment.h (revision 3fb0fdb3bbe7aed495109b3296b06c2409734023)
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 ]
98*3fb0fdb3SAndy Lutomirski  *  28 - unused
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
121bb898558SAl Viro 
12284f53788SDenys Vlasenko #define GDT_ENTRY_DOUBLEFAULT_TSS	31
123bb898558SAl Viro 
12472d64cc7SIngo Molnar /*
12572d64cc7SIngo Molnar  * Number of entries in the GDT table:
12672d64cc7SIngo Molnar  */
12772d64cc7SIngo Molnar #define GDT_ENTRIES			32
12872d64cc7SIngo Molnar 
12972d64cc7SIngo Molnar /*
13072d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
13172d64cc7SIngo Molnar  */
13272d64cc7SIngo Molnar 
13384f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
13484f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
13584f53788SDenys Vlasenko #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
13684f53788SDenys Vlasenko #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
137bb898558SAl Viro #define __ESPFIX_SS			(GDT_ENTRY_ESPFIX_SS*8)
13872d64cc7SIngo Molnar 
13972d64cc7SIngo Molnar /* segment for calling fn: */
14072d64cc7SIngo Molnar #define PNP_CS32			(GDT_ENTRY_PNPBIOS_CS32*8)
14172d64cc7SIngo Molnar /* code segment for BIOS: */
14272d64cc7SIngo Molnar #define PNP_CS16			(GDT_ENTRY_PNPBIOS_CS16*8)
14372d64cc7SIngo Molnar 
14484f53788SDenys Vlasenko /* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
14584f53788SDenys Vlasenko #define SEGMENT_IS_PNP_CODE(x)		(((x) & 0xf4) == PNP_CS32)
14672d64cc7SIngo Molnar 
14772d64cc7SIngo Molnar /* data segment for BIOS: */
14872d64cc7SIngo Molnar #define PNP_DS				(GDT_ENTRY_PNPBIOS_DS*8)
14972d64cc7SIngo Molnar /* transfer data segment: */
15072d64cc7SIngo Molnar #define PNP_TS1				(GDT_ENTRY_PNPBIOS_TS1*8)
15172d64cc7SIngo Molnar /* another data segment: */
15272d64cc7SIngo Molnar #define PNP_TS2				(GDT_ENTRY_PNPBIOS_TS2*8)
15372d64cc7SIngo Molnar 
154bb898558SAl Viro #ifdef CONFIG_SMP
155bb898558SAl Viro # define __KERNEL_PERCPU		(GDT_ENTRY_PERCPU*8)
156bb898558SAl Viro #else
157bb898558SAl Viro # define __KERNEL_PERCPU		0
158bb898558SAl Viro #endif
15972d64cc7SIngo Molnar 
16084f53788SDenys Vlasenko #else /* 64-bit: */
161bb898558SAl Viro 
162bb898558SAl Viro #include <asm/cache.h>
163bb898558SAl Viro 
164bb898558SAl Viro #define GDT_ENTRY_KERNEL32_CS		1
165bb898558SAl Viro #define GDT_ENTRY_KERNEL_CS		2
166bb898558SAl Viro #define GDT_ENTRY_KERNEL_DS		3
16772d64cc7SIngo Molnar 
168bb898558SAl Viro /*
16972d64cc7SIngo Molnar  * We cannot use the same code segment descriptor for user and kernel mode,
17072d64cc7SIngo Molnar  * not even in long flat mode, because of different DPL.
17172d64cc7SIngo Molnar  *
17272d64cc7SIngo Molnar  * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
17372d64cc7SIngo Molnar  * selectors:
17472d64cc7SIngo Molnar  *
17584f53788SDenys Vlasenko  *   if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
17684f53788SDenys Vlasenko  *   if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
17772d64cc7SIngo Molnar  *
17884f53788SDenys Vlasenko  * ss = STAR.SYSRET_CS+8 (in either case)
17972d64cc7SIngo Molnar  *
18084f53788SDenys Vlasenko  * thus USER_DS should be between 32-bit and 64-bit code selectors:
181bb898558SAl Viro  */
182bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER32_CS	4
183bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_DS	5
184bb898558SAl Viro #define GDT_ENTRY_DEFAULT_USER_CS	6
185bb898558SAl Viro 
18672d64cc7SIngo Molnar /* Needs two entries */
18772d64cc7SIngo Molnar #define GDT_ENTRY_TSS			8
18872d64cc7SIngo Molnar /* Needs two entries */
18972d64cc7SIngo Molnar #define GDT_ENTRY_LDT			10
19072d64cc7SIngo Molnar 
191bb898558SAl Viro #define GDT_ENTRY_TLS_MIN		12
192bb898558SAl Viro #define GDT_ENTRY_TLS_MAX		14
193bb898558SAl Viro 
19422245bdfSIngo Molnar #define GDT_ENTRY_CPUNODE		15
19584f53788SDenys Vlasenko 
19672d64cc7SIngo Molnar /*
19772d64cc7SIngo Molnar  * Number of entries in the GDT table:
19872d64cc7SIngo Molnar  */
19972d64cc7SIngo Molnar #define GDT_ENTRIES			16
20072d64cc7SIngo Molnar 
20172d64cc7SIngo Molnar /*
20272d64cc7SIngo Molnar  * Segment selector values corresponding to the above entries:
20372d64cc7SIngo Molnar  *
20472d64cc7SIngo Molnar  * Note, selectors also need to have a correct RPL,
20572d64cc7SIngo Molnar  * expressed with the +3 value for user-space selectors:
20672d64cc7SIngo Molnar  */
20772d64cc7SIngo Molnar #define __KERNEL32_CS			(GDT_ENTRY_KERNEL32_CS*8)
20884f53788SDenys Vlasenko #define __KERNEL_CS			(GDT_ENTRY_KERNEL_CS*8)
20984f53788SDenys Vlasenko #define __KERNEL_DS			(GDT_ENTRY_KERNEL_DS*8)
21084f53788SDenys Vlasenko #define __USER32_CS			(GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
21172d64cc7SIngo Molnar #define __USER_DS			(GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
21284f53788SDenys Vlasenko #define __USER32_DS			__USER_DS
21372d64cc7SIngo Molnar #define __USER_CS			(GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
21422245bdfSIngo Molnar #define __CPUNODE_SEG			(GDT_ENTRY_CPUNODE*8 + 3)
215bb898558SAl Viro 
216bb898558SAl Viro #endif
217bb898558SAl Viro 
218bb898558SAl Viro #define IDT_ENTRIES			256
219bb898558SAl Viro #define NUM_EXCEPTION_VECTORS		32
22072d64cc7SIngo Molnar 
22172d64cc7SIngo Molnar /* Bitmask of exception vectors which push an error code on the stack: */
2221aa9aa8eSJoerg Roedel #define EXCEPTION_ERRCODE_MASK		0x20027d00
22372d64cc7SIngo Molnar 
224bb898558SAl Viro #define GDT_SIZE			(GDT_ENTRIES*8)
225bb898558SAl Viro #define GDT_ENTRY_TLS_ENTRIES		3
226bb898558SAl Viro #define TLS_SIZE			(GDT_ENTRY_TLS_ENTRIES* 8)
227bb898558SAl Viro 
228ffebbaedSChang S. Bae #ifdef CONFIG_X86_64
229ffebbaedSChang S. Bae 
230ffebbaedSChang S. Bae /* Bit size and mask of CPU number stored in the per CPU data (and TSC_AUX) */
23122245bdfSIngo Molnar #define VDSO_CPUNODE_BITS		12
23222245bdfSIngo Molnar #define VDSO_CPUNODE_MASK		0xfff
233ffebbaedSChang S. Bae 
234ffebbaedSChang S. Bae #ifndef __ASSEMBLY__
235ffebbaedSChang S. Bae 
236ffebbaedSChang S. Bae /* Helper functions to store/load CPU and node numbers */
237ffebbaedSChang S. Bae 
23822245bdfSIngo Molnar static inline unsigned long vdso_encode_cpunode(int cpu, unsigned long node)
239ffebbaedSChang S. Bae {
24022245bdfSIngo Molnar 	return (node << VDSO_CPUNODE_BITS) | cpu;
241ffebbaedSChang S. Bae }
242ffebbaedSChang S. Bae 
24322245bdfSIngo Molnar static inline void vdso_read_cpunode(unsigned *cpu, unsigned *node)
244ffebbaedSChang S. Bae {
245ffebbaedSChang S. Bae 	unsigned int p;
246ffebbaedSChang S. Bae 
247ffebbaedSChang S. Bae 	/*
24822245bdfSIngo Molnar 	 * Load CPU and node number from the GDT.  LSL is faster than RDTSCP
249ffebbaedSChang S. Bae 	 * and works on all CPUs.  This is volatile so that it orders
250ffebbaedSChang S. Bae 	 * correctly with respect to barrier() and to keep GCC from cleverly
251ffebbaedSChang S. Bae 	 * hoisting it out of the calling function.
252ffebbaedSChang S. Bae 	 *
253ffebbaedSChang S. Bae 	 * If RDPID is available, use it.
254ffebbaedSChang S. Bae 	 */
255ffebbaedSChang S. Bae 	alternative_io ("lsl %[seg],%[p]",
256ffebbaedSChang S. Bae 			".byte 0xf3,0x0f,0xc7,0xf8", /* RDPID %eax/rax */
257ffebbaedSChang S. Bae 			X86_FEATURE_RDPID,
25822245bdfSIngo Molnar 			[p] "=a" (p), [seg] "r" (__CPUNODE_SEG));
259ffebbaedSChang S. Bae 
260ffebbaedSChang S. Bae 	if (cpu)
26122245bdfSIngo Molnar 		*cpu = (p & VDSO_CPUNODE_MASK);
262ffebbaedSChang S. Bae 	if (node)
26322245bdfSIngo Molnar 		*node = (p >> VDSO_CPUNODE_BITS);
264ffebbaedSChang S. Bae }
265ffebbaedSChang S. Bae 
266ffebbaedSChang S. Bae #endif /* !__ASSEMBLY__ */
267ffebbaedSChang S. Bae #endif /* CONFIG_X86_64 */
268ffebbaedSChang S. Bae 
269bb898558SAl Viro #ifdef __KERNEL__
270425be567SAndy Lutomirski 
271425be567SAndy Lutomirski /*
272425be567SAndy Lutomirski  * early_idt_handler_array is an array of entry points referenced in the
273425be567SAndy Lutomirski  * early IDT.  For simplicity, it's a real array with one entry point
274425be567SAndy Lutomirski  * every nine bytes.  That leaves room for an optional 'push $0' if the
275425be567SAndy Lutomirski  * vector has no error code (two bytes), a 'push $vector_number' (two
276425be567SAndy Lutomirski  * bytes), and a jump to the common entry code (up to five bytes).
277425be567SAndy Lutomirski  */
278425be567SAndy Lutomirski #define EARLY_IDT_HANDLER_SIZE 9
279425be567SAndy Lutomirski 
28042b3a4cbSJuergen Gross /*
28142b3a4cbSJuergen Gross  * xen_early_idt_handler_array is for Xen pv guests: for each entry in
28242b3a4cbSJuergen Gross  * early_idt_handler_array it contains a prequel in the form of
28342b3a4cbSJuergen Gross  * pop %rcx; pop %r11; jmp early_idt_handler_array[i]; summing up to
28442b3a4cbSJuergen Gross  * max 8 bytes.
28542b3a4cbSJuergen Gross  */
28642b3a4cbSJuergen Gross #define XEN_EARLY_IDT_HANDLER_SIZE 8
28742b3a4cbSJuergen Gross 
288bb898558SAl Viro #ifndef __ASSEMBLY__
28972d64cc7SIngo Molnar 
290425be567SAndy Lutomirski extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
29187e81786SThomas Gleixner extern void early_ignore_irq(void);
292f05e798aSDavid Howells 
293a13f2ef1SJuergen Gross #ifdef CONFIG_XEN_PV
29442b3a4cbSJuergen Gross extern const char xen_early_idt_handler_array[NUM_EXCEPTION_VECTORS][XEN_EARLY_IDT_HANDLER_SIZE];
29542b3a4cbSJuergen Gross #endif
29642b3a4cbSJuergen Gross 
297f05e798aSDavid Howells /*
29845e876f7SAndy Lutomirski  * Load a segment. Fall back on loading the zero segment if something goes
29945e876f7SAndy Lutomirski  * wrong.  This variant assumes that loading zero fully clears the segment.
30045e876f7SAndy Lutomirski  * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any
30145e876f7SAndy Lutomirski  * failure to fully clear the cached descriptor is only observable for
30245e876f7SAndy Lutomirski  * FS and GS.
303f05e798aSDavid Howells  */
30445e876f7SAndy Lutomirski #define __loadsegment_simple(seg, value)				\
305f05e798aSDavid Howells do {									\
306f05e798aSDavid Howells 	unsigned short __val = (value);					\
307f05e798aSDavid Howells 									\
308f05e798aSDavid Howells 	asm volatile("						\n"	\
309f05e798aSDavid Howells 		     "1:	movl %k0,%%" #seg "		\n"	\
310f05e798aSDavid Howells 									\
311f05e798aSDavid Howells 		     ".section .fixup,\"ax\"			\n"	\
312f05e798aSDavid Howells 		     "2:	xorl %k0,%k0			\n"	\
313f05e798aSDavid Howells 		     "		jmp 1b				\n"	\
314f05e798aSDavid Howells 		     ".previous					\n"	\
315f05e798aSDavid Howells 									\
316f05e798aSDavid Howells 		     _ASM_EXTABLE(1b, 2b)				\
317f05e798aSDavid Howells 									\
318f05e798aSDavid Howells 		     : "+r" (__val) : : "memory");			\
319f05e798aSDavid Howells } while (0)
320f05e798aSDavid Howells 
32145e876f7SAndy Lutomirski #define __loadsegment_ss(value) __loadsegment_simple(ss, (value))
32245e876f7SAndy Lutomirski #define __loadsegment_ds(value) __loadsegment_simple(ds, (value))
32345e876f7SAndy Lutomirski #define __loadsegment_es(value) __loadsegment_simple(es, (value))
32445e876f7SAndy Lutomirski 
32545e876f7SAndy Lutomirski #ifdef CONFIG_X86_32
32645e876f7SAndy Lutomirski 
32745e876f7SAndy Lutomirski /*
32845e876f7SAndy Lutomirski  * On 32-bit systems, the hidden parts of FS and GS are unobservable if
32945e876f7SAndy Lutomirski  * the selector is NULL, so there's no funny business here.
33045e876f7SAndy Lutomirski  */
33145e876f7SAndy Lutomirski #define __loadsegment_fs(value) __loadsegment_simple(fs, (value))
33245e876f7SAndy Lutomirski #define __loadsegment_gs(value) __loadsegment_simple(gs, (value))
33345e876f7SAndy Lutomirski 
33445e876f7SAndy Lutomirski #else
33545e876f7SAndy Lutomirski 
33645e876f7SAndy Lutomirski static inline void __loadsegment_fs(unsigned short value)
33745e876f7SAndy Lutomirski {
33845e876f7SAndy Lutomirski 	asm volatile("						\n"
33945e876f7SAndy Lutomirski 		     "1:	movw %0, %%fs			\n"
34045e876f7SAndy Lutomirski 		     "2:					\n"
34145e876f7SAndy Lutomirski 
34245e876f7SAndy Lutomirski 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs)
34345e876f7SAndy Lutomirski 
34445e876f7SAndy Lutomirski 		     : : "rm" (value) : "memory");
34545e876f7SAndy Lutomirski }
34645e876f7SAndy Lutomirski 
34745e876f7SAndy Lutomirski /* __loadsegment_gs is intentionally undefined.  Use load_gs_index instead. */
34845e876f7SAndy Lutomirski 
34945e876f7SAndy Lutomirski #endif
35045e876f7SAndy Lutomirski 
35145e876f7SAndy Lutomirski #define loadsegment(seg, value) __loadsegment_ ## seg (value)
35245e876f7SAndy Lutomirski 
353f05e798aSDavid Howells /*
35472d64cc7SIngo Molnar  * Save a segment register away:
355f05e798aSDavid Howells  */
356f05e798aSDavid Howells #define savesegment(seg, value)				\
357f05e798aSDavid Howells 	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
358f05e798aSDavid Howells 
359f05e798aSDavid Howells /*
360*3fb0fdb3SAndy Lutomirski  * x86-32 user GS accessors.  This is ugly and could do with some cleaning up.
361f05e798aSDavid Howells  */
362f05e798aSDavid Howells #ifdef CONFIG_X86_32
363f05e798aSDavid Howells # define get_user_gs(regs)		(u16)({ unsigned long v; savesegment(gs, v); v; })
364f05e798aSDavid Howells # define set_user_gs(regs, v)		loadsegment(gs, (unsigned long)(v))
365f05e798aSDavid Howells # define task_user_gs(tsk)		((tsk)->thread.gs)
366f05e798aSDavid Howells # define lazy_save_gs(v)		savesegment(gs, (v))
367f05e798aSDavid Howells # define lazy_load_gs(v)		loadsegment(gs, (v))
368*3fb0fdb3SAndy Lutomirski # define load_gs_index(v)		loadsegment(gs, (v))
369f05e798aSDavid Howells #endif	/* X86_32 */
370f05e798aSDavid Howells 
371f05e798aSDavid Howells #endif /* !__ASSEMBLY__ */
372f05e798aSDavid Howells #endif /* __KERNEL__ */
373bb898558SAl Viro 
3741965aae3SH. Peter Anvin #endif /* _ASM_X86_SEGMENT_H */
375