- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / include / mach-xen / asm / processor.h
1 #ifndef _ASM_X86_PROCESSOR_H
2 #define _ASM_X86_PROCESSOR_H
3
4 #include <asm/processor-flags.h>
5
6 /* Forward declaration, a strange C thing */
7 struct task_struct;
8 struct mm_struct;
9
10 #include <asm/vm86.h>
11 #include <asm/math_emu.h>
12 #include <asm/segment.h>
13 #include <asm/types.h>
14 #include <asm/sigcontext.h>
15 #include <asm/current.h>
16 #include <asm/cpufeature.h>
17 #include <asm/system.h>
18 #include <asm/page.h>
19 #include <asm/pgtable_types.h>
20 #include <asm/percpu.h>
21 #include <asm/msr.h>
22 #include <asm/desc_defs.h>
23 #include <asm/nops.h>
24
25 #include <linux/personality.h>
26 #include <linux/cpumask.h>
27 #include <linux/cache.h>
28 #include <linux/threads.h>
29 #include <linux/math64.h>
30 #include <linux/init.h>
31 #include <linux/err.h>
32
33 #include <xen/interface/physdev.h>
34
35 #define HBP_NUM 4
36 /*
37  * Default implementation of macro that returns current
38  * instruction pointer ("program counter").
39  */
40 static inline void *current_text_addr(void)
41 {
42         void *pc;
43
44         asm volatile("mov $1f, %0; 1:":"=r" (pc));
45
46         return pc;
47 }
48
49 #ifdef CONFIG_X86_VSMP
50 # define ARCH_MIN_TASKALIGN             (1 << INTERNODE_CACHE_SHIFT)
51 # define ARCH_MIN_MMSTRUCT_ALIGN        (1 << INTERNODE_CACHE_SHIFT)
52 #else
53 # define ARCH_MIN_TASKALIGN             16
54 # define ARCH_MIN_MMSTRUCT_ALIGN        0
55 #endif
56
57 /*
58  *  CPU type and hardware bug flags. Kept separately for each CPU.
59  *  Members of this structure are referenced in head.S, so think twice
60  *  before touching them. [mj]
61  */
62
63 struct cpuinfo_x86 {
64         __u8                    x86;            /* CPU family */
65         __u8                    x86_vendor;     /* CPU vendor */
66         __u8                    x86_model;
67         __u8                    x86_mask;
68 #ifdef CONFIG_X86_32
69         char                    wp_works_ok;    /* It doesn't on 386's */
70
71         /* Problems on some 486Dx4's and old 386's: */
72 #ifndef CONFIG_XEN
73         char                    hlt_works_ok;
74 #endif
75         char                    hard_math;
76 #ifndef CONFIG_XEN
77         char                    rfu;
78         char                    fdiv_bug;
79         char                    f00f_bug;
80         char                    coma_bug;
81         char                    pad0;
82 #endif
83 #else
84         /* Number of 4K pages in DTLB/ITLB combined(in pages): */
85         int                     x86_tlbsize;
86 #endif
87         __u8                    x86_virt_bits;
88         __u8                    x86_phys_bits;
89 #ifndef CONFIG_XEN
90         /* CPUID returned core id bits: */
91         __u8                    x86_coreid_bits;
92 #endif
93         /* Max extended CPUID function supported: */
94         __u32                   extended_cpuid_level;
95         /* Maximum supported CPUID level, -1=no CPUID: */
96         int                     cpuid_level;
97         __u32                   x86_capability[NCAPINTS];
98         char                    x86_vendor_id[16];
99         char                    x86_model_id[64];
100         /* in KB - valid for CPUS which support this call: */
101         int                     x86_cache_size;
102         int                     x86_cache_alignment;    /* In bytes */
103         int                     x86_power;
104         unsigned long           loops_per_jiffy;
105 #ifndef CONFIG_XEN
106         /* cpuid returned max cores value: */
107         u16                      x86_max_cores;
108         u16                     apicid;
109         u16                     initial_apicid;
110 #endif
111         u16                     x86_clflush_size;
112 #ifndef CONFIG_XEN
113         /* number of cores as seen by the OS: */
114         u16                     booted_cores;
115         /* Physical processor id: */
116         u16                     phys_proc_id;
117         /* Core id: */
118         u16                     cpu_core_id;
119         /* Compute unit id */
120         u8                      compute_unit_id;
121 #endif
122         /* Index into per_cpu list: */
123         u16                     cpu_index;
124 #ifndef CONFIG_XEN
125         u32                     microcode;
126 #endif
127 } __attribute__((__aligned__(SMP_CACHE_BYTES)));
128
129 #define X86_VENDOR_INTEL        0
130 #define X86_VENDOR_CYRIX        1
131 #define X86_VENDOR_AMD          2
132 #define X86_VENDOR_UMC          3
133 #define X86_VENDOR_CENTAUR      5
134 #define X86_VENDOR_TRANSMETA    7
135 #define X86_VENDOR_NSC          8
136 #define X86_VENDOR_NUM          9
137
138 #define X86_VENDOR_UNKNOWN      0xff
139
140 /*
141  * capabilities of CPUs
142  */
143 extern struct cpuinfo_x86       boot_cpu_data;
144 extern struct cpuinfo_x86       new_cpu_data;
145
146 extern __u32                    cpu_caps_cleared[NCAPINTS];
147 extern __u32                    cpu_caps_set[NCAPINTS];
148
149 #ifdef CONFIG_SMP
150 DECLARE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
151 #define cpu_data(cpu)           per_cpu(cpu_info, cpu)
152 #else
153 #define cpu_info                boot_cpu_data
154 #define cpu_data(cpu)           boot_cpu_data
155 #endif
156
157 extern const struct seq_operations cpuinfo_op;
158
159 static inline int hlt_works(int cpu)
160 {
161 #if defined(CONFIG_X86_32) && !defined(CONFIG_XEN)
162         return cpu_data(cpu).hlt_works_ok;
163 #else
164         return 1;
165 #endif
166 }
167
168 #define cache_line_size()       (boot_cpu_data.x86_cache_alignment)
169
170 extern void cpu_detect(struct cpuinfo_x86 *c);
171
172 extern struct pt_regs *idle_regs(struct pt_regs *);
173
174 extern void early_cpu_init(void);
175 extern void identify_boot_cpu(void);
176 extern void identify_secondary_cpu(struct cpuinfo_x86 *);
177 extern void print_cpu_info(struct cpuinfo_x86 *);
178 extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
179 extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
180 extern unsigned short num_cache_leaves;
181
182 extern void detect_extended_topology(struct cpuinfo_x86 *c);
183 extern void detect_ht(struct cpuinfo_x86 *c);
184
185 static inline void xen_cpuid(unsigned int *eax, unsigned int *ebx,
186                              unsigned int *ecx, unsigned int *edx)
187 {
188         /* ecx is often an input as well as an output. */
189         asm volatile(XEN_CPUID
190             : "=a" (*eax),
191               "=b" (*ebx),
192               "=c" (*ecx),
193               "=d" (*edx)
194             : "0" (*eax), "2" (*ecx)
195             : "memory");
196 }
197
198 static inline void load_cr3(pgd_t *pgdir)
199 {
200         write_cr3(__pa(pgdir));
201 }
202
203 #ifndef CONFIG_X86_NO_TSS
204 #ifdef CONFIG_X86_32
205 /* This is the TSS defined by the hardware. */
206 struct x86_hw_tss {
207         unsigned short          back_link, __blh;
208         unsigned long           sp0;
209         unsigned short          ss0, __ss0h;
210         unsigned long           sp1;
211         /* ss1 caches MSR_IA32_SYSENTER_CS: */
212         unsigned short          ss1, __ss1h;
213         unsigned long           sp2;
214         unsigned short          ss2, __ss2h;
215         unsigned long           __cr3;
216         unsigned long           ip;
217         unsigned long           flags;
218         unsigned long           ax;
219         unsigned long           cx;
220         unsigned long           dx;
221         unsigned long           bx;
222         unsigned long           sp;
223         unsigned long           bp;
224         unsigned long           si;
225         unsigned long           di;
226         unsigned short          es, __esh;
227         unsigned short          cs, __csh;
228         unsigned short          ss, __ssh;
229         unsigned short          ds, __dsh;
230         unsigned short          fs, __fsh;
231         unsigned short          gs, __gsh;
232         unsigned short          ldt, __ldth;
233         unsigned short          trace;
234         unsigned short          io_bitmap_base;
235
236 } __attribute__((packed));
237 extern struct tss_struct doublefault_tss;
238 #else
239 struct x86_hw_tss {
240         u32                     reserved1;
241         u64                     sp0;
242         u64                     sp1;
243         u64                     sp2;
244         u64                     reserved2;
245         u64                     ist[7];
246         u32                     reserved3;
247         u32                     reserved4;
248         u16                     reserved5;
249         u16                     io_bitmap_base;
250
251 } __attribute__((packed)) ____cacheline_aligned;
252 #endif
253 #endif /* CONFIG_X86_NO_TSS */
254
255 /*
256  * IO-bitmap sizes:
257  */
258 #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
263
264 #ifndef CONFIG_X86_NO_TSS
265 struct tss_struct {
266         /*
267          * The hardware state:
268          */
269         struct x86_hw_tss       x86_tss;
270
271         /*
272          * The extra 1 is there because the CPU will access an
273          * additional byte beyond the end of the IO permission
274          * bitmap. The extra byte must be all 1 bits, and must
275          * be within the limit.
276          */
277         unsigned long           io_bitmap[IO_BITMAP_LONGS + 1];
278
279         /*
280          * .. and then another 0x100 bytes for the emergency kernel stack:
281          */
282         unsigned long           stack[64];
283
284 } ____cacheline_aligned;
285
286 DECLARE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss);
287
288 /*
289  * Save the original ist values for checking stack pointers during debugging
290  */
291 struct orig_ist {
292         unsigned long           ist[7];
293 };
294 #endif /* CONFIG_X86_NO_TSS */
295
296 #define MXCSR_DEFAULT           0x1f80
297
298 struct i387_fsave_struct {
299         u32                     cwd;    /* FPU Control Word             */
300         u32                     swd;    /* FPU Status Word              */
301         u32                     twd;    /* FPU Tag Word                 */
302         u32                     fip;    /* FPU IP Offset                */
303         u32                     fcs;    /* FPU IP Selector              */
304         u32                     foo;    /* FPU Operand Pointer Offset   */
305         u32                     fos;    /* FPU Operand Pointer Selector */
306
307         /* 8*10 bytes for each FP-reg = 80 bytes:                       */
308         u32                     st_space[20];
309
310         /* Software status information [not touched by FSAVE ]:         */
311         u32                     status;
312 };
313
314 struct i387_fxsave_struct {
315         u16                     cwd; /* Control Word                    */
316         u16                     swd; /* Status Word                     */
317         u16                     twd; /* Tag Word                        */
318         u16                     fop; /* Last Instruction Opcode         */
319         union {
320                 struct {
321                         u64     rip; /* Instruction Pointer             */
322                         u64     rdp; /* Data Pointer                    */
323                 };
324                 struct {
325                         u32     fip; /* FPU IP Offset                   */
326                         u32     fcs; /* FPU IP Selector                 */
327                         u32     foo; /* FPU Operand Offset              */
328                         u32     fos; /* FPU Operand Selector            */
329                 };
330         };
331         u32                     mxcsr;          /* MXCSR Register State */
332         u32                     mxcsr_mask;     /* MXCSR Mask           */
333
334         /* 8*16 bytes for each FP-reg = 128 bytes:                      */
335         u32                     st_space[32];
336
337         /* 16*16 bytes for each XMM-reg = 256 bytes:                    */
338         u32                     xmm_space[64];
339
340         u32                     padding[12];
341
342         union {
343                 u32             padding1[12];
344                 u32             sw_reserved[12];
345         };
346
347 } __attribute__((aligned(16)));
348
349 struct i387_soft_struct {
350         u32                     cwd;
351         u32                     swd;
352         u32                     twd;
353         u32                     fip;
354         u32                     fcs;
355         u32                     foo;
356         u32                     fos;
357         /* 8*10 bytes for each FP-reg = 80 bytes: */
358         u32                     st_space[20];
359         u8                      ftop;
360         u8                      changed;
361         u8                      lookahead;
362         u8                      no_update;
363         u8                      rm;
364         u8                      alimit;
365         struct math_emu_info    *info;
366         u32                     entry_eip;
367 };
368
369 struct ymmh_struct {
370         /* 16 * 16 bytes for each YMMH-reg = 256 bytes */
371         u32 ymmh_space[64];
372 };
373
374 struct xsave_hdr_struct {
375         u64 xstate_bv;
376         u64 reserved1[2];
377         u64 reserved2[5];
378 } __attribute__((packed));
379
380 struct xsave_struct {
381         struct i387_fxsave_struct i387;
382         struct xsave_hdr_struct xsave_hdr;
383         struct ymmh_struct ymmh;
384         /* new processor state extensions will go here */
385 } __attribute__ ((packed, aligned (64)));
386
387 union thread_xstate {
388         struct i387_fsave_struct        fsave;
389         struct i387_fxsave_struct       fxsave;
390         struct i387_soft_struct         soft;
391         struct xsave_struct             xsave;
392 };
393
394 struct fpu {
395         unsigned int last_cpu;
396         unsigned int has_fpu;
397         union thread_xstate *state;
398 };
399
400 #ifdef CONFIG_X86_64
401 #ifndef CONFIG_X86_NO_TSS
402 DECLARE_PER_CPU(struct orig_ist, orig_ist);
403 #endif
404
405 union irq_stack_union {
406         char irq_stack[IRQ_STACK_SIZE];
407         /*
408          * GCC hardcodes the stack canary as %gs:40.  Since the
409          * irq_stack is the object at %gs:0, we reserve the bottom
410          * 48 bytes of the irq stack for the canary.
411          */
412         struct {
413                 char gs_base[40];
414                 unsigned long stack_canary;
415         };
416 };
417
418 DECLARE_PER_CPU_FIRST(union irq_stack_union, irq_stack_union);
419 DECLARE_INIT_PER_CPU(irq_stack_union);
420
421 DECLARE_PER_CPU(char *, irq_stack_ptr);
422 DECLARE_PER_CPU(unsigned int, irq_count);
423 extern unsigned long kernel_eflags;
424 extern asmlinkage void ignore_sysret(void);
425 #else   /* X86_64 */
426 #ifdef CONFIG_CC_STACKPROTECTOR
427 /*
428  * Make sure stack canary segment base is cached-aligned:
429  *   "For Intel Atom processors, avoid non zero segment base address
430  *    that is not aligned to cache line boundary at all cost."
431  * (Optim Ref Manual Assembly/Compiler Coding Rule 15.)
432  */
433 struct stack_canary {
434         char __pad[20];         /* canary at %gs:20 */
435         unsigned long canary;
436 };
437 DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
438 #endif
439 #endif  /* X86_64 */
440
441 extern unsigned int xstate_size;
442 extern void free_thread_xstate(struct task_struct *);
443 extern struct kmem_cache *task_xstate_cachep;
444
445 struct perf_event;
446
447 struct thread_struct {
448         /* Cached TLS descriptors: */
449         struct desc_struct      tls_array[GDT_ENTRY_TLS_ENTRIES];
450         unsigned long           sp0;
451         unsigned long           sp;
452 #ifdef CONFIG_X86_32
453         unsigned long           sysenter_cs;
454 #else
455         unsigned short          es;
456         unsigned short          ds;
457         unsigned short          fsindex;
458         unsigned short          gsindex;
459 #endif
460 #ifdef CONFIG_X86_32
461         unsigned long           ip;
462 #endif
463 #ifdef CONFIG_X86_64
464         unsigned long           fs;
465 #endif
466         unsigned long           gs;
467         /* Save middle states of ptrace breakpoints */
468         struct perf_event       *ptrace_bps[HBP_NUM];
469         /* Debug status used for traps, single steps, etc... */
470         unsigned long           debugreg6;
471         /* Keep track of the exact dr7 value set by the user */
472         unsigned long           ptrace_dr7;
473         /* Fault info: */
474         unsigned long           cr2;
475         unsigned long           trap_no;
476         unsigned long           error_code;
477         /* floating point and extended processor state */
478         struct fpu              fpu;
479 #ifdef CONFIG_X86_32
480         /* Virtual 86 mode info */
481         struct vm86_struct __user *vm86_info;
482         unsigned long           screen_bitmap;
483         unsigned long           v86flags, v86mask, saved_sp0;
484         unsigned int            saved_fs, saved_gs;
485 #endif
486         /* IO permissions: */
487         unsigned long           *io_bitmap_ptr;
488         unsigned long           iopl;
489         /* Max allowed port in the bitmap, in bytes: */
490         unsigned                io_bitmap_max;
491 };
492
493 static inline unsigned long xen_get_debugreg(int regno)
494 {
495         return HYPERVISOR_get_debugreg(regno);
496 }
497
498 static inline void xen_set_debugreg(int regno, unsigned long value)
499 {
500         WARN_ON(HYPERVISOR_set_debugreg(regno, value));
501 }
502
503 /*
504  * Set IOPL bits in EFLAGS from given mask
505  */
506 static inline void xen_set_iopl_mask(unsigned mask)
507 {
508         struct physdev_set_iopl set_iopl;
509
510         /* Force the change at ring 0. */
511         set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
512         WARN_ON(HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl));
513 }
514
515 #ifndef CONFIG_X86_NO_TSS
516 static inline void
517 native_load_sp0(struct tss_struct *tss, struct thread_struct *thread)
518 {
519         tss->x86_tss.sp0 = thread->sp0;
520 #ifdef CONFIG_X86_32
521         /* Only happens when SEP is enabled, no need to test "SEP"arately: */
522         if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
523                 tss->x86_tss.ss1 = thread->sysenter_cs;
524                 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
525         }
526 #endif
527 }
528 #else
529 #define xen_load_sp0(tss, thread) do { \
530         if (HYPERVISOR_stack_switch(__KERNEL_DS, (thread)->sp0)) \
531                 BUG(); \
532 } while (0)
533 #endif
534
535 #define __cpuid                 xen_cpuid
536 #define paravirt_enabled()      1
537
538 /*
539  * These special macros can be used to get or set a debugging register
540  */
541 #define get_debugreg(var, register)                             \
542         (var) = xen_get_debugreg(register)
543 #define set_debugreg(value, register)                           \
544         xen_set_debugreg(register, value)
545
546 #define load_sp0 xen_load_sp0
547
548 #define set_iopl_mask xen_set_iopl_mask
549
550 /*
551  * Save the cr4 feature set we're using (ie
552  * Pentium 4MB enable and PPro Global page
553  * enable), so that any CPU's that boot up
554  * after us can get the correct flags.
555  */
556 extern unsigned long            mmu_cr4_features;
557
558 static inline void set_in_cr4(unsigned long mask)
559 {
560         unsigned long cr4;
561
562         mmu_cr4_features |= mask;
563         cr4 = read_cr4();
564         cr4 |= mask;
565         write_cr4(cr4);
566 }
567
568 static inline void clear_in_cr4(unsigned long mask)
569 {
570         unsigned long cr4;
571
572         mmu_cr4_features &= ~mask;
573         cr4 = read_cr4();
574         cr4 &= ~mask;
575         write_cr4(cr4);
576 }
577
578 typedef struct {
579         unsigned long           seg;
580 } mm_segment_t;
581
582
583 /*
584  * create a kernel thread without removing it from tasklists
585  */
586 extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
587
588 /* Free all resources held by a thread. */
589 extern void release_thread(struct task_struct *);
590
591 /* Prepare to copy thread state - unlazy all lazy state */
592 extern void prepare_to_copy(struct task_struct *tsk);
593
594 unsigned long get_wchan(struct task_struct *p);
595
596 /*
597  * Generic CPUID function
598  * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
599  * resulting in stale register contents being returned.
600  */
601 static inline void cpuid(unsigned int op,
602                          unsigned int *eax, unsigned int *ebx,
603                          unsigned int *ecx, unsigned int *edx)
604 {
605         *eax = op;
606         *ecx = 0;
607         __cpuid(eax, ebx, ecx, edx);
608 }
609
610 /* Some CPUID calls want 'count' to be placed in ecx */
611 static inline void cpuid_count(unsigned int op, int count,
612                                unsigned int *eax, unsigned int *ebx,
613                                unsigned int *ecx, unsigned int *edx)
614 {
615         *eax = op;
616         *ecx = count;
617         __cpuid(eax, ebx, ecx, edx);
618 }
619
620 /*
621  * CPUID functions returning a single datum
622  */
623 static inline unsigned int cpuid_eax(unsigned int op)
624 {
625         unsigned int eax, ebx, ecx, edx;
626
627         cpuid(op, &eax, &ebx, &ecx, &edx);
628
629         return eax;
630 }
631
632 static inline unsigned int cpuid_ebx(unsigned int op)
633 {
634         unsigned int eax, ebx, ecx, edx;
635
636         cpuid(op, &eax, &ebx, &ecx, &edx);
637
638         return ebx;
639 }
640
641 static inline unsigned int cpuid_ecx(unsigned int op)
642 {
643         unsigned int eax, ebx, ecx, edx;
644
645         cpuid(op, &eax, &ebx, &ecx, &edx);
646
647         return ecx;
648 }
649
650 static inline unsigned int cpuid_edx(unsigned int op)
651 {
652         unsigned int eax, ebx, ecx, edx;
653
654         cpuid(op, &eax, &ebx, &ecx, &edx);
655
656         return edx;
657 }
658
659 /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
660 static inline void rep_nop(void)
661 {
662         asm volatile("rep; nop" ::: "memory");
663 }
664
665 static inline void cpu_relax(void)
666 {
667         rep_nop();
668 }
669
670 /* Stop speculative execution and prefetching of modified code. */
671 static inline void sync_core(void)
672 {
673         int tmp;
674
675 #if defined(CONFIG_M386) || defined(CONFIG_M486)
676         if (boot_cpu_data.x86 < 5)
677                 /* There is no speculative execution.
678                  * jmp is a barrier to prefetching. */
679                 asm volatile("jmp 1f\n1:\n" ::: "memory");
680         else
681 #endif
682                 /* cpuid is a barrier to speculative execution.
683                  * Prefetched instructions are automatically
684                  * invalidated when modified. */
685                 asm volatile("cpuid" : "=a" (tmp) : "0" (1)
686                              : "ebx", "ecx", "edx", "memory");
687 }
688
689 static inline void __monitor(const void *eax, unsigned long ecx,
690                              unsigned long edx)
691 {
692         /* "monitor %eax, %ecx, %edx;" */
693         asm volatile(".byte 0x0f, 0x01, 0xc8;"
694                      :: "a" (eax), "c" (ecx), "d"(edx));
695 }
696
697 static inline void __mwait(unsigned long eax, unsigned long ecx)
698 {
699         /* "mwait %eax, %ecx;" */
700         asm volatile(".byte 0x0f, 0x01, 0xc9;"
701                      :: "a" (eax), "c" (ecx));
702 }
703
704 static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
705 {
706         trace_hardirqs_on();
707         /* "mwait %eax, %ecx;" */
708         asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
709                      :: "a" (eax), "c" (ecx));
710 }
711
712 extern void select_idle_routine(const struct cpuinfo_x86 *c);
713 extern void init_amd_e400_c1e_mask(void);
714
715 extern unsigned long            boot_option_idle_override;
716 extern bool                     amd_e400_c1e_detected;
717
718 enum idle_boot_override {IDLE_NO_OVERRIDE=0, IDLE_HALT, IDLE_NOMWAIT,
719                          IDLE_POLL, IDLE_FORCE_MWAIT};
720
721 extern void enable_sep_cpu(void);
722 extern int sysenter_setup(void);
723
724 extern void early_trap_init(void);
725
726 /* Defined in head.S */
727 extern struct desc_ptr          early_gdt_descr;
728
729 extern void cpu_set_gdt(int);
730 extern void switch_to_new_gdt(int);
731 extern void load_percpu_segment(int);
732 extern void cpu_init(void);
733
734 static inline unsigned long get_debugctlmsr(void)
735 {
736         unsigned long debugctlmsr = 0;
737
738 #ifndef CONFIG_X86_DEBUGCTLMSR
739         if (boot_cpu_data.x86 < 6)
740                 return 0;
741 #endif
742         rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
743
744         return debugctlmsr;
745 }
746
747 static inline void update_debugctlmsr(unsigned long debugctlmsr)
748 {
749 #ifndef CONFIG_X86_DEBUGCTLMSR
750         if (boot_cpu_data.x86 < 6)
751                 return;
752 #endif
753         wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
754 }
755
756 /*
757  * from system description table in BIOS. Mostly for MCA use, but
758  * others may find it useful:
759  */
760 extern unsigned int             machine_id;
761 extern unsigned int             machine_submodel_id;
762 extern unsigned int             BIOS_revision;
763
764 /* Boot loader type from the setup header: */
765 extern int                      bootloader_type;
766 extern int                      bootloader_version;
767
768 extern char                     ignore_fpu_irq;
769
770 #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
771 #define ARCH_HAS_PREFETCHW
772 #define ARCH_HAS_SPINLOCK_PREFETCH
773
774 #ifdef CONFIG_X86_32
775 # define BASE_PREFETCH          ASM_NOP4
776 # define ARCH_HAS_PREFETCH
777 #else
778 # define BASE_PREFETCH          "prefetcht0 (%1)"
779 #endif
780
781 /*
782  * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
783  *
784  * It's not worth to care about 3dnow prefetches for the K6
785  * because they are microcoded there and very slow.
786  */
787 static inline void prefetch(const void *x)
788 {
789         alternative_input(BASE_PREFETCH,
790                           "prefetchnta (%1)",
791                           X86_FEATURE_XMM,
792                           "r" (x));
793 }
794
795 /*
796  * 3dnow prefetch to get an exclusive cache line.
797  * Useful for spinlocks to avoid one state transition in the
798  * cache coherency protocol:
799  */
800 static inline void prefetchw(const void *x)
801 {
802         alternative_input(BASE_PREFETCH,
803                           "prefetchw (%1)",
804                           X86_FEATURE_3DNOW,
805                           "r" (x));
806 }
807
808 static inline void spin_lock_prefetch(const void *x)
809 {
810         prefetchw(x);
811 }
812
813 #ifdef CONFIG_X86_32
814 /*
815  * User space process size: 3GB (default).
816  */
817 #define TASK_SIZE               PAGE_OFFSET
818 #define TASK_SIZE_MAX           TASK_SIZE
819 #define STACK_TOP               TASK_SIZE
820 #define STACK_TOP_MAX           STACK_TOP
821
822 #define INIT_THREAD  {                                                    \
823         .sp0                    = sizeof(init_stack) + (long)&init_stack, \
824         .vm86_info              = NULL,                                   \
825         .sysenter_cs            = __KERNEL_CS,                            \
826         .io_bitmap_ptr          = NULL,                                   \
827 }
828
829 /*
830  * Note that the .io_bitmap member must be extra-big. This is because
831  * the CPU will access an additional byte beyond the end of the IO
832  * permission bitmap. The extra byte must be all 1 bits, and must
833  * be within the limit.
834  */
835 #define INIT_TSS  {                                                       \
836         .x86_tss = {                                                      \
837                 .sp0            = sizeof(init_stack) + (long)&init_stack, \
838                 .ss0            = __KERNEL_DS,                            \
839                 .ss1            = __KERNEL_CS,                            \
840                 .io_bitmap_base = INVALID_IO_BITMAP_OFFSET,               \
841          },                                                               \
842         .io_bitmap              = { [0 ... IO_BITMAP_LONGS] = ~0 },       \
843 }
844
845 extern unsigned long thread_saved_pc(struct task_struct *tsk);
846
847 #define THREAD_SIZE_LONGS      (THREAD_SIZE/sizeof(unsigned long))
848 #define KSTK_TOP(info)                                                 \
849 ({                                                                     \
850        unsigned long *__ptr = (unsigned long *)(info);                 \
851        (unsigned long)(&__ptr[THREAD_SIZE_LONGS]);                     \
852 })
853
854 /*
855  * The below -8 is to reserve 8 bytes on top of the ring0 stack.
856  * This is necessary to guarantee that the entire "struct pt_regs"
857  * is accessible even if the CPU haven't stored the SS/ESP registers
858  * on the stack (interrupt gate does not save these registers
859  * when switching to the same priv ring).
860  * Therefore beware: accessing the ss/esp fields of the
861  * "struct pt_regs" is possible, but they may contain the
862  * completely wrong values.
863  */
864 #define task_pt_regs(task)                                             \
865 ({                                                                     \
866        struct pt_regs *__regs__;                                       \
867        __regs__ = (struct pt_regs *)(KSTK_TOP(task_stack_page(task))-8); \
868        __regs__ - 1;                                                   \
869 })
870
871 #else
872 /*
873  * User space process size. 47bits minus one guard page.
874  */
875 #define TASK_SIZE_MAX   ((1UL << 47) - PAGE_SIZE)
876
877 /* This decides where the kernel will search for a free chunk of vm
878  * space during mmap's.
879  */
880 #define IA32_PAGE_OFFSET        ((current->personality & ADDR_LIMIT_3GB) ? \
881                                         0xc0000000 : 0xFFFFe000)
882
883 #define TASK_SIZE               (test_thread_flag(TIF_IA32) ? \
884                                         IA32_PAGE_OFFSET : TASK_SIZE_MAX)
885 #define TASK_SIZE_OF(child)     ((test_tsk_thread_flag(child, TIF_IA32)) ? \
886                                         IA32_PAGE_OFFSET : TASK_SIZE_MAX)
887
888 #define STACK_TOP               TASK_SIZE
889 #define STACK_TOP_MAX           TASK_SIZE_MAX
890
891 #define INIT_THREAD  { \
892         .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
893 }
894
895 #define INIT_TSS  { \
896         .x86_tss.sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
897 }
898
899 /*
900  * Return saved PC of a blocked thread.
901  * What is this good for? it will be always the scheduler or ret_from_fork.
902  */
903 #define thread_saved_pc(t)      (*(unsigned long *)((t)->thread.sp - 8))
904
905 #define task_pt_regs(tsk)       ((struct pt_regs *)(tsk)->thread.sp0 - 1)
906 #endif /* CONFIG_X86_64 */
907
908 extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
909                                                unsigned long new_sp);
910
911 /*
912  * This decides where the kernel will search for a free chunk of vm
913  * space during mmap's.
914  */
915 #define TASK_UNMAPPED_BASE      (PAGE_ALIGN(TASK_SIZE / 3))
916
917 #define KSTK_EIP(task)          (task_pt_regs(task)->ip)
918 #define KSTK_ESP(task)          (task_pt_regs(task)->sp)
919
920 /* Get/set a process' ability to use the timestamp counter instruction */
921 #define GET_TSC_CTL(adr)        get_tsc_mode((adr))
922 #define SET_TSC_CTL(val)        set_tsc_mode((val))
923
924 extern int get_tsc_mode(unsigned long adr);
925 extern int set_tsc_mode(unsigned int val);
926
927 extern int amd_get_nb_id(int cpu);
928
929 struct aperfmperf {
930         u64 aperf, mperf;
931 };
932
933 static inline void get_aperfmperf(struct aperfmperf *am)
934 {
935         WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_APERFMPERF));
936
937         rdmsrl(MSR_IA32_APERF, am->aperf);
938         rdmsrl(MSR_IA32_MPERF, am->mperf);
939 }
940
941 #define APERFMPERF_SHIFT 10
942
943 static inline
944 unsigned long calc_aperfmperf_ratio(struct aperfmperf *old,
945                                     struct aperfmperf *new)
946 {
947         u64 aperf = new->aperf - old->aperf;
948         u64 mperf = new->mperf - old->mperf;
949         unsigned long ratio = aperf;
950
951         mperf >>= APERFMPERF_SHIFT;
952         if (mperf)
953                 ratio = div64_u64(aperf, mperf);
954
955         return ratio;
956 }
957
958 /*
959  * AMD errata checking
960  */
961 #ifdef CONFIG_CPU_SUP_AMD
962 extern const int amd_erratum_383[];
963 extern const int amd_erratum_400[];
964 extern bool cpu_has_amd_erratum(const int *);
965
966 #define AMD_LEGACY_ERRATUM(...)         { -1, __VA_ARGS__, 0 }
967 #define AMD_OSVW_ERRATUM(osvw_id, ...)  { osvw_id, __VA_ARGS__, 0 }
968 #define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end) \
969         ((f << 24) | (m_start << 16) | (s_start << 12) | (m_end << 4) | (s_end))
970 #define AMD_MODEL_RANGE_FAMILY(range)   (((range) >> 24) & 0xff)
971 #define AMD_MODEL_RANGE_START(range)    (((range) >> 12) & 0xfff)
972 #define AMD_MODEL_RANGE_END(range)      ((range) & 0xfff)
973
974 #else
975 #define cpu_has_amd_erratum(x)  (false)
976 #endif /* CONFIG_CPU_SUP_AMD */
977
978 #endif /* _ASM_X86_PROCESSOR_H */