Configure hcr_el2 with parameter twe_delay when TWED is enabled
Jingyi Wang (1): KVM: arm64: Make use of TWED feature
Zengruan Ye (1): KVM: arm64: cpufeature: TWED support detection
arch/arm64/Kconfig | 10 ++++++ arch/arm64/configs/openeuler_defconfig | 6 ++++ arch/arm64/include/asm/kvm_arm.h | 7 ++++ arch/arm64/include/asm/kvm_emulate.h | 44 ++++++++++++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 5 +++ arch/arm64/kernel/cpufeature.c | 9 ++++++ arch/arm64/kvm/arm.c | 10 ++++++ arch/arm64/tools/cpucaps | 1 + 8 files changed, 92 insertions(+)
From: Zengruan Ye yezengruan@huawei.com
virt inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8WMG6 CVE: NA
-----------------------------
TWE Delay is an optional feature in ARMv8.6 Extensions. This patch detect this feature.
Signed-off-by: Zengruan Ye yezengruan@huawei.com Signed-off-by: Jingyi Wang wangjingyi11@huawei.com Signed-off-by: lishusen lishusen2@huawei.com --- arch/arm64/Kconfig | 10 ++++++++++ arch/arm64/configs/openeuler_defconfig | 6 ++++++ arch/arm64/kernel/cpufeature.c | 9 +++++++++ arch/arm64/tools/cpucaps | 1 + 4 files changed, 26 insertions(+)
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 85ac1e83f747..e3821be64a22 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2183,6 +2183,16 @@ config ARM64_MTE
endmenu # "ARMv8.5 architectural features"
+menu "ARMv8.6 architectural features" + +config ARM64_TWED + bool "Enable support for delayed trapping of WFE" + default y + help + Delayed Trapping of WFE (part of the ARMv8.6 Extensions) + +endmenu + menu "ARMv8.7 architectural features"
config ARM64_EPAN diff --git a/arch/arm64/configs/openeuler_defconfig b/arch/arm64/configs/openeuler_defconfig index eb65de2f5a09..eec1886d07e9 100644 --- a/arch/arm64/configs/openeuler_defconfig +++ b/arch/arm64/configs/openeuler_defconfig @@ -533,6 +533,12 @@ CONFIG_ARM64_AS_HAS_MTE=y # CONFIG_ARM64_MTE is not set # end of ARMv8.5 architectural features
+# +# ARMv8.6 architectural features +# +CONFIG_ARM64_TWED=y +# end of ARMv8.6 architectural features + # # ARMv8.7 architectural features # diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 9410e4a032dd..a51aa8e6a92c 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -2791,6 +2791,15 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .cpu_enable = cpu_enable_mpam, ARM64_CPUID_FIELDS(ID_AA64PFR0_EL1, MPAM, 1) }, +#endif +#ifdef CONFIG_ARM64_TWED + { + .desc = "Delayed Trapping of WFE", + .capability = ARM64_HAS_TWED, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_cpuid_feature, + ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, TWED, IMP) + }, #endif {}, }; diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps index c60a603d2cfe..30b0cdaeb192 100644 --- a/arch/arm64/tools/cpucaps +++ b/arch/arm64/tools/cpucaps @@ -50,6 +50,7 @@ HAS_STAGE2_FWB HAS_TCR2 HAS_TIDCP1 HAS_TLB_RANGE +HAS_TWED HAS_VIRT_HOST_EXTN HAS_WFXT HW_DBM
From: Jingyi Wang wangjingyi11@huawei.com
virt inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8WMG6 CVE: NA
-----------------------------
For HCR_EL2, TWEDEn(bit[59]) decides whether TWED is enabled, and when the configurable delay is enabled, TWEDEL (bits[63:60]) encodes the minimum delay in taking a trap of WFE caused by the TWE bit in this register as 2^(TWEDEL + 8) cycles.
We use two kernel parameters "twed_enable" and "twe_delay" to configure the register.
Signed-off-by: Zengruan Ye yezengruan@huawei.com Signed-off-by: Jingyi Wang wangjingyi11@huawei.com Signed-off-by: lishusen lishusen2@huawei.com --- arch/arm64/include/asm/kvm_arm.h | 7 +++++ arch/arm64/include/asm/kvm_emulate.h | 44 ++++++++++++++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 5 ++++ arch/arm64/kvm/arm.c | 10 +++++++ 4 files changed, 66 insertions(+)
diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h index 2d8b243a86cd..9385293e6d45 100644 --- a/arch/arm64/include/asm/kvm_arm.h +++ b/arch/arm64/include/asm/kvm_arm.h @@ -14,6 +14,7 @@
/* Hyp Configuration Register (HCR) bits */
+#define HCR_TWEDEN (UL(1) << 59) #define HCR_TID5 (UL(1) << 58) #define HCR_DCT (UL(1) << 57) #define HCR_ATA_SHIFT 56 @@ -74,6 +75,12 @@ #define HCR_VM (UL(1) << 0) #define HCR_RES0 ((UL(1) << 48) | (UL(1) << 39))
+#ifdef CONFIG_ARM64_TWED +#define HCR_TWEDEL_SHIFT 60 +#define HCR_TWEDEL_MAX (UL(0xf)) +#define HCR_TWEDEL_MASK (HCR_TWEDEL_MAX << HCR_TWEDEL_SHIFT) +#endif + /* * The bits we set in HCR: * TLOR: Trap LORegion register accesses diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index 3d6725ff0bf6..3592a1d944e4 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -124,6 +124,50 @@ static inline void vcpu_set_wfx_traps(struct kvm_vcpu *vcpu) vcpu->arch.hcr_el2 |= HCR_TWI; }
+#ifdef CONFIG_ARM64_TWED +static inline void vcpu_twed_enable(struct kvm_vcpu *vcpu) +{ + if (!cpus_have_const_cap(ARM64_HAS_TWED)) + return; + + vcpu->arch.hcr_el2 |= HCR_TWEDEN; +} + +static inline void vcpu_twed_disable(struct kvm_vcpu *vcpu) +{ + if (!cpus_have_const_cap(ARM64_HAS_TWED)) + return; + + vcpu->arch.hcr_el2 &= ~HCR_TWEDEN; +} + +static inline void vcpu_set_twe_delay(struct kvm_vcpu *vcpu) +{ + if (!cpus_have_const_cap(ARM64_HAS_TWED)) + return; + + u64 delay = (u64)twe_delay; + + if (delay > HCR_TWEDEL_MAX) + delay = HCR_TWEDEL_MAX; + + vcpu->arch.hcr_el2 &= ~HCR_TWEDEL_MASK; + vcpu->arch.hcr_el2 |= (delay << HCR_TWEDEL_SHIFT); +} + +static inline void vcpu_set_twed(struct kvm_vcpu *vcpu) +{ + if (cpus_have_const_cap(ARM64_HAS_TWED) && twed_enable) { + vcpu_twed_enable(vcpu); + vcpu_set_twe_delay(vcpu); + } else { + vcpu_twed_disable(vcpu); + } +} +#else +static inline void vcpu_set_twed(struct kvm_vcpu *vcpu) {} +#endif + static inline void vcpu_ptrauth_enable(struct kvm_vcpu *vcpu) { vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK); diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index af06ccb7ee34..cf640d16d6af 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1151,6 +1151,11 @@ void __init kvm_hyp_reserve(void); static inline void kvm_hyp_reserve(void) { } #endif
+#ifdef CONFIG_ARM64_TWED +extern bool twed_enable; +extern unsigned int twe_delay; +#endif + void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu); bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 4866b3f7b4ea..02af6e6e8bfd 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -64,6 +64,14 @@ bool is_kvm_arm_initialised(void) return kvm_arm_initialised; }
+#ifdef CONFIG_ARM64_TWED +bool twed_enable; +module_param(twed_enable, bool, 0644); + +unsigned int twe_delay; +module_param(twe_delay, uint, 0644); +#endif + int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) { return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; @@ -983,6 +991,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) kvm_arm_setup_debug(vcpu); kvm_arch_vcpu_ctxflush_fp(vcpu);
+ vcpu_set_twed(vcpu); + /************************************************************** * Enter the guest */
反馈: 您发送到kernel@openeuler.org的补丁/补丁集,已成功转换为PR! PR链接地址: https://gitee.com/openeuler/kernel/pulls/4556 邮件列表地址:https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/W...
FeedBack: The patch(es) which you have sent to kernel@openeuler.org mailing list has been converted to a pull request successfully! Pull request link: https://gitee.com/openeuler/kernel/pulls/4556 Mailing list address: https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/W...