Support feature TWED
Jingyi Wang (1): KVM: arm64: Make use of TWED feature
Zengruan Ye (1): arm64: cpufeature: TWED support detection
arch/arm64/Kconfig | 10 ++++++++++ arch/arm64/include/asm/kvm_arm.h | 8 ++++++++ arch/arm64/include/asm/kvm_emulate.h | 27 +++++++++++++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 8 ++++++++ arch/arm64/include/asm/virt.h | 5 +++++ arch/arm64/kernel/cpufeature.c | 12 ++++++++++++ arch/arm64/kvm/arm.c | 15 +++++++++++++++ arch/arm64/tools/cpucaps | 1 + 8 files changed, 86 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 Reviewed-by: Keqian Zhu zhukeqian1@huawei.com Signed-off-by: Zheng Zengkai zhengzengkai@huawei.com
Signed-off-by: lishusen lishusen2@huawei.com --- arch/arm64/Kconfig | 10 ++++++++++ arch/arm64/kernel/cpufeature.c | 12 ++++++++++++ arch/arm64/tools/cpucaps | 1 + 3 files changed, 23 insertions(+)
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 9568049f36e3..ed1fd81acacd 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2175,6 +2175,16 @@ config ARM64_EPAN if the cpu does not implement the feature. endmenu # "ARMv8.7 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 + config ARM64_SVE bool "ARM Scalable Vector Extension support" default y diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 455e72ce080a..9169d622805d 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -2722,6 +2722,18 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature, ARM64_CPUID_FIELDS(ID_AA64MMFR2_EL1, EVT, IMP) }, +#ifdef CONFIG_ARM64_TWED + { + .desc = "Delayed Trapping of WFE", + .capability = ARM64_HAS_TWED, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_cpuid_feature, + .sys_reg = SYS_ID_AA64MMFR1_EL1, + .field_pos = ID_AA64MMFR1_EL1_TWED_SHIFT, + .sign = FTR_UNSIGNED, + .min_field_value = 1, + }, +#endif {}, };
diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps index 569ecec76c16..fea9f598f4ef 100644 --- a/arch/arm64/tools/cpucaps +++ b/arch/arm64/tools/cpucaps @@ -52,6 +52,7 @@ HAS_TIDCP1 HAS_TLB_RANGE HAS_VIRT_HOST_EXTN HAS_WFXT +HAS_TWED HW_DBM KVM_HVHE KVM_PROTECTED_MODE
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 "twedel" to configure the register.
Signed-off-by: Zengruan Ye yezengruan@huawei.com Signed-off-by: Jingyi Wang wangjingyi11@huawei.com Reviewed-by: Keqian Zhu zhukeqian1@huawei.com Signed-off-by: Zheng Zengkai zhengzengkai@huawei.com
Signed-off-by: lishusen lishusen2@huawei.com --- arch/arm64/include/asm/kvm_arm.h | 8 ++++++++ arch/arm64/include/asm/kvm_emulate.h | 27 +++++++++++++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 8 ++++++++ arch/arm64/include/asm/virt.h | 5 +++++ arch/arm64/kvm/arm.c | 15 +++++++++++++++ 5 files changed, 63 insertions(+)
diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h index 1095c6647e96..a661c99d129a 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,13 @@ #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) +#define HCR_TWEDEL (UL(1) << 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..8bca453739d0 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -124,6 +124,33 @@ 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) +{ + vcpu->arch.hcr_el2 |= HCR_TWEDEN; +} + +static inline void vcpu_twed_disable(struct kvm_vcpu *vcpu) +{ + vcpu->arch.hcr_el2 &= ~HCR_TWEDEN; +} + +static inline void vcpu_set_twed(struct kvm_vcpu *vcpu) +{ + u64 delay = (u64)twedel; + + if (delay > HCR_TWEDEL_MAX) + delay = HCR_TWEDEL_MAX; + + vcpu->arch.hcr_el2 &= ~HCR_TWEDEL_MASK; + vcpu->arch.hcr_el2 |= (delay << HCR_TWEDEL_SHIFT); +} +#else +static inline void vcpu_twed_enable(struct kvm_vcpu *vcpu) {}; +static inline void vcpu_twed_disable(struct kvm_vcpu *vcpu) {}; +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 e9bf556f206f..8a96ff22e64b 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1167,6 +1167,14 @@ static inline void kvm_hyp_reserve(void) { } void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu); bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
+#ifdef CONFIG_ARM64_TWED +#define use_twed() (has_twed() && twed_enable) +extern bool twed_enable; +extern unsigned int twedel; +#else +#define use_twed() (false) +#endif + extern bool kvm_ncsnp_support; extern bool kvm_dvmbm_support;
diff --git a/arch/arm64/include/asm/virt.h b/arch/arm64/include/asm/virt.h index 261d6e9df2e1..198f4f35534e 100644 --- a/arch/arm64/include/asm/virt.h +++ b/arch/arm64/include/asm/virt.h @@ -156,6 +156,11 @@ static inline bool is_hyp_nvhe(void) return is_hyp_mode_available() && !is_kernel_in_hyp_mode(); }
+static __always_inline bool has_twed(void) +{ + return cpus_have_const_cap(ARM64_HAS_TWED); +} + #endif /* __ASSEMBLY__ */
#endif /* ! __ASM__VIRT_H */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index ccef39bcf6ac..dce5804b5df2 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -74,6 +74,14 @@ bool is_kvm_arm_initialised(void) return kvm_arm_initialised; }
+#ifdef CONFIG_ARM64_TWED +bool twed_enable; +module_param(twed_enable, bool, S_IRUGO | S_IWUSR); + +unsigned int twedel; +module_param(twedel, uint, S_IRUGO | S_IWUSR); +#endif + int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) { return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; @@ -1027,6 +1035,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) kvm_arm_setup_debug(vcpu); kvm_arch_vcpu_ctxflush_fp(vcpu);
+ if (use_twed()) { + vcpu_twed_enable(vcpu); + vcpu_set_twed(vcpu); + } else { + vcpu_twed_disable(vcpu); + } + /************************************************************** * Enter the guest */
反馈: 您发送到kernel@openeuler.org的补丁/补丁集,转换为PR失败! 邮件列表地址:https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/X... 失败原因:应用补丁/补丁集失败,Patch failed at 0002 KVM: arm64: Make use of TWED feature 建议解决方法:请查看失败原因, 确认补丁是否可以应用在当前期望分支的最新代码上
FeedBack: The patch(es) which you have sent to kernel@openeuler.org has been converted to PR failed! Mailing list address: https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/X... Failed Reason: apply patch(es) failed, Patch failed at 0002 KVM: arm64: Make use of TWED feature Suggest Solution: please checkout if the failed patch(es) can work on the newest codes in expected branch