This can be used by the introspection tool to emulate SSE instructions.

Signed-off-by: Adalbert Lazăr <ala...@bitdefender.com>
---
 Documentation/virt/kvm/kvmi.rst               | 28 +++++++++++++++++++
 arch/x86/include/uapi/asm/kvmi.h              |  4 +++
 arch/x86/kvm/kvmi_msg.c                       | 21 ++++++++++++++
 include/uapi/linux/kvmi.h                     |  1 +
 .../testing/selftests/kvm/x86_64/kvmi_test.c  | 27 ++++++++++++++----
 5 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/Documentation/virt/kvm/kvmi.rst b/Documentation/virt/kvm/kvmi.rst
index c1ac47def4e9..56efeeb38980 100644
--- a/Documentation/virt/kvm/kvmi.rst
+++ b/Documentation/virt/kvm/kvmi.rst
@@ -859,6 +859,34 @@ Returns a buffer containing the XSAVE area.
 * -KVM_EAGAIN - the selected vCPU can't be introspected yet
 * -KVM_ENOMEM - there is not enough memory to allocate the reply
 
+20. KVMI_VCPU_SET_XSAVE
+-----------------------
+
+:Architectures: x86
+:Versions: >= 1
+:Parameters:
+
+::
+
+       struct kvmi_vcpu_hdr;
+       struct kvmi_vcpu_set_xsave {
+               struct kvm_xsave xsave;
+       };
+
+:Returns:
+
+::
+
+       struct kvmi_error_code;
+
+Modifies the XSAVE area.
+
+: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
+
 Events
 ======
 
diff --git a/arch/x86/include/uapi/asm/kvmi.h b/arch/x86/include/uapi/asm/kvmi.h
index 0d3696c52d88..6ec290b69b46 100644
--- a/arch/x86/include/uapi/asm/kvmi.h
+++ b/arch/x86/include/uapi/asm/kvmi.h
@@ -115,4 +115,8 @@ struct kvmi_vcpu_get_xsave_reply {
        struct kvm_xsave xsave;
 };
 
+struct kvmi_vcpu_set_xsave {
+       struct kvm_xsave xsave;
+};
+
 #endif /* _UAPI_ASM_X86_KVMI_H */
diff --git a/arch/x86/kvm/kvmi_msg.c b/arch/x86/kvm/kvmi_msg.c
index 77c753cd9705..c1b3bd56a42c 100644
--- a/arch/x86/kvm/kvmi_msg.c
+++ b/arch/x86/kvm/kvmi_msg.c
@@ -213,6 +213,26 @@ static int handle_vcpu_get_xsave(const struct 
kvmi_vcpu_msg_job *job,
        return err;
 }
 
