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