blob: 78e30ea492b2b0f33236f251f9cd3376f517d2ae [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>
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +010030#include <linux/init.h>
Peter Zijlstrafaa46022010-03-25 14:51:50 +010031#include <linux/err.h>
David Howellsf05e7982012-03-28 18:11:12 +010032#include <linux/irqflags.h>
33
34/*
35 * We handle most unaligned accesses in hardware. On the other hand
36 * unaligned DMA can be quite expensive on some Nehalem processors.
37 *
38 * Based on this we disable the IP header alignment in network drivers.
39 */
40#define NET_IP_ALIGN 0
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +010041
K.Prasadb332828c2009-06-01 23:43:10 +053042#define HBP_NUM 4
Glauber de Oliveira Costa0ccb8ac2008-01-30 13:31:27 +010043/*
44 * Default implementation of macro that returns current
45 * instruction pointer ("program counter").
46 */
47static inline void *current_text_addr(void)
48{
49 void *pc;
Ingo Molnar4d46a892008-02-21 04:24:40 +010050
51 asm volatile("mov $1f, %0; 1:":"=r" (pc));
52
Glauber de Oliveira Costa0ccb8ac2008-01-30 13:31:27 +010053 return pc;
54}
55
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010056#ifdef CONFIG_X86_VSMP
Ingo Molnar4d46a892008-02-21 04:24:40 +010057# define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
58# define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010059#else
Ingo Molnar4d46a892008-02-21 04:24:40 +010060# define ARCH_MIN_TASKALIGN 16
61# define ARCH_MIN_MMSTRUCT_ALIGN 0
Glauber de Oliveira Costadbcb4662008-01-30 13:31:31 +010062#endif
63
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010064/*
65 * CPU type and hardware bug flags. Kept separately for each CPU.
66 * Members of this structure are referenced in head.S, so think twice
67 * before touching them. [mj]
68 */
69
70struct cpuinfo_x86 {
Ingo Molnar4d46a892008-02-21 04:24:40 +010071 __u8 x86; /* CPU family */
72 __u8 x86_vendor; /* CPU vendor */
73 __u8 x86_model;
74 __u8 x86_mask;
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010075#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +010076 char wp_works_ok; /* It doesn't on 386's */
77
78 /* Problems on some 486Dx4's and old 386's: */
79 char hlt_works_ok;
80 char hard_math;
81 char rfu;
82 char fdiv_bug;
83 char f00f_bug;
84 char coma_bug;
85 char pad0;
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +010086#else
Ingo Molnar4d46a892008-02-21 04:24:40 +010087 /* Number of 4K pages in DTLB/ITLB combined(in pages): */
H. Peter Anvinb1882e62009-01-23 17:18:52 -080088 int x86_tlbsize;
Jan Beulich13c6c532009-03-12 12:37:34 +000089#endif
Ingo Molnar4d46a892008-02-21 04:24:40 +010090 __u8 x86_virt_bits;
91 __u8 x86_phys_bits;
92 /* CPUID returned core id bits: */
93 __u8 x86_coreid_bits;
94 /* Max extended CPUID function supported: */
95 __u32 extended_cpuid_level;
Ingo Molnar4d46a892008-02-21 04:24:40 +010096 /* Maximum supported CPUID level, -1=no CPUID: */
97 int cpuid_level;
98 __u32 x86_capability[NCAPINTS];
99 char x86_vendor_id[16];
100 char x86_model_id[64];
101 /* in KB - valid for CPUS which support this call: */
102 int x86_cache_size;
103 int x86_cache_alignment; /* In bytes */
104 int x86_power;
105 unsigned long loops_per_jiffy;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100106 /* cpuid returned max cores value: */
107 u16 x86_max_cores;
108 u16 apicid;
Yinghai Lu01aaea12008-03-06 13:46:39 -0800109 u16 initial_apicid;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100110 u16 x86_clflush_size;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100111 /* number of cores as seen by the OS: */
112 u16 booted_cores;
113 /* Physical processor id: */
114 u16 phys_proc_id;
115 /* Core id: */
116 u16 cpu_core_id;
Andreas Herrmann6057b4d2010-09-30 14:38:57 +0200117 /* Compute unit id */
118 u8 compute_unit_id;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100119 /* Index into per_cpu list: */
120 u16 cpu_index;
Andi Kleen506ed6b2011-10-12 17:46:33 -0700121 u32 microcode;
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100122} __attribute__((__aligned__(SMP_CACHE_BYTES)));
123
Ingo Molnar4d46a892008-02-21 04:24:40 +0100124#define X86_VENDOR_INTEL 0
125#define X86_VENDOR_CYRIX 1
126#define X86_VENDOR_AMD 2
127#define X86_VENDOR_UMC 3
Ingo Molnar4d46a892008-02-21 04:24:40 +0100128#define X86_VENDOR_CENTAUR 5
129#define X86_VENDOR_TRANSMETA 7
130#define X86_VENDOR_NSC 8
131#define X86_VENDOR_NUM 9
132
133#define X86_VENDOR_UNKNOWN 0xff
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100134
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100135/*
136 * capabilities of CPUs
137 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100138extern struct cpuinfo_x86 boot_cpu_data;
139extern struct cpuinfo_x86 new_cpu_data;
140
141extern struct tss_struct doublefault_tss;
Yinghai Lu3e0c3732009-05-09 23:47:42 -0700142extern __u32 cpu_caps_cleared[NCAPINTS];
143extern __u32 cpu_caps_set[NCAPINTS];
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100144
145#ifdef CONFIG_SMP
David Howells9b8de742009-04-21 23:00:24 +0100146DECLARE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100147#define cpu_data(cpu) per_cpu(cpu_info, cpu)
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100148#else
Tejun Heo7b543a52010-12-18 16:30:05 +0100149#define cpu_info boot_cpu_data
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100150#define cpu_data(cpu) boot_cpu_data
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100151#endif
152
Jaswinder Singh1c6c7272008-07-21 22:40:37 +0530153extern const struct seq_operations cpuinfo_op;
154
Glauber Costa3d3f4872008-03-03 14:12:48 -0300155static inline int hlt_works(int cpu)
156{
157#ifdef CONFIG_X86_32
158 return cpu_data(cpu).hlt_works_ok;
159#else
160 return 1;
161#endif
162}
163
Ingo Molnar4d46a892008-02-21 04:24:40 +0100164#define cache_line_size() (boot_cpu_data.x86_cache_alignment)
165
166extern void cpu_detect(struct cpuinfo_x86 *c);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100167
Jaswinder Singh8fd329a2008-07-21 22:54:56 +0530168extern struct pt_regs *idle_regs(struct pt_regs *);
169
Yinghai Luf5803662008-06-21 03:24:19 -0700170extern void early_cpu_init(void);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100171extern void identify_boot_cpu(void);
172extern void identify_secondary_cpu(struct cpuinfo_x86 *);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100173extern void print_cpu_info(struct cpuinfo_x86 *);
Yinghai Lu21c3fcf2012-02-12 09:53:57 -0800174void print_cpu_msr(struct cpuinfo_x86 *);
Glauber de Oliveira Costa5300db82008-01-30 13:31:33 +0100175extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
176extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
177extern unsigned short num_cache_leaves;
178
Suresh Siddhabbb65d22008-08-23 17:47:10 +0200179extern void detect_extended_topology(struct cpuinfo_x86 *c);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100180extern void detect_ht(struct cpuinfo_x86 *c);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100181
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100182static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
Ingo Molnar4d46a892008-02-21 04:24:40 +0100183 unsigned int *ecx, unsigned int *edx)
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100184{
185 /* ecx is often an input as well as an output. */
Suresh Siddha45a94d72009-12-16 16:25:42 -0800186 asm volatile("cpuid"
Joe Perchescca2e6f2008-03-23 01:03:15 -0700187 : "=a" (*eax),
188 "=b" (*ebx),
189 "=c" (*ecx),
190 "=d" (*edx)
Andi Kleen506ed6b2011-10-12 17:46:33 -0700191 : "0" (*eax), "2" (*ecx)
192 : "memory");
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100193}
194
Glauber de Oliveira Costac72dcf82008-01-30 13:31:27 +0100195static inline void load_cr3(pgd_t *pgdir)
196{
197 write_cr3(__pa(pgdir));
198}
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100199
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200200#ifdef CONFIG_X86_32
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100201/* This is the TSS defined by the hardware. */
202struct x86_hw_tss {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100203 unsigned short back_link, __blh;
204 unsigned long sp0;
205 unsigned short ss0, __ss0h;
206 unsigned long sp1;
207 /* ss1 caches MSR_IA32_SYSENTER_CS: */
208 unsigned short ss1, __ss1h;
209 unsigned long sp2;
210 unsigned short ss2, __ss2h;
211 unsigned long __cr3;
212 unsigned long ip;
213 unsigned long flags;
214 unsigned long ax;
215 unsigned long cx;
216 unsigned long dx;
217 unsigned long bx;
218 unsigned long sp;
219 unsigned long bp;
220 unsigned long si;
221 unsigned long di;
222 unsigned short es, __esh;
223 unsigned short cs, __csh;
224 unsigned short ss, __ssh;
225 unsigned short ds, __dsh;
226 unsigned short fs, __fsh;
227 unsigned short gs, __gsh;
228 unsigned short ldt, __ldth;
229 unsigned short trace;
230 unsigned short io_bitmap_base;
231
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100232} __attribute__((packed));
233#else
234struct x86_hw_tss {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100235 u32 reserved1;
236 u64 sp0;
237 u64 sp1;
238 u64 sp2;
239 u64 reserved2;
240 u64 ist[7];
241 u32 reserved3;
242 u32 reserved4;
243 u16 reserved5;
244 u16 io_bitmap_base;
245
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100246} __attribute__((packed)) ____cacheline_aligned;
247#endif
248
249/*
Ingo Molnar4d46a892008-02-21 04:24:40 +0100250 * IO-bitmap sizes:
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100251 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100252#define IO_BITMAP_BITS 65536
253#define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
254#define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
255#define IO_BITMAP_OFFSET offsetof(struct tss_struct, io_bitmap)
256#define INVALID_IO_BITMAP_OFFSET 0x8000
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100257
258struct tss_struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100259 /*
260 * The hardware state:
261 */
262 struct x86_hw_tss x86_tss;
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100263
264 /*
265 * The extra 1 is there because the CPU will access an
266 * additional byte beyond the end of the IO permission
267 * bitmap. The extra byte must be all 1 bits, and must
268 * be within the limit.
269 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100270 unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
Ingo Molnar4d46a892008-02-21 04:24:40 +0100271
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100272 /*
Ingo Molnar4d46a892008-02-21 04:24:40 +0100273 * .. and then another 0x100 bytes for the emergency kernel stack:
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100274 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100275 unsigned long stack[64];
276
Richard Kennedy84e65b02008-07-04 13:56:16 +0100277} ____cacheline_aligned;
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100278
David Howells9b8de742009-04-21 23:00:24 +0100279DECLARE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss);
Glauber de Oliveira Costaca241c72008-01-30 13:31:31 +0100280
Ingo Molnar4d46a892008-02-21 04:24:40 +0100281/*
282 * Save the original ist values for checking stack pointers during debugging
283 */
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100284struct orig_ist {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100285 unsigned long ist[7];
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100286};
287
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100288#define MXCSR_DEFAULT 0x1f80
289
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100290struct i387_fsave_struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100291 u32 cwd; /* FPU Control Word */
292 u32 swd; /* FPU Status Word */
293 u32 twd; /* FPU Tag Word */
294 u32 fip; /* FPU IP Offset */
295 u32 fcs; /* FPU IP Selector */
296 u32 foo; /* FPU Operand Pointer Offset */
297 u32 fos; /* FPU Operand Pointer Selector */
298
299 /* 8*10 bytes for each FP-reg = 80 bytes: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100300 u32 st_space[20];
Ingo Molnarca9cda22008-03-05 15:15:42 +0100301
302 /* Software status information [not touched by FSAVE ]: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100303 u32 status;
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100304};
305
306struct i387_fxsave_struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100307 u16 cwd; /* Control Word */
308 u16 swd; /* Status Word */
309 u16 twd; /* Tag Word */
310 u16 fop; /* Last Instruction Opcode */
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100311 union {
312 struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100313 u64 rip; /* Instruction Pointer */
314 u64 rdp; /* Data Pointer */
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100315 };
316 struct {
Ingo Molnarca9cda22008-03-05 15:15:42 +0100317 u32 fip; /* FPU IP Offset */
318 u32 fcs; /* FPU IP Selector */
319 u32 foo; /* FPU Operand Offset */
320 u32 fos; /* FPU Operand Selector */
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100321 };
322 };
Ingo Molnarca9cda22008-03-05 15:15:42 +0100323 u32 mxcsr; /* MXCSR Register State */
324 u32 mxcsr_mask; /* MXCSR Mask */
325
326 /* 8*16 bytes for each FP-reg = 128 bytes: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100327 u32 st_space[32];
Ingo Molnarca9cda22008-03-05 15:15:42 +0100328
329 /* 16*16 bytes for each XMM-reg = 256 bytes: */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100330 u32 xmm_space[64];
Ingo Molnarca9cda22008-03-05 15:15:42 +0100331
Suresh Siddhabdd8cab2008-07-29 10:29:24 -0700332 u32 padding[12];
333
334 union {
335 u32 padding1[12];
336 u32 sw_reserved[12];
337 };
Ingo Molnar4d46a892008-02-21 04:24:40 +0100338
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100339} __attribute__((aligned(16)));
340
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100341struct i387_soft_struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100342 u32 cwd;
343 u32 swd;
344 u32 twd;
345 u32 fip;
346 u32 fcs;
347 u32 foo;
348 u32 fos;
349 /* 8*10 bytes for each FP-reg = 80 bytes: */
350 u32 st_space[20];
351 u8 ftop;
352 u8 changed;
353 u8 lookahead;
354 u8 no_update;
355 u8 rm;
356 u8 alimit;
Tejun Heoae6af412009-02-09 22:17:39 +0900357 struct math_emu_info *info;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100358 u32 entry_eip;
Glauber de Oliveira Costa46265df2008-01-30 13:31:41 +0100359};
360
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700361struct ymmh_struct {
362 /* 16 * 16 bytes for each YMMH-reg = 256 bytes */
363 u32 ymmh_space[64];
364};
365
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700366struct xsave_hdr_struct {
367 u64 xstate_bv;
368 u64 reserved1[2];
369 u64 reserved2[5];
370} __attribute__((packed));
371
372struct xsave_struct {
373 struct i387_fxsave_struct i387;
374 struct xsave_hdr_struct xsave_hdr;
Suresh Siddhaa30469e2009-04-10 15:21:24 -0700375 struct ymmh_struct ymmh;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700376 /* new processor state extensions will go here */
377} __attribute__ ((packed, aligned (64)));
378
Suresh Siddha61c46282008-03-10 15:28:04 -0700379union thread_xstate {
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100380 struct i387_fsave_struct fsave;
381 struct i387_fxsave_struct fxsave;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100382 struct i387_soft_struct soft;
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700383 struct xsave_struct xsave;
Roland McGrath99f8ecd2008-01-30 13:31:48 +0100384};
385
Avi Kivity86603282010-05-06 11:45:46 +0300386struct fpu {
Linus Torvalds7e168382012-02-19 13:27:00 -0800387 unsigned int last_cpu;
388 unsigned int has_fpu;
Avi Kivity86603282010-05-06 11:45:46 +0300389 union thread_xstate *state;
390};
391
Glauber Costafe676202008-03-03 14:12:56 -0300392#ifdef CONFIG_X86_64
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100393DECLARE_PER_CPU(struct orig_ist, orig_ist);
Brian Gerst26f80bd2009-01-19 00:38:58 +0900394
Brian Gerst947e76c2009-01-19 12:21:28 +0900395union irq_stack_union {
396 char irq_stack[IRQ_STACK_SIZE];
397 /*
398 * GCC hardcodes the stack canary as %gs:40. Since the
399 * irq_stack is the object at %gs:0, we reserve the bottom
400 * 48 bytes of the irq stack for the canary.
401 */
402 struct {
403 char gs_base[40];
404 unsigned long stack_canary;
405 };
406};
407
David Howells9b8de742009-04-21 23:00:24 +0100408DECLARE_PER_CPU_FIRST(union irq_stack_union, irq_stack_union);
Brian Gerst2add8e22009-02-08 09:58:39 -0500409DECLARE_INIT_PER_CPU(irq_stack_union);
410
Brian Gerst26f80bd2009-01-19 00:38:58 +0900411DECLARE_PER_CPU(char *, irq_stack_ptr);
Jaswinder Singh Rajput9766cdb2009-03-14 11:19:49 +0530412DECLARE_PER_CPU(unsigned int, irq_count);
413extern unsigned long kernel_eflags;
414extern asmlinkage void ignore_sysret(void);
Tejun Heo60a53172009-02-09 22:17:40 +0900415#else /* X86_64 */
416#ifdef CONFIG_CC_STACKPROTECTOR
Jeremy Fitzhardinge1ea0d142009-09-03 12:27:15 -0700417/*
418 * Make sure stack canary segment base is cached-aligned:
419 * "For Intel Atom processors, avoid non zero segment base address
420 * that is not aligned to cache line boundary at all cost."
421 * (Optim Ref Manual Assembly/Compiler Coding Rule 15.)
422 */
423struct stack_canary {
424 char __pad[20]; /* canary at %gs:20 */
425 unsigned long canary;
426};
Jeremy Fitzhardinge53f82452009-09-03 14:31:44 -0700427DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200428#endif
Tejun Heo60a53172009-02-09 22:17:40 +0900429#endif /* X86_64 */
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100430
Suresh Siddha61c46282008-03-10 15:28:04 -0700431extern unsigned int xstate_size;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700432extern void free_thread_xstate(struct task_struct *);
433extern struct kmem_cache *task_xstate_cachep;
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100434
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200435struct perf_event;
436
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100437struct thread_struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100438 /* Cached TLS descriptors: */
439 struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
440 unsigned long sp0;
441 unsigned long sp;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100442#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100443 unsigned long sysenter_cs;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100444#else
Ingo Molnar4d46a892008-02-21 04:24:40 +0100445 unsigned long usersp; /* Copy from PDA */
446 unsigned short es;
447 unsigned short ds;
448 unsigned short fsindex;
449 unsigned short gsindex;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100450#endif
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400451#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100452 unsigned long ip;
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400453#endif
Alexey Dobriyand756f4ad2009-05-04 03:29:52 +0400454#ifdef CONFIG_X86_64
Ingo Molnar4d46a892008-02-21 04:24:40 +0100455 unsigned long fs;
Alexey Dobriyand756f4ad2009-05-04 03:29:52 +0400456#endif
Ingo Molnar4d46a892008-02-21 04:24:40 +0100457 unsigned long gs;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200458 /* Save middle states of ptrace breakpoints */
459 struct perf_event *ptrace_bps[HBP_NUM];
460 /* Debug status used for traps, single steps, etc... */
461 unsigned long debugreg6;
Frederic Weisbecker326264a2010-02-18 18:24:18 +0100462 /* Keep track of the exact dr7 value set by the user */
463 unsigned long ptrace_dr7;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100464 /* Fault info: */
465 unsigned long cr2;
466 unsigned long trap_no;
467 unsigned long error_code;
Suresh Siddha61c46282008-03-10 15:28:04 -0700468 /* floating point and extended processor state */
Avi Kivity86603282010-05-06 11:45:46 +0300469 struct fpu fpu;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100470#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100471 /* Virtual 86 mode info */
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100472 struct vm86_struct __user *vm86_info;
473 unsigned long screen_bitmap;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100474 unsigned long v86flags;
475 unsigned long v86mask;
476 unsigned long saved_sp0;
477 unsigned int saved_fs;
478 unsigned int saved_gs;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100479#endif
Ingo Molnar4d46a892008-02-21 04:24:40 +0100480 /* IO permissions: */
481 unsigned long *io_bitmap_ptr;
482 unsigned long iopl;
483 /* Max allowed port in the bitmap, in bytes: */
484 unsigned io_bitmap_max;
Glauber de Oliveira Costacb38d372008-01-30 13:31:31 +0100485};
486
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100487static inline unsigned long native_get_debugreg(int regno)
488{
Ingo Molnar4d46a892008-02-21 04:24:40 +0100489 unsigned long val = 0; /* Damn you, gcc! */
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100490
491 switch (regno) {
492 case 0:
Joe Perchescca2e6f2008-03-23 01:03:15 -0700493 asm("mov %%db0, %0" :"=r" (val));
494 break;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100495 case 1:
Joe Perchescca2e6f2008-03-23 01:03:15 -0700496 asm("mov %%db1, %0" :"=r" (val));
497 break;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100498 case 2:
Joe Perchescca2e6f2008-03-23 01:03:15 -0700499 asm("mov %%db2, %0" :"=r" (val));
500 break;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100501 case 3:
Joe Perchescca2e6f2008-03-23 01:03:15 -0700502 asm("mov %%db3, %0" :"=r" (val));
503 break;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100504 case 6:
Joe Perchescca2e6f2008-03-23 01:03:15 -0700505 asm("mov %%db6, %0" :"=r" (val));
506 break;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100507 case 7:
Joe Perchescca2e6f2008-03-23 01:03:15 -0700508 asm("mov %%db7, %0" :"=r" (val));
509 break;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100510 default:
511 BUG();
512 }
513 return val;
514}
515
516static inline void native_set_debugreg(int regno, unsigned long value)
517{
518 switch (regno) {
519 case 0:
Ingo Molnar4d46a892008-02-21 04:24:40 +0100520 asm("mov %0, %%db0" ::"r" (value));
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100521 break;
522 case 1:
Ingo Molnar4d46a892008-02-21 04:24:40 +0100523 asm("mov %0, %%db1" ::"r" (value));
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100524 break;
525 case 2:
Ingo Molnar4d46a892008-02-21 04:24:40 +0100526 asm("mov %0, %%db2" ::"r" (value));
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100527 break;
528 case 3:
Ingo Molnar4d46a892008-02-21 04:24:40 +0100529 asm("mov %0, %%db3" ::"r" (value));
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100530 break;
531 case 6:
Ingo Molnar4d46a892008-02-21 04:24:40 +0100532 asm("mov %0, %%db6" ::"r" (value));
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100533 break;
534 case 7:
Ingo Molnar4d46a892008-02-21 04:24:40 +0100535 asm("mov %0, %%db7" ::"r" (value));
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100536 break;
537 default:
538 BUG();
539 }
540}
541
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100542/*
543 * Set IOPL bits in EFLAGS from given mask
544 */
545static inline void native_set_iopl_mask(unsigned mask)
546{
547#ifdef CONFIG_X86_32
548 unsigned int reg;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100549
Joe Perchescca2e6f2008-03-23 01:03:15 -0700550 asm volatile ("pushfl;"
551 "popl %0;"
552 "andl %1, %0;"
553 "orl %2, %0;"
554 "pushl %0;"
555 "popfl"
556 : "=&r" (reg)
557 : "i" (~X86_EFLAGS_IOPL), "r" (mask));
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100558#endif
559}
560
Ingo Molnar4d46a892008-02-21 04:24:40 +0100561static inline void
562native_load_sp0(struct tss_struct *tss, struct thread_struct *thread)
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100563{
564 tss->x86_tss.sp0 = thread->sp0;
565#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100566 /* Only happens when SEP is enabled, no need to test "SEP"arately: */
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100567 if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
568 tss->x86_tss.ss1 = thread->sysenter_cs;
569 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
570 }
571#endif
572}
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100573
Glauber de Oliveira Costae801f862008-01-30 13:32:08 +0100574static inline void native_swapgs(void)
575{
576#ifdef CONFIG_X86_64
577 asm volatile("swapgs" ::: "memory");
578#endif
579}
580
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100581#ifdef CONFIG_PARAVIRT
582#include <asm/paravirt.h>
583#else
Ingo Molnar4d46a892008-02-21 04:24:40 +0100584#define __cpuid native_cpuid
585#define paravirt_enabled() 0
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100586
587/*
588 * These special macros can be used to get or set a debugging register
589 */
590#define get_debugreg(var, register) \
591 (var) = native_get_debugreg(register)
592#define set_debugreg(value, register) \
593 native_set_debugreg(register, value)
594
Joe Perchescca2e6f2008-03-23 01:03:15 -0700595static inline void load_sp0(struct tss_struct *tss,
596 struct thread_struct *thread)
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100597{
598 native_load_sp0(tss, thread);
599}
600
Glauber de Oliveira Costa62d7d7e2008-01-30 13:31:27 +0100601#define set_iopl_mask native_set_iopl_mask
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100602#endif /* CONFIG_PARAVIRT */
603
604/*
605 * Save the cr4 feature set we're using (ie
606 * Pentium 4MB enable and PPro Global page
607 * enable), so that any CPU's that boot up
608 * after us can get the correct flags.
609 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100610extern unsigned long mmu_cr4_features;
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100611
612static inline void set_in_cr4(unsigned long mask)
613{
Brian Gerst2df7a6e2010-09-03 21:17:08 -0400614 unsigned long cr4;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100615
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100616 mmu_cr4_features |= mask;
617 cr4 = read_cr4();
618 cr4 |= mask;
619 write_cr4(cr4);
620}
621
622static inline void clear_in_cr4(unsigned long mask)
623{
Brian Gerst2df7a6e2010-09-03 21:17:08 -0400624 unsigned long cr4;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100625
Glauber de Oliveira Costa1b46cbe2008-01-30 13:31:27 +0100626 mmu_cr4_features &= ~mask;
627 cr4 = read_cr4();
628 cr4 &= ~mask;
629 write_cr4(cr4);
630}
631
Glauber de Oliveira Costafc87e902008-01-30 13:31:38 +0100632typedef struct {
Ingo Molnar4d46a892008-02-21 04:24:40 +0100633 unsigned long seg;
Glauber de Oliveira Costafc87e902008-01-30 13:31:38 +0100634} mm_segment_t;
635
636
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100637/*
638 * create a kernel thread without removing it from tasklists
639 */
640extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
641
642/* Free all resources held by a thread. */
643extern void release_thread(struct task_struct *);
644
Ingo Molnar4d46a892008-02-21 04:24:40 +0100645/* Prepare to copy thread state - unlazy all lazy state */
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100646extern void prepare_to_copy(struct task_struct *tsk);
647
648unsigned long get_wchan(struct task_struct *p);
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100649
650/*
651 * Generic CPUID function
652 * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
653 * resulting in stale register contents being returned.
654 */
655static inline void cpuid(unsigned int op,
656 unsigned int *eax, unsigned int *ebx,
657 unsigned int *ecx, unsigned int *edx)
658{
659 *eax = op;
660 *ecx = 0;
661 __cpuid(eax, ebx, ecx, edx);
662}
663
664/* Some CPUID calls want 'count' to be placed in ecx */
665static inline void cpuid_count(unsigned int op, int count,
666 unsigned int *eax, unsigned int *ebx,
667 unsigned int *ecx, unsigned int *edx)
668{
669 *eax = op;
670 *ecx = count;
671 __cpuid(eax, ebx, ecx, edx);
672}
673
674/*
675 * CPUID functions returning a single datum
676 */
677static inline unsigned int cpuid_eax(unsigned int op)
678{
679 unsigned int eax, ebx, ecx, edx;
680
681 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100682
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100683 return eax;
684}
Ingo Molnar4d46a892008-02-21 04:24:40 +0100685
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100686static inline unsigned int cpuid_ebx(unsigned int op)
687{
688 unsigned int eax, ebx, ecx, edx;
689
690 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100691
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100692 return ebx;
693}
Ingo Molnar4d46a892008-02-21 04:24:40 +0100694
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100695static inline unsigned int cpuid_ecx(unsigned int op)
696{
697 unsigned int eax, ebx, ecx, edx;
698
699 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100700
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100701 return ecx;
702}
Ingo Molnar4d46a892008-02-21 04:24:40 +0100703
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100704static inline unsigned int cpuid_edx(unsigned int op)
705{
706 unsigned int eax, ebx, ecx, edx;
707
708 cpuid(op, &eax, &ebx, &ecx, &edx);
Ingo Molnar4d46a892008-02-21 04:24:40 +0100709
Glauber de Oliveira Costac758ecf2008-01-30 13:31:03 +0100710 return edx;
711}
712
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100713/* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
714static inline void rep_nop(void)
715{
Joe Perchescca2e6f2008-03-23 01:03:15 -0700716 asm volatile("rep; nop" ::: "memory");
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100717}
718
Ingo Molnar4d46a892008-02-21 04:24:40 +0100719static inline void cpu_relax(void)
720{
721 rep_nop();
722}
723
Ben Hutchings5367b682009-09-10 02:53:50 +0100724/* Stop speculative execution and prefetching of modified code. */
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100725static inline void sync_core(void)
726{
727 int tmp;
Ingo Molnar4d46a892008-02-21 04:24:40 +0100728
Ben Hutchings5367b682009-09-10 02:53:50 +0100729#if defined(CONFIG_M386) || defined(CONFIG_M486)
730 if (boot_cpu_data.x86 < 5)
731 /* There is no speculative execution.
732 * jmp is a barrier to prefetching. */
733 asm volatile("jmp 1f\n1:\n" ::: "memory");
734 else
735#endif
736 /* cpuid is a barrier to speculative execution.
737 * Prefetched instructions are automatically
738 * invalidated when modified. */
739 asm volatile("cpuid" : "=a" (tmp) : "0" (1)
740 : "ebx", "ecx", "edx", "memory");
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100741}
742
Joe Perchescca2e6f2008-03-23 01:03:15 -0700743static inline void __monitor(const void *eax, unsigned long ecx,
744 unsigned long edx)
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100745{
Ingo Molnar4d46a892008-02-21 04:24:40 +0100746 /* "monitor %eax, %ecx, %edx;" */
Joe Perchescca2e6f2008-03-23 01:03:15 -0700747 asm volatile(".byte 0x0f, 0x01, 0xc8;"
748 :: "a" (eax), "c" (ecx), "d"(edx));
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100749}
750
751static inline void __mwait(unsigned long eax, unsigned long ecx)
752{
Ingo Molnar4d46a892008-02-21 04:24:40 +0100753 /* "mwait %eax, %ecx;" */
Joe Perchescca2e6f2008-03-23 01:03:15 -0700754 asm volatile(".byte 0x0f, 0x01, 0xc9;"
755 :: "a" (eax), "c" (ecx));
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100756}
757
758static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
759{
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200760 trace_hardirqs_on();
Ingo Molnar4d46a892008-02-21 04:24:40 +0100761 /* "mwait %eax, %ecx;" */
Joe Perchescca2e6f2008-03-23 01:03:15 -0700762 asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
763 :: "a" (eax), "c" (ecx));
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100764}
765
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100766extern void select_idle_routine(const struct cpuinfo_x86 *c);
Len Brown02c68a02011-04-01 16:59:53 -0400767extern void init_amd_e400_c1e_mask(void);
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100768
Ingo Molnar4d46a892008-02-21 04:24:40 +0100769extern unsigned long boot_option_idle_override;
Len Brown02c68a02011-04-01 16:59:53 -0400770extern bool amd_e400_c1e_detected;
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100771
Thomas Renningerd1896042010-11-03 17:06:14 +0100772enum idle_boot_override {IDLE_NO_OVERRIDE=0, IDLE_HALT, IDLE_NOMWAIT,
773 IDLE_POLL, IDLE_FORCE_MWAIT};
774
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100775extern void enable_sep_cpu(void);
776extern int sysenter_setup(void);
777
Jan Kiszka29c84392010-05-20 21:04:29 -0500778extern void early_trap_init(void);
779
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100780/* Defined in head.S */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100781extern struct desc_ptr early_gdt_descr;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100782
783extern void cpu_set_gdt(int);
Brian Gerst552be872009-01-30 17:47:53 +0900784extern void switch_to_new_gdt(int);
Jeremy Fitzhardinge11e3a842009-01-30 17:47:54 +0900785extern void load_percpu_segment(int);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100786extern void cpu_init(void);
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100787
Markus Metzgerc2724772008-12-11 13:49:59 +0100788static inline unsigned long get_debugctlmsr(void)
789{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100790 unsigned long debugctlmsr = 0;
Markus Metzgerc2724772008-12-11 13:49:59 +0100791
792#ifndef CONFIG_X86_DEBUGCTLMSR
793 if (boot_cpu_data.x86 < 6)
794 return 0;
795#endif
796 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
797
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100798 return debugctlmsr;
Markus Metzgerc2724772008-12-11 13:49:59 +0100799}
800
Jan Beulich5b0e5082008-03-10 13:11:17 +0000801static inline void update_debugctlmsr(unsigned long debugctlmsr)
802{
803#ifndef CONFIG_X86_DEBUGCTLMSR
804 if (boot_cpu_data.x86 < 6)
805 return;
806#endif
807 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
808}
809
Ingo Molnar4d46a892008-02-21 04:24:40 +0100810/*
811 * from system description table in BIOS. Mostly for MCA use, but
812 * others may find it useful:
813 */
814extern unsigned int machine_id;
815extern unsigned int machine_submodel_id;
816extern unsigned int BIOS_revision;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100817
Ingo Molnar4d46a892008-02-21 04:24:40 +0100818/* Boot loader type from the setup header: */
819extern int bootloader_type;
H. Peter Anvin50312962009-05-07 16:54:11 -0700820extern int bootloader_version;
Glauber de Oliveira Costa1a539052008-01-30 13:31:39 +0100821
Ingo Molnar4d46a892008-02-21 04:24:40 +0100822extern char ignore_fpu_irq;
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100823
824#define HAVE_ARCH_PICK_MMAP_LAYOUT 1
825#define ARCH_HAS_PREFETCHW
826#define ARCH_HAS_SPINLOCK_PREFETCH
827
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100828#ifdef CONFIG_X86_32
Ingo Molnar4d46a892008-02-21 04:24:40 +0100829# define BASE_PREFETCH ASM_NOP4
830# define ARCH_HAS_PREFETCH
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100831#else
Ingo Molnar4d46a892008-02-21 04:24:40 +0100832# define BASE_PREFETCH "prefetcht0 (%1)"
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100833#endif
834
Ingo Molnar4d46a892008-02-21 04:24:40 +0100835/*
836 * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
837 *
838 * It's not worth to care about 3dnow prefetches for the K6
839 * because they are microcoded there and very slow.
840 */
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100841static inline void prefetch(const void *x)
842{
843 alternative_input(BASE_PREFETCH,
844 "prefetchnta (%1)",
845 X86_FEATURE_XMM,
846 "r" (x));
847}
848
Ingo Molnar4d46a892008-02-21 04:24:40 +0100849/*
850 * 3dnow prefetch to get an exclusive cache line.
851 * Useful for spinlocks to avoid one state transition in the
852 * cache coherency protocol:
853 */
Glauber de Oliveira Costaae2e15e2008-01-30 13:31:40 +0100854static inline void prefetchw(const void *x)
855{
856 alternative_input(BASE_PREFETCH,
857 "prefetchw (%1)",
858 X86_FEATURE_3DNOW,
859 "r" (x));
860}
861
Ingo Molnar4d46a892008-02-21 04:24:40 +0100862static inline void spin_lock_prefetch(const void *x)
863{
864 prefetchw(x);
865}
866
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100867#ifdef CONFIG_X86_32
868/*
869 * User space process size: 3GB (default).
870 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100871#define TASK_SIZE PAGE_OFFSET
Ingo Molnard9517342009-02-20 23:32:28 +0100872#define TASK_SIZE_MAX TASK_SIZE
Ingo Molnar4d46a892008-02-21 04:24:40 +0100873#define STACK_TOP TASK_SIZE
874#define STACK_TOP_MAX STACK_TOP
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100875
Ingo Molnar4d46a892008-02-21 04:24:40 +0100876#define INIT_THREAD { \
877 .sp0 = sizeof(init_stack) + (long)&init_stack, \
878 .vm86_info = NULL, \
879 .sysenter_cs = __KERNEL_CS, \
880 .io_bitmap_ptr = NULL, \
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100881}
882
883/*
884 * Note that the .io_bitmap member must be extra-big. This is because
885 * the CPU will access an additional byte beyond the end of the IO
886 * permission bitmap. The extra byte must be all 1 bits, and must
887 * be within the limit.
888 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100889#define INIT_TSS { \
890 .x86_tss = { \
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100891 .sp0 = sizeof(init_stack) + (long)&init_stack, \
Ingo Molnar4d46a892008-02-21 04:24:40 +0100892 .ss0 = __KERNEL_DS, \
893 .ss1 = __KERNEL_CS, \
894 .io_bitmap_base = INVALID_IO_BITMAP_OFFSET, \
895 }, \
896 .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 }, \
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100897}
898
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100899extern unsigned long thread_saved_pc(struct task_struct *tsk);
900
901#define THREAD_SIZE_LONGS (THREAD_SIZE/sizeof(unsigned long))
902#define KSTK_TOP(info) \
903({ \
904 unsigned long *__ptr = (unsigned long *)(info); \
905 (unsigned long)(&__ptr[THREAD_SIZE_LONGS]); \
906})
907
908/*
909 * The below -8 is to reserve 8 bytes on top of the ring0 stack.
910 * This is necessary to guarantee that the entire "struct pt_regs"
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400911 * is accessible even if the CPU haven't stored the SS/ESP registers
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100912 * on the stack (interrupt gate does not save these registers
913 * when switching to the same priv ring).
914 * Therefore beware: accessing the ss/esp fields of the
915 * "struct pt_regs" is possible, but they may contain the
916 * completely wrong values.
917 */
918#define task_pt_regs(task) \
919({ \
920 struct pt_regs *__regs__; \
921 __regs__ = (struct pt_regs *)(KSTK_TOP(task_stack_page(task))-8); \
922 __regs__ - 1; \
923})
924
Ingo Molnar4d46a892008-02-21 04:24:40 +0100925#define KSTK_ESP(task) (task_pt_regs(task)->sp)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100926
927#else
928/*
929 * User space process size. 47bits minus one guard page.
930 */
Ingo Molnard9517342009-02-20 23:32:28 +0100931#define TASK_SIZE_MAX ((1UL << 47) - PAGE_SIZE)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100932
933/* This decides where the kernel will search for a free chunk of vm
934 * space during mmap's.
935 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100936#define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? \
937 0xc0000000 : 0xFFFFe000)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100938
Ingo Molnar4d46a892008-02-21 04:24:40 +0100939#define TASK_SIZE (test_thread_flag(TIF_IA32) ? \
Ingo Molnard9517342009-02-20 23:32:28 +0100940 IA32_PAGE_OFFSET : TASK_SIZE_MAX)
Ingo Molnar4d46a892008-02-21 04:24:40 +0100941#define TASK_SIZE_OF(child) ((test_tsk_thread_flag(child, TIF_IA32)) ? \
Ingo Molnard9517342009-02-20 23:32:28 +0100942 IA32_PAGE_OFFSET : TASK_SIZE_MAX)
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100943
David Howells922a70d2008-02-08 04:19:26 -0800944#define STACK_TOP TASK_SIZE
Ingo Molnard9517342009-02-20 23:32:28 +0100945#define STACK_TOP_MAX TASK_SIZE_MAX
David Howells922a70d2008-02-08 04:19:26 -0800946
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100947#define INIT_THREAD { \
948 .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
949}
950
951#define INIT_TSS { \
952 .x86_tss.sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
953}
954
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100955/*
956 * Return saved PC of a blocked thread.
957 * What is this good for? it will be always the scheduler or ret_from_fork.
958 */
Ingo Molnar4d46a892008-02-21 04:24:40 +0100959#define thread_saved_pc(t) (*(unsigned long *)((t)->thread.sp - 8))
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100960
Ingo Molnar4d46a892008-02-21 04:24:40 +0100961#define task_pt_regs(tsk) ((struct pt_regs *)(tsk)->thread.sp0 - 1)
Stefani Seibold89240ba2009-11-03 10:22:40 +0100962extern unsigned long KSTK_ESP(struct task_struct *task);
Glauber de Oliveira Costa2f66dcc2008-01-30 13:31:57 +0100963#endif /* CONFIG_X86_64 */
964
Ingo Molnar513ad842008-02-21 05:18:40 +0100965extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
966 unsigned long new_sp);
967
Ingo Molnar4d46a892008-02-21 04:24:40 +0100968/*
969 * This decides where the kernel will search for a free chunk of vm
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100970 * space during mmap's.
971 */
972#define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
973
Ingo Molnar4d46a892008-02-21 04:24:40 +0100974#define KSTK_EIP(task) (task_pt_regs(task)->ip)
Glauber de Oliveira Costa683e0252008-01-30 13:31:27 +0100975
Erik Bosman529e25f2008-04-14 00:24:18 +0200976/* Get/set a process' ability to use the timestamp counter instruction */
977#define GET_TSC_CTL(adr) get_tsc_mode((adr))
978#define SET_TSC_CTL(val) set_tsc_mode((val))
979
980extern int get_tsc_mode(unsigned long adr);
981extern int set_tsc_mode(unsigned int val);
982
Andreas Herrmann6a812692009-09-16 11:33:40 +0200983extern int amd_get_nb_id(int cpu);
984
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200985struct aperfmperf {
986 u64 aperf, mperf;
987};
988
989static inline void get_aperfmperf(struct aperfmperf *am)
990{
991 WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_APERFMPERF));
992
993 rdmsrl(MSR_IA32_APERF, am->aperf);
994 rdmsrl(MSR_IA32_MPERF, am->mperf);
995}
996
997#define APERFMPERF_SHIFT 10
998
999static inline
1000unsigned long calc_aperfmperf_ratio(struct aperfmperf *old,
1001 struct aperfmperf *new)
1002{
1003 u64 aperf = new->aperf - old->aperf;
1004 u64 mperf = new->mperf - old->mperf;
1005 unsigned long ratio = aperf;
1006
1007 mperf >>= APERFMPERF_SHIFT;
1008 if (mperf)
1009 ratio = div64_u64(aperf, mperf);
1010
1011 return ratio;
1012}
1013
Hans Rosenfeldd78d6712010-07-28 19:09:30 +02001014/*
1015 * AMD errata checking
1016 */
1017#ifdef CONFIG_CPU_SUP_AMD
Hans Rosenfeld1be85a62010-07-28 19:09:32 +02001018extern const int amd_erratum_383[];
Hans Rosenfeld9d8888c2010-07-28 19:09:31 +02001019extern const int amd_erratum_400[];
Hans Rosenfeldd78d6712010-07-28 19:09:30 +02001020extern bool cpu_has_amd_erratum(const int *);
1021
1022#define AMD_LEGACY_ERRATUM(...) { -1, __VA_ARGS__, 0 }
1023#define AMD_OSVW_ERRATUM(osvw_id, ...) { osvw_id, __VA_ARGS__, 0 }
1024#define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end) \
1025 ((f << 24) | (m_start << 16) | (s_start << 12) | (m_end << 4) | (s_end))
1026#define AMD_MODEL_RANGE_FAMILY(range) (((range) >> 24) & 0xff)
1027#define AMD_MODEL_RANGE_START(range) (((range) >> 12) & 0xfff)
1028#define AMD_MODEL_RANGE_END(range) ((range) & 0xfff)
1029
1030#else
1031#define cpu_has_amd_erratum(x) (false)
1032#endif /* CONFIG_CPU_SUP_AMD */
1033
David Howellsf05e7982012-03-28 18:11:12 +01001034#ifdef CONFIG_X86_32
1035/*
1036 * disable hlt during certain critical i/o operations
1037 */
1038#define HAVE_DISABLE_HLT
1039#endif
1040
1041void disable_hlt(void);
1042void enable_hlt(void);
1043
1044void cpu_idle_wait(void);
1045
1046extern unsigned long arch_align_stack(unsigned long sp);
1047extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
1048
1049void default_idle(void);
1050bool set_pm_idle_to_default(void);
1051
1052void stop_this_cpu(void *dummy);
1053
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001054#endif /* _ASM_X86_PROCESSOR_H */