blob: a092a0cce0b759a20ce906efc03e3cb5e8d1d2af [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_PROCESSOR_H
2#define _ASM_X86_PROCESSOR_H
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +01003
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004#include <asm/processor-flags.h>
5
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +01006/* Forward declaration, a strange C thing */
7struct task_struct;
8struct mm_struct;
9
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010010#include <asm/vm86.h>
11#include <asm/math_emu.h>
12#include <asm/segment.h>
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010013#include <asm/types.h>
14#include <asm/sigcontext.h>
15#include <asm/current.h>
16#include <asm/cpufeature.h>
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010017#include <asm/page.h>
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080018#include <asm/pgtable_types.h>
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +010019#include <asm/percpu.h>
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010020#include <asm/msr.h>
21#include <asm/desc_defs.h>
Andi Kleenbd616432008-01-30 13:32:38 +010022#include <asm/nops.h>
David Howellsf05e7982012-03-28 18:11:12 +010023#include <asm/special_insns.h>
Ingo Molnar4d46a892008-02-21 04:24:40 +010024
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010025#include <linux/personality.h>
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010026#include <linux/cpumask.h>
27#include <linux/cache.h>
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010028#include <linux/threads.h>
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +020029#include <linux/math64.h>
Peter Zijlstrafaa46022010-03-25 14:51:50 +010030#include <linux/err.h>
David Howellsf05e7982012-03-28 18:11:12 +010031#include <linux/irqflags.h>
32
33/*
34 * We handle most unaligned accesses in hardware. On the other hand
35 * unaligned DMA can be quite expensive on some Nehalem processors.
36 *
37 * Based on this we disable the IP header alignment in network drivers.
38 */
39#define NET_IP_ALIGN 0
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010040
K.Prasadb332828c2009-06-01 23:43:10 +053041#define HBP_NUM 4
Glauber de Oliveira Costa0ccb8ac2008-01-30 13:31:27 +010042/*
43 * Default implementation of macro that returns current
44 * instruction pointer ("program counter").
45 */
46static inline void *current_text_addr(void)
47{
48 void *pc;
Ingo Molnar4d46a892008-02-21 04:24:40 +010049
50 asm volatile("mov $1f, %0; 1:":"=r" (pc));
51
Glauber de Oliveira Costa0ccb8ac2008-01-30 13:31:27 +010052 return pc;
53}
54
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010055#ifdef CONFIG_X86_VSMP
Ingo Molnar4d46a892008-02-21 04:24:40 +010056# define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
57# define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010058#else
Ingo Molnar4d46a892008-02-21 04:24:40 +010059# define ARCH_MIN_TASKALIGN 16
60# define ARCH_MIN_MMSTRUCT_ALIGN 0
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010061#endif
62
Alex Shie0ba94f2012-06-28 09:02:16 +080063enum tlb_infos {
64 ENTRIES,
65 NR_INFO
66};
67
68extern u16 __read_mostly tlb_lli_4k[NR_INFO];
69extern u16 __read_mostly tlb_lli_2m[NR_INFO];
70extern u16 __read_mostly tlb_lli_4m[NR_INFO];
71extern u16 __read_mostly tlb_lld_4k[NR_INFO];
72extern u16 __read_mostly tlb_lld_2m[NR_INFO];
73extern u16 __read_mostly tlb_lld_4m[NR_INFO];
Kirill A. Shutemovdd360392013-12-23 14:16:58 +020074extern u16 __read_mostly tlb_lld_1g[NR_INFO];
Alex Shic4211f42012-06-28 09:02:19 +080075
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010076/*
77 * CPU type and hardware bug flags. Kept separately for each CPU.
78 * Members of this structure are referenced in head.S, so think twice
79 * before touching them. [mj]
80 */
81
82struct cpuinfo_x86 {
Ingo Molnar4d46a892008-02-21 04:24:40 +010083 __u8 x86; /* CPU family */
84 __u8 x86_vendor; /* CPU vendor */
85 __u8 x86_model;
86 __u8 x86_mask;
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010087#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +010088 char wp_works_ok; /* It doesn't on 386's */
89
90 /* Problems on some 486Dx4's and old 386's: */
Ingo Molnar4d46a892008-02-21 04:24:40 +010091 char rfu;
Ingo Molnar4d46a892008-02-21 04:24:40 +010092 char pad0;
H. Peter Anvin60e019e2013-04-29 16:04:20 +020093 char pad1;
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010094#else
Ingo Molnar4d46a892008-02-21 04:24:40 +010095 /* Number of 4K pages in DTLB/ITLB combined(in pages): */
H. Peter Anvinb1882e62009-01-23 17:18:52 -080096 int x86_tlbsize;
Jan Beulich13c6c532009-03-12 12:37:34 +000097#endif
Ingo Molnar4d46a892008-02-21 04:24:40 +010098 __u8 x86_virt_bits;
99 __u8 x86_phys_bits;
100 /* CPUID returned core id bits: */
101 __u8 x86_coreid_bits;
102 /* Max extended CPUID function supported: */
103 __u32 extended_cpuid_level;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100104 /* Maximum supported CPUID level, -1=no CPUID: */
105 int cpuid_level;
Borislav Petkov65fc9852013-03-20 15:07:23 +0100106 __u32 x86_capability[NCAPINTS + NBUGINTS];
Ingo Molnar4d46a892008-02-21 04:24:40 +0100107 char x86_vendor_id[16];
108 char x86_model_id[64];
109 /* in KB - valid for CPUS which support this call: */
110 int x86_cache_size;
111 int x86_cache_alignment; /* In bytes */
112 int x86_power;
113 unsigned long loops_per_jiffy;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100114 /* cpuid returned max cores value: */
115 u16 x86_max_cores;
116 u16 apicid;
Yinghai Lu01aaea12008-03-06 13:46:39 -0800117 u16 initial_apicid;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100118 u16 x86_clflush_size;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100119 /* number of cores as seen by the OS: */
120 u16 booted_cores;
121 /* Physical processor id: */
122 u16 phys_proc_id;
123 /* Core id: */
124 u16 cpu_core_id;
Andreas Herrmann6057b4d2010-09-30 14:38:57 +0200125 /* Compute unit id */
126 u8 compute_unit_id;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100127 /* Index into per_cpu list: */
128 u16 cpu_index;
Andi Kleen506ed6b2011-10-12 17:46:33 -0700129 u32 microcode;
Jan Beulich2c773dd2014-11-04 08:26:42 +0000130};
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100131
Ingo Molnar4d46a892008-02-21 04:24:40 +0100132#define X86_VENDOR_INTEL 0
133#define X86_VENDOR_CYRIX 1
134#define X86_VENDOR_AMD 2
135#define X86_VENDOR_UMC 3
Ingo Molnar4d46a892008-02-21 04:24:40 +0100136#define X86_VENDOR_CENTAUR 5
137#define X86_VENDOR_TRANSMETA 7
138#define X86_VENDOR_NSC 8
139#define X86_VENDOR_NUM 9
140
141#define X86_VENDOR_UNKNOWN 0xff
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100142
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100143/*
144 * capabilities of CPUs
145 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100146extern struct cpuinfo_x86 boot_cpu_data;
147extern struct cpuinfo_x86 new_cpu_data;
148
149extern struct tss_struct doublefault_tss;
Yinghai Lu3e0c3732009-05-09 23:47:42 -0700150extern __u32 cpu_caps_cleared[NCAPINTS];
151extern __u32 cpu_caps_set[NCAPINTS];
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100152
153#ifdef CONFIG_SMP
Jan Beulich2c773dd2014-11-04 08:26:42 +0000154DECLARE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100155#define cpu_data(cpu) per_cpu(cpu_info, cpu)
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100156#else
Tejun Heo7b543a52010-12-18 16:30:05 +0100157#define cpu_info boot_cpu_data
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100158#define cpu_data(cpu) boot_cpu_data
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100159#endif
160
Jaswinder Singh1c6c7272008-07-21 22:40:37 +0530161extern const struct seq_operations cpuinfo_op;
162
Ingo Molnar4d46a892008-02-21 04:24:40 +0100163#define cache_line_size() (boot_cpu_data.x86_cache_alignment)
164
165extern void cpu_detect(struct cpuinfo_x86 *c);
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400166extern void fpu_detect(struct cpuinfo_x86 *c);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100167
Yinghai Luf5803662008-06-21 03:24:19 -0700168extern void early_cpu_init(void);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100169extern void identify_boot_cpu(void);
170extern void identify_secondary_cpu(struct cpuinfo_x86 *);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100171extern void print_cpu_info(struct cpuinfo_x86 *);
Yinghai Lu21c3fcf2012-02-12 09:53:57 -0800172void print_cpu_msr(struct cpuinfo_x86 *);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100173extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
174extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
Andreas Herrmann04a15412012-10-19 10:59:33 +0200175extern void init_amd_cacheinfo(struct cpuinfo_x86 *c);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100176
Suresh Siddhabbb65d22008-08-23 17:47:10 +0200177extern void detect_extended_topology(struct cpuinfo_x86 *c);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100178extern void detect_ht(struct cpuinfo_x86 *c);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100179
Fenghua Yud288e1c2012-12-20 23:44:23 -0800180#ifdef CONFIG_X86_32
181extern int have_cpuid_p(void);
182#else
183static inline int have_cpuid_p(void)
184{
185 return 1;
186}
187#endif
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100188static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
Ingo Molnar4d46a892008-02-21 04:24:40 +0100189 unsigned int *ecx, unsigned int *edx)
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100190{
191 /* ecx is often an input as well as an output. */
Suresh Siddha45a94d72009-12-16 16:25:42 -0800192 asm volatile("cpuid"
Joe Perchescca2e6f2008-03-23 01:03:15 -0700193 : "=a" (*eax),
194 "=b" (*ebx),
195 "=c" (*ecx),
196 "=d" (*edx)
Andi Kleen506ed6b2011-10-12 17:46:33 -0700197 : "0" (*eax), "2" (*ecx)
198 : "memory");
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100199}
200
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +0100201static inline void load_cr3(pgd_t *pgdir)
202{
203 write_cr3(__pa(pgdir));
204}
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100205
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200206#ifdef CONFIG_X86_32
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100207/* This is the TSS defined by the hardware. */
208struct x86_hw_tss {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100209 unsigned short back_link, __blh;
210 unsigned long sp0;
211 unsigned short ss0, __ss0h;
212 unsigned long sp1;
213 /* ss1 caches MSR_IA32_SYSENTER_CS: */
214 unsigned short ss1, __ss1h;
215 unsigned long sp2;
216 unsigned short ss2, __ss2h;
217 unsigned long __cr3;
218 unsigned long ip;
219 unsigned long flags;
220 unsigned long ax;
221 unsigned long cx;
222 unsigned long dx;
223 unsigned long bx;
224 unsigned long sp;
225 unsigned long bp;
226 unsigned long si;
227 unsigned long di;
228 unsigned short es, __esh;
229 unsigned short cs, __csh;
230 unsigned short ss, __ssh;
231 unsigned short ds, __dsh;
232 unsigned short fs, __fsh;
233 unsigned short gs, __gsh;
234 unsigned short ldt, __ldth;
235 unsigned short trace;
236 unsigned short io_bitmap_base;
237
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100238} __attribute__((packed));
239#else
240struct x86_hw_tss {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100241 u32 reserved1;
242 u64 sp0;
243 u64 sp1;
244 u64 sp2;
245 u64 reserved2;
246 u64 ist[7];
247 u32 reserved3;
248 u32 reserved4;
249 u16 reserved5;
250 u16 io_bitmap_base;
251
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100252} __attribute__((packed)) ____cacheline_aligned;
253#endif
254
255/*
Ingo Molnar4d46a892008-02-21 04:24:40 +0100256 * IO-bitmap sizes:
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100257 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100258#define IO_BITMAP_BITS 65536
259#define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
260#define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
261#define IO_BITMAP_OFFSET offsetof(struct tss_struct, io_bitmap)
262#define INVALID_IO_BITMAP_OFFSET 0x8000
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100263
264struct tss_struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100265 /*
266 * The hardware state:
267 */
268 struct x86_hw_tss x86_tss;
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100269
270 /*
271 * The extra 1 is there because the CPU will access an
272 * additional byte beyond the end of the IO permission
273 * bitmap. The extra byte must be all 1 bits, and must
274 * be within the limit.
275 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100276 unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
Ingo Molnar4d46a892008-02-21 04:24:40 +0100277
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100278 /*
Ingo Molnar4d46a892008-02-21 04:24:40 +0100279 * .. and then another 0x100 bytes for the emergency kernel stack:
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100280 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100281 unsigned long stack[64];
282
Richard Kennedy84e65b02008-07-04 13:56:16 +0100283} ____cacheline_aligned;
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100284
David Howells9b8de742009-04-21 23:00:24 +0100285DECLARE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss);
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100286
Ingo Molnar4d46a892008-02-21 04:24:40 +0100287/*
288 * Save the original ist values for checking stack pointers during debugging
289 */
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100290struct orig_ist {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100291 unsigned long ist[7];
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100292};
293
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100294#define MXCSR_DEFAULT 0x1f80
295
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100296struct i387_fsave_struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100297 u32 cwd; /* FPU Control Word */
298 u32 swd; /* FPU Status Word */
299 u32 twd; /* FPU Tag Word */
300 u32 fip; /* FPU IP Offset */
301 u32 fcs; /* FPU IP Selector */
302 u32 foo; /* FPU Operand Pointer Offset */
303 u32 fos; /* FPU Operand Pointer Selector */
304
305 /* 8*10 bytes for each FP-reg = 80 bytes: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100306 u32 st_space[20];
Ingo Molnarca9cda22008-03-05 15:15:42 +0100307
308 /* Software status information [not touched by FSAVE ]: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100309 u32 status;
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100310};
311
312struct i387_fxsave_struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100313 u16 cwd; /* Control Word */
314 u16 swd; /* Status Word */
315 u16 twd; /* Tag Word */
316 u16 fop; /* Last Instruction Opcode */
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100317 union {
318 struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100319 u64 rip; /* Instruction Pointer */
320 u64 rdp; /* Data Pointer */
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100321 };
322 struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100323 u32 fip; /* FPU IP Offset */
324 u32 fcs; /* FPU IP Selector */
325 u32 foo; /* FPU Operand Offset */
326 u32 fos; /* FPU Operand Selector */
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100327 };
328 };
Ingo Molnarca9cda22008-03-05 15:15:42 +0100329 u32 mxcsr; /* MXCSR Register State */
330 u32 mxcsr_mask; /* MXCSR Mask */
331
332 /* 8*16 bytes for each FP-reg = 128 bytes: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100333 u32 st_space[32];
Ingo Molnarca9cda22008-03-05 15:15:42 +0100334
335 /* 16*16 bytes for each XMM-reg = 256 bytes: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100336 u32 xmm_space[64];
Ingo Molnarca9cda22008-03-05 15:15:42 +0100337
Suresh Siddhabdd8cab2008-07-29 10:29:24 -0700338 u32 padding[12];
339
340 union {
341 u32 padding1[12];
342 u32 sw_reserved[12];
343 };
Ingo Molnar4d46a892008-02-21 04:24:40 +0100344
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100345} __attribute__((aligned(16)));
346
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100347struct i387_soft_struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100348 u32 cwd;
349 u32 swd;
350 u32 twd;
351 u32 fip;
352 u32 fcs;
353 u32 foo;
354 u32 fos;
355 /* 8*10 bytes for each FP-reg = 80 bytes: */
356 u32 st_space[20];
357 u8 ftop;
358 u8 changed;
359 u8 lookahead;
360 u8 no_update;
361 u8 rm;
362 u8 alimit;
Tejun Heoae6af412009-02-09 22:17:39 +0900363 struct math_emu_info *info;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100364 u32 entry_eip;
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100365};
366
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700367struct ymmh_struct {
368 /* 16 * 16 bytes for each YMMH-reg = 256 bytes */
369 u32 ymmh_space[64];
370};
371
Ingo Molnar741e3902014-01-20 19:51:05 +0100372/* We don't support LWP yet: */
Qiaowei Rene7d820a2013-12-05 17:15:34 +0800373struct lwp_struct {
Ingo Molnar741e3902014-01-20 19:51:05 +0100374 u8 reserved[128];
Qiaowei Rene7d820a2013-12-05 17:15:34 +0800375};
376
Dave Hansenc04e0512014-10-31 14:58:20 -0700377struct bndreg {
378 u64 lower_bound;
379 u64 upper_bound;
Qiaowei Rene7d820a2013-12-05 17:15:34 +0800380} __packed;
381
Dave Hansen62e77592014-11-14 07:18:17 -0800382struct bndcsr {
383 u64 bndcfgu;
384 u64 bndstatus;
Qiaowei Rene7d820a2013-12-05 17:15:34 +0800385} __packed;
386
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700387struct xsave_hdr_struct {
388 u64 xstate_bv;
Fenghua Yu0b296432014-05-29 11:12:33 -0700389 u64 xcomp_bv;
390 u64 reserved[6];
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700391} __attribute__((packed));
392
393struct xsave_struct {
394 struct i387_fxsave_struct i387;
395 struct xsave_hdr_struct xsave_hdr;
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700396 struct ymmh_struct ymmh;
Qiaowei Rene7d820a2013-12-05 17:15:34 +0800397 struct lwp_struct lwp;
Dave Hansenc04e0512014-10-31 14:58:20 -0700398 struct bndreg bndreg[4];
Dave Hansen62e77592014-11-14 07:18:17 -0800399 struct bndcsr bndcsr;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700400 /* new processor state extensions will go here */
401} __attribute__ ((packed, aligned (64)));
402
Suresh Siddha61c46282008-03-10 15:28:04 -0700403union thread_xstate {
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100404 struct i387_fsave_struct fsave;
405 struct i387_fxsave_struct fxsave;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100406 struct i387_soft_struct soft;
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700407 struct xsave_struct xsave;
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100408};
409
Avi Kivity86603282010-05-06 11:45:46 +0300410struct fpu {
Linus Torvalds7e168382012-02-19 13:27:00 -0800411 unsigned int last_cpu;
412 unsigned int has_fpu;
Avi Kivity86603282010-05-06 11:45:46 +0300413 union thread_xstate *state;
414};
415
Glauber Costafe676202008-03-03 14:12:56 -0300416#ifdef CONFIG_X86_64
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100417DECLARE_PER_CPU(struct orig_ist, orig_ist);
Brian Gerst26f80bd2009-01-19 00:38:58 +0900418
Brian Gerst947e76c2009-01-19 12:21:28 +0900419union irq_stack_union {
420 char irq_stack[IRQ_STACK_SIZE];
421 /*
422 * GCC hardcodes the stack canary as %gs:40. Since the
423 * irq_stack is the object at %gs:0, we reserve the bottom
424 * 48 bytes of the irq stack for the canary.
425 */
426 struct {
427 char gs_base[40];
428 unsigned long stack_canary;
429 };
430};
431
Andi Kleen277d5b42013-08-05 15:02:43 -0700432DECLARE_PER_CPU_FIRST(union irq_stack_union, irq_stack_union) __visible;
Brian Gerst2add8e22009-02-08 09:58:39 -0500433DECLARE_INIT_PER_CPU(irq_stack_union);
434
Brian Gerst26f80bd2009-01-19 00:38:58 +0900435DECLARE_PER_CPU(char *, irq_stack_ptr);
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530436DECLARE_PER_CPU(unsigned int, irq_count);
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530437extern asmlinkage void ignore_sysret(void);
Tejun Heo60a53172009-02-09 22:17:40 +0900438#else /* X86_64 */
439#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge1ea0d142009-09-03 12:27:15 -0700440/*
441 * Make sure stack canary segment base is cached-aligned:
442 * "For Intel Atom processors, avoid non zero segment base address
443 * that is not aligned to cache line boundary at all cost."
444 * (Optim Ref Manual Assembly/Compiler Coding Rule 15.)
445 */
446struct stack_canary {
447 char __pad[20]; /* canary at %gs:20 */
448 unsigned long canary;
449};
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -0700450DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200451#endif
Steven Rostedt198d2082014-02-06 09:41:31 -0500452/*
453 * per-CPU IRQ handling stacks
454 */
455struct irq_stack {
456 u32 stack[THREAD_SIZE/sizeof(u32)];
457} __aligned(THREAD_SIZE);
458
459DECLARE_PER_CPU(struct irq_stack *, hardirq_stack);
460DECLARE_PER_CPU(struct irq_stack *, softirq_stack);
Tejun Heo60a53172009-02-09 22:17:40 +0900461#endif /* X86_64 */
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100462
Suresh Siddha61c46282008-03-10 15:28:04 -0700463extern unsigned int xstate_size;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700464extern void free_thread_xstate(struct task_struct *);
465extern struct kmem_cache *task_xstate_cachep;
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100466
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200467struct perf_event;
468
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100469struct thread_struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100470 /* Cached TLS descriptors: */
471 struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
472 unsigned long sp0;
473 unsigned long sp;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100474#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100475 unsigned long sysenter_cs;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100476#else
Ingo Molnar4d46a892008-02-21 04:24:40 +0100477 unsigned long usersp; /* Copy from PDA */
478 unsigned short es;
479 unsigned short ds;
480 unsigned short fsindex;
481 unsigned short gsindex;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100482#endif
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400483#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100484 unsigned long ip;
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400485#endif
Alexey Dobriyand756f4ad2009-05-04 03:29:52 +0400486#ifdef CONFIG_X86_64
Ingo Molnar4d46a892008-02-21 04:24:40 +0100487 unsigned long fs;
Alexey Dobriyand756f4ad2009-05-04 03:29:52 +0400488#endif
Ingo Molnar4d46a892008-02-21 04:24:40 +0100489 unsigned long gs;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200490 /* Save middle states of ptrace breakpoints */
491 struct perf_event *ptrace_bps[HBP_NUM];
492 /* Debug status used for traps, single steps, etc... */
493 unsigned long debugreg6;
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100494 /* Keep track of the exact dr7 value set by the user */
495 unsigned long ptrace_dr7;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100496 /* Fault info: */
497 unsigned long cr2;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530498 unsigned long trap_nr;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100499 unsigned long error_code;
Suresh Siddha61c46282008-03-10 15:28:04 -0700500 /* floating point and extended processor state */
Avi Kivity86603282010-05-06 11:45:46 +0300501 struct fpu fpu;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100502#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100503 /* Virtual 86 mode info */
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100504 struct vm86_struct __user *vm86_info;
505 unsigned long screen_bitmap;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100506 unsigned long v86flags;
507 unsigned long v86mask;
508 unsigned long saved_sp0;
509 unsigned int saved_fs;
510 unsigned int saved_gs;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100511#endif
Ingo Molnar4d46a892008-02-21 04:24:40 +0100512 /* IO permissions: */
513 unsigned long *io_bitmap_ptr;
514 unsigned long iopl;
515 /* Max allowed port in the bitmap, in bytes: */
516 unsigned io_bitmap_max;
Vineet Guptac375f152013-11-12 15:08:46 -0800517 /*
518 * fpu_counter contains the number of consecutive context switches
519 * that the FPU is used. If this is over a threshold, the lazy fpu
520 * saving becomes unlazy to save the trap. This is an unsigned char
521 * so that after 256 times the counter wraps and the behavior turns
522 * lazy again; this to deal with bursty apps that only use FPU for
523 * a short time
524 */
525 unsigned char fpu_counter;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100526};
527
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100528/*
529 * Set IOPL bits in EFLAGS from given mask
530 */
531static inline void native_set_iopl_mask(unsigned mask)
532{
533#ifdef CONFIG_X86_32
534 unsigned int reg;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100535
Joe Perchescca2e6f2008-03-23 01:03:15 -0700536 asm volatile ("pushfl;"
537 "popl %0;"
538 "andl %1, %0;"
539 "orl %2, %0;"
540 "pushl %0;"
541 "popfl"
542 : "=&r" (reg)
543 : "i" (~X86_EFLAGS_IOPL), "r" (mask));
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100544#endif
545}
546
Ingo Molnar4d46a892008-02-21 04:24:40 +0100547static inline void
548native_load_sp0(struct tss_struct *tss, struct thread_struct *thread)
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100549{
550 tss->x86_tss.sp0 = thread->sp0;
551#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100552 /* Only happens when SEP is enabled, no need to test "SEP"arately: */
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100553 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
554 tss->x86_tss.ss1 = thread->sysenter_cs;
555 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
556 }
557#endif
558}
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100559
Glauber de Oliveira Costae801f862008-01-30 13:32:08 +0100560static inline void native_swapgs(void)
561{
562#ifdef CONFIG_X86_64
563 asm volatile("swapgs" ::: "memory");
564#endif
565}
566
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100567#ifdef CONFIG_PARAVIRT
568#include <asm/paravirt.h>
569#else
Ingo Molnar4d46a892008-02-21 04:24:40 +0100570#define __cpuid native_cpuid
571#define paravirt_enabled() 0
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100572
Joe Perchescca2e6f2008-03-23 01:03:15 -0700573static inline void load_sp0(struct tss_struct *tss,
574 struct thread_struct *thread)
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100575{
576 native_load_sp0(tss, thread);
577}
578
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100579#define set_iopl_mask native_set_iopl_mask
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100580#endif /* CONFIG_PARAVIRT */
581
582/*
583 * Save the cr4 feature set we're using (ie
584 * Pentium 4MB enable and PPro Global page
585 * enable), so that any CPU's that boot up
586 * after us can get the correct flags.
587 */
Jarkko Sakkinencda846f2012-05-08 21:22:46 +0300588extern unsigned long mmu_cr4_features;
589extern u32 *trampoline_cr4_features;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100590
591static inline void set_in_cr4(unsigned long mask)
592{
Brian Gerst2df7a6e2010-09-03 21:17:08 -0400593 unsigned long cr4;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100594
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100595 mmu_cr4_features |= mask;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +0300596 if (trampoline_cr4_features)
597 *trampoline_cr4_features = mmu_cr4_features;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100598 cr4 = read_cr4();
599 cr4 |= mask;
600 write_cr4(cr4);
601}
602
603static inline void clear_in_cr4(unsigned long mask)
604{
Brian Gerst2df7a6e2010-09-03 21:17:08 -0400605 unsigned long cr4;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100606
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100607 mmu_cr4_features &= ~mask;
Jarkko Sakkinencda846f2012-05-08 21:22:46 +0300608 if (trampoline_cr4_features)
609 *trampoline_cr4_features = mmu_cr4_features;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100610 cr4 = read_cr4();
611 cr4 &= ~mask;
612 write_cr4(cr4);
613}
614
Glauber de Oliveira Costafc87e902008-01-30 13:31:38 +0100615typedef struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100616 unsigned long seg;
Glauber de Oliveira Costafc87e902008-01-30 13:31:38 +0100617} mm_segment_t;
618
619
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100620/* Free all resources held by a thread. */
621extern void release_thread(struct task_struct *);
622
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100623unsigned long get_wchan(struct task_struct *p);
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100624
625/*
626 * Generic CPUID function
627 * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
628 * resulting in stale register contents being returned.
629 */
630static inline void cpuid(unsigned int op,
631 unsigned int *eax, unsigned int *ebx,
632 unsigned int *ecx, unsigned int *edx)
633{
634 *eax = op;
635 *ecx = 0;
636 __cpuid(eax, ebx, ecx, edx);
637}
638
639/* Some CPUID calls want 'count' to be placed in ecx */
640static inline void cpuid_count(unsigned int op, int count,
641 unsigned int *eax, unsigned int *ebx,
642 unsigned int *ecx, unsigned int *edx)
643{
644 *eax = op;
645 *ecx = count;
646 __cpuid(eax, ebx, ecx, edx);
647}
648
649/*
650 * CPUID functions returning a single datum
651 */
652static inline unsigned int cpuid_eax(unsigned int op)
653{
654 unsigned int eax, ebx, ecx, edx;
655
656 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100657
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100658 return eax;
659}
Ingo Molnar4d46a892008-02-21 04:24:40 +0100660
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100661static inline unsigned int cpuid_ebx(unsigned int op)
662{
663 unsigned int eax, ebx, ecx, edx;
664
665 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100666
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100667 return ebx;
668}
Ingo Molnar4d46a892008-02-21 04:24:40 +0100669
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100670static inline unsigned int cpuid_ecx(unsigned int op)
671{
672 unsigned int eax, ebx, ecx, edx;
673
674 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100675
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100676 return ecx;
677}
Ingo Molnar4d46a892008-02-21 04:24:40 +0100678
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100679static inline unsigned int cpuid_edx(unsigned int op)
680{
681 unsigned int eax, ebx, ecx, edx;
682
683 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100684
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100685 return edx;
686}
687
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100688/* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
689static inline void rep_nop(void)
690{
Joe Perchescca2e6f2008-03-23 01:03:15 -0700691 asm volatile("rep; nop" ::: "memory");
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100692}
693
Ingo Molnar4d46a892008-02-21 04:24:40 +0100694static inline void cpu_relax(void)
695{
696 rep_nop();
697}
698
Davidlohr Bueso3a6bfbc2014-06-29 15:09:33 -0700699#define cpu_relax_lowlatency() cpu_relax()
700
Ben Hutchings5367b6882009-09-10 02:53:50 +0100701/* Stop speculative execution and prefetching of modified code. */
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100702static inline void sync_core(void)
703{
704 int tmp;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100705
H. Peter Anvineb068e72012-11-28 11:50:23 -0800706#ifdef CONFIG_M486
H. Peter Anvin45c39fb2012-11-28 11:50:30 -0800707 /*
708 * Do a CPUID if available, otherwise do a jump. The jump
709 * can conveniently enough be the jump around CPUID.
710 */
711 asm volatile("cmpl %2,%1\n\t"
712 "jl 1f\n\t"
713 "cpuid\n"
714 "1:"
715 : "=a" (tmp)
716 : "rm" (boot_cpu_data.cpuid_level), "ri" (0), "0" (1)
717 : "ebx", "ecx", "edx", "memory");
718#else
719 /*
720 * CPUID is a barrier to speculative execution.
721 * Prefetched instructions are automatically
722 * invalidated when modified.
723 */
724 asm volatile("cpuid"
725 : "=a" (tmp)
726 : "0" (1)
727 : "ebx", "ecx", "edx", "memory");
Ben Hutchings5367b6882009-09-10 02:53:50 +0100728#endif
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100729}
730
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100731extern void select_idle_routine(const struct cpuinfo_x86 *c);
Len Brown02c68a02011-04-01 16:59:53 -0400732extern void init_amd_e400_c1e_mask(void);
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100733
Ingo Molnar4d46a892008-02-21 04:24:40 +0100734extern unsigned long boot_option_idle_override;
Len Brown02c68a02011-04-01 16:59:53 -0400735extern bool amd_e400_c1e_detected;
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100736
Thomas Renningerd1896042010-11-03 17:06:14 +0100737enum idle_boot_override {IDLE_NO_OVERRIDE=0, IDLE_HALT, IDLE_NOMWAIT,
Len Brown69fb3672013-02-10 01:38:39 -0500738 IDLE_POLL};
Thomas Renningerd1896042010-11-03 17:06:14 +0100739
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100740extern void enable_sep_cpu(void);
741extern int sysenter_setup(void);
742
Jan Kiszka29c84392010-05-20 21:04:29 -0500743extern void early_trap_init(void);
H. Peter Anvin8170e6b2013-01-24 12:19:52 -0800744void early_trap_pf_init(void);
Jan Kiszka29c84392010-05-20 21:04:29 -0500745
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100746/* Defined in head.S */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100747extern struct desc_ptr early_gdt_descr;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100748
749extern void cpu_set_gdt(int);
Brian Gerst552be872009-01-30 17:47:53 +0900750extern void switch_to_new_gdt(int);
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900751extern void load_percpu_segment(int);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100752extern void cpu_init(void);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100753
Markus Metzgerc2724772008-12-11 13:49:59 +0100754static inline unsigned long get_debugctlmsr(void)
755{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100756 unsigned long debugctlmsr = 0;
Markus Metzgerc2724772008-12-11 13:49:59 +0100757
758#ifndef CONFIG_X86_DEBUGCTLMSR
759 if (boot_cpu_data.x86 < 6)
760 return 0;
761#endif
762 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
763
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100764 return debugctlmsr;
Markus Metzgerc2724772008-12-11 13:49:59 +0100765}
766
Jan Beulich5b0e5082008-03-10 13:11:17 +0000767static inline void update_debugctlmsr(unsigned long debugctlmsr)
768{
769#ifndef CONFIG_X86_DEBUGCTLMSR
770 if (boot_cpu_data.x86 < 6)
771 return;
772#endif
773 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
774}
775
Oleg Nesterov9bd11902012-09-03 15:24:17 +0200776extern void set_task_blockstep(struct task_struct *task, bool on);
777
Ingo Molnar4d46a892008-02-21 04:24:40 +0100778/*
779 * from system description table in BIOS. Mostly for MCA use, but
780 * others may find it useful:
781 */
782extern unsigned int machine_id;
783extern unsigned int machine_submodel_id;
784extern unsigned int BIOS_revision;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100785
Ingo Molnar4d46a892008-02-21 04:24:40 +0100786/* Boot loader type from the setup header: */
787extern int bootloader_type;
H. Peter Anvin50312962009-05-07 16:54:11 -0700788extern int bootloader_version;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100789
Ingo Molnar4d46a892008-02-21 04:24:40 +0100790extern char ignore_fpu_irq;
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100791
792#define HAVE_ARCH_PICK_MMAP_LAYOUT 1
793#define ARCH_HAS_PREFETCHW
794#define ARCH_HAS_SPINLOCK_PREFETCH
795
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100796#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100797# define BASE_PREFETCH ASM_NOP4
798# define ARCH_HAS_PREFETCH
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100799#else
Ingo Molnar4d46a892008-02-21 04:24:40 +0100800# define BASE_PREFETCH "prefetcht0 (%1)"
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100801#endif
802
Ingo Molnar4d46a892008-02-21 04:24:40 +0100803/*
804 * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
805 *
806 * It's not worth to care about 3dnow prefetches for the K6
807 * because they are microcoded there and very slow.
808 */
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100809static inline void prefetch(const void *x)
810{
811 alternative_input(BASE_PREFETCH,
812 "prefetchnta (%1)",
813 X86_FEATURE_XMM,
814 "r" (x));
815}
816
Ingo Molnar4d46a892008-02-21 04:24:40 +0100817/*
818 * 3dnow prefetch to get an exclusive cache line.
819 * Useful for spinlocks to avoid one state transition in the
820 * cache coherency protocol:
821 */
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100822static inline void prefetchw(const void *x)
823{
824 alternative_input(BASE_PREFETCH,
825 "prefetchw (%1)",
826 X86_FEATURE_3DNOW,
827 "r" (x));
828}
829
Ingo Molnar4d46a892008-02-21 04:24:40 +0100830static inline void spin_lock_prefetch(const void *x)
831{
832 prefetchw(x);
833}
834
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100835#ifdef CONFIG_X86_32
836/*
837 * User space process size: 3GB (default).
838 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100839#define TASK_SIZE PAGE_OFFSET
Ingo Molnard9517342009-02-20 23:32:28 +0100840#define TASK_SIZE_MAX TASK_SIZE
Ingo Molnar4d46a892008-02-21 04:24:40 +0100841#define STACK_TOP TASK_SIZE
842#define STACK_TOP_MAX STACK_TOP
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100843
Ingo Molnar4d46a892008-02-21 04:24:40 +0100844#define INIT_THREAD { \
845 .sp0 = sizeof(init_stack) + (long)&init_stack, \
846 .vm86_info = NULL, \
847 .sysenter_cs = __KERNEL_CS, \
848 .io_bitmap_ptr = NULL, \
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100849}
850
851/*
852 * Note that the .io_bitmap member must be extra-big. This is because
853 * the CPU will access an additional byte beyond the end of the IO
854 * permission bitmap. The extra byte must be all 1 bits, and must
855 * be within the limit.
856 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100857#define INIT_TSS { \
858 .x86_tss = { \
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100859 .sp0 = sizeof(init_stack) + (long)&init_stack, \
Ingo Molnar4d46a892008-02-21 04:24:40 +0100860 .ss0 = __KERNEL_DS, \
861 .ss1 = __KERNEL_CS, \
862 .io_bitmap_base = INVALID_IO_BITMAP_OFFSET, \
863 }, \
864 .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 }, \
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100865}
866
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100867extern unsigned long thread_saved_pc(struct task_struct *tsk);
868
869#define THREAD_SIZE_LONGS (THREAD_SIZE/sizeof(unsigned long))
870#define KSTK_TOP(info) \
871({ \
872 unsigned long *__ptr = (unsigned long *)(info); \
873 (unsigned long)(&__ptr[THREAD_SIZE_LONGS]); \
874})
875
876/*
877 * The below -8 is to reserve 8 bytes on top of the ring0 stack.
878 * This is necessary to guarantee that the entire "struct pt_regs"
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400879 * is accessible even if the CPU haven't stored the SS/ESP registers
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100880 * on the stack (interrupt gate does not save these registers
881 * when switching to the same priv ring).
882 * Therefore beware: accessing the ss/esp fields of the
883 * "struct pt_regs" is possible, but they may contain the
884 * completely wrong values.
885 */
886#define task_pt_regs(task) \
887({ \
888 struct pt_regs *__regs__; \
889 __regs__ = (struct pt_regs *)(KSTK_TOP(task_stack_page(task))-8); \
890 __regs__ - 1; \
891})
892
Ingo Molnar4d46a892008-02-21 04:24:40 +0100893#define KSTK_ESP(task) (task_pt_regs(task)->sp)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100894
895#else
896/*
Andy Lutomirski07114f02014-11-04 15:46:21 -0800897 * User space process size. 47bits minus one guard page. The guard
898 * page is necessary on Intel CPUs: if a SYSCALL instruction is at
899 * the highest possible canonical userspace address, then that
900 * syscall will enter the kernel with a non-canonical return
901 * address, and SYSRET will explode dangerously. We avoid this
902 * particular problem by preventing anything from being mapped
903 * at the maximum canonical address.
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100904 */
Ingo Molnard9517342009-02-20 23:32:28 +0100905#define TASK_SIZE_MAX ((1UL << 47) - PAGE_SIZE)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100906
907/* This decides where the kernel will search for a free chunk of vm
908 * space during mmap's.
909 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100910#define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? \
911 0xc0000000 : 0xFFFFe000)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100912
H. Peter Anvin6bd33002012-02-06 13:03:09 -0800913#define TASK_SIZE (test_thread_flag(TIF_ADDR32) ? \
Ingo Molnard9517342009-02-20 23:32:28 +0100914 IA32_PAGE_OFFSET : TASK_SIZE_MAX)
H. Peter Anvin6bd33002012-02-06 13:03:09 -0800915#define TASK_SIZE_OF(child) ((test_tsk_thread_flag(child, TIF_ADDR32)) ? \
Ingo Molnard9517342009-02-20 23:32:28 +0100916 IA32_PAGE_OFFSET : TASK_SIZE_MAX)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100917
David Howells922a70d2008-02-08 04:19:26 -0800918#define STACK_TOP TASK_SIZE
Ingo Molnard9517342009-02-20 23:32:28 +0100919#define STACK_TOP_MAX TASK_SIZE_MAX
David Howells922a70d2008-02-08 04:19:26 -0800920
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100921#define INIT_THREAD { \
922 .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
923}
924
925#define INIT_TSS { \
926 .x86_tss.sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
927}
928
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100929/*
930 * Return saved PC of a blocked thread.
931 * What is this good for? it will be always the scheduler or ret_from_fork.
932 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100933#define thread_saved_pc(t) (*(unsigned long *)((t)->thread.sp - 8))
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100934
Ingo Molnar4d46a892008-02-21 04:24:40 +0100935#define task_pt_regs(tsk) ((struct pt_regs *)(tsk)->thread.sp0 - 1)
Stefani Seibold89240ba2009-11-03 10:22:40 +0100936extern unsigned long KSTK_ESP(struct task_struct *task);
H. J. Lud046ff82012-02-14 13:49:48 -0800937
938/*
939 * User space RSP while inside the SYSCALL fast path
940 */
941DECLARE_PER_CPU(unsigned long, old_rsp);
942
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100943#endif /* CONFIG_X86_64 */
944
Ingo Molnar513ad842008-02-21 05:18:40 +0100945extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
946 unsigned long new_sp);
947
Ingo Molnar4d46a892008-02-21 04:24:40 +0100948/*
949 * This decides where the kernel will search for a free chunk of vm
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100950 * space during mmap's.
951 */
952#define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
953
Ingo Molnar4d46a892008-02-21 04:24:40 +0100954#define KSTK_EIP(task) (task_pt_regs(task)->ip)
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100955
Erik Bosman529e25f2008-04-14 00:24:18 +0200956/* Get/set a process' ability to use the timestamp counter instruction */
957#define GET_TSC_CTL(adr) get_tsc_mode((adr))
958#define SET_TSC_CTL(val) set_tsc_mode((val))
959
960extern int get_tsc_mode(unsigned long adr);
961extern int set_tsc_mode(unsigned int val);
962
Dave Hansenfe3d1972014-11-14 07:18:29 -0800963/* Register/unregister a process' MPX related resource */
964#define MPX_ENABLE_MANAGEMENT(tsk) mpx_enable_management((tsk))
965#define MPX_DISABLE_MANAGEMENT(tsk) mpx_disable_management((tsk))
966
967#ifdef CONFIG_X86_INTEL_MPX
968extern int mpx_enable_management(struct task_struct *tsk);
969extern int mpx_disable_management(struct task_struct *tsk);
970#else
971static inline int mpx_enable_management(struct task_struct *tsk)
972{
973 return -EINVAL;
974}
975static inline int mpx_disable_management(struct task_struct *tsk)
976{
977 return -EINVAL;
978}
979#endif /* CONFIG_X86_INTEL_MPX */
980
Daniel J Blueman8b84c8d2012-11-27 14:32:10 +0800981extern u16 amd_get_nb_id(int cpu);
Andreas Herrmann6a812692009-09-16 11:33:40 +0200982
Jason Wang96e39ac2013-07-25 16:54:32 +0800983static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
984{
985 uint32_t base, eax, signature[3];
986
987 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
988 cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
989
990 if (!memcmp(sig, signature, 12) &&
991 (leaves == 0 || ((eax - base) >= leaves)))
992 return base;
993 }
994
995 return 0;
996}
997
David Howellsf05e7982012-03-28 18:11:12 +0100998extern unsigned long arch_align_stack(unsigned long sp);
999extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
1000
1001void default_idle(void);
Len Brown6a377dd2013-02-09 23:08:07 -05001002#ifdef CONFIG_XEN
1003bool xen_set_default_idle(void);
1004#else
1005#define xen_set_default_idle 0
1006#endif
David Howellsf05e7982012-03-28 18:11:12 +01001007
1008void stop_this_cpu(void *dummy);
Borislav Petkov4d067d82013-05-09 12:02:29 +02001009void df_debug(struct pt_regs *regs, long error_code);
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001010#endif /* _ASM_X86_PROCESSOR_H */