mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
Fix defects found in a code sweep and a TRM cross-check
Security Engine: CTR ignored its input and returned the raw keystream; CBC encrypt skipped the IV on the first block and saved the wrong chaining value, so every CMAC over more than one block was wrong; OFB was broken. The datapath now follows INPUT_SEL/XOR_POS/VCTRAM_SEL the way bdk drives it, and matches OpenSSL for ECB, CBC, OFB, CTR and CMAC. SHA-256 works in parts (SHA_CONTINUE), SE_OP_CTX_SAVE reads keys back as se_aes_ctx_get_keys() expects, unwrap honours the word quad, guest block counts and LL sizes are bounded, and unreachable buffers end the op with an error. Soft reboot now resets the whole SoC (GPIO/pinmux cache, UARTs, I2C, PMC, CAR, flow controller, SE, touch, SDMMC, display, ACTMON, ...) while keeping what hardware keeps (PMC SCRATCH0, the RTC, and the PMIC unless the reboot is a power cycle). DRAM gets fresh zero pages instead of a memset that made all 2 GB resident; stale BPMP translations are dropped; CPSR returns to SVC; scripts restart. Execution: a branch to address 0 no longer hangs the emulator (until=0 made PC 0 an exit); bdk's reset-then-bpmp_halt() reboots instead of exiting; an untimed halt with CPU0 running parks the BPMP and lets CPU0 continue; CPU0 starts in the same batch it is released in; CPU0 drops translations of pages the BPMP rewrote; shared models no longer see time run backwards. TRM cross-check: RST_CPUG_CMPLX resets to 0x2000feef and CPU0 runs on CPURESET0/CORERESET0/L2RESET/NONCPURESET (bit names corrected); RAM repair clears REQ and sets STS, and is no longer a run condition; refused releases are re-evaluated; MSELECT ENABLE_PCIE_APERTURE gates PCIe; RST_DEVICES_W/Y direct writes land; DL_UP is read-only; the whole config aperture and memory window decode; OSC_CTRL reports 38.4 MHz; PLLE/PLLREFE no longer get bit 27 forced into a divider; PMC is a R/W register file; RTC SECONDS counts; FLOW halt modes follow 17.2.2 and STOP_UNTIL_IRQ wakes on the armed TMR; UART IIR/IER/ loopback MSR; I2C NOACK code and empty I2C4/I2C6 buses; pinmux reset values; the COP ACTMON counts halt time. Peripherals: MAX77620 LDO power-good follows the rail (hwtest's gamecard probe no longer fails), the Erista GPU MAX77621 answers at 0x1C, regulators ACK per generation, RTC CONTROL is binary/24h, PS3 card detect reads "slot empty", packet-mode I2C parses 4 bytes per word, rawnand I/O splits at part boundaries, eMMC returns to its user area on CMD0, ADMA2 descriptors decode per spec, SD CMD6/ACMD13 data reaches the programmed buffer, ERR_INTERRUPT is reported, R1 follows block commands. Frontend and tooling: touch events queue instead of overwriting each other, map through rotation overrides and bdk's edge offset, and use finger ID 0; a scripted press is always seen before its release; zeroed fuses persist in the ini; dropped UTF-8 paths open on Windows; bad --input-script is an error; test scripts check exit status and missing arguments; make_sd.py quotes names and picks a FAT32-valid cluster size; the Dockerfile marks the mount safe for git. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
This commit is contained in:
26 files changed
+1615
-588
No files matched your search
+103
-23
@@ -1,7 +1,10 @@
|
||||
#include "ccplex.h"
|
||||
#include "bpmp.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <unicorn/unicorn.h>
|
||||
|
||||
@@ -27,10 +30,14 @@
|
||||
#define CLK_V_MSELECT 3
|
||||
#define CAR_RST_CPUG_CMPLX_SET 0x450
|
||||
#define CAR_RST_CPUG_CMPLX_CLR 0x454
|
||||
// The bits ccplex_boot_cpu0() clears for CPU0 - CPURESET0, DBGRESET0,
|
||||
// CORERESET0 and bit 30 - and NONCPURESET, which it clears first. The core
|
||||
// runs once all five are clear.
|
||||
#define RST_CPU0_MASK ((1u << 0) | (1u << 16) | (1u << 24) | (1u << 30))
|
||||
// RST_CPUG_CMPLX (TRM 5.2.152, reset 0x2000feef). CPU0 runs once nCPUPORESET
|
||||
// (CPURESET0, bit 0), nCORERESET (CORERESET0, bit 16), nL2RESET (bit 24) and
|
||||
// the nonCPU region's reset (NONCPURESET, bit 29) are all released.
|
||||
// ccplex_boot_cpu0() also clears PRESETDBG (bit 30), but that only resets
|
||||
// the CoreSight debug logic, which a core runs without; the DBGRESET/
|
||||
// WDRESET/DERESET bits in 15:4 are reserved on this SoC.
|
||||
#define RST_CPUG_CMPLX_RESET 0x2000FEEFu
|
||||
#define RST_CPU0_MASK ((1u << 0) | (1u << 16) | (1u << 24))
|
||||
#define RST_NONCPU (1u << 29)
|
||||
|
||||
// SB block, SYSREG + 0x200.
|
||||
@@ -94,7 +101,7 @@ namespace {
|
||||
enum class Cpu0 { Off, Running, Hung };
|
||||
|
||||
struct Model {
|
||||
uint32_t rst_cpug = 0xFFFFFFFFu; // everything held at power-on
|
||||
uint32_t rst_cpug = RST_CPUG_CMPLX_RESET;
|
||||
uint32_t clk_enb_v = 0;
|
||||
uint32_t cclk_burst = 0;
|
||||
uint32_t pllx_base = 0;
|
||||
@@ -122,6 +129,15 @@ struct Model {
|
||||
|
||||
Model m;
|
||||
EmuState *g_state = nullptr;
|
||||
|
||||
// Pages CPU0 has executed code from, with the bytes it translated; see
|
||||
// drop_stale_code().
|
||||
struct CodePage {
|
||||
uint64_t base;
|
||||
uint8_t bytes[4096];
|
||||
};
|
||||
std::vector<std::unique_ptr<CodePage>> code_pages;
|
||||
uint64_t last_code_page = ~0ULL;
|
||||
uc_hook h_block, h_unmapped, h_intr;
|
||||
|
||||
bool held() { return (m.rst_cpug & (RST_CPU0_MASK | RST_NONCPU)) != 0; }
|
||||
@@ -162,8 +178,6 @@ const char *boot_blocked(EmuState *state) {
|
||||
return "CCLK runs from PLLX but PLLX is disabled";
|
||||
if (!(m.clk_enb_v & (1u << CLK_V_MSELECT))) return "MSELECT clock disabled";
|
||||
if (!pcie_mselect_up()) return "MSELECT still in reset";
|
||||
if (!flow_ram_repair_done())
|
||||
return "fast-cluster RAM repair never requested (FLOW_CTLR_RAM_REPAIR)";
|
||||
if (!(m.sb_rst_lo & SB_AA64_RST_AARCH64_MODE_EN))
|
||||
return "SB_AA64_RESET_LOW has no AArch64 vector (AArch32 CPU boot is not modelled)";
|
||||
if (!entry_mapped(entry_vector())) return "reset vector outside IRAM/DRAM";
|
||||
@@ -171,6 +185,8 @@ const char *boot_blocked(EmuState *state) {
|
||||
}
|
||||
|
||||
void destroy_engine() {
|
||||
code_pages.clear();
|
||||
last_code_page = ~0ULL;
|
||||
if (!m.uc)
|
||||
return;
|
||||
mmio_unmap_bus(m.uc);
|
||||
@@ -199,6 +215,43 @@ void stop_cpu0(const char *why) {
|
||||
|
||||
// ---- CPU0 engine hooks ---------------------------------------------------
|
||||
|
||||
// ---- Code the BPMP rewrites under CPU0 ----------------------------------
|
||||
//
|
||||
// Stores the BPMP makes into shared DRAM/IRAM go through its own engine, so
|
||||
// this engine's translation cache never hears of them: a stub loaded for a
|
||||
// new job at the address of the last one would run the old one's code. A
|
||||
// full flush before every slice cures that but costs tens of milliseconds a
|
||||
// time (it walks the page descriptors of the whole 44-bit space), so instead
|
||||
// the pages CPU0 has executed from are remembered with a copy of their
|
||||
// bytes, and before each slice any that changed have just their own
|
||||
// translations dropped.
|
||||
void note_code_page(uint64_t address) {
|
||||
uint64_t base = address & ~0xFFFULL;
|
||||
if (base == last_code_page)
|
||||
return;
|
||||
last_code_page = base;
|
||||
for (auto &p : code_pages)
|
||||
if (p->base == base)
|
||||
return;
|
||||
const uint8_t *src = host_ptr(base, 4096);
|
||||
if (!src || code_pages.size() >= 256)
|
||||
return;
|
||||
auto p = std::make_unique<CodePage>();
|
||||
p->base = base;
|
||||
memcpy(p->bytes, src, 4096);
|
||||
code_pages.push_back(std::move(p));
|
||||
}
|
||||
|
||||
void drop_stale_code() {
|
||||
for (auto &p : code_pages) {
|
||||
const uint8_t *now = host_ptr(p->base, 4096);
|
||||
if (!now || memcmp(now, p->bytes, 4096) == 0)
|
||||
continue;
|
||||
uc_ctl_remove_cache(m.uc, p->base, p->base + 4096);
|
||||
memcpy(p->bytes, now, 4096);
|
||||
}
|
||||
}
|
||||
|
||||
void commit_pending() {
|
||||
m.ns += m.pending * NS_PER_INSN;
|
||||
m.retired += m.pending;
|
||||
@@ -228,6 +281,7 @@ void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *) {
|
||||
uc_emu_stop(uc);
|
||||
return;
|
||||
}
|
||||
note_code_page(address);
|
||||
// Every A64 instruction is four bytes.
|
||||
m.pending = size >= 4 ? size / 4 : 1;
|
||||
if (const uint8_t *p = host_ptr(address + size - 4, 4)) {
|
||||
@@ -288,8 +342,9 @@ bool start_cpu0(EmuState *state) {
|
||||
|
||||
// IRAM, DRAM and TZRAM are the same host memory the BPMP engine maps, so
|
||||
// a stub the BPMP copies into DRAM and the mailbox both cores poll are one
|
||||
// set of bytes. CPU0's engine is new on every release, so it cannot hold
|
||||
// stale translations of code the BPMP has rewritten since the last boot.
|
||||
// set of bytes. Stores from the other engine bypass this one's self-
|
||||
// modifying-code tracking, so ccplex_run() drops its translations before
|
||||
// every slice.
|
||||
err = uc_mem_map_ptr(uc, IRAM_BASE, IRAM_SIZE, UC_PROT_ALL, state->iram_ptr);
|
||||
if (err == UC_ERR_OK)
|
||||
err = uc_mem_map_ptr(uc, DRAM_BASE, DRAM_WINDOW_SIZE, UC_PROT_ALL,
|
||||
@@ -330,6 +385,10 @@ bool start_cpu0(EmuState *state) {
|
||||
m.bus_accesses = 0;
|
||||
m.cpu0 = Cpu0::Running;
|
||||
m.boots++;
|
||||
// Released from a BPMP register write: end the BPMP's batch at the next
|
||||
// block so CPU0's first slice starts now, not up to a batch (~10 ms) later.
|
||||
if (g_bus_master == BUS_BPMP)
|
||||
bpmp_yield();
|
||||
printf("[ccplex] CPU0 released: AArch64 EL3, MMU off, entry 0x%08llX "
|
||||
"(boot #%u)\n", (unsigned long long)entry, m.boots);
|
||||
fflush(stdout);
|
||||
@@ -352,6 +411,14 @@ void try_release(EmuState *state) {
|
||||
start_cpu0(state);
|
||||
}
|
||||
|
||||
// Out of reset but refused: on silicon the core simply starts the moment the
|
||||
// last missing piece (clock, partition, rail, vector) arrives, so every such
|
||||
// change looks again.
|
||||
void maybe_release(EmuState *state) {
|
||||
if (m.cpu0 == Cpu0::Off && !held() && state)
|
||||
try_release(state);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// ==========================================================================
|
||||
@@ -375,10 +442,10 @@ void ccplex_reset(EmuState *state) {
|
||||
|
||||
void ccplex_car_write(EmuState *state, uint32_t offset, uint32_t val) {
|
||||
switch (offset) {
|
||||
case CAR_CCLK_BURST_POLICY: m.cclk_burst = val; break;
|
||||
case CAR_PLLX_BASE: m.pllx_base = val; break;
|
||||
case CAR_CLK_OUT_ENB_V: m.clk_enb_v = val; break;
|
||||
case CAR_CLK_ENB_V_SET: m.clk_enb_v |= val; break;
|
||||
case CAR_CCLK_BURST_POLICY: m.cclk_burst = val; maybe_release(state); break;
|
||||
case CAR_PLLX_BASE: m.pllx_base = val; maybe_release(state); break;
|
||||
case CAR_CLK_OUT_ENB_V: m.clk_enb_v = val; maybe_release(state); break;
|
||||
case CAR_CLK_ENB_V_SET: m.clk_enb_v |= val; maybe_release(state); break;
|
||||
case CAR_CLK_ENB_V_CLR:
|
||||
m.clk_enb_v &= ~val;
|
||||
if ((val & (1u << CLK_V_CPUG)) && m.cpu0 == Cpu0::Running) {
|
||||
@@ -430,24 +497,23 @@ uint32_t ccplex_sb_read(uint32_t offset) {
|
||||
}
|
||||
|
||||
void ccplex_sb_write(EmuState *state, uint32_t offset, uint32_t val) {
|
||||
(void)state;
|
||||
switch (offset) {
|
||||
case SB_CSR:
|
||||
// NS_RST_VEC_WR_DIS (bit 1) is sticky until reset. It only locks out
|
||||
// non-secure writers; the BPMP is secure, so the vector stays writable
|
||||
// from here either way.
|
||||
// non-secure writers, and the vector write below does not check it.
|
||||
m.sb_csr |= val;
|
||||
break;
|
||||
case SB_AA64_RESET_LOW: m.sb_rst_lo = val; break;
|
||||
case SB_AA64_RESET_HIGH: m.sb_rst_hi = val; break;
|
||||
case SB_AA64_RESET_LOW: m.sb_rst_lo = val; maybe_release(state); break;
|
||||
case SB_AA64_RESET_HIGH: m.sb_rst_hi = val & 0xFFF; maybe_release(state); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void ccplex_partition_changed(EmuState *state, int part, bool on) {
|
||||
(void)state;
|
||||
if (on)
|
||||
if (on) {
|
||||
maybe_release(state);
|
||||
return;
|
||||
}
|
||||
if (part == POWER_RAIL_CRAIL) stop_cpu0("CRAIL partition powergated");
|
||||
if (part == POWER_RAIL_C0NC) stop_cpu0("C0NC partition powergated");
|
||||
if (part == POWER_RAIL_CE0) stop_cpu0("CE0 partition powergated");
|
||||
@@ -456,14 +522,20 @@ void ccplex_partition_changed(EmuState *state, int part, bool on) {
|
||||
void ccplex_rail_changed(EmuState *state) {
|
||||
if (m.cpu0 != Cpu0::Off && !cpu_rail_on(state))
|
||||
stop_cpu0("CPU rail switched off");
|
||||
else
|
||||
maybe_release(state);
|
||||
}
|
||||
|
||||
void ccplex_preconditions_changed(EmuState *state) { maybe_release(state); }
|
||||
|
||||
void ccplex_bus_tick(EmuState *state) {
|
||||
m.ns += NS_PER_BUS_ACCESS;
|
||||
m.bus_accesses++;
|
||||
state->emu_usec = m.ns / 1000;
|
||||
}
|
||||
|
||||
uint64_t ccplex_now_us() { return m.ns / 1000; }
|
||||
|
||||
bool ccplex_cpu0_running() { return m.cpu0 == Cpu0::Running && m.uc; }
|
||||
|
||||
void ccplex_run(EmuState *state, uint64_t until_us) {
|
||||
@@ -474,8 +546,12 @@ void ccplex_run(EmuState *state, uint64_t until_us) {
|
||||
return;
|
||||
|
||||
// While CPU0 runs, it is the bus master and "now" is its clock: every
|
||||
// register model reads state->emu_usec, and must see CPU0's time, not the
|
||||
// BPMP's. Both are put back afterwards.
|
||||
// register model reads state->emu_usec, and must see CPU0's time - a
|
||||
// model timing CPU0's own sequence (PERST# after Trefclkstable, say) has
|
||||
// to measure it on the clock CPU0 waits on. Both are put back afterwards.
|
||||
// CPU0 trails the BPMP by up to a slice, so a model both cores touch sees
|
||||
// time step back at the switch; such models must not assume `now` only
|
||||
// grows.
|
||||
uint64_t bpmp_usec = state->emu_usec;
|
||||
BusMaster prev = g_bus_master;
|
||||
g_bus_master = BUS_CPU0;
|
||||
@@ -483,7 +559,11 @@ void ccplex_run(EmuState *state, uint64_t until_us) {
|
||||
m.target_ns = target;
|
||||
m.in_run = true;
|
||||
|
||||
uc_err err = uc_emu_start(m.uc, m.pc, 0, 0, 0);
|
||||
drop_stale_code();
|
||||
|
||||
// `until` is an address an A64 PC can never hold (it is not 4-aligned).
|
||||
// 0 would make a branch to address 0 end every slice before it starts.
|
||||
uc_err err = uc_emu_start(m.uc, m.pc, ~0ULL, 0, 0);
|
||||
|
||||
commit_pending();
|
||||
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
||||
|
||||
Reference in new issue
Block a user