]> www.infradead.org Git - users/dwmw2/qemu.git/commitdiff
i386/xen: implement HYPERVISOR_sched_op
authorJoao Martins <joao.m.martins@oracle.com>
Fri, 20 Jul 2018 19:19:05 +0000 (15:19 -0400)
committerJoao Martins <joao.m.martins@oracle.com>
Tue, 19 Feb 2019 14:00:57 +0000 (09:00 -0500)
It allows to shutdown itself via hypercall with any of the 3 reasons:
  1) self-reboot
  2) shutdown
  3) crash

Implementing SCHEDOP_shutdown sub op let us handle crashes gracefully rather
than leading to triple faults if it remains unimplemented.

Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
target/i386/xen.c

index 6147a92ff660455587958344e2236b01dc09b425..90a0a52e029084c88b32ba24caecd2e7b48d74ea 100644 (file)
@@ -25,6 +25,7 @@
 #include "standard-headers/xen/hvm/hvm_op.h"
 #include "standard-headers/xen/hvm/params.h"
 #include "standard-headers/xen/vcpu.h"
+#include "standard-headers/xen/sched.h"
 #include "standard-headers/xen/event_channel.h"
 
 #define PAGE_OFFSET    0xffffffff80000000UL
@@ -528,6 +529,44 @@ static int kvm_xen_hcall_evtchn_op(struct kvm_xen_exit *exit,
     return err ? HCALL_ERR : 0;
 }
 
+static int schedop_shutdown(CPUState *cs, uint64_t arg)
+{
+    struct sched_shutdown *shutdown;
+
+    shutdown = gva_to_hva(cs, arg);
+    if (!shutdown) {
+        return -EFAULT;
+    }
+
+    if (shutdown->reason == SHUTDOWN_crash) {
+        cpu_dump_state(cs, stderr, fprintf, CPU_DUMP_CODE);
+        qemu_system_guest_panicked(NULL);
+    } else if (shutdown->reason == SHUTDOWN_reboot) {
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
+    } else if (shutdown->reason == SHUTDOWN_poweroff) {
+        qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+    }
+
+    return 0;
+}
+
+static int kvm_xen_hcall_sched_op(struct kvm_xen_exit *exit, X86CPU *cpu,
+                                  int cmd, uint64_t arg)
+{
+    CPUState *cs = CPU(cpu);
+    int err = -ENOSYS;
+
+    switch (cmd) {
+    case SCHEDOP_shutdown: {
+          err = schedop_shutdown(cs, arg);
+          break;
+       }
+    }
+
+    exit->u.hcall.result = err;
+    return err;
+}
+
 static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
 {
     uint16_t code = exit->u.hcall.input;
@@ -536,6 +575,10 @@ static int __kvm_xen_handle_exit(X86CPU *cpu, struct kvm_xen_exit *exit)
     case HVMOP_set_evtchn_upcall_vector:
         return kvm_xen_hcall_evtchn_upcall_vector(exit, cpu,
                                                   exit->u.hcall.params[0]);
+    case __HYPERVISOR_sched_op_compat:
+    case __HYPERVISOR_sched_op:
+        return kvm_xen_hcall_sched_op(exit, cpu, exit->u.hcall.params[0],
+                                      exit->u.hcall.params[1]);
     case __HYPERVISOR_event_channel_op_compat:
         return kvm_xen_hcall_evtchn_op_compat(exit, cpu,
                                               exit->u.hcall.params[0]);