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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/* * Copyright (c) 2019 Apple Computer, 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@ */ extern "C" { #include <kern/debug.h> #include <kern/queue.h> } #include <kern/sched_prim.h> #include <machine/machine_routines.h> #include <IOKit/IOLib.h> #include <IOKit/IOPlatformExpert.h> #include <IOKit/IOKitKeysPrivate.h> #include <IOKit/IOPlatformActions.h> #include "IOKitKernelInternal.h" static IOLock *gIOPlatformActionsLock; typedef kern_return_t (*iocpu_platform_action_t)(void * refcon0, void * refcon1, uint32_t priority, void * param1, void * param2, void * param3, const char * name, uint64_t platform_action_flags); struct iocpu_platform_action_entry { queue_chain_t link; iocpu_platform_action_t action; int32_t priority; const char * name; void * refcon0; void * refcon1; boolean_t callout_in_progress; struct iocpu_platform_action_entry * alloc_list; }; typedef struct iocpu_platform_action_entry iocpu_platform_action_entry_t; enum { kQueueSleep = 0, kQueueWake = 1, kQueueQuiesce = 2, kQueueActive = 3, kQueueHaltRestart = 4, kQueuePanic = 5, kQueueCount = 6 }; #define PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS 1 #define PLATFORM_ACTION_FLAGS_NO_LOGGING 2 const OSSymbol * gIOPlatformSleepActionKey; const OSSymbol * gIOPlatformWakeActionKey; const OSSymbol * gIOPlatformQuiesceActionKey; const OSSymbol * gIOPlatformActiveActionKey; const OSSymbol * gIOPlatformHaltRestartActionKey; const OSSymbol * gIOPlatformPanicActionKey; static queue_head_t gActionQueues[kQueueCount]; static const OSSymbol * gActionSymbols[kQueueCount]; static bool IOInstallServicePlatformAction(IOService * service, uint32_t qidx); static void iocpu_add_platform_action(queue_head_t * queue, iocpu_platform_action_entry_t * entry) { iocpu_platform_action_entry_t * next; queue_iterate(queue, next, iocpu_platform_action_entry_t *, link) { if (next->priority > entry->priority) { queue_insert_before(queue, entry, next, iocpu_platform_action_entry_t *, link); return; } } queue_enter(queue, entry, iocpu_platform_action_entry_t *, link); // at tail } static void iocpu_remove_platform_action(iocpu_platform_action_entry_t * entry) { remque(&entry->link); } static kern_return_t iocpu_run_platform_actions(queue_head_t * queue, uint32_t first_priority, uint32_t last_priority, void * param1, void * param2, void * param3, uint64_t platform_action_flags) { kern_return_t ret = KERN_SUCCESS; kern_return_t result = KERN_SUCCESS; iocpu_platform_action_entry_t * next; boolean_t allow_nested_callouts = (platform_action_flags & PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS); queue_iterate(queue, next, iocpu_platform_action_entry_t *, link) { uint32_t pri = (next->priority < 0) ? -next->priority : next->priority; if ((pri >= first_priority) && (pri <= last_priority)) { if (!allow_nested_callouts && !next->callout_in_progress) { next->callout_in_progress = TRUE; ret = (*next->action)(next->refcon0, next->refcon1, pri, param1, param2, param3, next->name, platform_action_flags); next->callout_in_progress = FALSE; } else if (allow_nested_callouts) { ret = (*next->action)(next->refcon0, next->refcon1, pri, param1, param2, param3, next->name, platform_action_flags); } } if (KERN_SUCCESS == result) { result = ret; } } return result; } extern "C" kern_return_t IOCPURunPlatformQuiesceActions(void) { assert(preemption_enabled() == false); cpu_event_debug_log(PLATFORM_QUIESCE, 0); return iocpu_run_platform_actions(&gActionQueues[kQueueQuiesce], 0, 0U - 1, NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS); } extern "C" kern_return_t IOCPURunPlatformActiveActions(void) { assert(preemption_enabled() == false); cpu_event_debug_log(PLATFORM_ACTIVE, 0); ml_hibernate_active_pre(); kern_return_t result = iocpu_run_platform_actions(&gActionQueues[kQueueActive], 0, 0U - 1, NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS); ml_hibernate_active_post(); return result; } extern "C" kern_return_t IOCPURunPlatformHaltRestartActions(uint32_t message) { if (!gActionQueues[kQueueHaltRestart].next) { return kIOReturnNotReady; } cpu_event_debug_log(PLATFORM_HALT_RESTART, 0); return iocpu_run_platform_actions(&gActionQueues[kQueueHaltRestart], 0, 0U - 1, (void *)(uintptr_t) message, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS); } extern "C" kern_return_t IOCPURunPlatformPanicActions(uint32_t message, uint32_t details) { // Don't allow nested calls of panic actions if (!gActionQueues[kQueuePanic].next) { return kIOReturnNotReady; } uint64_t platform_action_flags = 0; if (!verbose_panic_flow_logging) { platform_action_flags = PLATFORM_ACTION_FLAGS_NO_LOGGING; } cpu_event_debug_log(PLATFORM_PANIC, 0); return iocpu_run_platform_actions(&gActionQueues[kQueuePanic], 0, 0U - 1, (void *)(uintptr_t) message, (void *)(uintptr_t) details, NULL, platform_action_flags); } extern "C" kern_return_t IOCPURunPlatformPanicSyncAction(void *addr, uint32_t offset, uint32_t len) { PE_panic_save_context_t context = { .psc_buffer = addr, .psc_offset = offset, .psc_length = len }; // Don't allow nested calls of panic actions if (!gActionQueues[kQueuePanic].next) { return kIOReturnNotReady; } cpu_event_debug_log(PLATFORM_PANIC_SYNC, 0); return iocpu_run_platform_actions(&gActionQueues[kQueuePanic], 0, 0U - 1, (void *)(uintptr_t)(kPEPanicSync), &context, NULL, FALSE); } void IOPlatformActionsPreSleep(void) { cpu_event_debug_log(PLATFORM_PRE_SLEEP, 0); iocpu_run_platform_actions(&gActionQueues[kQueueSleep], 0, 0U - 1, NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS); } void IOPlatformActionsPostResume(void) { cpu_event_debug_log(PLATFORM_POST_RESUME, 0); iocpu_run_platform_actions(&gActionQueues[kQueueWake], 0, 0U - 1, NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS); } void IOPlatformActionsInitialize(void) { gIOPlatformActionsLock = IOLockAlloc(); for (uint32_t qidx = kQueueSleep; qidx < kQueueCount; qidx++) { queue_init(&gActionQueues[qidx]); } gIOPlatformSleepActionKey = gActionSymbols[kQueueSleep] = OSSymbol::withCStringNoCopy(kIOPlatformSleepActionKey); gIOPlatformWakeActionKey = gActionSymbols[kQueueWake] = OSSymbol::withCStringNoCopy(kIOPlatformWakeActionKey); gIOPlatformQuiesceActionKey = gActionSymbols[kQueueQuiesce] = OSSymbol::withCStringNoCopy(kIOPlatformQuiesceActionKey); gIOPlatformActiveActionKey = gActionSymbols[kQueueActive] = OSSymbol::withCStringNoCopy(kIOPlatformActiveActionKey); gIOPlatformHaltRestartActionKey = gActionSymbols[kQueueHaltRestart] = OSSymbol::withCStringNoCopy(kIOPlatformHaltRestartActionKey); gIOPlatformPanicActionKey = gActionSymbols[kQueuePanic] = OSSymbol::withCStringNoCopy(kIOPlatformPanicActionKey); } static kern_return_t IOServicePlatformAction(void * refcon0, void * refcon1, uint32_t priority, void * param1, void * param2, void * param3, const char * service_name, uint64_t platform_action_flags) { IOReturn ret; IOService * service = (IOService *) refcon0; const OSSymbol * function = (const OSSymbol *) refcon1; if (!(platform_action_flags & PLATFORM_ACTION_FLAGS_NO_LOGGING)) { IOLog("%s -> %s\n", function->getCStringNoCopy(), service_name); } /* * We intentionally don't trace params that are kernel addresses, * and truncate 64 bit values to 32 bit, so they all fit into * one tracepoint along with IOService registry id. */ SOCD_TRACE_XNU_START(PLATFORM_ACTION, ADDR(function->getCStringNoCopy()), ADDR(service->getMetaClass()), PACK_2X32(VALUE(param1), VALUE(service->getRegistryEntryID())), PACK_2X32(VALUE(param3), VALUE(param2))); ret = service->callPlatformFunction(function, false, (void *)(uintptr_t) priority, param1, param2, param3); SOCD_TRACE_XNU_END(PLATFORM_ACTION, ADDR(function->getCStringNoCopy()), ADDR(service->getMetaClass()), PACK_2X32(VALUE(param1), VALUE(service->getRegistryEntryID())), PACK_2X32(VALUE(param3), VALUE(param2))); return ret; } static bool IOInstallServicePlatformAction(IOService * service, uint32_t qidx) { iocpu_platform_action_entry_t * entry; OSNumber * num; uint32_t priority; const OSSymbol * key = gActionSymbols[qidx]; queue_head_t * queue = &gActionQueues[qidx]; bool reverse; num = OSDynamicCast(OSNumber, service->getProperty(key)); if (!num) { return true; } reverse = false; switch (qidx) { case kQueueWake: case kQueueActive: reverse = true; break; } queue_iterate(queue, entry, iocpu_platform_action_entry_t *, link) { if (service == entry->refcon0) { return true; } } entry = IOMallocType(iocpu_platform_action_entry_t); entry->action = &IOServicePlatformAction; entry->name = service->getName(); priority = num->unsigned32BitValue(); if (reverse) { entry->priority = -priority; } else { entry->priority = priority; } entry->refcon0 = service; entry->refcon1 = (void *) key; entry->callout_in_progress = FALSE; iocpu_add_platform_action(queue, entry); return false; } IOReturn IOInstallServicePlatformActions(IOService * service) { IOLockLock(gIOPlatformActionsLock); IOInstallServicePlatformAction(service, kQueueHaltRestart); IOInstallServicePlatformAction(service, kQueuePanic); IOLockUnlock(gIOPlatformActionsLock); return kIOReturnSuccess; } IOReturn IOInstallServiceSleepPlatformActions(IOService * service) { IOLockLock(gIOPlatformActionsLock); for (uint32_t qidx = kQueueSleep; qidx <= kQueueActive; qidx++) { IOInstallServicePlatformAction(service, qidx); } IOLockUnlock(gIOPlatformActionsLock); return kIOReturnSuccess; } IOReturn IORemoveServicePlatformActions(IOService * service) { iocpu_platform_action_entry_t * entry; iocpu_platform_action_entry_t * next; IOLockLock(gIOPlatformActionsLock); for (uint32_t qidx = kQueueSleep; qidx < kQueueCount; qidx++) { next = (typeof(entry))queue_first(&gActionQueues[qidx]); while (!queue_end(&gActionQueues[qidx], &next->link)) { entry = next; next = (typeof(entry))queue_next(&entry->link); if (service == entry->refcon0) { iocpu_remove_platform_action(entry); IOFreeType(entry, iocpu_platform_action_entry_t); } } } IOLockUnlock(gIOPlatformActionsLock); return kIOReturnSuccess; }