Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions core/libs/prx/libkernel_sys/Aio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ int Fail(int error) {
return -1;
}

template <typename Field>
bool Store(GuestAiocb* user, Field GuestAiocb::*member, Field value) {
static const GuestAiocb layout{};
const auto offset = reinterpret_cast<std::uintptr_t>(&(layout.*member)) - reinterpret_cast<std::uintptr_t>(&layout);
return GuestCopyOut_nid_no_patch(reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(user) + offset), &value, sizeof(value));
}

void Run(Job& job) {
std::int64_t result = 0;
if (job.opcode == LioRead) result = job.bytes == 0 ? 0 : pread_nid_postfix(job.descriptor, job.buffer, job.bytes, job.offset);
Expand Down Expand Up @@ -134,8 +141,8 @@ std::int64_t Consume(Aio& aio, std::list<std::shared_ptr<Job>>::iterator found)
const auto job = *found;
aio.done.erase(found);
aio.all.remove(job);
job->user->error = job->error;
job->user->status = job->status;
Store(job->user, &GuestAiocb::error, std::int64_t{job->error});
Store(job->user, &GuestAiocb::status, job->status);
return job->error != 0 ? Fail(job->error) : job->status;
}

Expand All @@ -149,19 +156,19 @@ std::optional<std::chrono::nanoseconds> WaitTime(const KernelTimespec& time) {
}

