mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-27 00:50:43 +00:00
Schedule XAsync work and completion through manual queues
When an XAsyncBlock carries a non-null XTaskQueue handle, submit work to the work port and completion to the completion port so guests can XTaskQueueDispatch them. Null-queue immediate dispatch is unchanged. Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
450a5e4889
commit
2aa0cb0a5a
6 files changed
+392
-204
No files matched your search
+1
-1
@@ -63,7 +63,7 @@ remains unsupported.
|
||||
|
||||
- Added C-locale CRT services (`setlocale`, `localeconv`), C-locale `ctype.h` predicates and case mapping, byte and wide string mutators (`strcpy`/`strncpy`/`strcat`/`strncat`/`memchr`/`strstr`/`strspn`/`strcspn`, `wcscpy`/`wcsncpy`/`wcscat`/`wcschr`/`wcsrchr`), and a bounded `sprintf`/`snprintf` subset (`%%`, `%c`, `%s`, `%d`/`%i`, `%u`, `%x`/`%X`, `%p`, optional widths and `l`/`ll`/`z`/`t`). Non-C locales and unsupported conversions fail closed. See `docs/interfaces.md`.
|
||||
|
||||
- Added an immediate-dispatch XAsync subset (`XAsyncRun`, `XAsyncGetStatus`, `XAsyncCancel`, `XAsyncGetResultSize`) on `xgameruntime.dll`. Work and completion run on the calling thread before `XAsyncRun` returns; waiting GetStatus is unsupported. Builds on the existing manual `XTaskQueue*` services.
|
||||
- Added an XAsync subset (`XAsyncRun`, `XAsyncGetStatus`, `XAsyncCancel`, `XAsyncGetResultSize`) on `xgameruntime.dll`. With a null queue, work and completion run on the calling thread before `XAsyncRun` returns. With a manual `XTaskQueue` handle in the `XAsyncBlock`, work is submitted to the work port and completion to the completion port for `XTaskQueueDispatch`. Waiting GetStatus is unsupported.
|
||||
|
||||
- Added in-memory CRT FILE streams (`fopen`/`fclose`/`fread`/`fwrite`/`fseek`/`ftell`/`feof`/`ferror`/`rewind`, `__acrt_iob_func`) and a runtime environment block for `getenv` / `GetEnvironmentVariableA` / `GetEnvironmentVariableW`. Stream names are single path components with no host mapping; the host process environment is not exposed by default.
|
||||
|
||||
|
||||
@@ -202,6 +202,9 @@ pub(crate) struct GuestCallback {
|
||||
pub(crate) entry: u64,
|
||||
pub(crate) context: u64,
|
||||
pub(crate) cancelled: bool,
|
||||
/// Optional host continuation after the guest callback returns. Used by
|
||||
/// queued `XAsyncRun` so work can update status and schedule completion.
|
||||
pub(crate) task: Option<Arc<Mutex<Box<dyn CallbackTask>>>>,
|
||||
}
|
||||
|
||||
type CallbackDispatch = Box<
|
||||
@@ -744,7 +747,7 @@ impl ImportRegistry {
|
||||
CallbackFrame {
|
||||
resume: ret_addr,
|
||||
preserved,
|
||||
task: None,
|
||||
task: callback.task,
|
||||
after,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,190 +1,285 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Documented XAsync helpers on top of the manual GDK task-queue subset.
|
||||
//!
|
||||
//! This first cut runs `XAsyncRun` work and completion on the calling thread
|
||||
//! before the service returns (immediate dispatch). Status is still tracked so
|
||||
//! `XAsyncGetStatus` / `XAsyncCancel` behave for callers that poll after the
|
||||
//! run returns. Queue-port scheduling remains future work.
|
||||
|
||||
use super::ServicesState;
|
||||
use crate::cpu::ExitReason;
|
||||
use crate::imports::{
|
||||
CallbackStart, CallbackStep, CallbackTask, ImportRegistry, ServiceStatus, Win64Args,
|
||||
};
|
||||
use crate::memory::{GuestMemory, MemoryError};
|
||||
use crate::runtime::HResult;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
const MODULE: &str = "xgameruntime.dll";
|
||||
const ASYNC_BLOCK_SIZE: u64 = 56;
|
||||
const E_PENDING: u64 = 0x8000_000A;
|
||||
const E_ABORT: u64 = 0x8000_4004;
|
||||
const E_INVALIDARG: u64 = 0x8007_0057;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Status {
|
||||
Pending,
|
||||
Done(u32),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct AsyncOps {
|
||||
ops: BTreeMap<u64, Status>,
|
||||
}
|
||||
|
||||
fn fault(error: MemoryError) -> ExitReason {
|
||||
ExitReason::MemoryFault(error)
|
||||
}
|
||||
|
||||
fn guest_hresult(code: u32) -> u64 {
|
||||
HResult::from_win32(code).guest_return()
|
||||
}
|
||||
|
||||
struct AsyncRun {
|
||||
block: u64,
|
||||
completion: u64,
|
||||
ops: Arc<Mutex<AsyncOps>>,
|
||||
phase: Phase,
|
||||
}
|
||||
|
||||
enum Phase {
|
||||
Work,
|
||||
Complete,
|
||||
}
|
||||
|
||||
impl CallbackTask for AsyncRun {
|
||||
fn resume(
|
||||
&mut self,
|
||||
result: u64,
|
||||
_: u64,
|
||||
_: &mut GuestMemory,
|
||||
) -> Result<CallbackStep, ExitReason> {
|
||||
match self.phase {
|
||||
Phase::Work => {
|
||||
let status = result as u32;
|
||||
self.ops
|
||||
.lock()
|
||||
.expect("async ops")
|
||||
.ops
|
||||
.insert(self.block, Status::Done(status));
|
||||
if self.completion != 0 {
|
||||
self.phase = Phase::Complete;
|
||||
return Ok(CallbackStep::Call {
|
||||
entry: self.completion,
|
||||
args: [self.block, 0, 0, 0],
|
||||
});
|
||||
}
|
||||
Ok(CallbackStep::Return(HResult::OK.guest_return()))
|
||||
}
|
||||
Phase::Complete => Ok(CallbackStep::Return(HResult::OK.guest_return())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
|
||||
let run = Arc::clone(state);
|
||||
registry.register_callback_task(
|
||||
MODULE,
|
||||
"XAsyncRun",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory, _| start_run(&run, args, memory),
|
||||
);
|
||||
|
||||
let status = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XAsyncGetStatus",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
let block = args.rcx;
|
||||
if block == 0 || memory.check_write(block, ASYNC_BLOCK_SIZE as usize).is_err() {
|
||||
return Ok(E_INVALIDARG);
|
||||
}
|
||||
if args.rdx != 0 {
|
||||
// Waiting would block the guest; the sync subset never leaves
|
||||
// work pending after XAsyncRun returns.
|
||||
return Ok(guest_hresult(50)); // ERROR_NOT_SUPPORTED as HRESULT
|
||||
}
|
||||
let ops = status.gdk_async.lock().expect("async ops");
|
||||
Ok(match ops.ops.get(&block) {
|
||||
Some(Status::Pending) => E_PENDING,
|
||||
Some(Status::Done(code)) => u64::from(*code),
|
||||
None => E_INVALIDARG,
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
let cancel = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XAsyncCancel",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
let block = args.rcx;
|
||||
if block == 0 || memory.check_write(block, ASYNC_BLOCK_SIZE as usize).is_err() {
|
||||
return Ok(0);
|
||||
}
|
||||
let mut ops = cancel.gdk_async.lock().expect("async ops");
|
||||
if let Some(Status::Pending) = ops.ops.get(&block) {
|
||||
ops.ops.insert(block, Status::Done(E_ABORT as u32));
|
||||
}
|
||||
Ok(0)
|
||||
},
|
||||
);
|
||||
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XAsyncGetResultSize",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
if args.rcx == 0
|
||||
|| args.rdx == 0
|
||||
|| memory.check_write(args.rcx, ASYNC_BLOCK_SIZE as usize).is_err()
|
||||
{
|
||||
return Ok(E_INVALIDARG);
|
||||
}
|
||||
memory.write_u64(args.rdx, 0).map_err(fault)?;
|
||||
Ok(HResult::OK.guest_return())
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn start_run(
|
||||
state: &Arc<ServicesState>,
|
||||
args: &Win64Args,
|
||||
memory: &mut GuestMemory,
|
||||
) -> Result<CallbackStart, ExitReason> {
|
||||
let block = args.rcx;
|
||||
let work = args.rdx;
|
||||
if block == 0 || work == 0 || memory.check_write(block, ASYNC_BLOCK_SIZE as usize).is_err() {
|
||||
return Ok(CallbackStart::Return(E_INVALIDARG));
|
||||
}
|
||||
memory
|
||||
.fetch_instruction_byte(work)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
let completion = memory.read_u64(block + 16).map_err(fault)?;
|
||||
if completion != 0 {
|
||||
memory
|
||||
.fetch_instruction_byte(completion)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
}
|
||||
{
|
||||
let mut ops = state.gdk_async.lock().expect("async ops");
|
||||
if matches!(ops.ops.get(&block), Some(Status::Pending)) {
|
||||
return Ok(CallbackStart::Return(E_INVALIDARG));
|
||||
}
|
||||
ops.ops.insert(block, Status::Pending);
|
||||
}
|
||||
Ok(CallbackStart::Call {
|
||||
task: Box::new(AsyncRun {
|
||||
block,
|
||||
completion,
|
||||
ops: Arc::clone(&state.gdk_async),
|
||||
phase: Phase::Work,
|
||||
}),
|
||||
entry: work,
|
||||
args: [block, 0, 0, 0],
|
||||
})
|
||||
}
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Documented XAsync helpers on top of the manual GDK task-queue subset.
|
||||
//!
|
||||
//! With a null queue in the `XAsyncBlock`, `XAsyncRun` runs work and optional
|
||||
//! completion on the calling thread before it returns (immediate dispatch).
|
||||
//! With a non-null manual `XTaskQueue` handle, work is submitted to the work
|
||||
//! port and completion to the completion port; the caller must
|
||||
//! `XTaskQueueDispatch` those ports. `XAsyncGetStatus` with `wait=true` is
|
||||
//! unsupported either way.
|
||||
|
||||
use super::ServicesState;
|
||||
use crate::cpu::ExitReason;
|
||||
use crate::imports::{
|
||||
CallbackStart, CallbackStep, CallbackTask, GuestCallback, ImportRegistry, ServiceStatus,
|
||||
Win64Args,
|
||||
};
|
||||
use crate::memory::{GuestMemory, MemoryError};
|
||||
use crate::runtime::HResult;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
|
||||
const MODULE: &str = "xgameruntime.dll";
|
||||
const ASYNC_BLOCK_SIZE: u64 = 56;
|
||||
const E_PENDING: u64 = 0x8000_000A;
|
||||
const E_ABORT: u64 = 0x8000_4004;
|
||||
const E_INVALIDARG: u64 = 0x8007_0057;
|
||||
const WORK_PORT: u32 = 0;
|
||||
const COMPLETION_PORT: u32 = 1;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Status {
|
||||
Pending,
|
||||
Done(u32),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct AsyncOps {
|
||||
ops: BTreeMap<u64, Status>,
|
||||
}
|
||||
|
||||
fn fault(error: MemoryError) -> ExitReason {
|
||||
ExitReason::MemoryFault(error)
|
||||
}
|
||||
|
||||
fn guest_hresult(code: u32) -> u64 {
|
||||
HResult::from_win32(code).guest_return()
|
||||
}
|
||||
|
||||
struct AsyncRun {
|
||||
block: u64,
|
||||
completion: u64,
|
||||
/// Non-zero when work was queued; completion is submitted to this handle.
|
||||
queue: u64,
|
||||
ops: Arc<Mutex<AsyncOps>>,
|
||||
state: Arc<ServicesState>,
|
||||
phase: Phase,
|
||||
}
|
||||
|
||||
enum Phase {
|
||||
Work,
|
||||
Complete,
|
||||
}
|
||||
|
||||
impl CallbackTask for AsyncRun {
|
||||
fn resume(
|
||||
&mut self,
|
||||
result: u64,
|
||||
_: u64,
|
||||
_: &mut GuestMemory,
|
||||
) -> Result<CallbackStep, ExitReason> {
|
||||
match self.phase {
|
||||
Phase::Work => {
|
||||
let status = result as u32;
|
||||
self.ops
|
||||
.lock()
|
||||
.expect("async ops")
|
||||
.ops
|
||||
.insert(self.block, Status::Done(status));
|
||||
if self.completion == 0 {
|
||||
// Immediate path returns S_OK to XAsyncRun; queued path
|
||||
// returns TRUE to XTaskQueueDispatch.
|
||||
let value = if self.queue == 0 {
|
||||
HResult::OK.guest_return()
|
||||
} else {
|
||||
1
|
||||
};
|
||||
return Ok(CallbackStep::Return(value));
|
||||
}
|
||||
if self.queue == 0 {
|
||||
self.phase = Phase::Complete;
|
||||
return Ok(CallbackStep::Call {
|
||||
entry: self.completion,
|
||||
args: [self.block, 0, 0, 0],
|
||||
});
|
||||
}
|
||||
// Queue completion onto the completion port; the guest must
|
||||
// dispatch that port separately.
|
||||
let mut queues = self
|
||||
.state
|
||||
.gdk_queues
|
||||
.lock()
|
||||
.expect("GDK queue handle table");
|
||||
let callback = super::gdk_queue::scheduled(
|
||||
Instant::now(),
|
||||
GuestCallback {
|
||||
entry: self.completion,
|
||||
context: self.block,
|
||||
cancelled: false,
|
||||
task: None,
|
||||
},
|
||||
);
|
||||
if queues
|
||||
.submit(self.queue, COMPLETION_PORT, callback)
|
||||
.is_err()
|
||||
{
|
||||
return Ok(CallbackStep::Return(0));
|
||||
}
|
||||
self.state.gdk_queue_changed.notify_all();
|
||||
Ok(CallbackStep::Return(1))
|
||||
}
|
||||
Phase::Complete => Ok(CallbackStep::Return(HResult::OK.guest_return())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
|
||||
let run = Arc::clone(state);
|
||||
registry.register_callback_task(
|
||||
MODULE,
|
||||
"XAsyncRun",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory, _| start_run(&run, args, memory),
|
||||
);
|
||||
|
||||
let status = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XAsyncGetStatus",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
let block = args.rcx;
|
||||
if block == 0
|
||||
|| memory
|
||||
.check_write(block, ASYNC_BLOCK_SIZE as usize)
|
||||
.is_err()
|
||||
{
|
||||
return Ok(E_INVALIDARG);
|
||||
}
|
||||
if args.rdx != 0 {
|
||||
return Ok(guest_hresult(50)); // ERROR_NOT_SUPPORTED as HRESULT
|
||||
}
|
||||
let ops = status.gdk_async.lock().expect("async ops");
|
||||
Ok(match ops.ops.get(&block) {
|
||||
Some(Status::Pending) => E_PENDING,
|
||||
Some(Status::Done(code)) => u64::from(*code),
|
||||
None => E_INVALIDARG,
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
let cancel = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XAsyncCancel",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
let block = args.rcx;
|
||||
if block == 0
|
||||
|| memory
|
||||
.check_write(block, ASYNC_BLOCK_SIZE as usize)
|
||||
.is_err()
|
||||
{
|
||||
return Ok(0);
|
||||
}
|
||||
let mut ops = cancel.gdk_async.lock().expect("async ops");
|
||||
if let Some(Status::Pending) = ops.ops.get(&block) {
|
||||
ops.ops.insert(block, Status::Done(E_ABORT as u32));
|
||||
}
|
||||
Ok(0)
|
||||
},
|
||||
);
|
||||
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XAsyncGetResultSize",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
if args.rcx == 0
|
||||
|| args.rdx == 0
|
||||
|| memory
|
||||
.check_write(args.rcx, ASYNC_BLOCK_SIZE as usize)
|
||||
.is_err()
|
||||
{
|
||||
return Ok(E_INVALIDARG);
|
||||
}
|
||||
memory.write_u64(args.rdx, 0).map_err(fault)?;
|
||||
Ok(HResult::OK.guest_return())
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn start_run(
|
||||
state: &Arc<ServicesState>,
|
||||
args: &Win64Args,
|
||||
memory: &mut GuestMemory,
|
||||
) -> Result<CallbackStart, ExitReason> {
|
||||
let block = args.rcx;
|
||||
let work = args.rdx;
|
||||
if block == 0
|
||||
|| work == 0
|
||||
|| memory
|
||||
.check_write(block, ASYNC_BLOCK_SIZE as usize)
|
||||
.is_err()
|
||||
{
|
||||
return Ok(CallbackStart::Return(E_INVALIDARG));
|
||||
}
|
||||
memory
|
||||
.fetch_instruction_byte(work)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
let queue = memory.read_u64(block).map_err(fault)?;
|
||||
let completion = memory.read_u64(block + 16).map_err(fault)?;
|
||||
if completion != 0 {
|
||||
memory
|
||||
.fetch_instruction_byte(completion)
|
||||
.map_err(ExitReason::MemoryFault)?;
|
||||
}
|
||||
{
|
||||
let mut ops = state.gdk_async.lock().expect("async ops");
|
||||
if matches!(ops.ops.get(&block), Some(Status::Pending)) {
|
||||
return Ok(CallbackStart::Return(E_INVALIDARG));
|
||||
}
|
||||
ops.ops.insert(block, Status::Pending);
|
||||
}
|
||||
|
||||
let task = AsyncRun {
|
||||
block,
|
||||
completion,
|
||||
queue,
|
||||
ops: Arc::clone(&state.gdk_async),
|
||||
state: Arc::clone(state),
|
||||
phase: Phase::Work,
|
||||
};
|
||||
|
||||
if queue == 0 {
|
||||
return Ok(CallbackStart::Call {
|
||||
task: Box::new(task),
|
||||
entry: work,
|
||||
args: [block, 0, 0, 0],
|
||||
});
|
||||
}
|
||||
|
||||
// Queue path: schedule work on the work port and return immediately.
|
||||
let mut queues = state.gdk_queues.lock().expect("GDK queue handle table");
|
||||
if !queues.has_handle(queue) {
|
||||
state
|
||||
.gdk_async
|
||||
.lock()
|
||||
.expect("async ops")
|
||||
.ops
|
||||
.remove(&block);
|
||||
return Ok(CallbackStart::Return(E_INVALIDARG));
|
||||
}
|
||||
let callback = super::gdk_queue::scheduled(
|
||||
Instant::now(),
|
||||
GuestCallback {
|
||||
entry: work,
|
||||
context: block,
|
||||
cancelled: false,
|
||||
task: Some(Arc::new(Mutex::new(Box::new(task)))),
|
||||
},
|
||||
);
|
||||
if let Err(error) = queues.submit(queue, WORK_PORT, callback) {
|
||||
state
|
||||
.gdk_async
|
||||
.lock()
|
||||
.expect("async ops")
|
||||
.ops
|
||||
.remove(&block);
|
||||
return Ok(CallbackStart::Return(guest_hresult(error)));
|
||||
}
|
||||
state.gdk_queue_changed.notify_all();
|
||||
Ok(CallbackStart::Return(HResult::OK.guest_return()))
|
||||
}
|
||||
@@ -34,13 +34,26 @@ struct Queue {
|
||||
ports: [u64; 2],
|
||||
}
|
||||
|
||||
struct ScheduledCallback {
|
||||
pub(super) struct ScheduledCallback {
|
||||
owner: u64,
|
||||
ready_at: Instant,
|
||||
callback: GuestCallback,
|
||||
}
|
||||
|
||||
/// Build a delayed/immediate scheduled callback for queue submission.
|
||||
pub(super) fn scheduled(ready_at: Instant, callback: GuestCallback) -> ScheduledCallback {
|
||||
ScheduledCallback {
|
||||
owner: 0,
|
||||
ready_at,
|
||||
callback,
|
||||
}
|
||||
}
|
||||
|
||||
impl QueueHandles {
|
||||
pub(super) fn has_handle(&self, handle: u64) -> bool {
|
||||
self.queues.contains_key(&handle)
|
||||
}
|
||||
|
||||
fn open(
|
||||
&mut self,
|
||||
duplicate: Option<Queue>,
|
||||
@@ -88,7 +101,7 @@ impl QueueHandles {
|
||||
}
|
||||
}
|
||||
|
||||
fn submit(
|
||||
pub(super) fn submit(
|
||||
&mut self,
|
||||
handle: u64,
|
||||
selector: u32,
|
||||
@@ -243,15 +256,15 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
{
|
||||
return Ok(E_ABORT);
|
||||
}
|
||||
let callback = ScheduledCallback {
|
||||
owner: 0,
|
||||
ready_at: Instant::now() + Duration::from_millis(u64::from(delay)),
|
||||
callback: GuestCallback {
|
||||
let callback = scheduled(
|
||||
Instant::now() + Duration::from_millis(u64::from(delay)),
|
||||
GuestCallback {
|
||||
entry,
|
||||
context,
|
||||
cancelled: false,
|
||||
task: None,
|
||||
},
|
||||
};
|
||||
);
|
||||
let result = queues.submit(args.rcx, args.rdx as u32, callback);
|
||||
if result.is_ok() {
|
||||
submit.gdk_queue_changed.notify_all();
|
||||
|
||||
@@ -2676,3 +2676,79 @@ fn guest_xasync_run_completes_on_the_calling_thread() {
|
||||
assert_eq!(result.execution.final_state.get(Register::Rax), 0x2222_0001);
|
||||
assert_eq!(result.image.memory.read_u8(block + 0x18).unwrap(), 0xa5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guest_xasync_run_schedules_work_and_completion_on_a_manual_queue() {
|
||||
const IMAGE: u64 = 0x140000000;
|
||||
const MODULE: &str = "xgameruntime.dll";
|
||||
let state = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));
|
||||
let call = |symbol, args: &[u64]| call_service_fixture(&state, MODULE, symbol, args);
|
||||
let output = PROCESS_FIXTURE_STACK_TOP - 0x480;
|
||||
let block = PROCESS_FIXTURE_STACK_TOP - 0x400;
|
||||
let created = call("XTaskQueueCreate", &[0, 0, output]);
|
||||
let queue = created.image.memory.read_u64(output).unwrap();
|
||||
|
||||
let mut code = vec![0x48, 0x83, 0xec, 0x48];
|
||||
// XAsyncBlock: queue at +0, completion at +0x10, rest zeroed.
|
||||
code.extend_from_slice(&[0x48, 0xb8]);
|
||||
code.extend_from_slice(&block.to_le_bytes());
|
||||
code.extend_from_slice(&[0x48, 0x31, 0xc9, 0x48, 0x89, 0x08, 0x48, 0x89, 0x48, 0x08]);
|
||||
code.extend_from_slice(&[0x48, 0x89, 0x48, 0x10, 0x48, 0x89, 0x48, 0x18]);
|
||||
code.extend_from_slice(&[
|
||||
0x48, 0x89, 0x48, 0x20, 0x48, 0x89, 0x48, 0x28, 0x48, 0x89, 0x48, 0x30,
|
||||
]);
|
||||
code.extend_from_slice(&[0x48, 0xb9]);
|
||||
code.extend_from_slice(&queue.to_le_bytes());
|
||||
code.extend_from_slice(&[0x48, 0x89, 0x08]); // block.queue = queue
|
||||
code.extend_from_slice(&[0x48, 0xb9]);
|
||||
code.extend_from_slice(&(IMAGE + 0x1110).to_le_bytes());
|
||||
code.extend_from_slice(&[0x48, 0x89, 0x48, 0x10]); // block.callback
|
||||
// XAsyncRun(block, work) — returns before work runs.
|
||||
code.extend_from_slice(&[0x48, 0x89, 0xc1]);
|
||||
code.extend_from_slice(&[0x48, 0xba]);
|
||||
code.extend_from_slice(&(IMAGE + 0x1100).to_le_bytes());
|
||||
append_import_call(&mut code, 0x2060);
|
||||
code.extend_from_slice(&[0x48, 0x89, 0xc3]); // rbx = XAsyncRun result
|
||||
// Dispatch work port, then completion port.
|
||||
code.extend_from_slice(&[0x48, 0xb9]);
|
||||
code.extend_from_slice(&queue.to_le_bytes());
|
||||
code.extend_from_slice(&[0x31, 0xd2, 0x45, 0x31, 0xc0]); // port 0, timeout 0
|
||||
append_import_call(&mut code, 0x21a0); // XTaskQueueDispatch
|
||||
code.extend_from_slice(&[0x48, 0xb9]);
|
||||
code.extend_from_slice(&queue.to_le_bytes());
|
||||
code.extend_from_slice(&[0xba, 0x01, 0x00, 0x00, 0x00, 0x45, 0x31, 0xc0]); // port 1
|
||||
append_import_call(&mut code, 0x21a0);
|
||||
code.extend_from_slice(&[0x48, 0x83, 0xc4, 0x48, 0xf4]);
|
||||
code.resize(0x100, 0);
|
||||
code.extend_from_slice(&[0xb8, 0x01, 0x00, 0x22, 0x22, 0xc3]); // work
|
||||
code.resize(0x110, 0);
|
||||
code.extend_from_slice(&[0xc6, 0x41, 0x18, 0xa5, 0xc3]); // completion
|
||||
let image = TestPeBuilder::new(IMAGE, 0x1000)
|
||||
.add_import(MODULE, "XAsyncRun")
|
||||
.add_import(MODULE, "XTaskQueueDispatch")
|
||||
.code(&code)
|
||||
.build();
|
||||
let mut registry = ImportRegistry::new();
|
||||
register_standard_services(&mut registry, Arc::clone(&state));
|
||||
let result = run_synthetic_startup(
|
||||
&image,
|
||||
StartupLayout {
|
||||
image_base: IMAGE,
|
||||
stack_top: PROCESS_FIXTURE_STACK_TOP,
|
||||
tls_base: 0x300000000,
|
||||
},
|
||||
registry,
|
||||
400,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result.execution.exit_reason, ExitReason::Halt);
|
||||
assert_eq!(result.execution.final_state.get(Register::Rbx), 0); // XAsyncRun S_OK
|
||||
assert_eq!(result.image.memory.read_u8(block + 0x18).unwrap(), 0xa5);
|
||||
assert_eq!(
|
||||
call("XAsyncGetStatus", &[block, 0])
|
||||
.execution
|
||||
.final_state
|
||||
.get(Register::Rax),
|
||||
0x2222_0001
|
||||
);
|
||||
}
|
||||
+6
-5
@@ -262,11 +262,12 @@ process environment is not visible unless an entry is installed here.
|
||||
### XAsync helpers
|
||||
|
||||
`XAsyncRun`, `XAsyncGetStatus`, `XAsyncCancel` and `XAsyncGetResultSize` are
|
||||
registered on `xgameruntime.dll`. The current subset runs the work callback
|
||||
and optional completion callback on the calling thread before `XAsyncRun`
|
||||
returns (immediate dispatch). `XAsyncGetStatus` with `wait=true` returns
|
||||
`ERROR_NOT_SUPPORTED`. Queue-port scheduling of XAsync work remains future
|
||||
work; see the existing manual `XTaskQueue*` subset for port dispatch.
|
||||
registered on `xgameruntime.dll`. With a null queue in the `XAsyncBlock`, the
|
||||
work callback and optional completion callback run on the calling thread
|
||||
before `XAsyncRun` returns (immediate dispatch). With a non-null manual
|
||||
`XTaskQueue` handle, work is submitted to the work port and completion to the
|
||||
completion port; the guest must `XTaskQueueDispatch` those ports.
|
||||
`XAsyncGetStatus` with `wait=true` returns `ERROR_NOT_SUPPORTED`.
|
||||
|
||||
### Built-in guest modules
|
||||
|
||||
|
||||
Reference in new issue
Block a user