xref: /xnu-12377.1.9/iokit/Kernel/IOPlatformActions.cpp (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 /*
2  * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 extern "C" {
30 #include <kern/debug.h>
31 #include <kern/queue.h>
32 }
33 
34 #include <kern/sched_prim.h>
35 #include <machine/machine_routines.h>
36 #include <IOKit/IOLib.h>
37 #include <IOKit/IOPlatformExpert.h>
38 #include <IOKit/IOKitKeysPrivate.h>
39 #include <IOKit/IOPlatformActions.h>
40 #include "IOKitKernelInternal.h"
41 
42 static IOLock *gIOPlatformActionsLock;
43 
44 typedef kern_return_t (*iocpu_platform_action_t)(void * refcon0, void * refcon1, uint32_t priority,
45     void * param1, void * param2, void * param3,
46     const char * name, uint64_t platform_action_flags);
47 
48 struct iocpu_platform_action_entry {
49 	queue_chain_t                     link;
50 	iocpu_platform_action_t           action;
51 	int32_t                           priority;
52 	const char *                      name;
53 	void *                            refcon0;
54 	void *                            refcon1;
55 	boolean_t                         callout_in_progress;
56 	struct iocpu_platform_action_entry * alloc_list;
57 };
58 typedef struct iocpu_platform_action_entry iocpu_platform_action_entry_t;
59 
60 enum {
61 	kQueueSleep       = 0,
62 	kQueueWake        = 1,
63 	kQueueQuiesce     = 2,
64 	kQueueActive      = 3,
65 	kQueueHaltRestart = 4,
66 	kQueuePanic       = 5,
67 	kQueueCount       = 6
68 };
69 
70 #define PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS 1
71 #define PLATFORM_ACTION_FLAGS_NO_LOGGING            2
72 
73 const OSSymbol *                gIOPlatformSleepActionKey;
74 const OSSymbol *                gIOPlatformWakeActionKey;
75 const OSSymbol *                gIOPlatformQuiesceActionKey;
76 const OSSymbol *                gIOPlatformActiveActionKey;
77 const OSSymbol *                gIOPlatformHaltRestartActionKey;
78 const OSSymbol *                gIOPlatformPanicActionKey;
79 
80 static queue_head_t             gActionQueues[kQueueCount];
81 static const OSSymbol *         gActionSymbols[kQueueCount];
82 
83 static bool
84 IOInstallServicePlatformAction(IOService * service, uint32_t qidx);
85 
86 static void
iocpu_add_platform_action(queue_head_t * queue,iocpu_platform_action_entry_t * entry)87 iocpu_add_platform_action(queue_head_t * queue, iocpu_platform_action_entry_t * entry)
88 {
89 	iocpu_platform_action_entry_t * next;
90 
91 	queue_iterate(queue, next, iocpu_platform_action_entry_t *, link)
92 	{
93 		if (next->priority > entry->priority) {
94 			queue_insert_before(queue, entry, next, iocpu_platform_action_entry_t *, link);
95 			return;
96 		}
97 	}
98 	queue_enter(queue, entry, iocpu_platform_action_entry_t *, link); // at tail
99 }
100 
101 static void
iocpu_remove_platform_action(iocpu_platform_action_entry_t * entry)102 iocpu_remove_platform_action(iocpu_platform_action_entry_t * entry)
103 {
104 	remque(&entry->link);
105 }
106 
107 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)108 iocpu_run_platform_actions(queue_head_t * queue, uint32_t first_priority, uint32_t last_priority,
109     void * param1, void * param2, void * param3, uint64_t platform_action_flags)
110 {
111 	kern_return_t                ret = KERN_SUCCESS;
112 	kern_return_t                result = KERN_SUCCESS;
113 	iocpu_platform_action_entry_t * next;
114 	boolean_t allow_nested_callouts = (platform_action_flags & PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS);
115 
116 	queue_iterate(queue, next, iocpu_platform_action_entry_t *, link)
117 	{
118 		uint32_t pri = (next->priority < 0) ? -next->priority : next->priority;
119 		if ((pri >= first_priority) && (pri <= last_priority)) {
120 			if (!allow_nested_callouts && !next->callout_in_progress) {
121 				next->callout_in_progress = TRUE;
122 				ret = (*next->action)(next->refcon0, next->refcon1, pri, param1, param2, param3, next->name, platform_action_flags);
123 				next->callout_in_progress = FALSE;
124 			} else if (allow_nested_callouts) {
125 				ret = (*next->action)(next->refcon0, next->refcon1, pri, param1, param2, param3, next->name, platform_action_flags);
126 			}
127 		}
128 		if (KERN_SUCCESS == result) {
129 			result = ret;
130 		}
131 	}
132 	return result;
133 }
134 
135 extern "C" kern_return_t
IOCPURunPlatformQuiesceActions(void)136 IOCPURunPlatformQuiesceActions(void)
137 {
138 	assert(preemption_enabled() == false);
139 	cpu_event_debug_log(PLATFORM_QUIESCE, 0);
140 	return iocpu_run_platform_actions(&gActionQueues[kQueueQuiesce], 0, 0U - 1,
141 	           NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS);
142 }
143 
144 extern "C" kern_return_t
IOCPURunPlatformActiveActions(void)145 IOCPURunPlatformActiveActions(void)
146 {
147 	assert(preemption_enabled() == false);
148 	cpu_event_debug_log(PLATFORM_ACTIVE, 0);
149 	ml_hibernate_active_pre();
150 	kern_return_t result = iocpu_run_platform_actions(&gActionQueues[kQueueActive], 0, 0U - 1,
151 	    NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS);
152 	ml_hibernate_active_post();
153 	return result;
154 }
155 
156 extern "C" kern_return_t
IOCPURunPlatformHaltRestartActions(uint32_t message)157 IOCPURunPlatformHaltRestartActions(uint32_t message)
158 {
159 	if (!gActionQueues[kQueueHaltRestart].next) {
160 		return kIOReturnNotReady;
161 	}
162 	cpu_event_debug_log(PLATFORM_HALT_RESTART, 0);
163 	return iocpu_run_platform_actions(&gActionQueues[kQueueHaltRestart], 0, 0U - 1,
164 	           (void *)(uintptr_t) message, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS);
165 }
166 
167 extern "C" kern_return_t
IOCPURunPlatformPanicActions(uint32_t message,uint32_t details)168 IOCPURunPlatformPanicActions(uint32_t message, uint32_t details)
169 {
170 	// Don't allow nested calls of panic actions
171 	if (!gActionQueues[kQueuePanic].next) {
172 		return kIOReturnNotReady;
173 	}
174 	uint64_t platform_action_flags = 0;
175 
176 	if (!verbose_panic_flow_logging) {
177 		platform_action_flags = PLATFORM_ACTION_FLAGS_NO_LOGGING;
178 	}
179 	cpu_event_debug_log(PLATFORM_PANIC, 0);
180 	return iocpu_run_platform_actions(&gActionQueues[kQueuePanic], 0, 0U - 1,
181 	           (void *)(uintptr_t) message, (void *)(uintptr_t) details, NULL, platform_action_flags);
182 }
183 
184 extern "C" kern_return_t
IOCPURunPlatformPanicSyncAction(void * addr,uint32_t offset,uint32_t len)185 IOCPURunPlatformPanicSyncAction(void *addr, uint32_t offset, uint32_t len)
186 {
187 	PE_panic_save_context_t context = {
188 		.psc_buffer = addr,
189 		.psc_offset = offset,
190 		.psc_length = len
191 	};
192 
193 	// Don't allow nested calls of panic actions
194 	if (!gActionQueues[kQueuePanic].next) {
195 		return kIOReturnNotReady;
196 	}
197 	cpu_event_debug_log(PLATFORM_PANIC_SYNC, 0);
198 	return iocpu_run_platform_actions(&gActionQueues[kQueuePanic], 0, 0U - 1,
199 	           (void *)(uintptr_t)(kPEPanicSync), &context, NULL, FALSE);
200 }
201 
202 void
IOPlatformActionsPreSleep(void)203 IOPlatformActionsPreSleep(void)
204 {
205 	cpu_event_debug_log(PLATFORM_PRE_SLEEP, 0);
206 	iocpu_run_platform_actions(&gActionQueues[kQueueSleep], 0, 0U - 1,
207 	    NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS);
208 }
209 
210 void
IOPlatformActionsPostResume(void)211 IOPlatformActionsPostResume(void)
212 {
213 	cpu_event_debug_log(PLATFORM_POST_RESUME, 0);
214 	iocpu_run_platform_actions(&gActionQueues[kQueueWake], 0, 0U - 1,
215 	    NULL, NULL, NULL, PLATFORM_ACTION_FLAGS_ALLOW_NESTED_CALLOUTS);
216 }
217 
218 void
IOPlatformActionsInitialize(void)219 IOPlatformActionsInitialize(void)
220 {
221 	gIOPlatformActionsLock = IOLockAlloc();
222 
223 	for (uint32_t qidx = kQueueSleep; qidx < kQueueCount; qidx++) {
224 		queue_init(&gActionQueues[qidx]);
225 	}
226 
227 	gIOPlatformSleepActionKey        = gActionSymbols[kQueueSleep]
228 	            = OSSymbol::withCStringNoCopy(kIOPlatformSleepActionKey);
229 	gIOPlatformWakeActionKey         = gActionSymbols[kQueueWake]
230 	            = OSSymbol::withCStringNoCopy(kIOPlatformWakeActionKey);
231 	gIOPlatformQuiesceActionKey      = gActionSymbols[kQueueQuiesce]
232 	            = OSSymbol::withCStringNoCopy(kIOPlatformQuiesceActionKey);
233 	gIOPlatformActiveActionKey       = gActionSymbols[kQueueActive]
234 	            = OSSymbol::withCStringNoCopy(kIOPlatformActiveActionKey);
235 	gIOPlatformHaltRestartActionKey  = gActionSymbols[kQueueHaltRestart]
236 	            = OSSymbol::withCStringNoCopy(kIOPlatformHaltRestartActionKey);
237 	gIOPlatformPanicActionKey = gActionSymbols[kQueuePanic]
238 	            = OSSymbol::withCStringNoCopy(kIOPlatformPanicActionKey);
239 }
240 
241 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)242 IOServicePlatformAction(void * refcon0, void * refcon1, uint32_t priority,
243     void * param1, void * param2, void * param3,
244     const char * service_name, uint64_t platform_action_flags)
245 {
246 	IOReturn         ret;
247 	IOService *      service  = (IOService *)      refcon0;
248 	const OSSymbol * function = (const OSSymbol *) refcon1;
249 
250 	if (!(platform_action_flags & PLATFORM_ACTION_FLAGS_NO_LOGGING)) {
251 		IOLog("%s -> %s\n", function->getCStringNoCopy(), service_name);
252 	}
253 
254 	/*
255 	 * We intentionally don't trace params that are kernel addresses,
256 	 * and truncate 64 bit values to 32 bit, so they all fit into
257 	 * one tracepoint along with IOService registry id.
258 	 */
259 	SOCD_TRACE_XNU_START(PLATFORM_ACTION,
260 	    ADDR(function->getCStringNoCopy()),
261 	    ADDR(service->getMetaClass()),
262 	    PACK_2X32(VALUE(param1), VALUE(service->getRegistryEntryID())),
263 	    PACK_2X32(VALUE(param3), VALUE(param2)));
264 
265 	ret = service->callPlatformFunction(function, false,
266 	    (void *)(uintptr_t) priority, param1, param2, param3);
267 
268 	SOCD_TRACE_XNU_END(PLATFORM_ACTION,
269 	    ADDR(function->getCStringNoCopy()),
270 	    ADDR(service->getMetaClass()),
271 	    PACK_2X32(VALUE(param1), VALUE(service->getRegistryEntryID())),
272 	    PACK_2X32(VALUE(param3), VALUE(param2)));
273 
274 	return ret;
275 }
276 
277 static bool
IOInstallServicePlatformAction(IOService * service,uint32_t qidx)278 IOInstallServicePlatformAction(IOService * service, uint32_t qidx)
279 {
280 	iocpu_platform_action_entry_t * entry;
281 	OSNumber *       num;
282 	uint32_t         priority;
283 	const OSSymbol * key = gActionSymbols[qidx];
284 	queue_head_t *   queue = &gActionQueues[qidx];
285 	bool             reverse;
286 
287 	num = OSDynamicCast(OSNumber, service->getProperty(key));
288 	if (!num) {
289 		return true;
290 	}
291 
292 	reverse = false;
293 	switch (qidx) {
294 	case kQueueWake:
295 	case kQueueActive:
296 		reverse = true;
297 		break;
298 	}
299 	queue_iterate(queue, entry, iocpu_platform_action_entry_t *, link)
300 	{
301 		if (service == entry->refcon0) {
302 			return true;
303 		}
304 	}
305 
306 	entry = IOMallocType(iocpu_platform_action_entry_t);
307 	entry->action = &IOServicePlatformAction;
308 	entry->name = service->getName();
309 	priority = num->unsigned32BitValue();
310 	if (reverse) {
311 		entry->priority = -priority;
312 	} else {
313 		entry->priority = priority;
314 	}
315 	entry->refcon0 = service;
316 	entry->refcon1 = (void *) key;
317 	entry->callout_in_progress = FALSE;
318 
319 	iocpu_add_platform_action(queue, entry);
320 	return false;
321 }
322 
323 
324 IOReturn
IOInstallServicePlatformActions(IOService * service)325 IOInstallServicePlatformActions(IOService * service)
326 {
327 	IOLockLock(gIOPlatformActionsLock);
328 
329 	IOInstallServicePlatformAction(service, kQueueHaltRestart);
330 	IOInstallServicePlatformAction(service, kQueuePanic);
331 
332 	IOLockUnlock(gIOPlatformActionsLock);
333 
334 	return kIOReturnSuccess;
335 }
336 
337 IOReturn
IOInstallServiceSleepPlatformActions(IOService * service)338 IOInstallServiceSleepPlatformActions(IOService * service)
339 {
340 	IOLockLock(gIOPlatformActionsLock);
341 
342 	for (uint32_t qidx = kQueueSleep; qidx <= kQueueActive; qidx++) {
343 		IOInstallServicePlatformAction(service, qidx);
344 	}
345 
346 	IOLockUnlock(gIOPlatformActionsLock);
347 
348 	return kIOReturnSuccess;
349 }
350 
351 IOReturn
IORemoveServicePlatformActions(IOService * service)352 IORemoveServicePlatformActions(IOService * service)
353 {
354 	iocpu_platform_action_entry_t * entry;
355 	iocpu_platform_action_entry_t * next;
356 
357 	IOLockLock(gIOPlatformActionsLock);
358 
359 	for (uint32_t qidx = kQueueSleep; qidx < kQueueCount; qidx++) {
360 		next = (typeof(entry))queue_first(&gActionQueues[qidx]);
361 		while (!queue_end(&gActionQueues[qidx], &next->link)) {
362 			entry = next;
363 			next = (typeof(entry))queue_next(&entry->link);
364 			if (service == entry->refcon0) {
365 				iocpu_remove_platform_action(entry);
366 				IOFreeType(entry, iocpu_platform_action_entry_t);
367 			}
368 		}
369 	}
370 
371 	IOLockUnlock(gIOPlatformActionsLock);
372 
373 	return kIOReturnSuccess;
374 }
375