xref: /xnu-11215/iokit/Kernel/IOStartIOKit.cpp (revision a3bb9fcc)
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>
3114e3d835SApple OSS Distributions #include <libkern/version.h>
32c1dac77fSApple OSS Distributions #include <IOKit/IORegistryEntry.h>
33c1dac77fSApple OSS Distributions #include <IOKit/IODeviceTreeSupport.h>
34c1dac77fSApple OSS Distributions #include <IOKit/IOCatalogue.h>
35c1dac77fSApple OSS Distributions #include <IOKit/IOUserClient.h>
36c1dac77fSApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
37c1dac77fSApple OSS Distributions #include <IOKit/IOPlatformExpert.h>
38c1dac77fSApple OSS Distributions #include <IOKit/IOLib.h>
39c1dac77fSApple OSS Distributions #include <IOKit/IOKitKeys.h>
40c1dac77fSApple OSS Distributions #include <IOKit/IOKitDebug.h>
41855239e5SApple OSS Distributions #include <IOKit/pwr_mgt/RootDomain.h>
42e13b1fa5SApple OSS Distributions #include <IOKit/pwr_mgt/IOPMinformeeList.h>
43855239e5SApple OSS Distributions #include <IOKit/IOStatisticsPrivate.h>
44855239e5SApple OSS Distributions #include <IOKit/IOKitKeysPrivate.h>
45*a3bb9fccSApple OSS Distributions #include <IOKit/IOInterruptAccountingPrivate.h>
46c1dac77fSApple OSS Distributions 
47c1dac77fSApple OSS Distributions #include <IOKit/assert.h>
48c1dac77fSApple OSS Distributions 
4914e3d835SApple OSS Distributions #include "IOKitKernelInternal.h"
5014e3d835SApple OSS Distributions 
51*a3bb9fccSApple OSS Distributions const OSSymbol * gIOProgressBackbufferKey;
52*a3bb9fccSApple OSS Distributions OSSet *          gIORemoveOnReadProperties;
53*a3bb9fccSApple OSS Distributions 
54c1dac77fSApple OSS Distributions extern "C" {
55c1dac77fSApple OSS Distributions 
56c1dac77fSApple OSS Distributions extern void OSlibkernInit (void);
573ca3bd55SApple OSS Distributions 
58186b8fceSApple OSS Distributions void iokit_post_constructor_init(void);
59c1dac77fSApple OSS Distributions 
60c1dac77fSApple OSS Distributions #include <kern/clock.h>
61e13b1fa5SApple OSS Distributions #include <sys/time.h>
62c1dac77fSApple OSS Distributions 
63e13b1fa5SApple OSS Distributions void IOKitInitializeTime( void )
64c1dac77fSApple OSS Distributions {
65c1dac77fSApple OSS Distributions 	mach_timespec_t		t;
66c1dac77fSApple OSS Distributions 
67c1dac77fSApple OSS Distributions 	t.tv_sec = 30;
68c1dac77fSApple OSS Distributions 	t.tv_nsec = 0;
69c1dac77fSApple OSS Distributions 	IOService::waitForService(
70c1dac77fSApple OSS Distributions 		IOService::resourceMatching("IORTC"), &t );
71855239e5SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
72c1dac77fSApple OSS Distributions 	IOService::waitForService(
73c1dac77fSApple OSS Distributions 		IOService::resourceMatching("IONVRAM"), &t );
74c1dac77fSApple OSS Distributions #endif
75c1dac77fSApple OSS Distributions 
76c1dac77fSApple OSS Distributions     clock_initialize_calendar();
77c1dac77fSApple OSS Distributions }
78c1dac77fSApple OSS Distributions 
79e13b1fa5SApple OSS Distributions void IOKitResetTime( void )
80e13b1fa5SApple OSS Distributions {
813ca3bd55SApple OSS Distributions     clock_sec_t		secs;
823ca3bd55SApple OSS Distributions 	clock_usec_t	microsecs;
83e13b1fa5SApple OSS Distributions 
84e13b1fa5SApple OSS Distributions     clock_initialize_calendar();
85e13b1fa5SApple OSS Distributions 
86e13b1fa5SApple OSS Distributions     clock_get_calendar_microtime(&secs, &microsecs);
87e13b1fa5SApple OSS Distributions     gIOLastWakeTime.tv_sec  = secs;
88e13b1fa5SApple OSS Distributions     gIOLastWakeTime.tv_usec = microsecs;
89855239e5SApple OSS Distributions 
90855239e5SApple OSS Distributions     IOService::updateConsoleUsers(NULL, kIOMessageSystemHasPoweredOn);
91e13b1fa5SApple OSS Distributions }
92e13b1fa5SApple OSS Distributions 
933ca3bd55SApple OSS Distributions void iokit_post_constructor_init(void)
94c1dac77fSApple OSS Distributions {
95c1dac77fSApple OSS Distributions     IORegistryEntry *		root;
96c1dac77fSApple OSS Distributions     OSObject *			obj;
97c1dac77fSApple OSS Distributions 
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();
107c1dac77fSApple OSS Distributions 
108e13b1fa5SApple OSS Distributions     // Initializes IOPMinformeeList class-wide shared lock
109e13b1fa5SApple OSS Distributions     IOPMinformeeList::getSharedRecursiveLock();
110e13b1fa5SApple OSS Distributions 
11114e3d835SApple OSS Distributions     obj = OSString::withCString( version );
112c1dac77fSApple OSS Distributions     assert( obj );
113c1dac77fSApple OSS Distributions     if( obj ) {
114c1dac77fSApple OSS Distributions         root->setProperty( kIOKitBuildVersionKey, obj );
115c1dac77fSApple OSS Distributions 	obj->release();
116c1dac77fSApple OSS Distributions     }
117c1dac77fSApple OSS Distributions     obj = IOKitDiagnostics::diagnostics();
118c1dac77fSApple OSS Distributions     if( obj ) {
119c1dac77fSApple OSS Distributions         root->setProperty( kIOKitDiagnosticsKey, obj );
120c1dac77fSApple OSS Distributions 	obj->release();
121c1dac77fSApple OSS Distributions     }
1223ca3bd55SApple OSS Distributions }
1233ca3bd55SApple OSS Distributions 
1243ca3bd55SApple OSS Distributions // From <osfmk/kern/debug.c>
1253ca3bd55SApple OSS Distributions extern int debug_mode;
1263ca3bd55SApple OSS Distributions 
1273ca3bd55SApple OSS Distributions /*****
1283ca3bd55SApple OSS Distributions  * Pointer into bootstrap KLD segment for functions never used past startup.
1293ca3bd55SApple OSS Distributions  */
1303ca3bd55SApple OSS Distributions void (*record_startup_extensions_function)(void) = 0;
1313ca3bd55SApple OSS Distributions 
1323ca3bd55SApple OSS Distributions void StartIOKit( void * p1, void * p2, void * p3, void * p4 )
1333ca3bd55SApple OSS Distributions {
1343ca3bd55SApple OSS Distributions     IOPlatformExpertDevice *	rootNub;
1353ca3bd55SApple OSS Distributions     int				debugFlags;
1363ca3bd55SApple OSS Distributions 
1373ca3bd55SApple OSS Distributions     if( PE_parse_boot_argn( "io", &debugFlags, sizeof (debugFlags) ))
1383ca3bd55SApple OSS Distributions 		gIOKitDebug = debugFlags;
1393ca3bd55SApple OSS Distributions 
140855239e5SApple OSS Distributions     if( PE_parse_boot_argn( "iotrace", &debugFlags, sizeof (debugFlags) ))
141855239e5SApple OSS Distributions 		gIOKitTrace = debugFlags;
142855239e5SApple OSS Distributions 
143855239e5SApple OSS Distributions 	// Compat for boot-args
144855239e5SApple OSS Distributions 	gIOKitTrace |= (gIOKitDebug & kIOTraceCompatBootArgs);
145855239e5SApple OSS Distributions 
1463ca3bd55SApple OSS Distributions     // Check for the log synchronous bit set in io
1473ca3bd55SApple OSS Distributions     if (gIOKitDebug & kIOLogSynchronous)
1483ca3bd55SApple OSS Distributions         debug_mode = true;
1493ca3bd55SApple OSS Distributions 
1503ca3bd55SApple OSS Distributions     //
1513ca3bd55SApple OSS Distributions     // Have to start IOKit environment before we attempt to start
1523ca3bd55SApple OSS Distributions     // the C++ runtime environment.  At some stage we have to clean up
1533ca3bd55SApple OSS Distributions     // the initialisation path so that OS C++ can initialise independantly
1543ca3bd55SApple OSS Distributions     // of iokit basic service initialisation, or better we have IOLib stuff
1553ca3bd55SApple OSS Distributions     // initialise as basic OS services.
1563ca3bd55SApple OSS Distributions     //
1573ca3bd55SApple OSS Distributions     IOLibInit();
1583ca3bd55SApple OSS Distributions     OSlibkernInit();
1593ca3bd55SApple OSS Distributions 
160*a3bb9fccSApple OSS Distributions     gIOProgressBackbufferKey  = OSSymbol::withCStringNoCopy(kIOProgressBackbufferKey);
161*a3bb9fccSApple OSS Distributions     gIORemoveOnReadProperties = OSSet::withObjects((const OSObject **) &gIOProgressBackbufferKey, 1);
162*a3bb9fccSApple OSS Distributions 
163*a3bb9fccSApple OSS Distributions     interruptAccountingInit();
164*a3bb9fccSApple OSS Distributions 
165c1dac77fSApple OSS Distributions     rootNub = new IOPlatformExpertDevice;
166c1dac77fSApple OSS Distributions 
167c1dac77fSApple OSS Distributions     if( rootNub && rootNub->initWithArgs( p1, p2, p3, p4)) {
168c1dac77fSApple OSS Distributions         rootNub->attach( 0 );
169c1dac77fSApple OSS Distributions 
1703ca3bd55SApple OSS Distributions        /* If the bootstrap segment set up a function to record startup
1713ca3bd55SApple OSS Distributions         * extensions, call it now.
172c1dac77fSApple OSS Distributions         */
1733ca3bd55SApple OSS Distributions         if (record_startup_extensions_function) {
1743ca3bd55SApple OSS Distributions             record_startup_extensions_function();
1753ca3bd55SApple OSS Distributions         }
176c1dac77fSApple OSS Distributions 
177c1dac77fSApple OSS Distributions         rootNub->registerService();
178e13b1fa5SApple OSS Distributions 
179e13b1fa5SApple OSS Distributions #if !NO_KEXTD
180e13b1fa5SApple OSS Distributions        /* Add a busy count to keep the registry busy until kextd has
181e13b1fa5SApple OSS Distributions         * completely finished launching. This is decremented when kextd
182e13b1fa5SApple OSS Distributions         * messages the kernel after the in-kernel linker has been
183e13b1fa5SApple OSS Distributions         * removed and personalities have been sent.
184e13b1fa5SApple OSS Distributions         */
185e13b1fa5SApple OSS Distributions         IOService::getServiceRoot()->adjustBusy(1);
186e13b1fa5SApple OSS Distributions #endif
187c1dac77fSApple OSS Distributions     }
188c1dac77fSApple OSS Distributions }
189c1dac77fSApple OSS Distributions 
190e13b1fa5SApple OSS Distributions void
191e13b1fa5SApple OSS Distributions IORegistrySetOSBuildVersion(char * build_version)
192e13b1fa5SApple OSS Distributions {
193e13b1fa5SApple OSS Distributions     IORegistryEntry * root = IORegistryEntry::getRegistryRoot();
194e13b1fa5SApple OSS Distributions 
195e13b1fa5SApple OSS Distributions     if (root) {
196e13b1fa5SApple OSS Distributions         if (build_version) {
197e13b1fa5SApple OSS Distributions             root->setProperty(kOSBuildVersionKey, build_version);
198e13b1fa5SApple OSS Distributions         } else {
199e13b1fa5SApple OSS Distributions             root->removeProperty(kOSBuildVersionKey);
200e13b1fa5SApple OSS Distributions         }
201e13b1fa5SApple OSS Distributions     }
202e13b1fa5SApple OSS Distributions 
203e13b1fa5SApple OSS Distributions     return;
204e13b1fa5SApple OSS Distributions }
205e13b1fa5SApple OSS Distributions 
206*a3bb9fccSApple OSS Distributions void
207*a3bb9fccSApple OSS Distributions IORecordProgressBackbuffer(void * buffer, size_t size, uint32_t theme)
208*a3bb9fccSApple OSS Distributions {
209*a3bb9fccSApple OSS Distributions     IORegistryEntry * chosen;
210*a3bb9fccSApple OSS Distributions     if ((chosen = IORegistryEntry::fromPath(kIODeviceTreePlane ":/chosen")))
211*a3bb9fccSApple OSS Distributions     {
212*a3bb9fccSApple OSS Distributions         chosen->setProperty(kIOProgressBackbufferKey, buffer, size);
213*a3bb9fccSApple OSS Distributions 	chosen->setProperty(kIOProgressColorThemeKey, theme, 32);
214*a3bb9fccSApple OSS Distributions 
215*a3bb9fccSApple OSS Distributions         chosen->release();
216*a3bb9fccSApple OSS Distributions     }
217*a3bb9fccSApple OSS Distributions }
218*a3bb9fccSApple OSS Distributions 
219c1dac77fSApple OSS Distributions }; /* extern "C" */
220