xref: /xnu-11215/iokit/Kernel/IOStartIOKit.cpp (revision bb611c8f)
1 /*
2  * Copyright (c) 1998-2010 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <libkern/c++/OSUnserialize.h>
30 #include <libkern/c++/OSKext.h>
31 #include <libkern/section_keywords.h>
32 #include <libkern/version.h>
33 #include <IOKit/IORegistryEntry.h>
34 #include <IOKit/IODeviceTreeSupport.h>
35 #include <IOKit/IOCatalogue.h>
36 #include <IOKit/IOUserClient.h>
37 #include <IOKit/IOMemoryDescriptor.h>
38 #include <IOKit/IOPlatformExpert.h>
39 #include <IOKit/IOKernelReporters.h>
40 #include <IOKit/IOLib.h>
41 #include <IOKit/IOKitKeys.h>
42 #include <IOKit/IOKitDebug.h>
43 #include <IOKit/pwr_mgt/RootDomain.h>
44 #include <IOKit/pwr_mgt/IOPMinformeeList.h>
45 #include <IOKit/IOStatisticsPrivate.h>
46 #include <IOKit/IOKitKeysPrivate.h>
47 #include <IOKit/IOInterruptAccountingPrivate.h>
48 #include <IOKit/assert.h>
49 #include <sys/conf.h>
50 
51 #include "IOKitKernelInternal.h"
52 
53 const OSSymbol * gIOProgressBackbufferKey;
54 OSSet *          gIORemoveOnReadProperties;
55 
56 extern "C" {
57 void InitIOKit(void *dtTop);
58 void ConfigureIOKit(void);
59 void StartIOKitMatching(void);
60 void IORegistrySetOSBuildVersion(char * build_version);
61 void IORecordProgressBackbuffer(void * buffer, size_t size, uint32_t theme);
62 
63 extern void OSlibkernInit(void);
64 
65 void iokit_post_constructor_init(void);
66 
67 SECURITY_READ_ONLY_LATE(static IOPlatformExpertDevice*) gRootNub;
68 
69 #include <kern/clock.h>
70 #include <sys/time.h>
71 
72 void
73 IOKitInitializeTime( void )
74 {
75 	mach_timespec_t         t;
76 
77 	t.tv_sec = 30;
78 	t.tv_nsec = 0;
79 
80 // RTC is not present on this target
81 #ifndef BCM2837
82 	IOService::waitForService(
83 		IOService::resourceMatching("IORTC"), &t );
84 #endif
85 #if defined(__i386__) || defined(__x86_64__)
86 	IOService::waitForService(
87 		IOService::resourceMatching("IONVRAM"), &t );
88 #endif
89 
90 	clock_initialize_calendar();
91 }
92 
93 void
94 iokit_post_constructor_init(void)
95 {
96 	IORegistryEntry *           root;
97 	OSObject *                  obj;
98 
99 	IOCPUInitialize();
100 	IOPlatformActionsInitialize();
101 	root = IORegistryEntry::initialize();
102 	assert( root );
103 	IOService::initialize();
104 	IOCatalogue::initialize();
105 	IOStatistics::initialize();
106 	OSKext::initialize();
107 	IOUserClient::initialize();
108 	IOMemoryDescriptor::initialize();
109 	IORootParent::initialize();
110 	IOReporter::initialize();
111 
112 	// Initializes IOPMinformeeList class-wide shared lock
113 	IOPMinformeeList::getSharedRecursiveLock();
114 
115 	obj = OSString::withCString( version );
116 	assert( obj );
117 	if (obj) {
118 		root->setProperty( kIOKitBuildVersionKey, obj );
119 		obj->release();
120 	}
121 	obj = IOKitDiagnostics::diagnostics();
122 	if (obj) {
123 		root->setProperty( kIOKitDiagnosticsKey, obj );
124 		obj->release();
125 	}
126 }
127 
128 /*****
129  * Pointer into bootstrap KLD segment for functions never used past startup.
130  */
131 void (*record_startup_extensions_function)(void) = NULL;
132 
133 void
134 InitIOKit(void *dtTop)
135 {
136 	int                         debugFlags = 0;
137 
138 	if (PE_parse_boot_argn( "io", &debugFlags, sizeof(debugFlags))) {
139 		gIOKitDebug = debugFlags;
140 	}
141 	// Enable IOWaitQuiet panics on arm64 macOS except on KASAN.
142 	// existing 3rd party KEXTs may hold the registry busy on x86 RELEASE kernels.
143 	// Enabling this on other platforms is tracked in rdar://66364108
144 #if XNU_TARGET_OS_OSX && defined(__arm64__) && !KASAN
145 	else {
146 		gIOKitDebug |= kIOWaitQuietPanics;
147 	}
148 #endif
149 
150 	if (PE_parse_boot_argn( "iotrace", &debugFlags, sizeof(debugFlags))) {
151 		gIOKitTrace = debugFlags;
152 	}
153 
154 	// Compat for boot-args
155 	gIOKitTrace |= (gIOKitDebug & kIOTraceCompatBootArgs);
156 
157 	if (PE_parse_boot_argn( "pmtimeout", &debugFlags, sizeof(debugFlags))) {
158 		gCanSleepTimeout = debugFlags;
159 	}
160 
161 	if (PE_parse_boot_argn( "dk", &debugFlags, sizeof(debugFlags))) {
162 		gIODKDebug = debugFlags;
163 	}
164 
165 
166 	//
167 	// Have to start IOKit environment before we attempt to start
168 	// the C++ runtime environment.  At some stage we have to clean up
169 	// the initialisation path so that OS C++ can initialise independantly
170 	// of iokit basic service initialisation, or better we have IOLib stuff
171 	// initialise as basic OS services.
172 	//
173 	IOLibInit();
174 	OSlibkernInit();
175 	IOMachPortInitialize();
176 	devsw_init();
177 
178 	gIOProgressBackbufferKey  = OSSymbol::withCStringNoCopy(kIOProgressBackbufferKey);
179 	gIORemoveOnReadProperties = OSSet::withObjects((const OSObject **) &gIOProgressBackbufferKey, 1);
180 
181 	interruptAccountingInit();
182 
183 	gRootNub = new IOPlatformExpertDevice;
184 	if (__improbable(gRootNub == NULL)) {
185 		panic("Failed to allocate IOKit root nub");
186 	}
187 	bool ok = gRootNub->init(dtTop);
188 	if (__improbable(!ok)) {
189 		panic("Failed to initialize IOKit root nub");
190 	}
191 	gRootNub->attach(NULL);
192 
193 	/* If the bootstrap segment set up a function to record startup
194 	 * extensions, call it now.
195 	 */
196 	if (record_startup_extensions_function) {
197 		record_startup_extensions_function();
198 	}
199 }
200 
201 void
202 ConfigureIOKit(void)
203 {
204 	assert(gRootNub != NULL);
205 	gRootNub->configureDefaults();
206 }
207 
208 void
209 StartIOKitMatching(void)
210 {
211 	assert(gRootNub != NULL);
212 	bool ok = gRootNub->startIOServiceMatching();
213 	if (__improbable(!ok)) {
214 		panic("Failed to start IOService matching");
215 	}
216 
217 #if !NO_KEXTD
218 	/* Add a busy count to keep the registry busy until kextd has
219 	 * completely finished launching. This is decremented when kextd
220 	 * messages the kernel after the in-kernel linker has been
221 	 * removed and personalities have been sent.
222 	 */
223 	IOService::getServiceRoot()->adjustBusy(1);
224 #endif
225 }
226 
227 void
228 IORegistrySetOSBuildVersion(char * build_version)
229 {
230 	IORegistryEntry * root = IORegistryEntry::getRegistryRoot();
231 
232 	if (root) {
233 		if (build_version) {
234 			root->setProperty(kOSBuildVersionKey, build_version);
235 		} else {
236 			root->removeProperty(kOSBuildVersionKey);
237 		}
238 	}
239 
240 	return;
241 }
242 
243 void
244 IORecordProgressBackbuffer(void * buffer, size_t size, uint32_t theme)
245 {
246 	IORegistryEntry * chosen;
247 
248 	if (((unsigned int) size) != size) {
249 		return;
250 	}
251 	if ((chosen = IORegistryEntry::fromPath(kIODeviceTreePlane ":/chosen"))) {
252 		chosen->setProperty(kIOProgressBackbufferKey, buffer, (unsigned int) size);
253 		chosen->setProperty(kIOProgressColorThemeKey, theme, 32);
254 
255 		chosen->release();
256 	}
257 }
258 }; /* extern "C" */
259