From: Mihai Donțu <[email protected]>

This vCPU command is used to get the XSAVE area.

Signed-off-by: Mihai Donțu <[email protected]>
Co-developed-by: Adalbert Lazăr <[email protected]>
Signed-off-by: Adalbert Lazăr <[email protected]>
---
 Documentation/virt/kvm/kvmi.rst               | 31 +++++++++++++++++++
 arch/x86/include/uapi/asm/kvmi.h              |  4 +++
 arch/x86/kvm/kvmi.c                           | 24 ++++++++++++++
 include/uapi/linux/kvmi.h                     |  1 +
 .../testing/selftests/kvm/x86_64/kvmi_test.c  | 30 ++++++++++++++++++
 virt/kvm/introspection/kvmi_int.h             |  3 ++
 virt/kvm/introspection/kvmi_msg.c             | 16 ++++++++++
 7 files changed, 109 insertions(+)

diff --git a/Documentation/virt/kvm/kvmi.rst b/Documentation/virt/kvm/kvmi.rst
index 1fdb73c6a6ff..9bf8b08eb585 100644
--- a/Documentation/virt/kvm/kvmi.rst
+++ b/Documentation/virt/kvm/kvmi.rst
@@ -842,6 +842,37 @@ Returns the value of an extended control register XCR.
 * -KVM_EINVAL - the padding is not zero
 * -KVM_EAGAIN - the selected vCPU can't be introspected yet
 
+19. KVMI_VCPU_GET_XSAVE
+-----------------------
+
+:Architecture: x86
+:Versions: >= 1
+:Parameters:
+
+::
+
+       struct kvmi_vcpu_hdr;
+
+:Returns:
+
+::
+
+       struct kvmi_error_code;
+       struct kvmi_vcpu_get_xsave_reply {
+               __u32 region[0];
+       };
+
+Returns a buffer containing the XSAVE area. Currently, the size of
+``kvm_xsave`` is used, but it could change. The userspace should get
+the buffer size from the message size (kvmi_msg_hdr.size).
+
+:Errors:
+
+* -KVM_EINVAL - the selected vCPU is invalid
+* -KVM_EINVAL - the padding is not zero
+* -KVM_EAGAIN - the selected vCPU can't be introspected yet
+* -KVM_ENOMEM - there is not enough memory to allocate the reply
+
 Events
 ======
 
diff --git a/arch/x86/include/uapi/asm/kvmi.h b/arch/x86/include/uapi/asm/kvmi.h
index cc4cb85366c8..a165c259ba0e 100644
--- a/arch/x86/include/uapi/asm/kvmi.h
+++ b/arch/x86/include/uapi/asm/kvmi.h
@@ -99,4 +99,8 @@ struct kvmi_vcpu_get_xcr_reply {
        u64 value;
 };
 
+struct kvmi_vcpu_get_xsave_reply {
+       __u32 region[0];
+};
+
 #endif /* _UAPI_ASM_X86_KVMI_H */
diff --git a/arch/x86/kvm/kvmi.c b/arch/x86/kvm/kvmi.c
index ff70c9a33ccf..7b7122784f0f 100644
--- a/arch/x86/kvm/kvmi.c
+++ b/arch/x86/kvm/kvmi.c
@@ -737,3 +737,27 @@ int kvmi_arch_cmd_vcpu_get_xcr(struct kvm_vcpu *vcpu,
        rpl->value = vcpu->arch.xcr0;
        return 0;
 }
+
+int kvmi_arch_cmd_vcpu_get_xsave(struct kvm_vcpu *vcpu,
+                                struct kvmi_vcpu_get_xsave_reply **dest,
+                                size_t *dest_size)
+{
+       struct kvmi_vcpu_get_xsave_reply *rpl = NULL;
+       size_t rpl_size = sizeof(*rpl) + sizeof(struct kvm_xsave);
+       struct kvm_xsave *area;
+
+       if (!valid_reply_size(rpl_size))
+               return -KVM_EINVAL;
+
+       rpl = kvmi_msg_alloc();
+       if (!rpl)
+               return -KVM_ENOMEM;
+
+       area = (struct kvm_xsave *) &rpl->region[0];
+       kvm_vcpu_ioctl_x86_get_xsave(vcpu, area);
+
+       *dest = rpl;
+       *dest_size = rpl_size;
+
+       return 0;
+}
diff --git a/include/uapi/linux/kvmi.h b/include/uapi/linux/kvmi.h
index d1c564ff3d6c..6b5662f54ba1 100644
--- a/include/uapi/linux/kvmi.h
+++ b/include/uapi/linux/kvmi.h
@@ -40,6 +40,7 @@ enum {
        KVMI_VM_GET_MAX_GFN = 17,
 
        KVMI_VCPU_GET_XCR = 18,
+       KVMI_VCPU_GET_XSAVE = 19,
 
        KVMI_NUM_MESSAGES
 };
