process.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  3. #include <linux/errno.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/smp.h>
  7. #include <linux/cpu.h>
  8. #include <linux/prctl.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched.h>
  11. #include <linux/sched/idle.h>
  12. #include <linux/sched/debug.h>
  13. #include <linux/sched/task.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <linux/init.h>
  16. #include <linux/export.h>
  17. #include <linux/pm.h>
  18. #include <linux/tick.h>
  19. #include <linux/random.h>
  20. #include <linux/user-return-notifier.h>
  21. #include <linux/dmi.h>
  22. #include <linux/utsname.h>
  23. #include <linux/stackprotector.h>
  24. #include <linux/cpuidle.h>
  25. #include <linux/acpi.h>
  26. #include <linux/elf-randomize.h>
  27. #include <linux/static_call.h>
  28. #include <trace/events/power.h>
  29. #include <linux/hw_breakpoint.h>
  30. #include <linux/entry-common.h>
  31. #include <asm/cpu.h>
  32. #include <asm/apic.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/mwait.h>
  35. #include <asm/fpu/api.h>
  36. #include <asm/fpu/sched.h>
  37. #include <asm/fpu/xstate.h>
  38. #include <asm/debugreg.h>
  39. #include <asm/nmi.h>
  40. #include <asm/tlbflush.h>
  41. #include <asm/mce.h>
  42. #include <asm/vm86.h>
  43. #include <asm/switch_to.h>
  44. #include <asm/desc.h>
  45. #include <asm/prctl.h>
  46. #include <asm/spec-ctrl.h>
  47. #include <asm/io_bitmap.h>
  48. #include <asm/proto.h>
  49. #include <asm/frame.h>
  50. #include <asm/unwind.h>
  51. #include <asm/tdx.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/shstk.h>
  54. #include "process.h"
  55. /*
  56. * per-CPU TSS segments. Threads are completely 'soft' on Linux,
  57. * no more per-task TSS's. The TSS size is kept cacheline-aligned
  58. * so they are allowed to end up in the .data..cacheline_aligned
  59. * section. Since TSS's are completely CPU-local, we want them
  60. * on exact cacheline boundaries, to eliminate cacheline ping-pong.
  61. */
  62. __visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = {
  63. .x86_tss = {
  64. /*
  65. * .sp0 is only used when entering ring 0 from a lower
  66. * privilege level. Since the init task never runs anything
  67. * but ring 0 code, there is no need for a valid value here.
  68. * Poison it.
  69. */
  70. .sp0 = (1UL << (BITS_PER_LONG-1)) + 1,
  71. #ifdef CONFIG_X86_32
  72. .sp1 = TOP_OF_INIT_STACK,
  73. .ss0 = __KERNEL_DS,
  74. .ss1 = __KERNEL_CS,
  75. #endif
  76. .io_bitmap_base = IO_BITMAP_OFFSET_INVALID,
  77. },
  78. };
  79. EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);
  80. DEFINE_PER_CPU(bool, __tss_limit_invalid);
  81. EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
  82. /*
  83. * this gets called so that we can store lazy state into memory and copy the
  84. * current task into the new thread.
  85. */
  86. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  87. {
  88. /* init_task is not dynamically sized (incomplete FPU state) */
  89. if (unlikely(src == &init_task))
  90. memcpy_and_pad(dst, arch_task_struct_size, src, sizeof(init_task), 0);
  91. else
  92. memcpy(dst, src, arch_task_struct_size);
  93. #ifdef CONFIG_VM86
  94. dst->thread.vm86 = NULL;
  95. #endif
  96. /* Drop the copied pointer to current's fpstate */
  97. dst->thread.fpu.fpstate = NULL;
  98. return 0;
  99. }
  100. #ifdef CONFIG_X86_64
  101. void arch_release_task_struct(struct task_struct *tsk)
  102. {
  103. if (fpu_state_size_dynamic())
  104. fpstate_free(&tsk->thread.fpu);
  105. }
  106. #endif
  107. /*
  108. * Free thread data structures etc..
  109. */
  110. void exit_thread(struct task_struct *tsk)
  111. {
  112. struct thread_struct *t = &tsk->thread;
  113. struct fpu *fpu = &t->fpu;
  114. if (test_thread_flag(TIF_IO_BITMAP))
  115. io_bitmap_exit(tsk);
  116. free_vm86(t);
  117. shstk_free(tsk);
  118. fpu__drop(fpu);
  119. }
  120. static int set_new_tls(struct task_struct *p, unsigned long tls)
  121. {
  122. struct user_desc __user *utls = (struct user_desc __user *)tls;
  123. if (in_ia32_syscall())
  124. return do_set_thread_area(p, -1, utls, 0);
  125. else
  126. return do_set_thread_area_64(p, ARCH_SET_FS, tls);
  127. }
  128. __visible void ret_from_fork(struct task_struct *prev, struct pt_regs *regs,
  129. int (*fn)(void *), void *fn_arg)
  130. {
  131. schedule_tail(prev);
  132. /* Is this a kernel thread? */
  133. if (unlikely(fn)) {
  134. fn(fn_arg);
  135. /*
  136. * A kernel thread is allowed to return here after successfully
  137. * calling kernel_execve(). Exit to userspace to complete the
  138. * execve() syscall.
  139. */
  140. regs->ax = 0;
  141. }
  142. syscall_exit_to_user_mode(regs);
  143. }
  144. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  145. {
  146. unsigned long clone_flags = args->flags;
  147. unsigned long sp = args->stack;
  148. unsigned long tls = args->tls;
  149. struct inactive_task_frame *frame;
  150. struct fork_frame *fork_frame;
  151. struct pt_regs *childregs;
  152. unsigned long new_ssp;
  153. int ret = 0;
  154. childregs = task_pt_regs(p);
  155. fork_frame = container_of(childregs, struct fork_frame, regs);
  156. frame = &fork_frame->frame;
  157. frame->bp = encode_frame_pointer(childregs);
  158. frame->ret_addr = (unsigned long) ret_from_fork_asm;
  159. p->thread.sp = (unsigned long) fork_frame;
  160. p->thread.io_bitmap = NULL;
  161. clear_tsk_thread_flag(p, TIF_IO_BITMAP);
  162. p->thread.iopl_warn = 0;
  163. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  164. #ifdef CONFIG_X86_64
  165. current_save_fsgs();
  166. p->thread.fsindex = current->thread.fsindex;
  167. p->thread.fsbase = current->thread.fsbase;
  168. p->thread.gsindex = current->thread.gsindex;
  169. p->thread.gsbase = current->thread.gsbase;
  170. savesegment(es, p->thread.es);
  171. savesegment(ds, p->thread.ds);
  172. if (p->mm && (clone_flags & (CLONE_VM | CLONE_VFORK)) == CLONE_VM)
  173. set_bit(MM_CONTEXT_LOCK_LAM, &p->mm->context.flags);
  174. #else
  175. p->thread.sp0 = (unsigned long) (childregs + 1);
  176. savesegment(gs, p->thread.gs);
  177. /*
  178. * Clear all status flags including IF and set fixed bit. 64bit
  179. * does not have this initialization as the frame does not contain
  180. * flags. The flags consistency (especially vs. AC) is there
  181. * ensured via objtool, which lacks 32bit support.
  182. */
  183. frame->flags = X86_EFLAGS_FIXED;
  184. #endif
  185. /*
  186. * Allocate a new shadow stack for thread if needed. If shadow stack,
  187. * is disabled, new_ssp will remain 0, and fpu_clone() will know not to
  188. * update it.
  189. */
  190. new_ssp = shstk_alloc_thread_stack(p, clone_flags, args->stack_size);
  191. if (IS_ERR_VALUE(new_ssp))
  192. return PTR_ERR((void *)new_ssp);
  193. fpu_clone(p, clone_flags, args->fn, new_ssp);
  194. /* Kernel thread ? */
  195. if (unlikely(p->flags & PF_KTHREAD)) {
  196. p->thread.pkru = pkru_get_init_value();
  197. memset(childregs, 0, sizeof(struct pt_regs));
  198. kthread_frame_init(frame, args->fn, args->fn_arg);
  199. return 0;
  200. }
  201. /*
  202. * Clone current's PKRU value from hardware. tsk->thread.pkru
  203. * is only valid when scheduled out.
  204. */
  205. p->thread.pkru = read_pkru();
  206. frame->bx = 0;
  207. *childregs = *current_pt_regs();
  208. childregs->ax = 0;
  209. if (sp)
  210. childregs->sp = sp;
  211. if (unlikely(args->fn)) {
  212. /*
  213. * A user space thread, but it doesn't return to
  214. * ret_after_fork().
  215. *
  216. * In order to indicate that to tools like gdb,
  217. * we reset the stack and instruction pointers.
  218. *
  219. * It does the same kernel frame setup to return to a kernel
  220. * function that a kernel thread does.
  221. */
  222. childregs->sp = 0;
  223. childregs->ip = 0;
  224. kthread_frame_init(frame, args->fn, args->fn_arg);
  225. return 0;
  226. }
  227. /* Set a new TLS for the child thread? */
  228. if (clone_flags & CLONE_SETTLS)
  229. ret = set_new_tls(p, tls);
  230. if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP)))
  231. io_bitmap_share(p);
  232. return ret;
  233. }
  234. static void pkru_flush_thread(void)
  235. {
  236. /*
  237. * If PKRU is enabled the default PKRU value has to be loaded into
  238. * the hardware right here (similar to context switch).
  239. */
  240. pkru_write_default();
  241. }
  242. void flush_thread(void)
  243. {
  244. struct task_struct *tsk = current;
  245. flush_ptrace_hw_breakpoint(tsk);
  246. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  247. fpu_flush_thread();
  248. pkru_flush_thread();
  249. }
  250. void disable_TSC(void)
  251. {
  252. preempt_disable();
  253. if (!test_and_set_thread_flag(TIF_NOTSC))
  254. /*
  255. * Must flip the CPU state synchronously with
  256. * TIF_NOTSC in the current running context.
  257. */
  258. cr4_set_bits(X86_CR4_TSD);
  259. preempt_enable();
  260. }
  261. static void enable_TSC(void)
  262. {
  263. preempt_disable();
  264. if (test_and_clear_thread_flag(TIF_NOTSC))
  265. /*
  266. * Must flip the CPU state synchronously with
  267. * TIF_NOTSC in the current running context.
  268. */
  269. cr4_clear_bits(X86_CR4_TSD);
  270. preempt_enable();
  271. }
  272. int get_tsc_mode(unsigned long adr)
  273. {
  274. unsigned int val;
  275. if (test_thread_flag(TIF_NOTSC))
  276. val = PR_TSC_SIGSEGV;
  277. else
  278. val = PR_TSC_ENABLE;
  279. return put_user(val, (unsigned int __user *)adr);
  280. }
  281. int set_tsc_mode(unsigned int val)
  282. {
  283. if (val == PR_TSC_SIGSEGV)
  284. disable_TSC();
  285. else if (val == PR_TSC_ENABLE)
  286. enable_TSC();
  287. else
  288. return -EINVAL;
  289. return 0;
  290. }
  291. DEFINE_PER_CPU(u64, msr_misc_features_shadow);
  292. static void set_cpuid_faulting(bool on)
  293. {
  294. u64 msrval;
  295. msrval = this_cpu_read(msr_misc_features_shadow);
  296. msrval &= ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
  297. msrval |= (on << MSR_MISC_FEATURES_ENABLES_CPUID_FAULT_BIT);
  298. this_cpu_write(msr_misc_features_shadow, msrval);
  299. wrmsrl(MSR_MISC_FEATURES_ENABLES, msrval);
  300. }
  301. static void disable_cpuid(void)
  302. {
  303. preempt_disable();
  304. if (!test_and_set_thread_flag(TIF_NOCPUID)) {
  305. /*
  306. * Must flip the CPU state synchronously with
  307. * TIF_NOCPUID in the current running context.
  308. */
  309. set_cpuid_faulting(true);
  310. }
  311. preempt_enable();
  312. }
  313. static void enable_cpuid(void)
  314. {
  315. preempt_disable();
  316. if (test_and_clear_thread_flag(TIF_NOCPUID)) {
  317. /*
  318. * Must flip the CPU state synchronously with
  319. * TIF_NOCPUID in the current running context.
  320. */
  321. set_cpuid_faulting(false);
  322. }
  323. preempt_enable();
  324. }
  325. static int get_cpuid_mode(void)
  326. {
  327. return !test_thread_flag(TIF_NOCPUID);
  328. }
  329. static int set_cpuid_mode(unsigned long cpuid_enabled)
  330. {
  331. if (!boot_cpu_has(X86_FEATURE_CPUID_FAULT))
  332. return -ENODEV;
  333. if (cpuid_enabled)
  334. enable_cpuid();
  335. else
  336. disable_cpuid();
  337. return 0;
  338. }
  339. /*
  340. * Called immediately after a successful exec.
  341. */
  342. void arch_setup_new_exec(void)
  343. {
  344. /* If cpuid was previously disabled for this task, re-enable it. */
  345. if (test_thread_flag(TIF_NOCPUID))
  346. enable_cpuid();
  347. /*
  348. * Don't inherit TIF_SSBD across exec boundary when
  349. * PR_SPEC_DISABLE_NOEXEC is used.
  350. */
  351. if (test_thread_flag(TIF_SSBD) &&
  352. task_spec_ssb_noexec(current)) {
  353. clear_thread_flag(TIF_SSBD);
  354. task_clear_spec_ssb_disable(current);
  355. task_clear_spec_ssb_noexec(current);
  356. speculation_ctrl_update(read_thread_flags());
  357. }
  358. mm_reset_untag_mask(current->mm);
  359. }
  360. #ifdef CONFIG_X86_IOPL_IOPERM
  361. static inline void switch_to_bitmap(unsigned long tifp)
  362. {
  363. /*
  364. * Invalidate I/O bitmap if the previous task used it. This prevents
  365. * any possible leakage of an active I/O bitmap.
  366. *
  367. * If the next task has an I/O bitmap it will handle it on exit to
  368. * user mode.
  369. */
  370. if (tifp & _TIF_IO_BITMAP)
  371. tss_invalidate_io_bitmap();
  372. }
  373. static void tss_copy_io_bitmap(struct tss_struct *tss, struct io_bitmap *iobm)
  374. {
  375. /*
  376. * Copy at least the byte range of the incoming tasks bitmap which
  377. * covers the permitted I/O ports.
  378. *
  379. * If the previous task which used an I/O bitmap had more bits
  380. * permitted, then the copy needs to cover those as well so they
  381. * get turned off.
  382. */
  383. memcpy(tss->io_bitmap.bitmap, iobm->bitmap,
  384. max(tss->io_bitmap.prev_max, iobm->max));
  385. /*
  386. * Store the new max and the sequence number of this bitmap
  387. * and a pointer to the bitmap itself.
  388. */
  389. tss->io_bitmap.prev_max = iobm->max;
  390. tss->io_bitmap.prev_sequence = iobm->sequence;
  391. }
  392. /**
  393. * native_tss_update_io_bitmap - Update I/O bitmap before exiting to user mode
  394. */
  395. void native_tss_update_io_bitmap(void)
  396. {
  397. struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
  398. struct thread_struct *t = &current->thread;
  399. u16 *base = &tss->x86_tss.io_bitmap_base;
  400. if (!test_thread_flag(TIF_IO_BITMAP)) {
  401. native_tss_invalidate_io_bitmap();
  402. return;
  403. }
  404. if (IS_ENABLED(CONFIG_X86_IOPL_IOPERM) && t->iopl_emul == 3) {
  405. *base = IO_BITMAP_OFFSET_VALID_ALL;
  406. } else {
  407. struct io_bitmap *iobm = t->io_bitmap;
  408. if (WARN_ON_ONCE(!iobm)) {
  409. clear_thread_flag(TIF_IO_BITMAP);
  410. native_tss_invalidate_io_bitmap();
  411. }
  412. /*
  413. * Only copy bitmap data when the sequence number differs. The
  414. * update time is accounted to the incoming task.
  415. */
  416. if (tss->io_bitmap.prev_sequence != iobm->sequence)
  417. tss_copy_io_bitmap(tss, iobm);
  418. /* Enable the bitmap */
  419. *base = IO_BITMAP_OFFSET_VALID_MAP;
  420. }
  421. /*
  422. * Make sure that the TSS limit is covering the IO bitmap. It might have
  423. * been cut down by a VMEXIT to 0x67 which would cause a subsequent I/O
  424. * access from user space to trigger a #GP because the bitmap is outside
  425. * the TSS limit.
  426. */
  427. refresh_tss_limit();
  428. }
  429. #else /* CONFIG_X86_IOPL_IOPERM */
  430. static inline void switch_to_bitmap(unsigned long tifp) { }
  431. #endif
  432. #ifdef CONFIG_SMP
  433. struct ssb_state {
  434. struct ssb_state *shared_state;
  435. raw_spinlock_t lock;
  436. unsigned int disable_state;
  437. unsigned long local_state;
  438. };
  439. #define LSTATE_SSB 0
  440. static DEFINE_PER_CPU(struct ssb_state, ssb_state);
  441. void speculative_store_bypass_ht_init(void)
  442. {
  443. struct ssb_state *st = this_cpu_ptr(&ssb_state);
  444. unsigned int this_cpu = smp_processor_id();
  445. unsigned int cpu;
  446. st->local_state = 0;
  447. /*
  448. * Shared state setup happens once on the first bringup
  449. * of the CPU. It's not destroyed on CPU hotunplug.
  450. */
  451. if (st->shared_state)
  452. return;
  453. raw_spin_lock_init(&st->lock);
  454. /*
  455. * Go over HT siblings and check whether one of them has set up the
  456. * shared state pointer already.
  457. */
  458. for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {
  459. if (cpu == this_cpu)
  460. continue;
  461. if (!per_cpu(ssb_state, cpu).shared_state)
  462. continue;
  463. /* Link it to the state of the sibling: */
  464. st->shared_state = per_cpu(ssb_state, cpu).shared_state;
  465. return;
  466. }
  467. /*
  468. * First HT sibling to come up on the core. Link shared state of
  469. * the first HT sibling to itself. The siblings on the same core
  470. * which come up later will see the shared state pointer and link
  471. * themselves to the state of this CPU.
  472. */
  473. st->shared_state = st;
  474. }
  475. /*
  476. * Logic is: First HT sibling enables SSBD for both siblings in the core
  477. * and last sibling to disable it, disables it for the whole core. This how
  478. * MSR_SPEC_CTRL works in "hardware":
  479. *
  480. * CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL
  481. */
  482. static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
  483. {
  484. struct ssb_state *st = this_cpu_ptr(&ssb_state);
  485. u64 msr = x86_amd_ls_cfg_base;
  486. if (!static_cpu_has(X86_FEATURE_ZEN)) {
  487. msr |= ssbd_tif_to_amd_ls_cfg(tifn);
  488. wrmsrl(MSR_AMD64_LS_CFG, msr);
  489. return;
  490. }
  491. if (tifn & _TIF_SSBD) {
  492. /*
  493. * Since this can race with prctl(), block reentry on the
  494. * same CPU.
  495. */
  496. if (__test_and_set_bit(LSTATE_SSB, &st->local_state))
  497. return;
  498. msr |= x86_amd_ls_cfg_ssbd_mask;
  499. raw_spin_lock(&st->shared_state->lock);
  500. /* First sibling enables SSBD: */
  501. if (!st->shared_state->disable_state)
  502. wrmsrl(MSR_AMD64_LS_CFG, msr);
  503. st->shared_state->disable_state++;
  504. raw_spin_unlock(&st->shared_state->lock);
  505. } else {
  506. if (!__test_and_clear_bit(LSTATE_SSB, &st->local_state))
  507. return;
  508. raw_spin_lock(&st->shared_state->lock);
  509. st->shared_state->disable_state--;
  510. if (!st->shared_state->disable_state)
  511. wrmsrl(MSR_AMD64_LS_CFG, msr);
  512. raw_spin_unlock(&st->shared_state->lock);
  513. }
  514. }
  515. #else
  516. static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
  517. {
  518. u64 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
  519. wrmsrl(MSR_AMD64_LS_CFG, msr);
  520. }
  521. #endif
  522. static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
  523. {
  524. /*
  525. * SSBD has the same definition in SPEC_CTRL and VIRT_SPEC_CTRL,
  526. * so ssbd_tif_to_spec_ctrl() just works.
  527. */
  528. wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));
  529. }
  530. /*
  531. * Update the MSRs managing speculation control, during context switch.
  532. *
  533. * tifp: Previous task's thread flags
  534. * tifn: Next task's thread flags
  535. */
  536. static __always_inline void __speculation_ctrl_update(unsigned long tifp,
  537. unsigned long tifn)
  538. {
  539. unsigned long tif_diff = tifp ^ tifn;
  540. u64 msr = x86_spec_ctrl_base;
  541. bool updmsr = false;
  542. lockdep_assert_irqs_disabled();
  543. /* Handle change of TIF_SSBD depending on the mitigation method. */
  544. if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {
  545. if (tif_diff & _TIF_SSBD)
  546. amd_set_ssb_virt_state(tifn);
  547. } else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
  548. if (tif_diff & _TIF_SSBD)
  549. amd_set_core_ssb_state(tifn);
  550. } else if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
  551. static_cpu_has(X86_FEATURE_AMD_SSBD)) {
  552. updmsr |= !!(tif_diff & _TIF_SSBD);
  553. msr |= ssbd_tif_to_spec_ctrl(tifn);
  554. }
  555. /* Only evaluate TIF_SPEC_IB if conditional STIBP is enabled. */
  556. if (IS_ENABLED(CONFIG_SMP) &&
  557. static_branch_unlikely(&switch_to_cond_stibp)) {
  558. updmsr |= !!(tif_diff & _TIF_SPEC_IB);
  559. msr |= stibp_tif_to_spec_ctrl(tifn);
  560. }
  561. if (updmsr)
  562. update_spec_ctrl_cond(msr);
  563. }
  564. static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)
  565. {
  566. if (test_and_clear_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE)) {
  567. if (task_spec_ssb_disable(tsk))
  568. set_tsk_thread_flag(tsk, TIF_SSBD);
  569. else
  570. clear_tsk_thread_flag(tsk, TIF_SSBD);
  571. if (task_spec_ib_disable(tsk))
  572. set_tsk_thread_flag(tsk, TIF_SPEC_IB);
  573. else
  574. clear_tsk_thread_flag(tsk, TIF_SPEC_IB);
  575. }
  576. /* Return the updated threadinfo flags*/
  577. return read_task_thread_flags(tsk);
  578. }
  579. void speculation_ctrl_update(unsigned long tif)
  580. {
  581. unsigned long flags;
  582. /* Forced update. Make sure all relevant TIF flags are different */
  583. local_irq_save(flags);
  584. __speculation_ctrl_update(~tif, tif);
  585. local_irq_restore(flags);
  586. }
  587. /* Called from seccomp/prctl update */
  588. void speculation_ctrl_update_current(void)
  589. {
  590. preempt_disable();
  591. speculation_ctrl_update(speculation_ctrl_update_tif(current));
  592. preempt_enable();
  593. }
  594. static inline void cr4_toggle_bits_irqsoff(unsigned long mask)
  595. {
  596. unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
  597. newval = cr4 ^ mask;
  598. if (newval != cr4) {
  599. this_cpu_write(cpu_tlbstate.cr4, newval);
  600. __write_cr4(newval);
  601. }
  602. }
  603. void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p)
  604. {
  605. unsigned long tifp, tifn;
  606. tifn = read_task_thread_flags(next_p);
  607. tifp = read_task_thread_flags(prev_p);
  608. switch_to_bitmap(tifp);
  609. propagate_user_return_notify(prev_p, next_p);
  610. if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) &&
  611. arch_has_block_step()) {
  612. unsigned long debugctl, msk;
  613. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  614. debugctl &= ~DEBUGCTLMSR_BTF;
  615. msk = tifn & _TIF_BLOCKSTEP;
  616. debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT;
  617. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  618. }
  619. if ((tifp ^ tifn) & _TIF_NOTSC)
  620. cr4_toggle_bits_irqsoff(X86_CR4_TSD);
  621. if ((tifp ^ tifn) & _TIF_NOCPUID)
  622. set_cpuid_faulting(!!(tifn & _TIF_NOCPUID));
  623. if (likely(!((tifp | tifn) & _TIF_SPEC_FORCE_UPDATE))) {
  624. __speculation_ctrl_update(tifp, tifn);
  625. } else {
  626. speculation_ctrl_update_tif(prev_p);
  627. tifn = speculation_ctrl_update_tif(next_p);
  628. /* Enforce MSR update to ensure consistent state */
  629. __speculation_ctrl_update(~tifn, tifn);
  630. }
  631. }
  632. /*
  633. * Idle related variables and functions
  634. */
  635. unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
  636. EXPORT_SYMBOL(boot_option_idle_override);
  637. /*
  638. * We use this if we don't have any better idle routine..
  639. */
  640. void __cpuidle default_idle(void)
  641. {
  642. raw_safe_halt();
  643. raw_local_irq_disable();
  644. }
  645. #if defined(CONFIG_APM_MODULE) || defined(CONFIG_HALTPOLL_CPUIDLE_MODULE)
  646. EXPORT_SYMBOL(default_idle);
  647. #endif
  648. DEFINE_STATIC_CALL_NULL(x86_idle, default_idle);
  649. static bool x86_idle_set(void)
  650. {
  651. return !!static_call_query(x86_idle);
  652. }
  653. #ifndef CONFIG_SMP
  654. static inline void __noreturn play_dead(void)
  655. {
  656. BUG();
  657. }
  658. #endif
  659. void arch_cpu_idle_enter(void)
  660. {
  661. tsc_verify_tsc_adjust(false);
  662. local_touch_nmi();
  663. }
  664. void __noreturn arch_cpu_idle_dead(void)
  665. {
  666. play_dead();
  667. }
  668. /*
  669. * Called from the generic idle code.
  670. */
  671. void __cpuidle arch_cpu_idle(void)
  672. {
  673. static_call(x86_idle)();
  674. }
  675. EXPORT_SYMBOL_GPL(arch_cpu_idle);
  676. #ifdef CONFIG_XEN
  677. bool xen_set_default_idle(void)
  678. {
  679. bool ret = x86_idle_set();
  680. static_call_update(x86_idle, default_idle);
  681. return ret;
  682. }
  683. #endif
  684. struct cpumask cpus_stop_mask;
  685. void __noreturn stop_this_cpu(void *dummy)
  686. {
  687. struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
  688. unsigned int cpu = smp_processor_id();
  689. local_irq_disable();
  690. /*
  691. * Remove this CPU from the online mask and disable it
  692. * unconditionally. This might be redundant in case that the reboot
  693. * vector was handled late and stop_other_cpus() sent an NMI.
  694. *
  695. * According to SDM and APM NMIs can be accepted even after soft
  696. * disabling the local APIC.
  697. */
  698. set_cpu_online(cpu, false);
  699. disable_local_APIC();
  700. mcheck_cpu_clear(c);
  701. /*
  702. * Use wbinvd on processors that support SME. This provides support
  703. * for performing a successful kexec when going from SME inactive
  704. * to SME active (or vice-versa). The cache must be cleared so that
  705. * if there are entries with the same physical address, both with and
  706. * without the encryption bit, they don't race each other when flushed
  707. * and potentially end up with the wrong entry being committed to
  708. * memory.
  709. *
  710. * Test the CPUID bit directly because the machine might've cleared
  711. * X86_FEATURE_SME due to cmdline options.
  712. */
  713. if (c->extended_cpuid_level >= 0x8000001f && (cpuid_eax(0x8000001f) & BIT(0)))
  714. native_wbinvd();
  715. /*
  716. * This brings a cache line back and dirties it, but
  717. * native_stop_other_cpus() will overwrite cpus_stop_mask after it
  718. * observed that all CPUs reported stop. This write will invalidate
  719. * the related cache line on this CPU.
  720. */
  721. cpumask_clear_cpu(cpu, &cpus_stop_mask);
  722. #ifdef CONFIG_SMP
  723. if (smp_ops.stop_this_cpu) {
  724. smp_ops.stop_this_cpu();
  725. BUG();
  726. }
  727. #endif
  728. for (;;) {
  729. /*
  730. * Use native_halt() so that memory contents don't change
  731. * (stack usage and variables) after possibly issuing the
  732. * native_wbinvd() above.
  733. */
  734. native_halt();
  735. }
  736. }
  737. /*
  738. * Prefer MWAIT over HALT if MWAIT is supported, MWAIT_CPUID leaf
  739. * exists and whenever MONITOR/MWAIT extensions are present there is at
  740. * least one C1 substate.
  741. *
  742. * Do not prefer MWAIT if MONITOR instruction has a bug or idle=nomwait
  743. * is passed to kernel commandline parameter.
  744. */
  745. static __init bool prefer_mwait_c1_over_halt(void)
  746. {
  747. const struct cpuinfo_x86 *c = &boot_cpu_data;
  748. u32 eax, ebx, ecx, edx;
  749. /* If override is enforced on the command line, fall back to HALT. */
  750. if (boot_option_idle_override != IDLE_NO_OVERRIDE)
  751. return false;
  752. /* MWAIT is not supported on this platform. Fallback to HALT */
  753. if (!cpu_has(c, X86_FEATURE_MWAIT))
  754. return false;
  755. /* Monitor has a bug or APIC stops in C1E. Fallback to HALT */
  756. if (boot_cpu_has_bug(X86_BUG_MONITOR) || boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E))
  757. return false;
  758. cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
  759. /*
  760. * If MWAIT extensions are not available, it is safe to use MWAIT
  761. * with EAX=0, ECX=0.
  762. */
  763. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED))
  764. return true;
  765. /*
  766. * If MWAIT extensions are available, there should be at least one
  767. * MWAIT C1 substate present.
  768. */
  769. return !!(edx & MWAIT_C1_SUBSTATE_MASK);
  770. }
  771. /*
  772. * MONITOR/MWAIT with no hints, used for default C1 state. This invokes MWAIT
  773. * with interrupts enabled and no flags, which is backwards compatible with the
  774. * original MWAIT implementation.
  775. */
  776. static __cpuidle void mwait_idle(void)
  777. {
  778. if (need_resched())
  779. return;
  780. x86_idle_clear_cpu_buffers();
  781. if (!current_set_polling_and_test()) {
  782. const void *addr = &current_thread_info()->flags;
  783. alternative_input("", "clflush (%[addr])", X86_BUG_CLFLUSH_MONITOR, [addr] "a" (addr));
  784. __monitor(addr, 0, 0);
  785. if (need_resched())
  786. goto out;
  787. __sti_mwait(0, 0);
  788. raw_local_irq_disable();
  789. }
  790. out:
  791. __current_clr_polling();
  792. }
  793. void __init select_idle_routine(void)
  794. {
  795. if (boot_option_idle_override == IDLE_POLL) {
  796. if (IS_ENABLED(CONFIG_SMP) && __max_threads_per_core > 1)
  797. pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
  798. return;
  799. }
  800. /* Required to guard against xen_set_default_idle() */
  801. if (x86_idle_set())
  802. return;
  803. if (prefer_mwait_c1_over_halt()) {
  804. pr_info("using mwait in idle threads\n");
  805. static_call_update(x86_idle, mwait_idle);
  806. } else if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
  807. pr_info("using TDX aware idle routine\n");
  808. static_call_update(x86_idle, tdx_halt);
  809. } else {
  810. static_call_update(x86_idle, default_idle);
  811. }
  812. }
  813. void amd_e400_c1e_apic_setup(void)
  814. {
  815. if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
  816. pr_info("Switch to broadcast mode on CPU%d\n", smp_processor_id());
  817. local_irq_disable();
  818. tick_broadcast_force();
  819. local_irq_enable();
  820. }
  821. }
  822. void __init arch_post_acpi_subsys_init(void)
  823. {
  824. u32 lo, hi;
  825. if (!boot_cpu_has_bug(X86_BUG_AMD_E400))
  826. return;
  827. /*
  828. * AMD E400 detection needs to happen after ACPI has been enabled. If
  829. * the machine is affected K8_INTP_C1E_ACTIVE_MASK bits are set in
  830. * MSR_K8_INT_PENDING_MSG.
  831. */
  832. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  833. if (!(lo & K8_INTP_C1E_ACTIVE_MASK))
  834. return;
  835. boot_cpu_set_bug(X86_BUG_AMD_APIC_C1E);
  836. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  837. mark_tsc_unstable("TSC halt in AMD C1E");
  838. if (IS_ENABLED(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST_IDLE))
  839. static_branch_enable(&arch_needs_tick_broadcast);
  840. pr_info("System has AMD C1E erratum E400. Workaround enabled.\n");
  841. }
  842. static int __init idle_setup(char *str)
  843. {
  844. if (!str)
  845. return -EINVAL;
  846. if (!strcmp(str, "poll")) {
  847. pr_info("using polling idle threads\n");
  848. boot_option_idle_override = IDLE_POLL;
  849. cpu_idle_poll_ctrl(true);
  850. } else if (!strcmp(str, "halt")) {
  851. /* 'idle=halt' HALT for idle. C-states are disabled. */
  852. boot_option_idle_override = IDLE_HALT;
  853. } else if (!strcmp(str, "nomwait")) {
  854. /* 'idle=nomwait' disables MWAIT for idle */
  855. boot_option_idle_override = IDLE_NOMWAIT;
  856. } else {
  857. return -EINVAL;
  858. }
  859. return 0;
  860. }
  861. early_param("idle", idle_setup);
  862. unsigned long arch_align_stack(unsigned long sp)
  863. {
  864. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  865. sp -= get_random_u32_below(8192);
  866. return sp & ~0xf;
  867. }
  868. unsigned long arch_randomize_brk(struct mm_struct *mm)
  869. {
  870. if (mmap_is_ia32())
  871. return randomize_page(mm->brk, SZ_32M);
  872. return randomize_page(mm->brk, SZ_1G);
  873. }
  874. /*
  875. * Called from fs/proc with a reference on @p to find the function
  876. * which called into schedule(). This needs to be done carefully
  877. * because the task might wake up and we might look at a stack
  878. * changing under us.
  879. */
  880. unsigned long __get_wchan(struct task_struct *p)
  881. {
  882. struct unwind_state state;
  883. unsigned long addr = 0;
  884. if (!try_get_task_stack(p))
  885. return 0;
  886. for (unwind_start(&state, p, NULL, NULL); !unwind_done(&state);
  887. unwind_next_frame(&state)) {
  888. addr = unwind_get_return_address(&state);
  889. if (!addr)
  890. break;
  891. if (in_sched_functions(addr))
  892. continue;
  893. break;
  894. }
  895. put_task_stack(p);
  896. return addr;
  897. }
  898. long do_arch_prctl_common(int option, unsigned long arg2)
  899. {
  900. switch (option) {
  901. case ARCH_GET_CPUID:
  902. return get_cpuid_mode();
  903. case ARCH_SET_CPUID:
  904. return set_cpuid_mode(arg2);
  905. case ARCH_GET_XCOMP_SUPP:
  906. case ARCH_GET_XCOMP_PERM:
  907. case ARCH_REQ_XCOMP_PERM:
  908. case ARCH_GET_XCOMP_GUEST_PERM:
  909. case ARCH_REQ_XCOMP_GUEST_PERM:
  910. return fpu_xstate_prctl(option, arg2);
  911. }
  912. return -EINVAL;
  913. }