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