Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | /* * Copyright (c) 2015 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <kern/kalloc.h> #include <kern/locks.h> #include <sys/kernel.h> #include <sys/kernel_types.h> #include <kern/zalloc.h> #include <sys/reason.h> #include <string.h> #include <kern/assert.h> #include <kern/debug.h> #if OS_REASON_DEBUG #include <pexpert/pexpert.h> extern int os_reason_debug_disabled; #endif extern int maxproc; /* * Lock group attributes for os_reason subsystem */ lck_grp_attr_t *os_reason_lock_grp_attr; lck_grp_t *os_reason_lock_grp; lck_attr_t *os_reason_lock_attr; #define OS_REASON_RESERVE_COUNT 100 #define OS_REASON_MAX_COUNT (maxproc + 100) static struct zone *os_reason_zone; static int os_reason_alloc_buffer_internal(os_reason_t cur_reason, uint32_t osr_bufsize, boolean_t can_block); void os_reason_init() { int reasons_allocated = 0; /* * Initialize OS reason group and lock attributes */ os_reason_lock_grp_attr = lck_grp_attr_alloc_init(); os_reason_lock_grp = lck_grp_alloc_init("os_reason_lock", os_reason_lock_grp_attr); os_reason_lock_attr = lck_attr_alloc_init(); /* * Create OS reason zone. */ os_reason_zone = zinit(sizeof(struct os_reason), OS_REASON_MAX_COUNT * sizeof(struct os_reason), OS_REASON_MAX_COUNT, "os reasons"); if (os_reason_zone == NULL) { panic("failed to initialize os_reason_zone"); } /* * We pre-fill the OS reason zone to reduce the likelihood that * the jetsam thread and others block when they create an exit * reason. This pre-filled memory is not-collectable since it's * foreign memory crammed in as part of zfill(). */ reasons_allocated = zfill(os_reason_zone, OS_REASON_RESERVE_COUNT); assert(reasons_allocated > 0); } /* * Creates a new reason and initializes it with the provided reason * namespace and code. Also sets up the buffer and kcdata_descriptor * associated with the reason. Returns a pointer to the newly created * reason. * * Returns: * REASON_NULL if unable to allocate a reason or initialize the nested buffer * a pointer to the reason otherwise */ os_reason_t os_reason_create(uint32_t osr_namespace, uint64_t osr_code) { os_reason_t new_reason = OS_REASON_NULL; new_reason = (os_reason_t) zalloc(os_reason_zone); if (new_reason == OS_REASON_NULL) { #if OS_REASON_DEBUG /* * We rely on OS reasons to communicate important things such * as process exit reason information, we should be aware * when issues prevent us from allocating them. */ if (os_reason_debug_disabled) { kprintf("os_reason_create: failed to allocate reason with namespace: %u, code : %llu\n", osr_namespace, osr_code); } else { panic("os_reason_create: failed to allocate reason with namespace: %u, code: %llu\n", osr_namespace, osr_code); } #endif return new_reason; } bzero(new_reason, sizeof(*new_reason)); new_reason->osr_namespace = osr_namespace; new_reason->osr_code = osr_code; new_reason->osr_flags = 0; new_reason->osr_bufsize = 0; 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; return new_reason; } static void os_reason_dealloc_buffer(os_reason_t cur_reason) { assert(cur_reason != OS_REASON_NULL); LCK_MTX_ASSERT(&cur_reason->osr_lock, LCK_MTX_ASSERT_OWNED); if (cur_reason->osr_kcd_buf != NULL && cur_reason->osr_bufsize != 0) { kfree(cur_reason->osr_kcd_buf, cur_reason->osr_bufsize); } cur_reason->osr_bufsize = 0; cur_reason->osr_kcd_buf = NULL; bzero(&cur_reason->osr_kcd_descriptor, sizeof(cur_reason->osr_kcd_descriptor)); return; } /* * Allocates and initializes a buffer of specified size for the reason. This function * may block and should not be called from extremely performance sensitive contexts * (i.e. jetsam). Also initializes the kcdata descriptor accordingly. If there is an * existing buffer, we dealloc the buffer before allocating a new one and * clear the associated kcdata descriptor. If osr_bufsize is passed as 0, * we deallocate the existing buffer and then return. * * Returns: * 0 on success * EINVAL if the passed reason pointer is invalid or the requested size is * larger than REASON_BUFFER_MAX_SIZE * EIO if we fail to initialize the kcdata buffer */ int os_reason_alloc_buffer(os_reason_t cur_reason, uint32_t osr_bufsize) { return os_reason_alloc_buffer_internal(cur_reason, osr_bufsize, TRUE); } /* * Allocates and initializes a buffer of specified size for the reason. Also * initializes the kcdata descriptor accordingly. If there is an existing * buffer, we dealloc the buffer before allocating a new one and * clear the associated kcdata descriptor. If osr_bufsize is passed as 0, * we deallocate the existing buffer and then return. * * Returns: * 0 on success * EINVAL if the passed reason pointer is invalid or the requested size is * larger than REASON_BUFFER_MAX_SIZE * ENOMEM if unable to allocate memory for the buffer * EIO if we fail to initialize the kcdata buffer */ int os_reason_alloc_buffer_noblock(os_reason_t cur_reason, uint32_t osr_bufsize) { return os_reason_alloc_buffer_internal(cur_reason, osr_bufsize, FALSE); } static int os_reason_alloc_buffer_internal(os_reason_t cur_reason, uint32_t osr_bufsize, boolean_t can_block) { if (cur_reason == OS_REASON_NULL) { return EINVAL; } if (osr_bufsize > OS_REASON_BUFFER_MAX_SIZE) { return EINVAL; } lck_mtx_lock(&cur_reason->osr_lock); os_reason_dealloc_buffer(cur_reason); if (osr_bufsize == 0) { lck_mtx_unlock(&cur_reason->osr_lock); return 0; } if (can_block) { cur_reason->osr_kcd_buf = kalloc_tag(osr_bufsize, VM_KERN_MEMORY_REASON); assert(cur_reason->osr_kcd_buf != NULL); } else { cur_reason->osr_kcd_buf = kalloc_noblock_tag(osr_bufsize, VM_KERN_MEMORY_REASON); if (cur_reason->osr_kcd_buf == NULL) { lck_mtx_unlock(&cur_reason->osr_lock); return ENOMEM; } } bzero(cur_reason->osr_kcd_buf, osr_bufsize); cur_reason->osr_bufsize = osr_bufsize; if (kcdata_memory_static_init(&cur_reason->osr_kcd_descriptor, (mach_vm_address_t) cur_reason->osr_kcd_buf, KCDATA_BUFFER_BEGIN_OS_REASON, osr_bufsize, KCFLAG_USE_MEMCOPY) != KERN_SUCCESS) { os_reason_dealloc_buffer(cur_reason); lck_mtx_unlock(&cur_reason->osr_lock); return EIO; } lck_mtx_unlock(&cur_reason->osr_lock); return 0; } /* * Returns a pointer to the kcdata descriptor associated with the specified * reason if there is a buffer allocated. */ struct kcdata_descriptor * os_reason_get_kcdata_descriptor(os_reason_t cur_reason) { if (cur_reason == OS_REASON_NULL) { return NULL; } if (cur_reason->osr_kcd_buf == NULL) { return NULL; } assert(cur_reason->osr_kcd_descriptor.kcd_addr_begin == (mach_vm_address_t) cur_reason->osr_kcd_buf); if (cur_reason->osr_kcd_descriptor.kcd_addr_begin != (mach_vm_address_t) cur_reason->osr_kcd_buf) { return NULL; } return &cur_reason->osr_kcd_descriptor; } /* * Takes a reference on the passed reason. */ void os_reason_ref(os_reason_t cur_reason) { if (cur_reason == OS_REASON_NULL) { return; } 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"); } lck_mtx_unlock(&cur_reason->osr_lock); return; } /* * Drops a reference on the passed reason, deallocates * the reason if no references remain. */ void os_reason_free(os_reason_t cur_reason) { if (cur_reason == OS_REASON_NULL) { 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) { lck_mtx_unlock(&cur_reason->osr_lock); return; } os_reason_dealloc_buffer(cur_reason); lck_mtx_unlock(&cur_reason->osr_lock); lck_mtx_destroy(&cur_reason->osr_lock, os_reason_lock_grp); zfree(os_reason_zone, cur_reason); } |