Skip to contentSkip to conversation

Attempt at Mac OS X acceleration using "Hypervisor.Framwork"

Qemu-devel Assaf Gordon 3 participants 3 messages Nov 12, 2015 Kbuild

lore
Share & tools
lore
#0 lore raw

Hello,

Starting with Mac OS X version 10.10.3, Apple provides a system-level hypervisor framework (similar to KVM's kernel driver functionality on Linux).

a good write-up here:
http://www.pagetable.com/?p=764
http://www.pagetable.com/?p=831

a working hypervisor (forked from BSD's bhyve) is here:
https://github.com/mist64/xhyve

This attached patch attempts to provide the infrastructure glue for qemu to use this framework (when compiled on Mac OS X). It is not yet functional, but enables the following:

  ./configure --enable-hvf --target-list=x86_64-softmmu
  qemu-system-x86_64 -machine accel=hvf [...]

Please advise as to whether this is the right approach, and whether it's worth pursuing. Comments welcomed,
- assaf

From 5825d45140cc7a935d5c35198d4ae41d961252c9 Mon Sep 17 00:00:00 2001
From: Assaf Gordon <[email protected]>
Date: Thu, 12 Nov 2015 12:33:12 +0200
Subject: [PATCH] STUB for Mac OS X Hypervisor.Framework accelerator

An attempt to add stub code for future support of Mac OS X Hypervisor.Framework (avaialble on OS X>=10.10.3).

Usage will be:
./configure --enable-hvf --target-list=x86_64-softmmu qemu-system-x86_64 -machine accel=hvf [...]

* configure: new variable 'hvf'.
accept --{enable,disable}-hvf.
write CONFIG_HVF=y to <target>/config-target.mak files. (During development, don't limit to Darwin hosts.)
* Makefile.target: compile and link either hvf-stub.o or hvf-all.o based on CONFIG_HVF.
* cpus.c: qemu_init_vcpu(): use hvf functions. qemu_hvf_start_vcpu(), qemu_hvf_cpu_thread_fn() - use hvf accelerator.
* include/sysemu/hvf{-int}.h: stub header files for hvf.
* hvf-stub.c: mimic kvm-stub.c, to be compiled with CONFIG_HVF=n .
* hvf-all.c: stub for Hypervisor.Framework accelerator.

---
 Makefile.target          |   2 +
 configure                |  42 +++++++++++
 cpus.c                   |  57 +++++++++++++++
 hvf-all.c                | 179 +++++++++++++++++++++++++++++++++++++++++++++++
 hvf-stub.c               |  30 ++++++++
 include/qom/cpu.h        |   4 ++
 include/sysemu/hvf.h     |  49 +++++++++++++
 include/sysemu/hvf_int.h |  21 ++++++
 8 files changed, 384 insertions(+)
 create mode 100644 hvf-all.c
 create mode 100644 hvf-stub.c
 create mode 100644 include/sysemu/hvf.h
 create mode 100644 include/sysemu/hvf_int.h
Makefile.target +2 -0
index 962d004..5bba20a 100644
@@ -96,6 +96,7 @@ obj-y += target-$(TARGET_BASE_ARCH)/
 obj-y += disas.o
 obj-$(call notempty,$(TARGET_XML_FILES)) += gdbstub-xml.o
 obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
+obj-$(call lnot,$(CONFIG_HVF)) += hvf-stub.o
 
 obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decContext.o
 obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decNumber.o
@@ -135,6 +136,7 @@ obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o numa.o
 obj-y += qtest.o bootdevice.o
 obj-y += hw/
 obj-$(CONFIG_KVM) += kvm-all.o
+obj-$(CONFIG_HVF) += hvf-all.o
 obj-y += memory.o cputlb.o
 obj-y += memory_mapping.o
 obj-y += dump.o
configure +42 -0

78 lines not on this page · raw message

cpus.c +57 -0

81 lines not on this page · raw message

hvf-all.c new file +179 -0

179 lines not on this page · raw message

hvf-stub.c new file +30 -0
new file mode 100644
index 0000000..8ba0371
@@ -0,0 +1,30 @@
+/*
+ * QEMU HVF stub
+ *
+ * Copyright (C) 2015 Assaf Gordon
+ *
+ * Based on kvm-stub.c:
+ *   Copyright Red Hat, Inc. 2010
+ *   Author: Paolo Bonzini     <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "hw/hw.h"
+#include "cpu.h"
+#include "sysemu/hvf.h"
+
+bool hvf_allowed;
+
+int hvf_init_vcpu(CPUState *cpu)
+{
+    return -ENOSYS;
+}
+
+int hvf_cpu_exec(CPUState *cpu)
+{
+    abort();
+}
include/qom/cpu.h +4 -0
index 51a1323..544f8d8 100644
@@ -207,6 +207,8 @@ typedef struct CPUWatchpoint {
 struct KVMState;
 struct kvm_run;
 
+struct HVFState;
+
 #define TB_JMP_CACHE_BITS 12
 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
 
@@ -314,6 +316,8 @@ struct CPUState {
     struct KVMState *kvm_state;
     struct kvm_run *kvm_run;
 
+    struct HVFState *hvf_state;
+
     /* TODO Move common fields from CPUArchState here. */
     int cpu_index; /* used by alpha TCG */
     uint32_t halted; /* used by alpha, cris, ppc TCG */
include/sysemu/hvf.h new file +49 -0

49 lines not on this page · raw message

include/sysemu/hvf_int.h new file +21 -0
new file mode 100644
index 0000000..ffab64a
@@ -0,0 +1,21 @@
+/*
+ * Internal definitions for a target's HVF support
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_HVF_INT_H
+#define QEMU_HVF_INT_H
+
+#include "sysemu/sysemu.h"
+#include "sysemu/accel.h"
+#include "sysemu/hvf.h"
+
+#define TYPE_HVF_ACCEL ACCEL_CLASS_NAME("hvf")
+
+#define HVF_STATE(obj) \
+    OBJECT_CHECK(HVFState, (obj), TYPE_HVF_ACCEL)
+
+#endif
Signature · 1 line
2.4.3
#1 lore raw
Quoted 8 lines · Assaf Gordon

Starting with Mac OS X version 10.10.3, Apple provides a system-level hypervisor framework (similar to KVM's kernel driver functionality on Linux).

This attached patch attempts to provide the infrastructure glue for qemu to use this framework (when compiled on Mac OS X). It is not yet functional, but enables the following:

Quoted 5 lines
  ./configure --enable-hvf --target-list=x86_64-softmmu
  qemu-system-x86_64 -machine accel=hvf [...]

Please advise as to whether this is the right approach, and whether it's worth pursuing.

I think it would be nice to be able to support hardware acceleration on OSX hosts, and I think the official Apple hypervisor APIs for this are the right way to do that. The questions I have are:
* how much code are we going to end up with in QEMU to support that? (I've been told that the Apple APIs are very basic and low level, so a lot of the code that in KVM is in the kernel to handle irqchips, instruction emulation, etc, would need to also be in QEMU for the benefit of OSX. We could probably take a lot of that from KVM, but then maintenance and tracking bugfixes that go into the kernel code would be an immense pain in the future.)
* is the code going to come with somebody who will stick around and help us maintain it for the long term?
(At the moment OSX is only a minimally supported platform for QEMU, because so few of the core developers use it for day to day development, so we don't have the capacity to take on maintenance of a lot of new code without new people to help)

There doesn't look to be anything particularly problematic with the skeleton code in your patch, but I think it would be easier to judge how the design should look if we had a discussion of what the differences between the KVM API and the hypervisor framework API are, where that would need more code in QEMU and how we'd handle that.

thanks
-- PMM

#2 lore raw
Quoted 17 lines · Peter Maydell

I think it would be nice to be able to support hardware acceleration on OSX hosts, and I think the official Apple hypervisor APIs for this are the right way to do that. The questions I have are:
* how much code are we going to end up with in QEMU to support that? (I've been told that the Apple APIs are very basic and low level, so a lot of the code that in KVM is in the kernel to handle irqchips, instruction emulation, etc, would need to also be in QEMU for the benefit of OSX. We could probably take a lot of that from KVM, but then maintenance and tracking bugfixes that go into the kernel code would be an immense pain in the future.)
* is the code going to come with somebody who will stick around and help us maintain it for the long term?
(At the moment OSX is only a minimally supported platform for QEMU, because so few of the core developers use it for day to day development, so we don't have the capacity to take on maintenance of a lot of new code without new people to help)

Quoted 6 lines

There doesn't look to be anything particularly problematic with the skeleton code in your patch, but I think it would be easier to judge how the design should look if we had a discussion of what the differences between the KVM API and the hypervisor framework API are, where that would need more code in QEMU and how we'd handle that.

It's completely different. The KVM API is much more high-level, while Hypervisor.framework exposes VMX directly with only a tiny C API on top.

It's not a lot of code if performance does not matter too much. The main complication would be support for dirty bitmaps, but apart from that it would be very straightforward. You do not need to support anything that lacks extended page tables, and that alone makes things a lot easier.

The other problematic issue is support for CPUID, which is a pain with KVM as well.

I cannot contribute due to lack of hardware, but I'm happy to review the patches and, when things break, suggest how to debug it and even prepare small patches.

I think it's a very important feature for QEMU. Speed is the #1 reason why people use VirtualBox instead of QEMU. Similarly, it would be nice to support HAXM on Windows.

Paolo