Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions shim/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ socshim=../$("$PYTHON" ../tools/boards.py "$board" --get soc_dir)/shim
for f in ufsdiag ufsinit; do
"$CC" $CFLAGS -I"$socshim" -I. -c "$socshim/$f.c" -o "$O/$f.o"
done
# Board hooks (soc_hooks.h): only a SoC whose soc.json sets "shim_hooks": true has a soc_hooks.c, compiled and
# linked here; boards.py emits SOC_SHIM_HOOKS 1 for it. Without the key nothing is compiled or linked.
shim_hooks=$("$PYTHON" ../tools/boards.py "$board" --get shim_hooks) || exit 2
hook_objs=
if [ "$shim_hooks" = 1 ]; then
"$CC" $CFLAGS -I"$socshim" -I. -c "$socshim/soc_hooks.c" -o "$O/soc_hooks.o"
hook_objs=$O/soc_hooks.o
fi
# Gate honesty: usbproto.c and usbgadget.c are
# cross-compiled with the shim CFLAGS on EVERY build so the object
# audits in tools/test-bluejay-boot.py always see them; they are only
Expand Down Expand Up @@ -508,10 +516,10 @@ if [ "$touch_rails" = 1 ] || [ "$touch_dvfs" = 1 ] || [ "$gpu_clk" = 1 ] || [ "$
usb_objs="$usb_objs $O/acpm.o"
fi
clr_obj=""; [ "$pmic_src_clear" != 1 ] || clr_obj="$O/pmicsrc_clear.o" # SC2046: no unquoted $(...) in the readelf list
abs=$("$READELF" -r $O/shim.o $O/shim_assert.o $O/console.o $O/ramlog.o $O/mmu.o $O/fdt.o $O/pstore.o $O/pr3marker.o $O/pr3run.o $O/loader.o $O/payload.o $O/ufs.o $O/ufsdiag.o $O/ufsinit.o $O/usbprobe.o $O/usbproto.o $O/usbgadget.o $O/usbpool.o $O/udump.o $O/wlanprobe.o $O/wlanrails.o $O/pmicsrc.o ${clr_obj:+"$clr_obj"} $O/wlanen.o $O/wlansmc.o $O/wlansmc_iso.o $O/wlansmc_link.o $O/wlanlink.o $O/gpuprobe.o $O/gpupower.o $O/gpupower_stack.o $O/gpupower_job.o $O/gpujob.o $O/gpuclk.o $O/mifclk.o $O/mifclk_audit.o $O/sweetprobe.o $O/sweetprobe_smc.o $O/sweetprobe_audit.o $O/sweetprobe_smc_audit.o $O/gpus2mpu.o $O/wlanh1.o $O/gpurails.o $O/gpurailon.o $O/acpm_railon.o $O/touchprobe.o $O/acpm.o $qr_objs $el2_objs | grep -c ABS64 || { rc=$?; [ $rc = 1 ] || { echo "error: build GAP: grep -c ABS64 failed rc=$rc" >&2; exit 1; }; })
abs=$("$READELF" -r $O/shim.o $O/shim_assert.o $O/console.o $O/ramlog.o $O/mmu.o $O/fdt.o $O/pstore.o $O/pr3marker.o $O/pr3run.o $O/loader.o $O/payload.o $O/ufs.o $O/ufsdiag.o $O/ufsinit.o $hook_objs $O/usbprobe.o $O/usbproto.o $O/usbgadget.o $O/usbpool.o $O/udump.o $O/wlanprobe.o $O/wlanrails.o $O/pmicsrc.o ${clr_obj:+"$clr_obj"} $O/wlanen.o $O/wlansmc.o $O/wlansmc_iso.o $O/wlansmc_link.o $O/wlanlink.o $O/gpuprobe.o $O/gpupower.o $O/gpupower_stack.o $O/gpupower_job.o $O/gpujob.o $O/gpuclk.o $O/mifclk.o $O/mifclk_audit.o $O/sweetprobe.o $O/sweetprobe_smc.o $O/sweetprobe_audit.o $O/sweetprobe_smc_audit.o $O/gpus2mpu.o $O/wlanh1.o $O/gpurails.o $O/gpurailon.o $O/acpm_railon.o $O/touchprobe.o $O/acpm.o $qr_objs $el2_objs | grep -c ABS64 || { rc=$?; [ $rc = 1 ] || { echo "error: build GAP: grep -c ABS64 failed rc=$rc" >&2; exit 1; }; })
[ "$abs" = 0 ] || { echo "error: $abs absolute relocations in C objects" >&2; exit 1; }
"$CC" $CFLAGS -static -T shim.ld -Wl,--build-id=none -Wl,--no-warn-rwx-segments -Wl,-N \
$O/start.o $O/shim.o $O/shim_assert.o $O/console.o $O/ramlog.o $O/mmu.o $O/fdt.o $O/pstore.o $O/loader.o $O/payload.o $O/ufs.o $O/ufsdiag.o $O/ufsinit.o $O/usbprobe.o $O/usbpool.o $usb_objs $qr_objs $el2_objs -o $O/shim.elf
$O/start.o $O/shim.o $O/shim_assert.o $O/console.o $O/ramlog.o $O/mmu.o $O/fdt.o $O/pstore.o $O/loader.o $O/payload.o $O/ufs.o $O/ufsdiag.o $O/ufsinit.o $hook_objs $O/usbprobe.o $O/usbpool.o $usb_objs $qr_objs $el2_objs -o $O/shim.elf
"$OBJCOPY" -O binary $O/shim.elf $O/Image
size_hex=$("$NM" $O/shim.elf | awk '$3=="_end"{print $1}')
size=$((16#$size_hex))
Expand Down Expand Up @@ -567,6 +575,15 @@ else
chk "wlan: every xnu/patches/*.patch ends with a newline" bash -c 'for p in ../xnu/patches/[0-9]*.patch; do [ "$(tail -c1 "$p" | od -An -c | tr -d " ")" = "\n" ] || { echo "no final newline: $p"; exit 1; }; done'
chk "host_test (compile)" gcc -O1 -Wall -o $O/host_test console.c ramlog.c host_test.c
chk "host_test" $O/host_test $O/host.ppm
# Board hooks: no SoC on trunk sets shim_hooks, so the `#if SOC_SHIM_HOOKS` call sites in shim.c/loader.c are
# compiled here for a hook SoC that is not gs101 (this board's header with SOC_GS101 0 + SOC_SHIM_HOOKS 1)
mkdir -p "$O/hookcheck"
sed -e 's/^#define SOC_GS101 1$/#define SOC_GS101 0/' "$O/board_config.h" >"$O/hookcheck/board_config.h"
echo '#define SOC_SHIM_HOOKS 1' >>"$O/hookcheck/board_config.h"
hk_cflags=${CFLAGS/"-include $O/board_config.h"/"-include $O/hookcheck/board_config.h"}
chk "soc_hooks call sites (shim.c, hook SoC)" "$CC" $hk_cflags -c shim.c -o "$O/hookcheck/shim.o"
chk "soc_hooks call sites (loader.c, hook SoC)" "$CC" $hk_cflags -c loader.c -o "$O/hookcheck/loader.o"
chk "soc_hooks call sites reference every always-built hook" sh -c 'u=$("$1" -u "$2/shim.o" "$2/loader.o") || exit 1; for h in entry video console pstore_fallback probes payload bootflags before_jump; do echo "$u" | grep -q " U soc_hook_$h\$" || { echo "no call of soc_hook_$h"; exit 1; }; done' sh "$NM" "$O/hookcheck"
chk "ramlog_host_test (compile)" gcc -O1 -Wall -Wextra -o $O/ramlog_host_test ramlog.c ramlog_host_test.c
chk "ramlog_host_test" $O/ramlog_host_test
# ASan/UBSan catch a 1-byte overflow of the report buffer (review T-98a7-assert F3); if the host gcc has no sanitizer runtime the test still runs, loudly without them
Expand Down
8 changes: 4 additions & 4 deletions shim/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const char *loader_boot(uintptr_t dtb)
tail_hit = payload_addr != 0;
#endif
if (!payload_addr)
payload_addr = shim_find_magic(rd_start, rd_end, "SWEETXNU", 8);
payload_addr = SOC_HOOK_FIND_PAYLOAD(rd_start, rd_end, "SWEETXNU", 8); /* soc_hooks.h */
__asm__ volatile("isb; mrs %0, cntpct_el0" : "=r"(t_scan1));
__asm__ volatile("mrs %0, cntfrq_el0" : "=r"(frq));
uint32_t scan_ms = (uint32_t)(frq >= 1000 ? (t_scan1 - t_scan0) / (frq / 1000) : 0);
Expand Down Expand Up @@ -219,9 +219,9 @@ const char *loader_boot(uintptr_t dtb)
zero64(ba_pa, PAGE_16K);
volatile struct boot_args *ba = (volatile struct boot_args *)ba_pa;
#ifdef EL2_RESIDENT
uint64_t bootflags = payload_bootflags(1, el2_s2_on);
uint64_t bootflags = SOC_HOOK_BOOTFLAGS(payload_bootflags(1, el2_s2_on));
#else
uint64_t bootflags = 0;
uint64_t bootflags = SOC_HOOK_BOOTFLAGS(0); /* soc_hooks.h */
#endif
payload_fill_boot_args(ba, p, top, adt_pa - pa_of_va_base, bootflags); /* xnu reads deviceTreeP with the MMU on: a VA */
/* --mif-clk (T-5030): MIF/INT GET + one MIF request, sweetmif* tokens; no-op otherwise */
Expand Down Expand Up @@ -258,7 +258,7 @@ const char *loader_boot(uintptr_t dtb)
uintptr_t entry_pa = p->entry_va + pa_of_va_base;
con_hex("JUMP", entry_pa);
con_str("XNU", "jumping (fbcon clears screen)");

SOC_HOOK_BEFORE_JUMP(); /* soc_hooks.h: the last SoC step before the jump */
/* Everything was written with the MMU and caches off; drop any stale I-cache lines too. */
__asm__ volatile("dsb sy; ic iallu; dsb sy; isb" ::: "memory");
((xnu_entry_t)entry_pa)(ba_pa);
Expand Down
2 changes: 2 additions & 0 deletions shim/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

/* Boot the SWEETXNU payload found in the ABL ramdisk; returns only on error. */
const char *loader_boot(uintptr_t dtb);
/* loader.c board hooks (SOC_HOOK_*): included here so loader.c keeps its line numbers (SHIM_ASSERT logs __LINE__) */
#include "soc_hooks.h"
90 changes: 78 additions & 12 deletions shim/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "gpus2mpu.h"
#include "gpujob.h"
#include "touchprobe.h"
#include "soc_hooks.h"
#ifdef EL2_TRAP
#include "el2.h"
#endif
Expand Down Expand Up @@ -111,7 +112,11 @@ static inline void wr32(uintptr_t a, uint32_t v) { *(volatile uint32_t *)a = v;
static inline void wr8(uintptr_t a, uint8_t v) { *(volatile uint8_t *)a = v; }

extern char secondary_entry[];
#if SOC_GS101
/* The probe flow (bluejay_probe, the fault-guarded accessors, the exception skip) runs on gs101 and
* on every SoC that opts into the board hooks (soc_hooks.h). Its gs101-only register steps keep
* their own SOC_GS101 guards: DECON re-arm, PMU CPU prep, the REBOOT_BL words. */
#define SHIM_PROBE (SOC_GS101 || SOC_SHIM_HOOKS)
#if SHIM_PROBE
static const uint64_t board_mpidrs[NR_CPUS] = {
BOARD_MPIDR_0, BOARD_MPIDR_1, BOARD_MPIDR_2, BOARD_MPIDR_3,
BOARD_MPIDR_4, BOARD_MPIDR_5, BOARD_MPIDR_6, BOARD_MPIDR_7,
Expand All @@ -121,7 +126,7 @@ static const uint64_t board_mpidrs[NR_CPUS] = {
static const char *stage; /* set at runtime: no absolute pointers in data */
volatile uint64_t probe_entry_el;
static volatile uint32_t probe_guard, probe_fault;
#if SOC_GS101
#if SHIM_PROBE
static volatile uintptr_t probe_fault_address;
#endif
static volatile uint64_t sec_mpidr[NR_CPUS];
Expand Down Expand Up @@ -162,9 +167,9 @@ static uint64_t smc(uint64_t fn, uint64_t a1, uint64_t a2, uint64_t a3)
* usbprobe.h): sets probe_guard around the load so a data abort or
* SError from a gated clock/power domain lands in exc_handler's
* probe_guard skip and the caller prints "unreadable" instead of
* panicking. That skip is compiled in only for SOC_GS101, so on
* other boards try_read32 is only as guarded as a raw read (and
* nothing calls it there).
* panicking. That skip is compiled in only for the probe SoCs
* (SHIM_PROBE), so on other boards try_read32 is only as guarded as
* a raw read (and nothing calls it there).
*/
int try_read32(uintptr_t address, uint32_t *value)
{
Expand All @@ -178,8 +183,8 @@ int try_read32(uintptr_t address, uint32_t *value)
return !probe_fault;
}

#if SOC_GS101
/* Defined for SOC_GS101 only; declared (with try_read32) in
#if SHIM_PROBE
/* Defined for the probe SoCs only (SHIM_PROBE); declared (with try_read32) in
* shim/usbgadget.h, whose only caller is usbgadget.c - linked into the
* shim only when build.sh runs with --usb-gadget. */
int try_write32(uintptr_t address, uint32_t value)
Expand Down Expand Up @@ -236,6 +241,9 @@ static void report_bluejay_watchdog(const char *label, const struct wdt_state *s
* reset. ponytail: the WDT path assumes the s3c2410 layout and an unmasked
* PMU reset source (UNSURE on gs101); it only runs after both PSCI calls
* returned, i.e. already failed. */
#if defined(REBOOT_BL) && !SOC_GS101
#error "REBOOT_BL: the bootloader-reset words below are gs101 PMU registers"
#endif
#ifdef REBOOT_BL
/* Reset INTO THE BOOTLOADER without a button press (docs/research/10): ABL reads the volatile PMU register
* SYSIP_DAT0 (0x17460810) after reset; 0xfc = "reboot bootloader". Only EL3 can write it: the PMU SMC
Expand Down Expand Up @@ -305,6 +313,7 @@ static void __attribute__((noreturn)) bluejay_reset_chain(void)
}

/* WTCON/WTCNT layout and ENABLE/RSTEN bits: https://github.com/torvalds/linux/blob/master/drivers/watchdog/s3c2410_wdt.c */
#if SOC_GS101 /* gs101 display block; a hook SoC arms its display in soc_hook_video() */
/* Linaro reports ABL disables DECON HW trigger: https://patchew.org/linux/20241216-gs101-simplefb-v1-0-8ccad1830281%40linaro.org/ */
/* DECON0 is 0x1c300000 in GS101 DTS; TRIG_CON is 0x30 and HW_TRIG_EN is bit 0: https://android.googlesource.com/kernel/google-modules/display/+/661047c0c1c4939aa81fcc822bb21dc5a484f913/samsung/cal_9845/regs-decon.h */
/* Whole-word command-mode trigger value 0x3061: https://gitlab.com/postmarketOS/pmaports/-/blob/v26.06/device/testing/soc-google-gs101/00-enable-fb.sh */
Expand All @@ -324,6 +333,7 @@ static void decon_rearm_trigger(void)
}
con_hex2("DECON TRIG", "before=", before, "after=", after);
}
#endif /* SOC_GS101 */

static void report_cpu(unsigned cpu)
{
Expand All @@ -342,6 +352,7 @@ static void report_cpu(unsigned cpu)
con_hex("timer freq used", counter_freq());
}

#if SOC_GS101 /* gs101 PMU words and SMC; no other SoC has a derived CPU prep */
static int prepare_bluejay_cpu(unsigned cpu)
{
uint64_t caller = rsr("mpidr_el1");
Expand All @@ -362,6 +373,7 @@ static int prepare_bluejay_cpu(unsigned cpu)
con_hex("PMU intr-gen success", ok);
return ok;
}
#endif /* SOC_GS101 */

#ifdef USB_GADGET
/* ---- stage-2 USB CDC-ACM log gadget (build flag --usb-gadget, default OFF) ----
Expand Down Expand Up @@ -571,6 +583,15 @@ static void udump_dump(void)
static void __attribute__((noreturn)) bluejay_probe(uintptr_t dtb)
{
stage = "bluejay probe";
#if SOC_SHIM_HOOKS
uint32_t soc_entry = soc_hook_entry(dtb); /* first: before anything can hang */
#ifdef USB_GADGET
if (soc_entry & SOC_ENTRY_USB_EARLY)
usb_gadget_start(dtb);
#else
(void)soc_entry;
#endif
#endif
struct wdt_state wdt_cl0 = {0}, wdt_cl1 = {0};
disable_bluejay_watchdog(WDT_CL0_BASE, &wdt_cl0);
disable_bluejay_watchdog(WDT_CL1_BASE, &wdt_cl1);
Expand All @@ -585,19 +606,35 @@ static void __attribute__((noreturn)) bluejay_probe(uintptr_t dtb)
} else {
video = (struct fdt_video){ FB_BASE, FB_SIZE, FB_W, FB_H, FB_STRIDE };
}
#if SOC_SHIM_HOOKS
soc_hook_video(&video);
#endif
con_set_video(video.base, video.width, video.height, video.stride);
con_clear(0xff000000U);
con_str("FB", live_framebuffer ? "from live FDT" : "board fallback");
#if SOC_SHIM_HOOKS
soc_hook_console(&video);
#endif
con_hex("ENTRY EL", probe_entry_el);
pr3_marker_run(); /* style-allow: func-len legacy */ /* style-allow: sized-types legacy */
/* Locate the ramoops console zone before anything can crash: from
* here on every checkpoint save (and the panic path) hands the log
* to Android through pstore. */
#if SOC_SHIM_HOOKS
if (!pstore_init(dtb))
soc_hook_pstore_fallback();
#else
pstore_init(dtb);
#endif
pstore_save(); /* checkpoint 1: earliest log */
#if SOC_SHIM_HOOKS
soc_hook_probes();
#endif
report_bluejay_watchdog("WDT CL0", &wdt_cl0);
report_bluejay_watchdog("WDT CL1", &wdt_cl1);
#if SOC_GS101
decon_rearm_trigger();
#endif
pstore_save(); /* checkpoint 2: watchdog + DECON */
#ifdef PMIC_SRC
/* READ-ONLY (--pmic-src, pmicsrc.c, docs/research/139): the PMIC reset-source latches through
Expand All @@ -617,6 +654,9 @@ static void __attribute__((noreturn)) bluejay_probe(uintptr_t dtb)
* probe (and the USB log, if built in), so a failure still yields
* data. The probe sections never run on the success path. */
con_str("SECTION", "boot XNU");
#if SOC_SHIM_HOOKS
soc_hook_pre_xnu();
#endif
#ifdef XNU_UFS
/* ABL shut the UFS host down; XNU's sweet_ufs driver (patch 0080) expects HCE + link up. */
con_hex("UFS cold init rc", (uint32_t)ufs_cold_init());
Expand Down Expand Up @@ -693,6 +733,9 @@ static void __attribute__((noreturn)) bluejay_probe(uintptr_t dtb)
}
#endif
#ifdef USB_GADGET
#if SOC_SHIM_HOOKS
if (!(soc_entry & SOC_ENTRY_USB_EARLY)) /* else soc_hook_entry() asked for it at the top */
#endif
usb_gadget_start(dtb); /* live log from here on */
#endif
#if defined(EL2_TRAP) && !defined(EL2_RESIDENT) /* EL2_RESIDENT (SPEC 3.4 (b) design item 1 (ii)): the T1/T2 test reads are compiled out, there is no trap page */
Expand Down Expand Up @@ -741,12 +784,16 @@ static void __attribute__((noreturn)) bluejay_probe(uintptr_t dtb)
/* ALREADY_ON / ON_PENDING: the core is coming; just wait. */
con_str("CPU path", "already on / on pending; wait");
} else {
#if SOC_GS101
con_str("CPU path", "error: PMU prep, then one retry");
prepare_bluejay_cpu(cpu);
ret = (int32_t)smc(PSCI_CPU_ON, board_mpidrs[cpu],
(uintptr_t)secondary_entry, cpu);
con_hex2("CPU_ON 2nd", "cpu=", cpu, "ret=", (uint32_t)ret);
retried = 1;
#else
con_str("CPU path", "error: no CPU prep for this SoC; not retried");
#endif
}
/* 2 s: A76s can take ~300 ms to reach secondary_entry. */
uint64_t deadline = now() + counter_freq() * 2;
Expand Down Expand Up @@ -1022,7 +1069,7 @@ static void __attribute__((noreturn)) bluejay_probe(uintptr_t dtb)
bluejay_reset_chain();
#endif
}
#endif
#endif /* SHIM_PROBE */