diff --git a/tools/testing/selftests/kvm/x86_64/kvmi_test.c 
b/tools/testing/selftests/kvm/x86_64/kvmi_test.c
index 8b92ff7cee6b..657684ec6ff5 100644
--- a/tools/testing/selftests/kvm/x86_64/kvmi_test.c
+++ b/tools/testing/selftests/kvm/x86_64/kvmi_test.c
@@ -1551,6 +1551,35 @@ static void test_cmd_vcpu_get_xcr(struct kvm_vm *vm)
        cmd_vcpu_get_xcr(vm, xcr1, &value, no_padding, -KVM_EINVAL);
 }
 
+static void cmd_vcpu_get_xsave(struct kvm_vm *vm)
+{
+       struct {
+               struct kvmi_msg_hdr hdr;
+               struct kvmi_vcpu_hdr vcpu_hdr;
+       } req = {};
+       struct kvm_xsave rpl;
+       int r;
+
+       r = do_vcpu0_command(vm, KVMI_VCPU_GET_XSAVE, &req.hdr, sizeof(req),
+                            &rpl, sizeof(rpl));
+       TEST_ASSERT(r == 0,
+                   "KVMI_VCPU_GET_XSAVE failed with error %d (%s)\n",
+                   -r, kvm_strerror(-r));
+}
+
+static void test_cmd_vcpu_get_xsave(struct kvm_vm *vm)
+{
+       struct kvm_cpuid_entry2 *entry;
+
+       entry = kvm_get_supported_cpuid_entry(1);
+       if (!(entry->ecx & X86_FEATURE_XSAVE)) {
+               print_skip("XSAVE not supported, ecx 0x%x", entry->ecx);
+               return;
+       }
+
+       cmd_vcpu_get_xsave(vm);
+}
+
 static void test_introspection(struct kvm_vm *vm)
 {
        srandom(time(0));
@@ -1579,6 +1608,7 @@ static void test_introspection(struct kvm_vm *vm)
        test_cmd_vm_get_max_gfn();
        test_event_xsetbv(vm);
        test_cmd_vcpu_get_xcr(vm);
+       test_cmd_vcpu_get_xsave(vm);
 
        unhook_introspection(vm);
 }
diff --git a/virt/kvm/introspection/kvmi_int.h 
b/virt/kvm/introspection/kvmi_int.h
index 0b84ac365ae7..26fc27f3fddc 100644
--- a/virt/kvm/introspection/kvmi_int.h
+++ b/virt/kvm/introspection/kvmi_int.h
@@ -108,5 +108,8 @@ void kvmi_arch_inject_exception(struct kvm_vcpu *vcpu);
 int kvmi_arch_cmd_vcpu_get_xcr(struct kvm_vcpu *vcpu,
                               const struct kvmi_vcpu_get_xcr *req,
                               struct kvmi_vcpu_get_xcr_reply *rpl);
+int kvmi_arch_cmd_vcpu_get_xsave(struct kvm_vcpu *vcpu,
+                                struct kvmi_vcpu_get_xsave_reply **dest,
+                                size_t *dest_size);
 
 #endif
diff --git a/virt/kvm/introspection/kvmi_msg.c 
b/virt/kvm/introspection/kvmi_msg.c
index aa4fb202115e..06a8fbb5c86d 100644
--- a/virt/kvm/introspection/kvmi_msg.c
+++ b/virt/kvm/introspection/kvmi_msg.c
@@ -538,6 +538,21 @@ static int handle_vcpu_get_xcr(const struct 
kvmi_vcpu_msg_job *job,
        return kvmi_msg_vcpu_reply(job, msg, ec, &rpl, sizeof(rpl));
 }
 
+static int handle_vcpu_get_xsave(const struct kvmi_vcpu_msg_job *job,
+                                const struct kvmi_msg_hdr *msg,
+                                const void *req)
+{
+       struct kvmi_vcpu_get_xsave_reply *rpl = NULL;
+       size_t rpl_size = 0;
+       int err, ec;
+
+       ec = kvmi_arch_cmd_vcpu_get_xsave(job->vcpu, &rpl, &rpl_size);
+
+       err = kvmi_msg_vcpu_reply(job, msg, ec, rpl, rpl_size);
+       kvmi_msg_free(rpl);
+       return err;
+}
+
 /*
  * These functions are executed from the vCPU thread. The receiving thread
  * passes the messages using a newly allocated 'struct kvmi_vcpu_msg_job'
@@ -553,6 +568,7 @@ static int(*const msg_vcpu[])(const struct 
kvmi_vcpu_msg_job *,
        [KVMI_VCPU_GET_INFO]         = handle_vcpu_get_info,
        [KVMI_VCPU_GET_REGISTERS]    = handle_vcpu_get_registers,
        [KVMI_VCPU_GET_XCR]          = handle_vcpu_get_xcr,
+       [KVMI_VCPU_GET_XSAVE]        = handle_vcpu_get_xsave,
        [KVMI_VCPU_INJECT_EXCEPTION] = handle_vcpu_inject_exception,
        [KVMI_VCPU_SET_REGISTERS]    = handle_vcpu_set_registers,
 };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Reply via email to