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