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