2 * Copyright (c) 1998-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <IOKit/IOBSD.h>
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOService.h>
31 #include <IOKit/IOCatalogue.h>
32 #include <IOKit/IODeviceTreeSupport.h>
33 #include <IOKit/IOKitKeys.h>
34 #include <IOKit/IOPlatformExpert.h>
35 #include <IOKit/IOUserClient.h>
39 #include <pexpert/pexpert.h>
40 #include <kern/clock.h>
41 #include <mach/machine.h>
42 #include <uuid/uuid.h>
43 #include <sys/vnode_internal.h>
44 #include <sys/mount.h>
46 // how long to wait for matching root device, secs
48 #define ROOTDEVICETIMEOUT 120
50 #define ROOTDEVICETIMEOUT 60
53 int panic_on_exception_triage
= 0;
55 extern dev_t
mdevadd(int devid
, uint64_t base
, unsigned int size
, int phys
);
56 extern dev_t
mdevlookup(int devid
);
57 extern void mdevremoveall(void);
58 extern int mdevgetrange(int devid
, uint64_t *base
, uint64_t *size
);
59 extern void di_root_ramfile(IORegistryEntry
* entry
);
62 #define IOPOLLED_COREFILE (CONFIG_KDP_INTERACTIVE_DEBUGGING)
64 #if defined(XNU_TARGET_OS_BRIDGE)
66 #define kIOCoreDumpSize 150ULL*1024ULL*1024ULL
67 // leave free space on volume:
68 #define kIOCoreDumpFreeSize 150ULL*1024ULL*1024ULL
69 #define kIOCoreDumpPath "/private/var/internal/kernelcore"
71 #else /* defined(XNU_TARGET_OS_BRIDGE) */
72 #define kIOCoreDumpMinSize 350ULL*1024ULL*1024ULL
73 #define kIOCoreDumpLargeSize 500ULL*1024ULL*1024ULL
74 // leave free space on volume:
75 #define kIOCoreDumpFreeSize 350ULL*1024ULL*1024ULL
76 #define kIOCoreDumpPath "/private/var/vm/kernelcore"
78 #endif /* defined(XNU_TARGET_OS_BRIDGE) */
80 #elif DEVELOPMENT /* CONFIG_EMBEDDED */
81 #define IOPOLLED_COREFILE 1
83 #define kIOCoreDumpSize 0ULL
84 #define kIOCoreDumpFreeSize 0ULL
85 #else /* CONFIG_EMBEDDED */
86 #define IOPOLLED_COREFILE 0
87 #endif /* CONFIG_EMBEDDED */
92 NewKernelCoreMedia(void * target
, void * refCon
,
93 IOService
* newService
,
94 IONotifier
* notifier
);
95 #endif /* IOPOLLED_COREFILE */
97 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
99 * Touched by IOFindBSDRoot() if a RAMDisk is used for the root device.
101 extern uint64_t kdp_core_ramdisk_addr
;
102 extern uint64_t kdp_core_ramdisk_size
;
108 IOService::publishResource("IOBSD");
110 return( kIOReturnSuccess
);
114 IOServicePublishResource( const char * property
, boolean_t value
)
117 IOService::publishResource( property
, kOSBooleanTrue
);
119 IOService::getResourceService()->removeProperty( property
);
123 IOServiceWaitForMatchingResource( const char * property
, uint64_t timeout
)
125 OSDictionary
* dict
= 0;
126 IOService
* match
= 0;
127 boolean_t found
= false;
131 dict
= IOService::resourceMatching( property
);
134 match
= IOService::waitForMatchingService( dict
, timeout
);
149 IOCatalogueMatchingDriversPresent( const char * property
)
151 OSDictionary
* dict
= 0;
152 OSOrderedSet
* set
= 0;
153 SInt32 generationCount
= 0;
154 boolean_t found
= false;
158 dict
= OSDictionary::withCapacity(1);
161 dict
->setObject( property
, kOSBooleanTrue
);
162 set
= gIOCatalogue
->findDrivers( dict
, &generationCount
);
163 if ( set
&& (set
->getCount() > 0))
176 OSDictionary
* IOBSDNameMatching( const char * name
)
179 const OSSymbol
* str
= 0;
183 dict
= IOService::serviceMatching( gIOServiceKey
);
186 str
= OSSymbol::withCString( name
);
189 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
204 OSDictionary
* IOUUIDMatching( void )
206 return IOService::resourceMatching( "boot-uuid-media" );
209 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
211 OSDictionary
* matching
;
212 OSDictionary
* propDict
= 0;
213 const OSSymbol
* str
= 0;
214 char networkType
[128];
217 matching
= IOService::serviceMatching( "IONetworkInterface" );
221 propDict
= OSDictionary::withCapacity(1);
225 str
= OSSymbol::withCString( prefix
);
229 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
233 // see if we're contrained to netroot off of specific network type
234 if(PE_parse_boot_argn( "network-type", networkType
, 128 ))
236 str
= OSSymbol::withCString( networkType
);
239 propDict
->setObject( "IONetworkRootType", str
);
245 if ( matching
->setObject( gIOPropertyMatchKey
,
246 (OSObject
*) propDict
) != true )
256 if ( matching
) matching
->release();
257 if ( propDict
) propDict
->release();
258 if ( str
) str
->release();
263 static bool IORegisterNetworkInterface( IOService
* netif
)
265 // A network interface is typically named and registered
266 // with BSD after receiving a request from a user space
267 // "namer". However, for cases when the system needs to
268 // root from the network, this registration task must be
269 // done inside the kernel and completed before the root
270 // device is handed to BSD.
275 OSDictionary
* dict
= 0;
278 enum { kMaxPathLen
= 512 };
281 stack
= IOService::waitForService(
282 IOService::serviceMatching("IONetworkStack") );
283 if ( stack
== 0 ) break;
285 dict
= OSDictionary::withCapacity(3);
286 if ( dict
== 0 ) break;
288 zero
= OSNumber::withNumber((UInt64
) 0, 32);
289 if ( zero
== 0 ) break;
291 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
292 if ( pathBuf
== 0 ) break;
295 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
298 path
= OSString::withCStringNoCopy( pathBuf
);
299 if ( path
== 0 ) break;
301 dict
->setObject( "IOInterfaceUnit", zero
);
302 dict
->setObject( kIOPathMatchKey
, path
);
304 stack
->setProperties( dict
);
308 if ( zero
) zero
->release();
309 if ( path
) path
->release();
310 if ( dict
) dict
->release();
311 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
313 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
316 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
318 OSDictionary
* matching
= NULL
;
325 len
= strlen( kIODeviceTreePlane
":" );
330 strlcpy( buf
, kIODeviceTreePlane
":", len
+ 1 );
333 len
= strlen( path
);
337 strlcpy( comp
, path
, len
+ 1 );
339 matching
= OSDictionary::withCapacity( 1 );
343 str
= OSString::withCString( buf
);
346 matching
->setObject( kIOPathMatchKey
, str
);
359 static int didRam
= 0;
360 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
362 kern_return_t
IOFindBSDRoot( char * rootName
, unsigned int rootNameSize
,
363 dev_t
* root
, u_int32_t
* oflags
)
367 IORegistryEntry
* regEntry
;
368 OSDictionary
* matching
= 0;
375 const char * mediaProperty
= 0;
378 const char * look
= 0;
380 bool debugInfoPrintedOnce
= false;
381 const char * uuidStr
= NULL
;
383 static int mountAttempts
= 0;
387 // stall here for anyone matching on the IOBSD resource to finish (filesystems)
388 matching
= IOService::serviceMatching(gIOResourcesKey
);
390 matching
->setObject(gIOResourceMatchedKey
, gIOBSDKey
);
392 if ((service
= IOService::waitForMatchingService(matching
, 30ULL * kSecondScale
))) {
402 IOLog("mount(%d) failed\n", mountAttempts
);
406 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
408 return( kIOReturnNoMemory
);
409 rdBootVar
= str
+ kMaxPathBuf
;
411 if (!PE_parse_boot_argn("rd", rdBootVar
, kMaxBootVar
)
412 && !PE_parse_boot_argn("rootdev", rdBootVar
, kMaxBootVar
))
416 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
417 di_root_ramfile(regEntry
);
418 data
= OSDynamicCast(OSData
, regEntry
->getProperty( "root-matching" ));
420 matching
= OSDynamicCast(OSDictionary
, OSUnserializeXML((char *)data
->getBytesNoCopy()));
426 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
428 uuidStr
= (const char*)data
->getBytesNoCopy();
429 OSString
*uuidString
= OSString::withCString( uuidStr
);
431 // match the boot-args boot-uuid processing below
433 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
434 IOService::publishResource( "boot-uuid", uuidString
);
435 uuidString
->release();
436 matching
= IOUUIDMatching();
437 mediaProperty
= "boot-uuid-media";
449 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
450 // It will become /dev/mdx, where x is 0-f.
453 if(!didRam
) { /* Have we already build this ram disk? */
454 didRam
= 1; /* Remember we did this */
455 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
456 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
457 if(data
) { /* We found one */
458 uintptr_t *ramdParms
;
459 ramdParms
= (uintptr_t *)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
460 (void)mdevadd(-1, ml_static_ptovirt(ramdParms
[0]) >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
462 regEntry
->release(); /* Toss the entry */
467 // Now check if we are trying to root on a memory device
470 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
471 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
472 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
474 xchar
= xchar
& ~' '; /* Fold to upper case */
475 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
476 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
477 dchar
= dchar
| ' '; /* Fold to lower case */
479 else xchar
= -1; /* Show bogus */
481 if(xchar
>= 0) { /* Do we have a valid memory device name? */
482 *root
= mdevlookup(xchar
); /* Find the device number */
483 if(*root
>= 0) { /* Did we find one? */
484 rootName
[0] = 'm'; /* Build root name */
485 rootName
[1] = 'd'; /* Build root name */
486 rootName
[2] = dchar
; /* Build root name */
487 rootName
[3] = 0; /* Build root name */
488 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
489 *oflags
= 0; /* Show that this is not network */
491 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
492 /* retrieve final ramdisk range and initialize KDP variables */
493 if (mdevgetrange(xchar
, &kdp_core_ramdisk_addr
, &kdp_core_ramdisk_size
) != 0) {
494 IOLog("Unable to retrieve range for root memory device %d\n", xchar
);
495 kdp_core_ramdisk_addr
= 0;
496 kdp_core_ramdisk_size
= 0;
500 goto iofrootx
; /* Join common exit... */
502 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
506 if( (!matching
) && rdBootVar
[0] ) {
512 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
513 matching
= IONetworkNamePrefixMatching( "en" );
514 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
516 OSString
*uuidString
;
518 uuid
= (char *)IOMalloc( kMaxBootVar
);
521 if (!PE_parse_boot_argn( "boot-uuid", uuid
, kMaxBootVar
)) {
522 panic( "rd=uuid but no boot-uuid=<value> specified" );
524 uuidString
= OSString::withCString( uuid
);
526 IOService::publishResource( "boot-uuid", uuidString
);
527 uuidString
->release();
528 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
529 matching
= IOUUIDMatching();
530 mediaProperty
= "boot-uuid-media";
532 IOFree( uuid
, kMaxBootVar
);
535 matching
= IOBSDNameMatching( look
);
541 // Match any HFS media
543 matching
= IOService::serviceMatching( "IOMedia" );
544 astring
= OSString::withCStringNoCopy("Apple_HFS");
546 matching
->setObject("Content", astring
);
551 if( gIOKitDebug
& kIOWaitQuietBeforeRoot
) {
552 IOLog( "Waiting for matching to complete\n" );
553 IOService::getPlatform()->waitQuiet();
556 if( true && matching
) {
557 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
559 if( matching
->serialize( s
)) {
560 IOLog( "Waiting on %s\n", s
->text() );
566 t
.tv_sec
= ROOTDEVICETIMEOUT
;
569 service
= IOService::waitForService( matching
, &t
);
570 if( (!service
) || (mountAttempts
== 10)) {
571 PE_display_icon( 0, "noroot");
572 IOLog( "Still waiting for root device\n" );
574 if( !debugInfoPrintedOnce
) {
575 debugInfoPrintedOnce
= true;
576 if( gIOKitDebug
& kIOLogDTree
) {
577 IOLog("\nDT plane:\n");
578 IOPrintPlane( gIODTPlane
);
580 if( gIOKitDebug
& kIOLogServiceTree
) {
581 IOLog("\nService plane:\n");
582 IOPrintPlane( gIOServicePlane
);
584 if( gIOKitDebug
& kIOLogMemory
)
591 if ( service
&& mediaProperty
) {
592 service
= (IOService
*)service
->getProperty(mediaProperty
);
598 // If the IOService we matched to is a subclass of IONetworkInterface,
599 // then make sure it has been registered with BSD and has a BSD name
603 && service
->metaCast( "IONetworkInterface" )
604 && !IORegisterNetworkInterface( service
) )
612 service
->getPath( str
, &len
, gIOServicePlane
);
613 IOLog( "Got boot device = %s\n", str
);
615 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
617 strlcpy( rootName
, iostr
->getCStringNoCopy(), rootNameSize
);
618 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
620 mjr
= off
->unsigned32BitValue();
621 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
623 mnr
= off
->unsigned32BitValue();
625 if( service
->metaCast( "IONetworkInterface" ))
630 IOLog( "Wait for root failed\n" );
631 strlcpy( rootName
, "en0", rootNameSize
);
635 IOLog( "BSD root: %s", rootName
);
637 IOLog(", major %d, minor %d\n", mjr
, mnr
);
641 *root
= makedev( mjr
, mnr
);
644 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
647 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
649 IOService::getPlatform()->waitQuiet();
650 if( gIOKitDebug
& kIOLogDTree
) {
651 IOLog("\nDT plane:\n");
652 IOPrintPlane( gIODTPlane
);
654 if( gIOKitDebug
& kIOLogServiceTree
) {
655 IOLog("\nService plane:\n");
656 IOPrintPlane( gIOServicePlane
);
658 if( gIOKitDebug
& kIOLogMemory
)
662 return( kIOReturnSuccess
);
665 bool IORamDiskBSDRoot(void)
667 char rdBootVar
[kMaxBootVar
];
668 if (PE_parse_boot_argn("rd", rdBootVar
, kMaxBootVar
)
669 || PE_parse_boot_argn("rootdev", rdBootVar
, kMaxBootVar
)) {
670 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
677 void IOSecureBSDRoot(const char * rootName
)
682 IOPlatformExpert
*pe
;
683 OSDictionary
*matching
;
684 const OSSymbol
*functionName
= OSSymbol::withCStringNoCopy("SecureRootName");
686 matching
= IOService::serviceMatching("IOPlatformExpert");
688 pe
= (IOPlatformExpert
*) IOService::waitForMatchingService(matching
, 30ULL * kSecondScale
);
691 // Returns kIOReturnNotPrivileged is the root device is not secure.
692 // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
693 result
= pe
->callPlatformFunction(functionName
, false, (void *)rootName
, (void *)0, (void *)0, (void *)0);
694 functionName
->release();
695 OSSafeReleaseNULL(pe
);
697 if (result
== kIOReturnNotPrivileged
) {
699 } else if (result
== kIOReturnSuccess
) {
700 // If we are booting with a secure root, and we have the right
701 // boot-arg, we will want to panic on exception triage. This
702 // behavior is intended as a debug aid (we can look at why an
703 // exception occured in the kernel debugger).
704 if (PE_parse_boot_argn("-panic_on_exception_triage", &tmpInt
, sizeof(tmpInt
))) {
705 panic_on_exception_triage
= 1;
709 #endif // CONFIG_EMBEDDED
713 IOBSDRegistryEntryForDeviceTree(char * path
)
715 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
719 IOBSDRegistryEntryRelease(void * entry
)
721 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
729 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
733 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
735 data
= (OSData
*) regEntry
->getProperty(property_name
);
737 *packet_length
= data
->getLength();
738 return (data
->getBytesNoCopy());
743 kern_return_t
IOBSDGetPlatformUUID( uuid_t uuid
, mach_timespec_t timeout
)
745 IOService
* resources
;
748 resources
= IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey
), ( timeout
.tv_sec
|| timeout
.tv_nsec
) ? &timeout
: 0 );
749 if ( resources
== 0 ) return KERN_OPERATION_TIMED_OUT
;
751 string
= ( OSString
* ) IOService::getPlatform( )->getProvider( )->getProperty( kIOPlatformUUIDKey
);
752 if ( string
== 0 ) return KERN_NOT_SUPPORTED
;
754 uuid_parse( string
->getCStringNoCopy( ), uuid
);
761 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
763 #include <sys/conf.h>
764 #include <sys/vnode.h>
765 #include <sys/vnode_internal.h>
766 #include <sys/fcntl.h>
767 #include <IOKit/IOPolledInterface.h>
768 #include <IOKit/IOBufferMemoryDescriptor.h>
770 IOPolledFileIOVars
* gIOPolledCoreFileVars
;
771 kern_return_t gIOPolledCoreFileOpenRet
= kIOReturnNotReady
;
772 #if IOPOLLED_COREFILE
775 IOOpenPolledCoreFile(const char * filename
)
779 uint64_t corefile_size_bytes
= 0;
781 if (gIOPolledCoreFileVars
) return (kIOReturnBusy
);
782 if (!IOPolledInterface::gMetaClass
.getInstanceCount()) return (kIOReturnUnsupported
);
785 PE_parse_boot_argn("debug", &debug
, sizeof (debug
));
786 if (DB_DISABLE_LOCAL_CORE
& debug
) return (kIOReturnUnsupported
);
789 unsigned int requested_corefile_size
= 0;
790 if (PE_parse_boot_argn("corefile_size_mb", &requested_corefile_size
, sizeof(requested_corefile_size
))) {
791 IOLog("Boot-args specify %d MB kernel corefile\n", requested_corefile_size
);
793 corefile_size_bytes
= (requested_corefile_size
* 1024ULL * 1024ULL);
799 #if defined(kIOCoreDumpLargeSize)
800 if (0 == corefile_size_bytes
)
802 // If no custom size was requested and we're on a device with >3GB of DRAM, attempt
803 // to allocate a large corefile otherwise use a small file.
804 if (max_mem
> (3 * 1024ULL * 1024ULL * 1024ULL))
806 corefile_size_bytes
= kIOCoreDumpLargeSize
;
807 err
= IOPolledFileOpen(filename
,
809 corefile_size_bytes
, kIOCoreDumpFreeSize
,
811 &gIOPolledCoreFileVars
, NULL
, NULL
, 0);
812 if (kIOReturnSuccess
== err
)
816 else if (kIOReturnNoSpace
== err
)
818 IOLog("Failed to open corefile of size %llu MB (low disk space)",
819 (corefile_size_bytes
/ (1024ULL * 1024ULL)));
820 if (corefile_size_bytes
== kIOCoreDumpMinSize
)
822 gIOPolledCoreFileOpenRet
= err
;
825 // Try to open a smaller corefile (set size and fall-through)
826 corefile_size_bytes
= kIOCoreDumpMinSize
;
830 IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
831 (corefile_size_bytes
/ (1024ULL * 1024ULL)), err
);
832 gIOPolledCoreFileOpenRet
= err
;
838 corefile_size_bytes
= kIOCoreDumpMinSize
;
841 #else /* defined(kIOCoreDumpLargeSize) */
842 if (0 == corefile_size_bytes
)
844 corefile_size_bytes
= kIOCoreDumpSize
;
846 #endif /* defined(kIOCoreDumpLargeSize) */
847 err
= IOPolledFileOpen(filename
,
849 corefile_size_bytes
, kIOCoreDumpFreeSize
,
851 &gIOPolledCoreFileVars
, NULL
, NULL
, 0);
852 if (kIOReturnSuccess
!= err
)
854 IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
855 (corefile_size_bytes
/ (1024ULL * 1024ULL)), err
);
856 gIOPolledCoreFileOpenRet
= err
;
861 err
= IOPolledFilePollersSetup(gIOPolledCoreFileVars
, kIOPolledPreflightCoreDumpState
);
862 if (kIOReturnSuccess
!= err
)
864 IOPolledFileClose(&gIOPolledCoreFileVars
, NULL
, NULL
, 0, 0, 0);
865 IOLog("IOPolledFilePollersSetup for corefile failed with error: 0x%x\n", err
);
866 gIOPolledCoreFileOpenRet
= err
;
870 IOLog("Opened corefile of size %llu MB\n", (corefile_size_bytes
/ (1024ULL * 1024ULL)));
877 IOClosePolledCoreFile(void)
879 gIOPolledCoreFileOpenRet
= kIOReturnNotOpen
;
880 IOPolledFilePollersClose(gIOPolledCoreFileVars
, kIOPolledPostflightCoreDumpState
);
881 IOPolledFileClose(&gIOPolledCoreFileVars
, NULL
, NULL
, 0, 0, 0);
884 static thread_call_t gIOOpenPolledCoreFileTC
;
885 static IONotifier
* gIOPolledCoreFileNotifier
;
886 static IONotifier
* gIOPolledCoreFileInterestNotifier
;
889 KernelCoreMediaInterest(void * target
, void * refCon
,
890 UInt32 messageType
, IOService
* provider
,
891 void * messageArgument
, vm_size_t argSize
)
893 if (kIOMessageServiceIsTerminated
== messageType
)
895 gIOPolledCoreFileInterestNotifier
->remove();
896 gIOPolledCoreFileInterestNotifier
= 0;
897 IOClosePolledCoreFile();
900 return (kIOReturnSuccess
);
904 OpenKernelCoreMedia(thread_call_param_t p0
, thread_call_param_t p1
)
906 IOService
* newService
;
910 newService
= (IOService
*) p1
;
913 if (gIOPolledCoreFileVars
) break;
914 string
= OSDynamicCast(OSString
, newService
->getProperty(kIOBSDNameKey
));
916 snprintf(filename
, sizeof(filename
), "/dev/%s", string
->getCStringNoCopy());
917 if (kIOReturnSuccess
!= IOOpenPolledCoreFile(filename
)) break;
918 gIOPolledCoreFileInterestNotifier
= newService
->registerInterest(
919 gIOGeneralInterest
, &KernelCoreMediaInterest
, NULL
, 0);
923 newService
->release();
927 NewKernelCoreMedia(void * target
, void * refCon
,
928 IOService
* newService
,
929 IONotifier
* notifier
)
931 static volatile UInt32 onlyOneCorePartition
= 0;
934 if (!OSCompareAndSwap(0, 1, &onlyOneCorePartition
)) break;
935 if (gIOPolledCoreFileVars
) break;
936 if (!gIOOpenPolledCoreFileTC
) break;
937 newService
= newService
->getProvider();
938 if (!newService
) break;
939 newService
->retain();
940 thread_call_enter1(gIOOpenPolledCoreFileTC
, newService
);
947 #endif /* IOPOLLED_COREFILE */
950 IOBSDMountChange(struct mount
* mp
, uint32_t op
)
952 #if IOPOLLED_COREFILE
954 OSDictionary
* bsdMatching
;
955 OSDictionary
* mediaMatching
;
958 if (!gIOPolledCoreFileNotifier
) do
960 if (!gIOOpenPolledCoreFileTC
) gIOOpenPolledCoreFileTC
= thread_call_allocate(&OpenKernelCoreMedia
, NULL
);
961 bsdMatching
= IOService::serviceMatching("IOMediaBSDClient");
962 if (!bsdMatching
) break;
963 mediaMatching
= IOService::serviceMatching("IOMedia");
964 string
= OSString::withCStringNoCopy("5361644D-6163-11AA-AA11-00306543ECAC");
965 if (!string
|| !mediaMatching
) break;
966 mediaMatching
->setObject("Content", string
);
968 bsdMatching
->setObject(gIOParentMatchKey
, mediaMatching
);
969 mediaMatching
->release();
971 gIOPolledCoreFileNotifier
= IOService::addMatchingNotification(
972 gIOFirstMatchNotification
, bsdMatching
,
973 &NewKernelCoreMedia
, NULL
, NULL
, -1000);
986 case kIOMountChangeMount
:
987 case kIOMountChangeDidResize
:
989 if (gIOPolledCoreFileVars
) break;
990 flags
= vfs_flags(mp
);
991 if (MNT_RDONLY
& flags
) break;
992 if (!(MNT_LOCAL
& flags
)) break;
994 vn
= vfs_vnodecovered(mp
);
996 pathLen
= sizeof(path
);
997 result
= vn_getpath(vn
, &path
[0], &pathLen
);
999 if (0 != result
) break;
1000 if (!pathLen
) break;
1001 #if defined(XNU_TARGET_OS_BRIDGE)
1002 // on bridgeOS systems we put the core in /private/var/internal. We don't
1003 // want to match with /private/var because /private/var/internal is often mounted
1004 // over /private/var
1005 if ((pathLen
- 1) < (int) strlen("/private/var/internal")) break;
1007 if (0 != strncmp(path
, kIOCoreDumpPath
, pathLen
- 1)) break;
1008 IOOpenPolledCoreFile(kIOCoreDumpPath
);
1011 case kIOMountChangeUnmount
:
1012 case kIOMountChangeWillResize
:
1013 if (gIOPolledCoreFileVars
&& (mp
== kern_file_mount(gIOPolledCoreFileVars
->fileRef
)))
1015 IOClosePolledCoreFile();
1019 #endif /* CONFIG_EMBEDDED */
1020 #endif /* IOPOLLED_COREFILE */
1023 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1025 extern "C" boolean_t
1026 IOTaskHasEntitlement(task_t task
, const char * entitlement
)
1029 obj
= IOUserClient::copyClientEntitlement(task
, entitlement
);
1030 if (!obj
) return (false);
1032 return (obj
!= kOSBooleanFalse
);