xref: /xnu-11215/iokit/Kernel/IOStartIOKit.cpp (revision 8d741a5d)
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
IOKitInitializeTime(void)73 IOKitInitializeTime( void )
74 {
75 	mach_timespec_t         t;
76 
77 	t.tv_sec = 30;
78 	t.tv_nsec = 0;
79 
80 	IOService::waitForService(
81 		IOService::resourceMatching("IORTC"), &t );
82 #if defined(__i386__) || defined(__x86_64__)
83 	IOService::waitForService(
84 		IOService::resourceMatching("IONVRAM"), &t );
85 #endif
86 
87 	clock_initialize_calendar();
88 }
89 
90 void
iokit_post_constructor_init(void)91 iokit_post_constructor_init(void)
92 {
93 	IORegistryEntry *           root;
94 	OSObject *                  obj;
95 
96 	IOCPUInitialize();
97 	IOPlatformActionsInitialize();
98 	root = IORegistryEntry::initialize();
99 	assert( root );
100 	IOService::initialize();
101 	IOCatalogue::initialize();
102 	IOStatistics::initialize();
103 	OSKext::initialize();
104 	IOUserClient::initialize();
105 	IOMemoryDescriptor::initialize();
106 	IORootParent::initialize();
107 	IOReporter::initialize();
108 
109 	// Initializes IOPMinformeeList class-wide shared lock
110 	IOPMinformeeList::getSharedRecursiveLock();
111 
112 	obj = OSString::withCString( version );
113 	assert( obj );
114 	if (obj) {
115 		root->setProperty( kIOKitBuildVersionKey, obj );
116 		obj->release();
117 	}
118 	obj = IOKitDiagnostics::diagnostics();
119 	if (obj) {
120 		root->setProperty( kIOKitDiagnosticsKey, obj );
121 		obj->release();
122 	}
123 }
124 
125 /*****
126  * Pointer into bootstrap KLD segment for functions never used past startup.
127  */
128 void (*record_startup_extensions_function)(void) = NULL;
129 
130 void
InitIOKit(void * dtTop)131 InitIOKit(void *dtTop)
132 {
133 	// Compat for boot-args
134 	gIOKitTrace |= (gIOKitDebug & kIOTraceCompatBootArgs);
135 
136 	//
137 	// Have to start IOKit environment before we attempt to start
138 	// the C++ runtime environment.  At some stage we have to clean up
139 	// the initialisation path so that OS C++ can initialise independantly
140 	// of iokit basic service initialisation, or better we have IOLib stuff
141 	// initialise as basic OS services.
142 	//
143 	IOLibInit();
144 	OSlibkernInit();
145 	IOMachPortInitialize();
146 
147 	gIOProgressBackbufferKey  = OSSymbol::withCStringNoCopy(kIOProgressBackbufferKey);
148 	gIORemoveOnReadProperties = OSSet::withObjects((const OSObject **) &gIOProgressBackbufferKey, 1);
149 
150 	interruptAccountingInit();
151 
152 	gRootNub = new IOPlatformExpertDevice;
153 	if (__improbable(gRootNub == NULL)) {
154 		panic("Failed to allocate IOKit root nub");
155 	}
156 	bool ok = gRootNub->init(dtTop);
157 	if (__improbable(!ok)) {
158 		panic("Failed to initialize IOKit root nub");
159 	}
160 	gRootNub->attach(NULL);
161 
162 	/* If the bootstrap segment set up a function to record startup
163 	 * extensions, call it now.
164 	 */
165 	if (record_startup_extensions_function) {
166 		record_startup_extensions_function();
167 	}
168 }
169 
170 void
ConfigureIOKit(void)171 ConfigureIOKit(void)
172 {
173 	assert(gRootNub != NULL);
174 	gRootNub->configureDefaults();
175 }
176 
177 void
StartIOKitMatching(void)178 StartIOKitMatching(void)
179 {
180 	SOCD_TRACE_XNU(START_IOKIT);
181 	assert(gRootNub != NULL);
182 	bool ok = gRootNub->startIOServiceMatching();
183 	if (__improbable(!ok)) {
184 		panic("Failed to start IOService matching");
185 	}
186 
187 #if !NO_KEXTD
188 	if (OSKext::iokitDaemonAvailable()) {
189 		/* Add a busy count to keep the registry busy until the IOKit daemon has
190 		 * completely finished launching. This is decremented when the IOKit daemon
191 		 * messages the kernel after the in-kernel linker has been
192 		 * removed and personalities have been sent.
193 		 */
194 		IOService::getServiceRoot()->adjustBusy(1);
195 	}
196 #endif
197 }
198 
199 void
IORegistrySetOSBuildVersion(char * build_version)200 IORegistrySetOSBuildVersion(char * build_version)
201 {
202 	IORegistryEntry * root = IORegistryEntry::getRegistryRoot();
203 
204 	if (root) {
205 		if (build_version) {
206 			root->setProperty(kOSBuildVersionKey, build_version);
207 		} else {
208 			root->removeProperty(kOSBuildVersionKey);
209 		}
210 	}
211 
212 	return;
213 }
214 
215 void
IORecordProgressBackbuffer(void * buffer,size_t size,uint32_t theme)216 IORecordProgressBackbuffer(void * buffer, size_t size, uint32_t theme)
217 {
218 	IORegistryEntry * chosen;
219 
220 	if (((unsigned int) size) != size) {
221 		return;
222 	}
223 	if ((chosen = IORegistryEntry::fromPath(kIODeviceTreePlane ":/chosen"))) {
224 		chosen->setProperty(kIOProgressBackbufferKey, buffer, (unsigned int) size);
225 		chosen->setProperty(kIOProgressColorThemeKey, theme, 32);
226 
227 		chosen->release();
228 	}
229 }
230 }; /* extern "C" */
231