Compare commits

...
Author SHA1 Message Date
ates d99e35ad42 acphal: IAcpHal slot order and shape contexts
IAcpHal is not COM (authorized GDK documentation): its slots are
Connect, Disconnect, SubmitCommand, PopMessage, GetNumMessages and
Release. AcpHalAllocateShapeContexts and AcpHalReleaseShapeContexts
allocate the documented SHAPE_CONTEXTS arrays from the APU heap, and a
second allocation fails with ACP_E_ALREADY_ALLOCATED; the default APU
heap is 64 MB. A project-authorized target build needs both to start.

test_audio_input checks the slots and the allocation lifecycle;
test_integration_boot releases IAcpHal through its sixth slot instead
of IUnknown::Release.

Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-11-29 05:25:31 +03:00
4 changed files with 162 additions and 36 deletions

No files matched your search

+5
View File
@@ -14,6 +14,11 @@ remains unsupported.
- `dxbc-accc` no longer classifies `DXIL` chunks whose bitcode payload starts with the `NOBC` header-only placeholder as executable DXIL (Track D prerequisite from ADR 0008 / `docs/roadmap.md`). `inspect_dxbc` and `bx_shader_inspect` still report the container's `dxil_chunk_size`, stage, and shader model, but leave `kind` as `Unknown` (or `AcccOnly` when paired with an `ACCC` chunk) instead of `Dxil` or `DualDxilAndAccc`, and `bx_shader_prepare_for_d3dmetal` returns `BX_SHADER_ERR_UNSUPPORTED` rather than passing a bitcode-less container through. Covered by synthetic `NOBC`, mixed-chunk, and bounds-safety unit tests in `dxbc-accc`; live CrossOver execution is unchanged.
### Added
- `AcpHal.dll`:
- **Shape contexts:** `AcpHalAllocateShapeContexts` and `AcpHalReleaseShapeContexts` allocate from the APU heap. The layout is the documented `SHAPE_CONTEXTS` (96 bytes; CPU pointers at 24, APU addresses at 72). A second allocation returns `ACP_E_ALREADY_ALLOCATED`.
- **`IAcpHal`** has the documented non-COM slot order: `Connect`, `Disconnect`, `SubmitCommand`, `PopMessage`, `GetNumMessages`, `Release`.
- **The default APU heap** is 64 MB.
- **Tests:** `test_audio_input` checks both. `test_integration_boot` releases `IAcpHal` through its own `Release` slot.
- `xgameruntime.dll!QueryApiImpl` now serves the runtime objects behind **`XSystemGetAnalyticsInfo`** and **`XDisplayTryEnableHdrMode`**, which a project-authorized target title requests during System Init (previously logged as unsupported `B884675D-…` and `03F0FE74-…/35F07670-…`). The keys and slots (analytics slot 3, which returns `XSystemAnalyticsInfo` by value; HDR slot 4) were derived from authorized GDK material, and no GDK files are in the repository. The function contracts follow Microsoft's public `XSystem.h` / `XDisplay.h` documentation. `XSystemGetAnalyticsInfo` reports family `Xbox`, a form that follows the profile (`Xbox Series S` for Lockhart, `Xbox Series X` for Anaconda) and BetterXen's synthetic OS version `10.0.26100.0` (logged with `synthetic=1`). `XDisplayTryEnableHdrMode` returns `XDisplayHdrModeResult::Disabled`, the documented success answer when HDR is not enabled, because BetterXen presents SDR. It clears the optional luminance output and logs the preference. `test_xgameruntime` calls both through `QueryApiImpl` and checks the public contract. The target title now gets past both calls; its next stops are fixed by #643 and #640.
- `xmem.dll` exports two new internal cross-DLL hooks, `bx_xmem_try_charge_external` and `bx_xmem_release_charge_external` (`abi/defs/xmem.def` grows 15 → 17; `scripts/build-shims.sh` now reports 113 exact exports) (#550). They admit and release bytes against the combined Lockhart/Anaconda title cap under the same `usage_lock` `can_commit` takes, and `can_commit` now counts a new `external_bytes` counter in `used`. `XMemGetWorkingSetStatistics` / `bx_xmem_get_internal_stats` report external charges inside `committed_virtual_bytes`, so reported usage matches what the cap counts. A release larger than the outstanding external charge is refused and logged (the counter is left unchanged, so other owners' charges survive), and a failed bookkeeping refresh during a charge is logged as `external_charge` instead of being reported silently as a budget rejection. New `test_xmem_external_charge` charges externally and checks that `XMemVirtualAlloc` is then refused at the cap, that the stats include the charge, that an over-release is refused, and that the allocation is admitted again after release. `d3d12_xs` switches to the hook in the same change (#552, below).
- `bx-cli run` maps the Xbox title drives before launch: `G:` to the executable's directory and `T:` to `drive_c/bx_temp` in the prefix or bottle (`launcher/crates/bx-cli/src/drives.rs`). An Xbox title opens its package on `G:` and writes crash logs to `T:`, and stops at startup when either is missing (observed on a project-authorized target build; the drive roles are an inference). Ownership is recorded before creating or retargeting links, so a failed manifest write cannot leave an unrecorded mapping; temporary links use per-process names and manifest targets use lossless path-byte encoding. A prefix lock prevents another `bx-cli run` from changing its drive mappings while a guest is active. The `.bx-drives` manifest is written to a temporary file and renamed into place without following an existing temporary symlink, so an interrupted write keeps the previous manifest instead of leaving a truncated one that later runs refuse. **Behaviour change:** `bx-cli run` takes a prefix-wide lock before system32 staging and holds it until the guest exits, so runs sharing a prefix or bottle are serial: a second launcher on the same prefix is refused, before it stages anything. `test_xbox_drives` checks the resulting layout, not who created the links.
+104 -23
View File
@@ -1,29 +1,52 @@
#include <windows.h>
#include <unknwn.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <mutex>
#include <new>
#include "bx_log.h"
namespace {
class AcpHal final : public IUnknown {
LONG refs = 1;
// IAcpHal (authorized GDK documentation; slot order derived from it): not a COM interface. Its
// six methods are Connect(numCommands, numMessages), Disconnect(), SubmitCommand(command,
// commandId, audioFrame, data, notification), PopMessage(ACP_MESSAGE*), GetNumMessages() and
// Release(). BetterXen has no audio control processor: commands are accepted and logged once,
// and no message ever arrives.
class AcpHal {
UINT32 commands_ = 0, messages_ = 0;
bool connected_ = false;
public:
HRESULT WINAPI QueryInterface(REFIID iid, void** out) override {
if (!out) return E_POINTER;
*out = nullptr;
if (iid != IID_IUnknown) return E_NOINTERFACE;
AddRef();
*out = this;
virtual HRESULT WINAPI Connect(UINT32 num_commands, UINT32 num_messages) {
if (connected_) return HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED);
commands_ = num_commands;
messages_ = num_messages;
connected_ = true;
char detail[96];
std::snprintf(detail, sizeof(detail), "commands=%u messages=%u (no ACP processing)", num_commands, num_messages);
bx_log_emit(BX_LOG_INFO, "acphal", "connect", detail);
return S_OK;
}
ULONG WINAPI AddRef() override { return InterlockedIncrement(&refs); }
ULONG WINAPI Release() override {
ULONG left = InterlockedDecrement(&refs);
if (!left) delete this;
return left;
virtual HRESULT WINAPI Disconnect() {
connected_ = false;
return S_OK;
}
virtual HRESULT WINAPI SubmitCommand(UINT32 command, UINT64, UINT32, const void*, UINT32) {
if (!connected_) return E_UNEXPECTED;
static LONG logged = 0;
if (InterlockedIncrement(&logged) == 1) {
char detail[96];
std::snprintf(detail, sizeof(detail), "first command type %u accepted; ACP commands are not processed", command);
bx_log_emit(BX_LOG_WARN, "acphal", "submit_command", detail);
}
return S_OK;
}
virtual bool WINAPI PopMessage(void*) { return false; }
virtual UINT32 WINAPI GetNumMessages() { return 0; }
virtual void WINAPI Release() { delete this; }
virtual ~AcpHal() = default;
};
// Sorted synthetic address ranges. Zero is reserved for failed lookups.
@@ -47,19 +70,77 @@ HRESULT WINAPI AcpHalCreate(void** out) {
return *out ? S_OK : E_OUTOFMEMORY;
}
// NDA constraint: the public Microsoft Learn page for AcpHal is gated behind
// an Xbox developer NDA, so the true signatures and context layout are unknown
// from public sources. BetterXen therefore returns E_NOTIMPL (not the console's
// behaviour) and keeps the stub log so bx-cli trace still surfaces the call.
HRESULT WINAPI AcpHalAllocateShapeContexts(void* context) {
(void)context;
BX_STUB_LOG("AcpHal.dll", "AcpHalAllocateShapeContexts");
return E_NOTIMPL;
HRESULT WINAPI ApuAlloc(void** va, uint32_t* apu, uint32_t size, uint32_t alignment);
HRESULT WINAPI ApuFree(void* va);
// AcpHalAllocateShapeContexts(SHAPE_CONTEXTS*) / AcpHalReleaseShapeContexts() (authorized GDK
// documentation; layout and element sizes derived from it, no header text): the caller fills six
// counts, the call allocates each array from the APU heap and returns its CPU pointer and APU
// address. Allocating twice without a release fails with ACP_E_ALREADY_ALLOCATED.
struct ShapeContexts {
uint32_t counts[6]; // SRC, EQ/Comp, Filt/Vol, DMA, XMA, PCM
void* arrays[6]; // CPU pointers, same order
uint32_t apu[6]; // APU addresses, same order
};
static_assert(sizeof(ShapeContexts) == 96, "SHAPE_CONTEXTS is 96 bytes");
static_assert(offsetof(ShapeContexts, arrays) == 24 && offsetof(ShapeContexts, apu) == 72, "SHAPE_CONTEXTS layout");
constexpr uint32_t kShapeElementBytes[6] = {32, 160, 32, 16, 64, 32};
constexpr HRESULT kAcpAlreadyAllocated = static_cast<HRESULT>(0x8AC80001);
constexpr uint32_t kDefaultApuHeapBytes = 64u << 20;
void* shape_arrays[6];
bool shape_allocated = false;
HRESULT WINAPI AcpHalAllocateShapeContexts(void* argument) {
auto* contexts = static_cast<ShapeContexts*>(argument);
if (!contexts) return E_INVALIDARG;
if (shape_allocated) return kAcpAlreadyAllocated;
for (int i = 0; i < 6; ++i) {
if (contexts->counts[i] > (0xFFFFFFFFu / kShapeElementBytes[i])) return E_INVALIDARG;
}
{
// The console has a default non-cached APU heap; create one when the title made none.
std::lock_guard guard(heap_lock);
if (!heap_created) {
capacity = kDefaultApuHeapBytes;
used = 0;
heap_created = true;
bx_log_emit(BX_LOG_INFO, "acphal", "apu_default_heap", "no ApuCreateHeap before shape contexts: 64 MB default heap");
}
}
void* arrays[6] = {};
uint32_t apu[6] = {};
for (int i = 0; i < 6; ++i) {
if (!contexts->counts[i]) continue;
const uint32_t bytes = contexts->counts[i] * kShapeElementBytes[i];
const HRESULT hr = ApuAlloc(&arrays[i], &apu[i], bytes, 64);
if (FAILED(hr)) {
for (int j = 0; j < i; ++j) ApuFree(arrays[j]);
return E_OUTOFMEMORY;
}
std::memset(arrays[i], 0, bytes);
}
for (int i = 0; i < 6; ++i) {
contexts->arrays[i] = arrays[i];
contexts->apu[i] = apu[i];
shape_arrays[i] = arrays[i];
}
shape_allocated = true;
char detail[160];
std::snprintf(detail, sizeof(detail), "src=%u eqcomp=%u filtvol=%u dma=%u xma=%u pcm=%u", contexts->counts[0],
contexts->counts[1], contexts->counts[2], contexts->counts[3], contexts->counts[4], contexts->counts[5]);
bx_log_emit(BX_LOG_INFO, "acphal", "shape_contexts_allocated", detail);
return S_OK;
}
HRESULT WINAPI AcpHalReleaseShapeContexts(void) {
BX_STUB_LOG("AcpHal.dll", "AcpHalReleaseShapeContexts");
return E_NOTIMPL;
if (!shape_allocated) return S_OK;
for (void*& array : shape_arrays) {
ApuFree(array);
array = nullptr;
}
shape_allocated = false;
return S_OK;
}
HRESULT WINAPI ApuCreateHeap(uint32_t cached, uint32_t non_cached) {
+51 -12
View File
@@ -1,6 +1,7 @@
#include <windows.h>
#include <unknwn.h>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
@@ -132,11 +133,27 @@ int main() {
}
const uint64_t log_start = log_file_size(dispatcher_log_path);
// IAcpHal is not a COM interface (authorized GDK documentation): slots Connect(numCommands,
// numMessages), Disconnect(), SubmitCommand(command, id, audioFrame, data, notification),
// PopMessage(message), GetNumMessages() and Release(). BetterXen has no ACP: commands are
// accepted and no message arrives.
void* object = nullptr;
CHECK(AcpHalCreate(&object) == S_OK && object);
auto* hal = static_cast<IUnknown*>(object);
CHECK(hal->QueryInterface(IID_IUnknown, &object) == S_OK && object == hal);
CHECK(hal->Release() == 1);
void* hal = object;
const void* const* acp = *static_cast<const void* const* const*>(hal);
using ConnectFn = HRESULT(WINAPI*)(void*, uint32_t, uint32_t);
using DisconnectFn = HRESULT(WINAPI*)(void*);
using SubmitFn = HRESULT(WINAPI*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t);
using PopFn = bool(WINAPI*)(void*, void*);
using CountFn = uint32_t(WINAPI*)(void*);
using ReleaseFn = void(WINAPI*)(void*);
CHECK(reinterpret_cast<SubmitFn>(acp[2])(hal, 2, 1, 0, nullptr, 0) == E_UNEXPECTED); // before Connect
CHECK(reinterpret_cast<ConnectFn>(acp[0])(hal, 0x1000, 0x1000) == S_OK);
CHECK(FAILED(reinterpret_cast<ConnectFn>(acp[0])(hal, 0x1000, 0x1000)));
CHECK(reinterpret_cast<SubmitFn>(acp[2])(hal, 2, 1, 0, nullptr, 0) == S_OK);
unsigned char message[64]{};
CHECK(!reinterpret_cast<PopFn>(acp[3])(hal, message) && reinterpret_cast<CountFn>(acp[4])(hal) == 0);
CHECK(reinterpret_cast<DisconnectFn>(acp[1])(hal) == S_OK);
void *va = nullptr, *second = nullptr;
uint32_t base = 0, other = 0;
CHECK(ApuAlloc(&va, &base, 64, 64) == E_OUTOFMEMORY && !va && !base);
@@ -165,20 +182,42 @@ int main() {
CHECK(ApuFree(tail) == E_INVALIDARG);
CHECK(ApuFree(va) == S_OK && !ApuMapApuAddress(base) && !ApuMapVirtualAddress(va));
CHECK(ApuFree(va) == E_INVALIDARG && ApuMapApuAddress(other) == second);
CHECK(ApuFree(second) == S_OK && hal->Release() == 0);
CHECK(ApuFree(second) == S_OK);
reinterpret_cast<ReleaseFn>(acp[5])(hal);
// AcpHal shape-context stubs: NDA-gated contract, so BetterXen returns
// E_NOTIMPL and logs the symbol. Null and sentinel pointers must not crash.
CHECK(AcpHalAllocateShapeContexts(nullptr) == E_NOTIMPL);
CHECK(AcpHalAllocateShapeContexts(reinterpret_cast<void*>(uintptr_t{0xDEADBEEF})) == E_NOTIMPL);
CHECK(AcpHalReleaseShapeContexts() == E_NOTIMPL);
CHECK(AcpHalReleaseShapeContexts() == E_NOTIMPL);
// AcpHalAllocateShapeContexts(SHAPE_CONTEXTS*) (authorized GDK documentation): six counts in
// (SRC, EQ/Comp, Filt/Vol, DMA, XMA, PCM), six CPU pointers and six APU addresses out, the
// arrays from the APU heap (XMA elements are 64 bytes). A second allocation without a release
// fails with ACP_E_ALREADY_ALLOCATED (0x8AC80001).
struct ShapeContexts {
uint32_t counts[6];
void* arrays[6];
uint32_t apu[6];
};
static_assert(sizeof(ShapeContexts) == 96 && offsetof(ShapeContexts, arrays) == 24 &&
offsetof(ShapeContexts, apu) == 72,
"SHAPE_CONTEXTS layout");
constexpr HRESULT kAlreadyAllocated = static_cast<HRESULT>(0x8AC80001);
CHECK(AcpHalAllocateShapeContexts(nullptr) == E_INVALIDARG);
CHECK(AcpHalReleaseShapeContexts() == S_OK); // nothing allocated
ShapeContexts shape{};
shape.counts[4] = 2; // two XMA contexts: 128 bytes, the whole 64 + 64 heap
CHECK(AcpHalAllocateShapeContexts(&shape) == S_OK);
CHECK(shape.arrays[4] && shape.apu[4] && ApuMapApuAddress(shape.apu[4]) == shape.arrays[4]);
CHECK(!shape.arrays[0] && !shape.arrays[5] && !shape.apu[0]);
CHECK(static_cast<const unsigned char*>(shape.arrays[4])[127] == 0); // zero-filled
ShapeContexts again{};
again.counts[4] = 1;
CHECK(AcpHalAllocateShapeContexts(&again) == kAlreadyAllocated);
CHECK(AcpHalReleaseShapeContexts() == S_OK && !ApuMapApuAddress(shape.apu[4]));
CHECK(AcpHalAllocateShapeContexts(&again) == S_OK && again.arrays[4]);
CHECK(AcpHalReleaseShapeContexts() == S_OK);
char log_path[MAX_PATH];
DWORD log_len = GetEnvironmentVariableA("BX_LOG_FILE", log_path, sizeof(log_path));
if (log_len > 0 && log_len < sizeof(log_path)) {
CHECK(file_contains(log_path, log_start, "\"event\":\"unimpl_api\"", "AcpHal.dll!AcpHalAllocateShapeContexts"));
CHECK(file_contains(log_path, log_start, "\"event\":\"unimpl_api\"", "AcpHal.dll!AcpHalReleaseShapeContexts"));
CHECK(file_contains(log_path, log_start, "\"event\":\"shape_contexts_allocated\"", "xma=2"));
CHECK(file_contains(log_path, log_start, "\"event\":\"connect\"", "commands=4096"));
} else {
// Unreachable under `bx-cli run --log`. A SKIP that still exits 0 would
// contradict the exit-77 protocol, so a missing log is a failure.
@@ -880,7 +880,8 @@ int main(int argc, char** argv) {
dsp_closed = deact_hr == S_OK && XDspDisconnect(dsp) == S_OK;
}
bool apu_closed = apu_conn ? XApuDisconnect(apu_conn) == S_OK : true;
release_com(acp_hal);
/* IAcpHal is not COM: Release is its sixth slot and returns nothing. */
if (acp_hal) reinterpret_cast<void(WINAPI*)(void*)>((*static_cast<void* const* const*>(acp_hal))[5])(acp_hal);
release_com(dstorage_factory);
if (psm_constrained_reg) UnregisterAppConstrainedChangeNotification(psm_constrained_reg);
if (psm_state_reg) UnregisterAppStateChangeNotification(psm_state_reg);