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