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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
"""Integrated multi-component PSP model (foundation for boot-chain emulation).
The per-component `Component` runs one module in isolation with stubbed SVC/RPC
boundaries. `System` instead maps ALL resident components -- sOS, SYS_DRV, ASD --
plus one TA window into a SINGLE Unicorn address space, so that inter-component
calls (a TA's SVC 0xF2 reaching the *real* SYS_DRV/ASD code, SYS_DRV calling back
into sOS) can execute for real instead of hitting a mock.
The resident components occupy non-overlapping ranges (verified: sOS 0x0..0x18280
+ task@0x200000, ASD 0xD00000.., SYS_DRV 0xE00000.., TA window 0x400000..), so a
flat map is sound.
STATUS: foundation only. This class stands up the unified address space and can
run a function in any component. It does NOT yet:
* run sOS's cold-boot init (hardware-coupled: SPI, CCP, MMU, IRQ, SMU),
* model the cooperative scheduler (multiple sOS tasks / SVC 0x60),
* translate VAs through the ARM MMU (runs flat-physical),
* route SVC 0xF2 into the real sOS gate (needs nested execution -- see
`route_svc_f2`, a stub hook point).
See FIDELITY-NOTES.md "Boot-chain roadmap" for the staged plan and effort.
"""
import unicorn
from unicorn import Uc, UC_ARCH_ARM, UC_MODE_THUMB, UC_MODE_LITTLE_ENDIAN, UC_PROT_ALL
from unicorn import UC_HOOK_INTR, UC_HOOK_MEM_INVALID
from unicorn.arm_const import (UC_ARM_REG_PC, UC_ARM_REG_SP, UC_ARM_REG_LR,
UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2,
UC_ARM_REG_R3, UC_ARM_REG_CPSR)
from . import loader
PAGE = 0x1000
# sentinel "function returned" address, in the gap below host_shm (SYS_DRV's
# scratch runs up to ~0xFE0000, so the old 0xFF0000 collided with it)
MAGIC_RET = 0x1FFF_0000
def _align_dn(x, a=PAGE): return x & ~(a - 1)
def _align_up(x, a=PAGE): return (x + a - 1) & ~(a - 1)
# One flat physical map covering every resident component and the shared
# hardware/mailbox apertures. Each entry: (name, base, size).
SYSTEM_REGIONS = [
("sos_low", 0x0000_0000, 0x0021_0000), # sOS code/data + task@0x200000 + mailbox 0x2060xx + pagetables 0xB0000
("ta_window", 0x0040_0000, 0x0021_0000), # loaded TA (HDCP/DTM/RAS) + its stack/scratch
("asd", 0x00D0_0000, 0x0009_0000), # ASD image + ram + stack/scratch (..0xD90000)
("sys_drv", 0x00E0_0000, 0x0020_0000), # SYS_DRV image + data + stack/scratch (..0x1000000)
("mmio_irq", 0x0301_0000, 0x0001_0000), # C2PMSG / IRQ mask array 0x3010200
("intctrl", 0x0320_0000, 0x0001_0000), # interrupt controller
("host_shm", 0x2000_0000, 0x0001_0000), # host command/response buffer
("magic_ret", MAGIC_RET, PAGE), # 0x1FFF0000, clear of SYS_DRV scratch
("sys_stack", 0x1000_0000, 0x0010_0000), # unified emulator-owned stack
]
# where each component's image loads (matches the ELF PT_LOAD base)
IMAGE_BASE = {"sos": 0x0, "sys_drv": 0xE00100, "asd": 0xD00100,
"hdcp": 0x400100, "dtm": 0x400100, "ras": 0x400100}
class System:
def __init__(self, resident=("sos", "sys_drv", "asd"), ta=None, root=None):
self.uc = Uc(UC_ARCH_ARM, UC_MODE_THUMB | UC_MODE_LITTLE_ENDIAN)
for _, base, size in SYSTEM_REGIONS:
self.uc.mem_map(base, size, UC_PROT_ALL)
self.images = {}
self.faults = []
self.svc_log = []
self.rpc_log = []
for name in resident:
self._load(name, root)
if ta:
self._load(ta, root)
self.uc.hook_add(UC_HOOK_INTR, self._on_svc)
self.uc.hook_add(UC_HOOK_MEM_INVALID, self._on_invalid)
def _load(self, name, root):
path = loader.resolve(name, root)
entry, placed = loader.load_image(self.uc, path, base=IMAGE_BASE[name])
self.images[name] = dict(path=path, entry=entry, placed=placed)
# -------------------------------------------------------------- memory I/O
def rd32(self, a): return int.from_bytes(self.uc.mem_read(a, 4), "little")
def wr32(self, a, v): self.uc.mem_write(a, int(v & 0xFFFFFFFF).to_bytes(4, "little"))
def read(self, a, n): return bytes(self.uc.mem_read(a, n))
def write(self, a, b): self.uc.mem_write(a, bytes(b))
# ------------------------------------------------------------------- hooks
def _on_svc(self, uc, intno, user):
from .svc import decode_svc_num
num, site = decode_svc_num(uc)
self.svc_log.append((site, num))
if num == 0xF2:
self.route_svc_f2(uc)
# other SVCs: benign default (a full model would run sOS's real handler)
def route_svc_f2(self, uc):
"""Hook point for real inter-component RPC routing.
On hardware SVC 0xF2 traps to sOS's svc_f2_rpc_gate (0x22B4), which
validates the caller, does a DACR domain switch, and hands off to the
destination service task (SYS_DRV / ASD / crypto). To run that for real
we must NEST execution: save the caller frame, enter the gate with the
caller's R0/R1, run until it returns, then resume the caller with R0.
Not yet implemented -- logged so harnesses can see the boundary.
"""
dest = uc.reg_read(UC_ARM_REG_R0)
req = uc.reg_read(UC_ARM_REG_R1)
self.rpc_log.append((dest, req))
uc.reg_write(UC_ARM_REG_R0, 0)
def _on_invalid(self, uc, access, address, size, value, user):
pc = uc.reg_read(UC_ARM_REG_PC)
self.faults.append((access, address, pc))
return False
# -------------------------------------------------------------- execution
def run_function(self, addr, args=(), thumb=True, timeout=1_000_000, count=0):
"""Run a function anywhere in the unified space.
`thumb` selects the instruction set (a full model would vote on it from
call sites, like Component.mode_of; here it is explicit). The Thumb bit of
the entry and of LR is set accordingly.
"""
regs = [UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_R3]
for r, v in zip(regs, list(args)[:4]):
self.uc.reg_write(r, v & 0xFFFFFFFF)
self.uc.reg_write(UC_ARM_REG_SP, 0x1010_0000 - 0x40)
self.uc.reg_write(UC_ARM_REG_LR, MAGIC_RET | 1) # return in Thumb to the sentinel
start = (addr | 1) if thumb else (addr & ~1)
try:
self.uc.emu_start(start, MAGIC_RET, timeout=timeout, count=count)
except unicorn.UcError:
pc = self.uc.reg_read(UC_ARM_REG_PC)
if pc in (MAGIC_RET, MAGIC_RET & ~1):
return self.uc.reg_read(UC_ARM_REG_R0)
raise
return self.uc.reg_read(UC_ARM_REG_R0)
|