Skip to content
Open
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
2 changes: 2 additions & 0 deletions core/libs/prx/libkernel/Aio/src/Aio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdexcept>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/Equeue/Equeue.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libc/include/PreciseWait.hpp"
#include "prx/libkernel/File/include/ArchivedFile.hpp"
Expand Down Expand Up @@ -213,6 +214,7 @@ void RunRequest(KernelAioRwRequest& req, bool write) {
req.result->state = AioCompleted;
return;
}
if (!write) EqueueNoteRead(req.fd);
req.result->return_value = done;
req.result->state = AioCompleted;
}
Expand Down
18 changes: 17 additions & 1 deletion core/libs/prx/libkernel/Equeue/Equeue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,25 @@ void KqueueDescriptorReadShut(int descriptor);
extern "C" bool IsKqueueDescriptor_nid_no_patch(int descriptor);
std::optional<bool> KqueueReadable(int descriptor);
KernelEqueueEvent EqueueDescriptorEvent(int descriptor, int16_t filter, uint16_t flags, void* udata);
void RequireVnodeNotes(uint32_t fflags);
void SetVnodeNotes(KernelEqueueEvent& event, uint32_t fflags);
int AddVnodeEvent(const KernelEqueueRef& queue, int descriptor, uint32_t fflags, uint16_t flags, void* udata);

struct EqueueCloseNote {
uint32_t device = 0;
uint32_t inode = 0;
uint32_t note = 0;
};

void EqueueNoteOpen(int descriptor);
void EqueueNoteRead(int descriptor);
EqueueCloseNote EqueuePrepareCloseNote(int descriptor);
void EqueueDeliverCloseNote(const EqueueCloseNote& note);
void EqueueNoteClose(int descriptor);

