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