]> git.saurik.com Git - apple/xnu.git/blob - README.md
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / README.md
1 What is XNU?
2 ===========
3
4 XNU 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 XNU 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 XNU runs on x86_64 for both single processor and multi-processor configurations.
7
8 XNU Source Tree
9 ===============
10
11 * `config` - configurations for exported apis for supported architecture and platform
12 * `SETUP` - Basic set of tools used for configuring the kernel, versioning and kextsymbol management.
13 * `EXTERNAL_HEADERS` - Headers sourced from other projects to avoid dependency cycles when building. These headers should be regularly synced when source is updated.
14 * `libkern` - C++ IOKit library code for handling of drivers and kexts.
15 * `libsa` - kernel bootstrap code for startup
16 * `libsyscall` - syscall library interface for userspace programs
17 * `libkdd` - source for user library for parsing kernel data like kernel chunked data.
18 * `makedefs` - top level rules and defines for kernel build.
19 * `osfmk` - Mach kernel based subsystems
20 * `pexpert` - Platform specific code like interrupt handling, atomics etc.
21 * `security` - Mandatory Access Check policy interfaces and related implementation.
22 * `bsd` - BSD subsystems code
23 * `tools` - A set of utilities for testing, debugging and profiling kernel.
24
25 How to build XNU
26 ================
27
28 Building `DEVELOPMENT` kernel
29 -----------------------------
30
31 The xnu make system can build kernel based on `KERNEL_CONFIGS` & `ARCH_CONFIGS` variables as arguments.
32 Here is the syntax:
33
34 make SDKROOT=<sdkroot> ARCH_CONFIGS=<arch> KERNEL_CONFIGS=<variant>
35
36 Where:
37
38 * \<sdkroot>: path to macOS SDK on disk. (defaults to `/`)
39 * \<variant>: can be `debug`, `development`, `release`, `profile` and configures compilation flags and asserts throughout kernel code.
40 * \<arch> : can be valid arch to build for. (E.g. `X86_64`)
41
42 To build a kernel for the same architecture as running OS, just type
43
44 $ make
45 $ make SDKROOT=macosx.internal
46
47 Additionally, there is support for configuring architectures through `ARCH_CONFIGS` and kernel configurations with `KERNEL_CONFIGS`.
48
49 $ make SDKROOT=macosx.internal ARCH_CONFIGS=X86_64 KERNEL_CONFIGS=DEVELOPMENT
50 $ make SDKROOT=macosx.internal ARCH_CONFIGS=X86_64 KERNEL_CONFIGS="RELEASE DEVELOPMENT DEBUG"
51
52
53 Note:
54 * By default, architecture is set to the build machine architecture, and the default kernel
55 config is set to build for DEVELOPMENT.
56
57
58 This will also create a bootable image, kernel.[config], and a kernel binary
59 with symbols, kernel.[config].unstripped.
60
61 To intall the kernel into a DSTROOT, use the `install_kernels` target:
62
63 $ make install_kernels DSTROOT=/tmp/xnu-dst
64
65 Hint:
66 For a more satisfying kernel debugging experience, with access to all
67 local variables and arguments, but without all the extra check of the
68 DEBUG kernel, add something like:
69 CFLAGS_DEVELOPMENTARM64="-O0 -g -DKERNEL_STACK_MULTIPLIER=2"
70 CXXFLAGS_DEVELOPMENTARM64="-O0 -g -DKERNEL_STACK_MULTIPLIER=2"
71 to your make command.
72 Replace DEVELOPMENT and ARM64 with the appropriate build and platform.
73
74
75 * To build with RELEASE kernel configuration
76
77 make KERNEL_CONFIGS=RELEASE SDKROOT=/path/to/SDK
78
79
80 Building FAT kernel binary
81 --------------------------
82
83 Define architectures in your environment or when running a make command.
84
85 $ make ARCH_CONFIGS="X86_64" exporthdrs all
86
87 Other makefile options
88 ----------------------
89
90 * $ make MAKEJOBS=-j8 # this will use 8 processes during the build. The default is 2x the number of active CPUS.
91 * $ make -j8 # the standard command-line option is also accepted
92 * $ make -w # trace recursive make invocations. Useful in combination with VERBOSE=YES
93 * $ make BUILD_LTO=0 # build without LLVM Link Time Optimization
94 * $ make REMOTEBUILD=user@remotehost # perform build on remote host
95 * $ make BUILD_JSON_COMPILATION_DATABASE=1 # Build Clang JSON Compilation Database
96
97 The XNU build system can optionally output color-formatted build output. To enable this, you can either
98 set the `XNU_LOGCOLORS` environment variable to `y`, or you can pass `LOGCOLORS=y` to the make command.
99
100
101 Debug information formats
102 =========================
103
104 By default, a DWARF debug information repository is created during the install phase; this is a "bundle" named kernel.development.\<variant>.dSYM
105 To select the older STABS debug information format (where debug information is embedded in the kernel.development.unstripped image), set the BUILD_STABS environment variable.
106
107 $ export BUILD_STABS=1
108 $ make
109
110
111 Building KernelCaches
112 =====================
113
114 To test the xnu kernel, you need to build a kernelcache that links the kexts and
115 kernel together into a single bootable image.
116 To build a kernelcache you can use the following mechanisms:
117
118 * Using automatic kernelcache generation with `kextd`.
119 The kextd daemon keeps watching for changing in `/System/Library/Extensions` directory.
120 So you can setup new kernel as
121
122 $ cp BUILD/obj/DEVELOPMENT/X86_64/kernel.development /System/Library/Kernels/
123 $ touch /System/Library/Extensions
124 $ ps -e | grep kextd
125
126 * Manually invoking `kextcache` to build new kernelcache.
127
128 $ kextcache -q -z -a x86_64 -l -n -c /var/tmp/kernelcache.test -K /var/tmp/kernel.test /System/Library/Extensions
129
130
131
132 Running KernelCache on Target machine
133 =====================================
134
135 The 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.
136 Following are the steps to get such a setup:
137
138 1. Create kernel cache using the kextcache command as `/kernelcache.test`
139 2. Copy exiting boot configurations to alternate file
140
141 $ cp /Library/Preferences/SystemConfiguration/com.apple.Boot.plist /next_boot.plist
142
143 3. Update the kernelcache and boot-args for your setup
144
145 $ plutil -insert "Kernel Cache" -string "kernelcache.test" /next_boot.plist
146 $ plutil -replace "Kernel Flags" -string "debug=0x144 -v kernelsuffix=test " /next_boot.plist
147
148 4. Copy the new config to `/Library/Preferences/SystemConfiguration/`
149
150 $ cp /next_boot.plist /Library/Preferences/SystemConfiguration/boot.plist
151
152 5. Bless the volume with new configs.
153
154 $ sudo -n bless --mount / --setBoot --nextonly --options "config=boot"
155
156 The `--nextonly` flag specifies that use the `boot.plist` configs only for one boot.
157 So if the kernel panic's you can easily power reboot and recover back to original kernel.
158
159
160
161
162 Creating tags and cscope
163 ========================
164
165 Set up your build environment and from the top directory, run:
166
167 $ make tags # this will build ctags and etags on a case-sensitive volume, only ctags on case-insensitive
168 $ make TAGS # this will build etags
169 $ make cscope # this will build cscope database
170
171
172 Code Style
173 ==========
174
175 Source files can be reformatted to comply with the xnu code style using the "restyle" make target invoked from the
176 top-level project directory.
177
178 $ make restyle # re-format all source files to be xnu code style conformant.
179
180 Compliance can be checked using the "checkstyle" make target.
181
182 $ make checkstyle # Check all relevant source files for xnu code style conformance.
183
184 How to install a new header file from XNU
185 =========================================
186
187 To install IOKit headers, see additional comments in [iokit/IOKit/Makefile]().
188
189 XNU installs header files at the following locations -
190
191 a. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
192 b. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
193 c. $(DSTROOT)/usr/include/
194 d. $(DSTROOT)/System/DriverKit/usr/include/
195 e. $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders
196
197 `Kernel.framework` is used by kernel extensions.\
198 The `System.framework` and `/usr/include` are used by user level applications. \
199 `/System/DriverKit/usr/include` is used by userspace drivers. \
200 The header files in framework's `PrivateHeaders` are only available for ** Apple Internal Development **.
201
202 The directory containing the header file should have a Makefile that
203 creates the list of files that should be installed at different locations.
204 If you are adding the first header file in a directory, you will need to
205 create Makefile similar to `xnu/bsd/sys/Makefile`.
206
207 Add your header file to the correct file list depending on where you want
208 to install it. The default locations where the header files are installed
209 from each file list are -
210
211 a. `DATAFILES` : To make header file available in user level -
212 `$(DSTROOT)/usr/include`
213
214 b. `DRIVERKIT_DATAFILES` : To make header file available to DriverKit userspace drivers -
215 `$(DSTROOT)/System/DriverKit/usr/include`
216
217 c. `PRIVATE_DATAFILES` : To make header file available to Apple internal in
218 user level -
219 `$(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders`
220
221 d. `KERNELFILES` : To make header file available in kernel level -
222 `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers`
223 `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders`
224
225 e. `PRIVATE_KERNELFILES` : To make header file available to Apple internal
226 for kernel extensions -
227 `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders`
228
229 The Makefile combines the file lists mentioned above into different
230 install lists which are used by build system to install the header files. There
231 are two types of install lists: machine-dependent and machine-independent.
232 These lists are indicated by the presence of `MD` and `MI` in the build
233 setting, respectively. If your header is architecture-specific, then you should
234 use a machine-dependent install list (e.g. `INSTALL_MD_LIST`). If your header
235 should be installed for all architectures, then you should use a
236 machine-independent install list (e.g. `INSTALL_MI_LIST`).
237
238 If the install list that you are interested does not exist, create it
239 by adding the appropriate file lists. The default install lists, its
240 member file lists and their default location are described below -
241
242 a. `INSTALL_MI_LIST` : Installs header file to a location that is available to everyone in user level.
243 Locations -
244 $(DSTROOT)/usr/include
245 Definition -
246 INSTALL_MI_LIST = ${DATAFILES}
247
248 b. `INSTALL_DRIVERKIT_MI_LIST` : Installs header file to a location that is
249 available to DriverKit userspace drivers.
250 Locations -
251 $(DSTROOT)/System/DriverKit/usr/include
252 Definition -
253 INSTALL_DRIVERKIT_MI_LIST = ${DRIVERKIT_DATAFILES}
254
255 c. `INSTALL_MI_LCL_LIST` : Installs header file to a location that is available
256 for Apple internal in user level.
257 Locations -
258 $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders
259 Definition -
260 INSTALL_MI_LCL_LIST = ${PRIVATE_DATAFILES}
261
262 d. `INSTALL_KF_MI_LIST` : Installs header file to location that is available
263 to everyone for kernel extensions.
264 Locations -
265 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
266 Definition -
267 INSTALL_KF_MI_LIST = ${KERNELFILES}
268
269 e. `INSTALL_KF_MI_LCL_LIST` : Installs header file to location that is
270 available for Apple internal for kernel extensions.
271 Locations -
272 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
273 Definition -
274 INSTALL_KF_MI_LCL_LIST = ${KERNELFILES} ${PRIVATE_KERNELFILES}
275
276 f. `EXPORT_MI_LIST` : Exports header file to all of xnu (bsd/, osfmk/, etc.)
277 for compilation only. Does not install anything into the SDK.
278 Definition -
279 EXPORT_MI_LIST = ${KERNELFILES} ${PRIVATE_KERNELFILES}
280
281 g. `INSTALL_MODULEMAP_INCDIR_MI_LIST` : Installs module map file to a
282 location that is available to everyone in user level, installing at the
283 root of INCDIR.
284 Locations -
285 $(DSTROOT)/usr/include
286 Definition -
287 INSTALL_MODULEMAP_INCDIR_MI_LIST = ${MODULEMAP_INCDIR_FILES}
288
289 If you want to install the header file in a sub-directory of the paths
290 described in (1), specify the directory name using two variables
291 `INSTALL_MI_DIR` and `EXPORT_MI_DIR` as follows -
292
293 INSTALL_MI_DIR = dirname
294 EXPORT_MI_DIR = dirname
295
296 A single header file can exist at different locations using the steps
297 mentioned above. However it might not be desirable to make all the code
298 in the header file available at all the locations. For example, you
299 want to export a function only to kernel level but not user level.
300
301 You can use C language's pre-processor directive (#ifdef, #endif, #ifndef)
302 to control the text generated before a header file is installed. The kernel
303 only includes the code if the conditional macro is TRUE and strips out
304 code for FALSE conditions from the header file.
305
306 Some pre-defined macros and their descriptions are -
307
308 a. `PRIVATE` : If defined, enclosed definitions are considered System
309 Private Interfaces. These are visible within xnu and
310 exposed in user/kernel headers installed within the AppleInternal
311 "PrivateHeaders" sections of the System and Kernel frameworks.
312 b. `KERNEL_PRIVATE` : If defined, enclosed code is available to all of xnu
313 kernel and Apple internal kernel extensions and omitted from user
314 headers.
315 c. `BSD_KERNEL_PRIVATE` : If defined, enclosed code is visible exclusively
316 within the xnu/bsd module.
317 d. `MACH_KERNEL_PRIVATE`: If defined, enclosed code is visible exclusively
318 within the xnu/osfmk module.
319 e. `XNU_KERNEL_PRIVATE`: If defined, enclosed code is visible exclusively
320 within xnu.
321 f. `KERNEL` : If defined, enclosed code is available within xnu and kernel
322 extensions and is not visible in user level header files. Only the
323 header files installed in following paths will have the code -
324
325 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
326 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
327 g. `DRIVERKIT`: If defined, enclosed code is visible exclusively in the
328 DriverKit SDK headers used by userspace drivers.
329
330 Conditional compilation
331 =======================
332
333 `xnu` offers the following mechanisms for conditionally compiling code:
334
335 a. *CPU Characteristics* If the code you are guarding has specific
336 characterstics that will vary only based on the CPU architecture being
337 targeted, use this option. Prefer checking for features of the
338 architecture (e.g. `__LP64__`, `__LITTLE_ENDIAN__`, etc.).
339 b. *New Features* If the code you are guarding, when taken together,
340 implements a feature, you should define a new feature in `config/MASTER`
341 and use the resulting `CONFIG` preprocessor token (e.g. for a feature
342 named `config_virtual_memory`, check for `#if CONFIG_VIRTUAL_MEMORY`).
343 This practice ensures that existing features may be brought to other
344 platforms by simply changing a feature switch.
345 c. *Existing Features* You can use existing features if your code is
346 strongly tied to them (e.g. use `SECURE_KERNEL` if your code implements
347 new functionality that is exclusively relevant to the trusted kernel and
348 updates the definition/understanding of what being a trusted kernel means).
349
350 It is recommended that you avoid compiling based on the target platform. `xnu`
351 does not define the platform macros from `TargetConditionals.h`
352 (`TARGET_OS_OSX`, `TARGET_OS_IOS`, etc.).
353
354
355 There is a deprecated `TARGET_OS_EMBEDDED` macro, but this should be avoided
356 as it is in general too broad a definition for most functionality.
357 Please refer to TargetConditionals.h for a full picture.
358
359 How to add a new syscall
360 ========================
361
362
363
364
365 Testing the kernel
366 ==================
367
368 XNU kernel has multiple mechanisms for testing.
369
370 * Assertions - The DEVELOPMENT and DEBUG kernel configs are compiled with assertions enabled. This allows developers to easily
371 test invariants and conditions.
372
373 * XNU Power On Self Tests (`XNUPOST`): The XNUPOST config allows for building the kernel with basic set of test functions
374 that are run before first user space process is launched. Since XNU is hybrid between MACH and BSD, we have two locations where
375 tests can be added.
376
377 xnu/osfmk/tests/ # For testing mach based kernel structures and apis.
378 bsd/tests/ # For testing BSD interfaces.
379 Please follow the documentation at [osfmk/tests/README.md](osfmk/tests/README.md)
380
381 * User level tests: The `tools/tests/` directory holds all the tests that verify syscalls and other features of the xnu kernel.
382 The make target `xnu_tests` can be used to build all the tests supported.
383
384 $ make RC_ProjectName=xnu_tests SDKROOT=/path/to/SDK
385
386 These tests are individual programs that can be run from Terminal and report tests status by means of std posix exit codes (0 -> success) and/or stdout.
387 Please read detailed documentation in [tools/tests/unit_tests/README.md](tools/tests/unit_tests/README.md)
388
389
390 Kernel data descriptors
391 =======================
392
393 XNU uses different data formats for passing data in its api. The most standard way is using syscall arguments. But for complex data
394 it often relies of sending memory saved by C structs. This packaged data transport mechanism is fragile and leads to broken interfaces
395 between user space programs and kernel apis. `libkdd` directory holds user space library that can parse custom data provided by the
396 same version of kernel. The kernel chunked data format is described in detail at [libkdd/README.md](libkdd/README.md).
397
398
399 Debugging the kernel
400 ====================
401
402 The xnu kernel supports debugging with a remote kernel debugging protocol (kdp). Please refer documentation at [technical note] [TN2063]
403 By default the kernel is setup to reboot on a panic. To debug a live kernel, the kdp server is setup to listen for UDP connections
404 over ethernet. For machines without ethernet port, this behavior can be altered with use of kernel boot-args. Following are some
405 common options.
406
407 * `debug=0x144` - setups debug variables to start kdp debugserver on panic
408 * `-v` - print kernel logs on screen. By default XNU only shows grey screen with boot art.
409 * `kdp_match_name=en1` - Override default port selection for kdp. Supported for ethernet, thunderbolt and serial debugging.
410
411 To debug a panic'ed kernel, use llvm debugger (lldb) along with unstripped symbol rich kernel binary.
412
413 sh$ lldb kernel.development.unstripped
414
415 And then you can connect to panic'ed machine with `kdp_remote [ip addr]` or `gdb_remote [hostip : port]` commands.
416
417 Each kernel is packaged with kernel specific debug scripts as part of the build process. For security reasons these special commands
418 and scripts do not get loaded automatically when lldb is connected to machine. Please add the following setting to your `~/.lldbinit`
419 if you wish to always load these macros.
420
421 settings set target.load-script-from-symbol-file true
422
423 The `tools/lldbmacros` directory contains the source for each of these commands. Please follow the [README.md](tools/lldbmacros/README.md)
424 for detailed explanation of commands and their usage.
425
426 [TN2118]: https://developer.apple.com/library/mac/technotes/tn2004/tn2118.html#//apple_ref/doc/uid/DTS10003352 "Kernel Core Dumps"
427 [TN2063]: https://developer.apple.com/library/mac/technotes/tn2063/_index.html "Understanding and Debugging Kernel Panics"
428 [Kernel Programming Guide]: https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/KernelProgramming/build/build.html#//apple_ref/doc/uid/TP30000905-CH221-BABDGEGF