Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

actual device mapping

Layle73b2d766a078
diff --git a/crates/gecko/src/hollywood/ipc.rs b/crates/gecko/src/hollywood/ipc.rs
index 7dff10d..1dc3211 100644
--- a/crates/gecko/src/hollywood/ipc.rs
+++ b/crates/gecko/src/hollywood/ipc.rs
@@ -1,38 +1,57 @@
pub mod stm;
-use crate::Wii;
use crate::hollywood::regs::{ArmCtrl, ArmMsg, PpcCtrl, PpcMsg};
-use crate::system::{System, SystemId};
+use crate::mmio::Mmio;
+use crate::scheduler::Scheduler;
+use crate::system::{System, SystemId, WII};
pub const IPC_EINVAL: i32 = -4;
pub const IPC_ENOENT: i32 = -6;
+pub struct DeviceContext<'a> {
+ pub mmio: &'a mut Mmio<{ WII }>,
+ pub scheduler: &'a mut Scheduler<{ WII }>,
+}
+
pub trait IosDevice: Send {
- fn open(&mut self, _wii: &mut Wii, _mode: u32) -> i32 {
+ fn open(&mut self, _ctx: &mut DeviceContext<'_>, _mode: u32) -> i32 {
+ tracing::warn!("IOS_Open: unimplemented");
0
}
- fn close(&mut self, _wii: &mut Wii) -> i32 {
+ fn close(&mut self, _ctx: &mut DeviceContext<'_>) -> i32 {
+ tracing::warn!("IOS_Close: unimplemented");
0
}
- fn read(&mut self, _wii: &mut Wii, _buf: u32, _len: u32) -> i32 {
+ fn read(&mut self, _ctx: &mut DeviceContext<'_>, _buf: u32, _len: u32) -> i32 {
+ tracing::warn!("IOS_Read: unimplemented");
IPC_EINVAL
}
- fn write(&mut self, _wii: &mut Wii, _buf: u32, _len: u32) -> i32 {
+ fn write(&mut self, _ctx: &mut DeviceContext<'_>, _buf: u32, _len: u32) -> i32 {
+ tracing::warn!("IOS_Write: unimplemented");
IPC_EINVAL
}
- fn seek(&mut self, _wii: &mut Wii, _where_: i32, _whence: i32) -> i32 {
+ fn seek(&mut self, _ctx: &mut DeviceContext<'_>, _where_: i32, _whence: i32) -> i32 {
+ tracing::warn!("IOS_Seek: unimplemented");
0
}
- fn ioctl(&mut self, _wii: &mut Wii, _cmd: u32, _in_buf: u32, _in_len: u32, _out_buf: u32, _out_len: u32) -> i32 {
+ fn ioctl(
+ &mut self,
+ _ctx: &mut DeviceContext<'_>,
+ _cmd: u32,
+ _in_buf: u32,
+ _in_len: u32,
+ _out_buf: u32,
+ _out_len: u32,
+ ) -> i32 {
IPC_EINVAL
}
- fn ioctlv(&mut self, _wii: &mut Wii, _cmd: u32, _argcin: u32, _argcio: u32, _vec: u32) -> i32 {
+ fn ioctlv(&mut self, _ctx: &mut DeviceContext<'_>, _cmd: u32, _argcin: u32, _argcio: u32, _vec: u32) -> i32 {
IPC_EINVAL
}
}
@@ -66,11 +85,7 @@ crate::mmio_device_dispatch! {
],
}
-pub fn deliver_response<const SYSTEM: SystemId>(
- sys: &mut System<SYSTEM>,
- cmd_paddr: u32,
- result: i32,
-) {
+pub fn deliver_response<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32, result: i32) {
sys.mmio.phys_write_u32(cmd_paddr + 0x04, result as u32);
sys.hollywood.ipc.armmsg = ArmMsg::from_raw(cmd_paddr);
sys.hollywood.ipc.ppcctrl = sys.hollywood.ipc.ppcctrl.with_ack_reply(true);
diff --git a/crates/gecko/src/hollywood/ipc/stm.rs b/crates/gecko/src/hollywood/ipc/stm.rs
index d0a7e79..7043216 100644
--- a/crates/gecko/src/hollywood/ipc/stm.rs
+++ b/crates/gecko/src/hollywood/ipc/stm.rs
@@ -1,3 +1,46 @@
pub struct Stm;
-impl super::IosDevice for Stm {}
+impl super::IosDevice for Stm {
+ fn open(&mut self, _ctx: &mut super::DeviceContext<'_>, mode: u32) -> i32 {
+ tracing::error!(mode, "IOS_Open: unimplemented");
+ 0
+ }
+
+ fn close(&mut self, _ctx: &mut super::DeviceContext<'_>) -> i32 {
+ tracing::error!("IOS_Close: unimplemented");
+ 0
+ }
+
+ fn read(&mut self, _ctx: &mut super::DeviceContext<'_>, buf: u32, len: u32) -> i32 {
+ tracing::error!(buf, len, "IOS_Read: unimplemented");
+ super::IPC_EINVAL
+ }
+
+ fn write(&mut self, _ctx: &mut super::DeviceContext<'_>, buf: u32, len: u32) -> i32 {
+ tracing::error!(buf, len, "IOS_Write: unimplemented");
+ super::IPC_EINVAL
+ }
+
+ fn seek(&mut self, _ctx: &mut super::DeviceContext<'_>, where_: i32, whence: i32) -> i32 {
+ tracing::error!(where_, whence, "IOS_Seek: unimplemented");
+ 0
+ }
+
+ fn ioctl(
+ &mut self,
+ _ctx: &mut super::DeviceContext<'_>,
+ cmd: u32,
+ in_buf: u32,
+ in_len: u32,
+ out_buf: u32,
+ out_len: u32,
+ ) -> i32 {
+ tracing::error!(cmd, in_buf, in_len, out_buf, out_len, "IOS_Ioctl: unimplemented");
+ super::IPC_EINVAL
+ }
+
+ fn ioctlv(&mut self, _ctx: &mut super::DeviceContext<'_>, cmd: u32, argcin: u32, argcio: u32, vec: u32) -> i32 {
+ tracing::error!(cmd, argcin, argcio, vec, "IOS_Ioctlv: unimplemented");
+ super::IPC_EINVAL
+ }
+}
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index cd91331..f6d396a 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -1,5 +1,8 @@
use crate::system::SystemId;
-use crate::{System, hollywood::ipc::IosDevice};
+use crate::{
+ System,
+ hollywood::ipc::{DeviceContext, IosDevice},
+};
use std::collections::{HashMap, VecDeque};
// As per zayd
@@ -61,6 +64,16 @@ impl System<{ crate::WII }> {
self.starlet
.register("/dev/stm/immediate", Box::new(crate::hollywood::ipc::stm::Stm));
}
+
+ pub fn create_device_context(&mut self) -> (&mut Starlet, DeviceContext<'_>) {
+ (
+ &mut self.starlet,
+ DeviceContext {
+ mmio: &mut self.mmio,
+ scheduler: &mut self.scheduler,
+ },
+ )
+ }
}
pub fn dispatch_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) {
@@ -77,14 +90,130 @@ pub fn schedule_drain<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
}
fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) -> i32 {
- let cmd = sys.mmio.phys_read_u32(cmd_paddr);
- tracing::warn!(
- cmd,
- paddr = format!("{cmd_paddr:#010X}"),
- "Starlet command decoder not implemented; returning IPC_ENOENT"
- );
-
- crate::hollywood::ipc::IPC_ENOENT
+ use crate::hollywood::ipc::{IPC_EINVAL, IPC_ENOENT};
+
+ const IOS_OPEN: u32 = 1;
+ const IOS_CLOSE: u32 = 2;
+ const IOS_READ: u32 = 3;
+ const IOS_WRITE: u32 = 4;
+ const IOS_SEEK: u32 = 5;
+ const IOS_IOCTL: u32 = 6;
+ const IOS_IOCTLV: u32 = 7;
+
+ debug_assert!(SYSTEM == crate::WII, "Starlet dispatch reached on non-Wii system");
+
+ let wii: &mut crate::Wii = unsafe { ::core::mem::transmute(sys) };
+ let cmd = wii.mmio.phys_read_u32(cmd_paddr);
+ let fd = wii.mmio.phys_read_u32(cmd_paddr + 0x08) as i32;
+
+ let (starlet, mut ctx) = wii.create_device_context();
+
+ match cmd {
+ IOS_OPEN => {
+ let path_ptr = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);
+ let mode = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);
+ let path = self::read_c_string(&mut ctx, path_ptr);
+ tracing::info!(%path, mode, "IOS_Open");
+
+ let Some(dev) = starlet.device_for_path(&path) else {
+ tracing::error!(%path, "IOS_Open: no device registered");
+ return IPC_ENOENT;
+ };
+ let rc = dev.open(&mut ctx, mode);
+ if rc >= 0 { starlet.allocate_fd(&path) } else { rc }
+ }
+ IOS_CLOSE => {
+ tracing::info!(fd, "IOS_Close");
+
+ let Some(path) = starlet.release_fd(fd) else {
+ return IPC_EINVAL;
+ };
+ let Some(dev) = starlet.device_for_path(&path) else {
+ return IPC_EINVAL;
+ };
+
+ dev.close(&mut ctx)
+ }
+ IOS_READ => {
+ let buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);
+ let len = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);
+
+ tracing::info!(fd, buf = format!("{buf:#010X}"), len, "IOS_Read");
+
+ match starlet.device_for_fd(fd) {
+ Some(dev) => dev.read(&mut ctx, buf, len),
+ None => IPC_EINVAL,
+ }
+ }
+ IOS_WRITE => {
+ let buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);
+ let len = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);
+
+ tracing::info!(fd, buf = format!("{buf:#010X}"), len, "IOS_Write");
+
+ match starlet.device_for_fd(fd) {
+ Some(dev) => dev.write(&mut ctx, buf, len),
+ None => IPC_EINVAL,
+ }
+ }
+ IOS_SEEK => {
+ let where_ = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C) as i32;
+ let whence = ctx.mmio.phys_read_u32(cmd_paddr + 0x10) as i32;
+
+ tracing::info!(fd, where_, whence, "IOS_Seek");
+
+ match starlet.device_for_fd(fd) {
+ Some(dev) => dev.seek(&mut ctx, where_, whence),
+ None => IPC_EINVAL,
+ }
+ }
+ IOS_IOCTL => {
+ let ioctl_cmd = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);
+ let in_buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);
+ let in_len = ctx.mmio.phys_read_u32(cmd_paddr + 0x14);
+ let out_buf = ctx.mmio.phys_read_u32(cmd_paddr + 0x18);
+ let out_len = ctx.mmio.phys_read_u32(cmd_paddr + 0x1C);
+
+ tracing::info!(fd, ioctl_cmd, "IOS_Ioctl");
+
+ match starlet.device_for_fd(fd) {
+ Some(dev) => dev.ioctl(&mut ctx, ioctl_cmd, in_buf, in_len, out_buf, out_len),
+ None => IPC_EINVAL,
+ }
+ }
+ IOS_IOCTLV => {
+ let ioctl_cmd = ctx.mmio.phys_read_u32(cmd_paddr + 0x0C);
+ let argcin = ctx.mmio.phys_read_u32(cmd_paddr + 0x10);
+ let argcio = ctx.mmio.phys_read_u32(cmd_paddr + 0x14);
+ let vec = ctx.mmio.phys_read_u32(cmd_paddr + 0x18);
+
+ tracing::info!(fd, ioctl_cmd, "IOS_Ioctlv");
+
+ match starlet.device_for_fd(fd) {
+ Some(dev) => dev.ioctlv(&mut ctx, ioctl_cmd, argcin, argcio, vec),
+ None => IPC_EINVAL,
+ }
+ }
+ other => {
+ tracing::error!(cmd = other, "unimplemented IOS command");
+ IPC_EINVAL
+ }
+ }
+}
+
+fn read_c_string(ctx: &mut DeviceContext<'_>, paddr: u32) -> String {
+ let mut bytes = Vec::with_capacity(64);
+
+ for i in 0..64 {
+ let b = ctx.mmio.phys_read_u8(paddr + i);
+ if b == 0 {
+ break;
+ }
+
+ bytes.push(b);
+ }
+
+ String::from_utf8_lossy(&bytes).into_owned()
}
fn deliver_pending<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {