processor.h 25 KB

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