template <typename Result> Result EqueueNotedRead(int descriptor, Result result) {
if (result >= 0) EqueueNoteRead(descriptor);
return result;
}
extern "C" {

KernelEqueueRef EqueuePin_nid_postfix(KernelEqueue eq);
Expand Down
140 changes: 125 additions & 15 deletions core/libs/prx/libkernel/Equeue/src/FileEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#include "prx/libkernel/include/Api.hpp"
#include "prx/libkernel/Equeue/Equeue.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libkernel/File/include/ArchivedFile.hpp"
#include "prx/libkernel/File/include/DescriptorControl.hpp"
#include "prx/libkernel/File/include/File.hpp"
#include "prx/libkernel/File/include/NativeStat.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"

#include <atomic>
#include <cerrno>
#include <cstdint>
#include <memory>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <thread>
Expand Down Expand Up @@ -37,7 +42,12 @@
namespace {

constexpr int GUEST_EBADF = 9;
constexpr int GUEST_ENODEV = 19;
constexpr int GUEST_EINVAL = 22;
constexpr std::uint32_t GUEST_NOTE_OPEN = 0x0080;
constexpr std::uint32_t GUEST_NOTE_CLOSE = 0x0100;
constexpr std::uint32_t GUEST_NOTE_CLOSE_WRITE = 0x0200;
constexpr std::uint32_t GUEST_NOTE_READ = 0x0400;
#ifdef __APPLE__
static_assert(NOTE_DELETE == 0x0001 && NOTE_WRITE == 0x0002 && NOTE_EXTEND == 0x0004 && NOTE_ATTRIB == 0x0008 &&
NOTE_LINK == 0x0010 && NOTE_RENAME == 0x0020);
Expand Down Expand Up @@ -148,6 +158,70 @@ void VnodeWatch::Deliver(std::uint32_t hint) {
owner->TriggerEvent(ident, EVFILT_VNODE, &message);
}

struct VnodeIdentity {
std::uint32_t device;
std::uint32_t inode;
};

struct AccessWatch {
VnodeIdentity identity;
std::weak_ptr<VnodeWatch> watch;
};

std::mutex g_accessMutex;
std::vector<AccessWatch> g_accessWatches;
std::atomic<std::size_t> g_accessWatchCount{0};

std::optional<VnodeIdentity> DescriptorIdentity(int descriptor) {
FileStat status{};
if (!File::FillFileStatFromDescriptor(descriptor, &status)) {
return std::nullopt;
}
return VnodeIdentity{status.st_dev, status.st_ino};
}

void WatchAccess(const VnodeIdentity& identity, const std::shared_ptr<VnodeWatch>& watch) {
std::lock_guard lock(g_accessMutex);
g_accessWatches.push_back({identity, watch});
g_accessWatchCount.store(g_accessWatches.size());
}

void ForgetAccess(const VnodeWatch* watch) {
std::lock_guard lock(g_accessMutex);
std::erase_if(g_accessWatches, [watch](const AccessWatch& entry) {
const auto live = entry.watch.lock();
return !live || live.get() == watch;
});
g_accessWatchCount.store(g_accessWatches.size());
}

void DeliverAccess(const VnodeIdentity& identity, std::uint32_t note) {
std::vector<std::shared_ptr<VnodeWatch>> watches;
{
std::lock_guard lock(g_accessMutex);
for (const auto& entry : g_accessWatches) {
if (entry.identity.device != identity.device || entry.identity.inode != identity.inode) {
continue;
}
if (auto live = entry.watch.lock()) {
watches.push_back(std::move(live));
}
}
}
for (const auto& watch : watches) {
watch->Deliver(note);
}
}

void DeliverAccess(int descriptor, std::uint32_t note) {
if (g_accessWatchCount.load() == 0) {
return;
}
if (const auto identity = DescriptorIdentity(descriptor)) {
DeliverAccess(*identity, note);
}
}

#ifdef _WIN32

constexpr DWORD VNODE_POLL_MILLISECONDS = 50;
Expand Down Expand Up @@ -355,8 +429,12 @@ int OpenHostWatch(int descriptor, std::uint32_t notes, HostWatch& host) {
if (type == FILE_TYPE_PIPE) {
return GUEST_EINVAL;
}
if (type == FILE_TYPE_CHAR) {
DWORD mode = 0;
return ::GetConsoleMode(handle, &mode) ? GUEST_EINVAL : GUEST_ENODEV;
}
if (type != FILE_TYPE_DISK) {
NotImplemented_nid_no_patch("EVFILT_VNODE: a descriptor that is not a file or a directory");
return GUEST_ENODEV;
}
host.target = ::ReOpenFile(handle, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0);
}
Expand Down Expand Up @@ -438,7 +516,7 @@ int OpenHostWatch(int descriptor, std::uint32_t, HostWatch& host) {
return GUEST_EINVAL;
}
if (!S_ISREG(status.st_mode) && !S_ISDIR(status.st_mode)) {
NotImplemented_nid_no_patch("EVFILT_VNODE: a descriptor that is not a file or a directory");
return ::isatty(descriptor) ? GUEST_EINVAL : GUEST_ENODEV;
}
host.target = ::fcntl(descriptor, F_DUPFD_CLOEXEC, 0);
if (host.target < 0) {
Expand Down Expand Up @@ -637,7 +715,7 @@ int OpenHostWatch(int descriptor, std::uint32_t, HostWatch& host) {
return GUEST_EINVAL;
}
if (!S_ISREG(status.st_mode) && !S_ISDIR(status.st_mode)) {
NotImplemented_nid_no_patch("EVFILT_VNODE: a descriptor that is not a file or a directory");
return ::isatty(descriptor) ? GUEST_EINVAL : GUEST_ENODEV;
}
host.directory = S_ISDIR(status.st_mode);
const auto path = "/proc/self/fd/" + std::to_string(descriptor);
Expand Down Expand Up @@ -682,7 +760,9 @@ void TriggerVnode(KernelEqueueEvent* event, void* data) {
}

void StopVnode(KernelEqueue, KernelEqueueEvent* event) {
static_cast<VnodeWatch*>(event->filter.data)->Stop();
auto* watch = static_cast<VnodeWatch*>(event->filter.data);
ForgetAccess(watch);
watch->Stop();
}

int VnodeDescriptorError(int descriptor) {
Expand All @@ -700,25 +780,22 @@ int VnodeDescriptorError(int descriptor) {

}

void RequireVnodeNotes(std::uint32_t fflags) {
if ((fflags & ~MAPPED_NOTES) != 0) {
NotImplemented_nid_no_patch(("EVFILT_VNODE: fflags " + std::to_string(fflags & ~MAPPED_NOTES)).c_str());
}
}

void SetVnodeNotes(KernelEqueueEvent& event, std::uint32_t fflags) {
static_cast<VnodeWatch*>(event.filter.data)->SetInterest(fflags);
}

int AddVnodeEvent(const KernelEqueueRef& queue, int descriptor, std::uint32_t fflags, std::uint16_t flags, void* udata) {
RequireVnodeNotes(fflags);
if (const int error = VnodeDescriptorError(descriptor)) {
return error;
}
const bool archived = File::IsArchivedFile(descriptor);
HostWatch host;
if (const int error = OpenHostWatch(descriptor, fflags, host)) {
return error;
if (!archived) {
if (const int error = OpenHostWatch(descriptor, fflags, host)) {
return error;
}
}
const auto identity = DescriptorIdentity(descriptor);
std::shared_ptr<VnodeWatch> watch;
try {
watch = std::make_shared<VnodeWatch>(queue, static_cast<std::uintptr_t>(descriptor), fflags);
Expand Down Expand Up @@ -746,10 +823,44 @@ int AddVnodeEvent(const KernelEqueueRef& queue, int descriptor, std::uint32_t ff
CloseHostWatch(host);
return result == EQUEUE_OK ? 0 : result & 0xffff;
}
std::thread(RunHostWatch, watch, std::move(host)).detach();
if (identity) {
WatchAccess(*identity, watch);
}
if (!archived) {
std::thread(RunHostWatch, watch, std::move(host)).detach();
}
return 0;
}

void EqueueNoteOpen(int descriptor) {
DeliverAccess(descriptor, GUEST_NOTE_OPEN);
}

void EqueueNoteRead(int descriptor) {
DeliverAccess(descriptor, GUEST_NOTE_READ);
}

EqueueCloseNote EqueuePrepareCloseNote(int descriptor) {
if (g_accessWatchCount.load() == 0 || !File::LastDescriptorOfOpenFile(descriptor)) {
return {};
}
const auto identity = DescriptorIdentity(descriptor);
if (!identity) {
return {};
}
return {identity->device, identity->inode, File::OpenFileWrites(descriptor) ? GUEST_NOTE_CLOSE_WRITE : GUEST_NOTE_CLOSE};
}

void EqueueDeliverCloseNote(const EqueueCloseNote& note) {
if (note.note != 0) {
DeliverAccess(VnodeIdentity{note.device, note.inode}, note.note);
}
}

void EqueueNoteClose(int descriptor) {
EqueueDeliverCloseNote(EqueuePrepareCloseNote(descriptor));
}

extern "C" {

int APS5_VABI sceKernelAddFileEvent(KernelEqueue eq, int fd, int watch, void* udata) {
Expand All @@ -758,7 +869,6 @@ int APS5_VABI sceKernelAddFileEvent(KernelEqueue eq, int fd, int watch, void* ud
return SCE_KERNEL_ERROR_EBADF;
}
const auto notes = static_cast<std::uint32_t>(watch);
RequireVnodeNotes(notes);
if (const int error = VnodeDescriptorError(fd)) {
return SceKernelError(error);
}
Expand Down
3 changes: 0 additions & 3 deletions core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,6 @@ int ModifyKnote(const KernelEqueueRef& queue, KernelEvent& change) {
}
const bool descriptor = change.filter == EVFILT_READ || change.filter == EVFILT_WRITE;
const auto lowWater = descriptor ? DescriptorLowWater(change) : 0;
if (change.filter == EVFILT_VNODE) {
RequireVnodeNotes(change.fflags);
}
return GuestErrno(queue->EditEvent(change.ident, change.filter, [&change, descriptor, lowWater](KernelEqueueEvent& event) {
event.event.udata = change.udata;
if (descriptor) {
Expand Down
2 changes: 2 additions & 0 deletions core/libs/prx/libkernel/File/include/DescriptorControl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class WriteSync {
bool IsVnode(int fd);
bool IsCharacterDevice(int fd);
bool GrantsAccess(int fd, bool write);
bool LastDescriptorOfOpenFile(int fd);
bool OpenFileWrites(int fd);

}

Expand Down
11 changes: 11 additions & 0 deletions core/libs/prx/libkernel/File/src/DescriptorControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,17 @@ bool IsVnode(int fd) {
#endif
}

bool LastDescriptorOfOpenFile(int fd) {
std::lock_guard lock(g_mutex);
const auto found = g_entries.find(fd);
return found == g_entries.end() || found->second.status.use_count() == 1;
}

bool OpenFileWrites(int fd) {
const auto kind = Classify(fd, "close");
return kind == Kind::File && AccessMode(fd, "close") != ReadOnly;
}

}

extern "C" bool DescriptorHasNoVnode_nid_no_patch(int fd) {
Expand Down
7 changes: 5 additions & 2 deletions core/libs/prx/libkernel/File/src/Open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ static int OpenNative(const std::filesystem::path& native, int flags, std::uint1
if (const auto archived = File::OpenArchivedFile(native, flags)) {
if (*archived < 0) return SceErrorFromErrno(-*archived);
File::RememberDescriptor(*archived, flags);
EqueueNoteOpen(*archived);
return *archived;
}
#ifdef _WIN32
Expand Down Expand Up @@ -289,6 +290,7 @@ static int OpenNative(const std::filesystem::path& native, int flags, std::uint1
if ((flags & SCE_KERNEL_O_ACCMODE) != SCE_KERNEL_O_RDONLY || (flags & (SCE_KERNEL_O_CREAT | SCE_KERNEL_O_TRUNC)))
RecordWrittenPath_nid_no_patch(native);
File::RememberDescriptor(fd, flags);
EqueueNoteOpen(fd);
return fd;
}

Expand Down Expand Up @@ -340,6 +342,7 @@ int APS5_VABI sceKernelClose(int d) {
#ifdef _WIN32
const auto positioned = File::LockPositionedTransfer(d);
#endif
EqueueNoteClose(d);
File::ForgetRandomDevice(d);
File::ForgetDescriptor(d);
ForgetJitSharedMemory(d);
Expand Down Expand Up @@ -368,10 +371,10 @@ std::int64_t APS5_VABI sceKernelRead(int d, void* buf, std::size_t nbytes) {
const GuestArena::HostWrite destination(buf, nbytes);
if (!destination.Open()) errno = EFAULT;
else if (File::ReadRandomDevice(d, buf, nbytes)) return static_cast<std::int64_t>(nbytes);
else if (const auto archived = File::ReadArchivedFile(d, buf, nbytes, nullptr)) return *archived;
else if (const auto archived = File::ReadArchivedFile(d, buf, nbytes, nullptr)) return EqueueNotedRead(d, *archived);
auto n = destination.Open() ? NativeRead(d, buf, nbytes) : -1;
if (n < 0) return SceIoErrorFromErrno(errno);
return static_cast<std::int64_t>(n);
return EqueueNotedRead(d, static_cast<std::int64_t>(n));
}

std::int64_t APS5_VABI sceKernelWrite(int d, const void* buf, std::size_t nbytes) {
Expand Down
Loading
Loading