Skip to content

Commit 0cf1f39

Browse files
amarioguymarcan
authored andcommitted
Add raw binary support to HV scripts
Signed-off-by: amarioguy <[email protected]>
1 parent b8dbb59 commit 0cf1f39

2 files changed

Lines changed: 57 additions & 49 deletions

File tree

proxyclient/m1n1/hv/__init__.py

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,51 +1494,8 @@ def enable_time_stealing(self):
14941494
def disable_time_stealing(self):
14951495
self.p.hv_set_time_stealing(False)
14961496

1497-
def load_macho(self, data, symfile=None):
1498-
if isinstance(data, str):
1499-
data = open(data, "rb")
1500-
1501-
self.macho = macho = MachO(data)
1502-
if symfile is not None:
1503-
if isinstance(symfile, str):
1504-
symfile = open(symfile, "rb")
1505-
syms = MachO(symfile)
1506-
macho.add_symbols("com.apple.kernel", syms)
1507-
self.xnu_mode = True
1508-
1509-
self.symbol_dict = macho.symbols
1510-
self.symbols = [(v, k) for k, v in macho.symbols.items()]
1511-
self.symbols.sort()
1512-
1513-
def load_hook(data, segname, size, fileoff, dest):
1514-
if segname != "__TEXT_EXEC":
1515-
return data
1516-
1517-
print(f"Patching segment {segname}...")
15181497

1519-
a = array.array("I", data)
1520-
1521-
output = []
1522-
1523-
p = 0
1524-
while (p := data.find(b"\x20\x00", p)) != -1:
1525-
if (p & 3) != 2:
1526-
p += 1
1527-
continue
1528-
1529-
opcode = a[p // 4]
1530-
inst = self.hvc((opcode & 0xffff))
1531-
off = fileoff + (p & ~3)
1532-
if off >= 0xbfcfc0:
1533-
print(f" 0x{off:x}: 0x{opcode:04x} -> hvc 0x{opcode:x} (0x{inst:x})")
1534-
a[p // 4] = inst
1535-
p += 4
1536-
1537-
print("Done.")
1538-
return a.tobytes()
1539-
1540-
#image = macho.prepare_image(load_hook)
1541-
image = macho.prepare_image()
1498+
def load_raw(self, image, entryoffset=0x800):
15421499
sepfw_start, sepfw_length = self.u.adt["chosen"]["memory-map"].SEPFW
15431500
tc_start, tc_size = self.u.adt["chosen"]["memory-map"].TrustCache
15441501
if hasattr(self.u.adt["chosen"]["memory-map"], "preoslog"):
@@ -1571,8 +1528,8 @@ def load_hook(data, segname, size, fileoff, dest):
15711528

15721529
print(f"Physical memory: 0x{phys_base:x} .. 0x{mem_top:x}")
15731530
print(f"Guest region start: 0x{guest_base:x}")
1574-
1575-
self.entry = macho.entry - macho.vmin + guest_base
1531+
1532+
self.entry = guest_base + entryoffset
15761533

15771534
print(f"Mapping guest physical memory...")
15781535
self.add_tracer(irange(self.ram_base, self.u.ba.phys_base - self.ram_base), "RAM-LOW", TraceMode.OFF)
@@ -1610,8 +1567,6 @@ def load_hook(data, segname, size, fileoff, dest):
16101567
self.tba.devtree = self.adt_base - phys_base + self.tba.virt_base
16111568
self.tba.top_of_kernel_data = guest_base + image_size
16121569

1613-
self.sym_offset = macho.vmin - guest_base + self.tba.phys_base - self.tba.virt_base
1614-
16151570
self.iface.writemem(guest_base + self.bootargs_off, BootArgs.build(self.tba))
16161571

16171572
print("Setting secondary CPU RVBARs...")
@@ -1621,6 +1576,55 @@ def load_hook(data, segname, size, fileoff, dest):
16211576
print(f" {cpu.name}: [0x{addr:x}] = 0x{rvbar:x}")
16221577
self.p.write64(addr, rvbar)
16231578

1579+
1580+
def load_macho(self, data, symfile=None):
1581+
if isinstance(data, str):
1582+
data = open(data, "rb")
1583+
1584+
self.macho = macho = MachO(data)
1585+
if symfile is not None:
1586+
if isinstance(symfile, str):
1587+
symfile = open(symfile, "rb")
1588+
syms = MachO(symfile)
1589+
macho.add_symbols("com.apple.kernel", syms)
1590+
self.xnu_mode = True
1591+
1592+
self.symbol_dict = macho.symbols
1593+
self.symbols = [(v, k) for k, v in macho.symbols.items()]
1594+
self.symbols.sort()
1595+
1596+
def load_hook(data, segname, size, fileoff, dest):
1597+
if segname != "__TEXT_EXEC":
1598+
return data
1599+
1600+
print(f"Patching segment {segname}...")
1601+
1602+
a = array.array("I", data)
1603+
1604+
output = []
1605+
1606+
p = 0
1607+
while (p := data.find(b"\x20\x00", p)) != -1:
1608+
if (p & 3) != 2:
1609+
p += 1
1610+
continue
1611+
1612+
opcode = a[p // 4]
1613+
inst = self.hvc((opcode & 0xffff))
1614+
off = fileoff + (p & ~3)
1615+
if off >= 0xbfcfc0:
1616+
print(f" 0x{off:x}: 0x{opcode:04x} -> hvc 0x{opcode:x} (0x{inst:x})")
1617+
a[p // 4] = inst
1618+
p += 4
1619+
1620+
print("Done.")
1621+
return a.tobytes()
1622+
1623+
#image = macho.prepare_image(load_hook)
1624+
image = macho.prepare_image()
1625+
self.load_raw(image, entryoffset=(macho.entry - macho.vmin))
1626+
1627+
16241628
def update_pac_mask(self):
16251629
tcr = TCR(self.u.mrs(TCR_EL12))
16261630
valid_bits = (1 << (64 - tcr.T1SZ)) - 1

proxyclient/tools/run_guest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
parser.add_argument('-d', '--debug-xnu', action="store_true")
1515
parser.add_argument('-l', '--logfile', type=pathlib.Path)
1616
parser.add_argument('-C', '--cpus', default=None)
17+
parser.add_argument('-r', '--raw', action="store_true")
1718
parser.add_argument('payload', type=pathlib.Path)
1819
parser.add_argument('boot_args', default=[], nargs="*")
1920
args = parser.parse_args()
@@ -61,7 +62,10 @@
6162
symfile = None
6263
if args.symbols:
6364
symfile = args.symbols.open("rb")
64-
hv.load_macho(args.payload.open("rb"), symfile=symfile)
65+
if args.raw:
66+
hv.load_raw(args.payload.read_bytes())
67+
else:
68+
hv.load_macho(args.payload.open("rb"), symfile=symfile)
6569

6670
PMU(u).reset_panic_counter()
6771

0 commit comments

Comments
 (0)