processor.h 25 KB

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