Repository navigation
Expand file tree
/
Copy pathpatch_kernel.py
More file actions
72 lines (63 loc) · 2.96 KB
/
Copy pathpatch_kernel.py
File metadata and controls
72 lines (63 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
"""
Patch the *staged* kernel collection in build/esp/gatehouse/kernel.
The tracked kernel/ collection stays pristine; Gatehouse hands XNU the staged
copy, and this writes the one fixup that copy needs on QEMU.
Sets:
mrs Six reads of Apple implementation-defined system registers
(`mrs xN, s3_6_c15_c0_0`, `s3_3_c15_c8_0`, ...) in the AMX/impl-state
save path the GPU driver reaches. QEMU's `-cpu max` does not
implement them, so the access traps and the kernel panics. Replacing
each with `mov xN, xzr` lets the path continue; the registers are
only used to save state the guest never restores on this machine.
none Restore the pristine collection.
Deliberately absent: the trust-cache / Image4 / AMFI "return success" patches
that were tried for the cryptex. Making loadTrustCache a no-op stops the
auxiliary kernel collection's trust cache from loading too, and the GPU kext
lives in that collection; the guest then stalls before the GPU driver starts.
The cryptex-less image needs no trust-cache patch.
"""
import os
import struct
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PRISTINE = os.path.join(ROOT, "kernel", "kernelcache.release.vma2.kc")
STAGED = os.path.join(ROOT, "build", "esp", "gatehouse", "kernel")
BASE = 0xFFFFFE0007004000
# (virtual address, name, expected original words, replacement words)
PATCHES = {
"mrs": [
(0xFFFFFE000A828B24, "mrs x8", [0xD53EF008], [0xAA1F03E8]),
(0xFFFFFE000A828B28, "mrs x9", [0xD53BF809], [0xAA1F03E9]),
(0xFFFFFE000A828B2C, "mrs x10", [0xD53BF90A], [0xAA1F03EA]),
(0xFFFFFE000A828B30, "mrs x11", [0xD53BFA0B], [0xAA1F03EB]),
(0xFFFFFE000A828B34, "mrs x12", [0xD53BF00C], [0xAA1F03EC]),
(0xFFFFFE000A828B38, "mrs x13", [0xD53CF00D], [0xAA1F03ED]),
],
"none": [],
}
def main():
name = sys.argv[1] if len(sys.argv) > 1 else "mrs"
if name not in PATCHES:
print(f"unknown set {name!r}; have {', '.join(PATCHES)}", file=sys.stderr)
return 2
data = bytearray(open(PRISTINE, "rb").read())
for va, label, expected, replacement in PATCHES[name]:
off = va - BASE
have = list(struct.unpack_from(f"<{len(expected)}I", data, off))
if have != expected:
print(f" SKIP {label} at {va:#x}: found "
f"{' '.join(f'{w:#010x}' for w in have)}, expected "
f"{' '.join(f'{w:#010x}' for w in expected)}", file=sys.stderr)
continue
struct.pack_into(f"<{len(replacement)}I", data, off, *replacement)
print(f" {label:10s} {va:#x} -> "
f"{' '.join(f'{w:#010x}' for w in replacement)}")
os.makedirs(os.path.dirname(STAGED), exist_ok=True)
with open(STAGED, "wb") as f:
f.write(data)
print(f"wrote {STAGED} ({len(data)} bytes, set {name})")
return 0
if __name__ == "__main__":
raise SystemExit(main())