mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #1278 from TheAggie/feat/libkernel-poll
feat(libkernel): implement poll for guest sockets
This commit is contained in:
@@ -385,6 +385,16 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_udp PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
endif()
|
||||
add_test_executable(guest_poll_tests ${LIBS_INCLUDE_DIR}/tests/GuestPoll.cpp)
|
||||
target_include_directories(guest_poll_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_poll_tests PRIVATE libkernel libc)
|
||||
configure_windows_unwind(guest_poll_tests)
|
||||
add_test(NAME guest_poll COMMAND guest_poll_tests)
|
||||
set_tests_properties(guest_poll PROPERTIES TIMEOUT 20)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_poll PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
endif()
|
||||
add_test_executable(guest_tcp_tests ${LIBS_INCLUDE_DIR}/tests/GuestTcp.cpp)
|
||||
target_include_directories(guest_tcp_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_tcp_tests PRIVATE libkernel libc)
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
#include <cerrno>
|
||||
#endif
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -22,6 +24,8 @@
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
extern "C" int* APS5_VABI __error_nid_postfix();
|
||||
@@ -166,6 +170,34 @@ bool GuestSockets::IsOpen(int descriptor) {
|
||||
return sockets.contains(descriptor);
|
||||
}
|
||||
|
||||
namespace {
|
||||
#ifdef _WIN32
|
||||
using NativePollDescriptor = WSAPOLLFD;
|
||||
#else
|
||||
using NativePollDescriptor = pollfd;
|
||||
#endif
|
||||
struct GuestPollDescriptor {
|
||||
int descriptor;
|
||||
short events;
|
||||
short revents;
|
||||
};
|
||||
struct PollFlag {
|
||||
short guest;
|
||||
short native;
|
||||
};
|
||||
constexpr short GuestPollPriority = 0x2;
|
||||
constexpr short GuestPollInvalid = 0x20;
|
||||
constexpr short GuestPollAccepted = 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80 | 0x100;
|
||||
constexpr PollFlag RequestFlags[] = {
|
||||
{0x1, POLLIN}, {0x2, POLLPRI}, {0x4, POLLOUT}, {0x40, POLLRDNORM}, {0x80, POLLRDBAND}, {0x100, POLLWRBAND}};
|
||||
constexpr PollFlag StatusFlags[] = {{0x8, POLLERR}, {0x10, POLLHUP}, {GuestPollInvalid, POLLNVAL}};
|
||||
std::shared_ptr<Socket> Find(int descriptor) {
|
||||
std::lock_guard lock(socketsMutex);
|
||||
const auto found = sockets.find(descriptor);
|
||||
return found != sockets.end() ? found->second : nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
int APS5_VABI fcntl_nid_postfix(int descriptor, int command, ...) {
|
||||
const auto socket = Lookup(descriptor);
|
||||
@@ -481,4 +513,60 @@ std::int64_t APS5_VABI recvmsg_nid_postfix(int descriptor, GuestMsghdr* message,
|
||||
message->controlLength = 0;
|
||||
return received;
|
||||
}
|
||||
int APS5_VABI poll_nid_postfix(GuestPollDescriptor* descriptors, std::uint32_t count, int timeout) {
|
||||
if (timeout < -1) return Fail(22);
|
||||
if (count && !descriptors) return Fail(14);
|
||||
std::vector<std::shared_ptr<Socket>> held;
|
||||
std::vector<NativePollDescriptor> native;
|
||||
std::vector<std::uint32_t> owners;
|
||||
int ready = 0;
|
||||
for (std::uint32_t i = 0; i < count; ++i) {
|
||||
auto& entry = descriptors[i];
|
||||
entry.revents = 0;
|
||||
if (entry.descriptor < 0) continue;
|
||||
if ((entry.events & ~GuestPollAccepted) != 0)
|
||||
throw std::runtime_error("poll: unsupported event flags " + std::to_string(entry.events));
|
||||
if (entry.descriptor < GuestSockets::FirstDescriptor)
|
||||
throw std::runtime_error("poll: descriptor " + std::to_string(entry.descriptor) + " is not a socket");
|
||||
auto socket = Find(entry.descriptor);
|
||||
if (!socket) {
|
||||
entry.revents = GuestPollInvalid;
|
||||
++ready;
|
||||
continue;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
if (entry.events & GuestPollPriority) throw std::runtime_error("poll: POLLPRI is not supported by WSAPoll");
|
||||
#endif
|
||||
NativePollDescriptor request{};
|
||||
request.fd = socket->value;
|
||||
for (const auto& flag : RequestFlags)
|
||||
if (entry.events & flag.guest) request.events = static_cast<short>(request.events | flag.native);
|
||||
native.push_back(request);
|
||||
owners.push_back(i);
|
||||
held.push_back(std::move(socket));
|
||||
}
|
||||
if (native.empty()) {
|
||||
if (ready || timeout == 0) return ready;
|
||||
if (timeout < 0) throw std::runtime_error("poll: an infinite wait without sockets never returns");
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
|
||||
return 0;
|
||||
}
|
||||
const int wait = ready ? 0 : timeout;
|
||||
#ifdef _WIN32
|
||||
const int result = WSAPoll(native.data(), static_cast<ULONG>(native.size()), wait);
|
||||
#else
|
||||
const int result = ::poll(native.data(), static_cast<nfds_t>(native.size()), wait);
|
||||
#endif
|
||||
if (result < 0) return Fail(NativeError());
|
||||
for (std::size_t i = 0; i < native.size(); ++i) {
|
||||
auto& entry = descriptors[owners[i]];
|
||||
for (const auto& flag : RequestFlags)
|
||||
if ((entry.events & flag.guest) && (native[i].revents & flag.native))
|
||||
entry.revents = static_cast<short>(entry.revents | flag.guest);
|
||||
for (const auto& flag : StatusFlags)
|
||||
if (native[i].revents & flag.native) entry.revents = static_cast<short>(entry.revents | flag.guest);
|
||||
if (entry.revents) ++ready;
|
||||
}
|
||||
return ready;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
struct PollDescriptor {
|
||||
int descriptor;
|
||||
short events;
|
||||
short revents;
|
||||
};
|
||||
extern "C" {
|
||||
int APS5_VABI socket_nid_postfix(int, int, int);
|
||||
int APS5_VABI bind_nid_postfix(int, const void*, std::uint32_t);
|
||||
int APS5_VABI listen_nid_postfix(int, int);
|
||||
int APS5_VABI accept_nid_postfix(int, void*, std::uint32_t*);
|
||||
int APS5_VABI connect_nid_postfix(int, const void*, std::uint32_t);
|
||||
int APS5_VABI getsockname_nid_postfix(int, void*, std::uint32_t*);
|
||||
std::int64_t APS5_VABI send_nid_postfix(int, const void*, std::uint64_t, int);
|
||||
std::int64_t APS5_VABI recv_nid_postfix(int, void*, std::uint64_t, int);
|
||||
int APS5_VABI close_nid_postfix(int);
|
||||
int APS5_VABI poll_nid_postfix(PollDescriptor*, std::uint32_t, int);
|
||||
int* APS5_VABI __error_nid_postfix();
|
||||
}
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
constexpr short In = 0x1;
|
||||
constexpr short Out = 0x4;
|
||||
constexpr short Hup = 0x10;
|
||||
constexpr short Invalid = 0x20;
|
||||
int main() {
|
||||
const int listener = socket_nid_postfix(2, 1, 0);
|
||||
Require(listener >= 0);
|
||||
std::array<unsigned char, 16> address{16, 2, 0, 0, 127, 0, 0, 1};
|
||||
Require(bind_nid_postfix(listener, address.data(), address.size()) == 0);
|
||||
std::uint32_t size = address.size();
|
||||
Require(getsockname_nid_postfix(listener, address.data(), &size) == 0);
|
||||
Require(listen_nid_postfix(listener, 4) == 0);
|
||||
PollDescriptor single{listener, In, -1};
|
||||
Require(poll_nid_postfix(&single, 1, 0) == 0 && single.revents == 0);
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
Require(poll_nid_postfix(&single, 1, 50) == 0);
|
||||
Require(std::chrono::steady_clock::now() - start >= std::chrono::milliseconds(40));
|
||||
const int client = socket_nid_postfix(2, 1, 0);
|
||||
Require(client >= 0 && connect_nid_postfix(client, address.data(), address.size()) == 0);
|
||||
Require(poll_nid_postfix(&single, 1, 1000) == 1 && single.revents == In);
|
||||
const int server = accept_nid_postfix(listener, nullptr, nullptr);
|
||||
Require(server >= 0);
|
||||
PollDescriptor pair[3]{{server, In, 0}, {-1, In, 7}, {client, Out, 0}};
|
||||
Require(poll_nid_postfix(pair, 3, 1000) == 1);
|
||||
Require(pair[0].revents == 0 && pair[1].revents == 0 && pair[2].revents == Out);
|
||||
const char message[] = "poll";
|
||||
Require(send_nid_postfix(client, message, sizeof(message), 0) == sizeof(message));
|
||||
pair[2].events = In;
|
||||
Require(poll_nid_postfix(pair, 3, 1000) == 1 && pair[0].revents == In && pair[2].revents == 0);
|
||||
char buffer[8]{};
|
||||
Require(recv_nid_postfix(server, buffer, sizeof(buffer), 0) == sizeof(message));
|
||||
Require(close_nid_postfix(client) == 0);
|
||||
Require(poll_nid_postfix(pair, 1, 1000) == 1 && (pair[0].revents & (In | Hup)) != 0);
|
||||
Require(recv_nid_postfix(server, buffer, sizeof(buffer), 0) == 0);
|
||||
PollDescriptor closed[2]{{client, In, 0}, {listener, In, 0}};
|
||||
Require(poll_nid_postfix(closed, 2, -1) == 1 && closed[0].revents == Invalid && closed[1].revents == 0);
|
||||
Require(poll_nid_postfix(nullptr, 0, 10) == 0);
|
||||
Require(poll_nid_postfix(nullptr, 1, 0) == -1 && *__error_nid_postfix() == 14);
|
||||
Require(poll_nid_postfix(&single, 1, -2) == -1 && *__error_nid_postfix() == 22);
|
||||
Require(close_nid_postfix(server) == 0);
|
||||
Require(close_nid_postfix(listener) == 0);
|
||||
}
|
||||
@@ -309,6 +309,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- [Raw `buffer_load_dwordx2`](../../core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cpp) is one 64-bit access on the hardware (with or without glc), so it never sees half of a `buffer_atomic_swap_x2`. Only a glc load that is 8-byte aligned and in bounds, in a program that has the 64-bit storage view (it uses 64-bit buffer atomics), is read with one 64-bit atomic load; every other dwordx2 load is two dword loads and can tear against a concurrent 64-bit write: loads without glc, and loads in a program without 64-bit buffer atomics while another shader writes the pair with `swap_x2`.
|
||||
- [wcsrtombs_s](../../core/libs/prx/libc/src/Multibyte.cpp) (libc) follows the console's `libc.prx`, but a runtime-constraint violation only returns its error: the default handler there writes the message and a newline to `stderr` before returning, which is not done here. Conversion uses the C locale of `wcrtomb` (`MB_CUR_MAX` 1), so the path where a multibyte character does not fit the remaining space is not reached
|
||||
- [collate<char>::_Getcat](../../core/libs/prx/libc/src/LocaleSupport.cpp) (libc) builds the facet for the C locale only and throws for any other locale name. The console's `libc.prx` stores pointers to its collation state tables at offsets 0x10 and 0x18 of the facet; ours leaves them null, as only the facet's own functions read them. `do_transform` throws for input with embedded null characters, which the console drops depending on the output buffer size, and for results of 4095 characters or more, whose string storage uses the guest's over-aligned large allocation.
|
||||
- [poll](../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) waits only on guest sockets, through `WSAPoll` on Windows and `poll` on Linux; the `pollfd` layout and flag values are FreeBSD's. A descriptor below the socket range (files, pipes, stdio) throws, as do event bits other than `POLLIN`, `POLLPRI`, `POLLOUT`, `POLLRDNORM`, `POLLRDBAND` and `POLLWRBAND` (the `POLLERR`, `POLLHUP` and `POLLNVAL` bits are ignored in `events`), `POLLPRI` on Windows, which `WSAPoll` rejects, and an infinite timeout with no socket to wait on. A closed socket descriptor reports `POLLNVAL`; the console's limit on the descriptor count is unknown and not checked
|
||||
- [Thread exit destructors](../../core/libs/prx/libSceLibcInternal/Export.cpp) (libSceLibcInternal) run when a libkernel thread finishes; on the main or a host thread only `_sceLibcInternalThreadDtors` runs them, as `exit` does, else they are dropped
|
||||
- [sceFontOpenFontSet](../../core/libs/prx/libSceFont/src/SystemFont.cpp) (libSceFont) opens only the primary file of a system font set. Characters the console takes from the set's other files (Hangul, Thai and Arabic in Japanese and Chinese sets, Arabic in European sets) are reported as unsupported glyphs. The console file names are those of the PS4 firmware; the names of the PS5-only JG2 sets (`0x1A......`) are unknown, so they use the SSTJpPro files. When a console file is missing, an openly licensed substitute (Noto Sans, Noto Sans Mono, Noto Sans Thai, Noto Sans CJK) is loaded instead. It is not the system font: its metrics differ, so text width, line height and wrapping differ from the console. System font faces are loaded by FreeType in host memory, not in the font memory the title passes to the library: FreeType needs about 1 MiB per CJK face, which the title's font memory is not sized for.
|
||||
- [Font writing lines](../../core/libs/prx/libSceFont/src/WritingLine.cpp) (libSceFont) - `sceFontCreateWritingLine`, `sceFontWritingLineWritesOrder`, `sceFontWritingLineRefersRenderStep`, `sceFontWritingLineGetRenderMetrics` and `sceFontWritingLineClear` follow [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/main/src/libs/libFont.cpp): runs are placed left to right, each at the line's advance so far, with no spacing or adjustment. Only horizontal and left-to-right lines, writing attribute 0 and non-null run metrics are modelled; other values, the metrics of an empty line and unknown or null handles throw, since no reference gives their result or error code. `sceFontWritingLineGetOrderingSpace` still throws (KytyPS5's values are placeholders). Lines are allocated on the host, not in the font memory
|
||||
|
||||
Reference in New Issue
Block a user