blob: e1a4afd2022310c274933054ab36422a7d4fc38a [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_SEGMENT_H
2#define _ASM_X86_SEGMENT_H
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +01003
H. Peter Anvin014eea52011-02-14 18:33:55 -08004#include <linux/const.h>
Andy Lutomirski45e876f2016-04-26 12:23:26 -07005#include <asm/alternative.h>
H. Peter Anvin014eea52011-02-14 18:33:55 -08006
Ingo Molnar72d64cc2015-03-24 20:45:42 +01007/*
8 * Constructor for a conventional segment GDT (or LDT) entry.
9 * This is a macro so it can be used in initializers.
10 */
H. Peter Anvin4fdf08b2008-07-17 11:29:24 -070011#define GDT_ENTRY(flags, base, limit) \
H. Peter Anvin014eea52011-02-14 18:33:55 -080012 ((((base) & _AC(0xff000000,ULL)) << (56-24)) | \
13 (((flags) & _AC(0x0000f0ff,ULL)) << 40) | \
14 (((limit) & _AC(0x000f0000,ULL)) << (48-16)) | \
15 (((base) & _AC(0x00ffffff,ULL)) << 16) | \
16 (((limit) & _AC(0x0000ffff,ULL))))
H. Peter Anvin4fdf08b2008-07-17 11:29:24 -070017
Ingo Molnar72d64cc2015-03-24 20:45:42 +010018/* Simple and small GDT entries for booting only: */
H. Peter Anvin2a6648e2008-01-30 13:33:01 +010019
20#define GDT_ENTRY_BOOT_CS 2
Denys Vlasenko84f53782015-03-22 22:01:12 +010021#define GDT_ENTRY_BOOT_DS 3
22#define GDT_ENTRY_BOOT_TSS 4
Ingo Molnar72d64cc2015-03-24 20:45:42 +010023#define __BOOT_CS (GDT_ENTRY_BOOT_CS*8)
24#define __BOOT_DS (GDT_ENTRY_BOOT_DS*8)
25#define __BOOT_TSS (GDT_ENTRY_BOOT_TSS*8)
H. Peter Anvin88089512008-01-30 13:33:02 +010026
Ingo Molnar72d64cc2015-03-24 20:45:42 +010027/*
28 * Bottom two bits of selector give the ring
29 * privilege level
30 */
31#define SEGMENT_RPL_MASK 0x3
32
33/* User mode is privilege level 3: */
34#define USER_RPL 0x3
35
36/* Bit 2 is Table Indicator (TI): selects between LDT or GDT */
37#define SEGMENT_TI_MASK 0x4
38/* LDT segment has TI set ... */
39#define SEGMENT_LDT 0x4
40/* ... GDT has it cleared */
41#define SEGMENT_GDT 0x0
Borislav Petkovbe9d1732014-12-09 13:25:59 +010042
Borislav Petkov6b513112015-04-03 14:25:28 +020043#define GDT_ENTRY_INVALID_SEG 0
44
Thomas Gleixner96a388d2007-10-11 11:20:03 +020045#ifdef CONFIG_X86_32
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010046/*
47 * The layout of the per-CPU GDT under Linux:
48 *
Ingo Molnar72d64cc2015-03-24 20:45:42 +010049 * 0 - null <=== cacheline #1
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010050 * 1 - reserved
51 * 2 - reserved
52 * 3 - reserved
53 *
Ingo Molnar72d64cc2015-03-24 20:45:42 +010054 * 4 - unused <=== cacheline #2
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010055 * 5 - unused
56 *
57 * ------- start of TLS (Thread-Local Storage) segments:
58 *
59 * 6 - TLS segment #1 [ glibc's TLS segment ]
60 * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
Ingo Molnar72d64cc2015-03-24 20:45:42 +010061 * 8 - TLS segment #3 <=== cacheline #3
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010062 * 9 - reserved
63 * 10 - reserved
64 * 11 - reserved
65 *
66 * ------- start of kernel segments:
67 *
Ingo Molnar72d64cc2015-03-24 20:45:42 +010068 * 12 - kernel code segment <=== cacheline #4
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010069 * 13 - kernel data segment
70 * 14 - default user CS
71 * 15 - default user DS
Ingo Molnar72d64cc2015-03-24 20:45:42 +010072 * 16 - TSS <=== cacheline #5
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010073 * 17 - LDT
74 * 18 - PNPBIOS support (16->32 gate)
75 * 19 - PNPBIOS support
Ingo Molnar72d64cc2015-03-24 20:45:42 +010076 * 20 - PNPBIOS support <=== cacheline #6
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010077 * 21 - PNPBIOS support
78 * 22 - PNPBIOS support
79 * 23 - APM BIOS support
Ingo Molnar72d64cc2015-03-24 20:45:42 +010080 * 24 - APM BIOS support <=== cacheline #7
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010081 * 25 - APM BIOS support
82 *
83 * 26 - ESPFIX small SS
84 * 27 - per-cpu [ offset to per-cpu data area ]
Ingo Molnar72d64cc2015-03-24 20:45:42 +010085 * 28 - stack_canary-20 [ for stack protector ] <=== cacheline #8
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010086 * 29 - unused
87 * 30 - unused
88 * 31 - TSS for double fault handler
89 */
Ingo Molnar72d64cc2015-03-24 20:45:42 +010090#define GDT_ENTRY_TLS_MIN 6
91#define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010092
Denys Vlasenko84f53782015-03-22 22:01:12 +010093#define GDT_ENTRY_KERNEL_CS 12
94#define GDT_ENTRY_KERNEL_DS 13
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010095#define GDT_ENTRY_DEFAULT_USER_CS 14
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010096#define GDT_ENTRY_DEFAULT_USER_DS 15
Denys Vlasenko84f53782015-03-22 22:01:12 +010097#define GDT_ENTRY_TSS 16
98#define GDT_ENTRY_LDT 17
99#define GDT_ENTRY_PNPBIOS_CS32 18
100#define GDT_ENTRY_PNPBIOS_CS16 19
101#define GDT_ENTRY_PNPBIOS_DS 20
102#define GDT_ENTRY_PNPBIOS_TS1 21
103#define GDT_ENTRY_PNPBIOS_TS2 22
104#define GDT_ENTRY_APMBIOS_BASE 23
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100105
Denys Vlasenko84f53782015-03-22 22:01:12 +0100106#define GDT_ENTRY_ESPFIX_SS 26
107#define GDT_ENTRY_PERCPU 27
108#define GDT_ENTRY_STACK_CANARY 28
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100109
Denys Vlasenko84f53782015-03-22 22:01:12 +0100110#define GDT_ENTRY_DOUBLEFAULT_TSS 31
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100111
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100112/*
113 * Number of entries in the GDT table:
114 */
115#define GDT_ENTRIES 32
116
117/*
118 * Segment selector values corresponding to the above entries:
119 */
120
Denys Vlasenko84f53782015-03-22 22:01:12 +0100121#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
122#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100123#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
124#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
Jan Beulich32342822010-10-19 14:52:26 +0100125#define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8)
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100126
127/* segment for calling fn: */
128#define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32*8)
129/* code segment for BIOS: */
130#define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16*8)
131
Denys Vlasenko84f53782015-03-22 22:01:12 +0100132/* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100133#define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == PNP_CS32)
134
135/* data segment for BIOS: */
136#define PNP_DS (GDT_ENTRY_PNPBIOS_DS*8)
137/* transfer data segment: */
138#define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1*8)
139/* another data segment: */
140#define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2*8)
141
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100142#ifdef CONFIG_SMP
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100143# define __KERNEL_PERCPU (GDT_ENTRY_PERCPU*8)
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200144#else
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100145# define __KERNEL_PERCPU 0
Tejun Heo60a53172009-02-09 22:17:40 +0900146#endif
147
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100148#ifdef CONFIG_CC_STACKPROTECTOR
149# define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8)
150#else
151# define __KERNEL_STACK_CANARY 0
152#endif
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100153
Denys Vlasenko84f53782015-03-22 22:01:12 +0100154#else /* 64-bit: */
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100155
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100156#include <asm/cache.h>
157
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100158#define GDT_ENTRY_KERNEL32_CS 1
159#define GDT_ENTRY_KERNEL_CS 2
160#define GDT_ENTRY_KERNEL_DS 3
161
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100162/*
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100163 * We cannot use the same code segment descriptor for user and kernel mode,
164 * not even in long flat mode, because of different DPL.
165 *
166 * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
167 * selectors:
168 *
169 * if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
170 * if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
171 *
Denys Vlasenko84f53782015-03-22 22:01:12 +0100172 * ss = STAR.SYSRET_CS+8 (in either case)
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100173 *
Denys Vlasenko84f53782015-03-22 22:01:12 +0100174 * thus USER_DS should be between 32-bit and 64-bit code selectors:
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100175 */
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100176#define GDT_ENTRY_DEFAULT_USER32_CS 4
177#define GDT_ENTRY_DEFAULT_USER_DS 5
178#define GDT_ENTRY_DEFAULT_USER_CS 6
Denys Vlasenko84f53782015-03-22 22:01:12 +0100179
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100180/* Needs two entries */
181#define GDT_ENTRY_TSS 8
182/* Needs two entries */
183#define GDT_ENTRY_LDT 10
Denys Vlasenko84f53782015-03-22 22:01:12 +0100184
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100185#define GDT_ENTRY_TLS_MIN 12
186#define GDT_ENTRY_TLS_MAX 14
Denys Vlasenko84f53782015-03-22 22:01:12 +0100187
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100188/* Abused to load per CPU data from limit */
189#define GDT_ENTRY_PER_CPU 15
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100190
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100191/*
192 * Number of entries in the GDT table:
193 */
194#define GDT_ENTRIES 16
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100195
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100196/*
197 * Segment selector values corresponding to the above entries:
198 *
199 * Note, selectors also need to have a correct RPL,
200 * expressed with the +3 value for user-space selectors:
201 */
202#define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS*8)
203#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
204#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
205#define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
206#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
207#define __USER32_DS __USER_DS
208#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
209#define __PER_CPU_SEG (GDT_ENTRY_PER_CPU*8 + 3)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100210
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100211/* TLS indexes for 64-bit - hardcoded in arch_prctl(): */
212#define FS_TLS 0
213#define GS_TLS 1
214
215#define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
216#define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100217
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200218#endif
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +0100219
220#ifndef CONFIG_PARAVIRT
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100221# define get_kernel_rpl() 0
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +0100222#endif
223
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100224#define IDT_ENTRIES 256
225#define NUM_EXCEPTION_VECTORS 32
226
227/* Bitmask of exception vectors which push an error code on the stack: */
228#define EXCEPTION_ERRCODE_MASK 0x00027d00
229
230#define GDT_SIZE (GDT_ENTRIES*8)
231#define GDT_ENTRY_TLS_ENTRIES 3
232#define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES* 8)
Glauber de Oliveira Costa15ee09b2008-01-30 13:31:09 +0100233
Roland McGrath8866cd92008-01-30 13:33:06 +0100234#ifdef __KERNEL__
Andy Lutomirski425be562015-05-22 16:15:47 -0700235
236/*
237 * early_idt_handler_array is an array of entry points referenced in the
238 * early IDT. For simplicity, it's a real array with one entry point
239 * every nine bytes. That leaves room for an optional 'push $0' if the
240 * vector has no error code (two bytes), a 'push $vector_number' (two
241 * bytes), and a jump to the common entry code (up to five bytes).
242 */
243#define EARLY_IDT_HANDLER_SIZE 9
244
Roland McGrath8866cd92008-01-30 13:33:06 +0100245#ifndef __ASSEMBLY__
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100246
Andy Lutomirski425be562015-05-22 16:15:47 -0700247extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400248#ifdef CONFIG_TRACING
Andy Lutomirski425be562015-05-22 16:15:47 -0700249# define trace_early_idt_handler_array early_idt_handler_array
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400250#endif
David Howellsf05e7982012-03-28 18:11:12 +0100251
252/*
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700253 * Load a segment. Fall back on loading the zero segment if something goes
254 * wrong. This variant assumes that loading zero fully clears the segment.
255 * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any
256 * failure to fully clear the cached descriptor is only observable for
257 * FS and GS.
David Howellsf05e7982012-03-28 18:11:12 +0100258 */
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700259#define __loadsegment_simple(seg, value) \
David Howellsf05e7982012-03-28 18:11:12 +0100260do { \
261 unsigned short __val = (value); \
262 \
263 asm volatile(" \n" \
264 "1: movl %k0,%%" #seg " \n" \
265 \
266 ".section .fixup,\"ax\" \n" \
267 "2: xorl %k0,%k0 \n" \
268 " jmp 1b \n" \
269 ".previous \n" \
270 \
271 _ASM_EXTABLE(1b, 2b) \
272 \
273 : "+r" (__val) : : "memory"); \
274} while (0)
275
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700276#define __loadsegment_ss(value) __loadsegment_simple(ss, (value))
277#define __loadsegment_ds(value) __loadsegment_simple(ds, (value))
278#define __loadsegment_es(value) __loadsegment_simple(es, (value))
279
280#ifdef CONFIG_X86_32
281
282/*
283 * On 32-bit systems, the hidden parts of FS and GS are unobservable if
284 * the selector is NULL, so there's no funny business here.
285 */
286#define __loadsegment_fs(value) __loadsegment_simple(fs, (value))
287#define __loadsegment_gs(value) __loadsegment_simple(gs, (value))
288
289#else
290
291static inline void __loadsegment_fs(unsigned short value)
292{
293 asm volatile(" \n"
294 "1: movw %0, %%fs \n"
295 "2: \n"
296
297 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs)
298
299 : : "rm" (value) : "memory");
300}
301
302/* __loadsegment_gs is intentionally undefined. Use load_gs_index instead. */
303
304#endif
305
306#define loadsegment(seg, value) __loadsegment_ ## seg (value)
307
David Howellsf05e7982012-03-28 18:11:12 +0100308/*
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100309 * Save a segment register away:
David Howellsf05e7982012-03-28 18:11:12 +0100310 */
311#define savesegment(seg, value) \
312 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
313
314/*
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100315 * x86-32 user GS accessors:
David Howellsf05e7982012-03-28 18:11:12 +0100316 */
317#ifdef CONFIG_X86_32
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100318# ifdef CONFIG_X86_32_LAZY_GS
319# define get_user_gs(regs) (u16)({ unsigned long v; savesegment(gs, v); v; })
320# define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
321# define task_user_gs(tsk) ((tsk)->thread.gs)
322# define lazy_save_gs(v) savesegment(gs, (v))
323# define lazy_load_gs(v) loadsegment(gs, (v))
324# else /* X86_32_LAZY_GS */
325# define get_user_gs(regs) (u16)((regs)->gs)
326# define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
327# define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
328# define lazy_save_gs(v) do { } while (0)
329# define lazy_load_gs(v) do { } while (0)
330# endif /* X86_32_LAZY_GS */
David Howellsf05e7982012-03-28 18:11:12 +0100331#endif /* X86_32 */
332
David Howellsf05e7982012-03-28 18:11:12 +0100333#endif /* !__ASSEMBLY__ */
334#endif /* __KERNEL__ */
Roland McGrath8866cd92008-01-30 13:33:06 +0100335
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700336#endif /* _ASM_X86_SEGMENT_H */