Author SHA1 Message Date
Yaya48andClaude Opus 5.5 9ad22f00bd StarletBootstrapProbe: --broadway-h1a's report and budget, from the final review
Two findings of the review of 2df9dfc909 (StarOS H-1A, D2):
- The report reads the bench window too (hw0c_report(): HW-M4-0D's
  area, and H-1A's record if it was never read), but a refused access
  there did not fail the run, as one during the run does. It does now:
  the failure on stderr, and the run fails.
- --cycles' bound of 2^34 was raised whenever "--broadway-h1a" appeared
  among the arguments, even as the value of --ipc-script or --stage-spk
  (a file of that name), the run then not in the client's mode.
  ClientRequested reads the options with their grammar: the word after
  --ipc-script, --stage-spk or --cycles is a value, never an option.

StarletProbeBroadwayTest: ClientRequested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 21:59:34 +01:00
Yaya48andClaude Opus 5.5 2df9dfc909 StarletBootstrapProbe: read --cycles where it stands, as before
--broadway-h1a had moved --cycles' reading after the options, which
changed the usage errors' text for a missing value or a second --cycles.
The bound is now known before the options are read (whether
--broadway-h1a is among them), and --cycles is read in place, with
master's messages: without --broadway-h1a, every run and every refusal
is master's, byte for byte.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 21:03:10 +01:00
Yaya48andClaude Opus 5.5 9cce9b35ef StarletBootstrapProbe: --broadway-h1a ends as the loader's monitor does
The first whole run of StarOS's H-1A image (interpreter, 2^34 cycles)
ran every round: Broadway posted 3000, the Starlet answered 3000, its
record COHERENT, END NONE, S0 PASS. But the probe never ended it: the
image's trace, 1 KiB, is full before its REV:BOOT:IDLE line, and the
probe looked for that line only. StarOS's main.c does not: its monitor
takes the Starlet for done once the SBR1 record's state is IDLE or after,
with EXI given back to Broadway (StarletDone), and its bench ended.

In this mode the probe now does the same: it reads the record, which
--broadway-h1a therefore requires (--sbr1-record), prints the trace as it
stands (and says on stderr when it is full: its last lines are in the
SBR1 ring the probe prints), then the client's report under SBL:STARLET
DONE. The run passes when the trace shows the boot's start, no panic or
fatal line, and the record's state is IDLE (or the trace's IDLE line).
Without --broadway-h1a nothing changes.

