processor.h 25 KB

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