xref: /xnu-11215/README.md (revision 8d741a5d)
194d3b452SApple OSS Distributions# What is XNU?
288cc0b97SApple OSS Distributions
3cc9a6355SApple 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.
4cc9a6355SApple 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.
594d3b452SApple OSS DistributionsXNU runs on x86_64 and ARM64 for both single processor and multi-processor configurations.
688cc0b97SApple OSS Distributions
794d3b452SApple OSS Distributions## The XNU Source Tree
888cc0b97SApple OSS Distributions
988cc0b97SApple OSS Distributions* `config` - configurations for exported apis for supported architecture and platform
1088cc0b97SApple OSS Distributions* `SETUP` - Basic set of tools used for configuring the kernel, versioning and kextsymbol management.
1188cc0b97SApple 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.
1288cc0b97SApple OSS Distributions* `libkern` - C++ IOKit library code for handling of drivers and kexts.
1388cc0b97SApple OSS Distributions* `libsa` -  kernel bootstrap code for startup
1488cc0b97SApple OSS Distributions* `libsyscall` - syscall library interface for userspace programs
1588cc0b97SApple OSS Distributions* `libkdd` - source for user library for parsing kernel data like kernel chunked data.
1688cc0b97SApple OSS Distributions* `makedefs` - top level rules and defines for kernel build.
1788cc0b97SApple OSS Distributions* `osfmk` - Mach kernel based subsystems
1888cc0b97SApple OSS Distributions* `pexpert` - Platform specific code like interrupt handling, atomics etc.
1988cc0b97SApple OSS Distributions* `security` - Mandatory Access Check policy interfaces and related implementation.
2088cc0b97SApple OSS Distributions* `bsd` - BSD subsystems code
2188cc0b97SApple OSS Distributions* `tools` - A set of utilities for testing, debugging and profiling kernel.
2288cc0b97SApple OSS Distributions
2394d3b452SApple OSS Distributions## How to Build XNU
2488cc0b97SApple OSS Distributions
2594d3b452SApple OSS Distributions### Building a `DEVELOPMENT` Kernel
2688cc0b97SApple OSS Distributions
2788cc0b97SApple OSS DistributionsThe xnu make system can build kernel based on `KERNEL_CONFIGS` & `ARCH_CONFIGS` variables as arguments.
2888cc0b97SApple OSS DistributionsHere is the syntax:
2988cc0b97SApple OSS Distributions
3094d3b452SApple OSS Distributions```text
3188cc0b97SApple OSS Distributionsmake SDKROOT=<sdkroot> ARCH_CONFIGS=<arch> KERNEL_CONFIGS=<variant>
3294d3b452SApple OSS Distributions```
3388cc0b97SApple OSS Distributions
3488cc0b97SApple OSS DistributionsWhere:
3588cc0b97SApple OSS Distributions
3694d3b452SApple OSS Distributions* `<sdkroot>`: path to macOS SDK on disk. (defaults to `/`)
3794d3b452SApple OSS Distributions* `<variant>`: can be `debug`, `development`, `release`, `profile` and configures compilation flags and asserts throughout kernel code.
3894d3b452SApple OSS Distributions* `<arch>`: can be valid arch to build for. (E.g. `X86_64`)
3988cc0b97SApple OSS Distributions
4088cc0b97SApple OSS DistributionsTo build a kernel for the same architecture as running OS, just type
4188cc0b97SApple OSS Distributions
4294d3b452SApple OSS Distributions```text
4394d3b452SApple OSS Distributionsmake SDKROOT=macosx.internal
4494d3b452SApple OSS Distributions```
4588cc0b97SApple OSS Distributions
4688cc0b97SApple OSS DistributionsAdditionally, there is support for configuring architectures through `ARCH_CONFIGS` and kernel configurations with `KERNEL_CONFIGS`.
4788cc0b97SApple OSS Distributions
4894d3b452SApple OSS Distributions```text
4994d3b452SApple OSS Distributionsmake SDKROOT=macosx.internal ARCH_CONFIGS=X86_64 KERNEL_CONFIGS=DEVELOPMENT
5094d3b452SApple OSS Distributionsmake SDKROOT=macosx.internal ARCH_CONFIGS=X86_64 KERNEL_CONFIGS="RELEASE DEVELOPMENT DEBUG"
5194d3b452SApple OSS Distributions```
5288cc0b97SApple OSS Distributions
5394d3b452SApple 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`.
5488cc0b97SApple OSS Distributions
5588cc0b97SApple OSS DistributionsThis will also create a bootable image, kernel.[config],  and a kernel binary
5688cc0b97SApple OSS Distributionswith symbols, kernel.[config].unstripped.
5788cc0b97SApple OSS Distributions
5894d3b452SApple OSS DistributionsTo install the kernel into a DSTROOT, use the `install_kernels` target:
59bb611c8fSApple OSS Distributions
6094d3b452SApple OSS Distributions```text
6194d3b452SApple OSS Distributionsmake install_kernels DSTROOT=/tmp/xnu-dst
6294d3b452SApple OSS Distributions```
63bb611c8fSApple OSS Distributions
64bb611c8fSApple OSS DistributionsFor a more satisfying kernel debugging experience, with access to all
65bb611c8fSApple OSS Distributionslocal variables and arguments, but without all the extra check of the
6694d3b452SApple OSS DistributionsDEBUG kernel, add something like the following to your make command:
6794d3b452SApple OSS Distributions
6894d3b452SApple OSS Distributions```text
69bb611c8fSApple OSS DistributionsCFLAGS_DEVELOPMENTARM64="-O0 -g -DKERNEL_STACK_MULTIPLIER=2"
70bb611c8fSApple OSS DistributionsCXXFLAGS_DEVELOPMENTARM64="-O0 -g -DKERNEL_STACK_MULTIPLIER=2"
7194d3b452SApple OSS Distributions```
7294d3b452SApple OSS Distributions
7394d3b452SApple OSS DistributionsRemember to replace `DEVELOPMENT` and `ARM64` with the appropriate build and platform.
7494d3b452SApple OSS Distributions
7594d3b452SApple 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.
7694d3b452SApple OSS Distributions>
7794d3b452SApple OSS Distributions> This setting allows you to e.g. selectively turn on debugging code that is guarded by a preprocessor macro. Example usage...
7894d3b452SApple OSS Distributions>
7994d3b452SApple OSS Distributions> ```text
8094d3b452SApple OSS Distributions> make SDKROOT=macosx.internal PRODUCT_CONFIGS=j314s
8194d3b452SApple OSS Distributions> EXTRA_CFLAGS='-DKERNEL_STACK_MULTIPLIER=2'
8294d3b452SApple OSS Distributions> ```
83bb611c8fSApple OSS Distributions
8488cc0b97SApple OSS Distributions
8588cc0b97SApple OSS Distributions* To build with RELEASE kernel configuration
8688cc0b97SApple OSS Distributions
8794d3b452SApple OSS Distributions    ```text
8888cc0b97SApple OSS Distributions    make KERNEL_CONFIGS=RELEASE SDKROOT=/path/to/SDK
8994d3b452SApple OSS Distributions    ```
9088cc0b97SApple OSS Distributions
9194d3b452SApple OSS Distributions### Building FAT Kernel Binary
9288cc0b97SApple OSS Distributions
9388cc0b97SApple OSS DistributionsDefine architectures in your environment or when running a make command.
9488cc0b97SApple OSS Distributions
9594d3b452SApple OSS Distributions```text
9694d3b452SApple OSS Distributionsmake ARCH_CONFIGS="X86_64" exporthdrs all
9794d3b452SApple OSS Distributions```
9888cc0b97SApple OSS Distributions
99*8d741a5dSApple OSS Distributions
100*8d741a5dSApple OSS Distributions
10194d3b452SApple OSS Distributions### Other Makefile Options
10288cc0b97SApple OSS Distributions
10388cc0b97SApple OSS Distributions* $ make MAKEJOBS=-j8    # this will use 8 processes during the build. The default is 2x the number of active CPUS.
10488cc0b97SApple OSS Distributions* $ make -j8             # the standard command-line option is also accepted
10588cc0b97SApple OSS Distributions* $ make -w              # trace recursive make invocations. Useful in combination with VERBOSE=YES
10688cc0b97SApple OSS Distributions* $ make BUILD_LTO=0     # build without LLVM Link Time Optimization
1075c2921b0SApple OSS Distributions* $ make BOUND_CHECKS=0  # disable -fbound-attributes for this build
10888cc0b97SApple OSS Distributions* $ make REMOTEBUILD=user@remotehost # perform build on remote host
109*8d741a5dSApple OSS Distributions* $ make BUILD_CODE_COVERAGE=1 # build with support for collecting code coverage information
11088cc0b97SApple OSS Distributions
11176e12aa3SApple OSS DistributionsThe XNU build system can optionally output color-formatted build output. To enable this, you can either
11276e12aa3SApple OSS Distributionsset the `XNU_LOGCOLORS` environment variable to `y`, or you can pass `LOGCOLORS=y` to the make command.
11388cc0b97SApple OSS Distributions
11494d3b452SApple OSS Distributions### Customize the XNU Version
1151031c584SApple OSS Distributions
1161031c584SApple OSS DistributionsThe xnu version is derived from the SDK or KDK by reading the `CFBundleVersion`
1171031c584SApple OSS Distributionsof their `System/Library/Extensions/System.kext/Info.plist` file.
1181031c584SApple OSS DistributionsThis can be customized by setting the `RC_DARWIN_KERNEL_VERSION` variable in
1191031c584SApple OSS Distributionsthe environment or on the `make` command line.
1201031c584SApple OSS Distributions
1211031c584SApple OSS Distributions
1221031c584SApple OSS DistributionsSee doc/xnu_version.md for more details.
12388cc0b97SApple OSS Distributions
12494d3b452SApple OSS Distributions### Debug Information Formats
12588cc0b97SApple OSS Distributions
12688cc0b97SApple OSS DistributionsBy default, a DWARF debug information repository is created during the install phase; this is a "bundle" named kernel.development.\<variant>.dSYM
12788cc0b97SApple 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.
12888cc0b97SApple OSS Distributions
12994d3b452SApple OSS Distributions```sh
13094d3b452SApple OSS Distributionsexport BUILD_STABS=1
13194d3b452SApple OSS Distributionsmake
13294d3b452SApple OSS Distributions```
13388cc0b97SApple OSS Distributions
13488cc0b97SApple OSS Distributions
13594d3b452SApple OSS Distributions## Building KernelCaches
13688cc0b97SApple OSS Distributions
13788cc0b97SApple OSS DistributionsTo test the xnu kernel, you need to build a kernelcache that links the kexts and
13888cc0b97SApple OSS Distributionskernel together into a single bootable image.
13988cc0b97SApple OSS DistributionsTo build a kernelcache you can use the following mechanisms:
14088cc0b97SApple OSS Distributions
14188cc0b97SApple OSS Distributions* Using automatic kernelcache generation with `kextd`.
14288cc0b97SApple OSS Distributions  The kextd daemon keeps watching for changing in `/System/Library/Extensions` directory.
14388cc0b97SApple OSS Distributions  So you can setup new kernel as
14488cc0b97SApple OSS Distributions
14594d3b452SApple OSS Distributions    ```text
14694d3b452SApple OSS Distributions    cp BUILD/obj/DEVELOPMENT/X86_64/kernel.development /System/Library/Kernels/
14794d3b452SApple OSS Distributions    touch /System/Library/Extensions
14894d3b452SApple OSS Distributions    ps -e | grep kextd
14994d3b452SApple OSS Distributions    ```
15088cc0b97SApple OSS Distributions
15188cc0b97SApple OSS Distributions* Manually invoking `kextcache` to build new kernelcache.
15288cc0b97SApple OSS Distributions
15394d3b452SApple OSS Distributions    ```text
15494d3b452SApple OSS Distributions    kextcache -q -z -a x86_64 -l -n -c /var/tmp/kernelcache.test -K /var/tmp/kernel.test /System/Library/Extensions
15594d3b452SApple OSS Distributions    ```
15688cc0b97SApple OSS Distributions
15788cc0b97SApple OSS Distributions
15894d3b452SApple OSS Distributions## Booting a KernelCache on a Target machine
15988cc0b97SApple OSS Distributions
16088cc0b97SApple 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.
16188cc0b97SApple OSS DistributionsFollowing are the steps to get such a setup:
16288cc0b97SApple OSS Distributions
16388cc0b97SApple OSS Distributions1. Create kernel cache using the kextcache command as `/kernelcache.test`
16488cc0b97SApple OSS Distributions2. Copy exiting boot configurations to alternate file
16588cc0b97SApple OSS Distributions
16694d3b452SApple OSS Distributions    ```sh
16794d3b452SApple OSS Distributions    cp /Library/Preferences/SystemConfiguration/com.apple.Boot.plist /next_boot.plist
16894d3b452SApple OSS Distributions    ```
16988cc0b97SApple OSS Distributions
17088cc0b97SApple OSS Distributions3. Update the kernelcache and boot-args for your setup
17188cc0b97SApple OSS Distributions
17294d3b452SApple OSS Distributions    ```sh
17394d3b452SApple OSS Distributions    plutil -insert "Kernel Cache" -string "kernelcache.test" /next_boot.plist
17494d3b452SApple OSS Distributions    plutil -replace "Kernel Flags" -string "debug=0x144 -v kernelsuffix=test " /next_boot.plist
17594d3b452SApple OSS Distributions    ```
17688cc0b97SApple OSS Distributions
17788cc0b97SApple OSS Distributions4. Copy the new config to `/Library/Preferences/SystemConfiguration/`
17888cc0b97SApple OSS Distributions
17994d3b452SApple OSS Distributions    ```sh
18094d3b452SApple OSS Distributions    cp /next_boot.plist /Library/Preferences/SystemConfiguration/boot.plist
18194d3b452SApple OSS Distributions    ```
18288cc0b97SApple OSS Distributions
18388cc0b97SApple OSS Distributions5. Bless the volume with new configs.
18488cc0b97SApple OSS Distributions
18594d3b452SApple OSS Distributions    ```text
18694d3b452SApple OSS Distributions    sudo -n bless  --mount / --setBoot --nextonly --options "config=boot"
18794d3b452SApple OSS Distributions    ```
18888cc0b97SApple OSS Distributions
18988cc0b97SApple OSS Distributions   The `--nextonly` flag specifies that use the `boot.plist` configs only for one boot.
19088cc0b97SApple OSS Distributions   So if the kernel panic's you can easily power reboot and recover back to original kernel.
19188cc0b97SApple OSS Distributions
19288cc0b97SApple OSS Distributions
19394d3b452SApple OSS Distributions## Creating tags and cscope
19488cc0b97SApple OSS Distributions
19588cc0b97SApple OSS DistributionsSet up your build environment and from the top directory, run:
19688cc0b97SApple OSS Distributions
19794d3b452SApple OSS Distributions    make tags     # this will build ctags and etags on a case-sensitive volume, only ctags on case-insensitive
19894d3b452SApple OSS Distributions    make TAGS     # this will build etags
19994d3b452SApple OSS Distributions    make cscope   # this will build cscope database
20088cc0b97SApple OSS Distributions
20194d3b452SApple OSS Distributions## Installing New Header Files from XNU
20288cc0b97SApple OSS Distributions
20388cc0b97SApple OSS DistributionsXNU installs header files at the following locations -
20488cc0b97SApple OSS Distributions
20588cc0b97SApple OSS Distributions    a. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
20688cc0b97SApple OSS Distributions    b. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
20788cc0b97SApple OSS Distributions    c. $(DSTROOT)/usr/include/
208e6231be0SApple OSS Distributions    d. $(DSTROOT)/usr/local/include/
209e6231be0SApple OSS Distributions    e. $(DSTROOT)/System/DriverKit/usr/include/
2105c2921b0SApple OSS Distributions    f. $(DSTROOT)/System/Library/Frameworks/IOKit.framework/Headers
2115c2921b0SApple OSS Distributions    g. $(DSTROOT)/System/Library/Frameworks/IOKit.framework/PrivateHeaders
2125c2921b0SApple OSS Distributions    h. $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders
21388cc0b97SApple OSS Distributions
21488cc0b97SApple OSS Distributions`Kernel.framework` is used by kernel extensions.\
215e6231be0SApple OSS DistributionsThe `System.framework`, `/usr/include` and `/usr/local/include` are used by user level applications. \
2165c2921b0SApple OSS Distributions`IOKit.framework` is used by IOKit userspace clients. \
217a5e72196SApple OSS Distributions`/System/DriverKit/usr/include` is used by userspace drivers. \
21888cc0b97SApple OSS DistributionsThe header files in framework's `PrivateHeaders` are only available for **Apple Internal Development**.
21988cc0b97SApple OSS Distributions
22088cc0b97SApple OSS DistributionsThe directory containing the header file should have a Makefile that
22188cc0b97SApple OSS Distributionscreates the list of files that should be installed at different locations.
222cc9a6355SApple OSS DistributionsIf you are adding the first header file in a directory, you will need to
223cc9a6355SApple OSS Distributionscreate Makefile similar to `xnu/bsd/sys/Makefile`.
22488cc0b97SApple OSS Distributions
22588cc0b97SApple OSS DistributionsAdd your header file to the correct file list depending on where you want
22688cc0b97SApple OSS Distributionsto install it. The default locations where the header files are installed
22788cc0b97SApple OSS Distributionsfrom each file list are -
22888cc0b97SApple OSS Distributions
22988cc0b97SApple OSS Distributions    a. `DATAFILES` : To make header file available in user level -
23088cc0b97SApple OSS Distributions       `$(DSTROOT)/usr/include`
231e6231be0SApple OSS Distributions       `$(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders`
23288cc0b97SApple OSS Distributions
233a5e72196SApple OSS Distributions    b. `DRIVERKIT_DATAFILES` : To make header file available to DriverKit userspace drivers -
234a5e72196SApple OSS Distributions       `$(DSTROOT)/System/DriverKit/usr/include`
235a5e72196SApple OSS Distributions
236a5e72196SApple OSS Distributions    c. `PRIVATE_DATAFILES` : To make header file available to Apple internal in
23788cc0b97SApple OSS Distributions       user level -
23888cc0b97SApple OSS Distributions       `$(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders`
23988cc0b97SApple OSS Distributions
240e6231be0SApple OSS Distributions    d. `EMBEDDED_PRIVATE_DATAFILES` : To make header file available in user
241e6231be0SApple OSS Distributions       level for macOS as `EXTRA_DATAFILES`, but Apple internal in user level
242e6231be0SApple OSS Distributions       for embedded OSes as `EXTRA_PRIVATE_DATAFILES` -
243e6231be0SApple OSS Distributions       `$(DSTROOT)/usr/include` (`EXTRA_DATAFILES`)
244e6231be0SApple OSS Distributions       `$(DSTROOT)/usr/local/include` (`EXTRA_PRIVATE_DATAFILES`)
245e6231be0SApple OSS Distributions
2461031c584SApple OSS Distributions    e. `KERNELFILES` : To make header file available in kernel level -
24788cc0b97SApple OSS Distributions       `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers`
24888cc0b97SApple OSS Distributions       `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders`
24988cc0b97SApple OSS Distributions
2501031c584SApple OSS Distributions    f. `PRIVATE_KERNELFILES` : To make header file available to Apple internal
25188cc0b97SApple OSS Distributions       for kernel extensions -
25288cc0b97SApple OSS Distributions       `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders`
25388cc0b97SApple OSS Distributions
2541031c584SApple OSS Distributions    g. `MODULEMAPFILES` : To make module map file available in user level -
255e6231be0SApple OSS Distributions       `$(DSTROOT)/usr/include`
256e6231be0SApple OSS Distributions
2571031c584SApple OSS Distributions    h. `PRIVATE_MODULEMAPFILES` : To make module map file available to Apple
258e6231be0SApple OSS Distributions       internal in user level -
259e6231be0SApple OSS Distributions       `$(DSTROOT)/usr/local/include`
260e6231be0SApple OSS Distributions
2611031c584SApple OSS Distributions    i. `LIBCXX_DATAFILES` : To make header file available to in-kernel libcxx clients:
2621031c584SApple OSS Distributions       `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders/kernel_sdkroot`
2631031c584SApple OSS Distributions
2641031c584SApple OSS Distributions    j. `EXCLAVEKIT_DATAFILES` : To make header file available to Apple internal
2651031c584SApple OSS Distributions       ExclaveKit SDK -
2661031c584SApple OSS Distributions       `$(DSTROOT)/System/ExclaveKit/usr/include`
2671031c584SApple OSS Distributions
2681031c584SApple OSS Distributions    k. `EXCLAVECORE_DATAFILES` : To make header file available to Apple internal
2691031c584SApple OSS Distributions       ExclaveCore SDK -
2701031c584SApple OSS Distributions       `$(DSTROOT)/System/ExclaveCore/usr/include`
2711031c584SApple OSS Distributions
27288cc0b97SApple OSS DistributionsThe Makefile combines the file lists mentioned above into different
273cc9a6355SApple OSS Distributionsinstall lists which are used by build system to install the header files. There
274cc9a6355SApple OSS Distributionsare two types of install lists: machine-dependent and machine-independent.
275cc9a6355SApple OSS DistributionsThese lists are indicated by the presence of `MD` and `MI` in the build
276cc9a6355SApple OSS Distributionssetting, respectively. If your header is architecture-specific, then you should
277cc9a6355SApple OSS Distributionsuse a machine-dependent install list (e.g. `INSTALL_MD_LIST`). If your header
278cc9a6355SApple OSS Distributionsshould be installed for all architectures, then you should use a
279cc9a6355SApple OSS Distributionsmachine-independent install list (e.g. `INSTALL_MI_LIST`).
28088cc0b97SApple OSS Distributions
28188cc0b97SApple OSS DistributionsIf the install list that you are interested does not exist, create it
28288cc0b97SApple OSS Distributionsby adding the appropriate file lists.  The default install lists, its
28388cc0b97SApple OSS Distributionsmember file lists and their default location are described below -
28488cc0b97SApple OSS Distributions
285e6231be0SApple OSS Distributionsa. `INSTALL_MI_LIST`, `INSTALL_MODULEMAP_MI_LIST` : Installs header and module map
286e6231be0SApple OSS Distributions    files to a location that is available to everyone in user level.
28788cc0b97SApple OSS Distributions    Locations -
28888cc0b97SApple OSS Distributions        $(DSTROOT)/usr/include
28988cc0b97SApple OSS Distributions    Definition -
29088cc0b97SApple OSS Distributions        INSTALL_MI_LIST = ${DATAFILES}
291e6231be0SApple OSS Distributions        INSTALL_MODULEMAP_MI_LIST = ${MODULEMAPFILES}
29288cc0b97SApple OSS Distributions
293a5e72196SApple OSS Distributionsb. `INSTALL_DRIVERKIT_MI_LIST` : Installs header file to a location that is
294a5e72196SApple OSS Distributions    available to DriverKit userspace drivers.
295a5e72196SApple OSS Distributions    Locations -
296a5e72196SApple OSS Distributions        $(DSTROOT)/System/DriverKit/usr/include
297a5e72196SApple OSS Distributions    Definition -
298a5e72196SApple OSS Distributions        INSTALL_DRIVERKIT_MI_LIST = ${DRIVERKIT_DATAFILES}
299a5e72196SApple OSS Distributions
300e6231be0SApple OSS Distributionsc.  `INSTALL_MI_LCL_LIST`, `INSTALL_MODULEMAP_MI_LCL_LIST` : Installs header and
301e6231be0SApple OSS Distributions    module map files to a location that is available for Apple internal in user level.
302e6231be0SApple OSS Distributions    Locations -
303e6231be0SApple OSS Distributions        $(DSTROOT)/usr/local/include
304e6231be0SApple OSS Distributions    Definition -
305e6231be0SApple OSS Distributions        INSTALL_MI_LCL_LIST =
306e6231be0SApple OSS Distributions        INSTALL_MODULEMAP_MI_LCL_LIST = ${PRIVATE_MODULEMAPFILES}
307e6231be0SApple OSS Distributions
3085c2921b0SApple OSS Distributionsd. `INSTALL_IF_MI_LIST` : Installs header file to location that is available
3095c2921b0SApple OSS Distributions    to everyone for IOKit userspace clients.
3105c2921b0SApple OSS Distributions    Locations -
3115c2921b0SApple OSS Distributions        $(DSTROOT)/System/Library/Frameworks/IOKit.framework/Headers
3125c2921b0SApple OSS Distributions    Definition -
3135c2921b0SApple OSS Distributions        INSTALL_IF_MI_LIST = ${DATAFILES}
3145c2921b0SApple OSS Distributions
3155c2921b0SApple OSS Distributionse. `INSTALL_IF_MI_LCL_LIST` : Installs header file to location that is
3165c2921b0SApple OSS Distributions    available to Apple internal for IOKit userspace clients.
3175c2921b0SApple OSS Distributions    Locations -
3185c2921b0SApple OSS Distributions        $(DSTROOT)/System/Library/Frameworks/IOKit.framework/PrivateHeaders
3195c2921b0SApple OSS Distributions    Definition -
3205c2921b0SApple OSS Distributions        INSTALL_IF_MI_LCL_LIST = ${DATAFILES} ${PRIVATE_DATAFILES}
3215c2921b0SApple OSS Distributions
3225c2921b0SApple OSS Distributionsf.  `INSTALL_SF_MI_LCL_LIST` : Installs header file to a location that is available
32388cc0b97SApple OSS Distributions    for Apple internal in user level.
32488cc0b97SApple OSS Distributions    Locations -
32588cc0b97SApple OSS Distributions        $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders
32688cc0b97SApple OSS Distributions    Definition -
327e6231be0SApple OSS Distributions        INSTALL_SF_MI_LCL_LIST = ${DATAFILES} ${PRIVATE_DATAFILES}
32888cc0b97SApple OSS Distributions
3295c2921b0SApple OSS Distributionsg. `INSTALL_KF_MI_LIST` : Installs header file to location that is available
33088cc0b97SApple OSS Distributions    to everyone for kernel extensions.
33188cc0b97SApple OSS Distributions    Locations -
33288cc0b97SApple OSS Distributions        $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
33388cc0b97SApple OSS Distributions    Definition -
33488cc0b97SApple OSS Distributions        INSTALL_KF_MI_LIST = ${KERNELFILES}
33588cc0b97SApple OSS Distributions
3365c2921b0SApple OSS Distributionsh. `INSTALL_KF_MI_LCL_LIST` : Installs header file to location that is
33788cc0b97SApple OSS Distributions    available for Apple internal for kernel extensions.
33888cc0b97SApple OSS Distributions    Locations -
33988cc0b97SApple OSS Distributions        $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
34088cc0b97SApple OSS Distributions    Definition -
34188cc0b97SApple OSS Distributions        INSTALL_KF_MI_LCL_LIST = ${KERNELFILES} ${PRIVATE_KERNELFILES}
34288cc0b97SApple OSS Distributions
3435c2921b0SApple OSS Distributionsi. `EXPORT_MI_LIST` : Exports header file to all of xnu (bsd/, osfmk/, etc.)
34476e12aa3SApple OSS Distributions    for compilation only. Does not install anything into the SDK.
34576e12aa3SApple OSS Distributions    Definition -
34676e12aa3SApple OSS Distributions        EXPORT_MI_LIST = ${KERNELFILES} ${PRIVATE_KERNELFILES}
34776e12aa3SApple OSS Distributions
3481031c584SApple OSS Distributionsj. `INSTALL_KF_LIBCXX_MI_LIST` : Installs header file for in-kernel libc++ support.
3491031c584SApple OSS Distributions    Locations -
3501031c584SApple OSS Distributions        $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders/kernel_sdkroot
3511031c584SApple OSS Distributions    Definition -
3521031c584SApple OSS Distributions        INSTALL_KF_LIBCXX_MI_LIST = ${LIBCXX_DATAFILES}
3531031c584SApple OSS Distributions
3541031c584SApple OSS Distributionsk. `INSTALL_EXCLAVEKIT_MI_LIST` : Installs header file to location that is
3551031c584SApple OSS Distributions    available for Apple internal for ExclaveKit.
3561031c584SApple OSS Distributions    Locations -
3571031c584SApple OSS Distributions        $(DSTROOT)/System/ExclaveKit/usr/include
3581031c584SApple OSS Distributions    Definition -
3591031c584SApple OSS Distributions        INSTALL_EXCLAVEKIT_MI_LIST = ${EXCLAVEKIT_DATAFILES}
3601031c584SApple OSS Distributions
3611031c584SApple OSS Distributionsl. `INSTALL_EXCLAVECORE_MI_LIST` : Installs header file to location that is
3621031c584SApple OSS Distributions    available for Apple internal for ExclaveCore.
3631031c584SApple OSS Distributions    Locations -
3641031c584SApple OSS Distributions        $(DSTROOT)/System/ExclaveCore/usr/include
3651031c584SApple OSS Distributions    Definition -
3661031c584SApple OSS Distributions        INSTALL_EXCLAVECORE_MI_LIST = ${EXCLAVECORE_DATAFILES}
3671031c584SApple OSS Distributions
36888cc0b97SApple OSS DistributionsIf you want to install the header file in a sub-directory of the paths
36988cc0b97SApple OSS Distributionsdescribed in (1), specify the directory name using two variables
37088cc0b97SApple OSS Distributions`INSTALL_MI_DIR` and `EXPORT_MI_DIR` as follows -
37188cc0b97SApple OSS Distributions
37294d3b452SApple OSS Distributions```text
37388cc0b97SApple OSS DistributionsINSTALL_MI_DIR = dirname
37488cc0b97SApple OSS DistributionsEXPORT_MI_DIR = dirname
37594d3b452SApple OSS Distributions```
37688cc0b97SApple OSS Distributions
377e6231be0SApple OSS DistributionsIf you want to install the module map file in a sub-directory, specify the
378e6231be0SApple OSS Distributionsdirectory name using the variable `INSTALL_MODULEMAP_MI_DIR` as follows -
379e6231be0SApple OSS Distributions
38094d3b452SApple OSS Distributions```text
381e6231be0SApple OSS DistributionsINSTALL_MODULEMAP_MI_DIR = dirname
38294d3b452SApple OSS Distributions```
383e6231be0SApple OSS Distributions
38488cc0b97SApple OSS DistributionsA single header file can exist at different locations using the steps
38588cc0b97SApple OSS Distributionsmentioned above.  However it might not be desirable to make all the code
38688cc0b97SApple OSS Distributionsin the header file available at all the locations.  For example, you
38788cc0b97SApple OSS Distributionswant to export a function only to kernel level but not user level.
38888cc0b97SApple OSS Distributions
38988cc0b97SApple OSS Distributions You can use C language's pre-processor directive (#ifdef, #endif, #ifndef)
39088cc0b97SApple OSS Distributions to control the text generated before a header file is installed.  The kernel
39188cc0b97SApple OSS Distributions only includes the code if the conditional macro is TRUE and strips out
39288cc0b97SApple OSS Distributions code for FALSE conditions from the header file.
39388cc0b97SApple OSS Distributions
39488cc0b97SApple OSS Distributions Some pre-defined macros and their descriptions are -
39588cc0b97SApple OSS Distributions
39694d3b452SApple OSS Distributions1. `PRIVATE` : If defined, enclosed definitions are considered System
397cc9a6355SApple OSS DistributionsPrivate Interfaces. These are visible within xnu and
398cc9a6355SApple OSS Distributionsexposed in user/kernel headers installed within the AppleInternal
399cc9a6355SApple OSS Distributions"PrivateHeaders" sections of the System and Kernel frameworks.
40094d3b452SApple OSS Distributions2. `KERNEL_PRIVATE` : If defined, enclosed code is available to all of xnu
401cc9a6355SApple OSS Distributionskernel and Apple internal kernel extensions and omitted from user
402cc9a6355SApple OSS Distributionsheaders.
40394d3b452SApple OSS Distributions3. `BSD_KERNEL_PRIVATE` : If defined, enclosed code is visible exclusively
404cc9a6355SApple OSS Distributionswithin the xnu/bsd module.
40594d3b452SApple OSS Distributions4. `MACH_KERNEL_PRIVATE`: If defined, enclosed code is visible exclusively
406cc9a6355SApple OSS Distributionswithin the xnu/osfmk module.
40794d3b452SApple OSS Distributions5. `XNU_KERNEL_PRIVATE`: If defined, enclosed code is visible exclusively
408cc9a6355SApple OSS Distributionswithin xnu.
40994d3b452SApple OSS Distributions6. `KERNEL` :  If defined, enclosed code is available within xnu and kernel
410cc9a6355SApple OSS Distributions    extensions and is not visible in user level header files.  Only the
41188cc0b97SApple OSS Distributions    header files installed in following paths will have the code -
41288cc0b97SApple OSS Distributions
41394d3b452SApple OSS Distributions    ```text
41488cc0b97SApple OSS Distributions    $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
41588cc0b97SApple OSS Distributions    $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
41694d3b452SApple OSS Distributions    ```
41794d3b452SApple OSS Distributions
41894d3b452SApple OSS Distributions7. `DRIVERKIT`: If defined, enclosed code is visible exclusively in the
419a5e72196SApple OSS DistributionsDriverKit SDK headers used by userspace drivers.
42094d3b452SApple OSS Distributions8. `EXCLAVEKIT`: If defined, enclosed code is visible exclusively in the
4211031c584SApple OSS DistributionsExclaveKit SDK headers.
42294d3b452SApple OSS Distributions9. `EXCLAVECORE`: If defined, enclosed code is visible exclusively in the
4231031c584SApple OSS DistributionsExclaveCore SDK headers.
42488cc0b97SApple OSS Distributions
425*8d741a5dSApple OSS Distributions## VM header file name convention
426*8d741a5dSApple OSS DistributionsThe VM headers follow the following naming conventions:
427*8d741a5dSApple OSS Distributions* `*_internal.h` headers contain components of the VM subsystem only for use by VM code.
428*8d741a5dSApple OSS Distributions* `*_xnu.h` headers contain components of the VM subsystem only for use by other xnu code.
429*8d741a5dSApple OSS Distributions* `*.h` headers contain components of the VM subsystem exported to kexts.
430*8d741a5dSApple OSS Distributions* `vm_iokit.h` header contains components of the VM subsystem exported to the iokit subsystem.
431*8d741a5dSApple OSS Distributions* `vm_ubc.h` header contains components of the VM subsystem exported to the ubc subsystem.
432*8d741a5dSApple OSS Distributions
433*8d741a5dSApple OSS Distributions
43494d3b452SApple OSS Distributions## Module map file name convention
435e6231be0SApple OSS Distributions
436e6231be0SApple OSS DistributionsIn the simple case, a subdirectory of `usr/include` or `usr/local/include`
437e6231be0SApple OSS Distributionscan be represented by a standalone module. Where this is the case, set
438e6231be0SApple OSS Distributions`INSTALL_MODULEMAP_MI_DIR` to `INSTALL_MI_DIR` and install a `module.modulemap`
439e6231be0SApple OSS Distributionsfile there. `module.modulemap` is used even for private modules in
440e6231be0SApple OSS Distributions`usr/local/include`; `module.private.modulemap` is not used. Caveat: in order
441e6231be0SApple OSS Distributionsto stay in the simple case, the module name needs to be exactly the same as
442e6231be0SApple OSS Distributionsthe directory name. If that's not possible, then the following method will
443e6231be0SApple OSS Distributionsneed to be applied.
444e6231be0SApple OSS Distributions
445e6231be0SApple OSS Distributions`xnu` contributes to the modules defined in CoreOSModuleMaps by installing
446e6231be0SApple OSS Distributionsmodule map files that are sourced from `usr/include/module.modulemap` and
447e6231be0SApple OSS Distributions`usr/local/include/module.modulemap`. The naming convention for the `xnu`
448e6231be0SApple OSS Distributionsmodule map files are as follows.
449e6231be0SApple OSS Distributions
450e6231be0SApple OSS Distributionsa. Ideally the module map file covers an entire directory. A module map
451e6231be0SApple OSS Distributions    file covering `usr/include/a/b/c` would be named `a_b_c.modulemap`.
452e6231be0SApple OSS Distributions    `usr/local/include/a/b/c` would be `a_b_c_private.modulemap`.
453e6231be0SApple OSS Distributionsb. Some headers are special and require their own module. In that case,
454e6231be0SApple OSS Distributions    the module map file would be named after the module it defines.
455e6231be0SApple OSS Distributions    A module map file defining the module `One.Two.Three` would be named
456e6231be0SApple OSS Distributions    `one_two_three.modulemap`.
457e6231be0SApple OSS Distributions
45894d3b452SApple OSS Distributions## Conditional Compilation
45988cc0b97SApple OSS Distributions
460cc9a6355SApple OSS Distributions`xnu` offers the following mechanisms for conditionally compiling code:
461cc9a6355SApple OSS Distributions
46294d3b452SApple OSS Distributions1. *CPU Characteristics* If the code you are guarding has specific
463cc9a6355SApple OSS Distributions    characterstics that will vary only based on the CPU architecture being
464cc9a6355SApple OSS Distributions    targeted, use this option. Prefer checking for features of the
465cc9a6355SApple OSS Distributions    architecture (e.g. `__LP64__`, `__LITTLE_ENDIAN__`, etc.).
46694d3b452SApple OSS Distributions2. *New Features* If the code you are guarding, when taken together,
467cc9a6355SApple OSS Distributions    implements a feature, you should define a new feature in `config/MASTER`
468cc9a6355SApple OSS Distributions    and use the resulting `CONFIG` preprocessor token (e.g. for a feature
469cc9a6355SApple OSS Distributions    named `config_virtual_memory`, check for `#if CONFIG_VIRTUAL_MEMORY`).
470cc9a6355SApple OSS Distributions    This practice ensures that existing features may be brought to other
471cc9a6355SApple OSS Distributions    platforms by simply changing a feature switch.
47294d3b452SApple OSS Distributions3. *Existing Features* You can use existing features if your code is
473cc9a6355SApple OSS Distributions    strongly tied to them (e.g. use `SECURE_KERNEL` if your code implements
474cc9a6355SApple OSS Distributions    new functionality that is exclusively relevant to the trusted kernel and
475cc9a6355SApple OSS Distributions    updates the definition/understanding of what being a trusted kernel means).
476cc9a6355SApple OSS Distributions
477cc9a6355SApple OSS DistributionsIt is recommended that you avoid compiling based on the target platform. `xnu`
478cc9a6355SApple OSS Distributionsdoes not define the platform macros from `TargetConditionals.h`
479cc9a6355SApple OSS Distributions(`TARGET_OS_OSX`, `TARGET_OS_IOS`, etc.).
480cc9a6355SApple OSS Distributions
481cc9a6355SApple OSS Distributions
48294d3b452SApple OSS Distributions## Debugging XNU
48388cc0b97SApple OSS Distributions
484e6231be0SApple OSS DistributionsBy default, the kernel reboots in the event of a panic.
485e6231be0SApple OSS DistributionsThis behavior can be overriden by the `debug` boot-arg -- `debug=0x14e` will cause a panic to wait for a debugger to attach.
486e6231be0SApple 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.
487e6231be0SApple OSS DistributionsEthernet, Thunderbolt, and serial debugging are supported, depending on the hardware.
48888cc0b97SApple OSS Distributions
489e6231be0SApple OSS DistributionsUse LLDB to debug the kernel:
49088cc0b97SApple OSS Distributions
49194d3b452SApple OSS Distributions```text
49294d3b452SApple OSS Distributionsxcrun -sdk macosx lldb <path-to-unstripped-kernel>
493e6231be0SApple OSS Distributions(lldb) gdb-remote [<host-ip>:]<port>
49494d3b452SApple OSS Distributions```
49588cc0b97SApple OSS Distributions
496e6231be0SApple OSS DistributionsThe debug info for the kernel (dSYM) comes with a set of macros to support kernel debugging.
497e6231be0SApple OSS DistributionsTo load these macros automatically when attaching to the kernel, add the following to `~/.lldbinit`:
49888cc0b97SApple OSS Distributions
49994d3b452SApple OSS Distributions```text
50088cc0b97SApple OSS Distributionssettings set target.load-script-from-symbol-file true
50194d3b452SApple OSS Distributions```
50288cc0b97SApple OSS Distributions
503e6231be0SApple OSS Distributions`tools/lldbmacros` contains the source for these commands.
504e6231be0SApple OSS DistributionsSee the README in that directory for their usage, or use the built-in LLDB help with:
50588cc0b97SApple OSS Distributions
50694d3b452SApple OSS Distributions```text
507e6231be0SApple OSS Distributions(lldb) help showcurrentstacks
50894d3b452SApple OSS Distributions```
5095c2921b0SApple OSS Distributions
510