diff --git a/Source/UnitTests/CMakeLists.txt b/Source/UnitTests/CMakeLists.txt index dca383a026..2acdbe7f3a 100644 --- a/Source/UnitTests/CMakeLists.txt +++ b/Source/UnitTests/CMakeLists.txt @@ -14,10 +14,35 @@ add_custom_command(TARGET tests POST_BUILD ) add_dependencies(unittests tests) +# StarletBootstrapProbe's --broadway-h1a client (Tools/StarletBootstrapProbe/ProbeBroadway.h): +# StarOS's bench loader sources, vendored unchanged in StarOSLoader/, over the probe's host HAL in +# BroadwayHAL/. Their SHA-256s, computed here, are built in: the probe prints them. +set(STAROS_LOADER_DIR ${CMAKE_SOURCE_DIR}/Tools/StarletBootstrapProbe/StarOSLoader) +set(STAROS_LOADER_SOURCES "// Generated by Source/UnitTests/CMakeLists.txt: the vendored StarOS loader sources.\n#pragma once\n#define STAROS_LOADER_SHA256 \\\n") +foreach(name hw0c.c hw0c.h hw0d.c hw0d.h h1a.c h1a.h staros_hw0c.h staros_hw0d.h staros_h1a.h) + file(SHA256 ${STAROS_LOADER_DIR}/${name} hash) + string(APPEND STAROS_LOADER_SOURCES " \"${name} ${hash}\\n\" \\\n") + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${STAROS_LOADER_DIR}/${name}) +endforeach() +string(APPEND STAROS_LOADER_SOURCES " \"\"\n") +file(CONFIGURE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/StarletProbeBroadway/ProbeBroadwaySources.h + CONTENT "${STAROS_LOADER_SOURCES}" @ONLY) +add_library(StarletProbeBroadway STATIC EXCLUDE_FROM_ALL + ${CMAKE_SOURCE_DIR}/Tools/StarletBootstrapProbe/ProbeBroadway.cpp + ${STAROS_LOADER_DIR}/hw0c.c ${STAROS_LOADER_DIR}/hw0d.c ${STAROS_LOADER_DIR}/h1a.c) +set_target_properties(StarletProbeBroadway PROPERTIES C_STANDARD 11 C_STANDARD_REQUIRED ON) +target_compile_definitions(StarletProbeBroadway PRIVATE STAROS_HW0C STAROS_HW0D STAROS_H1A + STAROS_HW0F_HOST STAROS_HW0D_HOST STAROS_H1A_HOST) +target_include_directories(StarletProbeBroadway + PRIVATE ${CMAKE_SOURCE_DIR}/Tools/StarletBootstrapProbe/BroadwayHAL ${STAROS_LOADER_DIR} + ${CMAKE_CURRENT_BINARY_DIR}/StarletProbeBroadway + PUBLIC ${CMAKE_SOURCE_DIR}/Tools/StarletBootstrapProbe) +target_link_libraries(StarletProbeBroadway PUBLIC common PRIVATE fmt::fmt) + # Runs a real Starlet ELF through the fork's CPU and SRAM bus without NAND or a frontend. add_executable(StarletBootstrapProbe EXCLUDE_FROM_ALL ${CMAKE_SOURCE_DIR}/Tools/StarletBootstrapProbe/StarletBootstrapProbe.cpp StubHost.cpp) -target_link_libraries(StarletBootstrapProbe PRIVATE fmt::fmt core uicommon) +target_link_libraries(StarletBootstrapProbe PRIVATE fmt::fmt core uicommon StarletProbeBroadway) macro(add_dolphin_test target) add_library(${target} OBJECT ${ARGN}) diff --git a/Source/UnitTests/Core/CMakeLists.txt b/Source/UnitTests/Core/CMakeLists.txt index 54d23e3213..3e107ec386 100644 --- a/Source/UnitTests/Core/CMakeLists.txt +++ b/Source/UnitTests/Core/CMakeLists.txt @@ -32,6 +32,10 @@ target_include_directories(StarletProbeStagingTest PRIVATE ${CMAKE_SOURCE_DIR}/T add_dolphin_test(StarletProbeScriptTest IOS/Starlet/ProbeScriptTest.cpp) target_include_directories(StarletProbeScriptTest PRIVATE ${CMAKE_SOURCE_DIR}/Tools/StarletBootstrapProbe) add_dolphin_test(StarletProbeInterruptTest IOS/Starlet/ProbeInterruptTest.cpp) +# StarletBootstrapProbe's --broadway-h1a client (Tools/StarletBootstrapProbe/ProbeBroadway.h). +add_dolphin_test(StarletProbeBroadwayTest IOS/Starlet/ProbeBroadwayTest.cpp) +target_link_libraries(StarletProbeBroadwayTest PUBLIC StarletProbeBroadway) +target_include_directories(StarletProbeBroadwayTest PRIVATE ${STAROS_LOADER_DIR}) add_dolphin_test(StarletDMATest IOS/Starlet/DMATest.cpp) add_dolphin_test(StarletSRAMReadTest IOS/Starlet/SRAMReadTest.cpp) add_dolphin_test(StarletSRAMWriteTest IOS/Starlet/SRAMWriteTest.cpp) diff --git a/Source/UnitTests/Core/IOS/Starlet/ProbeBroadwayTest.cpp b/Source/UnitTests/Core/IOS/Starlet/ProbeBroadwayTest.cpp new file mode 100644 index 0000000000..dc0b3f7342 --- /dev/null +++ b/Source/UnitTests/Core/IOS/Starlet/ProbeBroadwayTest.cpp @@ -0,0 +1,296 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include +#include + +#include + +#include "ProbeBroadway.h" +#include "ProbeBudget.h" +#include "staros_h1a.h" +#include "staros_hw0c.h" + +namespace StarletBootstrapProbe +{ +namespace +{ +// StarOS boot/wii/loader/board.h's timebase ticks a millisecond. +constexpr u64 TICKS_PER_MS = 60750; + +// PPC memory as the client sees it: the bench window's words, and a count of what fell outside. +class FakeMemory final : public BroadwayMemory +{ +public: + u32 LoadWord(u32 address) override + { + if (InBenchWindow(address)) + return words[(address - BENCH_WINDOW_BASE) / 4]; + ++outside; + return 0; + } + void StoreWord(u32 address, u32 word) override + { + if (InBenchWindow(address)) + words[(address - BENCH_WINDOW_BASE) / 4] = word; + else + ++outside; + } + u32& At(u32 offset) { return words[offset / 4]; } + + std::vector words = std::vector((BENCH_WINDOW_END - BENCH_WINDOW_BASE) / 4); + int outside = 0; +}; + +TEST(StarletProbeBroadway, TheWindowIsTheContractsAndNothingElseIsReached) +{ + EXPECT_EQ(BENCH_WINDOW_BASE, u32{STAROS_HW0C_BASE}); + EXPECT_EQ(BENCH_WINDOW_END, u32{STAROS_HW0C_BASE + STAROS_HW0C_SIZE}); + FakeMemory memory; + BroadwayHal hal(memory, 100); + hal.Write(BENCH_WINDOW_BASE, 7); + hal.Write(BENCH_WINDOW_END - 4, 9); + EXPECT_EQ(hal.Read(BENCH_WINDOW_BASE), u32{7}); + EXPECT_EQ(hal.Read(BENCH_WINDOW_END - 4), u32{9}); + hal.CacheLine(BENCH_WINDOW_BASE + 0x20); + EXPECT_FALSE(hal.Failed()); + EXPECT_EQ(hal.Accesses(), u64{5}); + + // The first access outside fails the client, and is never made. + EXPECT_EQ(hal.Read(BENCH_WINDOW_END), u32{0}); + EXPECT_TRUE(hal.Failed()); + EXPECT_EQ(hal.Failure(), "a read at 0x13510000, outside the bench window 0x13500000..0x13510000"); + hal.Write(BENCH_WINDOW_BASE - 4, 1); + hal.Write(BENCH_WINDOW_BASE + 2, 1); + EXPECT_EQ(memory.outside, 0); + EXPECT_EQ(hal.Failure(), "a read at 0x13510000, outside the bench window 0x13500000..0x13510000"); + EXPECT_FALSE(InBenchWindow(BENCH_WINDOW_BASE + 2)); + EXPECT_FALSE(InBenchWindow(0x135E0000)); + EXPECT_FALSE(InBenchWindow(u64{BENCH_WINDOW_BASE} + 0x100000000)); +} + +TEST(StarletProbeBroadway, TheTimebaseIsAQuarterOfTheStarletsCycles) +{ + FakeMemory memory; + BroadwayHal hal(memory, CLIENT_TIMEBASE_BASE); + hal.Elapse(0); + EXPECT_EQ(hal.Timebase(), CLIENT_TIMEBASE_BASE); + hal.Elapse(SLICE_CYCLES); + EXPECT_EQ(hal.Timebase(), CLIENT_TIMEBASE_BASE + 1024); + // 2 ms of Broadway's timebase (StarOS's TICKS_PER_MS, 60750) is 486,000 Starlet cycles. + hal.Elapse(486000); + EXPECT_EQ(hal.Timebase() - CLIENT_TIMEBASE_BASE - 1024, u64{121500}); +} + +TEST(StarletProbeBroadway, TheCaptureIsGeckosFormats) +{ + FakeMemory memory; + BroadwayHal hal(memory, 0); + hal.Put("SBL:X "); + hal.Hex(0x1A); + hal.Put(" "); + hal.Dec(0); + hal.Put(" "); + hal.Dec(0xFFFFFFFF); + hal.Put(" "); + hal.Hex(0xDEADBEEF); + EXPECT_EQ(hal.Capture(), "SBL:X 0x0000001a 0 4294967295 0xdeadbeef"); +} + +TEST(StarletProbeBroadway, TheClientsBudgetIsBoundedAndOnlyItsOwn) +{ + EXPECT_EQ(MAX_CLIENT_CYCLES, u64{1} << 34); + EXPECT_EQ(ParseCycles("17179869184", MAX_CLIENT_CYCLES), MAX_CLIENT_CYCLES); + EXPECT_FALSE(ParseCycles("17179873280", MAX_CLIENT_CYCLES)); + EXPECT_FALSE(ParseCycles("17179869185", MAX_CLIENT_CYCLES)); + // Without the client, 2^30 as before. + EXPECT_EQ(ParseCycles("1073741824"), MAX_CYCLES); + EXPECT_FALSE(ParseCycles("1073745920")); + EXPECT_FALSE(ParseCycles("17179869184")); +} + +TEST(StarletProbeBroadway, TheBuiltInSourcesAreTheNineFilesWithTheirHashes) +{ + const std::string_view sources = BroadwayClient::Sources(); + const char* names[] = {"hw0c.c", "hw0c.h", "hw0d.c", "hw0d.h", "h1a.c", + "h1a.h", "staros_hw0c.h", "staros_hw0d.h", "staros_h1a.h"}; + size_t at = 0; + for (const char* name : names) + { + const size_t end = sources.find('\n', at); + ASSERT_NE(end, std::string_view::npos); + const std::string_view line = sources.substr(at, end - at); + EXPECT_TRUE(line.starts_with(std::string(name) + " ")) << line; + EXPECT_EQ(line.size(), std::string_view(name).size() + 1 + 64) << line; + at = end + 1; + } + EXPECT_EQ(at, sources.size()); +} + +// A scripted Starlet over the fake window, as StarOS's tests/hardware-wii/h1a benches script it: +// READY 100 ms after the first step (unless `ready` is false), then each round answered at least +// 40,960 timebase ticks after it was first seen posted, S2B's words first for a Starlet -> Broadway +// round; after the last round or Broadway's END, its record (GEN 2) and its END (unless `ends` is +// false). +struct Starlet +{ + bool ready = true; + bool ends = true; + bool answered_ready = false; + bool ended = false; + u32 answered = 0; + bool seen = false; + u64 seen_at = 0; + + static u32 Scenario(u32 round) { return round <= STAROS_H1A_S1_ROUNDS ? STAROS_H1A_S1 : STAROS_H1A_S3; } + + void Tick(FakeMemory& memory, u64 now, u64 start) + { + const u32 broadway = STAROS_HW0C_BROADWAY, starlet = STAROS_HW0C_STARLET; + if (!answered_ready) + { + if (ready && now >= start + 100 * TICKS_PER_MS) + { + answered_ready = true; + memory.At(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_CONTROL) = 0x000520FB; + memory.At(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_DACR) = 1; + memory.At(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_DESCRIPTOR) = 0x13500552; + memory.At(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_SENTINEL_BAD) = 0; + memory.At(starlet + STAROS_HW0C_C_STATE) = STAROS_HW0C_STATE_READY; + } + return; + } + if (ended) + return; + if (answered == STAROS_H1A_ROUNDS || + memory.At(broadway + STAROS_HW0C_C_STATE) == STAROS_HW0C_STATE_END) + { + if (!ends) + return; + for (u32 i = 0; i < STAROS_H1A_RESULTS_WORDS; ++i) + memory.At(STAROS_H1A_RESULTS + 4 * i) = 0x1000 + i; + memory.At(STAROS_H1A_RESULTS + 4 * STAROS_H1A_F_MAGIC) = STAROS_H1A_MAGIC; + memory.At(STAROS_H1A_RESULTS + 4 * STAROS_H1A_F_VERSION) = STAROS_H1A_VERSION; + memory.At(STAROS_H1A_RESULTS + 4 * STAROS_H1A_F_GEN) = STAROS_H1A_GEN; + memory.At(starlet + STAROS_HW0C_C_STATE) = STAROS_HW0C_STATE_END; + ended = true; + return; + } + const u32 round = answered + 1; + if (memory.At(broadway + STAROS_HW0C_C_STATE) != STAROS_HW0C_STATE_ROUND || + memory.At(broadway + STAROS_HW0C_C_ROUND) != round || + memory.At(broadway + STAROS_HW0C_C_SCENARIO) != Scenario(round)) + { + return; + } + if (!seen) + { + seen = true; + seen_at = now; + } + if (now - seen_at < 40960) + return; + const bool s2b = round > STAROS_H1A_S1_ROUNDS && (round - STAROS_H1A_S1_ROUNDS) % 2 == 0; + if (s2b) + { + for (u32 w = 0; w < STAROS_H1A_AREA_WORDS; ++w) + { + const u32 offset = STAROS_H1A_S2B + 4 * w; + memory.At(offset) = STAROS_HW0C_PATTERN(STAROS_HW0C_TAG_S2B, round, offset / 4); + } + } + memory.At(starlet + STAROS_HW0C_C_SCENARIO) = Scenario(round); + memory.At(starlet + STAROS_HW0C_C_STATE) = STAROS_HW0C_STATE_ROUND; + memory.At(starlet + STAROS_HW0C_C_ROUND) = round; + answered = round; + seen = false; + } +}; + +// One whole run of the real loader code in this process (its state is global: each death test's +// child runs it alone). Its report goes to stderr; the child exits 0 only if the run ended (or not) +// as `done` says, reached nothing outside the window, held every line of `required` in its report, +// and a second client in the same process was refused. (Windows' gtest has only simple regular +// expressions, so the child checks the report itself.) +[[noreturn]] void RunClient(Starlet starlet, u64 slices, bool done, + std::vector required) +{ + FakeMemory memory; + BroadwayClient client; + if (!client.Start(memory)) + std::exit(2); + const u64 start = client.Timebase(); + for (u64 slice = 0; slice < slices && !client.Done() && !client.Failed(); ++slice) + { + starlet.Tick(memory, client.Timebase(), start); + client.Advance(slice == 0 ? 0 : SLICE_CYCLES); + } + const std::string report = + client.Report(client.Done() ? CLIENT_STATUS_DONE : CLIENT_STATUS_TIMEOUT); + std::fputs(report.c_str(), stderr); + int missing = 0; + for (const std::string_view line : required) + { + if (report.find(line) == std::string::npos) + { + std::fprintf(stderr, "MISSING %.*s\n", static_cast(line.size()), line.data()); + ++missing; + } + } + BroadwayClient second; + FakeMemory other; + const bool refused = !second.Start(other) && second.Failed(); + std::fprintf(stderr, "steps %llu outside %d failed %d done %d refused %d missing %d\n", + static_cast(client.Steps()), memory.outside, + static_cast(client.Failed()), static_cast(client.Done()), + static_cast(refused), missing); + const bool passed = client.Done() == done && !client.Failed() && memory.outside == 0 && + refused && missing == 0; + std::exit(passed ? 0 : 1); +} + +// Enough slices for 3000 rounds of at least 2 ms each, and the waits: 2^34 cycles' worth. +constexpr u64 SLICES = MAX_CLIENT_CYCLES / SLICE_CYCLES; + +TEST(StarletProbeBroadwayDeathTest, TheRealLoaderRunsAllRoundsAndReports) +{ + EXPECT_EXIT(RunClient(Starlet{}, SLICES, true, + {"SBL:STARLET DONE\n", "SBL:HW0C S0 PASS\n", + "SBL:H1A B POSTS 3000 LAST 3000 ACKS 3000 TIMEOUT 0 ", + "SBL:H1A B CHECK SENTINEL 0 SCRUB 0 POOL 0\n", + "SBL:H1A S GEN 0x00000002 0x00000002 ATTEMPTS 1 COHERENT\n", + "SBL:H1A END CRC 0x", "\nSBL:END GECKO DROPPED AFTER JUMP 0 BEFORE 0\n"}), + ::testing::ExitedWithCode(0), ""); +} + +TEST(StarletProbeBroadwayDeathTest, WithoutReadyTheBenchTimesOutAndNeverStartsH1A) +{ + Starlet starlet; + starlet.ready = false; + EXPECT_EXIT(RunClient(starlet, SLICES, true, + {"SBL:HW0C S0 INCONCLUSIVE\n", "SBL:HW0C TIMEOUT S0 ROUND 0\n", + "SBL:H1A B POSTS 0 LAST 0 ACKS 0 ", "ATTEMPTS 3 INCOMPLETE\n"}), + ::testing::ExitedWithCode(0), ""); +} + +TEST(StarletProbeBroadwayDeathTest, WithoutTheStarletsEndBroadwayStopsAtReadyPlus65s) +{ + Starlet starlet; + starlet.ends = false; + EXPECT_EXIT(RunClient(starlet, SLICES, true, + {"SBL:HW0C S0 INCONCLUSIVE\n", "SBL:HW0C TIMEOUT S0 ROUND 0\n", + "SBL:H1A B POSTS 3000 LAST 3000 ", "ATTEMPTS 3 INCOMPLETE\n"}), + ::testing::ExitedWithCode(0), ""); +} + +TEST(StarletProbeBroadwayDeathTest, ABudgetTooShortEndsUnderTimeout) +{ + EXPECT_EXIT(RunClient(Starlet{}, 1000, false, + {"SBL:TIMEOUT\n", "SBL:END GECKO DROPPED AFTER JUMP 0 BEFORE 0\n"}), + ::testing::ExitedWithCode(0), ""); +} +} // namespace +} // namespace StarletBootstrapProbe diff --git a/Tools/StarletBootstrapProbe/BroadwayHAL/board.h b/Tools/StarletBootstrapProbe/BroadwayHAL/board.h new file mode 100644 index 0000000000..456e16f303 --- /dev/null +++ b/Tools/StarletBootstrapProbe/BroadwayHAL/board.h @@ -0,0 +1,30 @@ +/* Copyright 2026 Dolphin Emulator Project */ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * StarletBootstrapProbe's Broadway client (ProbeBroadway.h): StarOS's loader header board.h as the + * vendored loader sources in ../StarOSLoader see it on a host. Every word access goes to + * ProbeBroadway.cpp, which allows only the bench window of PPC memory; sync is nothing in + * Dolphin's coherent memory; the timebase is the client's, 243 MHz / 4 from the Starlet's cycles. + */ +#ifndef STAROS_LOADER_BOARD_H +#define STAROS_LOADER_BOARD_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +uint32_t read32(uint32_t address); +void write32(uint32_t address, uint32_t value); +void sync(void); +uint64_t timebase(void); +/* StarOS boot/wii/loader/board.h's: timebase ticks a millisecond. */ +#define TICKS_PER_MS 60750u + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Tools/StarletBootstrapProbe/BroadwayHAL/gecko.h b/Tools/StarletBootstrapProbe/BroadwayHAL/gecko.h new file mode 100644 index 0000000000..ec8fd5df77 --- /dev/null +++ b/Tools/StarletBootstrapProbe/BroadwayHAL/gecko.h @@ -0,0 +1,27 @@ +/* Copyright 2026 Dolphin Emulator Project */ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * StarletBootstrapProbe's Broadway client (ProbeBroadway.h): StarOS's loader header gecko.h as the + * vendored loader sources see it on a host. The lines go to the client's capture, which the probe + * prints on stdout, in the formats of StarOS boot/wii/loader/gecko.c (hex: 0x and eight lowercase + * digits; decimal: no leading zero). + */ +#ifndef STAROS_LOADER_GECKO_H +#define STAROS_LOADER_GECKO_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void gecko_puts(const char *text); +void gecko_hex(uint32_t value); +void gecko_dec(uint32_t value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Tools/StarletBootstrapProbe/ProbeBroadway.cpp b/Tools/StarletBootstrapProbe/ProbeBroadway.cpp new file mode 100644 index 0000000000..a63f7f4d52 --- /dev/null +++ b/Tools/StarletBootstrapProbe/ProbeBroadway.cpp @@ -0,0 +1,185 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "ProbeBroadway.h" + +#include +#include + +#include + +#include "ProbeBroadwaySources.h" + +extern "C" { +#include "hw0c.h" +} + +namespace StarletBootstrapProbe +{ +namespace +{ +// The one HAL of the process: the vendored sources' functions are global, so is their HAL. +std::optional s_hal; +} // namespace + +bool BroadwayHal::Allowed(u32 address, std::string_view what) +{ + ++m_accesses; + if (InBenchWindow(address)) + return true; + if (m_failure.empty()) + { + m_failure = fmt::format("{} at {:#010x}, outside the bench window {:#010x}..{:#010x}", what, + address, BENCH_WINDOW_BASE, BENCH_WINDOW_END); + } + return false; +} + +u32 BroadwayHal::Read(u32 address) +{ + return Allowed(address, "a read") ? m_memory.LoadWord(address) : 0; +} + +void BroadwayHal::Write(u32 address, u32 value) +{ + if (Allowed(address, "a write")) + m_memory.StoreWord(address, value); +} + +void BroadwayHal::CacheLine(u32 address) +{ + // The line's first word stands for it: the loader passes line-aligned window addresses. + Allowed(address & ~u32{3}, "a cache line"); +} + +void BroadwayHal::Hex(u32 value) +{ + m_capture += fmt::format("0x{:08x}", value); +} + +void BroadwayHal::Dec(u32 value) +{ + m_capture += fmt::format("{}", value); +} + +bool BroadwayClient::Start(BroadwayMemory& memory) +{ + if (s_hal) + { + m_refusal = "a Broadway client already started in this process: the loader's state is global"; + return false; + } + s_hal.emplace(memory, CLIENT_TIMEBASE_BASE); + m_started = true; + // As the loader before the jump: the window's sentinel and control lines, HW-M4-0D's area, H-1A's + // pages and record, the plan and the header's magic last. + hw0c_setup(); + return true; +} + +void BroadwayClient::Advance(u64 elapsed) +{ + if (!m_started || m_done || Failed()) + return; + s_hal->Elapse(elapsed); + ++m_steps; + m_done = hw0c_step(s_hal->Timebase()) != 0; +} + +bool BroadwayClient::Failed() const +{ + return !m_refusal.empty() || (m_started && s_hal->Failed()); +} + +std::string BroadwayClient::Failure() const +{ + if (!m_refusal.empty()) + return m_refusal; + return m_started ? s_hal->Failure() : std::string{}; +} + +u64 BroadwayClient::Timebase() const +{ + return m_started ? s_hal->Timebase() : 0; +} + +std::string BroadwayClient::Report(std::string_view status) +{ + if (!m_started || m_reported) + return {}; + m_reported = true; + const size_t mark = s_hal->Capture().size(); + hw0c_report(); + return fmt::format("{}\n{}{}\n", status, s_hal->Capture().substr(mark), CLIENT_END); +} + +std::string_view BroadwayClient::Sources() +{ + return STAROS_LOADER_SHA256; +} +} // namespace StarletBootstrapProbe + +// The HAL's C functions (BroadwayHAL/board.h and gecko.h, and the cache and special-register +// functions the vendored sources declare under their _HOST macros). +extern "C" { +uint32_t read32(uint32_t address) +{ + return StarletBootstrapProbe::s_hal ? StarletBootstrapProbe::s_hal->Read(address) : 0; +} + +void write32(uint32_t address, uint32_t value) +{ + if (StarletBootstrapProbe::s_hal) + StarletBootstrapProbe::s_hal->Write(address, value); +} + +void sync(void) +{ +} + +uint64_t timebase(void) +{ + return StarletBootstrapProbe::s_hal ? StarletBootstrapProbe::s_hal->Timebase() : 0; +} + +void dcbf(uint32_t address) +{ + if (StarletBootstrapProbe::s_hal) + StarletBootstrapProbe::s_hal->CacheLine(address); +} + +void dcbi(uint32_t address) +{ + if (StarletBootstrapProbe::s_hal) + StarletBootstrapProbe::s_hal->CacheLine(address); +} + +// Not a Broadway: HID0 and L2CR, which hw0c.c only reports, read as 0. +uint32_t hid0(void) +{ + return 0; +} + +uint32_t l2cr(void) +{ + return 0; +} + +void gecko_puts(const char* text) +{ + if (StarletBootstrapProbe::s_hal) + StarletBootstrapProbe::s_hal->Put(text); +} + +void gecko_hex(uint32_t value) +{ + if (StarletBootstrapProbe::s_hal) + StarletBootstrapProbe::s_hal->Hex(value); +} + +void gecko_dec(uint32_t value) +{ + if (StarletBootstrapProbe::s_hal) + StarletBootstrapProbe::s_hal->Dec(value); +} +} diff --git a/Tools/StarletBootstrapProbe/ProbeBroadway.h b/Tools/StarletBootstrapProbe/ProbeBroadway.h new file mode 100644 index 0000000000..c288f37b98 --- /dev/null +++ b/Tools/StarletBootstrapProbe/ProbeBroadway.h @@ -0,0 +1,119 @@ +// Copyright 2026 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +#include "Common/CommonTypes.h" + +// StarletBootstrapProbe's --broadway-h1a: the probe stands in for Broadway with StarOS's own bench +// loader code, so that the H-1A bench (StarOS docs/architecture/h-1a.md, D-H1A-7) runs whole: its +// Starlet image here, on the ARM core; its Broadway side, StarOS's boot/wii/loader hw0c.c, hw0d.c +// and h1a.c, vendored unchanged in StarOSLoader/ and built over the host HAL of BroadwayHAL/. +// +// The client does what StarOS's loader does around the jump: hw0c_setup() before the Starlet's +// first instruction, then one hw0c_step() between each two slices, as the loader's monitor calls +// it, until it says 1; then the report, between the status and END lines main.c prints. It reaches +// PPC memory only in the bench window, by whole words; it knows nothing of the image and answers +// nothing for it: what it reads, the ARM code wrote. Dolphin's memory is coherent, so the run says +// nothing of the console's caches: it rehearses the protocol, its 3000 rounds, the capture and the +// judge, not the hardware. +namespace StarletBootstrapProbe +{ +// The bench window, StarOS boot/contract/staros_hw0c.h's STAROS_HW0C_BASE and _SIZE: the only PPC +// memory the client reads or writes. +constexpr u32 BENCH_WINDOW_BASE = 0x13500000; +// Exclusive. +constexpr u32 BENCH_WINDOW_END = 0x13510000; +// Broadway's timebase runs at 243 MHz / 4, the Starlet at 243 MHz: four cycles a tick. +constexpr u64 CYCLES_PER_TIMEBASE_TICK = 4; +// The client's first timebase. Any value would do (the loader only subtracts); a nonzero one wider +// than 32 bits keeps the 64-bit arithmetic honest. +constexpr u64 CLIENT_TIMEBASE_BASE = u64{1} << 32; +// The lines StarOS's main.c prints around hw0c_report(): its status line once the Starlet is done +// or timed out, and its END line. There is no Gecko here, so nothing was dropped. +constexpr std::string_view CLIENT_STATUS_DONE = "SBL:STARLET DONE"; +constexpr std::string_view CLIENT_STATUS_TIMEOUT = "SBL:TIMEOUT"; +constexpr std::string_view CLIENT_END = "SBL:END GECKO DROPPED AFTER JUMP 0 BEFORE 0"; + +// Whether a whole word at `address` lies in the bench window. +constexpr bool InBenchWindow(u64 address) +{ + return address >= BENCH_WINDOW_BASE && address + 4 <= BENCH_WINDOW_END && address % 4 == 0; +} + +// What the client reaches: PPC memory, by whole words. +class BroadwayMemory +{ +public: + virtual ~BroadwayMemory() = default; + virtual u32 LoadWord(u32 address) = 0; + virtual void StoreWord(u32 address, u32 word) = 0; +}; + +// The HAL the vendored loader sources call (BroadwayHAL/board.h and gecko.h): the window's bounds, +// the clock and the capture. A word outside the window is never touched: the first such access +// fails the client, and a read gives 0. +class BroadwayHal +{ +public: + BroadwayHal(BroadwayMemory& memory, u64 timebase) : m_memory(memory), m_timebase(timebase) {} + + u32 Read(u32 address); + void Write(u32 address, u32 value); + // dcbf and dcbi: a line of the window; nothing to do in Dolphin's coherent memory. + void CacheLine(u32 address); + u64 Timebase() const { return m_timebase; } + // `cycles` Starlet cycles went by. + void Elapse(u64 cycles) { m_timebase += cycles / CYCLES_PER_TIMEBASE_TICK; } + void Put(std::string_view text) { m_capture += text; } + void Hex(u32 value); + void Dec(u32 value); + + const std::string& Capture() const { return m_capture; } + u64 Accesses() const { return m_accesses; } + bool Failed() const { return !m_failure.empty(); } + const std::string& Failure() const { return m_failure; } + +private: + bool Allowed(u32 address, std::string_view what); + + BroadwayMemory& m_memory; + u64 m_timebase; + u64 m_accesses = 0; + std::string m_capture; + std::string m_failure; +}; + +// The client. The vendored sources keep their state in globals, so a process has one client, and +// it starts once. +class BroadwayClient +{ +public: + // hw0c_setup() over `memory`, at CLIENT_TIMEBASE_BASE; false, with the reason in Failure(), if a + // client already started in this process. + bool Start(BroadwayMemory& memory); + // `elapsed` Starlet cycles went by: one hw0c_step() at the timebase they make, unless done. + void Advance(u64 elapsed); + bool Started() const { return m_started; } + bool Done() const { return m_done; } + bool Failed() const; + std::string Failure() const; + u64 Steps() const { return m_steps; } + u64 Timebase() const; + // The capture main.c would print from its status line on: `status`, hw0c_report()'s lines + // (hw0d_report()'s and h1a_report()'s among them), the END line. Once. + std::string Report(std::string_view status); + // The vendored sources, as built: " " lines (CMake computes them at configure). + static std::string_view Sources(); + +private: + bool m_started = false; + bool m_done = false; + bool m_reported = false; + u64 m_steps = 0; + std::string m_refusal; +}; +} // namespace StarletBootstrapProbe diff --git a/Tools/StarletBootstrapProbe/ProbeBudget.h b/Tools/StarletBootstrapProbe/ProbeBudget.h index 5aec19226a..862b7862a9 100644 --- a/Tools/StarletBootstrapProbe/ProbeBudget.h +++ b/Tools/StarletBootstrapProbe/ProbeBudget.h @@ -19,12 +19,17 @@ constexpr u32 SLICE_CYCLES = 4096; constexpr u64 DEFAULT_CYCLES = 512 * SLICE_CYCLES; // 2^30 cycles: 262,144 slices. constexpr u64 MAX_CYCLES = u64{1} << 30; +// With --broadway-h1a only (ProbeBroadway.h): 2^34 cycles, about 70 s of the Starlet's 243 MHz. +// StarOS's H-1A bench runs 3000 rounds of Broadway's posts and the Starlet's answers, each at least +// Broadway's 2 ms between an answer and its next post, after the Starlet's wait for READY. +constexpr u64 MAX_CLIENT_CYCLES = u64{1} << 34; // The budget --cycles asks for: a decimal number of cycles, a whole number of slices, from one -// slice to MAX_CYCLES. Anything else is refused (nullopt), never rounded. -inline std::optional ParseCycles(std::string_view text) +// slice to `max` (MAX_CYCLES, or MAX_CLIENT_CYCLES with the client). Anything else is refused +// (nullopt), never rounded. +inline std::optional ParseCycles(std::string_view text, u64 max = MAX_CYCLES) { - // Twelve digits cannot overflow a u64; MAX_CYCLES has ten. + // Twelve digits cannot overflow a u64; MAX_CLIENT_CYCLES has eleven. if (text.empty() || text.size() > 12) return std::nullopt; u64 value = 0; @@ -34,7 +39,7 @@ inline std::optional ParseCycles(std::string_view text) return std::nullopt; value = value * 10 + static_cast(digit - '0'); } - if (value == 0 || value % SLICE_CYCLES != 0 || value > MAX_CYCLES) + if (value == 0 || value % SLICE_CYCLES != 0 || value > max) return std::nullopt; return value; } diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/.gitattributes b/Tools/StarletBootstrapProbe/StarOSLoader/.gitattributes new file mode 100644 index 0000000000..fa1385d99a --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/.gitattributes @@ -0,0 +1 @@ +* -text diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/h1a.c b/Tools/StarletBootstrapProbe/StarOSLoader/h1a.c new file mode 100644 index 0000000000..639a417ff0 --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/h1a.c @@ -0,0 +1,461 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * H-1A's Broadway side (docs/architecture/h-1a.md, G-H1A-2): crates/hw-bench's + * h1a::setup, mark, Broadway and collect in C, plus the SBL:H1A block. Built + * only with -DSTAROS_HW0C -DSTAROS_HW0D -DSTAROS_H1A, never with + * -DSTAROS_HW0E or -DSTAROS_HW0F; never run on the console without its own + * reviewed GO. + * + * Before the jump, h1a_clear zeroes the record, page 0's near scrub and pages + * 1 to 3, then writes Z in the pool's 188 lines. Once READY is seen, + * h1a_ready writes M in them (T4's publication). Then h1a_step posts the 3000 + * rounds of the frozen schedule, one action a call, as h1a::Broadway does: + * - it waits D_POST (2 ms) from the answer it saw, reads the Starlet's line + * once more and posts nothing to an ended Starlet; + * - for a Broadway -> Starlet round it fills the round's area (each arm turns + * over the four areas) with words naming the round and the address, and + * publishes it; then it writes SCENARIO and STATE, publishes, writes ROUND + * last and publishes; + * - it waits for the answer (200 ms): ROUND and SCENARIO the round's, STATE + * ROUND or END (the Starlet's END keeps its last answer's words); for a + * Starlet -> Broadway round it then reads S2B and counts its stale and + * other words; + * - after the last round, a timeout or the Starlet's END, it writes its END. + * It then waits for the Starlet's END (or READY + 65 s), reads the record at + * most ATTEMPTS times and makes its end checks. + * + * A read of the record is COHERENT only when GEN before, the body's GEN and GEN + * after are equal, even and not 0, and the magic, version and GEN 2 are the + * record's (h1a::qualify). The words are printed as the last read took them, + * never judged here: tools/gecko/check_h1a.py judges them. No AHB, IPC or + * memory-controller register is touched. Every store is an aligned word. + * + * The block ends with a CRC-32 of its numbers as they are printed (T4's I-1). + */ +#include +#include "board.h" +#include "gecko.h" +#include "h1a.h" +#include "staros_hw0c.h" +#include "staros_h1a.h" + +#if !defined(STAROS_HW0C) || !defined(STAROS_HW0D) +#error "h1a.c builds on -DSTAROS_HW0C -DSTAROS_HW0D" +#endif +#if defined(STAROS_HW0E) || defined(STAROS_HW0F) +#error "h1a.c excludes -DSTAROS_HW0E and -DSTAROS_HW0F: their window areas overlap H-1A's" +#endif + +#define AT(offset) (STAROS_HW0C_BASE + (offset)) +#define AREA_BYTES (4u * STAROS_H1A_AREA_WORDS) +#define RESULTS_BYTES (4u * STAROS_H1A_RESULTS_WORDS) +#define LINE STAROS_HW0C_LINE +/* Broadway's wait for the Starlet's END, from READY: past the engine's 60 s + * guard, inside the loader's bench timeout with the wait for READY. */ +#define H1A_END_TIMEOUT_MS 65000u + +static const uint32_t areas[STAROS_H1A_AREA_COUNT] = STAROS_H1A_AREAS; +static const uint32_t near_scrubs[STAROS_H1A_AREA_COUNT] = STAROS_H1A_NEAR; + +enum phase { IDLE, DELAY, POST, WAIT_ACK, ENDING, WAIT_END, DONE }; +enum arm { ARM_F, ARM_W, ARM_N, ARM_P }; + +/* Broadway's own counts and where H-1A is (h1a::Log and h1a::Broadway). */ +static struct { + enum phase phase; + uint32_t round; + uint64_t until, acked, ready; + uint32_t b2s[4]; + uint32_t posts, last, acks, timeout, s2b_rounds, s2b_stale, s2b_other, starlet_end; + uint64_t delay_min, delay_max; + uint32_t started, timed_out, collected; +} b; + +/* The last read of the record, and its end checks. */ +static uint32_t results[STAROS_H1A_RESULTS_WORDS]; +static uint32_t gen_before, gen_after, attempts, coherent; +static uint32_t check_sentinel, check_scrub, check_pool; +/* The CRC-32 of the numbers printed so far: only h1a_report touches it. */ +static uint32_t block_crc; + +#ifdef STAROS_H1A_HOST +/* tests/hardware-wii/h1a's host bench only: its model stands for the two + * cache instructions. */ +void dcbf(uint32_t address); +void dcbi(uint32_t address); +#else +static inline void dcbf(uint32_t address) +{ + __asm__ volatile("dcbf 0,%0" ::"r"(address) : "memory"); +} + +static inline void dcbi(uint32_t address) +{ + __asm__ volatile("dcbi 0,%0" ::"r"(address) : "memory"); +} +#endif + +static void publish(uint32_t offset, uint32_t bytes) +{ + for (uint32_t at = 0; at < bytes; at += LINE) + dcbf(AT(offset + at)); + sync(); +} + +static void refresh(uint32_t offset, uint32_t bytes) +{ + for (uint32_t at = 0; at < bytes; at += LINE) + dcbi(AT(offset + at)); + sync(); +} + +/* The schedule (h1a::s2b, scenario, arm). */ +static int s2b(uint32_t round) +{ + return round > STAROS_H1A_S1_ROUNDS && (round - STAROS_H1A_S1_ROUNDS) % 2u == 0; +} + +static uint32_t scenario(uint32_t round) +{ + return round <= STAROS_H1A_S1_ROUNDS ? STAROS_H1A_S1 : STAROS_H1A_S3; +} + +static enum arm arm(uint32_t round) +{ + uint32_t at = round - 1u; + uint32_t block = at / STAROS_H1A_BLOCK_ROUNDS, place = at % STAROS_H1A_BLOCK_ROUNDS; + uint32_t p = (block % 20u) * 2u; + if (place == p) + return ARM_P; + uint32_t k = place < p ? place : place - 1u; + static const enum arm turn[3] = {ARM_F, ARM_W, ARM_N}; + return turn[(k + block) % 3u]; +} + +static uint32_t sentinel_word(uint32_t offset) +{ + return STAROS_HW0C_PATTERN(STAROS_HW0C_TAG_SENTINEL, 0u, (offset - STAROS_HW0C_SENTINEL) / 4u); +} + +/* The pool's words: page 2's tail, then page 3's after the far scrub. */ +static void each_pool_word(void (*f)(uint32_t offset)) +{ + for (uint32_t w = 0; w < STAROS_H1A_AREA_LINES * STAROS_H1A_WORDS; w++) + f(STAROS_H1A_AREA_POOL + 4u * w); + for (uint32_t w = 0; w < STAROS_H1A_POLL_LINES * STAROS_H1A_WORDS; w++) + f(STAROS_H1A_POLL_POOL + 4u * w); +} + +static void write_z(uint32_t offset) +{ + write32(AT(offset), STAROS_H1A_Z(offset)); +} + +static void write_m(uint32_t offset) +{ + write32(AT(offset), STAROS_H1A_M(offset)); +} + +static void check_m(uint32_t offset) +{ + if (read32(AT(offset)) != STAROS_H1A_M(offset)) + check_pool++; +} + +/* The pool's span: from the area pool to page 3's end. */ +#define POOLS_BYTES (4u * 0x1000u - STAROS_H1A_AREA_POOL) + +void h1a_clear(void) +{ + static const uint32_t cleared[3][2] = { + {STAROS_H1A_CLEAR0_START, STAROS_H1A_CLEAR0_END}, + {STAROS_H1A_CLEAR1_START, STAROS_H1A_CLEAR1_END}, + {STAROS_H1A_CLEAR2_START, STAROS_H1A_CLEAR2_END}, + }; + for (uint32_t r = 0; r < 3; r++) { + for (uint32_t offset = cleared[r][0]; offset < cleared[r][1]; offset += 4) + write32(AT(offset), 0); + publish(cleared[r][0], cleared[r][1] - cleared[r][0]); + } + each_pool_word(write_z); + publish(STAROS_H1A_AREA_POOL, POOLS_BYTES); +} + +void h1a_ready(uint64_t now) +{ + if (b.started) + return; + each_pool_word(write_m); + publish(STAROS_H1A_AREA_POOL, POOLS_BYTES); + b.started = 1; + b.ready = now; + b.round = 1; + b.acked = now; + b.until = now + (uint64_t)STAROS_H1A_D_POST_TB; + b.delay_min = UINT64_MAX; + b.phase = DELAY; +} + +/* Broadway's post of the current round (h1a::Broadway's Post step). */ +static void post(uint64_t now) +{ + uint32_t round = b.round; + if (!s2b(round)) { + uint32_t slot = (uint32_t)arm(round); + uint32_t n = b.b2s[slot]++; + uint32_t base = areas[n % 4u]; + for (uint32_t w = 0; w < STAROS_H1A_AREA_WORDS; w++) { + uint32_t offset = base + 4u * w; + write32(AT(offset), STAROS_HW0C_PATTERN(STAROS_HW0C_TAG_B2S, round, offset / 4u)); + } + publish(base, AREA_BYTES); + } + const uint32_t line = STAROS_HW0C_BROADWAY; + write32(AT(line + STAROS_HW0C_C_SCENARIO), scenario(round)); + write32(AT(line + STAROS_HW0C_C_STATE), STAROS_HW0C_STATE_ROUND); + publish(line, LINE); + write32(AT(line + STAROS_HW0C_C_ROUND), round); + publish(line, LINE); + uint64_t delay = now - b.acked; + if (delay < b.delay_min) + b.delay_min = delay; + if (delay > b.delay_max) + b.delay_max = delay; + b.posts++; + b.last = round; + b.until = now + (uint64_t)STAROS_H1A_ACK_TIMEOUT_TB; + b.phase = WAIT_ACK; +} + +/* S2B once answered: its stale and other words (h1a::Broadway's WaitAck). */ +static void read_s2b(uint32_t round) +{ + refresh(STAROS_H1A_S2B, AREA_BYTES); + for (uint32_t w = 0; w < STAROS_H1A_AREA_WORDS; w++) { + uint32_t offset = STAROS_H1A_S2B + 4u * w; + uint32_t value = read32(AT(offset)); + if (value == STAROS_HW0C_PATTERN(STAROS_HW0C_TAG_S2B, round, offset / 4u)) + continue; + if ((value >> 28) == STAROS_HW0C_TAG_S2B && (value & 0x3FFFu) == ((offset / 4u) & 0x3FFFu) && + ((value >> 14) & 0x3FFFu) < (round & 0x3FFFu)) + b.s2b_stale++; + else + b.s2b_other++; + } + b.s2b_rounds++; +} + +static void wait_ack(uint64_t now) +{ + const uint32_t line = STAROS_HW0C_STARLET; + refresh(line, LINE); + uint32_t state = read32(AT(line + STAROS_HW0C_C_STATE)); + uint32_t round = b.round; + /* An answer counts with STATE ROUND, or END: the Starlet's END, written + * after its last answer, keeps that answer's ROUND and SCENARIO. */ + int answered = (state == STAROS_HW0C_STATE_ROUND || state == STAROS_HW0C_STATE_END) && + read32(AT(line + STAROS_HW0C_C_ROUND)) == round && + read32(AT(line + STAROS_HW0C_C_SCENARIO)) == scenario(round); + int ended = state == STAROS_HW0C_STATE_END; + if (ended) + b.starlet_end = 1; + if (answered) { + b.acks++; + if (s2b(round)) + read_s2b(round); + b.acked = now; + if (round >= STAROS_H1A_ROUNDS || ended) { + b.phase = ENDING; + } else { + b.round = round + 1u; + b.until = now + (uint64_t)STAROS_H1A_D_POST_TB; + b.phase = DELAY; + } + } else if (ended) { + b.phase = ENDING; + } else if (now >= b.until) { + b.timeout = round; + b.phase = ENDING; + } +} + +static int starlet_ended(void) +{ + refresh(STAROS_HW0C_STARLET, LINE); + return read32(AT(STAROS_HW0C_STARLET + STAROS_HW0C_C_STATE)) == STAROS_HW0C_STATE_END; +} + +uint32_t h1a_sentinel_end(void) +{ + uint32_t changed = 0; + refresh(STAROS_HW0C_SENTINEL, 4u * STAROS_HW0C_SENTINEL_WORDS); + for (uint32_t offset = STAROS_HW0C_SENTINEL; offset < STAROS_HW0C_SIZE; offset += 4) { + int inside = 0; + for (uint32_t a = 0; a < STAROS_H1A_AREA_COUNT; a++) + inside |= offset >= areas[a] && offset < areas[a] + AREA_BYTES; + if (!inside && read32(AT(offset)) != sentinel_word(offset)) + changed++; + } + return changed; +} + +/* The record read until one read qualifies (at most ATTEMPTS), then the end + * checks (h1a::collect). */ +static void collect(void) +{ + attempts = 0; + coherent = 0; + while (attempts < STAROS_H1A_ATTEMPTS && !coherent) { + refresh(STAROS_H1A_RESULTS, RESULTS_BYTES); + gen_before = read32(AT(STAROS_H1A_RESULTS + 4u * STAROS_H1A_F_GEN)); + for (uint32_t w = 0; w < STAROS_H1A_RESULTS_WORDS; w++) + results[w] = read32(AT(STAROS_H1A_RESULTS + 4u * w)); + gen_after = read32(AT(STAROS_H1A_RESULTS + 4u * STAROS_H1A_F_GEN)); + attempts++; + coherent = gen_before == gen_after && gen_before % 2u == 0 && gen_before != 0 && + results[STAROS_H1A_F_GEN] == gen_before && results[STAROS_H1A_F_MAGIC] == STAROS_H1A_MAGIC && + results[STAROS_H1A_F_VERSION] == STAROS_H1A_VERSION && gen_before == STAROS_H1A_GEN; + } + check_sentinel = h1a_sentinel_end(); + check_scrub = 0; + const uint32_t scrubs[3] = {STAROS_H1A_FAR, STAROS_H1A_NEAR_CONTROL, near_scrubs[0]}; + for (uint32_t s = 0; s < 3; s++) { + refresh(scrubs[s], STAROS_H1A_BLOCK); + for (uint32_t w = 0; w < STAROS_H1A_BLOCK / 4u; w++) + if (read32(AT(scrubs[s] + 4u * w)) != 0) + check_scrub++; + } + check_pool = 0; + refresh(STAROS_H1A_AREA_POOL, POOLS_BYTES); + each_pool_word(check_m); + b.collected = 1; +} + +int h1a_step(uint64_t now) +{ + if (!b.started) + return 0; + int late = now - b.ready >= (uint64_t)H1A_END_TIMEOUT_MS * TICKS_PER_MS; + switch (b.phase) { + case IDLE: + return 0; + case DELAY: + if (now >= b.until) + b.phase = POST; + break; + case POST: + /* The Starlet's line once more: an ended Starlet waits for no post. */ + if (starlet_ended()) { + b.starlet_end = 1; + b.phase = ENDING; + } else { + post(now); + } + break; + case WAIT_ACK: + wait_ack(now); + break; + case ENDING: + write32(AT(STAROS_HW0C_BROADWAY + STAROS_HW0C_C_STATE), STAROS_HW0C_STATE_END); + publish(STAROS_HW0C_BROADWAY, LINE); + b.phase = WAIT_END; + break; + case WAIT_END: + if (starlet_ended() || late) { + b.timed_out = !starlet_ended(); + collect(); + b.phase = DONE; + return b.timed_out ? -1 : 1; + } + break; + case DONE: + return b.timed_out ? -1 : 1; + } + /* Past READY + H1A_END_TIMEOUT_MS whatever is under way: Broadway ends. */ + if (late && b.phase != WAIT_END && b.phase != DONE) + b.phase = ENDING; + return 0; +} + +/* The block's CRC-32, word by word, most significant byte first (T4's). */ +static void crc_word(uint32_t word) +{ + for (int shift = 24; shift >= 0; shift -= 8) { + block_crc ^= (word >> shift) & 0xFFu; + for (int bit = 0; bit < 8; ++bit) + block_crc = (block_crc >> 1) ^ ((block_crc & 1u) ? 0xEDB88320u : 0u); + } +} + +/* Every number of the block goes through one of these, in the order printed. */ +static void hex_word(uint32_t value) +{ + crc_word(value); + gecko_hex(value); +} + +static void dec_word(uint32_t value) +{ + crc_word(value); + gecko_dec(value); +} + +static void wide(uint64_t value) +{ + hex_word((uint32_t)(value >> 32)); + hex_word((uint32_t)value); +} + +void h1a_report(void) +{ + /* A run that never reached READY reads the record anyway: what is there. */ + if (!b.collected) + collect(); + block_crc = 0xFFFFFFFFu; + gecko_puts("SBL:H1A B POSTS "); + dec_word(b.posts); + gecko_puts(" LAST "); + dec_word(b.last); + gecko_puts(" ACKS "); + dec_word(b.acks); + gecko_puts(" TIMEOUT "); + dec_word(b.timeout); + gecko_puts(" DELAY "); + wide(b.posts ? b.delay_min : UINT64_MAX); + gecko_puts(" "); + wide(b.delay_max); + gecko_puts(" S2B "); + dec_word(b.s2b_rounds); + gecko_puts(" "); + dec_word(b.s2b_stale); + gecko_puts(" "); + dec_word(b.s2b_other); + gecko_puts(" END "); + dec_word(b.starlet_end); + gecko_puts("\nSBL:H1A B CHECK SENTINEL "); + dec_word(check_sentinel); + gecko_puts(" SCRUB "); + dec_word(check_scrub); + gecko_puts(" POOL "); + dec_word(check_pool); + gecko_puts("\nSBL:H1A S GEN "); + hex_word(gen_before); + gecko_puts(" "); + hex_word(gen_after); + gecko_puts(" ATTEMPTS "); + dec_word(attempts); + crc_word(coherent); + gecko_puts(coherent ? " COHERENT\n" : " INCOMPLETE\n"); + for (uint32_t row = 0; row < STAROS_H1A_RESULTS_WORDS / 8u; row++) { + gecko_puts("SBL:H1A S RESULTS "); + dec_word(row); + for (uint32_t i = 0; i < 8u; i++) { + gecko_puts(" "); + hex_word(results[8u * row + i]); + } + gecko_puts("\n"); + } + gecko_puts("SBL:H1A END CRC "); + gecko_hex(~block_crc); + gecko_puts("\n"); +} diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/h1a.h b/Tools/StarletBootstrapProbe/StarOSLoader/h1a.h new file mode 100644 index 0000000000..8d5caf686b --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/h1a.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * H-1A's bench on Broadway (docs/architecture/h-1a.md, G-H1A-2), built into + * the bench's loader only with -DSTAROS_HW0C -DSTAROS_HW0D -DSTAROS_H1A, + * never with -DSTAROS_HW0E or -DSTAROS_HW0F. crates/hw-bench's h1a::setup, + * mark, Broadway and collect are the reference it follows; the other loaders + * do not contain it. + */ +#ifndef STAROS_LOADER_H1A_H +#define STAROS_LOADER_H1A_H + +#include + +/* In hw0c_setup, before the header: the record, page 0's near scrub and + * pages 1 to 3 cleared, Z in the pool's words, all published. */ +void h1a_clear(void); +/* Once READY is seen, at timebase `now`: M in the pool's words, published; + * round 1 is posted D_POST later. */ +void h1a_ready(uint64_t now); +/* One step at timebase `now`, at most one action: 0 while H-1A runs; 1 once + * the Starlet's END was seen after Broadway's own end; -1 when it was not by + * READY + H1A_END_TIMEOUT_MS. The record and the checks are read back before + * it gives 1 or -1. */ +int h1a_step(uint64_t now); +/* The sentinel's words outside H-1A's areas that are not as Broadway left + * them, read now (hw0c.c's S0 end check, and the CHECK line). */ +uint32_t h1a_sentinel_end(void); +/* After hw0d_report: the SBL:H1A block. */ +void h1a_report(void); + +#endif diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/hw0c.c b/Tools/StarletBootstrapProbe/StarOSLoader/hw0c.c new file mode 100644 index 0000000000..f874d9e940 --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/hw0c.c @@ -0,0 +1,473 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * HW-M4-0C's Broadway side: crates/hw-bench's BroadwaySide in C, over the + * shared window of boot/contract/staros_hw0c.h. Built only with + * -DSTAROS_HW0C; never run on the console without its own reviewed GO. + * + * MSR=0: the window's addresses are physical. Whether Broadway's caches are + * on is not assumed: HID0 and L2CR are recorded in the header. publish is + * dcbf over the lines then sync; refresh is dcbi over the lines then sync, + * used only on lines the Starlet writes, or on the sentinel's lines, which + * were flushed at setup. Every store is an aligned word (write32). + */ +#include + +#include "board.h" +#include "gecko.h" +#include "hw0c.h" +#include "staros_hw0c.h" +#ifdef STAROS_HW0D +#include "hw0d.h" +#include "staros_hw0d.h" +#endif +#ifdef STAROS_HW0E +#include "hw0e.h" +#endif +#ifdef STAROS_HW0F +#include "hw0f.h" +#endif +#ifdef STAROS_H1A +#include "h1a.h" +#endif + +#define HW0C_TIMEOUT_MS 2000u +/* The wait for READY, from the first step after the jump: the Starlet is + * ready only after the layout image's own checks, about 4 s on the console + * (tests/hardware-wii/adr-0003-layout-gates.md). */ +#define HW0C_READY_TIMEOUT_MS 30000u +#define AT(offset) (STAROS_HW0C_BASE + (offset)) + +enum phase { + SETUP, + WAIT_READY, + POST, + WAIT_ACK, + END, + DONE, +#ifdef STAROS_HW0F + T4, /* HW-M4-0F: T4's counter instead of the rounds, from READY to its end */ +#endif +#ifdef STAROS_H1A + H1A, /* H-1A: its own 3000 rounds instead of the bench's, from READY to its end */ +#endif +}; + +struct tally { + uint32_t rounds, bad_rounds, stale, corrupt; +}; + +static const uint32_t run_ids[3] = {STAROS_HW0C_S1_B2S, STAROS_HW0C_S2_S2B, STAROS_HW0C_S3_SEQUENCE}; +static const uint32_t run_rounds[3] = {STAROS_HW0C_S1_ROUNDS, STAROS_HW0C_S2_ROUNDS, STAROS_HW0C_S3_ROUNDS}; + +static struct { + enum phase phase; + uint32_t run, round; + uint64_t deadline; + struct tally tallies[4]; + uint32_t ready, refused, sentinel_ready, sentinel_end; + uint32_t timed_out, timed_out_id, timed_out_round; + uint32_t first, first_id, first_round, first_index, first_value; + uint32_t hid0, l2cr, control, dacr, descriptor; +} bench; + +#ifdef STAROS_HW0F_HOST +/* tests/hardware-wii/hw-m4-0f's host bench only: its model stands for the two + * cache instructions and the two special registers. */ +void dcbf(uint32_t address); +void dcbi(uint32_t address); +uint32_t hid0(void); +uint32_t l2cr(void); +#else +static inline void dcbf(uint32_t address) +{ + __asm__ volatile("dcbf 0,%0" ::"r"(address) : "memory"); +} + +static inline void dcbi(uint32_t address) +{ + __asm__ volatile("dcbi 0,%0" ::"r"(address) : "memory"); +} + +static uint32_t hid0(void) +{ + uint32_t value; + __asm__ volatile("mfspr %0,1008" : "=r"(value)); + return value; +} + +static uint32_t l2cr(void) +{ + uint32_t value; + __asm__ volatile("mfspr %0,1017" : "=r"(value)); + return value; +} +#endif + +static void publish(uint32_t offset, uint32_t bytes) +{ + for (uint32_t at = 0; at < bytes; at += STAROS_HW0C_LINE) + dcbf(AT(offset + at)); + sync(); +} + +static void refresh(uint32_t offset, uint32_t bytes) +{ + for (uint32_t at = 0; at < bytes; at += STAROS_HW0C_LINE) + dcbi(AT(offset + at)); + sync(); +} + +static void fill(uint32_t tag, uint32_t round, uint32_t base, uint32_t words) +{ + for (uint32_t i = 0; i < words; i++) + write32(AT(base + 4u * i), STAROS_HW0C_PATTERN(tag, round, i)); +} + +/* Compares `words` words with tag's round: mismatches, stale ones, the first. */ +static uint32_t check(uint32_t tag, uint32_t round, uint32_t base, uint32_t words, uint32_t *stale, + uint32_t *first_index, uint32_t *first_value) +{ + uint32_t mismatches = 0; + *stale = 0; + for (uint32_t i = 0; i < words; i++) { + uint32_t value = read32(AT(base + 4u * i)); + if (value == STAROS_HW0C_PATTERN(tag, round, i)) + continue; + if (mismatches++ == 0) { + *first_index = i; + *first_value = value; + } + if ((value >> 28) == tag && (value & 0x3FFFu) == (i & 0x3FFFu) && ((value >> 14) & 0x3FFFu) < (round & 0x3FFFu)) + (*stale)++; + } + return mismatches; +} + +static int broadway_writes(uint32_t id, uint32_t round) +{ + return id == STAROS_HW0C_S1_B2S || (id == STAROS_HW0C_S3_SEQUENCE && (round & 1u)); +} + +static void count(uint32_t id, uint32_t round, uint32_t mismatches, uint32_t stale, uint32_t index, uint32_t value) +{ + struct tally *tally = &bench.tallies[id & 3u]; + tally->rounds++; + if (!mismatches) + return; + tally->bad_rounds++; + tally->stale += stale; + tally->corrupt += mismatches - stale; + if (!bench.first) { + bench.first = 1; + bench.first_id = id; + bench.first_round = round; + bench.first_index = index; + bench.first_value = value; + } +} + +void hw0c_setup(void) +{ + bench.hid0 = hid0(); + bench.l2cr = l2cr(); + fill(STAROS_HW0C_TAG_SENTINEL, 0, STAROS_HW0C_SENTINEL, STAROS_HW0C_SENTINEL_WORDS); + publish(STAROS_HW0C_SENTINEL, 4u * STAROS_HW0C_SENTINEL_WORDS); + const uint32_t lines[3] = {STAROS_HW0C_BROADWAY, STAROS_HW0C_STARLET, STAROS_HW0C_ENVIRONMENT}; + for (uint32_t l = 0; l < 3; l++) { + for (uint32_t w = 0; w < STAROS_HW0C_LINE; w += 4) + write32(AT(lines[l] + w), 0); + publish(lines[l], STAROS_HW0C_LINE); + } +#ifdef STAROS_HW0D + hw0d_clear(); /* before the header: the Starlet starts after the jump */ +#endif +#ifdef STAROS_HW0E + hw0e_clear(); /* the probe lines' marker A and a cleared record, before the jump */ +#endif +#ifdef STAROS_HW0F + hw0f_clear(); /* T4's pages and record cleared, Z in its read lines, before the jump */ +#endif +#ifdef STAROS_H1A + h1a_clear(); /* H-1A's pages and record cleared, Z in its pool, before the jump */ +#endif + uint32_t plan = 1u << STAROS_HW0C_S0_SENTINEL; +/* T4 posts no round: its plan is S0 alone (G-T4-2, D5); nor does H-1A here, h1a.c posts its own. */ +#if !defined(STAROS_HW0F) && !defined(STAROS_H1A) + for (uint32_t r = 0; r < 3; r++) + plan |= 1u << run_ids[r]; +#endif + write32(AT(STAROS_HW0C_H_VERSION), STAROS_HW0C_VERSION); + write32(AT(STAROS_HW0C_H_SIZE), STAROS_HW0C_SIZE); + write32(AT(STAROS_HW0C_H_HID0), bench.hid0); + write32(AT(STAROS_HW0C_H_L2CR), bench.l2cr); + write32(AT(STAROS_HW0C_H_PLAN), plan); + write32(AT(STAROS_HW0C_H_ROUNDS), STAROS_HW0C_S1_ROUNDS); + publish(STAROS_HW0C_HEADER, STAROS_HW0C_LINE); + write32(AT(STAROS_HW0C_H_MAGIC), STAROS_HW0C_MAGIC); + publish(STAROS_HW0C_HEADER, STAROS_HW0C_LINE); +#ifdef STAROS_HW0D + hw0d_mark(STAROS_HW0D_E_B_SETUP, 0, 0); +#endif + bench.phase = WAIT_READY; + bench.deadline = 0; /* set by the first step, after the jump */ +} + +static void timeout_at(uint64_t now, uint32_t ms) +{ + bench.deadline = now + (uint64_t)ms * TICKS_PER_MS; +} + +int hw0c_step(uint64_t now) +{ + const uint32_t starlet = STAROS_HW0C_STARLET; + if (bench.phase == WAIT_READY && !bench.deadline) + timeout_at(now, HW0C_READY_TIMEOUT_MS); + switch (bench.phase) { + case SETUP: + case DONE: + return 1; + case WAIT_READY: { + refresh(starlet, STAROS_HW0C_LINE); + uint32_t state = read32(AT(starlet + STAROS_HW0C_C_STATE)); + if (state == STAROS_HW0C_STATE_READY) { + refresh(STAROS_HW0C_ENVIRONMENT, STAROS_HW0C_LINE); + bench.sentinel_ready = read32(AT(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_SENTINEL_BAD)); + bench.control = read32(AT(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_CONTROL)); + bench.dacr = read32(AT(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_DACR)); + bench.descriptor = read32(AT(STAROS_HW0C_ENVIRONMENT + STAROS_HW0C_E_DESCRIPTOR)); + bench.ready = 1; + bench.phase = POST; +#ifdef STAROS_HW0D + hw0d_mark(STAROS_HW0D_E_B_READY, 0, 0); +#endif +#ifdef STAROS_HW0F + bench.phase = T4; + hw0f_ready(now); +#endif +#ifdef STAROS_H1A + bench.phase = H1A; + h1a_ready(now); +#endif + } else if (state == STAROS_HW0C_STATE_REFUSED) { + bench.refused = 1; + bench.phase = END; + } else if (now >= bench.deadline) { + bench.timed_out = 1; + bench.timed_out_id = STAROS_HW0C_S0_SENTINEL; + bench.phase = END; + } + return 0; + } + case POST: { + if (bench.run >= 3) { + bench.phase = END; + return 0; + } + if (bench.round >= run_rounds[bench.run]) { + bench.run++; + bench.round = 0; + return 0; + } + uint32_t id = run_ids[bench.run]; + uint32_t round = ++bench.round; + if (broadway_writes(id, round)) { + fill(STAROS_HW0C_TAG_B2S, round, STAROS_HW0C_B2S, STAROS_HW0C_DATA_WORDS); + publish(STAROS_HW0C_B2S, 4u * STAROS_HW0C_DATA_WORDS); + } + write32(AT(STAROS_HW0C_BROADWAY + STAROS_HW0C_C_SCENARIO), id); + write32(AT(STAROS_HW0C_BROADWAY + STAROS_HW0C_C_STATE), STAROS_HW0C_STATE_ROUND); + publish(STAROS_HW0C_BROADWAY, STAROS_HW0C_LINE); + write32(AT(STAROS_HW0C_BROADWAY + STAROS_HW0C_C_ROUND), round); + publish(STAROS_HW0C_BROADWAY, STAROS_HW0C_LINE); +#ifdef STAROS_HW0D + hw0d_mark(STAROS_HW0D_E_B_POSTED, id, round); +#endif +#ifdef STAROS_HW0E + hw0e_posted(id, round); +#endif + bench.phase = WAIT_ACK; + timeout_at(now, HW0C_TIMEOUT_MS); + return 0; + } + case WAIT_ACK: { + uint32_t id = run_ids[bench.run]; + refresh(starlet, STAROS_HW0C_LINE); + if (read32(AT(starlet + STAROS_HW0C_C_STATE)) == STAROS_HW0C_STATE_REFUSED) { +#ifdef STAROS_HW0D + hw0d_mark(STAROS_HW0D_E_B_TIMEOUT, + STAROS_HW0D_LINE(STAROS_HW0C_STATE_REFUSED, read32(AT(starlet + STAROS_HW0C_C_SCENARIO))), + read32(AT(starlet + STAROS_HW0C_C_ROUND))); +#endif + bench.refused = 1; + bench.phase = END; + return 0; + } + if (read32(AT(starlet + STAROS_HW0C_C_ROUND)) != bench.round || + read32(AT(starlet + STAROS_HW0C_C_SCENARIO)) != id) { + if (now >= bench.deadline) { + bench.timed_out = 1; + bench.timed_out_id = id; + bench.timed_out_round = bench.round; +#ifdef STAROS_HW0D + hw0d_mark(STAROS_HW0D_E_B_TIMEOUT, + STAROS_HW0D_LINE(read32(AT(starlet + STAROS_HW0C_C_STATE)), + read32(AT(starlet + STAROS_HW0C_C_SCENARIO))), + read32(AT(starlet + STAROS_HW0C_C_ROUND))); +#endif + bench.phase = END; + } + return 0; + } +#ifdef STAROS_HW0D + hw0d_mark(STAROS_HW0D_E_B_ANSWERED, id, bench.round); +#endif + uint32_t mismatches, stale = 0, index = 0, value = 0; + if (broadway_writes(id, bench.round)) { + mismatches = read32(AT(starlet + STAROS_HW0C_C_MISMATCHES)); + stale = read32(AT(starlet + STAROS_HW0C_C_STALE)); + index = read32(AT(starlet + STAROS_HW0C_C_FIRST_INDEX)); + value = read32(AT(starlet + STAROS_HW0C_C_FIRST_VALUE)); + } else { + refresh(STAROS_HW0C_S2B, 4u * STAROS_HW0C_DATA_WORDS); + mismatches = check(STAROS_HW0C_TAG_S2B, bench.round, STAROS_HW0C_S2B, STAROS_HW0C_DATA_WORDS, &stale, + &index, &value); + } + count(id, bench.round, mismatches, stale, index, value); + bench.phase = POST; + return 0; + } +#ifdef STAROS_HW0F + case T4: { + /* Until the Starlet's END, or READY + 45 s: then the record is read. A + * missing END is a timeout, so S0 is INCONCLUSIVE (G-T4-2, D7). */ + int done = hw0f_step(now); + if (done < 0) { + bench.timed_out = 1; + bench.timed_out_id = STAROS_HW0C_S0_SENTINEL; + } + if (done) + bench.phase = END; + return 0; + } +#endif +#ifdef STAROS_H1A + case H1A: { + /* Until h1a.c has read the record back: after the Starlet's END, or READY + * + 65 s without one, which is a timeout, so S0 is INCONCLUSIVE. */ + int done = h1a_step(now); + if (done < 0) { + bench.timed_out = 1; + bench.timed_out_id = STAROS_HW0C_S0_SENTINEL; + } + if (done) + bench.phase = END; + return 0; + } +#endif + case END: { + uint32_t stale, index, value; + write32(AT(STAROS_HW0C_BROADWAY + STAROS_HW0C_C_STATE), STAROS_HW0C_STATE_END); + publish(STAROS_HW0C_BROADWAY, STAROS_HW0C_LINE); +#ifdef STAROS_HW0D + hw0d_mark(STAROS_HW0D_E_B_END, 0, 0); +#endif +#ifdef STAROS_H1A + /* H-1A writes its areas A1 to A3 inside the sentinel after READY: the rest + * is checked, as h1a.c's own CHECK line counts it. */ + (void)stale; + (void)index; + (void)value; + bench.sentinel_end = h1a_sentinel_end(); +#else + refresh(STAROS_HW0C_SENTINEL, 4u * STAROS_HW0C_SENTINEL_WORDS); + bench.sentinel_end = check(STAROS_HW0C_TAG_SENTINEL, 0, STAROS_HW0C_SENTINEL, STAROS_HW0C_SENTINEL_WORDS, + &stale, &index, &value); +#endif + bench.phase = DONE; + return 1; + } + } + return 1; +} + +static void line_hex(const char *label, uint32_t value) +{ + gecko_puts(label); + gecko_hex(value); +} + +static void line_dec(const char *label, uint32_t value) +{ + gecko_puts(label); + gecko_dec(value); +} + +/* One scenario's verdict, as hw_bench::Outcome::verdict gives it. */ +static void verdict(uint32_t id) +{ + struct tally *tally = &bench.tallies[id & 3u]; + line_dec("SBL:HW0C S", id); + if (id == STAROS_HW0C_S0_SENTINEL) { + uint32_t bad = bench.sentinel_ready + bench.sentinel_end; + if (!bench.ready) + gecko_puts(" INCONCLUSIVE"); + else if (bad) + line_dec(" FAIL CORRUPT ", bad); + else if (bench.timed_out || bench.refused) + gecko_puts(" INCONCLUSIVE"); + else + gecko_puts(" PASS"); + } else if (tally->bad_rounds) { + line_dec(" FAIL ROUNDS ", tally->bad_rounds); + line_dec(" STALE ", tally->stale); + line_dec(" CORRUPT ", tally->corrupt); + } else if (tally->rounds < run_rounds[id - 1u] || bench.refused) { + line_dec(" INCONCLUSIVE ROUNDS ", tally->rounds); + } else { + line_dec(" PASS ROUNDS ", tally->rounds); + } + gecko_puts("\n"); +} + +void hw0c_report(void) +{ + line_hex("SBL:HW0C HID0 ", bench.hid0); + line_hex(" L2CR ", bench.l2cr); + gecko_puts("\n"); + line_hex("SBL:HW0C ENV CTRL ", bench.control); + line_hex(" DACR ", bench.dacr); + line_hex(" PAGE ", bench.descriptor); + line_dec(" SENTINEL READY ", bench.sentinel_ready); + line_dec(" END ", bench.sentinel_end); + gecko_puts("\n"); + for (uint32_t id = 0; id < 4u; id++) + verdict(id); + gecko_puts("SBL:HW0C S4 GATED IPC\nSBL:HW0C S5 GATED AHB\n"); + if (bench.first) { + line_dec("SBL:HW0C FIRST S", bench.first_id); + line_dec(" ROUND ", bench.first_round); + line_dec(" INDEX ", bench.first_index); + line_hex(" VALUE ", bench.first_value); + gecko_puts("\n"); + } + if (bench.timed_out) { + line_dec("SBL:HW0C TIMEOUT S", bench.timed_out_id); + line_dec(" ROUND ", bench.timed_out_round); + gecko_puts("\n"); + } + if (bench.refused) + gecko_puts("SBL:HW0C REFUSED\n"); + gecko_puts("SBL:HW0C END\n"); +#ifdef STAROS_HW0D + hw0d_report(); +#endif +#ifdef STAROS_HW0E + hw0e_report(); +#endif +#ifdef STAROS_HW0F + hw0f_report(); +#endif +#ifdef STAROS_H1A + h1a_report(); +#endif +} diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/hw0c.h b/Tools/StarletBootstrapProbe/StarOSLoader/hw0c.h new file mode 100644 index 0000000000..2afa1b9c29 --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/hw0c.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * HW-M4-0C's Broadway side (tests/hardware-wii/hw-m4-0c.md), built into the + * loader only with -DSTAROS_HW0C. crates/hw-bench's BroadwaySide is the + * reference it follows; the default loader does not contain it. + */ +#ifndef STAROS_LOADER_HW0C_H +#define STAROS_LOADER_HW0C_H + +#include + +/* Before the MINI jump: fill the sentinel, write the header (magic last). */ +void hw0c_setup(void); +/* One step of the bench at timebase `now`: 1 once the bench is over. */ +int hw0c_step(uint64_t now); +/* After EXI is back: the SBL:HW0C lines. */ +void hw0c_report(void); + +#endif diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/hw0d.c b/Tools/StarletBootstrapProbe/StarOSLoader/hw0d.c new file mode 100644 index 0000000000..ab4c65a124 --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/hw0d.c @@ -0,0 +1,213 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * HW-M4-0D's Broadway side: crates/hw-bench's diag::Probe in C, plus the + * report of the Starlet's record. Built only with -DSTAROS_HW0C + * -DSTAROS_HW0D; never run on the console without its own reviewed GO. + * + * Broadway's events stay in its own memory and are stamped with its 64-bit + * timebase (243 MHz / 4). Broadway does not read HW_TIMER: whether the boot + * path's AHB protection lets the PPC reach that register is not known + * (docs/contracts/ppc-window.md, E20). The Starlet stamps its own events with + * HW_TIMER (243 MHz / 128); READY, which both sides stamp, ties the two + * clocks together at 32 timebase ticks to one HW_TIMER tick. + * + * The Starlet's record is read at the end, from the window's diagnostic + * area, whatever the Starlet's state, as diag::Snapshot::read does: the + * generation, every word, the generation again, at most + * STAROS_HW0D_ATTEMPTS times, never an unbounded wait. A read counts only + * between two equal even generations; otherwise the record is reported + * INCOMPLETE, with the words the last attempt read. Every store is an + * aligned word (write32, or a uint32_t of this file's own memory). + */ +#include +#include "board.h" +#include "gecko.h" +#include "hw0d.h" +#include "staros_hw0c.h" +#include "staros_hw0d.h" + +#define AT(offset) (STAROS_HW0C_BASE + (offset)) + +/* Broadway's first events and its latest, as diag::Log keeps them. */ +static uint32_t kinds[STAROS_HW0D_SLOTS], as[STAROS_HW0D_SLOTS], bs[STAROS_HW0D_SLOTS]; +static uint32_t highs[STAROS_HW0D_SLOTS], lows[STAROS_HW0D_SLOTS]; +static uint32_t count, last_kind, last_a, last_b, last_high, last_low; +/* The Starlet's area as the last read took it, STAROS_HW0D_F_MAGIC first. */ +static uint32_t words[STAROS_HW0D_RAW_WORDS]; + +#ifdef STAROS_HW0D_HOST +/* A host's build only (the Dolphin probe's Broadway client, H-1A's D-H1A-7): + * its model of the window stands for the two cache instructions. */ +void dcbf(uint32_t address); +void dcbi(uint32_t address); +#else +static inline void dcbf(uint32_t address) +{ + __asm__ volatile("dcbf 0,%0" ::"r"(address) : "memory"); +} + +static inline void dcbi(uint32_t address) +{ + __asm__ volatile("dcbi 0,%0" ::"r"(address) : "memory"); +} +#endif + +void hw0d_clear(void) +{ + for (uint32_t at = 0; at < STAROS_HW0D_SIZE; at += 4) + write32(AT(STAROS_HW0D_BASE + at), 0); + for (uint32_t at = 0; at < STAROS_HW0D_SIZE; at += STAROS_HW0C_LINE) + dcbf(AT(STAROS_HW0D_BASE + at)); + sync(); +} + +void hw0d_mark(uint32_t kind, uint32_t a, uint32_t b) +{ + uint64_t now = timebase(); + uint32_t high = (uint32_t)(now >> 32), low = (uint32_t)now; + if (count < STAROS_HW0D_SLOTS) { + kinds[count] = kind; + as[count] = a; + bs[count] = b; + highs[count] = high; + lows[count] = low; + } + last_kind = kind; + last_a = a; + last_b = b; + last_high = high; + last_low = low; + count++; +} + +static uint32_t area(uint32_t offset) +{ + return read32(AT(STAROS_HW0D_BASE + offset)); +} + +static void broadway_event(uint32_t kind, uint32_t a, uint32_t b, uint32_t high, uint32_t low) +{ + gecko_hex(kind); + gecko_puts(" A "); + gecko_hex(a); + gecko_puts(" B "); + gecko_hex(b); + gecko_puts(" TB "); + gecko_hex(high); + gecko_hex(low); + gecko_puts("\n"); +} + +/* The word at `offset` of the area, as the last read took it. */ +static uint32_t taken(uint32_t offset) +{ + return words[(offset / 4u) % STAROS_HW0D_RAW_WORDS]; +} + +static void starlet_event(uint32_t at) +{ + gecko_hex(taken(at)); + gecko_puts(" A "); + gecko_hex(taken(at + 4u)); + gecko_puts(" B "); + gecko_hex(taken(at + 8u)); + gecko_puts(" T "); + gecko_hex(taken(at + 12u)); + gecko_puts("\n"); +} + +/* Reads the area into `words`, at most STAROS_HW0D_ATTEMPTS times: 1 once + * both generations are equal and even. `first`, `last`: the last attempt's. */ +static int read_area(uint32_t *first, uint32_t *last, uint32_t *attempts) +{ + for (uint32_t attempt = 1; attempt <= STAROS_HW0D_ATTEMPTS; attempt++) { + for (uint32_t at = 0; at < STAROS_HW0D_SIZE; at += STAROS_HW0C_LINE) + dcbi(AT(STAROS_HW0D_BASE + at)); + sync(); + /* A sync on each side of the words: the generations frame them in the + * bus's order, not only in the program's. */ + *first = area(STAROS_HW0D_F_GEN); + sync(); + for (uint32_t i = 0; i < STAROS_HW0D_RAW_WORDS; i++) + words[i] = area(4u * i); + sync(); + *last = area(STAROS_HW0D_F_GEN); + *attempts = attempt; + if (*first == *last && (*first & 1u) == 0) + return 1; + } + return 0; +} + +void hw0d_report(void) +{ + uint32_t kept = count < STAROS_HW0D_SLOTS ? count : STAROS_HW0D_SLOTS; + gecko_puts("SBL:HW0D B EVENTS "); + gecko_dec(count); + gecko_puts("\n"); + for (uint32_t i = 0; i < kept; i++) { + gecko_puts("SBL:HW0D B "); + gecko_dec(i); + gecko_puts(" "); + broadway_event(kinds[i], as[i], bs[i], highs[i], lows[i]); + } + gecko_puts("SBL:HW0D B LAST "); + broadway_event(last_kind, last_a, last_b, last_high, last_low); + + /* The Starlet's record: one coherent read, or INCOMPLETE and the words + * the last attempt took (they are no record then). */ + uint32_t first = 0, last = 0, attempts = 0; + int coherent = read_area(&first, &last, &attempts); + gecko_puts("SBL:HW0D S GEN "); + gecko_hex(first); + gecko_puts(" "); + gecko_hex(last); + gecko_puts(" ATTEMPTS "); + gecko_dec(attempts); + gecko_puts(coherent ? " COHERENT\n" : " INCOMPLETE\n"); + uint32_t magic = taken(STAROS_HW0D_F_MAGIC), version = taken(STAROS_HW0D_F_VERSION); + if (magic != STAROS_HW0D_MAGIC || version != STAROS_HW0D_VERSION) { + gecko_puts("SBL:HW0D S NONE MAGIC "); + gecko_hex(magic); + gecko_puts(" VERSION "); + gecko_hex(version); + gecko_puts("\n"); + } else { + uint32_t events = taken(STAROS_HW0D_F_EVENTS); + uint32_t stored = events < STAROS_HW0D_SLOTS ? events : STAROS_HW0D_SLOTS; + gecko_puts("SBL:HW0D S START "); + gecko_hex(taken(STAROS_HW0D_F_START)); + gecko_puts(" POLLS "); + gecko_dec(taken(STAROS_HW0D_F_POLLS)); + gecko_puts(" NOW "); + gecko_hex(taken(STAROS_HW0D_F_NOW)); + gecko_puts(" LINE "); + gecko_hex(taken(STAROS_HW0D_F_STATE)); + gecko_puts(" "); + gecko_hex(taken(STAROS_HW0D_F_SCENARIO)); + gecko_puts(" "); + gecko_hex(taken(STAROS_HW0D_F_ROUND)); + gecko_puts(" EVENTS "); + gecko_dec(events); + gecko_puts("\n"); + for (uint32_t i = 0; i < stored; i++) { + gecko_puts("SBL:HW0D S "); + gecko_dec(i); + gecko_puts(" "); + starlet_event(STAROS_HW0D_F_LOG + i * STAROS_HW0D_EVENT_BYTES); + } + gecko_puts("SBL:HW0D S LAST "); + starlet_event(STAROS_HW0D_F_LAST); + } + + /* The Starlet's line, read again now: an answer seen late shows here. */ + dcbi(AT(STAROS_HW0C_STARLET)); + sync(); + gecko_puts("SBL:HW0D B LATE STATE "); + gecko_hex(read32(AT(STAROS_HW0C_STARLET + STAROS_HW0C_C_STATE))); + gecko_puts(" SCENARIO "); + gecko_hex(read32(AT(STAROS_HW0C_STARLET + STAROS_HW0C_C_SCENARIO))); + gecko_puts(" ROUND "); + gecko_hex(read32(AT(STAROS_HW0C_STARLET + STAROS_HW0C_C_ROUND))); + gecko_puts("\nSBL:HW0D END\n"); +} diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/hw0d.h b/Tools/StarletBootstrapProbe/StarOSLoader/hw0d.h new file mode 100644 index 0000000000..0369578f6d --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/hw0d.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * HW-M4-0D's diagnostic on Broadway (tests/hardware-wii/hw-m4-0d.md), built + * into the bench's loader only with -DSTAROS_HW0C -DSTAROS_HW0D. + * crates/hw-bench's diag::Probe is the reference its events follow; the + * HW-M4-0C loader and the default loader do not contain it. + */ +#ifndef STAROS_LOADER_HW0D_H +#define STAROS_LOADER_HW0D_H + +#include + +/* In hw0c_setup, before the header: clear the diagnostic area. */ +void hw0d_clear(void); +/* One of Broadway's events (STAROS_HW0D_E_B_*), stamped with its timebase. */ +void hw0d_mark(uint32_t kind, uint32_t a, uint32_t b); +/* At the end of hw0c_report: the SBL:HW0D lines. */ +void hw0d_report(void); + +#endif diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/staros_h1a.h b/Tools/StarletBootstrapProbe/StarOSLoader/staros_h1a.h new file mode 100644 index 0000000000..efe60ef008 --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/staros_h1a.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * H-1A's bench over the HW-M4-0C window (crates/hw-bench, module h1a; + * docs/architecture/h-1a.md): offsets from STAROS_HW0C_BASE. Broadway + * (boot/wii/loader/h1a.c) clears the pages and the pool, marks the pool, + * posts 3000 rounds and reads the record back; the Starlet + * (hw_bench::h1a::Engine) polls, reads the areas under R-1A, answers and + * writes the record once. Every value is checked against the crate by + * tests/contract/h1a_header.rs, the tables entry for entry. + */ +#ifndef STAROS_H1A_H +#define STAROS_H1A_H + +/* The four Broadway -> Starlet areas, 512 words each, in pages 1, 4, 10 and + * 15, and each area page's near scrub (arm N), just after its area. */ +#define STAROS_H1A_AREA_COUNT 0x00000004u +#define STAROS_H1A_AREAS {0x00001000u, 0x00004000u, 0x0000A000u, 0x0000F000u} +#define STAROS_H1A_NEAR {0x00001800u, 0x00004800u, 0x0000A800u, 0x0000F800u} +#define STAROS_H1A_AREA_WORDS 0x00000200u +#define STAROS_H1A_NEAR_CONTROL 0x00000F80u +#define STAROS_H1A_FAR 0x00003000u +/* The Starlet -> Broadway area, page 2. */ +#define STAROS_H1A_S2B 0x00002000u +/* Arm P's pool: page 2's tail, then page 3's after the far scrub. */ +#define STAROS_H1A_AREA_POOL 0x00002800u +#define STAROS_H1A_AREA_LINES 0x00000040u +#define STAROS_H1A_POLL_POOL 0x00003080u +#define STAROS_H1A_POLL_LINES 0x0000007Cu +#define STAROS_H1A_BLOCK 0x00000080u +#define STAROS_H1A_WORDS 0x00000008u + +/* The record: RESULTS, written once by the Starlet at its end (GEN 1, + * published; the words, published; GEN 2, published). Broadway reads GEN, + * RESULTS, GEN, at most ATTEMPTS times; a read counts only when GEN before, + * RESULTS[F_GEN] and GEN after are equal, even and not 0 (review F6), and the + * magic and version are the record's (hw_bench::h1a::qualify). */ +#define STAROS_H1A_RESULTS 0x00000100u +#define STAROS_H1A_RESULTS_WORDS 0x000001C0u +#define STAROS_H1A_MAGIC 0x48314131u +#define STAROS_H1A_VERSION 0x00000001u +#define STAROS_H1A_GEN 0x00000002u +#define STAROS_H1A_ATTEMPTS 0x00000003u +#define STAROS_H1A_F_MAGIC 0x00000000u +#define STAROS_H1A_F_VERSION 0x00000001u +#define STAROS_H1A_F_GEN 0x00000002u + +/* What Broadway's setup clears before the jump, [start, end): the record, + * page 0's near scrub and pages 1 to 3. */ +#define STAROS_H1A_CLEAR0_START 0x00000100u +#define STAROS_H1A_CLEAR0_END 0x00000800u +#define STAROS_H1A_CLEAR1_START 0x00000F80u +#define STAROS_H1A_CLEAR1_END 0x00001000u +#define STAROS_H1A_CLEAR2_START 0x00001000u +#define STAROS_H1A_CLEAR2_END 0x00004000u + +/* The pool's words: Z before the jump, M once READY is seen (T4's). */ +#define STAROS_H1A_Z(offset) (0x5A000000u | (offset)) +#define STAROS_H1A_M(offset) (0xA5000000u | (offset)) + +/* The rounds: S1's 1000 (Broadway -> Starlet), then S3's 2000 (odd ones + * Broadway -> Starlet, even ones Starlet -> Broadway); HW-M4-0C's scenario + * ids in the control lines. A block of 40 rounds: one P, then F, W and N. */ +#define STAROS_H1A_ROUNDS 0x00000BB8u +#define STAROS_H1A_S1_ROUNDS 0x000003E8u +#define STAROS_H1A_S1 0x00000001u +#define STAROS_H1A_S3 0x00000003u +#define STAROS_H1A_BLOCK_ROUNDS 0x00000028u + +/* Broadway's waits, in timebase ticks: from an answer seen to its next post + * (D_POST, 2 ms), and for an answer (200 ms). */ +#define STAROS_H1A_D_POST_TB 0x0001DA9Cu +#define STAROS_H1A_ACK_TIMEOUT_TB 0x00B964F0u +/* The engine's guard, in HW_TIMER ticks from READY (60 s). */ +#define STAROS_H1A_GUARD 0x06CA124Au + +#endif diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/staros_hw0c.h b/Tools/StarletBootstrapProbe/StarOSLoader/staros_hw0c.h new file mode 100644 index 0000000000..d1b3ea8ad1 --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/staros_hw0c.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * HW-M4-0C's visibility bench: the loader's view of crates/hw-bench (the + * source of truth), checked value for value by tests/contract/hw0c_header.rs. + * The window is MEM2 0x13500000-0x1350FFFF (dev-mini only); every field is a + * big-endian 32-bit word, every access a whole aligned word. + */ +#ifndef STAROS_HW0C_H +#define STAROS_HW0C_H + +#define STAROS_HW0C_BASE 0x13500000u +#define STAROS_HW0C_SIZE 0x00010000u +#define STAROS_HW0C_LINE 0x00000020u +#define STAROS_HW0C_MAGIC 0x48573043u +#define STAROS_HW0C_VERSION 0x00000001u + +/* The header (Broadway's, magic last). */ +#define STAROS_HW0C_HEADER 0x00000000u +#define STAROS_HW0C_H_MAGIC 0x00000000u +#define STAROS_HW0C_H_VERSION 0x00000004u +#define STAROS_HW0C_H_SIZE 0x00000008u +#define STAROS_HW0C_H_HID0 0x0000000Cu +#define STAROS_HW0C_H_L2CR 0x00000010u +#define STAROS_HW0C_H_PLAN 0x00000014u +#define STAROS_HW0C_H_ROUNDS 0x00000018u + +/* The control lines and their fields. */ +#define STAROS_HW0C_BROADWAY 0x00000020u +#define STAROS_HW0C_STARLET 0x00000040u +#define STAROS_HW0C_C_SCENARIO 0x00000000u +#define STAROS_HW0C_C_ROUND 0x00000004u +#define STAROS_HW0C_C_STATE 0x00000008u +#define STAROS_HW0C_C_MISMATCHES 0x0000000Cu +#define STAROS_HW0C_C_STALE 0x00000010u +#define STAROS_HW0C_C_FIRST_INDEX 0x00000014u +#define STAROS_HW0C_C_FIRST_VALUE 0x00000018u + +/* The Starlet's environment line. */ +#define STAROS_HW0C_ENVIRONMENT 0x00000060u +#define STAROS_HW0C_E_CONTROL 0x00000000u +#define STAROS_HW0C_E_DACR 0x00000004u +#define STAROS_HW0C_E_DESCRIPTOR 0x00000008u +#define STAROS_HW0C_E_SENTINEL_BAD 0x0000000Cu + +/* States. */ +#define STAROS_HW0C_STATE_NONE 0x00000000u +#define STAROS_HW0C_STATE_READY 0x00000001u +#define STAROS_HW0C_STATE_ROUND 0x00000002u +#define STAROS_HW0C_STATE_END 0x00000003u +#define STAROS_HW0C_STATE_REFUSED 0x00000004u + +/* The data areas and the sentinel. */ +#define STAROS_HW0C_B2S 0x00001000u +#define STAROS_HW0C_S2B 0x00002000u +#define STAROS_HW0C_DATA_WORDS 0x00000400u +#define STAROS_HW0C_SENTINEL 0x00004000u +#define STAROS_HW0C_SENTINEL_WORDS 0x00003000u + +/* Tags and scenarios (S4 and S5 are gated: the loader never posts them). */ +#define STAROS_HW0C_TAG_B2S 0x00000001u +#define STAROS_HW0C_TAG_S2B 0x00000002u +#define STAROS_HW0C_TAG_SENTINEL 0x00000003u +#define STAROS_HW0C_S0_SENTINEL 0x00000000u +#define STAROS_HW0C_S1_B2S 0x00000001u +#define STAROS_HW0C_S2_S2B 0x00000002u +#define STAROS_HW0C_S3_SEQUENCE 0x00000003u +#define STAROS_HW0C_S1_ROUNDS 0x000003E8u +#define STAROS_HW0C_S2_ROUNDS 0x000003E8u +#define STAROS_HW0C_S3_ROUNDS 0x000007D0u + +/* The word for index i of an area in a round: hw_bench::pattern. */ +#define STAROS_HW0C_PATTERN(tag, round, index) ((((tag) & 0xFu) << 28) | (((round) & 0x3FFFu) << 14) | ((index) & 0x3FFFu)) + +#endif diff --git a/Tools/StarletBootstrapProbe/StarOSLoader/staros_hw0d.h b/Tools/StarletBootstrapProbe/StarOSLoader/staros_hw0d.h new file mode 100644 index 0000000000..6e90b3585a --- /dev/null +++ b/Tools/StarletBootstrapProbe/StarOSLoader/staros_hw0d.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * HW-M4-0D's diagnostic area of the HW-M4-0C window (crates/hw-bench, module + * diag; tests/hardware-wii/hw-m4-0d.md): offsets from STAROS_HW0C_BASE. Only + * the Starlet writes it, once Broadway's setup has cleared it. Every value is + * checked against the crate by tests/contract/hw0d_header.rs. + */ +#ifndef STAROS_HW0D_H +#define STAROS_HW0D_H + +/* The area: in the window's gap between the environment line and B2S. */ +#define STAROS_HW0D_BASE 0x00000800u +#define STAROS_HW0D_SIZE 0x00000200u +#define STAROS_HW0D_MAGIC 0x48573044u +#define STAROS_HW0D_VERSION 0x00000001u +#define STAROS_HW0D_HEARTBEAT 0x00010000u +#define STAROS_HW0D_SLOTS 0x00000010u +#define STAROS_HW0D_EVENT_BYTES 0x00000010u +/* A read takes RAW_WORDS words from F_MAGIC; Broadway tries ATTEMPTS times. */ +#define STAROS_HW0D_RAW_WORDS 0x00000054u +#define STAROS_HW0D_ATTEMPTS 0x00000003u + +/* Its words. An event is four words: kind, a, b, the Starlet's HW_TIMER. */ +#define STAROS_HW0D_F_MAGIC 0x00000000u +#define STAROS_HW0D_F_VERSION 0x00000004u +#define STAROS_HW0D_F_START 0x00000008u +#define STAROS_HW0D_F_POLLS 0x0000000Cu +#define STAROS_HW0D_F_NOW 0x00000010u +#define STAROS_HW0D_F_STATE 0x00000014u +#define STAROS_HW0D_F_SCENARIO 0x00000018u +#define STAROS_HW0D_F_ROUND 0x0000001Cu +#define STAROS_HW0D_F_EVENTS 0x00000020u +/* The generation: odd while the Starlet updates the area, even once it is + * published whole. A read counts only between two equal even generations. */ +#define STAROS_HW0D_F_GEN 0x00000024u +#define STAROS_HW0D_F_LOG 0x00000040u +#define STAROS_HW0D_F_LAST 0x00000140u + +/* The Starlet's events. */ +#define STAROS_HW0D_E_START 0x00000001u +#define STAROS_HW0D_E_LINE 0x00000002u +#define STAROS_HW0D_E_IGNORED 0x00000003u +#define STAROS_HW0D_E_B2S_BEGIN 0x00000004u +#define STAROS_HW0D_E_B2S_END 0x00000005u +#define STAROS_HW0D_E_S2B_BEGIN 0x00000006u +#define STAROS_HW0D_E_S2B_END 0x00000007u +#define STAROS_HW0D_E_ANSWER 0x00000008u +#define STAROS_HW0D_E_END 0x00000009u +#define STAROS_HW0D_E_FINISH 0x0000000Au +/* Broadway's, kept in its own memory (boot/wii/loader/hw0d.c). */ +#define STAROS_HW0D_E_B_SETUP 0x00000011u +#define STAROS_HW0D_E_B_READY 0x00000012u +#define STAROS_HW0D_E_B_POSTED 0x00000013u +#define STAROS_HW0D_E_B_ANSWERED 0x00000014u +#define STAROS_HW0D_E_B_TIMEOUT 0x00000015u +#define STAROS_HW0D_E_B_END 0x00000016u + +/* A line's state and scenario in one word, as hw_bench::diag::line. */ +#define STAROS_HW0D_LINE(state, scenario) ((((state) & 0xFFFFu) << 16) | ((scenario) & 0xFFFFu)) + +#endif diff --git a/Tools/StarletBootstrapProbe/StarletBootstrapProbe.cpp b/Tools/StarletBootstrapProbe/StarletBootstrapProbe.cpp index c8d12007f9..2cb42cf6d3 100644 --- a/Tools/StarletBootstrapProbe/StarletBootstrapProbe.cpp +++ b/Tools/StarletBootstrapProbe/StarletBootstrapProbe.cpp @@ -18,6 +18,7 @@ #include "Core/IOS/Starlet/BootstrapELF.h" #include "Core/IOS/Starlet/StarletMemory.h" #include "Core/System.h" +#include "ProbeBroadway.h" #include "ProbeBudget.h" #include "ProbeRing.h" #include "ProbeScript.h" @@ -131,8 +132,23 @@ private: IOS::WiiIPC& m_ipc; }; +// What the Broadway client reaches: PPC memory, by whole words (ProbeBroadway.h bounds it to the +// bench window). +class ProbeMemory final : public BroadwayMemory +{ +public: + explicit ProbeMemory(Memory::MemoryManager& memory) : m_memory(memory) {} + + u32 LoadWord(u32 address) override { return m_memory.Read_U32(address); } + void StoreWord(u32 address, u32 word) override { m_memory.Write_U32(word, address); } + +private: + Memory::MemoryManager& m_memory; +}; + bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1, - ExpectedOutcome expected, u64 cycles, const std::vector* package, ScriptRunner* script) + ExpectedOutcome expected, u64 cycles, const std::vector* package, ScriptRunner* script, + BroadwayClient* client) { std::ifstream file(path, std::ios::binary | std::ios::ate); if (!file || file.tellg() <= 0 || file.tellg() > 16 * 1024 * 1024) @@ -178,6 +194,19 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1, << SPK_WINDOW_BASE << std::dec << '\n'; } + // --broadway-h1a: StarOS's loader sets the bench window up before the jump; so does the client, + // before the Starlet's first instruction. + ProbeMemory client_memory(system.GetMemory()); + if (client) + { + std::cerr << "--broadway-h1a: StarOS loader sources built in:\n" << BroadwayClient::Sources(); + if (!client->Start(client_memory) || client->Failed()) + { + std::cerr << "--broadway-h1a: " << client->Failure() << '\n'; + return false; + } + } + IOS::LLE::ARMCore core(memory); // Declared after memory, so destroyed first. memory.AttachInterruptLines(&core); @@ -207,6 +236,12 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1, std::cerr << "Starlet panic state " << memory.Read32(info.panic_state) << " code " << memory.Read32(info.panic_code) << '\n'; }; + // The client's capture on stdout, after the trace, and its counts on stderr. + const auto report_client = [&](std::string_view status) { + std::cout << client->Report(status); + std::cerr << "--broadway-h1a: " << client->Steps() << " steps, timebase 0x" << std::hex + << client->Timebase() << std::dec << ", done " << client->Done() << '\n'; + }; ProbeBus script_bus(mem1, system.GetWiiIPC()); // The model's own line raises the IPC cause: the probe has no Starlet object (ProbeScript.h). if (script) @@ -223,6 +258,16 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1, return false; } } + if (client) + { + // As the loader's monitor: one step of the bench a turn, the first at the jump. + client->Advance(slice == 0 ? 0 : SLICE_CYCLES); + if (client->Failed()) + { + std::cerr << "--broadway-h1a: " << client->Failure() << '\n'; + return false; + } + } core.SetIRQLine(system.GetWiiIPC().IsStarletIRQAsserted()); core.SetFIQLine(system.GetWiiIPC().IsStarletFIQAsserted()); core.RunCycles(SLICE_CYCLES); @@ -272,6 +317,9 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1, { continue; } + // The loader reports once the Starlet is done and its bench has ended (main.c's monitor). + if (client && !client->Done()) + continue; std::cout << trace; std::cout << "PC=0x" << std::hex << core.GetRegister(15) << std::dec << " instructions=" << core.GetExecutedInstructions() << '\n'; @@ -280,6 +328,8 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1, std::cerr << "--ipc-script: script unfinished at line " << script->Line() << '\n'; return false; } + if (client) + report_client(CLIENT_STATUS_DONE); const bool early_startup_ok = trace.find("REV:BOOT:ENTRY\n") != std::string::npos && trace.find("REV:BOOT:BSS_OK\n") != std::string::npos; if (expected == ExpectedOutcome::FatalData) @@ -320,6 +370,9 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1, } std::cerr << "No terminal bootstrap marker after " << cycles << " ARM cycles, PC=0x" << std::hex << core.GetRegister(15) << std::dec << '\n'; + // As main.c's monitor at its own timeout: the report anyway, under SBL:TIMEOUT. + if (client) + report_client(CLIENT_STATUS_TIMEOUT); return false; } } // namespace @@ -332,7 +385,7 @@ int main(int argc, char** argv) std::cerr << "Usage: StarletBootstrapProbe [--jit] [--mmu-faults] " "[--sbr1-record|--sbr1-absent] " "[--expect-panic|--expect-fatal-data|--expect-undefined-fault|--expect-phase2] " - "[--cycles ] [--stage-spk ] [--ipc-script ]\n"; + "[--cycles ] [--stage-spk ] [--ipc-script |--broadway-h1a]\n"; return 2; } bool jit = false; @@ -340,7 +393,8 @@ int main(int argc, char** argv) SBR1Mode sbr1 = SBR1Mode::None; ExpectedOutcome expected = ExpectedOutcome::Idle; bool outcome_selected = false; - std::optional cycles; + std::optional cycles_text; + bool broadway_h1a = false; std::optional package_path; std::optional script_path; for (int index = 2; index < argc; ++index) @@ -366,16 +420,25 @@ int main(int argc, char** argv) } package_path = argv[++index]; } - else if (option == "--cycles") + else if (option == "--broadway-h1a") { - // A whole number of slices, at most MAX_CYCLES (ProbeBudget.h); given once. - if (cycles || index + 1 == argc || !(cycles = ParseCycles(argv[index + 1]))) + // StarOS's H-1A bench loader as Broadway (ProbeBroadway.h), once. + if (broadway_h1a) { - std::cerr << "--cycles takes one decimal multiple of " << SLICE_CYCLES << ", at most " - << MAX_CYCLES << ", once\n"; + std::cerr << "--broadway-h1a is given once\n"; return 2; } - ++index; + broadway_h1a = true; + } + else if (option == "--cycles") + { + // Given once; read below, once the mode, which bounds it, is known. + if (cycles_text || index + 1 == argc) + { + std::cerr << "--cycles takes one decimal multiple of " << SLICE_CYCLES << ", once\n"; + return 2; + } + cycles_text = argv[++index]; } else if (option == "--jit") jit = true; @@ -411,6 +474,23 @@ int main(int argc, char** argv) } } + // The client drives Broadway's side alone: no script, no staged package with it. + if (broadway_h1a && (script_path || package_path)) + { + std::cerr << "--broadway-h1a takes neither --ipc-script nor --stage-spk\n"; + return 2; + } + // A whole number of slices, at most MAX_CYCLES, or MAX_CLIENT_CYCLES with the client + // (ProbeBudget.h). + const u64 max_cycles = broadway_h1a ? MAX_CLIENT_CYCLES : MAX_CYCLES; + std::optional cycles; + if (cycles_text && !(cycles = ParseCycles(*cycles_text, max_cycles))) + { + std::cerr << "--cycles takes one decimal multiple of " << SLICE_CYCLES << ", at most " + << max_cycles << ", once\n"; + return 2; + } + // The package file read and checked before anything is set up: a refused file ends the run // before StarOS starts. std::optional> package; @@ -438,8 +518,10 @@ int main(int argc, char** argv) memory.Write_U32(SBR1_RELAY_JUMPING, SBR1_BASE + SBR1_RELAY_STATUS); memory.Write_U32(SBR1_MAGIC, SBR1_BASE); } + BroadwayClient client; const bool passed = Run(argv[1], jit, mmu_faults, sbr1, expected, cycles.value_or(DEFAULT_CYCLES), - package ? &*package : nullptr, script ? &*script : nullptr); + package ? &*package : nullptr, script ? &*script : nullptr, + broadway_h1a ? &client : nullptr); if (sbr1 != SBR1Mode::None) { // The record's summary after the run; with --sbr1-absent every word must still be zero (an