Queue bounded GDK guest callbacks on shared ports

Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
Adeeb Rahman committed 2026-11-24 08:48:46 -05:00
1 parent 4b370efa12
commit b8fae106af
2 files changed
+81 -3

No files matched your search

+55 -3
View File
@@ -7,11 +7,12 @@ use super::{ERROR_INVALID_HANDLE, ERROR_INVALID_PARAMETER, ERROR_NOT_SUPPORTED};
use crate::imports::{ImportRegistry, ServiceStatus};
use crate::memory::GuestMemory;
use crate::runtime::HResult;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, VecDeque};
use std::sync::Arc;
const MODULE: &str = "xgameruntime.dll";
const MAX_HANDLES: usize = 64;
const MAX_CALLBACKS_PER_PORT: usize = 32;
const ERROR_OUTOFMEMORY: u32 = 14;
const MANUAL: u32 = 0;
@@ -19,6 +20,13 @@ const MANUAL: u32 = 0;
pub(super) struct QueueHandles {
last_id: u64,
queues: BTreeMap<u64, [u64; 2]>,
pending: BTreeMap<u64, VecDeque<GuestCallback>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct GuestCallback {
entry: u64,
context: u64,
}
impl QueueHandles {
@@ -50,6 +58,29 @@ impl QueueHandles {
);
Ok(())
}
fn close(&mut self, handle: u64) {
if let Some(ports) = self.queues.remove(&handle) {
for port in ports {
if !self.queues.values().any(|live| live.contains(&port)) {
self.pending.remove(&port);
}
}
}
}
fn submit(&mut self, handle: u64, selector: u32, callback: GuestCallback) -> Result<(), u32> {
let ports = self.queues.get(&handle).ok_or(ERROR_INVALID_HANDLE)?;
let port = ports
.get(selector as usize)
.ok_or(ERROR_INVALID_PARAMETER)?;
let queue = self.pending.entry(*port).or_default();
if queue.len() >= MAX_CALLBACKS_PER_PORT {
return Err(ERROR_OUTOFMEMORY);
}
queue.push_back(callback);
Ok(())
}
}
fn guest_hresult(result: Result<(), u32>) -> u64 {
@@ -98,8 +129,7 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
.gdk_queues
.lock()
.expect("GDK queue handle table")
.queues
.remove(&args.rcx);
.close(args.rcx);
Ok(0)
},
);
@@ -144,4 +174,26 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
Ok(guest_hresult(queues.open(Some(ports), args.r8, memory)))
},
);
let submit = Arc::clone(state);
registry.register_with_status(
MODULE,
"XTaskQueueSubmitCallback",
ServiceStatus::Partial,
move |args, _, memory| {
if args.r9 == 0 || memory.fetch_instruction_byte(args.r9).is_err() {
return Ok(guest_hresult(Err(ERROR_INVALID_PARAMETER)));
}
let callback = GuestCallback {
entry: args.r9,
context: args.r8,
};
let mut queues = submit.gdk_queues.lock().expect("GDK queue handle table");
Ok(guest_hresult(queues.submit(
args.rcx,
args.rdx as u32,
callback,
)))
},
);
}
+26
View File
@@ -1158,6 +1158,32 @@ fn guest_gdk_manual_queue_handles_have_independent_lifetimes() {
call("XTaskQueueCloseHandle", &[second]);
call("XTaskQueueCloseHandle", &[third]);
assert_eq!(open("XTaskQueueGetPort", &[composite, 1, output]), work);
assert_eq!(
call("XTaskQueueSubmitCallback", &[composite, 0, 17, output])
.execution
.final_state
.get(Register::Rax),
0x8007_0057
);
for index in 0..32 {
assert_eq!(
call(
"XTaskQueueSubmitCallback",
&[composite, index % 2, index, 0x140001000]
)
.execution
.final_state
.get(Register::Rax),
0
);
}
assert_eq!(
call("XTaskQueueSubmitCallback", &[composite, 1, 0, 0x140001000])
.execution
.final_state
.get(Register::Rax),
0x8007_000e
);
let stale = call("XTaskQueueCreateComposite", &[work, completion, output]);
assert_eq!(stale.execution.final_state.get(Register::Rax), 0x8007_0006);
call("XTaskQueueCloseHandle", &[composite]);