]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/x86/include/asm/processor.h
555c9478f3df5a7c02d566eb6012ee84f5e1c9b4
[linux.git] / arch / x86 / include / asm / processor.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PROCESSOR_H
3 #define _ASM_X86_PROCESSOR_H
4
5 #include <asm/processor-flags.h>
6
7 /* Forward declaration, a strange C thing */
8 struct task_struct;
9 struct mm_struct;
10 struct vm86;
11
12 #include <asm/math_emu.h>
13 #include <asm/segment.h>
14 #include <asm/types.h>
15 #include <uapi/asm/sigcontext.h>
16 #include <asm/current.h>
17 #include <asm/cpufeatures.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/special_insns.h>
25 #include <asm/fpu/types.h>
26 #include <asm/unwind_hints.h>
27
28 #include <linux/personality.h>
29 #include <linux/cache.h>
30 #include <linux/threads.h>
31 #include <linux/math64.h>
32 #include <linux/err.h>
33 #include <linux/irqflags.h>
34 #include <linux/mem_encrypt.h>
35
36 /*
37  * We handle most unaligned accesses in hardware.  On the other hand
38  * unaligned DMA can be quite expensive on some Nehalem processors.
39  *
40  * Based on this we disable the IP header alignment in network drivers.
41  */
42 #define NET_IP_ALIGN    0
43
44 #define HBP_NUM 4
45 /*
46  * Default implementation of macro that returns current
47  * instruction pointer ("program counter").
48  */
49 static inline void *current_text_addr(void)
50 {
51         void *pc;
52
53         asm volatile("mov $1f, %0; 1:":"=r" (pc));
54
55         return pc;
56 }
57
58 /*
59  * These alignment constraints are for performance in the vSMP case,
60  * but in the task_struct case we must also meet hardware imposed
61  * alignment requirements of the FPU state:
62  */
63 #ifdef CONFIG_X86_VSMP
64 # define ARCH_MIN_TASKALIGN             (1 << INTERNODE_CACHE_SHIFT)
65 # define ARCH_MIN_MMSTRUCT_ALIGN        (1 << INTERNODE_CACHE_SHIFT)
66 #else
67 # define ARCH_MIN_TASKALIGN             __alignof__(union fpregs_state)
68 # define ARCH_MIN_MMSTRUCT_ALIGN        0
69 #endif
70
71 enum tlb_infos {
72         ENTRIES,
73         NR_INFO
74 };
75
76 extern u16 __read_mostly tlb_lli_4k[NR_INFO];
77 extern u16 __read_mostly tlb_lli_2m[NR_INFO];
78 extern u16 __read_mostly tlb_lli_4m[NR_INFO];
79 extern u16 __read_mostly tlb_lld_4k[NR_INFO];
80 extern u16 __read_mostly tlb_lld_2m[NR_INFO];
81 extern u16 __read_mostly tlb_lld_4m[NR_INFO];
82 extern u16 __read_mostly tlb_lld_1g[NR_INFO];
83
84 /*
85  *  CPU type and hardware bug flags. Kept separately for each CPU.
86  *  Members of this structure are referenced in head_32.S, so think twice
87  *  before touching them. [mj]
88  */
89
90 struct cpuinfo_x86 {
91         __u8                    x86;            /* CPU family */
92         __u8                    x86_vendor;     /* CPU vendor */
93         __u8                    x86_model;
94         __u8                    x86_mask;
95 #ifdef CONFIG_X86_64
96         /* Number of 4K pages in DTLB/ITLB combined(in pages): */
97         int                     x86_tlbsize;
98 #endif
99         __u8                    x86_virt_bits;
100         __u8                    x86_phys_bits;
101         /* CPUID returned core id bits: */
102         __u8                    x86_coreid_bits;
103         __u8                    cu_id;
104         /* Max extended CPUID function supported: */
105         __u32                   extended_cpuid_level;
106         /* Maximum supported CPUID level, -1=no CPUID: */
107         int                     cpuid_level;
108         __u32                   x86_capability[NCAPINTS + NBUGINTS];
109         char                    x86_vendor_id[16];
110         char                    x86_model_id[64];
111         /* in KB - valid for CPUS which support this call: */
112         int                     x86_cache_size;
113         int                     x86_cache_alignment;    /* In bytes */
114         /* Cache QoS architectural values: */
115         int                     x86_cache_max_rmid;     /* max index */
116         int                     x86_cache_occ_scale;    /* scale to bytes */
117         int                     x86_power;
118         unsigned long           loops_per_jiffy;
119         /* cpuid returned max cores value: */
120         u16                      x86_max_cores;
121         u16                     apicid;
122         u16                     initial_apicid;
123         u16                     x86_clflush_size;
124         /* number of cores as seen by the OS: */
125         u16                     booted_cores;
126         /* Physical processor id: */
127         u16                     phys_proc_id;
128         /* Logical processor id: */
129         u16                     logical_proc_id;
130         /* Core id: */
131         u16                     cpu_core_id;
132         /* Index into per_cpu list: */
133         u16                     cpu_index;
134         u32                     microcode;
135 } __randomize_layout;
136
137 struct cpuid_regs {
138         u32 eax, ebx, ecx, edx;
139 };
140
141 enum cpuid_regs_idx {
142         CPUID_EAX = 0,
143         CPUID_EBX,
144         CPUID_ECX,
145         CPUID_EDX,
146 };
147
148 #define X86_VENDOR_INTEL        0
149 #define X86_VENDOR_CYRIX        1
150 #define X86_VENDOR_AMD          2
151 #define X86_VENDOR_UMC          3
152 #define X86_VENDOR_CENTAUR      5
153 #define X86_VENDOR_TRANSMETA    7
154 #define X86_VENDOR_NSC          8
155 #define X86_VENDOR_NUM          9
156
157 #define X86_VENDOR_UNKNOWN      0xff
158
159 /*
160  * capabilities of CPUs
161  */
162 extern struct cpuinfo_x86       boot_cpu_data;
163 extern struct cpuinfo_x86       new_cpu_data;
164
165 extern struct x86_hw_tss        doublefault_tss;
166 extern __u32                    cpu_caps_cleared[NCAPINTS];
167 extern __u32                    cpu_caps_set[NCAPINTS];
168
169 #ifdef CONFIG_SMP
170 DECLARE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
171 #define cpu_data(cpu)           per_cpu(cpu_info, cpu)
172 #else
173 #define cpu_info                boot_cpu_data
174 #define cpu_data(cpu)           boot_cpu_data
175 #endif
176
177 extern const struct seq_operations cpuinfo_op;
178
179 #define cache_line_size()       (boot_cpu_data.x86_cache_alignment)
180
181 extern void cpu_detect(struct cpuinfo_x86 *c);
182
183 extern void early_cpu_init(void);
184 extern void identify_boot_cpu(void);
185 extern void identify_secondary_cpu(struct cpuinfo_x86 *);
186 extern void print_cpu_info(struct cpuinfo_x86 *);
187 void print_cpu_msr(struct cpuinfo_x86 *);
188 extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
189 extern u32 get_scattered_cpuid_leaf(unsigned int level,
190                                     unsigned int sub_leaf,
191                                     enum cpuid_regs_idx reg);
192 extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
193 extern void init_amd_cacheinfo(struct cpuinfo_x86 *c);
194
195 extern void detect_extended_topology(struct cpuinfo_x86 *c);
196 extern void detect_ht(struct cpuinfo_x86 *c);
197
198 #ifdef CONFIG_X86_32
199 extern int have_cpuid_p(void);
200 #else
201 static inline int have_cpuid_p(void)
202 {
203         return 1;
204 }
205 #endif
206 static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
207                                 unsigned int *ecx, unsigned int *edx)
208 {
209         /* ecx is often an input as well as an output. */
210         asm volatile("cpuid"
211             : "=a" (*eax),
212               "=b" (*ebx),
213               "=c" (*ecx),
214               "=d" (*edx)
215             : "0" (*eax), "2" (*ecx)
216             : "memory");
217 }
218
219 #define native_cpuid_reg(reg)                                   \
220 static inline unsigned int native_cpuid_##reg(unsigned int op)  \
221 {                                                               \
222         unsigned int eax = op, ebx, ecx = 0, edx;               \
223                                                                 \
224         native_cpuid(&eax, &ebx, &ecx, &edx);                   \
225                                                                 \
226         return reg;                                             \
227 }
228
229 /*
230  * Native CPUID functions returning a single datum.
231  */
232 native_cpuid_reg(eax)
233 native_cpuid_reg(ebx)
234 native_cpuid_reg(ecx)
235 native_cpuid_reg(edx)
236
237 /*
238  * Friendlier CR3 helpers.
239  */
240 static inline unsigned long read_cr3_pa(void)
241 {
242         return __read_cr3() & CR3_ADDR_MASK;
243 }
244
245 static inline unsigned long native_read_cr3_pa(void)
246 {
247         return __native_read_cr3() & CR3_ADDR_MASK;
248 }
249
250 static inline void load_cr3(pgd_t *pgdir)
251 {
252         write_cr3(__sme_pa(pgdir));
253 }
254
255 /*
256  * Note that while the legacy 'TSS' name comes from 'Task State Segment',
257  * on modern x86 CPUs the TSS also holds information important to 64-bit mode,
258  * unrelated to the task-switch mechanism:
259  */
260 #ifdef CONFIG_X86_32
261 /* This is the TSS defined by the hardware. */
262 struct x86_hw_tss {
263         unsigned short          back_link, __blh;
264         unsigned long           sp0;
265         unsigned short          ss0, __ss0h;
266         unsigned long           sp1;
267
268         /*
269          * We don't use ring 1, so ss1 is a convenient scratch space in
270          * the same cacheline as sp0.  We use ss1 to cache the value in
271          * MSR_IA32_SYSENTER_CS.  When we context switch
272          * MSR_IA32_SYSENTER_CS, we first check if the new value being
273          * written matches ss1, and, if it's not, then we wrmsr the new
274          * value and update ss1.
275          *
276          * The only reason we context switch MSR_IA32_SYSENTER_CS is
277          * that we set it to zero in vm86 tasks to avoid corrupting the
278          * stack if we were to go through the sysenter path from vm86
279          * mode.
280          */
281         unsigned short          ss1;    /* MSR_IA32_SYSENTER_CS */
282
283         unsigned short          __ss1h;
284         unsigned long           sp2;
285         unsigned short          ss2, __ss2h;
286         unsigned long           __cr3;
287         unsigned long           ip;
288         unsigned long           flags;
289         unsigned long           ax;
290         unsigned long           cx;
291         unsigned long           dx;
292         unsigned long           bx;
293         unsigned long           sp;
294         unsigned long           bp;
295         unsigned long           si;
296         unsigned long           di;
297         unsigned short          es, __esh;
298         unsigned short          cs, __csh;
299         unsigned short          ss, __ssh;
300         unsigned short          ds, __dsh;
301         unsigned short          fs, __fsh;
302         unsigned short          gs, __gsh;
303         unsigned short          ldt, __ldth;
304         unsigned short          trace;
305         unsigned short          io_bitmap_base;
306
307 } __attribute__((packed));
308 #else
309 struct x86_hw_tss {
310         u32                     reserved1;
311         u64                     sp0;
312         u64                     sp1;
313         u64                     sp2;
314         u64                     reserved2;
315         u64                     ist[7];
316         u32                     reserved3;
317         u32                     reserved4;
318         u16                     reserved5;
319         u16                     io_bitmap_base;
320
321 } __attribute__((packed));
322 #endif
323
324 /*
325  * IO-bitmap sizes:
326  */
327 #define IO_BITMAP_BITS                  65536
328 #define IO_BITMAP_BYTES                 (IO_BITMAP_BITS/8)
329 #define IO_BITMAP_LONGS                 (IO_BITMAP_BYTES/sizeof(long))
330 #define IO_BITMAP_OFFSET                (offsetof(struct tss_struct, io_bitmap) - offsetof(struct tss_struct, x86_tss))
331 #define INVALID_IO_BITMAP_OFFSET        0x8000
332
333 struct tss_struct {
334         /*
335          * The hardware state:
336          */
337         struct x86_hw_tss       x86_tss;
338
339         /*
340          * The extra 1 is there because the CPU will access an
341          * additional byte beyond the end of the IO permission
342          * bitmap. The extra byte must be all 1 bits, and must
343          * be within the limit.
344          */
345         unsigned long           io_bitmap[IO_BITMAP_LONGS + 1];
346
347         /*
348          * Space for the temporary SYSENTER stack.
349          */
350         unsigned long           SYSENTER_stack_canary;
351         unsigned long           SYSENTER_stack[64];
352 } ____cacheline_aligned;
353
354 DECLARE_PER_CPU_SHARED_ALIGNED(struct tss_struct, cpu_tss);
355
356 /*
357  * sizeof(unsigned long) coming from an extra "long" at the end
358  * of the iobitmap.
359  *
360  * -1? seg base+limit should be pointing to the address of the
361  * last valid byte
362  */
363 #define __KERNEL_TSS_LIMIT      \
364         (IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1)
365
366 #ifdef CONFIG_X86_32
367 DECLARE_PER_CPU(unsigned long, cpu_current_top_of_stack);
368 #endif
369
370 /*
371  * Save the original ist values for checking stack pointers during debugging
372  */
373 struct orig_ist {
374         unsigned long           ist[7];
375 };
376
377 #ifdef CONFIG_X86_64
378 DECLARE_PER_CPU(struct orig_ist, orig_ist);
379
380 union irq_stack_union {
381         char irq_stack[IRQ_STACK_SIZE];
382         /*
383          * GCC hardcodes the stack canary as %gs:40.  Since the
384          * irq_stack is the object at %gs:0, we reserve the bottom
385          * 48 bytes of the irq stack for the canary.
386          */
387         struct {
388                 char gs_base[40];
389                 unsigned long stack_canary;
390         };
391 };
392
393 DECLARE_PER_CPU_FIRST(union irq_stack_union, irq_stack_union) __visible;
394 DECLARE_INIT_PER_CPU(irq_stack_union);
395
396 DECLARE_PER_CPU(char *, irq_stack_ptr);
397 DECLARE_PER_CPU(unsigned int, irq_count);
398 extern asmlinkage void ignore_sysret(void);
399 #else   /* X86_64 */
400 #ifdef CONFIG_CC_STACKPROTECTOR
401 /*
402  * Make sure stack canary segment base is cached-aligned:
403  *   "For Intel Atom processors, avoid non zero segment base address
404  *    that is not aligned to cache line boundary at all cost."
405  * (Optim Ref Manual Assembly/Compiler Coding Rule 15.)
406  */
407 struct stack_canary {
408         char __pad[20];         /* canary at %gs:20 */
409         unsigned long canary;
410 };
411 DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
412 #endif
413 /*
414  * per-CPU IRQ handling stacks
415  */
416 struct irq_stack {
417         u32                     stack[THREAD_SIZE/sizeof(u32)];
418 } __aligned(THREAD_SIZE);
419
420 DECLARE_PER_CPU(struct irq_stack *, hardirq_stack);
421 DECLARE_PER_CPU(struct irq_stack *, softirq_stack);
422 #endif  /* X86_64 */
423
424 extern unsigned int fpu_kernel_xstate_size;
425 extern unsigned int fpu_user_xstate_size;
426
427 struct perf_event;
428
429 typedef struct {
430         unsigned long           seg;
431 } mm_segment_t;
432
433 struct thread_struct {
434         /* Cached TLS descriptors: */
435         struct desc_struct      tls_array[GDT_ENTRY_TLS_ENTRIES];
436 #ifdef CONFIG_X86_32
437         unsigned long           sp0;
438 #endif
439         unsigned long           sp;
440 #ifdef CONFIG_X86_32
441         unsigned long           sysenter_cs;
442 #else
443         unsigned short          es;
444         unsigned short          ds;
445         unsigned short          fsindex;
446         unsigned short          gsindex;
447 #endif
448
449         u32                     status;         /* thread synchronous flags */
450
451 #ifdef CONFIG_X86_64
452         unsigned long           fsbase;
453         unsigned long           gsbase;
454 #else
455         /*
456          * XXX: this could presumably be unsigned short.  Alternatively,
457          * 32-bit kernels could be taught to use fsindex instead.
458          */
459         unsigned long fs;
460         unsigned long gs;
461 #endif
462
463         /* Save middle states of ptrace breakpoints */
464         struct perf_event       *ptrace_bps[HBP_NUM];
465         /* Debug status used for traps, single steps, etc... */
466         unsigned long           debugreg6;
467         /* Keep track of the exact dr7 value set by the user */
468         unsigned long           ptrace_dr7;
469         /* Fault info: */
470         unsigned long           cr2;
471         unsigned long           trap_nr;
472         unsigned long           error_code;
473 #ifdef CONFIG_VM86
474         /* Virtual 86 mode info */
475         struct vm86             *vm86;
476 #endif
477         /* IO permissions: */
478         unsigned long           *io_bitmap_ptr;
479         unsigned long           iopl;
480         /* Max allowed port in the bitmap, in bytes: */
481         unsigned                io_bitmap_max;
482
483         mm_segment_t            addr_limit;
484
485         unsigned int            sig_on_uaccess_err:1;
486         unsigned int            uaccess_err:1;  /* uaccess failed */
487
488         /* Floating point and extended processor state */
489         struct fpu              fpu;
490         /*
491          * WARNING: 'fpu' is dynamically-sized.  It *MUST* be at
492          * the end.
493          */
494 };
495
496 /*
497  * Thread-synchronous status.
498  *
499  * This is different from the flags in that nobody else
500  * ever touches our thread-synchronous status, so we don't
501  * have to worry about atomic accesses.
502  */
503 #define TS_COMPAT               0x0002  /* 32bit syscall active (64BIT)*/
504
505 /*
506  * Set IOPL bits in EFLAGS from given mask
507  */
508 static inline void native_set_iopl_mask(unsigned mask)
509 {
510 #ifdef CONFIG_X86_32
511         unsigned int reg;
512
513         asm volatile ("pushfl;"
514                       "popl %0;"
515                       "andl %1, %0;"
516                       "orl %2, %0;"
517                       "pushl %0;"
518                       "popfl"
519                       : "=&r" (reg)
520                       : "i" (~X86_EFLAGS_IOPL), "r" (mask));
521 #endif
522 }
523
524 static inline void
525 native_load_sp0(unsigned long sp0)
526 {
527         this_cpu_write(cpu_tss.x86_tss.sp0, sp0);
528 }
529
530 static inline void native_swapgs(void)
531 {
532 #ifdef CONFIG_X86_64
533         asm volatile("swapgs" ::: "memory");
534 #endif
535 }
536
537 static inline unsigned long current_top_of_stack(void)
538 {
539 #ifdef CONFIG_X86_64
540         return this_cpu_read_stable(cpu_tss.x86_tss.sp0);
541 #else
542         /* sp0 on x86_32 is special in and around vm86 mode. */
543         return this_cpu_read_stable(cpu_current_top_of_stack);
544 #endif
545 }
546
547 static inline bool on_thread_stack(void)
548 {
549         return (unsigned long)(current_top_of_stack() -
550                                current_stack_pointer) < THREAD_SIZE;
551 }
552
553 #ifdef CONFIG_PARAVIRT
554 #include <asm/paravirt.h>
555 #else
556 #define __cpuid                 native_cpuid
557
558 static inline void load_sp0(unsigned long sp0)
559 {
560         native_load_sp0(sp0);
561 }
562
563 #define set_iopl_mask native_set_iopl_mask
564 #endif /* CONFIG_PARAVIRT */
565
566 /* Free all resources held by a thread. */
567 extern void release_thread(struct task_struct *);
568
569 unsigned long get_wchan(struct task_struct *p);
570
571 /*
572  * Generic CPUID function
573  * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
574  * resulting in stale register contents being returned.
575  */
576 static inline void cpuid(unsigned int op,
577                          unsigned int *eax, unsigned int *ebx,
578                          unsigned int *ecx, unsigned int *edx)
579 {
580         *eax = op;
581         *ecx = 0;
582         __cpuid(eax, ebx, ecx, edx);
583 }
584
585 /* Some CPUID calls want 'count' to be placed in ecx */
586 static inline void cpuid_count(unsigned int op, int count,
587                                unsigned int *eax, unsigned int *ebx,
588                                unsigned int *ecx, unsigned int *edx)
589 {
590         *eax = op;
591         *ecx = count;
592         __cpuid(eax, ebx, ecx, edx);
593 }
594
595 /*
596  * CPUID functions returning a single datum
597  */
598 static inline unsigned int cpuid_eax(unsigned int op)
599 {
600         unsigned int eax, ebx, ecx, edx;
601
602         cpuid(op, &eax, &ebx, &ecx, &edx);
603
604         return eax;
605 }
606
607 static inline unsigned int cpuid_ebx(unsigned int op)
608 {
609         unsigned int eax, ebx, ecx, edx;
610
611         cpuid(op, &eax, &ebx, &ecx, &edx);
612
613         return ebx;
614 }
615
616 static inline unsigned int cpuid_ecx(unsigned int op)
617 {
618         unsigned int eax, ebx, ecx, edx;
619
620         cpuid(op, &eax, &ebx, &ecx, &edx);
621
622         return ecx;
623 }
624
625 static inline unsigned int cpuid_edx(unsigned int op)
626 {
627         unsigned int eax, ebx, ecx, edx;
628
629         cpuid(op, &eax, &ebx, &ecx, &edx);
630
631         return edx;
632 }
633
634 /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
635 static __always_inline void rep_nop(void)
636 {
637         asm volatile("rep; nop" ::: "memory");
638 }
639
640 static __always_inline void cpu_relax(void)
641 {
642         rep_nop();
643 }
644
645 /*
646  * This function forces the icache and prefetched instruction stream to
647  * catch up with reality in two very specific cases:
648  *
649  *  a) Text was modified using one virtual address and is about to be executed
650  *     from the same physical page at a different virtual address.
651  *
652  *  b) Text was modified on a different CPU, may subsequently be
653  *     executed on this CPU, and you want to make sure the new version
654  *     gets executed.  This generally means you're calling this in a IPI.
655  *
656  * If you're calling this for a different reason, you're probably doing
657  * it wrong.
658  */
659 static inline void sync_core(void)
660 {
661         /*
662          * There are quite a few ways to do this.  IRET-to-self is nice
663          * because it works on every CPU, at any CPL (so it's compatible
664          * with paravirtualization), and it never exits to a hypervisor.
665          * The only down sides are that it's a bit slow (it seems to be
666          * a bit more than 2x slower than the fastest options) and that
667          * it unmasks NMIs.  The "push %cs" is needed because, in
668          * paravirtual environments, __KERNEL_CS may not be a valid CS
669          * value when we do IRET directly.
670          *
671          * In case NMI unmasking or performance ever becomes a problem,
672          * the next best option appears to be MOV-to-CR2 and an
673          * unconditional jump.  That sequence also works on all CPUs,
674          * but it will fault at CPL3 (i.e. Xen PV).
675          *
676          * CPUID is the conventional way, but it's nasty: it doesn't
677          * exist on some 486-like CPUs, and it usually exits to a
678          * hypervisor.
679          *
680          * Like all of Linux's memory ordering operations, this is a
681          * compiler barrier as well.
682          */
683 #ifdef CONFIG_X86_32
684         asm volatile (
685                 "pushfl\n\t"
686                 "pushl %%cs\n\t"
687                 "pushl $1f\n\t"
688                 "iret\n\t"
689                 "1:"
690                 : ASM_CALL_CONSTRAINT : : "memory");
691 #else
692         unsigned int tmp;
693
694         asm volatile (
695                 UNWIND_HINT_SAVE
696                 "mov %%ss, %0\n\t"
697                 "pushq %q0\n\t"
698                 "pushq %%rsp\n\t"
699                 "addq $8, (%%rsp)\n\t"
700                 "pushfq\n\t"
701                 "mov %%cs, %0\n\t"
702                 "pushq %q0\n\t"
703                 "pushq $1f\n\t"
704                 "iretq\n\t"
705                 UNWIND_HINT_RESTORE
706                 "1:"
707                 : "=&r" (tmp), ASM_CALL_CONSTRAINT : : "cc", "memory");
708 #endif
709 }
710
711 extern void select_idle_routine(const struct cpuinfo_x86 *c);
712 extern void amd_e400_c1e_apic_setup(void);
713
714 extern unsigned long            boot_option_idle_override;
715
716 enum idle_boot_override {IDLE_NO_OVERRIDE=0, IDLE_HALT, IDLE_NOMWAIT,
717                          IDLE_POLL};
718
719 extern void enable_sep_cpu(void);
720 extern int sysenter_setup(void);
721
722 extern void early_trap_init(void);
723 void early_trap_pf_init(void);
724
725 /* Defined in head.S */
726 extern struct desc_ptr          early_gdt_descr;
727
728 extern void cpu_set_gdt(int);
729 extern void switch_to_new_gdt(int);
730 extern void load_direct_gdt(int);
731 extern void load_fixmap_gdt(int);
732 extern void load_percpu_segment(int);
733 extern void cpu_init(void);
734
735 static inline unsigned long get_debugctlmsr(void)
736 {
737         unsigned long debugctlmsr = 0;
738
739 #ifndef CONFIG_X86_DEBUGCTLMSR
740         if (boot_cpu_data.x86 < 6)
741                 return 0;
742 #endif
743         rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
744
745         return debugctlmsr;
746 }
747
748 static inline void update_debugctlmsr(unsigned long debugctlmsr)
749 {
750 #ifndef CONFIG_X86_DEBUGCTLMSR
751         if (boot_cpu_data.x86 < 6)
752                 return;
753 #endif
754         wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
755 }
756
757 extern void set_task_blockstep(struct task_struct *task, bool on);
758
759 /* Boot loader type from the setup header: */
760 extern int                      bootloader_type;
761 extern int                      bootloader_version;
762
763 extern char                     ignore_fpu_irq;
764
765 #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
766 #define ARCH_HAS_PREFETCHW
767 #define ARCH_HAS_SPINLOCK_PREFETCH
768
769 #ifdef CONFIG_X86_32
770 # define BASE_PREFETCH          ""
771 # define ARCH_HAS_PREFETCH
772 #else
773 # define BASE_PREFETCH          "prefetcht0 %P1"
774 #endif
775
776 /*
777  * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
778  *
779  * It's not worth to care about 3dnow prefetches for the K6
780  * because they are microcoded there and very slow.
781  */
782 static inline void prefetch(const void *x)
783 {
784         alternative_input(BASE_PREFETCH, "prefetchnta %P1",
785                           X86_FEATURE_XMM,
786                           "m" (*(const char *)x));
787 }
788
789 /*
790  * 3dnow prefetch to get an exclusive cache line.
791  * Useful for spinlocks to avoid one state transition in the
792  * cache coherency protocol:
793  */
794 static inline void prefetchw(const void *x)
795 {
796         alternative_input(BASE_PREFETCH, "prefetchw %P1",
797                           X86_FEATURE_3DNOWPREFETCH,
798                           "m" (*(const char *)x));
799 }
800
801 static inline void spin_lock_prefetch(const void *x)
802 {
803         prefetchw(x);
804 }
805
806 #define TOP_OF_INIT_STACK ((unsigned long)&init_stack + sizeof(init_stack) - \
807                            TOP_OF_KERNEL_STACK_PADDING)
808
809 #define task_top_of_stack(task) ((unsigned long)(task_pt_regs(task) + 1))
810
811 #define task_pt_regs(task) \
812 ({                                                                      \
813         unsigned long __ptr = (unsigned long)task_stack_page(task);     \
814         __ptr += THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING;             \
815         ((struct pt_regs *)__ptr) - 1;                                  \
816 })
817
818 #ifdef CONFIG_X86_32
819 /*
820  * User space process size: 3GB (default).
821  */
822 #define IA32_PAGE_OFFSET        PAGE_OFFSET
823 #define TASK_SIZE               PAGE_OFFSET
824 #define TASK_SIZE_LOW           TASK_SIZE
825 #define TASK_SIZE_MAX           TASK_SIZE
826 #define DEFAULT_MAP_WINDOW      TASK_SIZE
827 #define STACK_TOP               TASK_SIZE
828 #define STACK_TOP_MAX           STACK_TOP
829
830 #define INIT_THREAD  {                                                    \
831         .sp0                    = TOP_OF_INIT_STACK,                      \
832         .sysenter_cs            = __KERNEL_CS,                            \
833         .io_bitmap_ptr          = NULL,                                   \
834         .addr_limit             = KERNEL_DS,                              \
835 }
836
837 #define KSTK_ESP(task)          (task_pt_regs(task)->sp)
838
839 #else
840 /*
841  * User space process size. 47bits minus one guard page.  The guard
842  * page is necessary on Intel CPUs: if a SYSCALL instruction is at
843  * the highest possible canonical userspace address, then that
844  * syscall will enter the kernel with a non-canonical return
845  * address, and SYSRET will explode dangerously.  We avoid this
846  * particular problem by preventing anything from being mapped
847  * at the maximum canonical address.
848  */
849 #define TASK_SIZE_MAX   ((1UL << __VIRTUAL_MASK_SHIFT) - PAGE_SIZE)
850
851 #define DEFAULT_MAP_WINDOW      ((1UL << 47) - PAGE_SIZE)
852
853 /* This decides where the kernel will search for a free chunk of vm
854  * space during mmap's.
855  */
856 #define IA32_PAGE_OFFSET        ((current->personality & ADDR_LIMIT_3GB) ? \
857                                         0xc0000000 : 0xFFFFe000)
858
859 #define TASK_SIZE_LOW           (test_thread_flag(TIF_ADDR32) ? \
860                                         IA32_PAGE_OFFSET : DEFAULT_MAP_WINDOW)
861 #define TASK_SIZE               (test_thread_flag(TIF_ADDR32) ? \
862                                         IA32_PAGE_OFFSET : TASK_SIZE_MAX)
863 #define TASK_SIZE_OF(child)     ((test_tsk_thread_flag(child, TIF_ADDR32)) ? \
864                                         IA32_PAGE_OFFSET : TASK_SIZE_MAX)
865
866 #define STACK_TOP               TASK_SIZE_LOW
867 #define STACK_TOP_MAX           TASK_SIZE_MAX
868
869 #define INIT_THREAD  {                                          \
870         .addr_limit             = KERNEL_DS,                    \
871 }
872
873 extern unsigned long KSTK_ESP(struct task_struct *task);
874
875 #endif /* CONFIG_X86_64 */
876
877 extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
878                                                unsigned long new_sp);
879
880 /*
881  * This decides where the kernel will search for a free chunk of vm
882  * space during mmap's.
883  */
884 #define __TASK_UNMAPPED_BASE(task_size) (PAGE_ALIGN(task_size / 3))
885 #define TASK_UNMAPPED_BASE              __TASK_UNMAPPED_BASE(TASK_SIZE_LOW)
886
887 #define KSTK_EIP(task)          (task_pt_regs(task)->ip)
888
889 /* Get/set a process' ability to use the timestamp counter instruction */
890 #define GET_TSC_CTL(adr)        get_tsc_mode((adr))
891 #define SET_TSC_CTL(val)        set_tsc_mode((val))
892
893 extern int get_tsc_mode(unsigned long adr);
894 extern int set_tsc_mode(unsigned int val);
895
896 DECLARE_PER_CPU(u64, msr_misc_features_shadow);
897
898 /* Register/unregister a process' MPX related resource */
899 #define MPX_ENABLE_MANAGEMENT() mpx_enable_management()
900 #define MPX_DISABLE_MANAGEMENT()        mpx_disable_management()
901
902 #ifdef CONFIG_X86_INTEL_MPX
903 extern int mpx_enable_management(void);
904 extern int mpx_disable_management(void);
905 #else
906 static inline int mpx_enable_management(void)
907 {
908         return -EINVAL;
909 }
910 static inline int mpx_disable_management(void)
911 {
912         return -EINVAL;
913 }
914 #endif /* CONFIG_X86_INTEL_MPX */
915
916 #ifdef CONFIG_CPU_SUP_AMD
917 extern u16 amd_get_nb_id(int cpu);
918 extern u32 amd_get_nodes_per_socket(void);
919 #else
920 static inline u16 amd_get_nb_id(int cpu)                { return 0; }
921 static inline u32 amd_get_nodes_per_socket(void)        { return 0; }
922 #endif
923
924 static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
925 {
926         uint32_t base, eax, signature[3];
927
928         for (base = 0x40000000; base < 0x40010000; base += 0x100) {
929                 cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
930
931                 if (!memcmp(sig, signature, 12) &&
932                     (leaves == 0 || ((eax - base) >= leaves)))
933                         return base;
934         }
935
936         return 0;
937 }
938
939 extern unsigned long arch_align_stack(unsigned long sp);
940 extern void free_init_pages(char *what, unsigned long begin, unsigned long end);
941
942 void default_idle(void);
943 #ifdef  CONFIG_XEN
944 bool xen_set_default_idle(void);
945 #else
946 #define xen_set_default_idle 0
947 #endif
948
949 void stop_this_cpu(void *dummy);
950 void df_debug(struct pt_regs *regs, long error_code);
951 #endif /* _ASM_X86_PROCESSOR_H */