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