Skip to content

Build the kernel ​

The kernel step builds Apple's XNU source with UniXNU patches, then prelinks the result against the original kernelcache.

Source and tree ​

PathContents
out/<board>/tree/refer/xnuPinned XNU source
out/<board>/tree/.cache/xnu-workWorking clone with patches applied
out/<board>/tree/build/xnu/ios-objObject files and compile command records
out/<board>/tree/build/xnu/ios-symSymbols
out/<board>/tree/build/xnu/ios-dstInstall headers
out/<board>/tree/.cache/fakerootKernel SDK root with restored headers
out/<board>/kernel/kernel.machoPrelinked kernel

The source version comes from feeds.toml:

toml
[[source]]
name = "xnu"
kind = "refer"
git = "https://github.com/apple-oss-distributions/xnu.git"
ref = "xnu-10063.141.1"
commit = "d8b80295118ef25ac3a784134bcf95cd8e88109f"
darwin = "23.6.0"
version = "10063.141.1"
driverkit_tag = "xnu-10063.121.3"
trustcache_tag = "xnu-8796.101.5"

unixnu build kernel passes these values to scripts/build-xnu-4k-baseline.sh as UNIXNU_XNU_TAG, UNIXNU_XNU_COMMIT, UNIXNU_XNU_DARWIN, UNIXNU_XNU_VERSION, UNIXNU_DRIVERKIT_TAG and UNIXNU_TRUSTCACHE_TAG.

The build target is:

SDKROOT=iphoneos
ARCH_CONFIGS=ARM64
MACHINE_CONFIGS=BCM2837
KERNEL_CONFIGS=DEVELOPMENT
ARM64_LINK_ADDR=0xfffffff007004000
ARM64_PLKSEG_ADDR=0xfffffff004004000
DEPLOYMENT_TARGET_FLAGS="-miphoneos-version-min=17.0 -DXNU_TARGET_OS_IOS"

BCM2837 is the XNU machine configuration that carries the 4 KiB page layout. The kernel still targets iOS 17 so the prelink ABI matches the iPadOS kernelcache.

Defconfig flags ​

Each CONFIG_XNU_<NAME> line becomes XNU_<NAME> in the kernel build environment. y becomes 1, n becomes 0, quoted strings are passed as text, and {tree} in a string is replaced by the build tree path.

FlagEffectArchive
XNU_SOURCE_CORECRYPTO14Builds and links the source corecrypto providerbuild/corecrypto-2023/libcorecrypto_kernel_noasm-namespaced.a
XNU_SOURCE_PTHREADBuilds and registers the source pthread componentbuild/libpthread-kernel/libpthread_kernel.a
XNU_ARM_DT_PLATFORMLinks the Apple-format device tree platform expertbuild/armdt-platform/libArmDTPlatform.a
XNU_ARMVIRT_PLATFORMLinks the QEMU virt platform expertbuild/armvirt-platform/libArmVirtPlatform.a
XNU_ARMCPULinks the architectural CPU and interrupt providerbuild/armcpu/libArmCpu.a
XNU_NATIVE_UFSLinks the UFS storage driverbuild/native-ufs/libNativeUfs.a
XNU_NATIVE_AESLinks the AES accelerator driverbuild/native-aes/libNativeAES.a
XNU_NATIVE_VIRTIO_BLOCKLinks the VirtIO block driverXNU_NATIVE_VIRTIO_BLOCK_ARCHIVE or build/native-virtio-block/libNativeVirtioBlock.a
XNU_NATIVE_RAM_BLOCKLinks the RAM block driverbuild/native-ram-block/libNativeRamBlock.a
XNU_NATIVE_VIRTIO_CONSOLELinks the VirtIO console byte transportbuild/native-virtio-console/libNativeVirtioConsole.a
XNU_NATIVE_MOBILE_DISPLAYLinks the mobile display providerbuild/native-mobile-display/libNativeMobileDisplay.late.a
XNU_NATIVE_LEGACY_DISPLAYLinks the legacy display providerbuild/native-legacy-display/libNativeLegacyDisplay.late.a
XNU_NATIVE_ADRENOLinks the Adreno GPU driver and kitsbuild/native-adreno/libNativeAdreno.a
XNU_NATIVE_OFFLINE_GPULinks the offline GPU brokerbuild/native-offline-gpu/libNativeOfflineGPU.late.a
XNU_VERIFIED_ROOT_PROBELinks the verified root providerXNU_VERIFIED_ROOT_ARCHIVE or build/verified-root-xnu/libVerifiedRootProbe.a
XNU_NATIVE_CS_HASH_CACHEDerives 4 KiB code signature hashes from verified 16 KiB blocksnone
XNU_NATIVE_ARCH_TIMERUses the architectural timer entrynone
XNU_NATIVE_PANEnables ARMv8.1 PAN supportnone
XNU_DT_PANIC_REGIONReads the panic region from the device treenone
XNU_TRUST_CACHE_RUNTIMEInitializes the trust cache runtimenone
XNU_BOOT_PROGRESSAdds the boot progress layerbuild/boot-progress/libBootProgress.a
XNU_SOURCE_MOCKFSAdds the mockfs test filebuild/mockfs-fixture/libmockfs_fixture.a

The device repository owns the scripts that produce its archives. Run them after the first kernel build produces the compile command records, then run unixnu build kernel again.

Build sequence ​

  1. unixnu fetch clones XNU and the Apple references.
  2. unixnu build kernel applies patches and builds XNU. When XNU_BOOT_PROGRESS=1 and startup.o.json is absent, the step runs once with boot progress disabled, then again with it enabled.
  3. Build the device archives. Each device script compiles against the actual XNU compile flags recorded in build/xnu/ios-obj/.../*.cpo.json.
  4. unixnu build kernel links the archives into the kernel.
  5. unixnu build prelink produces out/<board>/kernel/kernel.macho.

The prelink reads [kernel.prelink] from board.toml:

toml
[kernel.prelink]
tool = "kits/NativePrelink/emit_prelink.py"
kernel = "build/xnu/ios-obj/DEVELOPMENT_ARM64_BCM2837/mach.development.bcm2837.unstripped.noctf"
kernelcache_sha256 = "..."
slide = "-0x1000000"
early_security_init = "com.apple.driver.AppleMobileFileIntegrity=0xfffffff005ab4ae0"
roots = [
  "com.apple.driver.AppleARMPlatform",
  "com.apple.filesystems.apfs",
]

roots lists the original kernelcache components the native kernel uses. early_security_init maps a component identifier to an address that runs during prelink security initialization.

Adding a kernel flag ​

  1. Add CONFIG_XNU_<NAME>=y to the device defconfig.
  2. Read XNU_<NAME> in scripts/build-xnu-4k-baseline.sh.
  3. Add the object flags with EXTRA_MAKE_ARGS+=("<file>.o_CFLAGS_ADD=-DXNU_<NAME>=$VALUE").
  4. Add the archive to KERNEL_EXTRA_ARCHIVES and KERNEL_ARCHIVE_DEPENDENCIES.
  5. Add the personality to iokit/KernelConfigTables.cpp through a patch.

Prebuilt kernel ​

unixnu build payload --kernel FILE packs an existing prelinked kernel. This path builds the boot image without rebuilding XNU.

Next steps ​

Released under the BSD-2-Clause-Patent License.