kvm: nested: Introduce read_and_check_msr_entry()
authorAaron Lewis <aaronlewis@google.com>
Fri, 8 Nov 2019 05:14:36 +0000 (21:14 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 15 Nov 2019 10:44:18 +0000 (11:44 +0100)
Add the function read_and_check_msr_entry() which just pulls some code
out of nested_vmx_store_msr().  This will be useful as reusable code in
upcoming patches.

Reviewed-by: Liran Alon <liran.alon@oracle.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Signed-off-by: Aaron Lewis <aaronlewis@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/vmx/nested.c

index c6f5e5821d4c34ba27f226f9f1b85672ce84a8c7..3ef529cc72fb88f538f320633485f6971254fbac 100644 (file)
@@ -940,6 +940,26 @@ fail:
        return i + 1;
 }
 
+static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
+                                    struct vmx_msr_entry *e)
+{
+       if (kvm_vcpu_read_guest(vcpu,
+                               gpa + i * sizeof(*e),
+                               e, 2 * sizeof(u32))) {
+               pr_debug_ratelimited(
+                       "%s cannot read MSR entry (%u, 0x%08llx)\n",
+                       __func__, i, gpa + i * sizeof(*e));
+               return false;
+       }
+       if (nested_vmx_store_msr_check(vcpu, e)) {
+               pr_debug_ratelimited(
+                       "%s check failed (%u, 0x%x, 0x%x)\n",
+                       __func__, i, e->index, e->reserved);
+               return false;
+       }
+       return true;
+}
+
 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
 {
        u64 data;
@@ -951,20 +971,9 @@ static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
                if (unlikely(i >= max_msr_list_size))
                        return -EINVAL;
 
-               if (kvm_vcpu_read_guest(vcpu,
-                                       gpa + i * sizeof(e),
-                                       &e, 2 * sizeof(u32))) {
-                       pr_debug_ratelimited(
-                               "%s cannot read MSR entry (%u, 0x%08llx)\n",
-                               __func__, i, gpa + i * sizeof(e));
+               if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
                        return -EINVAL;
-               }
-               if (nested_vmx_store_msr_check(vcpu, &e)) {
-                       pr_debug_ratelimited(
-                               "%s check failed (%u, 0x%x, 0x%x)\n",
-                               __func__, i, e.index, e.reserved);
-                       return -EINVAL;
-               }
+
                if (kvm_get_msr(vcpu, e.index, &data)) {
                        pr_debug_ratelimited(
                                "%s cannot read MSR (%u, 0x%x)\n",