KVM: arm64: Add support for hypercall services selection
Subsystems (5)
v3 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.
The capability, KVM_CAP_ARM_HVC_FW_REG_BMAP, is introduced to announce this extension, which returns the number of psuedo-firmware registers supported. During the VM initialization, the registers holds an upper-limit of the features supported by each one of them. 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 VMMs can simply ignore the capability and the hypercall services will be exposed unconditionally to the guests, thus ensuring backward compatibility.
The patches are based off of mainline kernel 5.16-rc8, with the selftest patches from [2] applied.
Patch-1 tracks if the VM has started running, which would be used in the upcoming patches.
Patch-2 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-3 introduces the KVM_CAP_ARM_HVC_FW_REG_BMAP capability definition.
Patch-4 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-5 introduces the firmware register, KVM_REG_ARM_STD_HYP_BMAP, which holds the standard hypervisor services (such as PV_TIME).
Patch-6 introduces the firmware register, KVM_REG_ARM_VENDOR_HYP_BMAP, which holds the vendor specific hypercall services.
Patch-7,8 Add the necessary documentation for the newly added firmware registers.
Patch-9 imports the SMCCC definitions from linux/arm-smccc.h into tools/ for further use in selftests.
Patch-10 adds the selftest to test the guest (using 'hvc') and VMM interfaces (SET/GET_ONE_REG).
Patch-11 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
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]/
Raghavendra Rao Ananta (11):
KVM: Capture VM start
KVM: arm64: Factor out firmware register handling from psci.c
KVM: Introduce KVM_CAP_ARM_HVC_FW_REG_BMAP
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: Add the bitmap firmware registers to
get-reg-list
Documentation/virt/kvm/api.rst | 21 + Documentation/virt/kvm/arm/hypercalls.rst | 128 +++++++ Documentation/virt/kvm/arm/psci.rst | 77 ---- arch/arm64/include/asm/kvm_host.h | 16 + arch/arm64/include/uapi/asm/kvm.h | 12 + arch/arm64/kvm/arm.c | 4 + arch/arm64/kvm/guest.c | 2 +- arch/arm64/kvm/hypercalls.c | 311 ++++++++++++++- arch/arm64/kvm/psci.c | 166 -------- arch/arm64/kvm/pvtime.c | 3 + arch/arm64/kvm/trng.c | 8 +- include/kvm/arm_hypercalls.h | 19 + include/kvm/arm_psci.h | 7 - include/linux/kvm_host.h | 3 + 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 | 358 ++++++++++++++++++ virt/kvm/kvm_main.c | 9 + 21 files changed, 1078 insertions(+), 260 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.34.1.448.ga2b2bfdf31-goog
RFC PATCH v3 1/11 KVM: Capture VM start
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
--- include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 9 +++++++++ 2 files changed, 12 insertions(+)
include/linux/kvm_host.h +3 -0
@@ -623,6 +623,7 @@ struct kvm {struct notifier_block pm_notifier;#endif char stats_id[KVM_STATS_NAME_SIZE ]; +bool vm_started; };#define kvm_err (fmt, ...) \
@@ -1666,6 +1667,8 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) } } +#define kvm_vm_has_started (kvm) (kvm->vm_started) +extern bool kvm_rebooting;extern unsigned int halt_poll_ns;
virt/kvm/kvm_main.c +9 -0
@@ -3686,6 +3686,7 @@ static long kvm_vcpu_ioctl(struct file *filp,int r;struct kvm_fpu *fpu =NULL ;struct kvm_sregs *kvm_sregs =NULL ; +struct kvm *kvm = vcpu->kvm;if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)return -EIO ;
@@ -3723,6 +3724,14 @@ static long kvm_vcpu_ioctl(struct file *filp,if (oldpid)synchronize_rcu ();put_pid (oldpid); + +/* +* Since we land here even on the first vCPU run, +* we can mark that the VM has started running. +*/ +mutex_lock (&kvm->lock); + kvm->vm_started = true; +mutex_unlock (&kvm->lock); } r =kvm_arch_vcpu_ioctl_run (vcpu);trace_kvm_userspace_exit (vcpu->run->exit_reason, r);
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Hi Raghu,
Quoted 18 lines · Raghavendra Rao Ananta
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 9 +++++++++ 2 files changed, 12 insertions(+)include/linux/kvm_host.h +0
-0
include/linux/kvm_host.h struct kvm { · line 626
Quoted 4 lines
include/linux/kvm_host.h +1
-0@@ -623,6 +623,7 @@ struct kvm {struct notifier_block pm_notifier;#endif char stats_id[KVM_STATS_NAME_SIZE ]; +bool vm_started;
Since KVM_RUN on any vCPUs doesn't necessarily mean that the VM started yet, the name might be a bit misleading IMHO. I would think 'has_run_once' or 'ran_once' might be more clear (?).
Quoted 25 lines
include/linux/kvm_host.h +2
-0};#define kvm_err (fmt, ...) \@@ -1666,6 +1667,8 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) } } +#define kvm_vm_has_started (kvm) (kvm->vm_started) +extern bool kvm_rebooting;extern unsigned int halt_poll_ns;virt/kvm/kvm_main.c +1
-0@@ -3686,6 +3686,7 @@ static long kvm_vcpu_ioctl(struct file *filp,int r;struct kvm_fpu *fpu =NULL ;struct kvm_sregs *kvm_sregs =NULL ; +struct kvm *kvm = vcpu->kvm;if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)return -EIO ;
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3731
Quoted 8 lines
virt/kvm/kvm_main.c +5
-0@@ -3723,6 +3724,14 @@ static long kvm_vcpu_ioctl(struct file *filp,if (oldpid)synchronize_rcu ();put_pid (oldpid); + +/* +* Since we land here even on the first vCPU run, +* we can mark that the VM has started running. +*/
It might be nicer to add a comment why the code below gets kvm->lock.
Anyway, the patch generally looks good to me, and thank you for making this change (it works for my purpose as well).
Thanks,
Reiji
Quoted 9 lines
virt/kvm/kvm_main.c +3
-1+mutex_lock (&kvm->lock); + kvm->vm_started = true; +mutex_unlock (&kvm->lock); } r =kvm_arch_vcpu_ioctl_run (vcpu);trace_kvm_userspace_exit (vcpu->run->exit_reason, r); --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Hi Reiji,
Quoted 18 lines · Reiji Watanabe
Hi Raghu,
Quoted 13 lines · Raghavendra Rao Ananta
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 9 +++++++++ 2 files changed, 12 insertions(+)
Quoted 14 lines
include/linux/kvm_host.h struct kvm { · line 626
Quoted 9 lines
include/linux/kvm_host.h +1
-0@@ -623,6 +623,7 @@ struct kvm {struct notifier_block pm_notifier;#endif char stats_id[KVM_STATS_NAME_SIZE ]; +bool vm_started;Since
KVM_RUNon any vCPUs doesn't necessarily mean that the VM started yet, the name might be a bit misleading IMHO. I would think 'has_run_once' or 'ran_once' might be more clear (?).
I always struggle with the names; but if you feel that 'ran_once' makes more sense for a reader, I can change it.
Quoted 23 lines
Quoted 23 lines
include/linux/kvm_host.h +2
-0};#define kvm_err (fmt, ...) \@@ -1666,6 +1667,8 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) } } +#define kvm_vm_has_started (kvm) (kvm->vm_started) +extern bool kvm_rebooting;extern unsigned int halt_poll_ns;virt/kvm/kvm_main.c +1
-0@@ -3686,6 +3686,7 @@ static long kvm_vcpu_ioctl(struct file *filp,int r;struct kvm_fpu *fpu =NULL ;struct kvm_sregs *kvm_sregs =NULL ; +struct kvm *kvm = vcpu->kvm;
Quoted 14 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3731
Quoted 11 lines
virt/kvm/kvm_main.c +5
-0if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)return -EIO ;@@ -3723,6 +3724,14 @@ static long kvm_vcpu_ioctl(struct file *filp,if (oldpid)synchronize_rcu ();put_pid (oldpid); + +/* +* Since we land here even on the first vCPU run, +* we can mark that the VM has started running. +*/ It might be nicer to add a comment why the code below gets
kvm->lock.
I've been going back and forth on this one. Initially I considered simply going with atomic_t, but the patch 4/11 (KVM: arm64: Setup a framework for hypercall bitmap firmware registers) kvm_arm_set_fw_reg_bmap()'s implementation felt like we need a lock to have the whole 'is the register busy?' operation atomic. But, that's just one of the applications.
Quoted 5 lines
Anyway, the patch generally looks good to me, and thank you for making this change (it works for my purpose as well).
Reviewed-by Reiji Watanabe <[email protected]>
Glad that it's helping you as well and thanks for the review.
Regards,
Raghavendra
Quoted 13 lines
Thanks,
ReijiQuoted 9 lines
virt/kvm/kvm_main.c +3
-1+mutex_lock (&kvm->lock); + kvm->vm_started = true; +mutex_unlock (&kvm->lock); } r =kvm_arch_vcpu_ioctl_run (vcpu);trace_kvm_userspace_exit (vcpu->run->exit_reason, r); --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 15 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 11 lines · Reiji Watanabe
Hi Raghu,
Quoted 6 lines · Raghavendra Rao Ananta
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
What about live migration, where the VM has already technically been started before the first call to KVM_RUN?
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 13 lines · Jim Mattson
Quoted 10 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Hi Raghu,
Quoted 1 line · Raghavendra Rao Ananta
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
What about live migration, where the VM has already technically been started before the first call to
KVM_RUN?
My understanding is that a new 'struct kvm' is created on the target machine and this flag should be reset, which would allow the VMM to restore the firmware registers. However, we would be running KVM_RUN for the first time on the target machine, thus setting the flag. So, you are right; It's more of a resume operation from the guest's point of view. I guess the name of the variable is what's confusing here.
Thanks,
Raghavendra
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 24 lines · Raghavendra Rao Ananta
Quoted 13 lines · Jim Mattson
Quoted 10 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Hi Raghu,
Quoted 1 line · Raghavendra Rao Ananta
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
What about live migration, where the VM has already technically been started before the first call to
KVM_RUN?
Quoted 7 lines
My understanding is that a new 'struct kvm' is created on the target machine and this flag should be reset, which would allow the VMM to restore the firmware registers. However, we would be running
KVM_RUNfor the first time on the target machine, thus setting the flag. So, you are right; It's more of a resume operation from the guest's point of view. I guess the name of the variable is what's confusing here.
I was actually thinking that live migration gives userspace an easy way to circumvent your restriction. You said, "This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running." However, if you don't ensure that these VM features are configured the same way on the target machine as they were on the source machine, you have not actually accomplished your stated goal.
Quoted 2 lines
Thanks,
Raghavendra
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 35 lines · Jim Mattson
Quoted 24 lines · Raghavendra Rao Ananta
Quoted 13 lines · Jim Mattson
Quoted 10 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Hi Raghu,
Quoted 1 line · Raghavendra Rao Ananta
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
What about live migration, where the VM has already technically been started before the first call to
KVM_RUN?Quoted 7 lines
My understanding is that a new 'struct kvm' is created on the target machine and this flag should be reset, which would allow the VMM to restore the firmware registers. However, we would be running
KVM_RUNfor the first time on the target machine, thus setting the flag. So, you are right; It's more of a resume operation from the guest's point of view. I guess the name of the variable is what's confusing here.
Quoted 8 lines
I was actually thinking that live migration gives userspace an easy way to circumvent your restriction. You said, "This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running." However, if you don't ensure that these VM features are configured the same way on the target machine as they were on the source machine, you have not actually accomplished your stated goal.
Isn't that up to the VMM to save/restore and validate the registers across migrations?
Perhaps I have to re-word my intentions for the patch- userspace should be able to configure the registers before issuing the first KVM_RUN.
Thanks,
Raghavendra
Quoted 2 lines
Quoted 2 lines
Thanks,
Raghavendra
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 45 lines · Raghavendra Rao Ananta
Quoted 35 lines · Jim Mattson
Quoted 24 lines · Raghavendra Rao Ananta
Quoted 13 lines · Jim Mattson
Quoted 10 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Hi Raghu,
Quoted 1 line · Raghavendra Rao Ananta
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
What about live migration, where the VM has already technically been started before the first call to
KVM_RUN?Quoted 7 lines
My understanding is that a new 'struct kvm' is created on the target machine and this flag should be reset, which would allow the VMM to restore the firmware registers. However, we would be running
KVM_RUNfor the first time on the target machine, thus setting the flag. So, you are right; It's more of a resume operation from the guest's point of view. I guess the name of the variable is what's confusing here.Quoted 8 lines
I was actually thinking that live migration gives userspace an easy way to circumvent your restriction. You said, "This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running." However, if you don't ensure that these VM features are configured the same way on the target machine as they were on the source machine, you have not actually accomplished your stated goal.
Quoted 2 lines
Isn't that up to the VMM to save/restore and validate the registers across migrations?
Yes, just as it is up to userspace not to make bad configuration changes after the first VMRUN.
Quoted 3 lines
Perhaps I have to re-word my intentions for the patch- userspace should be able to configure the registers before issuing the first
KVM_RUN.
Perhaps it would help if you explained *why* you are doing this. It sounds like you are either trying to protect against a malicious userspace, or you are trying to keep userspace from doing something stupid. In general, kvm only enforces constraints that are necessary to protect the host. If that's what you're doing, I don't understand why live migration doesn't provide an end-run around your protections.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 58 lines · Jim Mattson
Quoted 45 lines · Raghavendra Rao Ananta
Quoted 35 lines · Jim Mattson
Quoted 24 lines · Raghavendra Rao Ananta
Quoted 13 lines · Jim Mattson
Quoted 10 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 6 lines · Reiji Watanabe
Hi Raghu,
Quoted 1 line · Raghavendra Rao Ananta
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
What about live migration, where the VM has already technically been started before the first call to
KVM_RUN?Quoted 7 lines
My understanding is that a new 'struct kvm' is created on the target machine and this flag should be reset, which would allow the VMM to restore the firmware registers. However, we would be running
KVM_RUNfor the first time on the target machine, thus setting the flag. So, you are right; It's more of a resume operation from the guest's point of view. I guess the name of the variable is what's confusing here.Quoted 8 lines
I was actually thinking that live migration gives userspace an easy way to circumvent your restriction. You said, "This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running." However, if you don't ensure that these VM features are configured the same way on the target machine as they were on the source machine, you have not actually accomplished your stated goal.
Quoted 2 lines
Isn't that up to the VMM to save/restore and validate the registers across migrations?
Yes, just as it is up to userspace not to make bad configuration changes after the first VMRUN.
Quoted 3 lines
Perhaps I have to re-word my intentions for the patch- userspace should be able to configure the registers before issuing the first
KVM_RUN.
Quoted 6 lines
Perhaps it would help if you explained *why* you are doing this. It sounds like you are either trying to protect against a malicious userspace, or you are trying to keep userspace from doing something stupid. In general, kvm only enforces constraints that are necessary to protect the host. If that's what you're doing, I don't understand why live migration doesn't provide an end-run around your protections.
It's mainly to safeguard the guests. With respect to migration, KVM and the userspace are collectively playing a role here. It's up to the userspace to ensure that the registers are configured the same across migrations and KVM ensures that the userspace doesn't modify the registers after KVM_RUN so that they don't see features turned OFF/ON during execution. I'm not sure if it falls into the definition of protecting the host. Do you see a value in adding this extra protection from KVM?
Regards,
Raghavendra
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 7 lines · Raghavendra Rao Ananta
Quoted 6 lines · Jim Mattson
Perhaps it would help if you explained *why* you are doing this. It sounds like you are either trying to protect against a malicious userspace, or you are trying to keep userspace from doing something stupid. In general, kvm only enforces constraints that are necessary to protect the host. If that's what you're doing, I don't understand why live migration doesn't provide an end-run around your protections.
Quoted 8 lines
It's mainly to safeguard the guests. With respect to migration, KVM and the userspace are collectively playing a role here. It's up to the userspace to ensure that the registers are configured the same across migrations and KVM ensures that the userspace doesn't modify the registers after
KVM_RUNso that they don't see features turned OFF/ON during execution. I'm not sure if it falls into the definition of protecting the host. Do you see a value in adding this extra protection from KVM?
Short answer: probably not?
There is precedent for disallowing userspace from doing stupid things, but that's either for KVM's protection (as Jim pointed out), or because KVM can't honor the change, e.g. x86 is currently in the process of disallowing most CPUID changes after KVM_RUN because KVM itself consumes the CPUID information and KVM doesn't support updating some of it's own internal state (because removing features like GB hugepage support is nonsensical and would require a large pile of complicated, messy code).
Restricing CPUID changes does offer some "protection" to the guest, but that's not the goal. E.g. KVM won't detect CPUID misconfiguration in the migration case, and trying to do so is a fool's errand.
If restricting updates in the arm64 is necessary to ensure KVM provides sane behavior, then it could be justified. But if it's purely a sanity check on behalf of the guest, then it's not justified.
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 28 lines · Sean Christopherson
Quoted 7 lines · Raghavendra Rao Ananta
Quoted 6 lines · Jim Mattson
Perhaps it would help if you explained *why* you are doing this. It sounds like you are either trying to protect against a malicious userspace, or you are trying to keep userspace from doing something stupid. In general, kvm only enforces constraints that are necessary to protect the host. If that's what you're doing, I don't understand why live migration doesn't provide an end-run around your protections.
Quoted 8 lines
It's mainly to safeguard the guests. With respect to migration, KVM and the userspace are collectively playing a role here. It's up to the userspace to ensure that the registers are configured the same across migrations and KVM ensures that the userspace doesn't modify the registers after
KVM_RUNso that they don't see features turned OFF/ON during execution. I'm not sure if it falls into the definition of protecting the host. Do you see a value in adding this extra protection from KVM?Short answer: probably not?
There is precedent for disallowing userspace from doing stupid things, but that's either for KVM's protection (as Jim pointed out), or because KVM can't honor the change, e.g. x86 is currently in the process of disallowing most CPUID changes after
KVM_RUNbecause KVM itself consumes the CPUID information and KVM doesn't support updating some of it's own internal state (because removing features like GB hugepage support is nonsensical and would require a large pile of complicated, messy code).
Quoted 7 lines
Restricing CPUID changes does offer some "protection" to the guest, but that's not the goal. E.g. KVM won't detect CPUID misconfiguration in the migration case, and trying to do so is a fool's errand.
If restricting updates in the arm64 is necessary to ensure KVM provides sane behavior, then it could be justified. But if it's purely a sanity check on behalf of the guest, then it's not justified.
Agreed that KVM doesn't really safeguard the guests, but just curious, is there really a downside in adding this thin layer of safety check? On the bright side, the guests would be safe, and it could save the developers some time in hunting down the bugs in this path, no?
Regards,
Raghavendra
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 6 lines · Raghavendra Rao Ananta
Quoted 3 lines · Sean Christopherson
If restricting updates in the arm64 is necessary to ensure KVM provides sane behavior, then it could be justified. But if it's purely a sanity check on behalf of the guest, then it's not justified.
Agreed that KVM doesn't really safeguard the guests, but just curious, is there really a downside in adding this thin layer of safety check?
It's more stuff that KVM has to maintain, creates an ABI that KVM must adhere to, potentially creates inconsistencies in KVM, and prevents using KVM to intentionally do stupid things to test scenarios that are "impossible". And we also try to avoid defining arbitrary CPU behavior in KVM (that may not be the case here).
Quoted 2 lines
On the bright side, the guests would be safe, and it could save the developers some time in hunting down the bugs in this path, no?
Yes, but that can be said for lots and lots of things. This is both a slippery slope argument and the inconsistency argument above, e.g. if KVM actively prevents userspace from doing X, why doesn't KVM prevent userspace from doing Y? Having a decently defined rule for these types of things, e.g. protect KVM/kernel and adhere to the architecture but otherwise let userspace do whatever, avoids spending too much time arguing over what KVM should/shouldn't allow, or wondering why on earth KVM does XYZ, at least in theory :-)
There are certainly times where KVM could have saved userspace some pain, but overall I do think KVM is better off staying out of the way when possible.
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
In reply to Sean ChristophersonQuoted 28 lines · Sean Christopherson
Quoted 7 lines · Raghavendra Rao Ananta
Quoted 6 lines · Jim Mattson
Perhaps it would help if you explained *why* you are doing this. It sounds like you are either trying to protect against a malicious userspace, or you are trying to keep userspace from doing something stupid. In general, kvm only enforces constraints that are necessary to protect the host. If that's what you're doing, I don't understand why live migration doesn't provide an end-run around your protections.
Quoted 8 lines
It's mainly to safeguard the guests. With respect to migration, KVM and the userspace are collectively playing a role here. It's up to the userspace to ensure that the registers are configured the same across migrations and KVM ensures that the userspace doesn't modify the registers after
KVM_RUNso that they don't see features turned OFF/ON during execution. I'm not sure if it falls into the definition of protecting the host. Do you see a value in adding this extra protection from KVM?Short answer: probably not?
There is precedent for disallowing userspace from doing stupid things, but that's either for KVM's protection (as Jim pointed out), or because KVM can't honor the change, e.g. x86 is currently in the process of disallowing most CPUID changes after
KVM_RUNbecause KVM itself consumes the CPUID information and KVM doesn't support updating some of it's own internal state (because removing features like GB hugepage support is nonsensical and would require a large pile of complicated, messy code).
Quoted 7 lines
Restricing CPUID changes does offer some "protection" to the guest, but that's not the goal. E.g. KVM won't detect CPUID misconfiguration in the migration case, and trying to do so is a fool's errand.
If restricting updates in the arm64 is necessary to ensure KVM provides sane behavior, then it could be justified. But if it's purely a sanity check on behalf of the guest, then it's not justified.
The pseudo firmware hvc registers, which this series are adding, are used by KVM to identify available hvc features for the guest, and not directly exposed to the guest as registers.
The ways the KVM code in the series consumes the registers' values are very limited, and no KVM data/state is created based on their values. But, as the code that consumes the registers grows in the future, I wouldn't be surprised if KVM consumes them differently than it does now (e.g. create another data structure based on the register values). I'm not sure though :)
The restriction, with which KVM doesn't need to worry about the changes in the registers after KVM_RUN, could potentially protect or be useful to protect KVM and simplify future changes/maintenance of the KVM codes that consumes the values.
I thought this was one of the reasons for having the restriction.
Thanks,
Reiji
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 48 lines · Reiji Watanabe
Quoted 28 lines · Sean Christopherson
Quoted 7 lines · Raghavendra Rao Ananta
Quoted 6 lines · Jim Mattson
Perhaps it would help if you explained *why* you are doing this. It sounds like you are either trying to protect against a malicious userspace, or you are trying to keep userspace from doing something stupid. In general, kvm only enforces constraints that are necessary to protect the host. If that's what you're doing, I don't understand why live migration doesn't provide an end-run around your protections.
Quoted 8 lines
It's mainly to safeguard the guests. With respect to migration, KVM and the userspace are collectively playing a role here. It's up to the userspace to ensure that the registers are configured the same across migrations and KVM ensures that the userspace doesn't modify the registers after
KVM_RUNso that they don't see features turned OFF/ON during execution. I'm not sure if it falls into the definition of protecting the host. Do you see a value in adding this extra protection from KVM?Short answer: probably not?
There is precedent for disallowing userspace from doing stupid things, but that's either for KVM's protection (as Jim pointed out), or because KVM can't honor the change, e.g. x86 is currently in the process of disallowing most CPUID changes after
KVM_RUNbecause KVM itself consumes the CPUID information and KVM doesn't support updating some of it's own internal state (because removing features like GB hugepage support is nonsensical and would require a large pile of complicated, messy code).Quoted 7 lines
Restricing CPUID changes does offer some "protection" to the guest, but that's not the goal. E.g. KVM won't detect CPUID misconfiguration in the migration case, and trying to do so is a fool's errand.
If restricting updates in the arm64 is necessary to ensure KVM provides sane behavior, then it could be justified. But if it's purely a sanity check on behalf of the guest, then it's not justified.
The pseudo firmware hvc registers, which this series are adding, are used by KVM to identify available hvc features for the guest, and not directly exposed to the guest as registers.
The ways the KVM code in the series consumes the registers' values are very limited, and no KVM data/state is created based on their values. But, as the code that consumes the registers grows in the future, I wouldn't be surprised if KVM consumes them differently than it does now (e.g. create another data structure based on the register values). I'm not sure though :)
Quoted 6 lines
The restriction, with which KVM doesn't need to worry about the changes in the registers after
KVM_RUN, could potentially protect or be useful to protect KVM and simplify future changes/maintenance of the KVM codes that consumes the values.
I thought this was one of the reasons for having the restriction.
Well, that wasn't the original intention of the patch, but just to protect the guests from the userspace's dynamic updates. Having said that, and based on what Sean mentioned in his last reply, it could be inconsistent from what KVM has been doing so far and would be difficult to cover all the scenarios that userspace can mess things up for guests.
I'll plan to drop this patch in the next version, and bring it back back to arm64 if we really need it.
Thanks Sean, Jim, and Reiji for the comments and discussion.
Regards,
Raghavendra
Quoted 2 lines
Thanks,
Reiji
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
In reply to Reiji WatanabeQuoted 4 lines · Reiji Watanabe
The restriction, with which KVM doesn't need to worry about the changes in the registers after
KVM_RUN, could potentially protect or be useful to protect KVM and simplify future changes/maintenance of the KVM codes that consumes the values.
That sort of protection is definitely welcome, the previously mentioned CPUID mess on x86 would have benefit greatly by KVM being restrictive in the past. That said, hooking KVM_RUN is likely the wrong way to go about implementing any restrictions. Running a vCPU is where much of the vCPU's state is explicitly consumed, but it's all too easy for KVM to implicity/indirectly consume state via a different ioctl(), e.g. if there are side effects that are visible in other registers, than an update can also be visible to userspace via KVM_{G,S}ET_{S,}REGS, at which point disallowing modifying state after KVM_RUN but not after reading/writing regs is arbitrary and inconsitent.
If possible, preventing modification if kvm->created_vcpus > 0 is ideal as it's a relatively common pattern in KVM, and provides a clear boundary to userpace regarding what is/isn't allowed.
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Reiji Watanabe
The restriction, with which KVM doesn't need to worry about the changes in the registers after
KVM_RUN, could potentially protect or be useful to protect KVM and simplify future changes/maintenance of the KVM codes that consumes the values.
Quoted 9 lines
That sort of protection is definitely welcome, the previously mentioned CPUID mess on x86 would have benefit greatly by KVM being restrictive in the past. That said, hooking
KVM_RUNis likely the wrong way to go about implementing any restrictions. Running a vCPU is where much of the vCPU's state is explicitly consumed, but it's all too easy for KVM to implicity/indirectly consume state via a differentioctl(), e.g. if there are side effects that are visible in other registers, than an update can also be visible to userspace via KVM_{G,S}ET_{S,}REGS, at which point disallowing modifying state afterKVM_RUNbut not after reading/writing regs is arbitrary and inconsitent.
Thank you for your comments !
I think I understand your concern, and that's a great point. That's not the case for those pseudo registers though at least for now :) BTW, is this concern specific to hooking KVM_RUN ? (Wouldn't it be the same for the option with "if kvm->created_vcpus > 0" ?)
Quoted 3 lines
If possible, preventing modification if
kvm->created_vcpus> 0 is ideal as it's a relatively common pattern in KVM, and provides a clear boundary to userpace regarding what is/isn't allowed.
Yes, I agree that would be better in general. For (pseudo) registers, I would think preventing modification if kvm->created_vcpus > 0 might not be a very good option for KVM/ARM though considering usage of KVM_GET_REG_LIST and KVM_{G,S}ET_ONE_REG.
Thanks,
Reiji
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 18 lines · Reiji Watanabe
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Reiji Watanabe
The restriction, with which KVM doesn't need to worry about the changes in the registers after
KVM_RUN, could potentially protect or be useful to protect KVM and simplify future changes/maintenance of the KVM codes that consumes the values.Quoted 9 lines
That sort of protection is definitely welcome, the previously mentioned CPUID mess on x86 would have benefit greatly by KVM being restrictive in the past. That said, hooking
KVM_RUNis likely the wrong way to go about implementing any restrictions. Running a vCPU is where much of the vCPU's state is explicitly consumed, but it's all too easy for KVM to implicity/indirectly consume state via a differentioctl(), e.g. if there are side effects that are visible in other registers, than an update can also be visible to userspace via KVM_{G,S}ET_{S,}REGS, at which point disallowing modifying state afterKVM_RUNbut not after reading/writing regs is arbitrary and inconsitent.
Quoted 5 lines
Thank you for your comments !
I think I understand your concern, and that's a great point. That's not the case for those pseudo registers though at least for now :) BTW, is this concern specific to hookingKVM_RUN? (Wouldn't it be the same for the option with "ifkvm->created_vcpus> 0" ?)
Not really? The goal with created_vcpus is to avoid having inconsistent state in "struct kvm_vcpu" with respect to the VM as whole. "struct kvm" obvioulsy can't be inconsistent with itself, e.g. even if userspace consumes some side effect, that's simply "the state". Did that make sense? Hard to explain in writing :-)
Quoted 5 lines
Quoted 3 lines
If possible, preventing modification if
kvm->created_vcpus> 0 is ideal as it's a relatively common pattern in KVM, and provides a clear boundary to userpace regarding what is/isn't allowed.Yes, I agree that would be better in general. For (pseudo) registers,
What exactly are these pseudo registers? If it's something that's an immutable property of the (virtual) system, then it might make sense to use a separate, non-vCPU mechanism for setting/getting their values. Then you can easily restrict the <whatever> to pre-created_vcpus, e.g. see x86's KVM_SET_IDENTITY_MAP_ADDR.
Quoted 3 lines
I would think preventing modification if
kvm->created_vcpus> 0 might not be a very good option for KVM/ARM though considering usage ofKVM_GET_REG_LISTand KVM_{G,S}ET_ONE_REG.
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 35 lines · Sean Christopherson
Quoted 18 lines · Reiji Watanabe
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Reiji Watanabe
The restriction, with which KVM doesn't need to worry about the changes in the registers after
KVM_RUN, could potentially protect or be useful to protect KVM and simplify future changes/maintenance of the KVM codes that consumes the values.Quoted 9 lines
That sort of protection is definitely welcome, the previously mentioned CPUID mess on x86 would have benefit greatly by KVM being restrictive in the past. That said, hooking
KVM_RUNis likely the wrong way to go about implementing any restrictions. Running a vCPU is where much of the vCPU's state is explicitly consumed, but it's all too easy for KVM to implicity/indirectly consume state via a differentioctl(), e.g. if there are side effects that are visible in other registers, than an update can also be visible to userspace via KVM_{G,S}ET_{S,}REGS, at which point disallowing modifying state afterKVM_RUNbut not after reading/writing regs is arbitrary and inconsitent.Quoted 5 lines
Thank you for your comments !
I think I understand your concern, and that's a great point. That's not the case for those pseudo registers though at least for now :) BTW, is this concern specific to hookingKVM_RUN? (Wouldn't it be the same for the option with "ifkvm->created_vcpus> 0" ?)Not really? The goal with
created_vcpusis to avoid having inconsistent state in "structkvm_vcpu" with respect to the VM as whole. "struct kvm" obvioulsy can't be inconsistent with itself, e.g. even if userspace consumes some side effect, that's simply "the state". Did that make sense? Hard to explain in writing :-)Quoted 4 lines
Quoted 3 lines
If possible, preventing modification if
kvm->created_vcpus> 0 is ideal as it's a relatively common pattern in KVM, and provides a clear boundary to userpace regarding what is/isn't allowed.
Quoted 7 lines
Quoted 1 line
Yes, I agree that would be better in general. For (pseudo) registers,
What exactly are these pseudo registers? If it's something that's an immutable property of the (virtual) system, then it might make sense to use a separate, non-vCPU mechanism for setting/getting their values. Then you can easily restrict the <whatever> to pre-created_vcpus, e.g. see x86's
KVM_SET_IDENTITY_MAP_ADDR.
In general, these pseudo-registers are reserved non-architectural register spaces, currently being used to represent KVM-as-a-firmware's versioning across guests' migrations [1]. That is, the user-space configures these registers for the guests to see same 'firmware' versions before and after migrations. The model is built over the existing KVM_GET_REG_LIST and KVM_[SET|GET]_ONE_REG APIs. Since this series' efforts falls into the same realm, the idea was keep this consistent with the existing model to which VMMs (such as QEMU) are already used to.
Granted, even though these registers should technically be immutable, there was no similar protection employed for the existing psuedo-registers. I was wondering if that could be of any value if we start providing one; But I guess not, since it may break the user-space's expectations of KVM (and probably why we didn't have it earlier).
Regards,
Raghavendra
[1]: https://github.com/torvalds/linux/blob/master/Documentation/virt/kvm/arm/psci.rst
Quoted 3 lines
Quoted 3 lines
I would think preventing modification if
kvm->created_vcpus> 0 might not be a very good option for KVM/ARM though considering usage ofKVM_GET_REG_LISTand KVM_{G,S}ET_ONE_REG.
Marc Zyngier
maintainerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
In reply to Sean ChristophersonQuoted 17 lines · Sean Christopherson
Quoted 4 lines · Reiji Watanabe
The restriction, with which KVM doesn't need to worry about the changes in the registers after
KVM_RUN, could potentially protect or be useful to protect KVM and simplify future changes/maintenance of the KVM codes that consumes the values.That sort of protection is definitely welcome, the previously mentioned CPUID mess on x86 would have benefit greatly by KVM being restrictive in the past. That said, hooking
KVM_RUNis likely the wrong way to go about implementing any restrictions. Running a vCPU is where much of the vCPU's state is explicitly consumed, but it's all too easy for KVM to implicity/indirectly consume state via a differentioctl(), e.g. if there are side effects that are visible in other registers, than an update can also be visible to userspace via KVM_{G,S}ET_{S,}REGS, at which point disallowing modifying state afterKVM_RUNbut not after reading/writing regs is arbitrary and inconsitent.
Quoted 3 lines
If possible, preventing modification if
kvm->created_vcpus> 0 is ideal as it's a relatively common pattern in KVM, and provides a clear boundary to userpace regarding what is/isn't allowed.
No, that's way too late. The configuration is in general per-CPU, and I really don't want to expand the surface of the userspace API to allow all sort of magic trick depending on the nature of what you save/restore.
The "first run" crap is already there. We have it on a per-CPU basis, and we need it at the VM level for other reasons (see the recent discussion about PMU filtering vs binding to a specific PMU implementation).
M.
Signature · 5 lines
Without deviation from the norm, progress is not possible. _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
In reply to Sean ChristophersonIt's probably time I jump on this thread
Quoted 18 lines · Sean Christopherson
Quoted 7 lines · Raghavendra Rao Ananta
Quoted 6 lines · Jim Mattson
Perhaps it would help if you explained *why* you are doing this. It sounds like you are either trying to protect against a malicious userspace, or you are trying to keep userspace from doing something stupid. In general, kvm only enforces constraints that are necessary to protect the host. If that's what you're doing, I don't understand why live migration doesn't provide an end-run around your protections.
Quoted 8 lines
It's mainly to safeguard the guests. With respect to migration, KVM and the userspace are collectively playing a role here. It's up to the userspace to ensure that the registers are configured the same across migrations and KVM ensures that the userspace doesn't modify the registers after
KVM_RUNso that they don't see features turned OFF/ON during execution. I'm not sure if it falls into the definition of protecting the host. Do you see a value in adding this extra protection from KVM?
Quoted 1 line
Short answer: probably not?
Well, I beg to defer.
Quoted 8 lines
There is precedent for disallowing userspace from doing stupid things, but that's either for KVM's protection (as Jim pointed out), or because KVM can't honor the change, e.g. x86 is currently in the process of disallowing most CPUID changes after
KVM_RUNbecause KVM itself consumes the CPUID information and KVM doesn't support updating some of it's own internal state (because removing features like GB hugepage support is nonsensical and would require a large pile of complicated, messy code).
We provide quite a lot of CPU emulation based on the feature registers exposed to the guest. Allowing userspace to change this stuff once the guest is running is a massive attack vector.
Quoted 7 lines
Restricing CPUID changes does offer some "protection" to the guest, but that's not the goal. E.g. KVM won't detect CPUID misconfiguration in the migration case, and trying to do so is a fool's errand.
If restricting updates in the arm64 is necessary to ensure KVM provides sane behavior, then it could be justified. But if it's purely a sanity check on behalf of the guest, then it's not justified.
No. This is about preventing userspace from tripping the kernel into doing things it really shouldn't by flipping bits of configuration that should be set in stone.
M.
Signature · 5 lines
Without deviation from the norm, progress is not possible. _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
In reply to Raghavendra Rao AnantaQuoted 36 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 18 lines · Reiji Watanabe
Hi Raghu,
Quoted 13 lines · Raghavendra Rao Ananta
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 9 +++++++++ 2 files changed, 12 insertions(+)Quoted 14 lines
include/linux/kvm_host.h struct kvm { · line 626
Quoted 9 lines
include/linux/kvm_host.h +1
-0@@ -623,6 +623,7 @@ struct kvm {struct notifier_block pm_notifier;#endif char stats_id[KVM_STATS_NAME_SIZE ]; +bool vm_started;Since
KVM_RUNon any vCPUs doesn't necessarily mean that the VM started yet, the name might be a bit misleading IMHO. I would think 'has_run_once' or 'ran_once' might be more clear (?).
Quoted 2 lines
I always struggle with the names; but if you feel that '
ran_once' makes more sense for a reader, I can change it.
I would prefer 'ran_once'.
Quoted 36 lines
Quoted 22 lines
Quoted 22 lines
include/linux/kvm_host.h +2
-0};#define kvm_err (fmt, ...) \@@ -1666,6 +1667,8 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) } } +#define kvm_vm_has_started (kvm) (kvm->vm_started) +extern bool kvm_rebooting;extern unsigned int halt_poll_ns;virt/kvm/kvm_main.c +1
-0@@ -3686,6 +3686,7 @@ static long kvm_vcpu_ioctl(struct file *filp,int r;struct kvm_fpu *fpu =NULL ;struct kvm_sregs *kvm_sregs =NULL ; +struct kvm *kvm = vcpu->kvm;Quoted 14 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3731
Quoted 11 lines
virt/kvm/kvm_main.c +5
-0if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)return -EIO ;@@ -3723,6 +3724,14 @@ static long kvm_vcpu_ioctl(struct file *filp,if (oldpid)synchronize_rcu ();put_pid (oldpid); + +/* +* Since we land here even on the first vCPU run, +* we can mark that the VM has started running. +*/ It might be nicer to add a comment why the code below gets
kvm->lock.
Quoted 6 lines
I've been going back and forth on this one. Initially I considered simply going with
atomic_t, but the patch 4/11 (KVM: arm64: Setup a framework for hypercall bitmap firmware registers) kvm_arm_set_fw_reg_bmap()'s implementation felt like we need a lock to have the whole 'is the register busy?' operation atomic. But, that's just one of the applications.
I understand why you need the code to get the lock here with the current implementation.
But, since the code just set the one field (vm_started) with the lock, I thought the intention of getting the lock might not be so obvious. (But, maybe clear enough looking at the code in the patch-4)
Thanks,
Reiji
Quoted 23 lines
Quoted 5 lines
Anyway, the patch generally looks good to me, and thank you for making this change (it works for my purpose as well).
Reviewed-by Reiji Watanabe <[email protected]>Glad that it's helping you as well and thanks for the review.
Regards,
RaghavendraQuoted 13 lines
Thanks,
ReijiQuoted 9 lines
virt/kvm/kvm_main.c +3
-1+mutex_lock (&kvm->lock); + kvm->vm_started = true; +mutex_unlock (&kvm->lock); } r =kvm_arch_vcpu_ioctl_run (vcpu);trace_kvm_userspace_exit (vcpu->run->exit_reason, r); --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 39 lines · Reiji Watanabe
Quoted 36 lines · Raghavendra Rao Ananta
Hi Reiji,
Quoted 32 lines · Reiji Watanabe
Hi Raghu,
Quoted 18 lines · Raghavendra Rao Ananta
Capture the start of the KVM VM, which is basically the start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 9 +++++++++ 2 files changed, 12 insertions(+)include/linux/kvm_host.h +0
-0include/linux/kvm_host.h struct kvm { · line 626
Quoted 4 lines
include/linux/kvm_host.h +1
-0@@ -623,6 +623,7 @@ struct kvm {struct notifier_block pm_notifier;#endif char stats_id[KVM_STATS_NAME_SIZE ]; +bool vm_started;Since
KVM_RUNon any vCPUs doesn't necessarily mean that the VM started yet, the name might be a bit misleading IMHO. I would think 'has_run_once' or 'ran_once' might be more clear (?).
Quoted 5 lines
Quoted 2 lines
I always struggle with the names; but if you feel that '
ran_once' makes more sense for a reader, I can change it.I would prefer '
ran_once'.
Yes, that makes sense. I think the name created a lot of confusion for the patch.
Thanks,
Raghavendra
Quoted 77 lines
Quoted 37 lines
Quoted 23 lines
Quoted 23 lines
include/linux/kvm_host.h +2
-0};#define kvm_err (fmt, ...) \@@ -1666,6 +1667,8 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) } } +#define kvm_vm_has_started (kvm) (kvm->vm_started) +extern bool kvm_rebooting;extern unsigned int halt_poll_ns;virt/kvm/kvm_main.c +1
-0@@ -3686,6 +3686,7 @@ static long kvm_vcpu_ioctl(struct file *filp,int r;struct kvm_fpu *fpu =NULL ;struct kvm_sregs *kvm_sregs =NULL ; +struct kvm *kvm = vcpu->kvm;Quoted 14 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3731
Quoted 11 lines
virt/kvm/kvm_main.c +5
-0if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)return -EIO ;@@ -3723,6 +3724,14 @@ static long kvm_vcpu_ioctl(struct file *filp,if (oldpid)synchronize_rcu ();put_pid (oldpid); + +/* +* Since we land here even on the first vCPU run, +* we can mark that the VM has started running. +*/ It might be nicer to add a comment why the code below gets
kvm->lock.Quoted 6 lines
I've been going back and forth on this one. Initially I considered simply going with
atomic_t, but the patch 4/11 (KVM: arm64: Setup a framework for hypercall bitmap firmware registers) kvm_arm_set_fw_reg_bmap()'s implementation felt like we need a lock to have the whole 'is the register busy?' operation atomic. But, that's just one of the applications.I understand why you need the code to get the lock here with the current implementation.
But, since the code just set the one field (vm_started) with the lock, I thought the intention of getting the lock might not be so obvious. (But, maybe clear enough looking at the code in the patch-4)Thanks,
ReijiQuoted 23 lines
Quoted 5 lines
Anyway, the patch generally looks good to me, and thank you for making this change (it works for my purpose as well).
Reviewed-by Reiji Watanabe <[email protected]>Glad that it's helping you as well and thanks for the review.
Regards,
RaghavendraQuoted 13 lines
Thanks,
ReijiQuoted 9 lines
virt/kvm/kvm_main.c +3
-1+mutex_lock (&kvm->lock); + kvm->vm_started = true; +mutex_unlock (&kvm->lock); } r =kvm_arch_vcpu_ioctl_run (vcpu);trace_kvm_userspace_exit (vcpu->run->exit_reason, r); --2.34.1.448.ga2b2bfdf31-goog
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
In reply to Raghavendra Rao AnantaQuoted 1 line · Raghavendra Rao Ananta
Capture the start of the KVM VM, which is basically the
Please wrap at ~75 chars.
Quoted 4 lines
start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
Please provide context of how the flag will be used. I glanced at the future patches, and knowing very little about arm, I was unable to glean useful info about exactly who is being prevented from doing what.
Quoted 20 lines
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 9 +++++++++ 2 files changed, 12 insertions(+)include/linux/kvm_host.h +1
-0@@ -623,6 +623,7 @@ struct kvm {struct notifier_block pm_notifier;#endif char stats_id[KVM_STATS_NAME_SIZE ]; +bool vm_started; };#define kvm_err (fmt, ...) \
include/linux/kvm_host.h static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) · line 1670
Quoted 4 lines
include/linux/kvm_host.h +1
-0@@ -1666,6 +1667,8 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) } } +#define kvm_vm_has_started (kvm) (kvm->vm_started)
Needs parantheses around (kvm), but why bother with a macro? This is the same header that defines struct kvm.
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3689
Quoted 13 lines
include/linux/kvm_host.h +1
-0+extern bool kvm_rebooting;extern unsigned int halt_poll_ns;virt/kvm/kvm_main.c +1
-0@@ -3686,6 +3686,7 @@ static long kvm_vcpu_ioctl(struct file *filp,int r;struct kvm_fpu *fpu =NULL ;struct kvm_sregs *kvm_sregs =NULL ; +struct kvm *kvm = vcpu->kvm;
If you're going to bother grabbing kvm, replace the instances below that also do vcpu->kvm.
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3730
Quoted 11 lines
virt/kvm/kvm_main.c +4
-0if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)return -EIO ;@@ -3723,6 +3724,14 @@ static long kvm_vcpu_ioctl(struct file *filp,if (oldpid)synchronize_rcu ();put_pid (oldpid); + +/* +* Since we land here even on the first vCPU run, +* we can mark that the VM has started running.
Please avoid "we", "us", etc..
"vm_started" is also ambiguous. If we end up with a flag, then I would prefer a much more literal name, a la created_vcpus, e.g. ran_vcpus or something.
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3732
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+*/ +mutex_lock (&kvm->lock);
This adds unnecessary lock contention when running vCPUs. The naive solution would be:
if (!kvm->vm_started) {
...
}virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3734
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+ kvm->vm_started = true; +mutex_unlock (&kvm->lock);
Lastly, why is this in generic KVM?
Quoted 10 lines
virt/kvm/kvm_main.c +0
-0} r =kvm_arch_vcpu_ioctl_run (vcpu);trace_kvm_userspace_exit (vcpu->run->exit_reason, r);Signature · 6 lines
2.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 38 lines · Sean Christopherson
Quoted 1 line · Raghavendra Rao Ananta
Capture the start of the KVM VM, which is basically the
Please wrap at ~75 chars.
Quoted 4 lines
start of any vCPU run. This state of the VM is helpful in the upcoming patches to prevent user-space from configuring certain VM features after the VM has started running.
Please provide context of how the flag will be used. I glanced at the future patches, and knowing very little about arm, I was unable to glean useful info about exactly who is being prevented from doing what.
Quoted 23 lines
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 9 +++++++++ 2 files changed, 12 insertions(+)include/linux/kvm_host.h +1
-0@@ -623,6 +623,7 @@ struct kvm {struct notifier_block pm_notifier;#endif char stats_id[KVM_STATS_NAME_SIZE ]; +bool vm_started; };#define kvm_err (fmt, ...) \@@ -1666,6 +1667,8 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) } }
Quoted 5 lines
include/linux/kvm_host.h static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu) · line 1670
Quoted 1 line
include/linux/kvm_host.h +1
-0+#define kvm_vm_has_started (kvm) (kvm->vm_started)Needs parantheses around (kvm), but why bother with a macro? This is the same header that defines struct kvm.
No specific reason for creating a macro as such. I can remove it if it feels noisy.
Quoted 34 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3689
Quoted 13 lines
include/linux/kvm_host.h +1
-0+extern bool kvm_rebooting;extern unsigned int halt_poll_ns;virt/kvm/kvm_main.c +1
-0@@ -3686,6 +3686,7 @@ static long kvm_vcpu_ioctl(struct file *filp,int r;struct kvm_fpu *fpu =NULL ;struct kvm_sregs *kvm_sregs =NULL ; +struct kvm *kvm = vcpu->kvm;If you're going to bother grabbing kvm, replace the instances below that also do
vcpu->kvm.virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3730
Quoted 11 lines
virt/kvm/kvm_main.c +4
-0if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_dead)return -EIO ;@@ -3723,6 +3724,14 @@ static long kvm_vcpu_ioctl(struct file *filp,if (oldpid)synchronize_rcu ();put_pid (oldpid); + +/* +* Since we land here even on the first vCPU run, +* we can mark that the VM has started running. Please avoid "we", "us", etc..
"
vm_started" is also ambiguous. If we end up with a flag, then I would prefer a much more literal name, a lacreated_vcpus, e.g.ran_vcpusor something.
Quoted 9 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3732
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+*/ +mutex_lock (&kvm->lock);This adds unnecessary lock contention when running vCPUs. The naive solution would be:
if (!kvm->vm_started) { ... }
Not sure if I understood the solution..
Quoted 5 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3734
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+ kvm->vm_started = true; +mutex_unlock (&kvm->lock);Lastly, why is this in generic KVM?
The v1 of the series originally had it in the arm specific code. However, I was suggested to move it to the generic code since the book keeping is not arch specific and could be helpful to others too [1].
Thanks for the review. I'll add your other comments as well.
Regards,
Raghavendra
[1]: https://lore.kernel.org/kvmarm/[email protected]/
Quoted 10 lines
Quoted 10 lines
virt/kvm/kvm_main.c +0
-1} r =kvm_arch_vcpu_ioctl_run (vcpu);trace_kvm_userspace_exit (vcpu->run->exit_reason, r); --2.34.1.448.ga2b2bfdf31-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 10 lines · Raghavendra Rao Ananta
Quoted 7 lines · Sean Christopherson
Quoted 1 line · Raghavendra Rao Ananta
+#define
kvm_vm_has_started(kvm)(kvm->vm_started)Needs parantheses around (kvm), but why bother with a macro? This is the same header that defines struct kvm.
No specific reason for creating a macro as such. I can remove it if it feels noisy.
Please do. In the future, don't use a macro unless there's a good reason to do so. Don't get me wrong, I love abusing macros, but for things like this they are completely inferior to
static inline bool kvm_vm_has_started(struct kvm *kvm)
{
return kvm->vm_started;
}because a helper function gives us type safety, doesn't suffer from concatenation of tokens potentially doing weird things, is easier to extend to a multi-line implementation, etc...
An example of when it's ok to use a macro is x86's
#define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)
which uses a macro instead of a proper function to avoid a circular dependency due to arch/x86/include/asm/kvm_host.h being included by include/linux/kvm_host.h and thus x86's implementation of kvm_arch_vcpu_memslots_id() coming before the definition of struct kvm_vcpu. But that's very much an exception and done only because the alternatives suck more.
Quoted 10 lines
Quoted 9 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3732
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+*/ +mutex_lock (&kvm->lock);This adds unnecessary lock contention when running vCPUs. The naive solution would be:
if (!kvm->vm_started) { ... }Not sure if I understood the solution..
In your proposed patch, KVM_RUN will take kvm->lock _every_ time. That introduces unnecessary contention as it will serialize this bit of code if multiple vCPUs are attempting KVM_RUN. By checking !vm_started, only the "first" KVM_RUN for a VM will acquire kvm->lock and thus avoid contention once the VM is up and running. There's still a possibility that multiple vCPUs will contend for kvm->lock on their first KVM_RUN, hence the quotes. I called it "naive" because it's possible there's a more elegant solution depending on the use case, e.g. a lockless approach might work (or it might not).
Quoted 8 lines
Quoted 5 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3734
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+ kvm->vm_started = true; +mutex_unlock (&kvm->lock);Lastly, why is this in generic KVM?
The v1 of the series originally had it in the arm specific code. However, I was suggested to move it to the generic code since the book keeping is not arch specific and could be helpful to others too [1].
I'm definitely in favor of moving/adding thing to generic KVM when it makes sense, but I'm skeptical in this particular case. The code _is_ arch specific in that arm64 apparently needs to acquire kvm->lock when checking if a vCPU has run, e.g. versus a hypothetical x86 use case that might be completely ok with a lockless implementation. And it's not obvious that there's a plausible, safe use case outside of arm64, e.g. on x86, there is very, very little that is truly shared across the entire VM/system, most things are per-thread/core/package in some way, shape, or form. In other words, I'm a wary of providing something like this for x86 because odds are good that any use will be functionally incorrect.
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 28 lines · Sean Christopherson
Quoted 10 lines · Raghavendra Rao Ananta
Quoted 7 lines · Sean Christopherson
Quoted 1 line · Raghavendra Rao Ananta
+#define
kvm_vm_has_started(kvm)(kvm->vm_started)Needs parantheses around (kvm), but why bother with a macro? This is the same header that defines struct kvm.
No specific reason for creating a macro as such. I can remove it if it feels noisy.
Please do. In the future, don't use a macro unless there's a good reason to do so. Don't get me wrong, I love abusing macros, but for things like this they are completely inferior to
static inline bool kvm_vm_has_started(struct kvm *kvm) { return kvm->vm_started; }because a helper function gives us type safety, doesn't suffer from concatenation of tokens potentially doing weird things, is easier to extend to a multi-line implementation, etc...
An example of when it's ok to use a macro is x86's
Quoted 8 lines
#define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)which uses a macro instead of a proper function to avoid a circular dependency due to
arch/x86/include/asm/kvm_host.hbeing included byinclude/linux/kvm_host.hand thus x86's implementation ofkvm_arch_vcpu_memslots_id()coming before the definition of structkvm_vcpu. But that's very much an exception and done only because the alternatives suck more.
Understood. Thanks for the explanation! Will switch to an inline function.
Quoted 11 lines
Quoted 10 lines
Quoted 9 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3732
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+*/ +mutex_lock (&kvm->lock);This adds unnecessary lock contention when running vCPUs. The naive solution would be:
if (!kvm->vm_started) { ... }Not sure if I understood the solution..
Quoted 9 lines
In your proposed patch,
KVM_RUNwill takekvm->lock_every_ time. That introduces unnecessary contention as it will serialize this bit of code if multiple vCPUs are attemptingKVM_RUN. By checking !vm_started, only the "first"KVM_RUNfor a VM will acquirekvm->lockand thus avoid contention once the VM is up and running. There's still a possibility that multiple vCPUs will contend forkvm->lockon their firstKVM_RUN, hence the quotes. I called it "naive" because it's possible there's a more elegant solution depending on the use case, e.g. a lockless approach might work (or it might not).
But is it safe to read kvm->vm_started without grabbing the lock in the first place? use atomic_t maybe for this?
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3734
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+ kvm->vm_started = true; +mutex_unlock (&kvm->lock);Lastly, why is this in generic KVM?
The v1 of the series originally had it in the arm specific code. However, I was suggested to move it to the generic code since the book keeping is not arch specific and could be helpful to others too [1].
Quoted 9 lines
I'm definitely in favor of moving/adding thing to generic KVM when it makes sense, but I'm skeptical in this particular case. The code _is_ arch specific in that arm64 apparently needs to acquire
kvm->lockwhen checking if a vCPU has run, e.g. versus a hypothetical x86 use case that might be completely ok with a lockless implementation. And it's not obvious that there's a plausible, safe use case outside of arm64, e.g. on x86, there is very, very little that is truly shared across the entire VM/system, most things are per-thread/core/package in some way, shape, or form. In other words, I'm a wary of providing something like this for x86 because odds are good that any use will be functionally incorrect.
I've been going back and forth on this. I've seen a couple of variables declared in the generic struct and used only in the arch code. vcpu->valid_wakeup for instance, which is used only by s390 arch. Maybe I'm looking at it the wrong way as to what can and can't go in the generic kvm code.
Thanks,
Raghavendra
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 10 lines · Raghavendra Rao Ananta
Quoted 9 lines · Sean Christopherson
In your proposed patch,
KVM_RUNwill takekvm->lock_every_ time. That introduces unnecessary contention as it will serialize this bit of code if multiple vCPUs are attemptingKVM_RUN. By checking !vm_started, only the "first"KVM_RUNfor a VM will acquirekvm->lockand thus avoid contention once the VM is up and running. There's still a possibility that multiple vCPUs will contend forkvm->lockon their firstKVM_RUN, hence the quotes. I called it "naive" because it's possible there's a more elegant solution depending on the use case, e.g. a lockless approach might work (or it might not).
Quoted 2 lines
But is it safe to read
kvm->vm_startedwithout grabbing the lock in the first place?
Don't know, but that's my point. Without a consumer in generic KVM and due to my lack of arm64 knowledge, without a high-level description of how the flag will be used by arm64, it's really difficult to determine what's safe and what's not. For other architectures, it's an impossible question to answer because we don't know how the flag might be used.
Quoted 1 line
use
atomic_tmaybe for this?
No. An atomic_t is generally useful only if there are multiple writers that can possibly write different values. It's highly unlikely that simply switching to an atomic address the needs of arm64.
Quoted 18 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3734
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+ kvm->vm_started = true; +mutex_unlock (&kvm->lock);Lastly, why is this in generic KVM?
The v1 of the series originally had it in the arm specific code. However, I was suggested to move it to the generic code since the book keeping is not arch specific and could be helpful to others too [1].
Quoted 9 lines
I'm definitely in favor of moving/adding thing to generic KVM when it makes sense, but I'm skeptical in this particular case. The code _is_ arch specific in that arm64 apparently needs to acquire
kvm->lockwhen checking if a vCPU has run, e.g. versus a hypothetical x86 use case that might be completely ok with a lockless implementation. And it's not obvious that there's a plausible, safe use case outside of arm64, e.g. on x86, there is very, very little that is truly shared across the entire VM/system, most things are per-thread/core/package in some way, shape, or form. In other words, I'm a wary of providing something like this for x86 because odds are good that any use will be functionally incorrect.
Quoted 5 lines
I've been going back and forth on this. I've seen a couple of variables declared in the generic struct and used only in the arch code.
vcpu->valid_wakeupfor instance, which is used only by s390 arch. Maybe I'm looking at it the wrong way as to what can and can't go in the generic kvm code.
Ya, valid_wakeup is an oddball, I don't know why it's in kvm_vcpu instead of arch code that's wrapped with e.g. kvm_arch_vcpu_valid_wakeup().
That said, valid_wakeup is consumed by generic KVM, i.e. has well defined semantics for how it is used, so it's purely a "this code is rather odd" issue. vm_started on the other hand is only produced by generic KVM, and so its required semantics are unclear.
Re: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 51 lines · Sean Christopherson
Quoted 10 lines · Raghavendra Rao Ananta
Quoted 9 lines · Sean Christopherson
In your proposed patch,
KVM_RUNwill takekvm->lock_every_ time. That introduces unnecessary contention as it will serialize this bit of code if multiple vCPUs are attemptingKVM_RUN. By checking !vm_started, only the "first"KVM_RUNfor a VM will acquirekvm->lockand thus avoid contention once the VM is up and running. There's still a possibility that multiple vCPUs will contend forkvm->lockon their firstKVM_RUN, hence the quotes. I called it "naive" because it's possible there's a more elegant solution depending on the use case, e.g. a lockless approach might work (or it might not).Quoted 2 lines
But is it safe to read
kvm->vm_startedwithout grabbing the lock in the first place?Don't know, but that's my point. Without a consumer in generic KVM and due to my lack of arm64 knowledge, without a high-level description of how the flag will be used by arm64, it's really difficult to determine what's safe and what's not. For other architectures, it's an impossible question to answer because we don't know how the flag might be used.
Quoted 1 line
use
atomic_tmaybe for this?No. An
atomic_tis generally useful only if there are multiple writers that can possibly write different values. It's highly unlikely that simply switching to an atomic address the needs of arm64.Quoted 18 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 3734
Quoted 2 lines
virt/kvm/kvm_main.c +2
-0+ kvm->vm_started = true; +mutex_unlock (&kvm->lock);Lastly, why is this in generic KVM?
The v1 of the series originally had it in the arm specific code. However, I was suggested to move it to the generic code since the book keeping is not arch specific and could be helpful to others too [1].
Quoted 9 lines
I'm definitely in favor of moving/adding thing to generic KVM when it makes sense, but I'm skeptical in this particular case. The code _is_ arch specific in that arm64 apparently needs to acquire
kvm->lockwhen checking if a vCPU has run, e.g. versus a hypothetical x86 use case that might be completely ok with a lockless implementation. And it's not obvious that there's a plausible, safe use case outside of arm64, e.g. on x86, there is very, very little that is truly shared across the entire VM/system, most things are per-thread/core/package in some way, shape, or form. In other words, I'm a wary of providing something like this for x86 because odds are good that any use will be functionally incorrect.Quoted 5 lines
I've been going back and forth on this. I've seen a couple of variables declared in the generic struct and used only in the arch code.
vcpu->valid_wakeupfor instance, which is used only by s390 arch. Maybe I'm looking at it the wrong way as to what can and can't go in the generic kvm code.
Quoted 7 lines
Ya,
valid_wakeupis an oddball, I don't know why it's inkvm_vcpuinstead of arch code that's wrapped with e.g.kvm_arch_vcpu_valid_wakeup().That said,
valid_wakeupis consumed by generic KVM, i.e. has well defined semantics for how it is used, so it's purely a "this code is rather odd" issue.vm_startedon the other hand is only produced by generic KVM, and so its required semantics are unclear.
Understood. I'll move it to arm64 and we can refactor it if there's a need for any other arch(s).
Thanks,
Raghavendra
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 2 lines · Raghavendra Rao Ananta
Understood. I'll move it to arm64 and we can refactor it if there's a need for any other arch(s).
Before you do that, can you answer Jim's question on _why_ arm64 needs this?
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Sean Christopherson
reviewerRe: [RFC PATCH v3 01/11] KVM: Capture VM start
Quoted 5 lines · Sean Christopherson
Quoted 2 lines · Raghavendra Rao Ananta
Understood. I'll move it to arm64 and we can refactor it if there's a need for any other arch(s).
Before you do that, can you answer Jim's question on _why_ arm64 needs this?
Gah, sorry, [email protected] once again lost the spam battle with Gmail.
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 2/11 KVM: arm64: Factor out firmware register handling from psci.c
In reply to Raghavendra Rao AnantaCommon 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.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 3/11 KVM: Introduce KVM_CAP_ARM_HVC_FW_REG_BMAP
In reply to Raghavendra Rao AnantaIntroduce the KVM ARM64 capability, KVM_CAP_ARM_HVC_FW_REG_BMAP, to indicate the support for psuedo-firmware bitmap extension. Each of these registers holds a feature-set exposed to the guest in the form of a bitmap. If supported, a simple 'read' of the capability should return the number of psuedo-firmware registers supported. User-space can utilize this to discover the registers. It can further explore or modify the features using the classical GET/SET_ONE_REG interface.
--- Documentation/virt/kvm/api.rst | 21 +++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + 2 files changed, 22 insertions(+)
Documentation/virt/kvm/api.rst +21 -0
@@ -6925,6 +6925,27 @@ indicated by the fd to the VM this is called on. This is intended to support intra-host migration of VMs between userspace VMMs, upgrading the VMM process without interrupting the guest. +7.30 KVM_CAP_ARM_HVC_FW_REG_BMAP +-------------------------------- + +:Architectures: arm64 +:Parameters: None +:Returns: Number of psuedo-firmware registers supported + +This capability indicates that KVM for arm64 supports the psuedo-firmware +register bitmap extension. Each of these registers represent the features +supported by a particular type in the form of a bitmap. By default, these +registers are set with the upper limit of the features that are supported. + +The registers can be accessed via the standard SET_ONE_REG and KVM_GET_ONE_REG +interfaces. The user-space is expected to read the number of these registers +available by reading KVM_CAP_ARM_HVC_FW_REG_BMAP, read the current bitmap +configuration via GET_ONE_REG for each register, and then write back the +desired bitmap of features that it wishes the guest to see via SET_ONE_REG. + +Note that KVM doesn't allow the user-space to modify these registers after +the VM (any of the vCPUs) has started running. + 8. Other capabilities. ======================
include/uapi/linux/kvm.h +1 -0
@@ -1131,6 +1131,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204#define KVM_CAP_ARM_MTE 205#define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206 +#define KVM_CAP_ARM_HVC_FW_REG_BMAP 207#ifdef KVM_CAP_IRQ_ROUTING
Signature · 6 lines
2.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH v3 03/11] KVM: Introduce KVM_CAP_ARM_HVC_FW_REG_BMAP
Hi Raghu,
Quoted 21 lines · Raghavendra Rao Ananta
Introduce the KVM ARM64 capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, to indicate the support for psuedo-firmware bitmap extension. Each of these registers holds a feature-set exposed to the guest in the form of a bitmap. If supported, a simple 'read' of the capability should return the number of psuedo-firmware registers supported. User-space can utilize this to discover the registers. It can further explore or modify the features using the classical GET/SET_ONE_REG interface.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- Documentation/virt/kvm/api.rst | 21 +++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + 2 files changed, 22 insertions(+)Documentation/virt/kvm/api.rst +0
-0
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6928
Quoted 4 lines
Documentation/virt/kvm/api.rst +1
-0@@ -6925,6 +6925,27 @@ indicated by the fd to the VM this is called on. This is intended to support intra-host migration of VMs between userspace VMMs, upgrading the VMM process without interrupting the guest. +7.30 KVM_CAP_ARM_HVC_FW_REG_BMAP
IMHO, instead of including its format of the register in the name, including its purpose/function in the name might be better. e.g. KVM_CAP_ARM_HVC_FEATURE_REG ?
(Feature fields don't necessarily have to be in a bitmap format
if they don't fit well although I'm not sure if we have such fields.)
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6933
Quoted 5 lines
Documentation/virt/kvm/api.rst +5
-0+-------------------------------- + +:Architectures: arm64 +:Parameters: None +:Returns: Number of psuedo-firmware registers supported
Looking at patch-4, the return value of this would be the number of pseudo-firmware *bitmap* registers supported.
BTW, "4.68 KVM_SET_ONE_REG" in the doc uses the word "arm64 firmware pseudo-registers". It would be nicer to use the same term.
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+ +This capability indicates that KVM for arm64 supports the psuedo-firmware +register bitmap extension. Each of these registers represent the features +supported by a particular type in the form of a bitmap. By default, these
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6947
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+registers are set with the upper limit of the features that are supported. + +The registers can be accessed via the standard SET_ONE_REG and KVM_GET_ONE_REG +interfaces. The user-space is expected to read the number of these registers +available by reading KVM_CAP_ARM_HVC_FW_REG_BMAP, read the current bitmap +configuration via GET_ONE_REG for each register, and then write back the +desired bitmap of features that it wishes the guest to see via SET_ONE_REG. + +Note that KVM doesn't allow the user-space to modify these registers after +the VM (any of the vCPUs) has started running.
Since even if KVM_RUN fails, and the VM hasn't started yet, it will get immutable. So, "after any of the vCPUs run KVM_RUN." might be more clear ?
Thanks,
Reiji
Quoted 19 lines
Documentation/virt/kvm/api.rst +1
-0+ 8. Other capabilities. ======================include/uapi/linux/kvm.h +1
-1@@ -1131,6 +1131,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204#define KVM_CAP_ARM_MTE 205#define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206 +#define KVM_CAP_ARM_HVC_FW_REG_BMAP 207#ifdef KVM_CAP_IRQ_ROUTING --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 03/11] KVM: Introduce KVM_CAP_ARM_HVC_FW_REG_BMAP
Quoted 29 lines · Reiji Watanabe
Hi Raghu,
Quoted 24 lines · Raghavendra Rao Ananta
Introduce the KVM ARM64 capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, to indicate the support for psuedo-firmware bitmap extension. Each of these registers holds a feature-set exposed to the guest in the form of a bitmap. If supported, a simple 'read' of the capability should return the number of psuedo-firmware registers supported. User-space can utilize this to discover the registers. It can further explore or modify the features using the classical GET/SET_ONE_REG interface.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- Documentation/virt/kvm/api.rst | 21 +++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + 2 files changed, 22 insertions(+)Documentation/virt/kvm/api.rst +0
-0@@ -6925,6 +6925,27 @@ indicated by the fd to the VM this is called on. This is intended to support intra-host migration of VMs between userspace VMMs, upgrading the VMM process without interrupting the guest.
Quoted 8 lines
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6928
Quoted 1 line
Documentation/virt/kvm/api.rst +1
-0+7.30 KVM_CAP_ARM_HVC_FW_REG_BMAPIMHO, instead of including its format of the register in the name, including its purpose/function in the name might be better. e.g.
KVM_CAP_ARM_HVC_FEATURE_REG?
(Feature fields don't necessarily have to be in a bitmap format
if they don't fit well although I'm not sure if we have such fields.)
Well we do have registers, KVM_REG_ARM_PSCI_VERSION for instance, that's not covered by this CAP. But sure, I can explicitly add 'FEATURES' to the name. I also wanted to explicitly convey that we are covering the *bitmapped* firmware registers here. But not sure if appending 'BMAP' might give an impression that the CAP itself is bitmapped.
Do you think KVM_CAP_ARM_HVC_BMAP_FEAT_REG is better?
Quoted 10 lines
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6933
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+ +:Architectures: arm64 +:Parameters: None +:Returns: Number of psuedo-firmware registers supportedLooking at patch-4, the return value of this would be the number of pseudo-firmware *bitmap* registers supported.
BTW, "4.68KVM_SET_ONE_REG" in the doc uses the word "arm64 firmware pseudo-registers". It would be nicer to use the same term.
Nice catch. I'll fix it here in apr.rst.
Quoted 10 lines
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+ +This capability indicates that KVM for arm64 supports the psuedo-firmware +register bitmap extension. Each of these registers represent the features +supported by a particular type in the form of a bitmap. By default, these +registers are set with the upper limit of the features that are supported. + +The registers can be accessed via the standard SET_ONE_REG and KVM_GET_ONE_REG +interfaces. The user-space is expected to read the number of these registers +available by reading KVM_CAP_ARM_HVC_FW_REG_BMAP, read the current bitmap +configuration via GET_ONE_REG for each register, and then write back the
Quoted 9 lines
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6947
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+desired bitmap of features that it wishes the guest to see via SET_ONE_REG. + +Note that KVM doesn't allow the user-space to modify these registers after +the VM (any of the vCPUs) has started running.Since even if
KVM_RUNfails, and the VM hasn't started yet, it will get immutable. So, "after any of the vCPUs runKVM_RUN." might be more clear ?
Sure, that's probably more clear. I'll fix it.
Regards,
Raghavendra
Quoted 24 lines
Thanks,
ReijiQuoted 19 lines
Documentation/virt/kvm/api.rst +1
-0+ 8. Other capabilities. ======================include/uapi/linux/kvm.h +1
-1@@ -1131,6 +1131,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204#define KVM_CAP_ARM_MTE 205#define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206 +#define KVM_CAP_ARM_HVC_FW_REG_BMAP 207#ifdef KVM_CAP_IRQ_ROUTING --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 03/11] KVM: Introduce KVM_CAP_ARM_HVC_FW_REG_BMAP
Quoted 39 lines · Raghavendra Rao Ananta
Quoted 29 lines · Reiji Watanabe
Hi Raghu,
Quoted 24 lines · Raghavendra Rao Ananta
Introduce the KVM ARM64 capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, to indicate the support for psuedo-firmware bitmap extension. Each of these registers holds a feature-set exposed to the guest in the form of a bitmap. If supported, a simple 'read' of the capability should return the number of psuedo-firmware registers supported. User-space can utilize this to discover the registers. It can further explore or modify the features using the classical GET/SET_ONE_REG interface.Signed-off-by Raghavendra Rao Ananta <[email protected]>--- Documentation/virt/kvm/api.rst | 21 +++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + 2 files changed, 22 insertions(+)Documentation/virt/kvm/api.rst +0
-0@@ -6925,6 +6925,27 @@ indicated by the fd to the VM this is called on. This is intended to support intra-host migration of VMs between userspace VMMs, upgrading the VMM process without interrupting the guest.Quoted 8 lines
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6928
Quoted 1 line
Documentation/virt/kvm/api.rst +1
-0+7.30 KVM_CAP_ARM_HVC_FW_REG_BMAPIMHO, instead of including its format of the register in the name, including its purpose/function in the name might be better. e.g.
KVM_CAP_ARM_HVC_FEATURE_REG?
(Feature fields don't necessarily have to be in a bitmap format
if they don't fit well although I'm not sure if we have such fields.)
Quoted 7 lines
Well we do have registers,
KVM_REG_ARM_PSCI_VERSIONfor instance, that's not covered by this CAP. But sure, I can explicitly add 'FEATURES' to the name. I also wanted to explicitly convey that we are covering the *bitmapped* firmware registers here. But not sure if appending 'BMAP' might give an impression that the CAP itself is bitmapped.
Do you thinkKVM_CAP_ARM_HVC_BMAP_FEAT_REGis better?
Thank you for the explanation! That sounds better to me.
Regards,
Reiji
Quoted 59 lines
Quoted 10 lines
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6933
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+ +:Architectures: arm64 +:Parameters: None +:Returns: Number of psuedo-firmware registers supportedLooking at patch-4, the return value of this would be the number of pseudo-firmware *bitmap* registers supported.
BTW, "4.68KVM_SET_ONE_REG" in the doc uses the word "arm64 firmware pseudo-registers". It would be nicer to use the same term.Nice catch. I'll fix it here in
apr.rst.Quoted 10 lines
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+ +This capability indicates that KVM for arm64 supports the psuedo-firmware +register bitmap extension. Each of these registers represent the features +supported by a particular type in the form of a bitmap. By default, these +registers are set with the upper limit of the features that are supported. + +The registers can be accessed via the standard SET_ONE_REG and KVM_GET_ONE_REG +interfaces. The user-space is expected to read the number of these registers +available by reading KVM_CAP_ARM_HVC_FW_REG_BMAP, read the current bitmap +configuration via GET_ONE_REG for each register, and then write back theQuoted 9 lines
Documentation/virt/kvm/api.rst indicated by the fd to the VM this is called on. · line 6947
Quoted 4 lines
Documentation/virt/kvm/api.rst +4
-0+desired bitmap of features that it wishes the guest to see via SET_ONE_REG. + +Note that KVM doesn't allow the user-space to modify these registers after +the VM (any of the vCPUs) has started running.Since even if
KVM_RUNfails, and the VM hasn't started yet, it will get immutable. So, "after any of the vCPUs runKVM_RUN." might be more clear ?Sure, that's probably more clear. I'll fix it.
Regards,
RaghavendraQuoted 24 lines
Thanks,
ReijiQuoted 19 lines
Documentation/virt/kvm/api.rst +1
-0+ 8. Other capabilities. ======================include/uapi/linux/kvm.h +1
-1@@ -1131,6 +1131,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204#define KVM_CAP_ARM_MTE 205#define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206 +#define KVM_CAP_ARM_HVC_FW_REG_BMAP 207#ifdef KVM_CAP_IRQ_ROUTING --2.34.1.448.ga2b2bfdf31-goog
RFC PATCH v3 4/11 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 Virtual Machine Manager (VMM). 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 VMM to elect the services that it wishes the guest to discover. VMM 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.
The capability, KVM_CAP_ARM_HVC_FW_REG_BMAP, is used to announce this extension, which returns the number of psuedo-firmware registers supported. 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 VMMs 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. Other services are momentarily added in the upcoming patches.
--- arch/arm64/include/asm/kvm_host.h | 12 ++++ arch/arm64/include/uapi/asm/kvm.h | 4 ++ arch/arm64/kvm/arm.c | 4 ++ arch/arm64/kvm/hypercalls.c | 103 +++++++++++++++++++++++++++++- arch/arm64/kvm/trng.c | 8 +-- include/kvm/arm_hypercalls.h | 6 ++ 6 files changed, 129 insertions(+), 8 deletions(-)
arch/arm64/include/asm/kvm_host.h +12 -0
@@ -102,6 +102,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;
@@ -137,6 +146,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {
arch/arm64/include/uapi/asm/kvm.h +4 -0
@@ -281,6 +281,10 @@ 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) +#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_REG(3) +#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 +4 -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:
@@ -283,6 +284,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_HVC_FW_REG_BMAP : + r =kvm_arm_num_fw_bmap_regs (); +break ;default : r =0 ; }
arch/arm64/kvm/hypercalls.c +102 -1
162 lines not on this page · raw message
arch/arm64/kvm/trng.c +1 -7
@@ -60,14 +60,8 @@ int kvm_trng_call(struct kvm_vcpu *vcpu) val =ARM_SMCCC_TRNG_VERSION_1_0 ;break ;case ARM_SMCCC_TRNG_FEATURES : -switch (smccc_get_arg1 (vcpu)) { -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 : +if (kvm_hvc_call_supported (vcpu,smccc_get_arg1 (vcpu))) val =TRNG_SUCCESS ; - }break ;case ARM_SMCCC_TRNG_GET_UUID : smccc_set_retval(vcpu, le32_to_cpu(u[0]), le32_to_cpu(u[1]),
include/kvm/arm_hypercalls.h +6 -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,9 +45,12 @@ 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_num_fw_bmap_regs (void );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); +bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id);#endif
Signature · 1 line
2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 04/11] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Hi Raghu,
Quoted 107 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the Virtual Machine Manager (VMM). 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 VMM to elect the services that it wishes the guest to discover. VMM 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.
The capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, is used to announce this extension, which returns the number of psuedo-firmware registers supported. 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 viaGET_ONE_REG, and writeback the desired values usingSET_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.
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. 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 | 4 ++ arch/arm64/kvm/arm.c | 4 ++ arch/arm64/kvm/hypercalls.c | 103 +++++++++++++++++++++++++++++- arch/arm64/kvm/trng.c | 8 +-- include/kvm/arm_hypercalls.h | 6 ++ 6 files changed, 129 insertions(+), 8 deletions(-)arch/arm64/include/asm/kvm_host.h +12
-0@@ -102,6 +102,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;@@ -137,6 +146,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -281,6 +281,10 @@ 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) +#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_REG(3) +#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 +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:
arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 289
Quoted 6 lines
arch/arm64/kvm/arm.c +3
-0@@ -283,6 +284,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_HVC_FW_REG_BMAP : + r =kvm_arm_num_fw_bmap_regs (); +break ;
Looking at the discussion for the v2 series,
https://lore.kernel.org/kvmarm/[email protected]/
I assume that the number of the pseudo-firmware bitmap registers will be used to clear pseudo firmware bitmap registers that userspace doesn't know.
I'm wondering how userspace can identify which pseudo-firmware registers that KVM_GET_REG_LIST provides are the pseudo-firmware bitmap registers that it doesn't know.
For instance, suppose pseudo-firmware registers that KVM_GET_REG_LIST provides are KVM_REG_ARM_FW_REG(0) to KVM_REG_ARM_FW_REG(9), userspace doesn't knows KVM_REG_ARM_FW_REG(6) to KVM_REG_ARM_FW_REG(9), and KVM_CAP_ARM_HVC_FW_REG_BMAP returns 5, how can userspace identify remaining two bitmap registers from those 4 (fw-reg #6 to #9) firmware registers ?
Quoted 23 lines
arch/arm64/kvm/arm.c +0
-0default : r =0 ; }arch/arm64/kvm/hypercalls.c +12
-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; +} + +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) { +case ARM_SMCCC_TRNG_VERSION : +case ARM_SMCCC_TRNG_FEATURES :
arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 82
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +10
-0+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; + } +}
kvm_hvc_call_supported() could return true even for @func_id that kvm_hvc_call_handler() returns -EINVAL for. Is this behavior what you really want ?
If so, IMHO the function name might be a bit mis-leading. "kvm_hvc_call_disabled" (and flip the return value) might be closer to what it does(?).
Quoted 63 lines
arch/arm64/kvm/hypercalls.c +27
-1+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 ; }@@ -153,9 +180,25 @@ static const u64 kvm_arm_fw_reg_ids[] = { KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, }; +static const u64 kvm_arm_fw_reg_bmap_ids[] = { +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_num_fw_bmap_regs (void ) +{ +return ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) { -return ARRAY_SIZE (kvm_arm_fw_reg_ids); +return ARRAY_SIZE (kvm_arm_fw_reg_ids) +kvm_arm_num_fw_bmap_regs (); }int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices)@@ -167,6 +210,11 @@ int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)return -EFAULT ; } +for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); i++) { +if (put_user (kvm_arm_fw_reg_bmap_ids[i], uindices++)) +return -EFAULT ; + } +return 0 ; }@@ -211,9 +259,20 @@ static int get_kernel_wa_level(u64 regid)return -EINVAL ;
arch/arm64/kvm/hypercalls.c static int get_kernel_wa_level(u64 regid) · line 269
Quoted 10 lines
arch/arm64/kvm/hypercalls.c +8
-0} +static void +kvm_arm_get_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 fw_reg_bmap,u64 *val) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&kvm->lock); + *val = fw_reg_bmap; +mutex_unlock (&kvm->lock);
Why does it need to hold the lock ? (Wouldn't READ_ONCE be enough ?)
Quoted 78 lines
arch/arm64/kvm/hypercalls.c +45
-0+} +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;u64 val;switch (reg->id) {@@ -224,6 +283,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 : +kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_std_bmap, &val); +break ;default :return -ENOENT ; }@@ -234,6 +296,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; + +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); + +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. +*/ +if (kvm_vm_has_started (kvm)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + + *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;@@ -310,6 +409,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 ; }arch/arm64/kvm/trng.c +0
-0@@ -60,14 +60,8 @@ int kvm_trng_call(struct kvm_vcpu *vcpu) val =ARM_SMCCC_TRNG_VERSION_1_0 ;
arch/arm64/kvm/trng.c int kvm_trng_call(struct kvm_vcpu *vcpu) · line 64
Quoted 10 lines
arch/arm64/kvm/trng.c +1
-6break ;case ARM_SMCCC_TRNG_FEATURES : -switch (smccc_get_arg1 (vcpu)) { -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 : +if (kvm_hvc_call_supported (vcpu,smccc_get_arg1 (vcpu))) val =TRNG_SUCCESS ;
kvm_hvc_call_supported() returns true for any values that are not explicitly listed in kvm_hvc_call_supported() (i.e. it returns true even for @func_id that are not any of ARM_SMCCC_TRNG_*). So, I don't think it can simply use the current kvm_hvc_call_supported.
Thanks,
Reiji
Quoted 34 lines
arch/arm64/kvm/trng.c +0
-1- }break ;case ARM_SMCCC_TRNG_GET_UUID : smccc_set_retval(vcpu, le32_to_cpu(u[0]), le32_to_cpu(u[1]),include/kvm/arm_hypercalls.h +6
-1@@ -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,9 +45,12 @@ 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_num_fw_bmap_regs (void );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); +bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id);#endif --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 04/11] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Quoted 123 lines · Reiji Watanabe
Hi Raghu,
Quoted 107 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the Virtual Machine Manager (VMM). 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 VMM to elect the services that it wishes the guest to discover. VMM 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.
The capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, is used to announce this extension, which returns the number of psuedo-firmware registers supported. 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 viaGET_ONE_REG, and writeback the desired values usingSET_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.
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. 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 | 4 ++ arch/arm64/kvm/arm.c | 4 ++ arch/arm64/kvm/hypercalls.c | 103 +++++++++++++++++++++++++++++- arch/arm64/kvm/trng.c | 8 +-- include/kvm/arm_hypercalls.h | 6 ++ 6 files changed, 129 insertions(+), 8 deletions(-)arch/arm64/include/asm/kvm_host.h +12
-0@@ -102,6 +102,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;@@ -137,6 +146,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -281,6 +281,10 @@ 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) +#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_REG(3) +#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 +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:arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 289
Quoted 6 lines
arch/arm64/kvm/arm.c +3
-0@@ -283,6 +284,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_HVC_FW_REG_BMAP : + r =kvm_arm_num_fw_bmap_regs (); +break ;Looking at the discussion for the v2 series,
Quoted 13 lines
I assume that the number of the pseudo-firmware bitmap registers will be used to clear pseudo firmware bitmap registers that userspace doesn't know.
I'm wondering how userspace can identify which pseudo-firmware registers thatKVM_GET_REG_LISTprovides are the pseudo-firmware bitmap registers that it doesn't know.
For instance, suppose pseudo-firmware registers thatKVM_GET_REG_LISTprovides areKVM_REG_ARM_FW_REG(0)toKVM_REG_ARM_FW_REG(9), userspace doesn't knowsKVM_REG_ARM_FW_REG(6)toKVM_REG_ARM_FW_REG(9), andKVM_CAP_ARM_HVC_FW_REG_BMAPreturns 5, how can userspace identify remaining two bitmap registers from those 4 (fw-reg #6 to #9) firmware registers ?
In v3, we leave the decision upto the userspace. If the userspace encounters a register that it's unaware, it can choose either to clear it or let it get exposed to the guest as is (see the code snipped shared by Andrew in the link).
Trying to understand the question better- are you asking how would userspace distinguish between bitmap and regular fw registers with intermixed sequence numbers?
If yes, do you foresee a reason why they 'unaware' registers needed to be treated differently?
Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/arm.c +0
-0default : r =0 ; }arch/arm64/kvm/hypercalls.c +18
-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; +} + +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) { +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 :
Quoted 8 lines
arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 82
Quoted 4 lines
arch/arm64/kvm/hypercalls.c +4
-0+/* By default, allow the services that aren't listed here */ +return true; + } +}
kvm_hvc_call_supported()could return true even for @func_id thatkvm_hvc_call_handler()returns-EINVALfor. Is this behavior what you really want ?
Yes. My idea was to let kvm_hvc_call_supported() check for the support, while kvm_hvc_call_handler() does the real processing of the call.
Quoted 4 lines
If so, IMHO the function name might be a bit mis-leading. "
kvm_hvc_call_disabled" (and flip the return value) might be closer to what it does(?).
Sorry, I'm unclear how flipping is helping. Wouldn't we return 'false' if we don't have a case for the func_id, indicating it's NOT disabled, but kvm_hvc_call_handler() can still return SMCCC_RET_NOT_SUPPORTED?
Quoted 66 lines
Quoted 66 lines
arch/arm64/kvm/hypercalls.c +27
-1+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 ; }@@ -153,9 +180,25 @@ static const u64 kvm_arm_fw_reg_ids[] = { KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, }; +static const u64 kvm_arm_fw_reg_bmap_ids[] = { +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_num_fw_bmap_regs (void ) +{ +return ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) { -return ARRAY_SIZE (kvm_arm_fw_reg_ids); +return ARRAY_SIZE (kvm_arm_fw_reg_ids) +kvm_arm_num_fw_bmap_regs (); }int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices)@@ -167,6 +210,11 @@ int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)return -EFAULT ; } +for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); i++) { +if (put_user (kvm_arm_fw_reg_bmap_ids[i], uindices++)) +return -EFAULT ; + } +return 0 ; }@@ -211,9 +259,20 @@ static int get_kernel_wa_level(u64 regid)return -EINVAL ; }
Quoted 11 lines
arch/arm64/kvm/hypercalls.c static int get_kernel_wa_level(u64 regid) · line 269
Quoted 8 lines
arch/arm64/kvm/hypercalls.c +8
-0+static void +kvm_arm_get_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 fw_reg_bmap,u64 *val) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&kvm->lock); + *val = fw_reg_bmap; +mutex_unlock (&kvm->lock);Why does it need to hold the lock ? (Wouldn't
READ_ONCEbe enough ?)
I don't have much experience with READ_ONCE at this point, but do you think this read can be protected again the read/write in kvm_arm_set_fw_reg_bmap()?
Quoted 86 lines
Quoted 86 lines
arch/arm64/kvm/hypercalls.c +45
-0+} +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;u64 val;switch (reg->id) {@@ -224,6 +283,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 : +kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_std_bmap, &val); +break ;default :return -ENOENT ; }@@ -234,6 +296,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; + +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); + +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. +*/ +if (kvm_vm_has_started (kvm)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + + *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;@@ -310,6 +409,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 ; }arch/arm64/kvm/trng.c +0
-5@@ -60,14 +60,8 @@ int kvm_trng_call(struct kvm_vcpu *vcpu) val =ARM_SMCCC_TRNG_VERSION_1_0 ;break ;case ARM_SMCCC_TRNG_FEATURES : -switch (smccc_get_arg1 (vcpu)) { -case ARM_SMCCC_TRNG_VERSION : -case ARM_SMCCC_TRNG_FEATURES : -case ARM_SMCCC_TRNG_GET_UUID : -case ARM_SMCCC_TRNG_RND32 :
Quoted 9 lines
arch/arm64/kvm/trng.c int kvm_trng_call(struct kvm_vcpu *vcpu) · line 64
Quoted 3 lines
arch/arm64/kvm/trng.c +1
-1-case ARM_SMCCC_TRNG_RND64 : +if (kvm_hvc_call_supported (vcpu,smccc_get_arg1 (vcpu))) val =TRNG_SUCCESS ;
kvm_hvc_call_supported()returns true for any values that are not explicitly listed inkvm_hvc_call_supported()(i.e. it returns true even for @func_id that are not any of ARM_SMCCC_TRNG_*). So, I don't think it can simply use the currentkvm_hvc_call_supported.
You are right. Probably I should leave the case statements as is (or think of some better way).
Thanks for the review and suggestions.
Regards,
Raghavendra
Quoted 37 lines
Thanks,
ReijiQuoted 34 lines
arch/arm64/kvm/trng.c +0
-1- }break ;case ARM_SMCCC_TRNG_GET_UUID : smccc_set_retval(vcpu, le32_to_cpu(u[0]), le32_to_cpu(u[1]),include/kvm/arm_hypercalls.h +6
-1@@ -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,9 +45,12 @@ 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_num_fw_bmap_regs (void );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); +bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id);#endif --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 04/11] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Quoted 138 lines · Raghavendra Rao Ananta
Quoted 123 lines · Reiji Watanabe
Hi Raghu,
Quoted 107 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the Virtual Machine Manager (VMM). 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 VMM to elect the services that it wishes the guest to discover. VMM 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.
The capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, is used to announce this extension, which returns the number of psuedo-firmware registers supported. 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 viaGET_ONE_REG, and writeback the desired values usingSET_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.
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. 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 | 4 ++ arch/arm64/kvm/arm.c | 4 ++ arch/arm64/kvm/hypercalls.c | 103 +++++++++++++++++++++++++++++- arch/arm64/kvm/trng.c | 8 +-- include/kvm/arm_hypercalls.h | 6 ++ 6 files changed, 129 insertions(+), 8 deletions(-)arch/arm64/include/asm/kvm_host.h +12
-0@@ -102,6 +102,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;@@ -137,6 +146,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -281,6 +281,10 @@ 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) +#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_REG(3) +#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 +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:arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 289
Quoted 6 lines
arch/arm64/kvm/arm.c +3
-0@@ -283,6 +284,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_HVC_FW_REG_BMAP : + r =kvm_arm_num_fw_bmap_regs (); +break ;Looking at the discussion for the v2 series,
Quoted 13 lines
I assume that the number of the pseudo-firmware bitmap registers will be used to clear pseudo firmware bitmap registers that userspace doesn't know.
I'm wondering how userspace can identify which pseudo-firmware registers thatKVM_GET_REG_LISTprovides are the pseudo-firmware bitmap registers that it doesn't know.
For instance, suppose pseudo-firmware registers thatKVM_GET_REG_LISTprovides areKVM_REG_ARM_FW_REG(0)toKVM_REG_ARM_FW_REG(9), userspace doesn't knowsKVM_REG_ARM_FW_REG(6)toKVM_REG_ARM_FW_REG(9), andKVM_CAP_ARM_HVC_FW_REG_BMAPreturns 5, how can userspace identify remaining two bitmap registers from those 4 (fw-reg #6 to #9) firmware registers ?
Quoted 7 lines
In v3, we leave the decision upto the userspace. If the userspace encounters a register that it's unaware, it can choose either to clear it or let it get exposed to the guest as is (see the code snipped shared by Andrew in the link).
Trying to understand the question better- are you asking how would userspace distinguish between bitmap and regular fw registers with intermixed sequence numbers?
Yes, that's my question.
Quoted 2 lines
If yes, do you foresee a reason why they 'unaware' registers needed to be treated differently?
Since I'm not sure what the specification of 'unaware' (non-bitmap) registers will be, it would be safer for us to assume that they might need to be treated differently from the bitmap registers. Considering there is KVM_REG_ARM_PSCI_VERSION, which KVM doesn't allow userspace to set to 0, there might be similar registers that userspace cannot set to 0 in the future.
BTW, If you assume that all those 'unaware' firmware registers are treated in the same way, I don't think userspace needs the number of those bitmap registers from KVM_CAP_ARM_HVC_FW_REG_BMAP (Instead, I would think it can handle the 'unaware' registers with a list of firmware registers from KVM_GET_REG_LIST).
Quoted 42 lines
Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/arm.c +0
-0default : r =0 ; }arch/arm64/kvm/hypercalls.c +18
-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; +} + +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) { +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 :Quoted 8 lines
arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 82
Quoted 4 lines
arch/arm64/kvm/hypercalls.c +4
-0+/* By default, allow the services that aren't listed here */ +return true; + } +}
kvm_hvc_call_supported()could return true even for @func_id thatkvm_hvc_call_handler()returns-EINVALfor. Is this behavior what you really want ?Yes. My idea was to let
kvm_hvc_call_supported()check for the support, whilekvm_hvc_call_handler()does the real processing of the call.
Quoted 7 lines
Quoted 4 lines
If so, IMHO the function name might be a bit mis-leading. "
kvm_hvc_call_disabled" (and flip the return value) might be closer to what it does(?).Sorry, I'm unclear how flipping is helping. Wouldn't we return 'false' if we don't have a case for the
func_id, indicating it's NOT disabled, butkvm_hvc_call_handler()can still returnSMCCC_RET_NOT_SUPPORTED?
Yes, that's fine, too.
Since those services are disabled (because they are enabled by default), I just thought checking 'disabled' might be closer to what it does than checking 'enabled'. But, 'enabled' is also fine.
Quoted 77 lines
Quoted 66 lines
Quoted 66 lines
arch/arm64/kvm/hypercalls.c +27
-1+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 ; }@@ -153,9 +180,25 @@ static const u64 kvm_arm_fw_reg_ids[] = { KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, }; +static const u64 kvm_arm_fw_reg_bmap_ids[] = { +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_num_fw_bmap_regs (void ) +{ +return ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) { -return ARRAY_SIZE (kvm_arm_fw_reg_ids); +return ARRAY_SIZE (kvm_arm_fw_reg_ids) +kvm_arm_num_fw_bmap_regs (); }int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices)@@ -167,6 +210,11 @@ int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)return -EFAULT ; } +for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); i++) { +if (put_user (kvm_arm_fw_reg_bmap_ids[i], uindices++)) +return -EFAULT ; + } +return 0 ; }@@ -211,9 +259,20 @@ static int get_kernel_wa_level(u64 regid)return -EINVAL ; }Quoted 11 lines
arch/arm64/kvm/hypercalls.c static int get_kernel_wa_level(u64 regid) · line 269
Quoted 8 lines
arch/arm64/kvm/hypercalls.c +8
-0+static void +kvm_arm_get_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 fw_reg_bmap,u64 *val) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&kvm->lock); + *val = fw_reg_bmap; +mutex_unlock (&kvm->lock);Why does it need to hold the lock ? (Wouldn't
READ_ONCEbe enough ?)
Quoted 3 lines
I don't have much experience with
READ_ONCEat this point, but do you think this read can be protected again the read/write inkvm_arm_set_fw_reg_bmap()?
If kvm_arm_set_fw_reg_bmap is changed to use WRITE_ONCE to update hvc_desc->hvc_*_bmap (kvm_arm_set_fw_reg_bmap still needs to get the lock to prevent other vCPUs from running KVM_RUN), I would think using READ_ONCE in kvm_arm_get_fw_reg_bmap() without getting the lock should work (will see either old or new value).
Thanks,
Reiji
Quoted 140 lines
Quoted 86 lines
Quoted 86 lines
arch/arm64/kvm/hypercalls.c +45
-0+} +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;u64 val;switch (reg->id) {@@ -224,6 +283,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 : +kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_std_bmap, &val); +break ;default :return -ENOENT ; }@@ -234,6 +296,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; + +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); + +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. +*/ +if (kvm_vm_has_started (kvm)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + + *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;@@ -310,6 +409,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 ; }arch/arm64/kvm/trng.c +0
-5@@ -60,14 +60,8 @@ int kvm_trng_call(struct kvm_vcpu *vcpu) val =ARM_SMCCC_TRNG_VERSION_1_0 ;break ;case ARM_SMCCC_TRNG_FEATURES : -switch (smccc_get_arg1 (vcpu)) { -case ARM_SMCCC_TRNG_VERSION : -case ARM_SMCCC_TRNG_FEATURES : -case ARM_SMCCC_TRNG_GET_UUID : -case ARM_SMCCC_TRNG_RND32 :Quoted 9 lines
arch/arm64/kvm/trng.c int kvm_trng_call(struct kvm_vcpu *vcpu) · line 64
Quoted 3 lines
arch/arm64/kvm/trng.c +1
-1-case ARM_SMCCC_TRNG_RND64 : +if (kvm_hvc_call_supported (vcpu,smccc_get_arg1 (vcpu))) val =TRNG_SUCCESS ;
kvm_hvc_call_supported()returns true for any values that are not explicitly listed inkvm_hvc_call_supported()(i.e. it returns true even for @func_id that are not any of ARM_SMCCC_TRNG_*). So, I don't think it can simply use the currentkvm_hvc_call_supported.You are right. Probably I should leave the case statements as is (or think of some better way).
Thanks for the review and suggestions.
Regards,
RaghavendraQuoted 37 lines
Thanks,
ReijiQuoted 34 lines
arch/arm64/kvm/trng.c +0
-1- }break ;case ARM_SMCCC_TRNG_GET_UUID : smccc_set_retval(vcpu, le32_to_cpu(u[0]), le32_to_cpu(u[1]),include/kvm/arm_hypercalls.h +6
-1@@ -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,9 +45,12 @@ 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_num_fw_bmap_regs (void );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); +bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id);#endif --2.34.1.448.ga2b2bfdf31-goog
Re: [RFC PATCH v3 04/11] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Quoted 162 lines · Reiji Watanabe
Quoted 138 lines · Raghavendra Rao Ananta
Quoted 123 lines · Reiji Watanabe
Hi Raghu,
Quoted 107 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the Virtual Machine Manager (VMM). 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 VMM to elect the services that it wishes the guest to discover. VMM 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.
The capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, is used to announce this extension, which returns the number of psuedo-firmware registers supported. 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 viaGET_ONE_REG, and writeback the desired values usingSET_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.
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. 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 | 4 ++ arch/arm64/kvm/arm.c | 4 ++ arch/arm64/kvm/hypercalls.c | 103 +++++++++++++++++++++++++++++- arch/arm64/kvm/trng.c | 8 +-- include/kvm/arm_hypercalls.h | 6 ++ 6 files changed, 129 insertions(+), 8 deletions(-)arch/arm64/include/asm/kvm_host.h +12
-0@@ -102,6 +102,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;@@ -137,6 +146,9 @@ struct kvm_arch {/* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + +/* Hypercall firmware register' descriptor */ +struct kvm_hvc_desc hvc_desc; };struct kvm_vcpu_fault_info {arch/arm64/include/uapi/asm/kvm.h +4
-0@@ -281,6 +281,10 @@ 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) +#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_REG(3) +#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 +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:arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 289
Quoted 6 lines
arch/arm64/kvm/arm.c +3
-0@@ -283,6 +284,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC : r =system_has_full_ptr_auth ();break ; +case KVM_CAP_ARM_HVC_FW_REG_BMAP : + r =kvm_arm_num_fw_bmap_regs (); +break ;Looking at the discussion for the v2 series,
Quoted 13 lines
I assume that the number of the pseudo-firmware bitmap registers will be used to clear pseudo firmware bitmap registers that userspace doesn't know.
I'm wondering how userspace can identify which pseudo-firmware registers thatKVM_GET_REG_LISTprovides are the pseudo-firmware bitmap registers that it doesn't know.
For instance, suppose pseudo-firmware registers thatKVM_GET_REG_LISTprovides areKVM_REG_ARM_FW_REG(0)toKVM_REG_ARM_FW_REG(9), userspace doesn't knowsKVM_REG_ARM_FW_REG(6)toKVM_REG_ARM_FW_REG(9), andKVM_CAP_ARM_HVC_FW_REG_BMAPreturns 5, how can userspace identify remaining two bitmap registers from those 4 (fw-reg #6 to #9) firmware registers ?Quoted 7 lines
In v3, we leave the decision upto the userspace. If the userspace encounters a register that it's unaware, it can choose either to clear it or let it get exposed to the guest as is (see the code snipped shared by Andrew in the link).
Trying to understand the question better- are you asking how would userspace distinguish between bitmap and regular fw registers with intermixed sequence numbers?Yes, that's my question.
Quoted 2 lines
If yes, do you foresee a reason why they 'unaware' registers needed to be treated differently?
Since I'm not sure what the specification of 'unaware' (non-bitmap) registers will be, it would be safer for us to assume that they might need to be treated differently from the bitmap registers. Considering there is
KVM_REG_ARM_PSCI_VERSION, which KVM doesn't allow userspace to set to 0, there might be similar registers that userspace cannot set to 0 in the future.
Quoted 6 lines
BTW, If you assume that all those 'unaware' firmware registers are treated in the same way, I don't think userspace needs the number of those bitmap registers from
KVM_CAP_ARM_HVC_FW_REG_BMAP(Instead, I would think it can handle the 'unaware' registers with a list of firmware registers fromKVM_GET_REG_LIST).
You are right; mixing these registers would create an issue for the VMM. Instead, we can probably have a subset of the KVM_REG_ARM_FW_REG space dedicated for the bitmapped firmware registers, something like:
#define KVM_REG_ARM_FW_BMAP_BASE KVM_REG_ARM_FW_REG(0xff00) /*
Upper half of the fw reg space */
#define KVM_REG_ARM_FW_BMAP_REG(r) (KVM_REG_ARM_FW_BMAP_BASE | (r))
#define KVM_REG_ARM_STD_BMAP KVM_REG_ARM_FW_BMAP_REG(0)
With this, I think the VMM can easily detect a bitmap fw register. Also, if it encounters an unknown bitmapped register it can handle it separately if it likes.
The minor advantage of the CAP still returning the number of bitmapped registers can be an inexpensive shortcut to VMM to get a general idea of the number of registers.
Quoted 136 lines
Quoted 42 lines
Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/arm.c +0
-0default : r =0 ; }arch/arm64/kvm/hypercalls.c +18
-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; +} + +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) { +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 :Quoted 8 lines
arch/arm64/kvm/hypercalls.c static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) · line 82
Quoted 4 lines
arch/arm64/kvm/hypercalls.c +4
-0+/* By default, allow the services that aren't listed here */ +return true; + } +}
kvm_hvc_call_supported()could return true even for @func_id thatkvm_hvc_call_handler()returns-EINVALfor. Is this behavior what you really want ?Yes. My idea was to let
kvm_hvc_call_supported()check for the support, whilekvm_hvc_call_handler()does the real processing of the call.Quoted 7 lines
Quoted 4 lines
If so, IMHO the function name might be a bit mis-leading. "
kvm_hvc_call_disabled" (and flip the return value) might be closer to what it does(?).Sorry, I'm unclear how flipping is helping. Wouldn't we return 'false' if we don't have a case for the
func_id, indicating it's NOT disabled, butkvm_hvc_call_handler()can still returnSMCCC_RET_NOT_SUPPORTED?Yes, that's fine, too.
Since those services are disabled (because they are enabled by default), I just thought checking 'disabled' might be closer to what it does than checking 'enabled'. But, 'enabled' is also fine.Quoted 77 lines
Quoted 66 lines
Quoted 66 lines
arch/arm64/kvm/hypercalls.c +27
-1+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 ; }@@ -153,9 +180,25 @@ static const u64 kvm_arm_fw_reg_ids[] = { KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, }; +static const u64 kvm_arm_fw_reg_bmap_ids[] = { +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_num_fw_bmap_regs (void ) +{ +return ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); +} +int kvm_arm_get_fw_num_regs (struct kvm_vcpu *vcpu) { -return ARRAY_SIZE (kvm_arm_fw_reg_ids); +return ARRAY_SIZE (kvm_arm_fw_reg_ids) +kvm_arm_num_fw_bmap_regs (); }int kvm_arm_copy_fw_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices)@@ -167,6 +210,11 @@ int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)return -EFAULT ; } +for (i =0 ; i <ARRAY_SIZE (kvm_arm_fw_reg_bmap_ids); i++) { +if (put_user (kvm_arm_fw_reg_bmap_ids[i], uindices++)) +return -EFAULT ; + } +return 0 ; }@@ -211,9 +259,20 @@ static int get_kernel_wa_level(u64 regid)return -EINVAL ; }Quoted 11 lines
arch/arm64/kvm/hypercalls.c static int get_kernel_wa_level(u64 regid) · line 269
Quoted 8 lines
arch/arm64/kvm/hypercalls.c +8
-0+static void +kvm_arm_get_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 fw_reg_bmap,u64 *val) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&kvm->lock); + *val = fw_reg_bmap; +mutex_unlock (&kvm->lock);Why does it need to hold the lock ? (Wouldn't
READ_ONCEbe enough ?)Quoted 3 lines
I don't have much experience with
READ_ONCEat this point, but do you think this read can be protected again the read/write inkvm_arm_set_fw_reg_bmap()?
Quoted 6 lines
If
kvm_arm_set_fw_reg_bmapis changed to useWRITE_ONCEto update hvc_desc->hvc_*_bmap (kvm_arm_set_fw_reg_bmapstill needs to get the lock to prevent other vCPUs from runningKVM_RUN), I would think usingREAD_ONCEinkvm_arm_get_fw_reg_bmap()without getting the lock should work (will see either old or new value).
That makes sense. Thanks for the suggestion and all the reviews.
Regards,
Raghavendra
Quoted 140 lines
Thanks,
ReijiQuoted 136 lines
Quoted 86 lines
Quoted 86 lines
arch/arm64/kvm/hypercalls.c +45
-0+} +int kvm_arm_get_fw_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) {void __user *uaddr = (void __user *)(long )reg->addr; +struct kvm_hvc_desc *hvc_desc = &vcpu->kvm->arch.hvc_desc;u64 val;switch (reg->id) {@@ -224,6 +283,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 : +kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_std_bmap, &val); +break ;default :return -ENOENT ; }@@ -234,6 +296,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; + +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); + +/* +* If the VM (any vCPU) has already started running, return success +* if there's no change in the value. Else, return -EBUSY. +*/ +if (kvm_vm_has_started (kvm)) { + ret = *fw_reg_bmap != val ? -EBUSY :0 ; + goto out; + } + + *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;@@ -310,6 +409,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 ; }arch/arm64/kvm/trng.c +0
-5@@ -60,14 +60,8 @@ int kvm_trng_call(struct kvm_vcpu *vcpu) val =ARM_SMCCC_TRNG_VERSION_1_0 ;break ;case ARM_SMCCC_TRNG_FEATURES : -switch (smccc_get_arg1 (vcpu)) { -case ARM_SMCCC_TRNG_VERSION : -case ARM_SMCCC_TRNG_FEATURES : -case ARM_SMCCC_TRNG_GET_UUID : -case ARM_SMCCC_TRNG_RND32 :Quoted 9 lines
arch/arm64/kvm/trng.c int kvm_trng_call(struct kvm_vcpu *vcpu) · line 64
Quoted 3 lines
arch/arm64/kvm/trng.c +1
-1-case ARM_SMCCC_TRNG_RND64 : +if (kvm_hvc_call_supported (vcpu,smccc_get_arg1 (vcpu))) val =TRNG_SUCCESS ;
kvm_hvc_call_supported()returns true for any values that are not explicitly listed inkvm_hvc_call_supported()(i.e. it returns true even for @func_id that are not any of ARM_SMCCC_TRNG_*). So, I don't think it can simply use the currentkvm_hvc_call_supported.You are right. Probably I should leave the case statements as is (or think of some better way).
Thanks for the review and suggestions.
Regards,
RaghavendraQuoted 33 lines
Thanks,
ReijiQuoted 30 lines
arch/arm64/kvm/trng.c +0
-1- }break ;case ARM_SMCCC_TRNG_GET_UUID : smccc_set_retval(vcpu, le32_to_cpu(u[0]), le32_to_cpu(u[1]),include/kvm/arm_hypercalls.h +6
-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,9 +45,12 @@ 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_num_fw_bmap_regs (void );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); +bool kvm_hvc_call_supported (struct kvm_vcpu *vcpu,u32 func_id);
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
include/kvm/arm_hypercalls.h +0
-1#endif --2.34.1.448.ga2b2bfdf31-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [RFC PATCH v3 04/11] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/hypercalls.c static int get_kernel_wa_level(u64 regid) · line 269
Quoted 8 lines
arch/arm64/kvm/hypercalls.c +8
-0+static void +kvm_arm_get_fw_reg_bmap (struct kvm_vcpu *vcpu,u64 fw_reg_bmap,u64 *val) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&kvm->lock); + *val = fw_reg_bmap; +mutex_unlock (&kvm->lock);
I have another comment for kvm_arm_get_fw_reg_bmap.
I just noticed that @fw_reg_bmap is a value of the bitmap register (not a pointer). I believe what you meant was a pointer to hvc_desc->hvc_*_bmap. Also, you can remove @val and return the register value instead (change the type of the return value from void to u64). I'm not sure if you will keep this function in the next version though.
Thanks,
Reiji
Re: [RFC PATCH v3 04/11] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
In reply to Raghavendra Rao AnantaQuoted 9 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the Virtual Machine Manager (VMM). 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.
Quoted 5 lines
As a result, there's a need for the VMM to elect the services that it wishes the guest to discover. VMM 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.
Haven't gone through the series but I wonder whether it's better to have a (e)BPF filter for this like seccomp.
Thanks
Quoted 25 lines
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.
The capability,
KVM_CAP_ARM_HVC_FW_REG_BMAP, is used to announce this extension, which returns the number of psuedo-firmware registers supported. 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 viaGET_ONE_REG, and writeback the desired values usingSET_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.
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. Other services are momentarily added in the upcoming patches.
Signed-off-by Raghavendra Rao Ananta<[email protected]>
Marc Zyngier
maintainerRe: [RFC PATCH v3 04/11] KVM: arm64: Setup a framework for hypercall bitmap firmware registers
Quoted 20 lines · Jason Wang
Quoted 9 lines · Raghavendra Rao Ananta
KVM regularly introduces new hypercall services to the guests without any consent from the Virtual Machine Manager (VMM). 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.
Quoted 5 lines
As a result, there's a need for the VMM to elect the services that it wishes the guest to discover. VMM 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.
Quoted 2 lines
Haven't gone through the series but I wonder whether it's better to have a (e)BPF filter for this like seccomp.
No, please. This has to fit in the save/restore model, and should be under control of the VMM. If you want to filter things using seccomp, that's fine, but also that's completely orthogonal.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
RFC PATCH v3 5/11 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.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/hypercalls.c | 17 ++++++++++++++++- arch/arm64/kvm/pvtime.c | 3 +++ include/kvm/arm_hypercalls.h | 3 +++ 5 files changed, 28 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -106,9 +106,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
@@ -285,6 +285,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_REG(4) +#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/hypercalls.c +16 -1
@@ -75,6 +75,10 @@ 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;
@@ -134,7 +138,8 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) }break ;case ARM_SMCCC_HV_PV_TIME_FEATURES : - val[0 ] =SMCCC_RET_SUCCESS ; +if (kvm_hvc_call_supported (vcpu, feature)) + val[0 ] =SMCCC_RET_SUCCESS ;break ; }break ;
@@ -182,6 +187,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {static const u64 kvm_arm_fw_reg_bmap_ids[] = {KVM_REG_ARM_STD_BMAP , +KVM_REG_ARM_STD_HYP_BMAP , };void kvm_arm_init_hypercalls (struct kvm *kvm)
@@ -189,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_num_fw_bmap_regs (void )
@@ -286,6 +293,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_BMAP :kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_std_bmap, &val);break ; +case KVM_REG_ARM_STD_HYP_BMAP : +kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_std_hyp_bmap, &val); +break ;default :return -ENOENT ; }
@@ -308,6 +318,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 ; }
@@ -410,6 +424,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 ;
arch/arm64/kvm/pvtime.c +3 -0
@@ -37,6 +37,9 @@ long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)u32 feature =smccc_get_arg1 (vcpu);long val =SMCCC_RET_NOT_SUPPORTED ; +if (!kvm_hvc_call_supported (vcpu, feature)) +return val; +switch (feature) {case ARM_SMCCC_HV_PV_TIME_FEATURES :case ARM_SMCCC_HV_PV_TIME_ST :
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.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 6/11 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.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 4 ++++ arch/arm64/kvm/hypercalls.c | 23 ++++++++++++++++++++++- include/kvm/arm_hypercalls.h | 3 +++ 4 files changed, 31 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -107,10 +107,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
@@ -289,6 +289,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_REG(5) +#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/hypercalls.c +22 -1
@@ -79,6 +79,9 @@ 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;
@@ -87,6 +90,7 @@ 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;
@@ -159,7 +163,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 +199,7 @@ static const u64 kvm_arm_fw_reg_ids[] = {static const u64 kvm_arm_fw_reg_bmap_ids[] = {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 +208,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_num_fw_bmap_regs (void )
@@ -296,6 +309,9 @@ int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)case KVM_REG_ARM_STD_HYP_BMAP :kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_std_hyp_bmap, &val);break ; +case KVM_REG_ARM_VENDOR_HYP_BMAP : +kvm_arm_get_fw_reg_bmap (vcpu, hvc_desc->hvc_vendor_hyp_bmap, &val); +break ;default :return -ENOENT ; }
@@ -322,6 +338,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 ; }
@@ -425,6 +445,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.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 7/11 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.
--- Documentation/virt/kvm/arm/psci.rst | 85 +++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 17 deletions(-)
Documentation/virt/kvm/arm/psci.rst +68 -17
@@ -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. -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,55 @@ 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. + +A new KVM capability, KVM_CAP_ARM_HVC_FW_REG_BMAP, is introduced to let +user-space know of this extension. A simple 'read' of the capability would +return the number of bitmapped registers. The user-space is expected to +make a not of this value and configure each of the register. + +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 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.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 8/11 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.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 9/11 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.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 10/11 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 | 358 ++++++++++++++++++ 3 files changed, 360 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 /s390x/memop
tools/testing/selftests/kvm/Makefile +1 -0
@@ -94,6 +94,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 += demand_paging_test
tools/testing/selftests/kvm/aarch64/hypercalls.c new file +358 -0
358 lines not on this page · raw message
Signature · 6 lines
2.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
RFC PATCH v3 11/11 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_REG (3 ),/* KVM_REG_ARM_STD_BMAP */ +KVM_REG_ARM_FW_REG (4 ),/* KVM_REG_ARM_STD_HYP_BMAP */ +KVM_REG_ARM_FW_REG (5 ),/* 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.34.1.448.ga2b2bfdf31-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm