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