int Queue(GuestAiocb* user, int opcode) {
if (user == nullptr) return Fail(GuestEfault);
auto& aio = Instance();
std::unique_lock lock(aio.lock);
aio.used = true;
user->status = -1;
user->error = 0;
user->kernelinfo = reinterpret_cast<void*>(std::intptr_t{-1});
Store(user, &GuestAiocb::status, std::int64_t{-1});
Store(user, &GuestAiocb::error, std::int64_t{0});
Store(user, &GuestAiocb::kernelinfo, reinterpret_cast<void*>(std::intptr_t{-1}));
const auto fail = [&](int error) {
user->error = error;
Store(user, &GuestAiocb::error, std::int64_t{error});
return Fail(error);
};
if (aio.all.size() >= QueuePerProcess) return fail(GuestEagain);
const GuestAiocb request = *user;
GuestAiocb request{};
if (!GuestCopyIn_nid_no_patch(&request, user, sizeof(request))) return fail(GuestEfault);
if (request.nbytes > IoSizeMax) return Fail(GuestEinval);
const int notify = request.sigevent.notify;
if (notify != SigevKevent && notify != SigevSignal && notify != SigevThreadId && notify != SigevNone) return fail(GuestEinval);
Expand All @@ -179,9 +186,9 @@ int Queue(GuestAiocb* user, int opcode) {
const bool vnode = type != 0 && type != TypeFifo;
if (opcode == LioSync && !vnode) return fail(GuestEinval);
if (opcode != LioSync && request.offset < 0) return fail(GuestEinval);
user->kernelinfo = reinterpret_cast<void*>(aio.nextId++);
if (!Store(user, &GuestAiocb::kernelinfo, reinterpret_cast<void*>(aio.nextId++))) return fail(GuestEinval);
if (notify != SigevNone) NotImplemented_nid_no_patch("aio completion delivering a signal or a kevent");
user->error = GuestEinprogress;
Store(user, &GuestAiocb::error, std::int64_t{GuestEinprogress});
if (type != TypeRegular && type != TypeDirectory) return fail(GuestEopnotsupp);
auto job = std::make_shared<Job>(Job{user, request.fildes, opcode, request.offset, request.buf, request.nbytes});
aio.all.push_back(job);
Expand Down
61 changes: 61 additions & 0 deletions core/libs/tests/GuestKernelSysAio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <atomic>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
Expand All @@ -15,6 +16,16 @@
#include <string>
#include <thread>

#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <sys/mman.h>
#include <unistd.h>
#endif

namespace {

}
Expand Down Expand Up @@ -65,6 +76,36 @@ void Wait(GuestAiocb& request) {
Require(aio_suspend_nid_postfix(list, 1, nullptr) == 0);
}

std::size_t PageSize() {
#ifdef _WIN32
SYSTEM_INFO system{};
GetSystemInfo(&system);
return system.dwPageSize;
#else
return static_cast<std::size_t>(::sysconf(_SC_PAGESIZE));
#endif
}

char* Pages(std::size_t bytes) {
#ifdef _WIN32
auto* memory = static_cast<char*>(VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
Require(memory != nullptr);
#else
auto* memory = static_cast<char*>(::mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
Require(memory != MAP_FAILED);
#endif
return memory;
}

void Protect(void* address, std::size_t size, bool readable) {
#ifdef _WIN32
DWORD previous = 0;
Require(VirtualProtect(address, size, readable ? PAGE_READONLY : PAGE_NOACCESS, &previous) != 0);
#else
Require(::mprotect(address, size, readable ? PROT_READ : PROT_NONE) == 0);
#endif
}

void BeforeUse() {
GuestAiocb request{};
const GuestAiocb* list[] = {&request};
Expand All @@ -76,6 +117,26 @@ void BeforeUse() {
void Validation(int file, int readOnly) {
char buffer[8] = {};
Require(aio_read_nid_postfix(nullptr) == -1 && Error() == Efault);
Require(aio_read_nid_postfix(reinterpret_cast<GuestAiocb*>(std::uintptr_t{0x10})) == -1 && Error() == Efault);

const std::size_t page = PageSize();
char* memory = Pages(page * 4);
Protect(memory + page, page, false);
auto* straddling = reinterpret_cast<GuestAiocb*>(memory + page + 40 - sizeof(GuestAiocb));
Require(aio_read_nid_postfix(straddling) == -1 && Error() == Efault && straddling->status == -1 && straddling->error == Efault);

auto* locked = reinterpret_cast<GuestAiocb*>(memory + page * 2);
*locked = Request(file, buffer, sizeof(buffer));
Protect(locked, page, true);
Require(aio_read_nid_postfix(locked) == -1 && Error() == Einval && locked->status == 0 && aio_error_nid_postfix(locked) == Einval);

auto* finished = reinterpret_cast<GuestAiocb*>(memory + page * 3);
*finished = Request(file, buffer, sizeof(buffer));
Require(aio_read_nid_postfix(finished) == 0);
Wait(*finished);
Protect(finished, page, true);
Require(aio_return_nid_postfix(finished) == 0 && finished->status == -1 && finished->error == Einprogress);
Require(aio_return_nid_postfix(finished) == -1 && Error() == Einval);

auto bad = Request(-1, buffer, sizeof(buffer));
Require(aio_read_nid_postfix(&bad) == -1 && Error() == Ebadf);
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/debt/libkernel_sys.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ The following eight candidate spellings are absent from that aerolib revision. T
- [pathconf, fpathconf, _fpathconf](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) answer every file and directory as the console's UFS mount `/system_data` does in a payload process on a PS5 (firmware 13.60). Other mounts differ: `/data` and `/user` give `_PC_LINK_MAX` 1, `_PC_SYMLINK_MAX` 0, `_PC_NO_TRUNC` `EINVAL` and 255 for `_PC_MAX_CANON`, `_PC_MAX_INPUT` and `_PC_VDISABLE`; `/`, `/system`, `/hostapp` and `/update` answer only four names; tmpfs gives `_PC_FILESIZEBITS` 0 and `_PC_SYNC_IO` 1. What a title sees under `/app0`, `/savedata0`, `/temp0` and `/download0` is not known. `fpathconf` and `_fpathconf` answer `_PC_ASYNC_IO` 200112 and, for a pipe or a socket, only `_PC_PIPE_BUF` 512, as FreeBSD 11's `kern_fpathconf` does
- [mkdirat](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) passes `AT_FDCWD` (-100) and absolute paths to `mkdir`. Any other relative path is joined to the descriptor's host directory, found the same way as for `fchdir`, so guest path aliases and case-insensitive name matching do not apply to it, and a `..` component is not kept inside the guest root. It returns `EBADF` for a descriptor that is not open and `ENOTDIR` for one that is not a directory. On Windows, `mode` is ignored. Console behaviour is unverified
- [readlink](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) on Windows reads a symbolic link's target from its reparse data and returns it with `/` separators, only when the target is relative, as `symlink` creates them. A symbolic link with an absolute target returns its print name, or its substitute name when the print name is empty, without a `\??\` or `\\?\` prefix. A directory junction returns the same kind of name when it is a drive path (`X:\...`), using the print name when that is a drive path and the substitute name otherwise. A mounted volume (`\??\Volume{GUID}\`), any other name surrogate and every reparse point without the name surrogate bit (WOF compression, deduplication, cloud files) return `EINVAL`, as FreeBSD does for a file that is not a link and as CPython's `os.readlink`, which reads only symbolic links and junctions, does. Unwritable or unmapped destination buffers return `EFAULT` (14) via `GuestCopyOut`
- [aio_read, aio_write, aio_fsync, aio_error, aio_return, aio_suspend, aio_waitcomplete, aio_cancel](../../../core/libs/prx/libkernel_sys/Aio.cpp) (libkernel_sys) - a request with `SIGEV_SIGNAL`, `SIGEV_THREAD_ID` or `SIGEV_KEVENT` completion, or on a socket, throws, because delivering a guest signal or kevent and socket aio are not modelled. One worker thread runs the process's requests one at a time in submission order, so a request is cancellable only until that thread takes it. A queued request keeps the descriptor number, not the open file: closing or reusing the descriptor before it runs changes what it reaches. Pipes fail with `EOPNOTSUPP`, as FreeBSD 11 does with `vfs.aio.enable_unsafe` 0; the console's setting is unverified. Waits are not interrupted by signals (`EINTR`), and timeouts under 1 ms round up to 1 ms (hz 1000). Otherwise the rules follow FreeBSD 11 `vfs_aio.c`
- [aio_read, aio_write, aio_fsync, aio_error, aio_return, aio_suspend, aio_waitcomplete, aio_cancel](../../../core/libs/prx/libkernel_sys/Aio.cpp) (libkernel_sys) - a request with `SIGEV_SIGNAL`, `SIGEV_THREAD_ID` or `SIGEV_KEVENT` completion, or on a socket, throws, because delivering a guest signal or kevent and socket aio are not modelled. One worker thread runs the process's requests one at a time in submission order, so a request is cancellable only until that thread takes it. A queued request keeps the descriptor number, not the open file: closing or reusing the descriptor before it runs changes what it reaches. Pipes fail with `EOPNOTSUPP`, as FreeBSD 11 does with `vfs.aio.enable_unsafe` 0; the console's setting is unverified. Waits are not interrupted by signals (`EINTR`), and timeouts under 1 ms round up to 1 ms (hz 1000). A bad pointer passed to `aio_error`, `aio_suspend` or `aio_waitcomplete` crashes the host. Otherwise the rules follow FreeBSD 11 `vfs_aio.c`
- [symlink](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) stores the target unchanged in a host symbolic link, which the host follows later, not the guest path resolver. It throws for an absolute target, which would need a host path in the link and a translation back in `readlink`. A target with a `..` component is created only when the host and the guest would reach the same place: every directory a `..` leaves must exist and not be a symbolic link, so the host's physical `..` matches the guest resolver's lexical one, and the target joined to the link's host directory must equal the guest path it names, resolved through the guest mounts. A `..` that climbs out of a mount such as `/savedata0`, or past a directory reached through a symbolic link, throws, where FreeBSD 11.4's `namei` follows it from the link's directory. Console behaviour is unverified
- [readlinkat](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) passes `AT_FDCWD` (-100) and absolute paths to `readlink`. Any other relative path is joined to the descriptor's host directory, found the same way as for `fchdir`, so guest path aliases and case-insensitive name matching do not apply to it, and a `..` component is not kept inside the guest root. It returns `EBADF` for a descriptor that is not open and `ENOTDIR` for one that is not a directory. Console behaviour is unverified
- [link](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) and `linkat` count the symbolic links met while resolving the source and the new name, and return `ELOOP` at the 33rd, as FreeBSD 11.4's [`namei`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_lookup.c#L484-L487) does with `MAXSYMLINKS` (32), where the hosts allow longer chains (Linux 40). The count walks the host path from its root, so symbolic links in the host directories a guest path maps to count too; only Windows symbolic links count there, not junctions. A guest path, and on Windows a descriptor-relative one, reaches the count with its `..` components already dropped lexically, so a link a dropped component would have followed is not counted
Expand Down
Loading