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