]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/include/asm/segment.h
x86/asm: Deobfuscate segment.h
[mirror_ubuntu-artful-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
PA
4#include <linux/const.h>
5
4fdf08b5
PA
6/* Constructor for a conventional segment GDT (or LDT) entry */
7/* This is a macro so it can be used in initializers */
8#define GDT_ENTRY(flags, base, limit) \
014eea51
PA
9 ((((base) & _AC(0xff000000,ULL)) << (56-24)) | \
10 (((flags) & _AC(0x0000f0ff,ULL)) << 40) | \
11 (((limit) & _AC(0x000f0000,ULL)) << (48-16)) | \
12 (((base) & _AC(0x00ffffff,ULL)) << 16) | \
13 (((limit) & _AC(0x0000ffff,ULL))))
4fdf08b5 14
2a6648e6
PA
15/* Simple and small GDT entries for booting only */
16
17#define GDT_ENTRY_BOOT_CS 2
84f53788
DV
18#define GDT_ENTRY_BOOT_DS 3
19#define GDT_ENTRY_BOOT_TSS 4
2a6648e6 20#define __BOOT_CS (GDT_ENTRY_BOOT_CS * 8)
2a6648e6 21#define __BOOT_DS (GDT_ENTRY_BOOT_DS * 8)
88089519
PA
22#define __BOOT_TSS (GDT_ENTRY_BOOT_TSS * 8)
23
be9d1738
BP
24#define SEGMENT_RPL_MASK 0x3 /*
25 * Bottom two bits of selector give the ring
26 * privilege level
27 */
28#define SEGMENT_TI_MASK 0x4 /* Bit 2 is table indicator (LDT/GDT) */
29#define USER_RPL 0x3 /* User mode is privilege level 3 */
30#define SEGMENT_LDT 0x4 /* LDT segment has TI set... */
31#define SEGMENT_GDT 0x0 /* ... GDT has it cleared */
32
96a388de 33#ifdef CONFIG_X86_32
b23be399
GOC
34/*
35 * The layout of the per-CPU GDT under Linux:
36 *
37 * 0 - null
38 * 1 - reserved
39 * 2 - reserved
40 * 3 - reserved
41 *
42 * 4 - unused <==== new cacheline
43 * 5 - unused
44 *
45 * ------- start of TLS (Thread-Local Storage) segments:
46 *
47 * 6 - TLS segment #1 [ glibc's TLS segment ]
48 * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
49 * 8 - TLS segment #3
50 * 9 - reserved
51 * 10 - reserved
52 * 11 - reserved
53 *
54 * ------- start of kernel segments:
55 *
56 * 12 - kernel code segment <==== new cacheline
57 * 13 - kernel data segment
58 * 14 - default user CS
59 * 15 - default user DS
60 * 16 - TSS
61 * 17 - LDT
62 * 18 - PNPBIOS support (16->32 gate)
63 * 19 - PNPBIOS support
64 * 20 - PNPBIOS support
65 * 21 - PNPBIOS support
66 * 22 - PNPBIOS support
67 * 23 - APM BIOS support
68 * 24 - APM BIOS support
69 * 25 - APM BIOS support
70 *
71 * 26 - ESPFIX small SS
72 * 27 - per-cpu [ offset to per-cpu data area ]
60a5317f 73 * 28 - stack_canary-20 [ for stack protector ]
b23be399
GOC
74 * 29 - unused
75 * 30 - unused
76 * 31 - TSS for double fault handler
77 */
78#define GDT_ENTRY_TLS_MIN 6
79#define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
80
84f53788
DV
81#define GDT_ENTRY_KERNEL_CS 12
82#define GDT_ENTRY_KERNEL_DS 13
b23be399 83#define GDT_ENTRY_DEFAULT_USER_CS 14
b23be399 84#define GDT_ENTRY_DEFAULT_USER_DS 15
84f53788
DV
85#define GDT_ENTRY_TSS 16
86#define GDT_ENTRY_LDT 17
87#define GDT_ENTRY_PNPBIOS_CS32 18
88#define GDT_ENTRY_PNPBIOS_CS16 19
89#define GDT_ENTRY_PNPBIOS_DS 20
90#define GDT_ENTRY_PNPBIOS_TS1 21
91#define GDT_ENTRY_PNPBIOS_TS2 22
92#define GDT_ENTRY_APMBIOS_BASE 23
93
94#define GDT_ENTRY_ESPFIX_SS 26
95#define GDT_ENTRY_PERCPU 27
96#define GDT_ENTRY_STACK_CANARY 28
b23be399 97
84f53788 98#define GDT_ENTRY_DOUBLEFAULT_TSS 31
b23be399 99
84f53788
DV
100#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
101#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
102#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8+3)
103#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8+3)
3234282f 104#define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8)
84f53788
DV
105#define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */
106#define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */
107/* "Is this PNP code selector (PNP_CS32 or PNP_CS16)?" */
108#define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == PNP_CS32)
109#define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */
110#define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */
111#define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */
b23be399 112#ifdef CONFIG_SMP
84f53788 113#define __KERNEL_PERCPU (GDT_ENTRY_PERCPU*8)
b23be399 114#else
84f53788 115#define __KERNEL_PERCPU 0
b23be399 116#endif
60a5317f 117#ifdef CONFIG_CC_STACKPROTECTOR
3234282f 118#define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8)
60a5317f
TH
119#else
120#define __KERNEL_STACK_CANARY 0
121#endif
122
b23be399
GOC
123#define GDT_ENTRIES 32
124
84f53788 125#else /* 64-bit: */
b23be399 126
b23be399
GOC
127#include <asm/cache.h>
128
84f53788
DV
129#define GDT_ENTRY_KERNEL32_CS 1
130#define GDT_ENTRY_KERNEL_CS 2
131#define GDT_ENTRY_KERNEL_DS 3
b23be399
GOC
132/*
133 * we cannot use the same code segment descriptor for user and kernel
134 * -- not even in the long flat mode, because of different DPL /kkeil
84f53788
DV
135 * GDT layout to get 64bit syscall/sysret right. sysret hardcodes selectors:
136 * if returning to 32-bit userspace: cs = STAR.SYSRET_CS,
137 * if returning to 64-bit userspace: cs = STAR.SYSRET_CS+16,
138 * ss = STAR.SYSRET_CS+8 (in either case)
139 * thus USER_DS should be between 32-bit and 64-bit code selectors:
b23be399 140 */
a939098a
GC
141#define GDT_ENTRY_DEFAULT_USER32_CS 4
142#define GDT_ENTRY_DEFAULT_USER_DS 5
143#define GDT_ENTRY_DEFAULT_USER_CS 6
b23be399 144
84f53788
DV
145#define GDT_ENTRY_TSS 8 /* needs two entries */
146#define GDT_ENTRY_LDT 10 /* needs two entries */
147#define GDT_ENTRY_TLS_MIN 12
148#define GDT_ENTRY_TLS_MAX 14
b23be399 149
84f53788
DV
150#define GDT_ENTRY_PER_CPU 15 /* abused to load per CPU data from limit */
151
152/* Selectors need to also have a correct RPL (+3 thingy) */
153#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
154#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
155#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8+3)
156#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8+3)
157#define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS*8)
158#define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8+3)
159#define __USER32_DS __USER_DS
160#define __PER_CPU_SEG (GDT_ENTRY_PER_CPU*8+3)
b23be399
GOC
161
162/* TLS indexes for 64bit - hardcoded in arch_prctl */
163#define FS_TLS 0
164#define GS_TLS 1
165
166#define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
167#define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
168
169#define GDT_ENTRIES 16
170
96a388de 171#endif
cef18950
GOC
172
173#ifndef CONFIG_PARAVIRT
174#define get_kernel_rpl() 0
175#endif
176
15ee09b4 177#define IDT_ENTRIES 256
5524ea32 178#define NUM_EXCEPTION_VECTORS 32
84f4fc52
PA
179/* Bitmask of exception vectors which push an error code on the stack */
180#define EXCEPTION_ERRCODE_MASK 0x00027d00
15ee09b4
GOC
181#define GDT_SIZE (GDT_ENTRIES * 8)
182#define GDT_ENTRY_TLS_ENTRIES 3
183#define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)
184
8866cd9d
RM
185#ifdef __KERNEL__
186#ifndef __ASSEMBLY__
9900aa2f 187extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][2+2+5];
25c74b10
SA
188#ifdef CONFIG_TRACING
189#define trace_early_idt_handlers early_idt_handlers
190#endif
f05e798a
DH
191
192/*
193 * Load a segment. Fall back on loading the zero
194 * segment if something goes wrong..
195 */
196#define loadsegment(seg, value) \
197do { \
198 unsigned short __val = (value); \
199 \
200 asm volatile(" \n" \
201 "1: movl %k0,%%" #seg " \n" \
202 \
203 ".section .fixup,\"ax\" \n" \
204 "2: xorl %k0,%k0 \n" \
205 " jmp 1b \n" \
206 ".previous \n" \
207 \
208 _ASM_EXTABLE(1b, 2b) \
209 \
210 : "+r" (__val) : : "memory"); \
211} while (0)
212
213/*
214 * Save a segment register away
215 */
216#define savesegment(seg, value) \
217 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
218
219/*
220 * x86_32 user gs accessors.
221 */
222#ifdef CONFIG_X86_32
223#ifdef CONFIG_X86_32_LAZY_GS
224#define get_user_gs(regs) (u16)({unsigned long v; savesegment(gs, v); v;})
225#define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
226#define task_user_gs(tsk) ((tsk)->thread.gs)
227#define lazy_save_gs(v) savesegment(gs, (v))
228#define lazy_load_gs(v) loadsegment(gs, (v))
229#else /* X86_32_LAZY_GS */
230#define get_user_gs(regs) (u16)((regs)->gs)
231#define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
232#define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
233#define lazy_save_gs(v) do { } while (0)
234#define lazy_load_gs(v) do { } while (0)
235#endif /* X86_32_LAZY_GS */
236#endif /* X86_32 */
237
238static inline unsigned long get_limit(unsigned long segment)
239{
240 unsigned long __limit;
241 asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
242 return __limit + 1;
243}
244
245#endif /* !__ASSEMBLY__ */
246#endif /* __KERNEL__ */
8866cd9d 247
1965aae3 248#endif /* _ASM_X86_SEGMENT_H */