Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Copyright (C) 1994 Linus Torvalds
0004  *
0005  *  Pentium III FXSR, SSE support
0006  *  General FPU state handling cleanups
0007  *  Gareth Hughes <gareth@valinux.com>, May 2000
0008  */
0009 #include <asm/fpu/api.h>
0010 #include <asm/fpu/regset.h>
0011 #include <asm/fpu/sched.h>
0012 #include <asm/fpu/signal.h>
0013 #include <asm/fpu/types.h>
0014 #include <asm/traps.h>
0015 #include <asm/irq_regs.h>
0016 
0017 #include <uapi/asm/kvm.h>
0018 
0019 #include <linux/hardirq.h>
0020 #include <linux/pkeys.h>
0021 #include <linux/vmalloc.h>
0022 
0023 #include "context.h"
0024 #include "internal.h"
0025 #include "legacy.h"
0026 #include "xstate.h"
0027 
0028 #define CREATE_TRACE_POINTS
0029 #include <asm/trace/fpu.h>
0030 
0031 #ifdef CONFIG_X86_64
0032 DEFINE_STATIC_KEY_FALSE(__fpu_state_size_dynamic);
0033 DEFINE_PER_CPU(u64, xfd_state);
0034 #endif
0035 
0036 /* The FPU state configuration data for kernel and user space */
0037 struct fpu_state_config fpu_kernel_cfg __ro_after_init;
0038 struct fpu_state_config fpu_user_cfg __ro_after_init;
0039 
0040 /*
0041  * Represents the initial FPU state. It's mostly (but not completely) zeroes,
0042  * depending on the FPU hardware format:
0043  */
0044 struct fpstate init_fpstate __ro_after_init;
0045 
0046 /* Track in-kernel FPU usage */
0047 static DEFINE_PER_CPU(bool, in_kernel_fpu);
0048 
0049 /*
0050  * Track which context is using the FPU on the CPU:
0051  */
0052 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
0053 
0054 /*
0055  * Can we use the FPU in kernel mode with the
0056  * whole "kernel_fpu_begin/end()" sequence?
0057  */
0058 bool irq_fpu_usable(void)
0059 {
0060     if (WARN_ON_ONCE(in_nmi()))
0061         return false;
0062 
0063     /* In kernel FPU usage already active? */
0064     if (this_cpu_read(in_kernel_fpu))
0065         return false;
0066 
0067     /*
0068      * When not in NMI or hard interrupt context, FPU can be used in:
0069      *
0070      * - Task context except from within fpregs_lock()'ed critical
0071      *   regions.
0072      *
0073      * - Soft interrupt processing context which cannot happen
0074      *   while in a fpregs_lock()'ed critical region.
0075      */
0076     if (!in_hardirq())
0077         return true;
0078 
0079     /*
0080      * In hard interrupt context it's safe when soft interrupts
0081      * are enabled, which means the interrupt did not hit in
0082      * a fpregs_lock()'ed critical region.
0083      */
0084     return !softirq_count();
0085 }
0086 EXPORT_SYMBOL(irq_fpu_usable);
0087 
0088 /*
0089  * Track AVX512 state use because it is known to slow the max clock
0090  * speed of the core.
0091  */
0092 static void update_avx_timestamp(struct fpu *fpu)
0093 {
0094 
0095 #define AVX512_TRACKING_MASK    (XFEATURE_MASK_ZMM_Hi256 | XFEATURE_MASK_Hi16_ZMM)
0096 
0097     if (fpu->fpstate->regs.xsave.header.xfeatures & AVX512_TRACKING_MASK)
0098         fpu->avx512_timestamp = jiffies;
0099 }
0100 
0101 /*
0102  * Save the FPU register state in fpu->fpstate->regs. The register state is
0103  * preserved.
0104  *
0105  * Must be called with fpregs_lock() held.
0106  *
0107  * The legacy FNSAVE instruction clears all FPU state unconditionally, so
0108  * register state has to be reloaded. That might be a pointless exercise
0109  * when the FPU is going to be used by another task right after that. But
0110  * this only affects 20+ years old 32bit systems and avoids conditionals all
0111  * over the place.
0112  *
0113  * FXSAVE and all XSAVE variants preserve the FPU register state.
0114  */
0115 void save_fpregs_to_fpstate(struct fpu *fpu)
0116 {
0117     if (likely(use_xsave())) {
0118         os_xsave(fpu->fpstate);
0119         update_avx_timestamp(fpu);
0120         return;
0121     }
0122 
0123     if (likely(use_fxsr())) {
0124         fxsave(&fpu->fpstate->regs.fxsave);
0125         return;
0126     }
0127 
0128     /*
0129      * Legacy FPU register saving, FNSAVE always clears FPU registers,
0130      * so we have to reload them from the memory state.
0131      */
0132     asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->fpstate->regs.fsave));
0133     frstor(&fpu->fpstate->regs.fsave);
0134 }
0135 
0136 void restore_fpregs_from_fpstate(struct fpstate *fpstate, u64 mask)
0137 {
0138     /*
0139      * AMD K7/K8 and later CPUs up to Zen don't save/restore
0140      * FDP/FIP/FOP unless an exception is pending. Clear the x87 state
0141      * here by setting it to fixed values.  "m" is a random variable
0142      * that should be in L1.
0143      */
0144     if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
0145         asm volatile(
0146             "fnclex\n\t"
0147             "emms\n\t"
0148             "fildl %P[addr]"    /* set F?P to defined value */
0149             : : [addr] "m" (fpstate));
0150     }
0151 
0152     if (use_xsave()) {
0153         /*
0154          * Dynamically enabled features are enabled in XCR0, but
0155          * usage requires also that the corresponding bits in XFD
0156          * are cleared.  If the bits are set then using a related
0157          * instruction will raise #NM. This allows to do the
0158          * allocation of the larger FPU buffer lazy from #NM or if
0159          * the task has no permission to kill it which would happen
0160          * via #UD if the feature is disabled in XCR0.
0161          *
0162          * XFD state is following the same life time rules as
0163          * XSTATE and to restore state correctly XFD has to be
0164          * updated before XRSTORS otherwise the component would
0165          * stay in or go into init state even if the bits are set
0166          * in fpstate::regs::xsave::xfeatures.
0167          */
0168         xfd_update_state(fpstate);
0169 
0170         /*
0171          * Restoring state always needs to modify all features
0172          * which are in @mask even if the current task cannot use
0173          * extended features.
0174          *
0175          * So fpstate->xfeatures cannot be used here, because then
0176          * a feature for which the task has no permission but was
0177          * used by the previous task would not go into init state.
0178          */
0179         mask = fpu_kernel_cfg.max_features & mask;
0180 
0181         os_xrstor(fpstate, mask);
0182     } else {
0183         if (use_fxsr())
0184             fxrstor(&fpstate->regs.fxsave);
0185         else
0186             frstor(&fpstate->regs.fsave);
0187     }
0188 }
0189 
0190 void fpu_reset_from_exception_fixup(void)
0191 {
0192     restore_fpregs_from_fpstate(&init_fpstate, XFEATURE_MASK_FPSTATE);
0193 }
0194 
0195 #if IS_ENABLED(CONFIG_KVM)
0196 static void __fpstate_reset(struct fpstate *fpstate, u64 xfd);
0197 
0198 static void fpu_init_guest_permissions(struct fpu_guest *gfpu)
0199 {
0200     struct fpu_state_perm *fpuperm;
0201     u64 perm;
0202 
0203     if (!IS_ENABLED(CONFIG_X86_64))
0204         return;
0205 
0206     spin_lock_irq(&current->sighand->siglock);
0207     fpuperm = &current->group_leader->thread.fpu.guest_perm;
0208     perm = fpuperm->__state_perm;
0209 
0210     /* First fpstate allocation locks down permissions. */
0211     WRITE_ONCE(fpuperm->__state_perm, perm | FPU_GUEST_PERM_LOCKED);
0212 
0213     spin_unlock_irq(&current->sighand->siglock);
0214 
0215     gfpu->perm = perm & ~FPU_GUEST_PERM_LOCKED;
0216 }
0217 
0218 bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
0219 {
0220     struct fpstate *fpstate;
0221     unsigned int size;
0222 
0223     size = fpu_user_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
0224     fpstate = vzalloc(size);
0225     if (!fpstate)
0226         return false;
0227 
0228     /* Leave xfd to 0 (the reset value defined by spec) */
0229     __fpstate_reset(fpstate, 0);
0230     fpstate_init_user(fpstate);
0231     fpstate->is_valloc  = true;
0232     fpstate->is_guest   = true;
0233 
0234     gfpu->fpstate       = fpstate;
0235     gfpu->xfeatures     = fpu_user_cfg.default_features;
0236     gfpu->perm      = fpu_user_cfg.default_features;
0237 
0238     /*
0239      * KVM sets the FP+SSE bits in the XSAVE header when copying FPU state
0240      * to userspace, even when XSAVE is unsupported, so that restoring FPU
0241      * state on a different CPU that does support XSAVE can cleanly load
0242      * the incoming state using its natural XSAVE.  In other words, KVM's
0243      * uABI size may be larger than this host's default size.  Conversely,
0244      * the default size should never be larger than KVM's base uABI size;
0245      * all features that can expand the uABI size must be opt-in.
0246      */
0247     gfpu->uabi_size     = sizeof(struct kvm_xsave);
0248     if (WARN_ON_ONCE(fpu_user_cfg.default_size > gfpu->uabi_size))
0249         gfpu->uabi_size = fpu_user_cfg.default_size;
0250 
0251     fpu_init_guest_permissions(gfpu);
0252 
0253     return true;
0254 }
0255 EXPORT_SYMBOL_GPL(fpu_alloc_guest_fpstate);
0256 
0257 void fpu_free_guest_fpstate(struct fpu_guest *gfpu)
0258 {
0259     struct fpstate *fps = gfpu->fpstate;
0260 
0261     if (!fps)
0262         return;
0263 
0264     if (WARN_ON_ONCE(!fps->is_valloc || !fps->is_guest || fps->in_use))
0265         return;
0266 
0267     gfpu->fpstate = NULL;
0268     vfree(fps);
0269 }
0270 EXPORT_SYMBOL_GPL(fpu_free_guest_fpstate);
0271 
0272 /*
0273   * fpu_enable_guest_xfd_features - Check xfeatures against guest perm and enable
0274   * @guest_fpu:         Pointer to the guest FPU container
0275   * @xfeatures:         Features requested by guest CPUID
0276   *
0277   * Enable all dynamic xfeatures according to guest perm and requested CPUID.
0278   *
0279   * Return: 0 on success, error code otherwise
0280   */
0281 int fpu_enable_guest_xfd_features(struct fpu_guest *guest_fpu, u64 xfeatures)
0282 {
0283     lockdep_assert_preemption_enabled();
0284 
0285     /* Nothing to do if all requested features are already enabled. */
0286     xfeatures &= ~guest_fpu->xfeatures;
0287     if (!xfeatures)
0288         return 0;
0289 
0290     return __xfd_enable_feature(xfeatures, guest_fpu);
0291 }
0292 EXPORT_SYMBOL_GPL(fpu_enable_guest_xfd_features);
0293 
0294 #ifdef CONFIG_X86_64
0295 void fpu_update_guest_xfd(struct fpu_guest *guest_fpu, u64 xfd)
0296 {
0297     fpregs_lock();
0298     guest_fpu->fpstate->xfd = xfd;
0299     if (guest_fpu->fpstate->in_use)
0300         xfd_update_state(guest_fpu->fpstate);
0301     fpregs_unlock();
0302 }
0303 EXPORT_SYMBOL_GPL(fpu_update_guest_xfd);
0304 
0305 /**
0306  * fpu_sync_guest_vmexit_xfd_state - Synchronize XFD MSR and software state
0307  *
0308  * Must be invoked from KVM after a VMEXIT before enabling interrupts when
0309  * XFD write emulation is disabled. This is required because the guest can
0310  * freely modify XFD and the state at VMEXIT is not guaranteed to be the
0311  * same as the state on VMENTER. So software state has to be udpated before
0312  * any operation which depends on it can take place.
0313  *
0314  * Note: It can be invoked unconditionally even when write emulation is
0315  * enabled for the price of a then pointless MSR read.
0316  */
0317 void fpu_sync_guest_vmexit_xfd_state(void)
0318 {
0319     struct fpstate *fps = current->thread.fpu.fpstate;
0320 
0321     lockdep_assert_irqs_disabled();
0322     if (fpu_state_size_dynamic()) {
0323         rdmsrl(MSR_IA32_XFD, fps->xfd);
0324         __this_cpu_write(xfd_state, fps->xfd);
0325     }
0326 }
0327 EXPORT_SYMBOL_GPL(fpu_sync_guest_vmexit_xfd_state);
0328 #endif /* CONFIG_X86_64 */
0329 
0330 int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu, bool enter_guest)
0331 {
0332     struct fpstate *guest_fps = guest_fpu->fpstate;
0333     struct fpu *fpu = &current->thread.fpu;
0334     struct fpstate *cur_fps = fpu->fpstate;
0335 
0336     fpregs_lock();
0337     if (!cur_fps->is_confidential && !test_thread_flag(TIF_NEED_FPU_LOAD))
0338         save_fpregs_to_fpstate(fpu);
0339 
0340     /* Swap fpstate */
0341     if (enter_guest) {
0342         fpu->__task_fpstate = cur_fps;
0343         fpu->fpstate = guest_fps;
0344         guest_fps->in_use = true;
0345     } else {
0346         guest_fps->in_use = false;
0347         fpu->fpstate = fpu->__task_fpstate;
0348         fpu->__task_fpstate = NULL;
0349     }
0350 
0351     cur_fps = fpu->fpstate;
0352 
0353     if (!cur_fps->is_confidential) {
0354         /* Includes XFD update */
0355         restore_fpregs_from_fpstate(cur_fps, XFEATURE_MASK_FPSTATE);
0356     } else {
0357         /*
0358          * XSTATE is restored by firmware from encrypted
0359          * memory. Make sure XFD state is correct while
0360          * running with guest fpstate
0361          */
0362         xfd_update_state(cur_fps);
0363     }
0364 
0365     fpregs_mark_activate();
0366     fpregs_unlock();
0367     return 0;
0368 }
0369 EXPORT_SYMBOL_GPL(fpu_swap_kvm_fpstate);
0370 
0371 void fpu_copy_guest_fpstate_to_uabi(struct fpu_guest *gfpu, void *buf,
0372                     unsigned int size, u32 pkru)
0373 {
0374     struct fpstate *kstate = gfpu->fpstate;
0375     union fpregs_state *ustate = buf;
0376     struct membuf mb = { .p = buf, .left = size };
0377 
0378     if (cpu_feature_enabled(X86_FEATURE_XSAVE)) {
0379         __copy_xstate_to_uabi_buf(mb, kstate, pkru, XSTATE_COPY_XSAVE);
0380     } else {
0381         memcpy(&ustate->fxsave, &kstate->regs.fxsave,
0382                sizeof(ustate->fxsave));
0383         /* Make it restorable on a XSAVE enabled host */
0384         ustate->xsave.header.xfeatures = XFEATURE_MASK_FPSSE;
0385     }
0386 }
0387 EXPORT_SYMBOL_GPL(fpu_copy_guest_fpstate_to_uabi);
0388 
0389 int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf,
0390                    u64 xcr0, u32 *vpkru)
0391 {
0392     struct fpstate *kstate = gfpu->fpstate;
0393     const union fpregs_state *ustate = buf;
0394     struct pkru_state *xpkru;
0395     int ret;
0396 
0397     if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) {
0398         if (ustate->xsave.header.xfeatures & ~XFEATURE_MASK_FPSSE)
0399             return -EINVAL;
0400         if (ustate->fxsave.mxcsr & ~mxcsr_feature_mask)
0401             return -EINVAL;
0402         memcpy(&kstate->regs.fxsave, &ustate->fxsave, sizeof(ustate->fxsave));
0403         return 0;
0404     }
0405 
0406     if (ustate->xsave.header.xfeatures & ~xcr0)
0407         return -EINVAL;
0408 
0409     ret = copy_uabi_from_kernel_to_xstate(kstate, ustate);
0410     if (ret)
0411         return ret;
0412 
0413     /* Retrieve PKRU if not in init state */
0414     if (kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU) {
0415         xpkru = get_xsave_addr(&kstate->regs.xsave, XFEATURE_PKRU);
0416         *vpkru = xpkru->pkru;
0417     }
0418     return 0;
0419 }
0420 EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate);
0421 #endif /* CONFIG_KVM */
0422 
0423 void kernel_fpu_begin_mask(unsigned int kfpu_mask)
0424 {
0425     preempt_disable();
0426 
0427     WARN_ON_FPU(!irq_fpu_usable());
0428     WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
0429 
0430     this_cpu_write(in_kernel_fpu, true);
0431 
0432     if (!(current->flags & PF_KTHREAD) &&
0433         !test_thread_flag(TIF_NEED_FPU_LOAD)) {
0434         set_thread_flag(TIF_NEED_FPU_LOAD);
0435         save_fpregs_to_fpstate(&current->thread.fpu);
0436     }
0437     __cpu_invalidate_fpregs_state();
0438 
0439     /* Put sane initial values into the control registers. */
0440     if (likely(kfpu_mask & KFPU_MXCSR) && boot_cpu_has(X86_FEATURE_XMM))
0441         ldmxcsr(MXCSR_DEFAULT);
0442 
0443     if (unlikely(kfpu_mask & KFPU_387) && boot_cpu_has(X86_FEATURE_FPU))
0444         asm volatile ("fninit");
0445 }
0446 EXPORT_SYMBOL_GPL(kernel_fpu_begin_mask);
0447 
0448 void kernel_fpu_end(void)
0449 {
0450     WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
0451 
0452     this_cpu_write(in_kernel_fpu, false);
0453     preempt_enable();
0454 }
0455 EXPORT_SYMBOL_GPL(kernel_fpu_end);
0456 
0457 /*
0458  * Sync the FPU register state to current's memory register state when the
0459  * current task owns the FPU. The hardware register state is preserved.
0460  */
0461 void fpu_sync_fpstate(struct fpu *fpu)
0462 {
0463     WARN_ON_FPU(fpu != &current->thread.fpu);
0464 
0465     fpregs_lock();
0466     trace_x86_fpu_before_save(fpu);
0467 
0468     if (!test_thread_flag(TIF_NEED_FPU_LOAD))
0469         save_fpregs_to_fpstate(fpu);
0470 
0471     trace_x86_fpu_after_save(fpu);
0472     fpregs_unlock();
0473 }
0474 
0475 static inline unsigned int init_fpstate_copy_size(void)
0476 {
0477     if (!use_xsave())
0478         return fpu_kernel_cfg.default_size;
0479 
0480     /* XSAVE(S) just needs the legacy and the xstate header part */
0481     return sizeof(init_fpstate.regs.xsave);
0482 }
0483 
0484 static inline void fpstate_init_fxstate(struct fpstate *fpstate)
0485 {
0486     fpstate->regs.fxsave.cwd = 0x37f;
0487     fpstate->regs.fxsave.mxcsr = MXCSR_DEFAULT;
0488 }
0489 
0490 /*
0491  * Legacy x87 fpstate state init:
0492  */
0493 static inline void fpstate_init_fstate(struct fpstate *fpstate)
0494 {
0495     fpstate->regs.fsave.cwd = 0xffff037fu;
0496     fpstate->regs.fsave.swd = 0xffff0000u;
0497     fpstate->regs.fsave.twd = 0xffffffffu;
0498     fpstate->regs.fsave.fos = 0xffff0000u;
0499 }
0500 
0501 /*
0502  * Used in two places:
0503  * 1) Early boot to setup init_fpstate for non XSAVE systems
0504  * 2) fpu_init_fpstate_user() which is invoked from KVM
0505  */
0506 void fpstate_init_user(struct fpstate *fpstate)
0507 {
0508     if (!cpu_feature_enabled(X86_FEATURE_FPU)) {
0509         fpstate_init_soft(&fpstate->regs.soft);
0510         return;
0511     }
0512 
0513     xstate_init_xcomp_bv(&fpstate->regs.xsave, fpstate->xfeatures);
0514 
0515     if (cpu_feature_enabled(X86_FEATURE_FXSR))
0516         fpstate_init_fxstate(fpstate);
0517     else
0518         fpstate_init_fstate(fpstate);
0519 }
0520 
0521 static void __fpstate_reset(struct fpstate *fpstate, u64 xfd)
0522 {
0523     /* Initialize sizes and feature masks */
0524     fpstate->size       = fpu_kernel_cfg.default_size;
0525     fpstate->user_size  = fpu_user_cfg.default_size;
0526     fpstate->xfeatures  = fpu_kernel_cfg.default_features;
0527     fpstate->user_xfeatures = fpu_user_cfg.default_features;
0528     fpstate->xfd        = xfd;
0529 }
0530 
0531 void fpstate_reset(struct fpu *fpu)
0532 {
0533     /* Set the fpstate pointer to the default fpstate */
0534     fpu->fpstate = &fpu->__fpstate;
0535     __fpstate_reset(fpu->fpstate, init_fpstate.xfd);
0536 
0537     /* Initialize the permission related info in fpu */
0538     fpu->perm.__state_perm      = fpu_kernel_cfg.default_features;
0539     fpu->perm.__state_size      = fpu_kernel_cfg.default_size;
0540     fpu->perm.__user_state_size = fpu_user_cfg.default_size;
0541     /* Same defaults for guests */
0542     fpu->guest_perm = fpu->perm;
0543 }
0544 
0545 static inline void fpu_inherit_perms(struct fpu *dst_fpu)
0546 {
0547     if (fpu_state_size_dynamic()) {
0548         struct fpu *src_fpu = &current->group_leader->thread.fpu;
0549 
0550         spin_lock_irq(&current->sighand->siglock);
0551         /* Fork also inherits the permissions of the parent */
0552         dst_fpu->perm = src_fpu->perm;
0553         dst_fpu->guest_perm = src_fpu->guest_perm;
0554         spin_unlock_irq(&current->sighand->siglock);
0555     }
0556 }
0557 
0558 /* Clone current's FPU state on fork */
0559 int fpu_clone(struct task_struct *dst, unsigned long clone_flags, bool minimal)
0560 {
0561     struct fpu *src_fpu = &current->thread.fpu;
0562     struct fpu *dst_fpu = &dst->thread.fpu;
0563 
0564     /* The new task's FPU state cannot be valid in the hardware. */
0565     dst_fpu->last_cpu = -1;
0566 
0567     fpstate_reset(dst_fpu);
0568 
0569     if (!cpu_feature_enabled(X86_FEATURE_FPU))
0570         return 0;
0571 
0572     /*
0573      * Enforce reload for user space tasks and prevent kernel threads
0574      * from trying to save the FPU registers on context switch.
0575      */
0576     set_tsk_thread_flag(dst, TIF_NEED_FPU_LOAD);
0577 
0578     /*
0579      * No FPU state inheritance for kernel threads and IO
0580      * worker threads.
0581      */
0582     if (minimal) {
0583         /* Clear out the minimal state */
0584         memcpy(&dst_fpu->fpstate->regs, &init_fpstate.regs,
0585                init_fpstate_copy_size());
0586         return 0;
0587     }
0588 
0589     /*
0590      * If a new feature is added, ensure all dynamic features are
0591      * caller-saved from here!
0592      */
0593     BUILD_BUG_ON(XFEATURE_MASK_USER_DYNAMIC != XFEATURE_MASK_XTILE_DATA);
0594 
0595     /*
0596      * Save the default portion of the current FPU state into the
0597      * clone. Assume all dynamic features to be defined as caller-
0598      * saved, which enables skipping both the expansion of fpstate
0599      * and the copying of any dynamic state.
0600      *
0601      * Do not use memcpy() when TIF_NEED_FPU_LOAD is set because
0602      * copying is not valid when current uses non-default states.
0603      */
0604     fpregs_lock();
0605     if (test_thread_flag(TIF_NEED_FPU_LOAD))
0606         fpregs_restore_userregs();
0607     save_fpregs_to_fpstate(dst_fpu);
0608     if (!(clone_flags & CLONE_THREAD))
0609         fpu_inherit_perms(dst_fpu);
0610     fpregs_unlock();
0611 
0612     /*
0613      * Children never inherit PASID state.
0614      * Force it to have its init value:
0615      */
0616     if (use_xsave())
0617         dst_fpu->fpstate->regs.xsave.header.xfeatures &= ~XFEATURE_MASK_PASID;
0618 
0619     trace_x86_fpu_copy_src(src_fpu);
0620     trace_x86_fpu_copy_dst(dst_fpu);
0621 
0622     return 0;
0623 }
0624 
0625 /*
0626  * Whitelist the FPU register state embedded into task_struct for hardened
0627  * usercopy.
0628  */
0629 void fpu_thread_struct_whitelist(unsigned long *offset, unsigned long *size)
0630 {
0631     *offset = offsetof(struct thread_struct, fpu.__fpstate.regs);
0632     *size = fpu_kernel_cfg.default_size;
0633 }
0634 
0635 /*
0636  * Drops current FPU state: deactivates the fpregs and
0637  * the fpstate. NOTE: it still leaves previous contents
0638  * in the fpregs in the eager-FPU case.
0639  *
0640  * This function can be used in cases where we know that
0641  * a state-restore is coming: either an explicit one,
0642  * or a reschedule.
0643  */
0644 void fpu__drop(struct fpu *fpu)
0645 {
0646     preempt_disable();
0647 
0648     if (fpu == &current->thread.fpu) {
0649         /* Ignore delayed exceptions from user space */
0650         asm volatile("1: fwait\n"
0651                  "2:\n"
0652                  _ASM_EXTABLE(1b, 2b));
0653         fpregs_deactivate(fpu);
0654     }
0655 
0656     trace_x86_fpu_dropped(fpu);
0657 
0658     preempt_enable();
0659 }
0660 
0661 /*
0662  * Clear FPU registers by setting them up from the init fpstate.
0663  * Caller must do fpregs_[un]lock() around it.
0664  */
0665 static inline void restore_fpregs_from_init_fpstate(u64 features_mask)
0666 {
0667     if (use_xsave())
0668         os_xrstor(&init_fpstate, features_mask);
0669     else if (use_fxsr())
0670         fxrstor(&init_fpstate.regs.fxsave);
0671     else
0672         frstor(&init_fpstate.regs.fsave);
0673 
0674     pkru_write_default();
0675 }
0676 
0677 /*
0678  * Reset current->fpu memory state to the init values.
0679  */
0680 static void fpu_reset_fpregs(void)
0681 {
0682     struct fpu *fpu = &current->thread.fpu;
0683 
0684     fpregs_lock();
0685     fpu__drop(fpu);
0686     /*
0687      * This does not change the actual hardware registers. It just
0688      * resets the memory image and sets TIF_NEED_FPU_LOAD so a
0689      * subsequent return to usermode will reload the registers from the
0690      * task's memory image.
0691      *
0692      * Do not use fpstate_init() here. Just copy init_fpstate which has
0693      * the correct content already except for PKRU.
0694      *
0695      * PKRU handling does not rely on the xstate when restoring for
0696      * user space as PKRU is eagerly written in switch_to() and
0697      * flush_thread().
0698      */
0699     memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size());
0700     set_thread_flag(TIF_NEED_FPU_LOAD);
0701     fpregs_unlock();
0702 }
0703 
0704 /*
0705  * Reset current's user FPU states to the init states.  current's
0706  * supervisor states, if any, are not modified by this function.  The
0707  * caller guarantees that the XSTATE header in memory is intact.
0708  */
0709 void fpu__clear_user_states(struct fpu *fpu)
0710 {
0711     WARN_ON_FPU(fpu != &current->thread.fpu);
0712 
0713     fpregs_lock();
0714     if (!cpu_feature_enabled(X86_FEATURE_FPU)) {
0715         fpu_reset_fpregs();
0716         fpregs_unlock();
0717         return;
0718     }
0719 
0720     /*
0721      * Ensure that current's supervisor states are loaded into their
0722      * corresponding registers.
0723      */
0724     if (xfeatures_mask_supervisor() &&
0725         !fpregs_state_valid(fpu, smp_processor_id()))
0726         os_xrstor_supervisor(fpu->fpstate);
0727 
0728     /* Reset user states in registers. */
0729     restore_fpregs_from_init_fpstate(XFEATURE_MASK_USER_RESTORE);
0730 
0731     /*
0732      * Now all FPU registers have their desired values.  Inform the FPU
0733      * state machine that current's FPU registers are in the hardware
0734      * registers. The memory image does not need to be updated because
0735      * any operation relying on it has to save the registers first when
0736      * current's FPU is marked active.
0737      */
0738     fpregs_mark_activate();
0739     fpregs_unlock();
0740 }
0741 
0742 void fpu_flush_thread(void)
0743 {
0744     fpstate_reset(&current->thread.fpu);
0745     fpu_reset_fpregs();
0746 }
0747 /*
0748  * Load FPU context before returning to userspace.
0749  */
0750 void switch_fpu_return(void)
0751 {
0752     if (!static_cpu_has(X86_FEATURE_FPU))
0753         return;
0754 
0755     fpregs_restore_userregs();
0756 }
0757 EXPORT_SYMBOL_GPL(switch_fpu_return);
0758 
0759 #ifdef CONFIG_X86_DEBUG_FPU
0760 /*
0761  * If current FPU state according to its tracking (loaded FPU context on this
0762  * CPU) is not valid then we must have TIF_NEED_FPU_LOAD set so the context is
0763  * loaded on return to userland.
0764  */
0765 void fpregs_assert_state_consistent(void)
0766 {
0767     struct fpu *fpu = &current->thread.fpu;
0768 
0769     if (test_thread_flag(TIF_NEED_FPU_LOAD))
0770         return;
0771 
0772     WARN_ON_FPU(!fpregs_state_valid(fpu, smp_processor_id()));
0773 }
0774 EXPORT_SYMBOL_GPL(fpregs_assert_state_consistent);
0775 #endif
0776 
0777 void fpregs_mark_activate(void)
0778 {
0779     struct fpu *fpu = &current->thread.fpu;
0780 
0781     fpregs_activate(fpu);
0782     fpu->last_cpu = smp_processor_id();
0783     clear_thread_flag(TIF_NEED_FPU_LOAD);
0784 }
0785 
0786 /*
0787  * x87 math exception handling:
0788  */
0789 
0790 int fpu__exception_code(struct fpu *fpu, int trap_nr)
0791 {
0792     int err;
0793 
0794     if (trap_nr == X86_TRAP_MF) {
0795         unsigned short cwd, swd;
0796         /*
0797          * (~cwd & swd) will mask out exceptions that are not set to unmasked
0798          * status.  0x3f is the exception bits in these regs, 0x200 is the
0799          * C1 reg you need in case of a stack fault, 0x040 is the stack
0800          * fault bit.  We should only be taking one exception at a time,
0801          * so if this combination doesn't produce any single exception,
0802          * then we have a bad program that isn't synchronizing its FPU usage
0803          * and it will suffer the consequences since we won't be able to
0804          * fully reproduce the context of the exception.
0805          */
0806         if (boot_cpu_has(X86_FEATURE_FXSR)) {
0807             cwd = fpu->fpstate->regs.fxsave.cwd;
0808             swd = fpu->fpstate->regs.fxsave.swd;
0809         } else {
0810             cwd = (unsigned short)fpu->fpstate->regs.fsave.cwd;
0811             swd = (unsigned short)fpu->fpstate->regs.fsave.swd;
0812         }
0813 
0814         err = swd & ~cwd;
0815     } else {
0816         /*
0817          * The SIMD FPU exceptions are handled a little differently, as there
0818          * is only a single status/control register.  Thus, to determine which
0819          * unmasked exception was caught we must mask the exception mask bits
0820          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
0821          */
0822         unsigned short mxcsr = MXCSR_DEFAULT;
0823 
0824         if (boot_cpu_has(X86_FEATURE_XMM))
0825             mxcsr = fpu->fpstate->regs.fxsave.mxcsr;
0826 
0827         err = ~(mxcsr >> 7) & mxcsr;
0828     }
0829 
0830     if (err & 0x001) {  /* Invalid op */
0831         /*
0832          * swd & 0x240 == 0x040: Stack Underflow
0833          * swd & 0x240 == 0x240: Stack Overflow
0834          * User must clear the SF bit (0x40) if set
0835          */
0836         return FPE_FLTINV;
0837     } else if (err & 0x004) { /* Divide by Zero */
0838         return FPE_FLTDIV;
0839     } else if (err & 0x008) { /* Overflow */
0840         return FPE_FLTOVF;
0841     } else if (err & 0x012) { /* Denormal, Underflow */
0842         return FPE_FLTUND;
0843     } else if (err & 0x020) { /* Precision */
0844         return FPE_FLTRES;
0845     }
0846 
0847     /*
0848      * If we're using IRQ 13, or supposedly even some trap
0849      * X86_TRAP_MF implementations, it's possible
0850      * we get a spurious trap, which is not an error.
0851      */
0852     return 0;
0853 }
0854 
0855 /*
0856  * Initialize register state that may prevent from entering low-power idle.
0857  * This function will be invoked from the cpuidle driver only when needed.
0858  */
0859 void fpu_idle_fpregs(void)
0860 {
0861     /* Note: AMX_TILE being enabled implies XGETBV1 support */
0862     if (cpu_feature_enabled(X86_FEATURE_AMX_TILE) &&
0863         (xfeatures_in_use() & XFEATURE_MASK_XTILE)) {
0864         tile_release();
0865         fpregs_deactivate(&current->thread.fpu);
0866     }
0867 }