processor.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_PROCESSOR_H
  3. #define _ASM_X86_PROCESSOR_H
  4. #include <asm/processor-flags.h>
  5. /* Forward declaration, a strange C thing */
  6. struct task_struct;
  7. struct mm_struct;
  8. struct io_bitmap;
  9. struct vm86;
  10. #include <asm/math_emu.h>
  11. #include <asm/segment.h>
  12. #include <asm/types.h>
  13. #include <uapi/asm/sigcontext.h>
  14. #include <asm/current.h>
  15. #include <asm/cpufeatures.h>
  16. #include <asm/page.h>
  17. #include <asm/pgtable_types.h>
  18. #include <asm/percpu.h>
  19. #include <asm/msr.h>
  20. #include <asm/desc_defs.h>
  21. #include <asm/nops.h>
  22. #include <asm/special_insns.h>
  23. #include <asm/fpu/types.h>
  24. #include <asm/unwind_hints.h>
  25. #include <asm/vmxfeatures.h>
  26. #include <asm/vdso/processor.h>
  27. #include <linux/personality.h>
  28. #include <linux/cache.h>
  29. #include <linux/threads.h>
  30. #include <linux/math64.h>
  31. #include <linux/err.h>
  32. #include <linux/irqflags.h>
  33. #include <linux/mem_encrypt.h>
  34. /*
  35. * We handle most unaligned accesses in hardware. On the other hand
  36. * unaligned DMA can be quite expensive on some Nehalem processors.
  37. *
  38. * Based on this we disable the IP header alignment in network drivers.
  39. */
  40. #define NET_IP_ALIGN 0
  41. #define HBP_NUM 4
  42. /*
  43. * These alignment constraints are for performance in the vSMP case,
  44. * but in the task_struct case we must also meet hardware imposed
  45. * alignment requirements of the FPU state:
  46. */
  47. #ifdef CONFIG_X86_VSMP
  48. # define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
  49. # define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
  50. #else
  51. # define ARCH_MIN_TASKALIGN __alignof__(union fpregs_state)
  52. # define ARCH_MIN_MMSTRUCT_ALIGN 0
  53. #endif
  54. enum tlb_infos {
  55. ENTRIES,
  56. NR_INFO
  57. };
  58. extern u16 __read_mostly tlb_lli_4k[NR_INFO];
  59. extern u16 __read_mostly tlb_lli_2m[NR_INFO];
  60. extern u16 __read_mostly tlb_lli_4m[NR_INFO];
  61. extern u16 __read_mostly tlb_lld_4k[NR_INFO];
  62. extern u16 __read_mostly tlb_lld_2m[NR_INFO];
  63. extern u16 __read_mostly tlb_lld_4m[NR_INFO];
  64. extern u16 __read_mostly tlb_lld_1g[NR_INFO];
  65. /*
  66. * CPU type and hardware bug flags. Kept separately for each CPU.
  67. * Members of this structure are referenced in head_32.S, so think twice
  68. * before touching them. [mj]
  69. */
  70. struct cpuinfo_x86 {
  71. __u8 x86; /* CPU family */
  72. __u8 x86_vendor; /* CPU vendor */
  73. __u8 x86_model;
  74. __u8 x86_stepping;
  75. #ifdef CONFIG_X86_64
  76. /* Number of 4K pages in DTLB/ITLB combined(in pages): */
  77. int x86_tlbsize;
  78. #endif
  79. #ifdef CONFIG_X86_VMX_FEATURE_NAMES
  80. __u32 vmx_capability[NVMXINTS];
  81. #endif
  82. __u8 x86_virt_bits;
  83. __u8 x86_phys_bits;
  84. /* CPUID returned core id bits: */
  85. __u8 x86_coreid_bits;
  86. __u8 cu_id;
  87. /* Max extended CPUID function supported: */
  88. __u32 extended_cpuid_level;
  89. /* Maximum supported CPUID level, -1=no CPUID: */
  90. int cpuid_level;
  91. /*
  92. * Align to size of unsigned long because the x86_capability array
  93. * is passed to bitops which require the alignment. Use unnamed
  94. * union to enforce the array is aligned to size of unsigned long.
  95. */
  96. union {
  97. __u32 x86_capability[NCAPINTS + NBUGINTS];
  98. unsigned long x86_capability_alignment;
  99. };
  100. char x86_vendor_id[16];
  101. char x86_model_id[64];
  102. /* in KB - valid for CPUS which support this call: */
  103. unsigned int x86_cache_size;
  104. int x86_cache_alignment; /* In bytes */
  105. /* Cache QoS architectural values, valid only on the BSP: */
  106. int x86_cache_max_rmid; /* max index */
  107. int x86_cache_occ_scale; /* scale to bytes */
  108. int x86_cache_mbm_width_offset;
  109. int x86_power;
  110. unsigned long loops_per_jiffy;
  111. /* protected processor identification number */
  112. u64 ppin;
  113. /* cpuid returned max cores value: */
  114. u16 x86_max_cores;
  115. u16 apicid;
  116. u16 initial_apicid;
  117. u16 x86_clflush_size;
  118. /* number of cores as seen by the OS: */
  119. u16 booted_cores;
  120. /* Physical processor id: */
  121. u16 phys_proc_id;
  122. /* Logical processor id: */
  123. u16 logical_proc_id;
  124. /* Core id: */
  125. u16 cpu_core_id;
  126. u16 cpu_die_id;
  127. u16 logical_die_id;
  128. /* Index into per_cpu list: */
  129. u16 cpu_index;
  130. /* Is SMT active on this core? */
  131. bool smt_active;
  132. u32 microcode;
  133. /* Address space bits used by the cache internally */
  134. u8 x86_cache_bits;
  135. unsigned initialized : 1;
  136. } __randomize_layout;
  137. struct cpuid_regs {
  138. u32 eax, ebx, ecx, edx;
  139. };
  140. enum cpuid_regs_idx {
  141. CPUID_EAX = 0,
  142. CPUID_EBX,
  143. CPUID_ECX,
  144. CPUID_EDX,
  145. };
  146. #define X86_VENDOR_INTEL 0
  147. #define X86_VENDOR_CYRIX 1
  148. #define X86_VENDOR_AMD 2
  149. #define X86_VENDOR_UMC 3
  150. #define X86_VENDOR_CENTAUR 5
  151. #define X86_VENDOR_TRANSMETA 7
  152. #define X86_VENDOR_NSC 8
  153. #define X86_VENDOR_HYGON 9
  154. #define X86_VENDOR_ZHAOXIN 10
  155. #define X86_VENDOR_VORTEX 11
  156. #define X86_VENDOR_NUM 12
  157. #define X86_VENDOR_UNKNOWN 0xff
  158. /*
  159. * capabilities of CPUs
  160. */
  161. extern struct cpuinfo_x86 boot_cpu_data;
  162. extern struct cpuinfo_x86 new_cpu_data;
  163. extern __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS];
  164. extern __u32 cpu_caps_set[NCAPINTS + NBUGINTS];
  165. #ifdef CONFIG_SMP
  166. DECLARE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
  167. #define cpu_data(cpu) per_cpu(cpu_info, cpu)
  168. #else
  169. #define cpu_info boot_cpu_data
  170. #define cpu_data(cpu) boot_cpu_data
  171. #endif
  172. extern const struct seq_operations cpuinfo_op;
  173. #define cache_line_size() (boot_cpu_data.x86_cache_alignment)
  174. extern void cpu_detect(struct cpuinfo_x86 *c);
  175. static inline unsigned long long l1tf_pfn_limit(void)
  176. {
  177. return BIT_ULL(boot_cpu_data.x86_cache_bits - 1 - PAGE_SHIFT);
  178. }
  179. extern void early_cpu_init(void);
  180. extern void identify_boot_cpu(void);
  181. extern void identify_secondary_cpu(struct cpuinfo_x86 *);
  182. extern void print_cpu_info(struct cpuinfo_x86 *);
  183. void print_cpu_msr(struct cpuinfo_x86 *);
  184. #ifdef CONFIG_X86_32
  185. extern int have_cpuid_p(void);
  186. #else
  187. static inline int have_cpuid_p(void)
  188. {
  189. return 1;
  190. }
  191. #endif
  192. static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
  193. unsigned int *ecx, unsigned int *edx)
  194. {
  195. /* ecx is often an input as well as an output. */
  196. asm volatile("cpuid"
  197. : "=a" (*eax),
  198. "=b" (*ebx),
  199. "=c" (*ecx),
  200. "=d" (*edx)
  201. : "0" (*eax), "2" (*ecx)
  202. : "memory");
  203. }
  204. #define native_cpuid_reg(reg) \
  205. static inline unsigned int native_cpuid_##reg(unsigned int op) \
  206. { \
  207. unsigned int eax = op, ebx, ecx = 0, edx; \
  208. \
  209. native_cpuid(&eax, &ebx, &ecx, &edx); \
  210. \
  211. return reg; \
  212. }
  213. /*
  214. * Native CPUID functions returning a single datum.
  215. */
  216. native_cpuid_reg(eax)
  217. native_cpuid_reg(ebx)
  218. native_cpuid_reg(ecx)
  219. native_cpuid_reg(edx)
  220. /*
  221. * Friendlier CR3 helpers.
  222. */
  223. static inline unsigned long read_cr3_pa(void)
  224. {
  225. return __read_cr3() & CR3_ADDR_MASK;
  226. }
  227. static inline unsigned long native_read_cr3_pa(void)
  228. {
  229. return __native_read_cr3() & CR3_ADDR_MASK;
  230. }
  231. static inline void load_cr3(pgd_t *pgdir)
  232. {
  233. write_cr3(__sme_pa(pgdir));
  234. }
  235. /*
  236. * Note that while the legacy 'TSS' name comes from 'Task State Segment',
  237. * on modern x86 CPUs the TSS also holds information important to 64-bit mode,
  238. * unrelated to the task-switch mechanism:
  239. */
  240. #ifdef CONFIG_X86_32
  241. /* This is the TSS defined by the hardware. */
  242. struct x86_hw_tss {
  243. unsigned short back_link, __blh;
  244. unsigned long sp0;
  245. unsigned short ss0, __ss0h;
  246. unsigned long sp1;
  247. /*
  248. * We don't use ring 1, so ss1 is a convenient scratch space in
  249. * the same cacheline as sp0. We use ss1 to cache the value in
  250. * MSR_IA32_SYSENTER_CS. When we context switch
  251. * MSR_IA32_SYSENTER_CS, we first check if the new value being
  252. * written matches ss1, and, if it's not, then we wrmsr the new
  253. * value and update ss1.
  254. *
  255. * The only reason we context switch MSR_IA32_SYSENTER_CS is
  256. * that we set it to zero in vm86 tasks to avoid corrupting the
  257. * stack if we were to go through the sysenter path from vm86
  258. * mode.
  259. */
  260. unsigned short ss1; /* MSR_IA32_SYSENTER_CS */
  261. unsigned short __ss1h;
  262. unsigned long sp2;
  263. unsigned short ss2, __ss2h;
  264. unsigned long __cr3;
  265. unsigned long ip;
  266. unsigned long flags;
  267. unsigned long ax;
  268. unsigned long cx;
  269. unsigned long dx;
  270. unsigned long bx;
  271. unsigned long sp;
  272. unsigned long bp;
  273. unsigned long si;
  274. unsigned long di;
  275. unsigned short es, __esh;
  276. unsigned short cs, __csh;
  277. unsigned short ss, __ssh;
  278. unsigned short ds, __dsh;
  279. unsigned short fs, __fsh;
  280. unsigned short gs, __gsh;
  281. unsigned short ldt, __ldth;
  282. unsigned short trace;
  283. unsigned short io_bitmap_base;
  284. } __attribute__((packed));
  285. #else
  286. struct x86_hw_tss {
  287. u32 reserved1;
  288. u64 sp0;
  289. u64 sp1;
  290. /*
  291. * Since Linux does not use ring 2, the 'sp2' slot is unused by
  292. * hardware. entry_SYSCALL_64 uses it as scratch space to stash
  293. * the user RSP value.
  294. */
  295. u64 sp2;
  296. u64 reserved2;
  297. u64 ist[7];
  298. u32 reserved3;
  299. u32 reserved4;
  300. u16 reserved5;
  301. u16 io_bitmap_base;
  302. } __attribute__((packed));
  303. #endif
  304. /*
  305. * IO-bitmap sizes:
  306. */
  307. #define IO_BITMAP_BITS 65536
  308. #define IO_BITMAP_BYTES (IO_BITMAP_BITS / BITS_PER_BYTE)
  309. #define IO_BITMAP_LONGS (IO_BITMAP_BYTES / sizeof(long))
  310. #define IO_BITMAP_OFFSET_VALID_MAP \
  311. (offsetof(struct tss_struct, io_bitmap.bitmap) - \
  312. offsetof(struct tss_struct, x86_tss))
  313. #define IO_BITMAP_OFFSET_VALID_ALL \
  314. (offsetof(struct tss_struct, io_bitmap.mapall) - \
  315. offsetof(struct tss_struct, x86_tss))
  316. #ifdef CONFIG_X86_IOPL_IOPERM
  317. /*
  318. * sizeof(unsigned long) coming from an extra "long" at the end of the
  319. * iobitmap. The limit is inclusive, i.e. the last valid byte.
  320. */
  321. # define __KERNEL_TSS_LIMIT \
  322. (IO_BITMAP_OFFSET_VALID_ALL + IO_BITMAP_BYTES + \
  323. sizeof(unsigned long) - 1)
  324. #else
  325. # define __KERNEL_TSS_LIMIT \
  326. (offsetof(struct tss_struct, x86_tss) + sizeof(struct x86_hw_tss) - 1)
  327. #endif
  328. /* Base offset outside of TSS_LIMIT so unpriviledged IO causes #GP */
  329. #define IO_BITMAP_OFFSET_INVALID (__KERNEL_TSS_LIMIT + 1)
  330. struct entry_stack {
  331. char stack[PAGE_SIZE];
  332. };
  333. struct entry_stack_page {
  334. struct entry_stack stack;
  335. } __aligned(PAGE_SIZE);
  336. /*
  337. * All IO bitmap related data stored in the TSS:
  338. */
  339. struct x86_io_bitmap {
  340. /* The sequence number of the last active bitmap. */
  341. u64 prev_sequence;
  342. /*
  343. * Store the dirty size of the last io bitmap offender. The next
  344. * one will have to do the cleanup as the switch out to a non io
  345. * bitmap user will just set x86_tss.io_bitmap_base to a value
  346. * outside of the TSS limit. So for sane tasks there is no need to
  347. * actually touch the io_bitmap at all.
  348. */
  349. unsigned int prev_max;
  350. /*
  351. * The extra 1 is there because the CPU will access an
  352. * additional byte beyond the end of the IO permission
  353. * bitmap. The extra byte must be all 1 bits, and must
  354. * be within the limit.
  355. */
  356. unsigned long bitmap[IO_BITMAP_LONGS + 1];
  357. /*
  358. * Special I/O bitmap to emulate IOPL(3). All bytes zero,
  359. * except the additional byte at the end.
  360. */
  361. unsigned long mapall[IO_BITMAP_LONGS + 1];
  362. };
  363. struct tss_struct {
  364. /*
  365. * The fixed hardware portion. This must not cross a page boundary
  366. * at risk of violating the SDM's advice and potentially triggering
  367. * errata.
  368. */
  369. struct x86_hw_tss x86_tss;
  370. struct x86_io_bitmap io_bitmap;
  371. } __aligned(PAGE_SIZE);
  372. DECLARE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw);
  373. /* Per CPU interrupt stacks */
  374. struct irq_stack {
  375. char stack[IRQ_STACK_SIZE];
  376. } __aligned(IRQ_STACK_SIZE);
  377. DECLARE_PER_CPU(unsigned long, cpu_current_top_of_stack);
  378. #ifdef CONFIG_X86_64
  379. struct fixed_percpu_data {
  380. /*
  381. * GCC hardcodes the stack canary as %gs:40. Since the
  382. * irq_stack is the object at %gs:0, we reserve the bottom
  383. * 48 bytes of the irq stack for the canary.
  384. *
  385. * Once we are willing to require -mstack-protector-guard-symbol=
  386. * support for x86_64 stackprotector, we can get rid of this.
  387. */
  388. char gs_base[40];
  389. unsigned long stack_canary;
  390. };
  391. DECLARE_PER_CPU_FIRST(struct fixed_percpu_data, fixed_percpu_data) __visible;
  392. DECLARE_INIT_PER_CPU(fixed_percpu_data);
  393. static inline unsigned long cpu_kernelmode_gs_base(int cpu)
  394. {
  395. return (unsigned long)per_cpu(fixed_percpu_data.gs_base, cpu);
  396. }
  397. DECLARE_PER_CPU(void *, hardirq_stack_ptr);
  398. DECLARE_PER_CPU(bool, hardirq_stack_inuse);
  399. extern asmlinkage void ignore_sysret(void);
  400. /* Save actual FS/GS selectors and bases to current->thread */
  401. void current_save_fsgs(void);
  402. #else /* X86_64 */
  403. #ifdef CONFIG_STACKPROTECTOR
  404. DECLARE_PER_CPU(unsigned long, __stack_chk_guard);
  405. #endif
  406. DECLARE_PER_CPU(struct irq_stack *, hardirq_stack_ptr);
  407. DECLARE_PER_CPU(struct irq_stack *, softirq_stack_ptr);
  408. #endif /* !X86_64 */
  409. struct perf_event;
  410. struct thread_struct {
  411. /* Cached TLS descriptors: */
  412. struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
  413. #ifdef CONFIG_X86_32
  414. unsigned long sp0;
  415. #endif
  416. unsigned long sp;
  417. #ifdef CONFIG_X86_32
  418. unsigned long sysenter_cs;
  419. #else
  420. unsigned short es;
  421. unsigned short ds;
  422. unsigned short fsindex;
  423. unsigned short gsindex;
  424. #endif
  425. #ifdef CONFIG_X86_64
  426. unsigned long fsbase;
  427. unsigned long gsbase;
  428. #else
  429. /*
  430. * XXX: this could presumably be unsigned short. Alternatively,
  431. * 32-bit kernels could be taught to use fsindex instead.
  432. */
  433. unsigned long fs;
  434. unsigned long gs;
  435. #endif
  436. /* Save middle states of ptrace breakpoints */
  437. struct perf_event *ptrace_bps[HBP_NUM];
  438. /* Debug status used for traps, single steps, etc... */
  439. unsigned long virtual_dr6;
  440. /* Keep track of the exact dr7 value set by the user */
  441. unsigned long ptrace_dr7;
  442. /* Fault info: */
  443. unsigned long cr2;
  444. unsigned long trap_nr;
  445. unsigned long error_code;
  446. #ifdef CONFIG_VM86
  447. /* Virtual 86 mode info */
  448. struct vm86 *vm86;
  449. #endif
  450. /* IO permissions: */
  451. struct io_bitmap *io_bitmap;
  452. /*
  453. * IOPL. Privilege level dependent I/O permission which is
  454. * emulated via the I/O bitmap to prevent user space from disabling
  455. * interrupts.
  456. */
  457. unsigned long iopl_emul;
  458. unsigned int iopl_warn:1;
  459. unsigned int sig_on_uaccess_err:1;
  460. /*
  461. * Protection Keys Register for Userspace. Loaded immediately on
  462. * context switch. Store it in thread_struct to avoid a lookup in
  463. * the tasks's FPU xstate buffer. This value is only valid when a
  464. * task is scheduled out. For 'current' the authoritative source of
  465. * PKRU is the hardware itself.
  466. */
  467. u32 pkru;
  468. /* Floating point and extended processor state */
  469. struct fpu fpu;
  470. /*
  471. * WARNING: 'fpu' is dynamically-sized. It *MUST* be at
  472. * the end.
  473. */
  474. };
  475. extern void fpu_thread_struct_whitelist(unsigned long *offset, unsigned long *size);
  476. static inline void arch_thread_struct_whitelist(unsigned long *offset,
  477. unsigned long *size)
  478. {
  479. fpu_thread_struct_whitelist(offset, size);
  480. }
  481. static inline void
  482. native_load_sp0(unsigned long sp0)
  483. {
  484. this_cpu_write(cpu_tss_rw.x86_tss.sp0, sp0);
  485. }
  486. static __always_inline void native_swapgs(void)
  487. {
  488. #ifdef CONFIG_X86_64
  489. asm volatile("swapgs" ::: "memory");
  490. #endif
  491. }
  492. static __always_inline unsigned long current_top_of_stack(void)
  493. {
  494. /*
  495. * We can't read directly from tss.sp0: sp0 on x86_32 is special in
  496. * and around vm86 mode and sp0 on x86_64 is special because of the
  497. * entry trampoline.
  498. */
  499. return this_cpu_read_stable(cpu_current_top_of_stack);
  500. }
  501. static __always_inline bool on_thread_stack(void)
  502. {
  503. return (unsigned long)(current_top_of_stack() -
  504. current_stack_pointer) < THREAD_SIZE;
  505. }
  506. #ifdef CONFIG_PARAVIRT_XXL
  507. #include <asm/paravirt.h>
  508. #else
  509. #define __cpuid native_cpuid
  510. static inline void load_sp0(unsigned long sp0)
  511. {
  512. native_load_sp0(sp0);
  513. }
  514. #endif /* CONFIG_PARAVIRT_XXL */
  515. unsigned long __get_wchan(struct task_struct *p);
  516. /*
  517. * Generic CPUID function
  518. * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
  519. * resulting in stale register contents being returned.
  520. */
  521. static inline void cpuid(unsigned int op,
  522. unsigned int *eax, unsigned int *ebx,
  523. unsigned int *ecx, unsigned int *edx)
  524. {
  525. *eax = op;
  526. *ecx = 0;
  527. __cpuid(eax, ebx, ecx, edx);
  528. }
  529. /* Some CPUID calls want 'count' to be placed in ecx */
  530. static inline void cpuid_count(unsigned int op, int count,
  531. unsigned int *eax, unsigned int *ebx,
  532. unsigned int *ecx, unsigned int *edx)
  533. {
  534. *eax = op;
  535. *ecx = count;
  536. __cpuid(eax, ebx, ecx, edx);
  537. }
  538. /*
  539. * CPUID functions returning a single datum
  540. */
  541. static inline unsigned int cpuid_eax(unsigned int op)
  542. {
  543. unsigned int eax, ebx, ecx, edx;
  544. cpuid(op, &eax, &ebx, &ecx, &edx);
  545. return eax;
  546. }
  547. static inline unsigned int cpuid_ebx(unsigned int op)
  548. {
  549. unsigned int eax, ebx, ecx, edx;
  550. cpuid(op, &eax, &ebx, &ecx, &edx);
  551. return ebx;
  552. }
  553. static inline unsigned int cpuid_ecx(unsigned int op)
  554. {
  555. unsigned int eax, ebx, ecx, edx;
  556. cpuid(op, &eax, &ebx, &ecx, &edx);
  557. return ecx;
  558. }
  559. static inline unsigned int cpuid_edx(unsigned int op)
  560. {
  561. unsigned int eax, ebx, ecx, edx;
  562. cpuid(op, &eax, &ebx, &ecx, &edx);
  563. return edx;
  564. }
  565. extern void select_idle_routine(const struct cpuinfo_x86 *c);
  566. extern void amd_e400_c1e_apic_setup(void);
  567. extern unsigned long boot_option_idle_override;
  568. enum idle_boot_override {IDLE_NO_OVERRIDE=0, IDLE_HALT, IDLE_NOMWAIT,
  569. IDLE_POLL};
  570. extern void enable_sep_cpu(void);
  571. extern int sysenter_setup(void);
  572. /* Defined in head.S */
  573. extern struct desc_ptr early_gdt_descr;
  574. extern void switch_to_new_gdt(int);
  575. extern void load_direct_gdt(int);
  576. extern void load_fixmap_gdt(int);
  577. extern void load_percpu_segment(int);
  578. extern void cpu_init(void);
  579. extern void cpu_init_secondary(void);
  580. extern void cpu_init_exception_handling(void);
  581. extern void cr4_init(void);
  582. static inline unsigned long get_debugctlmsr(void)
  583. {
  584. unsigned long debugctlmsr = 0;
  585. #ifndef CONFIG_X86_DEBUGCTLMSR
  586. if (boot_cpu_data.x86 < 6)
  587. return 0;
  588. #endif
  589. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
  590. return debugctlmsr;
  591. }
  592. static inline void update_debugctlmsr(unsigned long debugctlmsr)
  593. {
  594. #ifndef CONFIG_X86_DEBUGCTLMSR
  595. if (boot_cpu_data.x86 < 6)
  596. return;
  597. #endif
  598. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
  599. }
  600. extern void set_task_blockstep(struct task_struct *task, bool on);
  601. /* Boot loader type from the setup header: */
  602. extern int bootloader_type;
  603. extern int bootloader_version;
  604. extern char ignore_fpu_irq;
  605. #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
  606. #define ARCH_HAS_PREFETCHW
  607. #define ARCH_HAS_SPINLOCK_PREFETCH
  608. #ifdef CONFIG_X86_32
  609. # define BASE_PREFETCH ""
  610. # define ARCH_HAS_PREFETCH
  611. #else
  612. # define BASE_PREFETCH "prefetcht0 %P1"
  613. #endif
  614. /*
  615. * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
  616. *
  617. * It's not worth to care about 3dnow prefetches for the K6
  618. * because they are microcoded there and very slow.
  619. */
  620. static inline void prefetch(const void *x)
  621. {
  622. alternative_input(BASE_PREFETCH, "prefetchnta %P1",
  623. X86_FEATURE_XMM,
  624. "m" (*(const char *)x));
  625. }
  626. /*
  627. * 3dnow prefetch to get an exclusive cache line.
  628. * Useful for spinlocks to avoid one state transition in the
  629. * cache coherency protocol:
  630. */
  631. static __always_inline void prefetchw(const void *x)
  632. {
  633. alternative_input(BASE_PREFETCH, "prefetchw %P1",
  634. X86_FEATURE_3DNOWPREFETCH,
  635. "m" (*(const char *)x));
  636. }
  637. static inline void spin_lock_prefetch(const void *x)
  638. {
  639. prefetchw(x);
  640. }
  641. #define TOP_OF_INIT_STACK ((unsigned long)&init_stack + sizeof(init_stack) - \
  642. TOP_OF_KERNEL_STACK_PADDING)
  643. #define task_top_of_stack(task) ((unsigned long)(task_pt_regs(task) + 1))
  644. #define task_pt_regs(task) \
  645. ({ \
  646. unsigned long __ptr = (unsigned long)task_stack_page(task); \
  647. __ptr += THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING; \
  648. ((struct pt_regs *)__ptr) - 1; \
  649. })
  650. #ifdef CONFIG_X86_32
  651. #define INIT_THREAD { \
  652. .sp0 = TOP_OF_INIT_STACK, \
  653. .sysenter_cs = __KERNEL_CS, \
  654. }
  655. #define KSTK_ESP(task) (task_pt_regs(task)->sp)
  656. #else
  657. #define INIT_THREAD { }
  658. extern unsigned long KSTK_ESP(struct task_struct *task);
  659. #endif /* CONFIG_X86_64 */
  660. extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
  661. unsigned long new_sp);
  662. /*
  663. * This decides where the kernel will search for a free chunk of vm
  664. * space during mmap's.
  665. */
  666. #define __TASK_UNMAPPED_BASE(task_size) (PAGE_ALIGN(task_size / 3))
  667. #define TASK_UNMAPPED_BASE __TASK_UNMAPPED_BASE(TASK_SIZE_LOW)
  668. #define KSTK_EIP(task) (task_pt_regs(task)->ip)
  669. /* Get/set a process' ability to use the timestamp counter instruction */
  670. #define GET_TSC_CTL(adr) get_tsc_mode((adr))
  671. #define SET_TSC_CTL(val) set_tsc_mode((val))
  672. extern int get_tsc_mode(unsigned long adr);
  673. extern int set_tsc_mode(unsigned int val);
  674. DECLARE_PER_CPU(u64, msr_misc_features_shadow);
  675. extern u16 get_llc_id(unsigned int cpu);
  676. #ifdef CONFIG_CPU_SUP_AMD
  677. extern u32 amd_get_nodes_per_socket(void);
  678. extern u32 amd_get_highest_perf(void);
  679. extern bool cpu_has_ibpb_brtype_microcode(void);
  680. extern void amd_clear_divider(void);
  681. #else
  682. static inline u32 amd_get_nodes_per_socket(void) { return 0; }
  683. static inline u32 amd_get_highest_perf(void) { return 0; }
  684. static inline bool cpu_has_ibpb_brtype_microcode(void) { return false; }
  685. static inline void amd_clear_divider(void) { }
  686. #endif
  687. #define for_each_possible_hypervisor_cpuid_base(function) \
  688. for (function = 0x40000000; function < 0x40010000; function += 0x100)
  689. static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
  690. {
  691. uint32_t base, eax, signature[3];
  692. for_each_possible_hypervisor_cpuid_base(base) {
  693. cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
  694. if (!memcmp(sig, signature, 12) &&
  695. (leaves == 0 || ((eax - base) >= leaves)))
  696. return base;
  697. }
  698. return 0;
  699. }
  700. extern unsigned long arch_align_stack(unsigned long sp);
  701. void free_init_pages(const char *what, unsigned long begin, unsigned long end);
  702. extern void free_kernel_image_pages(const char *what, void *begin, void *end);
  703. void default_idle(void);
  704. #ifdef CONFIG_XEN
  705. bool xen_set_default_idle(void);
  706. #else
  707. #define xen_set_default_idle 0
  708. #endif
  709. void __noreturn stop_this_cpu(void *dummy);
  710. void microcode_check(struct cpuinfo_x86 *prev_info);
  711. void store_cpu_caps(struct cpuinfo_x86 *info);
  712. enum l1tf_mitigations {
  713. L1TF_MITIGATION_OFF,
  714. L1TF_MITIGATION_FLUSH_NOWARN,
  715. L1TF_MITIGATION_FLUSH,
  716. L1TF_MITIGATION_FLUSH_NOSMT,
  717. L1TF_MITIGATION_FULL,
  718. L1TF_MITIGATION_FULL_FORCE
  719. };
  720. extern enum l1tf_mitigations l1tf_mitigation;
  721. enum mds_mitigations {
  722. MDS_MITIGATION_OFF,
  723. MDS_MITIGATION_FULL,
  724. MDS_MITIGATION_VMWERV,
  725. };
  726. #ifdef CONFIG_X86_SGX
  727. int arch_memory_failure(unsigned long pfn, int flags);
  728. #define arch_memory_failure arch_memory_failure
  729. bool arch_is_platform_page(u64 paddr);
  730. #define arch_is_platform_page arch_is_platform_page
  731. #endif
  732. extern bool gds_ucode_mitigated(void);
  733. #endif /* _ASM_X86_PROCESSOR_H */