]> git.saurik.com Git - apple/xnu.git/blob - README.md
xnu-3789.31.2.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 OS X and iOS operating systems. XNU is an acronym for XNU is Not Unix.
5 XNU is a hybrid kernel combining the Mach kernel developed at Carnegie Mellon University with components from FreeBSD and C++ API for writing drivers called IOKit.
6 XNU runs on I386, 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. `i386` or `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
62 * To build with RELEASE kernel configuration
63
64 make KERNEL_CONFIGS=RELEASE SDKROOT=/path/to/SDK
65
66
67 Building FAT kernel binary
68 --------------------------
69
70 Define architectures in your environment or when running a make command.
71
72 $ make ARCH_CONFIGS="I386 X86_64" exporthdrs all
73
74 Other makefile options
75 ----------------------
76
77 * $ make MAKEJOBS=-j8 # this will use 8 processes during the build. The default is 2x the number of active CPUS.
78 * $ make -j8 # the standard command-line option is also accepted
79 * $ make -w # trace recursive make invocations. Useful in combination with VERBOSE=YES
80 * $ make BUILD_LTO=0 # build without LLVM Link Time Optimization
81 * $ make REMOTEBUILD=user@remotehost # perform build on remote host
82 * $ make BUILD_JSON_COMPILATION_DATABASE=1 # Build Clang JSON Compilation Database
83
84
85
86 Debug information formats
87 =========================
88
89 By default, a DWARF debug information repository is created during the install phase; this is a "bundle" named kernel.development.\<variant>.dSYM
90 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.
91
92 $ export BUILD_STABS=1
93 $ make
94
95
96 Building KernelCaches
97 =====================
98
99 To test the xnu kernel, you need to build a kernelcache that links the kexts and
100 kernel together into a single bootable image.
101 To build a kernelcache you can use the following mechanisms:
102
103 * Using automatic kernelcache generation with `kextd`.
104 The kextd daemon keeps watching for changing in `/System/Library/Extensions` directory.
105 So you can setup new kernel as
106
107 $ cp BUILD/obj/DEVELOPMENT/X86_64/kernel.development /System/Library/Kernels/
108 $ touch /System/Library/Extensions
109 $ ps -e | grep kextd
110
111 * Manually invoking `kextcache` to build new kernelcache.
112
113 $ kextcache -q -z -a x86_64 -l -n -c /var/tmp/kernelcache.test -K /var/tmp/kernel.test /System/Library/Extensions
114
115
116
117 Running KernelCache on Target machine
118 =====================================
119
120 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.
121 Following are the steps to get such a setup:
122
123 1. Create kernel cache using the kextcache command as `/kernelcache.test`
124 2. Copy exiting boot configurations to alternate file
125
126 $ cp /Library/Preferences/SystemConfiguration/com.apple.Boot.plist /next_boot.plist
127
128 3. Update the kernelcache and boot-args for your setup
129
130 $ plutil -insert "Kernel Cache" -string "kernelcache.test" /next_boot.plist
131 $ plutil -replace "Kernel Flags" -string "debug=0x144 -v kernelsuffix=test " /next_boot.plist
132
133 4. Copy the new config to `/Library/Preferences/SystemConfiguration/`
134
135 $ cp /next_boot.plist /Library/Preferences/SystemConfiguration/boot.plist
136
137 5. Bless the volume with new configs.
138
139 $ sudo -n bless --mount / --setBoot --nextonly --options "config=boot"
140
141 The `--nextonly` flag specifies that use the `boot.plist` configs only for one boot.
142 So if the kernel panic's you can easily power reboot and recover back to original kernel.
143
144
145
146
147 Creating tags and cscope
148 ========================
149
150 Set up your build environment and from the top directory, run:
151
152 $ make tags # this will build ctags and etags on a case-sensitive volume, only ctags on case-insensitive
153 $ make TAGS # this will build etags
154 $ make cscope # this will build cscope database
155
156
157 Coding styles (Reindenting files)
158 =================================
159
160 Source files can be reindented using clang-format setup in .clang-format.
161 XNU follows a variant of WebKit style for source code formatting.
162 Please refer to format styles at [WebKit website](http://www.webkit.org/coding/coding-style.html).
163 Further options about style options is available at [clang docs](http://clang.llvm.org/docs/ClangFormatStyleOptions.html)
164
165 Note: clang-format binary may not be part of base installation. It can be compiled from llvm clang sources and is reachable in $PATH.
166
167 From the top directory, run:
168
169 $ make reindent # reindent all source files using clang format.
170
171
172
173 How to install a new header file from XNU
174 =========================================
175
176 To install IOKit headers, see additional comments in [iokit/IOKit/Makefile]().
177
178 XNU installs header files at the following locations -
179
180 a. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
181 b. $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
182 c. $(DSTROOT)/usr/include/
183 d. $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders
184
185 `Kernel.framework` is used by kernel extensions.\
186 The `System.framework` and `/usr/include` are used by user level applications. \
187 The header files in framework's `PrivateHeaders` are only available for ** Apple Internal Development **.
188
189 The directory containing the header file should have a Makefile that
190 creates the list of files that should be installed at different locations.
191 If you are adding first header file in a directory, you will need to
192 create Makefile similar to xnu/bsd/sys/Makefile.
193
194 Add your header file to the correct file list depending on where you want
195 to install it. The default locations where the header files are installed
196 from each file list are -
197
198 a. `DATAFILES` : To make header file available in user level -
199 `$(DSTROOT)/usr/include`
200
201 b. `PRIVATE_DATAFILES` : To make header file available to Apple internal in
202 user level -
203 `$(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders`
204
205 c. `KERNELFILES` : To make header file available in kernel level -
206 `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers`
207 `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders`
208
209 d. `PRIVATE_KERNELFILES` : To make header file available to Apple internal
210 for kernel extensions -
211 `$(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders`
212
213 The Makefile combines the file lists mentioned above into different
214 install lists which are used by build system to install the header files.
215
216 If the install list that you are interested does not exist, create it
217 by adding the appropriate file lists. The default install lists, its
218 member file lists and their default location are described below -
219
220 a. `INSTALL_MI_LIST` : Installs header file to a location that is available to everyone in user level.
221 Locations -
222 $(DSTROOT)/usr/include
223 Definition -
224 INSTALL_MI_LIST = ${DATAFILES}
225
226 b. `INSTALL_MI_LCL_LIST` : Installs header file to a location that is available
227 for Apple internal in user level.
228 Locations -
229 $(DSTROOT)/System/Library/Frameworks/System.framework/PrivateHeaders
230 Definition -
231 INSTALL_MI_LCL_LIST = ${PRIVATE_DATAFILES}
232
233 c. `INSTALL_KF_MI_LIST` : Installs header file to location that is available
234 to everyone for kernel extensions.
235 Locations -
236 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
237 Definition -
238 INSTALL_KF_MI_LIST = ${KERNELFILES}
239
240 d. `INSTALL_KF_MI_LCL_LIST` : Installs header file to location that is
241 available for Apple internal for kernel extensions.
242 Locations -
243 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
244 Definition -
245 INSTALL_KF_MI_LCL_LIST = ${KERNELFILES} ${PRIVATE_KERNELFILES}
246
247 If you want to install the header file in a sub-directory of the paths
248 described in (1), specify the directory name using two variables
249 `INSTALL_MI_DIR` and `EXPORT_MI_DIR` as follows -
250
251 INSTALL_MI_DIR = dirname
252 EXPORT_MI_DIR = dirname
253
254 A single header file can exist at different locations using the steps
255 mentioned above. However it might not be desirable to make all the code
256 in the header file available at all the locations. For example, you
257 want to export a function only to kernel level but not user level.
258
259 You can use C language's pre-processor directive (#ifdef, #endif, #ifndef)
260 to control the text generated before a header file is installed. The kernel
261 only includes the code if the conditional macro is TRUE and strips out
262 code for FALSE conditions from the header file.
263
264 Some pre-defined macros and their descriptions are -
265
266 a. `PRIVATE` : If true, code is available to all of the xnu kernel and is
267 not available in kernel extensions and user level header files. The
268 header files installed in all the paths described above in (1) will not
269 have code enclosed within this macro.
270
271 b. `KERNEL_PRIVATE` : If true, code is available to all of the xnu kernel and Apple
272 internal kernel extensions.
273
274 c. `BSD_KERNEL_PRIVATE` : If true, code is available to the xnu/bsd part of
275 the kernel and is not available to rest of the kernel, kernel extensions
276 and user level header files. The header files installed in all the
277 paths described above in (1) will not have code enclosed within this macro.
278
279 d. `KERNEL` : If true, code is available only in kernel and kernel
280 extensions and is not available in user level header files. Only the
281 header files installed in following paths will have the code -
282
283 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/Headers
284 $(DSTROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders
285
286 you should check [Testing the kernel][] for details.
287
288
289 How to add a new syscall
290 ========================
291
292
293
294
295 Testing the kernel
296 ==================
297
298 XNU kernel has multiple mechanisms for testing.
299
300 * Assertions - The DEVELOPMENT and DEBUG kernel configs are compiled with assertions enabled. This allows developers to easily
301 test invariants and conditions.
302
303 * XNU Power On Self Tests (`XNUPOST`): The XNUPOST config allows for building the kernel with basic set of test functions
304 that are run before first user space process is launched. Since XNU is hybrid between MACH and BSD, we have two locations where
305 tests can be added.
306
307 xnu/osfmk/tests/ # For testing mach based kernel structures and apis.
308 bsd/tests/ # For testing BSD interfaces.
309 Please follow the documentation at [osfmk/tests/README.md](osfmk/tests/README.md)
310
311 * User level tests: The `tools/tests/` directory holds all the tests that verify syscalls and other features of the xnu kernel.
312 The make target `xnu_tests` can be used to build all the tests supported.
313
314 $ make RC_ProjectName=xnu_tests SDKROOT=/path/to/SDK
315
316 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.
317 Please read detailed documentation in [tools/tests/unit_tests/README.md](tools/tests/unit_tests/README.md)
318
319
320 Kernel data descriptors
321 =======================
322
323 XNU uses different data formats for passing data in its api. The most standard way is using syscall arguments. But for complex data
324 it often relies of sending memory saved by C structs. This packaged data transport mechanism is fragile and leads to broken interfaces
325 between user space programs and kernel apis. `libkdd` directory holds user space library that can parse custom data provided by the
326 same version of kernel. The kernel chunked data format is described in detail at [libkdd/README.md](libkdd/README.md).
327
328
329 Debugging the kernel
330 ====================
331
332 The xnu kernel supports debugging with a remote kernel debugging protocol (kdp). Please refer documentation at [technical note] [TN2063]
333 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
334 over ethernet. For machines without ethernet port, this behavior can be altered with use of kernel boot-args. Following are some
335 common options.
336
337 * `debug=0x144` - setups debug variables to start kdp debugserver on panic
338 * `-v` - print kernel logs on screen. By default XNU only shows grey screen with boot art.
339 * `kdp_match_name=en1` - Override default port selection for kdp. Supported for ethernet, thunderbolt and serial debugging.
340
341 To debug a panic'ed kernel, use llvm debugger (lldb) along with unstripped symbol rich kernel binary.
342
343 sh$ lldb kernel.development.unstripped
344
345 And then you can connect to panic'ed machine with `kdp_remote [ip addr]` or `gdb_remote [hostip : port]` commands.
346
347 Each kernel is packaged with kernel specific debug scripts as part of the build process. For security reasons these special commands
348 and scripts do not get loaded automatically when lldb is connected to machine. Please add the following setting to your `~/.lldbinit`
349 if you wish to always load these macros.
350
351 settings set target.load-script-from-symbol-file true
352
353 The `tools/lldbmacros` directory contains the source for each of these commands. Please follow the [README.md](tools/lldbmacros/README.md)
354 for detailed explanation of commands and their usage.
355
356 [TN2118]: https://developer.apple.com/library/mac/technotes/tn2004/tn2118.html#//apple_ref/doc/uid/DTS10003352 "Kernel Core Dumps"
357 [TN2063]: https://developer.apple.com/library/mac/technotes/tn2063/_index.html "Understanding and Debugging Kernel Panics"
358 [Kernel Programming Guide]: https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/KernelProgramming/build/build.html#//apple_ref/doc/uid/TP30000905-CH221-BABDGEGF