+static int handle_vcpu_set_xsave(const struct kvmi_vcpu_msg_job *job,
+                                const struct kvmi_msg_hdr *msg,
+                                const void *req)
+{
+       size_t req_size, msg_size = msg->size;
+       int ec = 0;
+
+       if (check_sub_overflow(msg_size, sizeof(struct kvmi_vcpu_hdr),
+                              &req_size))
+               return -EINVAL;
+
+       if (req_size < sizeof(struct kvm_xsave))
+               ec = -KVM_EINVAL;
+       else if (kvm_vcpu_ioctl_x86_set_xsave(job->vcpu,
+                                             (struct kvm_xsave *) req))
+               ec = -KVM_EINVAL;
+
+       return kvmi_msg_vcpu_reply(job, msg, ec, NULL, 0);
+}
+
 static kvmi_vcpu_msg_job_fct const msg_vcpu[] = {
        [KVMI_VCPU_CONTROL_CR]       = handle_vcpu_control_cr,
        [KVMI_VCPU_GET_CPUID]        = handle_vcpu_get_cpuid,
@@ -222,6 +242,7 @@ static kvmi_vcpu_msg_job_fct const msg_vcpu[] = {
        [KVMI_VCPU_GET_XSAVE]        = handle_vcpu_get_xsave,
        [KVMI_VCPU_INJECT_EXCEPTION] = handle_vcpu_inject_exception,
        [KVMI_VCPU_SET_REGISTERS]    = handle_vcpu_set_registers,
+       [KVMI_VCPU_SET_XSAVE]        = handle_vcpu_set_xsave,
 };
 
 kvmi_vcpu_msg_job_fct kvmi_arch_vcpu_msg_handler(u16 id)
diff --git a/include/uapi/linux/kvmi.h b/include/uapi/linux/kvmi.h
index e47c4ce0f8ed..3baf5c7842bb 100644
--- a/include/uapi/linux/kvmi.h
+++ b/include/uapi/linux/kvmi.h
@@ -46,6 +46,7 @@ enum {
        KVMI_VCPU_INJECT_EXCEPTION = KVMI_VCPU_MESSAGE_ID(7),
        KVMI_VCPU_GET_XCR          = KVMI_VCPU_MESSAGE_ID(8),
        KVMI_VCPU_GET_XSAVE        = KVMI_VCPU_MESSAGE_ID(9),
+       KVMI_VCPU_SET_XSAVE        = KVMI_VCPU_MESSAGE_ID(10),
 
        KVMI_NEXT_VCPU_MESSAGE
 };
diff --git a/tools/testing/selftests/kvm/x86_64/kvmi_test.c 
b/tools/testing/selftests/kvm/x86_64/kvmi_test.c
index 277b1061410b..45c1f3132a3c 100644
--- a/tools/testing/selftests/kvm/x86_64/kvmi_test.c
+++ b/tools/testing/selftests/kvm/x86_64/kvmi_test.c
@@ -1448,21 +1448,35 @@ static void test_cmd_vcpu_get_xcr(struct kvm_vm *vm)
        cmd_vcpu_get_xcr(vm, xcr1, &value, -KVM_EINVAL);
 }
 
-static void cmd_vcpu_get_xsave(struct kvm_vm *vm)
+static void cmd_vcpu_get_xsave(struct kvm_vm *vm, struct kvm_xsave *rpl)
 {
        struct {
                struct kvmi_msg_hdr hdr;
                struct kvmi_vcpu_hdr vcpu_hdr;
        } req = {};
-       struct kvm_xsave rpl;
 
        test_vcpu0_command(vm, KVMI_VCPU_GET_XSAVE, &req.hdr, sizeof(req),
-                          &rpl, sizeof(rpl), 0);
+                          rpl, sizeof(*rpl), 0);
 }
 
-static void test_cmd_vcpu_get_xsave(struct kvm_vm *vm)
+static void cmd_vcpu_set_xsave(struct kvm_vm *vm, struct kvm_xsave *rpl)
+{
+       struct {
+               struct kvmi_msg_hdr hdr;
+               struct kvmi_vcpu_hdr vcpu_hdr;
+               struct kvm_xsave xsave;
+       } req = {};
+
+       memcpy(&req.xsave, rpl, sizeof(*rpl));
+
+       test_vcpu0_command(vm, KVMI_VCPU_SET_XSAVE, &req.hdr, sizeof(req),
+                          NULL, 0, 0);
+}
+
+static void test_cmd_vcpu_xsave(struct kvm_vm *vm)
 {
        struct kvm_cpuid_entry2 *entry;
+       struct kvm_xsave xsave;
 
        entry = kvm_get_supported_cpuid_entry(1);
        if (!(entry->ecx & X86_FEATURE_XSAVE)) {
@@ -1470,7 +1484,8 @@ static void test_cmd_vcpu_get_xsave(struct kvm_vm *vm)
                return;
        }
 
-       cmd_vcpu_get_xsave(vm);
+       cmd_vcpu_get_xsave(vm, &xsave);
+       cmd_vcpu_set_xsave(vm, &xsave);
 }
 
 static void test_introspection(struct kvm_vm *vm)
@@ -1501,7 +1516,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);
+       test_cmd_vcpu_xsave(vm);
 
        unhook_introspection(vm);
 }
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Reply via email to