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