0001
0002 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0003
0004 #include <linux/errno.h>
0005 #include <linux/kernel.h>
0006 #include <linux/mm.h>
0007 #include <linux/smp.h>
0008 #include <linux/prctl.h>
0009 #include <linux/slab.h>
0010 #include <linux/sched.h>
0011 #include <linux/sched/idle.h>
0012 #include <linux/sched/debug.h>
0013 #include <linux/sched/task.h>
0014 #include <linux/sched/task_stack.h>
0015 #include <linux/init.h>
0016 #include <linux/export.h>
0017 #include <linux/pm.h>
0018 #include <linux/tick.h>
0019 #include <linux/random.h>
0020 #include <linux/user-return-notifier.h>
0021 #include <linux/dmi.h>
0022 #include <linux/utsname.h>
0023 #include <linux/stackprotector.h>
0024 #include <linux/cpuidle.h>
0025 #include <linux/acpi.h>
0026 #include <linux/elf-randomize.h>
0027 #include <trace/events/power.h>
0028 #include <linux/hw_breakpoint.h>
0029 #include <asm/cpu.h>
0030 #include <asm/apic.h>
0031 #include <linux/uaccess.h>
0032 #include <asm/mwait.h>
0033 #include <asm/fpu/api.h>
0034 #include <asm/fpu/sched.h>
0035 #include <asm/fpu/xstate.h>
0036 #include <asm/debugreg.h>
0037 #include <asm/nmi.h>
0038 #include <asm/tlbflush.h>
0039 #include <asm/mce.h>
0040 #include <asm/vm86.h>
0041 #include <asm/switch_to.h>
0042 #include <asm/desc.h>
0043 #include <asm/prctl.h>
0044 #include <asm/spec-ctrl.h>
0045 #include <asm/io_bitmap.h>
0046 #include <asm/proto.h>
0047 #include <asm/frame.h>
0048 #include <asm/unwind.h>
0049 #include <asm/tdx.h>
0050
0051 #include "process.h"
0052
0053
0054
0055
0056
0057
0058
0059
0060 __visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = {
0061 .x86_tss = {
0062
0063
0064
0065
0066
0067
0068 .sp0 = (1UL << (BITS_PER_LONG-1)) + 1,
0069
0070 #ifdef CONFIG_X86_32
0071 .sp1 = TOP_OF_INIT_STACK,
0072
0073 .ss0 = __KERNEL_DS,
0074 .ss1 = __KERNEL_CS,
0075 #endif
0076 .io_bitmap_base = IO_BITMAP_OFFSET_INVALID,
0077 },
0078 };
0079 EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);
0080
0081 DEFINE_PER_CPU(bool, __tss_limit_invalid);
0082 EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
0083
0084
0085
0086
0087
0088 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
0089 {
0090 memcpy(dst, src, arch_task_struct_size);
0091 #ifdef CONFIG_VM86
0092 dst->thread.vm86 = NULL;
0093 #endif
0094
0095 dst->thread.fpu.fpstate = NULL;
0096
0097 return 0;
0098 }
0099
0100 #ifdef CONFIG_X86_64
0101 void arch_release_task_struct(struct task_struct *tsk)
0102 {
0103 if (fpu_state_size_dynamic())
0104 fpstate_free(&tsk->thread.fpu);
0105 }
0106 #endif
0107
0108
0109
0110
0111 void exit_thread(struct task_struct *tsk)
0112 {
0113 struct thread_struct *t = &tsk->thread;
0114 struct fpu *fpu = &t->fpu;
0115
0116 if (test_thread_flag(TIF_IO_BITMAP))
0117 io_bitmap_exit(tsk);
0118
0119 free_vm86(t);
0120
0121 fpu__drop(fpu);
0122 }
0123
0124 static int set_new_tls(struct task_struct *p, unsigned long tls)
0125 {
0126 struct user_desc __user *utls = (struct user_desc __user *)tls;
0127
0128 if (in_ia32_syscall())
0129 return do_set_thread_area(p, -1, utls, 0);
0130 else
0131 return do_set_thread_area_64(p, ARCH_SET_FS, tls);
0132 }
0133
0134 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
0135 {
0136 unsigned long clone_flags = args->flags;
0137 unsigned long sp = args->stack;
0138 unsigned long tls = args->tls;
0139 struct inactive_task_frame *frame;
0140 struct fork_frame *fork_frame;
0141 struct pt_regs *childregs;
0142 int ret = 0;
0143
0144 childregs = task_pt_regs(p);
0145 fork_frame = container_of(childregs, struct fork_frame, regs);
0146 frame = &fork_frame->frame;
0147
0148 frame->bp = encode_frame_pointer(childregs);
0149 frame->ret_addr = (unsigned long) ret_from_fork;
0150 p->thread.sp = (unsigned long) fork_frame;
0151 p->thread.io_bitmap = NULL;
0152 p->thread.iopl_warn = 0;
0153 memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
0154
0155 #ifdef CONFIG_X86_64
0156 current_save_fsgs();
0157 p->thread.fsindex = current->thread.fsindex;
0158 p->thread.fsbase = current->thread.fsbase;
0159 p->thread.gsindex = current->thread.gsindex;
0160 p->thread.gsbase = current->thread.gsbase;
0161
0162 savesegment(es, p->thread.es);
0163 savesegment(ds, p->thread.ds);
0164 #else
0165 p->thread.sp0 = (unsigned long) (childregs + 1);
0166 savesegment(gs, p->thread.gs);
0167
0168
0169
0170
0171
0172
0173 frame->flags = X86_EFLAGS_FIXED;
0174 #endif
0175
0176 fpu_clone(p, clone_flags, args->fn);
0177
0178
0179 if (unlikely(p->flags & PF_KTHREAD)) {
0180 p->thread.pkru = pkru_get_init_value();
0181 memset(childregs, 0, sizeof(struct pt_regs));
0182 kthread_frame_init(frame, args->fn, args->fn_arg);
0183 return 0;
0184 }
0185
0186
0187
0188
0189
0190 p->thread.pkru = read_pkru();
0191
0192 frame->bx = 0;
0193 *childregs = *current_pt_regs();
0194 childregs->ax = 0;
0195 if (sp)
0196 childregs->sp = sp;
0197
0198 if (unlikely(args->fn)) {
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209 childregs->sp = 0;
0210 childregs->ip = 0;
0211 kthread_frame_init(frame, args->fn, args->fn_arg);
0212 return 0;
0213 }
0214
0215
0216 if (clone_flags & CLONE_SETTLS)
0217 ret = set_new_tls(p, tls);
0218
0219 if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP)))
0220 io_bitmap_share(p);
0221
0222 return ret;
0223 }
0224
0225 static void pkru_flush_thread(void)
0226 {
0227
0228
0229
0230
0231 pkru_write_default();
0232 }
0233
0234 void flush_thread(void)
0235 {
0236 struct task_struct *tsk = current;
0237
0238 flush_ptrace_hw_breakpoint(tsk);
0239 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
0240
0241 fpu_flush_thread();
0242 pkru_flush_thread();
0243 }
0244
0245 void disable_TSC(void)
0246 {
0247 preempt_disable();
0248 if (!test_and_set_thread_flag(TIF_NOTSC))
0249
0250
0251
0252
0253 cr4_set_bits(X86_CR4_TSD);
0254 preempt_enable();
0255 }
0256
0257 static void enable_TSC(void)
0258 {
0259 preempt_disable();
0260 if (test_and_clear_thread_flag(TIF_NOTSC))
0261
0262
0263
0264
0265 cr4_clear_bits(X86_CR4_TSD);
0266 preempt_enable();
0267 }
0268
0269 int get_tsc_mode(unsigned long adr)
0270 {
0271 unsigned int val;
0272
0273 if (test_thread_flag(TIF_NOTSC))
0274 val = PR_TSC_SIGSEGV;
0275 else
0276 val = PR_TSC_ENABLE;
0277
0278 return put_user(val, (unsigned int __user *)adr);
0279 }
0280
0281 int set_tsc_mode(unsigned int val)
0282 {
0283 if (val == PR_TSC_SIGSEGV)
0284 disable_TSC();
0285 else if (val == PR_TSC_ENABLE)
0286 enable_TSC();
0287 else
0288 return -EINVAL;
0289
0290 return 0;
0291 }
0292
0293 DEFINE_PER_CPU(u64, msr_misc_features_shadow);
0294
0295 static void set_cpuid_faulting(bool on)
0296 {
0297 u64 msrval;
0298
0299 msrval = this_cpu_read(msr_misc_features_shadow);
0300 msrval &= ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
0301 msrval |= (on << MSR_MISC_FEATURES_ENABLES_CPUID_FAULT_BIT);
0302 this_cpu_write(msr_misc_features_shadow, msrval);
0303 wrmsrl(MSR_MISC_FEATURES_ENABLES, msrval);
0304 }
0305
0306 static void disable_cpuid(void)
0307 {
0308 preempt_disable();
0309 if (!test_and_set_thread_flag(TIF_NOCPUID)) {
0310
0311
0312
0313
0314 set_cpuid_faulting(true);
0315 }
0316 preempt_enable();
0317 }
0318
0319 static void enable_cpuid(void)
0320 {
0321 preempt_disable();
0322 if (test_and_clear_thread_flag(TIF_NOCPUID)) {
0323
0324
0325
0326
0327 set_cpuid_faulting(false);
0328 }
0329 preempt_enable();
0330 }
0331
0332 static int get_cpuid_mode(void)
0333 {
0334 return !test_thread_flag(TIF_NOCPUID);
0335 }
0336
0337 static int set_cpuid_mode(unsigned long cpuid_enabled)
0338 {
0339 if (!boot_cpu_has(X86_FEATURE_CPUID_FAULT))
0340 return -ENODEV;
0341
0342 if (cpuid_enabled)
0343 enable_cpuid();
0344 else
0345 disable_cpuid();
0346
0347 return 0;
0348 }
0349
0350
0351
0352
0353 void arch_setup_new_exec(void)
0354 {
0355
0356 if (test_thread_flag(TIF_NOCPUID))
0357 enable_cpuid();
0358
0359
0360
0361
0362
0363 if (test_thread_flag(TIF_SSBD) &&
0364 task_spec_ssb_noexec(current)) {
0365 clear_thread_flag(TIF_SSBD);
0366 task_clear_spec_ssb_disable(current);
0367 task_clear_spec_ssb_noexec(current);
0368 speculation_ctrl_update(read_thread_flags());
0369 }
0370 }
0371
0372 #ifdef CONFIG_X86_IOPL_IOPERM
0373 static inline void switch_to_bitmap(unsigned long tifp)
0374 {
0375
0376
0377
0378
0379
0380
0381
0382 if (tifp & _TIF_IO_BITMAP)
0383 tss_invalidate_io_bitmap();
0384 }
0385
0386 static void tss_copy_io_bitmap(struct tss_struct *tss, struct io_bitmap *iobm)
0387 {
0388
0389
0390
0391
0392
0393
0394
0395
0396 memcpy(tss->io_bitmap.bitmap, iobm->bitmap,
0397 max(tss->io_bitmap.prev_max, iobm->max));
0398
0399
0400
0401
0402
0403 tss->io_bitmap.prev_max = iobm->max;
0404 tss->io_bitmap.prev_sequence = iobm->sequence;
0405 }
0406
0407
0408
0409
0410 void native_tss_update_io_bitmap(void)
0411 {
0412 struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
0413 struct thread_struct *t = ¤t->thread;
0414 u16 *base = &tss->x86_tss.io_bitmap_base;
0415
0416 if (!test_thread_flag(TIF_IO_BITMAP)) {
0417 native_tss_invalidate_io_bitmap();
0418 return;
0419 }
0420
0421 if (IS_ENABLED(CONFIG_X86_IOPL_IOPERM) && t->iopl_emul == 3) {
0422 *base = IO_BITMAP_OFFSET_VALID_ALL;
0423 } else {
0424 struct io_bitmap *iobm = t->io_bitmap;
0425
0426
0427
0428
0429
0430 if (tss->io_bitmap.prev_sequence != iobm->sequence)
0431 tss_copy_io_bitmap(tss, iobm);
0432
0433
0434 *base = IO_BITMAP_OFFSET_VALID_MAP;
0435 }
0436
0437
0438
0439
0440
0441
0442
0443 refresh_tss_limit();
0444 }
0445 #else
0446 static inline void switch_to_bitmap(unsigned long tifp) { }
0447 #endif
0448
0449 #ifdef CONFIG_SMP
0450
0451 struct ssb_state {
0452 struct ssb_state *shared_state;
0453 raw_spinlock_t lock;
0454 unsigned int disable_state;
0455 unsigned long local_state;
0456 };
0457
0458 #define LSTATE_SSB 0
0459
0460 static DEFINE_PER_CPU(struct ssb_state, ssb_state);
0461
0462 void speculative_store_bypass_ht_init(void)
0463 {
0464 struct ssb_state *st = this_cpu_ptr(&ssb_state);
0465 unsigned int this_cpu = smp_processor_id();
0466 unsigned int cpu;
0467
0468 st->local_state = 0;
0469
0470
0471
0472
0473
0474 if (st->shared_state)
0475 return;
0476
0477 raw_spin_lock_init(&st->lock);
0478
0479
0480
0481
0482
0483 for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {
0484 if (cpu == this_cpu)
0485 continue;
0486
0487 if (!per_cpu(ssb_state, cpu).shared_state)
0488 continue;
0489
0490
0491 st->shared_state = per_cpu(ssb_state, cpu).shared_state;
0492 return;
0493 }
0494
0495
0496
0497
0498
0499
0500
0501 st->shared_state = st;
0502 }
0503
0504
0505
0506
0507
0508
0509
0510
0511 static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
0512 {
0513 struct ssb_state *st = this_cpu_ptr(&ssb_state);
0514 u64 msr = x86_amd_ls_cfg_base;
0515
0516 if (!static_cpu_has(X86_FEATURE_ZEN)) {
0517 msr |= ssbd_tif_to_amd_ls_cfg(tifn);
0518 wrmsrl(MSR_AMD64_LS_CFG, msr);
0519 return;
0520 }
0521
0522 if (tifn & _TIF_SSBD) {
0523
0524
0525
0526
0527 if (__test_and_set_bit(LSTATE_SSB, &st->local_state))
0528 return;
0529
0530 msr |= x86_amd_ls_cfg_ssbd_mask;
0531
0532 raw_spin_lock(&st->shared_state->lock);
0533
0534 if (!st->shared_state->disable_state)
0535 wrmsrl(MSR_AMD64_LS_CFG, msr);
0536 st->shared_state->disable_state++;
0537 raw_spin_unlock(&st->shared_state->lock);
0538 } else {
0539 if (!__test_and_clear_bit(LSTATE_SSB, &st->local_state))
0540 return;
0541
0542 raw_spin_lock(&st->shared_state->lock);
0543 st->shared_state->disable_state--;
0544 if (!st->shared_state->disable_state)
0545 wrmsrl(MSR_AMD64_LS_CFG, msr);
0546 raw_spin_unlock(&st->shared_state->lock);
0547 }
0548 }
0549 #else
0550 static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
0551 {
0552 u64 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
0553
0554 wrmsrl(MSR_AMD64_LS_CFG, msr);
0555 }
0556 #endif
0557
0558 static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
0559 {
0560
0561
0562
0563
0564 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));
0565 }
0566
0567
0568
0569
0570
0571
0572
0573 static __always_inline void __speculation_ctrl_update(unsigned long tifp,
0574 unsigned long tifn)
0575 {
0576 unsigned long tif_diff = tifp ^ tifn;
0577 u64 msr = x86_spec_ctrl_base;
0578 bool updmsr = false;
0579
0580 lockdep_assert_irqs_disabled();
0581
0582
0583 if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {
0584 if (tif_diff & _TIF_SSBD)
0585 amd_set_ssb_virt_state(tifn);
0586 } else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
0587 if (tif_diff & _TIF_SSBD)
0588 amd_set_core_ssb_state(tifn);
0589 } else if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
0590 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
0591 updmsr |= !!(tif_diff & _TIF_SSBD);
0592 msr |= ssbd_tif_to_spec_ctrl(tifn);
0593 }
0594
0595
0596 if (IS_ENABLED(CONFIG_SMP) &&
0597 static_branch_unlikely(&switch_to_cond_stibp)) {
0598 updmsr |= !!(tif_diff & _TIF_SPEC_IB);
0599 msr |= stibp_tif_to_spec_ctrl(tifn);
0600 }
0601
0602 if (updmsr)
0603 write_spec_ctrl_current(msr, false);
0604 }
0605
0606 static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)
0607 {
0608 if (test_and_clear_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE)) {
0609 if (task_spec_ssb_disable(tsk))
0610 set_tsk_thread_flag(tsk, TIF_SSBD);
0611 else
0612 clear_tsk_thread_flag(tsk, TIF_SSBD);
0613
0614 if (task_spec_ib_disable(tsk))
0615 set_tsk_thread_flag(tsk, TIF_SPEC_IB);
0616 else
0617 clear_tsk_thread_flag(tsk, TIF_SPEC_IB);
0618 }
0619
0620 return read_task_thread_flags(tsk);
0621 }
0622
0623 void speculation_ctrl_update(unsigned long tif)
0624 {
0625 unsigned long flags;
0626
0627
0628 local_irq_save(flags);
0629 __speculation_ctrl_update(~tif, tif);
0630 local_irq_restore(flags);
0631 }
0632
0633
0634 void speculation_ctrl_update_current(void)
0635 {
0636 preempt_disable();
0637 speculation_ctrl_update(speculation_ctrl_update_tif(current));
0638 preempt_enable();
0639 }
0640
0641 static inline void cr4_toggle_bits_irqsoff(unsigned long mask)
0642 {
0643 unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
0644
0645 newval = cr4 ^ mask;
0646 if (newval != cr4) {
0647 this_cpu_write(cpu_tlbstate.cr4, newval);
0648 __write_cr4(newval);
0649 }
0650 }
0651
0652 void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p)
0653 {
0654 unsigned long tifp, tifn;
0655
0656 tifn = read_task_thread_flags(next_p);
0657 tifp = read_task_thread_flags(prev_p);
0658
0659 switch_to_bitmap(tifp);
0660
0661 propagate_user_return_notify(prev_p, next_p);
0662
0663 if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) &&
0664 arch_has_block_step()) {
0665 unsigned long debugctl, msk;
0666
0667 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
0668 debugctl &= ~DEBUGCTLMSR_BTF;
0669 msk = tifn & _TIF_BLOCKSTEP;
0670 debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT;
0671 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
0672 }
0673
0674 if ((tifp ^ tifn) & _TIF_NOTSC)
0675 cr4_toggle_bits_irqsoff(X86_CR4_TSD);
0676
0677 if ((tifp ^ tifn) & _TIF_NOCPUID)
0678 set_cpuid_faulting(!!(tifn & _TIF_NOCPUID));
0679
0680 if (likely(!((tifp | tifn) & _TIF_SPEC_FORCE_UPDATE))) {
0681 __speculation_ctrl_update(tifp, tifn);
0682 } else {
0683 speculation_ctrl_update_tif(prev_p);
0684 tifn = speculation_ctrl_update_tif(next_p);
0685
0686
0687 __speculation_ctrl_update(~tifn, tifn);
0688 }
0689 }
0690
0691
0692
0693
0694 unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
0695 EXPORT_SYMBOL(boot_option_idle_override);
0696
0697 static void (*x86_idle)(void);
0698
0699 #ifndef CONFIG_SMP
0700 static inline void play_dead(void)
0701 {
0702 BUG();
0703 }
0704 #endif
0705
0706 void arch_cpu_idle_enter(void)
0707 {
0708 tsc_verify_tsc_adjust(false);
0709 local_touch_nmi();
0710 }
0711
0712 void arch_cpu_idle_dead(void)
0713 {
0714 play_dead();
0715 }
0716
0717
0718
0719
0720 void arch_cpu_idle(void)
0721 {
0722 x86_idle();
0723 }
0724
0725
0726
0727
0728 void __cpuidle default_idle(void)
0729 {
0730 raw_safe_halt();
0731 }
0732 #if defined(CONFIG_APM_MODULE) || defined(CONFIG_HALTPOLL_CPUIDLE_MODULE)
0733 EXPORT_SYMBOL(default_idle);
0734 #endif
0735
0736 #ifdef CONFIG_XEN
0737 bool xen_set_default_idle(void)
0738 {
0739 bool ret = !!x86_idle;
0740
0741 x86_idle = default_idle;
0742
0743 return ret;
0744 }
0745 #endif
0746
0747 void __noreturn stop_this_cpu(void *dummy)
0748 {
0749 local_irq_disable();
0750
0751
0752
0753 set_cpu_online(smp_processor_id(), false);
0754 disable_local_APIC();
0755 mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769 if (cpuid_eax(0x8000001f) & BIT(0))
0770 native_wbinvd();
0771 for (;;) {
0772
0773
0774
0775
0776
0777 native_halt();
0778 }
0779 }
0780
0781
0782
0783
0784
0785
0786
0787 static void amd_e400_idle(void)
0788 {
0789
0790
0791
0792
0793
0794 if (!boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
0795 default_idle();
0796 return;
0797 }
0798
0799 tick_broadcast_enter();
0800
0801 default_idle();
0802
0803
0804
0805
0806
0807 raw_local_irq_disable();
0808 tick_broadcast_exit();
0809 raw_local_irq_enable();
0810 }
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820 static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
0821 {
0822 u32 eax, ebx, ecx, edx;
0823
0824
0825 if (boot_option_idle_override == IDLE_NOMWAIT)
0826 return 0;
0827
0828
0829 if (!cpu_has(c, X86_FEATURE_MWAIT))
0830 return 0;
0831
0832
0833 if (boot_cpu_has_bug(X86_BUG_MONITOR))
0834 return 0;
0835
0836 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
0837
0838
0839
0840
0841
0842 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED))
0843 return 1;
0844
0845
0846
0847
0848
0849 return (edx & MWAIT_C1_SUBSTATE_MASK);
0850 }
0851
0852
0853
0854
0855
0856
0857 static __cpuidle void mwait_idle(void)
0858 {
0859 if (!current_set_polling_and_test()) {
0860 if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
0861 mb();
0862 clflush((void *)¤t_thread_info()->flags);
0863 mb();
0864 }
0865
0866 __monitor((void *)¤t_thread_info()->flags, 0, 0);
0867 if (!need_resched())
0868 __sti_mwait(0, 0);
0869 else
0870 raw_local_irq_enable();
0871 } else {
0872 raw_local_irq_enable();
0873 }
0874 __current_clr_polling();
0875 }
0876
0877 void select_idle_routine(const struct cpuinfo_x86 *c)
0878 {
0879 #ifdef CONFIG_SMP
0880 if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
0881 pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
0882 #endif
0883 if (x86_idle || boot_option_idle_override == IDLE_POLL)
0884 return;
0885
0886 if (boot_cpu_has_bug(X86_BUG_AMD_E400)) {
0887 pr_info("using AMD E400 aware idle routine\n");
0888 x86_idle = amd_e400_idle;
0889 } else if (prefer_mwait_c1_over_halt(c)) {
0890 pr_info("using mwait in idle threads\n");
0891 x86_idle = mwait_idle;
0892 } else if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
0893 pr_info("using TDX aware idle routine\n");
0894 x86_idle = tdx_safe_halt;
0895 } else
0896 x86_idle = default_idle;
0897 }
0898
0899 void amd_e400_c1e_apic_setup(void)
0900 {
0901 if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
0902 pr_info("Switch to broadcast mode on CPU%d\n", smp_processor_id());
0903 local_irq_disable();
0904 tick_broadcast_force();
0905 local_irq_enable();
0906 }
0907 }
0908
0909 void __init arch_post_acpi_subsys_init(void)
0910 {
0911 u32 lo, hi;
0912
0913 if (!boot_cpu_has_bug(X86_BUG_AMD_E400))
0914 return;
0915
0916
0917
0918
0919
0920
0921 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
0922 if (!(lo & K8_INTP_C1E_ACTIVE_MASK))
0923 return;
0924
0925 boot_cpu_set_bug(X86_BUG_AMD_APIC_C1E);
0926
0927 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
0928 mark_tsc_unstable("TSC halt in AMD C1E");
0929 pr_info("System has AMD C1E enabled\n");
0930 }
0931
0932 static int __init idle_setup(char *str)
0933 {
0934 if (!str)
0935 return -EINVAL;
0936
0937 if (!strcmp(str, "poll")) {
0938 pr_info("using polling idle threads\n");
0939 boot_option_idle_override = IDLE_POLL;
0940 cpu_idle_poll_ctrl(true);
0941 } else if (!strcmp(str, "halt")) {
0942
0943
0944
0945
0946
0947
0948
0949 x86_idle = default_idle;
0950 boot_option_idle_override = IDLE_HALT;
0951 } else if (!strcmp(str, "nomwait")) {
0952
0953
0954
0955
0956
0957 boot_option_idle_override = IDLE_NOMWAIT;
0958 } else
0959 return -1;
0960
0961 return 0;
0962 }
0963 early_param("idle", idle_setup);
0964
0965 unsigned long arch_align_stack(unsigned long sp)
0966 {
0967 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
0968 sp -= get_random_int() % 8192;
0969 return sp & ~0xf;
0970 }
0971
0972 unsigned long arch_randomize_brk(struct mm_struct *mm)
0973 {
0974 return randomize_page(mm->brk, 0x02000000);
0975 }
0976
0977
0978
0979
0980
0981
0982
0983 unsigned long __get_wchan(struct task_struct *p)
0984 {
0985 struct unwind_state state;
0986 unsigned long addr = 0;
0987
0988 if (!try_get_task_stack(p))
0989 return 0;
0990
0991 for (unwind_start(&state, p, NULL, NULL); !unwind_done(&state);
0992 unwind_next_frame(&state)) {
0993 addr = unwind_get_return_address(&state);
0994 if (!addr)
0995 break;
0996 if (in_sched_functions(addr))
0997 continue;
0998 break;
0999 }
1000
1001 put_task_stack(p);
1002
1003 return addr;
1004 }
1005
1006 long do_arch_prctl_common(int option, unsigned long arg2)
1007 {
1008 switch (option) {
1009 case ARCH_GET_CPUID:
1010 return get_cpuid_mode();
1011 case ARCH_SET_CPUID:
1012 return set_cpuid_mode(arg2);
1013 case ARCH_GET_XCOMP_SUPP:
1014 case ARCH_GET_XCOMP_PERM:
1015 case ARCH_REQ_XCOMP_PERM:
1016 case ARCH_GET_XCOMP_GUEST_PERM:
1017 case ARCH_REQ_XCOMP_GUEST_PERM:
1018 return fpu_xstate_prctl(option, arg2);
1019 }
1020
1021 return -EINVAL;
1022 }