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