lck_grp_t *os_reason_lock_grp;
lck_attr_t *os_reason_lock_attr;
+os_refgrp_decl(static, os_reason_refgrp, "os_reason", NULL);
+
#define OS_REASON_RESERVE_COUNT 100
#define OS_REASON_MAX_COUNT (maxproc + 100)
new_reason->osr_kcd_buf = NULL;
lck_mtx_init(&new_reason->osr_lock, os_reason_lock_grp, os_reason_lock_attr);
- new_reason->osr_refcount = 1;
+ os_ref_init(&new_reason->osr_refcount, &os_reason_refgrp);
return new_reason;
}
}
lck_mtx_lock(&cur_reason->osr_lock);
-
- assert(cur_reason->osr_refcount > 0);
- if (os_add_overflow(cur_reason->osr_refcount, 1, &cur_reason->osr_refcount)) {
- panic("os reason refcount overflow");
- }
-
+ os_ref_retain_locked(&cur_reason->osr_refcount);
lck_mtx_unlock(&cur_reason->osr_lock);
-
return;
}
lck_mtx_lock(&cur_reason->osr_lock);
- if (cur_reason->osr_refcount == 0) {
- panic("os_reason_free called on reason with zero refcount");
- }
-
- cur_reason->osr_refcount--;
- if (cur_reason->osr_refcount != 0) {
+ if (os_ref_release_locked(&cur_reason->osr_refcount) > 0) {
lck_mtx_unlock(&cur_reason->osr_lock);
return;
}
zfree(os_reason_zone, cur_reason);
}
+
+/*
+ * Sets flags on the passed reason.
+ */
+void
+os_reason_set_flags(os_reason_t cur_reason, uint64_t flags)
+{
+ if (cur_reason == OS_REASON_NULL) {
+ return;
+ }
+
+ lck_mtx_lock(&cur_reason->osr_lock);
+ cur_reason->osr_flags = flags;
+ lck_mtx_unlock(&cur_reason->osr_lock);
+}
+
+/*
+ * Allocates space and sets description data in kcd_descriptor on the passed reason.
+ */
+void
+os_reason_set_description_data(os_reason_t cur_reason, uint32_t type, void *reason_data, uint32_t reason_data_len)
+{
+ mach_vm_address_t osr_data_addr = 0;
+
+ if (cur_reason == OS_REASON_NULL) {
+ return;
+ }
+
+ if (0 != os_reason_alloc_buffer(cur_reason, kcdata_estimate_required_buffer_size(1, reason_data_len))) {
+ panic("os_reason failed to allocate");
+ }
+
+ lck_mtx_lock(&cur_reason->osr_lock);
+ if (KERN_SUCCESS != kcdata_get_memory_addr(&cur_reason->osr_kcd_descriptor, type, reason_data_len, &osr_data_addr)) {
+ panic("os_reason failed to get data address");
+ }
+ if (KERN_SUCCESS != kcdata_memcpy(&cur_reason->osr_kcd_descriptor, osr_data_addr, reason_data, reason_data_len)) {
+ panic("os_reason failed to copy description data");
+ }
+ lck_mtx_unlock(&cur_reason->osr_lock);
+}