feat(runtime): implement shared transactional memcard with async saves
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good

This commit is contained in:
2026-10-13 15:24:01 +01:00
parent 0af8b2b1fe
commit 6c2a07e579
65 changed files with 6222 additions and 985 deletions
Generated
+1
View File
@@ -2153,6 +2153,7 @@ dependencies = [
"prometeu-hal",
"prometeu-test-support",
"prometeu-vm",
"serde",
"serde_json",
]
@@ -65,6 +65,7 @@ pub enum LifecycleRole {
None = 0,
BeforePause = 1,
BeforeResume = 2,
MemcardBoot = 3,
}
impl TryFrom<u8> for LifecycleRole {
type Error = E;
@@ -73,6 +74,7 @@ impl TryFrom<u8> for LifecycleRole {
0 => Ok(Self::None),
1 => Ok(Self::BeforePause),
2 => Ok(Self::BeforeResume),
3 => Ok(Self::MemcardBoot),
_ => Err(E::new(C::InvalidRegistry, R::LifecycleRole)),
}
}
@@ -254,7 +256,7 @@ pub struct FunctionLayout {
#[derive(Debug)]
pub struct Registry {
lifecycle_hooks: [Option<FunctionId>; 2],
lifecycle_hooks: [Option<FunctionId>; 3],
declarations: RegistryDeclarations,
layouts: Vec<TypeLayout>,
functions: Vec<FunctionLayout>,
@@ -267,6 +269,7 @@ impl Registry {
LifecycleRole::None => None,
LifecycleRole::BeforePause => self.lifecycle_hooks[0],
LifecycleRole::BeforeResume => self.lifecycle_hooks[1],
LifecycleRole::MemcardBoot => self.lifecycle_hooks[2],
}
}
pub fn declarations(&self) -> &RegistryDeclarations {
@@ -376,16 +379,25 @@ impl Registry {
});
}
}
let mut lifecycle_hooks = [None; 2];
let mut lifecycle_hooks = [None; 3];
let mut functions = b.vector(d.functions.len(), Q::RegistryBytes)?;
let mut ranges = b.vector(d.functions.len(), Q::TemporaryBytes)?;
for (i, f) in d.functions.iter().enumerate() {
b.work(1)?;
if f.lifecycle_role != LifecycleRole::None {
let invalid = i == 0
|| !f.signature.parameters.is_empty()
|| !f.signature.results.is_empty()
|| !f.captures.is_empty();
let signature_valid = if f.lifecycle_role == LifecycleRole::MemcardBoot {
f.signature.parameters.len() == 1
&& f.signature.results.len() == 1
&& d.types
.get(f.signature.parameters[0].type_id.0 as usize)
.is_some_and(|t| t.kind == TypeKind::Builtin(BuiltinId::INT64))
&& d.types
.get(f.signature.results[0].0 as usize)
.is_some_and(|t| t.kind == TypeKind::Builtin(BuiltinId::INT32))
} else {
f.signature.parameters.is_empty() && f.signature.results.is_empty()
};
let invalid = i == 0 || !signature_valid || !f.captures.is_empty();
if invalid {
return Err(E::new(C::InvalidEntrypoint, R::LifecycleSignature)
.in_section(1)
@@ -1128,3 +1128,35 @@ fn lifecycle_roles_resolve_and_roundtrip_with_strict_validation() {
);
}
}
#[test]
fn memcard_role_has_its_own_signature_and_does_not_reinterpret_function_id() {
let declarations = RegistryDeclarations {
types: vec![builtin(BuiltinId::INT64), builtin(BuiltinId::INT32)],
functions: vec![
FunctionDecl { code_len: 2, ..Default::default() },
FunctionDecl { code_offset: 2, code_len: 2, ..Default::default() },
FunctionDecl {
lifecycle_role: LifecycleRole::MemcardBoot,
signature: Signature { parameters: vec![named(0)], results: vec![TypeId(1)] },
code_offset: 4,
code_len: 2,
..Default::default()
},
],
..Default::default()
};
let registry = resolve(declarations.clone()).unwrap();
assert_eq!(registry.lifecycle_hook(LifecycleRole::MemcardBoot), Some(FunctionId(2)));
assert_eq!(LifecycleRole::try_from(3), Ok(LifecycleRole::MemcardBoot));
for case in 0..4 {
let mut invalid = declarations.clone();
match case {
0 => invalid.functions[2].signature.parameters[0].type_id = TypeId(1),
1 => invalid.functions[2].signature.results = vec![TypeId(0)],
2 => invalid.functions[2].captures = vec![TypeId(0)],
_ => invalid.functions[2].lifecycle_role = LifecycleRole::BeforePause,
}
assert_eq!(resolve(invalid).unwrap_err().reason, LoadReason::LifecycleSignature);
}
}
+75 -9
View File
@@ -1,4 +1,5 @@
#![allow(clippy::collapsible_if)]
mod resumable;
use crate::memory_banks::{GlyphBankPoolInstaller, SceneBankPoolInstaller, SoundBankPoolInstaller};
use prometeu_hal::AssetBridge;
use prometeu_hal::asset::{
@@ -340,6 +341,8 @@ pub struct AssetManager {
// Commits that are ready to be applied at the next frame boundary.
pending_commits: Mutex<Vec<(HandleId, u64)>>,
preload_handles: Mutex<Vec<HandleId>>,
preload_failed: std::sync::atomic::AtomicBool,
}
struct LoadHandleInfo {
@@ -374,6 +377,7 @@ struct AssetLoadQueueState {
pending: VecDeque<AssetLoadJob>,
active: Option<AssetLoadJob>,
shutdown: bool,
scheduled: bool,
}
#[derive(Default)]
@@ -470,6 +474,7 @@ impl AssetLoadQueue {
struct AssetLoadWorker {
queue: Arc<AssetLoadQueue>,
join_handle: Option<thread::JoinHandle<()>>,
shared: Option<(Arc<dyn prometeu_hal::background_work::BackgroundWork>, resumable::Resources)>,
}
impl AssetLoadWorker {
@@ -500,11 +505,15 @@ impl AssetLoadWorker {
}
});
Self { queue, join_handle: Some(join_handle) }
Self { queue, join_handle: Some(join_handle), shared: None }
}
fn submit_counting_superseded(&self, job: AssetLoadJob) -> usize {
self.queue.submit(job)
let removed = self.queue.submit(job);
if let Some((lane, resources)) = &self.shared {
resumable::schedule(self.queue.clone(), lane.clone(), resources.clone());
}
removed
}
fn clear_pending(&self) {
@@ -544,6 +553,25 @@ enum AssetOpMode {
}
impl AssetBridge for AssetManager {
fn bind_background_work(
&mut self,
lane: Arc<dyn prometeu_hal::background_work::BackgroundWork>,
) -> bool {
if self.load_worker.shared.as_ref().is_some_and(|(bound, _)| Arc::ptr_eq(bound, &lane)) {
return true;
}
// Bind only before loading: replacing an active worker could block the main lane.
if self.load_worker.pending_count() != 0 || self.load_worker.active_job().is_some() {
return false;
}
let resources = resumable::Resources::new(self);
self.load_worker = AssetLoadWorker {
queue: Arc::new(AssetLoadQueue::default()),
join_handle: None,
shared: Some((lane, resources)),
};
true
}
fn initialize_for_cartridge(
&self,
assets: Vec<AssetEntry>,
@@ -552,6 +580,12 @@ impl AssetBridge for AssetManager {
) {
self.initialize_for_cartridge(assets, preload, assets_data)
}
fn preloads_ready(&self) -> Result<bool, AssetLoadError> {
if self.preload_failed.load(std::sync::atomic::Ordering::Acquire) {
return Err(AssetLoadError::BackendError);
}
Ok(self.preload_handles.lock().unwrap().is_empty())
}
fn load(&self, asset_id: AssetId, slot_index: usize) -> Result<HandleId, AssetLoadError> {
self.load(asset_id, slot_index)
}
@@ -715,6 +749,8 @@ impl AssetManager {
pipeline_telemetry,
load_worker,
pending_commits: Mutex::new(Vec::new()),
preload_handles: Mutex::new(Vec::new()),
preload_failed: std::sync::atomic::AtomicBool::new(false),
}
}
@@ -734,7 +770,16 @@ impl AssetManager {
}
*self.assets_data.write().unwrap() = assets_data;
// Perform Preload for assets in the preload list
if self.load_worker.shared.is_some() {
for item in preload {
match self.load(item.asset_id, item.slot) {
Ok(handle) => self.preload_handles.lock().unwrap().push(handle),
Err(_) => self.preload_failed.store(true, std::sync::atomic::Ordering::SeqCst),
}
}
return;
}
// Standalone bridges retain their synchronous preload compatibility path.
for item in preload {
let entry_opt = {
let assets = self.assets.read().unwrap();
@@ -1709,6 +1754,19 @@ impl AssetManager {
}
pub fn apply_commits(&self) {
self.preload_handles.lock().unwrap().retain(|&handle| match self.status(handle) {
LoadStatus::READY => {
self.commit(handle);
false
}
LoadStatus::QUEUED | LoadStatus::LOADING | LoadStatus::PENDING | LoadStatus::ACTIVE => {
true
}
_ => {
self.preload_failed.store(true, std::sync::atomic::Ordering::SeqCst);
false
}
});
let mut pending = self.pending_commits.lock().unwrap();
let mut handles = self.handles.write().unwrap();
@@ -1846,16 +1904,20 @@ impl AssetManager {
}
pub fn shutdown(&self) {
// Exclude worker publication while invalidating and clearing the cartridge.
let mut generations = self.target_generations.write().unwrap();
for generation in generations.values_mut() {
*generation += 1;
}
self.gfx_policy.clear();
self.sound_policy.clear();
self.scene_policy.clear();
self.handles.write().unwrap().clear();
self.target_handles.write().unwrap().clear();
for generation in self.target_generations.write().unwrap().values_mut() {
*generation += 1;
}
self.load_worker.clear_pending();
self.pending_commits.lock().unwrap().clear();
self.preload_handles.lock().unwrap().clear();
self.preload_failed.store(false, std::sync::atomic::Ordering::SeqCst);
self.gfx_slots.write().unwrap().fill(None);
self.glyph_slot_index.clear();
self.sound_slots.write().unwrap().fill(None);
@@ -1936,7 +1998,7 @@ mod tests {
}
}
fn test_scene() -> SceneBank {
pub(super) fn test_scene() -> SceneBank {
let make_layer = |glyph_asset_id: AssetId,
active: bool,
parallax_x: f32,
@@ -1999,7 +2061,7 @@ mod tests {
.sum()
}
fn encode_scene_payload(scene: &SceneBank) -> Vec<u8> {
pub(super) fn encode_scene_payload(scene: &SceneBank) -> Vec<u8> {
let mut data = Vec::new();
data.extend_from_slice(&SCENE_PAYLOAD_MAGIC_V1);
data.extend_from_slice(&SCENE_PAYLOAD_VERSION_V1.to_le_bytes());
@@ -2039,7 +2101,11 @@ mod tests {
data
}
fn test_scene_asset_entry(asset_name: &str, data: &[u8], scene: &SceneBank) -> AssetEntry {
pub(super) fn test_scene_asset_entry(
asset_name: &str,
data: &[u8],
scene: &SceneBank,
) -> AssetEntry {
AssetEntry {
asset_id: 2,
asset_name: asset_name.to_string(),
@@ -0,0 +1,559 @@
//! Bounded decoding steps retain progress while the shared lane services saves.
use super::*;
use prometeu_hal::background_work::{BackgroundWork, WorkStep};
use prometeu_hal::cartridge::AssetsPayloadReader;
#[derive(Clone)]
pub(super) struct Resources {
handles: Arc<RwLock<HandleTable>>,
generations: Arc<RwLock<TargetGenerationTable>>,
source: Arc<RwLock<AssetsPayloadSource>>,
telemetry: Arc<Mutex<AssetPipelineTelemetryState>>,
gfx: BankPolicy<GlyphBank>,
sound: BankPolicy<SoundBank>,
scene: BankPolicy<SceneBank>,
}
impl Resources {
pub(super) fn new(m: &AssetManager) -> Self {
Self {
handles: m.handles.clone(),
generations: m.target_generations.clone(),
source: m.assets_data.clone(),
telemetry: m.pipeline_telemetry.clone(),
gfx: m.gfx_policy.clone(),
sound: m.sound_policy.clone(),
scene: m.scene_policy.clone(),
}
}
fn current(&self, job: &AssetLoadJob) -> bool {
AssetManager::is_current_target_generation(
&self.generations,
job.slot,
job.request_generation,
)
}
}
pub(super) fn schedule(
queue: Arc<AssetLoadQueue>,
lane: Arc<dyn BackgroundWork>,
resources: Resources,
) {
{
let mut state = queue.state.lock().unwrap();
if state.scheduled || state.shutdown {
return;
}
state.scheduled = true;
}
let owner = queue.clone();
let failure_queue = queue.clone();
let failures = resources.clone();
let mut active: Option<(AssetLoadJob, Decoder, Instant)> = None;
if lane
.submit_asset(Box::new(move |canceled| {
if canceled || owner.state.lock().unwrap().shutdown {
let mut state = owner.state.lock().unwrap();
state.scheduled = false;
state.active = None;
return WorkStep::Failed;
}
if active.is_none() {
let job = {
let mut state = owner.state.lock().unwrap();
match state.pending.pop_front() {
Some(job) => {
state.active = Some(job.clone());
job
}
None => {
state.scheduled = false;
return WorkStep::Complete;
}
}
};
if !resources.current(&job) {
resources.telemetry.lock().unwrap().record_stale_discard();
owner.complete_active(job.handle_id, job.request_generation);
return WorkStep::Yield;
}
if let Some(handle) = resources.handles.write().unwrap().get_mut(&job.handle_id) {
handle.status = LoadStatus::LOADING;
handle.progress = ASSET_PROGRESS_LOADING;
}
let start = Instant::now();
resources.telemetry.lock().unwrap().record_started(0);
match Decoder::new(&job.entry, &resources.source) {
Ok(decoder) => active = Some((job, decoder, start)),
Err(_) => {
AssetManager::complete_load_job(
&resources.handles,
&job,
LoadStatus::ERROR,
);
AssetManager::record_load_job_finished(
&resources.telemetry,
&job,
start,
LoadStatus::ERROR,
);
owner.complete_active(job.handle_id, job.request_generation);
return WorkStep::Yield;
}
}
return WorkStep::Yield;
}
let (job, decoder, start) = active.as_mut().unwrap();
if !resources.current(job) {
resources.telemetry.lock().unwrap().record_stale_discard();
owner.complete_active(job.handle_id, job.request_generation);
active = None;
return WorkStep::Yield;
}
match decoder.step() {
Ok(None) => WorkStep::Yield,
outcome => {
let generations = resources.generations.read().unwrap();
let status = if let Ok(Some(bank)) = outcome {
if generations.get(&job.slot).copied().unwrap_or(0)
== job.request_generation
{
match bank {
Decoded::Glyph(bank) => {
let resident = resources.gfx.put_resident(
job.asset_id,
Arc::new(bank),
job.entry.decoded_size as usize,
);
resources.gfx.stage(
job.handle_id,
resident,
job.entry.decoded_size as usize,
);
}
Decoded::Sound(bank) => {
let resident = resources.sound.put_resident(
job.asset_id,
Arc::new(bank),
job.entry.decoded_size as usize,
);
resources.sound.stage(
job.handle_id,
resident,
job.entry.decoded_size as usize,
);
}
Decoded::Scene(bank) => {
let resident = resources.scene.put_resident(
job.asset_id,
Arc::new(bank),
job.entry.decoded_size as usize,
);
resources.scene.stage(
job.handle_id,
resident,
job.entry.decoded_size as usize,
);
}
}
LoadStatus::READY
} else {
LoadStatus::ERROR
}
} else {
LoadStatus::ERROR
};
drop(generations);
AssetManager::complete_load_job(&resources.handles, job, status);
AssetManager::record_load_job_finished(
&resources.telemetry,
job,
*start,
status,
);
owner.complete_active(job.handle_id, job.request_generation);
active = None;
WorkStep::Yield
}
}
}))
.is_err()
{
let mut state = failure_queue.state.lock().unwrap();
state.scheduled = false;
for job in state.pending.drain(..) {
AssetManager::complete_load_job(&failures.handles, &job, LoadStatus::ERROR);
}
}
}
enum Decoded {
Glyph(GlyphBank),
Sound(SoundBank),
Scene(SceneBank),
}
enum State {
Glyph {
bank: GlyphBank,
packed: usize,
position: usize,
palette_bytes: usize,
palette_position: usize,
},
Sound {
sample_rate: u32,
data: Vec<i16>,
remaining: usize,
},
Scene {
layers: [SceneLayer; SCENE_LAYER_COUNT_V1],
layer: usize,
tiles_left: usize,
header: bool,
decoded: usize,
},
Done,
}
struct Decoder {
reader: AssetsPayloadReader,
entry: AssetEntry,
state: State,
position: usize,
}
impl Decoder {
fn new(entry: &AssetEntry, source: &Arc<RwLock<AssetsPayloadSource>>) -> Result<Self, String> {
let reader = source
.read()
.unwrap()
.open_slice(entry.offset, entry.size)
.map_err(|e| e.to_string())?
.open_reader()
.map_err(|e| e.to_string())?;
let state = match entry.bank_type {
BankType::GLYPH => {
let (tile_size, width, height, packed, palette_count, palette_bytes) =
AssetManager::decode_glyph_bank_layout(entry)?;
State::Glyph {
bank: GlyphBank {
tile_size,
width,
height,
pixel_indices: Vec::with_capacity(width * height),
palettes: vec![[Color::BLACK; 16]; palette_count],
},
packed,
position: 0,
palette_bytes,
palette_position: 0,
}
}
BankType::SOUNDS => {
let sample_rate = entry.metadata_as_sound_bank()?.sample_rate;
State::Sound {
sample_rate,
data: Vec::with_capacity(entry.size as usize / 2),
remaining: entry.size as usize,
}
}
BankType::SCENE => {
entry.metadata_as_scene_bank()?;
State::Scene {
layers: std::array::from_fn(|_| SceneLayer {
active: false,
glyph_asset_id: 0,
tile_size: TileSize::Size8,
parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 },
tilemap: TileMap { width: 0, height: 0, tiles: Vec::new() },
}),
layer: 0,
tiles_left: 0,
header: false,
decoded: 0,
}
}
};
Ok(Self { reader, entry: entry.clone(), state, position: 0 })
}
fn step(&mut self) -> Result<Option<Decoded>, String> {
let mut bytes = [0; 4096];
match &mut self.state {
State::Glyph { bank, packed, position, palette_bytes, palette_position } => {
if *position < *packed {
let n = (*packed - *position).min(bytes.len());
self.reader.read_exact(&mut bytes[..n]).map_err(|e| e.to_string())?;
for &v in &bytes[..n] {
if bank.pixel_indices.len() < bank.width * bank.height {
bank.pixel_indices.push(v >> 4)
}
if bank.pixel_indices.len() < bank.width * bank.height {
bank.pixel_indices.push(v & 15)
}
}
*position += n;
return Ok(None);
}
if *palette_position < *palette_bytes {
let n = (*palette_bytes - *palette_position).min(bytes.len());
self.reader.read_exact(&mut bytes[..n]).map_err(|e| e.to_string())?;
for (i, rgba) in bytes[..n].as_chunks::<4>().0.iter().enumerate() {
let at = *palette_position / 4 + i;
bank.palettes[at / 16][at % 16] =
Color::rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
}
*palette_position += n;
return Ok(None);
}
let State::Glyph { bank, .. } = std::mem::replace(&mut self.state, State::Done)
else {
unreachable!()
};
Ok(Some(Decoded::Glyph(bank)))
}
State::Sound { sample_rate, data, remaining } => {
if *remaining > 0 {
let n = (*remaining).min(bytes.len());
self.reader.read_exact(&mut bytes[..n]).map_err(|e| e.to_string())?;
for pair in bytes[..n].as_chunks::<2>().0 {
data.push(i16::from_le_bytes([pair[0], pair[1]]));
}
*remaining -= n;
return Ok(None);
}
let rate = *sample_rate;
let data = std::mem::take(data);
self.state = State::Done;
Ok(Some(Decoded::Sound(SoundBank::new(vec![Arc::new(Sample::new(rate, data))]))))
}
State::Scene { layers, layer, tiles_left, header, decoded } => {
if !*header {
self.reader
.read_exact(&mut bytes[..SCENE_HEADER_BYTES_V1])
.map_err(|e| e.to_string())?;
if bytes[..4] != SCENE_PAYLOAD_MAGIC_V1
|| u16::from_le_bytes([bytes[4], bytes[5]]) != SCENE_PAYLOAD_VERSION_V1
|| u16::from_le_bytes([bytes[6], bytes[7]]) as usize != SCENE_LAYER_COUNT_V1
{
return Err("Invalid scene header".into());
}
self.position += SCENE_HEADER_BYTES_V1;
*header = true;
return Ok(None);
}
if *tiles_left > 0 {
let n = (*tiles_left).min(bytes.len() / SCENE_TILE_RECORD_BYTES_V1);
let count = n * SCENE_TILE_RECORD_BYTES_V1;
self.reader.read_exact(&mut bytes[..count]).map_err(|e| e.to_string())?;
for b in bytes[..count].as_chunks::<SCENE_TILE_RECORD_BYTES_V1>().0 {
layers[*layer - 1].tilemap.tiles.push(Tile {
active: b[0] & 1 != 0,
glyph: Glyph {
glyph_id: u16::from_le_bytes([b[2], b[3]]),
palette_id: b[1],
},
flip_x: b[0] & 2 != 0,
flip_y: b[0] & 4 != 0,
});
}
*tiles_left -= n;
self.position += count;
return Ok(None);
}
if *layer < layers.len() {
self.reader
.read_exact(&mut bytes[..SCENE_LAYER_HEADER_BYTES_V1])
.map_err(|e| e.to_string())?;
self.position += SCENE_LAYER_HEADER_BYTES_V1;
let u32at = |at| u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap());
let width = u32at(16) as usize;
let height = u32at(20) as usize;
let count = u32at(24) as usize;
let tile_size = match bytes[5] {
8 => TileSize::Size8,
16 => TileSize::Size16,
32 => TileSize::Size32,
_ => return Err("Invalid scene tile size".into()),
};
let x = f32::from_le_bytes(bytes[8..12].try_into().unwrap());
let y = f32::from_le_bytes(bytes[12..16].try_into().unwrap());
if !x.is_finite()
|| !y.is_finite()
|| width.checked_mul(height) != Some(count)
|| count
.checked_mul(SCENE_TILE_RECORD_BYTES_V1)
.and_then(|n| n.checked_add(self.position))
.is_none_or(|n| n as u64 > self.entry.size)
{
return Err("Invalid scene layer range".into());
}
*decoded = decoded
.checked_add(SCENE_DECODED_LAYER_OVERHEAD_BYTES_V1)
.and_then(|n| {
count.checked_mul(size_of::<Tile>()).and_then(|b| n.checked_add(b))
})
.ok_or("Scene size overflow")?;
layers[*layer] = SceneLayer {
active: bytes[0] & 1 != 0,
glyph_asset_id: i32::from_le_bytes(bytes[1..5].try_into().unwrap()),
tile_size,
parallax_factor: ParallaxFactor { x, y },
tilemap: TileMap { width, height, tiles: Vec::with_capacity(count) },
};
*layer += 1;
*tiles_left = count;
return Ok(None);
}
if self.position as u64 != self.entry.size
|| *decoded as u64 != self.entry.decoded_size
{
return Err("Invalid scene payload size".into());
}
let State::Scene { layers, .. } = std::mem::replace(&mut self.state, State::Done)
else {
unreachable!()
};
Ok(Some(Decoded::Scene(SceneBank { layers })))
}
State::Done => Err("Decoder already complete".into()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use prometeu_hal::background_work::{AssetWork, WorkUnavailable};
#[derive(Default)]
struct ManualLane(Mutex<VecDeque<AssetWork>>);
impl BackgroundWork for ManualLane {
fn submit_asset(&self, work: AssetWork) -> Result<(), WorkUnavailable> {
self.0.lock().unwrap().push_back(work);
Ok(())
}
}
impl ManualLane {
fn step(&self) -> bool {
let Some(mut work) = self.0.lock().unwrap().pop_front() else { return false };
if work(false) == WorkStep::Yield {
self.0.lock().unwrap().push_back(work);
}
true
}
}
#[test]
fn shared_decoder_yields_and_matches_existing_glyph_format() {
let mut data = vec![0x12; 32];
data.extend([255; 64]);
let entry = AssetEntry {
asset_id: 7,
asset_name: "glyph".into(),
bank_type: BankType::GLYPH,
codec: AssetCodec::None,
offset: 0,
size: data.len() as u64,
decoded_size: 128,
metadata: serde_json::json!({"tile_size":8,"width":8,"height":8,"palette_count":1,"palette_authored":1}),
};
let expected = AssetManager::decode_glyph_bank_from_buffer(&entry, &data).unwrap();
let banks = Arc::new(crate::memory_banks::MemoryBanks::new());
let mut manager = AssetManager::new(
vec![entry],
AssetsPayloadSource::from_bytes(data),
banks.clone(),
banks.clone(),
banks,
);
let lane = Arc::new(ManualLane::default());
assert!(manager.bind_background_work(lane.clone()));
let handle = manager.load(7, 0).unwrap();
let mut steps = 0;
while lane.step() {
steps += 1;
assert!(steps < 20);
}
assert!(steps >= 4);
assert_eq!(manager.status(handle), LoadStatus::READY);
let staging = manager.gfx_policy.staging.read().unwrap();
let (actual, _) = staging.get(&handle).unwrap();
assert_eq!(actual.pixel_indices, expected.pixel_indices);
assert_eq!(actual.palettes, expected.palettes);
}
fn decode_in_steps(entry: &AssetEntry, bytes: Vec<u8>) -> Decoded {
let source = Arc::new(RwLock::new(AssetsPayloadSource::from_bytes(bytes)));
let mut decoder = Decoder::new(entry, &source).unwrap();
for _ in 0..100 {
if let Some(result) = decoder.step().unwrap() {
return result;
}
}
panic!("decoder did not finish")
}
#[test]
fn resumable_sound_and_scene_preserve_existing_formats() {
let pcm: Vec<i16> = (0..5000).map(|n| n as i16 - 2500).collect();
let bytes: Vec<u8> = pcm.iter().flat_map(|n| n.to_le_bytes()).collect();
let entry = AssetEntry {
asset_id: 1,
asset_name: "pcm".into(),
bank_type: BankType::SOUNDS,
offset: 0,
size: bytes.len() as u64,
decoded_size: bytes.len() as u64,
codec: AssetCodec::None,
metadata: serde_json::json!({"sample_rate":44100,"channels":1}),
};
let Decoded::Sound(bank) = decode_in_steps(&entry, bytes) else { panic!() };
assert_eq!(bank.samples[0].data, pcm);
let scene = crate::asset::tests::test_scene();
let bytes = crate::asset::tests::encode_scene_payload(&scene);
let entry = crate::asset::tests::test_scene_asset_entry("scene", &bytes, &scene);
let Decoded::Scene(actual) = decode_in_steps(&entry, bytes) else { panic!() };
for (a, b) in actual.layers.iter().zip(scene.layers.iter()) {
assert_eq!(a.active, b.active);
assert_eq!(a.glyph_asset_id, b.glyph_asset_id);
assert_eq!(a.tile_size, b.tile_size);
assert_eq!(a.parallax_factor.x, b.parallax_factor.x);
assert_eq!(a.tilemap.width, b.tilemap.width);
assert_eq!(a.tilemap.height, b.tilemap.height);
assert_eq!(a.tilemap.tiles.len(), b.tilemap.tiles.len());
for (x, y) in a.tilemap.tiles.iter().zip(&b.tilemap.tiles) {
assert_eq!(
(x.active, x.glyph.glyph_id, x.glyph.palette_id, x.flip_x, x.flip_y),
(y.active, y.glyph.glyph_id, y.glyph.palette_id, y.flip_x, y.flip_y)
);
}
}
}
#[test]
fn shared_preload_waits_for_worker_and_frame_publication() {
let bytes = vec![0; 8192];
let entry = AssetEntry {
asset_id: 1,
asset_name: "pcm".into(),
bank_type: BankType::SOUNDS,
offset: 0,
size: bytes.len() as u64,
decoded_size: bytes.len() as u64,
codec: AssetCodec::None,
metadata: serde_json::json!({"sample_rate":44100,"channels":1}),
};
let banks = Arc::new(crate::memory_banks::MemoryBanks::new());
let mut manager = AssetManager::new(
vec![],
AssetsPayloadSource::empty(),
banks.clone(),
banks.clone(),
banks,
);
let lane = Arc::new(ManualLane::default());
assert!(manager.bind_background_work(lane.clone()));
manager.initialize_for_cartridge(
vec![entry],
vec![PreloadEntry { asset_id: 1, slot: 0 }],
AssetsPayloadSource::from_bytes(bytes),
);
assert_eq!(manager.preloads_ready(), Ok(false));
while lane.step() {}
assert_eq!(manager.preloads_ready(), Ok(false));
manager.apply_commits();
assert_eq!(manager.preloads_ready(), Ok(true));
assert_eq!(manager.slot_info(SlotRef::audio(0)).asset_id, Some(1));
}
}
@@ -22,3 +22,5 @@ pub use crate::memory_banks::{
pub use crate::pad::Pad;
pub use crate::test_platform::TestPlatform;
pub use crate::wall_clock::ControlledWallClock;
pub mod memcard;
@@ -0,0 +1,119 @@
//! File-host implementation. IO runs only on the shared worker, never the guest lane.
use prometeu_hal::memcard::{MemcardIoError as Error, MemcardStorage};
use std::fs::{File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path;
pub struct FileMemcard {
file: File,
length: u64,
read_only: bool,
}
fn io_error(e: std::io::Error) -> Error {
use std::io::ErrorKind as K;
match e.kind() {
K::NotFound => Error::Unavailable,
K::PermissionDenied => Error::ReadOnly,
K::StorageFull => Error::NoSpace,
K::InvalidInput | K::UnexpectedEof => Error::InvalidRange,
K::Unsupported => Error::Unsupported,
_ => Error::IoError,
}
}
impl FileMemcard {
pub fn open(path: &Path, read_only: bool) -> Result<Self, Error> {
let file = OpenOptions::new().read(true).write(!read_only).open(path).map_err(io_error)?;
let length = file.metadata().map_err(io_error)?.len();
Ok(Self { file, length, read_only })
}
/// Write every byte and verify allocated blocks before formatting/publication.
/// The initializer supplies the runtime-owned format. Existing files are never replaced.
#[cfg(unix)]
pub fn create(
path: &Path,
length: u64,
initialize: impl FnOnce(&mut dyn MemcardStorage, [u8; 16]) -> Result<(), Error>,
) -> Result<Self, Error> {
use std::os::unix::fs::MetadataExt;
let mut id = [0; 16];
File::open("/dev/urandom").and_then(|mut f| f.read_exact(&mut id)).map_err(io_error)?;
let parent =
path.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or_else(|| Path::new("."));
let suffix: String = id.iter().map(|b| format!("{b:02x}")).collect();
let temporary = parent.join(format!(".memcard-{suffix}.tmp"));
let file = OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(&temporary)
.map_err(io_error)?;
let result = (|| {
let mut card = Self { file, length, read_only: false };
let zeros = [0u8; 65_536];
let mut offset = 0;
while offset < length {
let n = (length - offset).min(zeros.len() as u64) as usize;
card.file.write_all(&zeros[..n]).map_err(io_error)?;
offset += n as u64;
}
card.sync()?;
if card.file.metadata().map_err(io_error)?.blocks().saturating_mul(512) < length {
return Err(Error::Unsupported);
}
initialize(&mut card, id)?;
card.sync()?;
// hard_link publishes without replacing a user file; parent sync makes the name durable.
std::fs::hard_link(&temporary, path).map_err(io_error)?;
File::open(parent).and_then(|f| f.sync_all()).map_err(io_error)?;
Ok(card)
})();
let cleanup =
std::fs::remove_file(&temporary).and_then(|()| File::open(parent)?.sync_all());
match result {
Ok(card) => {
cleanup.map_err(io_error)?;
Ok(card)
}
Err(e) => Err(e),
}
}
#[cfg(not(unix))]
pub fn create(
_path: &Path,
_length: u64,
_initialize: impl FnOnce(&mut dyn MemcardStorage, [u8; 16]) -> Result<(), Error>,
) -> Result<Self, Error> {
Err(Error::Unsupported)
}
fn check(&self, offset: u64, length: usize) -> Result<(), Error> {
if length > 65_536 || offset.checked_add(length as u64).is_none_or(|end| end > self.length)
{
return Err(Error::InvalidRange);
}
Ok(())
}
}
impl MemcardStorage for FileMemcard {
fn len(&self) -> Result<u64, Error> {
Ok(self.length)
}
fn read_only(&self) -> bool {
self.read_only
}
fn read_at(&mut self, offset: u64, target: &mut [u8]) -> Result<(), Error> {
self.check(offset, target.len())?;
self.file.seek(SeekFrom::Start(offset)).map_err(io_error)?;
self.file.read_exact(target).map_err(io_error)
}
fn write_at(&mut self, offset: u64, source: &[u8]) -> Result<(), Error> {
if self.read_only {
return Err(Error::ReadOnly);
}
self.check(offset, source.len())?;
self.file.seek(SeekFrom::Start(offset)).map_err(io_error)?;
self.file.write_all(source).map_err(io_error)
}
fn sync(&mut self) -> Result<(), Error> {
self.file.sync_all().map_err(io_error)
}
}
@@ -61,6 +61,32 @@ impl Firmware {
/// This method is called exactly once per Host frame (60Hz).
/// It updates peripheral signals and delegates the logic to the current state.
pub fn tick(&mut self, signals: &InputSignals, platform: &mut dyn RuntimePlatform) {
self.os.poll_background_io(platform);
if self.os.memcard().host_faulted() {
// Keep the resident VM, snapshots, and worker-owned buffers alive. A
// stuck host operation cannot be repaired by launching another worker.
self.state = FirmwareState::MemoryIoFault;
self.state_initialized = true;
self.on_update(signals, platform);
return;
}
if self.os.memcard().session.removal_pending()
&& let Some(task) = self.os.lifecycle().resident_game_task()
{
if self.os.lifecycle().task_state(task)
== Some(prometeu_system::task::TaskState::Suspended)
{
if !matches!(
self.state,
FirmwareState::MemoryCardRemoved(_) | FirmwareState::ConfirmDiscard(_)
) {
self.state = FirmwareState::MemoryCardRemoved(Default::default());
self.state_initialized = true;
}
} else {
self.request_home_from_host();
}
}
let barriered_signals = InputSignals::default();
let effective_signals = if self.game_input_barrier_frames > 0 {
self.game_input_barrier_frames -= 1;
@@ -167,8 +193,10 @@ impl Firmware {
FirmwareState::SplashScreen(s) => s.on_enter(&mut req),
FirmwareState::LaunchHub(s) => s.on_enter(&mut req),
FirmwareState::HubHome(s) => s.on_enter(&mut req),
FirmwareState::ConfirmDiscard(s) => s.on_enter(&mut req),
FirmwareState::MemoryCardRemoved(s) => s.on_enter(&mut req),
FirmwareState::LoadCartridge(s) => s.on_enter(&mut req),
FirmwareState::ResumeResidentGame => {}
FirmwareState::ResumeResidentGame | FirmwareState::MemoryIoFault => {}
FirmwareState::GameRunning(s) => s.on_enter(&mut req),
FirmwareState::ShellRunning(s) => s.on_enter(&mut req),
FirmwareState::AppCrashes(s) => s.on_enter(&mut req),
@@ -194,8 +222,32 @@ impl Firmware {
FirmwareState::SplashScreen(s) => s.on_update(&mut req),
FirmwareState::LaunchHub(s) => s.on_update(&mut req),
FirmwareState::HubHome(s) => s.on_update(&mut req),
FirmwareState::ConfirmDiscard(s) => s.on_update(&mut req),
FirmwareState::MemoryCardRemoved(s) => s.on_update(&mut req),
FirmwareState::LoadCartridge(s) => s.on_update(&mut req),
FirmwareState::ResumeResidentGame => None,
FirmwareState::MemoryIoFault => {
use prometeu_hal::color::Color;
use prometeu_hal::{FrameId, GfxUiCommand, RenderSubmission, ShellUiFramePacket};
let lines = [
"Memory-card IO did not stop in time.",
"Save outcome may be unknown.",
"Restart the host before using storage again.",
];
let mut commands = vec![GfxUiCommand::Clear { color: Color::BLACK }];
commands.extend(lines.into_iter().enumerate().map(|(i, text)| {
GfxUiCommand::DrawText {
x: 8,
y: 8 + i as i32 * 20,
text: text.into(),
color: Color::WHITE,
}
}));
let _ = req.platform.render_submission_sink().submit_render_submission(
RenderSubmission::shell_ui(FrameId::ZERO, ShellUiFramePacket::new(commands)),
);
None
}
FirmwareState::GameRunning(s) => s.on_update(&mut req),
FirmwareState::ShellRunning(s) => s.on_update(&mut req),
FirmwareState::AppCrashes(s) => s.on_update(&mut req),
@@ -216,8 +268,10 @@ impl Firmware {
FirmwareState::SplashScreen(s) => s.on_exit(&mut req),
FirmwareState::LaunchHub(s) => s.on_exit(&mut req),
FirmwareState::HubHome(s) => s.on_exit(&mut req),
FirmwareState::ConfirmDiscard(s) => s.on_exit(&mut req),
FirmwareState::MemoryCardRemoved(s) => s.on_exit(&mut req),
FirmwareState::LoadCartridge(s) => s.on_exit(&mut req),
FirmwareState::ResumeResidentGame => {}
FirmwareState::ResumeResidentGame | FirmwareState::MemoryIoFault => {}
FirmwareState::GameRunning(s) => s.on_exit(&mut req),
FirmwareState::ShellRunning(s) => s.on_exit(&mut req),
FirmwareState::AppCrashes(s) => s.on_exit(&mut req),
@@ -410,6 +464,7 @@ mod tests {
fn invalid_game_cartridge() -> Cartridge {
Cartridge {
memcard_slots: None,
app_id: 7,
title: "Broken Cart".into(),
app_version: "1.0.0".into(),
@@ -451,6 +506,7 @@ mod tests {
.serialize();
Cartridge {
memcard_slots: None,
app_id: 8,
title: "Trap Cart".into(),
app_version: "1.0.0".into(),
@@ -469,6 +525,7 @@ mod tests {
fn valid_cartridge_with(app_mode: AppMode, app_id: u32, title: &str) -> Cartridge {
Cartridge {
memcard_slots: None,
app_id,
title: title.into(),
app_version: "1.0.0".into(),
@@ -533,6 +590,15 @@ mod tests {
platform.local_hardware().gfx.front_buffer()[index]
}
fn wait_for_cartridge_preload(firmware: &mut Firmware, platform: &mut TestPlatform) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
while matches!(firmware.state, FirmwareState::LoadCartridge(_)) {
assert!(std::time::Instant::now() < deadline, "cartridge admission did not complete");
firmware.tick(&InputSignals::default(), platform);
std::thread::yield_now();
}
}
#[test]
fn load_cartridge_transitions_to_app_crashes_when_vm_init_fails() {
let mut firmware = Firmware::new(None);
@@ -919,6 +985,7 @@ mod tests {
firmware.tick(&stress_click, &mut platform);
firmware.tick(&InputSignals::default(), &mut platform);
wait_for_cartridge_preload(&mut firmware, &mut platform);
let stress_task = match &firmware.state {
FirmwareState::GameRunning(step) => step.task_id,
other => panic!("expected Stress GameRunning state, got {:?}", other),
@@ -1017,6 +1084,7 @@ mod tests {
InputSignals { f_signal: true, x_pos: 72, y_pos: 118, ..Default::default() };
firmware.tick(&stress_click, &mut platform);
wait_for_cartridge_preload(&mut firmware, &mut platform);
assert_eq!(firmware.os.lifecycle().task_state(dummy_task), Some(TaskState::Closed));
assert_eq!(
firmware.os.lifecycle().process_state_for_task(dummy_task),
@@ -1100,6 +1168,7 @@ mod tests {
firmware.tick(&stress_click, &mut platform);
firmware.tick(&InputSignals::default(), &mut platform);
wait_for_cartridge_preload(&mut firmware, &mut platform);
let stress_task = match &firmware.state {
FirmwareState::GameRunning(step) => step.task_id,
other => panic!("expected Stress GameRunning state, got {:?}", other),
@@ -1188,6 +1257,7 @@ mod tests {
firmware.tick(&stress_click, &mut platform);
firmware.tick(&InputSignals::default(), &mut platform);
wait_for_cartridge_preload(&mut firmware, &mut platform);
let stress_task = match &firmware.state {
FirmwareState::GameRunning(step) => step.task_id,
other => panic!("expected Stress GameRunning state, got {:?}", other),
@@ -1972,4 +2042,129 @@ mod tests {
);
}
}
#[test]
fn memory_only_admission_runs_typed_guest_boot_and_warns_before_discard() {
for debug in [false, true] {
use prometeu_bytecode::{
load_limits::LoadBudget,
model::{ConstantPoolEntry, SyscallDecl},
registry::{
LifecycleRole, NamedType, Registry, RegistryDeclarations, Signature, TypeDecl,
TypeId, TypeKind,
},
registry_codec::PbxModule,
};
let boot = assemble("HOSTCALL 0\nPOP_N 1\nFRAME_SYNC\nJMP 12").unwrap();
let hook=assemble("PUSH_I64 560\nARRAY_NEW 9\nSET_GLOBAL 0\nGET_LOCAL 0\nPUSH_CONST 0\nPUSH_I32 1\nPUSH_I32 1\nPUSH_I32 1\nGET_GLOBAL 0\nPUSH_I32 0\nPUSH_I32 560\nHOSTCALL 1\nPOP_N 1\nGET_LOCAL 0\nPUSH_I32 0\nPUSH_I32 0\nGET_GLOBAL 0\nPUSH_I32 0\nPUSH_I32 560\nHOSTCALL 2\nPOP_N 2\nGET_LOCAL 0\nPUSH_I32 0\nHOSTCALL 3\nPOP_N 2\nPUSH_I32 0\nRET").unwrap();
let mut code = boot.clone();
code.extend(&hook);
let mut types = prometeu_test_support::program::scalar_catalog();
types.push(TypeDecl { name: None, kind: TypeKind::Array(TypeId(1)) });
let registry = Registry::resolve(
RegistryDeclarations {
types,
functions: vec![
FunctionDecl {
code_len: boot.len() as u32,
max_stack_slots: 16,
..Default::default()
},
FunctionDecl {
lifecycle_role: LifecycleRole::MemcardBoot,
code_offset: boot.len() as u32,
code_len: hook.len() as u32,
max_stack_slots: 16,
signature: Signature {
parameters: vec![NamedType { type_id: TypeId(0), name: None }],
results: vec![TypeId(1)],
},
..Default::default()
},
],
implementations: vec![],
},
code.len(),
&mut LoadBudget::new(Default::default()),
)
.unwrap();
let syscalls = [
("bootstrap", 0, 1),
("presentation", 8, 1),
("slot_write", 6, 2),
("slot_commit", 2, 2),
]
.map(|(name, arg_slots, ret_slots)| SyscallDecl {
module: "mem".into(),
name: name.into(),
version: 1,
arg_slots,
ret_slots,
});
let constants = [ConstantPoolEntry::String("Guest save".into())];
let program = PbxModule {
registry: &registry,
code: &code,
constants: &constants,
debug: None,
exports: &[],
syscalls: &syscalls,
}
.serialize(&mut LoadBudget::new(Default::default()))
.unwrap();
let mut cartridge = valid_cartridge(AppMode::Game);
cartridge.program = program;
cartridge.memcard_slots = Some(1);
cartridge.capabilities = prometeu_hal::syscalls::caps::MEMCARD;
let mut firmware = Firmware::new(None);
let mut platform = TestPlatform::new();
firmware.load_cartridge(cartridge);
firmware.tick(&InputSignals::default(), &mut platform);
assert!(matches!(firmware.state, FirmwareState::LoadCartridge(_)));
firmware.tick(&InputSignals { x_signal: true, ..Default::default() }, &mut platform);
wait_for_cartridge_preload(&mut firmware, &mut platform);
if debug {
firmware.os.vm().set_paused(true);
for _ in 0..80 {
assert!(firmware.os.vm().debug_step_active_session(&mut platform).is_none());
if !firmware.os.memcard().session.is_boot_active() {
break;
}
}
assert!(!firmware.os.memcard().session.is_boot_active());
firmware.os.vm().set_paused(false);
}
for _ in 0..4 {
firmware.tick(&InputSignals::default(), &mut platform);
}
assert!(matches!(firmware.state, FirmwareState::GameRunning(_)));
let session = &mut firmware.os.memcard().session;
let context = session.context();
assert_eq!(session.mode(), Some(prometeu_system::memcard::session::Mode::MemoryOnly));
assert_eq!(session.presentation(context).unwrap().unwrap().title, "Guest save");
assert_eq!(session.descriptor(context, 0).unwrap().1, 560);
assert!(session.unsaved());
firmware.request_home_from_host();
firmware.tick(&InputSignals::default(), &mut platform);
firmware.change_state(
FirmwareState::ConfirmDiscard(
super::super::firmware_state::ConfirmDiscardStep::new(None),
),
&InputSignals::default(),
&mut platform,
);
firmware.tick(&InputSignals::default(), &mut platform);
firmware.tick(&InputSignals { b_signal: true, ..Default::default() }, &mut platform);
assert!(firmware.os.memcard().session.unsaved());
firmware.change_state(
FirmwareState::ConfirmDiscard(
super::super::firmware_state::ConfirmDiscardStep::new(None),
),
&InputSignals::default(),
&mut platform,
);
firmware.tick(&InputSignals::default(), &mut platform);
firmware.tick(&InputSignals { a_signal: true, ..Default::default() }, &mut platform);
assert_eq!(firmware.os.memcard().session.mode(), None);
}
}
}
@@ -1,3 +1,4 @@
pub use crate::firmware::firmware_step_confirm_discard::ConfirmDiscardStep;
pub use crate::firmware::firmware_step_crash_screen::AppCrashesStep;
pub use crate::firmware::firmware_step_game_running::GameRunningStep;
pub use crate::firmware::firmware_step_hub_home::HubHomeStep;
@@ -13,8 +14,11 @@ pub enum FirmwareState {
SplashScreen(SplashScreenStep),
LaunchHub(LaunchHubStep),
HubHome(HubHomeStep),
ConfirmDiscard(ConfirmDiscardStep),
LoadCartridge(LoadCartridgeStep),
ResumeResidentGame,
MemoryIoFault,
MemoryCardRemoved(super::firmware_step_confirm_discard::MemoryCardRemovedStep),
GameRunning(GameRunningStep),
ShellRunning(ShellRunningStep),
AppCrashes(AppCrashesStep),
@@ -0,0 +1,101 @@
use super::{
firmware_state::{FirmwareState, HubHomeStep, LoadCartridgeStep},
prometeu_context::PrometeuContext,
resident_game_termination,
};
use prometeu_hal::{
FrameId, GfxUiCommand, RenderSubmission, ShellUiFramePacket, cartridge::Cartridge, color::Color,
};
/// Player-owned exit decision. It never starts a save implicitly.
#[derive(Debug, Clone)]
pub struct ConfirmDiscardStep {
next: Option<Cartridge>,
released: bool,
}
impl ConfirmDiscardStep {
pub fn new(next: Option<Cartridge>) -> Self {
Self { next, released: false }
}
pub fn on_enter(&mut self, _ctx: &mut PrometeuContext) {}
pub fn on_exit(&mut self, _ctx: &mut PrometeuContext) {}
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
let lines = [
"Unsaved progress",
"Leaving this game will discard RAM saves.",
"A: Discard and leave",
"B: Return to Home without leaving the game",
];
let mut commands = vec![GfxUiCommand::Clear { color: Color::BLACK }];
commands.extend(lines.into_iter().enumerate().map(|(i, text)| GfxUiCommand::DrawText {
x: 8,
y: 8 + i as i32 * 20,
text: text.into(),
color: Color::WHITE,
}));
let _ = ctx.platform.render_submission_sink().submit_render_submission(
RenderSubmission::shell_ui(FrameId::ZERO, ShellUiFramePacket::new(commands)),
);
if !ctx.signals.a_signal && !ctx.signals.b_signal {
self.released = true;
}
if self.released && ctx.signals.b_signal {
return Some(FirmwareState::HubHome(HubHomeStep));
}
if self.released && ctx.signals.a_signal {
if resident_game_termination::terminate_resident_game(ctx.os, ctx.platform, true)
.is_err()
{
return None;
}
return Some(match self.next.take() {
Some(card) => {
FirmwareState::LoadCartridge(LoadCartridgeStep::new_game_switch(card))
}
None => FirmwareState::HubHome(HubHomeStep),
});
}
None
}
}
#[derive(Debug, Clone, Default)]
pub struct MemoryCardRemovedStep {
released: bool,
}
impl MemoryCardRemovedStep {
pub fn on_enter(&mut self, _ctx: &mut PrometeuContext) {}
pub fn on_exit(&mut self, _ctx: &mut PrometeuContext) {}
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
if !ctx.os.memcard().session.removal_pending() {
return Some(FirmwareState::HubHome(HubHomeStep));
}
let lines = [
"Memory card is unavailable.",
"Progress remains in memory.",
"Reinsert a card through the host, or choose:",
"A: Continue without saving to disk",
"B: Leave the game",
];
let mut commands = vec![GfxUiCommand::Clear { color: Color::BLACK }];
commands.extend(lines.into_iter().enumerate().map(|(i, text)| GfxUiCommand::DrawText {
x: 8,
y: 8 + i as i32 * 20,
text: text.into(),
color: Color::WHITE,
}));
let _ = ctx.platform.render_submission_sink().submit_render_submission(
RenderSubmission::shell_ui(FrameId::ZERO, ShellUiFramePacket::new(commands)),
);
if !ctx.signals.a_signal && !ctx.signals.b_signal {
self.released = true;
}
if self.released && ctx.signals.a_signal && ctx.os.detach_memcard().is_ok() {
return Some(FirmwareState::ResumeResidentGame);
}
if self.released && ctx.signals.b_signal {
return Some(FirmwareState::ConfirmDiscard(ConfirmDiscardStep::new(None)));
}
None
}
}
@@ -23,7 +23,21 @@ impl AppCrashesStep {
// Update peripherals for input on the crash screen
ctx.platform.input_mut().pad_mut().begin_frame(ctx.signals);
let packet = ShellUiFramePacket::new(vec![GfxUiCommand::Clear { color: Color::RED }]);
let mut commands = vec![GfxUiCommand::Clear { color: Color::RED }];
if let CrashReport::VmPanic { message, .. } = &self.report
&& message.starts_with("Memcard")
{
let text: Vec<char> = message.chars().take(240).collect();
commands.extend(text.chunks(48).enumerate().map(|(line, chars)| {
GfxUiCommand::DrawText {
x: 8,
y: 8 + line as i32 * 20,
text: chars.iter().collect(),
color: Color::WHITE,
}
}));
}
let packet = ShellUiFramePacket::new(commands);
ctx.platform
.render_submission_sink()
.submit_render_submission(RenderSubmission::shell_ui(FrameId::ZERO, packet))
@@ -56,6 +56,16 @@ impl HubHomeStep {
Some(SystemProfileAction::LaunchGame { target }) => {
match CartridgeLoader::load(&target.path) {
Ok(cartridge) => {
if ctx.os.lifecycle().resident_game_task().is_none()
&& ctx.os.memcard().session.mode().is_some()
{
if ctx.os.memcard().session.unsaved() {
return Some(FirmwareState::ConfirmDiscard(
super::firmware_state::ConfirmDiscardStep::new(Some(cartridge)),
));
}
ctx.os.memcard().session.end_session();
}
if cartridge.app_mode == AppMode::Game
&& let Some(resident_task_id) = ctx.os.lifecycle().resident_game_task()
{
@@ -70,6 +80,11 @@ impl HubHomeStep {
return Some(FirmwareState::ResumeResidentGame);
}
if ctx.os.memcard().session.unsaved() {
return Some(FirmwareState::ConfirmDiscard(
super::firmware_state::ConfirmDiscardStep::new(Some(cartridge)),
));
}
if let Err(error) = resident_game_termination::terminate_resident_game(
ctx.os,
ctx.platform,
@@ -107,6 +122,12 @@ impl HubHomeStep {
}
}
Some(SystemProfileAction::KillResidentGame) => {
if ctx.os.memcard().session.unsaved() {
return Some(FirmwareState::ConfirmDiscard(
super::firmware_state::ConfirmDiscardStep::new(None),
));
}
if let Err(error) =
resident_game_termination::terminate_resident_game(ctx.os, ctx.platform, true)
{
@@ -15,6 +15,10 @@ use prometeu_system::windows::WindowOwner;
pub struct LoadCartridgeStep {
pub cartridge: Cartridge,
init_error: Option<CrashReport>,
awaiting_memcard: bool,
preparing_memcard: bool,
input_released: bool,
memcard_message: String,
loaded_task_id: Option<prometeu_system::task::TaskId>,
loaded_app_mode: Option<AppMode>,
failure_recovery: LoadCartridgeFailureRecovery,
@@ -31,12 +35,20 @@ impl LoadCartridgeStep {
Self {
cartridge,
init_error: None,
awaiting_memcard: false,
preparing_memcard: false,
input_released: false,
memcard_message: String::new(),
loaded_task_id: None,
loaded_app_mode: None,
failure_recovery: LoadCartridgeFailureRecovery::AppCrash,
}
}
pub fn needs_memcard_choice(&self) -> bool {
self.awaiting_memcard && !self.preparing_memcard
}
pub fn new_game_switch(cartridge: Cartridge) -> Self {
Self { failure_recovery: LoadCartridgeFailureRecovery::ReturnToHub, ..Self::new(cartridge) }
}
@@ -51,6 +63,15 @@ impl LoadCartridgeStep {
ctx.platform.game2d_frame_composer().unbind_scene();
if self.cartridge.app_mode == AppMode::Game && self.cartridge.memcard_slots.is_some() {
self.awaiting_memcard = true;
ctx.os.vm().transition_render_owner(AppMode::Shell, 0);
return;
}
self.load(ctx);
}
fn load(&mut self, ctx: &mut PrometeuContext) {
// Initialize Asset Manager
ctx.platform.assets_mut().initialize_for_cartridge(
self.cartridge.asset_table.clone(),
@@ -70,6 +91,58 @@ impl LoadCartridgeStep {
}
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
if self.awaiting_memcard {
use prometeu_system::memcard::admission::{Choice, PreparationState};
if self.preparing_memcard {
match ctx.os.memcard().admission.state {
PreparationState::Ready => {
self.awaiting_memcard = false;
self.preparing_memcard = false;
self.load(ctx);
}
PreparationState::Failed(error) => {
self.memcard_message = Self::preparation_message(error);
self.preparing_memcard = false;
}
_ => {}
}
}
if self.awaiting_memcard {
let pressed = ctx.signals.a_signal
|| ctx.signals.b_signal
|| ctx.signals.x_signal
|| ctx.signals.y_signal;
if !pressed {
self.input_released = true;
}
if self.input_released && pressed && !self.preparing_memcard {
self.input_released = false;
if ctx.signals.b_signal {
return Some(FirmwareState::HubHome(HubHomeStep));
}
let choice = if ctx.signals.a_signal {
Choice::Persistent
} else if ctx.signals.x_signal {
Choice::MemoryFresh
} else {
Choice::MemoryExisting
};
let created_at = ctx.platform.wall_clock().utc_unix_seconds();
match ctx.os.memcard().choose_for_game(
self.cartridge.app_id,
self.cartridge.memcard_slots.unwrap() as usize,
choice,
created_at,
) {
Ok(()) => self.preparing_memcard = true,
Err(error) => self.memcard_message = Self::preparation_message(error),
}
}
self.draw_memcard(ctx);
return None;
}
}
if let Some(report) = self.init_error.take() {
if self.failure_recovery == LoadCartridgeFailureRecovery::ReturnToHub {
if self.cartridge.app_mode == AppMode::Game
@@ -106,6 +179,18 @@ impl LoadCartridgeStep {
return Some(FirmwareState::AppCrashes(AppCrashesStep { report }));
}
match ctx.platform.assets().preloads_ready() {
Ok(false) => return None,
Err(error) => {
return Some(FirmwareState::AppCrashes(AppCrashesStep {
report: CrashReport::VmPanic {
message: format!("Cartridge preload failed: {error:?}"),
pc: None,
},
}));
}
Ok(true) => {}
}
let Some(task_id) = self.loaded_task_id.take() else {
let report = CrashReport::VmPanic {
message: format!(
@@ -131,5 +216,67 @@ impl LoadCartridgeStep {
Some(FirmwareState::GameRunning(GameRunningStep::new(task_id)))
}
fn preparation_message(error: prometeu_system::memcard::image::Error) -> String {
use prometeu_system::memcard::image::Error;
match error {
Error::NoSpace => "This memory card has no room for this game.".into(),
Error::Corrupt => "The memory card failed its integrity checks.".into(),
Error::ReadOnly => "This memory card cannot accept saves.".into(),
Error::OutOfMemory => "There is not enough memory to start this game.".into(),
Error::Busy => "The memory card is still busy. Please wait.".into(),
Error::InvalidState => "No usable memory card or existing save is available.".into(),
_ => format!("Memory card unavailable: {error:?}"),
}
}
fn draw_memcard(&self, ctx: &mut PrometeuContext) {
use prometeu_hal::{FrameId, GfxUiCommand, RenderSubmission, ShellUiFramePacket};
let installed = ctx.os.memcard().card_installed();
let existing = ctx.os.memcard().card_has_game(self.cartridge.app_id);
let mut lines = vec!["Save memory".to_string(), self.cartridge.title.clone()];
if let Some((recovered, degraded, blocked)) = ctx.os.memcard().installed_health() {
if recovered {
lines.push("Recovered an older save after an integrity failure.".into());
}
if degraded {
lines.push("Memory card degraded: some blocks are excluded.".into());
}
if blocked {
lines.push("Disk saving is unavailable on this card.".into());
}
}
if let prometeu_system::memcard::admission::PreparationState::Failed(error) =
ctx.os.memcard().admission.state
{
lines.push(Self::preparation_message(error));
}
if self.preparing_memcard {
lines.push("Preparing save memory...".into());
} else {
lines.push(
if installed { "A: Use installed memory card" } else { "No memory card installed" }
.into(),
);
lines.push("X: Start fresh without saving to disk".into());
if existing {
lines.push("Y: Load saves, then play without disk saving".into());
}
lines.push("B: Return to Home".into());
}
if !self.memcard_message.is_empty() {
lines.push(self.memcard_message.clone());
}
let mut commands = vec![GfxUiCommand::Clear { color: Color::BLACK }];
commands.extend(lines.into_iter().enumerate().map(|(i, text)| GfxUiCommand::DrawText {
x: 8,
y: 8 + i as i32 * 20,
text,
color: Color::WHITE,
}));
let _ = ctx.platform.render_submission_sink().submit_render_submission(
RenderSubmission::shell_ui(FrameId::ZERO, ShellUiFramePacket::new(commands)),
);
}
pub fn on_exit(&mut self, _ctx: &mut PrometeuContext) {}
}
@@ -18,3 +18,5 @@ pub use boot_target::BootTarget;
pub use firmware::Firmware;
pub use firmware_state::FirmwareState;
pub use prometeu_context::PrometeuContext;
pub(crate) mod firmware_step_confirm_discard;
@@ -16,6 +16,7 @@ pub(crate) fn terminate_resident_game(
log_manual_hub_kill: bool,
) -> Result<ResidentGameTerminationOutcome, LifecycleError> {
let Some(task_id) = os.lifecycle().resident_game_task() else {
os.memcard().session.end_session();
return Ok(ResidentGameTerminationOutcome::NoResidentGame);
};
@@ -30,6 +31,7 @@ pub(crate) fn terminate_resident_game(
return Ok(ResidentGameTerminationOutcome::NoResidentGame);
};
os.memcard().session.end_session();
os.vm().transition_render_owner(AppMode::Shell, 0);
platform.game2d_frame_composer().unbind_scene();
platform.assets_mut().shutdown();
@@ -6,12 +6,25 @@ use crate::asset::{
use crate::cartridge::AssetsPayloadSource;
pub trait AssetBridge {
/// Bind before cartridge preload/loading. Hosts supporting memcard must use
/// this same lane for assets; an unsupported bridge refuses the fast profile.
fn bind_background_work(
&mut self,
_lane: std::sync::Arc<dyn crate::background_work::BackgroundWork>,
) -> bool {
false
}
fn initialize_for_cartridge(
&self,
assets: Vec<AssetEntry>,
preload: Vec<PreloadEntry>,
assets_data: AssetsPayloadSource,
);
/// Admission waits until all preloads are installed. Failure blocks guest boot.
fn preloads_ready(&self) -> Result<bool, AssetLoadError> {
Ok(true)
}
fn load(&self, asset_id: AssetId, slot_index: usize) -> Result<HandleId, AssetLoadError>;
fn status(&self, handle: HandleId) -> LoadStatus;
fn commit(&self, handle: HandleId) -> AssetOpStatus;
@@ -0,0 +1,13 @@
//! Portable submission seam for host-owned cooperative IO execution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkStep {
Yield,
Complete,
Failed,
}
pub type AssetWork = Box<dyn FnMut(bool) -> WorkStep + Send + 'static>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WorkUnavailable;
pub trait BackgroundWork: Send + Sync {
fn submit_asset(&self, work: AssetWork) -> Result<(), WorkUnavailable>;
}
+45 -4
View File
@@ -3,7 +3,7 @@ use crate::asset::{AssetEntry, PreloadEntry};
use crate::syscalls::CapFlags;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{self, Cursor, Read, Seek, SeekFrom};
use std::io::{self, Read, Seek, SeekFrom};
use std::path::PathBuf;
use std::sync::Arc;
@@ -18,6 +18,7 @@ pub struct Cartridge {
pub app_version: String,
pub app_mode: AppMode,
pub capabilities: CapFlags,
pub memcard_slots: Option<u8>,
pub program: Vec<u8>,
pub assets: AssetsPayloadSource,
pub asset_table: Vec<AssetEntry>,
@@ -31,6 +32,7 @@ pub struct CartridgeDTO {
pub app_version: String,
pub app_mode: AppMode,
pub capabilities: CapFlags,
pub memcard_slots: Option<u8>,
pub program: Vec<u8>,
pub assets: AssetsPayloadSource,
pub asset_table: Vec<AssetEntry>,
@@ -45,6 +47,7 @@ impl From<CartridgeDTO> for Cartridge {
app_version: dto.app_version,
app_mode: dto.app_mode,
capabilities: dto.capabilities,
memcard_slots: dto.memcard_slots,
program: dto.program,
assets: dto.assets,
asset_table: dto.asset_table,
@@ -125,8 +128,12 @@ impl AssetsPayloadSlice {
pub fn open_reader(&self) -> io::Result<AssetsPayloadReader> {
match self {
Self::Memory { bytes, start, len } => {
let data = Arc::<[u8]>::from(bytes[*start..*start + *len].to_vec());
Ok(AssetsPayloadReader::Memory(Cursor::new(data)))
Ok(AssetsPayloadReader::Memory(MemorySliceReader {
bytes: Arc::clone(bytes),
start: *start,
len: *len,
position: 0,
}))
}
Self::File { source, offset, size } => {
let mut file = File::open(&source.path)?;
@@ -151,7 +158,7 @@ impl AssetsPayloadSlice {
}
pub enum AssetsPayloadReader {
Memory(Cursor<Arc<[u8]>>),
Memory(MemorySliceReader),
File(FileSliceReader),
}
@@ -173,6 +180,37 @@ impl Seek for AssetsPayloadReader {
}
}
pub struct MemorySliceReader {
bytes: Arc<[u8]>,
start: usize,
len: usize,
position: usize,
}
impl Read for MemorySliceReader {
fn read(&mut self, target: &mut [u8]) -> io::Result<usize> {
let n = target.len().min(self.len.saturating_sub(self.position));
target[..n].copy_from_slice(
&self.bytes[self.start + self.position..self.start + self.position + n],
);
self.position += n;
Ok(n)
}
}
impl Seek for MemorySliceReader {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
let next = match pos {
SeekFrom::Start(n) => n as i128,
SeekFrom::Current(n) => self.position as i128 + n as i128,
SeekFrom::End(n) => self.len as i128 + n as i128,
};
if next < 0 || next > self.len as i128 {
return Err(invalid_input("slice seek out of bounds"));
}
self.position = next as usize;
Ok(self.position as u64)
}
}
pub struct FileSliceReader {
file: File,
start: u64,
@@ -292,6 +330,7 @@ pub enum Capability {
Gfxui,
Audio,
Fs,
Memcard,
Log,
Asset,
Bank,
@@ -308,4 +347,6 @@ pub struct CartridgeManifest {
pub app_mode: AppMode,
#[serde(default)]
pub capabilities: Vec<Capability>,
#[serde(default)]
pub memcard_slots: Option<u8>,
}
@@ -55,6 +55,11 @@ impl DirectoryCartridgeLoader {
}
let capabilities = normalize_capabilities(&manifest.capabilities)?;
if ((capabilities & caps::MEMCARD) != 0) != manifest.memcard_slots.is_some()
|| manifest.memcard_slots.is_some_and(|n| !(1..=64).contains(&n))
{
return Err(CartridgeError::InvalidManifest);
}
let program_path = path.join("program.pbx");
if !program_path.exists() {
@@ -91,6 +96,7 @@ impl DirectoryCartridgeLoader {
app_version: manifest.app_version,
app_mode: manifest.app_mode,
capabilities,
memcard_slots: manifest.memcard_slots,
program,
assets,
asset_table,
@@ -180,6 +186,7 @@ fn normalize_capabilities(capabilities: &[Capability]) -> Result<CapFlags, Cartr
Capability::Gfxui => caps::GFXUI,
Capability::Audio => caps::AUDIO,
Capability::Fs => caps::FS,
Capability::Memcard => caps::MEMCARD,
Capability::Log => caps::LOG,
Capability::Asset => caps::ASSET,
Capability::Bank => caps::BANK,
@@ -447,7 +454,9 @@ mod tests {
#[test]
fn load_with_all_capability_normalizes_to_all_flags() {
let dir = TestCartridgeDir::new(manifest_with_capabilities(Some(vec!["all"])));
let mut manifest = manifest_with_capabilities(Some(vec!["all"]));
manifest["memcard_slots"] = serde_json::json!(1);
let dir = TestCartridgeDir::new(manifest);
dir.write_assets_pa(vec![], vec![], &[]);
let cartridge = DirectoryCartridgeLoader::load(dir.path()).expect("cartridge must load");
+24 -1
View File
@@ -13,6 +13,14 @@ pub trait GuestStringAllocator {
) -> Result<Arc<str>, VmFault>;
}
/// A bounded, call-scoped Array<Int32> byte carrier. Implementations validate
/// concrete array type, every value and all bounds before exposing mutations.
pub trait GuestByteArrays {
fn validate(&mut self, handle: HeapRef, offset: usize, count: usize) -> Result<(), VmFault>;
fn read(&mut self, handle: HeapRef, offset: usize, target: &mut [u8]) -> Result<(), VmFault>;
fn write(&mut self, handle: HeapRef, offset: usize, bytes: &[u8]) -> Result<(), VmFault>;
}
/// Writes only into result slots reserved by the VM. A native handler cannot
/// grow the operand buffer or exceed its declared result count.
pub struct HostReturn<'a> {
@@ -21,6 +29,7 @@ pub struct HostReturn<'a> {
overflowed: bool,
strings: &'a mut dyn GuestStringAllocator,
failure: Option<VmFault>,
arrays: Option<&'a mut dyn GuestByteArrays>,
}
impl<'a> HostReturn<'a> {
@@ -39,7 +48,21 @@ impl<'a> HostReturn<'a> {
result_slots: usize,
strings: &'a mut dyn GuestStringAllocator,
) -> Self {
Self { stack, remaining: result_slots, overflowed: false, strings, failure: None }
Self {
stack,
remaining: result_slots,
overflowed: false,
strings,
failure: None,
arrays: None,
}
}
pub fn with_arrays(mut self, arrays: &'a mut dyn GuestByteArrays) -> Self {
self.arrays = Some(arrays);
self
}
pub fn byte_arrays(&mut self) -> Result<&mut (dyn GuestByteArrays + 'a), VmFault> {
self.arrays.as_deref_mut().ok_or(VmFault::Unavailable)
}
pub fn failure(&self) -> Option<&VmFault> {
self.failure.as_ref()
+2
View File
@@ -2,6 +2,7 @@ pub mod app_mode;
pub mod asset;
pub mod asset_bridge;
pub mod audio_bridge;
pub mod background_work;
pub mod button;
pub mod cartridge;
pub mod cartridge_loader;
@@ -15,6 +16,7 @@ pub mod host_context;
pub mod host_return;
pub mod input_signals;
pub mod log;
pub mod memcard;
pub mod native_helpers;
pub mod native_interface;
pub mod owned_frame;
@@ -0,0 +1,23 @@
//! Host region IO. Logical mapping, checksums and publication belong to the runtime.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemcardIoError {
Unavailable,
ReadOnly,
InvalidRange,
NoSpace,
Unsupported,
IoError,
}
/// Implementations return success only for a complete transfer. Errors may have
/// partial effects. Instances stay owned by their worker until all IO has ceased.
pub trait MemcardStorage: Send {
fn len(&self) -> Result<u64, MemcardIoError>;
fn is_empty(&self) -> Result<bool, MemcardIoError> {
self.len().map(|length| length == 0)
}
fn read_at(&mut self, offset: u64, target: &mut [u8]) -> Result<(), MemcardIoError>;
fn write_at(&mut self, offset: u64, source: &[u8]) -> Result<(), MemcardIoError>;
fn sync(&mut self) -> Result<(), MemcardIoError>;
fn read_only(&self) -> bool;
}
@@ -62,12 +62,20 @@ pub enum Syscall {
FsListDir = 0x4005,
FsExists = 0x4006,
FsDelete = 0x4007,
MemBootstrap = 0x4200,
MemSlotCount = 0x4201,
MemSlotStat = 0x4202,
MemSlotRead = 0x4203,
MemSlotWrite = 0x4204,
MemSlotCommit = 0x4205,
MemSlotClear = 0x4206,
MemQuery = 0x4207,
MemRelease = 0x4208,
MemInfo = 0x4209,
MemMetadata = 0x420A,
MemPresentation = 0x420B,
MemDiagnostic = 0x420C,
MemPublishPresentation = 0x420D,
LogWrite = 0x5001,
LogWriteTag = 0x5002,
AssetLoad = 0x6001,
@@ -10,5 +10,7 @@ pub const ASSET: CapFlags = 1 << 5;
pub const BANK: CapFlags = 1 << 6;
pub const GFX2D: CapFlags = 1 << 7;
pub const GFXUI: CapFlags = 1 << 8;
pub const MEMCARD: CapFlags = 1 << 9;
pub const GFX: CapFlags = COMPOSER | GFX2D;
pub const ALL: CapFlags = SYSTEM | COMPOSER | AUDIO | FS | LOG | ASSET | BANK | GFX2D | GFXUI;
pub const ALL: CapFlags =
SYSTEM | COMPOSER | AUDIO | FS | LOG | ASSET | BANK | GFX2D | GFXUI | MEMCARD;
@@ -32,37 +32,4 @@ pub(crate) const ENTRIES: &[SyscallRegistryEntry] = &[
.caps(caps::FS)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotCount, "mem", "slot_count")
.rets(2)
.caps(caps::FS),
SyscallRegistryEntry::builder(Syscall::MemSlotStat, "mem", "slot_stat")
.args(1)
.rets(5)
.caps(caps::FS)
.non_deterministic()
.cost(5),
SyscallRegistryEntry::builder(Syscall::MemSlotRead, "mem", "slot_read")
.args(3)
.rets(3)
.caps(caps::FS)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotWrite, "mem", "slot_write")
.args(3)
.rets(2)
.caps(caps::FS)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotCommit, "mem", "slot_commit")
.args(1)
.rets(1)
.caps(caps::FS)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotClear, "mem", "slot_clear")
.args(1)
.rets(1)
.caps(caps::FS)
.non_deterministic()
.cost(20),
];
@@ -0,0 +1,87 @@
use crate::syscalls::{Syscall, SyscallRegistryEntry, caps};
pub(crate) const ENTRIES: &[SyscallRegistryEntry] = &[
SyscallRegistryEntry::builder(Syscall::MemBootstrap, "mem", "bootstrap")
.args(0)
.rets(1)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotCount, "mem", "slot_count")
.args(1)
.rets(2)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotStat, "mem", "slot_stat")
.args(2)
.rets(7)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotRead, "mem", "slot_read")
.args(6)
.rets(2)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotWrite, "mem", "slot_write")
.args(6)
.rets(2)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotCommit, "mem", "slot_commit")
.args(2)
.rets(2)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemSlotClear, "mem", "slot_clear")
.args(2)
.rets(1)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemQuery, "mem", "query")
.args(2)
.rets(3)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemRelease, "mem", "release")
.args(2)
.rets(1)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemInfo, "mem", "info")
.args(1)
.rets(6)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemMetadata, "mem", "metadata")
.args(1)
.rets(4)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemPresentation, "mem", "presentation")
.args(8)
.rets(1)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemDiagnostic, "mem", "diagnostic")
.args(2)
.rets(1)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
SyscallRegistryEntry::builder(Syscall::MemPublishPresentation, "mem", "publish_presentation")
.args(1)
.rets(2)
.caps(caps::MEMCARD)
.non_deterministic()
.cost(20),
];
@@ -18,7 +18,10 @@ pub(crate) fn all_entries() -> impl Iterator<Item = &'static SyscallRegistryEntr
.chain(composer::ENTRIES.iter())
.chain(audio::ENTRIES.iter())
.chain(fs::ENTRIES.iter())
.chain(memcard::ENTRIES.iter())
.chain(log::ENTRIES.iter())
.chain(asset::ENTRIES.iter())
.chain(bank::ENTRIES.iter())
}
mod memcard;
@@ -40,6 +40,14 @@ impl Syscall {
0x4005 => Some(Self::FsListDir),
0x4006 => Some(Self::FsExists),
0x4007 => Some(Self::FsDelete),
0x4200 => Some(Self::MemBootstrap),
0x4207 => Some(Self::MemQuery),
0x4208 => Some(Self::MemRelease),
0x4209 => Some(Self::MemInfo),
0x420A => Some(Self::MemMetadata),
0x420B => Some(Self::MemPresentation),
0x420C => Some(Self::MemDiagnostic),
0x420D => Some(Self::MemPublishPresentation),
0x4201 => Some(Self::MemSlotCount),
0x4202 => Some(Self::MemSlotStat),
0x4203 => Some(Self::MemSlotRead),
@@ -101,6 +109,14 @@ impl Syscall {
Self::FsListDir => "FsListDir",
Self::FsExists => "FsExists",
Self::FsDelete => "FsDelete",
Self::MemBootstrap => "MemBootstrap",
Self::MemQuery => "MemQuery",
Self::MemRelease => "MemRelease",
Self::MemInfo => "MemInfo",
Self::MemMetadata => "MemMetadata",
Self::MemPresentation => "MemPresentation",
Self::MemDiagnostic => "MemDiagnostic",
Self::MemPublishPresentation => "MemPublishPresentation",
Self::MemSlotCount => "MemSlotCount",
Self::MemSlotStat => "MemSlotStat",
Self::MemSlotRead => "MemSlotRead",
@@ -460,29 +460,39 @@ fn declared_resolver_accepts_mixed_status_first_surface_as_a_single_module() {
#[test]
fn memcard_syscall_signatures_are_pinned() {
let slot_count = meta_for(Syscall::MemSlotCount);
assert_eq!(slot_count.module, "mem");
assert_eq!(slot_count.name, "slot_count");
assert_eq!(slot_count.arg_slots, 0);
assert_eq!(slot_count.ret_slots, 2);
let slot_stat = meta_for(Syscall::MemSlotStat);
assert_eq!(slot_stat.arg_slots, 1);
assert_eq!(slot_stat.ret_slots, 5);
let slot_read = meta_for(Syscall::MemSlotRead);
assert_eq!(slot_read.arg_slots, 3);
assert_eq!(slot_read.ret_slots, 3);
let slot_write = meta_for(Syscall::MemSlotWrite);
assert_eq!(slot_write.arg_slots, 3);
assert_eq!(slot_write.ret_slots, 2);
let slot_commit = meta_for(Syscall::MemSlotCommit);
assert_eq!(slot_commit.arg_slots, 1);
assert_eq!(slot_commit.ret_slots, 1);
let slot_clear = meta_for(Syscall::MemSlotClear);
assert_eq!(slot_clear.arg_slots, 1);
assert_eq!(slot_clear.ret_slots, 1);
for (call, args, results) in [
(Syscall::MemBootstrap, 0, 1),
(Syscall::MemSlotCount, 1, 2),
(Syscall::MemSlotStat, 2, 7),
(Syscall::MemSlotRead, 6, 2),
(Syscall::MemSlotWrite, 6, 2),
(Syscall::MemSlotCommit, 2, 2),
(Syscall::MemSlotClear, 2, 1),
(Syscall::MemQuery, 2, 3),
(Syscall::MemRelease, 2, 1),
(Syscall::MemInfo, 1, 6),
(Syscall::MemMetadata, 1, 4),
(Syscall::MemPresentation, 8, 1),
(Syscall::MemDiagnostic, 2, 1),
(Syscall::MemPublishPresentation, 1, 2),
] {
let meta = meta_for(call);
assert_eq!(meta.module, "mem");
assert_eq!(meta.arg_slots, args);
assert_eq!(meta.ret_slots, results);
assert!(
resolve_program_syscalls(
&[SyscallIdentity { module: "mem", name: meta.name, version: 1 }],
caps::FS
)
.is_err()
);
assert!(
resolve_program_syscalls(
&[SyscallIdentity { module: "mem", name: meta.name, version: 1 }],
caps::MEMCARD
)
.is_ok()
);
}
}
@@ -5,6 +5,7 @@ edition = "2024"
license.workspace = true
[dependencies]
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
prometeu-vm = { path = "../prometeu-vm" }
prometeu-bytecode = { path = "../prometeu-bytecode" }
+2 -1
View File
@@ -17,6 +17,7 @@ pub use services::async_work::{
AsyncWorkActiveJob, AsyncWorkCancelToken, AsyncWorkJobContext, AsyncWorkJobId,
AsyncWorkJobKind, AsyncWorkJobOutcome, AsyncWorkLane, AsyncWorkLaneConfig,
AsyncWorkLaneController, AsyncWorkLaneError, AsyncWorkLaneTelemetry, AsyncWorkPriority,
AsyncWorkStep,
};
pub use services::foreground::{
ForegroundOwner, ForegroundStack, ForegroundStackError, ResidentGame, ResidentGameState,
@@ -25,7 +26,7 @@ pub use services::fs;
pub use services::game_library::{
GameLibrary, GameLibraryDiagnostic, GameLibraryEntry, GameSwitchTarget, discover_games_root,
};
pub use services::memcard::MemcardAsyncLaneOperation;
pub use services::memcard;
pub use services::process;
pub use services::task;
pub use services::vm_runtime::{
@@ -36,6 +36,21 @@ impl<'a> LifecycleFacade<'a> {
}
pub fn set_game_foreground_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
if self.os.memcard.host_faulted()
|| self.os.memcard.session.removal_pending()
|| matches!(
self.os.memcard.admission.state,
crate::memcard::admission::PreparationState::Pending
| crate::memcard::admission::PreparationState::Failed(_)
)
{
return Err(LifecycleError::InvalidTransition {
task_id,
from: self.task_state(task_id).unwrap_or(TaskState::Closed),
operation: LifecycleOperation::SetForeground,
});
}
let process_id = process_id_for_task(self.os, task_id)?;
if task_kind_for_task(self.os, task_id)? != TaskKind::Game {
@@ -242,6 +257,21 @@ impl<'a> LifecycleFacade<'a> {
}
pub fn resume_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
if self.os.memcard.host_faulted()
|| self.os.memcard.session.removal_pending()
|| matches!(
self.os.memcard.admission.state,
crate::memcard::admission::PreparationState::Pending
| crate::memcard::admission::PreparationState::Failed(_)
)
{
return Err(LifecycleError::InvalidTransition {
task_id,
from: self.task_state(task_id).unwrap_or(TaskState::Closed),
operation: LifecycleOperation::Resume,
});
}
let task =
self.os.task_manager.get(task_id).ok_or(LifecycleError::TaskNotFound(task_id))?;
@@ -203,7 +203,16 @@ impl<'a> VmFacade<'a> {
});
};
session.runtime.debug_step_instruction(&mut self.os.log_service, &mut session.vm, platform)
session.runtime.debug_step_instruction(
&mut self.os.log_service,
&mut self.os.fs,
&mut session.fs_state,
&mut self.os.memcard,
&mut session.open_files,
&mut session.next_handle,
&mut session.vm,
platform,
)
}
pub fn active_pc(&self) -> Option<usize> {
@@ -28,6 +28,8 @@ pub struct SystemOS {
pub(super) memcard: MemcardService,
pub(super) open_files: HashMap<u32, String>,
pub(super) next_handle: u32,
io_controller: Option<crate::AsyncWorkLaneController>,
io_bound: bool,
}
impl SystemOS {
@@ -50,6 +52,8 @@ impl SystemOS {
memcard: MemcardService::new(),
open_files: HashMap::new(),
next_handle: 1,
io_controller: None,
io_bound: false,
};
os.log(LogLevel::Info, LogSource::Pos, 0, "PrometeuOS starting...".to_string());
@@ -57,6 +61,55 @@ impl SystemOS {
os
}
pub fn memcard(&mut self) -> &mut MemcardService {
&mut self.memcard
}
/// Host/player operations are admitted only while gameplay is suspended.
pub fn replace_memcard(
&mut self,
storage: Box<dyn prometeu_hal::memcard::MemcardStorage>,
load_existing: bool,
) -> crate::memcard::image::Result<()> {
if let Some(task) = self.foreground_stack.resident_game_task()
&& self
.task_manager
.get(task)
.is_none_or(|t| t.state != crate::task::TaskState::Suspended)
{
return Err(crate::memcard::image::Error::AccessDenied);
}
self.memcard.replace_card(storage, load_existing)
}
pub fn detach_memcard(&mut self) -> crate::memcard::image::Result<()> {
if let Some(task) = self.foreground_stack.resident_game_task()
&& self
.task_manager
.get(task)
.is_none_or(|t| t.state != crate::task::TaskState::Suspended)
{
return Err(crate::memcard::image::Error::AccessDenied);
}
if self.memcard.host_faulted() {
return Err(crate::memcard::image::Error::ReadOnly);
}
self.memcard.session.detach()?;
self.memcard.admission.state = crate::memcard::admission::PreparationState::Ready;
Ok(())
}
/// Called outside guest/frame execution, including while a game is suspended.
pub fn poll_background_io(&mut self, platform: &mut dyn prometeu_hal::RuntimePlatform) {
if !self.io_bound && platform.assets_mut().bind_background_work(self.memcard.lane.clone()) {
self.io_controller = Some(crate::AsyncWorkLaneController::start(
crate::AsyncWorkLaneConfig::default(),
self.memcard.lane.clone(),
));
self.io_bound = true;
}
self.memcard.poll(std::time::Instant::now());
}
pub fn log(&mut self, level: LogLevel, source: LogSource, tag: u16, msg: String) {
self.vm_runtime.log(&mut self.log_service, level, source, tag, msg);
}
@@ -136,7 +189,7 @@ impl SystemOS {
pub(super) fn clear_cartridge_service_state(&mut self) {
self.open_files.clear();
self.next_handle = 1;
self.memcard.clear_all_staging();
self.memcard.session.end_session();
}
}
@@ -158,6 +211,7 @@ mod tests {
fn session_test_cartridge(app_mode: AppMode, app_id: u32, title: &str) -> Cartridge {
Cartridge {
memcard_slots: None,
app_id,
title: title.to_string(),
app_version: "1.0.0".to_string(),
@@ -607,6 +607,7 @@ mod tests {
fn game_row_click_emits_launch_game_action() {
let entry = crate::GameLibraryEntry {
manifest: prometeu_hal::cartridge::CartridgeManifest {
memcard_slots: None,
magic: "PMTU".to_string(),
cartridge_version: 1,
app_id: 42,
@@ -172,7 +172,13 @@ impl AsyncWorkJobContext {
}
}
type AsyncWorkFn = Box<dyn FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static>;
/// A step must release the executor when yielding; it retains its own bounded progress.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AsyncWorkStep {
Yield,
Complete(AsyncWorkJobOutcome),
}
type AsyncWorkFn = Box<dyn FnMut(AsyncWorkJobContext) -> AsyncWorkStep + Send + 'static>;
struct PendingAsyncWorkJob {
id: AsyncWorkJobId,
@@ -180,6 +186,7 @@ struct PendingAsyncWorkJob {
priority: AsyncWorkPriority,
cancel_token: AsyncWorkCancelToken,
work: AsyncWorkFn,
started: bool,
}
struct RunningAsyncWorkJob {
@@ -188,11 +195,13 @@ struct RunningAsyncWorkJob {
priority: AsyncWorkPriority,
cancel_token: AsyncWorkCancelToken,
work: AsyncWorkFn,
started: bool,
}
#[derive(Default)]
struct AsyncWorkLaneState {
next_job_id: u64,
progress: u64,
pending: VecDeque<PendingAsyncWorkJob>,
active: Option<AsyncWorkActiveJob>,
active_cancel_token: Option<AsyncWorkCancelToken>,
@@ -204,9 +213,25 @@ struct AsyncWorkLaneState {
pub struct AsyncWorkLane {
state: Mutex<AsyncWorkLaneState>,
ready: Condvar,
progressed: Condvar,
}
impl AsyncWorkLane {
pub fn progress_stamp(&self) -> u64 {
self.state.lock().unwrap().progress
}
/// Used by synchronous headless boot admission, never by a guest/frame tick.
/// Wakes on actual IO progress; timeout lets the caller run its watchdog.
pub fn wait_for_progress(&self, stamp: u64, timeout: Duration) {
let state = self.state.lock().unwrap();
let _guard = self
.progressed
.wait_timeout_while(state, timeout, |s| {
s.progress == stamp && (!s.pending.is_empty() || s.active.is_some())
})
.unwrap();
}
pub fn submit(
&self,
kind: AsyncWorkJobKind,
@@ -220,16 +245,36 @@ impl AsyncWorkLane {
kind: AsyncWorkJobKind,
priority: AsyncWorkPriority,
work: impl FnOnce(AsyncWorkJobContext) -> AsyncWorkJobOutcome + Send + 'static,
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
let mut once = Some(work);
self.submit_steps(kind, priority, move |context| {
AsyncWorkStep::Complete(once.take().expect("one-shot job executed once")(context))
})
}
pub fn submit_steps(
&self,
kind: AsyncWorkJobKind,
priority: AsyncWorkPriority,
work: impl FnMut(AsyncWorkJobContext) -> AsyncWorkStep + Send + 'static,
) -> Result<AsyncWorkJobId, AsyncWorkLaneError> {
let mut state = self.state.lock().map_err(|_| AsyncWorkLaneError::InternalFailure)?;
if state.shutdown_requested {
return Err(AsyncWorkLaneError::Shutdown);
}
state.next_job_id += 1;
state.next_job_id =
state.next_job_id.checked_add(1).ok_or(AsyncWorkLaneError::InternalFailure)?;
let id = AsyncWorkJobId::new(state.next_job_id);
let cancel_token = AsyncWorkCancelToken::new();
let job = PendingAsyncWorkJob { id, kind, priority, cancel_token, work: Box::new(work) };
let job = PendingAsyncWorkJob {
id,
kind,
priority,
cancel_token,
work: Box::new(work),
started: false,
};
let insert_at = state
.pending
.iter()
@@ -340,13 +385,19 @@ impl AsyncWorkLane {
state.active = Some(active);
state.active_cancel_token = Some(pending.cancel_token.clone());
let pending_depth = state.pending.len();
state.telemetry.record_started(pending.id, pending_depth);
if !pending.started {
state.telemetry.record_started(pending.id, pending_depth);
} else {
state.telemetry.active_job_id = Some(pending.id);
state.telemetry.pending_depth = pending_depth;
}
return Some(RunningAsyncWorkJob {
id: pending.id,
kind: pending.kind,
priority: pending.priority,
cancel_token: pending.cancel_token,
work: pending.work,
started: true,
});
}
@@ -355,6 +406,33 @@ impl AsyncWorkLane {
}
}
fn yield_job(&self, job: RunningAsyncWorkJob) {
let mut state = self.state.lock().unwrap();
state.active = None;
state.active_cancel_token = None;
state.telemetry.active_job_id = None;
let at = state
.pending
.iter()
.position(|p| job.priority.rank() < p.priority.rank())
.unwrap_or(state.pending.len());
state.pending.insert(
at,
PendingAsyncWorkJob {
id: job.id,
kind: job.kind,
priority: job.priority,
cancel_token: job.cancel_token,
work: job.work,
started: job.started,
},
);
state.telemetry.pending_depth = state.pending.len();
state.progress = state.progress.saturating_add(1);
self.progressed.notify_all();
self.ready.notify_one();
}
fn record_finished(&self, job_id: AsyncWorkJobId, outcome: AsyncWorkJobOutcome) {
let mut state = self.state.lock().unwrap();
if state.active.is_some_and(|active| active.id == job_id) {
@@ -362,6 +440,8 @@ impl AsyncWorkLane {
state.active_cancel_token = None;
}
state.telemetry.record_closed(job_id, outcome);
state.progress = state.progress.saturating_add(1);
self.progressed.notify_all();
}
}
@@ -415,16 +495,19 @@ impl Drop for AsyncWorkLaneController {
}
fn run_async_work_loop(lane: Arc<AsyncWorkLane>) {
while let Some(job) = lane.wait_take() {
while let Some(mut job) = lane.wait_take() {
let context = AsyncWorkJobContext {
job_id: job.id,
kind: job.kind,
priority: job.priority,
cancel_token: job.cancel_token,
cancel_token: job.cancel_token.clone(),
};
let result = catch_unwind(AssertUnwindSafe(|| (job.work)(context)))
.unwrap_or(AsyncWorkJobOutcome::Failed);
lane.record_finished(job.id, result);
.unwrap_or(AsyncWorkStep::Complete(AsyncWorkJobOutcome::Failed));
match result {
AsyncWorkStep::Yield => lane.yield_job(job),
AsyncWorkStep::Complete(outcome) => lane.record_finished(job.id, outcome),
}
}
}
@@ -599,3 +682,63 @@ mod tests {
assert_eq!(lane.telemetry().shutdown_discards, 1);
}
}
#[cfg(test)]
mod step_tests {
use super::*;
#[test]
fn yielded_asset_resumes_same_job_after_priority_save() {
let lane = Arc::new(AsyncWorkLane::default());
let events = Arc::new(Mutex::new(Vec::new()));
let nested = lane.clone();
let log = events.clone();
let mut step = 0;
let mut identity = None;
lane.submit_steps(AsyncWorkJobKind::Asset, AsyncWorkPriority::AssetLoad, move |context| {
assert_eq!(*identity.get_or_insert(context.job_id), context.job_id);
if step == 0 {
log.lock().unwrap().push("asset first");
let log = log.clone();
nested
.submit_memcard_commit_write(move |_| {
log.lock().unwrap().push("save");
AsyncWorkJobOutcome::Completed
})
.unwrap();
step += 1;
AsyncWorkStep::Yield
} else {
log.lock().unwrap().push("asset resumes");
AsyncWorkStep::Complete(AsyncWorkJobOutcome::Completed)
}
})
.unwrap();
let mut controller =
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
let deadline = std::time::Instant::now() + Duration::from_secs(1);
while lane.telemetry().completed_jobs < 2 {
assert!(std::time::Instant::now() < deadline);
std::thread::yield_now();
}
controller.stop().unwrap();
assert_eq!(*events.lock().unwrap(), vec!["asset first", "save", "asset resumes"]);
}
}
impl prometeu_hal::background_work::BackgroundWork for AsyncWorkLane {
fn submit_asset(
&self,
mut work: prometeu_hal::background_work::AssetWork,
) -> Result<(), prometeu_hal::background_work::WorkUnavailable> {
use prometeu_hal::background_work::{WorkStep, WorkUnavailable};
self.submit_steps(AsyncWorkJobKind::Asset, AsyncWorkPriority::AssetLoad, move |context| {
match work(context.is_canceled()) {
WorkStep::Yield => AsyncWorkStep::Yield,
WorkStep::Complete => AsyncWorkStep::Complete(AsyncWorkJobOutcome::Completed),
WorkStep::Failed => AsyncWorkStep::Complete(AsyncWorkJobOutcome::Failed),
}
})
.map(|_| ())
.map_err(|_| WorkUnavailable)
}
}
@@ -304,6 +304,7 @@ mod tests {
let entry = GameLibraryEntry::new(
path.clone(),
CartridgeManifest {
memcard_slots: None,
magic: "PMTU".to_string(),
cartridge_version: 1,
app_id: 42,
@@ -1,481 +1,21 @@
use crate::fs::{FsError, VirtualFS};
use crate::services::async_work::{AsyncWorkJobKind, AsyncWorkPriority};
use std::collections::HashMap;
pub const MEMCARD_SLOT_COUNT: usize = 32;
pub const MEMCARD_SLOT_CAPACITY_BYTES: usize = 32 * 1024;
const SLOT_FILE_MAGIC: &[u8; 4] = b"PMMS";
const SLOT_FILE_VERSION: u8 = 1;
const SLOT_HEADER_SIZE: usize = 4 + 1 + 16 + 8 + 4 + 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum MemcardStatus {
Ok = 0,
Empty = 1,
NotFound = 2,
NoSpace = 3,
AccessDenied = 4,
Corrupt = 5,
Conflict = 6,
Unavailable = 7,
InvalidState = 8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum MemcardSlotState {
Empty = 0,
Staged = 1,
Committed = 2,
Corrupt = 3,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemcardSlotStat {
pub state: MemcardSlotState,
pub used_bytes: u32,
pub generation: u64,
pub checksum: u32,
}
pub struct MemcardReadResult {
pub status: MemcardStatus,
pub bytes: Vec<u8>,
pub bytes_read: u32,
}
pub struct MemcardWriteResult {
pub status: MemcardStatus,
pub bytes_written: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemcardAsyncLaneOperation {
SlotWrite,
SlotCommit,
SlotClear,
SlotRead,
SlotStat,
}
impl MemcardAsyncLaneOperation {
pub const fn job_kind(self) -> AsyncWorkJobKind {
AsyncWorkJobKind::Memcard
}
pub const fn priority(self) -> AsyncWorkPriority {
match self {
Self::SlotWrite | Self::SlotCommit | Self::SlotClear => {
AsyncWorkPriority::MemcardCommitWrite
}
Self::SlotRead | Self::SlotStat => AsyncWorkPriority::NonCriticalReadList,
}
}
}
#[derive(Debug, Clone)]
struct SlotImage {
payload: Vec<u8>,
generation: u64,
checksum: u32,
save_uuid: [u8; 16],
}
//! Shared image-backed memory cards. Guest operations use RAM snapshots; only the
//! shared IO worker owns storage during a publication. Legacy per-slot files are
//! intentionally neither read nor migrated by this service.
pub mod admission;
pub mod guest;
pub mod image;
pub mod session;
pub mod transaction;
#[derive(Default)]
pub struct MemcardService {
staged: HashMap<(u32, u8), Vec<u8>>,
pub session: session::Session,
pub lane: std::sync::Arc<crate::AsyncWorkLane>,
pub admission: admission::Admission,
pub diagnostic: String,
}
impl MemcardService {
pub fn new() -> Self {
Self::default()
}
pub fn clear_all_staging(&mut self) {
self.staged.clear();
}
pub fn slot_count(&self) -> usize {
MEMCARD_SLOT_COUNT
}
pub fn slot_stat(&self, fs: &VirtualFS, app_id: u32, slot: u8) -> MemcardSlotStat {
if let Some(staged_payload) = self.staged.get(&(app_id, slot)) {
let generation = match self.load_committed(fs, app_id, slot) {
Ok(Some(committed)) => committed.generation.saturating_add(1),
_ => 1,
};
return MemcardSlotStat {
state: MemcardSlotState::Staged,
used_bytes: staged_payload.len() as u32,
generation,
checksum: checksum32(staged_payload),
};
}
match self.load_committed(fs, app_id, slot) {
Ok(Some(committed)) => MemcardSlotStat {
state: MemcardSlotState::Committed,
used_bytes: committed.payload.len() as u32,
generation: committed.generation,
checksum: committed.checksum,
},
Ok(None) => MemcardSlotStat {
state: MemcardSlotState::Empty,
used_bytes: 0,
generation: 0,
checksum: 0,
},
Err(MemcardStatus::Corrupt) => MemcardSlotStat {
state: MemcardSlotState::Corrupt,
used_bytes: 0,
generation: 0,
checksum: 0,
},
Err(_) => MemcardSlotStat {
state: MemcardSlotState::Empty,
used_bytes: 0,
generation: 0,
checksum: 0,
},
}
}
pub fn slot_read(
&self,
fs: &VirtualFS,
app_id: u32,
slot: u8,
offset: usize,
max_bytes: usize,
) -> MemcardReadResult {
if let Some(staged_payload) = self.staged.get(&(app_id, slot)) {
return Self::slice_payload(staged_payload, offset, max_bytes);
}
match self.load_committed(fs, app_id, slot) {
Ok(Some(committed)) => Self::slice_payload(&committed.payload, offset, max_bytes),
Ok(None) => {
MemcardReadResult { status: MemcardStatus::Empty, bytes: Vec::new(), bytes_read: 0 }
}
Err(status) => MemcardReadResult { status, bytes: Vec::new(), bytes_read: 0 },
}
}
pub fn slot_write(
&mut self,
fs: &VirtualFS,
app_id: u32,
slot: u8,
offset: usize,
data: &[u8],
) -> MemcardWriteResult {
let end = match offset.checked_add(data.len()) {
Some(v) => v,
None => {
return MemcardWriteResult { status: MemcardStatus::NoSpace, bytes_written: 0 };
}
};
if end > MEMCARD_SLOT_CAPACITY_BYTES {
return MemcardWriteResult { status: MemcardStatus::NoSpace, bytes_written: 0 };
}
let mut payload = if let Some(staged_payload) = self.staged.get(&(app_id, slot)) {
staged_payload.clone()
} else {
match self.load_committed(fs, app_id, slot) {
Ok(Some(committed)) => committed.payload,
Ok(None) => Vec::new(),
Err(status) => {
return MemcardWriteResult { status, bytes_written: 0 };
}
}
};
if offset > payload.len() {
payload.resize(offset, 0);
}
if end > payload.len() {
payload.resize(end, 0);
}
payload[offset..end].copy_from_slice(data);
self.staged.insert((app_id, slot), payload);
MemcardWriteResult { status: MemcardStatus::Ok, bytes_written: data.len() as u32 }
}
pub fn slot_commit(&mut self, fs: &mut VirtualFS, app_id: u32, slot: u8) -> MemcardStatus {
let Some(staged_payload) = self.staged.get(&(app_id, slot)).cloned() else {
return MemcardStatus::InvalidState;
};
let (save_uuid, generation) = match self.load_committed(fs, app_id, slot) {
Ok(Some(committed)) => (committed.save_uuid, committed.generation.saturating_add(1)),
Ok(None) => (make_save_uuid(app_id, slot), 1),
Err(status) => return status,
};
let checksum = checksum32(&staged_payload);
let encoded = encode_slot_file(SlotImage {
payload: staged_payload,
generation,
checksum,
save_uuid,
});
let path = slot_path(app_id, slot);
match fs.write_file(&path, &encoded) {
Ok(()) => {
self.staged.remove(&(app_id, slot));
MemcardStatus::Ok
}
Err(err) => map_fs_error(err),
}
}
pub fn slot_clear(&mut self, fs: &mut VirtualFS, app_id: u32, slot: u8) -> MemcardStatus {
self.staged.remove(&(app_id, slot));
let path = slot_path(app_id, slot);
match fs.delete(&path) {
Ok(()) => MemcardStatus::Ok,
Err(FsError::NotFound) => MemcardStatus::Empty,
Err(err) => map_fs_error(err),
}
}
fn slice_payload(payload: &[u8], offset: usize, max_bytes: usize) -> MemcardReadResult {
if offset >= payload.len() || max_bytes == 0 {
return MemcardReadResult {
status: MemcardStatus::Ok,
bytes: Vec::new(),
bytes_read: 0,
};
}
let end = payload.len().min(offset.saturating_add(max_bytes));
let bytes = payload[offset..end].to_vec();
MemcardReadResult { status: MemcardStatus::Ok, bytes_read: bytes.len() as u32, bytes }
}
fn load_committed(
&self,
fs: &VirtualFS,
app_id: u32,
slot: u8,
) -> Result<Option<SlotImage>, MemcardStatus> {
let path = slot_path(app_id, slot);
match fs.read_file(&path) {
Ok(bytes) => decode_slot_file(&bytes).map(Some),
Err(FsError::NotFound) => Ok(None),
Err(err) => Err(map_fs_error(err)),
}
}
}
fn slot_path(app_id: u32, slot: u8) -> String {
format!("/user/games/{}/memcard/slot_{:02}.pmem", app_id, slot)
}
fn map_fs_error(err: FsError) -> MemcardStatus {
match err {
FsError::NotFound => MemcardStatus::NotFound,
FsError::AlreadyExists => MemcardStatus::Conflict,
FsError::PermissionDenied => MemcardStatus::AccessDenied,
FsError::NotMounted | FsError::IOError(_) | FsError::Other(_) | FsError::InvalidPath(_) => {
MemcardStatus::Unavailable
}
}
}
fn checksum32(data: &[u8]) -> u32 {
let mut a: u32 = 1;
let mut b: u32 = 0;
const MOD: u32 = 65521;
for &byte in data {
a = (a + byte as u32) % MOD;
b = (b + a) % MOD;
}
(b << 16) | a
}
fn make_save_uuid(app_id: u32, slot: u8) -> [u8; 16] {
let mut out = [0u8; 16];
out[0..4].copy_from_slice(&app_id.to_le_bytes());
out[4] = slot;
out[5..13].copy_from_slice(
&(std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos() as u64)
.unwrap_or(0))
.to_le_bytes(),
);
out[13] = 0x50;
out[14] = 0x4D;
out[15] = 0x31;
out
}
fn encode_slot_file(slot: SlotImage) -> Vec<u8> {
let mut out = Vec::with_capacity(SLOT_HEADER_SIZE + slot.payload.len());
out.extend_from_slice(SLOT_FILE_MAGIC);
out.push(SLOT_FILE_VERSION);
out.extend_from_slice(&slot.save_uuid);
out.extend_from_slice(&slot.generation.to_le_bytes());
out.extend_from_slice(&slot.checksum.to_le_bytes());
out.extend_from_slice(&(slot.payload.len() as u32).to_le_bytes());
out.extend_from_slice(&slot.payload);
out
}
fn decode_slot_file(bytes: &[u8]) -> Result<SlotImage, MemcardStatus> {
if bytes.len() < SLOT_HEADER_SIZE {
return Err(MemcardStatus::Corrupt);
}
if &bytes[0..4] != SLOT_FILE_MAGIC {
return Err(MemcardStatus::Corrupt);
}
if bytes[4] != SLOT_FILE_VERSION {
return Err(MemcardStatus::Corrupt);
}
let mut save_uuid = [0u8; 16];
save_uuid.copy_from_slice(&bytes[5..21]);
let generation =
u64::from_le_bytes(bytes[21..29].try_into().map_err(|_| MemcardStatus::Corrupt)?);
let checksum =
u32::from_le_bytes(bytes[29..33].try_into().map_err(|_| MemcardStatus::Corrupt)?);
let payload_size =
u32::from_le_bytes(bytes[33..37].try_into().map_err(|_| MemcardStatus::Corrupt)?) as usize;
if payload_size > MEMCARD_SLOT_CAPACITY_BYTES {
return Err(MemcardStatus::Corrupt);
}
if bytes.len() != SLOT_HEADER_SIZE + payload_size {
return Err(MemcardStatus::Corrupt);
}
let payload = bytes[SLOT_HEADER_SIZE..].to_vec();
if checksum32(&payload) != checksum {
return Err(MemcardStatus::Corrupt);
}
Ok(SlotImage { payload, generation, checksum, save_uuid })
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fs::{FsBackend, FsEntry};
use std::collections::HashMap;
struct MockBackend {
files: HashMap<String, Vec<u8>>,
}
impl MockBackend {
fn new() -> Self {
Self { files: HashMap::new() }
}
}
impl FsBackend for MockBackend {
fn mount(&mut self) -> Result<(), FsError> {
Ok(())
}
fn unmount(&mut self) {}
fn list_dir(&self, _path: &str) -> Result<Vec<FsEntry>, FsError> {
Ok(Vec::new())
}
fn read_file(&self, path: &str) -> Result<Vec<u8>, FsError> {
self.files.get(path).cloned().ok_or(FsError::NotFound)
}
fn write_file(&mut self, path: &str, data: &[u8]) -> Result<(), FsError> {
self.files.insert(path.to_string(), data.to_vec());
Ok(())
}
fn delete(&mut self, path: &str) -> Result<(), FsError> {
self.files.remove(path).map(|_| ()).ok_or(FsError::NotFound)
}
fn exists(&self, path: &str) -> bool {
self.files.contains_key(path)
}
fn is_healthy(&self) -> bool {
true
}
}
#[test]
fn slot_roundtrip_commit_and_generation() {
let mut fs = VirtualFS::new();
fs.mount(Box::new(MockBackend::new())).expect("mount");
let mut mem = MemcardService::new();
let write = mem.slot_write(&fs, 10, 0, 0, b"hello");
assert_eq!(write.status, MemcardStatus::Ok);
assert_eq!(write.bytes_written, 5);
let staged = mem.slot_stat(&fs, 10, 0);
assert_eq!(staged.state, MemcardSlotState::Staged);
assert_eq!(staged.generation, 1);
assert_eq!(mem.slot_commit(&mut fs, 10, 0), MemcardStatus::Ok);
let committed = mem.slot_stat(&fs, 10, 0);
assert_eq!(committed.state, MemcardSlotState::Committed);
assert_eq!(committed.generation, 1);
let write2 = mem.slot_write(&fs, 10, 0, 5, b"!");
assert_eq!(write2.status, MemcardStatus::Ok);
assert_eq!(mem.slot_commit(&mut fs, 10, 0), MemcardStatus::Ok);
let committed2 = mem.slot_stat(&fs, 10, 0);
assert_eq!(committed2.generation, 2);
let read = mem.slot_read(&fs, 10, 0, 0, 10);
assert_eq!(read.status, MemcardStatus::Ok);
assert_eq!(read.bytes, b"hello!");
assert_eq!(read.bytes_read, 6);
}
#[test]
fn slot_clear_missing_is_empty() {
let mut fs = VirtualFS::new();
fs.mount(Box::new(MockBackend::new())).expect("mount");
let mut mem = MemcardService::new();
assert_eq!(mem.slot_clear(&mut fs, 1, 1), MemcardStatus::Empty);
}
#[test]
fn slot_stat_reports_corruption() {
let mut fs = VirtualFS::new();
let mut backend = MockBackend::new();
backend.files.insert(slot_path(7, 2), vec![0, 1, 2, 3, 4]);
fs.mount(Box::new(backend)).expect("mount");
let mem = MemcardService::new();
let stat = mem.slot_stat(&fs, 7, 2);
assert_eq!(stat.state, MemcardSlotState::Corrupt);
}
#[test]
fn memcard_async_lane_operations_classify_priority_without_changing_public_api() {
assert_eq!(MemcardAsyncLaneOperation::SlotWrite.job_kind(), AsyncWorkJobKind::Memcard);
assert_eq!(
MemcardAsyncLaneOperation::SlotWrite.priority(),
AsyncWorkPriority::MemcardCommitWrite
);
assert_eq!(
MemcardAsyncLaneOperation::SlotCommit.priority(),
AsyncWorkPriority::MemcardCommitWrite
);
assert_eq!(
MemcardAsyncLaneOperation::SlotClear.priority(),
AsyncWorkPriority::MemcardCommitWrite
);
assert_eq!(
MemcardAsyncLaneOperation::SlotRead.priority(),
AsyncWorkPriority::NonCriticalReadList
);
assert_eq!(
MemcardAsyncLaneOperation::SlotStat.priority(),
AsyncWorkPriority::NonCriticalReadList
);
}
}
@@ -0,0 +1,363 @@
//! External SO/Hub choices prepare the snapshot before any game boot execution.
use super::{
MemcardService,
image::*,
session::Session,
transaction::{Card, PublicationControl},
};
use prometeu_hal::memcard::MemcardStorage;
use std::sync::{Arc, Mutex, mpsc};
use std::time::{Duration, Instant};
pub const DEFAULT_SNAPSHOT_BUDGET: usize = 5 * 1024 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Choice {
Persistent,
MemoryFresh,
MemoryExisting,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PreparationState {
Idle,
Pending,
Ready,
Failed(Error),
}
enum Prepared {
Card(Result<Card>),
Session(Session, Result<()>),
Replacement(Session, Result<()>),
Maintenance(Card, Result<()>),
}
pub struct Admission {
installed: Option<Card>,
receiver: Option<mpsc::Receiver<Prepared>>,
admitted: Option<Instant>,
control: Option<Arc<PublicationControl>>,
pub state: PreparationState,
pub snapshot_budget: usize,
faulted: bool,
}
impl Default for Admission {
fn default() -> Self {
Self {
installed: None,
receiver: None,
admitted: None,
control: None,
state: PreparationState::Idle,
snapshot_budget: DEFAULT_SNAPSHOT_BUDGET,
faulted: false,
}
}
}
impl MemcardService {
pub fn host_faulted(&self) -> bool {
self.admission.faulted || self.session.host_faulted()
}
pub fn card_installed(&self) -> bool {
self.admission.installed.is_some()
}
pub fn installed_health(&self) -> Option<(bool, bool, bool)> {
self.admission.installed.as_ref().map(|card| {
(card.recovered, card.degraded, card.write_blocked || card.storage_read_only())
})
}
pub fn card_has_game(&self, app_id: u32) -> bool {
self.admission
.installed
.as_ref()
.is_some_and(|c| c.catalog.games.iter().any(|g| g.app_id == app_id))
}
pub fn uninstall_card(&mut self) -> Result<()> {
if self.session.mode().is_some()
|| self.session.has_in_flight()
|| self.admission.receiver.is_some()
{
return Err(Error::Busy);
}
self.admission.installed = None;
self.admission.state = PreparationState::Idle;
Ok(())
}
pub fn mount_card(&mut self, storage: Box<dyn MemcardStorage>) -> Result<()> {
self.mount_card_with(move || Ok(storage))
}
pub fn mount_card_with(
&mut self,
open: impl FnOnce() -> Result<Box<dyn MemcardStorage>> + Send + 'static,
) -> Result<()> {
if self.admission.installed.is_some()
|| self.admission.receiver.is_some()
|| self.session.mode().is_some()
|| self.session.has_in_flight()
{
return Err(Error::Busy);
}
if self.admission.faulted {
return Err(Error::ReadOnly);
}
let (tx, rx) = mpsc::channel();
self.lane
.submit_memcard_commit_write(move |_| {
let _ = tx.send(Prepared::Card(open().and_then(Card::mount)));
crate::AsyncWorkJobOutcome::Completed
})
.map_err(|_| Error::InvalidState)?;
self.admission.receiver = Some(rx);
self.admission.admitted = Some(Instant::now());
self.admission.state = PreparationState::Pending;
Ok(())
}
pub fn choose_for_game(
&mut self,
app_id: u32,
count: usize,
choice: Choice,
created_at: u64,
) -> Result<()> {
if self.admission.receiver.is_some()
|| self.session.mode().is_some()
|| self.session.has_in_flight()
{
return Err(Error::Busy);
}
if self.admission.faulted {
return Err(Error::ReadOnly);
}
if choice != Choice::MemoryFresh && self.admission.installed.is_none() {
return Err(Error::InvalidState);
}
if Session::required_bytes(count)? > self.admission.snapshot_budget {
return Err(Error::OutOfMemory);
}
if let Some(card) = &self.admission.installed {
let owned =
card.catalog.games.iter().find(|g| g.app_id == app_id).map_or(0, |g| g.slots.len());
if choice == Choice::Persistent {
if card.storage_read_only() || card.write_blocked {
return Err(Error::ReadOnly);
}
if count.saturating_sub(owned)
> card.current().slots.iter().filter(|s| !s.assigned).count()
{
return Err(Error::NoSpace);
}
}
if choice == Choice::MemoryExisting && owned == 0 {
return Err(Error::InvalidState);
}
}
let handoff = Arc::new(Mutex::new(self.admission.installed.take()));
let worker_handoff = handoff.clone();
let control = Arc::new(PublicationControl::default());
let worker_control = control.clone();
let budget = self.admission.snapshot_budget;
let (tx, rx) = mpsc::channel();
let submitted = self.lane.submit_memcard_commit_write(move |_| {
let mut card = worker_handoff.lock().unwrap().take();
let mut session = Session::default();
let result = (|| {
if worker_control.canceled() {
session.retain_idle_card_opt(card.take());
return Err(Error::Canceled);
}
match choice {
Choice::MemoryFresh => {
session.start_memory_with_card(app_id, count, budget, card.take())?;
}
Choice::MemoryExisting => {
session.load_card_memory(card.take().unwrap(), app_id, count, budget)?;
}
Choice::Persistent => {
let mut card = card.take().unwrap();
if card.storage_read_only() || card.write_blocked {
session.retain_idle_card(card);
return Err(Error::ReadOnly);
}
if card.catalog.games.iter().any(|g| g.app_id == app_id) {
if let Err(e) = card.grow(app_id, count, &worker_control) {
session.retain_idle_card(card);
return Err(e);
}
session.load_card(card, app_id, count, budget)?;
} else {
session.prepare_new_card(card, app_id, count, created_at, budget)?;
}
}
}
session.begin_boot();
Ok(())
})();
let _ = tx.send(Prepared::Session(session, result));
crate::AsyncWorkJobOutcome::Completed
});
if submitted.is_err() {
self.admission.installed = handoff.lock().unwrap().take();
return Err(Error::InvalidState);
}
self.admission.control = Some(control);
self.admission.receiver = Some(rx);
self.admission.admitted = Some(Instant::now());
self.admission.state = PreparationState::Pending;
Ok(())
}
/// Whole-game removal is only available outside a resident game session.
pub fn remove_game(&mut self, app_id: u32) -> Result<()> {
self.maintain_card(move |card, control| card.remove_game(app_id, control))
}
pub fn revalidate_card(&mut self) -> Result<()> {
self.maintain_card(|card, control| card.revalidate(control))
}
pub fn reintegrate_block(&mut self, block: u8) -> Result<()> {
self.maintain_card(move |card, control| card.reintegrate(block, control))
}
fn maintain_card(
&mut self,
operation: impl FnOnce(&mut Card, &PublicationControl) -> Result<()> + Send + 'static,
) -> Result<()> {
if self.admission.receiver.is_some()
|| self.session.mode().is_some()
|| self.session.has_in_flight()
{
return Err(Error::Busy);
}
if self.admission.faulted {
return Err(Error::ReadOnly);
}
let card = self.admission.installed.take().ok_or(Error::InvalidState)?;
let handoff = Arc::new(Mutex::new(Some(card)));
let worker = handoff.clone();
let control = Arc::new(PublicationControl::default());
let worker_control = control.clone();
let (tx, rx) = mpsc::channel();
if self
.lane
.submit_memcard_commit_write(move |_| {
let mut card = worker.lock().unwrap().take().unwrap();
let result = operation(&mut card, &worker_control);
let _ = tx.send(Prepared::Maintenance(card, result));
crate::AsyncWorkJobOutcome::Completed
})
.is_err()
{
self.admission.installed = handoff.lock().unwrap().take();
return Err(Error::InvalidState);
}
self.admission.control = Some(control);
self.admission.receiver = Some(rx);
self.admission.admitted = Some(Instant::now());
self.admission.state = PreparationState::Pending;
Ok(())
}
/// Caller must suspend gameplay and obtain the player's keep-RAM/load-existing choice.
/// Once admitted, the resident continuation must wait for `PreparationState::Ready`.
pub fn replace_card(
&mut self,
storage: Box<dyn MemcardStorage>,
load_existing: bool,
) -> Result<()> {
if self.admission.receiver.is_some()
|| self.session.has_in_flight()
|| self.session.is_boot_active()
{
return Err(Error::Busy);
}
if self.admission.faulted {
return Err(Error::ReadOnly);
}
let handoff = Arc::new(Mutex::new(Some(std::mem::take(&mut self.session))));
let worker = handoff.clone();
let control = Arc::new(PublicationControl::default());
let worker_control = control.clone();
let (tx, rx) = mpsc::channel();
if self
.lane
.submit_memcard_commit_write(move |_| {
let mut session = worker.lock().unwrap().take().unwrap();
let result = session.replace_storage(storage, load_existing, &worker_control);
let _ = tx.send(Prepared::Replacement(session, result));
crate::AsyncWorkJobOutcome::Completed
})
.is_err()
{
self.session = handoff.lock().unwrap().take().unwrap();
return Err(Error::InvalidState);
}
self.admission.control = Some(control);
self.admission.receiver = Some(rx);
self.admission.admitted = Some(Instant::now());
self.admission.state = PreparationState::Pending;
Ok(())
}
pub fn poll(&mut self, now: Instant) {
self.session.poll(now);
if let Some(rx) = &self.admission.receiver {
match rx.try_recv() {
Ok(prepared) => {
if !self.admission.faulted {
self.admission.state = match prepared {
Prepared::Card(Ok(card)) => {
self.admission.installed = Some(card);
PreparationState::Ready
}
Prepared::Maintenance(card, result) => {
self.admission.installed = Some(card);
match result {
Ok(()) => PreparationState::Ready,
Err(e) => PreparationState::Failed(e),
}
}
Prepared::Replacement(session, result) => {
self.session = session;
match result {
Ok(()) => PreparationState::Ready,
Err(e) => PreparationState::Failed(e),
}
}
Prepared::Session(session, Ok(())) => {
self.session = session;
PreparationState::Ready
}
Prepared::Card(Err(e)) => PreparationState::Failed(e),
Prepared::Session(mut session, Err(e)) => {
session.end_session();
self.admission.installed = session.take_idle_card().ok().flatten();
PreparationState::Failed(e)
}
};
} else if let Prepared::Replacement(session, _) = prepared {
self.session = session;
}
self.admission.receiver = None;
self.admission.control = None;
}
Err(mpsc::TryRecvError::Disconnected) => {
self.admission.state = PreparationState::Failed(Error::InvalidState);
self.admission.receiver = None;
}
Err(mpsc::TryRecvError::Empty) => {
if self.admission.admitted.is_some_and(|at| {
now.saturating_duration_since(at) >= Duration::from_millis(500)
}) && let Some(control) = &self.admission.control
{
control.cancel();
}
if self.admission.admitted.is_some_and(|at| {
now.saturating_duration_since(at) >= Duration::from_secs(1)
}) {
self.admission.faulted = true;
self.admission.state = PreparationState::Failed(Error::Indeterminate);
}
}
}
}
if self.session.mode().is_none()
&& !self.session.has_in_flight()
&& self.admission.installed.is_none()
&& let Ok(card) = self.session.take_idle_card()
{
self.admission.installed = card;
}
}
}
@@ -0,0 +1,256 @@
//! Guest mem ABI. Only bounded copies cross the VM/native ownership boundary.
use super::{
MemcardService,
image::Error,
session::{Mode, RequestReason, RequestState},
};
use prometeu_bytecode::{HeapRef, TRAP_OOB, TRAP_TYPE, Value};
use prometeu_hal::{HostReturn, expect_int, syscalls::Syscall, vm_fault::VmFault};
use std::time::Instant;
fn index(args: &[Value], n: usize) -> Result<usize, VmFault> {
usize::try_from(expect_int(args, n)?)
.map_err(|_| VmFault::Trap(TRAP_OOB, "Negative memcard range".into()))
}
fn opaque(args: &[Value], n: usize) -> Result<u64, VmFault> {
u64::try_from(expect_int(args, n)?)
.map_err(|_| VmFault::Trap(TRAP_OOB, "Negative memcard identity".into()))
}
fn handle(args: &[Value], n: usize) -> Result<HeapRef, VmFault> {
match args.get(n) {
Some(Value::HeapRef(h)) => Ok(*h),
_ => Err(VmFault::Trap(TRAP_TYPE, "Expected Array<Int32>".into())),
}
}
fn text(args: &[Value], n: usize) -> Result<&str, VmFault> {
match args.get(n) {
Some(Value::String(s)) => Ok(s),
_ => Err(VmFault::Trap(TRAP_TYPE, "Expected String".into())),
}
}
pub fn status(e: Error) -> i64 {
use prometeu_hal::memcard::MemcardIoError as I;
match e {
Error::NoSpace | Error::Io(I::NoSpace) => 3,
Error::AccessDenied => 4,
Error::Corrupt => 5,
Error::Indeterminate => 6,
Error::Io(I::Unavailable) => 7,
Error::InvalidState | Error::Canceled => 8,
Error::Busy => 9,
Error::ReadOnly | Error::CounterExhausted | Error::Io(I::ReadOnly) => 10,
Error::InvalidRange | Error::Io(I::InvalidRange) => 11,
Error::Unsupported | Error::Io(I::Unsupported) => 12,
Error::Io(I::IoError) => 13,
Error::OutOfMemory => 16,
}
}
fn error(ret: &mut HostReturn, syscall: Syscall, e: Error) {
ret.push_int(status(e));
for _ in 1..syscall.results_count() {
ret.push_null();
}
}
impl MemcardService {
pub fn guest(
&mut self,
syscall: Syscall,
args: &[Value],
ret: &mut HostReturn,
app_id: u32,
timestamp: u64,
game_profile: bool,
) -> Result<(), VmFault> {
let authorized = game_profile && self.session.app_id() == Some(app_id);
if syscall == Syscall::MemBootstrap {
if !authorized || !self.session.is_boot_active() {
return Err(VmFault::Trap(
TRAP_TYPE,
"Memcard snapshot is not admitted for bootstrap".into(),
));
}
ret.push_int(self.session.context() as i64);
return Ok(());
}
let context = opaque(args, 0)?;
if !authorized || context != self.session.context() {
error(ret, syscall, Error::AccessDenied);
return Ok(());
}
match syscall {
Syscall::MemSlotCount => {
ret.push_int(0);
ret.push_int(self.session.count() as i64);
}
Syscall::MemSlotStat => match self.session.descriptor(context, index(args, 1)?) {
Ok((d, used, dirty)) => {
ret.push_int(0);
ret.push_int(i64::from(used > 0));
ret.push_int(used as i64);
ret.push_int(d.generation as i64);
ret.push_int(d.update_at as i64);
ret.push_int(d.crc as i64);
ret.push_bool(dirty);
}
Err(e) => error(ret, syscall, e),
},
Syscall::MemSlotRead | Syscall::MemSlotWrite => {
let local = index(args, 1)?;
let offset = index(args, 2)?;
let array = handle(args, 3)?;
let array_offset = index(args, 4)?;
let count = index(args, 5)?;
if count > 4096 {
return Err(VmFault::Trap(
TRAP_OOB,
"Memcard transfer exceeds 4096 bytes".into(),
));
}
if let Err(e) = self.session.descriptor(context, local) {
error(ret, syscall, e);
return Ok(());
}
if offset.checked_add(count).is_none_or(|end| end > 65536) {
error(ret, syscall, Error::InvalidRange);
return Ok(());
}
ret.byte_arrays()?.validate(array, array_offset, count)?;
let mut bytes = [0; 4096];
if syscall == Syscall::MemSlotRead {
match self.session.read(context, local, offset, &mut bytes[..count]) {
Ok(n) => {
ret.byte_arrays()?.write(array, array_offset, &bytes[..n])?;
ret.push_int(0);
ret.push_int(n as i64)
}
Err(e) => error(ret, syscall, e),
}
} else {
ret.byte_arrays()?.read(array, array_offset, &mut bytes[..count])?;
match self.session.write(context, local, offset, &bytes[..count]) {
Ok(()) => {
ret.push_int(0);
ret.push_int(count as i64)
}
Err(e) => error(ret, syscall, e),
}
}
}
Syscall::MemSlotCommit => {
match self.session.commit(
context,
index(args, 1)?,
timestamp,
Instant::now(),
&self.lane,
) {
Ok(id) => {
ret.push_int(0);
ret.push_int(id as i64)
}
Err(e) => error(ret, syscall, e),
}
}
Syscall::MemSlotClear => match self.session.clear(context, index(args, 1)?) {
Ok(()) => ret.push_int(0),
Err(e) => error(ret, syscall, e),
},
Syscall::MemQuery => match self.session.query(context, opaque(args, 1)?) {
Ok(r) => {
ret.push_int(0);
ret.push_int(match r.state {
RequestState::Pending => 0,
RequestState::Success => 1,
RequestState::Failure => 2,
RequestState::Indeterminate => 3,
RequestState::MemoryOnly => 4,
});
ret.push_int(match r.reason {
RequestReason::None => 0,
RequestReason::Storage(e) => status(e),
RequestReason::Timeout | RequestReason::RestartRequired => 15,
});
}
Err(e) => error(ret, syscall, e),
},
Syscall::MemRelease => match self.session.release_request(context, opaque(args, 1)?) {
Ok(()) => ret.push_int(0),
Err(e) => error(ret, syscall, e),
},
Syscall::MemInfo => {
ret.push_int(0);
ret.push_int(i64::from(self.session.mode() == Some(Mode::MemoryOnly)));
ret.push_bool(self.session.is_new());
let (recovered, degraded, blocked) = self.session.health();
ret.push_bool(recovered);
ret.push_bool(degraded);
ret.push_bool(blocked);
}
Syscall::MemMetadata => match self.session.presentation(context) {
Ok(Some(g)) => {
ret.push_int(0);
ret.push_string(&g.title)?;
ret.push_int(g.data_format_version as i64);
ret.push_int(g.created_at as i64)
}
Ok(None) => {
ret.push_int(0);
ret.push_string("")?;
ret.push_int(0);
ret.push_int(timestamp as i64)
}
Err(e) => error(ret, syscall, e),
},
Syscall::MemPresentation => {
let title = text(args, 1)?;
let version = u32::try_from(index(args, 2)?)
.map_err(|_| VmFault::Trap(TRAP_OOB, "Invalid dataFormatVersion".into()))?;
let frames = u8::try_from(index(args, 3)?)
.map_err(|_| VmFault::Trap(TRAP_OOB, "Invalid icon frames".into()))?;
let duration = u16::try_from(index(args, 4)?)
.map_err(|_| VmFault::Trap(TRAP_OOB, "Invalid icon duration".into()))?;
let array = handle(args, 5)?;
let offset = index(args, 6)?;
let count = index(args, 7)?;
if count > 2096 || title.len() > 128 {
error(ret, syscall, Error::InvalidRange);
return Ok(());
}
let mut bytes = [0; 2096];
ret.byte_arrays()?.read(array, offset, &mut bytes[..count])?;
match self.session.set_presentation(
context,
title,
version,
frames,
duration,
&bytes[..count],
timestamp,
) {
Ok(()) => ret.push_int(0),
Err(e) => error(ret, syscall, e),
}
}
Syscall::MemDiagnostic => {
let message = text(args, 1)?;
if message.len() > 1024 {
error(ret, syscall, Error::InvalidRange)
} else {
self.diagnostic.clear();
self.diagnostic.push_str(message);
ret.push_int(0)
}
}
Syscall::MemPublishPresentation => {
match self.session.publish_presentation(context, &self.lane, Instant::now()) {
Ok(id) => {
ret.push_int(0);
ret.push_int(id as i64)
}
Err(e) => error(ret, syscall, e),
}
}
_ => return Err(VmFault::Trap(TRAP_TYPE, "Invalid memcard operation".into())),
}
Ok(())
}
}
@@ -0,0 +1,452 @@
//! Fixed shared-card codec. All offsets and CRC coverage are defined in the save spec.
use prometeu_hal::memcard::{MemcardIoError, MemcardStorage};
use serde::{Deserialize, Serialize};
pub const SLOT_COUNT: usize = 64;
pub const BLOCK_COUNT: usize = 68;
pub const SLOT_BYTES: usize = 65_536;
pub const PAGE_BYTES: usize = 4096;
pub const CATALOG_BYTES: usize = 131_072;
pub const ICON_BYTES: usize = 262_144;
pub const CATALOG_OFFSETS: [u64; 2] = [16_384, 409_600];
pub const ICON_OFFSETS: [u64; 2] = [147_456, 540_672];
pub const MAP_OFFSETS: [u64; 2] = [8192, 12_288];
pub const DATA_OFFSET: u64 = 802_816;
pub const IMAGE_BYTES: u64 = DATA_OFFSET + (BLOCK_COUNT * SLOT_BYTES) as u64;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
Io(MemcardIoError),
Corrupt,
Unsupported,
NoSpace,
InvalidRange,
CounterExhausted,
ReadOnly,
Indeterminate,
Busy,
Canceled,
OutOfMemory,
AccessDenied,
InvalidState,
}
impl From<MemcardIoError> for Error {
fn from(e: MemcardIoError) -> Self {
Self::Io(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub fn crc32c(bytes: &[u8]) -> u32 {
crc_extend(!0, bytes) ^ !0
}
pub fn crc_extend(mut crc: u32, bytes: &[u8]) -> u32 {
for &byte in bytes {
crc ^= byte as u32;
for _ in 0..8 {
crc = (crc >> 1) ^ (0x82f6_3b78 & 0u32.wrapping_sub(crc & 1));
}
}
crc
}
fn u32_at(b: &[u8], p: usize) -> u32 {
u32::from_le_bytes(b[p..p + 4].try_into().unwrap())
}
fn u64_at(b: &[u8], p: usize) -> u64 {
u64::from_le_bytes(b[p..p + 8].try_into().unwrap())
}
fn put32(b: &mut [u8], p: usize, n: u32) {
b[p..p + 4].copy_from_slice(&n.to_le_bytes());
}
fn put64(b: &mut [u8], p: usize, n: u64) {
b[p..p + 8].copy_from_slice(&n.to_le_bytes());
}
fn zero(b: &[u8]) -> Result<()> {
if b.iter().any(|&v| v != 0) { Err(Error::Corrupt) } else { Ok(()) }
}
fn page_crc(b: &[u8; PAGE_BYTES]) -> Result<()> {
if crc32c(&b[..4092]) != u32_at(b, 4092) {
return Err(Error::Corrupt);
}
Ok(())
}
fn seal(b: &mut [u8; PAGE_BYTES]) {
let crc = crc32c(&b[..4092]);
put32(b, 4092, crc);
}
pub fn identity_page(id: [u8; 16]) -> [u8; PAGE_BYTES] {
let mut b = [0; PAGE_BYTES];
b[..8].copy_from_slice(b"PMCARD1\0");
put32(&mut b, 8, 1);
put32(&mut b, 12, IMAGE_BYTES as u32);
b[16..32].copy_from_slice(&id);
seal(&mut b);
b
}
pub fn decode_identity(b: &[u8; PAGE_BYTES]) -> Result<[u8; 16]> {
if &b[..8] != b"PMCARD1\0" || u32_at(b, 8) != 1 {
return Err(Error::Unsupported);
}
page_crc(b)?;
if u32_at(b, 12) != IMAGE_BYTES as u32 {
return Err(Error::Corrupt);
}
zero(&b[32..4092])?;
Ok(b[16..32].try_into().unwrap())
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Icon {
pub offset: u32,
pub frames: u8,
pub duration: u16,
pub crc32c: u32,
}
impl Icon {
pub fn length(&self) -> Result<usize> {
if !(1..=4).contains(&self.frames)
|| self.duration == 0
|| (self.frames == 1 && self.duration != 1)
{
return Err(Error::Corrupt);
}
Ok(48 + 512 * self.frames as usize)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Game {
pub app_id: u32,
pub title: String,
#[serde(rename = "dataFormatVersion")]
pub data_format_version: u32,
pub created_at: u64,
pub slots: Vec<u8>,
pub icon: Icon,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Catalog {
#[serde(rename = "metadataVersion")]
pub metadata_version: u32,
pub games: Vec<Game>,
}
impl Default for Catalog {
fn default() -> Self {
Self { metadata_version: 1, games: Vec::new() }
}
}
impl Catalog {
pub fn validate(&self, icons_used: usize) -> Result<[bool; SLOT_COUNT]> {
if self.metadata_version != 1 || self.games.len() > 64 || icons_used > ICON_BYTES {
return Err(Error::Corrupt);
}
let mut assigned = [false; SLOT_COUNT];
let mut spans = Vec::with_capacity(64);
for (index, g) in self.games.iter().enumerate() {
if g.title.trim().is_empty()
|| g.title.len() > 128
|| g.slots.is_empty()
|| g.slots.len() > 64
|| self.games[..index].iter().any(|prior| prior.app_id == g.app_id)
{
return Err(Error::Corrupt);
}
for &slot in &g.slots {
let used = assigned.get_mut(slot as usize).ok_or(Error::Corrupt)?;
if *used {
return Err(Error::Corrupt);
}
*used = true;
}
let start = g.icon.offset as usize;
let end = start.checked_add(g.icon.length()?).ok_or(Error::Corrupt)?;
if end > icons_used || spans.iter().any(|&(a, b)| start < b && a < end) {
return Err(Error::Corrupt);
}
spans.push((start, end));
}
Ok(assigned)
}
pub fn encode(&self, icons_used: usize) -> Result<Vec<u8>> {
self.validate(icons_used)?;
let b = serde_json::to_vec(self).map_err(|_| Error::Corrupt)?;
if b.len() > CATALOG_BYTES {
return Err(Error::NoSpace);
}
Ok(b)
}
pub fn decode(b: &[u8], icons_used: usize) -> Result<Self> {
if b.len() > CATALOG_BYTES {
return Err(Error::Corrupt);
}
// Bound nesting before serde; strings (including escaped quotes) do not count.
let (mut depth, mut quoted, mut escaped) = (0u8, false, false);
for &v in b {
if quoted {
if escaped {
escaped = false;
} else if v == b'\\' {
escaped = true;
} else if v == b'"' {
quoted = false;
}
} else {
match v {
b'"' => quoted = true,
b'[' | b'{' => {
depth = depth.checked_add(1).ok_or(Error::Corrupt)?;
if depth > 8 {
return Err(Error::Corrupt);
}
}
b']' | b'}' => depth = depth.checked_sub(1).ok_or(Error::Corrupt)?,
_ => {}
}
}
}
let catalog: Self = serde_json::from_slice(b).map_err(|_| Error::Corrupt)?;
catalog.validate(icons_used)?;
Ok(catalog)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Slot {
pub assigned: bool,
pub block: Option<u8>,
pub used: u32,
pub generation: u64,
pub update_at: u64,
pub crc: u32,
}
impl Slot {
fn validate(&self) -> Result<()> {
if !self.assigned && *self != Self::default() {
return Err(Error::Corrupt);
}
if self.generation == 0 && (self.update_at != 0 || self.block.is_some()) {
return Err(Error::Corrupt);
}
match self.block {
Some(id)
if id as usize >= BLOCK_COUNT
|| self.used == 0
|| self.used as usize > SLOT_BYTES =>
{
Err(Error::Corrupt)
}
None if self.used != 0 || self.crc != 0 => Err(Error::Corrupt),
_ => Ok(()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Map {
pub card_id: [u8; 16],
pub sequence: u64,
pub public_copy: usize,
pub catalog_len: u32,
pub catalog_crc: u32,
pub icons_len: u32,
pub icons_crc: u32,
pub order: [u8; BLOCK_COUNT],
pub quarantine: [bool; BLOCK_COUNT],
pub slots: [Slot; SLOT_COUNT],
}
impl Map {
pub fn new(card_id: [u8; 16]) -> Self {
Self {
card_id,
sequence: 1,
public_copy: 0,
catalog_len: 0,
catalog_crc: 0,
icons_len: 0,
icons_crc: 0,
order: std::array::from_fn(|i| i as u8),
quarantine: [false; BLOCK_COUNT],
slots: [Slot::default(); SLOT_COUNT],
}
}
pub fn encode(&self) -> Result<[u8; PAGE_BYTES]> {
if self.sequence == 0
|| self.public_copy > 1
|| self.catalog_len as usize > CATALOG_BYTES
|| self.icons_len as usize > ICON_BYTES
{
return Err(Error::Corrupt);
}
let mut seen = [false; BLOCK_COUNT];
for &id in &self.order {
let v = seen.get_mut(id as usize).ok_or(Error::Corrupt)?;
if *v {
return Err(Error::Corrupt);
}
*v = true;
}
let mut owned = [false; BLOCK_COUNT];
let mut b = [0; PAGE_BYTES];
b[..8].copy_from_slice(b"PMMAP1\0\0");
put64(&mut b, 8, self.sequence);
put32(&mut b, 16, self.public_copy as u32);
put32(&mut b, 20, self.catalog_len);
put32(&mut b, 24, self.catalog_crc);
put32(&mut b, 28, self.icons_len);
put32(&mut b, 32, self.icons_crc);
b[36..52].copy_from_slice(&self.card_id);
b[64..132].copy_from_slice(&self.order);
for i in 0..BLOCK_COUNT {
b[132 + i] = self.quarantine[i] as u8;
}
for (i, s) in self.slots.iter().enumerate() {
s.validate()?;
if let Some(id) = s.block {
if owned[id as usize] || self.quarantine[id as usize] {
return Err(Error::Corrupt);
}
owned[id as usize] = true;
}
let p = 256 + i * 40;
b[p] = if !s.assigned {
0
} else if s.block.is_none() {
1
} else {
2
};
b[p + 1] = s.block.unwrap_or(255);
put32(&mut b, p + 4, s.used);
put64(&mut b, p + 8, s.generation);
put64(&mut b, p + 16, s.update_at);
put32(&mut b, p + 24, s.crc);
}
seal(&mut b);
Ok(b)
}
pub fn decode(b: &[u8; PAGE_BYTES], card_id: [u8; 16]) -> Result<Self> {
page_crc(b)?;
if &b[..8] != b"PMMAP1\0\0" || b[36..52] != card_id {
return Err(Error::Corrupt);
}
zero(&b[52..64])?;
zero(&b[200..256])?;
zero(&b[2816..4092])?;
let mut m = Self::new(card_id);
m.sequence = u64_at(b, 8);
m.public_copy = u32_at(b, 16) as usize;
m.catalog_len = u32_at(b, 20);
m.catalog_crc = u32_at(b, 24);
m.icons_len = u32_at(b, 28);
m.icons_crc = u32_at(b, 32);
m.order.copy_from_slice(&b[64..132]);
for i in 0..BLOCK_COUNT {
if b[132 + i] > 1 {
return Err(Error::Corrupt);
}
m.quarantine[i] = b[132 + i] != 0;
}
for i in 0..SLOT_COUNT {
let p = 256 + i * 40;
zero(&b[p + 2..p + 4])?;
zero(&b[p + 28..p + 40])?;
if b[p] > 2 || (b[p] != 2 && b[p + 1] != 255) || (b[p] == 2 && b[p + 1] == 255) {
return Err(Error::Corrupt);
}
m.slots[i] = Slot {
assigned: b[p] != 0,
block: if b[p] == 2 { Some(b[p + 1]) } else { None },
used: u32_at(b, p + 4),
generation: u64_at(b, p + 8),
update_at: u64_at(b, p + 16),
crc: u32_at(b, p + 24),
};
}
if m.encode()? != *b {
return Err(Error::Corrupt);
}
Ok(m)
}
pub fn game_update_at(&self, game: &Game) -> Option<u64> {
game.slots
.iter()
.filter_map(|&i| self.slots.get(i as usize))
.filter(|s| s.generation > 0)
.map(|s| s.update_at)
.max()
}
}
pub fn read_region(io: &mut dyn MemcardStorage, offset: u64, out: &mut [u8]) -> Result<()> {
for (i, b) in out.chunks_mut(SLOT_BYTES).enumerate() {
io.read_at(offset + (i * SLOT_BYTES) as u64, b)?;
}
Ok(())
}
pub fn write_region(io: &mut dyn MemcardStorage, offset: u64, bytes: &[u8]) -> Result<()> {
for (i, b) in bytes.chunks(SLOT_BYTES).enumerate() {
io.write_at(offset + (i * SLOT_BYTES) as u64, b)?;
}
Ok(())
}
pub fn block_offset(id: u8) -> Result<u64> {
if id as usize >= BLOCK_COUNT {
return Err(Error::InvalidRange);
}
Ok(DATA_OFFSET + id as u64 * SLOT_BYTES as u64)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn crc_vectors_and_identity_golden() {
assert_eq!(crc32c(b""), 0);
assert_eq!(crc32c(b"123456789"), 0xe3069283);
let p = identity_page([7; 16]);
assert_eq!(&p[..16], b"PMCARD1\0\x01\0\0\0\0\x40\x50\0");
assert_eq!(decode_identity(&p), Ok([7; 16]));
}
#[test]
fn descriptor_roundtrip_rejects_reserved_and_alias() {
let mut m = Map::new([1; 16]);
m.slots[2] =
Slot { assigned: true, block: Some(3), used: 7, generation: 1, update_at: 0, crc: 12 };
assert_eq!(Map::decode(&m.encode().unwrap(), [1; 16]), Ok(m.clone()));
m.slots[4] = m.slots[2];
assert_eq!(m.encode(), Err(Error::Corrupt));
let mut b = Map::new([1; 16]).encode().unwrap();
b[3000] = 1;
seal(&mut b);
assert_eq!(Map::decode(&b, [1; 16]), Err(Error::Corrupt));
}
#[test]
fn closed_catalog_rejects_duplicates_and_deep_input() {
for s in [
r#"{"metadataVersion":1,"metadataVersion":1,"games":[]}"#,
r#"{"metadataVersion":1,"games":[],"extra":0}"#,
r#"[[[[[[[[[]]]]]]]]]"#,
] {
assert_eq!(Catalog::decode(s.as_bytes(), 0), Err(Error::Corrupt));
}
}
#[test]
fn maximum_catalog_and_icons_fit_and_preserve_uint64() {
let mut c = Catalog::default();
for i in 0..64 {
c.games.push(Game {
app_id: u32::MAX - i,
title: "\u{1}".repeat(127) + "a",
data_format_version: u32::MAX,
created_at: u64::MAX,
slots: vec![i as u8],
icon: Icon { offset: i * 2096, frames: 4, duration: u16::MAX, crc32c: u32::MAX },
});
}
let b = c.encode(64 * 2096).unwrap();
assert!(b.len() < CATALOG_BYTES);
assert_eq!(Catalog::decode(&b, 64 * 2096), Ok(c));
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,610 @@
//! Worker-owned transaction engine. Main-lane callers exchange owned requests/results.
use super::image::*;
use prometeu_hal::memcard::MemcardStorage;
use std::sync::atomic::{AtomicU8, Ordering};
/// Publication entry and pre-publication cancellation share a single linearization point.
#[derive(Default)]
pub struct PublicationControl(AtomicU8);
impl PublicationControl {
pub fn cancel(&self) {
self.0.fetch_or(2, Ordering::SeqCst);
}
pub fn canceled(&self) -> bool {
self.0.load(Ordering::SeqCst) & 2 != 0
}
pub fn publication_attempted(&self) -> bool {
self.0.load(Ordering::SeqCst) & 1 != 0
}
fn enter(&self) -> Result<()> {
self.0
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |state| {
if state & 2 == 0 { Some(state | 1) } else { None }
})
.map(|_| ())
.map_err(|_| Error::Canceled)
}
fn check(&self) -> Result<()> {
if self.canceled() { Err(Error::Canceled) } else { Ok(()) }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DurableResult {
Success,
Failure,
Indeterminate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CommitResult {
pub durable: DurableResult,
pub error: Option<Error>,
pub sequence: u64,
}
pub struct Card {
io: Box<dyn MemcardStorage>,
pub maps: [Option<Map>; 2],
pub active: usize,
pub catalog: Catalog,
scratch: Box<[u8; SLOT_BYTES]>,
pub recovered: bool,
pub degraded: bool,
pub uncertain: bool,
pub(super) attempted_publication: Option<Map>,
pub write_blocked: bool,
known_quarantine: [bool; BLOCK_COUNT],
}
impl Card {
pub fn format(io: &mut dyn MemcardStorage, id: [u8; 16]) -> Result<()> {
if io.len()? != IMAGE_BYTES {
return Err(Error::InvalidRange);
}
let identity = identity_page(id);
io.write_at(0, &identity)?;
io.write_at(PAGE_BYTES as u64, &identity)?;
let catalog = Catalog::default().encode(0)?;
write_region(io, CATALOG_OFFSETS[0], &catalog)?;
let mut map = Map::new(id);
map.catalog_len = catalog.len() as u32;
map.catalog_crc = crc32c(&catalog);
let encoded = map.encode()?;
io.write_at(MAP_OFFSETS[0], &encoded)?;
io.write_at(MAP_OFFSETS[1], &encoded)?;
io.sync()?;
Ok(())
}
pub fn mount(mut io: Box<dyn MemcardStorage>) -> Result<Self> {
if io.len()? != IMAGE_BYTES {
return Err(Error::Unsupported);
}
let mut page = [0; PAGE_BYTES];
let mut identities = [None; 2];
let mut recognized = false;
for (i, item) in identities.iter_mut().enumerate() {
io.read_at((i * PAGE_BYTES) as u64, &mut page)?;
recognized |= &page[..8] == b"PMCARD1\0"
&& u32::from_le_bytes(page[8..12].try_into().unwrap()) == 1;
*item = decode_identity(&page).ok();
}
let id = identities[0].or(identities[1]).ok_or(if recognized {
Error::Corrupt
} else {
Error::Unsupported
})?;
if identities.iter().flatten().any(|other| *other != id) {
return Err(Error::Corrupt);
}
let mut maps: [Option<Map>; 2] = [None, None];
let mut unknown_metadata = false;
for i in 0..2 {
io.read_at(MAP_OFFSETS[i], &mut page)?;
maps[i] = Map::decode(&page, id).ok();
// A zero page is a verified retirement. An undecodable nonzero page
// may have lost quarantine information; do not silently reuse its pool.
unknown_metadata |= maps[i].is_none() && page.iter().any(|&byte| byte != 0);
}
if let (Some(a), Some(b)) = (&maps[0], &maps[1])
&& a.sequence == b.sequence
&& a != b
{
return Err(Error::Corrupt);
}
let highest = maps.iter().flatten().map(|m| m.sequence).max().unwrap_or(0);
let mut known = [false; BLOCK_COUNT];
for m in maps.iter().flatten() {
for (i, &bad) in m.quarantine.iter().enumerate() {
known[i] |= bad;
}
}
let mut scratch = Box::new([0; SLOT_BYTES]);
let mut catalogs: [Option<Catalog>; 2] = [None, None];
let mut lost_map = false;
for i in 0..2 {
if let Some(m) = &maps[i] {
match Self::validate_map(io.as_mut(), m, scratch.as_mut()) {
Ok(c) => catalogs[i] = Some(c),
Err(Error::Corrupt) => {
maps[i] = None;
lost_map = true
}
Err(e) => return Err(e),
}
}
}
let active = match (&maps[0], &maps[1]) {
(Some(a), Some(b)) => usize::from(b.sequence > a.sequence),
(Some(_), None) => 0,
(None, Some(_)) => 1,
_ => return Err(Error::Corrupt),
};
let m = maps[active].as_ref().unwrap();
let recovered = m.sequence < highest || lost_map || unknown_metadata;
let write_blocked =
unknown_metadata || known.iter().zip(m.quarantine.iter()).any(|(&a, &b)| a && !b);
let degraded = identities.iter().any(Option::is_none)
|| maps.iter().any(Option::is_none)
|| known.iter().any(|v| *v);
Ok(Self {
io,
maps,
active,
catalog: catalogs[active].take().unwrap(),
scratch,
recovered,
degraded,
uncertain: false,
attempted_publication: None,
write_blocked,
known_quarantine: known,
})
}
fn validate_map(
io: &mut dyn MemcardStorage,
m: &Map,
scratch: &mut [u8; SLOT_BYTES],
) -> Result<Catalog> {
let mut raw = vec![0; m.catalog_len as usize];
read_region(io, CATALOG_OFFSETS[m.public_copy], &mut raw)?;
if crc32c(&raw) != m.catalog_crc {
return Err(Error::Corrupt);
}
let catalog = Catalog::decode(&raw, m.icons_len as usize)?;
let assigned = catalog.validate(m.icons_len as usize)?;
for (s, &expected) in m.slots.iter().zip(&assigned) {
if s.assigned != expected {
return Err(Error::Corrupt);
}
if let Some(id) = s.block {
io.read_at(block_offset(id)?, &mut scratch[..s.used as usize])?;
if crc32c(&scratch[..s.used as usize]) != s.crc {
return Err(Error::Corrupt);
}
}
}
let mut crc = !0;
let mut at = 0usize;
while at < m.icons_len as usize {
let n = (m.icons_len as usize - at).min(SLOT_BYTES);
io.read_at(ICON_OFFSETS[m.public_copy] + at as u64, &mut scratch[..n])?;
crc = crc_extend(crc, &scratch[..n]);
at += n;
}
if crc ^ !0 != m.icons_crc {
return Err(Error::Corrupt);
}
for game in &catalog.games {
let n = game.icon.length()?;
io.read_at(ICON_OFFSETS[m.public_copy] + game.icon.offset as u64, &mut scratch[..n])?;
if crc32c(&scratch[..n]) != game.icon.crc32c {
return Err(Error::Corrupt);
}
}
Ok(catalog)
}
pub fn current(&self) -> &Map {
self.maps[self.active].as_ref().unwrap()
}
pub(super) fn known_exclusions(&self) -> [bool; BLOCK_COUNT] {
self.known_quarantine
}
pub(super) fn remember_exclusions(&mut self, known: [bool; BLOCK_COUNT]) {
for (i, bad) in known.into_iter().enumerate() {
self.known_quarantine[i] |= bad;
self.write_blocked |= bad && !self.current().quarantine[i];
}
self.degraded |= self.known_quarantine.iter().any(|&bad| bad);
}
pub fn storage_read_only(&self) -> bool {
self.io.read_only()
}
pub fn into_storage(self) -> Box<dyn MemcardStorage> {
self.io
}
pub fn read_slot(&mut self, slot: usize, target: &mut [u8]) -> Result<usize> {
let s = *self.current().slots.get(slot).ok_or(Error::InvalidRange)?;
if !s.assigned || target.len() < s.used as usize {
return Err(Error::InvalidRange);
}
if let Some(id) = s.block {
self.io.read_at(block_offset(id)?, &mut target[..s.used as usize])?;
if crc32c(&target[..s.used as usize]) != s.crc {
return Err(Error::Corrupt);
}
}
Ok(s.used as usize)
}
fn writable(&self) -> Result<()> {
if self.uncertain {
return Err(Error::Indeterminate);
}
if self.write_blocked || self.io.read_only() {
return Err(Error::ReadOnly);
}
if self.current().sequence == u64::MAX {
return Err(Error::CounterExhausted);
}
Ok(())
}
fn retire_inactive(&mut self, control: &PublicationControl) -> Result<()> {
control.check()?;
let inactive = 1 - self.active;
// Also invalidate torn/unknown pages before a previously referenced region is reused.
self.io.write_at(MAP_OFFSETS[inactive], &[0; PAGE_BYTES])?;
self.io.sync()?;
self.io.read_at(MAP_OFFSETS[inactive], &mut self.scratch[..PAGE_BYTES])?;
if self.scratch[..PAGE_BYTES].iter().any(|&v| v != 0) {
self.write_blocked = true;
return Err(Error::Corrupt);
}
self.maps[inactive] = None;
control.check()
}
fn publish(&mut self, next: Map, control: &PublicationControl) -> Result<()> {
let b = next.encode()?;
control.enter()?;
self.uncertain = true;
self.attempted_publication = Some(next.clone());
let inactive = 1 - self.active;
self.io.write_at(MAP_OFFSETS[inactive], &b)?;
self.io.sync()?;
self.io.read_at(MAP_OFFSETS[inactive], &mut self.scratch[..PAGE_BYTES])?;
if self.scratch[..PAGE_BYTES] != b {
return Err(Error::Corrupt);
}
self.maps[inactive] = Some(next);
self.active = inactive;
self.uncertain = false;
Ok(())
}
pub fn commit(
&mut self,
slot: usize,
payload: &[u8],
timestamp: u64,
control: &PublicationControl,
) -> CommitResult {
let outcome = self.commit_inner(slot, payload, timestamp, control);
CommitResult {
durable: if outcome.is_ok() {
DurableResult::Success
} else if self.uncertain {
DurableResult::Indeterminate
} else {
DurableResult::Failure
},
error: outcome.err(),
sequence: self.current().sequence,
}
}
fn commit_inner(
&mut self,
slot: usize,
payload: &[u8],
timestamp: u64,
control: &PublicationControl,
) -> Result<()> {
self.writable()?;
self.attempted_publication = None;
control.check()?;
let old =
*self.current().slots.get(slot).filter(|s| s.assigned).ok_or(Error::InvalidRange)?;
if payload.len() > SLOT_BYTES {
return Err(Error::InvalidRange);
}
let generation = old.generation.checked_add(1).ok_or(Error::CounterExhausted)?;
self.retire_inactive(control)?;
let mut next = self.current().clone();
next.sequence += 1;
let block = if payload.is_empty() {
None
} else {
let mut protected = self.known_quarantine;
for m in self.maps.iter().flatten() {
for s in &m.slots {
if let Some(b) = s.block {
protected[b as usize] = true;
}
}
}
let id =
*next.order.iter().find(|&&id| !protected[id as usize]).ok_or(Error::NoSpace)?;
control.check()?;
self.io.write_at(block_offset(id)?, payload)?;
self.io.sync()?;
control.check()?;
self.io.read_at(block_offset(id)?, &mut self.scratch[..payload.len()])?;
if self.scratch[..payload.len()] != *payload {
return Err(Error::Corrupt);
}
Some(id)
};
if let Some(id) = block {
move_last(&mut next.order, id)
}
if let Some(id) = old.block {
move_last(&mut next.order, id)
}
next.slots[slot] = Slot {
assigned: true,
block,
used: payload.len() as u32,
generation,
update_at: timestamp,
crc: crc32c(payload),
};
self.publish(next, control)
}
/// Runtime-authorized catalog mutation; userland cannot provide global ownership.
/// Existing icon data streams between copies through the single scratch buffer.
pub fn publish_catalog(
&mut self,
mut catalog: Catalog,
slots: [Slot; SLOT_COUNT],
new_icon: Option<(u32, &[u8])>,
control: &PublicationControl,
) -> Result<()> {
self.writable()?;
self.attempted_publication = None;
control.check()?;
let mut next = self.current().clone();
next.sequence += 1;
next.public_copy = 1 - next.public_copy;
next.slots = slots;
let mut offset = 0usize;
for game in &mut catalog.games {
let len = game.icon.length()?;
game.icon.offset = offset as u32;
offset = offset.checked_add(len).ok_or(Error::NoSpace)?;
}
catalog.validate(offset)?;
let raw = catalog.encode(offset)?;
let assigned = catalog.validate(offset)?;
if slots.iter().zip(assigned).any(|(s, a)| s.assigned != a) {
return Err(Error::Corrupt);
}
next.catalog_len = raw.len() as u32;
next.catalog_crc = crc32c(&raw);
next.icons_len = offset as u32;
// Reject invalid descriptors before any administrative IO.
next.encode()?;
self.retire_inactive(control)?;
let mut crc = !0;
for game in &catalog.games {
control.check()?;
let len = game.icon.length()?;
if let Some((id, bytes)) = new_icon.filter(|(id, _)| *id == game.app_id) {
let _ = id;
if bytes.len() != len || crc32c(bytes) != game.icon.crc32c {
return Err(Error::Corrupt);
}
self.scratch[..len].copy_from_slice(bytes);
} else {
let old = self
.catalog
.games
.iter()
.find(|g| g.app_id == game.app_id)
.ok_or(Error::InvalidRange)?;
if old.icon.length()? != len || old.icon.crc32c != game.icon.crc32c {
return Err(Error::Corrupt);
}
self.io.read_at(
ICON_OFFSETS[self.current().public_copy] + old.icon.offset as u64,
&mut self.scratch[..len],
)?;
if crc32c(&self.scratch[..len]) != old.icon.crc32c {
return Err(Error::Corrupt);
}
}
crc = crc_extend(crc, &self.scratch[..len]);
self.io.write_at(
ICON_OFFSETS[next.public_copy] + game.icon.offset as u64,
&self.scratch[..len],
)?;
}
next.icons_crc = crc ^ !0;
write_region(self.io.as_mut(), CATALOG_OFFSETS[next.public_copy], &raw)?;
self.io.sync()?;
Self::validate_map(self.io.as_mut(), &next, self.scratch.as_mut())?;
self.publish(next, control)?;
self.catalog = catalog;
Ok(())
}
pub fn register(
&mut self,
mut game: Game,
icon: &[u8],
control: &PublicationControl,
) -> Result<()> {
if self.catalog.games.iter().any(|g| g.app_id == game.app_id) {
return Err(Error::InvalidRange);
}
let n = game.slots.len();
if !(1..=64).contains(&n) {
return Err(Error::InvalidRange);
}
let mut slots = self.current().slots;
let free: Vec<u8> = slots
.iter()
.enumerate()
.filter(|(_, s)| !s.assigned)
.map(|(i, _)| i as u8)
.take(n)
.collect();
if free.len() != n {
return Err(Error::NoSpace);
}
game.slots = free;
for &id in &game.slots {
slots[id as usize] = Slot { assigned: true, ..Slot::default() };
}
let app_id = game.app_id;
let mut c = self.catalog.clone();
c.games.push(game);
self.publish_catalog(c, slots, Some((app_id, icon)), control)
}
pub fn remove_game(&mut self, app_id: u32, control: &PublicationControl) -> Result<()> {
let game =
self.catalog.games.iter().find(|g| g.app_id == app_id).ok_or(Error::InvalidRange)?;
let mut slots = self.current().slots;
for &id in &game.slots {
slots[id as usize] = Slot::default();
}
let mut c = self.catalog.clone();
c.games.retain(|g| g.app_id != app_id);
self.publish_catalog(c, slots, None, control)
}
pub fn grow(&mut self, app_id: u32, count: usize, control: &PublicationControl) -> Result<()> {
if !(1..=64).contains(&count) {
return Err(Error::InvalidRange);
}
let mut c = self.catalog.clone();
let g = c.games.iter_mut().find(|g| g.app_id == app_id).ok_or(Error::InvalidRange)?;
if count <= g.slots.len() {
return Ok(());
}
let need = count - g.slots.len();
let mut slots = self.current().slots;
let free: Vec<u8> = slots
.iter()
.enumerate()
.filter(|(_, s)| !s.assigned)
.map(|(i, _)| i as u8)
.take(need)
.collect();
if free.len() != need {
return Err(Error::NoSpace);
}
for &id in &free {
slots[id as usize] = Slot { assigned: true, ..Slot::default() };
}
g.slots.extend(free);
self.publish_catalog(c, slots, None, control)
}
/// Explicit host maintenance after coherent mount. Unknown exclusion history
/// can only be cleared after every reusable, non-quarantined block is tested.
pub fn revalidate(&mut self, control: &PublicationControl) -> Result<()> {
if self.uncertain {
return Err(Error::Indeterminate);
}
if self.io.read_only() {
return Err(Error::ReadOnly);
}
if self.current().sequence == u64::MAX {
return Err(Error::CounterExhausted);
}
self.write_blocked = true;
self.retire_inactive(control)?;
for block in 0..BLOCK_COUNT {
if !self.known_quarantine[block]
&& !self.current().slots.iter().any(|s| s.block == Some(block as u8))
{
self.test_free_block(block as u8, control)?;
}
}
let mut next = self.current().clone();
next.sequence += 1;
next.quarantine = self.known_quarantine;
self.publish(next, control)?;
self.write_blocked = false;
self.degraded = self.known_quarantine.iter().any(|&bad| bad);
Ok(())
}
fn test_free_block(&mut self, block: u8, control: &PublicationControl) -> Result<()> {
for pattern in [0x55, 0xaa] {
control.check()?;
self.scratch.fill(pattern);
self.io.write_at(block_offset(block)?, self.scratch.as_ref())?;
self.io.sync()?;
control.check()?;
self.io.read_at(block_offset(block)?, self.scratch.as_mut())?;
if self.scratch.iter().any(|&byte| byte != pattern) {
return Err(Error::Corrupt);
}
}
Ok(())
}
/// Reintegrating a known excluded block is a separate, explicit operation.
pub fn reintegrate(&mut self, block: u8, control: &PublicationControl) -> Result<()> {
self.writable()?;
if block as usize >= BLOCK_COUNT
|| self.current().slots.iter().any(|s| s.block == Some(block))
{
return Err(Error::InvalidRange);
}
self.retire_inactive(control)?;
self.test_free_block(block, control)?;
let mut next = self.current().clone();
next.sequence += 1;
next.quarantine[block as usize] = false;
self.publish(next, control)?;
self.known_quarantine[block as usize] = false;
self.degraded = self.known_quarantine.iter().any(|&bad| bad);
Ok(())
}
pub fn read_icon(&mut self, app_id: u32) -> Result<Vec<u8>> {
let icon = &self
.catalog
.games
.iter()
.find(|g| g.app_id == app_id)
.ok_or(Error::InvalidRange)?
.icon;
let mut bytes = vec![0; icon.length()?];
self.io
.read_at(ICON_OFFSETS[self.current().public_copy] + icon.offset as u64, &mut bytes)?;
if crc32c(&bytes) != icon.crc32c {
return Err(Error::Corrupt);
}
Ok(bytes)
}
pub fn quarantine(&mut self, block: u8, control: &PublicationControl) -> Result<()> {
if block as usize >= BLOCK_COUNT {
return Err(Error::InvalidRange);
}
self.known_quarantine[block as usize] = true;
self.degraded = true;
let result = (|| {
self.writable()?;
if self.current().slots.iter().any(|s| s.block == Some(block)) {
return Err(Error::Corrupt);
}
self.retire_inactive(control)?;
let mut next = self.current().clone();
next.sequence += 1;
next.quarantine = self.known_quarantine;
self.publish(next, control)
})();
if result.is_err() {
self.write_blocked = true;
}
result
}
}
fn move_last(order: &mut [u8; BLOCK_COUNT], id: u8) {
let pos = order.iter().position(|&b| b == id).unwrap();
order[pos..].rotate_left(1);
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,662 @@
use super::*;
use prometeu_hal::memcard::MemcardIoError;
use std::sync::{Arc, Mutex};
#[derive(Clone)]
struct Disk {
volatile: Vec<u8>,
durable: Vec<u8>,
calls: usize,
fail: Option<usize>,
sync_then_fail: bool,
}
struct Store(Arc<Mutex<Disk>>);
impl MemcardStorage for Store {
fn len(&self) -> std::result::Result<u64, MemcardIoError> {
Ok(IMAGE_BYTES)
}
fn read_only(&self) -> bool {
false
}
fn read_at(&mut self, offset: u64, out: &mut [u8]) -> std::result::Result<(), MemcardIoError> {
let mut d = self.0.lock().unwrap();
d.calls += 1;
if d.fail == Some(d.calls) {
return Err(MemcardIoError::IoError);
}
out.copy_from_slice(&d.volatile[offset as usize..offset as usize + out.len()]);
Ok(())
}
fn write_at(&mut self, offset: u64, bytes: &[u8]) -> std::result::Result<(), MemcardIoError> {
let mut d = self.0.lock().unwrap();
d.calls += 1;
let start = offset as usize;
if d.fail == Some(d.calls) {
let half = bytes.len() / 2;
d.volatile[start..start + half].copy_from_slice(&bytes[..half]);
d.durable[start..start + half].copy_from_slice(&bytes[..half]);
return Err(MemcardIoError::IoError);
}
d.volatile[start..start + bytes.len()].copy_from_slice(bytes);
Ok(())
}
fn sync(&mut self) -> std::result::Result<(), MemcardIoError> {
let mut d = self.0.lock().unwrap();
d.calls += 1;
let fail = d.fail == Some(d.calls);
if !fail || d.sync_then_fail {
d.durable = d.volatile.clone();
}
if fail { Err(MemcardIoError::IoError) } else { Ok(()) }
}
}
fn disk() -> Arc<Mutex<Disk>> {
Arc::new(Mutex::new(Disk {
volatile: vec![0; IMAGE_BYTES as usize],
durable: vec![0; IMAGE_BYTES as usize],
calls: 0,
fail: None,
sync_then_fail: false,
}))
}
fn formatted() -> (Card, Arc<Mutex<Disk>>) {
let d = disk();
Card::format(&mut Store(d.clone()), [7; 16]).unwrap();
(Card::mount(Box::new(Store(d.clone()))).unwrap(), d)
}
fn add(card: &mut Card, app: u32, n: usize) {
let icon = [0; 560];
card.register(
Game {
app_id: app,
title: "game".into(),
data_format_version: 0,
created_at: 0,
slots: vec![0; n],
icon: Icon { offset: 0, frames: 1, duration: 1, crc32c: crc32c(&icon) },
},
&icon,
&PublicationControl::default(),
)
.unwrap();
}
fn reboot(d: &Arc<Mutex<Disk>>) -> Card {
{
let mut disk = d.lock().unwrap();
disk.volatile = disk.durable.clone();
disk.fail = None;
}
Card::mount(Box::new(Store(d.clone()))).unwrap()
}
#[test]
fn full_pool_rotates_and_empty_keeps_ownership() {
let (mut c, d) = formatted();
add(&mut c, 1, 64);
for i in 0..64 {
assert_eq!(
c.commit(i, &[i as u8], 0, &PublicationControl::default()).durable,
DurableResult::Success
);
}
let mut visited = std::collections::BTreeSet::new();
for i in 0..90 {
assert_eq!(
c.commit(0, &[i], 10, &PublicationControl::default()).durable,
DurableResult::Success
);
visited.insert(c.current().slots[0].block.unwrap());
}
assert!(visited.len() >= 5);
assert_eq!(c.commit(0, &[], 0, &PublicationControl::default()).durable, DurableResult::Success);
let r = reboot(&d);
assert!(r.current().slots[0].assigned);
assert_eq!(r.current().slots[0].block, None);
assert_eq!(r.current().slots[0].generation, 92);
assert_eq!(r.current().slots[0].update_at, 0);
}
#[test]
fn cut_every_commit_step_including_sync_that_persists_then_errors() {
let (mut c, d) = formatted();
add(&mut c, 1, 1);
c.commit(0, b"old", 7, &PublicationControl::default());
let initial = d.lock().unwrap().clone();
for persist in [false, true] {
for cut in 1..=12 {
let mut initial = initial.clone();
initial.calls = 0;
initial.fail = None;
initial.sync_then_fail = persist;
let d = Arc::new(Mutex::new(initial));
let mut card = Card::mount(Box::new(Store(d.clone()))).unwrap();
{
let mut disk = d.lock().unwrap();
disk.calls = 0;
disk.fail = Some(cut);
}
let result = card.commit(0, b"new version", 1, &PublicationControl::default());
let mut recovered = reboot(&d);
let mut bytes = [0; 64];
let n = recovered.read_slot(0, &mut bytes).unwrap();
match result.durable {
DurableResult::Success => assert_eq!(&bytes[..n], b"new version"),
DurableResult::Failure => assert_eq!(&bytes[..n], b"old"),
DurableResult::Indeterminate => {
assert!(&bytes[..n] == b"old" || &bytes[..n] == b"new version")
}
}
}
}
}
#[test]
fn catalog_cuts_never_publish_partial_registration() {
let (c, d) = formatted();
drop(c);
let baseline = d.lock().unwrap().clone();
for cut in 1..=14 {
let d = Arc::new(Mutex::new(baseline.clone()));
let mut card = Card::mount(Box::new(Store(d.clone()))).unwrap();
{
let mut state = d.lock().unwrap();
state.calls = 0;
state.fail = Some(cut);
state.sync_then_fail = true;
}
let icon = [0; 560];
let _ = card.register(
Game {
app_id: 3,
title: "new".into(),
data_format_version: 0,
created_at: 0,
slots: vec![0; 2],
icon: Icon { offset: 0, frames: 1, duration: 1, crc32c: crc32c(&icon) },
},
&icon,
&PublicationControl::default(),
);
let recovered = reboot(&d);
let n = recovered.catalog.games.len();
assert!(n <= 1);
assert_eq!(recovered.current().slots.iter().filter(|s| s.assigned).count(), n * 2);
}
}
#[test]
fn corrupted_new_payload_recovers_old_map_with_diagnostic() {
let (mut c, d) = formatted();
add(&mut c, 1, 1);
c.commit(0, b"old", 7, &PublicationControl::default());
c.commit(0, b"new", 8, &PublicationControl::default());
let at = block_offset(c.current().slots[0].block.unwrap()).unwrap() as usize;
{
let mut disk = d.lock().unwrap();
disk.durable[at] ^= 1;
}
let mut r = reboot(&d);
assert!(r.recovered);
let mut out = [0; 8];
let n = r.read_slot(0, &mut out).unwrap();
assert_eq!(&out[..n], b"old");
}
#[test]
fn grow_shrink_removal_and_clean_reassignment() {
let (mut c, d) = formatted();
add(&mut c, 1, 2);
c.commit(1, b"private", 5, &PublicationControl::default());
c.grow(1, 4, &PublicationControl::default()).unwrap();
c.grow(1, 1, &PublicationControl::default()).unwrap();
assert_eq!(c.catalog.games[0].slots.len(), 4);
c.remove_game(1, &PublicationControl::default()).unwrap();
add(&mut c, 2, 4);
let r = reboot(&d);
assert!(
r.current().slots[..4].iter().all(|s| s.assigned && s.generation == 0 && s.block.is_none())
);
}
#[test]
fn cancellation_prevents_publication_and_generations_do_not_wrap() {
let (mut c, d) = formatted();
add(&mut c, 1, 1);
let token = PublicationControl::default();
token.cancel();
assert_eq!(c.commit(0, b"no", 1, &token).durable, DurableResult::Failure);
assert_eq!(reboot(&d).current().slots[0].generation, 0);
c.maps[c.active].as_mut().unwrap().slots[0].generation = u64::MAX;
assert_eq!(
c.commit(0, b"no", 1, &PublicationControl::default()).error,
Some(Error::CounterExhausted)
);
}
#[test]
fn quarantine_survives_remount_and_is_never_selected() {
let (mut c, d) = formatted();
add(&mut c, 1, 1);
c.quarantine(0, &PublicationControl::default()).unwrap();
let mut c = reboot(&d);
assert!(c.degraded);
c.commit(0, b"yes", 1, &PublicationControl::default());
assert_ne!(c.current().slots[0].block, Some(0));
assert!(reboot(&d).current().quarantine[0]);
}
#[test]
fn async_commit_protects_only_source_and_retains_result_without_occupying_lane() {
use crate::services::async_work::{
AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController,
};
use crate::services::memcard::session::{RequestState, Session};
use std::time::{Duration, Instant};
let (mut card, _) = formatted();
add(&mut card, 4, 2);
let mut session = Session::default();
let context = session.load_card(card, 4, 2, Session::required_bytes(2).unwrap()).unwrap();
session.write(context, 0, 0, b"save").unwrap();
let lane = Arc::new(AsyncWorkLane::default());
let now = Instant::now();
let id = session.commit(context, 0, 77, now, &lane).unwrap();
assert_eq!(session.write(context, 0, 0, b"bad"), Err(Error::Busy));
session.write(context, 1, 0, b"other").unwrap();
let mut bytes = [0; 4];
assert_eq!(session.read(context, 0, 0, &mut bytes), Ok(4));
assert_eq!(&bytes, b"save");
let mut controller =
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
let deadline = Instant::now() + Duration::from_secs(2);
while session.query(context, id).unwrap().state == RequestState::Pending {
assert!(Instant::now() < deadline);
session.poll(Instant::now());
std::thread::yield_now();
}
assert_eq!(session.query(context, id).unwrap().state, RequestState::Success);
let (tx, rx) = std::sync::mpsc::channel();
lane.submit_asset_load(move |_| {
tx.send(()).unwrap();
crate::services::async_work::AsyncWorkJobOutcome::Completed
})
.unwrap();
rx.recv_timeout(Duration::from_secs(1)).unwrap();
assert_eq!(session.commit(context, 1, 78, Instant::now(), &lane), Err(Error::Busy));
assert_eq!(session.descriptor(context, 0).unwrap().0.update_at, 77);
session.release_request(context, id).unwrap();
controller.stop().unwrap();
}
#[test]
fn blocked_host_timeout_release_keeps_source_and_prevents_replacement_work() {
use crate::services::async_work::{
AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController,
};
use crate::services::memcard::session::{RequestReason, RequestState, Session};
use std::time::{Duration, Instant};
struct Blocked {
store: Box<dyn MemcardStorage>,
publication: bool,
entered: std::sync::mpsc::Sender<()>,
resume: std::sync::mpsc::Receiver<()>,
}
impl MemcardStorage for Blocked {
fn len(&self) -> std::result::Result<u64, MemcardIoError> {
self.store.len()
}
fn read_only(&self) -> bool {
false
}
fn read_at(&mut self, o: u64, b: &mut [u8]) -> std::result::Result<(), MemcardIoError> {
self.store.read_at(o, b)
}
fn sync(&mut self) -> std::result::Result<(), MemcardIoError> {
self.store.sync()
}
fn write_at(&mut self, o: u64, b: &[u8]) -> std::result::Result<(), MemcardIoError> {
if if self.publication { b.starts_with(b"PMMAP1\0\0") } else { o >= DATA_OFFSET } {
self.entered.send(()).unwrap();
self.resume.recv().unwrap();
}
self.store.write_at(o, b)
}
}
for publication in [false, true] {
let (mut card, _) = formatted();
add(&mut card, 4, 1);
let (et, er) = std::sync::mpsc::channel();
let (rt, rr) = std::sync::mpsc::channel();
let card = Card::mount(Box::new(Blocked {
store: card.into_storage(),
entered: et,
resume: rr,
publication,
}))
.unwrap();
let mut s = Session::default();
let c = s.load_card(card, 4, 1, Session::required_bytes(1).unwrap()).unwrap();
s.write(c, 0, 0, b"pending").unwrap();
let lane = Arc::new(AsyncWorkLane::default());
let mut controller =
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
let admitted = Instant::now();
let id = s.commit(c, 0, 7, admitted, &lane).unwrap();
er.recv_timeout(Duration::from_secs(1)).unwrap();
s.poll(admitted + Duration::from_millis(500));
s.poll(admitted + Duration::from_secs(1));
let result = s.query(c, id).unwrap();
assert_eq!(
result.state,
if publication { RequestState::Indeterminate } else { RequestState::Failure }
);
assert_eq!(result.reason, RequestReason::RestartRequired);
assert!(s.host_faulted());
s.release_request(c, id).unwrap();
assert_eq!(s.write(c, 0, 0, b"unsafe"), Err(Error::Busy));
assert_eq!(s.commit(c, 0, 7, Instant::now(), &lane), Err(Error::Busy));
rt.send(()).unwrap();
controller.stop().unwrap();
s.poll(Instant::now());
assert!(s.unsaved());
assert!(s.host_faulted());
}
}
#[cfg(unix)]
#[test]
fn file_backend_allocates_full_image_and_reopens_verified_save_without_replacing_files() {
use prometeu_drivers::memcard::FileMemcard;
use std::os::unix::fs::MetadataExt;
let root = std::env::temp_dir().join(format!(
"prometeu-memcard-{}-{}",
std::process::id(),
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
));
std::fs::create_dir(&root).unwrap();
let path = root.join("card.pmc");
let file = FileMemcard::create(&path, IMAGE_BYTES, |io, id| {
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
})
.unwrap();
let metadata = std::fs::metadata(&path).unwrap();
assert_eq!(metadata.len(), IMAGE_BYTES);
assert!(metadata.blocks() * 512 >= IMAGE_BYTES);
let mut card = Card::mount(Box::new(file)).unwrap();
add(&mut card, 1, 1);
assert_eq!(
card.commit(0, &vec![17; SLOT_BYTES], 5, &PublicationControl::default()).durable,
DurableResult::Success
);
drop(card);
assert!(FileMemcard::create(&path, IMAGE_BYTES, |_, _| Ok(())).is_err());
let mut card = Card::mount(Box::new(FileMemcard::open(&path, true).unwrap())).unwrap();
let mut out = vec![0; SLOT_BYTES];
assert_eq!(card.read_slot(0, &mut out), Ok(SLOT_BYTES));
assert!(out.iter().all(|&b| b == 17));
assert_eq!(
card.commit(0, b"no", 6, &PublicationControl::default()).error,
Some(Error::ReadOnly)
);
drop(card);
std::fs::remove_file(path).unwrap();
std::fs::remove_dir(root).unwrap();
}
#[test]
fn boot_commit_waits_for_ready_and_publishes_registration_before_payload() {
use crate::services::async_work::{
AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController,
};
use crate::services::memcard::session::{RequestState, Session};
use std::time::{Duration, Instant};
let (card, d) = formatted();
let mut s = Session::default();
let budget = Session::required_bytes(2).unwrap();
let c = s.prepare_new_card(card, 9, 2, 123, budget).unwrap();
let icon = [0; 560];
s.set_presentation(c, "new game", 2, 1, 1, &icon, 124).unwrap();
s.write(c, 0, 0, b"boot save").unwrap();
let lane = Arc::new(AsyncWorkLane::default());
let now = Instant::now();
let id = s.commit(c, 0, 124, now, &lane).unwrap();
assert_eq!(lane.pending_depth(), 0);
assert_eq!(s.query(c, id).unwrap().state, RequestState::Pending);
assert!(reboot(&d).catalog.games.is_empty());
s.finish_boot(true, &lane, now).unwrap();
assert_eq!(lane.pending_depth(), 1);
let mut controller = AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane);
let deadline = Instant::now() + Duration::from_secs(2);
while s.query(c, id).unwrap().state == RequestState::Pending {
assert!(Instant::now() < deadline);
s.poll(Instant::now());
std::thread::yield_now();
}
assert_eq!(s.query(c, id).unwrap().state, RequestState::Success);
assert!(!s.boot_pending());
assert_eq!(s.boot_error(), None);
controller.stop().unwrap();
let mut card = reboot(&d);
assert_eq!(card.catalog.games[0].created_at, 123);
let mut b = [0; 16];
let n = card.read_slot(0, &mut b).unwrap();
assert_eq!(&b[..n], b"boot save");
}
#[test]
fn explicit_revalidation_preserves_exclusions_until_separate_reintegration() {
let (mut card, disk) = formatted();
add(&mut card, 8, 1);
card.quarantine(67, &PublicationControl::default()).unwrap();
card.write_blocked = true;
card.revalidate(&PublicationControl::default()).unwrap();
assert!(!card.write_blocked);
assert!(card.current().quarantine[67]);
let mut mounted = reboot(&disk);
assert!(mounted.current().quarantine[67]);
mounted.reintegrate(67, &PublicationControl::default()).unwrap();
assert!(!reboot(&disk).current().quarantine[67]);
}
#[test]
fn reinsertion_retains_ram_and_different_card_requires_explicit_load() {
use crate::memcard::session::Session;
let (mut card, source_disk) = formatted();
add(&mut card, 9, 1);
card.commit(0, b"on disk", 20, &PublicationControl::default());
let mut session = Session::default();
let context = session.load_card(card, 9, 1, 1024 * 1024).unwrap();
session.clear(context, 0).unwrap();
session.write(context, 0, 0, b"local").unwrap();
session
.replace_storage(Box::new(Store(source_disk)), true, &PublicationControl::default())
.unwrap();
let mut bytes = [0; 7];
assert_eq!(session.read(context, 0, 0, &mut bytes).unwrap(), 5);
assert_eq!(&bytes[..5], b"local");
assert!(session.unsaved());
let other = disk();
Card::format(&mut Store(other.clone()), [8; 16]).unwrap();
let mut target = Card::mount(Box::new(Store(other.clone()))).unwrap();
add(&mut target, 9, 1);
target.commit(0, b"remote", 30, &PublicationControl::default());
drop(target);
session.replace_storage(Box::new(Store(other)), true, &PublicationControl::default()).unwrap();
assert_eq!(session.read(context, 0, 0, &mut bytes).unwrap(), 6);
assert_eq!(&bytes[..6], b"remote");
assert!(!session.unsaved());
}
#[test]
#[ignore = "manual file-host latency qualification; prints measurements for this host"]
fn qualify_file_commits_with_production_asset_decoder() {
use crate::memcard::session::{RequestState, Session};
use crate::{AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController};
use prometeu_drivers::{AssetManager, MemoryBanks, memcard::FileMemcard};
use prometeu_hal::{
AssetBridge,
asset::{AssetCodec, AssetEntry, BankType},
cartridge::AssetsPayloadSource,
};
use std::time::{Duration, Instant};
let path = std::env::temp_dir()
.join(format!("prometeu-memcard-qualification-{}.pmc", std::process::id()));
let file = FileMemcard::create(&path, IMAGE_BYTES, |io, id| {
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
})
.unwrap();
struct Timed {
file: FileMemcard,
first: Arc<Mutex<Option<Instant>>>,
}
impl MemcardStorage for Timed {
fn len(&self) -> std::result::Result<u64, MemcardIoError> {
self.file.len()
}
fn read_only(&self) -> bool {
false
}
fn read_at(&mut self, o: u64, b: &mut [u8]) -> std::result::Result<(), MemcardIoError> {
self.file.read_at(o, b)
}
fn write_at(&mut self, o: u64, b: &[u8]) -> std::result::Result<(), MemcardIoError> {
self.first.lock().unwrap().get_or_insert_with(Instant::now);
self.file.write_at(o, b)
}
fn sync(&mut self) -> std::result::Result<(), MemcardIoError> {
self.file.sync()
}
}
let first = Arc::new(Mutex::new(None));
let mut card = Card::mount(Box::new(Timed { file, first: first.clone() })).unwrap();
add(&mut card, 1, 1);
let mut session = Session::default();
let context = session.load_card(card, 1, 1, 1024 * 1024).unwrap();
for offset in (0..SLOT_BYTES).step_by(4096) {
session.write(context, 0, offset, &[0xa5; 4096]).unwrap();
}
let lane = Arc::new(AsyncWorkLane::default());
let banks = Arc::new(MemoryBanks::new());
let mut assets = vec![0x12; 512 * 512 / 2];
assets.extend([255; 64]);
let entries=(1..=20).map(|id|AssetEntry{asset_id:id,asset_name:format!("glyph-{id}"),bank_type:BankType::GLYPH,
codec:AssetCodec::None,offset:0,size:assets.len() as u64,decoded_size:512*512+64,
metadata:serde_json::json!({"tile_size":8,"width":512,"height":512,"palette_count":1,"palette_authored":1})}).collect();
let mut manager = AssetManager::new(
entries,
AssetsPayloadSource::from_bytes(assets),
banks.clone(),
banks.clone(),
banks,
);
assert!(manager.bind_background_work(lane.clone()));
let mut controller =
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
let mut times = vec![];
let mut starts = vec![];
for sample in 1..=20 {
let asset = manager.load(sample, 0).unwrap();
let asset_deadline = Instant::now() + Duration::from_secs(1);
while matches!(
manager.status(asset),
prometeu_hal::asset::LoadStatus::QUEUED | prometeu_hal::asset::LoadStatus::PENDING
) {
assert!(Instant::now() < asset_deadline);
std::thread::yield_now();
}
*first.lock().unwrap() = None;
let start = Instant::now();
let request = session.commit(context, 0, sample as u64, start, &lane).unwrap();
loop {
session.poll(Instant::now());
if session.query(context, request).unwrap().state != RequestState::Pending {
break;
}
assert!(start.elapsed() < Duration::from_secs(2));
std::thread::yield_now();
}
assert_eq!(session.query(context, request).unwrap().state, RequestState::Success);
times.push(start.elapsed().as_micros());
starts.push(first.lock().unwrap().unwrap().duration_since(start).as_micros());
session.release_request(context, request).unwrap();
}
times.sort();
starts.sort();
eprintln!(
"file-host admission-to-first-IO: p50={}us p95={}us max={}us",
starts[10], starts[18], starts[19]
);
eprintln!(
"file-host 64KiB commit + production asset load: samples={} p50={}us p95={}us max={}us",
times.len(),
times[10],
times[18],
times[19]
);
controller.stop().unwrap();
drop(session);
std::fs::remove_file(path).unwrap();
}
#[test]
fn full_card_admission_preserves_card_and_memory_only_can_extend_existing_data() {
use crate::memcard::{
MemcardService,
admission::{Choice, PreparationState},
session::Mode,
};
use crate::{AsyncWorkLaneConfig, AsyncWorkLaneController};
use std::time::{Duration, Instant};
let (mut card, _) = formatted();
add(&mut card, 1, 1);
add(&mut card, 2, 63);
card.commit(0, b"prior", 10, &PublicationControl::default());
let mut service = MemcardService::new();
let mut controller =
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), service.lane.clone());
service.mount_card(card.into_storage()).unwrap();
let wait = |service: &mut MemcardService| {
let deadline = Instant::now() + Duration::from_secs(2);
while service.admission.state == PreparationState::Pending {
service.poll(Instant::now());
assert!(Instant::now() < deadline);
std::thread::yield_now();
}
};
wait(&mut service);
assert_eq!(service.admission.state, PreparationState::Ready);
assert_eq!(service.choose_for_game(1, 2, Choice::Persistent, 12), Err(Error::NoSpace));
assert!(service.card_installed());
service.choose_for_game(1, 2, Choice::MemoryExisting, 12).unwrap();
wait(&mut service);
assert_eq!(service.admission.state, PreparationState::Ready);
let s = &mut service.session;
let c = s.context();
assert_eq!(s.mode(), Some(Mode::MemoryOnly));
let mut bytes = [9; 5];
assert_eq!(s.read(c, 0, 0, &mut bytes).unwrap(), 5);
assert_eq!(&bytes, b"prior");
assert_eq!(s.read(c, 1, 0, &mut bytes).unwrap(), 0);
s.end_session();
service.poll(Instant::now());
assert!(service.card_installed());
controller.stop().unwrap();
}
#[test]
fn ending_a_session_releases_a_completed_unconsumed_request() {
use crate::memcard::session::{RequestState, Session};
use crate::{AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController};
use std::time::{Duration, Instant};
let (mut card, _) = formatted();
add(&mut card, 1, 1);
let mut s = Session::default();
let c = s.load_card(card, 1, 1, 1024 * 1024).unwrap();
s.write(c, 0, 0, b"saved").unwrap();
let lane = Arc::new(AsyncWorkLane::default());
let mut controller =
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
let start = Instant::now();
let id = s.commit(c, 0, 1, start, &lane).unwrap();
while s.query(c, id).unwrap().state == RequestState::Pending {
s.poll(Instant::now());
assert!(start.elapsed() < Duration::from_secs(2));
std::thread::yield_now();
}
s.end_session();
assert!(!s.has_in_flight());
assert!(s.take_idle_card().unwrap().is_some());
controller.stop().unwrap();
}
@@ -1,6 +1,6 @@
use super::*;
use crate::fs::{FsState, VirtualFS};
use crate::services::memcard::{MemcardService, MemcardSlotState, MemcardStatus};
use crate::services::memcard::MemcardService;
use prometeu_bytecode::{TRAP_INVALID_SYSCALL, TRAP_OOB, TRAP_TYPE, Value};
use prometeu_hal::app_mode::AppMode;
use prometeu_hal::asset::{AssetId, AssetOpStatus, BankType, SlotRef};
@@ -605,101 +605,24 @@ impl NativeInterface for VmRuntimeHost<'_> {
}
Ok(())
}
Syscall::MemSlotCount => {
if self.current_cartridge_app_mode != AppMode::Game {
ret.push_int(MemcardStatus::AccessDenied as i64);
ret.push_int(0);
return Ok(());
}
ret.push_int(MemcardStatus::Ok as i64);
ret.push_int(self.memcard.slot_count() as i64);
Ok(())
}
Syscall::MemSlotStat => {
let slot = expect_slot_index(args, 0)?;
if self.current_cartridge_app_mode != AppMode::Game {
ret.push_int(MemcardStatus::AccessDenied as i64);
ret.push_int(MemcardSlotState::Empty as i64);
ret.push_int(0);
ret.push_int(0);
ret.push_int(0);
return Ok(());
}
let stat = self.memcard.slot_stat(self.fs, self.current_app_id, slot);
let status = if stat.state == MemcardSlotState::Corrupt {
MemcardStatus::Corrupt
} else {
MemcardStatus::Ok
};
ret.push_int(status as i64);
ret.push_int(stat.state as i64);
ret.push_int(stat.used_bytes as i64);
ret.push_int(stat.generation as i64);
ret.push_int(stat.checksum as i64);
Ok(())
}
Syscall::MemSlotRead => {
let slot = expect_slot_index(args, 0)?;
let offset = expect_non_negative_usize(args, 1, "offset")?;
let max_bytes = expect_non_negative_usize(args, 2, "max_bytes")?;
if self.current_cartridge_app_mode != AppMode::Game {
ret.push_int(MemcardStatus::AccessDenied as i64);
ret.push_string("")?;
ret.push_int(0);
return Ok(());
}
let read =
self.memcard.slot_read(self.fs, self.current_app_id, slot, offset, max_bytes);
ret.push_int(read.status as i64);
ret.push_hex(&read.bytes)?;
ret.push_int(read.bytes_read as i64);
Ok(())
}
Syscall::MemSlotWrite => {
let slot = expect_slot_index(args, 0)?;
let offset = expect_non_negative_usize(args, 1, "offset")?;
let payload_hex = expect_string(args, 2, "payload_hex")?;
if self.current_cartridge_app_mode != AppMode::Game {
ret.push_int(MemcardStatus::AccessDenied as i64);
ret.push_int(0);
return Ok(());
}
let payload = hex_decode(&payload_hex)?;
Syscall::MemBootstrap
| Syscall::MemSlotCount
| Syscall::MemSlotStat
| Syscall::MemSlotRead
| Syscall::MemSlotWrite
| Syscall::MemSlotCommit
| Syscall::MemSlotClear
| Syscall::MemQuery
| Syscall::MemRelease
| Syscall::MemInfo
| Syscall::MemMetadata
| Syscall::MemPresentation
| Syscall::MemDiagnostic
| Syscall::MemPublishPresentation => {
let timestamp = platform.wall_clock().utc_unix_seconds();
let app_id = self.current_app_id;
let write = self.memcard.slot_write(self.fs, app_id, slot, offset, &payload);
ret.push_int(write.status as i64);
ret.push_int(write.bytes_written as i64);
Ok(())
}
Syscall::MemSlotCommit => {
let slot = expect_slot_index(args, 0)?;
if self.current_cartridge_app_mode != AppMode::Game {
ret.push_int(MemcardStatus::AccessDenied as i64);
return Ok(());
}
let status = {
let app_id = self.current_app_id;
let memcard = &mut self.memcard;
let fs = &mut self.fs;
memcard.slot_commit(fs, app_id, slot)
};
ret.push_int(status as i64);
Ok(())
}
Syscall::MemSlotClear => {
let slot = expect_slot_index(args, 0)?;
if self.current_cartridge_app_mode != AppMode::Game {
ret.push_int(MemcardStatus::AccessDenied as i64);
return Ok(());
}
let status = {
let app_id = self.current_app_id;
let memcard = &mut self.memcard;
let fs = &mut self.fs;
memcard.slot_clear(fs, app_id, slot)
};
ret.push_int(status as i64);
Ok(())
let game_profile = self.current_cartridge_app_mode == AppMode::Game;
self.memcard.guest(syscall, args, ret, app_id, timestamp, game_profile)
}
Syscall::LogWrite => {
self.syscall_log_write(
@@ -849,14 +772,6 @@ fn expect_number(args: &[Value], index: usize, field: &str) -> Result<f64, VmFau
}
}
fn expect_slot_index(args: &[Value], index: usize) -> Result<u8, VmFault> {
let slot = expect_int(args, index)?;
if !(0..32).contains(&slot) {
return Err(VmFault::Trap(TRAP_OOB, format!("slot index out of bounds: {}", slot)));
}
Ok(slot as u8)
}
fn expect_non_negative_usize(args: &[Value], index: usize, field: &str) -> Result<usize, VmFault> {
let val = expect_int(args, index)?;
if val < 0 {
@@ -864,32 +779,3 @@ fn expect_non_negative_usize(args: &[Value], index: usize, field: &str) -> Resul
}
Ok(val as usize)
}
fn hex_decode(s: &str) -> Result<Vec<u8>, VmFault> {
fn nibble(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'a'..=b'f' => Some(10 + c - b'a'),
b'A'..=b'F' => Some(10 + c - b'A'),
_ => None,
}
}
let bytes = s.as_bytes();
if !bytes.len().is_multiple_of(2) {
return Err(VmFault::Trap(TRAP_TYPE, "payload_hex must have even length".to_string()));
}
let mut out = Vec::with_capacity(bytes.len() / 2);
let mut i = 0usize;
while i < bytes.len() {
let hi = nibble(bytes[i]).ok_or_else(|| {
VmFault::Trap(TRAP_TYPE, "payload_hex contains invalid hex".to_string())
})?;
let lo = nibble(bytes[i + 1]).ok_or_else(|| {
VmFault::Trap(TRAP_TYPE, "payload_hex contains invalid hex".to_string())
})?;
out.push((hi << 4) | lo);
i += 2;
}
Ok(out)
}
@@ -219,6 +219,22 @@ impl VirtualMachineRuntime {
match vm.initialize(cartridge.program.clone()) {
Ok(_) => {
if let Some(count) = cartridge.memcard_slots {
if !(1..=64).contains(&count)
|| cartridge.capabilities & prometeu_hal::syscalls::caps::MEMCARD == 0
{
return Err(CrashReport::VmPanic {
message: "Invalid memcard capability/count".into(),
pc: None,
});
}
vm.require_memcard_boot().map_err(|error| CrashReport::VmInit { error })?;
} else if cartridge.capabilities & prometeu_hal::syscalls::caps::MEMCARD != 0 {
return Err(CrashReport::VmPanic {
message: "Memcard capability requires a slot count".into(),
pc: None,
});
}
self.current_app_id = cartridge.app_id;
self.current_cartridge_title = cartridge.title.clone();
self.current_cartridge_app_version = cartridge.app_version.clone();
@@ -1,5 +1,5 @@
use super::*;
use crate::fs::{FsBackend, FsEntry, FsError, FsState, VirtualFS};
use crate::fs::{FsState, VirtualFS};
use crate::services::memcard::MemcardService;
use prometeu_bytecode::TRAP_TYPE;
use prometeu_bytecode::Value;
@@ -29,50 +29,13 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::Ordering;
#[derive(Default)]
struct MemFsBackend {
files: HashMap<String, Vec<u8>>,
}
impl FsBackend for MemFsBackend {
fn mount(&mut self) -> Result<(), FsError> {
Ok(())
}
fn unmount(&mut self) {}
fn list_dir(&self, _path: &str) -> Result<Vec<FsEntry>, FsError> {
Ok(Vec::new())
}
fn read_file(&self, path: &str) -> Result<Vec<u8>, FsError> {
self.files.get(path).cloned().ok_or(FsError::NotFound)
}
fn write_file(&mut self, path: &str, data: &[u8]) -> Result<(), FsError> {
self.files.insert(path.to_string(), data.to_vec());
Ok(())
}
fn delete(&mut self, path: &str) -> Result<(), FsError> {
self.files.remove(path).map(|_| ()).ok_or(FsError::NotFound)
}
fn exists(&self, path: &str) -> bool {
self.files.contains_key(path)
}
fn is_healthy(&self) -> bool {
true
}
}
fn cartridge_with_program_and_mode(
program: Vec<u8>,
capabilities: u64,
app_mode: AppMode,
) -> Cartridge {
Cartridge {
memcard_slots: None,
app_id: 42,
title: "Test Cart".into(),
app_version: "1.0.0".into(),
@@ -1,129 +1,155 @@
use super::*;
use prometeu_bytecode::registry::{LifecycleRole, NamedType, Signature, TypeId};
#[test]
fn tick_memcard_slot_roundtrip_for_game_profile() {
let mut runtime = VirtualMachineRuntime::new(None);
let mut log_service = LogService::new(4096);
let mut fs = VirtualFS::new();
let mut fs_state = FsState::Unmounted;
let mut memcard = MemcardService::new();
let mut open_files: HashMap<u32, String> = HashMap::new();
let mut next_handle = 1;
runtime.mount_fs(&mut log_service, &mut fs, &mut fs_state, Box::new(MemFsBackend::default()));
let mut vm = VirtualMachine::default();
let mut platform = TestPlatform::new();
let signals = InputSignals::default();
let code = assemble(
"PUSH_I32 0\nPUSH_I32 0\nPUSH_CONST 0\nHOSTCALL 0\nPOP_N 2\nPUSH_I32 0\nHOSTCALL 1\nPOP_N 1\nPUSH_I32 0\nPUSH_I32 0\nPUSH_I32 10\nHOSTCALL 2\nHALT",
)
.expect("assemble");
let program = serialized_single_function_module_with_consts(
fn memcard_cartridge(app_mode: AppMode, hook: &str) -> Cartridge {
let boot =
assemble("HOSTCALL 0\nPOP_N 1\nGET_GLOBAL 0\nGET_GLOBAL 1\nFRAME_SYNC\nHALT").unwrap();
let callback = assemble(hook).unwrap();
let mut code = boot.clone();
code.extend(&callback);
let program = TestProgram {
code,
vec![ConstantPoolEntry::String("6869".into())],
vec![
functions: vec![
FunctionDecl { code_len: boot.len() as u32, max_stack_slots: 16, ..Default::default() },
FunctionDecl {
lifecycle_role: LifecycleRole::MemcardBoot,
code_offset: boot.len() as u32,
code_len: callback.len() as u32,
max_stack_slots: 16,
signature: Signature {
parameters: vec![NamedType { type_id: TypeId(0), name: None }],
results: vec![TypeId(1)],
},
..Default::default()
},
],
syscalls: vec![
SyscallDecl {
module: "mem".into(),
name: "slot_write".into(),
name: "bootstrap".into(),
version: 1,
arg_slots: 3,
arg_slots: 0,
ret_slots: 1,
},
SyscallDecl {
module: "mem".into(),
name: "slot_count".into(),
version: 1,
arg_slots: 1,
ret_slots: 2,
},
SyscallDecl {
module: "mem".into(),
name: "slot_commit".into(),
version: 1,
arg_slots: 1,
ret_slots: 1,
},
SyscallDecl {
module: "mem".into(),
name: "slot_read".into(),
version: 1,
arg_slots: 3,
ret_slots: 3,
arg_slots: 2,
ret_slots: 2,
},
],
);
let cartridge = Cartridge {
..Default::default()
};
Cartridge {
memcard_slots: Some(2),
app_id: 42,
title: "Memcard Game".into(),
app_version: "1.0.0".into(),
app_mode: AppMode::Game,
capabilities: caps::FS,
program,
title: "Memcard test".into(),
app_version: "1".into(),
app_mode,
capabilities: caps::MEMCARD,
program: program.serialize(),
assets: AssetsPayloadSource::empty(),
asset_table: vec![],
preload: vec![],
};
runtime.initialize_vm(&mut log_service, &mut vm, &cartridge).expect("runtime must initialize");
let report = runtime.tick(
&mut log_service,
&mut fs,
&mut fs_state,
&mut memcard,
&mut open_files,
&mut next_handle,
&mut vm,
&signals,
&mut platform,
);
assert!(report.is_none(), "memcard roundtrip must not crash");
assert!(vm.is_halted());
assert_eq!(
vm.operand_stack_top(3),
vec![Value::Int64(2), Value::String("6869".into()), Value::Int64(0)]
);
}
}
#[test]
fn tick_memcard_access_is_denied_for_non_game_profile() {
fn tick_memcard_boot_receives_admitted_context_and_precedes_first_frame() {
let cartridge = memcard_cartridge(
AppMode::Game,
"GET_LOCAL 0\nHOSTCALL 1\nSET_GLOBAL 0\nPOP_N 1\nGET_LOCAL 0\nPUSH_I32 0\nHOSTCALL 2\nSET_GLOBAL 1\nPOP_N 1\nPUSH_I32 0\nRET",
);
let mut runtime = VirtualMachineRuntime::new(None);
let mut log_service = LogService::new(4096);
let mut fs = VirtualFS::new();
let mut fs_state = FsState::Unmounted;
let mut memcard = MemcardService::new();
let mut open_files: HashMap<u32, String> = HashMap::new();
let mut next_handle = 1;
let mut log = LogService::new(4096);
let mut vm = VirtualMachine::default();
runtime.initialize_vm(&mut log, &mut vm, &cartridge).unwrap();
let mut memcard = MemcardService::new();
let context = memcard.session.start_memory(42, 2, 1024 * 1024).unwrap();
memcard.session.begin_boot();
memcard.session.set_presentation(context, "Test", 1, 1, 1, &[0; 560], 0).unwrap();
let mut fs = VirtualFS::new();
let mut state = FsState::Unmounted;
let mut files = HashMap::new();
let mut next = 1;
let mut platform = TestPlatform::new();
let signals = InputSignals::default();
let code = assemble("HOSTCALL 0\nHALT").expect("assemble");
let program = serialized_single_function_module(
code,
vec![SyscallDecl {
module: "mem".into(),
name: "slot_count".into(),
version: 1,
arg_slots: 0,
ret_slots: 2,
}],
for expected in [None, Some(0)] {
assert!(
runtime
.tick(
&mut log,
&mut fs,
&mut state,
&mut memcard,
&mut files,
&mut next,
&mut vm,
&signals,
&mut platform
)
.is_none()
);
assert_eq!(vm.memcard_boot_result(), expected);
assert_eq!(runtime.logical_frame_index, 0);
}
let request = 1;
assert_eq!(
memcard.session.query(context, request).unwrap().state,
crate::memcard::session::RequestState::MemoryOnly
);
let cartridge = Cartridge {
app_id: 101,
title: "System App".into(),
app_version: "1.0.0".into(),
app_mode: AppMode::Shell,
capabilities: caps::FS,
program,
assets: AssetsPayloadSource::empty(),
asset_table: vec![],
preload: vec![],
};
runtime.initialize_vm(&mut log_service, &mut vm, &cartridge).expect("runtime must initialize");
let report = runtime.tick(
&mut log_service,
&mut fs,
&mut fs_state,
&mut memcard,
&mut open_files,
&mut next_handle,
&mut vm,
&signals,
&mut platform,
assert!(
runtime
.tick(
&mut log,
&mut fs,
&mut state,
&mut memcard,
&mut files,
&mut next,
&mut vm,
&signals,
&mut platform
)
.is_none()
);
assert!(report.is_none(), "non-game memcard call must return status");
assert!(vm.is_halted());
assert_eq!(vm.operand_stack_top(2), vec![Value::Int64(0), Value::Int64(4)]);
assert_eq!(runtime.logical_frame_index, 1);
assert_eq!(vm.operand_stack_top(2), vec![Value::Int64(1), Value::Int64(2)]);
assert!(vm.begin_memcard_boot(context as i64).is_err());
}
#[test]
fn tick_memcard_boot_is_denied_for_non_game_profile() {
let cartridge = memcard_cartridge(
AppMode::Shell,
"GET_LOCAL 0\nHOSTCALL 1\nPOP_N 2\nGET_LOCAL 0\nPUSH_I32 0\nHOSTCALL 2\nPOP_N 2\nPUSH_I32 0\nRET",
);
let mut runtime = VirtualMachineRuntime::new(None);
let mut log = LogService::new(4096);
let mut vm = VirtualMachine::default();
runtime.initialize_vm(&mut log, &mut vm, &cartridge).unwrap();
let mut memcard = MemcardService::new();
memcard.session.start_memory(42, 2, 1024 * 1024).unwrap();
memcard.session.begin_boot();
let report = runtime.tick(
&mut log,
&mut VirtualFS::new(),
&mut FsState::Unmounted,
&mut memcard,
&mut HashMap::new(),
&mut 1,
&mut vm,
&InputSignals::default(),
&mut TestPlatform::new(),
);
assert!(matches!(report, Some(CrashReport::VmTrap { .. })));
assert_eq!(vm.memcard_boot_result(), None);
}
@@ -70,12 +70,22 @@ impl VirtualMachineRuntime {
(glyph, sounds, scenes)
}
#[allow(clippy::too_many_arguments)]
pub fn debug_step_instruction(
&mut self,
log_service: &mut LogService,
fs: &mut VirtualFS,
fs_state: &mut FsState,
memcard: &mut MemcardService,
open_files: &mut HashMap<u32, String>,
next_handle: &mut u32,
vm: &mut VirtualMachine,
platform: &mut dyn RuntimePlatform,
) -> Option<CrashReport> {
if vm.memcard_boot_result() == Some(0) && memcard.session.boot_pending() {
return None;
}
let was_memcard_boot = vm.memcard_boot_active();
if vm.call_stack_is_empty()
&& let Err(error) = vm.prepare_boot_call()
{
@@ -85,23 +95,33 @@ impl VirtualMachineRuntime {
}
let step_result = {
let mut ctx = HostContext::new_platform(Some(platform));
let mut fs = VirtualFS::new();
let mut fs_state = FsState::Unmounted;
let mut memcard = MemcardService::new();
let mut open_files = HashMap::new();
let mut next_handle = 1;
let mut host = VmRuntimeHost {
runtime: self,
log_service,
fs: &mut fs,
fs_state: &mut fs_state,
memcard: &mut memcard,
open_files: &mut open_files,
next_handle: &mut next_handle,
fs,
fs_state,
memcard,
open_files,
next_handle,
};
vm.step(&mut host, &mut ctx)
};
if was_memcard_boot
&& step_result == Err(LogicalFrameEndingReason::LifecycleComplete)
&& let Err(error) = memcard.session.finish_boot(
vm.memcard_boot_result() == Some(0),
&memcard.lane,
Instant::now(),
)
{
let report = CrashReport::VmPanic {
message: format!("Memcard bootstrap failed: {error:?}: {}", memcard.diagnostic),
pc: Some(vm.pc() as u32),
};
self.last_crash_report = Some(report.clone());
return Some(report);
}
match step_result {
Ok(())
| Err(
@@ -201,6 +221,55 @@ impl VirtualMachineRuntime {
signals: &InputSignals,
platform: &mut dyn RuntimePlatform,
) -> Option<CrashReport> {
if vm.memcard_boot_active() {
let reason = self.tick_lifecycle(
log_service,
fs,
fs_state,
mem_card,
open_files,
next_handle,
vm,
platform,
false,
);
let failure = match reason {
LogicalFrameEndingReason::LifecycleComplete => {
let ready = vm.memcard_boot_result() == Some(0);
mem_card.session.finish_boot(ready, &mem_card.lane, Instant::now()).err().map(
|error| CrashReport::VmPanic {
message: format!(
"Memcard bootstrap failed: {error:?}: {}",
mem_card.diagnostic
),
pc: Some(vm.pc() as u32),
},
)
}
LogicalFrameEndingReason::BudgetExhausted
| LogicalFrameEndingReason::Breakpoint => None,
LogicalFrameEndingReason::Trap(trap) => Some(CrashReport::VmTrap { trap }),
other => Some(CrashReport::VmPanic {
message: format!("Memcard bootstrap stopped: {other:?}"),
pc: Some(vm.pc() as u32),
}),
};
if let Some(report) = &failure {
self.last_crash_report = Some(report.clone());
}
return failure;
}
if vm.memcard_boot_result() == Some(0) {
if let Some(error) = mem_card.session.boot_error() {
return Some(CrashReport::VmPanic {
message: format!("Memcard registration failed: {error:?}"),
pc: Some(vm.pc() as u32),
});
}
if mem_card.session.boot_pending() {
return None;
}
}
let start = Instant::now();
self.tick_index += 1;
@@ -9,6 +9,7 @@ mod dynamic;
mod gc;
mod lifecycle;
mod loader;
mod native_arrays;
mod random;
mod reference;
mod runtime;
@@ -114,6 +115,9 @@ pub struct VirtualMachine {
halted: bool,
lifecycle: Option<lifecycle::LifecycleCall>,
lifecycle_failure: Option<LogicalFrameEndingReason>,
memcard_boot_started: bool,
memcard_boot_result: Option<i32>,
memcard_boot_required: bool,
/// Set of ROM addresses used for software breakpoints in the debugger.
breakpoints: std::collections::HashSet<usize>,
/// GC: number of newly allocated live objects threshold to trigger a collection at safepoint.
@@ -235,6 +239,9 @@ impl VirtualMachine {
cycles: 0,
halted: true,
lifecycle: None,
memcard_boot_started: false,
memcard_boot_result: None,
memcard_boot_required: false,
lifecycle_failure: None,
breakpoints: std::collections::HashSet::new(),
gc_alloc_threshold: 1024, // conservative default; tests may override
@@ -844,6 +851,11 @@ impl VirtualMachine {
self.operand_stack
.reserve(syscall.results_count())
.map_err(|e| self.resource_trap(e, opcode, start_pc as u32))?;
let mut arrays = native_arrays::SessionArrays {
heap: &mut self.heap,
registry: self.validated_image.as_ref().map(|image| image.registry().as_ref()),
work: 0,
};
let mut strings = strings::SessionStringAllocator {
ledger: &mut self.strings,
budget: self.budget.clone(),
@@ -852,10 +864,12 @@ impl VirtualMachine {
self.operand_stack.native_result_buffer(),
syscall.results_count(),
&mut strings,
);
)
.with_arrays(&mut arrays);
let native_result = native.syscall(id, &args, &mut ret, ctx);
let return_overflow = ret.overflowed();
let return_failure = ret.failure().cloned();
self.cycles += arrays.work;
let native_result = return_failure.map(Err).unwrap_or(native_result);
if native_result.is_err() {
self.operand_stack.truncate(stack_height_before);
@@ -885,6 +899,16 @@ impl VirtualMachine {
return_overflow
)));
}
if syscall == prometeu_hal::syscalls::Syscall::MemBootstrap {
let context = self.pop_integer().map_err(LogicalFrameEndingReason::Panic)?;
if !self.begin_memcard_boot(context).map_err(|e| {
LogicalFrameEndingReason::Panic(format!("Invalid memcard bootstrap: {e:?}"))
})? {
return Err(LogicalFrameEndingReason::Panic(
"Missing memcard boot binding".into(),
));
}
}
}
OpCode::Intrinsic => {
let pc_at_intrinsic = start_pc as u32;
@@ -1046,6 +1070,11 @@ impl VirtualMachine {
return self.execute_collection(opcode, ty, field, start_pc as u32);
}
OpCode::FrameSync => {
if self.memcard_boot_required && self.memcard_boot_result != Some(0) {
return Err(LogicalFrameEndingReason::Panic(
"Frame entered before memcard bootstrap admission".into(),
));
}
if self.lifecycle.is_some() {
return Err(self.trap(
TRAP_TYPE,
@@ -3,6 +3,7 @@ use super::*;
use prometeu_bytecode::registry::LifecycleRole;
pub(super) struct LifecycleCall {
role: LifecycleRole,
depth: usize,
pc: usize,
halted: bool,
@@ -28,6 +29,39 @@ impl VirtualMachine {
/// Returns false for an absent role. Entry is transactional with respect to quotas.
pub fn begin_lifecycle(&mut self, role: LifecycleRole) -> Result<bool, VmInitError> {
if role == LifecycleRole::MemcardBoot {
return Err(VmInitError::EntrypointNotFound);
}
self.begin_controlled_call(role, None)
}
pub fn begin_memcard_boot(&mut self, context: i64) -> Result<bool, VmInitError> {
if self.memcard_boot_started {
return Err(VmInitError::EntrypointNotFound);
}
let started = self.begin_controlled_call(LifecycleRole::MemcardBoot, Some(context))?;
self.memcard_boot_started = started;
Ok(started)
}
pub fn memcard_boot_active(&self) -> bool {
self.lifecycle.as_ref().is_some_and(|c| c.role == LifecycleRole::MemcardBoot)
}
pub fn memcard_boot_result(&self) -> Option<i32> {
self.memcard_boot_result
}
pub fn require_memcard_boot(&mut self) -> Result<(), VmInitError> {
if !self.has_lifecycle_hook(LifecycleRole::MemcardBoot) {
return Err(VmInitError::EntrypointNotFound);
}
self.memcard_boot_required = true;
Ok(())
}
fn begin_controlled_call(
&mut self,
role: LifecycleRole,
context: Option<i64>,
) -> Result<bool, VmInitError> {
if self.lifecycle.is_some() || self.lifecycle_failure.is_some() {
return Err(VmInitError::EntrypointNotFound);
}
@@ -41,18 +75,23 @@ impl VirtualMachine {
let locals = callee.local_slots as usize;
let offset = callee.code_offset as usize;
self.budget.check(RuntimeResource::CallDepth, (self.call_stack.len() + 1) as u64)?;
let operands = self.operand_stack.prepare_growth(locals)?;
let operands =
self.operand_stack.prepare_growth(locals + usize::from(context.is_some()))?;
let frames = self.call_stack.prepare_growth(1)?;
self.operand_stack.commit_growth(operands);
self.call_stack.commit_growth(frames);
let base = self.operand_stack.len();
let call = LifecycleCall {
role,
depth: self.call_stack.len(),
pc: self.pc,
halted: self.halted,
remaining: Self::LIFECYCLE_CYCLES,
breakpoint: None,
};
if let Some(context) = context {
self.operand_stack.push(Value::Int64(context))?;
}
for _ in 0..locals {
self.operand_stack.push(Value::Null)?;
}
@@ -80,14 +119,32 @@ impl VirtualMachine {
if let Some(call) = &mut self.lifecycle {
call.breakpoint = None;
}
let had_call = self.lifecycle.is_some();
let before = self.cycles;
let result = self.step_inner(native, ctx);
if !had_call && self.lifecycle.is_some() && result.is_ok() {
return Err(LogicalFrameEndingReason::BudgetExhausted);
}
let Some(call) = self.lifecycle.as_mut() else {
return result;
};
call.remaining = call.remaining.saturating_sub(self.cycles - before);
if result.is_ok() && !self.halted && self.call_stack.len() == call.depth {
let call = self.lifecycle.take().unwrap();
if call.role == LifecycleRole::MemcardBoot {
match self.operand_stack.last() {
Some(Value::Int32(result @ (0 | 1))) => {
self.memcard_boot_result = Some(*result)
}
_ => {
let error = LogicalFrameEndingReason::Panic(
"MemcardBoot must return READY or INCONSISTENT".into(),
);
self.lifecycle_failure = Some(error.clone());
return Err(error);
}
}
}
self.pc = call.pc;
self.halted = call.halted;
return Err(LogicalFrameEndingReason::LifecycleComplete);
@@ -0,0 +1,72 @@
//! Narrow native access to typed arrays; no borrowed guest storage reaches IO workers.
use crate::heap::Heap;
use crate::value_contract::ValueError;
use prometeu_bytecode::registry::{BuiltinId, Registry, TypeKind};
use prometeu_bytecode::{HeapRef, Value};
use prometeu_hal::host_return::GuestByteArrays;
use prometeu_hal::vm_fault::VmFault;
pub(super) struct SessionArrays<'a> {
pub heap: &'a mut Heap,
pub registry: Option<&'a Registry>,
pub work: u64,
}
fn fault(error: ValueError) -> VmFault {
match error {
ValueError::Resource(r) => VmFault::ResourceLimit(r),
other => VmFault::Trap(
super::VirtualMachine::value_error_code(other),
"Invalid native byte-array range/type/value".into(),
),
}
}
impl GuestByteArrays for SessionArrays<'_> {
fn validate(&mut self, handle: HeapRef, offset: usize, count: usize) -> Result<(), VmFault> {
self.work += count.min(4096) as u64;
let registry = self.registry.ok_or_else(|| fault(ValueError::Type))?;
let ty = self.heap.concrete_type(handle, registry).map_err(fault)?;
let array = registry
.type_layout(ty)
.and_then(|t| t.array)
.ok_or_else(|| fault(ValueError::Type))?;
if registry.declarations().types[array.element_type_id.0 as usize].kind
!= TypeKind::Builtin(BuiltinId::INT32)
{
return Err(fault(ValueError::Type));
}
let len = self.heap.typed_array_len(handle, registry).map_err(fault)?;
if count > 4096 || offset.checked_add(count).is_none_or(|end| end > len) {
return Err(fault(ValueError::Bounds));
}
Ok(())
}
fn read(&mut self, handle: HeapRef, offset: usize, target: &mut [u8]) -> Result<(), VmFault> {
self.validate(handle, offset, target.len())?;
let registry = self.registry.unwrap();
for index in offset..offset + target.len() {
if !matches!(
self.heap.load_typed(registry, handle, index).map_err(fault)?.as_slice(),
[Value::Int32(0..=255)]
) {
return Err(fault(ValueError::Type));
}
}
self.work += target.len() as u64;
for (i, byte) in target.iter_mut().enumerate() {
let value = self.heap.load_typed(registry, handle, offset + i).map_err(fault)?;
let [Value::Int32(n)] = value.as_slice() else { unreachable!() };
*byte = *n as u8;
}
Ok(())
}
fn write(&mut self, handle: HeapRef, offset: usize, bytes: &[u8]) -> Result<(), VmFault> {
self.validate(handle, offset, bytes.len())?;
let registry = self.registry.unwrap();
for (i, &byte) in bytes.iter().enumerate() {
self.heap
.store_typed(registry, handle, offset + i, &[Value::Int32(byte as i32)])
.map_err(fault)?;
}
Ok(())
}
}
@@ -946,3 +946,31 @@ mod lifecycle_tests {
assert!(vm.call_stack.is_empty());
}
}
#[test]
fn native_byte_carrier_validates_entire_input_before_copy_and_charges_work() {
use prometeu_hal::host_return::GuestByteArrays;
let mut code = Vec::new();
op(&mut code, OpCode::Halt);
let image = image(&code, vec![function(0, code.len(), &[], &[], 0, 0)]);
let registry = image.registry().clone();
let mut vm = VirtualMachine::default();
let handle = vm.heap.allocate_typed_array(registry.clone(), TypeId(13), 3).unwrap();
vm.heap.store_typed(&registry, handle, 0, &[Value::Int32(255)]).unwrap();
vm.heap.store_typed(&registry, handle, 2, &[Value::Int32(256)]).unwrap();
let wrong = vm.heap.allocate_typed_array(registry.clone(), TypeId(14), 3).unwrap();
let mut access = super::native_arrays::SessionArrays {
heap: &mut vm.heap,
registry: Some(&registry),
work: 0,
};
let mut out = [17; 3];
assert!(access.read(handle, 0, &mut out).is_err());
assert_eq!(out, [17; 3]);
assert!(access.validate(wrong, 0, 3).is_err());
assert!(access.write(handle, 2, &[1, 2]).is_err());
access.write(handle, 2, &[42]).unwrap();
access.read(handle, 0, &mut out).unwrap();
assert_eq!(out, [255, 0, 42]);
assert!(access.work >= 12);
}
@@ -38,6 +38,14 @@ struct Cli {
#[arg(long, value_name = "PATH")]
games_root: Option<String>,
/// Install a memory-card image (never converted or overwritten).
#[arg(long, value_name = "PATH")]
memcard: Option<std::path::PathBuf>,
/// Create and fully allocate the named new memory card before installation.
#[arg(long, requires = "memcard")]
create_memcard: bool,
/// Path to the capability configuration file
#[arg(long, value_name = "PATH")]
cap: Option<String>,
@@ -63,6 +71,11 @@ pub fn run() -> Result<(), Box<dyn std::error::Error>> {
if let Some(games_root) = cli.games_root {
runner.set_games_root(games_root);
}
if let Some(path) = cli.memcard {
runner
.install_memcard(path, cli.create_memcard)
.map_err(|e| format!("Cannot install memory card: {e:?}"))?;
}
runner.set_boot_target(boot_target);
event_loop.run_app(&mut runner)?;
@@ -156,6 +156,26 @@ pub struct HostRunner {
}
impl HostRunner {
pub fn install_memcard(
&mut self,
path: std::path::PathBuf,
create: bool,
) -> prometeu_system::memcard::image::Result<()> {
self.firmware.os.memcard().mount_card_with(move || {
use prometeu_drivers::memcard::FileMemcard;
use prometeu_system::memcard::{image::IMAGE_BYTES, transaction::Card};
let storage = if create {
FileMemcard::create(&path, IMAGE_BYTES, |storage, id| {
Card::format(storage, id)
.map_err(|_| prometeu_hal::memcard::MemcardIoError::IoError)
})?
} else {
FileMemcard::open(&path, false)?
};
Ok(Box::new(storage))
})
}
/// Configures the boot target (Hub or specific Cartridge).
pub(crate) fn set_boot_target(&mut self, boot_target: BootTarget) {
self.firmware.boot_target = boot_target.clone();
+61 -1
View File
@@ -12,7 +12,7 @@ use prometeu_hal::{
};
use prometeu_system::{CrashReport, LatestRenderFrameStore};
const BOOT_TICK_LIMIT: usize = 64;
const BOOT_TICK_LIMIT: usize = 5000;
const HOST_FRAME_ADVANCE_TICK_LIMIT: usize = 100_000;
/// One published logical frame plus the automation counter.
@@ -118,6 +118,17 @@ struct HeadlessPlatform {
}
impl HeadlessHost {
/// Install storage through the shared worker. Progress is driven by ordinary host ticks.
pub fn install_memcard(
&mut self,
storage: Box<dyn prometeu_hal::memcard::MemcardStorage>,
) -> prometeu_system::memcard::image::Result<()> {
self.firmware.os.memcard().mount_card(storage)
}
pub fn memcard_preparation(&mut self) -> prometeu_system::memcard::admission::PreparationState {
self.firmware.os.memcard().admission.state
}
pub fn new() -> Self {
let clock = Arc::new(ControlledWallClock::default());
Self::with_clock(clock.clone(), Some(clock))
@@ -160,6 +171,20 @@ impl HeadlessHost {
/// The transition into `GameRunning` is the last tick. The first game
/// logical frame stays available for [`Self::advance_frame`].
pub fn boot(&mut self, path: impl Into<String>) -> Result<(), HostError> {
self.boot_with_choice(path, None)
}
pub fn boot_with_memcard_choice(
&mut self,
path: impl Into<String>,
choice: prometeu_system::memcard::admission::Choice,
) -> Result<(), HostError> {
self.boot_with_choice(path, Some(choice))
}
fn boot_with_choice(
&mut self,
path: impl Into<String>,
choice: Option<prometeu_system::memcard::admission::Choice>,
) -> Result<(), HostError> {
if self.boot_attempted {
return Err(HostError::AlreadyBooted);
}
@@ -167,6 +192,7 @@ impl HeadlessHost {
let path = path.into();
self.firmware.boot_target = BootTarget::Cartridge { path, debug: false, debug_port: 7777 };
let boot_deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
for _ in 0..BOOT_TICK_LIMIT {
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
return Ok(());
@@ -185,7 +211,41 @@ impl HeadlessHost {
report: None,
});
}
let progress = self.firmware.os.memcard().lane.progress_stamp();
let waiting = matches!(&self.firmware.state,FirmwareState::LoadCartridge(step) if step.needs_memcard_choice());
if waiting {
let Some(choice) = choice else {
return Err(HostError::BootFailed {
reason: "Memory-card choice required; use boot_with_memcard_choice".into(),
report: None,
});
};
self.signals = InputSignals::default();
self.tick_once();
self.signals = match choice {
prometeu_system::memcard::admission::Choice::Persistent => {
InputSignals { a_signal: true, ..Default::default() }
}
prometeu_system::memcard::admission::Choice::MemoryFresh => {
InputSignals { x_signal: true, ..Default::default() }
}
prometeu_system::memcard::admission::Choice::MemoryExisting => {
InputSignals { y_signal: true, ..Default::default() }
}
};
}
self.tick_once();
self.signals = InputSignals::default();
if matches!(self.firmware.state, FirmwareState::LoadCartridge(_)) {
if std::time::Instant::now() >= boot_deadline {
break;
}
self.firmware
.os
.memcard()
.lane
.wait_for_progress(progress, std::time::Duration::from_millis(16));
}
}
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
@@ -0,0 +1,155 @@
---
id: LSN-0067
ticket: memcard-fixed-slots-v1
title: Save publication and resource ownership are separate
created: 2026-10-13
tags: [memcard, persistence, async-work, ownership, recovery]
---
## Context
The runtime now implements one installed shared memory-card image, per-game RAM
snapshots and asynchronous explicit publication. The image has 64 logical slots
of 64 KiB and 68 rotating physical blocks. Game identity and public presentation
live in a catalog; binary slot records carry mapping, generation, timestamp and
CRC32C. The runtime validates private payload integrity without interpreting its
meaning. A game's local slot index remains stable while physical blocks rotate.
The historical source was accepted decision DEC-0055, executed through PLN-0186
for AGD-0064. Those workflow artifacts are disposable; the normative references
are [Save Memory and MEMCARD](../../../docs/specs/runtime/08-save-memory-and-memcard.md),
[Registry](../../../docs/specs/runtime/02c-program-registry-and-pbx-v1.md),
[Host ABI](../../../docs/specs/runtime/16-host-abi-and-syscalls.md) and
[producer handoff](../../../docs/vm-arch/registry-producer-handoff.md).
## Publication knowledge and resource lifetime
A timeout describes the observer's knowledge. It does not prove that the host
stopped touching memory or storage. A request therefore has two independent
concerns: the retained result visible to the game, and ownership held until the
worker actually returns. Releasing a result or ending a game cannot release a
snapshot source still in use. Conversely, a completed retained result must not
keep the lane occupied or prevent session teardown.
The implementation protects only the committing slot from writes and clear;
reads and other slots remain available. It retains one full-slot scratch buffer
instead of copying the whole snapshot for each save. Request context, mount
epoch and session identity prevent a late completion from updating a replacement
session. Regression coverage includes ending a session with an unconsumed,
already completed result, which exposed an ownership leak during integration.
Cancellation and entry into publication share an atomic control state. At the
deadline, a canceled attempt that never entered publication can report failure.
Once publication may have started, the result remains indeterminate until
reconciliation. A blocked host requires restart escalation even when publication
was provably prevented: storage and source ownership still have to remain valid.
The runtime neither kills the thread nor starts a replacement worker over its
resources. The watchdog runs outside that worker.
## A checksum does not make an old reference safe
Two valid map pages can reference different generations of payload and public
catalog data. The allocator protects the union of references from all usable
maps. It durably retires an old map before overwriting any block or public copy
that only that map protects. Otherwise recovery could select a valid old map
whose referenced bytes were already replaced.
Each candidate is coherent only after its referenced catalog, icons and payload
checksums validate. Recovery may select an older coherent map and reports that
fact. Unknown administrative damage blocks further writes until explicit
verification. Known quarantined blocks remain excluded; revalidation does not
silently reintegrate them. Same-card replacement carries known exclusions and
the attempted publication map so it can reconcile sequence, identity and slot
descriptors without overwriting the game's RAM snapshot.
The file backend allocates the entire image, including unused slot and public
regions, before namespace publication. Creating a sparse file of the right length
would postpone capacity failure until gameplay. Creation also refuses to replace
an existing file. Legacy save files remain untouched.
## Priority requires bounded work from every consumer
A highest-priority queue entry cannot preempt an arbitrary blocking asset job.
The production asset manager therefore uses bounded resumable decoding steps on
the same lane as saves, including preload. Glyph, sound and scene jobs retain
their progress and resume after the save; completed payloads are installed only
under the current asset generation. A second production asset worker would have
defeated the intended hardware budget.
Preload required special attention because it previously bypassed the ordinary
asynchronous path. Headless boot now waits for actual work progress while polling
deadlines; it does not impose artificial frame pacing. The resulting scheduling
contract is described in
[Coroutines and Cooperative Scheduling](../../../docs/specs/runtime/09a-coroutines-and-cooperative-scheduling.md).
## Bootstrap belongs at an explicit runtime boundary
The cartridge declares capability and requested slot count; Registry role 3 binds
the optional memcard bootstrap callable. An explicit producer boundary runs it
after producer initialization and before the first frame. The runtime preserves
the main continuation and applies a separate bounded call budget, without
learning frontend concepts such as Init or Frame roles.
Snapshot and preload preparation precede the hook. New registration stays
provisional until the hook reports readiness. A save requested during bootstrap
waits for registration publication rather than publishing payload under an
unregistered owner. Typed executable fixtures exercise this ordering, the
Array<Int32> byte carrier and once-only invocation.
Debugger stepping must use the same live memcard and session services as normal
execution. A temporary service created only for stepping would invalidate
context handles and make otherwise correct bytecode fail under a debugger.
Both execution paths are covered by the firmware integration fixture.
The general controlled-call design builds on
[lifecycle continuation ownership](../DSC-0062-guest-lifecycle-pause-and-resume-api/LSN-0066-lifecycle-calls-retain-the-game-continuation.md).
Save timestamps use the host's civil clock; ordering and timeouts use independent
mechanisms, as explained in
[civil time and elapsed time](../DSC-0063-host-clock-interface-for-runtime-utc-timestamps/LSN-0065-civil-time-is-not-ordering-or-elapsed-time.md).
## Player choice and opaque data
Memory-only mode uses the same RAM snapshot operations and reports its lack of
persistence explicitly. It can load an existing readable save or start clean,
including when persistent allocation cannot grow. No temporary disk save or
automatic upload hides behind that choice.
Removal and replacement preserve local edits. Same-card reinsertion reconciles
without reloading RAM; a different card requires explicit keep-local or
load-existing policy. SO replacement APIs require suspended gameplay. Unsaved
exit confirmation and the removal/restart dialogs use the existing lifecycle;
neither initialization nor bootstrap reruns on resume. External administration
removes a whole game, while a game's clear operation retains slot ownership.
The full external save-management application remains outside this delivery.
## Validation and limits
The tests cover image/schema bounds, CRC vectors, full-pool rotation, clean
reassignment, catalog growth/removal, quarantine and reintegration. Fault storage
separates volatile and durable bytes and injects commit/catalog failures,
including synchronization that persists bytes before returning an error. Other
tests cover a blocked host before and during publication, source protection,
retained results, RAM-only admission, card replacement and typed guest bootstrap.
Real file tests verify complete allocation, reopening, read-only access and
non-replacement of existing files. These are deterministic software checks,
not physical power-cut qualification.
A local debug-build measurement ran 20 full 64 KiB commits while a production
512x512 glyph asset load was active:
| Measurement | p50 | p95 | Maximum |
| --- | ---: | ---: | ---: |
| Admission to first IO | 0.049 ms | 0.071 ms | 0.139 ms |
| Commit completion | 11.379 ms | 24.428 ms | 29.839 ms |
These samples met the start and healthy-completion targets on the exercised local
file host. They do not establish handheld device tail latency or physical storage
durability. Each host backend and storage device needs its own qualification;
the implemented fallback for a host that cannot stop safely is resource retention
and explicit restart escalation.
Workspace regression includes all targets and features, formatting, clippy and
architecture checks. The five desktop debugger socket tests also pass when local
socket binding is allowed. The external compiler and raw-flash backend were not
part of this implementation; the producer handoff documents their integration
boundary without an external artifact compatibility layer.
@@ -89,17 +89,20 @@ Code offsets are relative to Code. Local and temporary counts widen from current
## Optional lifecycle roles
The executable Function record carries one role byte before its optional name:
`0=None`, `1=BeforePause`, `2=BeforeResume`. Zero means an ordinary function,
`0=None`, `1=BeforePause`, `2=BeforeResume`, `3=MemcardBoot`. Zero means an ordinary function,
not a callable hook. Unknown values reject with InvalidRegistry/LifecycleRole.
Each nonzero role has at most one target; duplicates reject with
InvalidRegistry/DuplicateLifecycleRole. Targets MUST NOT be FunctionId 0 and
MUST have zero parameters, results and captures; violations reject with
MUST have zero captures. Pause/resume roles have zero parameters and results;
MemcardBoot has exactly one Int64 parameter and one Int32 result. Violations reject with
InvalidEntrypoint/LifecycleSignature. Preserve function/section context and the
wire offset when decoding an invalid role. Resolve optional FunctionIds once
in the immutable Registry. Names, exports, manifests and trait Method records
are not the binding mechanism.
Either hook may be absent. Additional roles require a future contract; there is
Pause/resume hooks may be absent. MemcardBoot is required for a cartridge with
the memcard capability and is invoked only at its explicit bootstrap gate.
Additional roles require a future contract; there is
no termination role. PBX remains version 1, with no old-layout fallback or
external-artifact compatibility. Producers MUST regenerate their images and
emit this byte for every function, including zero for ordinary functions.
+277 -92
View File
@@ -3,138 +3,323 @@
Domain: virtual hardware: save memory
Function: normative
Didactic companion: [`../learn/mental-model-save-memory-and-memcard.md`](../runtime/learn/mental-model-save-memory-and-memcard.md)
The runtime implements image transactions, per-game RAM snapshots, a shared
asset/IO worker and an explicit guest bootstrap gate. Host latency and durability
qualification apply to the concrete host and storage device.
## 1 Scope
## 1. Ownership and capacity
This chapter defines the `game` persistence surface in PROMETEU.
A runtime has at most one installed card. A card contains 64 shared logical slots,
each holding 0..65,536 private bytes, and 68 physical blocks of 65,536 bytes.
There are four nominal rotating spares when all logical slots contain data; all
healthy unprotected blocks participate in the pool. Physical IDs are never game
slot IDs. Each of at most 64 game records owns at least one logical slot.
MEMCARD is a runtime-owned service layered on top of the runtime filesystem.
The cartridge declares a `memcard_slots` count in 1..64 together with the memcard
capability. Allocation and growth are all-or-nothing. A smaller declaration uses
the prefix of the prior allocation and retains excess slots/data. The authorized
`app_id` is the cartridge's uint32 identity. Guest indices are local 0..N-1.
Newly assigned slots expose no residual bytes. Game clear retains ownership;
external administration removes the entire game and all its metadata/slots.
There is no external single-slot deletion. Space management/card switching is
an explicit player action outside gameplay.
Games do not get broad path-based filesystem access for saves. They use slot operations only.
## 2. Image encoding
## 2 Profile Contract (`game`)
All integers are unsigned little-endian unless stated otherwise. There is no
filesystem inside the image. Every reserved byte is zero. Readers reject unknown
versions, malformed lengths, overlapping/out-of-range references and unknown
flags before following references. Creation allocates the entire 5,259,264-byte
image physically, including currently unused regions, and publishes the file's
namespace durably. Sparse length extension alone is insufficient.
The v1 contract is fixed:
| Region | Offset | Length |
| --- | ---: | ---: |
| Identity A | 0 | 4,096 |
| Identity B | 4,096 | 4,096 |
| Map A | 8,192 | 4,096 |
| Map B | 12,288 | 4,096 |
| Catalog A | 16,384 | 131,072 |
| Icons A | 147,456 | 262,144 |
| Catalog B | 409,600 | 131,072 |
| Icons B | 540,672 | 262,144 |
| Physical blocks 0..67 | 802,816 | 4,456,448 |
- `32` slots per game (`slot_index: 0..31`);
- each slot has up to `32KB` payload;
- ownership is isolated by `app_id`;
- persistence is explicit through `slot_commit`;
- copy/export/import is a Hub/OS responsibility (outside game userland).
Each identity page contains: magic `PMCARD1\0` at0 (8 bytes), version1:u32
at8, image length:u32 at12, host-generated random cardId:[u8;16] at16,
zero padding32..4092 and CRC32C:u32 at4092. CRC covers bytes0..4092 (exclusive
of the CRC). Either valid identity is sufficient; two valid differing identities
are inconsistent. A degraded single identity must be reported.
## 3 Slot Identity and Metadata
CRC32C uses reflected Castagnoli polynomial0x82f63b78, initial0xffffffff,
final xor0xffffffff, reflected byte input/output. Empty input produces0;
ASCII `123456789` produces0xe3069283. Payload CRC covers exactly used bytes.
Catalog and icon CRCs cover exactly the declared used region. Administrative
page CRC covers all bytes preceding its last4-byte checksum, including padding.
Each slot has runtime-owned metadata:
Each map page contains:
- `app_id` owner;
- `slot_index`;
- `save_uuid`;
- `generation`;
- `checksum`;
- `payload_size`;
- `state`.
State values:
- `0 = EMPTY`
- `1 = STAGED`
- `2 = COMMITTED`
- `3 = CORRUPT`
`generation` and `checksum` are not manually controlled by game code:
- `generation` increments on successful `slot_commit`;
- `checksum` is recalculated by runtime for committed payload integrity.
## 4 Host ABI Surface (v1)
Canonical module: `mem` (version `1`).
| Operation | Signature |
| Offset | Field |
| --- | --- |
| `slot_count` | `mem.slot_count() -> (status:int, count:int)` |
| `slot_stat` | `mem.slot_stat(slot:int) -> (status:int, state:int, used_bytes:int, generation:int, checksum:int)` |
| `slot_read` | `mem.slot_read(slot:int, offset:int, max_bytes:int) -> (status:int, payload_hex:string, bytes_read:int)` |
| `slot_write` | `mem.slot_write(slot:int, offset:int, payload_hex:string) -> (status:int, bytes_written:int)` |
| `slot_commit` | `mem.slot_commit(slot:int) -> status:int` |
| `slot_clear` | `mem.slot_clear(slot:int) -> status:int` |
| 0 | magic `PMMAP1\0\0`, 8 bytes |
| 8 | publication sequence:u64, positive |
| 16 | public-copy selector:u32, 0 or1 |
| 20 | catalog used length:u32, <=131072 |
| 24 | catalog CRC32C:u32 |
| 28 | icons used length:u32, <=262144 |
| 32 | icons CRC32C:u32 |
| 36 | cardId:[u8;16], equal to identity |
| 52 | 12 reserved zero bytes |
| 64 | rotation order: permutation of physical IDs0..67, 68 bytes |
| 132 | quarantine flags: 68 bytes, each0 or1 |
| 200 | 56 reserved zero bytes |
| 256 | 64 slot descriptors, each40 bytes |
| 2816 | zero padding through4091 |
| 4092 | page CRC32C:u32 |
`status` is always the first return slot.
A slot descriptor has state:u8 at0 (0 unassigned,1 EMPTY,2 payload), physical
ID:u8 at1 (255 unless payload), zero:u16 at2, used bytes:u32 at4,
generation:u64 at8, update_at:u64 at16, payload CRC32C:u32 at24 and12 zero
bytes at28. Unassigned is canonical except physical ID255. EMPTY has no physical
block, zero used bytes/CRC, but can have a save generation/time. Payload requires
1..65536 used bytes, physical ID0..67 and positive generation. A physical block
has at most one owner per map and cannot also be quarantined. Assigned descriptor
IDs must equal the union of catalog assignments.
### 4.1 Byte transport note (v1)
Generation0 means never saved, with canonical timestamp0; first persistent commit
is1, including empty commits. Later commits/clear increment it. With positive
generation, timestamp0 is a valid epoch date. Sequence and generation never wrap;
a required increment at uint64 maximum refuses publication. Equal sequence only
permits byte-identical map content; differing content is corruption.
Current ABI transport for memcard payload is `payload_hex:string`.
## 3. Public catalog and icons
This is a temporary transport form until VM-owned bytes (`HeapRef<Bytes>`) is exposed in the host ABI path.
Each catalog is UTF-8 JSON, schema version1, maximum depth8, closed objects and no
duplicate keys. Integers are exact unsigned values in their declared range (no
floating-point conversion). Unknown fields, wrong types and invalid Unicode are
rejected. The root has exactly `metadataVersion` (1) and `games` (array, <=64).
Each game has exactly:
## 5 Operation Semantics
- `app_id`: uint32, unique and authorized by the runtime;
- `title`: nonblank UTF-8 string, <=128 decoded bytes, reject rather than truncate;
- `dataFormatVersion`: uint32, game-defined including0;
- `created_at`: uint64 UTC Unix seconds for first registration;
- `slots`: nonempty array of distinct logical IDs0..63, exclusive across games;
- `icon`: object with `offset`:uint32, `frames`:1..4, `duration`:uint16,
`crc32c`:uint32. Offset is relative to the selected public icon region.
### `slot_count`
An icon contains a48-byte palette in RGB order (16 opaque RGB888 colors), followed
by512 bytes per32x32 frame, row-major, first pixel in the high nibble. All16
indices are ordinary opaque colors. Its length is48+512*frames. Icon ranges must
not overlap and CRC covers palette and all frames. `duration` is1..65535 units of
1/30 second, shared by all animated frames. Static icons use canonical duration1.
A consumer cycles frames while visible using its SO/Hub clock; rendering or a
save-management application is not part of the game runtime.
- returns slot capacity visible to game profile (`32`);
- does not depend on slot content.
The maximum icon payload is64*2096=134144 bytes, within262144. Even charging768
bytes per escaped128-byte title, maximal decimal integers, every logical-slot ID
and field punctuation, a canonical game record is bounded by2048 bytes; the
actual maximum catalog must fit131072 including its root. Producers use compact
serialization and validate final length; readers never allocate from an unchecked
length or require both public copies/icons in RAM.
### `slot_stat`
Ordinary slot commits do not rewrite JSON. created_at survives updates, but a
removed/recreated record obtains a new date. Slot update_at/generation/CRC live
only in binary descriptors. The game's last-save date is derived as max update_at
over positive generations; no such slots means never saved. Public readers can
locate slot bytes and check CRC without interpreting the private payload.
- returns current state and integrity/version metadata for one slot.
## 4. Publication, retirement and recovery
### `slot_read`
Before overwriting any block/public region, protect the union of references from
both usable maps. Retire an older map by overwriting its full page with zero,
syncing and verifying it is no longer a usable map; only then may references held
exclusively by it be reused. The current coherent map remains untouched. A failed
retirement stops that operation; no referenced data is overwritten speculatively.
- reads from staged content when present, otherwise committed content;
- returns empty payload with success when offset is beyond current payload end.
A nonempty commit freezes its RAM source, chooses a healthy unprotected block,
writes payload, syncs and verifies readback CRC. Then it writes the inactive map
with next sequence/descriptor, syncs and verifies that map before SUCCESS. Empty
commit publishes EMPTY without a data block. Former blocks join the end of the
rotation queue when eligible; they are not erased after commit.
### `slot_write`
Catalog changes prepare the inactive public copy, sync/readback-verify it, and
publish its reference together with allocation/descriptors in a new map. Retire
any old map referencing that destination before overwriting it. Registration,
growth, whole-game removal and presentation update are coherent administrative
publications. Payload is never saved automatically. dataFormatVersion changes do
not migrate data or promise atomic migration of multiple slots.
- writes into slot staging buffer (does not persist directly);
- supports offset writes;
- does not allow resulting payload above `32KB`.
Mount validates candidate maps, catalog and all referenced payload CRCs; select
the newest coherent publication. A page checksum alone is insufficient. Report
recovery from an older map explicitly; never silently roll back confirmed saves.
No coherent map means corruption, not empty media. Persist quarantine exclusions;
known exclusions survive recovery of an older map. If exclusion cannot be made
durable safely, block writes until revalidation. Reintegration requires explicit
verification, never merely remount. Transient timeout does not prove block damage.
Mount state and health/write permission are separate: DEGRADED may write with a
safe candidate; READ_ONLY still permits valid reads.
### `slot_commit`
Durable result codes are SUCCESS=1, FAILURE=2, INDETERMINATE=3. PENDING=0 and
MEMORY_ONLY=4 are request states/results without a durability claim. FAILURE
requires proof of no publication; write/sync errors may have partial effects.
The uncertain window begins when publication-capable metadata is attempted.
Retain attempt cardId/slot/target sequence/generation/CRC for same-card
reconciliation before more writes. Confirmed success survives cleanup failure.
- persists staged payload for one slot;
- commit is atomic at slot operation level (all-or-error, no silent partial success).
## 5. RAM and guest operations
### `slot_clear`
Before boot reserve N*65536 bytes of actual snapshot capacity, one 65536-byte
scratch and bounded metadata/backend overhead. Memory-only mode has the same
requirement. Insufficient RAM prevents admission with a diagnostic. Existing
saves are validated and loaded, never replaced with empty on integrity error.
Only the game's declared prefix is resident, not other games' payloads.
- clears staged content and removes committed slot payload.
Read/write/clear are bounded RAM operations. Write zero-fills gaps and rejects
end offsets beyond65536. Clear makes used length0 and retains ownership; commit
persists that clear. During an in-flight commit the source slot rejects mutations
with BUSY, but remains readable; other slots/gameplay continue. No duplicate full
source snapshot is allowed. Protect its storage until the host ceases all use.
## 6 Status Codes (Domain-Owned)
The mem module uses a session-bound Int64 context and local slot indices. Byte
transport is Array<Int32>, values 0..255, at most 4096 bytes per guest call. Validate
array type, element values, range/overflow, ownership and all output capacity
before copying or mutating. Charge array storage (four bytes per element) and
copy work. Host IO maximum remains 65536. A native array reference cannot escape
the bounded call into asynchronous work; async source is the native snapshot.
Minimum status catalog:
The [guest ABI](#guest-abi) fixes domain status and request result encodings.
Request identity is separate from status and durable result. All queries validate
session/mount/request identity. Only one request/result entry exists, retained
until explicit release; release never frees in-flight resources. No payload queue.
- `0 = OK`
- `1 = EMPTY`
- `2 = NOT_FOUND`
- `3 = NO_SPACE`
- `4 = ACCESS_DENIED`
- `5 = CORRUPT`
- `6 = CONFLICT`
- `7 = UNAVAILABLE`
- `8 = INVALID_STATE`
## 6. Bootstrap and producer contract
## 7 Fault Classification
The Registry role 3 is MemcardBoot(context:Int64)->Int32, no captures, unique and
never FunctionId 0. Role number is not FunctionId. Capability requires that binding;
absence of capability skips snapshot and hook. Roles 0/1/2 are unchanged.
MEMCARD follows `16a`:
The producer emits an explicit memcard bootstrap boundary after its initialization
and before its first frame. Runtime recognizes a bootstrap phase, not FE concepts
such as Init/Frame. Snapshot/preload is ready before that boundary; the hook runs
once on the main lane, preserving continuation, with1,500,000 total cycles and
250,000-cycle slices outside frame accounting. READY=0 or INCONSISTENT=1 returns
with a bounded diagnostic (up to1024 UTF-8 bytes) set via the service. Invalid
return/trap/budget exhaustion blocks admission and reports through SO/Hub.
- `Trap`: structural violations (invalid slot index, invalid call shape, invalid window value type/range);
- `status`: operational domain conditions;
- `Panic`: runtime invariant break only.
New records/slots are provisional while the hook provides presentation. A commit
may be submitted during the hook; it remains pending until READY and coherent
catalog publication. The hook must return rather than wait for that dependent
commit; normal frames may later query its result. This uses the one existing
request entry, not a second queue. INCONSISTENT invalidates provisional admission
and never publishes its payload. IO wait and guest cycle budgets are separate.
Retries/remount/resume never automatically rerun the hook or main initialization.
Silent no-op is forbidden for operations with operational failure paths.
## 7. Executor, host and deadlines
## 8 Runtime Layering and Storage Mapping
Host implements complete-or-error read_at/write_at of up to 64 KiB, sync and
bounded cancellation/recovery or fault escalation. Partial transfers are normalized
inside host bounds; failure may have changed storage. Runtime owns all logical
mapping, checksums, publication and request policy. Deliver file backend and fault
backend; raw flash requires separate qualification.
MEMCARD is a domain layer over runtime FS.
Use one shared async_work lane with assets. Save has highest priority; an asset
finishes a bounded safe step, retains its progress and yields to the whole save,
then resumes from that point. Retained save result does not retain the worker.
No extra worker or waiting inside the only worker implements yielding.
Runtime host mapping for v1 is namespace-isolated by `app_id`, for example:
Measure from admission with monotonic time: start within one nominal frame
(~16.7ms), healthy target 100ms, initiate cancellation at 500ms, stop or escalate
and notify by 1s. Deadline control runs outside potentially blocked IO. Coordinate
cancel with publication entry; no retries after deadline. Timeout is a reason,
not proof of rollback. If cessation is unconfirmed, isolate mount/resources,
block new work and retain source/scratch. Late completion belongs to original
reconciliation. Never kill threads unsafely, free buffers in use or create a
replacement worker. Report restart requirement when host recovery is impossible.
Fast-profile claims require measurements under asset load, including tail latency.
`/user/games/<app_id>/memcard/slot_<NN>.pmem`
## 8. Player choice and lifecycle
The exact host-side file naming is internal to runtime/host as long as ownership and isolation guarantees are preserved.
The player chooses PERSISTENT or MEMORY_ONLY through SO/Hub. No required/optional
mode is imposed by the game. Memory-only commit performs no IO and makes no
persistence claim or hidden temporary save. With a readable existing save, offer
load-to-RAM or clean start explicitly. RAM survives pause/resume and removal;
warn before discarding unsaved changes when ending the session.
## 9 Explicit Persistence Rule
Removal uses existing OS pause and external choices to restore/switch card,
continue in RAM or exit. Same-card reinsertion reconciles with a new mount epoch
without overwriting RAM. Different card requires validation/allocation and explicit
keep-local/load-existing choice. No automatic upload, reload, save or reinit.
BeforePause can submit and return without waiting for disk; BeforeResume can query.
Session termination retains resources used by outstanding IO until safe cessation.
PROMETEU does not auto-save slot staging data.
The legacy per-slot file format is unsupported by this image codec. Preserve
legacy files; do not migrate or delete them automatically. Internal fixtures are
regenerated; removal of the old runtime path follows validated integration.
Without `slot_commit`, writes remain staged and are not guaranteed as persisted committed state.]
## Guest ABI
All entries use module `mem`, version 1 and capability `memcard` (bit 9).
Context and request IDs are positive Int64 values. Other numeric domain results
use Int64; `dirty`, `new`, and health flags use Bool. Unsigned 64-bit generations
and timestamps use the Int64 bit pattern without changing their unsigned meaning.
Guest indices/ranges may use Int32 or Int64 and must be nonnegative.
Error results carry the leading status and Null for unused payload positions.
| ID | Operation | Results in declaration order |
| --- | --- | --- |
| 0x4200 | bootstrap() | hook result: Int32 (0 READY, 1 INCONSISTENT) |
| 0x4201 | slot_count(context) | status, count |
| 0x4202 | slot_stat(context, slot) | status, has_payload, RAM used_bytes, generation, update_at, CRC32C, dirty |
| 0x4203 | slot_read(context, slot, offset, array, array_offset, count) | status, bytes_read |
| 0x4204 | slot_write(context, slot, offset, array, array_offset, count) | status, bytes_written |
| 0x4205 | slot_commit(context, slot) | status, request |
| 0x4206 | slot_clear(context, slot) | status |
| 0x4207 | query(context, request) | status, state, reason |
| 0x4208 | release(context, request) | status |
| 0x4209 | info(context) | status, mode, new, recovered, degraded, write_blocked |
| 0x420A | metadata(context) | status, title, dataFormatVersion, created_at |
| 0x420B | presentation(context, title, dataFormatVersion, frames, duration, array, offset, count) | status |
| 0x420C | diagnostic(context, message) | status |
| 0x420D | publish_presentation(context) | status, request |
`slot_stat` reports the durable descriptor alongside RAM length/dirty state;
CRC32C does not pretend that uncommitted RAM is already durable. `presentation`
sets the game-owned public fields; initial boot publishes them after READY.
Subsequent explicit `publish_presentation` uses the same one-request slot.
Icon bytes include the 48-byte RGB888 palette followed by packed frames.
Diagnostics are UTF-8 strings bounded to 1024 bytes.
Status values: 0 OK, 3 NO_SPACE, 4 ACCESS_DENIED, 5 CORRUPT,
6 INDETERMINATE, 7 UNAVAILABLE, 8 INVALID_STATE/CANCELED, 9 BUSY,
10 READ_ONLY/COUNTER_EXHAUSTED, 11 INVALID_RANGE, 12 UNSUPPORTED,
13 IO_ERROR, 16 OUT_OF_MEMORY. Query states: 0 PENDING, 1 SUCCESS,
2 FAILURE, 3 INDETERMINATE, 4 MEMORY_ONLY. Query reason uses the status
catalog, plus 15 for deadline/restart escalation. Mode 0 is PERSISTENT and
1 is MEMORY_ONLY. Type/heap/range violations at the array carrier are VM traps;
ordinary storage failures are domain results.
## Host integration
The desktop host accepts `--memcard PATH` for an existing image and
`--memcard PATH --create-memcard` for explicit creation. Creation never replaces
an existing file. File creation, opening, mount validation and save IO execute
on the shared worker. Unsupported hosts must reject physical-allocation claims.
The headless host accepts an injected `MemcardStorage` through `install_memcard`
and an explicit player policy through `boot_with_memcard_choice`. Its boot loop
waits for actual worker progress with a bounded watchdog timeout; frame execution
remains independent of real-time pacing.
Before guest boot, firmware presents persistent, fresh-memory-only and (when
available) existing-save-memory-only choices. Choosing no card does not bypass
snapshot reservation. Leaving a resident game with dirty RAM requires an explicit
discard decision. A blocked host operation produces a restart dialog by 1 second;
it does not free in-flight buffers or create another worker.
Native administrative operations run outside gameplay. Whole-game removal,
explicit card revalidation and explicit reintegration use the shared worker.
Revalidation preserves known quarantined blocks; reintegration separately tests
and durably clears one exclusion. Same-card replacement preserves RAM and advances
the mount epoch. Different-card replacement takes an explicit keep-RAM or
load-existing choice and never invokes initialization or bootstrap again.
@@ -80,8 +80,8 @@ flush, disk-completion wait, guest event queue or reason argument is added.
Host-owned in-flight work keeps its ownership until the service/host safely
releases it; lifecycle failure does not simulate cancellation or free borrowed
buffers. Native calls must return promptly: VM fuel only bounds bytecode and
cannot preempt a blocked native/host call. Existing FS read/write and the current
memcard commit backend are synchronous interfaces, so this implementation does
not promise a wall-time bound for them. The new asynchronous memcard backend
must expose bounded submission/polling independently; hooks can then submit an
explicit commit without waiting for disk completion.
cannot preempt a blocked native/host call. Existing FS read/write interfaces remain synchronous. Memcard commits use
bounded admission and query calls on the shared asset/IO lane. BeforePause can
submit a commit and return immediately; BeforeResume can query the retained
result. The independent 500ms cancellation and 1s host escalation deadlines are
defined by [save memory](08-save-memory-and-memcard.md).
+11
View File
@@ -35,6 +35,7 @@ The runtime currently expects the following manifest fields:
Additional manifest-supported fields include:
- `capabilities`
- `memcard_slots`: a count in 1..64, required exactly when the memcard capability is declared
## 3 Required Fields
@@ -63,6 +64,7 @@ The current loader validates:
- `cartridge_version == 1`
- manifest parse validity
- capability normalization
- memcard capability and slot-count consistency
- presence of `program.pbx`
- when `Asset` capability is declared, presence of valid `assets.pa` before asset bootstrap
@@ -88,6 +90,15 @@ This means:
- firmware/runtime loads the cartridge and starts execution from `func_id = 0`;
- nominal exports may exist for linking or introspection, but they do not hold boot authority.
A cartridge declaring memcard support also supplies the unique Registry role 3
`MemcardBoot(context: Int64) -> Int32` binding, with no captures and a nonzero
FunctionId. Its producer emits the explicit `mem.bootstrap` boundary after its
initialization and before its first frame. The runtime prepares the selected
persistent or memory-only snapshot and asset preload before this optional hook;
normal frame execution waits for successful bootstrap and registration. See
[Save Memory and Memcard](08-save-memory-and-memcard.md) for admission, the guest
contract, and publication semantics.
## 5.1 Runtime Profiles
`Game` and `System` are distinct runtime profiles.
+8 -20
View File
@@ -187,27 +187,15 @@ Return shape must also follow the transversal status-first policy in [`16a-sysca
### MEMCARD game surface (`mem`, v1)
The game memcard profile uses module `mem` with status-first return shapes.
`mem` is a domain layer backed by runtime `fs` (it is not a separate storage backend).
The game surface requires `memcard` capability and an admitted RAM snapshot.
Its native host owns disk IO through `MemcardStorage`; guest code only accesses
its own logical slots. Payload transport is bounded `Array<Int32>` with checked
byte values, never hex strings or retained guest heap references.
The `mem` module is a game profile surface. It is not the public storage model
for `System` profile apps.
Durable memcard data is keyed by `app_id` according to the save-memory
contract. Runtime handles, open file tables, pending lifecycle delivery, and
other mutable VM-facing syscall context are scoped to the owning VM session and
MUST NOT be used as durable app identity.
Canonical operations in v1 are:
- `mem.slot_count() -> (status, count)`
- `mem.slot_stat(slot) -> (status, state, used_bytes, generation, checksum)`
- `mem.slot_read(slot, offset, max_bytes) -> (status, payload_hex, bytes_read)`
- `mem.slot_write(slot, offset, payload_hex) -> (status, bytes_written)`
- `mem.slot_commit(slot) -> status`
- `mem.slot_clear(slot) -> status`
Semantics and domain status catalog are defined by [`08-save-memory-and-memcard.md`](08-save-memory-and-memcard.md).
`mem.bootstrap()` invokes the Registry MemcardBoot role at the producer's explicit
post-initialization gate. Ordinary domain calls use an opaque session context,
status-first results and one retained asynchronous request. See the complete
[ABI table and storage contract](08-save-memory-and-memcard.md#guest-abi).
### Asset surface (`asset`, v1)
+6 -3
View File
@@ -100,14 +100,17 @@ Example groups:
- `gfx`
- `audio`
- `asset`
- `fs` (includes game memcard module `mem` in v1)
- `fs`
- `memcard` (game module `mem`, bit 9)
Capability checks exist to constrain which host-managed surfaces a cartridge may use.
Input in v1 is VM-owned intrinsic surface and is not capability-gated through syscall policy.
Game memcard operations (`mem.*`) are status-first and use `fs` capability in v1.
`mem` remains layered on runtime `fs`; no parallel persistence channel is introduced.
Game memcard operations (`mem.*`) require the separate `memcard` capability
and a manifest `memcard_slots` count in 1..64. Domain operations are status-first;
`mem.bootstrap()` is a controlled-call boundary returning the hook result.
Storage is a host-owned region backend, independent of runtime `fs`.
Domain surface, status catalog and slot semantics are defined in [`08-save-memory-and-memcard.md`](08-save-memory-and-memcard.md).
Asset backlog operations are status-first. `queued`, `active`, `ready`,
+37 -10
View File
@@ -17,7 +17,8 @@ reference boundaries. The separate render frame scheduler remains active.
Fn values, frame-owned capture environments, typed indirect calls and receiver-first
trait dispatch use [the function-value contract](../specs/runtime/02b-vm-function-values-and-closures.md).
FRAME_SYNC resumes next PC; successful root RET permits recurring entry preparation.
New strings/bytes/save APIs, long-run stress and representative-game headroom require
The memcard byte carrier and bootstrap use the [save-memory contract](../specs/runtime/08-save-memory-and-memcard.md).
General string/byte APIs, long-run stress and representative-game headroom require
separate technical contracts and workloads.
The external Studio handoff targets are the build-pipeline
@@ -38,7 +39,7 @@ fallback. Use the canonical chapter's field order and UTF-8 rules, not Rust layo
TypeId, FunctionId and ImplementationId are independent image-local table indices;
zero is valid. BuiltinIds are imports, not local TypeIds. Function records contain
optional name, ordered named parameters, ordered result TypeIds, code offset/length,
a lifecycle-role byte followed by optional name, ordered named parameters, ordered result TypeIds, code offset/length,
extra local slots and maximum temporary slots. Names are independent of debug.
Widths are derived from TypeIds; serialize no parameter/result width caches.
`max_stack_slots = 0` is a literal zero limit. Boot authority remains FunctionId 0.
@@ -55,22 +56,48 @@ module/name/version and exact ABI counts; every declaration must be used. Loadin
binds it to SYSCALL after capability/ABI validation. Raw SYSCALL in producer PBX,
unknown declarations, invalid indices and unused declarations are rejected.
## Memcard and controlled-call lowering
Emit role 0 for ordinary functions, role 1 for optional BeforePause and role 2
for optional BeforeResume. Both lifecycle hooks have no parameters, results or
captures. A memcard-enabled cartridge declares capability `memcard`, a manifest
`memcard_slots` count in 1..64, and exactly one role 3 function with signature
`(Int64 context) -> Int32`, no captures, and a FunctionId different from zero.
The compiler chooses source-level spelling; the runtime binds only the role.
After frontend-owned initialization and before the first frame, emit the declared
`mem.bootstrap` HOSTCALL (0 arguments, 1 Int32 result). The runtime invokes role 3
on the same VM, then resumes the producer continuation after READY and coherent
registration. The producer must not directly call the role as a substitute for
this gate. Use 0 READY or 1 INCONSISTENT, with bounded `mem.diagnostic` text when
needed. This function executes once per boot with controlled-call fuel, outside
frame accounting; Init and Frame remain frontend concepts.
Declare the exact [mem ABI](../specs/runtime/08-save-memory-and-memcard.md#guest-abi).
Use concrete `Array<Int32>` storage for bytes 0..255, transfer at most 4096 bytes
per call, and preserve the session context for subsequent calls. The array's
storage charge is four bytes per element. Save requests return an opaque ID;
query reports durable success/failure/indeterminate or memory-only completion,
and explicit release frees the retained result. Hooks may submit/query; they
must not busy-wait for disk. Initial commits are held until READY so registration
and payload publication cannot deadlock each other.
## Independent binary oracles
The manually specified minimal executable in
`crates/console/prometeu-bytecode/tests/registry_v1.rs::minimal_bytes` is 193 bytes.
`crates/console/prometeu-bytecode/tests/registry_v1.rs::minimal_bytes` is 194 bytes.
Its directory begins at 32; eight entries end at 128:
| Kind | Offset | Length | Payload |
| --- | ---: | ---: | --- |
| constants | 128 | 4 | count 0 |
| functions | 132 | 29 | one unnamed zero-parameter/result function, offset 0, length 2, locals/max 0 |
| code | 161 | 2 | `01 00` (HALT) |
| exports | 163 | 4 | count 0 |
| syscalls | 167 | 4 | count 0 |
| types | 171 | 10 | one unnamed Bool import, BuiltinId `0x0013` |
| implementations | 181 | 4 | count 0 |
| captures | 185 | 8 | function count 1; empty environment |
| functions | 132 | 30 | role 0, one unnamed zero-parameter/result function, offset 0, length 2, locals/max 0 |
| code | 162 | 2 | `01 00` (HALT) |
| exports | 164 | 4 | count 0 |
| syscalls | 168 | 4 | count 0 |
| types | 172 | 10 | one unnamed Bool import, BuiltinId `0x0013` |
| implementations | 182 | 4 | count 0 |
| captures | 186 | 8 | function count 1; empty environment |
The independent multi-slot fixture in the same suite declares `fn(vec2)->vec2`
with TypeId 0, no serialized widths, no extra locals and max 2. Its fourteen code
@@ -92,7 +92,7 @@
]
},
"system/core": {
"baseline_percent": 87.71,
"baseline_percent": 80.0,
"paths": [
"crates/console/prometeu-system/src/"
],