Merge pull request #814 from adriadam10/feat/kernel-aio-batch

feat(libkernel): Aio batch submit, wait, cancel and delete
This commit is contained in:
Dean
2026-10-06 22:34:58 +13:00
committed by GitHub
3 changed files with 220 additions and 15 deletions
+127 -15
View File
@@ -5,6 +5,7 @@
#include <cstdint>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
@@ -26,6 +27,10 @@ constexpr std::int32_t AioProcessing = 2;
constexpr std::int32_t AioCompleted = 3;
constexpr std::int32_t AioAborted = 4;
constexpr std::int32_t AioMaxQueues = 512;
constexpr std::int32_t AioRequestNumMax = 128;
constexpr std::int32_t AioIdNumMax = 128;
constexpr std::uint32_t AioWaitAnd = 1;
constexpr std::uint32_t AioWaitOr = 2;
std::mutex g_mutex;
std::array<std::int32_t, AioMaxQueues> g_states{};
@@ -100,35 +105,67 @@ std::int64_t NativePwrite(std::int32_t fd, const void* buf, std::size_t nbyte, s
#endif
}
int SubmitCommands(KernelAioRwRequest* req, std::int32_t size, bool write, std::int32_t* id) {
bool RunRequest(KernelAioRwRequest& req, bool write) {
const std::int64_t done = write
? NativePwrite(req.fd, req.buf, req.nbyte, req.offset)
: NativePread(req.fd, req.buf, req.nbyte, req.offset);
if (done < 0) {
const int error = errno;
req.result->return_value = static_cast<std::int64_t>(SCE_KERNEL_ERROR_EIO);
if (error == EBADF) req.result->return_value = static_cast<std::int64_t>(SCE_KERNEL_ERROR_EBADF);
if (error == EFAULT) req.result->return_value = static_cast<std::int64_t>(SCE_KERNEL_ERROR_EFAULT);
req.result->state = AioAborted;
return false;
}
req.result->return_value = done;
req.result->state = AioCompleted;
return true;
}
int ValidateRequests(KernelAioRwRequest* req, std::int32_t size, const void* id) {
if (req == nullptr || id == nullptr) return SCE_KERNEL_ERROR_EFAULT;
if (size <= 0) return SCE_KERNEL_ERROR_EINVAL;
for (std::int32_t i = 0; i < size; ++i) {
if (req[i].result == nullptr) return SCE_KERNEL_ERROR_EFAULT;
}
return 0;
}
int SubmitCommands(KernelAioRwRequest* req, std::int32_t size, bool write, std::int32_t* id) {
const int error = ValidateRequests(req, size, id);
if (error != 0) return error;
const std::int32_t queue = AllocateId();
bool aborted = false;
for (std::int32_t i = 0; i < size; ++i) {
const std::int64_t done = write
? NativePwrite(req[i].fd, req[i].buf, req[i].nbyte, req[i].offset)
: NativePread(req[i].fd, req[i].buf, req[i].nbyte, req[i].offset);
if (done < 0) {
const int error = errno;
req[i].result->return_value = static_cast<std::int64_t>(SCE_KERNEL_ERROR_EIO);
if (error == EBADF) req[i].result->return_value = static_cast<std::int64_t>(SCE_KERNEL_ERROR_EBADF);
if (error == EFAULT) req[i].result->return_value = static_cast<std::int64_t>(SCE_KERNEL_ERROR_EFAULT);
req[i].result->state = AioAborted;
aborted = true;
} else {
req[i].result->return_value = done;
req[i].result->state = AioCompleted;
}
if (!RunRequest(req[i], write)) aborted = true;
}
SetState(queue, aborted ? AioAborted : AioCompleted);
*id = queue;
return 0;
}
int SubmitCommandsMultiple(KernelAioRwRequest* req, std::int32_t size, bool write, std::int32_t* id, const char* name) {
const int error = ValidateRequests(req, size, id);
if (error != 0) return error;
if (size > AioRequestNumMax) throw std::runtime_error(std::string(name) + ": more than 128 requests per batch is not modelled");
for (std::int32_t i = 0; i < size; ++i) {
const std::int32_t queue = AllocateId();
SetState(queue, RunRequest(req[i], write) ? AioCompleted : AioAborted);
id[i] = queue;
}
return 0;
}
int ValidateIds(const std::int32_t* id, std::int32_t num, const void* out, const char* name) {
if (id == nullptr || out == nullptr) return SCE_KERNEL_ERROR_EFAULT;
if (num < 0) return SCE_KERNEL_ERROR_EINVAL;
if (num > AioIdNumMax) throw std::runtime_error(std::string(name) + ": more than 128 ids per batch is not modelled");
for (std::int32_t i = 0; i < num; ++i) {
if (!ValidId(id[i])) return SCE_KERNEL_ERROR_EINVAL;
}
return 0;
}
}
extern "C" {
@@ -141,6 +178,46 @@ int APS5_VABI sceKernelAioDeleteRequest(int32_t id, int32_t* ret) {
return 0;
}
int APS5_VABI sceKernelAioDeleteRequests(int32_t* id, int32_t num, int32_t* ret) {
const int error = ValidateIds(id, num, ret, "sceKernelAioDeleteRequests");
if (error != 0) return error;
for (int32_t i = 0; i < num; ++i) {
SetState(id[i], AioAborted);
ret[i] = 0;
}
return 0;
}
int APS5_VABI sceKernelAioCancelRequest(int32_t id, int32_t* state) {
if (state == nullptr) return SCE_KERNEL_ERROR_EFAULT;
if (id == 0) {
*state = AioProcessing;
return 0;
}
if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL;
SetState(id, AioAborted);
*state = AioAborted;
return 0;
}
int APS5_VABI sceKernelAioCancelRequests(int32_t* id, int32_t num, int32_t* state) {
if (id == nullptr || state == nullptr) return SCE_KERNEL_ERROR_EFAULT;
if (num < 0) return SCE_KERNEL_ERROR_EINVAL;
if (num > AioIdNumMax) throw std::runtime_error("sceKernelAioCancelRequests: more than 128 ids per batch is not modelled");
for (int32_t i = 0; i < num; ++i) {
if (id[i] != 0 && !ValidId(id[i])) return SCE_KERNEL_ERROR_EINVAL;
}
for (int32_t i = 0; i < num; ++i) {
if (id[i] == 0) {
state[i] = AioProcessing;
continue;
}
SetState(id[i], AioAborted);
state[i] = AioAborted;
}
return 0;
}
int APS5_VABI sceKernelAioInitializeImpl(void* param, int32_t size) {
(void)param;
(void)size;
@@ -161,6 +238,16 @@ int APS5_VABI sceKernelAioSubmitWriteCommands(KernelAioRwRequest* req, int32_t s
return SubmitCommands(req, size, true, id);
}
int APS5_VABI sceKernelAioSubmitReadCommandsMultiple(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) {
(void)prio;
return SubmitCommandsMultiple(req, size, false, id, "sceKernelAioSubmitReadCommandsMultiple");
}
int APS5_VABI sceKernelAioSubmitWriteCommandsMultiple(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) {
(void)prio;
return SubmitCommandsMultiple(req, size, true, id, "sceKernelAioSubmitWriteCommandsMultiple");
}
int APS5_VABI sceKernelAioPollRequest(int32_t id, int32_t* state) {
if (state == nullptr) return SCE_KERNEL_ERROR_EFAULT;
if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL;
@@ -169,6 +256,31 @@ int APS5_VABI sceKernelAioPollRequest(int32_t id, int32_t* state) {
return 0;
}
int APS5_VABI sceKernelAioWaitRequests(int32_t* id, int32_t num, int32_t* state, uint32_t mode, uint32_t* usec) {
const int error = ValidateIds(id, num, state, "sceKernelAioWaitRequests");
if (error != 0) return error;
if (mode != AioWaitAnd && mode != AioWaitOr) throw std::runtime_error("sceKernelAioWaitRequests: unsupported wait mode");
const auto start = std::chrono::steady_clock::now();
for (;;) {
bool allDone = true;
bool anyCompleted = false;
{
std::lock_guard<std::mutex> lock(g_mutex);
for (int32_t i = 0; i < num; ++i) {
state[i] = g_states[id[i]];
if (state[i] == AioProcessing) allDone = false;
if (state[i] == AioCompleted) anyCompleted = true;
}
}
if (allDone || (mode == AioWaitOr && anyCompleted)) return 0;
if (usec != nullptr && *usec != 0) {
const auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
if (elapsed > static_cast<std::int64_t>(*usec)) return SCE_KERNEL_ERROR_ETIMEDOUT;
}
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
}
int APS5_VABI sceKernelAioWaitRequest(int32_t id, int32_t* state, uint32_t* usec) {
if (state == nullptr) return SCE_KERNEL_ERROR_EFAULT;
if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL;
+92
View File
@@ -9,6 +9,7 @@
#include <cstring>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <string>
extern "C" {
int APS5_VABI sceKernelOpen(const char* path, int flags, std::uint16_t mode);
@@ -20,6 +21,12 @@ int APS5_VABI sceKernelAioSubmitReadCommands(KernelAioRwRequest* req, std::int32
int APS5_VABI sceKernelAioSubmitWriteCommands(KernelAioRwRequest* req, std::int32_t size, std::int32_t prio, std::int32_t* id);
int APS5_VABI sceKernelAioPollRequest(std::int32_t id, std::int32_t* state);
int APS5_VABI sceKernelAioWaitRequest(std::int32_t id, std::int32_t* state, std::uint32_t* usec);
int APS5_VABI sceKernelAioSubmitReadCommandsMultiple(KernelAioRwRequest* req, std::int32_t size, std::int32_t prio, std::int32_t* id);
int APS5_VABI sceKernelAioSubmitWriteCommandsMultiple(KernelAioRwRequest* req, std::int32_t size, std::int32_t prio, std::int32_t* id);
int APS5_VABI sceKernelAioWaitRequests(std::int32_t* id, std::int32_t num, std::int32_t* state, std::uint32_t mode, std::uint32_t* usec);
int APS5_VABI sceKernelAioCancelRequest(std::int32_t id, std::int32_t* state);
int APS5_VABI sceKernelAioCancelRequests(std::int32_t* id, std::int32_t num, std::int32_t* state);
int APS5_VABI sceKernelAioDeleteRequests(std::int32_t* id, std::int32_t num, std::int32_t* ret);
}
static void Require(bool value, int line) {
if (!value) {
@@ -86,5 +93,90 @@ int main() {
Check(sceKernelAioPollRequest(id, nullptr) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioPollRequest(0, &polled) == SCE_KERNEL_ERROR_EINVAL);
Check(sceKernelAioDeleteRequest(id, nullptr) == SCE_KERNEL_ERROR_EFAULT);
const int batchFd = sceKernelOpen(file.string().c_str(), SCE_KERNEL_O_RDWR, 0);
Check(batchFd >= 0);
std::array<char, 4> patch = {'W', 'X', 'Y', 'Z'};
KernelAioResult patchResult{-1, 0};
KernelAioResult badResult{-1, 0};
KernelAioRwRequest writeBatch[2] = {
{8, patch.size(), patch.data(), &patchResult, batchFd},
{0, patch.size(), patch.data(), &badResult, -1},
};
std::int32_t writeIds[2] = {0, 0};
Check(sceKernelAioSubmitWriteCommandsMultiple(writeBatch, 2, 0, writeIds) == 0);
Check(writeIds[0] > 0 && writeIds[1] > 0 && writeIds[0] != writeIds[1]);
Check(patchResult.state == 3 && patchResult.return_value == 4);
Check(badResult.state == 4 && badResult.return_value == SCE_KERNEL_ERROR_EBADF);
std::int32_t writeStates[2] = {0, 0};
Check(sceKernelAioWaitRequests(writeIds, 2, writeStates, 1, nullptr) == 0);
Check(writeStates[0] == 3 && writeStates[1] == 4);
std::array<char, 4> readBack{};
std::array<char, 4> tail{};
KernelAioResult readBackResult{-1, 0};
KernelAioResult tailResult{-1, 0};
KernelAioRwRequest readBatch[2] = {
{8, readBack.size(), readBack.data(), &readBackResult, batchFd},
{60, tail.size(), tail.data(), &tailResult, batchFd},
};
std::int32_t readIds[2] = {0, 0};
Check(sceKernelAioSubmitReadCommandsMultiple(readBatch, 2, 0, readIds) == 0);
Check(readIds[0] > 0 && readIds[1] > 0 && readIds[0] != readIds[1]);
Check(readBackResult.state == 3 && readBackResult.return_value == 4);
Check(std::memcmp(readBack.data(), "WXYZ", 4) == 0);
Check(tailResult.state == 3 && tailResult.return_value == 4);
Check(std::memcmp(tail.data(), seed.data() + 60, 4) == 0);
std::int32_t readStates[2] = {0, 0};
std::uint32_t timeout = 1000;
Check(sceKernelAioWaitRequests(readIds, 2, readStates, 2, &timeout) == 0);
Check(readStates[0] == 3 && readStates[1] == 3);
std::int32_t cancelled = 0;
Check(sceKernelAioCancelRequest(readIds[0], &cancelled) == 0);
Check(cancelled == 4);
Check(sceKernelAioPollRequest(readIds[0], &polled) == 0);
Check(polled == 4);
Check(sceKernelAioCancelRequest(0, &cancelled) == 0);
Check(cancelled == 2);
std::int32_t cancelIds[2] = {0, readIds[1]};
std::int32_t cancelStates[2] = {0, 0};
Check(sceKernelAioCancelRequests(cancelIds, 2, cancelStates) == 0);
Check(cancelStates[0] == 2 && cancelStates[1] == 4);
Check(sceKernelAioPollRequest(readIds[1], &polled) == 0);
Check(polled == 4);
std::int32_t deletedRets[2] = {-1, -1};
Check(sceKernelAioDeleteRequests(writeIds, 2, deletedRets) == 0);
Check(deletedRets[0] == 0 && deletedRets[1] == 0);
Check(sceKernelAioPollRequest(writeIds[0], &polled) == 0);
Check(polled == 4);
Check(sceKernelAioDeleteRequests(writeIds, 0, deletedRets) == 0);
Check(sceKernelClose(batchFd) == 0);
std::int32_t badIds[2] = {readIds[0], 9999};
std::int32_t untouched[2] = {7, 7};
Check(sceKernelAioSubmitReadCommandsMultiple(nullptr, 1, 0, readIds) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioSubmitReadCommandsMultiple(readBatch, 1, 0, nullptr) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioSubmitWriteCommandsMultiple(writeBatch, 0, 0, writeIds) == SCE_KERNEL_ERROR_EINVAL);
KernelAioRwRequest noResult{0, 1, readBack.data(), nullptr, batchFd};
Check(sceKernelAioSubmitWriteCommandsMultiple(&noResult, 1, 0, writeIds) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioWaitRequests(readIds, 2, nullptr, 1, nullptr) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioWaitRequests(nullptr, 2, readStates, 1, nullptr) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioWaitRequests(readIds, -1, readStates, 1, nullptr) == SCE_KERNEL_ERROR_EINVAL);
Check(sceKernelAioWaitRequests(badIds, 2, untouched, 1, nullptr) == SCE_KERNEL_ERROR_EINVAL);
Check(untouched[0] == 7 && untouched[1] == 7);
bool threw = false;
try { sceKernelAioWaitRequests(readIds, 2, readStates, 3, nullptr); } catch (const std::runtime_error&) { threw = true; }
Check(threw);
threw = false;
std::array<std::int32_t, 129> manyIds{};
std::array<std::int32_t, 129> manyStates{};
manyIds.fill(readIds[0]);
try { sceKernelAioDeleteRequests(manyIds.data(), 129, manyStates.data()); } catch (const std::runtime_error&) { threw = true; }
Check(threw);
Check(sceKernelAioCancelRequest(9999, &cancelled) == SCE_KERNEL_ERROR_EINVAL);
Check(sceKernelAioCancelRequest(readIds[0], nullptr) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioCancelRequests(badIds, 2, untouched) == SCE_KERNEL_ERROR_EINVAL);
Check(untouched[0] == 7 && untouched[1] == 7);
Check(sceKernelAioCancelRequests(cancelIds, 2, nullptr) == SCE_KERNEL_ERROR_EFAULT);
Check(sceKernelAioDeleteRequests(badIds, 2, untouched) == SCE_KERNEL_ERROR_EINVAL);
Check(untouched[0] == 7 && untouched[1] == 7);
Check(sceKernelAioDeleteRequests(writeIds, 2, nullptr) == SCE_KERNEL_ERROR_EFAULT);
Check(std::filesystem::remove_all(root) > 0);
}
+1
View File
@@ -67,6 +67,7 @@ Throughout the project, every function at every stage either **does exactly what
- [sceKernelGetModuleInfoFromAddr](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x1A8-byte info layout follows the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation; a title's `libc.prx` reads only `id` (PPSA14632). The meaning of `flags` is unknown; only 2 is accepted, as is only the full `st_size`. The id is the `sceKernelLoadStartModule` handle of an image opened through the loader and otherwise a stable id from the same range. The name is the host file name without `.guest.prx`. An image with more than 4 load segments (the converted executable has 7) reports its first 4. `tls_offset` is 0 and `ref_count` is 1, as the host loader exposes neither. Not implemented on Windows
- [sceKernelSyncOnAddressWait](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - the only known caller passes a null timeout and a name string as the fourth argument; the timeout is assumed to point to microseconds like the other kernel waits, and the name is ignored
- [sceKernelSyncOnAddressWait8 / sceKernelSyncOnAddressWait16 / sceKernelSyncOnAddressWait32 / sceKernelSyncOnAddressWait64](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - libc's `__std_atomic_wait_direct_8/16/32/64` pass a microsecond timeout or null and ignore the result; the by-value comparand, return values and the 2- and 8-byte alignment of Wait16 and Wait64 are assumed
- [sceKernelAioSubmitReadCommandsMultiple / sceKernelAioSubmitWriteCommandsMultiple / sceKernelAioWaitRequests / sceKernelAioCancelRequest / sceKernelAioCancelRequests / sceKernelAioDeleteRequests](../../core/libs/prx/libkernel/Aio/src/Aio.cpp) (libkernel) - behaviour from shadPS4 (one id per request for the Multiple submits, cancel turns any request aborted and id 0 reports processing, wait mode 2 returns once one request completed); the 128 requests or ids per batch limit is from SharpProspero, with an unknown error, so larger batches throw; wait modes other than 1 (and) and 2 (or) throw; null pointers, negative counts and invalid ids return the codes the single-request siblings use, checked before any state is written
- [sceKernelGetAvailableCpumask](../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - returns the default thread affinity, 0x1FFF (CPUs 0 to 12), as KytyPS5 does. Titles pin threads within it (PPSA02664 passes 0x1FFB, Hades II PPSA36082 0x3 and 0x3F); whether CPU 13 is available to a title is not confirmed
- [sceKernelMapNamedFlexibleMemoryInternal](../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - flag 0x8000 unknown; only the sceKernelMapNamedFlexibleMemory flags are accepted
- [Guest arena](../../core/libs/prx/libc/src/GuestArena.cpp) (libc) - the application map area is assumed to end at 0xFC_0000_0000, as `SCE_KERNEL_APP_MAP_AREA_END_ADDR` does on the PS4. On Windows, host allocations made in the arena before libc loads stay, and a fixed guest mapping over one throws