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