static uint64_t now(void)
{
Expand All @@ -1044,8 +1091,9 @@ uint64_t touch_ticks(void) /* touchprobe.c / gpupower.c: bounded waits (CNTPCT)
}
#endif

static void __attribute__((noreturn)) reset(unsigned after_s)
static void __attribute__((noreturn)) reset(uint32_t after_s)
{
/* style-allow: assert on the stop path (STYLE-C rule 4: no assertion in reset) */
(void)after_s; /* unused under --pstore-fast */
#if SOC_GS101
#ifdef PSTORE_FAST
Expand All @@ -1059,6 +1107,20 @@ static void __attribute__((noreturn)) reset(unsigned after_s)
pager_replay();
bluejay_reset_chain();
#endif
#elif SOC_SHIM_HOOKS
/* A hook SoC: PSCI only. The branch below writes sweet's Qualcomm restart-reason and PSHOLD
* words, which are not this SoC's. */
con_skip();
#ifdef PSTORE_FAST
after_s = 5;
#endif
con_hex("RESET PSCI SYSTEM_RESET in s", after_s);
wait_ticks(counter_freq() * after_s);
__asm__ volatile("dsb sy" ::: "memory");
smc(PSCI_SYSTEM_RESET, 0, 0, 0);
/* bounded: halt on purpose: the reset was requested, wait for it to take effect */
for (;;)
__asm__ volatile("wfe");
#else
con_skip();
con_str("RESET", after_s > 20 ? "in 40s" : "in 20s");
Expand Down Expand Up @@ -1101,6 +1163,8 @@ void __attribute__((noreturn)) shim_halt(void)
(void)try_write32(WDT_CL0_BASE + 8, 1);
(void)try_write32(WDT_CL0_BASE, (3U << 3) | (1U << 5) | 1U);
}
#elif SOC_SHIM_HOOKS
(void)smc(PSCI_SYSTEM_RESET, 0, 0, 0); /* a hook SoC: PSCI only (no sweet words) */
#else
wr32(IMEM_RESTART_REASON, RESTART_BOOTLOADER);
__asm__ volatile("dsb sy" ::: "memory");
Expand Down Expand Up @@ -1145,11 +1209,12 @@ static void irq_handler(void)

void exc_handler(uint64_t slot, uint64_t *regs)
{
/* style-allow: assert on the stop path (STYLE-C rule 4: no assertion in exc_handler) */
if (slot == 5) { /* IRQ from current EL */
irq_handler();
return;
}
#if SOC_GS101
#if SHIM_PROBE
if (probe_guard) {
if (slot == 4) {
uint64_t esr = rsr("esr_el1");
Expand Down Expand Up @@ -1435,7 +1500,8 @@ static uint32_t to_us(uint64_t ticks)

void shim_main(uint64_t dtb)
{
#if SOC_GS101
/* style-allow: assert stage dispatch only; the checks live in the stages it calls (loader.c) */
#if SHIM_PROBE
bluejay_probe(dtb);
#endif
struct mmu_info mi = { 0 }; /* mmu_enable can bail before n_l3/sctlr_* are set */
Expand Down Expand Up @@ -1463,7 +1529,7 @@ void shim_main(uint64_t dtb)
uint64_t mem_off = ticks_mem();

stage = "mmu enable";
int rc = mmu_enable(&mi, dtb);
int32_t rc = mmu_enable(&mi, dtb);

stage = "fill on";
uint64_t fill_on = rc ? 0 : ticks_fill(RED);
Expand Down
Loading