mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
Boot Linux on CPU0
This commit is contained in:
1 parent
d5bf5922ed
commit
20949b4242
21 files changed
+1861
-21
No files matched your search
@@ -28,6 +28,16 @@ CXXFLAGS = -Wall -g -O2 -std=c++17 -MMD -MP -I. -I$(IMGUI_DIR) -I$(IMGUI_DIR)/ba
|
|||||||
LDFLAGS =
|
LDFLAGS =
|
||||||
LIBS = -lunicorn -lSDL2 -lpthread
|
LIBS = -lunicorn -lSDL2 -lpthread
|
||||||
|
|
||||||
|
# UNICORN_DIR=<prefix>: build against a Unicorn installed there (statically)
|
||||||
|
# instead of the system one. Booting a kernel on CPU0 needs Unicorn 2.1 or
|
||||||
|
# later: 2.0.x checks guest accesses against its memory map by VIRTUAL
|
||||||
|
# address once the guest MMU is on, so every access through a kernel's
|
||||||
|
# TTBR1 mapping looks unmapped. Everything else works on either. Run
|
||||||
|
# `make clean` when switching, since the objects depend on its headers.
|
||||||
|
ifneq ($(strip $(UNICORN_DIR)),)
|
||||||
|
CXXFLAGS += -I$(UNICORN_DIR)/include
|
||||||
|
endif
|
||||||
|
|
||||||
# pkg-config for SDL2 if available
|
# pkg-config for SDL2 if available
|
||||||
SDL2_CFLAGS := $(shell pkg-config --cflags sdl2 2>/dev/null)
|
SDL2_CFLAGS := $(shell pkg-config --cflags sdl2 2>/dev/null)
|
||||||
SDL2_LIBS := $(shell pkg-config --libs sdl2 2>/dev/null)
|
SDL2_LIBS := $(shell pkg-config --libs sdl2 2>/dev/null)
|
||||||
@@ -37,6 +47,11 @@ ifneq ($(SDL2_CFLAGS),)
|
|||||||
LIBS = -lunicorn $(SDL2_LIBS) -lpthread
|
LIBS = -lunicorn $(SDL2_LIBS) -lpthread
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# After the SDL2 block above, which sets LIBS again.
|
||||||
|
ifneq ($(strip $(UNICORN_DIR)),)
|
||||||
|
LIBS := $(UNICORN_DIR)/lib/libunicorn.a $(if $(SDL2_LIBS),$(SDL2_LIBS),-lSDL2) -lpthread -lm
|
||||||
|
endif
|
||||||
|
|
||||||
# ---- Windows cross-build ---------------------------------------------------
|
# ---- Windows cross-build ---------------------------------------------------
|
||||||
#
|
#
|
||||||
# `make windows` builds rcm_emu.exe with the MinGW toolchain. Both deps come
|
# `make windows` builds rcm_emu.exe with the MinGW toolchain. Both deps come
|
||||||
@@ -98,6 +113,9 @@ tests/ccplex/payload.bin: tests/ccplex/payload.c tests/ccplex/link.ld
|
|||||||
$(TEST_CC) $(TEST_CFLAGS) -T tests/ccplex/link.ld -o tests/ccplex/payload.elf $<
|
$(TEST_CC) $(TEST_CFLAGS) -T tests/ccplex/link.ld -o tests/ccplex/payload.elf $<
|
||||||
$(TEST_OBJCOPY) -O binary tests/ccplex/payload.elf $@
|
$(TEST_OBJCOPY) -O binary tests/ccplex/payload.elf $@
|
||||||
|
|
||||||
|
tests/kernelcpu/payload.bin: tests/kernelcpu/payload.c tests/kernelcpu/cpu0.S tests/kernelcpu/link.ld
|
||||||
|
sh tests/kernelcpu/build.sh
|
||||||
|
|
||||||
tests/se/payload.bin: tests/se/payload.c tests/se/vectors.h tests/se/link.ld
|
tests/se/payload.bin: tests/se/payload.c tests/se/vectors.h tests/se/link.ld
|
||||||
$(TEST_CC) $(TEST_CFLAGS) -T tests/se/link.ld -o tests/se/payload.elf $<
|
$(TEST_CC) $(TEST_CFLAGS) -T tests/se/link.ld -o tests/se/payload.elf $<
|
||||||
$(TEST_OBJCOPY) -O binary tests/se/payload.elf $@
|
$(TEST_OBJCOPY) -O binary tests/se/payload.elf $@
|
||||||
@@ -116,8 +134,9 @@ tests/display/payload_%.bin: tests/display/payload.c tests/display/link.ld
|
|||||||
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/display/link.ld -o tests/display/payload_$*.elf $<
|
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/display/link.ld -o tests/display/payload_$*.elf $<
|
||||||
$(TEST_OBJCOPY) -O binary tests/display/payload_$*.elf $@
|
$(TEST_OBJCOPY) -O binary tests/display/payload_$*.elf $@
|
||||||
|
|
||||||
test: $(OUTPUT) tests/ccplex/payload.bin tests/se/payload.bin $(USB_PAYLOADS) $(DISPLAY_PAYLOADS)
|
test: $(OUTPUT) tests/ccplex/payload.bin tests/kernelcpu/payload.bin tests/se/payload.bin $(USB_PAYLOADS) $(DISPLAY_PAYLOADS)
|
||||||
sh tests/ccplex/run.sh ./$(OUTPUT) tests/ccplex/payload.bin
|
sh tests/ccplex/run.sh ./$(OUTPUT) tests/ccplex/payload.bin
|
||||||
|
sh tests/kernelcpu/run.sh ./$(OUTPUT) tests/kernelcpu/payload.bin
|
||||||
sh tests/se/run.sh ./$(OUTPUT) tests/se/payload.bin
|
sh tests/se/run.sh ./$(OUTPUT) tests/se/payload.bin
|
||||||
sh tests/usb/run.sh ./$(OUTPUT) tests/usb
|
sh tests/usb/run.sh ./$(OUTPUT) tests/usb
|
||||||
sh tests/display/run.sh ./$(OUTPUT) tests/display
|
sh tests/display/run.sh ./$(OUTPUT) tests/display
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ slow or destructive.
|
|||||||
checks it against the SD image, writes a few sectors back unchanged and
|
checks it against the SD image, writes a few sectors back unchanged and
|
||||||
ejects it. With `--usb-host nbd` the PC serves the disk over NBD instead,
|
ejects it. With `--usb-host nbd` the PC serves the disk over NBD instead,
|
||||||
so it can be mounted on the machine running the emulator.
|
so it can be mounted on the machine running the emulator.
|
||||||
|
- Boot a Linux kernel on CPU0 (switchroot's L4T 4.9), with a GIC-400, the
|
||||||
|
generic timer, PSCI and the clock controller's reset values for it. See
|
||||||
|
[Booting Linux on CPU0](#booting-linux-on-cpu0).
|
||||||
- Tweak emulated hardware live (battery, charger, thermal, USB-PD, PMIC,
|
- Tweak emulated hardware live (battery, charger, thermal, USB-PD, PMIC,
|
||||||
fuses, SD insertion, the USB host) from a side window. See [Live hardware tweaks](#live-hardware-tweaks).
|
fuses, SD insertion, the USB host) from a side window. See [Live hardware tweaks](#live-hardware-tweaks).
|
||||||
- Inspect each Tegra UART port (TX history) and inject keystrokes into the
|
- Inspect each Tegra UART port (TX history) and inject keystrokes into the
|
||||||
@@ -55,6 +58,16 @@ sudo apt-get install -y build-essential libunicorn-dev libsdl2-dev
|
|||||||
make
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Booting a kernel on CPU0 needs Unicorn 2.1 or later: 2.0.x, which some
|
||||||
|
distributions still ship, checks guest accesses by virtual address once the
|
||||||
|
guest MMU is on. Build one into a prefix and point the Makefile at it, which
|
||||||
|
links it statically (`make clean` first when switching):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make UNICORN_DIR=$HOME/opt/unicorn-2.1
|
||||||
|
ldd rcm_emu | grep unicorn # prints nothing when the static one is in
|
||||||
|
```
|
||||||
|
|
||||||
### Build (Docker)
|
### Build (Docker)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -180,10 +193,37 @@ run: CPU rail off (MAX77621 VOUT_EN / MAX77620 GPIO5 EN pin)`. PCIe and
|
|||||||
MSELECT answer CPU0 only; the BPMP reads all ones from them, as measured on
|
MSELECT answer CPU0 only; the BPMP reads all ones from them, as measured on
|
||||||
real consoles. Details in [DESIGN.md](DESIGN.md#ccplex-cpu0).
|
real consoles. Details in [DESIGN.md](DESIGN.md#ccplex-cpu0).
|
||||||
|
|
||||||
|
### Booting Linux on CPU0
|
||||||
|
|
||||||
|
`linux/` boots an arm64 kernel Image, switchroot's L4T 4.9 for instance, the
|
||||||
|
way a bootloader would hand over to it:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
linux/build.sh # linux/boot.bin (devkitARM + devkitA64)
|
||||||
|
linux/mkdtb.sh tegra210-odin.dtb odin-emu.dtb # console on UART-B, one CPU
|
||||||
|
linux/run.sh Image odin-emu.dtb boot.log 120
|
||||||
|
```
|
||||||
|
|
||||||
|
`--load FILE@ADDR` places the Image at 0x80080000 and the device tree at
|
||||||
|
0x83000000 before the payload starts; `linux/boot.bin` sets the clocks and
|
||||||
|
the CPU rail, releases CPU0 into `linux/shim.S`, which drops to EL1 with the
|
||||||
|
device tree in x0, and halts the BPMP. For the kernel, CPU0 has a GIC-400
|
||||||
|
(distributor and CPU interface), the generic timer counting emulated time
|
||||||
|
at 19.2 MHz, interrupts taken at block boundaries, BRK and undefined
|
||||||
|
instructions taken to its own vectors (WARN and BUG), PSCI on SMC, WFI
|
||||||
|
sleeping to the next timer, TMR10..13 as clock event timers, and every
|
||||||
|
unmodelled page of the peripheral band mapped as plain registers on first
|
||||||
|
access. The clock controller's registers read their TRM reset values until
|
||||||
|
written ([t210/car_reset.h](t210/car_reset.h)), since the kernel's clock
|
||||||
|
driver walks hundreds of them. `RCM_EMU_CPU0_PROFILE=1` prints where CPU0 is
|
||||||
|
once a second of real time, to tell a hung kernel from a slow one. The
|
||||||
|
console comes out on UART-B; the saved runs reach the kernel's core
|
||||||
|
initialisation.
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
make test # CCPLEX, SE, USB + display regression payloads (arm-none-eabi-gcc, python3)
|
make test # CCPLEX, kernel CPU, SE, USB + display regression payloads (arm-none-eabi-gcc, devkitA64, python3)
|
||||||
tests/hwtest/build.sh build-hwtest # build hwtest-rcm (+ gcc-aarch64-linux-gnu)
|
tests/hwtest/build.sh build-hwtest # build hwtest-rcm (+ gcc-aarch64-linux-gnu)
|
||||||
tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin
|
tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin
|
||||||
```
|
```
|
||||||
@@ -192,6 +232,8 @@ tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin
|
|||||||
refusal of an unpowered release, the EL3 entry, the mailbox, a stopped clock,
|
refusal of an unpowered release, the EL3 entry, the mailbox, a stopped clock,
|
||||||
WFE parking and the reset, then runs again with a dead CPU rail and dead
|
WFE parking and the reset, then runs again with a dead CPU rail and dead
|
||||||
bucks (`--fault`) and checks the RAM repair is held and the core stays dark;
|
bucks (`--fault`) and checks the RAM repair is held and the core stays dark;
|
||||||
|
one drops CPU0 to EL1 and checks it takes timer interrupts through the GIC,
|
||||||
|
a BRK into its own vector and a PSCI call over SMC;
|
||||||
one checks the Security Engine's RSA, SHA-256, AES
|
one checks the Security Engine's RSA, SHA-256, AES
|
||||||
and RNG against vectors computed in Python; one brings up a USB mass storage
|
and RNG against vectors computed in Python; one brings up a USB mass storage
|
||||||
gadget on each device controller (and a HID gadget) for the `--usb-host` PC to
|
gadget on each device controller (and a HID gadget) for the `--usb-host` PC to
|
||||||
@@ -322,6 +364,7 @@ needed.
|
|||||||
| `--wifi-radio` | `healthy` \| `faulty` \| `absent` | The WLAN half of the same CYW4356: a PCI Express endpoint on root port 1, reachable only from CPU0 (the BPMP reads all ones, as on silicon). `healthy` trains the link, enumerates as `14E4:43EC` and answers a ChipCommon ChipID read with `0x4356`; `faulty` still trains and enumerates but reads all ones on the backplane (live PCIe front-end, dead radio die); `absent` never leaves detect. The model also enforces the datasheet's power-up ordering, so a payload that releases PERST# too early gets a link that stays down and a `[pcie]` line naming the reason. Overrides `[wifi] radio` in the ini. Default `healthy`. |
|
| `--wifi-radio` | `healthy` \| `faulty` \| `absent` | The WLAN half of the same CYW4356: a PCI Express endpoint on root port 1, reachable only from CPU0 (the BPMP reads all ones, as on silicon). `healthy` trains the link, enumerates as `14E4:43EC` and answers a ChipCommon ChipID read with `0x4356`; `faulty` still trains and enumerates but reads all ones on the backplane (live PCIe front-end, dead radio die); `absent` never leaves detect. The model also enforces the datasheet's power-up ordering, so a payload that releases PERST# too early gets a link that stays down and a `[pcie]` line naming the reason. Overrides `[wifi] radio` in the ini. Default `healthy`. |
|
||||||
| `--fault` | `NAME[,NAME…]` \| `list` | Break a chip or a rail; repeatable. A **chip** fault (`max77812`, `max77621_cpu`, `max77621_gpu`, `bq24193`, `max17050`, `tmp451`, `bm92t36`) makes the part NAK its I2C address, refuse writes and take every rail it supplies down with it. A **rail** fault (`vdd_cpu`) leaves the regulator answering and reading back as enabled while its output never comes up, so only what the rail feeds notices: the CPU RAM repair never completes and CPU0 cannot run. `list` prints the names and exits. Faults survive a payload reboot, like the hardware they model, and add to `[faults]` in the ini (`NAME=dead`). See [DESIGN.md](DESIGN.md#fault-injection). |
|
| `--fault` | `NAME[,NAME…]` \| `list` | Break a chip or a rail; repeatable. A **chip** fault (`max77812`, `max77621_cpu`, `max77621_gpu`, `bq24193`, `max17050`, `tmp451`, `bm92t36`) makes the part NAK its I2C address, refuse writes and take every rail it supplies down with it. A **rail** fault (`vdd_cpu`) leaves the regulator answering and reading back as enabled while its output never comes up, so only what the rail feeds notices: the CPU RAM repair never completes and CPU0 cannot run. `list` prints the names and exits. Faults survive a payload reboot, like the hardware they model, and add to `[faults]` in the ini (`NAME=dead`). See [DESIGN.md](DESIGN.md#fault-injection). |
|
||||||
| `--usb-host` | `nbd[:port]` (optional) | A PC on the USB-C port. It enumerates whatever USB gadget the payload brings up; a mass storage disk is read back (checked against the SD image), its last 8 sectors are written back unchanged and read again unless it is write-protected, and it is ejected; HID reports are polled. With `nbd` the disk is served over NBD on 127.0.0.1 (port 10809 by default) until the client disconnects, then ejected - see [Mounting a gadget's disk](#mounting-a-gadgets-disk). Logged as `[usb-host]`; `RCM_USB_TRACE=1` traces every transfer. Overrides `[usb] host` and `[usb] nbd_port` in the ini. |
|
| `--usb-host` | `nbd[:port]` (optional) | A PC on the USB-C port. It enumerates whatever USB gadget the payload brings up; a mass storage disk is read back (checked against the SD image), its last 8 sectors are written back unchanged and read again unless it is write-protected, and it is ejected; HID reports are polled. With `nbd` the disk is served over NBD on 127.0.0.1 (port 10809 by default) until the client disconnects, then ejected - see [Mounting a gadget's disk](#mounting-a-gadgets-disk). Logged as `[usb-host]`; `RCM_USB_TRACE=1` traces every transfer. Overrides `[usb] host` and `[usb] nbd_port` in the ini. |
|
||||||
|
| `--load` | `FILE@ADDR` | Place a file in DRAM before the payload starts, the way a bootloader leaves a kernel and its device tree; repeatable. See [Booting Linux on CPU0](#booting-linux-on-cpu0). |
|
||||||
| `--input-script` | file or spec | Scripted input keyed to emulated time: `<ms> P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, `<ms> TAP <x> <y> [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, `<ms> SNAP <file>` saves the next displayed frame to `<file>` (raw B, G, R, A like `last_fb.rgba`, size in the matching `.meta`), `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. |
|
| `--input-script` | file or spec | Scripted input keyed to emulated time: `<ms> P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, `<ms> TAP <x> <y> [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, `<ms> SNAP <file>` saves the next displayed frame to `<file>` (raw B, G, R, A like `last_fb.rgba`, size in the matching `.meta`), `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. |
|
||||||
| `--auto-pin-recovery` | (none) | Drive the Lockpick PIN-recovery menu without user input. |
|
| `--auto-pin-recovery` | (none) | Drive the Lockpick PIN-recovery menu without user input. |
|
||||||
| `--auto-te-script` | (none) | Drive `recover_pin.te` in TegraExplorer without input. |
|
| `--auto-te-script` | (none) | Drive `recover_pin.te` in TegraExplorer without input. |
|
||||||
@@ -334,10 +377,11 @@ flowchart LR
|
|||||||
root --> top["main.cpp<br/>emu_state.h<br/>Makefile<br/>Dockerfile<br/>README.md / DESIGN.md"]
|
root --> top["main.cpp<br/>emu_state.h<br/>Makefile<br/>Dockerfile<br/>README.md / DESIGN.md"]
|
||||||
root --> t210["t210/<br/>(SoC peripheral models)"]
|
root --> t210["t210/<br/>(SoC peripheral models)"]
|
||||||
root --> display["display/"]
|
root --> display["display/"]
|
||||||
root --> tests["tests/<br/>ccplex/, se/, usb/, display/, hwtest/"]
|
root --> linux["linux/<br/>kernel boot: boot.c, shim.S, run.sh"]
|
||||||
|
root --> tests["tests/<br/>ccplex/, kernelcpu/, se/, usb/, display/, hwtest/"]
|
||||||
|
|
||||||
t210 --> mmio["mmio.{h,cpp}<br/>memory_map.h<br/>regcache.h, tegra_bl.h"]
|
t210 --> mmio["mmio.{h,cpp}<br/>memory_map.h<br/>regcache.h, tegra_bl.h"]
|
||||||
t210 --> cpus["bpmp.{h,cpp} BPMP clock<br/>ccplex.{h,cpp} CPU0 (A57)"]
|
t210 --> cpus["bpmp.{h,cpp} BPMP clock<br/>ccplex.{h,cpp} CPU0 (A57)<br/>gic.{h,cpp} GIC-400"]
|
||||||
t210 --> pcie["pcie.{h,cpp}<br/>root complex + CYW4356"]
|
t210 --> pcie["pcie.{h,cpp}<br/>root complex + CYW4356"]
|
||||||
t210 --> se["se_engine.{h,cpp}<br/>AES-128, SHA-256, RSA, RNG"]
|
t210 --> se["se_engine.{h,cpp}<br/>AES-128, SHA-256, RSA, RNG"]
|
||||||
t210 --> i2c["i2c3.{h,cpp}<br/>STMFTS / FTS4 touch"]
|
t210 --> i2c["i2c3.{h,cpp}<br/>STMFTS / FTS4 touch"]
|
||||||
|
|||||||
+122
@@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* BPMP side of booting a Linux kernel on the emulator's CPU0.
|
||||||
|
*
|
||||||
|
* run.sh has the emulator place the kernel Image at 0x80080000 and the
|
||||||
|
* device tree at 0x83000000 (--load), as a bootloader would have. This
|
||||||
|
* payload powers CPU0 the way bdk's ccplex_boot_cpu0() does (Erista rail),
|
||||||
|
* releases it at shim.S - which drops to EL1 and enters the kernel - and
|
||||||
|
* then halts the BPMP for good, so every emulated cycle goes to CPU0.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef unsigned int u32;
|
||||||
|
|
||||||
|
#define REG(a) (*(volatile u32 *)(a))
|
||||||
|
|
||||||
|
#define TIMERUS 0x60005010u
|
||||||
|
#define CAR 0x60006000u
|
||||||
|
#define FLOW 0x60007000u
|
||||||
|
#define SB 0x6000C200u
|
||||||
|
#define UARTB_THR 0x70006040u
|
||||||
|
#define PMC 0x7000E400u
|
||||||
|
#define I2C5 0x7000D000u
|
||||||
|
|
||||||
|
#define CPU_ENTRY 0xA0000000u
|
||||||
|
#define KERNEL 0x80080000u
|
||||||
|
|
||||||
|
static void putc_(char c) { REG(UARTB_THR) = (u32)(unsigned char)c; }
|
||||||
|
|
||||||
|
static void puts_(const char *s) {
|
||||||
|
while (*s)
|
||||||
|
putc_(*s++);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void puthex(u32 v) {
|
||||||
|
for (int i = 28; i >= 0; i -= 4)
|
||||||
|
putc_("0123456789ABCDEF"[(v >> i) & 0xF]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void udelay(u32 us) {
|
||||||
|
u32 start = REG(TIMERUS);
|
||||||
|
while (REG(TIMERUS) - start < us)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* bdk-style normal-mode I2C write: [reg, val] in CMD_DATA1, 2 bytes, GO. */
|
||||||
|
static void i2c5_write(u32 dev, u32 reg, u32 val) {
|
||||||
|
REG(I2C5 + 0x04) = dev << 1; /* CMD_ADDR0 */
|
||||||
|
REG(I2C5 + 0x0C) = reg | (val << 8); /* CMD_DATA1 */
|
||||||
|
REG(I2C5 + 0x00) = (1u << 1) | (1u << 9); /* CNFG: 2 bytes, write, GO */
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pwrgate_on(u32 part) {
|
||||||
|
if (!(REG(PMC + 0x38) & (1u << part))) /* PWRGATE_STATUS */
|
||||||
|
REG(PMC + 0x30) = part | (1u << 8); /* PWRGATE_TOGGLE */
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RST_CPU0 ((1u << 30) | (1u << 24) | (1u << 16) | (1u << 0))
|
||||||
|
#define RST_NONCPU (1u << 29)
|
||||||
|
|
||||||
|
static void release_cpu0(void) {
|
||||||
|
REG(SB + 0x30) = CPU_ENTRY | 1u; /* SB_AA64_RESET_LOW */
|
||||||
|
REG(SB + 0x34) = 0; /* SB_AA64_RESET_HIGH */
|
||||||
|
REG(CAR + 0x454) = RST_NONCPU; /* RST_CPUG_CMPLX_CLR */
|
||||||
|
REG(CAR + 0x454) = RST_CPU0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void hold_cpu0(void) {
|
||||||
|
REG(CAR + 0x450) = RST_CPU0; /* RST_CPUG_CMPLX_SET */
|
||||||
|
REG(CAR + 0x450) = RST_NONCPU;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include "shim_blob.h"
|
||||||
|
|
||||||
|
__attribute__((section(".text.start"), noreturn))
|
||||||
|
void _start(void) {
|
||||||
|
puts_("linux boot: kernel header ");
|
||||||
|
puthex(((volatile u32 *)KERNEL)[14]); /* 'ARM\x64' at offset 0x38 */
|
||||||
|
putc_('\n');
|
||||||
|
for (u32 i = 0; i < sizeof(shim_blob) / sizeof(shim_blob[0]); i++)
|
||||||
|
((volatile u32 *)CPU_ENTRY)[i] = shim_blob[i];
|
||||||
|
|
||||||
|
/* The clocks a bootloader has set before any OS runs: bdk hw_init()'s
|
||||||
|
* _config_oscillators(), as hekate leaves them for switchroot's chain.
|
||||||
|
* The kernel's super-clock code BUG()s on a burst policy still in
|
||||||
|
* STANDBY, which is what these registers hold until someone writes
|
||||||
|
* them. */
|
||||||
|
REG(CAR + 0x55C) = (REG(CAR + 0x55C) & 0xFFFFFFF3) | 4; /* CLK_M divisor 2 */
|
||||||
|
REG(0x700F0000 + 0x20) = 19200000; /* SYSCTR0_CNTFID0 */
|
||||||
|
REG(0x60005014) = 0x45F; /* TIMERUS_USEC_CFG, 19.2 MHz */
|
||||||
|
REG(CAR + 0x050) = 0x50000071; /* OSC_CTRL: 38.4 MHz */
|
||||||
|
REG(CAR + 0x030) = 0x10; /* CLK_SYSTEM_RATE */
|
||||||
|
REG(CAR + 0x400) = 0; /* CLK_SOURCE_SYS */
|
||||||
|
REG(CAR + 0x028) = 0x20004444; /* SCLK_BURST_POLICY: run, PLLP_OUT2 */
|
||||||
|
REG(CAR + 0x02C) = 0x80000000; /* SUPER_SCLK_DIVIDER */
|
||||||
|
REG(CAR + 0x030) = 2; /* HCLK /1, PCLK /3 */
|
||||||
|
REG(CAR + 0x370) = 0x20000000; /* CCLKLP_BURST_POLICY: run, clk_m */
|
||||||
|
|
||||||
|
i2c5_write(0x3C, 0x40, 0x1C); /* MAX77620 AME_GPIO: GPIO5 plain */
|
||||||
|
i2c5_write(0x3C, 0x3B, 0x09); /* GPIO5: push-pull, out high */
|
||||||
|
i2c5_write(0x1B, 0x00, 0x80 | 55); /* MAX77621 VOUT: on, 0.950 V */
|
||||||
|
REG(CAR + 0x0E0) = (1u << 30) | (2u << 20) | (156u << 8) | 2u; /* PLLX */
|
||||||
|
REG(CAR + 0x3B4) = 4u << 1; /* CLK_SOURCE_MSELECT */
|
||||||
|
REG(CAR + 0x440) = 1u << 3; /* CLK_ENB_V_SET: MSELECT */
|
||||||
|
REG(CAR + 0x020) = 0x20008888; /* CCLK_BURST: PLLX_OUT0_LJ */
|
||||||
|
REG(CAR + 0x024) = 1u << 31; /* SUPER_CCLK_DIVIDER */
|
||||||
|
REG(CAR + 0x440) = 1u << 0; /* CLK_ENB_V_SET: CPUG */
|
||||||
|
pwrgate_on(0); /* CRAIL */
|
||||||
|
pwrgate_on(15); /* C0NC */
|
||||||
|
pwrgate_on(14); /* CE0 */
|
||||||
|
REG(FLOW + 0x40) = 1; /* RAM_REPAIR_REQ */
|
||||||
|
while (!(REG(FLOW + 0x40) & 2))
|
||||||
|
;
|
||||||
|
REG(CAR + 0x434) = 1u << 3; /* RST_DEV_V_CLR: MSELECT */
|
||||||
|
release_cpu0();
|
||||||
|
puts_("linux boot: CPU0 released, BPMP halting\n");
|
||||||
|
|
||||||
|
/* bdk's bpmp_halt(): WAITEVENT | JTAG, no wake source. */
|
||||||
|
REG(FLOW + 0x04) = (2u << 29) | (1u << 28);
|
||||||
|
for (;;)
|
||||||
|
;
|
||||||
|
}
|
||||||
Executable
+24
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Builds linux/boot.bin: shim.S (devkitA64) embedded in the BPMP payload.
|
||||||
|
set -e
|
||||||
|
D=$(dirname "$0")
|
||||||
|
A64=${A64:-/opt/devkitpro/devkitA64/bin/aarch64-none-elf-}
|
||||||
|
ARM=${ARM:-/opt/devkitpro/devkitARM/bin/arm-none-eabi-}
|
||||||
|
${A64}gcc -c -o "$D/shim.o" "$D/shim.S"
|
||||||
|
${A64}ld -Ttext=0xA0000000 -o "$D/shim.elf" "$D/shim.o"
|
||||||
|
${A64}objcopy -O binary "$D/shim.elf" "$D/shim.bin"
|
||||||
|
python3 - "$D/shim.bin" "$D/shim_blob.h" <<'PY'
|
||||||
|
import sys, struct
|
||||||
|
b = open(sys.argv[1], "rb").read()
|
||||||
|
b += b"\0" * (-len(b) % 4)
|
||||||
|
w = struct.unpack("<%dI" % (len(b) // 4), b)
|
||||||
|
with open(sys.argv[2], "w") as f:
|
||||||
|
f.write("/* Generated by build.sh from shim.S. */\nstatic const u32 shim_blob[] = {\n")
|
||||||
|
for i in range(0, len(w), 6):
|
||||||
|
f.write(" " + ", ".join("0x%08x" % x for x in w[i:i + 6]) + ",\n")
|
||||||
|
f.write("};\n")
|
||||||
|
PY
|
||||||
|
${ARM}gcc -march=armv4t -marm -Os -ffreestanding -nostdlib -fno-builtin -I"$D" \
|
||||||
|
-T "$D/link.ld" -o "$D/boot.elf" "$D/boot.c"
|
||||||
|
${ARM}objcopy -O binary "$D/boot.elf" "$D/boot.bin"
|
||||||
|
echo "built $D/boot.bin"
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/* Loaded and entered at 0x40010000, like any RCM payload. */
|
||||||
|
ENTRY(_start)
|
||||||
|
SECTIONS {
|
||||||
|
. = 0x40010000;
|
||||||
|
.text : { KEEP(*(.text.start)) *(.text*) }
|
||||||
|
.rodata : { *(.rodata*) }
|
||||||
|
.data : { *(.data*) }
|
||||||
|
.bss : { *(.bss*) *(COMMON) }
|
||||||
|
/DISCARD/ : { *(.comment) *(.note*) *(.ARM.attributes) *(.ARM.exidx*) }
|
||||||
|
}
|
||||||
Executable
+23
@@ -0,0 +1,23 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# linux/mkdtb.sh IN.dtb OUT.dtb - the switchroot device tree with what the
|
||||||
|
# emulator boot needs: a console on UART-B (the port the emulator prints),
|
||||||
|
# kept past init, one CPU, verbose logging. The bootloader normally edits
|
||||||
|
# bootargs the same way; nothing else changes.
|
||||||
|
set -e
|
||||||
|
DTC=${DTC:-$HOME/l4t/kernel/kernel-4.9/scripts/dtc/dtc}
|
||||||
|
IN=$1; OUT=$2
|
||||||
|
T=$(mktemp -d)
|
||||||
|
"$DTC" -I dtb -O dts -q -o "$T/in.dts" "$IN"
|
||||||
|
ARGS=${BOOTARGS:-"earlycon=uart8250,mmio32,0x70006040 keep_bootcon console=tty0 maxcpus=1 nosmp loglevel=8 ignore_loglevel clk_ignore_unused printk.time=1"}
|
||||||
|
python3 - "$T/in.dts" "$T/out.dts" "$ARGS" <<'PY'
|
||||||
|
import re, sys
|
||||||
|
s = open(sys.argv[1]).read()
|
||||||
|
args = sys.argv[3]
|
||||||
|
s, n = re.subn(r'(\n\tchosen \{[^}]*?bootargs = )"[^"]*";', lambda m: m.group(1) + '"' + args + '";', s, count=1)
|
||||||
|
if n != 1:
|
||||||
|
raise SystemExit("no /chosen bootargs found")
|
||||||
|
open(sys.argv[2], "w").write(s)
|
||||||
|
PY
|
||||||
|
"$DTC" -I dts -O dtb -q -o "$OUT" "$T/out.dts"
|
||||||
|
rm -rf "$T"
|
||||||
|
echo "built $OUT"
|
||||||
Executable
+22
@@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# linux/run.sh IMAGE DTB [LOG] [SECS] [extra rcm_emu args...]
|
||||||
|
#
|
||||||
|
# Boots a Linux kernel (an arm64 Image, e.g. switchroot's L4T 4.9) on the
|
||||||
|
# emulator's CPU0: the emulator places the Image at 0x80080000 and the
|
||||||
|
# device tree at 0x83000000, linux/boot.bin powers CPU0 and hands over at
|
||||||
|
# EL1 through linux/shim.S. Run linux/mkdtb.sh on the device tree first so
|
||||||
|
# it carries an earlycon on UART-B, which the emulator prints.
|
||||||
|
#
|
||||||
|
# Needs an emulator built against Unicorn 2.1 or later (make UNICORN_DIR=...).
|
||||||
|
set -e
|
||||||
|
D=$(cd "$(dirname "$0")" && pwd)
|
||||||
|
E=$(cd "$D/.." && pwd)
|
||||||
|
IMAGE=$1; DTB=$2; LOG=${3:-$D/boot.log}; SECS=${4:-600}
|
||||||
|
shift 4 2>/dev/null || shift $#
|
||||||
|
export PATH=/opt/devkitpro/devkitARM/bin:/opt/devkitpro/devkitA64/bin:$PATH
|
||||||
|
[ -f "$D/boot.bin" ] || sh "$D/build.sh"
|
||||||
|
cd "$E"
|
||||||
|
SDL_VIDEODRIVER=${SDL_VIDEODRIVER:-dummy} RCM_EMU_NO_AUDIO=1 timeout "$SECS" \
|
||||||
|
./rcm_emu "$D/boot.bin" --oem erista \
|
||||||
|
--load "$IMAGE@0x80080000" --load "$DTB@0x83000000" "$@" > "$LOG" 2>&1 || true
|
||||||
|
echo "log: $LOG ($(grep -a -c '\[uartB\]' "$LOG") kernel lines)"
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* The hand-over a kernel expects from its bootloader, as switchroot's chain
|
||||||
|
* (hekate, ATF, U-Boot) leaves it: AArch64 EL1h, non-secure, MMU and caches
|
||||||
|
* off, x0 = the device tree, x1..x3 = 0 (Documentation/arm64/booting.txt).
|
||||||
|
*
|
||||||
|
* CPU0 enters here at EL3. EL2 is configured so EL1 runs AArch64 with the
|
||||||
|
* timers and FP/SIMD untrapped, then one ERET lands in the kernel at EL1.
|
||||||
|
* PSCI calls the kernel makes over SMC are answered by the emulator, which
|
||||||
|
* plays the secure monitor.
|
||||||
|
*/
|
||||||
|
.equ KERNEL, 0x80080000
|
||||||
|
.equ DTB, 0x83000000
|
||||||
|
|
||||||
|
.text
|
||||||
|
.global _start
|
||||||
|
_start:
|
||||||
|
mov x0, #0x431 /* SCR_EL3: RW, RES1, NS */
|
||||||
|
msr scr_el3, x0
|
||||||
|
msr cptr_el3, xzr /* FP/SIMD not trapped to EL3 */
|
||||||
|
mov x0, #0x33FF
|
||||||
|
msr cptr_el2, x0 /* RES1 bits, nothing trapped to EL2 */
|
||||||
|
mov x0, #(1 << 31) /* HCR_EL2.RW: EL1 is AArch64 */
|
||||||
|
msr hcr_el2, x0
|
||||||
|
mov x0, #3 /* EL1 may use the physical timer */
|
||||||
|
msr cnthctl_el2, x0
|
||||||
|
msr cntvoff_el2, xzr
|
||||||
|
ldr x0, =0x30C50830 /* SCTLR_EL1: RES1, MMU and caches off */
|
||||||
|
msr sctlr_el1, x0
|
||||||
|
mov x0, #0x3C5 /* EL1h, DAIF masked */
|
||||||
|
msr spsr_el3, x0
|
||||||
|
ldr x0, =KERNEL
|
||||||
|
msr elr_el3, x0
|
||||||
|
ldr x0, =DTB
|
||||||
|
mov x1, xzr
|
||||||
|
mov x2, xzr
|
||||||
|
mov x3, xzr
|
||||||
|
eret
|
||||||
|
.ltorg
|
||||||
@@ -510,6 +510,40 @@ int main(int argc, char *argv[]) {
|
|||||||
state.payload_len = payload_size;
|
state.payload_len = payload_size;
|
||||||
if (!uc) { free(payload); return 1; }
|
if (!uc) { free(payload); return 1; }
|
||||||
|
|
||||||
|
// --load FILE@ADDR (repeatable): place a file in DRAM before the payload
|
||||||
|
// starts, the way a bootloader would have left a kernel and its device
|
||||||
|
// tree there. The payload decides what to do with it.
|
||||||
|
for (int i = 2; i + 1 < argc; i++) {
|
||||||
|
if (strcmp(argv[i], "--load") != 0)
|
||||||
|
continue;
|
||||||
|
const char *spec = argv[++i];
|
||||||
|
const char *at = strrchr(spec, '@');
|
||||||
|
if (!at) {
|
||||||
|
fprintf(stderr, "[error] --load expects FILE@ADDR, got '%s'\n", spec);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::string path(spec, at - spec);
|
||||||
|
uint64_t addr = strtoull(at + 1, nullptr, 0);
|
||||||
|
FILE *f = fopen(path.c_str(), "rb");
|
||||||
|
if (!f) {
|
||||||
|
fprintf(stderr, "[error] --load cannot open '%s'\n", path.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
long len = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
if (len < 0 || addr < DRAM_BASE || addr + (uint64_t)len > DRAM_BASE + DRAM_WINDOW_SIZE) {
|
||||||
|
fprintf(stderr, "[error] --load '%s' (%ld bytes) does not fit in DRAM at 0x%llX\n",
|
||||||
|
path.c_str(), len, (unsigned long long)addr);
|
||||||
|
fclose(f);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
size_t got = fread(state.dram_low_ptr + (addr - DRAM_BASE), 1, (size_t)len, f);
|
||||||
|
fclose(f);
|
||||||
|
printf("[loader] --load %s: %zu bytes at 0x%08llX\n", path.c_str(), got,
|
||||||
|
(unsigned long long)addr);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize SDL2 display
|
// Initialize SDL2 display
|
||||||
if (!sdl_display_init()) {
|
if (!sdl_display_init()) {
|
||||||
fprintf(stderr, "[error] Failed to initialize display\n");
|
fprintf(stderr, "[error] Failed to initialize display\n");
|
||||||
|
|||||||
@@ -0,0 +1,297 @@
|
|||||||
|
// Generated from the Tegra X1 TRM (DP-07225-001 v1.3p), chapter 5.2:
|
||||||
|
// CLK_RST_CONTROLLER register reset values, unknown bits as 0.
|
||||||
|
// Regenerate with trm_car_resets.py; do not edit by hand.
|
||||||
|
#ifndef T210_CAR_RESET_H
|
||||||
|
#define T210_CAR_RESET_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct CarReset { uint16_t off; uint32_t val; const char *name; };
|
||||||
|
static const CarReset kCarReset[] = {
|
||||||
|
{0x000, 0x00000000, "RST_SOURCE"},
|
||||||
|
{0x004, 0x1CD3D000, "RST_DEVICES_L"},
|
||||||
|
{0x008, 0x87D1F320, "RST_DEVICES_H"},
|
||||||
|
{0x00C, 0x828EC5FA, "RST_DEVICES_U"},
|
||||||
|
{0x010, 0x80000130, "CLK_OUT_ENB_L"},
|
||||||
|
{0x014, 0x00000080, "CLK_OUT_ENB_H"},
|
||||||
|
{0x018, 0x01F00200, "CLK_OUT_ENB_U"},
|
||||||
|
{0x024, 0x00000000, "SUPER_CCLK_DIVIDER"},
|
||||||
|
{0x028, 0x10000000, "SCLK_BURST_POLICY"},
|
||||||
|
{0x02C, 0x00000000, "SUPER_SCLK_DIVIDER"},
|
||||||
|
{0x030, 0x00000000, "CLK_SYSTEM_RATE"},
|
||||||
|
{0x044, 0x00000000, "CLK_MASK_ARM"},
|
||||||
|
{0x048, 0x00000000, "MISC_CLK_ENB"},
|
||||||
|
{0x050, 0x500003F1, "OSC_CTRL"},
|
||||||
|
{0x05C, 0x00000000, "OSC_FREQ_DET_STATUS"},
|
||||||
|
{0x068, 0x00005C00, "PLLE_SS_CNTL"},
|
||||||
|
{0x06C, 0x00000010, "PLLE_MISC1"},
|
||||||
|
{0x080, 0x00108002, "PLLC_BASE"},
|
||||||
|
{0x084, 0x00000002, "PLLC_OUT"},
|
||||||
|
{0x088, 0x40000000, "PLLC_MISC"},
|
||||||
|
{0x08C, 0x08000000, "PLLC_MISC_1"},
|
||||||
|
{0x090, 0x00002A02, "PLLM_BASE"},
|
||||||
|
{0x098, 0x00000000, "PLLM_MISC1"},
|
||||||
|
{0x09C, 0x00000010, "PLLM_MISC2"},
|
||||||
|
{0x0A0, 0x00115408, "PLLP_BASE"},
|
||||||
|
{0x0A4, 0x00000002, "PLLP_OUTA"},
|
||||||
|
{0x0A8, 0x00020002, "PLLP_OUTB"},
|
||||||
|
{0x0AC, 0x00040000, "PLLP_MISC"},
|
||||||
|
{0x0B0, 0x00803003, "PLLA_BASE"},
|
||||||
|
{0x0B4, 0x00000002, "PLLA_OUT"},
|
||||||
|
{0x0B8, 0x00000000, "PLLA_MISC1"},
|
||||||
|
{0x0BC, 0x12000000, "PLLA_MISC"},
|
||||||
|
{0x0C0, 0x00011902, "PLLU_BASE"},
|
||||||
|
{0x0C4, 0x00020002, "PLLU_OUTA"},
|
||||||
|
{0x0C8, 0x00000000, "PLLU_MISC1"},
|
||||||
|
{0x0CC, 0x00000000, "PLLU_MISC"},
|
||||||
|
{0x0D0, 0x00211001, "PLLD_BASE"},
|
||||||
|
{0x0D8, 0x00000000, "PLLD_MISC1"},
|
||||||
|
{0x0DC, 0x00000000, "PLLD_MISC"},
|
||||||
|
{0x0E0, 0x00002401, "PLLX_BASE"},
|
||||||
|
{0x0E4, 0x00000000, "PLLX_MISC"},
|
||||||
|
{0x0E8, 0x0E007D02, "PLLE_BASE"},
|
||||||
|
{0x0EC, 0x00000000, "PLLE_MISC"},
|
||||||
|
{0x0F0, 0x00000000, "PLLE_SS_CNTL1"},
|
||||||
|
{0x0F4, 0x00000000, "PLLE_SS_CNTL2"},
|
||||||
|
{0x0F8, 0x00000000, "LVL2_CLK_GATE_OVRA"},
|
||||||
|
{0x0FC, 0x00000000, "LVL2_CLK_GATE_OVRB"},
|
||||||
|
{0x100, 0xD0000000, "CLK_SOURCE_I2S2"},
|
||||||
|
{0x104, 0xD0000000, "CLK_SOURCE_I2S3"},
|
||||||
|
{0x108, 0xC0000000, "CLK_SOURCE_SPDIF_OUT"},
|
||||||
|
{0x10C, 0x00000000, "CLK_SOURCE_SPDIF_IN"},
|
||||||
|
{0x110, 0xC0000000, "CLK_SOURCE_PWM"},
|
||||||
|
{0x118, 0xC0000000, "CLK_SOURCE_SPI2"},
|
||||||
|
{0x11C, 0xC0000000, "CLK_SOURCE_SPI3"},
|
||||||
|
{0x124, 0xC0000000, "CLK_SOURCE_I2C1"},
|
||||||
|
{0x134, 0xC0000000, "CLK_SOURCE_SPI1"},
|
||||||
|
{0x138, 0xC0000000, "CLK_SOURCE_DISP1"},
|
||||||
|
{0x13C, 0xC0000000, "CLK_SOURCE_DISP2"},
|
||||||
|
{0x144, 0xC0000000, "CLK_SOURCE_ISP"},
|
||||||
|
{0x148, 0x80000000, "CLK_SOURCE_VI"},
|
||||||
|
{0x150, 0xC0000000, "CLK_SOURCE_SDMMC1"},
|
||||||
|
{0x154, 0xC0000000, "CLK_SOURCE_SDMMC2"},
|
||||||
|
{0x164, 0xC0000000, "CLK_SOURCE_SDMMC4"},
|
||||||
|
{0x178, 0xC0000002, "CLK_SOURCE_UARTA"},
|
||||||
|
{0x17C, 0xC0000002, "CLK_SOURCE_UARTB"},
|
||||||
|
{0x180, 0x80000000, "CLK_SOURCE_HOST1X"},
|
||||||
|
{0x198, 0xC0000000, "CLK_SOURCE_I2C2"},
|
||||||
|
{0x1A0, 0xC0000002, "CLK_SOURCE_UARTC"},
|
||||||
|
{0x1A8, 0x80000000, "CLK_SOURCE_VI_SENSOR"},
|
||||||
|
{0x1B4, 0xC0000000, "CLK_SOURCE_SPI4"},
|
||||||
|
{0x1B8, 0xC0000000, "CLK_SOURCE_I2C3"},
|
||||||
|
{0x1BC, 0xC0000000, "CLK_SOURCE_SDMMC3"},
|
||||||
|
{0x1C0, 0xC0000002, "CLK_SOURCE_UARTD"},
|
||||||
|
{0x1CC, 0xC0000000, "CLK_SOURCE_OWR"},
|
||||||
|
{0x1D4, 0xC0000000, "CLK_SOURCE_CSITE"},
|
||||||
|
{0x1D8, 0xD0000000, "CLK_SOURCE_I2S1"},
|
||||||
|
{0x1DC, 0x00000000, "CLK_SOURCE_DTV"},
|
||||||
|
{0x1F4, 0xC0000000, "CLK_SOURCE_TSEC"},
|
||||||
|
{0x1F8, 0xC0000000, "CLK_SOURCE_LA"},
|
||||||
|
{0x1FC, 0x00000000, "CLK_SPARE2"},
|
||||||
|
{0x280, 0x23000780, "CLK_OUT_ENB_X"},
|
||||||
|
{0x284, 0x23000780, "CLK_ENB_X_SET"},
|
||||||
|
{0x288, 0x23000780, "CLK_ENB_X_CLR"},
|
||||||
|
{0x28C, 0x01E42049, "RST_DEVICES_X"},
|
||||||
|
{0x290, 0x01E42049, "RST_DEV_X_SET"},
|
||||||
|
{0x294, 0x01E42049, "RST_DEV_X_CLR"},
|
||||||
|
{0x298, 0x00000300, "CLK_OUT_ENB_Y"},
|
||||||
|
{0x29C, 0x00000300, "CLK_ENB_Y_SET"},
|
||||||
|
{0x2A0, 0x00000300, "CLK_ENB_Y_CLR"},
|
||||||
|
{0x2A4, 0x0FE9F0DD, "RST_DEVICES_Y"},
|
||||||
|
{0x2A8, 0x0FE9F0DD, "RST_DEV_Y_SET"},
|
||||||
|
{0x2AC, 0x0FE9F0DD, "RST_DEV_Y_CLR"},
|
||||||
|
{0x2F4, 0x00000001, "DFLL_BASE"},
|
||||||
|
{0x304, 0x1CD3D000, "RST_DEV_L_CLR"},
|
||||||
|
{0x308, 0x87D1F320, "RST_DEV_H_SET"},
|
||||||
|
{0x30C, 0x87D1F327, "RST_DEV_H_CLR"},
|
||||||
|
{0x310, 0x828EC5FA, "RST_DEV_U_SET"},
|
||||||
|
{0x314, 0x828EC5FA, "RST_DEV_U_CLR"},
|
||||||
|
{0x320, 0x80000130, "CLK_ENB_L_SET"},
|
||||||
|
{0x324, 0x80000130, "CLK_ENB_L_CLR"},
|
||||||
|
{0x328, 0x00000080, "CLK_ENB_H_SET"},
|
||||||
|
{0x32C, 0x00000080, "CLK_ENB_H_CLR"},
|
||||||
|
{0x330, 0x01F80200, "CLK_ENB_U_SET"},
|
||||||
|
{0x334, 0x01F00200, "CLK_ENB_U_CLR"},
|
||||||
|
{0x33C, 0x00000000, "CCPLEX_PG_SM_OVRD"},
|
||||||
|
{0x340, 0x2000FEEF, "RST_CPU_CMPLX_SET"},
|
||||||
|
{0x358, 0xFF818080, "RST_DEVICES_V"},
|
||||||
|
{0x35C, 0x190077FF, "RST_DEVICES_W"},
|
||||||
|
{0x360, 0x00400008, "CLK_OUT_ENB_V"},
|
||||||
|
{0x364, 0x402000FC, "CLK_OUT_ENB_W"},
|
||||||
|
{0x368, 0x10000000, "CCLKG_BURST_POLICY"},
|
||||||
|
{0x36C, 0x00000000, "SUPER_CCLKG_DIVIDER"},
|
||||||
|
{0x374, 0x00000000, "SUPER_CCLKLP_DIVIDER"},
|
||||||
|
{0x380, 0x00000010, "CPU_SOFTRST_CTRL"},
|
||||||
|
{0x384, 0x00040004, "CPU_SOFTRST_CTRL1"},
|
||||||
|
{0x388, 0x07000200, "CPU_SOFTRST_CTRL2"},
|
||||||
|
{0x3A0, 0x00000000, "LVL2_CLK_GATE_OVRC"},
|
||||||
|
{0x3A4, 0x00000000, "LVL2_CLK_GATE_OVRD"},
|
||||||
|
{0x3B4, 0xC0000000, "CLK_SOURCE_MSELECT"},
|
||||||
|
{0x3B8, 0xC0000000, "CLK_SOURCE_TSENSOR"},
|
||||||
|
{0x3BC, 0xD0000000, "CLK_SOURCE_I2S4"},
|
||||||
|
{0x3C0, 0xD0000000, "CLK_SOURCE_I2S5"},
|
||||||
|
{0x3C4, 0xC0000000, "CLK_SOURCE_I2C4"},
|
||||||
|
{0x3D0, 0xD0000000, "CLK_SOURCE_AHUB"},
|
||||||
|
{0x3E4, 0xC0000000, "CLK_SOURCE_HDA2CODEC_2X"},
|
||||||
|
{0x3E8, 0xC0000000, "CLK_SOURCE_ACTMON"},
|
||||||
|
{0x3EC, 0x60000000, "CLK_SOURCE_EXTPERIPH1"},
|
||||||
|
{0x3F0, 0x60000000, "CLK_SOURCE_EXTPERIPH2"},
|
||||||
|
{0x3F4, 0x60000000, "CLK_SOURCE_EXTPERIPH3"},
|
||||||
|
{0x3FC, 0xC0000000, "CLK_SOURCE_I2C_SLOW"},
|
||||||
|
{0x410, 0xC0000000, "CLK_SOURCE_SOR1"},
|
||||||
|
{0x414, 0x00000000, "CLK_SOURCE_SOR0"},
|
||||||
|
{0x420, 0xC0000000, "CLK_SOURCE_SATA_OOB"},
|
||||||
|
{0x424, 0xC1000000, "CLK_SOURCE_SATA"},
|
||||||
|
{0x428, 0xC0000000, "CLK_SOURCE_HDA"},
|
||||||
|
{0x430, 0xFF818080, "RST_DEV_V_SET"},
|
||||||
|
{0x434, 0xFF818080, "RST_DEV_V_CLR"},
|
||||||
|
{0x438, 0x190077FF, "RST_DEV_W_SET"},
|
||||||
|
{0x43C, 0x190077FF, "RST_DEV_W_CLR"},
|
||||||
|
{0x440, 0x00400008, "CLK_ENB_V_SET"},
|
||||||
|
{0x444, 0x00400008, "CLK_ENB_V_CLR"},
|
||||||
|
{0x448, 0x402000FC, "CLK_ENB_W_SET"},
|
||||||
|
{0x44C, 0x402000FC, "CLK_ENB_W_CLR"},
|
||||||
|
{0x450, 0x2000FEEF, "RST_CPUG_CMPLX_SET"},
|
||||||
|
{0x454, 0x2000FEEF, "RST_CPUG_CMPLX_CLR"},
|
||||||
|
{0x460, 0x00000000, "CLK_CPUG_CMPLX_SET"},
|
||||||
|
{0x464, 0x00000000, "CLK_CPUG_CMPLX_CLR"},
|
||||||
|
{0x470, 0x00000000, "CPU_CMPLX_STATUS"},
|
||||||
|
{0x47C, 0x00000000, "INTMASK"},
|
||||||
|
{0x488, 0x00318015, "UTMIP_PLL_CFG2"},
|
||||||
|
{0x48C, 0x00002070, "PLLE_AUX"},
|
||||||
|
{0x494, 0x00011400, "SATA_PLL_CFG1"},
|
||||||
|
{0x4A0, 0x00000000, "AUDIO_SYNC_CLK_I2S1"},
|
||||||
|
{0x4A4, 0x00000000, "AUDIO_SYNC_CLK_I2S2"},
|
||||||
|
{0x4A8, 0x00000000, "AUDIO_SYNC_CLK_I2S3"},
|
||||||
|
{0x4AC, 0x00000000, "AUDIO_SYNC_CLK_I2S4"},
|
||||||
|
{0x4B0, 0x00000000, "AUDIO_SYNC_CLK_I2S5"},
|
||||||
|
{0x4B4, 0x00000000, "AUDIO_SYNC_CLK_SPDIF"},
|
||||||
|
{0x4B8, 0x00041401, "PLLD2_BASE"},
|
||||||
|
{0x4BC, 0x40000000, "PLLD2_MISC"},
|
||||||
|
{0x4C0, 0x00000000, "UTMIP_PLL_CFG3"},
|
||||||
|
{0x4C4, 0x00004104, "PLLREFE_BASE"},
|
||||||
|
{0x4C8, 0x00000000, "PLLREFE_MISC"},
|
||||||
|
{0x4CC, 0x00020002, "PLLREFE_OUT"},
|
||||||
|
{0x4D0, 0x00000000, "CPU_FINETRIM_BYP"},
|
||||||
|
{0x4D4, 0x00000000, "CPU_FINETRIM_SELECT"},
|
||||||
|
{0x4D8, 0x00000000, "CPU_FINETRIM_DR"},
|
||||||
|
{0x4DC, 0x00000000, "CPU_FINETRIM_DF"},
|
||||||
|
{0x4E0, 0x00000000, "CPU_FINETRIM_F"},
|
||||||
|
{0x4E4, 0x00000000, "CPU_FINETRIM_R"},
|
||||||
|
{0x4E8, 0x00108002, "PLLC2_BASE"},
|
||||||
|
{0x4EC, 0x40000000, "PLLC2_MISC"},
|
||||||
|
{0x4F0, 0x08000000, "PLLC2_MISC_1"},
|
||||||
|
{0x4F4, 0x1F720005, "PLLC2_MISC_2"},
|
||||||
|
{0x4F8, 0x000000C4, "PLLC2_MISC_3"},
|
||||||
|
{0x4FC, 0x00108002, "PLLC3_BASE"},
|
||||||
|
{0x500, 0x40000000, "PLLC3_MISC"},
|
||||||
|
{0x504, 0x08000000, "PLLC3_MISC_1"},
|
||||||
|
{0x508, 0x1F720005, "PLLC3_MISC_2"},
|
||||||
|
{0x50C, 0x000000C4, "PLLC3_MISC_3"},
|
||||||
|
{0x510, 0x00000000, "PLLX_MISC_1"},
|
||||||
|
{0x514, 0x00000000, "PLLX_MISC_2"},
|
||||||
|
{0x518, 0x00000008, "PLLX_MISC_3"},
|
||||||
|
{0x520, 0x00000014, "XUSBIO_PLL_CFG1"},
|
||||||
|
{0x524, 0x00000105, "PLLE_AUX1"},
|
||||||
|
{0x528, 0x00000053, "PLLP_RESHIFT"},
|
||||||
|
{0x52C, 0x0000000F, "UTMIPLL_HW_PWRDN_CFG0"},
|
||||||
|
{0x530, 0x0000008D, "PLLU_HW_PWRDN_CFG0"},
|
||||||
|
{0x534, 0x8002A80F, "XUSB_PLL_CFG0"},
|
||||||
|
{0x53C, 0x00000002, "CLK_CPU_MISC"},
|
||||||
|
{0x540, 0x00000002, "CLK_CPUG_MISC"},
|
||||||
|
{0x548, 0x00000001, "PLLX_HW_CTRL_CFG"},
|
||||||
|
{0x54C, 0x00000000, "PLLX_SW_RAMP_CFG"},
|
||||||
|
{0x550, 0x00000000, "PLLX_HW_CTRL_STATUS"},
|
||||||
|
{0x554, 0x00000000, "LVL2_CLK_GATE_OVRE"},
|
||||||
|
{0x55C, 0x00000000, "SPARE_REG0"},
|
||||||
|
{0x560, 0x00000000, "AUDIO_SYNC_CLK_DMIC1"},
|
||||||
|
{0x564, 0x00000000, "AUDIO_SYNC_CLK_DMIC2"},
|
||||||
|
{0x570, 0x10000000, "PLLD2_SS_CFG"},
|
||||||
|
{0x574, 0x00000000, "PLLD2_SS_CTRL1"},
|
||||||
|
{0x578, 0x00000000, "PLLD2_SS_CTRL2"},
|
||||||
|
{0x594, 0x40000000, "PLLDP_MISC"},
|
||||||
|
{0x598, 0x10000000, "PLLDP_SS_CFG"},
|
||||||
|
{0x59C, 0x00000000, "PLLDP_SS_CTRL1"},
|
||||||
|
{0x5A0, 0x00000000, "PLLDP_SS_CTRL2"},
|
||||||
|
{0x5A4, 0x000C2302, "PLLC4_BASE"},
|
||||||
|
{0x5A8, 0x40000000, "PLLC4_MISC"},
|
||||||
|
{0x5C4, 0x00000000, "CLK_SPARE0"},
|
||||||
|
{0x5C8, 0x00000000, "CLK_SPARE1"},
|
||||||
|
{0x5CC, 0x00000000, "GPU_ISOB_CTRL"},
|
||||||
|
{0x5D0, 0x1F720005, "PLLC_MISC_2"},
|
||||||
|
{0x5D4, 0x000000C4, "PLLC_MISC_3"},
|
||||||
|
{0x5D8, 0x00000000, "PLLA_MISC2"},
|
||||||
|
{0x5E4, 0x00000002, "PLLC4_OUT"},
|
||||||
|
{0x5E8, 0x00002A02, "PLLMB_BASE"},
|
||||||
|
{0x5EC, 0x00010000, "PLLMB_MISC1"},
|
||||||
|
{0x5F0, 0x00000000, "PLLX_MISC_4"},
|
||||||
|
{0x5F4, 0x00000000, "PLLX_MISC_5"},
|
||||||
|
{0x600, 0x00000000, "CLK_SOURCE_XUSB_CORE_HOST"},
|
||||||
|
{0x604, 0x00000000, "CLK_SOURCE_XUSB_FALCON"},
|
||||||
|
{0x608, 0x00000000, "CLK_SOURCE_XUSB_FS"},
|
||||||
|
{0x60C, 0x00000000, "CLK_SOURCE_XUSB_CORE_DEV"},
|
||||||
|
{0x610, 0x04000000, "CLK_SOURCE_XUSB_SS"},
|
||||||
|
{0x614, 0xC0000000, "CLK_SOURCE_CILAB"},
|
||||||
|
{0x618, 0xC0000000, "CLK_SOURCE_CILCD"},
|
||||||
|
{0x61C, 0xC0000000, "CLK_SOURCE_CILEF"},
|
||||||
|
{0x620, 0xC0000000, "CLK_SOURCE_DSIA_LP"},
|
||||||
|
{0x624, 0xC0000000, "CLK_SOURCE_DSIB_LP"},
|
||||||
|
{0x628, 0x40000000, "CLK_SOURCE_ENTROPY"},
|
||||||
|
{0x62C, 0xD0000000, "CLK_SOURCE_DVFS_REF"},
|
||||||
|
{0x630, 0xD0000000, "CLK_SOURCE_DVFS_SOC"},
|
||||||
|
{0x640, 0x60000000, "CLK_SOURCE_EMC_LATENCY"},
|
||||||
|
{0x644, 0x40000000, "CLK_SOURCE_SOC_THERM"},
|
||||||
|
{0x64C, 0x00000000, "CLK_SOURCE_DMIC1"},
|
||||||
|
{0x650, 0x00000000, "CLK_SOURCE_DMIC2"},
|
||||||
|
{0x658, 0x80000000, "CLK_SOURCE_VI_SENSOR2"},
|
||||||
|
{0x65C, 0xC0000000, "CLK_SOURCE_I2C6"},
|
||||||
|
{0x660, 0xC0000000, "CLK_SOURCE_MIPIBIF"},
|
||||||
|
{0x664, 0x60000000, "CLK_SOURCE_EMC_DLL"},
|
||||||
|
{0x66C, 0x00000000, "CLK_SOURCE_UART_FST_MIPI_CAL"},
|
||||||
|
{0x678, 0xC0000000, "CLK_SOURCE_VIC"},
|
||||||
|
{0x67C, 0x00030000, "PLLP_OUTC"},
|
||||||
|
{0x680, 0x30000000, "PLLP_MISC1"},
|
||||||
|
{0x694, 0x00000000, "CLK_SOURCE_SDMMC_LEGACY_TM"},
|
||||||
|
{0x698, 0xE0000000, "CLK_SOURCE_NVDEC"},
|
||||||
|
{0x69C, 0xE0000000, "CLK_SOURCE_NVJPG"},
|
||||||
|
{0x6A0, 0x80000000, "CLK_SOURCE_NVENC"},
|
||||||
|
{0x6A4, 0x00108002, "PLLA1_BASE"},
|
||||||
|
{0x6A8, 0x40000000, "PLLA1_MISC"},
|
||||||
|
{0x6AC, 0x08000000, "PLLA1_MISC_1"},
|
||||||
|
{0x6B0, 0x1F720005, "PLLA1_MISC_2"},
|
||||||
|
{0x6B4, 0x000000C4, "PLLA1_MISC_3"},
|
||||||
|
{0x6B8, 0x00000000, "AUDIO_SYNC_CLK_DMIC3"},
|
||||||
|
{0x6BC, 0x00000000, "CLK_SOURCE_DMIC3"},
|
||||||
|
{0x6C0, 0xC0000000, "CLK_SOURCE_APE"},
|
||||||
|
{0x6C4, 0xC0000000, "CLK_SOURCE_QSPI"},
|
||||||
|
{0x6C8, 0xC0000000, "CLK_SOURCE_VI_I2C"},
|
||||||
|
{0x6CC, 0x00000000, "CLK_SOURCE_USB2_HSIC_TRK"},
|
||||||
|
{0x6D0, 0x00000000, "CLK_SOURCE_PEX_SATA_USB_RX_BYP"},
|
||||||
|
{0x6D4, 0x40000000, "CLK_SOURCE_MAUD"},
|
||||||
|
{0x6D8, 0xC0000000, "CLK_SOURCE_TSECB"},
|
||||||
|
{0x6DC, 0x00000002, "CLK_CPUG_MISC1"},
|
||||||
|
{0x6E0, 0x10006666, "ACLK_BURST_POLICY"},
|
||||||
|
{0x6E4, 0x00000000, "SUPER_ACLK_DIVIDER"},
|
||||||
|
{0x6E8, 0x80000000, "NVENC_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x6EC, 0x80000000, "VI_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x6F0, 0x80000000, "VIC_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x6F4, 0x80000000, "NVDEC_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x6F8, 0x80000000, "ISP_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x6FC, 0x80000000, "ISPB_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x700, 0x80000000, "NVJPG_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x704, 0x80000000, "SE_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x708, 0x80000000, "TSEC_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x70C, 0x80000000, "TSECB_SUPER_CLK_DIVIDER"},
|
||||||
|
{0x710, 0xC0000002, "CLK_SOURCE_UARTAPE"},
|
||||||
|
{0x714, 0x00000000, "CLK_CPUG_MISC2"},
|
||||||
|
{0x718, 0xC0000000, "CLK_SOURCE_DBGAPB"},
|
||||||
|
{0x71C, 0x001FFFFF, "CLK_CCPLEX_CC4_RET_CLK_ENB"},
|
||||||
|
{0x720, 0x00000400, "ACTMON_CPU_CLK"},
|
||||||
|
{0x724, 0x60000000, "CLK_SOURCE_EMC_SAFE"},
|
||||||
|
{0x728, 0x00000C00, "SDMMC2_PLLC4_OUT0_SHAPER_CTRL"},
|
||||||
|
{0x72C, 0x00000C00, "SDMMC2_PLLC4_OUT1_SHAPER_CTRL"},
|
||||||
|
{0x730, 0x00000C00, "SDMMC2_PLLC4_OUT2_SHAPER_CTRL"},
|
||||||
|
{0x734, 0x00000C00, "SDMMC2_DIV_CLK_SHAPER_CTRL"},
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+480
-15
@@ -1,7 +1,10 @@
|
|||||||
#include "ccplex.h"
|
#include "ccplex.h"
|
||||||
#include "bpmp.h"
|
#include "bpmp.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -12,6 +15,7 @@
|
|||||||
#include "memory_map.h"
|
#include "memory_map.h"
|
||||||
#include "mmio.h"
|
#include "mmio.h"
|
||||||
#include "pcie.h"
|
#include "pcie.h"
|
||||||
|
#include "gic.h"
|
||||||
|
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
// Registers (bdk soc/clock.h, soc/t210.h, soc/pmc.h; Tegra X1 TRM ch.5/16)
|
// Registers (bdk soc/clock.h, soc/t210.h, soc/pmc.h; Tegra X1 TRM ch.5/16)
|
||||||
@@ -138,6 +142,16 @@ struct Model {
|
|||||||
uint64_t bus_accesses = 0;
|
uint64_t bus_accesses = 0;
|
||||||
uint32_t boots = 0;
|
uint32_t boots = 0;
|
||||||
const char *last_refusal = nullptr;
|
const char *last_refusal = nullptr;
|
||||||
|
|
||||||
|
// Generic timer (ARM ARM D11): the EL1 physical and virtual timers.
|
||||||
|
uint64_t cntp_cval = 0, cntv_cval = 0, cntvoff = 0;
|
||||||
|
uint32_t cntp_ctl = 0, cntv_ctl = 0, cntkctl = 0, cnthctl = 0;
|
||||||
|
|
||||||
|
// An interrupt found pending and unmasked at a block boundary: the run
|
||||||
|
// stops there and ccplex_run() takes it.
|
||||||
|
bool irq_take = false;
|
||||||
|
uint64_t irqs_taken = 0, syncs_taken = 0, smcs = 0;
|
||||||
|
uint32_t xlat_gen = 1; // bumped when TTBR/TCR/SCTLR change
|
||||||
};
|
};
|
||||||
|
|
||||||
Model m;
|
Model m;
|
||||||
@@ -151,10 +165,201 @@ struct CodePage {
|
|||||||
};
|
};
|
||||||
std::vector<std::unique_ptr<CodePage>> code_pages;
|
std::vector<std::unique_ptr<CodePage>> code_pages;
|
||||||
uint64_t last_code_page = ~0ULL;
|
uint64_t last_code_page = ~0ULL;
|
||||||
uc_hook h_block, h_unmapped, h_intr;
|
uc_hook h_block, h_unmapped, h_intr, h_mrs, h_msr;
|
||||||
|
|
||||||
bool held() { return (m.rst_cpug & (RST_CPU0_MASK | RST_NONCPU)) != 0; }
|
bool held() { return (m.rst_cpug & (RST_CPU0_MASK | RST_NONCPU)) != 0; }
|
||||||
|
|
||||||
|
// ---- Generic timer -------------------------------------------------------
|
||||||
|
//
|
||||||
|
// The A57s' system counter runs from the 19.2 MHz TSC on the Tegra X1 (the
|
||||||
|
// device tree's arch timer node says 19200000), so the count is CPU0's own
|
||||||
|
// clock in those units: deterministic, like everything else here. Every
|
||||||
|
// access to the timer registers is answered from this model; QEMU's own
|
||||||
|
// timers never see one.
|
||||||
|
static constexpr uint64_t CNT_HZ = 19200000;
|
||||||
|
|
||||||
|
uint64_t cnt_now() { return m.ns * 12 / 625; } // ns * 19.2e6 / 1e9
|
||||||
|
|
||||||
|
// TMR10..13 (see ccplex.h): PTV bit 31 EN, bit 30 PER, 28:0 the count of
|
||||||
|
// microseconds in the n+1 scheme (TRM 8.7.1); PCR bit 30 clears the
|
||||||
|
// interrupt. Kept with CPU0 because only its clock times them.
|
||||||
|
struct CpuTmr { uint32_t ptv = 0; uint64_t armed_ns = 0; bool pending = false; };
|
||||||
|
CpuTmr cpu_tmr[4];
|
||||||
|
|
||||||
|
uint64_t tmr_deadline(const CpuTmr &t) {
|
||||||
|
return t.armed_ns + ((uint64_t)(t.ptv & 0x1FFFFFFF) + 1) * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cpu_tmrs_update() {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
CpuTmr &t = cpu_tmr[i];
|
||||||
|
if ((t.ptv & (1u << 31)) && m.ns >= tmr_deadline(t)) {
|
||||||
|
t.pending = true;
|
||||||
|
if (t.ptv & (1u << 30)) {
|
||||||
|
uint64_t per = ((uint64_t)(t.ptv & 0x1FFFFFFF) + 1) * 1000;
|
||||||
|
while (tmr_deadline(t) <= m.ns)
|
||||||
|
t.armed_ns += per;
|
||||||
|
} else {
|
||||||
|
t.ptv &= ~(1u << 31);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gic_set_spi(176 + i, t.pending);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint64_t ns_of_cnt(uint64_t c) {
|
||||||
|
return c > UINT64_MAX / 625 ? UINT64_MAX : (c * 625 + 11) / 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
void timers_update() {
|
||||||
|
cpu_tmrs_update();
|
||||||
|
uint64_t p = cnt_now(), v = p - m.cntvoff;
|
||||||
|
bool pt = (m.cntp_ctl & 1) && p >= m.cntp_cval;
|
||||||
|
bool vt = (m.cntv_ctl & 1) && v >= m.cntv_cval;
|
||||||
|
gic_set_ppi(GIC_PPI_PTIMER, pt && !(m.cntp_ctl & 2));
|
||||||
|
gic_set_ppi(GIC_PPI_VTIMER, vt && !(m.cntv_ctl & 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the next unmasked timer fires, on CPU0's clock; UINT64_MAX for never.
|
||||||
|
uint64_t next_timer_ns() {
|
||||||
|
uint64_t t = UINT64_MAX;
|
||||||
|
for (const CpuTmr &c : cpu_tmr)
|
||||||
|
if (c.ptv & (1u << 31))
|
||||||
|
t = std::min(t, tmr_deadline(c));
|
||||||
|
if ((m.cntp_ctl & 3) == 1)
|
||||||
|
t = std::min(t, ns_of_cnt(m.cntp_cval));
|
||||||
|
if ((m.cntv_ctl & 3) == 1) {
|
||||||
|
uint64_t c = m.cntv_cval + m.cntvoff;
|
||||||
|
if (c >= m.cntv_cval) // no wrap
|
||||||
|
t = std::min(t, ns_of_cnt(c));
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- EL1&0 stage-1 translation ---------------------------------------------
|
||||||
|
//
|
||||||
|
// Only for the emulator's own peeks (is the last instruction of a block a
|
||||||
|
// WFI?), so 4 KiB granules only, which is what Linux and the stubs use. A
|
||||||
|
// small direct-mapped cache, emptied when the translation registers change.
|
||||||
|
uint64_t cp_read(uc_engine *uc, uint32_t op0, uint32_t op1, uint32_t crn,
|
||||||
|
uint32_t crm, uint32_t op2) {
|
||||||
|
uc_arm64_cp_reg r = {crn, crm, op0, op1, op2, 0};
|
||||||
|
uc_reg_read(uc, UC_ARM64_REG_CP_REG, &r);
|
||||||
|
return r.val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cp_write(uc_engine *uc, uint32_t op0, uint32_t op1, uint32_t crn,
|
||||||
|
uint32_t crm, uint32_t op2, uint64_t val) {
|
||||||
|
uc_arm64_cp_reg r = {crn, crm, op0, op1, op2, val};
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_CP_REG, &r);
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *host_ptr(uint64_t a, uint32_t len);
|
||||||
|
|
||||||
|
bool walk_va(uc_engine *uc, uint64_t va, uint64_t *pa) {
|
||||||
|
uint64_t sctlr = cp_read(uc, 3, 0, 1, 0, 0);
|
||||||
|
if (!(sctlr & 1)) {
|
||||||
|
*pa = va;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
uint64_t tcr = cp_read(uc, 3, 0, 2, 0, 2);
|
||||||
|
bool hi = (va >> 55) & 1;
|
||||||
|
uint64_t ttbr = hi ? cp_read(uc, 3, 0, 2, 0, 1) : cp_read(uc, 3, 0, 2, 0, 0);
|
||||||
|
unsigned tsz = hi ? (tcr >> 16) & 0x3F : tcr & 0x3F;
|
||||||
|
unsigned tg = hi ? (tcr >> 30) & 3 : (tcr >> 14) & 3;
|
||||||
|
if ((hi && tg != 2) || (!hi && tg != 0))
|
||||||
|
return false; // not a 4 KiB granule
|
||||||
|
unsigned va_bits = 64 - tsz;
|
||||||
|
if (va_bits < 25 || va_bits > 48)
|
||||||
|
return false;
|
||||||
|
int levels = (int)(va_bits - 12 + 8) / 9;
|
||||||
|
uint64_t table = ttbr & 0x0000FFFFFFFFF000ULL;
|
||||||
|
for (int lvl = 4 - levels; lvl <= 3; lvl++) {
|
||||||
|
unsigned shift = 12 + 9 * (3 - lvl);
|
||||||
|
uint64_t idx = (va >> shift) & 0x1FF;
|
||||||
|
const uint8_t *p = host_ptr(table + idx * 8, 8);
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
uint64_t d;
|
||||||
|
memcpy(&d, p, 8);
|
||||||
|
if (!(d & 1))
|
||||||
|
return false;
|
||||||
|
if (lvl == 3 || !(d & 2)) {
|
||||||
|
if (lvl == 3 && !(d & 2))
|
||||||
|
return false;
|
||||||
|
uint64_t mask = (1ULL << shift) - 1;
|
||||||
|
*pa = (d & 0x0000FFFFFFFFF000ULL & ~mask) | (va & mask);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
table = d & 0x0000FFFFFFFFF000ULL;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct XlatEntry { uint64_t va_page; uint64_t pa_page; uint32_t gen; };
|
||||||
|
XlatEntry xlat_cache[1024];
|
||||||
|
|
||||||
|
bool va_to_pa(uc_engine *uc, uint64_t va, uint64_t *pa) {
|
||||||
|
uint64_t page = va >> 12;
|
||||||
|
XlatEntry &e = xlat_cache[page & 1023];
|
||||||
|
if (e.gen == m.xlat_gen && e.va_page == page) {
|
||||||
|
*pa = (e.pa_page << 12) | (va & 0xFFF);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
uint64_t p;
|
||||||
|
if (!walk_va(uc, va & ~0xFFFULL, &p))
|
||||||
|
return false;
|
||||||
|
e = {page, p >> 12, m.xlat_gen};
|
||||||
|
*pa = p | (va & 0xFFF);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool read_insn(uc_engine *uc, uint64_t va, uint32_t *insn) {
|
||||||
|
uint64_t pa;
|
||||||
|
if (!va_to_pa(uc, va, &pa))
|
||||||
|
return false;
|
||||||
|
const uint8_t *p = host_ptr(pa, 4);
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
memcpy(insn, p, 4);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Exception entry ---------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Unicorn never delivers guest exceptions (see the reset-entry note), so the
|
||||||
|
// model takes them itself: SPSR/ELR/ESR, PSTATE, then the vector. Only to
|
||||||
|
// EL1 from EL1 on SP_EL1, which is every exception a kernel without
|
||||||
|
// userspace or hypervisor takes - and it is the case where the translator's
|
||||||
|
// cached EL stays right, because the EL does not change.
|
||||||
|
enum ExcKind { EXC_SYNC = 0, EXC_IRQ = 1, EXC_FIQ = 2, EXC_SERROR = 3 };
|
||||||
|
|
||||||
|
bool take_exception(uc_engine *uc, uint64_t ret_pc, ExcKind kind,
|
||||||
|
uint32_t esr, uint64_t far) {
|
||||||
|
uint64_t ps = 0, vbar = 0;
|
||||||
|
uc_reg_read(uc, UC_ARM64_REG_PSTATE, &ps);
|
||||||
|
unsigned el = (ps >> 2) & 3;
|
||||||
|
if (el != 1 || !(ps & 1)) {
|
||||||
|
printf("[ccplex] CPU0 exception (kind %d, ESR 0x%08X) at PC=0x%llX from "
|
||||||
|
"EL%u%c: only EL1h->EL1 entry is modelled, CPU0 halted\n", kind,
|
||||||
|
esr, (unsigned long long)ret_pc, el, (ps & 1) ? 'h' : 't');
|
||||||
|
fflush(stdout);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uc_reg_read(uc, UC_ARM64_REG_VBAR_EL1, &vbar);
|
||||||
|
cp_write(uc, 3, 0, 4, 0, 0, ps); // SPSR_EL1
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_ELR_EL1, &ret_pc);
|
||||||
|
if (kind == EXC_SYNC) {
|
||||||
|
uint64_t e = esr;
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_ESR_EL1, &e);
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_FAR_EL1, &far);
|
||||||
|
}
|
||||||
|
uint64_t nps = (ps & 0xF0000000ULL) | 0x3C0 | 0x5; // EL1h, DAIF masked
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_PSTATE, &nps);
|
||||||
|
uint64_t vec = vbar + 0x200 + 0x80 * (uint64_t)kind;
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_PC, &vec);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t entry_vector() {
|
uint64_t entry_vector() {
|
||||||
return ((uint64_t)m.sb_rst_hi << 32) | (m.sb_rst_lo & ~1u);
|
return ((uint64_t)m.sb_rst_hi << 32) | (m.sb_rst_lo & ~1u);
|
||||||
}
|
}
|
||||||
@@ -290,28 +495,158 @@ void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *) {
|
|||||||
commit_pending();
|
commit_pending();
|
||||||
if (m.wfe_next) {
|
if (m.wfe_next) {
|
||||||
// The block that just ran ended in WFE/WFI: the core sleeps until
|
// The block that just ran ended in WFE/WFI: the core sleeps until
|
||||||
// something wakes it. Nothing in this model sends it an event, so it
|
// something wakes it - an interrupt, which here means a timer firing or
|
||||||
// sleeps to the end of the slice and resumes - a spurious wake-up, which
|
// a device line the GIC already holds. With neither in sight it sleeps
|
||||||
// the architecture allows, and which a `for(;;) wfe;` park loop turns
|
// to the end of the slice and resumes, a spurious wake-up the
|
||||||
// straight back into sleep. Without this that loop would spin through
|
// architecture allows and a `for(;;) wfe;` park loop turns straight back
|
||||||
// hundreds of millions of instructions per emulated second.
|
// into sleep. Without this that loop would spin through hundreds of
|
||||||
|
// millions of instructions per emulated second.
|
||||||
m.wfe_next = false;
|
m.wfe_next = false;
|
||||||
if (m.ns < m.target_ns)
|
if (!gic_irq_pending()) {
|
||||||
m.ns = m.target_ns;
|
uint64_t wake = std::min(m.target_ns, next_timer_ns());
|
||||||
|
if (wake > m.ns)
|
||||||
|
m.ns = wake;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
timers_update();
|
||||||
g_state->emu_usec = m.ns / 1000;
|
g_state->emu_usec = m.ns / 1000;
|
||||||
if (m.ns >= m.target_ns || m.kill_pending) {
|
if (m.ns >= m.target_ns || m.kill_pending) {
|
||||||
uc_emu_stop(uc);
|
uc_emu_stop(uc);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (gic_irq_pending()) {
|
||||||
|
uint64_t ps = 0;
|
||||||
|
uc_reg_read(uc, UC_ARM64_REG_PSTATE, &ps);
|
||||||
|
if (!(ps & (1u << 7))) { // PSTATE.I clear: take it before this block
|
||||||
|
m.irq_take = true;
|
||||||
|
uc_emu_stop(uc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
note_code_page(address);
|
note_code_page(address);
|
||||||
// Every A64 instruction is four bytes.
|
// Every A64 instruction is four bytes.
|
||||||
m.pending = size >= 4 ? size / 4 : 1;
|
m.pending = size >= 4 ? size / 4 : 1;
|
||||||
if (const uint8_t *p = host_ptr(address + size - 4, 4)) {
|
|
||||||
uint32_t last;
|
uint32_t last;
|
||||||
memcpy(&last, p, 4);
|
if (read_insn(uc, address + size - 4, &last))
|
||||||
m.wfe_next = (last == A64_WFE || last == A64_WFI);
|
m.wfe_next = (last == A64_WFE || last == A64_WFI);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- System registers --------------------------------------------------------
|
||||||
|
//
|
||||||
|
// MRS/MSR of the generic timer are answered here (and skipped by Unicorn);
|
||||||
|
// writes to the translation registers also empty the emulator's VA cache.
|
||||||
|
bool is(const uc_arm64_cp_reg *r, uint32_t op0, uint32_t op1, uint32_t crn,
|
||||||
|
uint32_t crm, uint32_t op2) {
|
||||||
|
return r->op0 == op0 && r->op1 == op1 && r->crn == crn && r->crm == crm &&
|
||||||
|
r->op2 == op2;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t hook_mrs(uc_engine *uc, uc_arm64_reg reg, const uc_arm64_cp_reg *r,
|
||||||
|
void *) {
|
||||||
|
if (r->op0 != 3 || r->crn != 14)
|
||||||
|
return 0;
|
||||||
|
uint64_t p = cnt_now(), v = p - m.cntvoff, out;
|
||||||
|
if (is(r, 3, 3, 14, 0, 0)) out = CNT_HZ; // CNTFRQ_EL0
|
||||||
|
else if (is(r, 3, 3, 14, 0, 1)) out = p; // CNTPCT_EL0
|
||||||
|
else if (is(r, 3, 3, 14, 0, 2)) out = v; // CNTVCT_EL0
|
||||||
|
else if (is(r, 3, 3, 14, 2, 0)) out = (uint32_t)(m.cntp_cval - p);
|
||||||
|
else if (is(r, 3, 3, 14, 2, 1)) out = m.cntp_ctl | (((m.cntp_ctl & 1) && p >= m.cntp_cval) ? 4 : 0);
|
||||||
|
else if (is(r, 3, 3, 14, 2, 2)) out = m.cntp_cval;
|
||||||
|
else if (is(r, 3, 3, 14, 3, 0)) out = (uint32_t)(m.cntv_cval - v);
|
||||||
|
else if (is(r, 3, 3, 14, 3, 1)) out = m.cntv_ctl | (((m.cntv_ctl & 1) && v >= m.cntv_cval) ? 4 : 0);
|
||||||
|
else if (is(r, 3, 3, 14, 3, 2)) out = m.cntv_cval;
|
||||||
|
else if (is(r, 3, 0, 14, 1, 0)) out = m.cntkctl; // CNTKCTL_EL1
|
||||||
|
else if (is(r, 3, 4, 14, 0, 3)) out = m.cntvoff; // CNTVOFF_EL2
|
||||||
|
else if (is(r, 3, 4, 14, 1, 0)) out = m.cnthctl; // CNTHCTL_EL2
|
||||||
|
else return 0;
|
||||||
|
if (reg != UC_ARM64_REG_XZR)
|
||||||
|
uc_reg_write(uc, reg, &out);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t hook_msr(uc_engine *uc, uc_arm64_reg reg, const uc_arm64_cp_reg *r,
|
||||||
|
void *) {
|
||||||
|
(void)reg;
|
||||||
|
(void)uc;
|
||||||
|
if (r->op0 == 3 && r->op1 == 0 && ((r->crn == 2 && r->crm == 0 && r->op2 <= 2) ||
|
||||||
|
(r->crn == 1 && r->crm == 0 && r->op2 == 0))) {
|
||||||
|
m.xlat_gen++; // TTBR0/TTBR1/TCR/SCTLR_EL1
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (r->op0 != 3 || r->crn != 14)
|
||||||
|
return 0;
|
||||||
|
uint64_t p = cnt_now(), v = p - m.cntvoff, val = r->val;
|
||||||
|
if (is(r, 3, 3, 14, 0, 0)) ; // CNTFRQ: fixed
|
||||||
|
else if (is(r, 3, 3, 14, 2, 0)) m.cntp_cval = p + (uint64_t)(int64_t)(int32_t)val;
|
||||||
|
else if (is(r, 3, 3, 14, 2, 1)) m.cntp_ctl = val & 3;
|
||||||
|
else if (is(r, 3, 3, 14, 2, 2)) m.cntp_cval = val;
|
||||||
|
else if (is(r, 3, 3, 14, 3, 0)) m.cntv_cval = v + (uint64_t)(int64_t)(int32_t)val;
|
||||||
|
else if (is(r, 3, 3, 14, 3, 1)) m.cntv_ctl = val & 3;
|
||||||
|
else if (is(r, 3, 3, 14, 3, 2)) m.cntv_cval = val;
|
||||||
|
else if (is(r, 3, 0, 14, 1, 0)) m.cntkctl = (uint32_t)val;
|
||||||
|
else if (is(r, 3, 4, 14, 0, 3)) m.cntvoff = val;
|
||||||
|
else if (is(r, 3, 4, 14, 1, 0)) m.cnthctl = (uint32_t)val;
|
||||||
|
else return 0;
|
||||||
|
timers_update();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- PSCI (ARM DEN 0022), answered on SMC -------------------------------------
|
||||||
|
//
|
||||||
|
// On a Switch the secure monitor (ATF) answers these; here the emulator is
|
||||||
|
// the monitor. One core is modelled, so CPU_ON is refused.
|
||||||
|
void psci_call(uc_engine *uc) {
|
||||||
|
uint64_t x0 = 0, x1 = 0, ret;
|
||||||
|
uc_reg_read(uc, UC_ARM64_REG_X0, &x0);
|
||||||
|
uc_reg_read(uc, UC_ARM64_REG_X1, &x1);
|
||||||
|
uint32_t fid = (uint32_t)x0;
|
||||||
|
m.smcs++;
|
||||||
|
switch (fid) {
|
||||||
|
case 0x84000000: ret = 0x00010000; break; // PSCI_VERSION 1.0
|
||||||
|
case 0x84000006: ret = 2; break; // MIGRATE_INFO_TYPE: none
|
||||||
|
case 0x8400000A: { // PSCI_FEATURES
|
||||||
|
uint32_t q = (uint32_t)x1;
|
||||||
|
ret = (q == 0x84000000 || q == 0x84000001 || q == 0xC4000001 ||
|
||||||
|
q == 0x84000002 || q == 0x84000003 || q == 0xC4000003 ||
|
||||||
|
q == 0x84000004 || q == 0xC4000004 || q == 0x84000006 ||
|
||||||
|
q == 0x84000008 || q == 0x84000009 || q == 0x8400000A) ? 0 : (uint64_t)-1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x84000001: case 0xC4000001: ret = 0; break; // CPU_SUSPEND: a WFI
|
||||||
|
case 0x84000003: case 0xC4000003: ret = (uint64_t)-3; break; // CPU_ON: DENIED
|
||||||
|
case 0x84000004: case 0xC4000004: ret = (x1 & 0xFF) == 0 ? 0 : 1; break; // AFFINITY_INFO
|
||||||
|
case 0x84000002: // CPU_OFF
|
||||||
|
printf("[ccplex] PSCI CPU_OFF from CPU0\n");
|
||||||
|
m.kill_pending = true;
|
||||||
|
m.kill_reason = "PSCI CPU_OFF";
|
||||||
|
uc_emu_stop(uc);
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
case 0x84000008: // SYSTEM_OFF
|
||||||
|
case 0x84000009: // SYSTEM_RESET
|
||||||
|
printf("[ccplex] PSCI %s from CPU0\n", fid == 0x84000008 ? "SYSTEM_OFF" : "SYSTEM_RESET");
|
||||||
|
m.kill_pending = true;
|
||||||
|
m.kill_reason = fid == 0x84000008 ? "PSCI SYSTEM_OFF" : "PSCI SYSTEM_RESET";
|
||||||
|
uc_emu_stop(uc);
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
default: {
|
||||||
|
static uint32_t seen[32];
|
||||||
|
static int nseen;
|
||||||
|
bool known = false;
|
||||||
|
for (int i = 0; i < nseen; i++)
|
||||||
|
known |= seen[i] == fid;
|
||||||
|
if (!known && nseen < 32) {
|
||||||
|
seen[nseen++] = fid;
|
||||||
|
printf("[ccplex] SMC 0x%08X (x1=0x%llX) not implemented, returns NOT_SUPPORTED\n",
|
||||||
|
fid, (unsigned long long)x1);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
ret = (uint64_t)-1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_X0, &ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
||||||
@@ -320,6 +655,22 @@ bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
|||||||
(void)value;
|
(void)value;
|
||||||
uint64_t pc = 0;
|
uint64_t pc = 0;
|
||||||
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
||||||
|
// The peripheral band decodes everywhere on the APB/AHB: a register the
|
||||||
|
// emulator does not model reads back what was written, as it does for the
|
||||||
|
// BPMP. Undecoded space outside it (the PCIe apertures' holes, nothing at
|
||||||
|
// all) still wedges the core.
|
||||||
|
if (address >= 0x50000000 && address < 0x80000000 &&
|
||||||
|
type != UC_MEM_FETCH_UNMAPPED) {
|
||||||
|
static std::vector<uint64_t> told;
|
||||||
|
uint64_t page = address & ~0xFFFULL;
|
||||||
|
if (std::find(told.begin(), told.end(), page) == told.end() && told.size() < 512) {
|
||||||
|
told.push_back(page);
|
||||||
|
printf("[ccplex] CPU0 first access to unmodelled page 0x%08llX (PC=0x%llX)\n",
|
||||||
|
(unsigned long long)page, (unsigned long long)pc);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
return mmio_map_page(uc, g_state, page);
|
||||||
|
}
|
||||||
const char *kind = type == UC_MEM_FETCH_UNMAPPED ? "fetch"
|
const char *kind = type == UC_MEM_FETCH_UNMAPPED ? "fetch"
|
||||||
: type == UC_MEM_WRITE_UNMAPPED ? "write"
|
: type == UC_MEM_WRITE_UNMAPPED ? "write"
|
||||||
: "read";
|
: "read";
|
||||||
@@ -330,16 +681,54 @@ bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
|||||||
printf("[ccplex] CPU0 %s of unmapped 0x%llX at PC=0x%llX: nothing answers, "
|
printf("[ccplex] CPU0 %s of unmapped 0x%llX at PC=0x%llX: nothing answers, "
|
||||||
"CPU0 wedged\n", kind, (unsigned long long)address,
|
"CPU0 wedged\n", kind, (unsigned long long)address,
|
||||||
(unsigned long long)pc);
|
(unsigned long long)pc);
|
||||||
|
{
|
||||||
|
uint64_t pa = 0;
|
||||||
|
bool ok = walk_va(uc, address, &pa);
|
||||||
|
printf("[ccplex] SCTLR_EL1 %08llX TCR_EL1 %llX TTBR0 %llX TTBR1 %llX; walk %s 0x%llX\n",
|
||||||
|
(unsigned long long)cp_read(uc, 3, 0, 1, 0, 0),
|
||||||
|
(unsigned long long)cp_read(uc, 3, 0, 2, 0, 2),
|
||||||
|
(unsigned long long)cp_read(uc, 3, 0, 2, 0, 0),
|
||||||
|
(unsigned long long)cp_read(uc, 3, 0, 2, 0, 1), ok ? "->" : "failed",
|
||||||
|
(unsigned long long)pa);
|
||||||
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hook_intr(uc_engine *uc, uint32_t intno, void *) {
|
void hook_intr(uc_engine *uc, uint32_t intno, void *) {
|
||||||
// Unicorn does not deliver guest exceptions (see the reset-entry note),
|
// Unicorn does not deliver guest exceptions (see the reset-entry note).
|
||||||
// so a fault cannot reach the stub's own vectors. Say what happened and
|
// SMC is answered here (PSCI) and execution goes on after it; BRK and
|
||||||
// stop the core rather than resume past the faulting instruction.
|
// undefined instructions are taken to EL1's own vectors, which is how a
|
||||||
|
// kernel's WARN() and BUG() work. Anything else stops the core rather
|
||||||
|
// than resume past the faulting instruction.
|
||||||
uint64_t pc = 0;
|
uint64_t pc = 0;
|
||||||
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
||||||
|
uint32_t insn = 0;
|
||||||
|
if (intno == 13 || intno == 12) { // EXCP_SMC (QEMU 5: 13)
|
||||||
|
if (read_insn(uc, pc - 4, &insn) && (insn & 0xFFE0001F) == 0xD4000003) {
|
||||||
|
psci_call(uc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (intno == 11 && read_insn(uc, pc - 4, &insn) && (insn & 0xFFE0001F) == 0xD4000002) {
|
||||||
|
uint64_t r = (uint64_t)-1; // HVC with no hypervisor
|
||||||
|
uc_reg_write(uc, UC_ARM64_REG_X0, &r);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (intno == 7 && read_insn(uc, pc, &insn) && (insn & 0xFFE0001F) == 0xD4200000) {
|
||||||
|
uint32_t esr = (0x3Cu << 26) | (1u << 25) | ((insn >> 5) & 0xFFFF);
|
||||||
|
m.syncs_taken++;
|
||||||
|
if (take_exception(uc, pc, EXC_SYNC, esr, 0))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (intno == 1) { // EXCP_UDEF
|
||||||
|
m.syncs_taken++;
|
||||||
|
if (read_insn(uc, pc, &insn))
|
||||||
|
printf("[ccplex] CPU0 undefined instruction 0x%08X at PC=0x%llX, taken to EL1\n",
|
||||||
|
insn, (unsigned long long)pc);
|
||||||
|
if (take_exception(uc, pc, EXC_SYNC, (1u << 25), 0))
|
||||||
|
return;
|
||||||
|
}
|
||||||
static const char *names[] = {"?", "UDEF", "SWI", "PREFETCH_ABORT",
|
static const char *names[] = {"?", "UDEF", "SWI", "PREFETCH_ABORT",
|
||||||
"DATA_ABORT", "IRQ", "FIQ", "BKPT", "EXIT",
|
"DATA_ABORT", "IRQ", "FIQ", "BKPT", "EXIT",
|
||||||
"KERNEL_TRAP", "HVC", "HYP_TRAP", "SMC"};
|
"KERNEL_TRAP", "HVC", "HYP_TRAP", "SMC"};
|
||||||
@@ -362,6 +751,11 @@ bool start_cpu0(EmuState *state) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uc_ctl_set_cpu_model(uc, UC_CPU_ARM64_A57);
|
uc_ctl_set_cpu_model(uc, UC_CPU_ARM64_A57);
|
||||||
|
#if UC_API_MAJOR > 2 || (UC_API_MAJOR == 2 && UC_API_MINOR >= 1)
|
||||||
|
// Translate through the guest's own page tables (the 2.1 default, set
|
||||||
|
// explicitly): a kernel runs with its MMU on.
|
||||||
|
uc_ctl_tlb_mode(uc, UC_TLB_CPU);
|
||||||
|
#endif
|
||||||
|
|
||||||
// IRAM, DRAM and TZRAM are the same host memory the BPMP engine maps, so
|
// 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
|
// a stub the BPMP copies into DRAM and the mailbox both cores poll are one
|
||||||
@@ -397,6 +791,15 @@ bool start_cpu0(EmuState *state) {
|
|||||||
uc_hook_add(uc, &h_unmapped, UC_HOOK_MEM_UNMAPPED, (void *)hook_unmapped,
|
uc_hook_add(uc, &h_unmapped, UC_HOOK_MEM_UNMAPPED, (void *)hook_unmapped,
|
||||||
nullptr, 1, 0);
|
nullptr, 1, 0);
|
||||||
uc_hook_add(uc, &h_intr, UC_HOOK_INTR, (void *)hook_intr, nullptr, 1, 0);
|
uc_hook_add(uc, &h_intr, UC_HOOK_INTR, (void *)hook_intr, nullptr, 1, 0);
|
||||||
|
uc_hook_add(uc, &h_mrs, UC_HOOK_INSN, (void *)hook_mrs, nullptr, 1, 0,
|
||||||
|
UC_ARM64_INS_MRS);
|
||||||
|
if (!getenv("RCM_EMU_NO_MSR_HOOK"))
|
||||||
|
uc_hook_add(uc, &h_msr, UC_HOOK_INSN, (void *)hook_msr, nullptr, 1, 0,
|
||||||
|
UC_ARM64_INS_MSR);
|
||||||
|
m.cntp_cval = m.cntv_cval = m.cntvoff = 0;
|
||||||
|
m.cntp_ctl = m.cntv_ctl = m.cntkctl = m.cnthctl = 0;
|
||||||
|
m.irq_take = false;
|
||||||
|
m.xlat_gen++;
|
||||||
|
|
||||||
m.uc = uc;
|
m.uc = uc;
|
||||||
m.pc = RESET_TRAMPOLINE;
|
m.pc = RESET_TRAMPOLINE;
|
||||||
@@ -572,6 +975,21 @@ void ccplex_bus_tick(EmuState *state) {
|
|||||||
|
|
||||||
uint64_t ccplex_now_us() { return m.ns / 1000; }
|
uint64_t ccplex_now_us() { return m.ns / 1000; }
|
||||||
|
|
||||||
|
void ccplex_tmr_write(uint32_t off, uint32_t val) {
|
||||||
|
if (off < 0x90 || off >= 0xB0)
|
||||||
|
return;
|
||||||
|
CpuTmr &t = cpu_tmr[(off - 0x90) / 8];
|
||||||
|
if ((off - 0x90) % 8 == 4) { // PCR
|
||||||
|
if (val & (1u << 30)) {
|
||||||
|
t.pending = false;
|
||||||
|
gic_set_spi(176 + (int)((off - 0x90) / 8), false);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
t.ptv = val; // PTV: (re)armed now
|
||||||
|
t.armed_ns = m.ns;
|
||||||
|
}
|
||||||
|
|
||||||
bool ccplex_cpu0_running() { return m.cpu0 == Cpu0::Running && m.uc; }
|
bool ccplex_cpu0_running() { return m.cpu0 == Cpu0::Running && m.uc; }
|
||||||
|
|
||||||
void ccplex_run(EmuState *state, uint64_t until_us) {
|
void ccplex_run(EmuState *state, uint64_t until_us) {
|
||||||
@@ -601,18 +1019,65 @@ void ccplex_run(EmuState *state, uint64_t until_us) {
|
|||||||
m.target_ns = target;
|
m.target_ns = target;
|
||||||
m.in_run = true;
|
m.in_run = true;
|
||||||
|
|
||||||
|
// The BPMP can only rewrite CPU0's code while it runs; parked, it cannot,
|
||||||
|
// and comparing hundreds of kernel pages before every slice is the one
|
||||||
|
// thing that would make a booted kernel crawl.
|
||||||
|
if (!state->bpmp_halted)
|
||||||
drop_stale_code();
|
drop_stale_code();
|
||||||
|
|
||||||
// `until` is an address an A64 PC can never hold (it is not 4-aligned).
|
// `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.
|
// 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);
|
uc_err err;
|
||||||
|
for (;;) {
|
||||||
|
err = uc_emu_start(m.uc, m.pc, ~0ULL, 0, 0);
|
||||||
commit_pending();
|
commit_pending();
|
||||||
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
||||||
|
if (err != UC_ERR_OK || m.kill_pending || m.cpu0 != Cpu0::Running || !m.irq_take)
|
||||||
|
break;
|
||||||
|
// An interrupt was pending and unmasked at the block that starts at PC:
|
||||||
|
// take it there, then carry on with the slice from the vector.
|
||||||
|
m.irq_take = false;
|
||||||
|
m.irqs_taken++;
|
||||||
|
if (!take_exception(m.uc, m.pc, EXC_IRQ, 0, 0)) {
|
||||||
|
m.cpu0 = Cpu0::Hung;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
||||||
|
if (m.ns >= m.target_ns)
|
||||||
|
break;
|
||||||
|
}
|
||||||
m.in_run = false;
|
m.in_run = false;
|
||||||
g_bus_master = prev;
|
g_bus_master = prev;
|
||||||
state->emu_usec = bpmp_usec;
|
state->emu_usec = bpmp_usec;
|
||||||
|
|
||||||
|
// RCM_EMU_CPU0_PROFILE=1: one line every second of real time with where
|
||||||
|
// CPU0 is, how much it ran and how many interrupts it took - enough to
|
||||||
|
// tell a hung kernel from a slow one.
|
||||||
|
{
|
||||||
|
static int prof = -1;
|
||||||
|
static uint64_t last_wall = 0, last_ret = 0;
|
||||||
|
if (prof < 0)
|
||||||
|
prof = getenv("RCM_EMU_CPU0_PROFILE") ? 1 : 0;
|
||||||
|
if (prof) {
|
||||||
|
uint64_t wall = (uint64_t)std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||||
|
if (wall - last_wall >= 1000) {
|
||||||
|
last_wall = wall;
|
||||||
|
printf("[cpu0prof] t=%llu us pc=0x%llX retired=%llu (+%llu) irqs=%llu syncs=%llu smcs=%llu gic=%d\n",
|
||||||
|
(unsigned long long)(m.ns / 1000), (unsigned long long)m.pc,
|
||||||
|
(unsigned long long)m.retired, (unsigned long long)(m.retired - last_ret),
|
||||||
|
(unsigned long long)m.irqs_taken, (unsigned long long)m.syncs_taken,
|
||||||
|
(unsigned long long)m.smcs, gic_highest_pending());
|
||||||
|
last_ret = m.retired;
|
||||||
|
char g27[160];
|
||||||
|
gic_describe(GIC_PPI_VTIMER, g27, sizeof(g27));
|
||||||
|
printf("[cpu0prof] vtimer ctl %u cval %llu cnt %llu; %s\n", m.cntv_ctl,
|
||||||
|
(unsigned long long)m.cntv_cval, (unsigned long long)(cnt_now() - m.cntvoff), g27);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (m.kill_pending) {
|
if (m.kill_pending) {
|
||||||
m.kill_pending = false;
|
m.kill_pending = false;
|
||||||
const char *why = m.kill_reason ? m.kill_reason : "stopped";
|
const char *why = m.kill_reason ? m.kill_reason : "stopped";
|
||||||
|
|||||||
@@ -58,6 +58,11 @@ void ccplex_bus_tick(EmuState *state);
|
|||||||
// True while CPU0 is out of reset and executing (not parked on a hung bus).
|
// True while CPU0 is out of reset and executing (not parked on a hung bus).
|
||||||
bool ccplex_cpu0_running();
|
bool ccplex_cpu0_running();
|
||||||
|
|
||||||
|
// TMR10..TMR13 (TMR base + 0x90..0xAF): the timers Tegra210 kernels use as
|
||||||
|
// per-CPU clock event devices. Their interrupts go to GIC SPI 176..179 on
|
||||||
|
// CPU0's clock.
|
||||||
|
void ccplex_tmr_write(uint32_t off, uint32_t val);
|
||||||
|
|
||||||
// Run CPU0 until its clock reaches `until_us` - the BPMP's current time.
|
// Run CPU0 until its clock reaches `until_us` - the BPMP's current time.
|
||||||
void ccplex_run(EmuState *state, uint64_t until_us);
|
void ccplex_run(EmuState *state, uint64_t until_us);
|
||||||
|
|
||||||
|
|||||||
+307
@@ -0,0 +1,307 @@
|
|||||||
|
#include "gic.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* GIC-400 model. See gic.h. Register offsets and reset values: ARM IHI 0048B
|
||||||
|
* (GICv2 architecture) ch.4, GIC-400 TRM (DDI 0471B) table 3-1 / 3-6.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct Gic {
|
||||||
|
uint32_t dctlr = 0;
|
||||||
|
uint32_t cctlr = 0;
|
||||||
|
uint32_t pmr = 0;
|
||||||
|
uint32_t bpr = 2;
|
||||||
|
uint32_t group[GIC_NR_IRQS / 32] = {};
|
||||||
|
bool enabled[GIC_NR_IRQS] = {};
|
||||||
|
bool latched[GIC_NR_IRQS] = {}; // edge, SGI or software-set pending
|
||||||
|
bool line[GIC_NR_IRQS] = {}; // level input
|
||||||
|
bool active[GIC_NR_IRQS] = {};
|
||||||
|
uint8_t prio[GIC_NR_IRQS] = {};
|
||||||
|
uint8_t target[GIC_NR_IRQS] = {};
|
||||||
|
uint8_t cfg[GIC_NR_IRQS] = {}; // 2 bits each, bit 1 set = edge
|
||||||
|
// Active priorities, most recent last: the running priority is the
|
||||||
|
// highest (numerically lowest) of them.
|
||||||
|
int stack[GIC_NR_IRQS] = {};
|
||||||
|
int depth = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
Gic g;
|
||||||
|
|
||||||
|
// CPU0 asks "is an interrupt pending?" before every translated block, so the
|
||||||
|
// answer is cached and recomputed only after something changed.
|
||||||
|
bool g_dirty = true;
|
||||||
|
int g_best = 1023;
|
||||||
|
bool g_line_up = false;
|
||||||
|
|
||||||
|
bool is_edge(int id) { return id < 16 || (g.cfg[id] & 2); }
|
||||||
|
|
||||||
|
bool pending(int id) {
|
||||||
|
if (g.latched[id])
|
||||||
|
return true;
|
||||||
|
return !is_edge(id) && g.line[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
int running_prio() {
|
||||||
|
int p = 0x100;
|
||||||
|
for (int i = 0; i < g.depth; i++)
|
||||||
|
if (g.prio[g.stack[i]] < p)
|
||||||
|
p = g.prio[g.stack[i]];
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
int best_scan() {
|
||||||
|
int bestid = 1023, bestp = 0x100;
|
||||||
|
for (int id = 0; id < GIC_NR_IRQS; id++) {
|
||||||
|
if (!g.enabled[id] || g.active[id] || !pending(id))
|
||||||
|
continue;
|
||||||
|
if (g.prio[id] < bestp) {
|
||||||
|
bestp = g.prio[id];
|
||||||
|
bestid = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestid;
|
||||||
|
}
|
||||||
|
|
||||||
|
int best() {
|
||||||
|
if (g_dirty) {
|
||||||
|
g_best = best_scan();
|
||||||
|
g_line_up = false;
|
||||||
|
if ((g.dctlr & 3) && (g.cctlr & 3) && g_best != 1023)
|
||||||
|
g_line_up = g.prio[g_best] < g.pmr && g.prio[g_best] < running_prio();
|
||||||
|
g_dirty = false;
|
||||||
|
}
|
||||||
|
return g_best;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t bits_read(const bool *v, uint32_t word) {
|
||||||
|
uint32_t r = 0;
|
||||||
|
for (int b = 0; b < 32; b++)
|
||||||
|
if (v[word * 32 + b])
|
||||||
|
r |= 1u << b;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t pend_read(uint32_t word) {
|
||||||
|
uint32_t r = 0;
|
||||||
|
for (int b = 0; b < 32; b++)
|
||||||
|
if (pending(word * 32 + b))
|
||||||
|
r |= 1u << b;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Byte-array registers (priority, targets) accessed by byte or by word.
|
||||||
|
uint32_t bytes_read(const uint8_t *a, uint32_t off, unsigned size) {
|
||||||
|
uint32_t r = 0;
|
||||||
|
for (unsigned i = 0; i < size && off + i < GIC_NR_IRQS; i++)
|
||||||
|
r |= (uint32_t)a[off + i] << (8 * i);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bytes_write(uint8_t *a, uint32_t off, unsigned size, uint32_t v,
|
||||||
|
uint8_t mask) {
|
||||||
|
for (unsigned i = 0; i < size && off + i < GIC_NR_IRQS; i++)
|
||||||
|
a[off + i] = (uint8_t)((v >> (8 * i)) & mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void gic_reset() {
|
||||||
|
g = Gic();
|
||||||
|
g_dirty = true;
|
||||||
|
for (int id = 0; id < 32; id++)
|
||||||
|
g.target[id] = 0x01; // banked: reads this CPU's own bit
|
||||||
|
for (int id = 0; id < 16; id++)
|
||||||
|
g.enabled[id] = true; // SGIs are always enabled on GIC-400
|
||||||
|
for (int id = 16; id < 32; id++)
|
||||||
|
g.cfg[id] = 0; // PPIs level
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t gic_dist_read(uint32_t off, unsigned size) {
|
||||||
|
if (off >= 0x400 && off < 0x400 + GIC_NR_IRQS)
|
||||||
|
return bytes_read(g.prio, off - 0x400, size);
|
||||||
|
if (off >= 0x800 && off < 0x800 + GIC_NR_IRQS) {
|
||||||
|
uint32_t i = off - 0x800;
|
||||||
|
if (i < 32)
|
||||||
|
return bytes_read(g.target, i, size);
|
||||||
|
return bytes_read(g.target, i, size);
|
||||||
|
}
|
||||||
|
uint32_t w = (off & 0x7F) >> 2;
|
||||||
|
switch (off & ~0x7Fu) {
|
||||||
|
case 0x080: return w < GIC_NR_IRQS / 32 ? g.group[w] : 0;
|
||||||
|
case 0x100: case 0x180: return w < GIC_NR_IRQS / 32 ? bits_read(g.enabled, w) : 0;
|
||||||
|
case 0x200: case 0x280: return w < GIC_NR_IRQS / 32 ? pend_read(w) : 0;
|
||||||
|
case 0x300: case 0x380: return w < GIC_NR_IRQS / 32 ? bits_read(g.active, w) : 0;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
if (off >= 0xC00 && off < 0xC00 + GIC_NR_IRQS / 4) {
|
||||||
|
uint32_t base = (off - 0xC00) * 4, r = 0;
|
||||||
|
for (int i = 0; i < 16; i++)
|
||||||
|
r |= (uint32_t)(g.cfg[base + i] & 3) << (2 * i);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
switch (off) {
|
||||||
|
case 0x000: return g.dctlr;
|
||||||
|
case 0x004: return (1u << 10) | (3u << 5) | (GIC_NR_IRQS / 32 - 1); // SecExt, 4 CPUs
|
||||||
|
case 0x008: return 0x0200143B; // GIC-400, implementer ARM
|
||||||
|
case 0xFE8: return 0x2B; // PIDR2: GICv2
|
||||||
|
case 0xFD0: return 0x04;
|
||||||
|
case 0xFE0: return 0x90;
|
||||||
|
case 0xFE4: return 0xB4;
|
||||||
|
case 0xFEC: return 0x00;
|
||||||
|
case 0xFF0: return 0x0D;
|
||||||
|
case 0xFF4: return 0xF0;
|
||||||
|
case 0xFF8: return 0x05;
|
||||||
|
case 0xFFC: return 0xB1;
|
||||||
|
default: return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void gic_dist_write(uint32_t off, unsigned size, uint32_t val) {
|
||||||
|
g_dirty = true;
|
||||||
|
if (off >= 0x400 && off < 0x400 + GIC_NR_IRQS) {
|
||||||
|
bytes_write(g.prio, off - 0x400, size, val, 0xF8); // 5 priority bits
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (off >= 0x800 && off < 0x800 + GIC_NR_IRQS) {
|
||||||
|
uint32_t i = off - 0x800;
|
||||||
|
if (i >= 32)
|
||||||
|
bytes_write(g.target, i, size, val, 0xFF);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (off >= 0xC00 && off < 0xC00 + GIC_NR_IRQS / 4) {
|
||||||
|
uint32_t base = (off - 0xC00) * 4;
|
||||||
|
for (int i = 0; i < 16; i++)
|
||||||
|
if (base + i >= 16) // SGI configuration is read-only
|
||||||
|
g.cfg[base + i] = (uint8_t)((val >> (2 * i)) & 3);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint32_t w = (off & 0x7F) >> 2;
|
||||||
|
bool inrange = w < GIC_NR_IRQS / 32;
|
||||||
|
switch (off & ~0x7Fu) {
|
||||||
|
case 0x080: if (inrange) g.group[w] = val; return;
|
||||||
|
case 0x100:
|
||||||
|
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.enabled[w * 32 + b] = true;
|
||||||
|
return;
|
||||||
|
case 0x180:
|
||||||
|
if (inrange) for (int b = 0; b < 32; b++)
|
||||||
|
if ((val & (1u << b)) && w * 32 + b >= 16) g.enabled[w * 32 + b] = false;
|
||||||
|
return;
|
||||||
|
case 0x200:
|
||||||
|
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.latched[w * 32 + b] = true;
|
||||||
|
return;
|
||||||
|
case 0x280:
|
||||||
|
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.latched[w * 32 + b] = false;
|
||||||
|
return;
|
||||||
|
case 0x300:
|
||||||
|
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.active[w * 32 + b] = true;
|
||||||
|
return;
|
||||||
|
case 0x380:
|
||||||
|
if (inrange) for (int b = 0; b < 32; b++) if (val & (1u << b)) g.active[w * 32 + b] = false;
|
||||||
|
return;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
switch (off) {
|
||||||
|
case 0x000: g.dctlr = val & 3; return;
|
||||||
|
case 0xF00: { // GICD_SGIR
|
||||||
|
int id = val & 0xF;
|
||||||
|
uint32_t filter = (val >> 24) & 3, targets = (val >> 16) & 0xFF;
|
||||||
|
bool to_me = filter == 2 || (filter == 0 && (targets & 1));
|
||||||
|
if (to_me)
|
||||||
|
g.latched[id] = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t gic_cpu_read(uint32_t off, unsigned size) {
|
||||||
|
(void)size;
|
||||||
|
if (off == 0x0C || off == 0x20)
|
||||||
|
g_dirty = true;
|
||||||
|
switch (off) {
|
||||||
|
case 0x00: return g.cctlr;
|
||||||
|
case 0x04: return g.pmr;
|
||||||
|
case 0x08: return g.bpr;
|
||||||
|
case 0x0C: case 0x20: { // IAR / AIAR: acknowledge
|
||||||
|
if (!(g.dctlr & 3) || !(g.cctlr & 3))
|
||||||
|
return 1023;
|
||||||
|
int id = best();
|
||||||
|
if (id == 1023 || g.prio[id] >= g.pmr || g.prio[id] >= running_prio())
|
||||||
|
return 1023;
|
||||||
|
g.active[id] = true;
|
||||||
|
g.latched[id] = false;
|
||||||
|
if (g.depth < GIC_NR_IRQS)
|
||||||
|
g.stack[g.depth++] = id;
|
||||||
|
return (uint32_t)id; // SGI source CPU field: CPU0, i.e. 0
|
||||||
|
}
|
||||||
|
case 0x14: return (uint32_t)(running_prio() & 0xFF);
|
||||||
|
case 0x18: case 0x28: return (uint32_t)best();
|
||||||
|
case 0x1C: return g.bpr;
|
||||||
|
case 0xFC: return 0x0202143B; // GICC_IIDR, GIC-400
|
||||||
|
default: return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void gic_cpu_write(uint32_t off, unsigned size, uint32_t val) {
|
||||||
|
(void)size;
|
||||||
|
g_dirty = true;
|
||||||
|
switch (off) {
|
||||||
|
case 0x00: g.cctlr = val & 0x3FF; return;
|
||||||
|
case 0x04: g.pmr = val & 0xF8; return;
|
||||||
|
case 0x08: case 0x1C: g.bpr = val & 7; return;
|
||||||
|
case 0x10: case 0x24: case 0x1000: { // EOIR / AEOIR / DIR
|
||||||
|
int id = (int)(val & 0x3FF);
|
||||||
|
if (id >= GIC_NR_IRQS)
|
||||||
|
return;
|
||||||
|
g.active[id] = false;
|
||||||
|
for (int i = g.depth - 1; i >= 0; i--)
|
||||||
|
if (g.stack[i] == id) {
|
||||||
|
memmove(&g.stack[i], &g.stack[i + 1], (g.depth - i - 1) * sizeof(int));
|
||||||
|
g.depth--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void gic_set_spi(int spi, bool level) {
|
||||||
|
int id = 32 + spi;
|
||||||
|
if (spi < 0 || id >= GIC_NR_IRQS)
|
||||||
|
return;
|
||||||
|
if (g.line[id] == level)
|
||||||
|
return;
|
||||||
|
if (level && is_edge(id))
|
||||||
|
g.latched[id] = true; // rising edge of an edge-configured line
|
||||||
|
g.line[id] = level;
|
||||||
|
g_dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gic_set_ppi(int id, bool level) {
|
||||||
|
if (id < 16 || id >= 32 || g.line[id] == level)
|
||||||
|
return;
|
||||||
|
g.line[id] = level;
|
||||||
|
g_dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool gic_irq_pending() {
|
||||||
|
best();
|
||||||
|
return g_line_up;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gic_highest_pending() { return best(); }
|
||||||
|
|
||||||
|
void gic_describe(int id, char *buf, unsigned n) {
|
||||||
|
if (id < 0 || id >= GIC_NR_IRQS) {
|
||||||
|
snprintf(buf, n, "?");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
snprintf(buf, n, "dctlr %u cctlr %u pmr %02X id%d en %d line %d latch %d act %d prio %02X depth %d",
|
||||||
|
g.dctlr, g.cctlr, g.pmr, id, g.enabled[id], g.line[id], g.latched[id],
|
||||||
|
g.active[id], g.prio[id], g.depth);
|
||||||
|
}
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef T210_GIC_H
|
||||||
|
#define T210_GIC_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* GIC-400 - the interrupt controller in front of the CCPLEX (Tegra X1 TRM
|
||||||
|
* ch.3; ARM IHI 0048B, GICv2). Distributor at 0x50041000, CPU interface at
|
||||||
|
* 0x50042000 (8 KiB: GICC_DIR is at +0x1000).
|
||||||
|
*
|
||||||
|
* One CPU interface is modelled: CPU0 is the only core the emulator runs.
|
||||||
|
* Security is not: both groups behave as one, which is all a kernel booted
|
||||||
|
* non-secure ever sees. Peripherals drive level lines (gic_set_spi); SGIs
|
||||||
|
* and edge-configured interrupts latch.
|
||||||
|
*/
|
||||||
|
|
||||||
|
constexpr uint64_t GICD_BASE = 0x50041000;
|
||||||
|
constexpr uint64_t GICD_SIZE = 0x1000;
|
||||||
|
constexpr uint64_t GICC_BASE = 0x50042000;
|
||||||
|
constexpr uint64_t GICC_SIZE = 0x2000;
|
||||||
|
|
||||||
|
constexpr int GIC_NR_IRQS = 256; // 32 private + 224 SPIs (ITLinesNumber 7)
|
||||||
|
constexpr int GIC_PPI_VTIMER = 27; // EL1 virtual timer
|
||||||
|
constexpr int GIC_PPI_PTIMER = 30; // EL1 physical (non-secure) timer
|
||||||
|
|
||||||
|
void gic_reset();
|
||||||
|
uint32_t gic_dist_read(uint32_t off, unsigned size);
|
||||||
|
void gic_dist_write(uint32_t off, unsigned size, uint32_t val);
|
||||||
|
uint32_t gic_cpu_read(uint32_t off, unsigned size);
|
||||||
|
void gic_cpu_write(uint32_t off, unsigned size, uint32_t val);
|
||||||
|
|
||||||
|
// A peripheral's interrupt line. `spi` is the SPI number as device trees
|
||||||
|
// write it (<GIC_SPI n ...>); the interrupt ID is 32 + n.
|
||||||
|
void gic_set_spi(int spi, bool level);
|
||||||
|
// A private peripheral interrupt line (ID 16..31) of CPU0.
|
||||||
|
void gic_set_ppi(int id, bool level);
|
||||||
|
|
||||||
|
// CPU0's nIRQ: something enabled, pending, not active and above the
|
||||||
|
// priority mask and the running priority.
|
||||||
|
bool gic_irq_pending();
|
||||||
|
|
||||||
|
// For the log: the interrupt ID that IRQ would deliver, or 1023.
|
||||||
|
int gic_highest_pending();
|
||||||
|
|
||||||
|
// One line of the distributor and CPU interface state for interrupt `id`.
|
||||||
|
void gic_describe(int id, char *buf, unsigned n);
|
||||||
|
|
||||||
|
#endif // T210_GIC_H
|
||||||
+36
-2
@@ -8,6 +8,7 @@
|
|||||||
#include "bpmp.h"
|
#include "bpmp.h"
|
||||||
#include "ccplex.h"
|
#include "ccplex.h"
|
||||||
#include "pcie.h"
|
#include "pcie.h"
|
||||||
|
#include "gic.h"
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
#include "regcache.h"
|
#include "regcache.h"
|
||||||
#include "se_engine.h"
|
#include "se_engine.h"
|
||||||
@@ -3198,6 +3199,8 @@ static constexpr uint32_t TMR_EN = 1u << 31, TMR_PER = 1u << 30;
|
|||||||
|
|
||||||
static void tmr_write(EmuState *state, uint64_t addr, uint32_t val) {
|
static void tmr_write(EmuState *state, uint64_t addr, uint32_t val) {
|
||||||
uint32_t off = (uint32_t)(addr - TMR_BASE);
|
uint32_t off = (uint32_t)(addr - TMR_BASE);
|
||||||
|
if (off >= 0x90 && off < 0xB0) // TMR10..13: CPU clock event timers
|
||||||
|
ccplex_tmr_write(off, val);
|
||||||
for (int t = 0; t < 10; t++)
|
for (int t = 0; t < 10; t++)
|
||||||
if (off == kTmrPtv[t] && (val & TMR_EN))
|
if (off == kTmrPtv[t] && (val & TMR_EN))
|
||||||
tmr_armed_us[t] = state->emu_usec;
|
tmr_armed_us[t] = state->emu_usec;
|
||||||
@@ -3391,6 +3394,19 @@ static const CarBank *car_bank_of(uint32_t off) {
|
|||||||
// The EMC clock as CAR sets it (EMC section below).
|
// The EMC clock as CAR sets it (EMC section below).
|
||||||
static uint32_t emc_rate_khz();
|
static uint32_t emc_rate_khz();
|
||||||
|
|
||||||
|
#include "car_reset.h"
|
||||||
|
static uint32_t car_trm_reset(uint32_t off) {
|
||||||
|
static uint32_t table[0x800 / 4];
|
||||||
|
static bool built = false;
|
||||||
|
if (!built) {
|
||||||
|
for (const CarReset &r : kCarReset)
|
||||||
|
if (r.off < 0x800)
|
||||||
|
table[r.off / 4] = r.val;
|
||||||
|
built = true;
|
||||||
|
}
|
||||||
|
return off < 0x800 ? table[off / 4] : 0;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t clk_rst_read(EmuState *state, uint64_t addr) {
|
uint32_t clk_rst_read(EmuState *state, uint64_t addr) {
|
||||||
uint32_t offset = (uint32_t)(addr - CLK_RST_BASE);
|
uint32_t offset = (uint32_t)(addr - CLK_RST_BASE);
|
||||||
if (const CarBank *b = car_bank_of(offset)) {
|
if (const CarBank *b = car_bank_of(offset)) {
|
||||||
@@ -3582,8 +3598,11 @@ uint32_t clk_rst_read(EmuState *state, uint64_t addr) {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
// Everything else is a plain R/W register (the CLK_SOURCE_* dividers,
|
// Everything else is a plain R/W register (the CLK_SOURCE_* dividers,
|
||||||
// PLL MISC words, ...): it reads back what was written.
|
// PLL MISC words, ...): it reads back what was written, and its TRM reset
|
||||||
return mmio_regs.get(addr);
|
// value until then (car_reset.h). A kernel's clock driver reads hundreds
|
||||||
|
// of these to find each clock's parent; 0 is a reset value the hardware
|
||||||
|
// never has for many (a super clock's burst policy BUG()s in STANDBY).
|
||||||
|
return mmio_regs.get(addr, car_trm_reset(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
// A CLK_SOURCE_EMC write is the CAR/EMC clock-change handshake (EMC below).
|
// A CLK_SOURCE_EMC write is the CAR/EMC clock-change handshake (EMC below).
|
||||||
@@ -4702,6 +4721,10 @@ uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) {
|
|||||||
return sysreg_read(state, address);
|
return sysreg_read(state, address);
|
||||||
if (address >= BPMP_CACHE_BASE && address < BPMP_CACHE_BASE + BPMP_CACHE_SIZE)
|
if (address >= BPMP_CACHE_BASE && address < BPMP_CACHE_BASE + BPMP_CACHE_SIZE)
|
||||||
return bpmp_cache_read(state, address);
|
return bpmp_cache_read(state, address);
|
||||||
|
if (address >= GICD_BASE && address < GICD_BASE + GICD_SIZE)
|
||||||
|
return gic_dist_read((uint32_t)(address - GICD_BASE), size);
|
||||||
|
if (address >= GICC_BASE && address < GICC_BASE + GICC_SIZE)
|
||||||
|
return gic_cpu_read((uint32_t)(address - GICC_BASE), size);
|
||||||
if (address >= SOC_THERM_BASE && address < SOC_THERM_BASE + SOC_THERM_SIZE)
|
if (address >= SOC_THERM_BASE && address < SOC_THERM_BASE + SOC_THERM_SIZE)
|
||||||
return soc_therm_read(state, address);
|
return soc_therm_read(state, address);
|
||||||
if (address >= FLOW_CTLR_BASE && address < FLOW_CTLR_BASE + FLOW_CTLR_SIZE)
|
if (address >= FLOW_CTLR_BASE && address < FLOW_CTLR_BASE + FLOW_CTLR_SIZE)
|
||||||
@@ -4873,6 +4896,10 @@ void mmio_bus_write(EmuState *state, uint64_t address, unsigned size,
|
|||||||
} else if (address >= BPMP_CACHE_BASE &&
|
} else if (address >= BPMP_CACHE_BASE &&
|
||||||
address < BPMP_CACHE_BASE + BPMP_CACHE_SIZE) {
|
address < BPMP_CACHE_BASE + BPMP_CACHE_SIZE) {
|
||||||
bpmp_cache_write(state, address, val);
|
bpmp_cache_write(state, address, val);
|
||||||
|
} else if (address >= GICD_BASE && address < GICD_BASE + GICD_SIZE) {
|
||||||
|
gic_dist_write((uint32_t)(address - GICD_BASE), size, val);
|
||||||
|
} else if (address >= GICC_BASE && address < GICC_BASE + GICC_SIZE) {
|
||||||
|
gic_cpu_write((uint32_t)(address - GICC_BASE), size, val);
|
||||||
} else if (address >= SOC_THERM_BASE &&
|
} else if (address >= SOC_THERM_BASE &&
|
||||||
address < SOC_THERM_BASE + SOC_THERM_SIZE) {
|
address < SOC_THERM_BASE + SOC_THERM_SIZE) {
|
||||||
soc_therm_write(state, address, val);
|
soc_therm_write(state, address, val);
|
||||||
@@ -5030,6 +5057,8 @@ const struct {
|
|||||||
{USB_OTG_BASE, USB_OTG_SIZE},
|
{USB_OTG_BASE, USB_OTG_SIZE},
|
||||||
{XUSB_DEV_BASE, XUSB_DEV_SIZE},
|
{XUSB_DEV_BASE, XUSB_DEV_SIZE},
|
||||||
{MSELECT_BASE, MSELECT_SIZE},
|
{MSELECT_BASE, MSELECT_SIZE},
|
||||||
|
// GIC-400 distributor, CPU interface and the virtualisation pages.
|
||||||
|
{GICD_BASE, 0x7000},
|
||||||
// PCIe. T210 puts the root complex at the BOTTOM of the address map,
|
// PCIe. T210 puts the root complex at the BOTTOM of the address map,
|
||||||
// below every other peripheral.
|
// below every other peripheral.
|
||||||
{PCIE_BLOCK_BASE, PCIE_BLOCK_SIZE},
|
{PCIE_BLOCK_BASE, PCIE_BLOCK_SIZE},
|
||||||
@@ -5100,6 +5129,10 @@ void mmio_map_bus(uc_engine *uc, EmuState *state) {
|
|||||||
uc_mem_map_ptr(uc, TZRAM_BASE, TZRAM_SIZE, UC_PROT_ALL, g_tzram);
|
uc_mem_map_ptr(uc, TZRAM_BASE, TZRAM_SIZE, UC_PROT_ALL, g_tzram);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mmio_map_page(uc_engine *uc, EmuState *state, uint64_t page) {
|
||||||
|
return map_window(uc, state, page & ~0xFFFULL, 0x1000) == UC_ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
void mmio_unmap_bus(uc_engine *uc) {
|
void mmio_unmap_bus(uc_engine *uc) {
|
||||||
g_windows.erase(std::remove_if(g_windows.begin(), g_windows.end(),
|
g_windows.erase(std::remove_if(g_windows.begin(), g_windows.end(),
|
||||||
[uc](const std::unique_ptr<MmioWindow> &w) {
|
[uc](const std::unique_ptr<MmioWindow> &w) {
|
||||||
@@ -5114,6 +5147,7 @@ void mmio_init(uc_engine *uc, EmuState *state) {
|
|||||||
mmio_map_bus(uc, state);
|
mmio_map_bus(uc, state);
|
||||||
pcie_reset(state);
|
pcie_reset(state);
|
||||||
ccplex_reset(state);
|
ccplex_reset(state);
|
||||||
|
gic_reset();
|
||||||
sdhci_reset_regs(sdhci_regs[0]);
|
sdhci_reset_regs(sdhci_regs[0]);
|
||||||
sdhci_reset_regs(sdhci_regs[1]);
|
sdhci_reset_regs(sdhci_regs[1]);
|
||||||
dc_reset(state);
|
dc_reset(state);
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ void mmio_init(uc_engine *uc, EmuState *state);
|
|||||||
void mmio_map_bus(uc_engine *uc, EmuState *state);
|
void mmio_map_bus(uc_engine *uc, EmuState *state);
|
||||||
// Forget the windows mapped into `uc`, before that engine is closed.
|
// Forget the windows mapped into `uc`, before that engine is closed.
|
||||||
void mmio_unmap_bus(uc_engine *uc);
|
void mmio_unmap_bus(uc_engine *uc);
|
||||||
|
// Map one 4 KiB page of the peripheral band as a register window (CPU0's
|
||||||
|
// first access to a block the emulator does not model).
|
||||||
|
bool mmio_map_page(uc_engine *uc, EmuState *state, uint64_t page);
|
||||||
|
|
||||||
// The shared dispatch both engines' MMIO callbacks funnel into. `size` is the
|
// The shared dispatch both engines' MMIO callbacks funnel into. `size` is the
|
||||||
// access width in bytes (1, 2 or 4).
|
// access width in bytes (1, 2 or 4).
|
||||||
|
|||||||
Executable
+25
@@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Builds tests/kernelcpu/payload.bin: cpu0.S with devkitA64's assembler,
|
||||||
|
# embedded as a word array into the BPMP payload built with arm-none-eabi.
|
||||||
|
set -e
|
||||||
|
D=$(dirname "$0")
|
||||||
|
A64=${A64:-/opt/devkitpro/devkitA64/bin/aarch64-none-elf-}
|
||||||
|
ARM=${ARM:-/opt/devkitpro/devkitARM/bin/arm-none-eabi-}
|
||||||
|
${A64}gcc -c -o "$D/cpu0.o" "$D/cpu0.S"
|
||||||
|
${A64}ld -Ttext=0xA0000000 -o "$D/cpu0.elf" "$D/cpu0.o"
|
||||||
|
${A64}objcopy -O binary "$D/cpu0.elf" "$D/cpu0.bin"
|
||||||
|
python3 - "$D/cpu0.bin" "$D/cpu0_blob.h" <<'PY'
|
||||||
|
import sys, struct
|
||||||
|
b = open(sys.argv[1], "rb").read()
|
||||||
|
b += b"\0" * (-len(b) % 4)
|
||||||
|
w = struct.unpack("<%dI" % (len(b) // 4), b)
|
||||||
|
with open(sys.argv[2], "w") as f:
|
||||||
|
f.write("/* Generated by build.sh from cpu0.S. */\nstatic const u32 cpu0_blob[] = {\n")
|
||||||
|
for i in range(0, len(w), 6):
|
||||||
|
f.write(" " + ", ".join("0x%08x" % x for x in w[i:i + 6]) + ",\n")
|
||||||
|
f.write("};\n")
|
||||||
|
PY
|
||||||
|
${ARM}gcc -march=armv4t -marm -Os -ffreestanding -nostdlib -fno-builtin -I"$D" \
|
||||||
|
-T "$D/link.ld" -o "$D/payload.elf" "$D/payload.c"
|
||||||
|
${ARM}objcopy -O binary "$D/payload.elf" "$D/payload.bin"
|
||||||
|
echo "built $D/payload.bin"
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
/*
|
||||||
|
* CPU0 side of the kernel-CPU test: what a kernel needs from the core
|
||||||
|
* model, exercised the way Linux uses it.
|
||||||
|
*
|
||||||
|
* Entered at EL3, MMU off, at 0xA0000000. Drops to EL1h the way a boot
|
||||||
|
* shim hands over to a kernel, then:
|
||||||
|
* [1] CurrentEL at EL1 (expect 0x4)
|
||||||
|
* [2] PSCI_VERSION over SMC (expect 0x00010000)
|
||||||
|
* [3] ESR_EL1 seen by the BRK handler (expect 0xF2000123)
|
||||||
|
* [4] CNTFRQ_EL0 (expect 19200000)
|
||||||
|
* [5] virtual timer interrupts taken (expect 10)
|
||||||
|
* [6] CNTVCT ticks across them (expect about 10 x 19200)
|
||||||
|
* [7] the last interrupt ID acknowledged (expect 27)
|
||||||
|
* [8] instructions spent waiting: the WFI loop count (expect small)
|
||||||
|
* [0] magic 'KCPU', written last
|
||||||
|
* then parks in WFE.
|
||||||
|
*/
|
||||||
|
.equ MBOX, 0xA0030000
|
||||||
|
.equ GICD, 0x50041000
|
||||||
|
.equ GICC, 0x50042000
|
||||||
|
.equ TICK, 19200 /* 1 ms at 19.2 MHz */
|
||||||
|
|
||||||
|
.text
|
||||||
|
.global _start
|
||||||
|
_start:
|
||||||
|
mov x0, #0x431 /* SCR_EL3: RW, RES1, NS */
|
||||||
|
msr scr_el3, x0
|
||||||
|
mov x0, #(1 << 31) /* HCR_EL2.RW: EL1 is AArch64 */
|
||||||
|
msr hcr_el2, x0
|
||||||
|
mov x0, #3
|
||||||
|
msr cnthctl_el2, x0
|
||||||
|
msr cntvoff_el2, xzr
|
||||||
|
ldr x0, =0x30C50830 /* SCTLR_EL1: RES1, MMU off */
|
||||||
|
msr sctlr_el1, x0
|
||||||
|
mov x0, #0x3C5 /* EL1h, DAIF masked */
|
||||||
|
msr spsr_el3, x0
|
||||||
|
adr x0, el1
|
||||||
|
msr elr_el3, x0
|
||||||
|
eret
|
||||||
|
|
||||||
|
el1:
|
||||||
|
adr x0, vectors
|
||||||
|
msr vbar_el1, x0
|
||||||
|
isb
|
||||||
|
ldr x1, =MBOX
|
||||||
|
mrs x2, currentel
|
||||||
|
str w2, [x1, #4]
|
||||||
|
|
||||||
|
ldr x0, =0x84000000 /* PSCI_VERSION */
|
||||||
|
smc #0
|
||||||
|
str w0, [x1, #8]
|
||||||
|
|
||||||
|
brk #0x123 /* the sync handler steps over it */
|
||||||
|
|
||||||
|
mrs x3, cntfrq_el0
|
||||||
|
str w3, [x1, #16]
|
||||||
|
|
||||||
|
ldr x4, =GICD
|
||||||
|
mov w5, #1
|
||||||
|
str w5, [x4] /* GICD_CTLR */
|
||||||
|
mov w5, #(1 << 27)
|
||||||
|
str w5, [x4, #0x100] /* ISENABLER0: PPI 27 */
|
||||||
|
ldr w5, [x4, #0x418]
|
||||||
|
and w5, w5, #0x00FFFFFF
|
||||||
|
mov w12, #0xA0000000
|
||||||
|
orr w5, w5, w12
|
||||||
|
str w5, [x4, #0x418] /* IPRIORITYR[27] = 0xA0 */
|
||||||
|
ldr x6, =GICC
|
||||||
|
mov w5, #0xF0
|
||||||
|
str w5, [x6, #4] /* GICC_PMR */
|
||||||
|
mov w5, #1
|
||||||
|
str w5, [x6] /* GICC_CTLR */
|
||||||
|
|
||||||
|
mov w7, #TICK
|
||||||
|
msr cntv_tval_el0, x7
|
||||||
|
mov w7, #1
|
||||||
|
msr cntv_ctl_el0, x7
|
||||||
|
mrs x8, cntvct_el0
|
||||||
|
mov w11, #0
|
||||||
|
msr daifclr, #2 /* IRQs on */
|
||||||
|
1: wfi
|
||||||
|
add w11, w11, #1
|
||||||
|
ldr w9, [x1, #20]
|
||||||
|
cmp w9, #10
|
||||||
|
b.lo 1b
|
||||||
|
msr daifset, #2
|
||||||
|
mrs x10, cntvct_el0
|
||||||
|
sub x10, x10, x8
|
||||||
|
str w10, [x1, #24]
|
||||||
|
str w11, [x1, #32]
|
||||||
|
mov w7, #0
|
||||||
|
msr cntv_ctl_el0, x7
|
||||||
|
|
||||||
|
ldr w2, =0x4B435055 /* 'KCPU' */
|
||||||
|
str w2, [x1]
|
||||||
|
2: wfe
|
||||||
|
b 2b
|
||||||
|
|
||||||
|
.ltorg
|
||||||
|
|
||||||
|
.balign 2048
|
||||||
|
vectors:
|
||||||
|
.skip 0x200 /* current EL with SP_EL0: unused */
|
||||||
|
/* 0x200: synchronous, current EL with SP_ELx */
|
||||||
|
mrs x20, esr_el1
|
||||||
|
str w20, [x1, #12]
|
||||||
|
mrs x21, elr_el1
|
||||||
|
add x21, x21, #4
|
||||||
|
msr elr_el1, x21
|
||||||
|
eret
|
||||||
|
.balign 0x80
|
||||||
|
/* 0x280: IRQ, current EL with SP_ELx */
|
||||||
|
ldr w22, [x6, #0x0C] /* GICC_IAR */
|
||||||
|
ldr w23, [x1, #20]
|
||||||
|
add w23, w23, #1
|
||||||
|
str w23, [x1, #20]
|
||||||
|
str w22, [x1, #28]
|
||||||
|
mov w24, #TICK
|
||||||
|
msr cntv_tval_el0, x24
|
||||||
|
str w22, [x6, #0x10] /* GICC_EOIR */
|
||||||
|
eret
|
||||||
|
.balign 0x80
|
||||||
|
.skip 0x80 * 2 /* FIQ, SError */
|
||||||
|
.skip 0x400 /* lower ELs: unused */
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/* Loaded and entered at 0x40010000, like any RCM payload. */
|
||||||
|
ENTRY(_start)
|
||||||
|
SECTIONS {
|
||||||
|
. = 0x40010000;
|
||||||
|
.text : { KEEP(*(.text.start)) *(.text*) }
|
||||||
|
.rodata : { *(.rodata*) }
|
||||||
|
.data : { *(.data*) }
|
||||||
|
.bss : { *(.bss*) *(COMMON) }
|
||||||
|
/DISCARD/ : { *(.comment) *(.note*) *(.ARM.attributes) *(.ARM.exidx*) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* Kernel-CPU regression payload: boots CPU0 at cpu0.S and reports its mailbox.
|
||||||
|
*
|
||||||
|
* A freestanding BPMP payload (ARM mode, loaded at 0x40010000 like any RCM
|
||||||
|
* payload) that drives the emulator's CPU0 model through the same register
|
||||||
|
* sequence as bdk's ccplex_boot_cpu0(), and reports what happened on UART-B.
|
||||||
|
* tests/ccplex/run.sh checks the emulator's log for the expected lines.
|
||||||
|
*
|
||||||
|
* 1. Release CPU0 from reset with nothing powered. The core must NOT run,
|
||||||
|
* and the emulator must say which precondition is missing.
|
||||||
|
* 2. Do the real bring-up - CPU rail (MAX77620 GPIO5 + MAX77621 VOUT on
|
||||||
|
* I2C5), CRAIL/C0NC/CE0 partitions, CPUG + MSELECT clocks, MSELECT out
|
||||||
|
* of reset, PLLX, RAM repair, SB_AA64_RESET_LOW - then release it. An
|
||||||
|
* AArch64 blob copied to DRAM records CurrentEL and a TIMERUS delta in
|
||||||
|
* a mailbox, writes a magic word last, and parks in WFE.
|
||||||
|
* Released first with its clock stopped (CLK_CPUG_CMPLX), it must
|
||||||
|
* stay still until the stop is cleared.
|
||||||
|
* 3. Put CPU0 back into reset and power the console off.
|
||||||
|
*
|
||||||
|
* Built by `make test` with arm-none-eabi-gcc; no other toolchain needed, as
|
||||||
|
* the AArch64 side is a handful of pre-assembled words.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef unsigned int u32;
|
||||||
|
|
||||||
|
#define REG(a) (*(volatile u32 *)(a))
|
||||||
|
|
||||||
|
#define TIMERUS 0x60005010u
|
||||||
|
#define CAR 0x60006000u
|
||||||
|
#define FLOW 0x60007000u
|
||||||
|
#define SB 0x6000C200u
|
||||||
|
#define UARTB_THR 0x70006040u
|
||||||
|
#define PMC 0x7000E400u
|
||||||
|
#define I2C5 0x7000D000u
|
||||||
|
|
||||||
|
#define CPU_ENTRY 0xA0000000u
|
||||||
|
#define MBOX 0xA0030000u
|
||||||
|
#define KCPU_MAGIC 0x4B435055u
|
||||||
|
|
||||||
|
static void putc_(char c) { REG(UARTB_THR) = (u32)(unsigned char)c; }
|
||||||
|
|
||||||
|
static void puts_(const char *s) {
|
||||||
|
while (*s)
|
||||||
|
putc_(*s++);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void puthex(u32 v) {
|
||||||
|
for (int i = 28; i >= 0; i -= 4)
|
||||||
|
putc_("0123456789ABCDEF"[(v >> i) & 0xF]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void udelay(u32 us) {
|
||||||
|
u32 start = REG(TIMERUS);
|
||||||
|
while (REG(TIMERUS) - start < us)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* bdk-style normal-mode I2C write: [reg, val] in CMD_DATA1, 2 bytes, GO. */
|
||||||
|
static void i2c5_write(u32 dev, u32 reg, u32 val) {
|
||||||
|
REG(I2C5 + 0x04) = dev << 1; /* CMD_ADDR0 */
|
||||||
|
REG(I2C5 + 0x0C) = reg | (val << 8); /* CMD_DATA1 */
|
||||||
|
REG(I2C5 + 0x00) = (1u << 1) | (1u << 9); /* CNFG: 2 bytes, write, GO */
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pwrgate_on(u32 part) {
|
||||||
|
if (!(REG(PMC + 0x38) & (1u << part))) /* PWRGATE_STATUS */
|
||||||
|
REG(PMC + 0x30) = part | (1u << 8); /* PWRGATE_TOGGLE */
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RST_CPU0 ((1u << 30) | (1u << 24) | (1u << 16) | (1u << 0))
|
||||||
|
#define RST_NONCPU (1u << 29)
|
||||||
|
|
||||||
|
static void release_cpu0(void) {
|
||||||
|
REG(SB + 0x30) = CPU_ENTRY | 1u; /* SB_AA64_RESET_LOW */
|
||||||
|
REG(SB + 0x34) = 0; /* SB_AA64_RESET_HIGH */
|
||||||
|
REG(CAR + 0x454) = RST_NONCPU; /* RST_CPUG_CMPLX_CLR */
|
||||||
|
REG(CAR + 0x454) = RST_CPU0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void hold_cpu0(void) {
|
||||||
|
REG(CAR + 0x450) = RST_CPU0; /* RST_CPUG_CMPLX_SET */
|
||||||
|
REG(CAR + 0x450) = RST_NONCPU;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#include "cpu0_blob.h"
|
||||||
|
|
||||||
|
__attribute__((section(".text.start"), noreturn))
|
||||||
|
void _start(void) {
|
||||||
|
volatile u32 *mb = (volatile u32 *)MBOX;
|
||||||
|
|
||||||
|
puts_("kernelcpu test: start\n");
|
||||||
|
for (u32 i = 0; i < sizeof(cpu0_blob) / sizeof(cpu0_blob[0]); i++)
|
||||||
|
((volatile u32 *)CPU_ENTRY)[i] = cpu0_blob[i];
|
||||||
|
for (u32 i = 0; i < 16; i++)
|
||||||
|
mb[i] = 0;
|
||||||
|
|
||||||
|
/* The bring-up, in ccplex_boot_cpu0() order (Erista). */
|
||||||
|
i2c5_write(0x3C, 0x40, 0x1C); /* MAX77620 AME_GPIO: GPIO5 plain */
|
||||||
|
i2c5_write(0x3C, 0x3B, 0x09); /* GPIO5: push-pull, out high */
|
||||||
|
i2c5_write(0x1B, 0x00, 0x80 | 55); /* MAX77621 VOUT: on, 0.950 V */
|
||||||
|
REG(CAR + 0x0E0) = (1u << 30) | (2u << 20) | (156u << 8) | 2u; /* PLLX */
|
||||||
|
REG(CAR + 0x3B4) = 4u << 1; /* CLK_SOURCE_MSELECT */
|
||||||
|
REG(CAR + 0x440) = 1u << 3; /* CLK_ENB_V_SET: MSELECT */
|
||||||
|
REG(CAR + 0x020) = 0x20008888; /* CCLK_BURST: PLLX_OUT0_LJ */
|
||||||
|
REG(CAR + 0x024) = 1u << 31; /* SUPER_CCLK_DIVIDER */
|
||||||
|
REG(CAR + 0x440) = 1u << 0; /* CLK_ENB_V_SET: CPUG */
|
||||||
|
pwrgate_on(0); /* CRAIL */
|
||||||
|
pwrgate_on(15); /* C0NC */
|
||||||
|
pwrgate_on(14); /* CE0 */
|
||||||
|
REG(FLOW + 0x40) = 1; /* RAM_REPAIR_REQ */
|
||||||
|
while (!(REG(FLOW + 0x40) & 2))
|
||||||
|
;
|
||||||
|
REG(CAR + 0x434) = 1u << 3; /* RST_DEV_V_CLR: MSELECT */
|
||||||
|
release_cpu0();
|
||||||
|
|
||||||
|
u32 start = REG(TIMERUS);
|
||||||
|
while (mb[0] != KCPU_MAGIC && REG(TIMERUS) - start < 200000)
|
||||||
|
udelay(100);
|
||||||
|
static const char *names[9] = { "magic", "el", "psci", "esr", "cntfrq",
|
||||||
|
"irqs", "ticks", "irqid", "wfis" };
|
||||||
|
for (u32 i = 0; i < 9; i++) {
|
||||||
|
puts_(names[i]);
|
||||||
|
putc_('=');
|
||||||
|
puthex(mb[i]);
|
||||||
|
putc_(i == 8 ? '\n' : ' ');
|
||||||
|
}
|
||||||
|
hold_cpu0();
|
||||||
|
puts_("kernelcpu test: done\n");
|
||||||
|
i2c5_write(0x3C, 0x41, 0x02); /* MAX77620 ONOFFCNFG1: PWR_OFF */
|
||||||
|
for (;;)
|
||||||
|
;
|
||||||
|
}
|
||||||
Executable
+53
@@ -0,0 +1,53 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Kernel-CPU regression test: CPU0 dropped to EL1 takes timer interrupts
|
||||||
|
# through the GIC, a BRK into its own vector, and a PSCI call over SMC.
|
||||||
|
# Usage: tests/kernelcpu/run.sh [rcm_emu] [payload]
|
||||||
|
EMU=${1:-./rcm_emu}
|
||||||
|
PAYLOAD=${2:-tests/kernelcpu/payload.bin}
|
||||||
|
LOG=${LOG:-tests/kernelcpu/out.log}
|
||||||
|
|
||||||
|
SDL_VIDEODRIVER=dummy RCM_EMU_NO_AUDIO=1 timeout 120 "$EMU" "$PAYLOAD" --oem erista \
|
||||||
|
> "$LOG" 2>&1
|
||||||
|
status=$?
|
||||||
|
|
||||||
|
fail=0
|
||||||
|
expect() {
|
||||||
|
if grep -q -- "$1" "$LOG"; then
|
||||||
|
echo " ok $2"
|
||||||
|
else
|
||||||
|
echo " FAIL $2 (missing: $1)"
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "kernelcpu test (emulator exit $status):"
|
||||||
|
expect 'magic=4B435055' 'CPU0 ran the whole test and wrote the mailbox'
|
||||||
|
expect 'el=00000004' 'the shim dropped it to EL1'
|
||||||
|
expect 'psci=00010000' 'PSCI_VERSION over SMC answers 1.0'
|
||||||
|
expect 'esr=F2000123' 'BRK is taken to the EL1 vector with its ESR'
|
||||||
|
expect 'cntfrq=0124F800' 'the counter runs at 19.2 MHz'
|
||||||
|
expect 'irqs=0000000A' 'ten virtual timer interrupts arrive through the GIC'
|
||||||
|
expect 'irqid=0000001B' 'the GIC acknowledges them as PPI 27'
|
||||||
|
|
||||||
|
ticks=$(sed -n 's/.* ticks=\([0-9A-F]*\).*/\1/p' "$LOG" | head -1)
|
||||||
|
if [ -n "$ticks" ] && [ $((0x$ticks)) -ge 190000 ] && [ $((0x$ticks)) -le 200000 ]; then
|
||||||
|
echo " ok ten 1 ms ticks take about 192000 counts (0x$ticks)"
|
||||||
|
else
|
||||||
|
echo " FAIL the tick count across ten interrupts is off (0x${ticks:-?})"
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
retired=$(sed -n 's/.*CPU0 stopped (held in reset) after \([0-9]*\) instructions.*/\1/p' "$LOG" | head -1)
|
||||||
|
if [ -n "$retired" ] && [ "$retired" -lt 5000 ]; then
|
||||||
|
echo " ok WFI sleeps until the timer ($retired instructions retired)"
|
||||||
|
else
|
||||||
|
echo " FAIL WFI spun instead of sleeping (retired: ${retired:-?})"
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
if [ "$status" -eq 0 ]; then
|
||||||
|
echo " ok the emulator exited cleanly"
|
||||||
|
else
|
||||||
|
echo " FAIL the emulator exited with status $status (124 = timed out)"
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
[ $fail -eq 0 ] && echo "kernelcpu test: PASS" || echo "kernelcpu test: FAIL"
|
||||||
|
exit $fail
|
||||||
Reference in new issue
Block a user