StarletProbeBroadwayTest: StarletDone against StarOS's states.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 18:18:05 +01:00
Yaya48andClaude Opus 5.5 f391ae6c85 StarletBootstrapProbe: --broadway-h1a
The probe stands in for Broadway with StarOS's own bench loader code, so
that StarOS's H-1A bench (docs/architecture/h-1a.md, D-H1A-7) runs whole:
its Starlet image on the ARM core, its Broadway side from StarOS's
boot/wii/loader hw0c.c, hw0d.c and h1a.c, vendored unchanged in
Tools/StarletBootstrapProbe/StarOSLoader (StarOS 480119d, stored -text so
their bytes stay StarOS's) with their contract headers, and built over a
host HAL in BroadwayHAL/. CMake computes the nine files' SHA-256s at
configure; the probe prints them when the mode starts.

As StarOS's loader around the jump: hw0c_setup() before the Starlet's
first instruction, one hw0c_step() between each two slices, the timebase
243 MHz / 4 from the Starlet's cycles, until it says 1; once the image's
trace is terminal and the bench is done, the trace, then the report
between main.c's status and END lines on stdout. If the budget runs out,
the report under SBL:TIMEOUT, and the run fails. The client reaches PPC
memory only in the bench window 0x13500000..0x13510000, by whole words:
the first access outside fails the run, and is not made. Dolphin's memory
is coherent: the run rehearses the protocol, the capture and the judge,
not the console's caches.

--cycles may reach 2^34 in this mode only (MAX_CLIENT_CYCLES), 2^30 as
before otherwise. --broadway-h1a takes neither --ipc-script nor
--stage-spk, whose behaviour is unchanged.

StarletProbeBroadwayTest: the window's bounds, the clock, Gecko's number
formats, the budgets, the built-in sources; and, each in a death test's
own process (the loader's state is global), the real loader code against
a scripted Starlet: 3000 rounds and a COHERENT record, no READY, no
Starlet END, a budget too short; a second client refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 18:00:12 +01:00
Yaya48andClaude Opus 5.5 a709fddfa8 StarletBootstrapProbe: --ipc-script
The probe stands in for Broadway, so that a real request can enter StarOS's
image through the Hollywood IPC registers and its reply be read back (StarOS
docs/contracts/hollywood-ipc-injection.md, approved for development). A
script is data: whole words stored in one range of PPC memory,
0x135E0000..0x13600000, PPCMSG and PPCCTRL written, PPCCTRL and ARMMSG read,
every wait bounded in ARM cycles. Steps run between the probe's slices. The
probe knows nothing of the code under test and answers nothing for it.

A script refused when read ends the run with exit status 5, before StarOS
starts. A step that fails, or a terminal trace line with steps left, fails
the run with its line on stderr. The option given twice, or without its
file, is a usage error.

The one change in Source/Core: WiiIPC::SetStarletAttached, an opt-in flag
UpdateInterrupts reads beside GetStarlet(), because the probe runs an
ARMCore with no Starlet object in the System. The IPC cause is then raised
by the model's own line, X1 and IX1 included, with no second model of it.
Reset clears the flag, and nothing sets it without --ipc-script.

ProbeScript.h holds the parser and the runner, tested by
StarletProbeScriptTest over a fake bus; StarletProbeInterruptTest holds the
flag's behaviour with and without it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 00:55:58 +01:00
25 changed files with 2953 additions and 14 deletions

No files matched your search

+2 -1
View File
@@ -141,6 +141,7 @@ void WiiIPC::Reset()
{
INFO_LOG_FMT(WII_IPC, "Resetting ...");
InitState();
m_starlet_attached = false;
}
void WiiIPC::Shutdown()
@@ -478,7 +479,7 @@ void WiiIPC::UpdateInterrupts()
if ((m_ctrl.X1 & m_ctrl.IX1) || (m_ctrl.X2 & m_ctrl.IX2))
{
if (m_system.GetStarlet())
if (m_system.GetStarlet() || m_starlet_attached)
m_arm_irq_flags |= INT_CAUSE_IPC_STARLET;
else
m_ppc_irq_flags |= INT_CAUSE_IPC_STARLET;
+4
View File
@@ -164,6 +164,9 @@ public:
bool IsStarletIRQAsserted() const;
bool IsStarletFIQAsserted() const;
void SetStarletInterrupt(u32 cause, bool asserted);
// Set by a tool that runs an ARMCore with no IOS::LLE::Starlet object in the System, for
// UpdateInterrupts to raise the Starlet's IPC cause all the same. Reset clears it.
void SetStarletAttached(bool attached) { m_starlet_attached = attached; }
Common::Flags<GPIO> GetGPIOOutFlags() const { return m_gpio_out; }
@@ -189,6 +192,7 @@ private:
u32 m_resets = 0;
bool m_broadway_in_reset = false;
bool m_starlet_attached = false;
CoreTiming::EventType* m_event_type_update_interrupts = nullptr;
+26 -1
View File
@@ -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})
+8
View File
@@ -28,6 +28,14 @@ target_include_directories(StarletProbeRingTest PRIVATE ${CMAKE_SOURCE_DIR}/Tool
# StarletBootstrapProbe's --stage-spk checks (Tools/StarletBootstrapProbe/ProbeStaging.h).
add_dolphin_test(StarletProbeStagingTest IOS/Starlet/ProbeStagingTest.cpp)
target_include_directories(StarletProbeStagingTest PRIVATE ${CMAKE_SOURCE_DIR}/Tools/StarletBootstrapProbe)
# StarletBootstrapProbe's --ipc-script (Tools/StarletBootstrapProbe/ProbeScript.h) and its flag.
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)
@@ -0,0 +1,325 @@
// Copyright 2026 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <cstdio>
#include <cstdlib>
#include <string>
#include <string_view>
#include <vector>
#include <gtest/gtest.h>
#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<u32> words = std::vector<u32>((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, OnlyTheOptionItselfRaisesTheBudget)
{
const auto requested = [](std::vector<const char*> argv) {
return ClientRequested(static_cast<int>(argv.size()), argv.data());
};
EXPECT_TRUE(requested({"probe", "image.elf", "--broadway-h1a"}));
EXPECT_TRUE(requested({"probe", "image.elf", "--jit", "--cycles", "4096", "--broadway-h1a"}));
EXPECT_TRUE(requested({"probe", "image.elf", "--stage-spk", "p.bin", "--broadway-h1a"}));
EXPECT_FALSE(requested({"probe", "image.elf"}));
// The image's own name, or another option's value, is no option.
EXPECT_FALSE(requested({"probe", "--broadway-h1a"}));
EXPECT_FALSE(requested({"probe", "image.elf", "--ipc-script", "--broadway-h1a"}));
EXPECT_FALSE(requested({"probe", "image.elf", "--stage-spk", "--broadway-h1a", "--jit"}));
EXPECT_FALSE(requested({"probe", "image.elf", "--cycles", "--broadway-h1a"}));
}
TEST(StarletProbeBroadway, TheStarletIsDoneAsTheLoadersMonitorSaysIt)
{
// StarOS boot/contract/staros_boot.h: RUNNING 2, IDLE 3, FATAL 4, PANIC 5, FAULT 6; EXI PPC 1.
EXPECT_EQ(SBR1_STATE_IDLE, u32{3});
EXPECT_TRUE(StarletDone(false, 3, SBR1_EXI_PPC));
EXPECT_TRUE(StarletDone(false, 6, SBR1_EXI_PPC));
EXPECT_FALSE(StarletDone(false, 2, SBR1_EXI_PPC));
EXPECT_FALSE(StarletDone(false, 3, SBR1_EXI_STARLET));
EXPECT_FALSE(StarletDone(false, 0, 0));
// A terminal trace line counts on its own.
EXPECT_TRUE(StarletDone(true, 0, 0));
}
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<std::string_view> 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<int>(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<unsigned long long>(client.Steps()), memory.outside,
static_cast<int>(client.Failed()), static_cast<int>(client.Done()),
static_cast<int>(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
@@ -0,0 +1,68 @@
// Copyright 2026 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <gtest/gtest.h>
#include "Core/Core.h"
#include "Core/HW/WII_IPC.h"
#include "Core/System.h"
namespace IOS
{
namespace
{
constexpr u32 PPCCTRL = 0x04;
constexpr u32 ARMCTRL = 0x0c;
constexpr u32 ARM_IRQMASK = 0x3c;
constexpr u32 IX1 = 0x10;
constexpr u32 X1 = 0x01;
WiiIPC& Fresh(bool attached)
{
Core::DeclareAsCPUThread();
auto& ipc = Core::System::GetInstance().GetWiiIPC();
ipc.Reset();
ipc.SetStarletAttached(attached);
ipc.WriteStarletRegister(ARM_IRQMASK, INT_CAUSE_IPC_STARLET);
return ipc;
}
TEST(StarletProbeInterrupt, WithoutTheFlagX1NeverReachesTheStarlet)
{
// master's behaviour, with no Starlet object in the system.
auto& ipc = Fresh(false);
ipc.WriteStarletRegister(ARMCTRL, IX1);
ipc.WriteStarletRegister(PPCCTRL, X1);
EXPECT_FALSE(ipc.IsStarletIRQAsserted());
}
TEST(StarletProbeInterrupt, WithTheFlagTheModelRaisesItOnX1AndIX1)
{
auto& ipc = Fresh(true);
ipc.WriteStarletRegister(ARMCTRL, IX1);
EXPECT_FALSE(ipc.IsStarletIRQAsserted());
ipc.WriteStarletRegister(PPCCTRL, X1);
EXPECT_TRUE(ipc.IsStarletIRQAsserted());
}
TEST(StarletProbeInterrupt, WithoutIX1TheCauseWaitsForIt)
{
auto& ipc = Fresh(true);
ipc.WriteStarletRegister(PPCCTRL, X1);
EXPECT_FALSE(ipc.IsStarletIRQAsserted());
// The same model line, on the Starlet's first ARMCTRL write.
ipc.WriteStarletRegister(ARMCTRL, IX1);
EXPECT_TRUE(ipc.IsStarletIRQAsserted());
}
TEST(StarletProbeInterrupt, ResetClearsTheFlag)
{
auto& ipc = Fresh(true);
ipc.Reset();
ipc.WriteStarletRegister(ARM_IRQMASK, INT_CAUSE_IPC_STARLET);
ipc.WriteStarletRegister(ARMCTRL, IX1);
ipc.WriteStarletRegister(PPCCTRL, X1);
EXPECT_FALSE(ipc.IsStarletIRQAsserted());
}
} // namespace
} // namespace IOS
@@ -0,0 +1,213 @@
// Copyright 2026 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
#include "ProbeScript.h"
namespace StarletBootstrapProbe
{
namespace
{
// The script's error, or "" when it parses.
std::string Refusal(std::string_view script)
{
std::string error;
return ParseScript(script, &error) ? "" : error;
}
std::vector<ScriptStep> Steps(std::string_view script)
{
std::string error;
const auto steps = ParseScript(script, &error);
EXPECT_TRUE(steps.has_value()) << error;
return steps.value_or(std::vector<ScriptStep>{});
}
class FakeBus final : public ScriptBus
{
public:
void StoreWord(u32 address, u32 word) override
{
log.push_back("store");
words[address] = word;
}
u32 LoadWord(u32 address) override { return words[address]; }
void WriteRegister(u32 offset, u32 value) override
{
log.push_back(offset == IPC_PPCMSG ? "ppcmsg" : "ppcctrl");
registers[offset] = value;
}
u32 ReadRegister(u32 offset) override { return registers[offset]; }
std::map<u32, u32> words;
std::map<u32, u32> registers;
std::vector<std::string> log;
};
TEST(StarletProbeScript, TheRangeIsTheContracts)
{
EXPECT_EQ(IPC_RANGE_BASE, u32{0x135E0000});
EXPECT_EQ(IPC_RANGE_END, u32{0x13600000});
EXPECT_TRUE(InIPCRange(0x135E0000));
EXPECT_TRUE(InIPCRange(0x135FFFFC));
EXPECT_FALSE(InIPCRange(0x135DFFFC));
EXPECT_FALSE(InIPCRange(0x13600000));
EXPECT_FALSE(InIPCRange(0x135E0002));
EXPECT_FALSE(InIPCRange(0x135FFFFE));
// The package window and the kernel's memory lie above the boundary.
EXPECT_FALSE(InIPCRange(0x13720000));
}
TEST(StarletProbeScript, ParsesEveryStepCommentsAndBlankLines)
{
const auto steps = Steps("# a comment\n"
"\n"
"store 0x135E0000 1 # trailing\n"
" ppcmsg\t0x135E0000\r\n"
"ppcctrl 0x31\n"
"wait 0x02 0x02 4096\n"
"expect-armmsg 0x135E0000\n"
"expect 0x135E0004 0xFFFFFFFA\n"
"run 8192");
ASSERT_EQ(steps.size(), size_t{7});
EXPECT_EQ(steps[0].kind, StepKind::Store);
EXPECT_EQ(steps[0].line, 3);
EXPECT_EQ(steps[0].first, u32{0x135E0000});
EXPECT_EQ(steps[3].kind, StepKind::Wait);
EXPECT_EQ(steps[3].cycles, u64{4096});
EXPECT_EQ(steps[5].second, u32{0xFFFFFFFA});
EXPECT_EQ(steps[6].kind, StepKind::Run);
}
TEST(StarletProbeScript, APpcMsgMayNameAnAddressOutOfTheRange)
{
// A register's value: the negative scenario's, an envelope the kernel must refuse.
EXPECT_EQ(Refusal("ppcmsg 0x00001000\n"), "");
}
TEST(StarletProbeScript, RefusesWhatIsNotTheFormat)
{
EXPECT_EQ(Refusal("nop\n"), "line 1: unknown step: nop");
EXPECT_EQ(Refusal("\n\nstore 1\n"), "line 3: takes an address and a word");
EXPECT_EQ(Refusal("store 0x135E0000 1 2\n"), "line 1: takes an address and a word");
EXPECT_EQ(Refusal("store 0x135E0000 0x100000000\n"), "line 1: takes an address and a word");
EXPECT_EQ(Refusal("store 0x135E0000 x\n"), "line 1: not a number: x");
EXPECT_EQ(Refusal("store 0x 1\n"), "line 1: not a number: 0x");
EXPECT_EQ(Refusal("store -4 1\n"), "line 1: not a number: -4");
EXPECT_EQ(Refusal("ppcmsg\n"), "line 1: takes one word");
EXPECT_EQ(Refusal("ppcctrl 0x40\n"), "line 1: takes PPCCTRL's six bits");
EXPECT_EQ(Refusal("wait 1 1\n"), "line 1: takes a mask, a value and a bound in cycles");
EXPECT_EQ(Refusal("wait 1 1 0\n"), "line 1: takes a mask, a value and a bound in cycles");
EXPECT_EQ(Refusal("run 0\n"), "line 1: takes a number of cycles");
EXPECT_EQ(Refusal("run 1073741825\n"), "line 1: takes a number of cycles");
}
TEST(StarletProbeScript, RefusesAStoreOrAnExpectOutOfTheRange)
{
const std::string_view why = "line 1: the address is not a whole word of the range";
EXPECT_EQ(Refusal("store 0x135DFFFC 1\n"), why);
EXPECT_EQ(Refusal("store 0x13600000 1\n"), why);
EXPECT_EQ(Refusal("store 0x135E0002 1\n"), why);
EXPECT_EQ(Refusal("store 0x13720000 1\n"), why);
EXPECT_EQ(Refusal("expect 0x00200000 1\n"), why);
}
TEST(StarletProbeScript, RefusesTooManySteps)
{
std::string script;
for (size_t index = 0; index < SCRIPT_MAX_STEPS; ++index)
script += "run 4096\n";
EXPECT_EQ(Refusal(script), "");
script += "run 4096\n";
EXPECT_EQ(Refusal(script), "line 257: more than 256 steps");
}
TEST(StarletProbeScript, StepsRunInOrderUpToOneThatWaits)
{
FakeBus bus;
ScriptRunner runner(Steps("store 0x135E0000 7\n"
"ppcmsg 0x135E0000\n"
"ppcctrl 0x31\n"
"wait 0x02 0x02 8192\n"
"ppcctrl 0x32\n"));
runner.Advance(bus, 0);
EXPECT_EQ(bus.log, (std::vector<std::string>{"store", "ppcmsg", "ppcctrl"}));
EXPECT_EQ(bus.words[0x135E0000], u32{7});
EXPECT_EQ(bus.registers[IPC_PPCMSG], u32{0x135E0000});
EXPECT_FALSE(runner.Done());
EXPECT_EQ(runner.Line(), 4);
// Y2 raised by the code under test: the wait ends, the next step runs.
runner.Advance(bus, 4096);
EXPECT_FALSE(runner.Done());
bus.registers[IPC_PPCCTRL] = 0x33;
runner.Advance(bus, 4096);
EXPECT_TRUE(runner.Done());
EXPECT_EQ(bus.registers[IPC_PPCCTRL], u32{0x32});
EXPECT_FALSE(runner.Failed());
}
TEST(StarletProbeScript, AWaitThatRunsOutOfItsBoundFailsItsLine)
{
FakeBus bus;
ScriptRunner runner(Steps("\nwait 0x04 0x04 8192\n"));
runner.Advance(bus, 0);
runner.Advance(bus, 4096);
EXPECT_FALSE(runner.Failed());
bus.registers[IPC_PPCCTRL] = 0x30;
runner.Advance(bus, 4096);
ASSERT_TRUE(runner.Failed());
EXPECT_EQ(runner.Failure(), "line 2: PPCCTRL reads 0x30 after 8192 cycles");
EXPECT_FALSE(runner.Done());
// A failed script runs nothing more.
bus.registers[IPC_PPCCTRL] = 0x34;
runner.Advance(bus, 4096);
EXPECT_FALSE(runner.Done());
}
TEST(StarletProbeScript, AnExpectationNotMetFailsItsLine)
{
FakeBus bus;
ScriptRunner armmsg(Steps("expect-armmsg 0x135E0000\n"));
armmsg.Advance(bus, 0);
EXPECT_EQ(armmsg.Failure(), "line 1: ARMMSG reads 0x0, not 0x135e0000");
ScriptRunner word(Steps("expect 0x135E0004 1\n"));
word.Advance(bus, 0);
EXPECT_EQ(word.Failure(), "line 1: the word at 0x135e0004 reads 0x0, not 0x1");
bus.words[0x135E0004] = 1;
ScriptRunner met(Steps("expect 0x135E0004 1\n"));
met.Advance(bus, 0);
EXPECT_TRUE(met.Done());
EXPECT_FALSE(met.Failed());
}
TEST(StarletProbeScript, RunWaitsItsCycles)
{
FakeBus bus;
ScriptRunner runner(Steps("run 8192\nppcctrl 1\n"));
runner.Advance(bus, 0);
runner.Advance(bus, 4096);
EXPECT_TRUE(bus.log.empty());
runner.Advance(bus, 4096);
EXPECT_EQ(bus.log, (std::vector<std::string>{"ppcctrl"}));
EXPECT_TRUE(runner.Done());
}
TEST(StarletProbeScript, AnEmptyScriptIsDone)
{
FakeBus bus;
ScriptRunner runner(Steps("# nothing\n"));
runner.Advance(bus, 0);
EXPECT_TRUE(runner.Done());
}
} // namespace
} // namespace StarletBootstrapProbe
@@ -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 <stdint.h>
#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
@@ -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 <stdint.h>
#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
@@ -0,0 +1,185 @@
// Copyright 2026 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "ProbeBroadway.h"
#include <cstdint>
#include <optional>
#include <fmt/format.h>
#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<BroadwayHal> 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);
}
}
+145
View File
@@ -0,0 +1,145 @@
// Copyright 2026 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <string>
#include <string_view>
#include "Common/CommonTypes.h"
#include "StarOSPanicCheck.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 StarOS's loader would take the Starlet for done, as main.c's monitor does: the SBR1
// record's state IDLE or after, with EXI given back to Broadway. The client's mode takes
// --sbr1-record so that the record is there. The trace's own terminal line also counts; it can be
// missing, the trace holding 1 KiB, which H-1A's image fills before its IDLE line.
constexpr bool StarletDone(bool trace_terminal, u32 sbr1_state, u32 sbr1_exi_owner)
{
return trace_terminal || (sbr1_state >= SBR1_STATE_IDLE && sbr1_exi_owner == SBR1_EXI_PPC);
}
// Whether --broadway-h1a is among the probe's options (argv[2] on), read with their grammar: the
// word after --ipc-script, --stage-spk or --cycles is that option's value, never an option. It
// raises --cycles' bound, which is read where it stands, before the other options are known.
inline bool ClientRequested(int argc, const char* const* argv)
{
for (int index = 2; index < argc; ++index)
{
const std::string_view option = argv[index];
if (option == "--ipc-script" || option == "--stage-spk" || option == "--cycles")
++index;
else if (option == "--broadway-h1a")
return true;
}
return false;
}
// 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: "<file> <sha256>" 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
+9 -4
View File
@@ -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<u64> 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<u64> 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<u64> ParseCycles(std::string_view text)
return std::nullopt;
value = value * 10 + static_cast<u64>(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;
}
+273
View File
@@ -0,0 +1,273 @@
// Copyright 2026 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <charconv>
#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <fmt/format.h>
#include "Common/CommonTypes.h"
// StarletBootstrapProbe's --ipc-script <file>: the probe stands in for Broadway (StarOS
// docs/contracts/hollywood-ipc-injection.md). A script is data: whole words stored in one range
// of PPC memory, the registers Broadway writes and reads, every wait bounded. It knows nothing of
// the code under test and answers nothing for it: what it reads back, the ARM code wrote.
namespace StarletBootstrapProbe
{
// The one range a script may store to or expect from: the 128 KiB below Broadway's boundary.
constexpr u32 IPC_RANGE_BASE = 0x135E0000;
// Exclusive.
constexpr u32 IPC_RANGE_END = 0x13600000;
constexpr u64 SCRIPT_MAX_BYTES = 64 * 1024;
constexpr size_t SCRIPT_MAX_STEPS = 256;
constexpr u64 SCRIPT_MAX_CYCLES = u64{1} << 30;
// Exit status of a run whose script is refused before StarOS starts (1: failed expectation,
// 2: usage, 3: Dolphin alert, 4: refused package file).
constexpr int SCRIPT_EXIT_STATUS = 5;
// The registers, as offsets from Hollywood's register page.
constexpr u32 IPC_PPCMSG = 0x00;
constexpr u32 IPC_PPCCTRL = 0x04;
constexpr u32 IPC_ARMMSG = 0x08;
// PPCCTRL's bits a write means: X1, Y2, Y1, X2, IY1, IY2.
constexpr u32 IPC_PPCCTRL_BITS = 0x3F;
enum class StepKind
{
Store,
PpcMsg,
PpcCtrl,
Wait,
ExpectArmMsg,
Expect,
Run
};
struct ScriptStep
{
StepKind kind;
int line;
u32 first;
u32 second;
u64 cycles;
};
// What a script reaches: PPC memory and the registers, nothing of the code under test.
class ScriptBus
{
public:
virtual ~ScriptBus() = default;
virtual void StoreWord(u32 address, u32 word) = 0;
virtual u32 LoadWord(u32 address) = 0;
virtual void WriteRegister(u32 offset, u32 value) = 0;
virtual u32 ReadRegister(u32 offset) = 0;
};
// A number, hexadecimal with 0x or decimal, whole: nothing else.
inline std::optional<u64> ParseScriptNumber(std::string_view text)
{
const bool hexadecimal = text.starts_with("0x");
if (hexadecimal)
text.remove_prefix(2);
if (text.empty())
return std::nullopt;
u64 value = 0;
const auto [end, status] =
std::from_chars(text.data(), text.data() + text.size(), value, hexadecimal ? 16 : 10);
if (status != std::errc{} || end != text.data() + text.size())
return std::nullopt;
return value;
}
// Whether a whole word at `address` lies in the range.
constexpr bool InIPCRange(u64 address)
{
return address >= IPC_RANGE_BASE && address + 4 <= IPC_RANGE_END && address % 4 == 0;
}
// The script's steps, or nullopt with the reason in `error`. Nothing is run.
inline std::optional<std::vector<ScriptStep>> ParseScript(std::string_view text, std::string* error)
{
const auto refuse = [error](int line, std::string_view why) {
*error = fmt::format("line {}: {}", line, why);
return std::nullopt;
};
std::vector<ScriptStep> steps;
int line_number = 0;
while (!text.empty())
{
++line_number;
const size_t end = text.find('\n');
std::string_view line = text.substr(0, end);
text = end == std::string_view::npos ? std::string_view{} : text.substr(end + 1);
line = line.substr(0, line.find('#'));
std::vector<std::string_view> words;
while (!line.empty())
{
const size_t start = line.find_first_not_of(" \t\r");
if (start == std::string_view::npos)
break;
line.remove_prefix(start);
const size_t stop = line.find_first_of(" \t\r");
words.push_back(line.substr(0, stop));
line = stop == std::string_view::npos ? std::string_view{} : line.substr(stop);
}
if (words.empty())
continue;
if (steps.size() == SCRIPT_MAX_STEPS)
return refuse(line_number, fmt::format("more than {} steps", SCRIPT_MAX_STEPS));
std::vector<u64> numbers;
for (size_t index = 1; index < words.size(); ++index)
{
const std::optional<u64> number = ParseScriptNumber(words[index]);
if (!number)
return refuse(line_number, fmt::format("not a number: {}", words[index]));
numbers.push_back(*number);
}
const auto operands = [&numbers](size_t count) { return numbers.size() == count; };
const auto word = [&numbers](size_t index) { return numbers[index] <= 0xFFFFFFFF; };
const auto cycles = [&numbers](size_t index) {
return numbers[index] != 0 && numbers[index] <= SCRIPT_MAX_CYCLES;
};
const std::string_view name = words[0];
ScriptStep step{StepKind::Run, line_number, 0, 0, 0};
if (name == "store" || name == "expect")
{
if (!operands(2) || !word(1))
return refuse(line_number, "takes an address and a word");
if (!InIPCRange(numbers[0]))
return refuse(line_number, "the address is not a whole word of the range");
step.kind = name == "store" ? StepKind::Store : StepKind::Expect;
step.first = static_cast<u32>(numbers[0]);
step.second = static_cast<u32>(numbers[1]);
}
else if (name == "ppcmsg" || name == "expect-armmsg")
{
// A register's value, in or out of the range: nothing is accessed through it here.
if (!operands(1) || !word(0))
return refuse(line_number, "takes one word");
step.kind = name == "ppcmsg" ? StepKind::PpcMsg : StepKind::ExpectArmMsg;
step.first = static_cast<u32>(numbers[0]);
}
else if (name == "ppcctrl")
{
if (!operands(1) || numbers[0] > IPC_PPCCTRL_BITS)
return refuse(line_number, "takes PPCCTRL's six bits");
step.kind = StepKind::PpcCtrl;
step.first = static_cast<u32>(numbers[0]);
}
else if (name == "wait")
{
if (!operands(3) || !word(0) || !word(1) || !cycles(2))
return refuse(line_number, "takes a mask, a value and a bound in cycles");
step.kind = StepKind::Wait;
step.first = static_cast<u32>(numbers[0]);
step.second = static_cast<u32>(numbers[1]);
step.cycles = numbers[2];
}
else if (name == "run")
{
if (!operands(1) || !cycles(0))
return refuse(line_number, "takes a number of cycles");
step.kind = StepKind::Run;
step.cycles = numbers[0];
}
else
{
return refuse(line_number, fmt::format("unknown step: {}", name));
}
steps.push_back(step);
}
return steps;
}
// Runs a script's steps in order, between the probe's slices.
class ScriptRunner
{
public:
explicit ScriptRunner(std::vector<ScriptStep> steps) : m_steps(std::move(steps)) {}
// `elapsed` ARM cycles went by: every step that can run now does, up to one that must wait.
void Advance(ScriptBus& bus, u64 elapsed)
{
m_waited += elapsed;
while (m_failure.empty() && m_next < m_steps.size())
{
const ScriptStep& step = m_steps[m_next];
switch (step.kind)
{
case StepKind::Store:
bus.StoreWord(step.first, step.second);
break;
case StepKind::PpcMsg:
bus.WriteRegister(IPC_PPCMSG, step.first);
break;
case StepKind::PpcCtrl:
bus.WriteRegister(IPC_PPCCTRL, step.first);
break;
case StepKind::Wait:
{
const u32 seen = bus.ReadRegister(IPC_PPCCTRL);
if ((seen & step.first) != step.second)
{
if (m_waited >= step.cycles)
Fail(step, fmt::format("PPCCTRL reads {:#x} after {} cycles", seen, m_waited));
return;
}
break;
}
case StepKind::ExpectArmMsg:
{
const u32 seen = bus.ReadRegister(IPC_ARMMSG);
if (seen != step.first)
return Fail(step, fmt::format("ARMMSG reads {:#x}, not {:#x}", seen, step.first));
break;
}
case StepKind::Expect:
{
const u32 seen = bus.LoadWord(step.first);
if (seen != step.second)
{
return Fail(step, fmt::format("the word at {:#x} reads {:#x}, not {:#x}", step.first,
seen, step.second));
}
break;
}
case StepKind::Run:
if (m_waited < step.cycles)
return;
break;
}
++m_next;
m_waited = 0;
}
}
bool Done() const { return m_next == m_steps.size(); }
bool Failed() const { return !m_failure.empty(); }
const std::string& Failure() const { return m_failure; }
// The line of the step it stands on.
int Line() const { return Done() ? 0 : m_steps[m_next].line; }
private:
void Fail(const ScriptStep& step, const std::string& why)
{
m_failure = fmt::format("line {}: {}", step.line, why);
}
std::vector<ScriptStep> m_steps;
size_t m_next = 0;
u64 m_waited = 0;
std::string m_failure;
};
} // namespace StarletBootstrapProbe
@@ -0,0 +1 @@
* -text
@@ -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 <stdint.h>
#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");
}
@@ -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 <stdint.h>
/* 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
@@ -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 <stdint.h>
#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
}
@@ -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 <stdint.h>
/* 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
@@ -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 <stdint.h>
#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");
}
@@ -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 <stdint.h>
/* 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
@@ -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
@@ -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
@@ -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
@@ -41,6 +41,9 @@ constexpr u32 SBR1_RING_DROPPED = 0x30;
constexpr u32 SBR1_EXI_PPC = 1;
constexpr u32 SBR1_EXI_STARLET = 2;
constexpr u32 SBR1_RELAY_JUMPING = 4;
// StarOS boot/contract/staros_boot.h's STAROS_STATE_IDLE: the Starlet's run is over; FATAL, PANIC and
// FAULT come after it.
constexpr u32 SBR1_STATE_IDLE = 3;
constexpr u32 SBR1_STATE_PANIC = 5;
// __panic_state once the path is entered; __panic_code of a Rust panic.
constexpr u32 PANIC_ENTERED = 1;
@@ -18,8 +18,10 @@
#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"
#include "ProbeStaging.h"
#include "StarOSPanicCheck.h"
@@ -82,8 +84,71 @@ std::optional<std::vector<u8>> ReadPackage(const std::string& path)
return package;
}
// Reads and parses the file of --ipc-script: its runner, or nullopt once the reason is on stderr.
std::optional<ScriptRunner> ReadScript(const std::string& path)
{
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file)
{
std::cerr << "--ipc-script: cannot open the script: " << path << '\n';
return std::nullopt;
}
const std::streamoff end = file.tellg();
if (end < 0 || static_cast<u64>(end) > SCRIPT_MAX_BYTES)
{
std::cerr << "--ipc-script: refused " << path << ": over " << SCRIPT_MAX_BYTES << " bytes\n";
return std::nullopt;
}
std::string text(static_cast<size_t>(end), '\0');
file.seekg(0);
if (!file.read(text.data(), end))
{
std::cerr << "--ipc-script: cannot read the script: " << path << '\n';
return std::nullopt;
}
std::string error;
auto steps = ParseScript(text, &error);
if (!steps)
{
std::cerr << "--ipc-script: refused " << path << ": " << error << '\n';
return std::nullopt;
}
return ScriptRunner(std::move(*steps));
}
// What a script reaches: PPC memory and the registers, as Broadway does.
class ProbeBus final : public ScriptBus
{
public:
ProbeBus(Memory::MemoryManager& memory, IOS::WiiIPC& ipc) : m_memory(memory), m_ipc(ipc) {}
void StoreWord(u32 address, u32 word) override { m_memory.Write_U32(word, address); }
u32 LoadWord(u32 address) override { return m_memory.Read_U32(address); }
void WriteRegister(u32 offset, u32 value) override { m_ipc.WriteStarletRegister(offset, value); }
u32 ReadRegister(u32 offset) override { return m_ipc.ReadStarletRegister(offset); }
private:
Memory::MemoryManager& m_memory;
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<u8>* package)
ExpectedOutcome expected, u64 cycles, const std::vector<u8>* 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)
@@ -129,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);
@@ -158,9 +236,42 @@ 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. The report reads the
// window too: false if it reached outside (the access refused, as during the run).
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';
if (client->Failed())
std::cerr << "--broadway-h1a: " << client->Failure() << '\n';
return !client->Failed();
};
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)
system.GetWiiIPC().SetStarletAttached(true);
int panic_settled = 0;
for (u64 slice = 0; slice < cycles / SLICE_CYCLES; ++slice)
{
if (script)
{
script->Advance(script_bus, slice == 0 ? 0 : SLICE_CYCLES);
if (script->Failed())
{
std::cerr << "--ipc-script: " << script->Failure() << '\n';
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);
@@ -193,6 +304,41 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1,
report_panic();
return true;
}
if (client)
{
// --broadway-h1a: the loader's monitor reports once the SBR1 record says the Starlet is done
// (StarletDone) and its bench has ended; the trace, possibly full, is printed as it stands.
const u32 length = memory.Read32(info.trace) == TRACE_MAGIC ?
std::min(memory.Read32(info.trace + 4), TRACE_CAPACITY) :
0;
std::string trace(length, '\0');
for (u32 index = 0; index < length; ++index)
trace[index] = static_cast<char>(memory.Read8(info.trace + 8 + index));
const bool trace_terminal = trace.find("REV:BOOT:IDLE\n") != std::string::npos ||
trace.find("REV:PANIC") != std::string::npos ||
trace.find("REV:FATAL") != std::string::npos;
const u32 state = mem1.Read_U32(SBR1_BASE + SBR1_STARLET_STATE);
if (!StarletDone(trace_terminal, state, mem1.Read_U32(SBR1_BASE + SBR1_EXI_OWNER)) ||
!client->Done())
{
continue;
}
std::cout << trace;
std::cout << "PC=0x" << std::hex << core.GetRegister(15) << std::dec
<< " instructions=" << core.GetExecutedInstructions() << '\n';
if (length == TRACE_CAPACITY)
std::cerr << "--broadway-h1a: the trace is full: its last lines are in the SBR1 ring\n";
const bool reported = report_client(CLIENT_STATUS_DONE);
const bool startup_ok = trace.find("REV:BOOT:ENTRY\n") != std::string::npos &&
trace.find("REV:BOOT:BSS_OK\n") != std::string::npos &&
trace.find("REV:BOOT:DATA_OK\n") != std::string::npos &&
trace.find("REV:BOOT:STACK_OK\n") != std::string::npos &&
trace.find("REV:BOOT:RUST\n") != std::string::npos;
const bool idle =
state == SBR1_STATE_IDLE || trace.find("REV:BOOT:IDLE\n") != std::string::npos;
return reported && startup_ok && idle && trace.find("REV:PANIC") == std::string::npos &&
trace.find("REV:FATAL") == std::string::npos;
}
if (memory.Read32(info.trace) != TRACE_MAGIC)
continue;
const u32 length = memory.Read32(info.trace + 4);
@@ -213,6 +359,11 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1,
std::cout << trace;
std::cout << "PC=0x" << std::hex << core.GetRegister(15) << std::dec
<< " instructions=" << core.GetExecutedInstructions() << '\n';
if (script && !script->Done())
{
std::cerr << "--ipc-script: script unfinished at line " << script->Line() << '\n';
return false;
}
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)
@@ -253,6 +404,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
@@ -260,12 +414,12 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1,
int main(int argc, char** argv)
{
Common::RegisterMsgAlertHandler(FailOnAlert);
if (argc < 2 || argc > 10)
if (argc < 2 || argc > 12)
{
std::cerr << "Usage: StarletBootstrapProbe <ELF> [--jit] [--mmu-faults] "
"[--sbr1-record|--sbr1-absent] "
"[--expect-panic|--expect-fatal-data|--expect-undefined-fault|--expect-phase2] "
"[--cycles <n>] [--stage-spk <file>]\n";
"[--cycles <n>] [--stage-spk <file>] [--ipc-script <file>|--broadway-h1a]\n";
return 2;
}
bool jit = false;
@@ -274,11 +428,27 @@ int main(int argc, char** argv)
ExpectedOutcome expected = ExpectedOutcome::Idle;
bool outcome_selected = false;
std::optional<u64> cycles;
bool broadway_h1a = false;
// --cycles' bound: MAX_CYCLES, or MAX_CLIENT_CYCLES when --broadway-h1a is among the options
// (ProbeBudget.h, ClientRequested), known before they are read so that --cycles is read where
// it stands.
const u64 max_cycles = ClientRequested(argc, argv) ? MAX_CLIENT_CYCLES : MAX_CYCLES;
std::optional<std::string> package_path;
std::optional<std::string> script_path;
for (int index = 2; index < argc; ++index)
{
const std::string_view option = argv[index];
if (option == "--stage-spk")
if (option == "--ipc-script")
{
// One script, given once (ProbeScript.h).
if (script_path || index + 1 == argc)
{
std::cerr << "--ipc-script takes one file, once\n";
return 2;
}
script_path = argv[++index];
}
else if (option == "--stage-spk")
{
// One package file, given once (ProbeStaging.h).
if (package_path || index + 1 == argc)
@@ -288,13 +458,23 @@ int main(int argc, char** argv)
}
package_path = argv[++index];
}
else if (option == "--broadway-h1a")
{
// StarOS's H-1A bench loader as Broadway (ProbeBroadway.h), once.
if (broadway_h1a)
{
std::cerr << "--broadway-h1a is given once\n";
return 2;
}
broadway_h1a = true;
}
else if (option == "--cycles")
{
// A whole number of slices, at most MAX_CYCLES (ProbeBudget.h); given once.
if (cycles || index + 1 == argc || !(cycles = ParseCycles(argv[index + 1])))
// A whole number of slices, at most max_cycles (ProbeBudget.h); given once.
if (cycles || index + 1 == argc || !(cycles = ParseCycles(argv[index + 1], max_cycles)))
{
std::cerr << "--cycles takes one decimal multiple of " << SLICE_CYCLES << ", at most "
<< MAX_CYCLES << ", once\n";
<< max_cycles << ", once\n";
return 2;
}
++index;
@@ -333,11 +513,27 @@ int main(int argc, char** argv)
}
}
// The client drives Broadway's side alone: no script, no staged package with it; and it reads
// the SBR1 record, as the loader's monitor does, so the record must be there.
if (broadway_h1a && (script_path || package_path))
{
std::cerr << "--broadway-h1a takes neither --ipc-script nor --stage-spk\n";
return 2;
}
if (broadway_h1a && sbr1 != SBR1Mode::Record)
{
std::cerr << "--broadway-h1a takes --sbr1-record: the loader's monitor reads the record\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<std::vector<u8>> package;
if (package_path && !(package = ReadPackage(*package_path)))
return STAGING_EXIT_STATUS;
std::optional<ScriptRunner> script;
if (script_path && !(script = ReadScript(*script_path)))
return SCRIPT_EXIT_STATUS;
// The Starlet shares MEM1 and MEM2 with Broadway. Back them as on a retail Wii (24 and 64 MiB,
// zeroed): without them, any Starlet access there, such as StarOS reading its SBR1 record
@@ -357,8 +553,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);
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