KVM: arm64: Add support for hypercall services selection
Subsystems (6)
v4 cover KVM: arm64: Add support for hypercall services selection
Hello,
Continuing the discussion from [1], the series tries to add support for the user-space to elect the hypercall services that it wishes to expose to the guest, rather than the guest discovering them unconditionally. The idea employed by the series was taken from
[1] as suggested by Marc Z.
In a broad sense, the idea is similar to the current implementation of PSCI interface- create a 'firmware psuedo-register' to handle the firmware revisions. The series extends this idea to all the other hypercalls such as TRNG (True Random Number Generator), PV_TIME (Paravirtualized Time), and PTP (Precision Time protocol).
For better categorization and future scaling, these firmware registers are categorized based on the service call owners, but unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by each one of them. It's expected that the userspace discover the features provided by each register via GET_ONE_REG, and writeback the desired values using SET_ONE_REG. KVM allows this modification only until the VM has started.
Older VMMs can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
Since these registers, including the old ones such as KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2] maintain its state per-VM. Thus accessing them via KVM_[GET|SET]_ONE_REG for every vCPU is redundant. To optimize this, the series also introduces the capability KVM_CAP_ARM_REG_SCOPE. If enabled, KVM_GET_REG_LIST will advertise the registers that are VM-scoped by dynamically modifying the register encoding. KVM_REG_ARM_SCOPE_* helper macros are introduced to decode the same. By learning this, userspace can access such registers only once.
The patches are based off of mainline kernel 5.17-rc5, with the selftest patches from [2] applied.
Patch-1 factors out the non-PSCI related interface from psci.c to hypercalls.c, as the series would extend the list in the upcoming patches.
Patch-2 introduces the KVM_CAP_ARM_REG_SCOPE capability.
Patch-3 provides helpers to encode existing registers withe the scope information. The only users in the patch would be the registers, KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2]
Patch-4 tracks if the VM has started running, which would be used in the upcoming patches.
Patch-5 sets up the framework for the bitmap firmware psuedo-registers. It includes read/write helpers for the registers, and a helper to check if a particular hypercall service is supported for the guest. It also adds the register KVM_REG_ARM_STD_HYP_BMAP to support ARM's standard secure services.
Patch-6 introduces the firmware register, KVM_REG_ARM_STD_HYP_BMAP, which holds the standard hypervisor services (such as PV_TIME).
Patch-7 introduces the firmware register, KVM_REG_ARM_VENDOR_HYP_BMAP, which holds the vendor specific hypercall services.
Patch-8,9 Add the necessary documentation for the newly added firmware registers.
Patch-10 imports the SMCCC definitions from linux/arm-smccc.h into tools/ for further use in selftests.
Patch-11,12 adds the selftest to test the guest (using 'hvc') and userspace interfaces (SET/GET_ONE_REG).
Patch-13 adds these firmware registers into the get-reg-list selftest.
[1]: https://lore.kernel.org/kvmarm/[email protected]/T/
[2]: https://lore.kernel.org/kvmarm/[email protected]/
Regards,
Raghavendra
v3 -> v4
Addressed comments and took suggestions by Reiji, Oliver, Marc, Sean and Jim:
- Renamed and moved the VM has run once check to arm64.
- Introduced the capability to dynamically modify the register encodings to include the scope information.
- Replaced mutex_lock with READ_ONCE and WRITE_ONCE when the bitmaps are accessed.
- The hypercalls selftest re-runs with KVM_CAP_ARM_REG_SCOPE enabled.
v2 -> v3
Addressed comments by Marc and Andrew:
- Dropped kvm_vcpu_has_run_once() implementation.
- Redifined kvm_vm_has_run_once() as kvm_vm_has_started() in the core KVM code that introduces a new field, 'vm_started', to track this.
- KVM_CAP_ARM_HVC_FW_REG_BMAP returns the number of psuedo-firmware bitmap registers upon a 'read'. Support for 'write' removed.
- Removed redundant spinlock, 'fw_reg_bmap_enabled' fields from the hypercall descriptor structure.
- A separate sub-struct to hold the bitmap info is removed. The bitmap info is directly stored in the hypercall descriptor structure (struct kvm_hvc_desc).
v1 -> v2
Addressed comments by Oliver (thanks!):
- Introduced kvm_vcpu_has_run_once() and kvm_vm_has_run_once() in the core kvm code, rather than relying on ARM specific vcpu->arch.has_run_once.
- Writing to KVM_REG_ARM_PSCI_VERSION is done in hypercalls.c itself, rather than separating out to psci.c.
- Introduced KVM_CAP_ARM_HVC_FW_REG_BMAP to enable the extension.
- Tracks the register accesses from VMM to decide whether to sanitize a register or not, as opposed to sanitizing upon the first 'write' in v1.
- kvm_hvc_call_supported() is implemented using a direct switch-case statement, instead of looping over all the registers to pick the register for the function-id.
- Replaced the register bit definitions with #defines, instead of enums.
- Removed the patch v1-06/08 that imports the firmware register definitions as it's not needed.
- Separated out the documentations in its own patch, and the renaming of hypercalls.rst to psci.rst into another patch.
- Add the new firmware registers to get-reg-list KVM selftest.
v1: https://lore.kernel.org/kvmarm/[email protected]/
v2: https://lore.kernel.org/kvmarm/[email protected]/
v3: https://lore.kernel.org/linux-arm-kernel/[email protected]/
Raghavendra Rao Ananta (13):
KVM: arm64: Factor out firmware register handling from psci.c
KVM: arm64: Introduce KVM_CAP_ARM_REG_SCOPE
KVM: arm64: Encode the scope for firmware registers
KVM: arm64: Capture VM's first run
KVM: arm64: Setup a framework for hypercall bitmap firmware registers
KVM: arm64: Add standard hypervisor firmware register
KVM: arm64: Add vendor hypervisor firmware register
Docs: KVM: Add doc for the bitmap firmware registers
Docs: KVM: Rename psci.rst to hypercalls.rst
tools: Import ARM SMCCC definitions
selftests: KVM: aarch64: Introduce hypercall ABI test
selftests: KVM: aarch64: hypercalls: Test with KVM_CAP_ARM_REG_SCOPE
selftests: KVM: aarch64: Add the bitmap firmware registers to
get-reg-list
Documentation/virt/kvm/api.rst | 16 + Documentation/virt/kvm/arm/hypercalls.rst | 124 +++++ Documentation/virt/kvm/arm/psci.rst | 77 --- arch/arm64/include/asm/kvm_host.h | 30 ++ arch/arm64/include/uapi/asm/kvm.h | 22 + arch/arm64/kvm/arm.c | 23 +- arch/arm64/kvm/guest.c | 82 +++- arch/arm64/kvm/hypercalls.c | 301 +++++++++++- arch/arm64/kvm/psci.c | 166 ------- include/kvm/arm_hypercalls.h | 17 + include/kvm/arm_psci.h | 7 - include/uapi/linux/kvm.h | 1 + tools/include/linux/arm-smccc.h | 188 ++++++++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/get-reg-list.c | 3 + .../selftests/kvm/aarch64/hypercalls.c | 443 ++++++++++++++++++ 17 files changed, 1243 insertions(+), 259 deletions(-) create mode 100644 Documentation/virt/kvm/arm/hypercalls.rst delete mode 100644 Documentation/virt/kvm/arm/psci.rst create mode 100644 tools/include/linux/arm-smccc.h create mode 100644 tools/testing/selftests/kvm/aarch64/hypercalls.c
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
PATCH v4 1/13 KVM: arm64: Factor out firmware register handling from psci.c
Common hypercall firmware register handing is currently employed by psci.c. Since the upcoming patches add more of these registers, it's better to move the generic handling to hypercall.c for a cleaner presentation.
While we are at it, collect all the firmware registers under fw_reg_ids[] to help implement kvm_arm_get_fw_num_regs() and kvm_arm_copy_fw_reg_indices() in a generic way.
No functional change intended.
--- arch/arm64/kvm/guest.c | 2 +- arch/arm64/kvm/hypercalls.c | 170 +++++++++++++++++++++++++++++++++++ arch/arm64/kvm/psci.c | 166 ---------------------------------- include/kvm/arm_hypercalls.h | 7 ++ include/kvm/arm_psci.h | 7 -- 5 files changed, 178 insertions(+), 174 deletions(-)
arch/arm64/kvm/guest.c +1 -1
@@ -18,7 +18,7 @@#include <linux/string.h> #include <linux/vmalloc.h> #include <linux/fs.h> -#include <kvm/arm_psci.h> +#include <kvm/arm_hypercalls.h> #include <asm/cputype.h> #include <linux/uaccess.h> #include <asm/fpsimd.h>
arch/arm64/kvm/hypercalls.c +170 -0
173 lines not on this page · raw message
arch/arm64/kvm/psci.c +0 -166
169 lines not on this page · raw message
include/kvm/arm_hypercalls.h +7 -0
@@ -40,4 +40,11 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu, vcpu_set_reg(vcpu, 3, a3); } +struct kvm_one_reg ; + +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu); +int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +#endif
include/kvm/arm_psci.h +0 -7
@@ -42,11 +42,4 @@ static inline int kvm_psci_version(struct kvm_vcpu *vcpu, struct kvm *kvm)int kvm_psci_call (struct kvm_vcpu *vcpu); -struct kvm_one_reg ; - -int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu); -int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); -int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); -int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); -#endif /* __KVM_ARM_PSCI_H__ */
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 2/13 KVM: arm64: Introduce KVM_CAP_ARM_REG_SCOPE
In reply to Raghavendra Rao AnantaKVM_[GET|SET]_ONE_REG act on per-vCPU basis. Currently certain ARM64 registers, such as KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2], are accessed via this interface even though the effect that they have are really per-VM. As a result, userspace could just waste cycles to read/write the same information for every vCPU that it spawns, only to realize that there's absolutely no change in the VM's state. The problem gets worse in proportion to the number of vCPUs created.
As a result, to avoid this redundancy, introduce the capability KVM_CAP_ARM_REG_SCOPE. If enabled, KVM_GET_REG_LIST will advertise the registers that are VM-scoped by dynamically modifying the register encoding. KVM_REG_ARM_SCOPE_* helper macros are introduced to decode the same. By learning this, userspace can access such registers only once.
--- Documentation/virt/kvm/api.rst | 16 ++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/include/uapi/asm/kvm.h | 6 ++++++ arch/arm64/kvm/arm.c | 13 +++++++------ include/uapi/linux/kvm.h | 1 + 5 files changed, 33 insertions(+), 6 deletions(-)
Documentation/virt/kvm/api.rst +16 -0
@@ -7561,3 +7561,19 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.34 KVM_CAP_ARM_REG_SCOPE +-------------------------- + +:Architectures: arm64 + +The capability, if enabled, amends the existing register encoding +with additional information to the userspace if a particular register +is scoped per-vCPU or per-VM via KVM_GET_REG_LIST. KVM provides +KVM_REG_ARM_SCOPE_* helper macros to decode the same. Userspace can +use this information from the register encoding to access a VM-scopped +regiser only once, as opposed to accessing it for every vCPU for the +same effect. + +On the other hand, if the capability is disabled, all the registers +remain vCPU-scopped by default, retaining backward compatibility.
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -136,6 +136,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Register scoping enabled for KVM registers */ +bool reg_scope_enabled; };struct kvm_vcpu_fault_info {
arch/arm64/include/uapi/asm/kvm.h +6 -0
@@ -199,6 +199,12 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_COPROC_MASK 0x000000000FFF0000#define KVM_REG_ARM_COPROC_SHIFT 16 +/* Defines if a KVM register is one per-vCPU or one per-VM */ +#define KVM_REG_ARM_SCOPE_MASK 0x0000000010000000 +#define KVM_REG_ARM_SCOPE_SHIFT 28 +#define KVM_REG_ARM_SCOPE_VCPU 0 +#define KVM_REG_ARM_SCOPE_VM 1 +/* Normal registers are mapped as coprocessor 16. */ #define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT)#define KVM_REG_ARM_CORE_REG (name) (offsetof(struct kvm_regs, name) / sizeof(__u32))
arch/arm64/kvm/arm.c +7 -6
@@ -81,26 +81,26 @@ int kvm_arch_check_processor_compat(void *opaque)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { -int r; +int r =0 ;if (cap->flags)return -EINVAL ;switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : - r =0 ; kvm->arch.return_nisv_io_abort_to_user = true;break ;case KVM_CAP_ARM_MTE :mutex_lock (&kvm->lock); -if (!system_supports_mte () || kvm->created_vcpus) { +if (!system_supports_mte () || kvm->created_vcpus) r = -EINVAL ; - }else { - r =0 ; +else kvm->arch.mte_enabled = true; - }mutex_unlock (&kvm->lock);break ; +case KVM_CAP_ARM_REG_SCOPE : +WRITE_ONCE (kvm->arch.reg_scope_enabled, true); +break ;default : r = -EINVAL ;break ;
@@ -209,6 +209,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SET_GUEST_DEBUG :case KVM_CAP_VCPU_ATTRIBUTES :case KVM_CAP_PTP_KVM : +case KVM_CAP_ARM_REG_SCOPE : r =1 ;break ;case KVM_CAP_SET_GUEST_DEBUG2 :
include/uapi/linux/kvm.h +1 -0
@@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_VM_GPA_BITS 207#define KVM_CAP_XSAVE2 208#define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_ARM_REG_SCOPE 210#ifdef KVM_CAP_IRQ_ROUTING
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 3/13 KVM: arm64: Encode the scope for firmware registers
In reply to Raghavendra Rao AnantaThe psuedo-firmware registers, KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 and KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, can be scopped as per-VM registers. Hence, during the KVM_GET_REG_LIST call, encode KVM_REG_ARM_SCOPE_VM into the registers, but during KVM_[GET|SET]_ONE_REG calls, clear the scope information such that they can be processed like before.
For future expansion, helper functions such as kvm_arm_reg_id_encode_scope() and kvm_arm_reg_id_clear_scope() are introduced.
--- arch/arm64/include/asm/kvm_host.h | 2 + arch/arm64/kvm/guest.c | 77 +++++++++++++++++++++++++++++++ arch/arm64/kvm/hypercalls.c | 31 +++++++++---- 3 files changed, 100 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -794,6 +794,8 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); (test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))int kvm_trng_call (struct kvm_vcpu *vcpu); +int kvm_arm_reg_id_encode_scope (struct kvm_vcpu *vcpu,u64 *reg_id); +void kvm_arm_reg_id_clear_scope (struct kvm_vcpu *vcpu,u64 *reg_id);#ifdef CONFIG_KVM extern phys_addr_t hyp_mem_base;extern phys_addr_t hyp_mem_size;
arch/arm64/kvm/guest.c +77 -0
83 lines not on this page · raw message
arch/arm64/kvm/hypercalls.c +21 -10
@@ -160,10 +160,17 @@ int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) { -int i; +int i, ret; +u64 reg_id;for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_ids); i++) { -if (put_user (kvm_arm_fw_reg_ids[i], uindices++)) + reg_id = kvm_arm_fw_reg_ids[i]; + + ret =kvm_arm_reg_id_encode_scope (vcpu, ®_id); +if (ret <0 ) +return ret; + +if (put_user (reg_id, uindices++))return -EFAULT ; }
@@ -214,21 +221,23 @@ static int get_kernel_wa_level(u64 regid)int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; +u64 val, reg_id = reg->id; -switch (reg->id) { +kvm_arm_reg_id_clear_scope (vcpu, ®_id); + +switch (reg_id) {case KVM_REG_ARM_PSCI_VERSION : val =kvm_psci_version (vcpu, vcpu->kvm);break ;case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 :case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : - val =get_kernel_wa_level (reg->id) &KVM_REG_FEATURE_LEVEL_MASK ; + val =get_kernel_wa_level (reg_id) &KVM_REG_FEATURE_LEVEL_MASK ;break ;default :return -ENOENT ; } -if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg->id))) +if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg_id)))return -EFAULT ;return 0 ;
@@ -237,13 +246,15 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; +u64 val, reg_id = reg->id;int wa_level;if (copy_from_user (&val, uaddr,KVM_REG_SIZE (reg->id)))return -EFAULT ; -switch (reg->id) { +kvm_arm_reg_id_clear_scope (vcpu, ®_id); + +switch (reg_id) {case KVM_REG_ARM_PSCI_VERSION : {bool wants_02;
@@ -270,7 +281,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if (val & ~KVM_REG_FEATURE_LEVEL_MASK )return -EINVAL ; -if (get_kernel_wa_level (reg->id) < val) +if (get_kernel_wa_level (reg_id) < val)return -EINVAL ;return 0 ;
@@ -306,7 +317,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)* We can deal with NOT_AVAIL on NOT_REQUIRED, but not the * other way around. */ -if (get_kernel_wa_level (reg->id) < wa_level) +if (get_kernel_wa_level (reg_id) < wa_level)return -EINVAL ;return 0 ;
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 4/13 KVM: arm64: Capture VM's first run
In reply to Raghavendra Rao AnantaCapture the first run of the KVM VM, which is basically the first KVM_RUN issued for any vCPU. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features, such as the feature bitmap exposed by the psuedo-firmware registers, after the VM has started running.
--- arch/arm64/include/asm/kvm_host.h | 9 +++++++++ arch/arm64/kvm/arm.c | 2 ++ 2 files changed, 11 insertions(+)
arch/arm64/include/asm/kvm_host.h +9 -0
@@ -139,6 +139,9 @@ struct kvm_arch {/* Register scoping enabled for KVM registers */ bool reg_scope_enabled; + +/* Capture first run of the VM */ +bool has_run_once; };struct kvm_vcpu_fault_info {
@@ -796,6 +799,12 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);int kvm_trng_call (struct kvm_vcpu *vcpu);int kvm_arm_reg_id_encode_scope (struct kvm_vcpu *vcpu,u64 *reg_id);void kvm_arm_reg_id_clear_scope (struct kvm_vcpu *vcpu,u64 *reg_id); + +static inline bool kvm_arm_vm_has_run_once (struct kvm_arch *kvm_arch) +{ +return kvm_arch->has_run_once; +} +#ifdef CONFIG_KVM extern phys_addr_t hyp_mem_base;extern phys_addr_t hyp_mem_size;
arch/arm64/kvm/arm.c +2 -0
@@ -635,6 +635,8 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu); + kvm->arch.has_run_once = true; +return ret; }
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 5/13 KVM: arm64: Setup a framework for hypercall bitmap firmware registers
In reply to Raghavendra Rao AnantaKVM regularly introduces new hypercall services to the guests without any consent from the userspace. This means, the guests can observe hypercall services in and out as they migrate across various host kernel versions. This could be a major problem if the guest discovered a hypercall, started using it, and after getting migrated to an older kernel realizes that it's no longer available. Depending on how the guest handles the change, there's a potential chance that the guest would just panic.
As a result, there's a need for the userspace to elect the services that it wishes the guest to discover. It can elect these services based on the kernels spread across its (migration) fleet. To remedy this, extend the existing firmware psuedo-registers, such as KVM_REG_ARM_PSCI_VERSION, for all the hypercall services available.
These firmware registers are categorized based on the service call owners, and unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by the corresponding registers. It's expected that the VMMs discover the features provided by each register via GET_ONE_REG, and writeback the desired values using SET_ONE_REG. KVM allows this modification only until the VM has started.
Older userspace code can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
In this patch, the framework adds the register only for ARM's standard secure services (owner value 4). Currently, this includes support only for ARM True Random Number Generator (TRNG) service, with bit-0 of the register representing mandatory features of v1.0. The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM. Other services are momentarily added in the upcoming patches.
--- arch/arm64/include/asm/kvm_host.h | 12 +++++ arch/arm64/include/uapi/asm/kvm.h | 8 ++++ arch/arm64/kvm/arm.c | 8 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 78 +++++++++++++++++++++++++++++++ include/kvm/arm_hypercalls.h | 4 ++ 6 files changed, 111 insertions(+)
arch/arm64/include/asm/kvm_host.h +12 -0
@@ -101,6 +101,15 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { }; +/** +* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor +* +* @hvc_std_bmap: Bitmap of standard secure service calls +*/ +struct kvm_hvc_desc { +u64 hvc_std_bmap; +}; +struct kvm_arch {struct kvm_s2_mmu mmu;
@@ -142,6 +151,9 @@ struct kvm_arch {/* Capture first run of the VM */ bool has_run_once; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {
arch/arm64/include/uapi/asm/kvm.h +8 -0
@@ -287,6 +287,14 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED 3#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED (1U << 4) +/* Bitmap firmware registers, extension to the existing psuedo-register space */ +#define KVM_REG_ARM_FW_BMAP KVM_REG_ARM_FW_REG(0xff00) +#define KVM_REG_ARM_FW_BMAP_REG (r) (KVM_REG_ARM_FW_BMAP | (r)) + +#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_BMAP_REG(0) +#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0) +#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)
arch/arm64/kvm/arm.c +8 -0
@@ -156,6 +156,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm); +kvm_arm_init_hypercalls (kvm);return ret; out_free_stage2_pgd:
@@ -635,7 +636,14 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu); +/* +* Grab kvm->lock such that the reader of has_run_once can finish +* the necessary operation atomically, such as deciding whether to +* block the writes to the firmware registers if the VM has run once. +*/ +mutex_lock (&kvm->lock); kvm->arch.has_run_once = true; +mutex_unlock (&kvm->lock);return ret; }
arch/arm64/kvm/guest.c +1 -0
@@ -65,6 +65,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +KVM_REG_ARM_STD_BMAP , };/**
arch/arm64/kvm/hypercalls.c +78 -0
128 lines not on this page · raw message
include/kvm/arm_hypercalls.h +4 -0
@@ -6,6 +6,9 @@#include <asm/kvm_emulate.h> +#define ARM_SMCCC_STD_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)
@@ -42,6 +45,7 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu,struct kvm_one_reg ; +void kvm_arm_init_hypercalls (struct kvm *kvm);int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu);int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg);
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 6/13 KVM: arm64: Add standard hypervisor firmware register
In reply to Raghavendra Rao AnantaIntroduce the firmware register to hold the standard hypervisor service calls (owner value 5) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Paravirtualized time, represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 20 +++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 29 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -105,9 +105,11 @@ struct kvm_arch_memory_slot {* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor * * @hvc_std_bmap: Bitmap of standard secure service calls +* @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap; +u64 hvc_std_hyp_bmap; };struct kvm_arch {
arch/arm64/include/uapi/asm/kvm.h +4 -0
@@ -295,6 +295,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0)#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_STD_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(1) +#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0) +#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)
arch/arm64/kvm/guest.c +1 -0
@@ -66,6 +66,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP , + KVM_REG_ARM_STD_HYP_BMAP, };/**
arch/arm64/kvm/hypercalls.c +19 -1
@@ -75,6 +75,10 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_TRNG_RND64 :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap,KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +case ARM_SMCCC_HV_PV_TIME_FEATURES : +case ARM_SMCCC_HV_PV_TIME_ST : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap, +KVM_REG_ARM_STD_HYP_BIT_PV_TIME );default :/* By default, allow the services that aren't listed here */ return true;
@@ -83,6 +87,7 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;u32 func_id =smccc_get_function (vcpu);u64 val[4 ] = {SMCCC_RET_NOT_SUPPORTED };u32 feature;
@@ -134,7 +139,10 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) }break ;case ARM_SMCCC_HV_PV_TIME_FEATURES : - val[0 ] =SMCCC_RET_SUCCESS ; +if (kvm_arm_fw_reg_feat_enabled ( + hvc_desc->hvc_std_hyp_bmap, +KVM_REG_ARM_STD_HYP_BIT_PV_TIME )) + val[0 ] =SMCCC_RET_SUCCESS ;break ; }break ;
@@ -179,6 +187,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP , + KVM_REG_ARM_STD_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)
@@ -186,6 +195,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm)struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; + hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)
@@ -272,6 +282,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_BMAP : val =READ_ONCE (hvc_desc->hvc_std_bmap);break ; +case KVM_REG_ARM_STD_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap); +break ;default :return -ENOENT ; }
@@ -294,6 +307,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_bmap; fw_reg_features =ARM_SMCCC_STD_FEATURES ;break ; +case KVM_REG_ARM_STD_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; + fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ; +break ;default :return -ENOENT ; }
@@ -398,6 +415,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP : +case KVM_REG_ARM_STD_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;
include/kvm/arm_hypercalls.h +3 -0
@@ -9,6 +9,9 @@#define ARM_SMCCC_STD_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_STD_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 7/13 KVM: arm64: Add vendor hypervisor firmware register
In reply to Raghavendra Rao AnantaIntroduce the firmware register to hold the vendor specific hypervisor service calls (owner value 6) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Precision Time Protocol (PTP), represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 22 +++++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -106,10 +106,12 @@ struct kvm_arch_memory_slot {* * @hvc_std_bmap: Bitmap of standard secure service calls * @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls +* @hvc_vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap;u64 hvc_std_hyp_bmap; +u64 hvc_vendor_hyp_bmap; };struct kvm_arch {
arch/arm64/include/uapi/asm/kvm.h +4 -0
@@ -299,6 +299,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0)#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_VENDOR_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(2) +#define KVM_REG_ARM_VENDOR_HYP_BIT_PTP BIT(0) +#define KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)
arch/arm64/kvm/guest.c +1 -0
@@ -67,6 +67,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };/**
arch/arm64/kvm/hypercalls.c +21 -1
@@ -79,6 +79,9 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_HV_PV_TIME_ST :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap,KVM_REG_ARM_STD_HYP_BIT_PV_TIME ); +case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_vendor_hyp_bmap, +KVM_REG_ARM_VENDOR_HYP_BIT_PTP );default :/* By default, allow the services that aren't listed here */ return true;
@@ -162,7 +165,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID : val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); - val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_PTP ); + +/* +* The feature bits exposed to user-space doesn't include +* ARM_SMCCC_KVM_FUNC_FEATURES. However, we expose this to +* the guest as bit-0. Hence, left-shift the user-space +* exposed bitmap by 1 to accommodate this. +*/ + val[0 ] |= hvc_desc->hvc_vendor_hyp_bmap <<1 ;break ;case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID :kvm_ptp_get_time (vcpu, val);
@@ -188,6 +198,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)
@@ -196,6 +207,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm) hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; + hvc_desc->hvc_vendor_hyp_bmap =ARM_SMCCC_VENDOR_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)
@@ -285,6 +297,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_HYP_BMAP : val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap);break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_vendor_hyp_bmap); +break ;default :return -ENOENT ; }
@@ -311,6 +326,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ;break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_vendor_hyp_bmap; + fw_reg_features =ARM_SMCCC_VENDOR_HYP_FEATURES ; +break ;default :return -ENOENT ; }
@@ -416,6 +435,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP :case KVM_REG_ARM_STD_HYP_BMAP : +case KVM_REG_ARM_VENDOR_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;
include/kvm/arm_hypercalls.h +3 -0
@@ -12,6 +12,9 @@#define ARM_SMCCC_STD_HYP_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_VENDOR_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 8/13 Docs: KVM: Add doc for the bitmap firmware registers
In reply to Raghavendra Rao AnantaAdd the documentation for the bitmap firmware registers in psci.rst. This includes the details for KVM_REG_ARM_STD_BMAP, KVM_REG_ARM_STD_HYP_BMAP, and KVM_REG_ARM_VENDOR_HYP_BMAP registers.
Since the document is growing to carry other hypercall related information, make necessary adjustments to present the document in a generic sense, rather than being PSCI focused.
--- Documentation/virt/kvm/arm/psci.rst | 83 ++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 18 deletions(-)
Documentation/virt/kvm/arm/psci.rst +65 -18
@@ -1,32 +1,32 @@ .. SPDX-License-Identifier: GPL-2.0 -========================================= -Power State Coordination Interface (PSCI) -========================================= +======================= +ARM Hypercall Interface +======================= -KVM implements the PSCI (Power State Coordination Interface) -specification in order to provide services such as CPU on/off, reset -and power-off to the guest. +KVM handles the hypercall services as requested by the guests. New hypercall +services are regularly made available by the ARM specification or by KVM (as +vendor services) if they make sense from a virtualization point of view. -The PSCI specification is regularly updated to provide new features, -and KVM implements these updates if they make sense from a virtualization -point of view. - -This means that a guest booted on two different versions of KVM can -observe two different "firmware" revisions. This could cause issues if -a given guest is tied to a particular PSCI revision (unlikely), or if -a migration causes a different PSCI version to be exposed out of the -blue to an unsuspecting guest. +This means that a guest booted on two different versions of KVM can observe +two different "firmware" revisions. This could cause issues if a given guest +is tied to a particular version of a hypercall service, or if a migration +causes a different version to be exposed out of the blue to an unsuspecting +guest. In order to remedy this situation, KVM exposes a set of "firmware pseudo-registers" that can be manipulated using the GET/SET_ONE_REG interface. These registers can be saved/restored by userspace, and set -to a convenient value if required. +to a convenient value as required. -The following register is defined: +The following registers are defined: * KVM_REG_ARM_PSCI_VERSION: + KVM implements the PSCI (Power State Coordination Interface) + specification in order to provide services such as CPU on/off, reset + and power-off to the guest. + - Only valid if the vcpu has the KVM_ARM_VCPU_PSCI_0_2 feature set (and thus has already been initialized) - Returns the current PSCI version on GET_ONE_REG (defaulting to the
@@ -74,4 +74,51 @@ The following register is defined: KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED: The workaround is always active on this vCPU or it is not needed. -.. [1] https://developer.arm.com/-/media/developer/pdf/ARM_DEN_0070A_Firmware_interfaces_for_mitigating_CVE-2017-5715.pdf +Contrary to the above registers, the following registers exposes the hypercall +services in the form of a feature-bitmap. This bitmap is translated to the +services that are exposed to the guest. There is a register defined per service +call owner and can be accessed via GET/SET_ONE_REG interface. + +By default, these registers are set with the upper limit of the features that +are supported. User-space can discover this configuration via GET_ONE_REG. If +unsatisfied, the user-space can write back the desired bitmap back via +SET_ONE_REG. The features for the registers that are untouched, probably because +userspace isn't aware of them, will be exposed as is to the guest. + +The psuedo-firmware bitmap register are as follows: + +* KVM_REG_ARM_STD_BMAP: + Controls the bitmap of the ARM Standard Secure Service Calls. + + The following bits are accepted: + + KVM_REG_ARM_STD_BIT_TRNG_V1_0: + The bit represents the services offered under v1.0 of ARM True Random + Number Generator (TRNG) specification, ARM DEN0098. + +* KVM_REG_ARM_STD_HYP_BMAP: + Controls the bitmap of the ARM Standard Hypervisor Service Calls. + + The following bits are accepted: + + KVM_REG_ARM_STD_HYP_BIT_PV_TIME: + The bit represents the Paravirtualized Time service as represented by + ARM DEN0057A. + +* KVM_REG_ARM_VENDOR_HYP_BMAP: + Controls the bitmap of the Vendor specific Hypervisor Service Calls. + + The following bits are accepted: + + KVM_REG_ARM_VENDOR_HYP_BIT_PTP: + The bit represents the Precision Time Protocol KVM service. + +Errors: + + ======= ============================================================= + -ENOENT Unknown register accessed. + -EBUSY Attempt a 'write' to the register after the VM has started. + -EINVAL Invalid bitmap written to the register. + ======= ============================================================= + +.. [1] https://developer.arm.com/-/media/developer/pdf/ARM_DEN_0070A_Firmware_interfaces_for_mitigating_CVE-2017-5715.pdf
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 9/13 Docs: KVM: Rename psci.rst to hypercalls.rst
In reply to Raghavendra Rao AnantaSince the doc now covers more of general hypercalls' details, rather than just PSCI, rename the file to a more appropriate name- hypercalls.rst.
---
Documentation/virt/kvm/arm/{psci.rst => hypercalls.rst} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename Documentation/virt/kvm/arm/{psci.rst => hypercalls.rst} (100%)Documentation/virt/kvm/arm/hypercalls.rst renamed from Documentation/virt/kvm/arm/psci.rst +0 -0
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 10/13 tools: Import ARM SMCCC definitions
In reply to Raghavendra Rao AnantaImport the standard SMCCC definitions from include/linux/arm-smccc.h.
--- tools/include/linux/arm-smccc.h | 188 ++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 tools/include/linux/arm-smccc.h
tools/include/linux/arm-smccc.h new file +188 -0
@@ -0,0 +1,188 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (c) 2015, Linaro Limited +*/ +#ifndef __LINUX_ARM_SMCCC_H +#define __LINUX_ARM_SMCCC_H + +#include <linux/const.h> + +/* +* This file provides common defines for ARM SMC Calling Convention as +* specified in +* https://developer.arm.com/docs/den0028/latest +* +* This code is up-to-date with version DEN 0028 C +*/ + +#define ARM_SMCCC_STD_CALL _AC(0,U) +#define ARM_SMCCC_FAST_CALL _AC(1,U) +#define ARM_SMCCC_TYPE_SHIFT 31 + +#define ARM_SMCCC_SMC_32 0 +#define ARM_SMCCC_SMC_64 1 +#define ARM_SMCCC_CALL_CONV_SHIFT 30 + +#define ARM_SMCCC_OWNER_MASK 0x3F +#define ARM_SMCCC_OWNER_SHIFT 24 + +#define ARM_SMCCC_FUNC_MASK 0xFFFF + +#define ARM_SMCCC_IS_FAST_CALL (smc_val) \ + ((smc_val) & (ARM_SMCCC_FAST_CALL << ARM_SMCCC_TYPE_SHIFT)) +#define ARM_SMCCC_IS_64 (smc_val) \ + ((smc_val) & (ARM_SMCCC_SMC_64 << ARM_SMCCC_CALL_CONV_SHIFT)) +#define ARM_SMCCC_FUNC_NUM (smc_val) ((smc_val) & ARM_SMCCC_FUNC_MASK) +#define ARM_SMCCC_OWNER_NUM (smc_val) \ + (((smc_val) >> ARM_SMCCC_OWNER_SHIFT) & ARM_SMCCC_OWNER_MASK) + +#define ARM_SMCCC_CALL_VAL (type, calling_convention, owner, func_num) \ + (((type) << ARM_SMCCC_TYPE_SHIFT) | \ + ((calling_convention) << ARM_SMCCC_CALL_CONV_SHIFT) | \ + (((owner) & ARM_SMCCC_OWNER_MASK) << ARM_SMCCC_OWNER_SHIFT) | \ + ((func_num) & ARM_SMCCC_FUNC_MASK)) + +#define ARM_SMCCC_OWNER_ARCH 0 +#define ARM_SMCCC_OWNER_CPU 1 +#define ARM_SMCCC_OWNER_SIP 2 +#define ARM_SMCCC_OWNER_OEM 3 +#define ARM_SMCCC_OWNER_STANDARD 4 +#define ARM_SMCCC_OWNER_STANDARD_HYP 5 +#define ARM_SMCCC_OWNER_VENDOR_HYP 6 +#define ARM_SMCCC_OWNER_TRUSTED_APP 48 +#define ARM_SMCCC_OWNER_TRUSTED_APP_END 49 +#define ARM_SMCCC_OWNER_TRUSTED_OS 50 +#define ARM_SMCCC_OWNER_TRUSTED_OS_END 63 + +#define ARM_SMCCC_FUNC_QUERY_CALL_UID 0xff01 + +#define ARM_SMCCC_QUIRK_NONE 0 +#define ARM_SMCCC_QUIRK_QCOM_A6 1/* Save/restore register a6 */ + +#define ARM_SMCCC_VERSION_1_0 0x10000 +#define ARM_SMCCC_VERSION_1_1 0x10001 +#define ARM_SMCCC_VERSION_1_2 0x10002 +#define ARM_SMCCC_VERSION_1_3 0x10003 + +#define ARM_SMCCC_1_3_SVE_HINT 0x10000 + +#define ARM_SMCCC_VERSION_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 0) + +#define ARM_SMCCC_ARCH_FEATURES_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 1) + +#define ARM_SMCCC_ARCH_SOC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 2) + +#define ARM_SMCCC_ARCH_WORKAROUND_1 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 0x8000) + +#define ARM_SMCCC_ARCH_WORKAROUND_2 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 0x7fff) + +#define ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_FUNC_QUERY_CALL_UID) + +/* KVM UID value: 28b46fb6-2ec5-11e9-a9ca-4b564d003a74 */ +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 0xb66fb428U +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 0xe911c52eU +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 0x564bcaa9U +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3 0x743a004dU + +/* KVM "vendor specific" services */ +#define ARM_SMCCC_KVM_FUNC_FEATURES 0 +#define ARM_SMCCC_KVM_FUNC_PTP 1 +#define ARM_SMCCC_KVM_FUNC_FEATURES_2 127 +#define ARM_SMCCC_KVM_NUM_FUNCS 128 + +#define ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_KVM_FUNC_FEATURES) + +#define SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED 1 + +/* +* ptp_kvm is a feature used for time sync between vm and host. +* ptp_kvm module in guest kernel will get service from host using +* this hypercall ID. +*/ +#define ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_KVM_FUNC_PTP) + +/* ptp_kvm counter type ID */ +#define KVM_PTP_VIRT_COUNTER 0 +#define KVM_PTP_PHYS_COUNTER 1 + +/* Paravirtualised time calls (defined by ARM DEN0057A) */ +#define ARM_SMCCC_HV_PV_TIME_FEATURES \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x20) + +#define ARM_SMCCC_HV_PV_TIME_ST \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x21) + +/* TRNG entropy source calls (defined by ARM DEN0098) */ +#define ARM_SMCCC_TRNG_VERSION \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x50) + +#define ARM_SMCCC_TRNG_FEATURES \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x51) + +#define ARM_SMCCC_TRNG_GET_UUID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x52) + +#define ARM_SMCCC_TRNG_RND32 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x53) + +#define ARM_SMCCC_TRNG_RND64 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x53) + +/* +* Return codes defined in ARM DEN 0070A +* ARM DEN 0070A is now merged/consolidated into ARM DEN 0028 C +*/ +#define SMCCC_RET_SUCCESS 0 +#define SMCCC_RET_NOT_SUPPORTED -1 +#define SMCCC_RET_NOT_REQUIRED -2 +#define SMCCC_RET_INVALID_PARAMETER -3 + +#endif /*__LINUX_ARM_SMCCC_H*/
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 11/13 selftests: KVM: aarch64: Introduce hypercall ABI test
In reply to Raghavendra Rao AnantaIntroduce a KVM selftest to check the hypercall interface for arm64 platforms. The test validates the user-space's IOCTL interface to read/write the psuedo-firmware registers as well as its effects on the guest upon certain configurations.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/hypercalls.c | 376 ++++++++++++++++++ 3 files changed, 378 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/hypercalls.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -2,6 +2,7 @@ /aarch64/arch_timer /aarch64/debug-exceptions /aarch64/get-reg-list +/aarch64/hypercalls /aarch64/psci_test /aarch64/vgic_init /aarch64/vgic_irq
tools/testing/selftests/kvm/Makefile +1 -0
@@ -102,6 +102,7 @@ TEST_GEN_PROGS_x86_64 += system_counter_offset_test TEST_GEN_PROGS_aarch64 += aarch64/arch_timer TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list +TEST_GEN_PROGS_aarch64 += aarch64/hypercalls TEST_GEN_PROGS_aarch64 += aarch64/psci_test TEST_GEN_PROGS_aarch64 += aarch64/vgic_init TEST_GEN_PROGS_aarch64 += aarch64/vgic_irq
tools/testing/selftests/kvm/aarch64/hypercalls.c new file +376 -0
376 lines not on this page · raw message
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 12/13 selftests: KVM: aarch64: hypercalls: Test with KVM_CAP_ARM_REG_SCOPE
In reply to Raghavendra Rao AnantaUpgrade the aarch64/hypercalls test to also consider the firmware register's scope information. Thus, run the test with the capability KVM_CAP_ARM_REG_SCOPE disabled and enabled.
--- .../selftests/kvm/aarch64/hypercalls.c | 83 +++++++++++++++++-- 1 file changed, 75 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/aarch64/hypercalls.c +75 -8
@@ -29,7 +29,7 @@ struct kvm_fw_reg_info { .max_feat_bit = bit_max, \ } -static const struct kvm_fw_reg_info fw_reg_info[] = { +static struct kvm_fw_reg_info fw_reg_info[] = {FW_REG_INFO (KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_BMAP_BIT_MAX ),FW_REG_INFO (KVM_REG_ARM_STD_HYP_BMAP ,KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX ),FW_REG_INFO (KVM_REG_ARM_VENDOR_HYP_BMAP ,KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX ),
@@ -294,19 +294,78 @@ static void test_fw_regs_after_vm_start(struct kvm_vm *vm) } } -static struct kvm_vm *test_vm_create (void ) +static bool test_reg_is_bmap_fw_reg (uint64_t reg) +{ +if ((reg &KVM_REG_ARM_COPROC_MASK ) ==KVM_REG_ARM_FW && + (reg &0xffff ) >=0xff00 )/* Bitmap firmware register space */ +return true; + +return false; +} + +static void test_fw_regs_encode_vm_scope (void ) +{ +int i; + +/* +* Encode the scope as KVM_REG_ARM_SCOPE_VM for further use +* in KVM_SET_ONE_REG and KVM_GET_ONE_REG operations. +*/ +for (i =0 ; i <ARRAY_SIZE (fw_reg_info); i++) + fw_reg_info[i].reg |= (KVM_REG_ARM_SCOPE_VM <<KVM_REG_ARM_SCOPE_SHIFT ); +} + +static void test_validate_fw_regs (struct kvm_vm *vm,int scope) +{ +uint64_t i, reg; +int obtained_scope; +struct kvm_reg_list *reg_list; + + reg_list =vcpu_get_reg_list (vm,0 ); + +for (i =0 ; i < reg_list->n; i++) { + reg = reg_list->reg[i]; +if (!test_reg_is_bmap_fw_reg (reg)) +continue ; + +/* +* Depending on KVM_CAP_ARM_REG_SCOPE, currently all the firmware +* bitmap registers are either completely VM-scoped or vCPU scoped. +*/ + obtained_scope = (reg &KVM_REG_ARM_SCOPE_MASK ) >>KVM_REG_ARM_SCOPE_SHIFT ; +TEST_ASSERT (obtained_scope == scope, +"Incorrect scope detected for reg: %lx. Expected: %d; Obtained: %d\n" , + reg, scope, obtained_scope); + } +} + +static struct kvm_vm *test_vm_create (int scope) {struct kvm_vm *vm; +struct kvm_enable_cap reg_scope_cap = { + .cap =KVM_CAP_ARM_REG_SCOPE , + }; + +if (scope ==KVM_REG_ARM_SCOPE_VM && !kvm_check_cap (KVM_CAP_ARM_REG_SCOPE )) { +print_skip ("Capability KVM_CAP_ARM_REG_SCOPE unavailable\n" ); +return NULL ; + } vm =vm_create_default (0 ,0 , guest_code);ucall_init (vm,NULL );steal_time_init (vm); +if (scope ==KVM_REG_ARM_SCOPE_VM ) { +vm_enable_cap (vm, ®_scope_cap); +test_fw_regs_encode_vm_scope (); + } +return vm; } -static struct kvm_vm *test_guest_stage (struct kvm_vm *vm) +static struct kvm_vm * +test_guest_stage (struct kvm_vm *vm,int scope) {struct kvm_vm *ret_vm = vm;
@@ -319,7 +378,7 @@ static struct kvm_vm *test_guest_stage(struct kvm_vm *vm)case TEST_STAGE_HVC_IFACE_FEAT_DISABLED :/* Start a new VM so that all the features are now enabled by default */ kvm_vm_free (vm); - ret_vm =test_vm_create (); + ret_vm =test_vm_create (scope);break ;case TEST_STAGE_HVC_IFACE_FEAT_ENABLED :case TEST_STAGE_HVC_IFACE_FALSE_INFO :
@@ -334,14 +393,20 @@ static struct kvm_vm *test_guest_stage(struct kvm_vm *vm)return ret_vm; } -static void test_run (void ) +static void test_run (int scope) {struct kvm_vm *vm;struct ucall uc;bool guest_done = false; - vm =test_vm_create (); + vm =test_vm_create (scope); +if (!vm) +return ; + stage =TEST_STAGE_REG_IFACE ; +sync_global_to_guest (vm, stage); + +test_validate_fw_regs (vm, scope);test_fw_regs_before_vm_start (vm);while (!guest_done) {
@@ -349,7 +414,7 @@ static void test_run(void)switch (get_ucall (vm,0 , &uc)) {case UCALL_SYNC : - vm =test_guest_stage (vm); + vm =test_guest_stage (vm, scope);break ;case UCALL_DONE : guest_done = true;
@@ -371,6 +436,8 @@ int main(void) {setbuf (stdout,NULL ); -test_run (); +test_run (KVM_REG_ARM_SCOPE_VCPU ); +test_run (KVM_REG_ARM_SCOPE_VM ); +return 0 ; }
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 13/13 selftests: KVM: aarch64: Add the bitmap firmware registers to get-reg-list
In reply to Raghavendra Rao AnantaAdd the psuedo-firmware registers KVM_REG_ARM_STD_BMAP, KVM_REG_ARM_STD_HYP_BMAP, and KVM_REG_ARM_VENDOR_HYP_BMAP to the base_regs[] list.
--- tools/testing/selftests/kvm/aarch64/get-reg-list.c | 3 +++ 1 file changed, 3 insertions(+)
tools/testing/selftests/kvm/aarch64/get-reg-list.c +3 -0
@@ -686,6 +686,9 @@ static __u64 base_regs[] = {KVM_REG_ARM_FW_REG (0 ),KVM_REG_ARM_FW_REG (1 ),KVM_REG_ARM_FW_REG (2 ), +KVM_REG_ARM_FW_BMAP_REG (0 ),/* KVM_REG_ARM_STD_BMAP */ +KVM_REG_ARM_FW_BMAP_REG (1 ),/* KVM_REG_ARM_STD_HYP_BMAP */ +KVM_REG_ARM_FW_BMAP_REG (2 ),/* KVM_REG_ARM_VENDOR_HYP_BMAP */ ARM64_SYS_REG (3 ,3 ,14 ,3 ,1 ),/* CNTV_CTL_EL0 */ ARM64_SYS_REG (3 ,3 ,14 ,3 ,2 ),/* CNTV_CVAL_EL0 */ ARM64_SYS_REG(3, 3, 14, 0, 2),
Signature · 6 lines
2.35.1.473.g83b2b277ed-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 02/13] KVM: arm64: Introduce KVM_CAP_ARM_REG_SCOPE
In reply to Raghavendra Rao AnantaQuoted 37 lines · Raghavendra Rao Ananta
KVM_[GET|SET]_ONE_REG act on per-vCPU basis. Currently certain ARM64 registers, such as KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2], are accessed via this interface even though the effect that they have are really per-VM. As a result, userspace could just waste cycles to read/write the same information for every vCPU that it spawns, only to realize that there's absolutely no change in the VM's state. The problem gets worse in proportion to the number of vCPUs created.
As a result, to avoid this redundancy, introduce the capability
KVM_CAP_ARM_REG_SCOPE. If enabled,KVM_GET_REG_LISTwill advertise the registers that are VM-scoped by dynamically modifying the register encoding. KVM_REG_ARM_SCOPE_* helper macros are introduced to decode the same. By learning this, userspace can access such registers only once.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- Documentation/virt/kvm/api.rst | 16 ++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/include/uapi/asm/kvm.h | 6 ++++++ arch/arm64/kvm/arm.c | 13 +++++++------ include/uapi/linux/kvm.h | 1 + 5 files changed, 33 insertions(+), 6 deletions(-)Documentation/virt/kvm/api.rst +4
-0@@ -7561,3 +7561,19 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.34 KVM_CAP_ARM_REG_SCOPE +-------------------------- +
Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7577
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+:Architectures: arm64 + +The capability, if enabled, amends the existing register encoding +with additional information to the userspace if a particular register +is scoped per-vCPU or per-VM via KVM_GET_REG_LIST. KVM provides +KVM_REG_ARM_SCOPE_* helper macros to decode the same. Userspace can +use this information from the register encoding to access a VM-scopped +regiser only once, as opposed to accessing it for every vCPU for the +same effect. +
Could you describe the encoding changes in 4.68 'KVM_SET_ONE_REG', along with the other ARM encoding details?
Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7579
Quoted 2 lines
Documentation/virt/kvm/api.rst +2
-0+On the other hand, if the capability is disabled, all the registers +remain vCPU-scopped by default, retaining backward compatibility.
typo: vCPU-scoped
That said, I don't believe we need to document behavior if the CAP is disabled, as the implicated ioctls should continue to work the same.
Quoted 19 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -136,6 +136,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Register scoping enabled for KVM registers */ +bool reg_scope_enabled; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 204
Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0@@ -199,6 +199,12 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_COPROC_MASK 0x000000000FFF0000#define KVM_REG_ARM_COPROC_SHIFT 16 +/* Defines if a KVM register is one per-vCPU or one per-VM */ +#define KVM_REG_ARM_SCOPE_MASK 0x0000000010000000 +#define KVM_REG_ARM_SCOPE_SHIFT 28
Thinking about the advertisement of VM- and vCPU-scoped registers, this could be generally useful. Might it make sense to add such an encoding to the arch-generic register definitions?
If that is the case, we may want to snap up a few more bits (a nybble) for future expansion.
Quoted 27 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_SCOPE_VCPU 0 +#define KVM_REG_ARM_SCOPE_VM 1 +/* Normal registers are mapped as coprocessor 16. */ #define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT)#define KVM_REG_ARM_CORE_REG (name) (offsetof(struct kvm_regs, name) / sizeof(__u32))arch/arm64/kvm/arm.c +1
-2@@ -81,26 +81,26 @@ int kvm_arch_check_processor_compat(void *opaque)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { -int r; +int r =0 ;if (cap->flags)return -EINVAL ;switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : - r =0 ; kvm->arch.return_nisv_io_abort_to_user = true;break ;case KVM_CAP_ARM_MTE :mutex_lock (&kvm->lock);
arch/arm64/kvm/arm.c int kvm_arch_check_processor_compat(void *opaque) · line 100
Quoted 10 lines
arch/arm64/kvm/arm.c +2
-4-if (!system_supports_mte () || kvm->created_vcpus) { +if (!system_supports_mte () || kvm->created_vcpus) r = -EINVAL ; - }else { - r =0 ; +else kvm->arch.mte_enabled = true; - }mutex_unlock (&kvm->lock);break ;
Hmm.. these all look like cleanups. If you want to propose these, could you do it in a separate patch?
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 212
Quoted 11 lines
arch/arm64/kvm/arm.c +4
-0+case KVM_CAP_ARM_REG_SCOPE : +WRITE_ONCE (kvm->arch.reg_scope_enabled, true); +break ;default : r = -EINVAL ;break ;@@ -209,6 +209,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SET_GUEST_DEBUG :case KVM_CAP_VCPU_ATTRIBUTES :case KVM_CAP_PTP_KVM : +case KVM_CAP_ARM_REG_SCOPE :
It is a bit odd to advertise a capability (and allow userspace to enable it), despite the fact that the feature itself hasn't yet been implemented.
Is it possible to fold the feature in to the patch that exposes it to userspace? Otherwise, you could punt advertisement of the CAP until it is actually implemented in kernel.
--
Oliver
Re: [PATCH v4 02/13] KVM: arm64: Introduce KVM_CAP_ARM_REG_SCOPE
Hey Oliver,
Quoted 53 lines · Oliver Upton
Quoted 37 lines · Raghavendra Rao Ananta
KVM_[GET|SET]_ONE_REG act on per-vCPU basis. Currently certain ARM64 registers, such as KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2], are accessed via this interface even though the effect that they have are really per-VM. As a result, userspace could just waste cycles to read/write the same information for every vCPU that it spawns, only to realize that there's absolutely no change in the VM's state. The problem gets worse in proportion to the number of vCPUs created.
As a result, to avoid this redundancy, introduce the capability
KVM_CAP_ARM_REG_SCOPE. If enabled,KVM_GET_REG_LISTwill advertise the registers that are VM-scoped by dynamically modifying the register encoding. KVM_REG_ARM_SCOPE_* helper macros are introduced to decode the same. By learning this, userspace can access such registers only once.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- Documentation/virt/kvm/api.rst | 16 ++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/include/uapi/asm/kvm.h | 6 ++++++ arch/arm64/kvm/arm.c | 13 +++++++------ include/uapi/linux/kvm.h | 1 + 5 files changed, 33 insertions(+), 6 deletions(-)Documentation/virt/kvm/api.rst +4
-0@@ -7561,3 +7561,19 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.34 KVM_CAP_ARM_REG_SCOPE +-------------------------- +Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7577
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+:Architectures: arm64 + +The capability, if enabled, amends the existing register encoding +with additional information to the userspace if a particular register +is scoped per-vCPU or per-VM via KVM_GET_REG_LIST. KVM provides +KVM_REG_ARM_SCOPE_* helper macros to decode the same. Userspace can +use this information from the register encoding to access a VM-scopped +regiser only once, as opposed to accessing it for every vCPU for the +same effect. +Could you describe the encoding changes in 4.68 '
KVM_SET_ONE_REG', along with the other ARM encoding details?
Quoted 8 lines
Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7579
Quoted 2 lines
Documentation/virt/kvm/api.rst +2
-0+On the other hand, if the capability is disabled, all the registers +remain vCPU-scopped by default, retaining backward compatibility.typo: vCPU-scoped
That said, I don't believe we need to document behavior if the CAP is disabled, as the implicated ioctls should continue to work the same.
Sure, I'll address the above two Doc comments.
Quoted 30 lines
Quoted 19 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -136,6 +136,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Register scoping enabled for KVM registers */ +bool reg_scope_enabled; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 204
Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0@@ -199,6 +199,12 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_COPROC_MASK 0x000000000FFF0000#define KVM_REG_ARM_COPROC_SHIFT 16 +/* Defines if a KVM register is one per-vCPU or one per-VM */ +#define KVM_REG_ARM_SCOPE_MASK 0x0000000010000000 +#define KVM_REG_ARM_SCOPE_SHIFT 28Thinking about the advertisement of VM- and vCPU-scoped registers, this could be generally useful. Might it make sense to add such an encoding to the arch-generic register definitions?
Quoted 3 lines
If that is the case, we may want to snap up a few more bits (a nybble) for future expansion.
That's a great idea! But I wonder if we'll get a push-back since there are no users of it in other arch(s) yet. Not sure if there was any need/discussion regarding the same, but I'm happy to share a patch for the same if you sense that there's a strong potential for the patch.
Quoted 32 lines
Quoted 32 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_SCOPE_VCPU 0 +#define KVM_REG_ARM_SCOPE_VM 1 +/* Normal registers are mapped as coprocessor 16. */ #define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT)#define KVM_REG_ARM_CORE_REG (name) (offsetof(struct kvm_regs, name) / sizeof(__u32))arch/arm64/kvm/arm.c +2
-5@@ -81,26 +81,26 @@ int kvm_arch_check_processor_compat(void *opaque)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { -int r; +int r =0 ;if (cap->flags)return -EINVAL ;switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : - r =0 ; kvm->arch.return_nisv_io_abort_to_user = true;break ;case KVM_CAP_ARM_MTE :mutex_lock (&kvm->lock); -if (!system_supports_mte () || kvm->created_vcpus) { +if (!system_supports_mte () || kvm->created_vcpus) r = -EINVAL ; - }else { - r =0 ;
Quoted 9 lines
arch/arm64/kvm/arm.c int kvm_arch_check_processor_compat(void *opaque) · line 100
Quoted 5 lines
arch/arm64/kvm/arm.c +1
-1+else kvm->arch.mte_enabled = true; - }mutex_unlock (&kvm->lock);break ;Hmm.. these all look like cleanups. If you want to propose these, could you do it in a separate patch?
Ahh, I thought I could squeeze it in. But sure, I can separate it out.
Quoted 16 lines
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 212
Quoted 11 lines
arch/arm64/kvm/arm.c +4
-0+case KVM_CAP_ARM_REG_SCOPE : +WRITE_ONCE (kvm->arch.reg_scope_enabled, true); +break ;default : r = -EINVAL ;break ;@@ -209,6 +209,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SET_GUEST_DEBUG :case KVM_CAP_VCPU_ATTRIBUTES :case KVM_CAP_PTP_KVM : +case KVM_CAP_ARM_REG_SCOPE :It is a bit odd to advertise a capability (and allow userspace to enable it), despite the fact that the feature itself hasn't yet been implemented.
Quoted 4 lines
Is it possible to fold the feature in to the patch that exposes it to userspace? Otherwise, you could punt advertisement of the CAP until it is actually implemented in kernel.
Well, I didn't want to complicate the patch, but technically the feature is available with this patch, including all the CAP and macro definitions. Userspace can still decode the scope information, only that no registers are added yet, which is done in the next patch. So, the userspace can still remain the same between this and the next patch.
Thank you.
Raghavendra
Quoted 3 lines
--
Oliver
Re: [PATCH v4 02/13] KVM: arm64: Introduce KVM_CAP_ARM_REG_SCOPE
Quoted 95 lines · Raghavendra Rao Ananta
Hey Oliver,
Quoted 53 lines · Oliver Upton
Quoted 37 lines · Raghavendra Rao Ananta
KVM_[GET|SET]_ONE_REG act on per-vCPU basis. Currently certain ARM64 registers, such as KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2], are accessed via this interface even though the effect that they have are really per-VM. As a result, userspace could just waste cycles to read/write the same information for every vCPU that it spawns, only to realize that there's absolutely no change in the VM's state. The problem gets worse in proportion to the number of vCPUs created.
As a result, to avoid this redundancy, introduce the capability
KVM_CAP_ARM_REG_SCOPE. If enabled,KVM_GET_REG_LISTwill advertise the registers that are VM-scoped by dynamically modifying the register encoding. KVM_REG_ARM_SCOPE_* helper macros are introduced to decode the same. By learning this, userspace can access such registers only once.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- Documentation/virt/kvm/api.rst | 16 ++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/include/uapi/asm/kvm.h | 6 ++++++ arch/arm64/kvm/arm.c | 13 +++++++------ include/uapi/linux/kvm.h | 1 + 5 files changed, 33 insertions(+), 6 deletions(-)Documentation/virt/kvm/api.rst +4
-0@@ -7561,3 +7561,19 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.34 KVM_CAP_ARM_REG_SCOPE +-------------------------- +Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7577
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+:Architectures: arm64 + +The capability, if enabled, amends the existing register encoding +with additional information to the userspace if a particular register +is scoped per-vCPU or per-VM via KVM_GET_REG_LIST. KVM provides +KVM_REG_ARM_SCOPE_* helper macros to decode the same. Userspace can +use this information from the register encoding to access a VM-scopped +regiser only once, as opposed to accessing it for every vCPU for the +same effect. +Could you describe the encoding changes in 4.68 '
KVM_SET_ONE_REG', along with the other ARM encoding details?Quoted 8 lines
Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7579
Quoted 2 lines
Documentation/virt/kvm/api.rst +2
-0+On the other hand, if the capability is disabled, all the registers +remain vCPU-scopped by default, retaining backward compatibility.typo: vCPU-scoped
That said, I don't believe we need to document behavior if the CAP is disabled, as the implicated ioctls should continue to work the same.
Sure, I'll address the above two Doc comments.
Quoted 30 lines
Quoted 19 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -136,6 +136,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Register scoping enabled for KVM registers */ +bool reg_scope_enabled; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 204
Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0@@ -199,6 +199,12 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_COPROC_MASK 0x000000000FFF0000#define KVM_REG_ARM_COPROC_SHIFT 16 +/* Defines if a KVM register is one per-vCPU or one per-VM */ +#define KVM_REG_ARM_SCOPE_MASK 0x0000000010000000 +#define KVM_REG_ARM_SCOPE_SHIFT 28Thinking about the advertisement of VM- and vCPU-scoped registers, this could be generally useful. Might it make sense to add such an encoding to the arch-generic register definitions?
Quoted 8 lines
Quoted 3 lines
If that is the case, we may want to snap up a few more bits (a nybble) for future expansion.
That's a great idea! But I wonder if we'll get a push-back since there are no users of it in other arch(s) yet. Not sure if there was any need/discussion regarding the same, but I'm happy to share a patch for the same if you sense that there's a strong potential for the patch.
I'm unsure if this is actually of interest to other architectures, it just doesn't seem ARM-specific so we should probably raise the question so we only grab these bits once.
Quoted 62 lines
Quoted 32 lines
Quoted 32 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_SCOPE_VCPU 0 +#define KVM_REG_ARM_SCOPE_VM 1 +/* Normal registers are mapped as coprocessor 16. */ #define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT)#define KVM_REG_ARM_CORE_REG (name) (offsetof(struct kvm_regs, name) / sizeof(__u32))arch/arm64/kvm/arm.c +2
-5@@ -81,26 +81,26 @@ int kvm_arch_check_processor_compat(void *opaque)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { -int r; +int r =0 ;if (cap->flags)return -EINVAL ;switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : - r =0 ; kvm->arch.return_nisv_io_abort_to_user = true;break ;case KVM_CAP_ARM_MTE :mutex_lock (&kvm->lock); -if (!system_supports_mte () || kvm->created_vcpus) { +if (!system_supports_mte () || kvm->created_vcpus) r = -EINVAL ; - }else { - r =0 ;Quoted 9 lines
arch/arm64/kvm/arm.c int kvm_arch_check_processor_compat(void *opaque) · line 100
Quoted 5 lines
arch/arm64/kvm/arm.c +1
-1+else kvm->arch.mte_enabled = true; - }mutex_unlock (&kvm->lock);break ;Hmm.. these all look like cleanups. If you want to propose these, could you do it in a separate patch?
Ahh, I thought I could squeeze it in. But sure, I can separate it out.
Quoted 16 lines
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 212
Quoted 11 lines
arch/arm64/kvm/arm.c +4
-0+case KVM_CAP_ARM_REG_SCOPE : +WRITE_ONCE (kvm->arch.reg_scope_enabled, true); +break ;default : r = -EINVAL ;break ;@@ -209,6 +209,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SET_GUEST_DEBUG :case KVM_CAP_VCPU_ATTRIBUTES :case KVM_CAP_PTP_KVM : +case KVM_CAP_ARM_REG_SCOPE :It is a bit odd to advertise a capability (and allow userspace to enable it), despite the fact that the feature itself hasn't yet been implemented.
Quoted 4 lines
Is it possible to fold the feature in to the patch that exposes it to userspace? Otherwise, you could punt advertisement of the CAP until it is actually implemented in kernel.
Quoted 6 lines
Well, I didn't want to complicate the patch, but technically the feature is available with this patch, including all the CAP and macro definitions. Userspace can still decode the scope information, only that no registers are added yet, which is done in the next patch. So, the userspace can still remain the same between this and the next patch.
But the series isn't cleanly bisectable. There will exist commits in history that report KVM_CAP_ARM_REG_SCOPE as implemented even though that is not actually the case. You should really only advertise support to userspace when the feature is implemented.
Defining kvm->arch.reg_scope_enabled can be done earlier so you have a bit to test and guard all of the new code, and only expose the CAP in the last patch of the series.
Also, as an FYI Marc has a patch that I'll be picking up in my own series which uses bits instead of bools to keep track of certain VM-wide features:
Marc, if neither of our series land in 5.18 could you at least submit this patch in preparation? Should keep conflicts minimal that way.
Thanks!
--
Oliver
Re: [PATCH v4 02/13] KVM: arm64: Introduce KVM_CAP_ARM_REG_SCOPE
Quoted 106 lines · Oliver Upton
Quoted 95 lines · Raghavendra Rao Ananta
Hey Oliver,
Quoted 53 lines · Oliver Upton
Quoted 37 lines · Raghavendra Rao Ananta
KVM_[GET|SET]_ONE_REG act on per-vCPU basis. Currently certain ARM64 registers, such as KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2], are accessed via this interface even though the effect that they have are really per-VM. As a result, userspace could just waste cycles to read/write the same information for every vCPU that it spawns, only to realize that there's absolutely no change in the VM's state. The problem gets worse in proportion to the number of vCPUs created.
As a result, to avoid this redundancy, introduce the capability
KVM_CAP_ARM_REG_SCOPE. If enabled,KVM_GET_REG_LISTwill advertise the registers that are VM-scoped by dynamically modifying the register encoding. KVM_REG_ARM_SCOPE_* helper macros are introduced to decode the same. By learning this, userspace can access such registers only once.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- Documentation/virt/kvm/api.rst | 16 ++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/include/uapi/asm/kvm.h | 6 ++++++ arch/arm64/kvm/arm.c | 13 +++++++------ include/uapi/linux/kvm.h | 1 + 5 files changed, 33 insertions(+), 6 deletions(-)Documentation/virt/kvm/api.rst +4
-0@@ -7561,3 +7561,19 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.34 KVM_CAP_ARM_REG_SCOPE +-------------------------- +Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7577
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+:Architectures: arm64 + +The capability, if enabled, amends the existing register encoding +with additional information to the userspace if a particular register +is scoped per-vCPU or per-VM via KVM_GET_REG_LIST. KVM provides +KVM_REG_ARM_SCOPE_* helper macros to decode the same. Userspace can +use this information from the register encoding to access a VM-scopped +regiser only once, as opposed to accessing it for every vCPU for the +same effect. +Could you describe the encoding changes in 4.68 '
KVM_SET_ONE_REG', along with the other ARM encoding details?Quoted 8 lines
Documentation/virt/kvm/api.rst The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset · line 7579
Quoted 2 lines
Documentation/virt/kvm/api.rst +2
-0+On the other hand, if the capability is disabled, all the registers +remain vCPU-scopped by default, retaining backward compatibility.typo: vCPU-scoped
That said, I don't believe we need to document behavior if the CAP is disabled, as the implicated ioctls should continue to work the same.
Sure, I'll address the above two Doc comments.
Quoted 30 lines
Quoted 19 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -136,6 +136,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Register scoping enabled for KVM registers */ +bool reg_scope_enabled; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 204
Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0@@ -199,6 +199,12 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_COPROC_MASK 0x000000000FFF0000#define KVM_REG_ARM_COPROC_SHIFT 16 +/* Defines if a KVM register is one per-vCPU or one per-VM */ +#define KVM_REG_ARM_SCOPE_MASK 0x0000000010000000 +#define KVM_REG_ARM_SCOPE_SHIFT 28Thinking about the advertisement of VM- and vCPU-scoped registers, this could be generally useful. Might it make sense to add such an encoding to the arch-generic register definitions?
Quoted 8 lines
Quoted 3 lines
If that is the case, we may want to snap up a few more bits (a nybble) for future expansion.
That's a great idea! But I wonder if we'll get a push-back since there are no users of it in other arch(s) yet. Not sure if there was any need/discussion regarding the same, but I'm happy to share a patch for the same if you sense that there's a strong potential for the patch.
Quoted 4 lines
I'm unsure if this is actually of interest to other architectures, it just doesn't seem ARM-specific so we should probably raise the question so we only grab these bits once.
I've CC'ed a few more arch-specific kvm lists for comments/concerns/suggestions on the idea (feel free to add any other relevant groups/persons). Based on the response, I can start an independent RFC series for the same.
Quoted 69 lines
Quoted 62 lines
Quoted 32 lines
Quoted 32 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_SCOPE_VCPU 0 +#define KVM_REG_ARM_SCOPE_VM 1 +/* Normal registers are mapped as coprocessor 16. */ #define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT)#define KVM_REG_ARM_CORE_REG (name) (offsetof(struct kvm_regs, name) / sizeof(__u32))arch/arm64/kvm/arm.c +2
-5@@ -81,26 +81,26 @@ int kvm_arch_check_processor_compat(void *opaque)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { -int r; +int r =0 ;if (cap->flags)return -EINVAL ;switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : - r =0 ; kvm->arch.return_nisv_io_abort_to_user = true;break ;case KVM_CAP_ARM_MTE :mutex_lock (&kvm->lock); -if (!system_supports_mte () || kvm->created_vcpus) { +if (!system_supports_mte () || kvm->created_vcpus) r = -EINVAL ; - }else { - r =0 ;Quoted 9 lines
arch/arm64/kvm/arm.c int kvm_arch_check_processor_compat(void *opaque) · line 100
Quoted 5 lines
arch/arm64/kvm/arm.c +1
-1+else kvm->arch.mte_enabled = true; - }mutex_unlock (&kvm->lock);break ;Hmm.. these all look like cleanups. If you want to propose these, could you do it in a separate patch?
Ahh, I thought I could squeeze it in. But sure, I can separate it out.
Quoted 16 lines
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 212
Quoted 11 lines
arch/arm64/kvm/arm.c +4
-0+case KVM_CAP_ARM_REG_SCOPE : +WRITE_ONCE (kvm->arch.reg_scope_enabled, true); +break ;default : r = -EINVAL ;break ;@@ -209,6 +209,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SET_GUEST_DEBUG :case KVM_CAP_VCPU_ATTRIBUTES :case KVM_CAP_PTP_KVM : +case KVM_CAP_ARM_REG_SCOPE :It is a bit odd to advertise a capability (and allow userspace to enable it), despite the fact that the feature itself hasn't yet been implemented.
Quoted 4 lines
Is it possible to fold the feature in to the patch that exposes it to userspace? Otherwise, you could punt advertisement of the CAP until it is actually implemented in kernel.
Quoted 6 lines
Well, I didn't want to complicate the patch, but technically the feature is available with this patch, including all the CAP and macro definitions. Userspace can still decode the scope information, only that no registers are added yet, which is done in the next patch. So, the userspace can still remain the same between this and the next patch.
Quoted 9 lines
But the series isn't cleanly bisectable. There will exist commits in history that report
KVM_CAP_ARM_REG_SCOPEas implemented even though that is not actually the case. You should really only advertise support to userspace when the feature is implemented.Defining
kvm->arch.reg_scope_enabledcan be done earlier so you have a bit to test and guard all of the new code, and only expose the CAP in the last patch of the series.
Got it. I'll arrange that in the next spin.
Quoted 6 lines
Also, as an FYI Marc has a patch that I'll be picking up in my own series which uses bits instead of bools to keep track of certain VM-wide features:
Thanks. This is great. I can steal a couple of bits and implement the flags introduced in the series here.
Quoted 7 lines
Marc, if neither of our series land in 5.18 could you at least submit this patch in preparation? Should keep conflicts minimal that way.
Thanks!
--
Oliver
Thank you.
Raghavendra
Re: [PATCH v4 04/13] KVM: arm64: Capture VM's first run
In reply to Raghavendra Rao AnantaHi Raghavendra,
Quoted 8 lines · Raghavendra Rao Ananta
Capture the first run of the KVM VM, which is basically the first
KVM_RUNissued for any vCPU. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features, such as the feature bitmap exposed by the psuedo-firmware registers, after the VM has started running.Signed-off-by Raghavendra Rao Ananta <[email protected]>
I believe this patch is superseded by commit:
5177fe91e4cf ("KVM: arm64: Do not change the PMU event filter after a VCPU has run")
on kvmarm/next.
--
Thanks,
Oliver
Re: [PATCH v4 01/13] KVM: arm64: Factor out firmware register handling from psci.c
In reply to Raghavendra Rao AnantaHi Raghu,
Quoted 5 lines · Raghavendra Rao Ananta
Common hypercall firmware register handing is currently employed by
psci.c. Since the upcoming patches add more of these registers, it's better to move the generic handling tohypercall.cfor a cleaner presentation.
Quoted 7 lines
While we are at it, collect all the firmware registers under fw_reg_ids[] to help implement
kvm_arm_get_fw_num_regs()andkvm_arm_copy_fw_reg_indices()in a generic way.No functional change intended.
Signed-off-by Raghavendra Rao Ananta <[email protected]>
You might owe a rebase, thanks to the new Spectre bits KVM picked up (in Linus' tree) and PSCIv1.1 (kvmarm/next) :-)
Besides the nits:
Quoted 45 lines
--- arch/arm64/kvm/guest.c | 2 +- arch/arm64/kvm/hypercalls.c | 170 +++++++++++++++++++++++++++++++++++ arch/arm64/kvm/psci.c | 166 ---------------------------------- include/kvm/arm_hypercalls.h | 7 ++ include/kvm/arm_psci.h | 7 -- 5 files changed, 178 insertions(+), 174 deletions(-)arch/arm64/kvm/guest.c +1
-1@@ -18,7 +18,7 @@#include <linux/string.h> #include <linux/vmalloc.h> #include <linux/fs.h> -#include <kvm/arm_psci.h> +#include <kvm/arm_hypercalls.h> #include <asm/cputype.h> #include <linux/uaccess.h> #include <asm/fpsimd.h> arch/arm64/kvm/hypercalls.c +16
-0@@ -146,3 +146,173 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)smccc_set_retval (vcpu, val[0 ], val[1 ], val[2 ], val[3 ]);return 1 ; } + +static const u64 kvm_arm_fw_reg_ids[] = { +KVM_REG_ARM_PSCI_VERSION , +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 , +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +}; + +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) +{ +return ARRAY_SIZE (kvm_arm_fw_reg_ids); +} + +int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) +{ +int i; +
arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 174
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_ids); i++) { +if (put_user (kvm_arm_fw_reg_ids[i], uindices++)) +return -EFAULT ; + } + +return 0 ; +} + +#define KVM_REG_FEATURE_LEVEL_WIDTH 4 +#define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)
Hrm... Not your code but this should really use GENMASK() to be consistent with the rest of the kernel. Also, looks like _WIDTH is useless.
Perhaps fold these together:
#define KVM_REG_FEATURE_LEVEL_MASK GENMASK(3, 0)
Also these kind of macros probably belong in headers.
Quoted 353 lines
arch/arm64/kvm/hypercalls.c +144
-0+ +/* +* Convert the workaround level into an easy-to-compare number, where higher +* values mean better protection. +*/ +static int get_kernel_wa_level (u64 regid) +{ +switch (regid) { +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : +switch (arm64_get_spectre_v2_state ()) { +case SPECTRE_VULNERABLE : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; +case SPECTRE_MITIGATED : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL ; +case SPECTRE_UNAFFECTED : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED ; + } +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : +switch (arm64_get_spectre_v4_state ()) { +case SPECTRE_MITIGATED : +/* +* As for the hypercall discovery, we pretend we +* don't have any FW mitigation if SSBS is there at +* all times. +*/ +if (cpus_have_final_cap (ARM64_SSBS )) +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; + fallthrough; +case SPECTRE_UNAFFECTED : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; +case SPECTRE_VULNERABLE : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; + } + } + +return -EINVAL ; +} + +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) +{ +void __user *uaddr = (void __user *)(long )reg->addr; +u64 val; + +switch (reg->id) { +case KVM_REG_ARM_PSCI_VERSION : + val =kvm_psci_version (vcpu, vcpu->kvm); +break ; +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : + val =get_kernel_wa_level (reg->id) &KVM_REG_FEATURE_LEVEL_MASK ; +break ; +default : +return -ENOENT ; + } + +if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + +return 0 ; +} + +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) +{ +void __user *uaddr = (void __user *)(long )reg->addr; +u64 val; +int wa_level; + +if (copy_from_user (&val, uaddr,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + +switch (reg->id) { +case KVM_REG_ARM_PSCI_VERSION : + { +bool wants_02; + + wants_02 =test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features); + +switch (val) { +case KVM_ARM_PSCI_0_1 : +if (wants_02) +return -EINVAL ; + vcpu->kvm->arch.psci_version = val; +return 0 ; +case KVM_ARM_PSCI_0_2 : +case KVM_ARM_PSCI_1_0 : +if (!wants_02) +return -EINVAL ; + vcpu->kvm->arch.psci_version = val; +return 0 ; + } +break ; + } + +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : +if (val & ~KVM_REG_FEATURE_LEVEL_MASK ) +return -EINVAL ; + +if (get_kernel_wa_level (reg->id) < val) +return -EINVAL ; + +return 0 ; + +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : +if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED )) +return -EINVAL ; + +/* The enabled bit must not be set unless the level is AVAIL. */ +if ((val &KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED ) && + (val &KVM_REG_FEATURE_LEVEL_MASK ) !=KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL ) +return -EINVAL ; + +/* +* Map all the possible incoming states to the only two we +* really want to deal with. +*/ +switch (val &KVM_REG_FEATURE_LEVEL_MASK ) { +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL : +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN : + wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; +break ; +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL : +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED : + wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; +break ; +default : +return -EINVAL ; + } + +/* +* We can deal with NOT_AVAIL on NOT_REQUIRED, but not the +* other way around. +*/ +if (get_kernel_wa_level (reg->id) < wa_level) +return -EINVAL ; + +return 0 ; +default : +return -ENOENT ; + } + +return -EINVAL ; +}arch/arm64/kvm/psci.c +0
-166@@ -403,169 +403,3 @@ int kvm_psci_call(struct kvm_vcpu *vcpu)return -EINVAL ; }; } - -int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) -{ -return 3 ;/* PSCI version and two workaround registers */ -} - -int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) -{ -if (put_user (KVM_REG_ARM_PSCI_VERSION , uindices++)) -return -EFAULT ; - -if (put_user (KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 , uindices++)) -return -EFAULT ; - -if (put_user (KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , uindices++)) -return -EFAULT ; - -return 0 ; -} - -#define KVM_REG_FEATURE_LEVEL_WIDTH 4 -#define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1) - -/* -* Convert the workaround level into an easy-to-compare number, where higher -* values mean better protection. -*/ -static int get_kernel_wa_level (u64 regid) -{ -switch (regid) { -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : -switch (arm64_get_spectre_v2_state ()) { -case SPECTRE_VULNERABLE : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; -case SPECTRE_MITIGATED : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL ; -case SPECTRE_UNAFFECTED : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED ; - } -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : -switch (arm64_get_spectre_v4_state ()) { -case SPECTRE_MITIGATED : -/* -* As for the hypercall discovery, we pretend we -* don't have any FW mitigation if SSBS is there at -* all times. -*/ -if (cpus_have_final_cap (ARM64_SSBS )) -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; - fallthrough; -case SPECTRE_UNAFFECTED : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; -case SPECTRE_VULNERABLE : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; - } - } - -return -EINVAL ; -} - -int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) -{ -void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; - -switch (reg->id) { -case KVM_REG_ARM_PSCI_VERSION : - val =kvm_psci_version (vcpu, vcpu->kvm); -break ; -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : - val =get_kernel_wa_level (reg->id) &KVM_REG_FEATURE_LEVEL_MASK ; -break ; -default : -return -ENOENT ; - } - -if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg->id))) -return -EFAULT ; - -return 0 ; -} - -int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) -{ -void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; -int wa_level; - -if (copy_from_user (&val, uaddr,KVM_REG_SIZE (reg->id))) -return -EFAULT ; - -switch (reg->id) { -case KVM_REG_ARM_PSCI_VERSION : - { -bool wants_02; - - wants_02 =test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features); - -switch (val) { -case KVM_ARM_PSCI_0_1 : -if (wants_02) -return -EINVAL ; - vcpu->kvm->arch.psci_version = val; -return 0 ; -case KVM_ARM_PSCI_0_2 : -case KVM_ARM_PSCI_1_0 : -if (!wants_02) -return -EINVAL ; - vcpu->kvm->arch.psci_version = val; -return 0 ; - } -break ; - } - -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : -if (val & ~KVM_REG_FEATURE_LEVEL_MASK ) -return -EINVAL ; - -if (get_kernel_wa_level (reg->id) < val) -return -EINVAL ; - -return 0 ; - -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : -if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | -KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED )) -return -EINVAL ; - -/* The enabled bit must not be set unless the level is AVAIL. */ -if ((val &KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED ) && - (val &KVM_REG_FEATURE_LEVEL_MASK ) !=KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL ) -return -EINVAL ; - -/* -* Map all the possible incoming states to the only two we -* really want to deal with. -*/ -switch (val &KVM_REG_FEATURE_LEVEL_MASK ) { -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL : -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN : - wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; -break ; -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL : -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED : - wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; -break ; -default : -return -EINVAL ; - } - -/* -* We can deal with NOT_AVAIL on NOT_REQUIRED, but not the -* other way around. -*/ -if (get_kernel_wa_level (reg->id) < wa_level) -return -EINVAL ; - -return 0 ; -default : -return -ENOENT ; - } - -return -EINVAL ; -}include/kvm/arm_hypercalls.h +7
-0@@ -40,4 +40,11 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu, vcpu_set_reg(vcpu, 3, a3); } +struct kvm_one_reg ; + +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu); +int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +#endif include/kvm/arm_psci.h +0
-7@@ -42,11 +42,4 @@ static inline int kvm_psci_version(struct kvm_vcpu *vcpu, struct kvm *kvm)int kvm_psci_call (struct kvm_vcpu *vcpu); -struct kvm_one_reg ; - -int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu); -int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); -int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); -int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); -#endif /* __KVM_ARM_PSCI_H__ */ Signature · 1 line
2.35.1.473.g83b2b277ed-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 03/13] KVM: arm64: Encode the scope for firmware registers
In reply to Raghavendra Rao AnantaHi Raghu,
Quoted 36 lines · Raghavendra Rao Ananta
The psuedo-firmware registers,
KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1andKVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, can be scopped as per-VM registers. Hence, during theKVM_GET_REG_LISTcall, encodeKVM_REG_ARM_SCOPE_VMinto the registers, but during KVM_[GET|SET]_ONE_REG calls, clear the scope information such that they can be processed like before.For future expansion, helper functions such as
kvm_arm_reg_id_encode_scope()andkvm_arm_reg_id_clear_scope()are introduced.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 + arch/arm64/kvm/guest.c | 77 +++++++++++++++++++++++++++++++ arch/arm64/kvm/hypercalls.c | 31 +++++++++---- 3 files changed, 100 insertions(+), 10 deletions(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -794,6 +794,8 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); (test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))int kvm_trng_call (struct kvm_vcpu *vcpu); +int kvm_arm_reg_id_encode_scope (struct kvm_vcpu *vcpu,u64 *reg_id); +void kvm_arm_reg_id_clear_scope (struct kvm_vcpu *vcpu,u64 *reg_id);#ifdef CONFIG_KVM extern phys_addr_t hyp_mem_base;extern phys_addr_t hyp_mem_size;arch/arm64/kvm/guest.c +0
-0
arch/arm64/kvm/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 68
Quoted 8 lines
arch/arm64/kvm/guest.c +5
-0@@ -61,6 +61,83 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { sizeof(kvm_vcpu_stats_desc), }; +/* Registers that are VM scopped */ +static const u64 kvm_arm_vm_scope_fw_regs[] = { +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 , +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +};
Is there a reason that KVM_REG_ARM_PSCI_VERSION is omitted? I know that we have KVM_ARM_VCPU_PSCI_0_2, but we really don't allow for asymmetry in the PSCI implementation between vCPUs. Logically, this is a system-wide property. That's a complete wreck if KVM_ARM_VCPU_PSCI_0_2 isn't required to be set/clear for all vCPUs in a VM.
Quoted 55 lines
arch/arm64/kvm/guest.c +55
-0+ +/** +* kvm_arm_reg_id_encode_scope - Encode the KVM_REG_ARM_SCOPE info into the +* register-id +* @vcpu: The vcpu pointer +* @reg_id: Pointer to the register +* +* The function adds the register's scoping information into its encoding. +* If it's explicitly marked as a per-VM register, it's encoded with +* KVM_REG_ARM_SCOPE_VM. Else, it's marked as KVM_REG_ARM_SCOPE_VCPU, which +* is also the default if KVM_CAP_ARM_REG_SCOPE is disabled. +* +* For any error cases, the function returns an error code, else it returns +* the integer value of the encoding. +*/ +int kvm_arm_reg_id_encode_scope (struct kvm_vcpu *vcpu,u64 *reg_id) +{ +const u64 *vm_scope_reg_arr; +unsigned int arr_size, idx; + +if (!READ_ONCE (vcpu->kvm->arch.reg_scope_enabled)) +return KVM_REG_ARM_SCOPE_VCPU ; + +if (!reg_id) +return -EINVAL ; + +switch (*reg_id &KVM_REG_ARM_COPROC_MASK ) { +case KVM_REG_ARM_FW : + vm_scope_reg_arr = kvm_arm_vm_scope_fw_regs; + arr_size =ARRAY_SIZE (kvm_arm_vm_scope_fw_regs); +break ; +default : +/* All the other register classes are currently +* treated as per-vCPU registers. +*/ +return KVM_REG_ARM_SCOPE_VCPU ; + } + +/* By default, all the registers encodings are scoped as vCPU. +* Modify the scope only if a register is marked as per-VM. +*/ +for (idx =0 ; idx < arr_size; idx++) { +if (vm_scope_reg_arr[idx] == *reg_id) { + *reg_id |= +KVM_REG_ARM_SCOPE_VM <<KVM_REG_ARM_SCOPE_SHIFT ; +return KVM_REG_ARM_SCOPE_VM ; + } + } + +return KVM_REG_ARM_SCOPE_VCPU ; +} + +/** +* kvm_arm_reg_id_clear_scope - Clear the KVM_REG_ARM_SCOPE info from the +* register-id
arch/arm64/kvm/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 133
Quoted 10 lines
arch/arm64/kvm/guest.c +10
-0+* @vcpu: The vcpu pointer +* @reg_id: Pointer to the register +* +* The function clears the register's scoping information, which ultimately +* is the raw encoding of the register. Note that the result is same as that +* of re-encoding the register as KVM_REG_ARM_SCOPE_VCPU. +* The function can be helpful to the existing code that uses the original +* register encoding to operate on the register. +*/ +void kvm_arm_reg_id_clear_scope (struct kvm_vcpu *vcpu,u64 *reg_id)
How about just taking the reg_id by value and returning it? You could probably just bundle this up in a macro
arch/arm64/kvm/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 135
Quoted 2 lines
arch/arm64/kvm/guest.c +2
-0+{ +if (!READ_ONCE (vcpu->kvm->arch.reg_scope_enabled) || !reg_id)
Shouldn't masking these bits work regardless? They must be zero without the CAP.
arch/arm64/kvm/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 138
Quoted 4 lines
arch/arm64/kvm/guest.c +3
-0+return ; + + *reg_id &= ~(1 <<KVM_REG_ARM_SCOPE_SHIFT );
define a _MASK for KVM_REG_ARM_SCOPE and just use that.
Quoted 14 lines
arch/arm64/kvm/guest.c +2
-0+} +static bool core_reg_offset_is_vreg (u64 off) {arch/arm64/kvm/guest.c +0
-0return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&arch/arm64/kvm/hypercalls.c +0
-1@@ -160,10 +160,17 @@ int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) { -int i;
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu) · line 171
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +7
-1+int i, ret; +u64 reg_id;for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_ids); i++) { -if (put_user (kvm_arm_fw_reg_ids[i], uindices++)) + reg_id = kvm_arm_fw_reg_ids[i]; + + ret =kvm_arm_reg_id_encode_scope (vcpu, ®_id); +if (ret <0 ) +return ret;
You could also store the register IDs with the scope already encoded and simply mask those bits off if the CAP is disabled. That way, its immediately obvious what the scope of any given register is just by looking at the register table.
Quoted 71 lines
arch/arm64/kvm/hypercalls.c +13
-8+if (put_user (reg_id, uindices++))return -EFAULT ; }@@ -214,21 +221,23 @@ static int get_kernel_wa_level(u64 regid)int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; +u64 val, reg_id = reg->id; -switch (reg->id) { +kvm_arm_reg_id_clear_scope (vcpu, ®_id); + +switch (reg_id) {case KVM_REG_ARM_PSCI_VERSION : val =kvm_psci_version (vcpu, vcpu->kvm);break ;case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 :case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : - val =get_kernel_wa_level (reg->id) &KVM_REG_FEATURE_LEVEL_MASK ; + val =get_kernel_wa_level (reg_id) &KVM_REG_FEATURE_LEVEL_MASK ;break ;default :return -ENOENT ; } -if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg->id))) +if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg_id)))return -EFAULT ;return 0 ;@@ -237,13 +246,15 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; +u64 val, reg_id = reg->id;int wa_level;if (copy_from_user (&val, uaddr,KVM_REG_SIZE (reg->id)))return -EFAULT ; -switch (reg->id) { +kvm_arm_reg_id_clear_scope (vcpu, ®_id); + +switch (reg_id) {case KVM_REG_ARM_PSCI_VERSION : {bool wants_02;@@ -270,7 +281,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if (val & ~KVM_REG_FEATURE_LEVEL_MASK )return -EINVAL ; -if (get_kernel_wa_level (reg->id) < val) +if (get_kernel_wa_level (reg_id) < val)return -EINVAL ;return 0 ;@@ -306,7 +317,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)* We can deal with NOT_AVAIL on NOT_REQUIRED, but not the * other way around. */ -if (get_kernel_wa_level (reg->id) < wa_level) +if (get_kernel_wa_level (reg_id) < wa_level)return -EINVAL ;return 0 ;Signature · 1 line
2.35.1.473.g83b2b277ed-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 05/13] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
In reply to Raghavendra Rao AnantaQuoted 51 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the userspace. This means, the guests can observe hypercall services in and out as they migrate across various host kernel versions. This could be a major problem if the guest discovered a hypercall, started using it, and after getting migrated to an older kernel realizes that it's no longer available. Depending on how the guest handles the change, there's a potential chance that the guest would just panic.
As a result, there's a need for the userspace to elect the services that it wishes the guest to discover. It can elect these services based on the kernels spread across its (migration) fleet. To remedy this, extend the existing firmware psuedo-registers, such as
KVM_REG_ARM_PSCI_VERSION, for all the hypercall services available.These firmware registers are categorized based on the service call owners, and unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by the corresponding registers. It's expected that the VMMs discover the features provided by each register via
GET_ONE_REG, and writeback the desired values usingSET_ONE_REG. KVM allows this modification only until the VM has started.Older userspace code can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
In this patch, the framework adds the register only for ARM's standard secure services (owner value 4). Currently, this includes support only for ARM True Random Number Generator (TRNG) service, with bit-0 of the register representing mandatory features of v1.0. The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM. Other services are momentarily added in the upcoming patches.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 12 +++++ arch/arm64/include/uapi/asm/kvm.h | 8 ++++ arch/arm64/kvm/arm.c | 8 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 78 +++++++++++++++++++++++++++++++ include/kvm/arm_hypercalls.h | 4 ++ 6 files changed, 111 insertions(+)arch/arm64/include/asm/kvm_host.h +0
-0
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 109
Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6
-0@@ -101,6 +101,15 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { }; +/** +* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor +* +* @hvc_std_bmap: Bitmap of standard secure service calls +*/ +struct kvm_hvc_desc {
nit: maybe call this structure kvm_hypercall_features? When nested comes along guests will need to use the SVC conduit as HVC traps are always taken to EL2. Same will need to be true for virtual EL2.
Quoted 21 lines
arch/arm64/include/asm/kvm_host.h +6
-0+u64 hvc_std_bmap; +}; +struct kvm_arch {struct kvm_s2_mmu mmu;@@ -142,6 +151,9 @@ struct kvm_arch {/* Capture first run of the VM */ bool has_run_once; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 291
Quoted 5 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0@@ -287,6 +287,14 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED 3#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED (1U << 4) +/* Bitmap firmware registers, extension to the existing psuedo-register space */ +#define KVM_REG_ARM_FW_BMAP KVM_REG_ARM_FW_REG(0xff00)
What is the motivation for moving the bitmap register indices so far away from the rest of the firmware regs?
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 292
Quoted 1 line
arch/arm64/include/uapi/asm/kvm.h +1
-0+#define KVM_REG_ARM_FW_BMAP_REG (r) (KVM_REG_ARM_FW_BMAP | (r))
If you are still going to use the index offset, just pass 'r' through to the other macro:
#define KVM_REG_ARM_FW_BMAP_REG(r) KVM_REG_ARM_FW_REG(0xff00 + r)
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 296
Quoted 3 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_BMAP_REG(0) +#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0) +#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */
Implementation details such as this probably shouldn't live in UAPI headers. We'll likely need to bump the value in the future.
Quoted 18 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0+/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/arm.c +1
-0@@ -156,6 +156,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm); +kvm_arm_init_hypercalls (kvm);return ret; out_free_stage2_pgd:@@ -635,7 +636,14 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))
arch/arm64/kvm/arm.c int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) · line 646
Quoted 10 lines
arch/arm64/kvm/arm.c +7
-0kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu); +/* +* Grab kvm->lock such that the reader of has_run_once can finish +* the necessary operation atomically, such as deciding whether to +* block the writes to the firmware registers if the VM has run once. +*/ +mutex_lock (&kvm->lock); kvm->arch.has_run_once = true; +mutex_unlock (&kvm->lock);
Shouldn't this have just grabbed the kvm lock in patch 04/13?
Quoted 32 lines
arch/arm64/kvm/arm.c +0
-0return ret; }arch/arm64/kvm/guest.c +1
-0@@ -65,6 +65,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +KVM_REG_ARM_STD_BMAP , };/** arch/arm64/kvm/hypercalls.c +10
-0@@ -58,6 +58,29 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) val[3 ] =lower_32_bits (cycles); } +static bool kvm_arm_fw_reg_feat_enabled (u64 reg_bmap,u64 feat_bit) +{ +return reg_bmap & feat_bit; +} + +static bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id) +{ +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc; + +switch (func_id) {
arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 80
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+case ARM_SMCCC_TRNG_VERSION : +case ARM_SMCCC_TRNG_FEATURES : +case ARM_SMCCC_TRNG_GET_UUID : +case ARM_SMCCC_TRNG_RND32 : +case ARM_SMCCC_TRNG_RND64 : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap, +KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +default : +/* By default, allow the services that aren't listed here */ +return true;
I think your default case should really return false. It keeps people honest when they add new patches to set up a new hypercall bit (no bit? no call!)
That of course requires that you only return false once all of the preexisting hypercalls are enumerated, otherwise such a patch would cause a regression in isolation.
Quoted 60 lines
arch/arm64/kvm/hypercalls.c +19
-0+ } +} +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) {u32 func_id =smccc_get_function (vcpu);@@ -65,6 +88,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)u32 feature;gpa_t gpa; +if (!kvm_hvc_call_supported (vcpu, func_id)) + goto out; +switch (func_id) {case ARM_SMCCC_VERSION_FUNC_ID : val[0 ] =ARM_SMCCC_VERSION_1_1 ;@@ -143,6 +169,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)return kvm_psci_call (vcpu); } +out:smccc_set_retval (vcpu, val[0 ], val[1 ], val[2 ], val[3 ]);return 1 ; }@@ -151,8 +178,16 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_PSCI_VERSION ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , + KVM_REG_ARM_STD_BMAP, }; +void kvm_arm_init_hypercalls (struct kvm *kvm) +{ +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; + + hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) {return ARRAY_SIZE (kvm_arm_fw_reg_ids);@@ -220,6 +255,7 @@ static int get_kernel_wa_level(u64 regid)int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;void __user *uaddr = (void __user *)(long )reg->addr;u64 val, reg_id = reg->id;@@ -233,6 +269,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : val =get_kernel_wa_level (reg_id) &KVM_REG_FEATURE_LEVEL_MASK ;break ; +case KVM_REG_ARM_STD_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_bmap); +break ;default :return -ENOENT ; }
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 290
Quoted 9 lines
arch/arm64/kvm/hypercalls.c +6
-0@@ -243,6 +282,43 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ; } +static int kvm_arm_set_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 reg_id,u64 val) +{ +int ret =0 ; +struct kvm *kvm = vcpu->kvm; +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; +u64 *fw_reg_bmap, fw_reg_features;
nit: use reverse fir tree ordering for locals (longest line first, shortest last).
Quoted 8 lines
arch/arm64/kvm/hypercalls.c +8
-0+switch (reg_id) { +case KVM_REG_ARM_STD_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_bmap; + fw_reg_features =ARM_SMCCC_STD_FEATURES ; +break ; +default : +return -ENOENT ; + }
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 309
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+ +/* Check for unsupported bit */ +if (val & ~fw_reg_features) +return -EINVAL ; + +mutex_lock (&kvm->lock); + +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY.
How about returning -EINVAL instead? We already do this for KVM_ARM_VCPU_INIT if userspace uses a different target than the one previously set.
Quoted 29 lines
arch/arm64/kvm/hypercalls.c +14
-0+*/ +if (kvm_arm_vm_has_run_once (&kvm->arch)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + +WRITE_ONCE (*fw_reg_bmap, val); +out: +mutex_unlock (&kvm->lock); +return ret; +} +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr;@@ -321,6 +397,8 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return -EINVAL ;return 0 ; +case KVM_REG_ARM_STD_BMAP : +return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ; }include/kvm/arm_hypercalls.h +0
-0
include/kvm/arm_hypercalls.h line 11
Quoted 6 lines
include/kvm/arm_hypercalls.h +3
-0@@ -6,6 +6,9 @@#include <asm/kvm_emulate.h> +#define ARM_SMCCC_STD_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +
This probably needs KVM_ somewhere in its name for the sake of scoping.
Quoted 14 lines
include/kvm/arm_hypercalls.h +1
-0int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)@@ -42,6 +45,7 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu,struct kvm_one_reg ; +void kvm_arm_init_hypercalls (struct kvm *kvm);int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu);int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg);Signature · 1 line
2.35.1.473.g83b2b277ed-goog
Re: [PATCH v4 06/13] KVM: arm64: Add standard hypervisor firmware register
In reply to Raghavendra Rao AnantaQuoted 85 lines · Raghavendra Rao Ananta
Introduce the firmware register to hold the standard hypervisor service calls (owner value 5) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Paravirtualized time, represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 20 +++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 29 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -105,9 +105,11 @@ struct kvm_arch_memory_slot {* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor * * @hvc_std_bmap: Bitmap of standard secure service calls +* @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap; +u64 hvc_std_hyp_bmap; };struct kvm_arch {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -295,6 +295,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0)#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_STD_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(1) +#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0) +#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/guest.c +1
-0@@ -66,6 +66,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP , + KVM_REG_ARM_STD_HYP_BMAP, };/** arch/arm64/kvm/hypercalls.c +5
-0@@ -75,6 +75,10 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_TRNG_RND64 :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap,KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +case ARM_SMCCC_HV_PV_TIME_FEATURES : +case ARM_SMCCC_HV_PV_TIME_ST : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap, +KVM_REG_ARM_STD_HYP_BIT_PV_TIME );default :/* By default, allow the services that aren't listed here */ return true;@@ -83,6 +87,7 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;u32 func_id =smccc_get_function (vcpu);u64 val[4 ] = {SMCCC_RET_NOT_SUPPORTED };u32 feature;
arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 143
Quoted 6 lines
arch/arm64/kvm/hypercalls.c +2
-1@@ -134,7 +139,10 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) }break ;case ARM_SMCCC_HV_PV_TIME_FEATURES : - val[0 ] =SMCCC_RET_SUCCESS ; + if (kvm_arm_fw_reg_feat_enabled( + hvc_desc->hvc_std_hyp_bmap,
It is probably OK to keep this parameter on the line above (just stay under 100 characters a line).
Quoted 67 lines
arch/arm64/kvm/hypercalls.c +12
-0+ KVM_REG_ARM_STD_HYP_BIT_PV_TIME)) + val[0 ] =SMCCC_RET_SUCCESS ;break ; }break ;@@ -179,6 +187,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP , + KVM_REG_ARM_STD_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)@@ -186,6 +195,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm)struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; + hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)@@ -272,6 +282,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_BMAP : val =READ_ONCE (hvc_desc->hvc_std_bmap);break ; +case KVM_REG_ARM_STD_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap); +break ;default :return -ENOENT ; }@@ -294,6 +307,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_bmap; fw_reg_features =ARM_SMCCC_STD_FEATURES ;break ; +case KVM_REG_ARM_STD_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; + fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ; +break ;default :return -ENOENT ; }@@ -398,6 +415,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP : +case KVM_REG_ARM_STD_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;include/kvm/arm_hypercalls.h +3
-0@@ -9,6 +9,9 @@#define ARM_SMCCC_STD_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_STD_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)Signature · 1 line
2.35.1.473.g83b2b277ed-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 07/13] KVM: arm64: Add vendor hypervisor firmware register
In reply to Raghavendra Rao AnantaQuoted 80 lines · Raghavendra Rao Ananta
Introduce the firmware register to hold the vendor specific hypervisor service calls (owner value 6) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Precision Time Protocol (PTP), represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 22 +++++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -106,10 +106,12 @@ struct kvm_arch_memory_slot {* * @hvc_std_bmap: Bitmap of standard secure service calls * @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls +* @hvc_vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap;u64 hvc_std_hyp_bmap; +u64 hvc_vendor_hyp_bmap; };struct kvm_arch {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -299,6 +299,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0)#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_VENDOR_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(2) +#define KVM_REG_ARM_VENDOR_HYP_BIT_PTP BIT(0) +#define KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/guest.c +1
-0@@ -67,6 +67,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };/** arch/arm64/kvm/hypercalls.c +3
-0@@ -79,6 +79,9 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_HV_PV_TIME_ST :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap,KVM_REG_ARM_STD_HYP_BIT_PV_TIME ); +case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_vendor_hyp_bmap, +KVM_REG_ARM_VENDOR_HYP_BIT_PTP );default :/* By default, allow the services that aren't listed here */ return true;@@ -162,7 +165,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID :
arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 175
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +8
-1val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); - val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_PTP ); + +/* +* The feature bits exposed to user-space doesn't include +* ARM_SMCCC_KVM_FUNC_FEATURES. However, we expose this to +* the guest as bit-0. Hence, left-shift the user-space +* exposed bitmap by 1 to accommodate this. +*/ + val[0 ] |= hvc_desc->hvc_vendor_hyp_bmap <<1 ;
Having an off-by-one difference between the userspace and guest representations of this bitmap seems like it could be a source of bugs in the future. Its also impossible for the guest to completely hide the vendor range if it so chooses.
Why not tie ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID and ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID to BIT(0)? PTP would then become BIT(1).
Quoted 65 lines
arch/arm64/kvm/hypercalls.c +10
-0break ;case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID :kvm_ptp_get_time (vcpu, val);@@ -188,6 +198,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)@@ -196,6 +207,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm) hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; + hvc_desc->hvc_vendor_hyp_bmap =ARM_SMCCC_VENDOR_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)@@ -285,6 +297,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_HYP_BMAP : val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap);break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_vendor_hyp_bmap); +break ;default :return -ENOENT ; }@@ -311,6 +326,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ;break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_vendor_hyp_bmap; + fw_reg_features =ARM_SMCCC_VENDOR_HYP_FEATURES ; +break ;default :return -ENOENT ; }@@ -416,6 +435,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP :case KVM_REG_ARM_STD_HYP_BMAP : +case KVM_REG_ARM_VENDOR_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;include/kvm/arm_hypercalls.h +3
-0@@ -12,6 +12,9 @@#define ARM_SMCCC_STD_HYP_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_VENDOR_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)Signature · 1 line
2.35.1.473.g83b2b277ed-goog
Re: [PATCH v4 09/13] Docs: KVM: Rename psci.rst to hypercalls.rst
In reply to Raghavendra Rao AnantaQuoted 5 lines · Raghavendra Rao Ananta
Since the doc now covers more of general hypercalls' details, rather than just PSCI, rename the file to a more appropriate name-
hypercalls.rst.Signed-off-by Raghavendra Rao Ananta <[email protected]>
You should move the file before adding to it IMO (switch this patch with the previous one).
Quoted 12 lines
--- Documentation/virt/kvm/arm/{psci.rst => hypercalls.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Documentation/virt/kvm/arm/{psci.rst => hypercalls.rst} (100%)Documentation/virt/kvm/arm/hypercalls.rst renamed from Documentation/virt/kvm/arm/psci.rst +0
-0Signature · 1 line
2.35.1.473.g83b2b277ed-goog
Re: [PATCH v4 13/13] selftests: KVM: aarch64: Add the bitmap firmware registers to get-reg-list
In reply to Raghavendra Rao AnantaQuoted 5 lines · Raghavendra Rao Ananta
Add the psuedo-firmware registers
KVM_REG_ARM_STD_BMAP,KVM_REG_ARM_STD_HYP_BMAP, andKVM_REG_ARM_VENDOR_HYP_BMAPto the base_regs[] list.Signed-off-by Raghavendra Rao Ananta <[email protected]>
Quoted 21 lines
--- tools/testing/selftests/kvm/aarch64/get-reg-list.c | 3 +++ 1 file changed, 3 insertions(+)tools/testing/selftests/kvm/aarch64/get-reg-list.c +3
-0@@ -686,6 +686,9 @@ static __u64 base_regs[] = {KVM_REG_ARM_FW_REG (0 ),KVM_REG_ARM_FW_REG (1 ),KVM_REG_ARM_FW_REG (2 ), +KVM_REG_ARM_FW_BMAP_REG (0 ),/* KVM_REG_ARM_STD_BMAP */ +KVM_REG_ARM_FW_BMAP_REG (1 ),/* KVM_REG_ARM_STD_HYP_BMAP */ +KVM_REG_ARM_FW_BMAP_REG (2 ),/* KVM_REG_ARM_VENDOR_HYP_BMAP */ ARM64_SYS_REG (3 ,3 ,14 ,3 ,1 ),/* CNTV_CTL_EL0 */ ARM64_SYS_REG (3 ,3 ,14 ,3 ,2 ),/* CNTV_CVAL_EL0 */ ARM64_SYS_REG(3, 3, 14, 0, 2),Signature · 1 line
2.35.1.473.g83b2b277ed-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 05/13] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
In reply to Oliver UptonQuoted 56 lines · Oliver Upton
Quoted 54 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the userspace. This means, the guests can observe hypercall services in and out as they migrate across various host kernel versions. This could be a major problem if the guest discovered a hypercall, started using it, and after getting migrated to an older kernel realizes that it's no longer available. Depending on how the guest handles the change, there's a potential chance that the guest would just panic.
As a result, there's a need for the userspace to elect the services that it wishes the guest to discover. It can elect these services based on the kernels spread across its (migration) fleet. To remedy this, extend the existing firmware psuedo-registers, such as
KVM_REG_ARM_PSCI_VERSION, for all the hypercall services available.These firmware registers are categorized based on the service call owners, and unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by the corresponding registers. It's expected that the VMMs discover the features provided by each register via
GET_ONE_REG, and writeback the desired values usingSET_ONE_REG. KVM allows this modification only until the VM has started.Older userspace code can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
In this patch, the framework adds the register only for ARM's standard secure services (owner value 4). Currently, this includes support only for ARM True Random Number Generator (TRNG) service, with bit-0 of the register representing mandatory features of v1.0. The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM. Other services are momentarily added in the upcoming patches.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 12 +++++ arch/arm64/include/uapi/asm/kvm.h | 8 ++++ arch/arm64/kvm/arm.c | 8 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 78 +++++++++++++++++++++++++++++++ include/kvm/arm_hypercalls.h | 4 ++ 6 files changed, 111 insertions(+)arch/arm64/include/asm/kvm_host.h +0
-0@@ -101,6 +101,15 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { };
Quoted 10 lines
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 109
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6
-0+/** +* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor +* +* @hvc_std_bmap: Bitmap of standard secure service calls +*/ +struct kvm_hvc_desc {nit: maybe call this structure
kvm_hypercall_features? When nested comes along guests will need to use the SVC conduit as HVC traps are always taken to EL2. Same will need to be true for virtual EL2.
actually, I'd recommend using SMCCC as the token instead, so kvm_smccc_features. It is the one constant between HVC and SMC calls and governs our whole hypercall ABI anyway.
Quoted 274 lines
Quoted 21 lines
arch/arm64/include/asm/kvm_host.h +6
-0+u64 hvc_std_bmap; +}; +struct kvm_arch {struct kvm_s2_mmu mmu;@@ -142,6 +151,9 @@ struct kvm_arch {/* Capture first run of the VM */ bool has_run_once; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 291
Quoted 5 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0@@ -287,6 +287,14 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED 3#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED (1U << 4) +/* Bitmap firmware registers, extension to the existing psuedo-register space */ +#define KVM_REG_ARM_FW_BMAP KVM_REG_ARM_FW_REG(0xff00)What is the motivation for moving the bitmap register indices so far away from the rest of the firmware regs?
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 292
Quoted 1 line
arch/arm64/include/uapi/asm/kvm.h +1
-0+#define KVM_REG_ARM_FW_BMAP_REG (r) (KVM_REG_ARM_FW_BMAP | (r))If you are still going to use the index offset, just pass 'r' through to the other macro:
#define KVM_REG_ARM_FW_BMAP_REG(r) KVM_REG_ARM_FW_REG(0xff00 + r)arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 296
Quoted 3 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_BMAP_REG(0) +#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0) +#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ Implementation details such as this probably shouldn't live in UAPI headers. We'll likely need to bump the value in the future.
Quoted 18 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0+/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/arm.c +1
-0@@ -156,6 +156,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm); +kvm_arm_init_hypercalls (kvm);return ret; out_free_stage2_pgd:@@ -635,7 +636,14 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))arch/arm64/kvm/arm.c int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) · line 646
Quoted 10 lines
arch/arm64/kvm/arm.c +7
-0kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu); +/* +* Grab kvm->lock such that the reader of has_run_once can finish +* the necessary operation atomically, such as deciding whether to +* block the writes to the firmware registers if the VM has run once. +*/ +mutex_lock (&kvm->lock); kvm->arch.has_run_once = true; +mutex_unlock (&kvm->lock);Shouldn't this have just grabbed the kvm lock in patch 04/13?
Quoted 32 lines
arch/arm64/kvm/arm.c +0
-0return ret; }arch/arm64/kvm/guest.c +1
-0@@ -65,6 +65,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +KVM_REG_ARM_STD_BMAP , };/** arch/arm64/kvm/hypercalls.c +10
-0@@ -58,6 +58,29 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) val[3 ] =lower_32_bits (cycles); } +static bool kvm_arm_fw_reg_feat_enabled (u64 reg_bmap,u64 feat_bit) +{ +return reg_bmap & feat_bit; +} + +static bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id) +{ +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc; + +switch (func_id) {arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 80
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+case ARM_SMCCC_TRNG_VERSION : +case ARM_SMCCC_TRNG_FEATURES : +case ARM_SMCCC_TRNG_GET_UUID : +case ARM_SMCCC_TRNG_RND32 : +case ARM_SMCCC_TRNG_RND64 : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap, +KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +default : +/* By default, allow the services that aren't listed here */ +return true;I think your default case should really return false. It keeps people honest when they add new patches to set up a new hypercall bit (no bit? no call!)
That of course requires that you only return false once all of the preexisting hypercalls are enumerated, otherwise such a patch would cause a regression in isolation.
Quoted 60 lines
arch/arm64/kvm/hypercalls.c +19
-0+ } +} +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) {u32 func_id =smccc_get_function (vcpu);@@ -65,6 +88,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)u32 feature;gpa_t gpa; +if (!kvm_hvc_call_supported (vcpu, func_id)) + goto out; +switch (func_id) {case ARM_SMCCC_VERSION_FUNC_ID : val[0 ] =ARM_SMCCC_VERSION_1_1 ;@@ -143,6 +169,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)return kvm_psci_call (vcpu); } +out:smccc_set_retval (vcpu, val[0 ], val[1 ], val[2 ], val[3 ]);return 1 ; }@@ -151,8 +178,16 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_PSCI_VERSION ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , + KVM_REG_ARM_STD_BMAP, }; +void kvm_arm_init_hypercalls (struct kvm *kvm) +{ +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; + + hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) {return ARRAY_SIZE (kvm_arm_fw_reg_ids);@@ -220,6 +255,7 @@ static int get_kernel_wa_level(u64 regid)int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;void __user *uaddr = (void __user *)(long )reg->addr;u64 val, reg_id = reg->id;@@ -233,6 +269,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : val =get_kernel_wa_level (reg_id) &KVM_REG_FEATURE_LEVEL_MASK ;break ; +case KVM_REG_ARM_STD_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_bmap); +break ;default :return -ENOENT ; }arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 290
Quoted 9 lines
arch/arm64/kvm/hypercalls.c +6
-0@@ -243,6 +282,43 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ; } +static int kvm_arm_set_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 reg_id,u64 val) +{ +int ret =0 ; +struct kvm *kvm = vcpu->kvm; +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; +u64 *fw_reg_bmap, fw_reg_features;nit: use reverse fir tree ordering for locals (longest line first, shortest last).
Quoted 8 lines
arch/arm64/kvm/hypercalls.c +8
-0+switch (reg_id) { +case KVM_REG_ARM_STD_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_bmap; + fw_reg_features =ARM_SMCCC_STD_FEATURES ; +break ; +default : +return -ENOENT ; + }arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 309
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+ +/* Check for unsupported bit */ +if (val & ~fw_reg_features) +return -EINVAL ; + +mutex_lock (&kvm->lock); + +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. How about returning
-EINVALinstead? We already do this forKVM_ARM_VCPU_INITif userspace uses a different target than the one previously set.Quoted 29 lines
arch/arm64/kvm/hypercalls.c +14
-0+*/ +if (kvm_arm_vm_has_run_once (&kvm->arch)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + +WRITE_ONCE (*fw_reg_bmap, val); +out: +mutex_unlock (&kvm->lock); +return ret; +} +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr;@@ -321,6 +397,8 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return -EINVAL ;return 0 ; +case KVM_REG_ARM_STD_BMAP : +return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ; }include/kvm/arm_hypercalls.h +0
-0include/kvm/arm_hypercalls.h line 11
Quoted 6 lines
include/kvm/arm_hypercalls.h +3
-0@@ -6,6 +6,9 @@#include <asm/kvm_emulate.h> +#define ARM_SMCCC_STD_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +This probably needs
KVM_somewhere in its name for the sake of scoping.Quoted 14 lines
include/kvm/arm_hypercalls.h +1
-1int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)@@ -42,6 +45,7 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu,struct kvm_one_reg ; +void kvm_arm_init_hypercalls (struct kvm *kvm);int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu);int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); --2.35.1.473.g83b2b277ed-goog
Re: [PATCH v4 04/13] KVM: arm64: Capture VM's first run
In reply to Oliver UptonOn Mon, Mar 14, 2022 at 11:02 AM Oliver Upton <[email protected]> wrote:
Hi Oliver,
Quoted 11 lines
Hi Raghavendra,
Quoted 7 lines · Raghavendra Rao Ananta
Capture the first run of the KVM VM, which is basically the first
KVM_RUNissued for any vCPU. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features, such as the feature bitmap exposed by the psuedo-firmware registers, after the VM has started running.
Quoted 8 lines
Quoted 1 line
Signed-off-by Raghavendra Rao Ananta <[email protected]>I believe this patch is superseded by commit:
5177fe91e4cf("KVM: arm64: Do not change the PMU event filter after a VCPU has run")on kvmarm/next.
Perfect! Just what we needed. I'll drop this patch.
Regards,
Raghavendra
Quoted 4 lines
--
Thanks,
Oliver
Re: [PATCH v4 01/13] KVM: arm64: Factor out firmware register handling from psci.c
In reply to Oliver UptonQuoted 84 lines · Oliver Upton
Hi Raghu,
Quoted 5 lines · Raghavendra Rao Ananta
Common hypercall firmware register handing is currently employed by
psci.c. Since the upcoming patches add more of these registers, it's better to move the generic handling tohypercall.cfor a cleaner presentation.Quoted 7 lines
While we are at it, collect all the firmware registers under fw_reg_ids[] to help implement
kvm_arm_get_fw_num_regs()andkvm_arm_copy_fw_reg_indices()in a generic way.No functional change intended.
Signed-off-by Raghavendra Rao Ananta <[email protected]>You might owe a rebase, thanks to the new Spectre bits KVM picked up (in Linus' tree) and PSCIv1.1 (kvmarm/next) :-)
Besides the nits:
Reviewed-by Oliver Upton <[email protected]>Quoted 45 lines
--- arch/arm64/kvm/guest.c | 2 +- arch/arm64/kvm/hypercalls.c | 170 +++++++++++++++++++++++++++++++++++ arch/arm64/kvm/psci.c | 166 ---------------------------------- include/kvm/arm_hypercalls.h | 7 ++ include/kvm/arm_psci.h | 7 -- 5 files changed, 178 insertions(+), 174 deletions(-)arch/arm64/kvm/guest.c +1
-1@@ -18,7 +18,7 @@#include <linux/string.h> #include <linux/vmalloc.h> #include <linux/fs.h> -#include <kvm/arm_psci.h> +#include <kvm/arm_hypercalls.h> #include <asm/cputype.h> #include <linux/uaccess.h> #include <asm/fpsimd.h> arch/arm64/kvm/hypercalls.c +16
-0@@ -146,3 +146,173 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)smccc_set_retval (vcpu, val[0 ], val[1 ], val[2 ], val[3 ]);return 1 ; } + +static const u64 kvm_arm_fw_reg_ids[] = { +KVM_REG_ARM_PSCI_VERSION , +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 , +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +}; + +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) +{ +return ARRAY_SIZE (kvm_arm_fw_reg_ids); +} + +int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) +{ +int i; +arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 174
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_ids); i++) { +if (put_user (kvm_arm_fw_reg_ids[i], uindices++)) +return -EFAULT ; + } + +return 0 ; +} + +#define KVM_REG_FEATURE_LEVEL_WIDTH 4 +#define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)Hrm... Not your code but this should really use
GENMASK()to be consistent with the rest of the kernel. Also, looks like_WIDTHis useless.
Quoted 6 lines
Perhaps fold these together:
#define KVM_REG_FEATURE_LEVEL_MASK GENMASK(3, 0)Also these kind of macros probably belong in headers.
Thanks for the suggestion. I'll definitely rebase and will try to clean these up as well.
Regards,
Raghavendra
Quoted 353 lines
Quoted 353 lines
arch/arm64/kvm/hypercalls.c +144
-0+ +/* +* Convert the workaround level into an easy-to-compare number, where higher +* values mean better protection. +*/ +static int get_kernel_wa_level (u64 regid) +{ +switch (regid) { +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : +switch (arm64_get_spectre_v2_state ()) { +case SPECTRE_VULNERABLE : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; +case SPECTRE_MITIGATED : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL ; +case SPECTRE_UNAFFECTED : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED ; + } +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : +switch (arm64_get_spectre_v4_state ()) { +case SPECTRE_MITIGATED : +/* +* As for the hypercall discovery, we pretend we +* don't have any FW mitigation if SSBS is there at +* all times. +*/ +if (cpus_have_final_cap (ARM64_SSBS )) +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; + fallthrough; +case SPECTRE_UNAFFECTED : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; +case SPECTRE_VULNERABLE : +return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; + } + } + +return -EINVAL ; +} + +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) +{ +void __user *uaddr = (void __user *)(long )reg->addr; +u64 val; + +switch (reg->id) { +case KVM_REG_ARM_PSCI_VERSION : + val =kvm_psci_version (vcpu, vcpu->kvm); +break ; +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : + val =get_kernel_wa_level (reg->id) &KVM_REG_FEATURE_LEVEL_MASK ; +break ; +default : +return -ENOENT ; + } + +if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + +return 0 ; +} + +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) +{ +void __user *uaddr = (void __user *)(long )reg->addr; +u64 val; +int wa_level; + +if (copy_from_user (&val, uaddr,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + +switch (reg->id) { +case KVM_REG_ARM_PSCI_VERSION : + { +bool wants_02; + + wants_02 =test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features); + +switch (val) { +case KVM_ARM_PSCI_0_1 : +if (wants_02) +return -EINVAL ; + vcpu->kvm->arch.psci_version = val; +return 0 ; +case KVM_ARM_PSCI_0_2 : +case KVM_ARM_PSCI_1_0 : +if (!wants_02) +return -EINVAL ; + vcpu->kvm->arch.psci_version = val; +return 0 ; + } +break ; + } + +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : +if (val & ~KVM_REG_FEATURE_LEVEL_MASK ) +return -EINVAL ; + +if (get_kernel_wa_level (reg->id) < val) +return -EINVAL ; + +return 0 ; + +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : +if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | +KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED )) +return -EINVAL ; + +/* The enabled bit must not be set unless the level is AVAIL. */ +if ((val &KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED ) && + (val &KVM_REG_FEATURE_LEVEL_MASK ) !=KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL ) +return -EINVAL ; + +/* +* Map all the possible incoming states to the only two we +* really want to deal with. +*/ +switch (val &KVM_REG_FEATURE_LEVEL_MASK ) { +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL : +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN : + wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; +break ; +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL : +case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED : + wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; +break ; +default : +return -EINVAL ; + } + +/* +* We can deal with NOT_AVAIL on NOT_REQUIRED, but not the +* other way around. +*/ +if (get_kernel_wa_level (reg->id) < wa_level) +return -EINVAL ; + +return 0 ; +default : +return -ENOENT ; + } + +return -EINVAL ; +}arch/arm64/kvm/psci.c +0
-166@@ -403,169 +403,3 @@ int kvm_psci_call(struct kvm_vcpu *vcpu)return -EINVAL ; }; } - -int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) -{ -return 3 ;/* PSCI version and two workaround registers */ -} - -int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) -{ -if (put_user (KVM_REG_ARM_PSCI_VERSION , uindices++)) -return -EFAULT ; - -if (put_user (KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 , uindices++)) -return -EFAULT ; - -if (put_user (KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , uindices++)) -return -EFAULT ; - -return 0 ; -} - -#define KVM_REG_FEATURE_LEVEL_WIDTH 4 -#define KVM_REG_FEATURE_LEVEL_MASK (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1) - -/* -* Convert the workaround level into an easy-to-compare number, where higher -* values mean better protection. -*/ -static int get_kernel_wa_level (u64 regid) -{ -switch (regid) { -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : -switch (arm64_get_spectre_v2_state ()) { -case SPECTRE_VULNERABLE : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; -case SPECTRE_MITIGATED : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL ; -case SPECTRE_UNAFFECTED : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED ; - } -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL ; -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : -switch (arm64_get_spectre_v4_state ()) { -case SPECTRE_MITIGATED : -/* -* As for the hypercall discovery, we pretend we -* don't have any FW mitigation if SSBS is there at -* all times. -*/ -if (cpus_have_final_cap (ARM64_SSBS )) -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; - fallthrough; -case SPECTRE_UNAFFECTED : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; -case SPECTRE_VULNERABLE : -return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; - } - } - -return -EINVAL ; -} - -int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) -{ -void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; - -switch (reg->id) { -case KVM_REG_ARM_PSCI_VERSION : - val =kvm_psci_version (vcpu, vcpu->kvm); -break ; -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : - val =get_kernel_wa_level (reg->id) &KVM_REG_FEATURE_LEVEL_MASK ; -break ; -default : -return -ENOENT ; - } - -if (copy_to_user (uaddr, &val,KVM_REG_SIZE (reg->id))) -return -EFAULT ; - -return 0 ; -} - -int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) -{ -void __user *uaddr = (void __user *)(long )reg->addr; -u64 val; -int wa_level; - -if (copy_from_user (&val, uaddr,KVM_REG_SIZE (reg->id))) -return -EFAULT ; - -switch (reg->id) { -case KVM_REG_ARM_PSCI_VERSION : - { -bool wants_02; - - wants_02 =test_bit (KVM_ARM_VCPU_PSCI_0_2 , vcpu->arch.features); - -switch (val) { -case KVM_ARM_PSCI_0_1 : -if (wants_02) -return -EINVAL ; - vcpu->kvm->arch.psci_version = val; -return 0 ; -case KVM_ARM_PSCI_0_2 : -case KVM_ARM_PSCI_1_0 : -if (!wants_02) -return -EINVAL ; - vcpu->kvm->arch.psci_version = val; -return 0 ; - } -break ; - } - -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 : -if (val & ~KVM_REG_FEATURE_LEVEL_MASK ) -return -EINVAL ; - -if (get_kernel_wa_level (reg->id) < val) -return -EINVAL ; - -return 0 ; - -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : -if (val & ~(KVM_REG_FEATURE_LEVEL_MASK | -KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED )) -return -EINVAL ; - -/* The enabled bit must not be set unless the level is AVAIL. */ -if ((val &KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED ) && - (val &KVM_REG_FEATURE_LEVEL_MASK ) !=KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL ) -return -EINVAL ; - -/* -* Map all the possible incoming states to the only two we -* really want to deal with. -*/ -switch (val &KVM_REG_FEATURE_LEVEL_MASK ) { -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL : -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN : - wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL ; -break ; -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL : -case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED : - wa_level =KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED ; -break ; -default : -return -EINVAL ; - } - -/* -* We can deal with NOT_AVAIL on NOT_REQUIRED, but not the -* other way around. -*/ -if (get_kernel_wa_level (reg->id) < wa_level) -return -EINVAL ; - -return 0 ; -default : -return -ENOENT ; - } - -return -EINVAL ; -}include/kvm/arm_hypercalls.h +7
-0@@ -40,4 +40,11 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu, vcpu_set_reg(vcpu, 3, a3); } +struct kvm_one_reg ; + +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu); +int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +#endif include/kvm/arm_psci.h +0
-8@@ -42,11 +42,4 @@ static inline int kvm_psci_version(struct kvm_vcpu *vcpu, struct kvm *kvm)int kvm_psci_call (struct kvm_vcpu *vcpu); -struct kvm_one_reg ; - -int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu); -int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); -int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); -int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); -#endif /* __KVM_ARM_PSCI_H__ */ --2.35.1.473.g83b2b277ed-goog
Re: [PATCH v4 05/13] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
In reply to Oliver UptonHi Oliver,
Quoted 56 lines · Oliver Upton
Quoted 54 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the userspace. This means, the guests can observe hypercall services in and out as they migrate across various host kernel versions. This could be a major problem if the guest discovered a hypercall, started using it, and after getting migrated to an older kernel realizes that it's no longer available. Depending on how the guest handles the change, there's a potential chance that the guest would just panic.
As a result, there's a need for the userspace to elect the services that it wishes the guest to discover. It can elect these services based on the kernels spread across its (migration) fleet. To remedy this, extend the existing firmware psuedo-registers, such as
KVM_REG_ARM_PSCI_VERSION, for all the hypercall services available.These firmware registers are categorized based on the service call owners, and unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by the corresponding registers. It's expected that the VMMs discover the features provided by each register via
GET_ONE_REG, and writeback the desired values usingSET_ONE_REG. KVM allows this modification only until the VM has started.Older userspace code can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
In this patch, the framework adds the register only for ARM's standard secure services (owner value 4). Currently, this includes support only for ARM True Random Number Generator (TRNG) service, with bit-0 of the register representing mandatory features of v1.0. The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM. Other services are momentarily added in the upcoming patches.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 12 +++++ arch/arm64/include/uapi/asm/kvm.h | 8 ++++ arch/arm64/kvm/arm.c | 8 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 78 +++++++++++++++++++++++++++++++ include/kvm/arm_hypercalls.h | 4 ++ 6 files changed, 111 insertions(+)arch/arm64/include/asm/kvm_host.h +0
-0@@ -101,6 +101,15 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { };
Quoted 11 lines
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 109
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6
-0+/** +* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor +* +* @hvc_std_bmap: Bitmap of standard secure service calls +*/ +struct kvm_hvc_desc {nit: maybe call this structure
kvm_hypercall_features? When nested comes along guests will need to use the SVC conduit as HVC traps are always taken to EL2. Same will need to be true for virtual EL2.
Sure, I can rename it to be more generic.
Quoted 24 lines
Quoted 24 lines
arch/arm64/include/asm/kvm_host.h +6
-0+u64 hvc_std_bmap; +}; +struct kvm_arch {struct kvm_s2_mmu mmu;@@ -142,6 +151,9 @@ struct kvm_arch {/* Capture first run of the VM */ bool has_run_once; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -287,6 +287,14 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED 3#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED (1U << 4)
Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 291
Quoted 2 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0+/* Bitmap firmware registers, extension to the existing psuedo-register space */ +#define KVM_REG_ARM_FW_BMAP KVM_REG_ARM_FW_REG(0xff00)What is the motivation for moving the bitmap register indices so far away from the rest of the firmware regs?
The original motivation to create a sub-space came from Reiji's comment on v3 [1] so that user-space can distinguish between bitmapped and regular fw registers.
As with the spacing, I thought a 50/50 split would do a good job of avoiding collisions. Do you have any recommendations here?
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 292
Quoted 1 line
arch/arm64/include/uapi/asm/kvm.h +1
-0+#define KVM_REG_ARM_FW_BMAP_REG (r) (KVM_REG_ARM_FW_BMAP | (r))If you are still going to use the index offset, just pass 'r' through to the other macro:
#define KVM_REG_ARM_FW_BMAP_REG(r) KVM_REG_ARM_FW_REG(0xff00 + r)
I'm sorry, what's the advantage of doing this?
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 296
Quoted 3 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_BMAP_REG(0) +#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0) +#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ Implementation details such as this probably shouldn't live in UAPI headers. We'll likely need to bump the value in the future.
Wouldn't the macros act as helpers to userspace as well?
I agree with your point about the limited spacing, and we may need to expand/shrink as needed, but isn't that a general register-space problem?
Quoted 20 lines
Quoted 20 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0+/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/arm.c +1
-0@@ -156,6 +156,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm); +kvm_arm_init_hypercalls (kvm);return ret; out_free_stage2_pgd:@@ -635,7 +636,14 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu);
Quoted 11 lines
arch/arm64/kvm/arm.c int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) · line 646
Quoted 8 lines
arch/arm64/kvm/arm.c +7
-0+/* +* Grab kvm->lock such that the reader of has_run_once can finish +* the necessary operation atomically, such as deciding whether to +* block the writes to the firmware registers if the VM has run once. +*/ +mutex_lock (&kvm->lock); kvm->arch.has_run_once = true; +mutex_unlock (&kvm->lock);Shouldn't this have just grabbed the kvm lock in patch 04/13?
It could have, and I agree it's a little igly. But thinking that there's are no parallel threads/consumers when it was initially implemented gave me no reason to grab the locks.
Quoted 47 lines
Quoted 32 lines
arch/arm64/kvm/arm.c +0
-0return ret; }arch/arm64/kvm/guest.c +1
-0@@ -65,6 +65,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +KVM_REG_ARM_STD_BMAP , };/** arch/arm64/kvm/hypercalls.c +10
-0@@ -58,6 +58,29 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) val[3 ] =lower_32_bits (cycles); } +static bool kvm_arm_fw_reg_feat_enabled (u64 reg_bmap,u64 feat_bit) +{ +return reg_bmap & feat_bit; +} + +static bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id) +{ +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc; + +switch (func_id) {arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 80
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+case ARM_SMCCC_TRNG_VERSION : +case ARM_SMCCC_TRNG_FEATURES : +case ARM_SMCCC_TRNG_GET_UUID : +case ARM_SMCCC_TRNG_RND32 : +case ARM_SMCCC_TRNG_RND64 : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap, +KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +default : +/* By default, allow the services that aren't listed here */ +return true;I think your default case should really return false. It keeps people honest when they add new patches to set up a new hypercall bit (no bit? no call!)
Quoted 4 lines
That of course requires that you only return false once all of the preexisting hypercalls are enumerated, otherwise such a patch would cause a regression in isolation.
I agree, but not all hypercalls may have a bit. For example, guarding ARM_SMCCC_VERSION_FUNC_ID doesn't make sense and may not be gated by the bit. Hence, the idea was to do an initial check if the func_id is in fact gated. If yes, check the bit, else let the actual implementation take care of it.
Quoted 62 lines
Quoted 62 lines
arch/arm64/kvm/hypercalls.c +18
-0+} +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) {u32 func_id =smccc_get_function (vcpu);@@ -65,6 +88,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)u32 feature;gpa_t gpa; +if (!kvm_hvc_call_supported (vcpu, func_id)) + goto out; +switch (func_id) {case ARM_SMCCC_VERSION_FUNC_ID : val[0 ] =ARM_SMCCC_VERSION_1_1 ;@@ -143,6 +169,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)return kvm_psci_call (vcpu); } +out:smccc_set_retval (vcpu, val[0 ], val[1 ], val[2 ], val[3 ]);return 1 ; }@@ -151,8 +178,16 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_PSCI_VERSION ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , + KVM_REG_ARM_STD_BMAP, }; +void kvm_arm_init_hypercalls (struct kvm *kvm) +{ +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; + + hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) {return ARRAY_SIZE (kvm_arm_fw_reg_ids);@@ -220,6 +255,7 @@ static int get_kernel_wa_level(u64 regid)int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;void __user *uaddr = (void __user *)(long )reg->addr;u64 val, reg_id = reg->id;@@ -233,6 +269,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : val =get_kernel_wa_level (reg_id) &KVM_REG_FEATURE_LEVEL_MASK ;break ; +case KVM_REG_ARM_STD_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_bmap); +break ;default :return -ENOENT ; }@@ -243,6 +282,43 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ; }
Quoted 10 lines
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 290
Quoted 6 lines
arch/arm64/kvm/hypercalls.c +6
-0+static int kvm_arm_set_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 reg_id,u64 val) +{ +int ret =0 ; +struct kvm *kvm = vcpu->kvm; +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; +u64 *fw_reg_bmap, fw_reg_features;nit: use reverse fir tree ordering for locals (longest line first, shortest last).
Got it.
Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/hypercalls.c +14
-0+switch (reg_id) { +case KVM_REG_ARM_STD_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_bmap; + fw_reg_features =ARM_SMCCC_STD_FEATURES ; +break ; +default : +return -ENOENT ; + } + +/* Check for unsupported bit */ +if (val & ~fw_reg_features) +return -EINVAL ; + +mutex_lock (&kvm->lock);
Quoted 9 lines
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 309
Quoted 4 lines
arch/arm64/kvm/hypercalls.c +4
-0+ +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. How about returning
-EINVALinstead? We already do this forKVM_ARM_VCPU_INITif userspace uses a different target than the one previously set.
Returning EBUSY could help userspace distinguish the cause of failure better as compared to EINVAL, since technically, the arguments supplied by the user are in fact valid. It's just that the timing is wrong.
Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/hypercalls.c +14
-0+*/ +if (kvm_arm_vm_has_run_once (&kvm->arch)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + +WRITE_ONCE (*fw_reg_bmap, val); +out: +mutex_unlock (&kvm->lock); +return ret; +} +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr;@@ -321,6 +397,8 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return -EINVAL ;return 0 ; +case KVM_REG_ARM_STD_BMAP : +return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ; }include/kvm/arm_hypercalls.h +0
-0@@ -6,6 +6,9 @@
Quoted 8 lines
include/kvm/arm_hypercalls.h line 11
Quoted 5 lines
include/kvm/arm_hypercalls.h +3
-0#include <asm/kvm_emulate.h> +#define ARM_SMCCC_STD_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +This probably needs
KVM_somewhere in its name for the sake of scoping.
RIght, I can append that.
Quoted 7 lines
Quoted 7 lines
include/kvm/arm_hypercalls.h +0
-0int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)@@ -42,6 +45,7 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu,struct kvm_one_reg ;
Quoted 7 lines
Quoted 7 lines
include/kvm/arm_hypercalls.h +1
-1+void kvm_arm_init_hypercalls (struct kvm *kvm);int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu);int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); --2.35.1.473.g83b2b277ed-goog
Regards,
Raghavendra
[1]: https://lore.kernel.org/linux-arm-kernel/CAJHc60y6b-scY8zcPuLnjGtr6HzSBnmhi2mCnmkNm4nTxgMTUQ@mail.gmail.com/
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 06/13] KVM: arm64: Add standard hypervisor firmware register
In reply to Oliver UptonQuoted 80 lines · Oliver Upton
Quoted 78 lines · Raghavendra Rao Ananta
Introduce the firmware register to hold the standard hypervisor service calls (owner value 5) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Paravirtualized time, represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 20 +++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 29 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -105,9 +105,11 @@ struct kvm_arch_memory_slot {* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor * * @hvc_std_bmap: Bitmap of standard secure service calls +* @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap; +u64 hvc_std_hyp_bmap; };struct kvm_arch {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -295,6 +295,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0)#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_STD_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(1) +#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0) +#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/guest.c +1
-0@@ -66,6 +66,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP , + KVM_REG_ARM_STD_HYP_BMAP, };/** arch/arm64/kvm/hypercalls.c +4
-0@@ -75,6 +75,10 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_TRNG_RND64 :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap,KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +case ARM_SMCCC_HV_PV_TIME_FEATURES : +case ARM_SMCCC_HV_PV_TIME_ST : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap, +KVM_REG_ARM_STD_HYP_BIT_PV_TIME );default :/* By default, allow the services that aren't listed here */ return true;@@ -83,6 +87,7 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)
Quoted 17 lines
arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 143
Quoted 13 lines
arch/arm64/kvm/hypercalls.c +3
-1int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;u32 func_id =smccc_get_function (vcpu);u64 val[4 ] = {SMCCC_RET_NOT_SUPPORTED };u32 feature;@@ -134,7 +139,10 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) }break ;case ARM_SMCCC_HV_PV_TIME_FEATURES : - val[0 ] =SMCCC_RET_SUCCESS ; + if (kvm_arm_fw_reg_feat_enabled( + hvc_desc->hvc_std_hyp_bmap,It is probably OK to keep this parameter on the line above (just stay under 100 characters a line).
Will rearrange this in the next series. Thanks.
Regards,
Raghavendra
Quoted 67 lines
Quoted 67 lines
arch/arm64/kvm/hypercalls.c +12
-0+ KVM_REG_ARM_STD_HYP_BIT_PV_TIME)) + val[0 ] =SMCCC_RET_SUCCESS ;break ; }break ;@@ -179,6 +187,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP , + KVM_REG_ARM_STD_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)@@ -186,6 +195,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm)struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; + hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)@@ -272,6 +282,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_BMAP : val =READ_ONCE (hvc_desc->hvc_std_bmap);break ; +case KVM_REG_ARM_STD_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap); +break ;default :return -ENOENT ; }@@ -294,6 +307,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_bmap; fw_reg_features =ARM_SMCCC_STD_FEATURES ;break ; +case KVM_REG_ARM_STD_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; + fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ; +break ;default :return -ENOENT ; }@@ -398,6 +415,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP : +case KVM_REG_ARM_STD_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;include/kvm/arm_hypercalls.h +3
-1@@ -9,6 +9,9 @@#define ARM_SMCCC_STD_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_STD_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) --2.35.1.473.g83b2b277ed-goog
Re: [PATCH v4 07/13] KVM: arm64: Add vendor hypervisor firmware register
In reply to Oliver UptonQuoted 98 lines · Oliver Upton
Quoted 80 lines · Raghavendra Rao Ananta
Introduce the firmware register to hold the vendor specific hypervisor service calls (owner value 6) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Precision Time Protocol (PTP), represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 22 +++++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -106,10 +106,12 @@ struct kvm_arch_memory_slot {* * @hvc_std_bmap: Bitmap of standard secure service calls * @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls +* @hvc_vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap;u64 hvc_std_hyp_bmap; +u64 hvc_vendor_hyp_bmap; };struct kvm_arch {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -299,6 +299,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0)#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_VENDOR_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(2) +#define KVM_REG_ARM_VENDOR_HYP_BIT_PTP BIT(0) +#define KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/guest.c +1
-0@@ -67,6 +67,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };/** arch/arm64/kvm/hypercalls.c +3
-0@@ -79,6 +79,9 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_HV_PV_TIME_ST :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap,KVM_REG_ARM_STD_HYP_BIT_PV_TIME ); +case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_vendor_hyp_bmap, +KVM_REG_ARM_VENDOR_HYP_BIT_PTP );default :/* By default, allow the services that aren't listed here */ return true;@@ -162,7 +165,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID :arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 175
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +8
-1val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); - val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_PTP ); + +/* +* The feature bits exposed to user-space doesn't include +* ARM_SMCCC_KVM_FUNC_FEATURES. However, we expose this to +* the guest as bit-0. Hence, left-shift the user-space +* exposed bitmap by 1 to accommodate this. +*/ + val[0 ] |= hvc_desc->hvc_vendor_hyp_bmap <<1 ;Having an off-by-one difference between the userspace and guest representations of this bitmap seems like it could be a source of bugs in the future. Its also impossible for the guest to completely hide the vendor range if it so chooses.
Quoted 4 lines
Why not tie
ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_IDandARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_IDto BIT(0)? PTP would then become BIT(1).
I agree it's a little asymmetrical. But exposing a bit for the func_ids that you mentioned means providing a capability to disable them by the userspace. This would block the guests from even discovering the space. If it's not too ugly, we can maintain certain bits to always remain read-only to the user-space. On the other hand, we can simply ignore what the userspace configure and simply treat it as a userspace bug. What do you think?
Regards,
Raghavendra
Quoted 61 lines
Quoted 61 lines
arch/arm64/kvm/hypercalls.c +10
-0break ;case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID :kvm_ptp_get_time (vcpu, val);@@ -188,6 +198,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)@@ -196,6 +207,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm) hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; + hvc_desc->hvc_vendor_hyp_bmap =ARM_SMCCC_VENDOR_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)@@ -285,6 +297,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_HYP_BMAP : val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap);break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_vendor_hyp_bmap); +break ;default :return -ENOENT ; }@@ -311,6 +326,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ;break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_vendor_hyp_bmap; + fw_reg_features =ARM_SMCCC_VENDOR_HYP_FEATURES ; +break ;default :return -ENOENT ; }@@ -416,6 +435,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP :case KVM_REG_ARM_STD_HYP_BMAP : +case KVM_REG_ARM_VENDOR_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;include/kvm/arm_hypercalls.h +3
-0@@ -12,6 +12,9 @@#define ARM_SMCCC_STD_HYP_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_VENDOR_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);
Quoted 4 lines
Quoted 4 lines
include/kvm/arm_hypercalls.h +0
-1static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) --2.35.1.473.g83b2b277ed-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 09/13] Docs: KVM: Rename psci.rst to hypercalls.rst
In reply to Oliver UptonQuoted 11 lines · Oliver Upton
Quoted 5 lines · Raghavendra Rao Ananta
Since the doc now covers more of general hypercalls' details, rather than just PSCI, rename the file to a more appropriate name-
hypercalls.rst.Signed-off-by Raghavendra Rao Ananta <[email protected]>You should move the file before adding to it IMO (switch this patch with the previous one).
Sure, I can do that.
Regards,
Raghavendra
Quoted 7 lines
Reviewed-by Oliver Upton <[email protected]>Quoted 5 lines
--- Documentation/virt/kvm/arm/{psci.rst => hypercalls.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Documentation/virt/kvm/arm/{psci.rst => hypercalls.rst} (100%)
Quoted 7 lines
Quoted 7 lines
Documentation/virt/kvm/arm/hypercalls.rst renamed from Documentation/virt/kvm/arm/psci.rst +0
-1--2.35.1.473.g83b2b277ed-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 07/13] KVM: arm64: Add vendor hypervisor firmware register
In reply to Raghavendra Rao AnantaQuoted 103 lines · Raghavendra Rao Ananta
Quoted 98 lines · Oliver Upton
Quoted 80 lines · Raghavendra Rao Ananta
Introduce the firmware register to hold the vendor specific hypervisor service calls (owner value 6) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Precision Time Protocol (PTP), represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 22 +++++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -106,10 +106,12 @@ struct kvm_arch_memory_slot {* * @hvc_std_bmap: Bitmap of standard secure service calls * @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls +* @hvc_vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap;u64 hvc_std_hyp_bmap; +u64 hvc_vendor_hyp_bmap; };struct kvm_arch {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -299,6 +299,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0)#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_VENDOR_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(2) +#define KVM_REG_ARM_VENDOR_HYP_BIT_PTP BIT(0) +#define KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/guest.c +1
-0@@ -67,6 +67,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };/** arch/arm64/kvm/hypercalls.c +3
-0@@ -79,6 +79,9 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_HV_PV_TIME_ST :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap,KVM_REG_ARM_STD_HYP_BIT_PV_TIME ); +case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_vendor_hyp_bmap, +KVM_REG_ARM_VENDOR_HYP_BIT_PTP );default :/* By default, allow the services that aren't listed here */ return true;@@ -162,7 +165,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID :arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 175
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +8
-1val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); - val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_PTP ); + +/* +* The feature bits exposed to user-space doesn't include +* ARM_SMCCC_KVM_FUNC_FEATURES. However, we expose this to +* the guest as bit-0. Hence, left-shift the user-space +* exposed bitmap by 1 to accommodate this. +*/ + val[0 ] |= hvc_desc->hvc_vendor_hyp_bmap <<1 ;Having an off-by-one difference between the userspace and guest representations of this bitmap seems like it could be a source of bugs in the future. Its also impossible for the guest to completely hide the vendor range if it so chooses.
Quoted 4 lines
Why not tie
ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_IDandARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_IDto BIT(0)? PTP would then become BIT(1).
Quoted 7 lines
I agree it's a little asymmetrical. But exposing a bit for the
func_idsthat you mentioned means providing a capability to disable them by the userspace. This would block the guests from even discovering the space. If it's not too ugly, we can maintain certain bits to always remain read-only to the user-space. On the other hand, we can simply ignore what the userspace configure and simply treat it as a userspace bug. What do you think?
I think that assigning a bit to the aforementioned hypercalls would be best. If userspace decides to hide all the features enumerated in the subrange then there isn't much point to the guest knowing that the range even exists. It shouldn't amount to much for userspace, as it will likely just keep the default value and only worry about these registers when migrating.
Apologies if I'm being pedantic, but such a subtle implementation detail could be overlooked in future changes.
--
Oliver
Quoted 61 lines
Quoted 61 lines
Quoted 61 lines
arch/arm64/kvm/hypercalls.c +10
-0break ;case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID :kvm_ptp_get_time (vcpu, val);@@ -188,6 +198,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)@@ -196,6 +207,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm) hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; + hvc_desc->hvc_vendor_hyp_bmap =ARM_SMCCC_VENDOR_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)@@ -285,6 +297,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_HYP_BMAP : val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap);break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_vendor_hyp_bmap); +break ;default :return -ENOENT ; }@@ -311,6 +326,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ;break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_vendor_hyp_bmap; + fw_reg_features =ARM_SMCCC_VENDOR_HYP_FEATURES ; +break ;default :return -ENOENT ; }@@ -416,6 +435,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP :case KVM_REG_ARM_STD_HYP_BMAP : +case KVM_REG_ARM_VENDOR_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;include/kvm/arm_hypercalls.h +3
-0@@ -12,6 +12,9 @@#define ARM_SMCCC_STD_HYP_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_VENDOR_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
include/kvm/arm_hypercalls.h +0
-1static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) --2.35.1.473.g83b2b277ed-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 05/13] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
In reply to Raghavendra Rao AnantaQuoted 102 lines · Raghavendra Rao Ananta
Hi Oliver,
Quoted 56 lines · Oliver Upton
Quoted 54 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the userspace. This means, the guests can observe hypercall services in and out as they migrate across various host kernel versions. This could be a major problem if the guest discovered a hypercall, started using it, and after getting migrated to an older kernel realizes that it's no longer available. Depending on how the guest handles the change, there's a potential chance that the guest would just panic.
As a result, there's a need for the userspace to elect the services that it wishes the guest to discover. It can elect these services based on the kernels spread across its (migration) fleet. To remedy this, extend the existing firmware psuedo-registers, such as
KVM_REG_ARM_PSCI_VERSION, for all the hypercall services available.These firmware registers are categorized based on the service call owners, and unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by the corresponding registers. It's expected that the VMMs discover the features provided by each register via
GET_ONE_REG, and writeback the desired values usingSET_ONE_REG. KVM allows this modification only until the VM has started.Older userspace code can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
In this patch, the framework adds the register only for ARM's standard secure services (owner value 4). Currently, this includes support only for ARM True Random Number Generator (TRNG) service, with bit-0 of the register representing mandatory features of v1.0. The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM. Other services are momentarily added in the upcoming patches.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 12 +++++ arch/arm64/include/uapi/asm/kvm.h | 8 ++++ arch/arm64/kvm/arm.c | 8 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 78 +++++++++++++++++++++++++++++++ include/kvm/arm_hypercalls.h | 4 ++ 6 files changed, 111 insertions(+)arch/arm64/include/asm/kvm_host.h +0
-0@@ -101,6 +101,15 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { };Quoted 11 lines
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 109
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6
-0+/** +* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor +* +* @hvc_std_bmap: Bitmap of standard secure service calls +*/ +struct kvm_hvc_desc {nit: maybe call this structure
kvm_hypercall_features? When nested comes along guests will need to use the SVC conduit as HVC traps are always taken to EL2. Same will need to be true for virtual EL2.Sure, I can rename it to be more generic.
Quoted 24 lines
Quoted 24 lines
arch/arm64/include/asm/kvm_host.h +6
-0+u64 hvc_std_bmap; +}; +struct kvm_arch {struct kvm_s2_mmu mmu;@@ -142,6 +151,9 @@ struct kvm_arch {/* Capture first run of the VM */ bool has_run_once; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -287,6 +287,14 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED 3#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED (1U << 4)Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 291
Quoted 2 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0+/* Bitmap firmware registers, extension to the existing psuedo-register space */ +#define KVM_REG_ARM_FW_BMAP KVM_REG_ARM_FW_REG(0xff00)What is the motivation for moving the bitmap register indices so far away from the rest of the firmware regs?
Quoted 6 lines
The original motivation to create a sub-space came from Reiji's comment on v3 [1] so that user-space can distinguish between bitmapped and regular fw registers.
As with the spacing, I thought a 50/50 split would do a good job of avoiding collisions. Do you have any recommendations here?
I see. This is for the sake of ABI stability with future expansion, right? A new register could be added in the future that controls more SMCCC features, and we expect userspace to zero them if it cares about ABI stability.
If that is all true, we probably need some strong supporting documentation. Additionally, using a new COPROC value for the register range might be better than partitioning the existing FW reg range.
Quoted 16 lines
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 292
Quoted 1 line
arch/arm64/include/uapi/asm/kvm.h +1
-0+#define KVM_REG_ARM_FW_BMAP_REG (r) (KVM_REG_ARM_FW_BMAP | (r))If you are still going to use the index offset, just pass 'r' through to the other macro:
#define KVM_REG_ARM_FW_BMAP_REG(r) KVM_REG_ARM_FW_REG(0xff00 + r)I'm sorry, what's the advantage of doing this?
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 296
Quoted 3 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_BMAP_REG(0) +#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0) +#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ Implementation details such as this probably shouldn't live in UAPI headers. We'll likely need to bump the value in the future.
Quoted 5 lines
Wouldn't the macros act as helpers to userspace as well?
I agree with your point about the limited spacing, and we may need to expand/shrink as needed, but isn't that a general register-space problem?
I think that the kernel's responsibility to userspace starts and ends with enumerating the register bits. Those values remain invariant on future KVM. On the other hand, you'll need to bump the _MAX value every time you add to the register. Between documentation and bit definitions, userspace should have enough info to work out the rest on its own :)
Quoted 31 lines
Quoted 20 lines
Quoted 20 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0+/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/arm.c +1
-0@@ -156,6 +156,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm); +kvm_arm_init_hypercalls (kvm);return ret; out_free_stage2_pgd:@@ -635,7 +636,14 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu);Quoted 11 lines
arch/arm64/kvm/arm.c int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) · line 646
Quoted 8 lines
arch/arm64/kvm/arm.c +7
-0+/* +* Grab kvm->lock such that the reader of has_run_once can finish +* the necessary operation atomically, such as deciding whether to +* block the writes to the firmware registers if the VM has run once. +*/ +mutex_lock (&kvm->lock); kvm->arch.has_run_once = true; +mutex_unlock (&kvm->lock);Shouldn't this have just grabbed the kvm lock in patch 04/13?
Quoted 3 lines
It could have, and I agree it's a little igly. But thinking that there's are no parallel threads/consumers when it was initially implemented gave me no reason to grab the locks.
I see, I was just curious if there was something I had missed.
Quoted 51 lines
Quoted 47 lines
Quoted 32 lines
arch/arm64/kvm/arm.c +0
-0return ret; }arch/arm64/kvm/guest.c +1
-0@@ -65,6 +65,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +KVM_REG_ARM_STD_BMAP , };/** arch/arm64/kvm/hypercalls.c +10
-0@@ -58,6 +58,29 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) val[3 ] =lower_32_bits (cycles); } +static bool kvm_arm_fw_reg_feat_enabled (u64 reg_bmap,u64 feat_bit) +{ +return reg_bmap & feat_bit; +} + +static bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id) +{ +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc; + +switch (func_id) {arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 80
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+case ARM_SMCCC_TRNG_VERSION : +case ARM_SMCCC_TRNG_FEATURES : +case ARM_SMCCC_TRNG_GET_UUID : +case ARM_SMCCC_TRNG_RND32 : +case ARM_SMCCC_TRNG_RND64 : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap, +KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +default : +/* By default, allow the services that aren't listed here */ +return true;I think your default case should really return false. It keeps people honest when they add new patches to set up a new hypercall bit (no bit? no call!)
Quoted 4 lines
That of course requires that you only return false once all of the preexisting hypercalls are enumerated, otherwise such a patch would cause a regression in isolation.
Quoted 5 lines
I agree, but not all hypercalls may have a bit. For example, guarding
ARM_SMCCC_VERSION_FUNC_IDdoesn't make sense and may not be gated by the bit. Hence, the idea was to do an initial check if thefunc_idis in fact gated. If yes, check the bit, else let the actual implementation take care of it.
The set of hypercalls that are not under userspace control need to exist on an allowlist then. Any new hypercall after the introduction of these registers must have a bit assigned then, right? Otherwise it would seem that new hypercalls could sneak into the guest ABI when migrating to an upgraded kernel.
Quoted 97 lines
Quoted 63 lines
Quoted 63 lines
arch/arm64/kvm/hypercalls.c +18
-0+} +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) {u32 func_id =smccc_get_function (vcpu);@@ -65,6 +88,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)u32 feature;gpa_t gpa; +if (!kvm_hvc_call_supported (vcpu, func_id)) + goto out; +switch (func_id) {case ARM_SMCCC_VERSION_FUNC_ID : val[0 ] =ARM_SMCCC_VERSION_1_1 ;@@ -143,6 +169,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)return kvm_psci_call (vcpu); } +out:smccc_set_retval (vcpu, val[0 ], val[1 ], val[2 ], val[3 ]);return 1 ; }@@ -151,8 +178,16 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_PSCI_VERSION ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , + KVM_REG_ARM_STD_BMAP, }; +void kvm_arm_init_hypercalls (struct kvm *kvm) +{ +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; + + hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) {return ARRAY_SIZE (kvm_arm_fw_reg_ids);@@ -220,6 +255,7 @@ static int get_kernel_wa_level(u64 regid)int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;void __user *uaddr = (void __user *)(long )reg->addr;u64 val, reg_id = reg->id;@@ -233,6 +269,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : val =get_kernel_wa_level (reg_id) &KVM_REG_FEATURE_LEVEL_MASK ;break ; +case KVM_REG_ARM_STD_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_bmap); +break ;default :return -ENOENT ; }@@ -243,6 +282,43 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ; }Quoted 10 lines
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 290
Quoted 6 lines
arch/arm64/kvm/hypercalls.c +6
-0+static int kvm_arm_set_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 reg_id,u64 val) +{ +int ret =0 ; +struct kvm *kvm = vcpu->kvm; +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; +u64 *fw_reg_bmap, fw_reg_features;nit: use reverse fir tree ordering for locals (longest line first, shortest last).
Got it.
Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/hypercalls.c +14
-0+switch (reg_id) { +case KVM_REG_ARM_STD_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_bmap; + fw_reg_features =ARM_SMCCC_STD_FEATURES ; +break ; +default : +return -ENOENT ; + } + +/* Check for unsupported bit */ +if (val & ~fw_reg_features) +return -EINVAL ; + +mutex_lock (&kvm->lock);Quoted 9 lines
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 309
Quoted 4 lines
arch/arm64/kvm/hypercalls.c +4
-0+ +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. How about returning
-EINVALinstead? We already do this forKVM_ARM_VCPU_INITif userspace uses a different target than the one previously set.
Quoted 5 lines
Returning EBUSY could help userspace distinguish the cause of failure better as compared to EINVAL, since technically, the arguments supplied by the user are in fact valid. It's just that the timing is wrong.
Good point :-) Also, this matches the PMU event filter handling as well. Apologies.
Quoted 59 lines
Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/hypercalls.c +14
-0+*/ +if (kvm_arm_vm_has_run_once (&kvm->arch)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + +WRITE_ONCE (*fw_reg_bmap, val); +out: +mutex_unlock (&kvm->lock); +return ret; +} +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr;@@ -321,6 +397,8 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return -EINVAL ;return 0 ; +case KVM_REG_ARM_STD_BMAP : +return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ; }include/kvm/arm_hypercalls.h +0
-0@@ -6,6 +6,9 @@Quoted 8 lines
include/kvm/arm_hypercalls.h line 11
Quoted 5 lines
include/kvm/arm_hypercalls.h +3
-0#include <asm/kvm_emulate.h> +#define ARM_SMCCC_STD_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +This probably needs
KVM_somewhere in its name for the sake of scoping.RIght, I can append that.
Quoted 7 lines
Quoted 7 lines
include/kvm/arm_hypercalls.h +0
-0int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)@@ -42,6 +45,7 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu,struct kvm_one_reg ;Quoted 7 lines
Quoted 7 lines
include/kvm/arm_hypercalls.h +1
-1+void kvm_arm_init_hypercalls (struct kvm *kvm);int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu);int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); --2.35.1.473.g83b2b277ed-goog
Regards,
Raghavendra
Re: [PATCH v4 05/13] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Quoted 111 lines · Oliver Upton
Quoted 102 lines · Raghavendra Rao Ananta
Hi Oliver,
Quoted 56 lines · Oliver Upton
Quoted 54 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the userspace. This means, the guests can observe hypercall services in and out as they migrate across various host kernel versions. This could be a major problem if the guest discovered a hypercall, started using it, and after getting migrated to an older kernel realizes that it's no longer available. Depending on how the guest handles the change, there's a potential chance that the guest would just panic.
As a result, there's a need for the userspace to elect the services that it wishes the guest to discover. It can elect these services based on the kernels spread across its (migration) fleet. To remedy this, extend the existing firmware psuedo-registers, such as
KVM_REG_ARM_PSCI_VERSION, for all the hypercall services available.These firmware registers are categorized based on the service call owners, and unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by the corresponding registers. It's expected that the VMMs discover the features provided by each register via
GET_ONE_REG, and writeback the desired values usingSET_ONE_REG. KVM allows this modification only until the VM has started.Older userspace code can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
In this patch, the framework adds the register only for ARM's standard secure services (owner value 4). Currently, this includes support only for ARM True Random Number Generator (TRNG) service, with bit-0 of the register representing mandatory features of v1.0. The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM. Other services are momentarily added in the upcoming patches.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 12 +++++ arch/arm64/include/uapi/asm/kvm.h | 8 ++++ arch/arm64/kvm/arm.c | 8 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 78 +++++++++++++++++++++++++++++++ include/kvm/arm_hypercalls.h | 4 ++ 6 files changed, 111 insertions(+)arch/arm64/include/asm/kvm_host.h +0
-0@@ -101,6 +101,15 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { };Quoted 11 lines
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 109
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6
-0+/** +* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor +* +* @hvc_std_bmap: Bitmap of standard secure service calls +*/ +struct kvm_hvc_desc {nit: maybe call this structure
kvm_hypercall_features? When nested comes along guests will need to use the SVC conduit as HVC traps are always taken to EL2. Same will need to be true for virtual EL2.Sure, I can rename it to be more generic.
Quoted 24 lines
Quoted 24 lines
arch/arm64/include/asm/kvm_host.h +6
-0+u64 hvc_std_bmap; +}; +struct kvm_arch {struct kvm_s2_mmu mmu;@@ -142,6 +151,9 @@ struct kvm_arch {/* Capture first run of the VM */ bool has_run_once; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -287,6 +287,14 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED 3#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED (1U << 4)Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 291
Quoted 2 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0+/* Bitmap firmware registers, extension to the existing psuedo-register space */ +#define KVM_REG_ARM_FW_BMAP KVM_REG_ARM_FW_REG(0xff00)What is the motivation for moving the bitmap register indices so far away from the rest of the firmware regs?
Quoted 6 lines
The original motivation to create a sub-space came from Reiji's comment on v3 [1] so that user-space can distinguish between bitmapped and regular fw registers.
As with the spacing, I thought a 50/50 split would do a good job of avoiding collisions. Do you have any recommendations here?
Quoted 9 lines
I see. This is for the sake of ABI stability with future expansion, right? A new register could be added in the future that controls more SMCCC features, and we expect userspace to zero them if it cares about ABI stability.
If that is all true, we probably need some strong supporting documentation. Additionally, using a new COPROC value for the register range might be better than partitioning the existing FW reg range.
I assumed the 50/50 split could be fine even for future expansion, but I can go for a new COPROC value. However, wouldn't the same problem exist even with that? We could never have enough space :)
Quoted 22 lines
Quoted 16 lines
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 292
Quoted 1 line
arch/arm64/include/uapi/asm/kvm.h +1
-0+#define KVM_REG_ARM_FW_BMAP_REG (r) (KVM_REG_ARM_FW_BMAP | (r))If you are still going to use the index offset, just pass 'r' through to the other macro:
#define KVM_REG_ARM_FW_BMAP_REG(r) KVM_REG_ARM_FW_REG(0xff00 + r)I'm sorry, what's the advantage of doing this?
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 296
Quoted 3 lines
arch/arm64/include/uapi/asm/kvm.h +3
-0+#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_BMAP_REG(0) +#define KVM_REG_ARM_STD_BIT_TRNG_V1_0 BIT(0) +#define KVM_REG_ARM_STD_BMAP_BIT_MAX 0/* Last valid bit */ Implementation details such as this probably shouldn't live in UAPI headers. We'll likely need to bump the value in the future.
Quoted 5 lines
Wouldn't the macros act as helpers to userspace as well?
I agree with your point about the limited spacing, and we may need to expand/shrink as needed, but isn't that a general register-space problem?
Quoted 6 lines
I think that the kernel's responsibility to userspace starts and ends with enumerating the register bits. Those values remain invariant on future KVM. On the other hand, you'll need to bump the
_MAXvalue every time you add to the register. Between documentation and bit definitions, userspace should have enough info to work out the rest on its own :)
Understood. I'll move the _MAX definitions to the kernel-only headers.
Quoted 94 lines
Quoted 31 lines
Quoted 20 lines
Quoted 20 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0+/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/arm.c +1
-0@@ -156,6 +156,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus =kvm_arm_default_max_vcpus ();set_default_spectre (kvm); +kvm_arm_init_hypercalls (kvm);return ret; out_free_stage2_pgd:@@ -635,7 +636,14 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu);Quoted 11 lines
arch/arm64/kvm/arm.c int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) · line 646
Quoted 8 lines
arch/arm64/kvm/arm.c +7
-0+/* +* Grab kvm->lock such that the reader of has_run_once can finish +* the necessary operation atomically, such as deciding whether to +* block the writes to the firmware registers if the VM has run once. +*/ +mutex_lock (&kvm->lock); kvm->arch.has_run_once = true; +mutex_unlock (&kvm->lock);Shouldn't this have just grabbed the kvm lock in patch 04/13?
Quoted 3 lines
It could have, and I agree it's a little igly. But thinking that there's are no parallel threads/consumers when it was initially implemented gave me no reason to grab the locks.
I see, I was just curious if there was something I had missed.
Quoted 51 lines
Quoted 47 lines
Quoted 32 lines
arch/arm64/kvm/arm.c +0
-0return ret; }arch/arm64/kvm/guest.c +1
-0@@ -65,6 +65,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , +KVM_REG_ARM_STD_BMAP , };/** arch/arm64/kvm/hypercalls.c +10
-0@@ -58,6 +58,29 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) val[3 ] =lower_32_bits (cycles); } +static bool kvm_arm_fw_reg_feat_enabled (u64 reg_bmap,u64 feat_bit) +{ +return reg_bmap & feat_bit; +} + +static bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id) +{ +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc; + +switch (func_id) {arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 80
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+case ARM_SMCCC_TRNG_VERSION : +case ARM_SMCCC_TRNG_FEATURES : +case ARM_SMCCC_TRNG_GET_UUID : +case ARM_SMCCC_TRNG_RND32 : +case ARM_SMCCC_TRNG_RND64 : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_bmap, +KVM_REG_ARM_STD_BIT_TRNG_V1_0 ); +default : +/* By default, allow the services that aren't listed here */ +return true;I think your default case should really return false. It keeps people honest when they add new patches to set up a new hypercall bit (no bit? no call!)
Quoted 4 lines
That of course requires that you only return false once all of the preexisting hypercalls are enumerated, otherwise such a patch would cause a regression in isolation.
Quoted 5 lines
I agree, but not all hypercalls may have a bit. For example, guarding
ARM_SMCCC_VERSION_FUNC_IDdoesn't make sense and may not be gated by the bit. Hence, the idea was to do an initial check if thefunc_idis in fact gated. If yes, check the bit, else let the actual implementation take care of it.
Quoted 6 lines
The set of hypercalls that are not under userspace control need to exist on an allowlist then. Any new hypercall after the introduction of these registers must have a bit assigned then, right? Otherwise it would seem that new hypercalls could sneak into the guest ABI when migrating to an upgraded kernel.
Ah, I see your point. I agree, I can create an allowed-list of the func_ids that are not under user-space's control.
Thanks for your review.
Regards,
Raghavendra
Quoted 165 lines
Quoted 97 lines
Quoted 63 lines
Quoted 63 lines
arch/arm64/kvm/hypercalls.c +18
-0+} +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) {u32 func_id =smccc_get_function (vcpu);@@ -65,6 +88,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)u32 feature;gpa_t gpa; +if (!kvm_hvc_call_supported (vcpu, func_id)) + goto out; +switch (func_id) {case ARM_SMCCC_VERSION_FUNC_ID : val[0 ] =ARM_SMCCC_VERSION_1_1 ;@@ -143,6 +169,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)return kvm_psci_call (vcpu); } +out:smccc_set_retval (vcpu, val[0 ], val[1 ], val[2 ], val[3 ]);return 1 ; }@@ -151,8 +178,16 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_PSCI_VERSION ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1 ,KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 , + KVM_REG_ARM_STD_BMAP, }; +void kvm_arm_init_hypercalls (struct kvm *kvm) +{ +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; + + hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) {return ARRAY_SIZE (kvm_arm_fw_reg_ids);@@ -220,6 +255,7 @@ static int get_kernel_wa_level(u64 regid)int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) { +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;void __user *uaddr = (void __user *)(long )reg->addr;u64 val, reg_id = reg->id;@@ -233,6 +269,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 : val =get_kernel_wa_level (reg_id) &KVM_REG_FEATURE_LEVEL_MASK ;break ; +case KVM_REG_ARM_STD_BMAP : + val =READ_ONCE (hvc_desc->hvc_std_bmap); +break ;default :return -ENOENT ; }@@ -243,6 +282,43 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ; }Quoted 10 lines
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 290
Quoted 6 lines
arch/arm64/kvm/hypercalls.c +6
-0+static int kvm_arm_set_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 reg_id,u64 val) +{ +int ret =0 ; +struct kvm *kvm = vcpu->kvm; +struct kvm_hvc_desc *hvc_desc = &kvm->arch.hvc_desc; +u64 *fw_reg_bmap, fw_reg_features;nit: use reverse fir tree ordering for locals (longest line first, shortest last).
Got it.
Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/hypercalls.c +14
-0+switch (reg_id) { +case KVM_REG_ARM_STD_BMAP : + fw_reg_bmap = &hvc_desc->hvc_std_bmap; + fw_reg_features =ARM_SMCCC_STD_FEATURES ; +break ; +default : +return -ENOENT ; + } + +/* Check for unsupported bit */ +if (val & ~fw_reg_features) +return -EINVAL ; + +mutex_lock (&kvm->lock);Quoted 9 lines
arch/arm64/kvm/hypercalls.c int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 309
Quoted 4 lines
arch/arm64/kvm/hypercalls.c +4
-0+ +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. How about returning
-EINVALinstead? We already do this forKVM_ARM_VCPU_INITif userspace uses a different target than the one previously set.Quoted 5 lines
Returning EBUSY could help userspace distinguish the cause of failure better as compared to EINVAL, since technically, the arguments supplied by the user are in fact valid. It's just that the timing is wrong.
Good point :-) Also, this matches the PMU event filter handling as well. Apologies.
Quoted 59 lines
Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/hypercalls.c +14
-0+*/ +if (kvm_arm_vm_has_run_once (&kvm->arch)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + +WRITE_ONCE (*fw_reg_bmap, val); +out: +mutex_unlock (&kvm->lock); +return ret; +} +int kvm_arm_set_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr;@@ -321,6 +397,8 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return -EINVAL ;return 0 ; +case KVM_REG_ARM_STD_BMAP : +return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ; }include/kvm/arm_hypercalls.h +0
-0@@ -6,6 +6,9 @@Quoted 8 lines
include/kvm/arm_hypercalls.h line 11
Quoted 5 lines
include/kvm/arm_hypercalls.h +3
-0#include <asm/kvm_emulate.h> +#define ARM_SMCCC_STD_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_STD_BMAP_BIT_MAX, 0) +This probably needs
KVM_somewhere in its name for the sake of scoping.RIght, I can append that.
Quoted 7 lines
Quoted 7 lines
include/kvm/arm_hypercalls.h +0
-0int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu)@@ -42,6 +45,7 @@ static inline void smccc_set_retval(struct kvm_vcpu *vcpu,struct kvm_one_reg ;Quoted 7 lines
Quoted 7 lines
include/kvm/arm_hypercalls.h +1
-1+void kvm_arm_init_hypercalls (struct kvm *kvm);int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu);int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices);int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); --2.35.1.473.g83b2b277ed-goog
Regards,
Raghavendra
Re: [PATCH v4 05/13] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Quoted 117 lines · Raghavendra Rao Ananta
Quoted 116 lines · Oliver Upton
Quoted 102 lines · Raghavendra Rao Ananta
Hi Oliver,
Quoted 56 lines · Oliver Upton
Quoted 54 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the userspace. This means, the guests can observe hypercall services in and out as they migrate across various host kernel versions. This could be a major problem if the guest discovered a hypercall, started using it, and after getting migrated to an older kernel realizes that it's no longer available. Depending on how the guest handles the change, there's a potential chance that the guest would just panic.
As a result, there's a need for the userspace to elect the services that it wishes the guest to discover. It can elect these services based on the kernels spread across its (migration) fleet. To remedy this, extend the existing firmware psuedo-registers, such as
KVM_REG_ARM_PSCI_VERSION, for all the hypercall services available.These firmware registers are categorized based on the service call owners, and unlike the existing firmware psuedo-registers, they hold the features supported in the form of a bitmap.
During the VM initialization, the registers holds an upper-limit of the features supported by the corresponding registers. It's expected that the VMMs discover the features provided by each register via
GET_ONE_REG, and writeback the desired values usingSET_ONE_REG. KVM allows this modification only until the VM has started.Older userspace code can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
In this patch, the framework adds the register only for ARM's standard secure services (owner value 4). Currently, this includes support only for ARM True Random Number Generator (TRNG) service, with bit-0 of the register representing mandatory features of v1.0. The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM. Other services are momentarily added in the upcoming patches.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 12 +++++ arch/arm64/include/uapi/asm/kvm.h | 8 ++++ arch/arm64/kvm/arm.c | 8 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 78 +++++++++++++++++++++++++++++++ include/kvm/arm_hypercalls.h | 4 ++ 6 files changed, 111 insertions(+)arch/arm64/include/asm/kvm_host.h +0
-0@@ -101,6 +101,15 @@ struct kvm_s2_mmu {struct kvm_arch_memory_slot { };Quoted 11 lines
arch/arm64/include/asm/kvm_host.h struct kvm_s2_mmu { · line 109
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +6
-0+/** +* struct kvm_hvc_desc: KVM ARM64 hypercall descriptor +* +* @hvc_std_bmap: Bitmap of standard secure service calls +*/ +struct kvm_hvc_desc {nit: maybe call this structure
kvm_hypercall_features? When nested comes along guests will need to use the SVC conduit as HVC traps are always taken to EL2. Same will need to be true for virtual EL2.Sure, I can rename it to be more generic.
Quoted 24 lines
Quoted 24 lines
arch/arm64/include/asm/kvm_host.h +6
-0+u64 hvc_std_bmap; +}; +struct kvm_arch {struct kvm_s2_mmu mmu;@@ -142,6 +151,9 @@ struct kvm_arch {/* Capture first run of the VM */ bool has_run_once; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +0
-0@@ -287,6 +287,14 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED 3#define KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED (1U << 4)Quoted 6 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 291
Quoted 2 lines
arch/arm64/include/uapi/asm/kvm.h +2
-0+/* Bitmap firmware registers, extension to the existing psuedo-register space */ +#define KVM_REG_ARM_FW_BMAP KVM_REG_ARM_FW_REG(0xff00)What is the motivation for moving the bitmap register indices so far away from the rest of the firmware regs?
Quoted 6 lines
The original motivation to create a sub-space came from Reiji's comment on v3 [1] so that user-space can distinguish between bitmapped and regular fw registers.
As with the spacing, I thought a 50/50 split would do a good job of avoiding collisions. Do you have any recommendations here?I see. This is for the sake of ABI stability with future expansion, right? A new register could be added in the future that controls more SMCCC features, and we expect userspace to zero them if it cares about ABI stability.
Quoted 7 lines
Quoted 4 lines
If that is all true, we probably need some strong supporting documentation. Additionally, using a new COPROC value for the register range might be better than partitioning the existing FW reg range.
I assumed the 50/50 split could be fine even for future expansion, but I can go for a new COPROC value. However, wouldn't the same problem exist even with that? We could never have enough space :)
Of course, but I think the UAPI is consistent if you use a new COPROC value for the bitmaps. That way, you can add documentation that covers the entire COPROC value you've selected, and doesn't require any further twiddling with an existing register range. It seems that we have plenty of COPROC values that are available as well.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 292
Quoted 1 line
arch/arm64/include/uapi/asm/kvm.h +1
-0+#define KVM_REG_ARM_FW_BMAP_REG (r) (KVM_REG_ARM_FW_BMAP | (r))If you are still going to use the index offset, just pass 'r' through to the other macro:
#define KVM_REG_ARM_FW_BMAP_REG(r) KVM_REG_ARM_FW_REG(0xff00 + r)I'm sorry, what's the advantage of doing this?
Just a style nit :)
--
Thanks,
Oliver
Re: [PATCH v4 07/13] KVM: arm64: Add vendor hypervisor firmware register
In reply to Oliver UptonQuoted 113 lines · Oliver Upton
Quoted 103 lines · Raghavendra Rao Ananta
Quoted 98 lines · Oliver Upton
Quoted 80 lines · Raghavendra Rao Ananta
Introduce the firmware register to hold the vendor specific hypervisor service calls (owner value 6) as a bitmap. The bitmap represents the features that'll be enabled for the guest, as configured by the user-space. Currently, this includes support only for Precision Time Protocol (PTP), represented by bit-0.
The register is also added to the kvm_arm_vm_scope_fw_regs[] list as it maintains its state per-VM.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/guest.c | 1 + arch/arm64/kvm/hypercalls.c | 22 +++++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -106,10 +106,12 @@ struct kvm_arch_memory_slot {* * @hvc_std_bmap: Bitmap of standard secure service calls * @hvc_std_hyp_bmap: Bitmap of standard hypervisor service calls +* @hvc_vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls */ struct kvm_hvc_desc {u64 hvc_std_bmap;u64 hvc_std_hyp_bmap; +u64 hvc_vendor_hyp_bmap; };struct kvm_arch {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -299,6 +299,10 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_STD_HYP_BIT_PV_TIME BIT(0)#define KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +#define KVM_REG_ARM_VENDOR_HYP_BMAP KVM_REG_ARM_FW_BMAP_REG(2) +#define KVM_REG_ARM_VENDOR_HYP_BIT_PTP BIT(0) +#define KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX 0/* Last valid bit */ +/* SVE registers */ #define KVM_REG_ARM64_SVE (0x15 << KVM_REG_ARM_COPROC_SHIFT)arch/arm64/kvm/guest.c +1
-0@@ -67,6 +67,7 @@ static const u64 kvm_arm_vm_scope_fw_regs[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };/** arch/arm64/kvm/hypercalls.c +3
-0@@ -79,6 +79,9 @@ static bool kvm_hvc_call_supported(struct kvm_vcpu *vcpu, u32 func_id)case ARM_SMCCC_HV_PV_TIME_ST :return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_std_hyp_bmap,KVM_REG_ARM_STD_HYP_BIT_PV_TIME ); +case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID : +return kvm_arm_fw_reg_feat_enabled (hvc_desc->hvc_vendor_hyp_bmap, +KVM_REG_ARM_VENDOR_HYP_BIT_PTP );default :/* By default, allow the services that aren't listed here */ return true;@@ -162,7 +165,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID :arch/arm64/kvm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 175
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +8
-1val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); - val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_PTP ); + +/* +* The feature bits exposed to user-space doesn't include +* ARM_SMCCC_KVM_FUNC_FEATURES. However, we expose this to +* the guest as bit-0. Hence, left-shift the user-space +* exposed bitmap by 1 to accommodate this. +*/ + val[0 ] |= hvc_desc->hvc_vendor_hyp_bmap <<1 ;Having an off-by-one difference between the userspace and guest representations of this bitmap seems like it could be a source of bugs in the future. Its also impossible for the guest to completely hide the vendor range if it so chooses.
Quoted 4 lines
Why not tie
ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_IDandARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_IDto BIT(0)? PTP would then become BIT(1).Quoted 7 lines
I agree it's a little asymmetrical. But exposing a bit for the
func_idsthat you mentioned means providing a capability to disable them by the userspace. This would block the guests from even discovering the space. If it's not too ugly, we can maintain certain bits to always remain read-only to the user-space. On the other hand, we can simply ignore what the userspace configure and simply treat it as a userspace bug. What do you think?
Quoted 7 lines
I think that assigning a bit to the aforementioned hypercalls would be best. If userspace decides to hide all the features enumerated in the subrange then there isn't much point to the guest knowing that the range even exists. It shouldn't amount to much for userspace, as it will likely just keep the default value and only worry about these registers when migrating.
Sure, I'll include a bit for these as well.
Thanks,
Raghavenadra
Quoted 71 lines
Apologies if I'm being pedantic, but such a subtle implementation detail could be overlooked in future changes.
--
OliverQuoted 65 lines
Quoted 65 lines
Quoted 65 lines
arch/arm64/kvm/hypercalls.c +10
-0break ;case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID :kvm_ptp_get_time (vcpu, val);@@ -188,6 +198,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2 ,KVM_REG_ARM_STD_BMAP ,KVM_REG_ARM_STD_HYP_BMAP , + KVM_REG_ARM_VENDOR_HYP_BMAP, };void kvm_arm_init_hypercalls (struct kvm *kvm)@@ -196,6 +207,7 @@ void kvm_arm_init_hypercalls(struct kvm *kvm) hvc_desc->hvc_std_bmap =ARM_SMCCC_STD_FEATURES ; hvc_desc->hvc_std_hyp_bmap =ARM_SMCCC_STD_HYP_FEATURES ; + hvc_desc->hvc_vendor_hyp_bmap =ARM_SMCCC_VENDOR_HYP_FEATURES ; }int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu)@@ -285,6 +297,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_HYP_BMAP : val =READ_ONCE (hvc_desc->hvc_std_hyp_bmap);break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + val =READ_ONCE (hvc_desc->hvc_vendor_hyp_bmap); +break ;default :return -ENOENT ; }@@ -311,6 +326,10 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val) fw_reg_bmap = &hvc_desc->hvc_std_hyp_bmap; fw_reg_features =ARM_SMCCC_STD_HYP_FEATURES ;break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : + fw_reg_bmap = &hvc_desc->hvc_vendor_hyp_bmap; + fw_reg_features =ARM_SMCCC_VENDOR_HYP_FEATURES ; +break ;default :return -ENOENT ; }@@ -416,6 +435,7 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)return 0 ;case KVM_REG_ARM_STD_BMAP :case KVM_REG_ARM_STD_HYP_BMAP : +case KVM_REG_ARM_VENDOR_HYP_BMAP :return kvm_arm_set_fw_reg_bmap (vcpu, reg_id, val);default :return -ENOENT ;include/kvm/arm_hypercalls.h +3
-1@@ -12,6 +12,9 @@#define ARM_SMCCC_STD_HYP_FEATURES \ GENMASK_ULL(KVM_REG_ARM_STD_HYP_BMAP_BIT_MAX, 0) +#define ARM_SMCCC_VENDOR_HYP_FEATURES \ + GENMASK_ULL(KVM_REG_ARM_VENDOR_HYP_BMAP_BIT_MAX, 0) +int kvm_hvc_call_handler (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) --2.35.1.473.g83b2b277ed-goog