From 383e0123996c90abe9e905bafef331447e5f248e Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 27 Jul 2016 11:12:55 +0100 Subject: [PATCH 1/5] Switch vlapic OSSpinlock to pthread_mutex_t The former is deprecated in OSX 10.12 (Sierra). Introduce error checking variants of the init/lock/unlock functions, these will be used for future conversions. Signed-off-by: Ian Campbell --- include/xhyve/support/misc.h | 27 +++++++++++++++++++++++++++ include/xhyve/vmm/io/vlapic_priv.h | 4 ++-- src/vmm/io/vlapic.c | 7 ++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/xhyve/support/misc.h b/include/xhyve/support/misc.h index f4d36a8b..74d32cd5 100644 --- a/include/xhyve/support/misc.h +++ b/include/xhyve/support/misc.h @@ -1,8 +1,11 @@ #pragma once +#include +#include #include #include #include +#include #define UNUSED __attribute__ ((unused)) #define CTASSERT(x) _Static_assert ((x), "CTASSERT") @@ -65,3 +68,27 @@ static inline void do_cpuid(unsigned ax, unsigned *p) { /* Used to trigger a self-shutdown */ extern void push_power_button(void); + +/* Error checking pthread mutex operations */ +static inline void xpthread_mutex_init(pthread_mutex_t *mutex) +{ + int rc = pthread_mutex_init(mutex, NULL); + if (__builtin_expect(rc != 0, 0)) + xhyve_abort("pthread_mutex_init failed: %d: %s\n", + rc, strerror(rc)); +} + +static inline void xpthread_mutex_lock(pthread_mutex_t *mutex) +{ + int rc = pthread_mutex_lock(mutex); + if (__builtin_expect(rc != 0, 0)) + xhyve_abort("pthread_mutex_lock failed: %d: %s\n", + rc, strerror(rc)); +} +static inline void xpthread_mutex_unlock(pthread_mutex_t *mutex) +{ + int rc = pthread_mutex_unlock(mutex); + if (__builtin_expect(rc != 0, 0)) + xhyve_abort("pthread_mutex_unlock failed: %d: %s\n", + rc, strerror(rc)); +} diff --git a/include/xhyve/vmm/io/vlapic_priv.h b/include/xhyve/vmm/io/vlapic_priv.h index 62d28117..9d2364e0 100644 --- a/include/xhyve/vmm/io/vlapic_priv.h +++ b/include/xhyve/vmm/io/vlapic_priv.h @@ -30,7 +30,6 @@ #include #include -#include #include #include @@ -163,7 +162,8 @@ 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; + pthread_mutex_t 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/vlapic.c b/src/vmm/io/vlapic.c index 62cf730f..4e389f62 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) xpthread_mutex_init(&(v)->timer_lock) +#define VLAPIC_TIMER_LOCK(v) xpthread_mutex_lock(&(v)->timer_lock) +#define VLAPIC_TIMER_UNLOCK(v) xpthread_mutex_unlock(&(v)->timer_lock) /* * APIC timer frequency: From 909a93641e3f6304f72bdd8239178cf7a734820b Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 27 Jul 2016 11:12:55 +0100 Subject: [PATCH 2/5] Switch vioapic OSSpinlock to pthread_mutex_t The former is deprecated in OSX 10.12 (Sierra). Signed-off-by: Ian Campbell --- src/vmm/io/vioapic.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/vmm/io/vioapic.c b/src/vmm/io/vioapic.c index 55005c40..f8cdcdb7 100644 --- a/src/vmm/io/vioapic.c +++ b/src/vmm/io/vioapic.c @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -49,7 +48,7 @@ #pragma clang diagnostic ignored "-Wpadded" struct vioapic { struct vm *vm; - OSSpinLock lock; + pthread_mutex_t lock; uint32_t id; uint32_t ioregsel; struct { @@ -59,9 +58,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) xpthread_mutex_init(&(v)->lock); +#define VIOAPIC_LOCK(v) xpthread_mutex_lock(&(v)->lock) +#define VIOAPIC_UNLOCK(v) xpthread_mutex_unlock(&(v)->lock) #define VIOAPIC_CTR1(vioapic, fmt, a1) \ VM_CTR1((vioapic)->vm, fmt, a1) From 84c41593350848a00da61616e00fd991540dac7c Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 27 Jul 2016 11:12:55 +0100 Subject: [PATCH 3/5] Switch vatpic OSSpinlock to pthread_mutex_t The former is deprecated in OSX 10.12 (Sierra). Signed-off-by: Ian Campbell --- src/vmm/io/vatpic.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/vmm/io/vatpic.c b/src/vmm/io/vatpic.c index 0b0bb258..73e53c0a 100644 --- a/src/vmm/io/vatpic.c +++ b/src/vmm/io/vatpic.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -38,9 +37,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) xpthread_mutex_init(&(v)->lock) +#define VATPIC_LOCK(v) xpthread_mutex_lock(&(v)->lock) +#define VATPIC_UNLOCK(v) xpthread_mutex_unlock(&(v)->lock) enum irqstate { IRQSTATE_ASSERT, @@ -70,7 +69,7 @@ struct atpic { struct vatpic { struct vm *vm; - OSSpinLock lock; + pthread_mutex_t lock; struct atpic atpic[2]; uint8_t elc[2]; }; From 0b1ef4f16e05562bf3e9cb59fb9847bc229d3bdf Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 27 Jul 2016 11:12:55 +0100 Subject: [PATCH 4/5] Switch vatpit OSSpinlock to pthread_mutex_t The former is deprecated in OSX 10.12 (Sierra). Signed-off-by: Ian Campbell --- src/vmm/io/vatpit.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/vmm/io/vatpit.c b/src/vmm/io/vatpit.c index 5c59425f..a9c2479a 100644 --- a/src/vmm/io/vatpit.c +++ b/src/vmm/io/vatpit.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include @@ -37,9 +37,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) xpthread_mutex_init(&(v)->lock) +#define VATPIT_LOCK(v) xpthread_mutex_lock(&(v)->lock) +#define VATPIT_UNLOCK(v) xpthread_mutex_unlock(&(v)->lock) #define TIMER_SEL_MASK 0xc0 #define TIMER_RW_MASK 0x30 @@ -85,7 +85,7 @@ struct channel { struct vatpit { struct vm *vm; - OSSpinLock lock; + pthread_mutex_t lock; sbintime_t freq_sbt; struct channel channel[3]; }; @@ -423,7 +423,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); From ec661a0b388451e60ab992abb682c886e5ea4883 Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Wed, 27 Jul 2016 11:12:55 +0100 Subject: [PATCH 5/5] Switch vcpu OSSpinlock to pthread_mutex_t The former is deprecated in OSX 10.12 (Sierra). Signed-off-by: Ian Campbell --- src/vmm/vmm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/vmm/vmm.c b/src/vmm/vmm.c index 01b141dd..164ac36a 100644 --- a/src/vmm/vmm.c +++ b/src/vmm/vmm.c @@ -69,7 +69,7 @@ struct vlapic; * (x) initialized before use */ struct vcpu { - OSSpinLock lock; /* (o) protects 'state' */ + pthread_mutex_t lock; /* (o) protects 'state' */ pthread_mutex_t state_sleep_mtx; pthread_cond_t state_sleep_cnd; pthread_mutex_t vcpu_sleep_mtx; @@ -90,9 +90,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) xpthread_mutex_init(&(v)->lock) +#define vcpu_lock(v) xpthread_mutex_lock(&(v)->lock) +#define vcpu_unlock(v) xpthread_mutex_unlock(&(v)->lock) struct mem_seg { uint64_t gpa;