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