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