xref: /xnu-8796.141.3/iokit/Kernel/IOWorkloadConfig.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2021 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions #include <IOKit/IOLib.h>
32*1b191cb5SApple OSS Distributions #include <IOKit/IOReturn.h>
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions #include <libkern/c++/OSArray.h>
35*1b191cb5SApple OSS Distributions #include <libkern/c++/OSDictionary.h>
36*1b191cb5SApple OSS Distributions #include <libkern/c++/OSNumber.h>
37*1b191cb5SApple OSS Distributions #include <libkern/c++/OSString.h>
38*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSymbol.h>
39*1b191cb5SApple OSS Distributions #include <libkern/c++/OSUnserialize.h>
40*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
41*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSerialize.h>
42*1b191cb5SApple OSS Distributions 
43*1b191cb5SApple OSS Distributions #include <sys/work_interval.h>
44*1b191cb5SApple OSS Distributions #include <sys/param.h>
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions #include <kern/thread_group.h>
47*1b191cb5SApple OSS Distributions #include <kern/work_interval.h>
48*1b191cb5SApple OSS Distributions #include <kern/workload_config.h>
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions #if DEVELOPMENT || DEBUG
51*1b191cb5SApple OSS Distributions #define WLC_LOG(fmt, args...) IOLog("WorkloadConfig: " fmt, ##args)
52*1b191cb5SApple OSS Distributions #else
53*1b191cb5SApple OSS Distributions #define WLC_LOG(fmt, args...)
54*1b191cb5SApple OSS Distributions #endif
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions /* Limit criticality offsets.  */
57*1b191cb5SApple OSS Distributions #define MAX_CRITICALITY_OFFSET 16
58*1b191cb5SApple OSS Distributions 
59*1b191cb5SApple OSS Distributions 
60*1b191cb5SApple OSS Distributions /* Plist keys/values. */
61*1b191cb5SApple OSS Distributions #define kWorkloadIDTableKey   "WorkloadIDTable"
62*1b191cb5SApple OSS Distributions #define kRootKey              "Root"
63*1b191cb5SApple OSS Distributions #define kPhasesKey            "Phases"
64*1b191cb5SApple OSS Distributions #define kWorkIntervalTypeKey  "WorkIntervalType"
65*1b191cb5SApple OSS Distributions #define kWorkloadClassKey     "WorkloadClass"
66*1b191cb5SApple OSS Distributions #define kCriticalityOffsetKey "CriticalityOffset"
67*1b191cb5SApple OSS Distributions #define kDefaultPhaseKey      "DefaultPhase"
68*1b191cb5SApple OSS Distributions #define kFlagsKey             "Flags"
69*1b191cb5SApple OSS Distributions #define kWorkloadIDConfigurationFlagsKey "WorkloadIDConfigurationFlags"
70*1b191cb5SApple OSS Distributions 
71*1b191cb5SApple OSS Distributions #define kDisableWorkloadClassThreadPolicyValue "DisableWorkloadClassThreadPolicy"
72*1b191cb5SApple OSS Distributions #define kWIComplexityAllowedValue              "ComplexityAllowed"
73*1b191cb5SApple OSS Distributions 
74*1b191cb5SApple OSS Distributions #define ARRAY_LEN(x) (sizeof (x) / sizeof (x[0]))
75*1b191cb5SApple OSS Distributions 
76*1b191cb5SApple OSS Distributions #if !CONFIG_THREAD_GROUPS
77*1b191cb5SApple OSS Distributions #define THREAD_GROUP_FLAGS_EFFICIENT   0
78*1b191cb5SApple OSS Distributions #define THREAD_GROUP_FLAGS_APPLICATION 0
79*1b191cb5SApple OSS Distributions #define THREAD_GROUP_FLAGS_CRITICAL    0
80*1b191cb5SApple OSS Distributions #define THREAD_GROUP_FLAGS_BEST_EFFORT 0
81*1b191cb5SApple OSS Distributions #define THREAD_GROUP_FLAGS_ABSENT      0
82*1b191cb5SApple OSS Distributions #endif /* CONFIG_THREAD_GROUPS */
83*1b191cb5SApple OSS Distributions 
84*1b191cb5SApple OSS Distributions /* BEGIN IGNORE CODESTYLE */
85*1b191cb5SApple OSS Distributions static const struct WorkloadClassData {
86*1b191cb5SApple OSS Distributions 	const char *name;
87*1b191cb5SApple OSS Distributions 	UInt32 workIntervalFlags;
88*1b191cb5SApple OSS Distributions 	UInt32 threadGroupFlags;
89*1b191cb5SApple OSS Distributions } wlClassData[] = {
90*1b191cb5SApple OSS Distributions 	[WI_CLASS_NONE] =
91*1b191cb5SApple OSS Distributions 	{
92*1b191cb5SApple OSS Distributions 		.name = "NONE",
93*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
94*1b191cb5SApple OSS Distributions 		.threadGroupFlags = THREAD_GROUP_FLAGS_ABSENT,
95*1b191cb5SApple OSS Distributions 	},
96*1b191cb5SApple OSS Distributions 	[WI_CLASS_DISCRETIONARY] =
97*1b191cb5SApple OSS Distributions 	{
98*1b191cb5SApple OSS Distributions 		.name = "DISCRETIONARY",
99*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
100*1b191cb5SApple OSS Distributions 		.threadGroupFlags = THREAD_GROUP_FLAGS_EFFICIENT,
101*1b191cb5SApple OSS Distributions 	},
102*1b191cb5SApple OSS Distributions 	[WI_CLASS_BEST_EFFORT] =
103*1b191cb5SApple OSS Distributions 	{
104*1b191cb5SApple OSS Distributions 		.name = "BEST_EFFORT",
105*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
106*1b191cb5SApple OSS Distributions 		.threadGroupFlags = THREAD_GROUP_FLAGS_BEST_EFFORT,
107*1b191cb5SApple OSS Distributions 	},
108*1b191cb5SApple OSS Distributions 	[WI_CLASS_APP_SUPPORT] =
109*1b191cb5SApple OSS Distributions 	{
110*1b191cb5SApple OSS Distributions 		.name = "APPLICATION_SUPPORT",
111*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
112*1b191cb5SApple OSS Distributions 		.threadGroupFlags = 0,
113*1b191cb5SApple OSS Distributions 	},
114*1b191cb5SApple OSS Distributions 	[WI_CLASS_APPLICATION] =
115*1b191cb5SApple OSS Distributions 	{
116*1b191cb5SApple OSS Distributions 		.name = "APPLICATION",
117*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
118*1b191cb5SApple OSS Distributions 		.threadGroupFlags = THREAD_GROUP_FLAGS_APPLICATION,
119*1b191cb5SApple OSS Distributions 	},
120*1b191cb5SApple OSS Distributions 	[WI_CLASS_SYSTEM] =
121*1b191cb5SApple OSS Distributions 	{
122*1b191cb5SApple OSS Distributions 		.name = "SYSTEM",
123*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
124*1b191cb5SApple OSS Distributions 		.threadGroupFlags = 0,
125*1b191cb5SApple OSS Distributions 	},
126*1b191cb5SApple OSS Distributions 	[WI_CLASS_SYSTEM_CRITICAL] =
127*1b191cb5SApple OSS Distributions 	{
128*1b191cb5SApple OSS Distributions 		.name = "SYSTEM_CRITICAL",
129*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID,
130*1b191cb5SApple OSS Distributions 		.threadGroupFlags = THREAD_GROUP_FLAGS_CRITICAL,
131*1b191cb5SApple OSS Distributions 	},
132*1b191cb5SApple OSS Distributions 	[WI_CLASS_REALTIME] =
133*1b191cb5SApple OSS Distributions 	{
134*1b191cb5SApple OSS Distributions 		.name = "REALTIME",
135*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID |
136*1b191cb5SApple OSS Distributions 		                     WORK_INTERVAL_WORKLOAD_ID_RT_ALLOWED,
137*1b191cb5SApple OSS Distributions 		.threadGroupFlags = 0,
138*1b191cb5SApple OSS Distributions 	},
139*1b191cb5SApple OSS Distributions 	[WI_CLASS_REALTIME_CRITICAL] =
140*1b191cb5SApple OSS Distributions 	{
141*1b191cb5SApple OSS Distributions 		.name = "REALTIME_CRITICAL",
142*1b191cb5SApple OSS Distributions 		.workIntervalFlags = WORK_INTERVAL_WORKLOAD_ID_HAS_ID |
143*1b191cb5SApple OSS Distributions 		                     WORK_INTERVAL_WORKLOAD_ID_RT_ALLOWED |
144*1b191cb5SApple OSS Distributions 		                     WORK_INTERVAL_WORKLOAD_ID_RT_CRITICAL,
145*1b191cb5SApple OSS Distributions 		.threadGroupFlags = THREAD_GROUP_FLAGS_CRITICAL,
146*1b191cb5SApple OSS Distributions 	},
147*1b191cb5SApple OSS Distributions };
148*1b191cb5SApple OSS Distributions /* END IGNORE CODESTYLE */
149*1b191cb5SApple OSS Distributions 
150*1b191cb5SApple OSS Distributions struct FlagMap {
151*1b191cb5SApple OSS Distributions 	const char *str;
152*1b191cb5SApple OSS Distributions 	UInt32 flags;
153*1b191cb5SApple OSS Distributions };
154*1b191cb5SApple OSS Distributions 
155*1b191cb5SApple OSS Distributions static inline IOReturn
stringToFlags(const OSString & str,UInt32 & flags,const struct FlagMap * map,size_t mapLen)156*1b191cb5SApple OSS Distributions stringToFlags(const OSString &str, UInt32 &flags, const struct FlagMap *map,
157*1b191cb5SApple OSS Distributions     size_t mapLen)
158*1b191cb5SApple OSS Distributions {
159*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < mapLen; i++) {
160*1b191cb5SApple OSS Distributions 		if (str.isEqualTo(map[i].str)) {
161*1b191cb5SApple OSS Distributions 			flags = map[i].flags;
162*1b191cb5SApple OSS Distributions 			return kIOReturnSuccess;
163*1b191cb5SApple OSS Distributions 		}
164*1b191cb5SApple OSS Distributions 	}
165*1b191cb5SApple OSS Distributions 
166*1b191cb5SApple OSS Distributions 	return kIOReturnNotFound;
167*1b191cb5SApple OSS Distributions }
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions static inline IOReturn
flagsToString(const UInt32 flags,OSSharedPtr<OSString> & str,const struct FlagMap * map,size_t mapLen)170*1b191cb5SApple OSS Distributions flagsToString(const UInt32 flags, OSSharedPtr<OSString> &str, const struct FlagMap *map,
171*1b191cb5SApple OSS Distributions     size_t mapLen)
172*1b191cb5SApple OSS Distributions {
173*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < mapLen; i++) {
174*1b191cb5SApple OSS Distributions 		if (flags == map[i].flags) {
175*1b191cb5SApple OSS Distributions 			str = OSString::withCStringNoCopy(map[i].str);
176*1b191cb5SApple OSS Distributions 			return kIOReturnSuccess;
177*1b191cb5SApple OSS Distributions 		}
178*1b191cb5SApple OSS Distributions 	}
179*1b191cb5SApple OSS Distributions 
180*1b191cb5SApple OSS Distributions 	return kIOReturnNotFound;
181*1b191cb5SApple OSS Distributions }
182*1b191cb5SApple OSS Distributions 
183*1b191cb5SApple OSS Distributions /* BEGIN IGNORE CODESTYLE */
184*1b191cb5SApple OSS Distributions static const struct FlagMap typeMap[] = {
185*1b191cb5SApple OSS Distributions 	{
186*1b191cb5SApple OSS Distributions 		.str = "DEFAULT",
187*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_DEFAULT |
188*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_UNRESTRICTED,
189*1b191cb5SApple OSS Distributions 	},
190*1b191cb5SApple OSS Distributions 	{
191*1b191cb5SApple OSS Distributions 		.str = "COREAUDIO",
192*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_COREAUDIO |
193*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_ENABLE_AUTO_JOIN |
194*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_ENABLE_DEFERRED_FINISH,
195*1b191cb5SApple OSS Distributions 	},
196*1b191cb5SApple OSS Distributions 	{
197*1b191cb5SApple OSS Distributions 		.str = "COREANIMATION",
198*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_COREANIMATION,
199*1b191cb5SApple OSS Distributions 	},
200*1b191cb5SApple OSS Distributions 	{
201*1b191cb5SApple OSS Distributions 		.str = "CA_RENDER_SERVER",
202*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_CA_RENDER_SERVER,
203*1b191cb5SApple OSS Distributions 	},
204*1b191cb5SApple OSS Distributions 	{
205*1b191cb5SApple OSS Distributions 		.str = "FRAME_COMPOSITOR",
206*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_FRAME_COMPOSITOR,
207*1b191cb5SApple OSS Distributions 	},
208*1b191cb5SApple OSS Distributions 	{
209*1b191cb5SApple OSS Distributions 		.str = "CA_CLIENT",
210*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_CA_CLIENT |
211*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_UNRESTRICTED,
212*1b191cb5SApple OSS Distributions 	},
213*1b191cb5SApple OSS Distributions 	{
214*1b191cb5SApple OSS Distributions 		.str = "HID_DELIVERY",
215*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_HID_DELIVERY,
216*1b191cb5SApple OSS Distributions 	},
217*1b191cb5SApple OSS Distributions 	{
218*1b191cb5SApple OSS Distributions 		.str = "COREMEDIA",
219*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_COREMEDIA,
220*1b191cb5SApple OSS Distributions 	},
221*1b191cb5SApple OSS Distributions 	{
222*1b191cb5SApple OSS Distributions 		.str = "ARKIT",
223*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_ARKIT |
224*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_FINISH_AT_DEADLINE,
225*1b191cb5SApple OSS Distributions 	},
226*1b191cb5SApple OSS Distributions 	{
227*1b191cb5SApple OSS Distributions 		.str  = "AUDIO_CLIENT",
228*1b191cb5SApple OSS Distributions 		.flags = WORK_INTERVAL_TYPE_COREAUDIO |
229*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_UNRESTRICTED |
230*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_ENABLE_AUTO_JOIN |
231*1b191cb5SApple OSS Distributions 		         WORK_INTERVAL_FLAG_ENABLE_DEFERRED_FINISH
232*1b191cb5SApple OSS Distributions 	},
233*1b191cb5SApple OSS Distributions };
234*1b191cb5SApple OSS Distributions /* END IGNORE CODESTYLE */
235*1b191cb5SApple OSS Distributions 
236*1b191cb5SApple OSS Distributions static IOReturn
unparseWorkIntervalType(const UInt32 createFlags,OSSharedPtr<OSString> & typeStr)237*1b191cb5SApple OSS Distributions unparseWorkIntervalType(const UInt32 createFlags, OSSharedPtr<OSString> &typeStr)
238*1b191cb5SApple OSS Distributions {
239*1b191cb5SApple OSS Distributions 	IOReturn ret = flagsToString(createFlags, typeStr, typeMap,
240*1b191cb5SApple OSS Distributions 	    ARRAY_LEN(typeMap));
241*1b191cb5SApple OSS Distributions 	if (ret != kIOReturnSuccess) {
242*1b191cb5SApple OSS Distributions 		WLC_LOG("unrecognised create flags: 0x%x\n", createFlags);
243*1b191cb5SApple OSS Distributions 	}
244*1b191cb5SApple OSS Distributions 
245*1b191cb5SApple OSS Distributions 	return ret;
246*1b191cb5SApple OSS Distributions }
247*1b191cb5SApple OSS Distributions 
248*1b191cb5SApple OSS Distributions static IOReturn
parseWorkIntervalType(const OSSymbol & id,const OSObject * typeObj,UInt32 & createFlags)249*1b191cb5SApple OSS Distributions parseWorkIntervalType(const OSSymbol &id, const OSObject *typeObj, UInt32 &createFlags)
250*1b191cb5SApple OSS Distributions {
251*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSString> defaultIntervalType = OSString::withCString("DEFAULT");
252*1b191cb5SApple OSS Distributions 
253*1b191cb5SApple OSS Distributions 	const OSString *typeStr = OSDynamicCast(OSString, typeObj);
254*1b191cb5SApple OSS Distributions 	if (typeStr == nullptr) {
255*1b191cb5SApple OSS Distributions 		typeStr = defaultIntervalType.get();
256*1b191cb5SApple OSS Distributions 	}
257*1b191cb5SApple OSS Distributions 
258*1b191cb5SApple OSS Distributions 	IOReturn ret = stringToFlags(*typeStr, createFlags, typeMap,
259*1b191cb5SApple OSS Distributions 	    ARRAY_LEN(typeMap));
260*1b191cb5SApple OSS Distributions 	if (ret != kIOReturnSuccess) {
261*1b191cb5SApple OSS Distributions 		WLC_LOG("unrecognised \"" kWorkIntervalTypeKey "\": \"%s\"\n",
262*1b191cb5SApple OSS Distributions 		    typeStr->getCStringNoCopy());
263*1b191cb5SApple OSS Distributions 	}
264*1b191cb5SApple OSS Distributions 
265*1b191cb5SApple OSS Distributions 	return ret;
266*1b191cb5SApple OSS Distributions }
267*1b191cb5SApple OSS Distributions 
268*1b191cb5SApple OSS Distributions static IOReturn
parseWorkloadClass(const OSSymbol & id,const OSObject * wlClassObj,wi_class_t & wiClass)269*1b191cb5SApple OSS Distributions parseWorkloadClass(const OSSymbol &id, const OSObject *wlClassObj, wi_class_t &wiClass)
270*1b191cb5SApple OSS Distributions {
271*1b191cb5SApple OSS Distributions 	const OSString *wlClass = OSDynamicCast(OSString, wlClassObj);
272*1b191cb5SApple OSS Distributions 	if (wlClass == nullptr) {
273*1b191cb5SApple OSS Distributions 		wiClass = WI_CLASS_NONE;
274*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
275*1b191cb5SApple OSS Distributions 	}
276*1b191cb5SApple OSS Distributions 
277*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < ARRAY_LEN(wlClassData); i++) {
278*1b191cb5SApple OSS Distributions 		if (wlClassData[i].name != nullptr &&
279*1b191cb5SApple OSS Distributions 		    wlClass->isEqualTo(wlClassData[i].name)) {
280*1b191cb5SApple OSS Distributions 			wiClass = (wi_class_t)i;
281*1b191cb5SApple OSS Distributions 			return kIOReturnSuccess;
282*1b191cb5SApple OSS Distributions 		}
283*1b191cb5SApple OSS Distributions 	}
284*1b191cb5SApple OSS Distributions 
285*1b191cb5SApple OSS Distributions 	WLC_LOG("%s: unknown %s: \"%s\"\n", id.getCStringNoCopy(),
286*1b191cb5SApple OSS Distributions 	    kWorkloadClassKey, wlClass->getCStringNoCopy());
287*1b191cb5SApple OSS Distributions 	return kIOReturnError;
288*1b191cb5SApple OSS Distributions }
289*1b191cb5SApple OSS Distributions 
290*1b191cb5SApple OSS Distributions static IOReturn
parseCriticalityOffset(const OSSymbol & id,const wi_class_t wiClass,const OSObject * cOffsetObj,uint8_t & criticalityOffset)291*1b191cb5SApple OSS Distributions parseCriticalityOffset(const OSSymbol &id, const wi_class_t wiClass,
292*1b191cb5SApple OSS Distributions     const OSObject *cOffsetObj, uint8_t &criticalityOffset)
293*1b191cb5SApple OSS Distributions {
294*1b191cb5SApple OSS Distributions 	if (wiClass != WI_CLASS_SYSTEM_CRITICAL &&
295*1b191cb5SApple OSS Distributions 	    wiClass != WI_CLASS_REALTIME_CRITICAL &&
296*1b191cb5SApple OSS Distributions 	    wiClass != WI_CLASS_BEST_EFFORT &&
297*1b191cb5SApple OSS Distributions 	    wiClass != WI_CLASS_APP_SUPPORT &&
298*1b191cb5SApple OSS Distributions 	    wiClass != WI_CLASS_SYSTEM) {
299*1b191cb5SApple OSS Distributions 		criticalityOffset = 0;
300*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
301*1b191cb5SApple OSS Distributions 	}
302*1b191cb5SApple OSS Distributions 
303*1b191cb5SApple OSS Distributions 	const OSNumber *cOffset = OSDynamicCast(OSNumber, cOffsetObj);
304*1b191cb5SApple OSS Distributions 	if (cOffset == nullptr) {
305*1b191cb5SApple OSS Distributions 		criticalityOffset = 0;
306*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
307*1b191cb5SApple OSS Distributions 	}
308*1b191cb5SApple OSS Distributions 
309*1b191cb5SApple OSS Distributions 	UInt64 criticalityOffset64 = cOffset->unsigned64BitValue();
310*1b191cb5SApple OSS Distributions 	const int nBytes = cOffset->numberOfBytes();
311*1b191cb5SApple OSS Distributions 	if (nBytes <= sizeof(criticalityOffset64) &&
312*1b191cb5SApple OSS Distributions 	    criticalityOffset64 < MAX_CRITICALITY_OFFSET) {
313*1b191cb5SApple OSS Distributions 		criticalityOffset = (uint8_t)criticalityOffset64;
314*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
315*1b191cb5SApple OSS Distributions 	}
316*1b191cb5SApple OSS Distributions 
317*1b191cb5SApple OSS Distributions 	WLC_LOG("%s: criticality offset too large\n", id.getCStringNoCopy());
318*1b191cb5SApple OSS Distributions 	return kIOReturnError;
319*1b191cb5SApple OSS Distributions }
320*1b191cb5SApple OSS Distributions 
321*1b191cb5SApple OSS Distributions static IOReturn
parseFlags(const OSSymbol & id,const OSObject * flagsObj,UInt32 & threadGroupFlags,UInt32 & workIntervalFlags)322*1b191cb5SApple OSS Distributions parseFlags(const OSSymbol &id, const OSObject *flagsObj, UInt32 &threadGroupFlags,
323*1b191cb5SApple OSS Distributions     UInt32 &workIntervalFlags)
324*1b191cb5SApple OSS Distributions {
325*1b191cb5SApple OSS Distributions 	/* Optional, so just carry on if not found. */
326*1b191cb5SApple OSS Distributions 	if (flagsObj == nullptr) {
327*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
328*1b191cb5SApple OSS Distributions 	}
329*1b191cb5SApple OSS Distributions 
330*1b191cb5SApple OSS Distributions 	OSArray *flags = OSDynamicCast(OSArray, flagsObj);
331*1b191cb5SApple OSS Distributions 	if (flags == nullptr) {
332*1b191cb5SApple OSS Distributions 		WLC_LOG("failed to parse \"" kFlagsKey "\"\n");
333*1b191cb5SApple OSS Distributions 		return kIOReturnError;
334*1b191cb5SApple OSS Distributions 	}
335*1b191cb5SApple OSS Distributions 
336*1b191cb5SApple OSS Distributions 	/* BEGIN IGNORE CODESTYLE */
337*1b191cb5SApple OSS Distributions 	__block IOReturn ret = kIOReturnSuccess;
338*1b191cb5SApple OSS Distributions 	flags->iterateObjects(^bool (OSObject *object) {
339*1b191cb5SApple OSS Distributions 		const OSString *flag = OSDynamicCast(OSString, object);
340*1b191cb5SApple OSS Distributions 		if (flag == nullptr) {
341*1b191cb5SApple OSS Distributions 			WLC_LOG("%s: non-string flag found\n", id.getCStringNoCopy());
342*1b191cb5SApple OSS Distributions 			ret = kIOReturnError;
343*1b191cb5SApple OSS Distributions 			return true;
344*1b191cb5SApple OSS Distributions 
345*1b191cb5SApple OSS Distributions 		}
346*1b191cb5SApple OSS Distributions 
347*1b191cb5SApple OSS Distributions 		/* Ignore unknown flags. */
348*1b191cb5SApple OSS Distributions 		if (flag->isEqualTo(kWIComplexityAllowedValue)) {
349*1b191cb5SApple OSS Distributions 			workIntervalFlags |= WORK_INTERVAL_WORKLOAD_ID_COMPLEXITY_ALLOWED;
350*1b191cb5SApple OSS Distributions 		}
351*1b191cb5SApple OSS Distributions 
352*1b191cb5SApple OSS Distributions 		return false;
353*1b191cb5SApple OSS Distributions 	});
354*1b191cb5SApple OSS Distributions 	/* END IGNORE CODESTYLE */
355*1b191cb5SApple OSS Distributions 
356*1b191cb5SApple OSS Distributions 	return ret;
357*1b191cb5SApple OSS Distributions }
358*1b191cb5SApple OSS Distributions 
359*1b191cb5SApple OSS Distributions static
360*1b191cb5SApple OSS Distributions IOReturn
parsePhases(workload_config_ctx_t * ctx,const OSSymbol & id,OSObject * phasesObj)361*1b191cb5SApple OSS Distributions parsePhases(workload_config_ctx_t *ctx, const OSSymbol &id, OSObject *phasesObj)
362*1b191cb5SApple OSS Distributions {
363*1b191cb5SApple OSS Distributions 	__block IOReturn ret = kIOReturnError;
364*1b191cb5SApple OSS Distributions 
365*1b191cb5SApple OSS Distributions 	OSDictionary *phases = OSDynamicCast(OSDictionary, phasesObj);
366*1b191cb5SApple OSS Distributions 	if (phases == nullptr) {
367*1b191cb5SApple OSS Distributions 		WLC_LOG("%s: failed to find dictionary for \"" kPhasesKey "\"\n",
368*1b191cb5SApple OSS Distributions 		    id.getCStringNoCopy());
369*1b191cb5SApple OSS Distributions 		return kIOReturnError;
370*1b191cb5SApple OSS Distributions 	}
371*1b191cb5SApple OSS Distributions 
372*1b191cb5SApple OSS Distributions 	/* There should be at least one phase described. */
373*1b191cb5SApple OSS Distributions 	ret = kIOReturnError;
374*1b191cb5SApple OSS Distributions 
375*1b191cb5SApple OSS Distributions 	/* BEGIN IGNORE CODESTYLE */
376*1b191cb5SApple OSS Distributions 	phases->iterateObjects(^bool (const OSSymbol *phase, OSObject *value) {
377*1b191cb5SApple OSS Distributions 		const OSDictionary *dict = OSDynamicCast(OSDictionary, value);
378*1b191cb5SApple OSS Distributions 		if (dict == nullptr) {
379*1b191cb5SApple OSS Distributions 			WLC_LOG("%s: failed to find dictionary for \"%s\" phase\n",
380*1b191cb5SApple OSS Distributions 			    id.getCStringNoCopy(), phase->getCStringNoCopy());
381*1b191cb5SApple OSS Distributions 			ret = kIOReturnError;
382*1b191cb5SApple OSS Distributions 			return true;
383*1b191cb5SApple OSS Distributions 		}
384*1b191cb5SApple OSS Distributions 
385*1b191cb5SApple OSS Distributions 		UInt32 createFlags = 0;
386*1b191cb5SApple OSS Distributions 		ret = parseWorkIntervalType(id, dict->getObject(kWorkIntervalTypeKey),
387*1b191cb5SApple OSS Distributions 		    createFlags);
388*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
389*1b191cb5SApple OSS Distributions 			return true;
390*1b191cb5SApple OSS Distributions 		}
391*1b191cb5SApple OSS Distributions 
392*1b191cb5SApple OSS Distributions 		wi_class_t wiClass = WI_CLASS_NONE;
393*1b191cb5SApple OSS Distributions 		ret = parseWorkloadClass(id, dict->getObject(kWorkloadClassKey), wiClass);
394*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
395*1b191cb5SApple OSS Distributions 			return true;
396*1b191cb5SApple OSS Distributions 		}
397*1b191cb5SApple OSS Distributions 		const struct WorkloadClassData classData = wlClassData[wiClass];
398*1b191cb5SApple OSS Distributions 
399*1b191cb5SApple OSS Distributions 		uint8_t criticalityOffset = 0;
400*1b191cb5SApple OSS Distributions 		ret = parseCriticalityOffset(id, wiClass,
401*1b191cb5SApple OSS Distributions 		    dict->getObject(kCriticalityOffsetKey), criticalityOffset);
402*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
403*1b191cb5SApple OSS Distributions 			return true;
404*1b191cb5SApple OSS Distributions 		}
405*1b191cb5SApple OSS Distributions 
406*1b191cb5SApple OSS Distributions 		UInt32 threadGroupFlags = classData.threadGroupFlags;
407*1b191cb5SApple OSS Distributions 		UInt32 workIntervalFlags = classData.workIntervalFlags;
408*1b191cb5SApple OSS Distributions 		ret = parseFlags(id, dict->getObject(kFlagsKey), threadGroupFlags, workIntervalFlags);
409*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
410*1b191cb5SApple OSS Distributions 			return true;
411*1b191cb5SApple OSS Distributions 		}
412*1b191cb5SApple OSS Distributions 
413*1b191cb5SApple OSS Distributions 		const workload_config_t config = {
414*1b191cb5SApple OSS Distributions 		    .wc_thread_group_flags = threadGroupFlags,
415*1b191cb5SApple OSS Distributions 		    .wc_flags = workIntervalFlags,
416*1b191cb5SApple OSS Distributions 		    .wc_create_flags = createFlags,
417*1b191cb5SApple OSS Distributions 		    .wc_class_offset = (uint8_t)criticalityOffset,
418*1b191cb5SApple OSS Distributions 		    .wc_class = wiClass,
419*1b191cb5SApple OSS Distributions 		};
420*1b191cb5SApple OSS Distributions 		ret = workload_config_insert(ctx, id.getCStringNoCopy(), phase->getCStringNoCopy(), &config);
421*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
422*1b191cb5SApple OSS Distributions 			WLC_LOG("%s: failed to add \"%s\" phase\n",
423*1b191cb5SApple OSS Distributions 			id.getCStringNoCopy(), phase->getCStringNoCopy());
424*1b191cb5SApple OSS Distributions 			return true;
425*1b191cb5SApple OSS Distributions 		}
426*1b191cb5SApple OSS Distributions 
427*1b191cb5SApple OSS Distributions 		return false;
428*1b191cb5SApple OSS Distributions 	});
429*1b191cb5SApple OSS Distributions 	/* END IGNORE CODESTYLE */
430*1b191cb5SApple OSS Distributions 
431*1b191cb5SApple OSS Distributions 	return ret;
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions 
434*1b191cb5SApple OSS Distributions static IOReturn
parseRoot(const OSSymbol & id,const OSObject * rootDict,OSString * & defaultPhase)435*1b191cb5SApple OSS Distributions parseRoot(const OSSymbol &id, const OSObject *rootDict, OSString *&defaultPhase)
436*1b191cb5SApple OSS Distributions {
437*1b191cb5SApple OSS Distributions 	const OSDictionary *root = OSDynamicCast(OSDictionary, rootDict);
438*1b191cb5SApple OSS Distributions 	if (root == nullptr) {
439*1b191cb5SApple OSS Distributions 		WLC_LOG("%s: failed to find dictionary for \"" kRootKey "\"\n",
440*1b191cb5SApple OSS Distributions 		    id.getCStringNoCopy());
441*1b191cb5SApple OSS Distributions 		return kIOReturnError;
442*1b191cb5SApple OSS Distributions 	}
443*1b191cb5SApple OSS Distributions 
444*1b191cb5SApple OSS Distributions 	defaultPhase = OSDynamicCast(OSString, root->getObject(kDefaultPhaseKey));
445*1b191cb5SApple OSS Distributions 	if (defaultPhase == nullptr) {
446*1b191cb5SApple OSS Distributions 		WLC_LOG("%s: failed to find \"" kDefaultPhaseKey"\" in \"" kRootKey "\" dictionary\n",
447*1b191cb5SApple OSS Distributions 		    id.getCStringNoCopy());
448*1b191cb5SApple OSS Distributions 		return kIOReturnError;
449*1b191cb5SApple OSS Distributions 	}
450*1b191cb5SApple OSS Distributions 
451*1b191cb5SApple OSS Distributions 	if (defaultPhase->getLength() == 0) {
452*1b191cb5SApple OSS Distributions 		WLC_LOG("%s: \"" kDefaultPhaseKey" \" is empty in \"" kRootKey "\" dictionary\n",
453*1b191cb5SApple OSS Distributions 		    id.getCStringNoCopy());
454*1b191cb5SApple OSS Distributions 		return kIOReturnError;
455*1b191cb5SApple OSS Distributions 	}
456*1b191cb5SApple OSS Distributions 
457*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
458*1b191cb5SApple OSS Distributions }
459*1b191cb5SApple OSS Distributions 
460*1b191cb5SApple OSS Distributions static IOReturn
parseWorkloadIDTable(workload_config_ctx_t * ctx,OSDictionary * IDTable)461*1b191cb5SApple OSS Distributions parseWorkloadIDTable(workload_config_ctx_t *ctx, OSDictionary *IDTable)
462*1b191cb5SApple OSS Distributions {
463*1b191cb5SApple OSS Distributions 	/*
464*1b191cb5SApple OSS Distributions 	 * At least one valid entry is expected, so start off with error to
465*1b191cb5SApple OSS Distributions 	 * catch an empty table or one with no valid entries.
466*1b191cb5SApple OSS Distributions 	 */
467*1b191cb5SApple OSS Distributions 	__block IOReturn ret = kIOReturnError;
468*1b191cb5SApple OSS Distributions 
469*1b191cb5SApple OSS Distributions 	/* BEGIN IGNORE CODESTYLE */
470*1b191cb5SApple OSS Distributions 	IDTable->iterateObjects(^bool (const OSSymbol *id, OSObject *value) {
471*1b191cb5SApple OSS Distributions 		/* Validate the workload ID. */
472*1b191cb5SApple OSS Distributions 		if (id->getLength() == 0) {
473*1b191cb5SApple OSS Distributions 			WLC_LOG("zero length ID in \"" kWorkloadIDTableKey "\"\n");
474*1b191cb5SApple OSS Distributions 			ret = kIOReturnError;
475*1b191cb5SApple OSS Distributions 			return true;
476*1b191cb5SApple OSS Distributions 		}
477*1b191cb5SApple OSS Distributions 
478*1b191cb5SApple OSS Distributions 		/* Parse its properties. */
479*1b191cb5SApple OSS Distributions 		OSDictionary *idConfig = OSDynamicCast(OSDictionary, value);
480*1b191cb5SApple OSS Distributions 		if (idConfig == nullptr) {
481*1b191cb5SApple OSS Distributions 			WLC_LOG("failed to find dictionary for \"%s\"\n",
482*1b191cb5SApple OSS Distributions 			id->getCStringNoCopy());
483*1b191cb5SApple OSS Distributions 			ret = kIOReturnError;
484*1b191cb5SApple OSS Distributions 			return true;
485*1b191cb5SApple OSS Distributions 		}
486*1b191cb5SApple OSS Distributions 
487*1b191cb5SApple OSS Distributions 		ret = parsePhases(ctx, *id, idConfig->getObject(kPhasesKey));
488*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
489*1b191cb5SApple OSS Distributions 			return true;
490*1b191cb5SApple OSS Distributions 		}
491*1b191cb5SApple OSS Distributions 
492*1b191cb5SApple OSS Distributions 		OSString *defaultPhase = nullptr;
493*1b191cb5SApple OSS Distributions 		ret = parseRoot(*id, idConfig->getObject(kRootKey), defaultPhase);
494*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
495*1b191cb5SApple OSS Distributions 			return true;
496*1b191cb5SApple OSS Distributions 		}
497*1b191cb5SApple OSS Distributions 
498*1b191cb5SApple OSS Distributions 		/* Fails if the specified phase doesn't exist.. */
499*1b191cb5SApple OSS Distributions 		ret = workload_config_set_default(ctx, id->getCStringNoCopy(),
500*1b191cb5SApple OSS Distributions 		defaultPhase->getCStringNoCopy());
501*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
502*1b191cb5SApple OSS Distributions 			WLC_LOG("failed to set default phase (%s) for \"%s\"\n",
503*1b191cb5SApple OSS Distributions 			defaultPhase->getCStringNoCopy(), id->getCStringNoCopy());
504*1b191cb5SApple OSS Distributions 			return true;
505*1b191cb5SApple OSS Distributions 		}
506*1b191cb5SApple OSS Distributions 
507*1b191cb5SApple OSS Distributions 		return false;
508*1b191cb5SApple OSS Distributions 	});
509*1b191cb5SApple OSS Distributions 	/* END IGNORE CODESTYLE */
510*1b191cb5SApple OSS Distributions 
511*1b191cb5SApple OSS Distributions 	return ret;
512*1b191cb5SApple OSS Distributions }
513*1b191cb5SApple OSS Distributions 
514*1b191cb5SApple OSS Distributions static IOReturn
parseWorkloadIDConfigurationFlags(workload_config_ctx_t * ctx,const OSObject * idTableFlagsObj)515*1b191cb5SApple OSS Distributions parseWorkloadIDConfigurationFlags(workload_config_ctx_t *ctx, const OSObject *idTableFlagsObj)
516*1b191cb5SApple OSS Distributions {
517*1b191cb5SApple OSS Distributions 	/* Optional, so just carry on if not found. */
518*1b191cb5SApple OSS Distributions 	if (idTableFlagsObj == nullptr) {
519*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
520*1b191cb5SApple OSS Distributions 	}
521*1b191cb5SApple OSS Distributions 
522*1b191cb5SApple OSS Distributions 	OSArray *idTableFlags = OSDynamicCast(OSArray, idTableFlagsObj);
523*1b191cb5SApple OSS Distributions 	if (idTableFlags == nullptr) {
524*1b191cb5SApple OSS Distributions 		WLC_LOG("failed to parse \""
525*1b191cb5SApple OSS Distributions 		    kWorkloadIDConfigurationFlagsKey "\"\n");
526*1b191cb5SApple OSS Distributions 		return kIOReturnError;
527*1b191cb5SApple OSS Distributions 	}
528*1b191cb5SApple OSS Distributions 
529*1b191cb5SApple OSS Distributions 	/* BEGIN IGNORE CODESTYLE */
530*1b191cb5SApple OSS Distributions 	__block IOReturn ret = kIOReturnSuccess;
531*1b191cb5SApple OSS Distributions 	idTableFlags->iterateObjects(^bool (OSObject *object) {
532*1b191cb5SApple OSS Distributions 		const OSString *flag = OSDynamicCast(OSString, object);
533*1b191cb5SApple OSS Distributions 		if (flag == nullptr) {
534*1b191cb5SApple OSS Distributions 			WLC_LOG("non-string Workload ID Table flag found\n");
535*1b191cb5SApple OSS Distributions 			ret = kIOReturnError;
536*1b191cb5SApple OSS Distributions 			return true;
537*1b191cb5SApple OSS Distributions 		}
538*1b191cb5SApple OSS Distributions 
539*1b191cb5SApple OSS Distributions 		if (flag->isEqualTo(kDisableWorkloadClassThreadPolicyValue)) {
540*1b191cb5SApple OSS Distributions 			workload_config_clear_flag(ctx, WLC_F_THREAD_POLICY);
541*1b191cb5SApple OSS Distributions 		}
542*1b191cb5SApple OSS Distributions 
543*1b191cb5SApple OSS Distributions 		return false;
544*1b191cb5SApple OSS Distributions 	});
545*1b191cb5SApple OSS Distributions 	/* END IGNORE CODESTYLE */
546*1b191cb5SApple OSS Distributions 
547*1b191cb5SApple OSS Distributions 	return ret;
548*1b191cb5SApple OSS Distributions }
549*1b191cb5SApple OSS Distributions 
550*1b191cb5SApple OSS Distributions static IOReturn
unparseWorkloadIDConfigurationFlags(OSSharedPtr<OSDictionary> & plist)551*1b191cb5SApple OSS Distributions unparseWorkloadIDConfigurationFlags(OSSharedPtr<OSDictionary> &plist)
552*1b191cb5SApple OSS Distributions {
553*1b191cb5SApple OSS Distributions 	workload_config_flags_t flags = WLC_F_NONE;
554*1b191cb5SApple OSS Distributions 
555*1b191cb5SApple OSS Distributions 	/* There may be no config at all. That's ok. */
556*1b191cb5SApple OSS Distributions 	if (workload_config_get_flags(&flags) != KERN_SUCCESS) {
557*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
558*1b191cb5SApple OSS Distributions 	}
559*1b191cb5SApple OSS Distributions 
560*1b191cb5SApple OSS Distributions 	/* Workload config can change thread policy scheduling - the default. */
561*1b191cb5SApple OSS Distributions 	if ((flags & WLC_F_THREAD_POLICY) != 0) {
562*1b191cb5SApple OSS Distributions 		return kIOReturnSuccess;
563*1b191cb5SApple OSS Distributions 	}
564*1b191cb5SApple OSS Distributions 
565*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSArray> idTableFlags = OSArray::withCapacity(1);
566*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSString> flag = OSString::withCString(kDisableWorkloadClassThreadPolicyValue);
567*1b191cb5SApple OSS Distributions 	if (!idTableFlags->setObject(flag) ||
568*1b191cb5SApple OSS Distributions 	    !plist->setObject(kWorkloadIDConfigurationFlagsKey, idTableFlags)) {
569*1b191cb5SApple OSS Distributions 		return kIOReturnError;
570*1b191cb5SApple OSS Distributions 	}
571*1b191cb5SApple OSS Distributions 
572*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
573*1b191cb5SApple OSS Distributions }
574*1b191cb5SApple OSS Distributions 
575*1b191cb5SApple OSS Distributions extern "C" {
576*1b191cb5SApple OSS Distributions extern IOReturn IOParseWorkloadConfig(workload_config_ctx_t *, const char *, size_t);
577*1b191cb5SApple OSS Distributions extern IOReturn IOUnparseWorkloadConfig(char *, size_t *);
578*1b191cb5SApple OSS Distributions }
579*1b191cb5SApple OSS Distributions 
580*1b191cb5SApple OSS Distributions /* Called locked. */
581*1b191cb5SApple OSS Distributions IOReturn
IOParseWorkloadConfig(workload_config_ctx_t * ctx,const char * buffer,size_t size)582*1b191cb5SApple OSS Distributions IOParseWorkloadConfig(workload_config_ctx_t *ctx, const char *buffer, size_t size)
583*1b191cb5SApple OSS Distributions {
584*1b191cb5SApple OSS Distributions 	IOReturn ret = kIOReturnError;
585*1b191cb5SApple OSS Distributions 
586*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSString> unserializeErrorString = nullptr;
587*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSObject> obj = nullptr;
588*1b191cb5SApple OSS Distributions 	OSDictionary *idTable = nullptr;
589*1b191cb5SApple OSS Distributions 	OSDictionary *dict = nullptr;
590*1b191cb5SApple OSS Distributions 
591*1b191cb5SApple OSS Distributions 	ret = workload_config_init(ctx);
592*1b191cb5SApple OSS Distributions 	if (ret != kIOReturnSuccess) {
593*1b191cb5SApple OSS Distributions 		WLC_LOG("failed to initialize workload configuration\n");
594*1b191cb5SApple OSS Distributions 		goto out;
595*1b191cb5SApple OSS Distributions 	}
596*1b191cb5SApple OSS Distributions 
597*1b191cb5SApple OSS Distributions 	obj = OSUnserializeXML(buffer, unserializeErrorString);
598*1b191cb5SApple OSS Distributions 	dict = OSDynamicCast(OSDictionary, obj.get());
599*1b191cb5SApple OSS Distributions 	if (dict == nullptr) {
600*1b191cb5SApple OSS Distributions 		WLC_LOG("failed to unserialize plist\n");
601*1b191cb5SApple OSS Distributions 		ret = kIOReturnError;
602*1b191cb5SApple OSS Distributions 		goto out;
603*1b191cb5SApple OSS Distributions 	}
604*1b191cb5SApple OSS Distributions 
605*1b191cb5SApple OSS Distributions 	idTable = OSDynamicCast(OSDictionary, dict->getObject(kWorkloadIDTableKey));
606*1b191cb5SApple OSS Distributions 	if (idTable == nullptr) {
607*1b191cb5SApple OSS Distributions 		WLC_LOG("failed to find " kWorkloadIDTableKey "\n");
608*1b191cb5SApple OSS Distributions 		ret = kIOReturnError;
609*1b191cb5SApple OSS Distributions 		goto out;
610*1b191cb5SApple OSS Distributions 	}
611*1b191cb5SApple OSS Distributions 
612*1b191cb5SApple OSS Distributions 	ret = parseWorkloadIDTable(ctx, idTable);
613*1b191cb5SApple OSS Distributions 	if (ret != kIOReturnSuccess) {
614*1b191cb5SApple OSS Distributions 		goto out;
615*1b191cb5SApple OSS Distributions 	}
616*1b191cb5SApple OSS Distributions 
617*1b191cb5SApple OSS Distributions 	ret = parseWorkloadIDConfigurationFlags(ctx, dict->getObject(kWorkloadIDConfigurationFlagsKey));
618*1b191cb5SApple OSS Distributions 	if (ret != kIOReturnSuccess) {
619*1b191cb5SApple OSS Distributions 		goto out;
620*1b191cb5SApple OSS Distributions 	}
621*1b191cb5SApple OSS Distributions 
622*1b191cb5SApple OSS Distributions 	ret = kIOReturnSuccess;
623*1b191cb5SApple OSS Distributions 
624*1b191cb5SApple OSS Distributions out:
625*1b191cb5SApple OSS Distributions 	if (ret != kIOReturnSuccess) {
626*1b191cb5SApple OSS Distributions 		workload_config_free(ctx);
627*1b191cb5SApple OSS Distributions 	}
628*1b191cb5SApple OSS Distributions 
629*1b191cb5SApple OSS Distributions 	return ret;
630*1b191cb5SApple OSS Distributions }
631*1b191cb5SApple OSS Distributions 
632*1b191cb5SApple OSS Distributions /*
633*1b191cb5SApple OSS Distributions  * Does the reverse of IOParseWorkloadConfig() - i.e. serializes the internal
634*1b191cb5SApple OSS Distributions  * workload configuration.
635*1b191cb5SApple OSS Distributions  * The serialized workload config is copied to 'buffer' (if non-NULL).
636*1b191cb5SApple OSS Distributions  * size is in/out - it describes the size of buffer and on return the length of
637*1b191cb5SApple OSS Distributions  * the serialized config.
638*1b191cb5SApple OSS Distributions  */
639*1b191cb5SApple OSS Distributions IOReturn
IOUnparseWorkloadConfig(char * buffer,size_t * size)640*1b191cb5SApple OSS Distributions IOUnparseWorkloadConfig(char *buffer, size_t *size)
641*1b191cb5SApple OSS Distributions {
642*1b191cb5SApple OSS Distributions 	assert(size != nullptr);
643*1b191cb5SApple OSS Distributions 
644*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSDictionary> dict = nullptr;;
645*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSDictionary> idTable = nullptr;
646*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSSerialize> serialize = nullptr;
647*1b191cb5SApple OSS Distributions 
648*1b191cb5SApple OSS Distributions 	serialize = OSSerialize::withCapacity(1);
649*1b191cb5SApple OSS Distributions 	if (serialize == nullptr) {
650*1b191cb5SApple OSS Distributions 		return kIOReturnNoMemory;
651*1b191cb5SApple OSS Distributions 	}
652*1b191cb5SApple OSS Distributions 
653*1b191cb5SApple OSS Distributions 	dict = OSDictionary::withCapacity(1);
654*1b191cb5SApple OSS Distributions 	if (dict == nullptr) {
655*1b191cb5SApple OSS Distributions 		return kIOReturnNoMemory;
656*1b191cb5SApple OSS Distributions 	}
657*1b191cb5SApple OSS Distributions 
658*1b191cb5SApple OSS Distributions 	idTable = OSDictionary::withCapacity(1);
659*1b191cb5SApple OSS Distributions 	if (idTable == nullptr) {
660*1b191cb5SApple OSS Distributions 		return kIOReturnNoMemory;
661*1b191cb5SApple OSS Distributions 	}
662*1b191cb5SApple OSS Distributions 
663*1b191cb5SApple OSS Distributions 	__block IOReturn ret = kIOReturnSuccess;
664*1b191cb5SApple OSS Distributions 	/* BEGIN IGNORE CODESTYLE */
665*1b191cb5SApple OSS Distributions 	workload_config_iterate(^(const char *id_str, const void *config) {
666*1b191cb5SApple OSS Distributions 		OSSharedPtr<OSDictionary> idDict = OSDictionary::withCapacity(1);
667*1b191cb5SApple OSS Distributions 		if (idDict == nullptr) {
668*1b191cb5SApple OSS Distributions 			ret = kIOReturnNoMemory;
669*1b191cb5SApple OSS Distributions 			return true;
670*1b191cb5SApple OSS Distributions 		}
671*1b191cb5SApple OSS Distributions 
672*1b191cb5SApple OSS Distributions 		OSSharedPtr<OSDictionary> phase = OSDictionary::withCapacity(1);
673*1b191cb5SApple OSS Distributions 		if (phase == nullptr) {
674*1b191cb5SApple OSS Distributions 			ret = kIOReturnNoMemory;
675*1b191cb5SApple OSS Distributions 			return true;
676*1b191cb5SApple OSS Distributions 		}
677*1b191cb5SApple OSS Distributions 
678*1b191cb5SApple OSS Distributions 		workload_config_phases_iterate(config, ^(const char *phase_str,
679*1b191cb5SApple OSS Distributions 		    const bool is_default, const workload_config_t *wc) {
680*1b191cb5SApple OSS Distributions 			OSSharedPtr<OSDictionary> phaseData = OSDictionary::withCapacity(1);
681*1b191cb5SApple OSS Distributions 			if (phaseData == nullptr) {
682*1b191cb5SApple OSS Distributions 				ret = kIOReturnNoMemory;
683*1b191cb5SApple OSS Distributions 				return true;
684*1b191cb5SApple OSS Distributions 			}
685*1b191cb5SApple OSS Distributions 
686*1b191cb5SApple OSS Distributions 			if (wc->wc_class != WI_CLASS_NONE) {
687*1b191cb5SApple OSS Distributions 				assert3u(wc->wc_class, <, WI_CLASS_COUNT);
688*1b191cb5SApple OSS Distributions 				OSSharedPtr<OSString> wClass = OSString::withCString(wlClassData[wc->wc_class].name);
689*1b191cb5SApple OSS Distributions 				if (wClass == nullptr || !phaseData->setObject(kWorkloadClassKey, wClass)) {
690*1b191cb5SApple OSS Distributions 					ret = kIOReturnError;
691*1b191cb5SApple OSS Distributions 					return true;
692*1b191cb5SApple OSS Distributions 				}
693*1b191cb5SApple OSS Distributions 			}
694*1b191cb5SApple OSS Distributions 
695*1b191cb5SApple OSS Distributions 			if (wc->wc_class_offset > 0) {
696*1b191cb5SApple OSS Distributions 				OSSharedPtr<OSNumber> criticalityOffset = OSNumber::withNumber(wc->wc_class_offset, 8);
697*1b191cb5SApple OSS Distributions 				if (criticalityOffset == nullptr ||
698*1b191cb5SApple OSS Distributions 				    !phaseData->setObject(kCriticalityOffsetKey, criticalityOffset)) {
699*1b191cb5SApple OSS Distributions 					ret = kIOReturnError;
700*1b191cb5SApple OSS Distributions 					return true;
701*1b191cb5SApple OSS Distributions 				}
702*1b191cb5SApple OSS Distributions 			}
703*1b191cb5SApple OSS Distributions 
704*1b191cb5SApple OSS Distributions 			OSSharedPtr<OSString> type = nullptr;
705*1b191cb5SApple OSS Distributions 			if (unparseWorkIntervalType(wc->wc_create_flags, type) != kIOReturnSuccess ||
706*1b191cb5SApple OSS Distributions 			    !phaseData->setObject(kWorkIntervalTypeKey, type)) {
707*1b191cb5SApple OSS Distributions 				ret = kIOReturnError;
708*1b191cb5SApple OSS Distributions 				return true;
709*1b191cb5SApple OSS Distributions 			}
710*1b191cb5SApple OSS Distributions 
711*1b191cb5SApple OSS Distributions 
712*1b191cb5SApple OSS Distributions 			OSSharedPtr<OSArray> flags = OSArray::withCapacity(2);
713*1b191cb5SApple OSS Distributions 			if (flags == nullptr) {
714*1b191cb5SApple OSS Distributions 				ret = kIOReturnError;
715*1b191cb5SApple OSS Distributions 				return true;
716*1b191cb5SApple OSS Distributions 			}
717*1b191cb5SApple OSS Distributions 			if ((wc->wc_flags & WORK_INTERVAL_WORKLOAD_ID_COMPLEXITY_ALLOWED) != 0) {
718*1b191cb5SApple OSS Distributions 				OSSharedPtr<OSString> WIComplexityAllowedStr =
719*1b191cb5SApple OSS Distributions 				    OSString::withCString(kWIComplexityAllowedValue);
720*1b191cb5SApple OSS Distributions 				if (WIComplexityAllowedStr == nullptr || !flags->setObject(WIComplexityAllowedStr)) {
721*1b191cb5SApple OSS Distributions 					ret = kIOReturnError;
722*1b191cb5SApple OSS Distributions 					return true;
723*1b191cb5SApple OSS Distributions 				}
724*1b191cb5SApple OSS Distributions 			}
725*1b191cb5SApple OSS Distributions 			if (flags->getCount() && !phaseData->setObject(kFlagsKey, flags)) {
726*1b191cb5SApple OSS Distributions 				ret = kIOReturnError;
727*1b191cb5SApple OSS Distributions 				return true;
728*1b191cb5SApple OSS Distributions 			}
729*1b191cb5SApple OSS Distributions 
730*1b191cb5SApple OSS Distributions 			if (!phase->setObject(phase_str, phaseData)) {
731*1b191cb5SApple OSS Distributions 				ret = kIOReturnError;
732*1b191cb5SApple OSS Distributions 				return true;
733*1b191cb5SApple OSS Distributions 			}
734*1b191cb5SApple OSS Distributions 
735*1b191cb5SApple OSS Distributions 			if (is_default) {
736*1b191cb5SApple OSS Distributions 				OSSharedPtr<OSDictionary> root = OSDictionary::withCapacity(1);
737*1b191cb5SApple OSS Distributions 				OSSharedPtr<OSString> phaseStr = OSString::withCString(phase_str);
738*1b191cb5SApple OSS Distributions 
739*1b191cb5SApple OSS Distributions 				if (root == nullptr || phaseStr == nullptr ||
740*1b191cb5SApple OSS Distributions 				    !root->setObject(kDefaultPhaseKey, phaseStr)) {
741*1b191cb5SApple OSS Distributions 					ret = kIOReturnError;
742*1b191cb5SApple OSS Distributions 					return true;
743*1b191cb5SApple OSS Distributions 				}
744*1b191cb5SApple OSS Distributions 
745*1b191cb5SApple OSS Distributions 				if (!idDict->setObject(kRootKey, root)) {
746*1b191cb5SApple OSS Distributions 					ret = kIOReturnError;
747*1b191cb5SApple OSS Distributions 					return true;
748*1b191cb5SApple OSS Distributions 				}
749*1b191cb5SApple OSS Distributions 			}
750*1b191cb5SApple OSS Distributions 
751*1b191cb5SApple OSS Distributions 			return false;
752*1b191cb5SApple OSS Distributions 
753*1b191cb5SApple OSS Distributions 		});
754*1b191cb5SApple OSS Distributions 
755*1b191cb5SApple OSS Distributions 		if (ret != kIOReturnSuccess) {
756*1b191cb5SApple OSS Distributions 			return true;
757*1b191cb5SApple OSS Distributions 		}
758*1b191cb5SApple OSS Distributions 
759*1b191cb5SApple OSS Distributions 		if (!idDict->setObject(kPhasesKey, phase)) {
760*1b191cb5SApple OSS Distributions 			ret = kIOReturnError;
761*1b191cb5SApple OSS Distributions 			return true;
762*1b191cb5SApple OSS Distributions 		}
763*1b191cb5SApple OSS Distributions 
764*1b191cb5SApple OSS Distributions 		if (!idTable->setObject(id_str, idDict)) {
765*1b191cb5SApple OSS Distributions 			ret = kIOReturnError;
766*1b191cb5SApple OSS Distributions 			return true;
767*1b191cb5SApple OSS Distributions 		}
768*1b191cb5SApple OSS Distributions 
769*1b191cb5SApple OSS Distributions 		return false;
770*1b191cb5SApple OSS Distributions 	});
771*1b191cb5SApple OSS Distributions 	/* END IGNORE CODESTYLE */
772*1b191cb5SApple OSS Distributions 
773*1b191cb5SApple OSS Distributions 	if (ret != kIOReturnSuccess) {
774*1b191cb5SApple OSS Distributions 		return ret;
775*1b191cb5SApple OSS Distributions 	}
776*1b191cb5SApple OSS Distributions 
777*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSDictionary> plist = OSDictionary::withCapacity(1);
778*1b191cb5SApple OSS Distributions 	if (plist == nullptr) {
779*1b191cb5SApple OSS Distributions 		return kIOReturnError;
780*1b191cb5SApple OSS Distributions 	}
781*1b191cb5SApple OSS Distributions 
782*1b191cb5SApple OSS Distributions 	if (idTable->getCount() > 0 &&
783*1b191cb5SApple OSS Distributions 	    !plist->setObject(kWorkloadIDTableKey, idTable)) {
784*1b191cb5SApple OSS Distributions 		return kIOReturnError;
785*1b191cb5SApple OSS Distributions 	}
786*1b191cb5SApple OSS Distributions 
787*1b191cb5SApple OSS Distributions 	if (unparseWorkloadIDConfigurationFlags(plist) != kIOReturnSuccess) {
788*1b191cb5SApple OSS Distributions 		return kIOReturnError;
789*1b191cb5SApple OSS Distributions 	}
790*1b191cb5SApple OSS Distributions 
791*1b191cb5SApple OSS Distributions 	if (!plist->serialize(serialize.get())) {
792*1b191cb5SApple OSS Distributions 		return kIOReturnError;
793*1b191cb5SApple OSS Distributions 	}
794*1b191cb5SApple OSS Distributions 
795*1b191cb5SApple OSS Distributions 	if (buffer != nullptr) {
796*1b191cb5SApple OSS Distributions 		(void) strlcpy(buffer, serialize->text(), *size);
797*1b191cb5SApple OSS Distributions 	}
798*1b191cb5SApple OSS Distributions 	*size = serialize->getLength();
799*1b191cb5SApple OSS Distributions 
800*1b191cb5SApple OSS Distributions 	return kIOReturnSuccess;
801*1b191cb5SApple OSS Distributions }
802