//! The program's GPU channel, through std's Pinnacle system calls: what //! Venus is carried over (`pinnacle_abi::gpu`). use std::os::pinnacle::abi::{CLOSED, ERROR, TIMEOUT, call, gpu}; use std::os::pinnacle::{RawHandle, syscall}; /// A context on the 3D GPU of the program's own. pub struct Gpu { channel: RawHandle, } /// Memory the host shares, mapped into the program. pub struct Shared { pub address: usize, pub size: usize, } impl Gpu { /// None without a 3D GPU (or for an app not granted one). pub fn open() -> Option { let (channel, _, _) = syscall(call::GPU, [0; 7]); (channel != ERROR).then_some(Gpu { channel }) } fn send(&self, message: &[u8]) -> bool { let args = [ self.channel, message.as_ptr() as u64, message.len() as u64, 0, 0, 0, 0, ]; syscall(call::SEND, args).0 != ERROR } } impl Drop for Gpu { fn drop(&mut self) { syscall(call::CLOSE, [self.channel, 0, 0, 0, 0, 0, 0]); } } impl pinnacle_venus::Host for Gpu { type Memory = Shared; fn submit(&mut self, stream: &[u8]) -> bool { let mut message = Vec::with_capacity(4 + stream.len()); message.extend_from_slice(&gpu::SUBMIT.to_le_bytes()); message.extend_from_slice(stream); self.send(&message) } fn blob(&mut self, blob_id: u64, size: u64) -> Option<(u32, Shared)> { let mut message = Vec::with_capacity(20); message.extend_from_slice(&gpu::BLOB.to_le_bytes()); message.extend_from_slice(&blob_id.to_le_bytes()); message.extend_from_slice(&size.to_le_bytes()); self.blob_message(gpu::BLOB, &message) } fn address(memory: &Shared) -> usize { memory.address } fn free(&mut self, memory: Shared) { syscall(call::UNMAP, [memory.address as u64, 0, 0, 0, 0, 0, 0]); } fn now_ms(&self) -> u64 { syscall(call::TIME, [0; 7]).0 } } impl Gpu { /// At least `size` bytes of the guest's own memory the host's GPU uses as /// it is (abi `GUEST_BLOB`), mapped here: its resource id and where it /// is. None where the host cannot share the guest's pages. pub(crate) fn guest_blob(&mut self, size: u64) -> Option<(u32, Shared)> { let mut message = Vec::with_capacity(12); message.extend_from_slice(&gpu::GUEST_BLOB.to_le_bytes()); message.extend_from_slice(&size.to_le_bytes()); self.blob_message(gpu::GUEST_BLOB, &message) } /// Sends a blob message of `kind` and maps the memory its answer holds. fn blob_message(&mut self, kind: u32, message: &[u8]) -> Option<(u32, Shared)> { if !self.send(message) { return None; } let mut reply = [0u8; 12]; let mut handles = [0u64; 1]; let channels = [self.channel]; let args = [ channels.as_ptr() as u64, 1, reply.as_mut_ptr() as u64, reply.len() as u64, handles.as_mut_ptr() as u64, 1, 0, ]; let (index, len, count) = syscall(call::RECEIVE, args); if index == TIMEOUT || index == ERROR || len == CLOSED || len != 12 || count != 1 { return None; } let word = |at: usize| u32::from_le_bytes(reply[at..at + 4].try_into().unwrap()); if word(0) != kind || word(4) != 0 { syscall(call::CLOSE, [handles[0], 0, 0, 0, 0, 0, 0]); return None; } let (address, size, _) = syscall(call::MAP, [handles[0], 0, 0, 0, 0, 0, 0]); // The mapping holds the memory; the handle is not needed after. syscall(call::CLOSE, [handles[0], 0, 0, 0, 0, 0, 0]); (address != ERROR).then_some(( word(8), Shared { address: address as usize, size: size as usize, }, )) } }