StarletBootstrapProbe: --stage-spk
StarOS's I2-C image admits its User services from an SPK1 package that the loader stages in MEM2 before the Starlet's first instruction (StarOS docs/contracts/spk-staging.md). --stage-spk <file> places the file at the package window's base, 0x13720000, once the image is loaded: the address is the contract's, never an argument. The bytes are copied as the file holds them (big-endian words) and read back before the first instruction; a window that does not read back fails the run. The file is read and checked before anything is set up. A missing or unreadable file, an empty one, one whose length is not whole 32-bit words or that exceeds the 128 KiB window ends the run with its reason on stderr and exit status 4, before StarOS starts. The option given twice or without its file is a usage error (exit 2). The probe checks none of SPK1's fields or digests: that is StarOS's job. The size rule and the window are in ProbeStaging.h, tested by StarletProbeStagingTest. Without the option the probe runs as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
dd2336f278
commit
9d66381b3d
4 files changed
+153
-6
No files matched your search
@@ -20,6 +20,7 @@
|
||||
#include "Core/System.h"
|
||||
#include "ProbeBudget.h"
|
||||
#include "ProbeRing.h"
|
||||
#include "ProbeStaging.h"
|
||||
#include "StarOSPanicCheck.h"
|
||||
|
||||
namespace
|
||||
@@ -50,8 +51,39 @@ bool FailOnAlert(const char* caption, const char* text, bool, Common::MsgType)
|
||||
std::_Exit(ALERT_EXIT_STATUS);
|
||||
}
|
||||
|
||||
// Reads the package file for --stage-spk: its bytes, or nullopt once the reason is on stderr (a
|
||||
// missing or unreadable file; a length StagingRefusal refuses).
|
||||
std::optional<std::vector<u8>> ReadPackage(const std::string& path)
|
||||
{
|
||||
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << "--stage-spk: cannot open the package file: " << path << '\n';
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::streamoff end = file.tellg();
|
||||
if (end < 0)
|
||||
{
|
||||
std::cerr << "--stage-spk: cannot size the package file: " << path << '\n';
|
||||
return std::nullopt;
|
||||
}
|
||||
if (const auto refusal = StagingRefusal(static_cast<u64>(end)))
|
||||
{
|
||||
std::cerr << "--stage-spk: refused " << path << " (" << end << " bytes): " << *refusal << '\n';
|
||||
return std::nullopt;
|
||||
}
|
||||
std::vector<u8> package(static_cast<size_t>(end));
|
||||
file.seekg(0);
|
||||
if (!file.read(reinterpret_cast<char*>(package.data()), end))
|
||||
{
|
||||
std::cerr << "--stage-spk: cannot read the package file: " << path << '\n';
|
||||
return std::nullopt;
|
||||
}
|
||||
return package;
|
||||
}
|
||||
|
||||
bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1,
|
||||
ExpectedOutcome expected, u64 cycles)
|
||||
ExpectedOutcome expected, u64 cycles, const std::vector<u8>* package)
|
||||
{
|
||||
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
||||
if (!file || file.tellg() <= 0 || file.tellg() > 16 * 1024 * 1024)
|
||||
@@ -80,6 +112,22 @@ bool Run(const std::string& path, bool jit, bool mmu_faults, SBR1Mode sbr1,
|
||||
std::cerr << error << '\n';
|
||||
return false;
|
||||
}
|
||||
if (package)
|
||||
{
|
||||
// The image loaded, then the package at the window's base, its bytes as the file holds them
|
||||
// (big-endian words, as the Starlet reads them); read back before the first instruction.
|
||||
auto& mem2 = system.GetMemory();
|
||||
mem2.CopyToEmu(SPK_WINDOW_BASE, package->data(), package->size());
|
||||
std::vector<u8> staged(package->size());
|
||||
mem2.CopyFromEmu(staged.data(), SPK_WINDOW_BASE, staged.size());
|
||||
if (staged != *package)
|
||||
{
|
||||
std::cerr << "--stage-spk: the package window does not read back as staged\n";
|
||||
return false;
|
||||
}
|
||||
std::cerr << "Staged SPK package: " << package->size() << " bytes at 0x" << std::hex
|
||||
<< SPK_WINDOW_BASE << std::dec << '\n';
|
||||
}
|
||||
|
||||
IOS::LLE::ARMCore core(memory);
|
||||
// Declared after memory, so destroyed first.
|
||||
@@ -212,12 +260,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 > 8)
|
||||
if (argc < 2 || argc > 10)
|
||||
{
|
||||
std::cerr << "Usage: StarletBootstrapProbe <ELF> [--jit] [--mmu-faults] "
|
||||
"[--sbr1-record|--sbr1-absent] "
|
||||
"[--expect-panic|--expect-fatal-data|--expect-undefined-fault|--expect-phase2] "
|
||||
"[--cycles <n>]\n";
|
||||
"[--cycles <n>] [--stage-spk <file>]\n";
|
||||
return 2;
|
||||
}
|
||||
bool jit = false;
|
||||
@@ -226,10 +274,21 @@ int main(int argc, char** argv)
|
||||
ExpectedOutcome expected = ExpectedOutcome::Idle;
|
||||
bool outcome_selected = false;
|
||||
std::optional<u64> cycles;
|
||||
std::optional<std::string> package_path;
|
||||
for (int index = 2; index < argc; ++index)
|
||||
{
|
||||
const std::string_view option = argv[index];
|
||||
if (option == "--cycles")
|
||||
if (option == "--stage-spk")
|
||||
{
|
||||
// One package file, given once (ProbeStaging.h).
|
||||
if (package_path || index + 1 == argc)
|
||||
{
|
||||
std::cerr << "--stage-spk takes one package file, once\n";
|
||||
return 2;
|
||||
}
|
||||
package_path = argv[++index];
|
||||
}
|
||||
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])))
|
||||
@@ -274,6 +333,12 @@ int main(int argc, char** argv)
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
// 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
|
||||
// header at 0x00200000, is an access to unmapped memory and raises an alert.
|
||||
@@ -292,8 +357,8 @@ int main(int argc, char** argv)
|
||||
memory.Write_U32(SBR1_RELAY_JUMPING, SBR1_BASE + SBR1_RELAY_STATUS);
|
||||
memory.Write_U32(SBR1_MAGIC, SBR1_BASE);
|
||||
}
|
||||
const bool passed =
|
||||
Run(argv[1], jit, mmu_faults, sbr1, expected, cycles.value_or(DEFAULT_CYCLES));
|
||||
const bool passed = Run(argv[1], jit, mmu_faults, sbr1, expected, cycles.value_or(DEFAULT_CYCLES),
|
||||
package ? &*package : nullptr);
|
||||
if (sbr1 != SBR1Mode::None)
|
||||
{
|
||||
// The record's summary after the run; with --sbr1-absent every word must still be zero (an
|
||||
|
||||
Reference in new issue
Block a user