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