process.c 26 KB

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