From 0c5614bfbb168dcd4c7592299923dc9991f789cc Mon Sep 17 00:00:00 2001 From: David Wisner Date: Sun, 24 Jul 2016 14:17:55 -0700 Subject: [PATCH 1/5] updated for Sierra --- include/xhyve/vmm/io/vlapic_priv.h | 3 ++- src/vmm/io/vatpic.c | 15 ++++++----- src/vmm/io/vatpit.c | 11 ++++---- src/vmm/io/vioapic.c | 11 ++++---- src/vmm/io/vlapic.c | 43 +++++++++++++++--------------- src/vmm/vmm.c | 24 +++++++++-------- 6 files changed, 57 insertions(+), 50 deletions(-) diff --git a/include/xhyve/vmm/io/vlapic_priv.h b/include/xhyve/vmm/io/vlapic_priv.h index 62d28117..59a8a773 100644 --- a/include/xhyve/vmm/io/vlapic_priv.h +++ b/include/xhyve/vmm/io/vlapic_priv.h @@ -33,6 +33,7 @@ #include #include #include +#include /* * APIC Register: Offset Description @@ -163,7 +164,7 @@ struct vlapic { struct bintime timer_fire_bt; /* callout expiry time */ struct bintime timer_freq_bt; /* timer frequency */ struct bintime timer_period_bt; /* timer period */ - OSSpinLock timer_lock; + os_unfair_lock timer_lock; /* * The 'isrvec_stk' is a stack of vectors injected by the local apic. * A vector is popped from the stack when the processor does an EOI. diff --git a/src/vmm/io/vatpic.c b/src/vmm/io/vatpic.c index 0b0bb258..cdf64944 100644 --- a/src/vmm/io/vatpic.c +++ b/src/vmm/io/vatpic.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -38,9 +39,9 @@ #include #include -#define VATPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; -#define VATPIC_LOCK(v) OSSpinLockLock(&(v)->lock) -#define VATPIC_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) +// #define VATPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VATPIC_LOCK(v) os_unfair_lock_lock(&(v)->lock) +#define VATPIC_UNLOCK(v) os_unfair_lock_unlock(&(v)->lock) enum irqstate { IRQSTATE_ASSERT, @@ -70,7 +71,7 @@ struct atpic { struct vatpic { struct vm *vm; - OSSpinLock lock; + os_unfair_lock lock; struct atpic atpic[2]; uint8_t elc[2]; }; @@ -700,11 +701,11 @@ vatpic_master_handler(struct vm *vm, UNUSED int vcpuid, bool in, int port, if (bytes != 1) return (-1); - + if (in) { return (vatpic_read(vatpic, atpic, in, port, bytes, eax)); } - + return (vatpic_write(vatpic, atpic, in, port, bytes, eax)); } @@ -780,7 +781,7 @@ vatpic_init(struct vm *vm) bzero(vatpic, sizeof(struct vatpic)); vatpic->vm = vm; - VATPIC_LOCK_INIT(vatpic); + // VATPIC_LOCK_INIT(vatpic); return (vatpic); } diff --git a/src/vmm/io/vatpit.c b/src/vmm/io/vatpit.c index 5c59425f..28710df0 100644 --- a/src/vmm/io/vatpit.c +++ b/src/vmm/io/vatpit.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -37,9 +38,9 @@ #include #include -#define VATPIT_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; -#define VATPIT_LOCK(v) OSSpinLockLock(&(v)->lock) -#define VATPIT_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) +// #define VATPIT_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VATPIT_LOCK(v) os_unfair_lock_lock(&(v)->lock) +#define VATPIT_UNLOCK(v) os_unfair_lock_unlock(&(v)->lock) #define TIMER_SEL_MASK 0xc0 #define TIMER_RW_MASK 0x30 @@ -85,7 +86,7 @@ struct channel { struct vatpit { struct vm *vm; - OSSpinLock lock; + os_unfair_lock lock; sbintime_t freq_sbt; struct channel channel[3]; }; @@ -423,7 +424,7 @@ vatpit_init(struct vm *vm) bzero(vatpit, sizeof(struct vatpit)); vatpit->vm = vm; - VATPIT_LOCK_INIT(vatpit) + // VATPIT_LOCK_INIT(vatpit) FREQ2BT(PIT_8254_FREQ, &bt); vatpit->freq_sbt = bttosbt(bt); diff --git a/src/vmm/io/vioapic.c b/src/vmm/io/vioapic.c index 55005c40..b5eddc95 100644 --- a/src/vmm/io/vioapic.c +++ b/src/vmm/io/vioapic.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -49,7 +50,7 @@ #pragma clang diagnostic ignored "-Wpadded" struct vioapic { struct vm *vm; - OSSpinLock lock; + os_unfair_lock lock; uint32_t id; uint32_t ioregsel; struct { @@ -59,9 +60,9 @@ struct vioapic { }; #pragma clang diagnostic pop -#define VIOAPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; -#define VIOAPIC_LOCK(v) OSSpinLockLock(&(v)->lock) -#define VIOAPIC_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) +// #define VIOAPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VIOAPIC_LOCK(v) os_unfair_lock_lock(&(v)->lock) +#define VIOAPIC_UNLOCK(v) os_unfair_lock_unlock(&(v)->lock) #define VIOAPIC_CTR1(vioapic, fmt, a1) \ VM_CTR1((vioapic)->vm, fmt, a1) @@ -461,7 +462,7 @@ vioapic_init(struct vm *vm) bzero(vioapic, sizeof(struct vioapic)); vioapic->vm = vm; - VIOAPIC_LOCK_INIT(vioapic); + // VIOAPIC_LOCK_INIT(vioapic); /* Initialize all redirection entries to mask all interrupts */ for (i = 0; i < REDIR_ENTRIES; i++) diff --git a/src/vmm/io/vlapic.c b/src/vmm/io/vlapic.c index 62cf730f..aba9df4b 100644 --- a/src/vmm/io/vlapic.c +++ b/src/vmm/io/vlapic.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -56,9 +57,9 @@ * - timer_freq_bt, timer_period_bt, timer_fire_bt * - timer LVT register */ -#define VLAPIC_TIMER_LOCK_INIT(v) (v)->timer_lock = OS_SPINLOCK_INIT; -#define VLAPIC_TIMER_LOCK(v) OSSpinLockLock(&(v)->timer_lock) -#define VLAPIC_TIMER_UNLOCK(v) OSSpinLockUnlock(&(v)->timer_lock) +// #define VLAPIC_TIMER_LOCK_INIT(v) (v)->timer_lock = OS_SPINLOCK_INIT; +#define VLAPIC_TIMER_LOCK(v) os_unfair_lock_lock(&(v)->timer_lock) +#define VLAPIC_TIMER_UNLOCK(v) os_unfair_lock_unlock(&(v)->timer_lock) /* * APIC timer frequency: @@ -136,7 +137,7 @@ void vlapic_id_write_handler(struct vlapic *vlapic) { struct LAPIC *lapic; - + /* * We don't allow the ID register to be modified so reset it back to * its default value. @@ -186,7 +187,7 @@ vlapic_get_ccr(struct vlapic *vlapic) struct bintime bt_now, bt_rem; struct LAPIC *lapic; uint32_t ccr; - + ccr = 0; lapic = vlapic->apic_page; @@ -217,7 +218,7 @@ vlapic_dcr_write_handler(struct vlapic *vlapic) { struct LAPIC *lapic; int divisor; - + lapic = vlapic->apic_page; VLAPIC_TIMER_LOCK(vlapic); @@ -242,7 +243,7 @@ void vlapic_esr_write_handler(struct vlapic *vlapic) { struct LAPIC *lapic; - + lapic = vlapic->apic_page; lapic->esr = vlapic->esr_pending; vlapic->esr_pending = 0; @@ -371,9 +372,9 @@ vlapic_lvt_write_handler(struct vlapic *vlapic, uint32_t offset) uint32_t *lvtptr, mask, val; struct LAPIC *lapic; int idx; - + lapic = vlapic->apic_page; - lvtptr = vlapic_get_lvtptr(vlapic, offset); + lvtptr = vlapic_get_lvtptr(vlapic, offset); val = *lvtptr; idx = lvt_off_to_idx(offset); @@ -593,7 +594,7 @@ static __inline int vlapic_periodic_timer(struct vlapic *vlapic) { uint32_t lvt; - + lvt = vlapic_get_lvt(vlapic, APIC_OFFSET_TIMER_LVT); return (vlapic_get_lvt_field(lvt, APIC_LVTT_TM_PERIODIC)); @@ -625,7 +626,7 @@ static void vlapic_fire_timer(struct vlapic *vlapic) { uint32_t lvt; - + // The timer LVT always uses the fixed delivery mode. lvt = vlapic_get_lvt(vlapic, APIC_OFFSET_TIMER_LVT); if (vlapic_fire_lvt(vlapic, lvt | APIC_LVT_DM_FIXED)) { @@ -804,7 +805,7 @@ vlapic_icrtmr_write_handler(struct vlapic *vlapic) /* * This function populates 'dmask' with the set of vcpus that match the * addressing specified by the (dest, phys, lowprio) tuple. - * + * * 'x2apic_dest' specifies whether 'dest' is interpreted as x2APIC (32-bit) * or xAPIC (8-bit) destination field. */ @@ -1102,7 +1103,7 @@ vlapic_pending_intr(struct vlapic *vlapic, int *vecptr) if (vecptr != NULL) *vecptr = vector; return (1); - } else + } else break; } } @@ -1122,7 +1123,7 @@ vlapic_intr_accepted(struct vlapic *vlapic, int vector) } /* - * clear the ready bit for vector being accepted in irr + * clear the ready bit for vector being accepted in irr * and set the vector as in service in isr. */ idx = (vector / 32) * 4; @@ -1214,7 +1215,7 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, *data = 0; goto done; } - + offset &= ~((uint64_t) 3); switch(offset) { @@ -1284,12 +1285,12 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, case APIC_OFFSET_ESR: *data = lapic->esr; break; - case APIC_OFFSET_ICR_LOW: + case APIC_OFFSET_ICR_LOW: *data = lapic->icr_lo; if (x2apic(vlapic)) *data |= (uint64_t)lapic->icr_hi << 32; break; - case APIC_OFFSET_ICR_HI: + case APIC_OFFSET_ICR_HI: *data = lapic->icr_hi; break; case APIC_OFFSET_CMCI_LVT: @@ -1299,7 +1300,7 @@ vlapic_read(struct vlapic *vlapic, int mmio_access, uint64_t offset, case APIC_OFFSET_LINT0_LVT: case APIC_OFFSET_LINT1_LVT: case APIC_OFFSET_ERROR_LVT: - *data = vlapic_get_lvt(vlapic, ((uint32_t) offset)); + *data = vlapic_get_lvt(vlapic, ((uint32_t) offset)); #ifdef INVARIANTS reg = vlapic_get_lvtptr(vlapic, offset); KASSERT(*data == *reg, ("inconsistent lvt value at " @@ -1389,7 +1390,7 @@ vlapic_write(struct vlapic *vlapic, int mmio_access, uint64_t offset, lapic->svr = (uint32_t) data; vlapic_svr_write_handler(vlapic); break; - case APIC_OFFSET_ICR_LOW: + case APIC_OFFSET_ICR_LOW: lapic->icr_lo = (uint32_t) data; if (x2apic(vlapic)) lapic->icr_hi = data >> 32; @@ -1469,7 +1470,7 @@ static void vlapic_reset(struct vlapic *vlapic) { struct LAPIC *lapic; - + lapic = vlapic->apic_page; bzero(lapic, sizeof(struct LAPIC)); @@ -1508,7 +1509,7 @@ vlapic_init(struct vlapic *vlapic) * Therefore the timer mutex must be a spinlock because blockable * mutexes cannot be acquired in a critical section. */ - VLAPIC_TIMER_LOCK_INIT(vlapic); + // VLAPIC_TIMER_LOCK_INIT(vlapic); callout_init(&vlapic->callout, 1); vlapic->msr_apicbase = DEFAULT_APIC_BASE | APICBASE_ENABLED; diff --git a/src/vmm/vmm.c b/src/vmm/vmm.c index 01b141dd..aa43e05a 100644 --- a/src/vmm/vmm.c +++ b/src/vmm/vmm.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -69,7 +70,8 @@ struct vlapic; * (x) initialized before use */ struct vcpu { - OSSpinLock lock; /* (o) protects 'state' */ + // OSSpinLock lock; /* (o) protects 'state' */ + os_unfair_lock lock; pthread_mutex_t state_sleep_mtx; pthread_cond_t state_sleep_cnd; pthread_mutex_t vcpu_sleep_mtx; @@ -90,9 +92,9 @@ struct vcpu { uint64_t nextrip; /* (x) next instruction to execute */ }; -#define vcpu_lock_init(v) (v)->lock = OS_SPINLOCK_INIT; -#define vcpu_lock(v) OSSpinLockLock(&(v)->lock) -#define vcpu_unlock(v) OSSpinLockUnlock(&(v)->lock) +// #define vcpu_lock_init(v) (v)->lock = OS_SPINLOCK_INIT; +#define vcpu_lock(v) os_unfair_lock_lock(&(v)->lock) +#define vcpu_unlock(v) os_unfair_lock_unlock(&(v)->lock) struct mem_seg { uint64_t gpa; @@ -189,7 +191,7 @@ vcpu_cleanup(struct vm *vm, int i, bool destroy) VLAPIC_CLEANUP(vm->cookie, vcpu->vlapic); if (destroy) { - vmm_stat_free(vcpu->stats); + vmm_stat_free(vcpu->stats); } } @@ -200,11 +202,11 @@ vcpu_init(struct vm *vm, int vcpu_id, bool create) KASSERT(vcpu_id >= 0 && vcpu_id < VM_MAXCPU, ("vcpu_init: invalid vcpu %d", vcpu_id)); - + vcpu = &vm->vcpu[vcpu_id]; if (create) { - vcpu_lock_init(vcpu); + // vcpu_lock_init(vcpu); pthread_mutex_init(&vcpu->state_sleep_mtx, NULL); pthread_cond_init(&vcpu->state_sleep_cnd, NULL); pthread_mutex_init(&vcpu->vcpu_sleep_mtx, NULL); @@ -266,7 +268,7 @@ vmm_init(void) error = vmm_mem_init(); if (error) return (error); - + ops = &vmm_ops_intel; error = VMM_INIT(); @@ -516,7 +518,7 @@ vm_malloc(struct vm *vm, uint64_t gpa, size_t len) if ((gpa & XHYVE_PAGE_MASK) || (len & XHYVE_PAGE_MASK) || len == 0) return (EINVAL); - + available = allocated = 0; g = gpa; while (g < gpa + len) { @@ -985,7 +987,7 @@ vm_handle_inst_emul(struct vm *vm, int vcpuid, bool *retu) vme->inst_length = vie->num_processed; vcpu->nextrip += vie->num_processed; } - + /* return to userland unless this is an in-kernel emulated device */ if (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + XHYVE_PAGE_SIZE) { mread = lapic_mmio_read; @@ -1958,7 +1960,7 @@ vm_copyin(UNUSED struct vm *vm, UNUSED int vcpuid, struct vm_copyinfo *copyinfo, { char *dst; int idx; - + dst = kaddr; idx = 0; while (len > 0) { From 3247a1d29595a83313f69e693e070a6ec2dbc871 Mon Sep 17 00:00:00 2001 From: Anil Madhavapeddy Date: Mon, 25 Jul 2016 14:00:44 +0100 Subject: [PATCH 2/5] Add a `platform_locks.h` which lets us switch between old/new locking The old locking scheme using OSSpinLockLock is deprecated as of OSX 10.12, but the new one is only available in OSX 10.12 onwards, so we need to support both to transition. With this patch, the code will compile cleanly on 10.12, but will fail if MACOSX_DEPLOYMENT_TARGET is set to an earlier version (due to Werror on the deprecation warning). This can be addressed in a future patchset, as this lets us test the new spinlocks on OSX 10.12 beta right now. Signed-off-by: David Wisner --- include/xhyve/support/platform_locks.h | 34 ++++++++++++++++++++++++++ include/xhyve/vmm/io/vlapic_priv.h | 7 ++++-- src/vmm/io/vatpic.c | 13 ++++++++-- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 include/xhyve/support/platform_locks.h diff --git a/include/xhyve/support/platform_locks.h b/include/xhyve/support/platform_locks.h new file mode 100644 index 00000000..1020a0c1 --- /dev/null +++ b/include/xhyve/support/platform_locks.h @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 2016 Anil Madhavapeddy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_12 +#define XHYVE_USE_OSLOCKS 1 +#include +#else +#include +#endif diff --git a/include/xhyve/vmm/io/vlapic_priv.h b/include/xhyve/vmm/io/vlapic_priv.h index 59a8a773..ee3b916f 100644 --- a/include/xhyve/vmm/io/vlapic_priv.h +++ b/include/xhyve/vmm/io/vlapic_priv.h @@ -30,10 +30,9 @@ #include #include -#include #include #include -#include +#include /* * APIC Register: Offset Description @@ -164,7 +163,11 @@ struct vlapic { struct bintime timer_fire_bt; /* callout expiry time */ struct bintime timer_freq_bt; /* timer frequency */ struct bintime timer_period_bt; /* timer period */ +#ifdef XHYVE_USE_OSLOCKS os_unfair_lock timer_lock; +#else + OSSpinLock timer_lock; +#endif /* * The 'isrvec_stk' is a stack of vectors injected by the local apic. * A vector is popped from the stack when the processor does an EOI. diff --git a/src/vmm/io/vatpic.c b/src/vmm/io/vatpic.c index cdf64944..3d50dab0 100644 --- a/src/vmm/io/vatpic.c +++ b/src/vmm/io/vatpic.c @@ -29,7 +29,7 @@ #include #include #include -#include + #include #include #include @@ -38,10 +38,15 @@ #include #include #include +#include -// #define VATPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#ifdef XHYVE_USE_OSLOCKS #define VATPIC_LOCK(v) os_unfair_lock_lock(&(v)->lock) #define VATPIC_UNLOCK(v) os_unfair_lock_unlock(&(v)->lock) +#else +#define VATPIC_LOCK(v) OSSpinLockLock(&(v)->lock) +#define VATPIC_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) +#endif enum irqstate { IRQSTATE_ASSERT, @@ -71,7 +76,11 @@ struct atpic { struct vatpic { struct vm *vm; +#ifdef XHYVE_USE_OSLOCKS os_unfair_lock lock; +#else + OSSpinLock lock; +#endif struct atpic atpic[2]; uint8_t elc[2]; }; From 873a42855136d7100d47fb1ab632b2131d367385 Mon Sep 17 00:00:00 2001 From: David Wisner Date: Mon, 25 Jul 2016 17:50:09 -0700 Subject: [PATCH 3/5] Added patch for replacing OSSpinLock on macOS 10.12 beta Signed-off-by: David Wisner --- src/vmm/io/vatpic.c | 1 - src/vmm/io/vatpit.c | 16 ++++++++++++---- src/vmm/io/vioapic.c | 16 ++++++++++++---- src/vmm/io/vlapic.c | 13 +++++++------ src/vmm/vmm.c | 19 +++++++++++++------ 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/vmm/io/vatpic.c b/src/vmm/io/vatpic.c index 3d50dab0..e807f566 100644 --- a/src/vmm/io/vatpic.c +++ b/src/vmm/io/vatpic.c @@ -29,7 +29,6 @@ #include #include #include - #include #include #include diff --git a/src/vmm/io/vatpit.c b/src/vmm/io/vatpit.c index 28710df0..f05f29c4 100644 --- a/src/vmm/io/vatpit.c +++ b/src/vmm/io/vatpit.c @@ -29,8 +29,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -38,9 +37,14 @@ #include #include -// #define VATPIT_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#ifdef XHYVE_USE_OSLOCKS #define VATPIT_LOCK(v) os_unfair_lock_lock(&(v)->lock) #define VATPIT_UNLOCK(v) os_unfair_lock_unlock(&(v)->lock) +#else +#define VATPIT_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VATPIT_LOCK(v) OSSpinLockLock(&(v)->lock) +#define VATPIT_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) +#endif #define TIMER_SEL_MASK 0xc0 #define TIMER_RW_MASK 0x30 @@ -86,7 +90,11 @@ struct channel { struct vatpit { struct vm *vm; - os_unfair_lock lock; + #ifdef XHYVE_USE_OSLOCKS + os_unfair_lock lock; + #else + OSSpinLock lock; + #endif sbintime_t freq_sbt; struct channel channel[3]; }; diff --git a/src/vmm/io/vioapic.c b/src/vmm/io/vioapic.c index b5eddc95..95325d9f 100644 --- a/src/vmm/io/vioapic.c +++ b/src/vmm/io/vioapic.c @@ -32,8 +32,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -50,7 +49,11 @@ #pragma clang diagnostic ignored "-Wpadded" struct vioapic { struct vm *vm; - os_unfair_lock lock; + #ifdef XHYVE_USE_OSLOCKS + os_unfair_lock lock; + #else + OSSpinLock lock; + #endif uint32_t id; uint32_t ioregsel; struct { @@ -60,9 +63,14 @@ struct vioapic { }; #pragma clang diagnostic pop -// #define VIOAPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#ifdef XHYVE_USE_OSLOCKS #define VIOAPIC_LOCK(v) os_unfair_lock_lock(&(v)->lock) #define VIOAPIC_UNLOCK(v) os_unfair_lock_unlock(&(v)->lock) +#else +#define VIOAPIC_LOCK_INIT(v) (v)->lock = OS_SPINLOCK_INIT; +#define VIOAPIC_LOCK(v) OSSpinLockLock(&(v)->lock) +#define VIOAPIC_UNLOCK(v) OSSpinLockUnlock(&(v)->lock) +#endif #define VIOAPIC_CTR1(vioapic, fmt, a1) \ VM_CTR1((vioapic)->vm, fmt, a1) diff --git a/src/vmm/io/vlapic.c b/src/vmm/io/vlapic.c index aba9df4b..88b52d33 100644 --- a/src/vmm/io/vlapic.c +++ b/src/vmm/io/vlapic.c @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include @@ -57,10 +57,14 @@ * - timer_freq_bt, timer_period_bt, timer_fire_bt * - timer LVT register */ -// #define VLAPIC_TIMER_LOCK_INIT(v) (v)->timer_lock = OS_SPINLOCK_INIT; +#ifdef XHYVE_USE_OSLOCKS #define VLAPIC_TIMER_LOCK(v) os_unfair_lock_lock(&(v)->timer_lock) #define VLAPIC_TIMER_UNLOCK(v) os_unfair_lock_unlock(&(v)->timer_lock) - +#else +#define VLAPIC_TIMER_LOCK_INIT(v) (v)->timer_lock = OS_SPINLOCK_INIT; +#define VLAPIC_TIMER_LOCK(v) OSSpinLockLock(&(v)->timer_lock) +#define VLAPIC_TIMER_UNLOCK(v) OSSpinLockUnlock(&(v)->timer_lock) +#endif /* * APIC timer frequency: * - arbitrary but chosen to be in the ballpark of contemporary hardware. @@ -137,7 +141,6 @@ void vlapic_id_write_handler(struct vlapic *vlapic) { struct LAPIC *lapic; - /* * We don't allow the ID register to be modified so reset it back to * its default value. @@ -225,7 +228,6 @@ vlapic_dcr_write_handler(struct vlapic *vlapic) divisor = vlapic_timer_divisor(lapic->dcr_timer); VLAPIC_CTR2(vlapic, "vlapic dcr_timer=%#x, divisor=%d", lapic->dcr_timer, divisor); - /* * Update the timer frequency and the timer period. * @@ -626,7 +628,6 @@ static void vlapic_fire_timer(struct vlapic *vlapic) { uint32_t lvt; - // The timer LVT always uses the fixed delivery mode. lvt = vlapic_get_lvt(vlapic, APIC_OFFSET_TIMER_LVT); if (vlapic_fire_lvt(vlapic, lvt | APIC_LVT_DM_FIXED)) { diff --git a/src/vmm/vmm.c b/src/vmm/vmm.c index aa43e05a..8e2122bd 100644 --- a/src/vmm/vmm.c +++ b/src/vmm/vmm.c @@ -33,8 +33,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -70,8 +69,11 @@ struct vlapic; * (x) initialized before use */ struct vcpu { - // OSSpinLock lock; /* (o) protects 'state' */ - os_unfair_lock lock; + #ifdef XHYVE_USE_OSLOCKS + os_unfair_lock lock; + #else + OSSpinLock timer_lock; + #endif pthread_mutex_t state_sleep_mtx; pthread_cond_t state_sleep_cnd; pthread_mutex_t vcpu_sleep_mtx; @@ -92,9 +94,14 @@ struct vcpu { uint64_t nextrip; /* (x) next instruction to execute */ }; -// #define vcpu_lock_init(v) (v)->lock = OS_SPINLOCK_INIT; +#ifdef XHYVE_USE_OSLOCKS #define vcpu_lock(v) os_unfair_lock_lock(&(v)->lock) #define vcpu_unlock(v) os_unfair_lock_unlock(&(v)->lock) +#else +#define vcpu_lock_init(v) (v)->lock = OS_SPINLOCK_INIT; +#define vcpu_lock(v) OSSpinLockLock(&(v)->lock) +#define vcpu_unlock(v) OSSpinLockUnlock(&(v)->lock) +#endif struct mem_seg { uint64_t gpa; @@ -206,7 +213,7 @@ vcpu_init(struct vm *vm, int vcpu_id, bool create) vcpu = &vm->vcpu[vcpu_id]; if (create) { - // vcpu_lock_init(vcpu); + // vcpu_lock_init(vcpu); pthread_mutex_init(&vcpu->state_sleep_mtx, NULL); pthread_cond_init(&vcpu->state_sleep_cnd, NULL); pthread_mutex_init(&vcpu->vcpu_sleep_mtx, NULL); From 3fa4f4ce11cb8284b30dc1e88ed5ef461fe4cc78 Mon Sep 17 00:00:00 2001 From: David Wisner Date: Mon, 25 Jul 2016 18:30:57 -0700 Subject: [PATCH 4/5] updated os_init Signed-off-by: David Wisner --- src/vmm/io/vatpic.c | 6 +++--- src/vmm/io/vatpit.c | 16 ++++++++-------- src/vmm/io/vioapic.c | 16 ++++++++-------- src/vmm/io/vlapic.c | 4 +++- src/vmm/vmm.c | 14 ++++++++------ 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/vmm/io/vatpic.c b/src/vmm/io/vatpic.c index e807f566..acd43b66 100644 --- a/src/vmm/io/vatpic.c +++ b/src/vmm/io/vatpic.c @@ -788,9 +788,9 @@ vatpic_init(struct vm *vm) assert(vatpic); bzero(vatpic, sizeof(struct vatpic)); vatpic->vm = vm; - - // VATPIC_LOCK_INIT(vatpic); - +#ifndef XHYVE_USE_OSLOCKS + VATPIC_LOCK_INIT(vatpic); +#endif return (vatpic); } diff --git a/src/vmm/io/vatpit.c b/src/vmm/io/vatpit.c index f05f29c4..9568f1b7 100644 --- a/src/vmm/io/vatpit.c +++ b/src/vmm/io/vatpit.c @@ -90,11 +90,11 @@ struct channel { struct vatpit { struct vm *vm; - #ifdef XHYVE_USE_OSLOCKS - os_unfair_lock lock; - #else - OSSpinLock lock; - #endif +#ifdef XHYVE_USE_OSLOCKS + os_unfair_lock lock; +#else + OSSpinLock lock; +#endif sbintime_t freq_sbt; struct channel channel[3]; }; @@ -431,9 +431,9 @@ vatpit_init(struct vm *vm) assert(vatpit); bzero(vatpit, sizeof(struct vatpit)); vatpit->vm = vm; - - // VATPIT_LOCK_INIT(vatpit) - +#ifndef XHYVE_USE_OSLOCKS + VATPIT_LOCK_INIT(vatpit) +#endif FREQ2BT(PIT_8254_FREQ, &bt); vatpit->freq_sbt = bttosbt(bt); diff --git a/src/vmm/io/vioapic.c b/src/vmm/io/vioapic.c index 95325d9f..009f0272 100644 --- a/src/vmm/io/vioapic.c +++ b/src/vmm/io/vioapic.c @@ -49,11 +49,11 @@ #pragma clang diagnostic ignored "-Wpadded" struct vioapic { struct vm *vm; - #ifdef XHYVE_USE_OSLOCKS - os_unfair_lock lock; - #else - OSSpinLock lock; - #endif +#ifdef XHYVE_USE_OSLOCKS + os_unfair_lock lock; +#else + OSSpinLock lock; +#endif uint32_t id; uint32_t ioregsel; struct { @@ -469,9 +469,9 @@ vioapic_init(struct vm *vm) assert(vioapic); bzero(vioapic, sizeof(struct vioapic)); vioapic->vm = vm; - - // VIOAPIC_LOCK_INIT(vioapic); - +#ifndef XHYVE_USE_OSLOCKS + VIOAPIC_LOCK_INIT(vioapic); +#endif /* Initialize all redirection entries to mask all interrupts */ for (i = 0; i < REDIR_ENTRIES; i++) vioapic->rtbl[i].reg = 0x0001000000010000UL; diff --git a/src/vmm/io/vlapic.c b/src/vmm/io/vlapic.c index 88b52d33..f9121457 100644 --- a/src/vmm/io/vlapic.c +++ b/src/vmm/io/vlapic.c @@ -1510,7 +1510,9 @@ vlapic_init(struct vlapic *vlapic) * Therefore the timer mutex must be a spinlock because blockable * mutexes cannot be acquired in a critical section. */ - // VLAPIC_TIMER_LOCK_INIT(vlapic); +#ifndef XHYVE_USE_OSLOCKS + VLAPIC_TIMER_LOCK_INIT(vlapic); +#endif callout_init(&vlapic->callout, 1); vlapic->msr_apicbase = DEFAULT_APIC_BASE | APICBASE_ENABLED; diff --git a/src/vmm/vmm.c b/src/vmm/vmm.c index 8e2122bd..70a807a0 100644 --- a/src/vmm/vmm.c +++ b/src/vmm/vmm.c @@ -69,11 +69,11 @@ struct vlapic; * (x) initialized before use */ struct vcpu { - #ifdef XHYVE_USE_OSLOCKS - os_unfair_lock lock; - #else - OSSpinLock timer_lock; - #endif +#ifdef XHYVE_USE_OSLOCKS + os_unfair_lock lock; +#else + OSSpinLock timer_lock; +#endif pthread_mutex_t state_sleep_mtx; pthread_cond_t state_sleep_cnd; pthread_mutex_t vcpu_sleep_mtx; @@ -213,7 +213,9 @@ vcpu_init(struct vm *vm, int vcpu_id, bool create) vcpu = &vm->vcpu[vcpu_id]; if (create) { - // vcpu_lock_init(vcpu); + #ifndef XHYVE_USE_OSLOCKS + vcpu_lock_init(vcpu); + #endif pthread_mutex_init(&vcpu->state_sleep_mtx, NULL); pthread_cond_init(&vcpu->state_sleep_cnd, NULL); pthread_mutex_init(&vcpu->vcpu_sleep_mtx, NULL); From 01913e78d688877fa76e2ff489b3c85eae13fa1f Mon Sep 17 00:00:00 2001 From: David Wisner Date: Mon, 25 Jul 2016 18:51:44 -0700 Subject: [PATCH 5/5] updated osspinlock for 10.12 beta Signed-off-by: David Wisner --- include/xhyve/support/platform_locks.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/xhyve/support/platform_locks.h b/include/xhyve/support/platform_locks.h index 1020a0c1..453e7da0 100644 --- a/include/xhyve/support/platform_locks.h +++ b/include/xhyve/support/platform_locks.h @@ -26,7 +26,7 @@ #include -#if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_12 +#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1012 #define XHYVE_USE_OSLOCKS 1 #include #else