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