@@ -56,7 +56,46 @@ static void __hyp_text __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
5656 vcpu -> arch .ctxt .sys_regs [FPEXC32_EL2 ] = read_sysreg (fpexc32_el2 );
5757}
5858
59- static void __hyp_text __activate_traps_vhe (void )
59+ static void __hyp_text __activate_traps_fpsimd32 (struct kvm_vcpu * vcpu )
60+ {
61+ /*
62+ * We are about to set CPTR_EL2.TFP to trap all floating point
63+ * register accesses to EL2, however, the ARM ARM clearly states that
64+ * traps are only taken to EL2 if the operation would not otherwise
65+ * trap to EL1. Therefore, always make sure that for 32-bit guests,
66+ * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
67+ * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
68+ * it will cause an exception.
69+ */
70+ if (vcpu_el1_is_32bit (vcpu ) && system_supports_fpsimd ()) {
71+ write_sysreg (1 << 30 , fpexc32_el2 );
72+ isb ();
73+ }
74+ }
75+
76+ static void __hyp_text __activate_traps_common (struct kvm_vcpu * vcpu )
77+ {
78+ /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
79+ write_sysreg (1 << 15 , hstr_el2 );
80+
81+ /*
82+ * Make sure we trap PMU access from EL0 to EL2. Also sanitize
83+ * PMSELR_EL0 to make sure it never contains the cycle
84+ * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
85+ * EL1 instead of being trapped to EL2.
86+ */
87+ write_sysreg (0 , pmselr_el0 );
88+ write_sysreg (ARMV8_PMU_USERENR_MASK , pmuserenr_el0 );
89+ write_sysreg (vcpu -> arch .mdcr_el2 , mdcr_el2 );
90+ }
91+
92+ static void __hyp_text __deactivate_traps_common (void )
93+ {
94+ write_sysreg (0 , hstr_el2 );
95+ write_sysreg (0 , pmuserenr_el0 );
96+ }
97+
98+ static void __hyp_text __activate_traps_vhe (struct kvm_vcpu * vcpu )
6099{
61100 u64 val ;
62101
@@ -68,7 +107,7 @@ static void __hyp_text __activate_traps_vhe(void)
68107 write_sysreg (kvm_get_hyp_vector (), vbar_el1 );
69108}
70109
71- static void __hyp_text __activate_traps_nvhe (void )
110+ static void __hyp_text __activate_traps_nvhe (struct kvm_vcpu * vcpu )
72111{
73112 u64 val ;
74113
@@ -85,37 +124,14 @@ static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
85124{
86125 u64 hcr = vcpu -> arch .hcr_el2 ;
87126
88- /*
89- * We are about to set CPTR_EL2.TFP to trap all floating point
90- * register accesses to EL2, however, the ARM ARM clearly states that
91- * traps are only taken to EL2 if the operation would not otherwise
92- * trap to EL1. Therefore, always make sure that for 32-bit guests,
93- * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
94- * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
95- * it will cause an exception.
96- */
97- if (vcpu_el1_is_32bit (vcpu ) && system_supports_fpsimd ()) {
98- write_sysreg (1 << 30 , fpexc32_el2 );
99- isb ();
100- }
127+ write_sysreg (hcr , hcr_el2 );
101128
102129 if (cpus_have_const_cap (ARM64_HAS_RAS_EXTN ) && (hcr & HCR_VSE ))
103130 write_sysreg_s (vcpu -> arch .vsesr_el2 , SYS_VSESR_EL2 );
104131
105- write_sysreg (hcr , hcr_el2 );
106-
107- /* Trap on AArch32 cp15 c15 accesses (EL1 or EL0) */
108- write_sysreg (1 << 15 , hstr_el2 );
109- /*
110- * Make sure we trap PMU access from EL0 to EL2. Also sanitize
111- * PMSELR_EL0 to make sure it never contains the cycle
112- * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
113- * EL1 instead of being trapped to EL2.
114- */
115- write_sysreg (0 , pmselr_el0 );
116- write_sysreg (ARMV8_PMU_USERENR_MASK , pmuserenr_el0 );
117- write_sysreg (vcpu -> arch .mdcr_el2 , mdcr_el2 );
118- __activate_traps_arch ()();
132+ __activate_traps_fpsimd32 (vcpu );
133+ __activate_traps_common (vcpu );
134+ __activate_traps_arch ()(vcpu );
119135}
120136
121137static void __hyp_text __deactivate_traps_vhe (void )
@@ -160,9 +176,8 @@ static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
160176 if (vcpu -> arch .hcr_el2 & HCR_VSE )
161177 vcpu -> arch .hcr_el2 = read_sysreg (hcr_el2 );
162178
179+ __deactivate_traps_common ();
163180 __deactivate_traps_arch ()();
164- write_sysreg (0 , hstr_el2 );
165- write_sysreg (0 , pmuserenr_el0 );
166181}
167182
168183static void __hyp_text __activate_vm (struct kvm * kvm )
0 commit comments