1*5c2921b0SApple OSS DistributionsWhat is XNU? 2*5c2921b0SApple OSS Distributions=========== 3*5c2921b0SApple OSS Distributions 4*5c2921b0SApple OSS DistributionsXNU kernel is part of the Darwin operating system for use in macOS and iOS operating systems. XNU is an acronym for X is Not Unix. 5*5c2921b0SApple OSS DistributionsXNU is a hybrid kernel combining the Mach kernel developed at Carnegie Mellon University with components from FreeBSD and a C++ API for writing drivers called IOKit. 6*5c2921b0SApple OSS DistributionsXNU runs on x86_64 for both single processor and multi-processor configurations. 7*5c2921b0SApple OSS Distributions 8*5c2921b0SApple OSS DistributionsXNU Source Tree 9*5c2921b0SApple OSS Distributions=============== 10*5c2921b0SApple OSS Distributions 11*5c2921b0SApple OSS Distributions * `config` - configurations for exported apis for supported architecture and platform 12*5c2921b0SApple OSS Distributions * `SETUP` - Basic set of tools used for configuring the kernel, versioning and kextsymbol management. 13*5c2921b0SApple OSS Distributions * `EXTERNAL_HEADERS` - Headers sourced from other projects to avoid dependency cycles when building. These headers should be regularly synced when source is updated. 14*5c2921b0SApple OSS Distributions * `libkern` - C++ IOKit library code for handling of drivers and kexts. 15*5c2921b0SApple OSS Distributions * `libsa` - kernel bootstrap code for startup 16*5c2921b0SApple OSS Distributions * `libsyscall` - syscall library interface for userspace programs 17*5c2921b0SApple OSS Distributions * `libkdd` - source for user library for parsing kernel data like kernel chunked data. 18*5c2921b0SApple OSS Distributions * `makedefs` - top level rules and defines for kernel build. 19*5c2921b0SApple OSS Distributions * `osfmk` - Mach kernel based subsystems 20*5c2921b0SApple OSS Distributions * `pexpert` - Platform specific code like interrupt handling, atomics etc. 21*5c2921b0SApple OSS Distributions * `security` - Mandatory Access Check policy interfaces and related implementation. 22*5c2921b0SApple OSS Distributions * `bsd` - BSD subsystems code 23*5c2921b0SApple OSS Distributions * `tools` - A set of utilities for testing, debugging and profiling kernel. 24*5c2921b0SApple OSS Distributions 25*5c2921b0SApple OSS DistributionsHow to build XNU 26*5c2921b0SApple OSS Distributions================ 27*5c2921b0SApple OSS Distributions 28*5c2921b0SApple OSS DistributionsBuilding `DEVELOPMENT` kernel 29*5c2921b0SApple OSS Distributions----------------------------- 30*5c2921b0SApple OSS Distributions 31*5c2921b0SApple OSS DistributionsThe xnu make system can build kernel based on `KERNEL_CONFIGS` & `ARCH_CONFIGS` variables as arguments. 32*5c2921b0SApple OSS DistributionsHere is the syntax: 33*5c2921b0SApple OSS Distributions 34*5c2921b0SApple OSS Distributions make SDKROOT=<sdkroot> ARCH_CONFIGS=<arch> KERNEL_CONFIGS=<variant> 35*5c2921b0SApple OSS Distributions 36*5c2921b0SApple OSS DistributionsWhere: 37*5c2921b0SApple OSS Distributions 38*5c2921b0SApple OSS Distributions * \<sdkroot>: path to macOS SDK on disk. (defaults to `/`) 39*5c2921b0SApple OSS Distributions * \<variant>: can be `debug`, `development`, `release`, `profile` and configures compilation flags and asserts throughout kernel code. 40*5c2921b0SApple OSS Distributions * \<arch> : can be valid arch to build for. (E.g. `X86_64`) 41*5c2921b0SApple OSS Distributions 42*5c2921b0SApple OSS DistributionsTo build a kernel for the same architecture as running OS, just type 43*5c2921b0SApple OSS Distributions 44*5c2921b0SApple OSS Distributions $ make 45*5c2921b0SApple OSS Distributions $ make SDKROOT=macosx.internal 46*5c2921b0SApple OSS Distributions 47*5c2921b0SApple OSS DistributionsAdditionally, there is support for configuring architectures through `ARCH_CONFIGS` and kernel configurations with `KERNEL_CONFIGS`. 48*5c2921b0SApple OSS Distributions 49*5c2921b0SApple OSS Distributions $ make SDKROOT=macosx.internal ARCH_CONFIGS=X86_64 KERNEL_CONFIGS=DEVELOPMENT 50*5c2921b0SApple OSS Distributions $ make SDKROOT=macosx.internal ARCH_CONFIGS=X86_64 KERNEL_CONFIGS="RELEASE DEVELOPMENT DEBUG" 51*5c2921b0SApple OSS Distributions 52*5c2921b0SApple OSS Distributions 53*5c2921b0SApple OSS DistributionsNote: 54*5c2921b0SApple OSS Distributions * By default, architecture is set to the build machine architecture, and the default kernel 55*5c2921b0SApple OSS Distributions config is set to build for DEVELOPMENT. 56*5c2921b0SApple OSS Distributions 57*5c2921b0SApple OSS Distributions 58*5c2921b0SApple OSS DistributionsThis will also create a bootable image, kernel.[config], and a kernel binary 59*5c2921b0SApple OSS Distributionswith symbols, kernel.[config].unstripped. 60*5c2921b0SApple OSS Distributions 61*5c2921b0SApple OSS DistributionsTo intall the kernel into a DSTROOT, use the `install_kernels` target: 62*5c2921b0SApple OSS Distributions 63*5c2921b0SApple OSS Distributions $ make install_kernels DSTROOT=/tmp/xnu-dst 64*5c2921b0SApple OSS Distributions 65*5c2921b0SApple OSS DistributionsHint: 66*5c2921b0SApple OSS DistributionsFor a more satisfying kernel debugging experience, with access to all 67*5c2921b0SApple OSS Distributionslocal variables and arguments, but without all the extra check of the 68*5c2921b0SApple OSS DistributionsDEBUG kernel, add something like: 69*5c2921b0SApple OSS Distributions CFLAGS_DEVELOPMENTARM64="-O0 -g -DKERNEL_STACK_MULTIPLIER=2" 70*5c2921b0SApple OSS Distributions CXXFLAGS_DEVELOPMENTARM64="-O0 -g -DKERNEL_STACK_MULTIPLIER=2" 71*5c2921b0SApple OSS Distributionsto your make command. 72*5c2921b0SApple OSS DistributionsReplace DEVELOPMENT and ARM64 with the appropriate build and platform. 73*5c2921b0SApple OSS Distributions 74*5c2921b0SApple OSS Distributions 75*5c2921b0SApple OSS Distributions * To build with RELEASE kernel configuration 76*5c2921b0SApple OSS Distributions 77*5c2921b0SApple OSS Distributions make KERNEL_CONFIGS=RELEASE SDKROOT=/path/to/SDK 78*5c2921b0SApple OSS Distributions 79*5c2921b0SApple OSS Distributions 80*5c2921b0SApple OSS DistributionsBuilding FAT kernel binary 81*5c2921b0SApple OSS Distributions-------------------------- 82*5c2921b0SApple OSS Distributions 83*5c2921b0SApple OSS DistributionsDefine architectures in your environment or when running a make command. 84*5c2921b0SApple OSS Distributions 85*5c2921b0SApple OSS Distributions $ make ARCH_CONFIGS="X86_64" exporthdrs all 86*5c2921b0SApple OSS Distributions 87*5c2921b0SApple OSS DistributionsOther makefile options 88*5c2921b0SApple OSS Distributions---------------------- 89*5c2921b0SApple OSS Distributions 90*5c2921b0SApple OSS Distributions * $ make MAKEJOBS=-j8 # this will use 8 processes during the build. The default is 2x the number of active CPUS. 91*5c2921b0SApple OSS Distributions * $ make -j8 # the standard command-line option is also accepted 92*5c2921b0SApple OSS Distributions * $ make -w # trace recursive make invocations. Useful in combination with VERBOSE=YES 93*5c2921b0SApple OSS Distributions * $ make BUILD_LTO=0 # build without LLVM Link Time Optimization 94*5c2921b0SApple OSS Distributions * $ make BOUND_CHECKS=0 # disable -fbound-attributes for this build 95*5c2921b0SApple OSS Distributions * $ make REMOTEBUILD=user@remotehost # perform build on remote host 96*5c2921b0SApple OSS Distributions * $ make BUILD_JSON_COMPILATION_DATABASE=1 # Build Clang JSON Compilation Database 97*5c2921b0SApple OSS Distributions 98*5c2921b0SApple OSS DistributionsThe XNU build system can optionally output color-formatted build output. To enable this, you can either 99*5c2921b0SApple OSS Distributionsset the `XNU_LOGCOLORS` environment variable to `y`, or you can pass `LOGCOLORS=y` to the make command. 100*5c2921b0SApple OSS Distributions 101*5c2921b0SApple OSS Distributions 102*5c2921b0SApple OSS DistributionsDebug information formats 103*5c2921b0SApple OSS Distributions========================= 104*5c2921b0SApple OSS Distributions 105*5c2921b0SApple OSS DistributionsBy default, a DWARF debug information repository is created during the install phase; this is a "bundle" named kernel.development.\<variant>.dSYM 106*5c2921b0SApple OSS DistributionsTo select the older STABS debug information format (where debug information is embedded in the kernel.development.unstripped image), set the BUILD_STABS environment variable. 107*5c2921b0SApple OSS Distributions 108*5c2921b0SApple OSS Distributions $ export BUILD_STABS=1 109*5c2921b0SApple OSS Distributions $ make 110*5c2921b0SApple OSS Distributions 111*5c2921b0SApple OSS Distributions 112*5c2921b0SApple OSS DistributionsBuilding KernelCaches 113*5c2921b0SApple OSS Distributions===================== 114*5c2921b0SApple OSS Distributions 115*5c2921b0SApple OSS DistributionsTo test the xnu kernel, you need to build a kernelcache that links the kexts and 116*5c2921b0SApple OSS Distributionskernel together into a single bootable image. 117*5c2921b0SApple OSS DistributionsTo build a kernelcache you can use the following mechanisms: 118*5c2921b0SApple OSS Distributions 119*5c2921b0SApple OSS Distributions * Using automatic kernelcache generation with `kextd`. 120*5c2921b0SApple OSS Distributions The kextd daemon keeps watching for changing in `/System/Library/Extensions` directory. 121*5c2921b0SApple OSS Distributions So you can setup new kernel as 122*5c2921b0SApple OSS Distributions 123*5c2921b0SApple OSS Distributions $ cp BUILD/obj/DEVELOPMENT/X86_64/kernel.development /System/Library/Kernels/ 124*5c2921b0SApple OSS Distributions $ touch /System/Library/Extensions 125*5c2921b0SApple OSS Distributions $ ps -e | grep kextd 126*5c2921b0SApple OSS Distributions 127*5c2921b0SApple OSS Distributions * Manually invoking `kextcache` to build new kernelcache. 128*5c2921b0SApple OSS Distributions 129*5c2921b0SApple OSS Distributions $ kextcache -q -z -a x86_64 -l -n -c /var/tmp/kernelcache.test -K /var/tmp/kernel.test /System/Library/Extensions 130*5c2921b0SApple OSS Distributions 131*5c2921b0SApple OSS Distributions 132*5c2921b0SApple OSS Distributions 133*5c2921b0SApple OSS DistributionsRunning KernelCache on Target machine 134*5c2921b0SApple OSS Distributions===================================== 135*5c2921b0SApple OSS Distributions 136*5c2921b0SApple OSS DistributionsThe development kernel and iBoot supports configuring boot arguments so that we can safely boot into test kernel and, if things go wrong, safely fall back to previously used kernelcache. 137*5c2921b0SApple OSS DistributionsFollowing are the steps to get such a setup: 138*5c2921b0SApple OSS Distributions 139*5c2921b0SApple OSS Distributions 1. Create kernel cache using the kextcache command as `/kernelcache.test` 140*5c2921b0SApple OSS Distributions 2. Copy exiting boot configurations to alternate file 141*5c2921b0SApple OSS Distributions 142*5c2921b0SApple OSS Distributions $ cp /Library/Preferences/SystemConfiguration/com.apple.Boot.plist /next_boot.plist 143*5c2921b0SApple OSS Distributions 144*5c2921b0SApple OSS Distributions 3. Update the kernelcache and boot-args for your setup 145*5c2921b0SApple OSS Distributions 146*5c2921b0SApple OSS Distributions $ plutil -insert "Kernel Cache" -string "kernelcache.test" /next_boot.plist 147*5c2921b0SApple OSS Distributions $ plutil -replace "Kernel Flags" -string "debug=0x144 -v kernelsuffix=test " /next_boot.plist 148*5c2921b0SApple OSS Distributions 149*5c2921b0SApple OSS Distributions 4. Copy the new config to `/Library/Preferences/SystemConfiguration/` 150*5c2921b0SApple OSS Distributions 151*5c2921b0SApple OSS Distributions $ cp /next_boot.plist /Library/Preferences/SystemConfiguration/boot.plist 152*5c2921b0SApple OSS Distributions 153*5c2921b0SApple OSS Distributions 5. Bless the volume with new configs. 154*5c2921b0SApple OSS Distributions 155*5c2921b0SApple OSS Distributions $ sudo -n bless --mount / --setBoot --nextonly --options "config=boot" 156*5c2921b0SApple OSS Distributions 157*5c2921b0SApple OSS Distributions The `--nextonly` flag specifies that use the `boot.plist` configs only for one boot. 158*5c2921b0SApple OSS Distributions So if the kernel panic's you can easily power reboot and recover back to original kernel. 159*5c2921b0SApple OSS Distributions 160*5c2921b0SApple OSS Distributions 161*5c2921b0SApple OSS Distributions 162*5c2921b0SApple OSS Distributions 163*5c2921b0SApple OSS DistributionsCreating tags and cscope 164*5c2921b0SApple OSS Distributions======================== 165*5c2921b0SApple OSS Distributions 166*5c2921b0SApple OSS DistributionsSet up your build environment and from the top directory, run: 167*5c2921b0SApple OSS Distributions 168*5c2921b0SApple OSS Distributions $ make tags # this will build ctags and etags on a case-sensitive volume, only ctags on case-insensitive 169*5c2921b0SApple OSS Distributions $ make TAGS # this will build etags 170*5c2921b0SApple OSS Distributions $ make cscope # this will build cscope database 171*5c2921b0SApple OSS Distributions 172*5c2921b0SApple OSS Distributions 173*5c2921b0SApple OSS DistributionsHow to install a new header file from XNU 174*5c2921b0SApple OSS Distributions========================================= 175*5c2921b0SApple OSS Distributions 176*5c2921b0SApple OSS DistributionsXNU installs header files at the following locations - 177*5c2921b0SApple OSS Distributions 178*5c2921b0SApple OSS Distributions a. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers 179*5c2921b0SApple OSS Distributions b. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders 180*5c2921b0SApple OSS Distributions c. $(DSTROOT)/usr/include/ 181*5c2921b0SApple OSS Distributions d. $(DSTROOT)/usr/local/include/ 182*5c2921b0SApple OSS Distributions e. $(DSTROOT)/System/DriverKit/usr/include/ 183*5c2921b0SApple OSS Distributions f. $(DSTROOT)/System/Library/Frameworks/IOKit.framework/Headers 184*5c2921b0SApple OSS Distributions g. $(DSTROOT)/System/Library/Frameworks/IOKit.framework/PrivateHeaders 185*5c2921b0SApple OSS Distributions h. $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders 186*5c2921b0SApple OSS Distributions 187*5c2921b0SApple OSS Distributions`Kernel.framework` is used by kernel extensions.\ 188*5c2921b0SApple OSS DistributionsThe `System.framework`, `/usr/include` and `/usr/local/include` are used by user level applications. \ 189*5c2921b0SApple OSS Distributions`IOKit.framework` is used by IOKit userspace clients. \ 190*5c2921b0SApple OSS Distributions`/System/DriverKit/usr/include` is used by userspace drivers. \ 191*5c2921b0SApple OSS DistributionsThe header files in framework's `PrivateHeaders` are only available for ** Apple Internal Development **. 192*5c2921b0SApple OSS Distributions 193*5c2921b0SApple OSS DistributionsThe directory containing the header file should have a Makefile that 194*5c2921b0SApple OSS Distributionscreates the list of files that should be installed at different locations. 195*5c2921b0SApple OSS DistributionsIf you are adding the first header file in a directory, you will need to 196*5c2921b0SApple OSS Distributionscreate Makefile similar to `xnu/bsd/sys/Makefile`. 197*5c2921b0SApple OSS Distributions 198*5c2921b0SApple OSS DistributionsAdd your header file to the correct file list depending on where you want 199*5c2921b0SApple OSS Distributionsto install it. The default locations where the header files are installed 200*5c2921b0SApple OSS Distributionsfrom each file list are - 201*5c2921b0SApple OSS Distributions 202*5c2921b0SApple OSS Distributions a. `DATAFILES` : To make header file available in user level - 203*5c2921b0SApple OSS Distributions `$(DSTROOT)/usr/include` 204*5c2921b0SApple OSS Distributions `$(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders` 205*5c2921b0SApple OSS Distributions 206*5c2921b0SApple OSS Distributions b. `DRIVERKIT_DATAFILES` : To make header file available to DriverKit userspace drivers - 207*5c2921b0SApple OSS Distributions `$(DSTROOT)/System/DriverKit/usr/include` 208*5c2921b0SApple OSS Distributions 209*5c2921b0SApple OSS Distributions c. `PRIVATE_DATAFILES` : To make header file available to Apple internal in 210*5c2921b0SApple OSS Distributions user level - 211*5c2921b0SApple OSS Distributions `$(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders` 212*5c2921b0SApple OSS Distributions 213*5c2921b0SApple OSS Distributions d. `EMBEDDED_PRIVATE_DATAFILES` : To make header file available in user 214*5c2921b0SApple OSS Distributions level for macOS as `EXTRA_DATAFILES`, but Apple internal in user level 215*5c2921b0SApple OSS Distributions for embedded OSes as `EXTRA_PRIVATE_DATAFILES` - 216*5c2921b0SApple OSS Distributions `$(DSTROOT)/usr/include` (`EXTRA_DATAFILES`) 217*5c2921b0SApple OSS Distributions `$(DSTROOT)/usr/local/include` (`EXTRA_PRIVATE_DATAFILES`) 218*5c2921b0SApple OSS Distributions 219*5c2921b0SApple OSS Distributions d. `KERNELFILES` : To make header file available in kernel level - 220*5c2921b0SApple OSS Distributions `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers` 221*5c2921b0SApple OSS Distributions `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders` 222*5c2921b0SApple OSS Distributions 223*5c2921b0SApple OSS Distributions e. `PRIVATE_KERNELFILES` : To make header file available to Apple internal 224*5c2921b0SApple OSS Distributions for kernel extensions - 225*5c2921b0SApple OSS Distributions `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders` 226*5c2921b0SApple OSS Distributions 227*5c2921b0SApple OSS Distributions f. `MODULEMAPFILES` : To make module map file available in user level - 228*5c2921b0SApple OSS Distributions `$(DSTROOT)/usr/include` 229*5c2921b0SApple OSS Distributions 230*5c2921b0SApple OSS Distributions g. `PRIVATE_MODULEMAPFILES` : To make module map file available to Apple 231*5c2921b0SApple OSS Distributions internal in user level - 232*5c2921b0SApple OSS Distributions `$(DSTROOT)/usr/local/include` 233*5c2921b0SApple OSS Distributions 234*5c2921b0SApple OSS DistributionsThe Makefile combines the file lists mentioned above into different 235*5c2921b0SApple OSS Distributionsinstall lists which are used by build system to install the header files. There 236*5c2921b0SApple OSS Distributionsare two types of install lists: machine-dependent and machine-independent. 237*5c2921b0SApple OSS DistributionsThese lists are indicated by the presence of `MD` and `MI` in the build 238*5c2921b0SApple OSS Distributionssetting, respectively. If your header is architecture-specific, then you should 239*5c2921b0SApple OSS Distributionsuse a machine-dependent install list (e.g. `INSTALL_MD_LIST`). If your header 240*5c2921b0SApple OSS Distributionsshould be installed for all architectures, then you should use a 241*5c2921b0SApple OSS Distributionsmachine-independent install list (e.g. `INSTALL_MI_LIST`). 242*5c2921b0SApple OSS Distributions 243*5c2921b0SApple OSS DistributionsIf the install list that you are interested does not exist, create it 244*5c2921b0SApple OSS Distributionsby adding the appropriate file lists. The default install lists, its 245*5c2921b0SApple OSS Distributionsmember file lists and their default location are described below - 246*5c2921b0SApple OSS Distributions 247*5c2921b0SApple OSS Distributions a. `INSTALL_MI_LIST`, `INSTALL_MODULEMAP_MI_LIST` : Installs header and module map 248*5c2921b0SApple OSS Distributions files to a location that is available to everyone in user level. 249*5c2921b0SApple OSS Distributions Locations - 250*5c2921b0SApple OSS Distributions $(DSTROOT)/usr/include 251*5c2921b0SApple OSS Distributions Definition - 252*5c2921b0SApple OSS Distributions INSTALL_MI_LIST = ${DATAFILES} 253*5c2921b0SApple OSS Distributions INSTALL_MODULEMAP_MI_LIST = ${MODULEMAPFILES} 254*5c2921b0SApple OSS Distributions 255*5c2921b0SApple OSS Distributions b. `INSTALL_DRIVERKIT_MI_LIST` : Installs header file to a location that is 256*5c2921b0SApple OSS Distributions available to DriverKit userspace drivers. 257*5c2921b0SApple OSS Distributions Locations - 258*5c2921b0SApple OSS Distributions $(DSTROOT)/System/DriverKit/usr/include 259*5c2921b0SApple OSS Distributions Definition - 260*5c2921b0SApple OSS Distributions INSTALL_DRIVERKIT_MI_LIST = ${DRIVERKIT_DATAFILES} 261*5c2921b0SApple OSS Distributions 262*5c2921b0SApple OSS Distributions c. `INSTALL_MI_LCL_LIST`, `INSTALL_MODULEMAP_MI_LCL_LIST` : Installs header and 263*5c2921b0SApple OSS Distributions module map files to a location that is available for Apple internal in user level. 264*5c2921b0SApple OSS Distributions Locations - 265*5c2921b0SApple OSS Distributions $(DSTROOT)/usr/local/include 266*5c2921b0SApple OSS Distributions Definition - 267*5c2921b0SApple OSS Distributions INSTALL_MI_LCL_LIST = 268*5c2921b0SApple OSS Distributions INSTALL_MODULEMAP_MI_LCL_LIST = ${PRIVATE_MODULEMAPFILES} 269*5c2921b0SApple OSS Distributions 270*5c2921b0SApple OSS Distributions d. `INSTALL_IF_MI_LIST` : Installs header file to location that is available 271*5c2921b0SApple OSS Distributions to everyone for IOKit userspace clients. 272*5c2921b0SApple OSS Distributions Locations - 273*5c2921b0SApple OSS Distributions $(DSTROOT)/System/Library/Frameworks/IOKit.framework/Headers 274*5c2921b0SApple OSS Distributions Definition - 275*5c2921b0SApple OSS Distributions INSTALL_IF_MI_LIST = ${DATAFILES} 276*5c2921b0SApple OSS Distributions 277*5c2921b0SApple OSS Distributions e. `INSTALL_IF_MI_LCL_LIST` : Installs header file to location that is 278*5c2921b0SApple OSS Distributions available to Apple internal for IOKit userspace clients. 279*5c2921b0SApple OSS Distributions Locations - 280*5c2921b0SApple OSS Distributions $(DSTROOT)/System/Library/Frameworks/IOKit.framework/PrivateHeaders 281*5c2921b0SApple OSS Distributions Definition - 282*5c2921b0SApple OSS Distributions INSTALL_IF_MI_LCL_LIST = ${DATAFILES} ${PRIVATE_DATAFILES} 283*5c2921b0SApple OSS Distributions 284*5c2921b0SApple OSS Distributions f. `INSTALL_SF_MI_LCL_LIST` : Installs header file to a location that is available 285*5c2921b0SApple OSS Distributions for Apple internal in user level. 286*5c2921b0SApple OSS Distributions Locations - 287*5c2921b0SApple OSS Distributions $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders 288*5c2921b0SApple OSS Distributions Definition - 289*5c2921b0SApple OSS Distributions INSTALL_SF_MI_LCL_LIST = ${DATAFILES} ${PRIVATE_DATAFILES} 290*5c2921b0SApple OSS Distributions 291*5c2921b0SApple OSS Distributions g. `INSTALL_KF_MI_LIST` : Installs header file to location that is available 292*5c2921b0SApple OSS Distributions to everyone for kernel extensions. 293*5c2921b0SApple OSS Distributions Locations - 294*5c2921b0SApple OSS Distributions $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers 295*5c2921b0SApple OSS Distributions Definition - 296*5c2921b0SApple OSS Distributions INSTALL_KF_MI_LIST = ${KERNELFILES} 297*5c2921b0SApple OSS Distributions 298*5c2921b0SApple OSS Distributions h. `INSTALL_KF_MI_LCL_LIST` : Installs header file to location that is 299*5c2921b0SApple OSS Distributions available for Apple internal for kernel extensions. 300*5c2921b0SApple OSS Distributions Locations - 301*5c2921b0SApple OSS Distributions $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders 302*5c2921b0SApple OSS Distributions Definition - 303*5c2921b0SApple OSS Distributions INSTALL_KF_MI_LCL_LIST = ${KERNELFILES} ${PRIVATE_KERNELFILES} 304*5c2921b0SApple OSS Distributions 305*5c2921b0SApple OSS Distributions i. `EXPORT_MI_LIST` : Exports header file to all of xnu (bsd/, osfmk/, etc.) 306*5c2921b0SApple OSS Distributions for compilation only. Does not install anything into the SDK. 307*5c2921b0SApple OSS Distributions Definition - 308*5c2921b0SApple OSS Distributions EXPORT_MI_LIST = ${KERNELFILES} ${PRIVATE_KERNELFILES} 309*5c2921b0SApple OSS Distributions 310*5c2921b0SApple OSS DistributionsIf you want to install the header file in a sub-directory of the paths 311*5c2921b0SApple OSS Distributionsdescribed in (1), specify the directory name using two variables 312*5c2921b0SApple OSS Distributions`INSTALL_MI_DIR` and `EXPORT_MI_DIR` as follows - 313*5c2921b0SApple OSS Distributions 314*5c2921b0SApple OSS Distributions INSTALL_MI_DIR = dirname 315*5c2921b0SApple OSS Distributions EXPORT_MI_DIR = dirname 316*5c2921b0SApple OSS Distributions 317*5c2921b0SApple OSS DistributionsIf you want to install the module map file in a sub-directory, specify the 318*5c2921b0SApple OSS Distributionsdirectory name using the variable `INSTALL_MODULEMAP_MI_DIR` as follows - 319*5c2921b0SApple OSS Distributions 320*5c2921b0SApple OSS Distributions INSTALL_MODULEMAP_MI_DIR = dirname 321*5c2921b0SApple OSS Distributions 322*5c2921b0SApple OSS DistributionsA single header file can exist at different locations using the steps 323*5c2921b0SApple OSS Distributionsmentioned above. However it might not be desirable to make all the code 324*5c2921b0SApple OSS Distributionsin the header file available at all the locations. For example, you 325*5c2921b0SApple OSS Distributionswant to export a function only to kernel level but not user level. 326*5c2921b0SApple OSS Distributions 327*5c2921b0SApple OSS Distributions You can use C language's pre-processor directive (#ifdef, #endif, #ifndef) 328*5c2921b0SApple OSS Distributions to control the text generated before a header file is installed. The kernel 329*5c2921b0SApple OSS Distributions only includes the code if the conditional macro is TRUE and strips out 330*5c2921b0SApple OSS Distributions code for FALSE conditions from the header file. 331*5c2921b0SApple OSS Distributions 332*5c2921b0SApple OSS Distributions Some pre-defined macros and their descriptions are - 333*5c2921b0SApple OSS Distributions 334*5c2921b0SApple OSS Distributions a. `PRIVATE` : If defined, enclosed definitions are considered System 335*5c2921b0SApple OSS Distributions Private Interfaces. These are visible within xnu and 336*5c2921b0SApple OSS Distributions exposed in user/kernel headers installed within the AppleInternal 337*5c2921b0SApple OSS Distributions "PrivateHeaders" sections of the System and Kernel frameworks. 338*5c2921b0SApple OSS Distributions b. `KERNEL_PRIVATE` : If defined, enclosed code is available to all of xnu 339*5c2921b0SApple OSS Distributions kernel and Apple internal kernel extensions and omitted from user 340*5c2921b0SApple OSS Distributions headers. 341*5c2921b0SApple OSS Distributions c. `BSD_KERNEL_PRIVATE` : If defined, enclosed code is visible exclusively 342*5c2921b0SApple OSS Distributions within the xnu/bsd module. 343*5c2921b0SApple OSS Distributions d. `MACH_KERNEL_PRIVATE`: If defined, enclosed code is visible exclusively 344*5c2921b0SApple OSS Distributions within the xnu/osfmk module. 345*5c2921b0SApple OSS Distributions e. `XNU_KERNEL_PRIVATE`: If defined, enclosed code is visible exclusively 346*5c2921b0SApple OSS Distributions within xnu. 347*5c2921b0SApple OSS Distributions f. `KERNEL` : If defined, enclosed code is available within xnu and kernel 348*5c2921b0SApple OSS Distributions extensions and is not visible in user level header files. Only the 349*5c2921b0SApple OSS Distributions header files installed in following paths will have the code - 350*5c2921b0SApple OSS Distributions 351*5c2921b0SApple OSS Distributions $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers 352*5c2921b0SApple OSS Distributions $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders 353*5c2921b0SApple OSS Distributions g. `DRIVERKIT`: If defined, enclosed code is visible exclusively in the 354*5c2921b0SApple OSS Distributions DriverKit SDK headers used by userspace drivers. 355*5c2921b0SApple OSS Distributions 356*5c2921b0SApple OSS DistributionsModule map file name convention 357*5c2921b0SApple OSS Distributions=============================== 358*5c2921b0SApple OSS Distributions 359*5c2921b0SApple OSS DistributionsIn the simple case, a subdirectory of `usr/include` or `usr/local/include` 360*5c2921b0SApple OSS Distributionscan be represented by a standalone module. Where this is the case, set 361*5c2921b0SApple OSS Distributions`INSTALL_MODULEMAP_MI_DIR` to `INSTALL_MI_DIR` and install a `module.modulemap` 362*5c2921b0SApple OSS Distributionsfile there. `module.modulemap` is used even for private modules in 363*5c2921b0SApple OSS Distributions`usr/local/include`; `module.private.modulemap` is not used. Caveat: in order 364*5c2921b0SApple OSS Distributionsto stay in the simple case, the module name needs to be exactly the same as 365*5c2921b0SApple OSS Distributionsthe directory name. If that's not possible, then the following method will 366*5c2921b0SApple OSS Distributionsneed to be applied. 367*5c2921b0SApple OSS Distributions 368*5c2921b0SApple OSS Distributions`xnu` contributes to the modules defined in CoreOSModuleMaps by installing 369*5c2921b0SApple OSS Distributionsmodule map files that are sourced from `usr/include/module.modulemap` and 370*5c2921b0SApple OSS Distributions`usr/local/include/module.modulemap`. The naming convention for the `xnu` 371*5c2921b0SApple OSS Distributionsmodule map files are as follows. 372*5c2921b0SApple OSS Distributions 373*5c2921b0SApple OSS Distributions a. Ideally the module map file covers an entire directory. A module map 374*5c2921b0SApple OSS Distributions file covering `usr/include/a/b/c` would be named `a_b_c.modulemap`. 375*5c2921b0SApple OSS Distributions `usr/local/include/a/b/c` would be `a_b_c_private.modulemap`. 376*5c2921b0SApple OSS Distributions b. Some headers are special and require their own module. In that case, 377*5c2921b0SApple OSS Distributions the module map file would be named after the module it defines. 378*5c2921b0SApple OSS Distributions A module map file defining the module `One.Two.Three` would be named 379*5c2921b0SApple OSS Distributions `one_two_three.modulemap`. 380*5c2921b0SApple OSS Distributions 381*5c2921b0SApple OSS DistributionsConditional compilation 382*5c2921b0SApple OSS Distributions======================= 383*5c2921b0SApple OSS Distributions 384*5c2921b0SApple OSS Distributions`xnu` offers the following mechanisms for conditionally compiling code: 385*5c2921b0SApple OSS Distributions 386*5c2921b0SApple OSS Distributions a. *CPU Characteristics* If the code you are guarding has specific 387*5c2921b0SApple OSS Distributions characterstics that will vary only based on the CPU architecture being 388*5c2921b0SApple OSS Distributions targeted, use this option. Prefer checking for features of the 389*5c2921b0SApple OSS Distributions architecture (e.g. `__LP64__`, `__LITTLE_ENDIAN__`, etc.). 390*5c2921b0SApple OSS Distributions b. *New Features* If the code you are guarding, when taken together, 391*5c2921b0SApple OSS Distributions implements a feature, you should define a new feature in `config/MASTER` 392*5c2921b0SApple OSS Distributions and use the resulting `CONFIG` preprocessor token (e.g. for a feature 393*5c2921b0SApple OSS Distributions named `config_virtual_memory`, check for `#if CONFIG_VIRTUAL_MEMORY`). 394*5c2921b0SApple OSS Distributions This practice ensures that existing features may be brought to other 395*5c2921b0SApple OSS Distributions platforms by simply changing a feature switch. 396*5c2921b0SApple OSS Distributions c. *Existing Features* You can use existing features if your code is 397*5c2921b0SApple OSS Distributions strongly tied to them (e.g. use `SECURE_KERNEL` if your code implements 398*5c2921b0SApple OSS Distributions new functionality that is exclusively relevant to the trusted kernel and 399*5c2921b0SApple OSS Distributions updates the definition/understanding of what being a trusted kernel means). 400*5c2921b0SApple OSS Distributions 401*5c2921b0SApple OSS DistributionsIt is recommended that you avoid compiling based on the target platform. `xnu` 402*5c2921b0SApple OSS Distributionsdoes not define the platform macros from `TargetConditionals.h` 403*5c2921b0SApple OSS Distributions(`TARGET_OS_OSX`, `TARGET_OS_IOS`, etc.). 404*5c2921b0SApple OSS Distributions 405*5c2921b0SApple OSS Distributions 406*5c2921b0SApple OSS DistributionsDebugging xnu 407*5c2921b0SApple OSS Distributions============= 408*5c2921b0SApple OSS Distributions 409*5c2921b0SApple OSS DistributionsBy default, the kernel reboots in the event of a panic. 410*5c2921b0SApple OSS DistributionsThis behavior can be overriden by the `debug` boot-arg -- `debug=0x14e` will cause a panic to wait for a debugger to attach. 411*5c2921b0SApple OSS DistributionsTo boot a kernel so it can be debugged by an attached machine, override the `kdp_match_name` boot-arg with the appropriate `ifconfig` interface. 412*5c2921b0SApple OSS DistributionsEthernet, Thunderbolt, and serial debugging are supported, depending on the hardware. 413*5c2921b0SApple OSS Distributions 414*5c2921b0SApple OSS DistributionsUse LLDB to debug the kernel: 415*5c2921b0SApple OSS Distributions 416*5c2921b0SApple OSS Distributions ; xcrun -sdk macosx lldb <path-to-unstripped-kernel> 417*5c2921b0SApple OSS Distributions (lldb) gdb-remote [<host-ip>:]<port> 418*5c2921b0SApple OSS Distributions 419*5c2921b0SApple OSS DistributionsThe debug info for the kernel (dSYM) comes with a set of macros to support kernel debugging. 420*5c2921b0SApple OSS DistributionsTo load these macros automatically when attaching to the kernel, add the following to `~/.lldbinit`: 421*5c2921b0SApple OSS Distributions 422*5c2921b0SApple OSS Distributions settings set target.load-script-from-symbol-file true 423*5c2921b0SApple OSS Distributions 424*5c2921b0SApple OSS Distributions`tools/lldbmacros` contains the source for these commands. 425*5c2921b0SApple OSS DistributionsSee the README in that directory for their usage, or use the built-in LLDB help with: 426*5c2921b0SApple OSS Distributions 427*5c2921b0SApple OSS Distributions (lldb) help showcurrentstacks 428*5c2921b0SApple OSS Distributions 429