mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #102 from asimplestray/feat/kernel-aio
feat(kernel): implement Aio request lifecycle
This commit is contained in:
@@ -270,6 +270,15 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_directory PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_executable(guest_aio_tests tests/GuestAio.cpp)
|
||||
target_include_directories(guest_aio_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_aio_tests PRIVATE libkernel libc)
|
||||
configure_windows_unwind(guest_aio_tests)
|
||||
add_test(NAME guest_aio COMMAND guest_aio_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_aio PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
endif()
|
||||
add_executable(guest_environment_tests tests/GuestEnvironment.cpp)
|
||||
target_include_directories(guest_environment_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_environment_tests PRIVATE libc)
|
||||
|
||||
@@ -1,53 +1,190 @@
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "prx/libkernel/KernelErrors.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#include <limits>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr std::int32_t AioProcessing = 2;
|
||||
constexpr std::int32_t AioCompleted = 3;
|
||||
constexpr std::int32_t AioAborted = 4;
|
||||
constexpr std::int32_t AioMaxQueues = 512;
|
||||
|
||||
std::mutex g_mutex;
|
||||
std::array<std::int32_t, AioMaxQueues> g_states{};
|
||||
std::int32_t g_nextId = 1;
|
||||
|
||||
bool ValidId(std::int32_t id) {
|
||||
return id > 0 && id < AioMaxQueues;
|
||||
}
|
||||
|
||||
std::int32_t AllocateId() {
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
const std::int32_t id = g_nextId;
|
||||
g_nextId = (g_nextId + 1) % AioMaxQueues;
|
||||
if (g_nextId == 0) g_nextId = 1;
|
||||
g_states[id] = AioProcessing;
|
||||
return id;
|
||||
}
|
||||
|
||||
void SetState(std::int32_t id, std::int32_t state) {
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
g_states[id] = state;
|
||||
}
|
||||
|
||||
std::int64_t NativePread(std::int32_t fd, void* buf, std::size_t nbyte, std::int64_t offset) {
|
||||
#ifdef _WIN32
|
||||
if (nbyte > static_cast<std::size_t>(std::numeric_limits<unsigned int>::max())) {
|
||||
throw std::runtime_error("sceKernelAioSubmitReadCommands: nbytes exceeds platform limit");
|
||||
}
|
||||
const int duped = ::_dup(fd);
|
||||
if (duped < 0) return -1;
|
||||
if (::_lseeki64(duped, offset, SEEK_SET) < 0) {
|
||||
const int error = errno;
|
||||
::_close(duped);
|
||||
errno = error;
|
||||
return -1;
|
||||
}
|
||||
const int result = ::_read(duped, buf, static_cast<unsigned int>(nbyte));
|
||||
const int error = errno;
|
||||
::_close(duped);
|
||||
errno = error;
|
||||
return result;
|
||||
#else
|
||||
return static_cast<std::int64_t>(::pread(fd, buf, nbyte, static_cast<off_t>(offset)));
|
||||
#endif
|
||||
}
|
||||
|
||||
std::int64_t NativePwrite(std::int32_t fd, const void* buf, std::size_t nbyte, std::int64_t offset) {
|
||||
#ifdef _WIN32
|
||||
if (nbyte > static_cast<std::size_t>(std::numeric_limits<unsigned int>::max())) {
|
||||
throw std::runtime_error("sceKernelAioSubmitWriteCommands: nbytes exceeds platform limit");
|
||||
}
|
||||
const int duped = ::_dup(fd);
|
||||
if (duped < 0) return -1;
|
||||
if (::_lseeki64(duped, offset, SEEK_SET) < 0) {
|
||||
const int error = errno;
|
||||
::_close(duped);
|
||||
errno = error;
|
||||
return -1;
|
||||
}
|
||||
const int result = ::_write(duped, buf, static_cast<unsigned int>(nbyte));
|
||||
const int error = errno;
|
||||
::_close(duped);
|
||||
errno = error;
|
||||
return result;
|
||||
#else
|
||||
return static_cast<std::int64_t>(::pwrite(fd, buf, nbyte, static_cast<off_t>(offset)));
|
||||
#endif
|
||||
}
|
||||
|
||||
int SubmitCommands(KernelAioRwRequest* req, std::int32_t size, bool write, std::int32_t* 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;
|
||||
}
|
||||
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);
|
||||
req[i].result->state = AioAborted;
|
||||
aborted = true;
|
||||
} else {
|
||||
req[i].result->return_value = done;
|
||||
req[i].result->state = AioCompleted;
|
||||
}
|
||||
}
|
||||
SetState(queue, aborted ? AioAborted : AioCompleted);
|
||||
*id = queue;
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int APS5_VABI sceKernelAioDeleteRequest(int32_t id, int32_t* ret) {
|
||||
(void)id;
|
||||
(void)ret;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
if (ret == nullptr) return SCE_KERNEL_ERROR_EFAULT;
|
||||
if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL;
|
||||
SetState(id, AioAborted);
|
||||
*ret = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceKernelAioInitializeImpl(void* param, int32_t size) {
|
||||
(void)param;
|
||||
(void)size;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
(void)param;
|
||||
(void)size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void APS5_VABI sceKernelAioInitializeParam(void* param) {
|
||||
(void)param;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
if (param == nullptr) throw std::invalid_argument("sceKernelAioInitializeParam: param is null");
|
||||
}
|
||||
|
||||
int APS5_VABI sceKernelAioSubmitReadCommands(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) {
|
||||
(void)req;
|
||||
(void)size;
|
||||
(void)prio;
|
||||
(void)id;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
(void)prio;
|
||||
return SubmitCommands(req, size, false, id);
|
||||
}
|
||||
|
||||
int APS5_VABI sceKernelAioSubmitWriteCommands(KernelAioRwRequest* req, int32_t size, int32_t prio, int32_t* id) {
|
||||
(void)req;
|
||||
(void)size;
|
||||
(void)prio;
|
||||
(void)id;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
(void)prio;
|
||||
return SubmitCommands(req, size, true, id);
|
||||
}
|
||||
|
||||
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;
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
*state = g_states[id];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceKernelAioWaitRequest(int32_t id, int32_t* state, uint32_t* usec) {
|
||||
(void)id;
|
||||
(void)state;
|
||||
(void)usec;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
if (state == nullptr) return SCE_KERNEL_ERROR_EFAULT;
|
||||
if (!ValidId(id)) return SCE_KERNEL_ERROR_EINVAL;
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
for (;;) {
|
||||
std::int32_t current;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_mutex);
|
||||
current = g_states[id];
|
||||
}
|
||||
if (current != AioProcessing) {
|
||||
*state = current;
|
||||
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)) {
|
||||
*state = current;
|
||||
return SCE_KERNEL_ERROR_ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(10));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,11 +15,6 @@ int APS5_VABI sceCoredumpWriteUserData() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceKernelAioPollRequest() {
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI sceKernelAprGetFileSize() {
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include "prx/libkernel/File/include/FileFlags.hpp"
|
||||
#include "prx/libkernel/KernelErrors.hpp"
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
extern "C" {
|
||||
int APS5_VABI sceKernelOpen(const char* path, int flags, std::uint16_t mode);
|
||||
int APS5_VABI sceKernelClose(int d);
|
||||
int APS5_VABI sceKernelAioDeleteRequest(std::int32_t id, std::int32_t* ret);
|
||||
int APS5_VABI sceKernelAioInitializeImpl(void* param, std::int32_t size);
|
||||
void APS5_VABI sceKernelAioInitializeParam(void* param);
|
||||
int APS5_VABI sceKernelAioSubmitReadCommands(KernelAioRwRequest* req, std::int32_t size, std::int32_t prio, std::int32_t* id);
|
||||
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);
|
||||
}
|
||||
static void Require(bool value, int line) {
|
||||
if (!value) {
|
||||
std::fprintf(stderr, "Aio check failed at line %d\n", line);
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
#define Check(value) Require((value), __LINE__)
|
||||
int main() {
|
||||
const auto root = std::filesystem::path("anyps5-aio-test-" +
|
||||
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
Check(std::filesystem::create_directory(root));
|
||||
const auto file = root / "data.bin";
|
||||
const std::string seed = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
{ std::ofstream stream(file, std::ios::binary); stream << seed; }
|
||||
Check(sceKernelAioInitializeImpl(nullptr, 0) == 0);
|
||||
int initParam = 0;
|
||||
sceKernelAioInitializeParam(&initParam);
|
||||
const int fd = sceKernelOpen(file.string().c_str(), SCE_KERNEL_O_RDONLY, 0);
|
||||
Check(fd >= 0);
|
||||
std::array<char, 16> first{};
|
||||
std::array<char, 16> second{};
|
||||
KernelAioResult firstResult{-1, 0};
|
||||
KernelAioResult secondResult{-1, 0};
|
||||
KernelAioRwRequest requests[2] = {
|
||||
{0, first.size(), first.data(), &firstResult, fd},
|
||||
{32, second.size(), second.data(), &secondResult, fd},
|
||||
};
|
||||
std::int32_t id = 0;
|
||||
Check(sceKernelAioSubmitReadCommands(requests, 2, 0, &id) == 0);
|
||||
Check(id > 0);
|
||||
std::int32_t state = 0;
|
||||
Check(sceKernelAioWaitRequest(id, &state, nullptr) == 0);
|
||||
Check(state == 3);
|
||||
Check(firstResult.state == 3 && firstResult.return_value == 16);
|
||||
Check(secondResult.state == 3 && secondResult.return_value == 16);
|
||||
Check(std::memcmp(first.data(), seed.data(), 16) == 0);
|
||||
Check(std::memcmp(second.data(), seed.data() + 32, 16) == 0);
|
||||
std::int32_t polled = 0;
|
||||
Check(sceKernelAioPollRequest(id, &polled) == 0);
|
||||
Check(polled == 3);
|
||||
std::int32_t deleted = -1;
|
||||
Check(sceKernelAioDeleteRequest(id, &deleted) == 0);
|
||||
Check(deleted == 0);
|
||||
Check(sceKernelClose(fd) == 0);
|
||||
const int writeFd = sceKernelOpen(file.string().c_str(), SCE_KERNEL_O_RDWR, 0);
|
||||
Check(writeFd >= 0);
|
||||
std::array<char, 5> written = {'H', 'E', 'L', 'L', 'O'};
|
||||
KernelAioResult writeResult{-1, 0};
|
||||
KernelAioRwRequest writeRequest{0, written.size(), written.data(), &writeResult, writeFd};
|
||||
std::int32_t writeId = 0;
|
||||
Check(sceKernelAioSubmitWriteCommands(&writeRequest, 1, 0, &writeId) == 0);
|
||||
std::int32_t writeState = 0;
|
||||
Check(sceKernelAioWaitRequest(writeId, &writeState, nullptr) == 0);
|
||||
Check(writeState == 3 && writeResult.state == 3 && writeResult.return_value == 5);
|
||||
Check(sceKernelClose(writeFd) == 0);
|
||||
{ std::ifstream stream(file, std::ios::binary); std::string contents; std::getline(stream, contents);
|
||||
Check(contents.rfind("HELLO", 0) == 0); }
|
||||
Check(sceKernelAioSubmitReadCommands(nullptr, 1, 0, &id) == SCE_KERNEL_ERROR_EFAULT);
|
||||
Check(sceKernelAioSubmitReadCommands(requests, 0, 0, &id) == SCE_KERNEL_ERROR_EINVAL);
|
||||
Check(sceKernelAioSubmitReadCommands(requests, 1, 0, nullptr) == SCE_KERNEL_ERROR_EFAULT);
|
||||
Check(sceKernelAioWaitRequest(9999, &state, nullptr) == SCE_KERNEL_ERROR_EINVAL);
|
||||
Check(sceKernelAioWaitRequest(id, nullptr, nullptr) == SCE_KERNEL_ERROR_EFAULT);
|
||||
Check(sceKernelAioPollRequest(id, nullptr) == SCE_KERNEL_ERROR_EFAULT);
|
||||
Check(sceKernelAioPollRequest(0, &polled) == SCE_KERNEL_ERROR_EINVAL);
|
||||
Check(sceKernelAioDeleteRequest(id, nullptr) == SCE_KERNEL_ERROR_EFAULT);
|
||||
Check(std::filesystem::remove_all(root) > 0);
|
||||
}
|
||||
Reference in New Issue
Block a user