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