firehose_inline_internal.h [plain text]
#ifndef __FIREHOSE_INLINE_INTERNAL__
#define __FIREHOSE_INLINE_INTERNAL__
#define firehose_atomic_maxv2o(p, f, v, o, m) \
os_atomic_rmw_loop2o(p, f, *(o), (v), m, { \
if (*(o) >= (v)) os_atomic_rmw_loop_give_up(break); \
})
#define firehose_atomic_max2o(p, f, v, m) ({ \
typeof((p)->f) _old; \
firehose_atomic_maxv2o(p, f, v, &_old, m); \
})
#ifndef KERNEL
OS_ALWAYS_INLINE
static inline uint16_t
firehose_bitmap_first_set(uint64_t bitmap)
{
dispatch_assert(bitmap != 0);
return (uint16_t)__builtin_ffsll((long long)bitmap) - 1;
}
#endif
#pragma mark -
#pragma mark Mach Misc.
#ifndef KERNEL
OS_ALWAYS_INLINE
static inline mach_port_t
firehose_mach_port_allocate(uint32_t flags, void *ctx)
{
mach_port_t port = MACH_PORT_NULL;
mach_port_options_t opts = {
.flags = flags,
};
kern_return_t kr = mach_port_construct(mach_task_self(), &opts,
(mach_port_context_t)ctx, &port);
if (unlikely(kr)) {
DISPATCH_VERIFY_MIG(kr);
DISPATCH_CLIENT_CRASH(kr, "Unable to allocate mach port");
}
return port;
}
OS_ALWAYS_INLINE
static inline kern_return_t
firehose_mach_port_recv_dispose(mach_port_t port, void *ctx)
{
kern_return_t kr;
kr = mach_port_destruct(mach_task_self(), port, 0,
(mach_port_context_t)ctx);
DISPATCH_VERIFY_MIG(kr);
return kr;
}
OS_ALWAYS_INLINE
static inline void
firehose_mach_port_send_release(mach_port_t port)
{
kern_return_t kr = mach_port_deallocate(mach_task_self(), port);
DISPATCH_VERIFY_MIG(kr);
dispatch_assume_zero(kr);
}
OS_ALWAYS_INLINE
static inline void
firehose_mach_port_guard(mach_port_t port, bool strict, void *ctx)
{
kern_return_t kr = mach_port_guard(mach_task_self(), port,
(mach_port_context_t)ctx, strict);
DISPATCH_VERIFY_MIG(kr);
dispatch_assume_zero(kr);
}
OS_ALWAYS_INLINE
static inline void
firehose_mig_server(dispatch_mig_callback_t demux, size_t maxmsgsz,
mach_msg_header_t *hdr)
{
mig_reply_error_t *msg_reply = (mig_reply_error_t *)alloca(maxmsgsz);
kern_return_t rc = KERN_SUCCESS;
bool expects_reply = false;
if (MACH_MSGH_BITS_REMOTE(hdr->msgh_bits) == MACH_MSG_TYPE_MOVE_SEND_ONCE) {
expects_reply = true;
}
if (!fastpath(demux(hdr, &msg_reply->Head))) {
rc = MIG_BAD_ID;
} else if (msg_reply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
rc = KERN_SUCCESS;
} else {
rc = msg_reply->RetCode;
}
if (slowpath(rc == KERN_SUCCESS && expects_reply)) {
DISPATCH_INTERNAL_CRASH(msg_reply->Head.msgh_id,
"firehose_mig_server doesn't handle replies");
}
if (slowpath(rc != KERN_SUCCESS && rc != MIG_NO_REPLY)) {
hdr->msgh_remote_port = 0;
mach_msg_destroy(hdr);
}
}
#endif // !KERNEL
#pragma mark -
#pragma mark firehose buffer
OS_ALWAYS_INLINE
static inline firehose_chunk_t
firehose_buffer_chunk_for_address(void *addr)
{
uintptr_t chunk_addr = (uintptr_t)addr & ~(FIREHOSE_CHUNK_SIZE - 1);
return (firehose_chunk_t)chunk_addr;
}
OS_ALWAYS_INLINE
static inline uint16_t
firehose_buffer_chunk_to_ref(firehose_buffer_t fb, firehose_chunk_t fbc)
{
return (uint16_t)(fbc - fb->fb_chunks);
}
OS_ALWAYS_INLINE
static inline firehose_chunk_t
firehose_buffer_ref_to_chunk(firehose_buffer_t fb, uint16_t ref)
{
return fb->fb_chunks + ref;
}
#ifndef FIREHOSE_SERVER
#if DISPATCH_PURE_C
OS_ALWAYS_INLINE
static inline uint8_t
firehose_buffer_qos_bits_propagate(void)
{
#ifndef KERNEL
pthread_priority_t pp = _dispatch_priority_propagate();
pp &= _PTHREAD_PRIORITY_QOS_CLASS_MASK;
return (uint8_t)(pp >> _PTHREAD_PRIORITY_QOS_CLASS_SHIFT);
#else
return 0;
#endif
}
OS_ALWAYS_INLINE
static inline void
firehose_buffer_stream_flush(firehose_buffer_t fb, firehose_stream_t stream)
{
firehose_buffer_stream_t fbs = &fb->fb_header.fbh_stream[stream];
firehose_stream_state_u old_state, new_state;
firehose_chunk_t fc;
uint64_t stamp = UINT64_MAX; uint16_t ref;
long result;
old_state.fss_atomic_state =
os_atomic_load2o(fbs, fbs_state.fss_atomic_state, relaxed);
ref = old_state.fss_current;
if (!ref || ref == FIREHOSE_STREAM_STATE_PRISTINE) {
#ifndef KERNEL
firehose_buffer_force_connect(fb);
#endif
return;
}
fc = firehose_buffer_ref_to_chunk(fb, old_state.fss_current);
result = firehose_chunk_tracepoint_try_reserve(fc, stamp, stream,
firehose_buffer_qos_bits_propagate(), 1, 0, NULL);
if (likely(result < 0)) {
firehose_buffer_ring_enqueue(fb, old_state.fss_current);
}
if (unlikely(result > 0)) {
DISPATCH_INTERNAL_CRASH(result, "Allocation should always fail");
}
new_state = old_state;
new_state.fss_current = 0;
(void)os_atomic_cmpxchg2o(fbs, fbs_state.fss_atomic_state,
old_state.fss_atomic_state, new_state.fss_atomic_state, relaxed);
}
OS_ALWAYS_INLINE
static inline firehose_tracepoint_t
firehose_buffer_tracepoint_reserve(firehose_buffer_t fb, uint64_t stamp,
firehose_stream_t stream, uint16_t pubsize,
uint16_t privsize, uint8_t **privptr)
{
firehose_buffer_stream_t fbs = &fb->fb_header.fbh_stream[stream];
firehose_stream_state_u old_state, new_state;
firehose_chunk_t fc;
#if KERNEL
bool failable = false;
#endif
bool success;
long result;
uint16_t ref;
old_state.fss_atomic_state =
os_atomic_load2o(fbs, fbs_state.fss_atomic_state, relaxed);
for (;;) {
new_state = old_state;
ref = old_state.fss_current;
if (likely(ref && ref != FIREHOSE_STREAM_STATE_PRISTINE)) {
fc = firehose_buffer_ref_to_chunk(fb, ref);
result = firehose_chunk_tracepoint_try_reserve(fc, stamp, stream,
firehose_buffer_qos_bits_propagate(),
pubsize, privsize, privptr);
if (likely(result > 0)) {
uint64_t thread;
#ifdef KERNEL
thread = thread_tid(current_thread());
#else
thread = _pthread_threadid_self_np_direct();
#endif
return firehose_chunk_tracepoint_begin(fc,
stamp, pubsize, thread, result);
}
if (likely(result < 0)) {
firehose_buffer_ring_enqueue(fb, old_state.fss_current);
}
new_state.fss_current = 0;
}
#if KERNEL
if (failable) {
return NULL;
}
#endif
if (unlikely(old_state.fss_allocator)) {
_dispatch_gate_wait(&fbs->fbs_state.fss_gate,
DLOCK_LOCK_DATA_CONTENTION);
old_state.fss_atomic_state =
os_atomic_load2o(fbs, fbs_state.fss_atomic_state, relaxed);
#if KERNEL
failable = true;
#endif
continue;
}
__firehose_critical_region_enter();
#if KERNEL
new_state.fss_allocator = (uint32_t)cpu_number();
#else
new_state.fss_allocator = _dispatch_lock_value_for_self();
#endif
success = os_atomic_cmpxchgv2o(fbs, fbs_state.fss_atomic_state,
old_state.fss_atomic_state, new_state.fss_atomic_state,
&old_state.fss_atomic_state, relaxed);
if (likely(success)) {
break;
}
__firehose_critical_region_leave();
}
struct firehose_tracepoint_query_s ask = {
.pubsize = pubsize,
.privsize = privsize,
.stream = stream,
.for_io = (firehose_stream_uses_io_bank & (1UL << stream)) != 0,
#ifndef KERNEL
.quarantined = fb->fb_header.fbh_quarantined,
#endif
.stamp = stamp,
};
return firehose_buffer_tracepoint_reserve_slow(fb, &ask, privptr);
}
OS_ALWAYS_INLINE
static inline void
firehose_buffer_tracepoint_flush(firehose_buffer_t fb,
firehose_tracepoint_t ft, firehose_tracepoint_id_u ftid)
{
firehose_chunk_t fc = firehose_buffer_chunk_for_address(ft);
if (firehose_chunk_tracepoint_end(fc, ft, ftid)) {
firehose_buffer_ring_enqueue(fb, firehose_buffer_chunk_to_ref(fb, fc));
}
}
#ifndef KERNEL
OS_ALWAYS_INLINE
static inline void
firehose_buffer_clear_bank_flags(firehose_buffer_t fb, unsigned long bits)
{
firehose_buffer_bank_t fbb = &fb->fb_header.fbh_bank;
unsigned long orig_flags;
orig_flags = os_atomic_and_orig2o(fbb, fbb_flags, ~bits, relaxed);
if (orig_flags != (orig_flags & ~bits)) {
firehose_buffer_update_limits(fb);
}
}
OS_ALWAYS_INLINE
static inline void
firehose_buffer_set_bank_flags(firehose_buffer_t fb, unsigned long bits)
{
firehose_buffer_bank_t fbb = &fb->fb_header.fbh_bank;
unsigned long orig_flags;
orig_flags = os_atomic_or_orig2o(fbb, fbb_flags, bits, relaxed);
if (orig_flags != (orig_flags | bits)) {
firehose_buffer_update_limits(fb);
}
}
#endif // !KERNEL
#endif // !defined(FIREHOSE_SERVER)
#endif // DISPATCH_PURE_C
#endif // __FIREHOSE_INLINE_INTERNAL__