blob: db257a58571f0b47429e2dcc70cf628f1a968fe4 [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>
5
H. Peter Anvin4fdf08b2008-07-17 11:29:24 -07006/* 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) \
H. Peter Anvin014eea52011-02-14 18:33:55 -08009 ((((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))))
H. Peter Anvin4fdf08b2008-07-17 11:29:24 -070014
H. Peter Anvin2a6648e2008-01-30 13:33:01 +010015/* Simple and small GDT entries for booting only */
16
17#define GDT_ENTRY_BOOT_CS 2
18#define __BOOT_CS (GDT_ENTRY_BOOT_CS * 8)
19
20#define GDT_ENTRY_BOOT_DS (GDT_ENTRY_BOOT_CS + 1)
21#define __BOOT_DS (GDT_ENTRY_BOOT_DS * 8)
22
H. Peter Anvin88089512008-01-30 13:33:02 +010023#define GDT_ENTRY_BOOT_TSS (GDT_ENTRY_BOOT_CS + 2)
24#define __BOOT_TSS (GDT_ENTRY_BOOT_TSS * 8)
25
Borislav Petkovbe9d1732014-12-09 13:25:59 +010026#define SEGMENT_RPL_MASK 0x3 /*
27 * Bottom two bits of selector give the ring
28 * privilege level
29 */
30#define SEGMENT_TI_MASK 0x4 /* Bit 2 is table indicator (LDT/GDT) */
31#define USER_RPL 0x3 /* User mode is privilege level 3 */
32#define SEGMENT_LDT 0x4 /* LDT segment has TI set... */
33#define SEGMENT_GDT 0x0 /* ... GDT has it cleared */
34
Thomas Gleixner96a388d2007-10-11 11:20:03 +020035#ifdef CONFIG_X86_32
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010036/*
37 * The layout of the per-CPU GDT under Linux:
38 *
39 * 0 - null
40 * 1 - reserved
41 * 2 - reserved
42 * 3 - reserved
43 *
44 * 4 - unused <==== new cacheline
45 * 5 - unused
46 *
47 * ------- start of TLS (Thread-Local Storage) segments:
48 *
49 * 6 - TLS segment #1 [ glibc's TLS segment ]
50 * 7 - TLS segment #2 [ Wine's %fs Win32 segment ]
51 * 8 - TLS segment #3
52 * 9 - reserved
53 * 10 - reserved
54 * 11 - reserved
55 *
56 * ------- start of kernel segments:
57 *
58 * 12 - kernel code segment <==== new cacheline
59 * 13 - kernel data segment
60 * 14 - default user CS
61 * 15 - default user DS
62 * 16 - TSS
63 * 17 - LDT
64 * 18 - PNPBIOS support (16->32 gate)
65 * 19 - PNPBIOS support
66 * 20 - PNPBIOS support
67 * 21 - PNPBIOS support
68 * 22 - PNPBIOS support
69 * 23 - APM BIOS support
70 * 24 - APM BIOS support
71 * 25 - APM BIOS support
72 *
73 * 26 - ESPFIX small SS
74 * 27 - per-cpu [ offset to per-cpu data area ]
Tejun Heo60a53172009-02-09 22:17:40 +090075 * 28 - stack_canary-20 [ for stack protector ]
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010076 * 29 - unused
77 * 30 - unused
78 * 31 - TSS for double fault handler
79 */
80#define GDT_ENTRY_TLS_MIN 6
81#define GDT_ENTRY_TLS_MAX (GDT_ENTRY_TLS_MIN + GDT_ENTRY_TLS_ENTRIES - 1)
82
83#define GDT_ENTRY_DEFAULT_USER_CS 14
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010084
85#define GDT_ENTRY_DEFAULT_USER_DS 15
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010086
Jan Beulich32342822010-10-19 14:52:26 +010087#define GDT_ENTRY_KERNEL_BASE (12)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010088
Jan Beulich32342822010-10-19 14:52:26 +010089#define GDT_ENTRY_KERNEL_CS (GDT_ENTRY_KERNEL_BASE+0)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010090
Jan Beulich32342822010-10-19 14:52:26 +010091#define GDT_ENTRY_KERNEL_DS (GDT_ENTRY_KERNEL_BASE+1)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010092
Jan Beulich32342822010-10-19 14:52:26 +010093#define GDT_ENTRY_TSS (GDT_ENTRY_KERNEL_BASE+4)
94#define GDT_ENTRY_LDT (GDT_ENTRY_KERNEL_BASE+5)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010095
Jan Beulich32342822010-10-19 14:52:26 +010096#define GDT_ENTRY_PNPBIOS_BASE (GDT_ENTRY_KERNEL_BASE+6)
97#define GDT_ENTRY_APMBIOS_BASE (GDT_ENTRY_KERNEL_BASE+11)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +010098
Jan Beulich32342822010-10-19 14:52:26 +010099#define GDT_ENTRY_ESPFIX_SS (GDT_ENTRY_KERNEL_BASE+14)
100#define __ESPFIX_SS (GDT_ENTRY_ESPFIX_SS*8)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100101
Jan Beulich32342822010-10-19 14:52:26 +0100102#define GDT_ENTRY_PERCPU (GDT_ENTRY_KERNEL_BASE+15)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100103#ifdef CONFIG_SMP
104#define __KERNEL_PERCPU (GDT_ENTRY_PERCPU * 8)
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200105#else
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100106#define __KERNEL_PERCPU 0
107#endif
108
Jan Beulich32342822010-10-19 14:52:26 +0100109#define GDT_ENTRY_STACK_CANARY (GDT_ENTRY_KERNEL_BASE+16)
Tejun Heo60a53172009-02-09 22:17:40 +0900110#ifdef CONFIG_CC_STACKPROTECTOR
Jan Beulich32342822010-10-19 14:52:26 +0100111#define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8)
Tejun Heo60a53172009-02-09 22:17:40 +0900112#else
113#define __KERNEL_STACK_CANARY 0
114#endif
115
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100116#define GDT_ENTRY_DOUBLEFAULT_TSS 31
117
118/*
119 * The GDT has 32 entries
120 */
121#define GDT_ENTRIES 32
122
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100123/* The PnP BIOS entries in the GDT */
124#define GDT_ENTRY_PNPBIOS_CS32 (GDT_ENTRY_PNPBIOS_BASE + 0)
125#define GDT_ENTRY_PNPBIOS_CS16 (GDT_ENTRY_PNPBIOS_BASE + 1)
126#define GDT_ENTRY_PNPBIOS_DS (GDT_ENTRY_PNPBIOS_BASE + 2)
127#define GDT_ENTRY_PNPBIOS_TS1 (GDT_ENTRY_PNPBIOS_BASE + 3)
128#define GDT_ENTRY_PNPBIOS_TS2 (GDT_ENTRY_PNPBIOS_BASE + 4)
129
130/* The PnP BIOS selectors */
131#define PNP_CS32 (GDT_ENTRY_PNPBIOS_CS32 * 8) /* segment for calling fn */
132#define PNP_CS16 (GDT_ENTRY_PNPBIOS_CS16 * 8) /* code segment for BIOS */
133#define PNP_DS (GDT_ENTRY_PNPBIOS_DS * 8) /* data segment for BIOS */
134#define PNP_TS1 (GDT_ENTRY_PNPBIOS_TS1 * 8) /* transfer data segment */
135#define PNP_TS2 (GDT_ENTRY_PNPBIOS_TS2 * 8) /* another data segment */
136
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100137
138/*
139 * Matching rules for certain types of segments.
140 */
141
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100142/* Matches PNP_CS32 and PNP_CS16 (they must be consecutive) */
143#define SEGMENT_IS_PNP_CODE(x) (((x) & 0xf4) == GDT_ENTRY_PNPBIOS_BASE * 8)
144
145
146#else
147#include <asm/cache.h>
148
Glauber Costaa9390982008-05-28 16:19:53 -0700149#define GDT_ENTRY_KERNEL32_CS 1
150#define GDT_ENTRY_KERNEL_CS 2
151#define GDT_ENTRY_KERNEL_DS 3
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100152
Glauber Costaa9390982008-05-28 16:19:53 -0700153#define __KERNEL32_CS (GDT_ENTRY_KERNEL32_CS * 8)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100154
155/*
156 * we cannot use the same code segment descriptor for user and kernel
157 * -- not even in the long flat mode, because of different DPL /kkeil
158 * The segment offset needs to contain a RPL. Grr. -AK
159 * GDT layout to get 64bit syscall right (sysret hardcodes gdt offsets)
160 */
Glauber Costaa9390982008-05-28 16:19:53 -0700161#define GDT_ENTRY_DEFAULT_USER32_CS 4
162#define GDT_ENTRY_DEFAULT_USER_DS 5
163#define GDT_ENTRY_DEFAULT_USER_CS 6
Borislav Petkovdd2897b2011-06-03 22:07:22 +0200164#define __USER32_CS (GDT_ENTRY_DEFAULT_USER32_CS*8+3)
Glauber de Oliveira Costab23be392008-01-30 13:31:09 +0100165#define __USER32_DS __USER_DS
166
167#define GDT_ENTRY_TSS 8 /* needs two entries */
168#define GDT_ENTRY_LDT 10 /* needs two entries */
169#define GDT_ENTRY_TLS_MIN 12
170#define GDT_ENTRY_TLS_MAX 14
171
172#define GDT_ENTRY_PER_CPU 15 /* Abused to load per CPU data from limit */
173#define __PER_CPU_SEG (GDT_ENTRY_PER_CPU * 8 + 3)
174
175/* TLS indexes for 64bit - hardcoded in arch_prctl */
176#define FS_TLS 0
177#define GS_TLS 1
178
179#define GS_TLS_SEL ((GDT_ENTRY_TLS_MIN+GS_TLS)*8 + 3)
180#define FS_TLS_SEL ((GDT_ENTRY_TLS_MIN+FS_TLS)*8 + 3)
181
182#define GDT_ENTRIES 16
183
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200184#endif
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +0100185
Jan Beulich32342822010-10-19 14:52:26 +0100186#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
187#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
188#define __USER_DS (GDT_ENTRY_DEFAULT_USER_DS*8+3)
189#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS*8+3)
Glauber de Oliveira Costacef18952008-01-30 13:31:09 +0100190#ifndef CONFIG_PARAVIRT
191#define get_kernel_rpl() 0
192#endif
193
Glauber de Oliveira Costa15ee09b2008-01-30 13:31:09 +0100194#define IDT_ENTRIES 256
Andi Kleen5524ea32008-03-11 02:23:20 +0100195#define NUM_EXCEPTION_VECTORS 32
H. Peter Anvin84f4fc52012-04-18 17:16:47 -0700196/* Bitmask of exception vectors which push an error code on the stack */
197#define EXCEPTION_ERRCODE_MASK 0x00027d00
Glauber de Oliveira Costa15ee09b2008-01-30 13:31:09 +0100198#define GDT_SIZE (GDT_ENTRIES * 8)
199#define GDT_ENTRY_TLS_ENTRIES 3
200#define TLS_SIZE (GDT_ENTRY_TLS_ENTRIES * 8)
201
Roland McGrath8866cd92008-01-30 13:33:06 +0100202#ifdef __KERNEL__
203#ifndef __ASSEMBLY__
H. Peter Anvin9900aa22012-04-18 17:16:49 -0700204extern const char early_idt_handlers[NUM_EXCEPTION_VECTORS][2+2+5];
Seiji Aguchi25c74b12013-10-30 16:37:00 -0400205#ifdef CONFIG_TRACING
206#define trace_early_idt_handlers early_idt_handlers
207#endif
David Howellsf05e7982012-03-28 18:11:12 +0100208
209/*
210 * Load a segment. Fall back on loading the zero
211 * segment if something goes wrong..
212 */
213#define loadsegment(seg, value) \
214do { \
215 unsigned short __val = (value); \
216 \
217 asm volatile(" \n" \
218 "1: movl %k0,%%" #seg " \n" \
219 \
220 ".section .fixup,\"ax\" \n" \
221 "2: xorl %k0,%k0 \n" \
222 " jmp 1b \n" \
223 ".previous \n" \
224 \
225 _ASM_EXTABLE(1b, 2b) \
226 \
227 : "+r" (__val) : : "memory"); \
228} while (0)
229
230/*
231 * Save a segment register away
232 */
233#define savesegment(seg, value) \
234 asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
235
236/*
237 * x86_32 user gs accessors.
238 */
239#ifdef CONFIG_X86_32
240#ifdef CONFIG_X86_32_LAZY_GS
241#define get_user_gs(regs) (u16)({unsigned long v; savesegment(gs, v); v;})
242#define set_user_gs(regs, v) loadsegment(gs, (unsigned long)(v))
243#define task_user_gs(tsk) ((tsk)->thread.gs)
244#define lazy_save_gs(v) savesegment(gs, (v))
245#define lazy_load_gs(v) loadsegment(gs, (v))
246#else /* X86_32_LAZY_GS */
247#define get_user_gs(regs) (u16)((regs)->gs)
248#define set_user_gs(regs, v) do { (regs)->gs = (v); } while (0)
249#define task_user_gs(tsk) (task_pt_regs(tsk)->gs)
250#define lazy_save_gs(v) do { } while (0)
251#define lazy_load_gs(v) do { } while (0)
252#endif /* X86_32_LAZY_GS */
253#endif /* X86_32 */
254
255static inline unsigned long get_limit(unsigned long segment)
256{
257 unsigned long __limit;
258 asm("lsll %1,%0" : "=r" (__limit) : "r" (segment));
259 return __limit + 1;
260}
261
262#endif /* !__ASSEMBLY__ */
263#endif /* __KERNEL__ */
Roland McGrath8866cd92008-01-30 13:33:06 +0100264
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700265#endif /* _ASM_X86_SEGMENT_H */