blob: e293c122d0d54fbc802c1212edc9ed099be582f0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_SEGMENT_H
3#define _ASM_X86_SEGMENT_H
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +01004
H. Peter Anvin014eea52011-02-14 18:33:55 -08005#include <linux/const.h>
Andy Lutomirski45e876f2016-04-26 12:23:26 -07006#include <asm/alternative.h>
H. Peter Anvin014eea52011-02-14 18:33:55 -08007
Ingo Molnar72d64cc2015-03-24 20:45:42 +01008/*
9 * Constructor for a conventional segment GDT (or LDT) entry.
10 * This is a macro so it can be used in initializers.
11 */
H. Peter Anvin4fdf08b2008-07-17 11:29:24 -070012#define GDT_ENTRY(flags, base, limit) \
H. Peter Anvin014eea52011-02-14 18:33:55 -080013 ((((base) & _AC(0xff000000,ULL)) << (56-24)) | \
14 (((flags) & _AC(0x0000f0ff,ULL)) << 40) | \
15 (((limit) & _AC(0x000f0000,ULL)) << (48-16)) | \
16 (((base) & _AC(0x00ffffff,ULL)) << 16) | \
17 (((limit) & _AC(0x0000ffff,ULL))))
H. Peter Anvin4fdf08b2008-07-17 11:29:24 -070018
Ingo Molnar72d64cc2015-03-24 20:45:42 +010019/* Simple and small GDT entries for booting only: */
H. Peter Anvin2a6648e2008-01-30 13:33:01 +010020
21#define GDT_ENTRY_BOOT_CS 2
Denys Vlasenko84f53782015-03-22 22:01:12 +010022#define GDT_ENTRY_BOOT_DS 3
23#define GDT_ENTRY_BOOT_TSS 4
Ingo Molnar72d64cc2015-03-24 20:45:42 +010024#define __BOOT_CS (GDT_ENTRY_BOOT_CS*8)
25#define __BOOT_DS (GDT_ENTRY_BOOT_DS*8)
26#define __BOOT_TSS (GDT_ENTRY_BOOT_TSS*8)
H. Peter Anvin88089512008-01-30 13:33:02 +010027
Ingo Molnar72d64cc2015-03-24 20:45:42 +010028/*
29 * Bottom two bits of selector give the ring
30 * privilege level
31 */
32#define SEGMENT_RPL_MASK 0x3
33
34/* User mode is privilege level 3: */
35#define USER_RPL 0x3
36
37/* Bit 2 is Table Indicator (TI): selects between LDT or GDT */
38#define SEGMENT_TI_MASK 0x4
39/* LDT segment has TI set ... */
40#define SEGMENT_LDT 0x4
41/* ... GDT has it cleared */
42#define SEGMENT_GDT 0x0
Borislav Petkovbe9d1732014-12-09 13:25:59 +010043
Borislav Petkov6b513112015-04-03 14:25:28 +020044#define GDT_ENTRY_INVALID_SEG 0
45
Thomas Gleixner96a388d2007-10-11 11:20:03 +020046#ifdef CONFIG_X86_32
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010047/*
48 * The layout of the per-CPU GDT under Linux:
49 *
Ingo Molnar72d64cc2015-03-24 20:45:42 +010050 * 0 - null <=== cacheline #1
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010051 * 1 - reserved
52 * 2 - reserved
53 * 3 - reserved
54 *
Ingo Molnar72d64cc2015-03-24 20:45:42 +010055 * 4 - unused <=== cacheline #2
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010056 * 5 - unused
57 *
58 * ------- start of TLS (Thread-Local Storage) segments:
59 *
60 * 6 - TLS segment #1 [ glibc's TLS segment ]
61 * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
Ingo Molnar72d64cc2015-03-24 20:45:42 +010062 * 8 - TLS segment #3 <=== cacheline #3
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010063 * 9 - reserved
64 * 10 - reserved
65 * 11 - reserved
66 *
67 * ------- start of kernel segments:
68 *
Ingo Molnar72d64cc2015-03-24 20:45:42 +010069 * 12 - kernel code segment <=== cacheline #4
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010070 * 13 - kernel data segment
71 * 14 - default user CS
72 * 15 - default user DS
Ingo Molnar72d64cc2015-03-24 20:45:42 +010073 * 16 - TSS <=== cacheline #5
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010074 * 17 - LDT
75 * 18 - PNPBIOS support (16->32 gate)
76 * 19 - PNPBIOS support
Ingo Molnar72d64cc2015-03-24 20:45:42 +010077 * 20 - PNPBIOS support <=== cacheline #6
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010078 * 21 - PNPBIOS support
79 * 22 - PNPBIOS support
80 * 23 - APM BIOS support
Ingo Molnar72d64cc2015-03-24 20:45:42 +010081 * 24 - APM BIOS support <=== cacheline #7
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010082 * 25 - APM BIOS support
83 *
84 * 26 - ESPFIX small SS
85 * 27 - per-cpu [ offset to per-cpu data area ]
Ingo Molnar72d64cc2015-03-24 20:45:42 +010086 * 28 - stack_canary-20 [ for stack protector ] <=== cacheline #8
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010087 * 29 - unused
88 * 30 - unused
89 * 31 - TSS for double fault handler
90 */
Ingo Molnar72d64cc2015-03-24 20:45:42 +010091#define GDT_ENTRY_TLS_MIN 6
92#define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010093
Denys Vlasenko84f53782015-03-22 22:01:12 +010094#define GDT_ENTRY_KERNEL_CS 12
95#define GDT_ENTRY_KERNEL_DS 13
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010096#define GDT_ENTRY_DEFAULT_USER_CS 14
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010097#define GDT_ENTRY_DEFAULT_USER_DS 15
Denys Vlasenko84f53782015-03-22 22:01:12 +010098#define GDT_ENTRY_TSS 16
99#define GDT_ENTRY_LDT 17
100#define GDT_ENTRY_PNPBIOS_CS32 18
101#define GDT_ENTRY_PNPBIOS_CS16 19
102#define GDT_ENTRY_PNPBIOS_DS 20
103#define GDT_ENTRY_PNPBIOS_TS1 21
104#define GDT_ENTRY_PNPBIOS_TS2 22
105#define GDT_ENTRY_APMBIOS_BASE 23
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100106
Denys Vlasenko84f53782015-03-22 22:01:12 +0100107#define GDT_ENTRY_ESPFIX_SS 26
108#define GDT_ENTRY_PERCPU 27
109#define GDT_ENTRY_STACK_CANARY 28
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100110
Denys Vlasenko84f53782015-03-22 22:01:12 +0100111#define GDT_ENTRY_DOUBLEFAULT_TSS 31
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100112
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100113/*
114 * Number of entries in the GDT table:
115 */
116#define GDT_ENTRIES 32
117
118/*
119 * Segment selector values corresponding to the above entries:
120 */
121
Denys Vlasenko84f53782015-03-22 22:01:12 +0100122#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
123#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100124#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
125#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
Jan Beulich32342822010-10-19 14:52:26 +0100126#define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8)
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100127
128/* segment for calling fn: */
129#define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32*8)
130/* code segment for BIOS: */
131#define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16*8)
132
Denys Vlasenko84f53782015-03-22 22:01:12 +0100133/* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100134#define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == PNP_CS32)
135
136/* data segment for BIOS: */
137#define PNP_DS (GDT_ENTRY_PNPBIOS_DS*8)
138/* transfer data segment: */
139#define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1*8)
140/* another data segment: */
141#define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2*8)
142
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100143#ifdef CONFIG_SMP
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100144# define __KERNEL_PERCPU (GDT_ENTRY_PERCPU*8)
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200145#else
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100146# define __KERNEL_PERCPU 0
Tejun Heo60a53172009-02-09 22:17:40 +0900147#endif
148
Linus Torvalds050e9ba2018-06-14 12:21:18 +0900149#ifdef CONFIG_STACKPROTECTOR
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100150# define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8)
151#else
152# define __KERNEL_STACK_CANARY 0
153#endif
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100154
Denys Vlasenko84f53782015-03-22 22:01:12 +0100155#else /* 64-bit: */
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100156
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100157#include <asm/cache.h>
158
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100159#define GDT_ENTRY_KERNEL32_CS 1
160#define GDT_ENTRY_KERNEL_CS 2
161#define GDT_ENTRY_KERNEL_DS 3
162
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100163/*
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100164 * We cannot use the same code segment descriptor for user and kernel mode,
165 * not even in long flat mode, because of different DPL.
166 *
167 * GDT layout to get 64-bit SYSCALL/SYSRET support right. SYSRET hardcodes
168 * selectors:
169 *
170 * if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
171 * if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
172 *
Denys Vlasenko84f53782015-03-22 22:01:12 +0100173 * ss = STAR.SYSRET_CS+8 (in either case)
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100174 *
Denys Vlasenko84f53782015-03-22 22:01:12 +0100175 * thus USER_DS should be between 32-bit and 64-bit code selectors:
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100176 */
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100177#define GDT_ENTRY_DEFAULT_USER32_CS 4
178#define GDT_ENTRY_DEFAULT_USER_DS 5
179#define GDT_ENTRY_DEFAULT_USER_CS 6
Denys Vlasenko84f53782015-03-22 22:01:12 +0100180
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100181/* Needs two entries */
182#define GDT_ENTRY_TSS 8
183/* Needs two entries */
184#define GDT_ENTRY_LDT 10
Denys Vlasenko84f53782015-03-22 22:01:12 +0100185
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100186#define GDT_ENTRY_TLS_MIN 12
187#define GDT_ENTRY_TLS_MAX 14
Denys Vlasenko84f53782015-03-22 22:01:12 +0100188
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100189/* Abused to load per CPU data from limit */
190#define GDT_ENTRY_PER_CPU 15
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100191
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100192/*
193 * Number of entries in the GDT table:
194 */
195#define GDT_ENTRIES 16
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100196
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100197/*
198 * Segment selector values corresponding to the above entries:
199 *
200 * Note, selectors also need to have a correct RPL,
201 * expressed with the +3 value for user-space selectors:
202 */
203#define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS*8)
204#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
205#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
206#define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8 + 3)
207#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
208#define __USER32_DS __USER_DS
209#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
210#define __PER_CPU_SEG (GDT_ENTRY_PER_CPU*8 + 3)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100211
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200212#endif
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +0100213
214#ifndef CONFIG_PARAVIRT
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100215# define get_kernel_rpl() 0
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +0100216#endif
217
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100218#define IDT_ENTRIES 256
219#define NUM_EXCEPTION_VECTORS 32
220
221/* Bitmask of exception vectors which push an error code on the stack: */
222#define EXCEPTION_ERRCODE_MASK 0x00027d00
223
224#define GDT_SIZE (GDT_ENTRIES*8)
225#define GDT_ENTRY_TLS_ENTRIES 3
226#define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES* 8)
Glauber de Oliveira Costa15ee09b2008-01-30 13:31:09 +0100227
Roland McGrath8866cd92008-01-30 13:33:06 +0100228#ifdef __KERNEL__
Andy Lutomirski425be562015-05-22 16:15:47 -0700229
230/*
231 * early_idt_handler_array is an array of entry points referenced in the
232 * early IDT. For simplicity, it's a real array with one entry point
233 * every nine bytes. That leaves room for an optional 'push $0' if the
234 * vector has no error code (two bytes), a 'push $vector_number' (two
235 * bytes), and a jump to the common entry code (up to five bytes).
236 */
237#define EARLY_IDT_HANDLER_SIZE 9
238
Juergen Gross42b3a4c2017-11-24 09:42:21 +0100239/*
240 * xen_early_idt_handler_array is for Xen pv guests: for each entry in
241 * early_idt_handler_array it contains a prequel in the form of
242 * pop %rcx; pop %r11; jmp early_idt_handler_array[i]; summing up to
243 * max 8 bytes.
244 */
245#define XEN_EARLY_IDT_HANDLER_SIZE 8
246
Roland McGrath8866cd92008-01-30 13:33:06 +0100247#ifndef __ASSEMBLY__
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100248
Andy Lutomirski425be562015-05-22 16:15:47 -0700249extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
Thomas Gleixner87e81782017-08-28 08:47:48 +0200250extern void early_ignore_irq(void);
David Howellsf05e7982012-03-28 18:11:12 +0100251
Juergen Gross42b3a4c2017-11-24 09:42:21 +0100252#if defined(CONFIG_X86_64) && defined(CONFIG_XEN_PV)
253extern const char xen_early_idt_handler_array[NUM_EXCEPTION_VECTORS][XEN_EARLY_IDT_HANDLER_SIZE];
254#endif
255
David Howellsf05e7982012-03-28 18:11:12 +0100256/*
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700257 * Load a segment. Fall back on loading the zero segment if something goes
258 * wrong. This variant assumes that loading zero fully clears the segment.
259 * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any
260 * failure to fully clear the cached descriptor is only observable for
261 * FS and GS.
David Howellsf05e7982012-03-28 18:11:12 +0100262 */
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700263#define __loadsegment_simple(seg, value) \
David Howellsf05e7982012-03-28 18:11:12 +0100264do { \
265 unsigned short __val = (value); \
266 \
267 asm volatile(" \n" \
268 "1: movl %k0,%%" #seg " \n" \
269 \
270 ".section .fixup,\"ax\" \n" \
271 "2: xorl %k0,%k0 \n" \
272 " jmp 1b \n" \
273 ".previous \n" \
274 \
275 _ASM_EXTABLE(1b, 2b) \
276 \
277 : "+r" (__val) : : "memory"); \
278} while (0)
279
Andy Lutomirski45e876f2016-04-26 12:23:26 -0700280#define __loadsegment_ss(value) __loadsegment_simple(ss, (value))
281#define __loadsegment_ds(value) __loadsegment_simple(ds, (value))
282#define __loadsegment_es(value) __loadsegment_simple(es, (value))
283
284#ifdef CONFIG_X86_32
285
286/*
287 * On 32-bit systems, the hidden parts of FS and GS are unobservable if
288 * the selector is NULL, so there's no funny business here.
289 */
290#define __loadsegment_fs(value) __loadsegment_simple(fs, (value))
291#define __loadsegment_gs(value) __loadsegment_simple(gs, (value))
292
293#else
294
295static inline void __loadsegment_fs(unsigned short value)
296{
297 asm volatile(" \n"
298 "1: movw %0, %%fs \n"
299 "2: \n"
300
301 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs)
302
303 : : "rm" (value) : "memory");
304}
305
306/* __loadsegment_gs is intentionally undefined. Use load_gs_index instead. */
307
308#endif
309
310#define loadsegment(seg, value) __loadsegment_ ## seg (value)
311
David Howellsf05e7982012-03-28 18:11:12 +0100312/*
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100313 * Save a segment register away:
David Howellsf05e7982012-03-28 18:11:12 +0100314 */
315#define savesegment(seg, value) \
316 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
317
318/*
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100319 * x86-32 user GS accessors:
David Howellsf05e7982012-03-28 18:11:12 +0100320 */
321#ifdef CONFIG_X86_32
Ingo Molnar72d64cc2015-03-24 20:45:42 +0100322# ifdef CONFIG_X86_32_LAZY_GS
323# define get_user_gs(regs) (u16)({ unsigned long v; savesegment(gs, v); v; })
324# define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
325# define task_user_gs(tsk) ((tsk)->thread.gs)
326# define lazy_save_gs(v) savesegment(gs, (v))
327# define lazy_load_gs(v) loadsegment(gs, (v))
328# else /* X86_32_LAZY_GS */
329# define get_user_gs(regs) (u16)((regs)->gs)
330# define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
331# define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
332# define lazy_save_gs(v) do { } while (0)
333# define lazy_load_gs(v) do { } while (0)
334# endif /* X86_32_LAZY_GS */
David Howellsf05e7982012-03-28 18:11:12 +0100335#endif /* X86_32 */
336
David Howellsf05e7982012-03-28 18:11:12 +0100337#endif /* !__ASSEMBLY__ */
338#endif /* __KERNEL__ */
Roland McGrath8866cd92008-01-30 13:33:06 +0100339
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700340#endif /* _ASM_X86_SEGMENT_H */