]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/include/asm/segment.h
x86/mm: Simplify p[g4um]d_page() macros
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / include / asm / segment.h
CommitLineData
1965aae3
PA
1#ifndef _ASM_X86_SEGMENT_H
2#define _ASM_X86_SEGMENT_H
cef18950 3
014eea51 4#include <linux/const.h>
45e876f7 5#include <asm/alternative.h>
014eea51 6
72d64cc7
IM
7/*
8 * Constructor for a conventional segment GDT (or LDT) entry.
9 * This is a macro so it can be used in initializers.
10 */
4fdf08b5 11#define GDT_ENTRY(flags, base, limit) \
014eea51
PA
12 ((((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))))
4fdf08b5 17
72d64cc7 18/* Simple and small GDT entries for booting only: */
2a6648e6
PA
19
20#define GDT_ENTRY_BOOT_CS 2
84f53788
DV
21#define GDT_ENTRY_BOOT_DS 3
22#define GDT_ENTRY_BOOT_TSS 4
72d64cc7
IM
23#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)
26
27/*
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
be9d1738 42
6b51311c
BP
43#define GDT_ENTRY_INVALID_SEG 0
44
96a388de 45#ifdef CONFIG_X86_32
b23be399
GOC
46/*
47 * The layout of the per-CPU GDT under Linux:
48 *
72d64cc7 49 * 0 - null <=== cacheline #1
b23be399
GOC
50 * 1 - reserved
51 * 2 - reserved
52 * 3 - reserved
53 *
72d64cc7 54 * 4 - unused <=== cacheline #2
b23be399
GOC
55 * 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 ]
72d64cc7 61 * 8 - TLS segment #3 <=== cacheline #3
b23be399
GOC
62 * 9 - reserved
63 * 10 - reserved
64 * 11 - reserved
65 *
66 * ------- start of kernel segments:
67 *
72d64cc7 68 * 12 - kernel code segment <=== cacheline #4
b23be399
GOC
69 * 13 - kernel data segment
70 * 14 - default user CS
71 * 15 - default user DS
72d64cc7 72 * 16 - TSS <=== cacheline #5
b23be399
GOC
73 * 17 - LDT
74 * 18 - PNPBIOS support (16->32 gate)
75 * 19 - PNPBIOS support
72d64cc7 76 * 20 - PNPBIOS support <=== cacheline #6
b23be399
GOC
77 * 21 - PNPBIOS support
78 * 22 - PNPBIOS support
79 * 23 - APM BIOS support
72d64cc7 80 * 24 - APM BIOS support <=== cacheline #7
b23be399
GOC
81 * 25 - APM BIOS support
82 *
83 * 26 - ESPFIX small SS
84 * 27 - per-cpu [ offset to per-cpu data area ]
72d64cc7 85 * 28 - stack_canary-20 [ for stack protector ] <=== cacheline #8
b23be399
GOC
86 * 29 - unused
87 * 30 - unused
88 * 31 - TSS for double fault handler
89 */
72d64cc7
IM
90#define GDT_ENTRY_TLS_MIN 6
91#define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
b23be399 92
84f53788
DV
93#define GDT_ENTRY_KERNEL_CS 12
94#define GDT_ENTRY_KERNEL_DS 13
b23be399 95#define GDT_ENTRY_DEFAULT_USER_CS 14
b23be399 96#define GDT_ENTRY_DEFAULT_USER_DS 15
84f53788
DV
97#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
105
106#define GDT_ENTRY_ESPFIX_SS 26
107#define GDT_ENTRY_PERCPU 27
108#define GDT_ENTRY_STACK_CANARY 28
b23be399 109
84f53788 110#define GDT_ENTRY_DOUBLEFAULT_TSS 31
b23be399 111
72d64cc7
IM
112/*
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
84f53788
DV
121#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
122#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
72d64cc7
IM
123#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8 + 3)
124#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8 + 3)
3234282f 125#define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8)
72d64cc7
IM
126
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
84f53788 132/* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
72d64cc7
IM
133#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
b23be399 142#ifdef CONFIG_SMP
72d64cc7 143# define __KERNEL_PERCPU (GDT_ENTRY_PERCPU*8)
b23be399 144#else
72d64cc7 145# define __KERNEL_PERCPU 0
b23be399 146#endif
72d64cc7 147
60a5317f 148#ifdef CONFIG_CC_STACKPROTECTOR
72d64cc7 149# define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8)
60a5317f 150#else
72d64cc7 151# define __KERNEL_STACK_CANARY 0
60a5317f
TH
152#endif
153
84f53788 154#else /* 64-bit: */
b23be399 155
b23be399
GOC
156#include <asm/cache.h>
157
72d64cc7
IM
158#define GDT_ENTRY_KERNEL32_CS 1
159#define GDT_ENTRY_KERNEL_CS 2
160#define GDT_ENTRY_KERNEL_DS 3
161
b23be399 162/*
72d64cc7
IM
163 * 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 *
84f53788 172 * ss = STAR.SYSRET_CS+8 (in either case)
72d64cc7 173 *
84f53788 174 * thus USER_DS should be between 32-bit and 64-bit code selectors:
b23be399 175 */
72d64cc7
IM
176#define GDT_ENTRY_DEFAULT_USER32_CS 4
177#define GDT_ENTRY_DEFAULT_USER_DS 5
178#define GDT_ENTRY_DEFAULT_USER_CS 6
179
180/* Needs two entries */
181#define GDT_ENTRY_TSS 8
182/* Needs two entries */
183#define GDT_ENTRY_LDT 10
b23be399 184
72d64cc7
IM
185#define GDT_ENTRY_TLS_MIN 12
186#define GDT_ENTRY_TLS_MAX 14
b23be399 187
72d64cc7
IM
188/* Abused to load per CPU data from limit */
189#define GDT_ENTRY_PER_CPU 15
84f53788 190
72d64cc7
IM
191/*
192 * Number of entries in the GDT table:
193 */
194#define GDT_ENTRIES 16
b23be399 195
72d64cc7
IM
196/*
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)
b23be399 210
96a388de 211#endif
cef18950
GOC
212
213#ifndef CONFIG_PARAVIRT
72d64cc7 214# define get_kernel_rpl() 0
cef18950
GOC
215#endif
216
72d64cc7
IM
217#define IDT_ENTRIES 256
218#define NUM_EXCEPTION_VECTORS 32
219
220/* Bitmask of exception vectors which push an error code on the stack: */
221#define EXCEPTION_ERRCODE_MASK 0x00027d00
222
223#define GDT_SIZE (GDT_ENTRIES*8)
224#define GDT_ENTRY_TLS_ENTRIES 3
225#define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES* 8)
15ee09b4 226
8866cd9d 227#ifdef __KERNEL__
425be567
AL
228
229/*
230 * early_idt_handler_array is an array of entry points referenced in the
231 * early IDT. For simplicity, it's a real array with one entry point
232 * every nine bytes. That leaves room for an optional 'push $0' if the
233 * vector has no error code (two bytes), a 'push $vector_number' (two
234 * bytes), and a jump to the common entry code (up to five bytes).
235 */
236#define EARLY_IDT_HANDLER_SIZE 9
237
8866cd9d 238#ifndef __ASSEMBLY__
72d64cc7 239
425be567 240extern const char early_idt_handler_array[NUM_EXCEPTION_VECTORS][EARLY_IDT_HANDLER_SIZE];
25c74b10 241#ifdef CONFIG_TRACING
425be567 242# define trace_early_idt_handler_array early_idt_handler_array
25c74b10 243#endif
f05e798a
DH
244
245/*
45e876f7
AL
246 * Load a segment. Fall back on loading the zero segment if something goes
247 * wrong. This variant assumes that loading zero fully clears the segment.
248 * This is always the case on Intel CPUs and, even on 64-bit AMD CPUs, any
249 * failure to fully clear the cached descriptor is only observable for
250 * FS and GS.
f05e798a 251 */
45e876f7 252#define __loadsegment_simple(seg, value) \
f05e798a
DH
253do { \
254 unsigned short __val = (value); \
255 \
256 asm volatile(" \n" \
257 "1: movl %k0,%%" #seg " \n" \
258 \
259 ".section .fixup,\"ax\" \n" \
260 "2: xorl %k0,%k0 \n" \
261 " jmp 1b \n" \
262 ".previous \n" \
263 \
264 _ASM_EXTABLE(1b, 2b) \
265 \
266 : "+r" (__val) : : "memory"); \
267} while (0)
268
45e876f7
AL
269#define __loadsegment_ss(value) __loadsegment_simple(ss, (value))
270#define __loadsegment_ds(value) __loadsegment_simple(ds, (value))
271#define __loadsegment_es(value) __loadsegment_simple(es, (value))
272
273#ifdef CONFIG_X86_32
274
275/*
276 * On 32-bit systems, the hidden parts of FS and GS are unobservable if
277 * the selector is NULL, so there's no funny business here.
278 */
279#define __loadsegment_fs(value) __loadsegment_simple(fs, (value))
280#define __loadsegment_gs(value) __loadsegment_simple(gs, (value))
281
282#else
283
284static inline void __loadsegment_fs(unsigned short value)
285{
286 asm volatile(" \n"
287 "1: movw %0, %%fs \n"
288 "2: \n"
289
290 _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_clear_fs)
291
292 : : "rm" (value) : "memory");
293}
294
295/* __loadsegment_gs is intentionally undefined. Use load_gs_index instead. */
296
297#endif
298
299#define loadsegment(seg, value) __loadsegment_ ## seg (value)
300
f05e798a 301/*
72d64cc7 302 * Save a segment register away:
f05e798a
DH
303 */
304#define savesegment(seg, value) \
305 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
306
307/*
72d64cc7 308 * x86-32 user GS accessors:
f05e798a
DH
309 */
310#ifdef CONFIG_X86_32
72d64cc7
IM
311# ifdef CONFIG_X86_32_LAZY_GS
312# define get_user_gs(regs) (u16)({ unsigned long v; savesegment(gs, v); v; })
313# define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
314# define task_user_gs(tsk) ((tsk)->thread.gs)
315# define lazy_save_gs(v) savesegment(gs, (v))
316# define lazy_load_gs(v) loadsegment(gs, (v))
317# else /* X86_32_LAZY_GS */
318# define get_user_gs(regs) (u16)((regs)->gs)
319# define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
320# define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
321# define lazy_save_gs(v) do { } while (0)
322# define lazy_load_gs(v) do { } while (0)
323# endif /* X86_32_LAZY_GS */
f05e798a
DH
324#endif /* X86_32 */
325
f05e798a
DH
326#endif /* !__ASSEMBLY__ */
327#endif /* __KERNEL__ */
8866cd9d 328
1965aae3 329#endif /* _ASM_X86_SEGMENT_H */