Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions core/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,17 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel_sys>")
endif()

add_test_executable(guest_kernel_sys_phys_page_size_tests tests/GuestKernelSysPhysPageSize.cpp)
target_include_directories(guest_kernel_sys_phys_page_size_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_kernel_sys_phys_page_size_tests PRIVATE libkernel_sys libkernel libc)
configure_windows_unwind(guest_kernel_sys_phys_page_size_tests)
add_test(NAME guest_kernel_sys_phys_page_size COMMAND guest_kernel_sys_phys_page_size_tests)
set_tests_properties(guest_kernel_sys_phys_page_size PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_kernel_sys_phys_page_size PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel_sys>")
endif()

add_test_executable(crash_text_tests tests/CrashText.cpp)
target_include_directories(crash_text_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
if(MINGW)
Expand Down
17 changes: 17 additions & 0 deletions core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,23 @@ bool QueryDirectMapping(std::uintptr_t address, std::uintptr_t* start, std::uint
return true;
}

std::uint64_t DirectMappingPageSize(std::uintptr_t address) {
constexpr std::uint64_t SmallPage = 0x1000;
constexpr std::uint64_t LargePage = 0x200000;
std::uintptr_t start = 0;
std::uintptr_t end = 0;
std::uint64_t phys = 0;
int memoryType = 0;
if (!QueryDirectMapping(address, &start, &end, &phys, &memoryType)) return SmallPage;
const auto granule = address & ~static_cast<std::uintptr_t>(LargePage - 1);
if (granule < start || granule + LargePage > end || (phys + (granule - start)) % LargePage != 0) return SmallPage;
std::lock_guard lock(g_protectionLock);
auto next = g_protections.upper_bound(granule);
if (next == g_protections.begin()) return SmallPage;
const auto containing = std::prev(next);
return granule + LargePage <= containing->second.end ? LargePage : SmallPage;
}

bool DirectMemoryAliased(std::uintptr_t address, std::size_t bytes) {
const auto end = address + bytes;
std::lock_guard lock(g_directLock);
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void DirectMemoryFinishRelease(int64_t start, size_t len);
void DirectMemoryAbandonRelease(int64_t start, size_t len) noexcept;
void CreateDirectMemoryBacking(int64_t start, size_t len, int memoryType);
bool QueryDirectMapping(std::uintptr_t address, std::uintptr_t* start, std::uintptr_t* end, std::uint64_t* offset, int* memoryType);
std::uint64_t DirectMappingPageSize(std::uintptr_t address);
bool DirectMemoryAliased(std::uintptr_t address, std::size_t bytes);
void ForgetDirectMemory(int64_t start, size_t len);

Expand Down
25 changes: 25 additions & 0 deletions core/libs/prx/libkernel/DirectMemory/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,31 @@ bool GuestRangeMapped(std::uintptr_t start, std::uintptr_t end) {
return true;
}

extern "C" int GuestMappingPageSize_nid_no_patch(const void* address, std::uint64_t* pageSize) {
constexpr int GuestEfault = 14;
constexpr int GuestEinval = 22;
constexpr std::uint64_t SmallPage = 0x1000;
const auto location = reinterpret_cast<std::uintptr_t>(address);
std::uintptr_t reservedStart = 0;
std::uintptr_t reservedEnd = 0;
if (GuestReservation(location, &reservedStart, &reservedEnd)) return GuestEinval;
std::uint64_t size = SmallPage;
const auto range = GuestAllocations::GuestAllocationsLookup_nid_postfix(location, false);
if (range != nullptr) {
std::uintptr_t directStart = 0;
std::uintptr_t directEnd = 0;
std::uint64_t physicalOffset = 0;
int memoryType = 0;
if (QueryDirectMapping(location, &directStart, &directEnd, &physicalOffset, &memoryType)) size = DirectMappingPageSize(location);
else if (range->releasable) return GuestEinval;
} else {
VirtualQueryInfo info{};
if (sceKernelVirtualQuery(address, 0, &info, sizeof(info)) != 0) return GuestEinval;
}
if (pageSize == nullptr) return GuestEinval;
return GuestCopyOut_nid_no_patch(pageSize, &size, sizeof(size)) ? 0 : GuestEfault;
}

namespace {

bool PageRange(void* address, std::uint64_t length, std::uintptr_t& start, std::uintptr_t& end) {
Expand Down
9 changes: 7 additions & 2 deletions core/libs/prx/libkernel_sys/Export.Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,13 @@ int APS5_VABI sceKernelGetParentApp(void) {
return 0;
}

int APS5_VABI sceKernelGetPhysPageSize(void) {
return static_cast<int>(PS5_PAGE_SIZE);
extern "C" int GuestMappingPageSize_nid_no_patch(const void* address, std::uint64_t* pageSize);

int APS5_VABI sceKernelGetPhysPageSize(const void* address, std::uint64_t* pageSize) {
const int error = GuestMappingPageSize_nid_no_patch(address, pageSize);
if (error == 0) return 0;
*__error_nid_postfix() = error;
return SceKernelError(error);
}

int APS5_VABI sceKernelGetProcessName(void) {
Expand Down
2 changes: 1 addition & 1 deletion core/libs/prx/libkernel_sys/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ int APS5_VABI sceKernelGetOpenPsIdForSystem(void* id);
int APS5_VABI sceKernelGetPagingStatsOfAllObjects(void);
int APS5_VABI sceKernelGetPagingStatsOfAllThreads(void);
int APS5_VABI sceKernelGetParentApp(void);
int APS5_VABI sceKernelGetPhysPageSize(void);
int APS5_VABI sceKernelGetPhysPageSize(const void* address, std::uint64_t* pageSize);
int APS5_VABI sceKernelGetProcessName(void);
int APS5_VABI sceKernelGetProcessTypeOfBudget(void);
int APS5_VABI sceKernelGetProductCode(void);
Expand Down
1 change: 0 additions & 1 deletion core/libs/tests/GuestKernelSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,6 @@ int main() {
CharUpperBuffW(hostDirectorySpelling.data(), static_cast<DWORD>(hostDirectorySpelling.size()));
Require(SetCurrentDirectoryW(hostDirectorySpelling.c_str()) != 0);
#endif
Require(sceKernelGetPhysPageSize() == 16384);
CheckProcessQueries();
CheckRawGetcwd();
CheckKqueueFromEqueue();
Expand Down
132 changes: 132 additions & 0 deletions core/libs/tests/GuestKernelSysPhysPageSize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include "prx/libkernel/include/Api.hpp"
#include "prx/libkernel/DirectMemory/DirectMemory.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include "prx/libkernel_sys/include/Api.hpp"
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <source_location>

namespace {

constexpr std::uint64_t Small = 0x1000;
constexpr std::uint64_t Large = 0x200000;
constexpr std::size_t Fixed = 0x10;
constexpr std::size_t Granule = 0x200000;
constexpr std::size_t Page = 0x4000;
constexpr int ReadWrite = 3;
constexpr int Unmeasured = 0x7777;

void Require(bool condition, std::source_location location = std::source_location::current()) {
if (!condition) {
std::fprintf(stderr, "sceKernelGetPhysPageSize check failed at %s:%u\n", location.file_name(), static_cast<unsigned>(location.line()));
std::abort();
}
}

std::uint64_t PageSizeAt(const void* address) {
std::uint64_t size = 0xA5A5A5A5A5A5A5A5ull;
Require(sceKernelGetPhysPageSize(address, &size) == 0);
return size;
}

void RequireRejected(const void* address, std::uint64_t* out) {
errno = Unmeasured;
Require(sceKernelGetPhysPageSize(address, out) == SCE_KERNEL_ERROR_EINVAL);
Require(errno == 22);
}

void MapDirect(char* at, std::size_t bytes, std::int64_t physical) {
void* address = at;
Require(sceKernelMapDirectMemory(&address, bytes, ReadWrite, Fixed, physical, Page) == 0);
Require(address == at);
}

void CheckDirectMappings() {
std::int64_t physical = -1;
Require(sceKernelAllocateDirectMemory(0, static_cast<std::int64_t>(sceKernelGetDirectMemorySize()), 4 * Granule, Granule, 3, &physical) == 0);
Require(physical % static_cast<std::int64_t>(Granule) == 0);
void* reserved = nullptr;
Require(sceKernelReserveVirtualRange(&reserved, 8 * Granule, 0, Granule) == 0);
auto* base = static_cast<char*>(reserved);

MapDirect(base, 2 * Granule + Page, physical);
Require(PageSizeAt(base) == Large && PageSizeAt(base + Granule - 1) == Large);
Require(PageSizeAt(base + 2 * Granule) == Small && PageSizeAt(base + 2 * Granule + Page - 1) == Small);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(base), 2 * Granule + Page) == 0);

MapDirect(base + Page, 2 * Granule, physical);
Require(PageSizeAt(base + Page) == Small && PageSizeAt(base + Granule) == Small && PageSizeAt(base + 2 * Granule) == Small);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(base + Page), 2 * Granule) == 0);

MapDirect(base + Page, 2 * Granule, physical + Page);
Require(PageSizeAt(base + Page) == Small && PageSizeAt(base + Granule - 1) == Small);
Require(PageSizeAt(base + Granule) == Large && PageSizeAt(base + 2 * Granule - 1) == Large);
Require(PageSizeAt(base + 2 * Granule + Page - 1) == Small);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(base + Page), 2 * Granule) == 0);

MapDirect(base, Granule, physical + Page);
Require(PageSizeAt(base) == Small && PageSizeAt(base + Granule / 2) == Small);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(base), Granule) == 0);

MapDirect(base, Granule, physical);
MapDirect(base + Granule, Granule, physical + Granule);
Require(PageSizeAt(base) == Large && PageSizeAt(base + Granule) == Large);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(base), 2 * Granule) == 0);

MapDirect(base, 2 * Granule, physical);
Require(PageSizeAt(base) == Large);
Require(sceKernelMprotect(base + Page, Page, 1) == 0);
Require(PageSizeAt(base) == Small && PageSizeAt(base + Granule) == Large);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(base), 2 * Granule) == 0);

Require(sceKernelReleaseDirectMemory(physical, 4 * Granule) == 0);
}

void CheckOtherMemory() {
int local = 0;
Require(PageSizeAt(&local) == Small);

void* flexible = nullptr;
Require(sceKernelMapNamedFlexibleMemory(&flexible, Page, ReadWrite, 0, "page-size") == 0);
std::uint64_t out = 0;
RequireRejected(flexible, &out);
Require(out == 0);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(flexible), Page) == 0);

void* reserved = nullptr;
Require(sceKernelReserveVirtualRange(&reserved, Granule, 0, Granule) == 0);
RequireRejected(reserved, &out);
Require(sceKernelMunmap(reinterpret_cast<std::uint64_t>(reserved), Granule) == 0);

RequireRejected(nullptr, &out);
RequireRejected(reinterpret_cast<const void*>(0x1000), &out);
RequireRejected(reinterpret_cast<const void*>(0xffffffff80000000ull), &out);
}

void CheckArguments() {
int local = 0;
std::uint64_t out = 0xA5A5A5A5A5A5A5A5ull;
errno = Unmeasured;
Require(sceKernelGetPhysPageSize(&local, &out) == 0 && out == Small && errno == Unmeasured);
RequireRejected(&local, nullptr);
RequireRejected(nullptr, nullptr);
errno = Unmeasured;
Require(sceKernelGetPhysPageSize(&local, reinterpret_cast<std::uint64_t*>(0x10)) == SCE_KERNEL_ERROR_EFAULT);
Require(errno == 14);
errno = Unmeasured;
Require(sceKernelGetPhysPageSize(&local, reinterpret_cast<std::uint64_t*>(0xffffffff80000000ull)) == SCE_KERNEL_ERROR_EFAULT);
Require(errno == 14);
RequireRejected(reinterpret_cast<const void*>(0x1000), reinterpret_cast<std::uint64_t*>(0x10));
RequireRejected(nullptr, reinterpret_cast<std::uint64_t*>(0x10));
}

}

int main() {
CheckDirectMappings();
CheckOtherMemory();
CheckArguments();
return 0;
}
2 changes: 1 addition & 1 deletion docs/dev/debt/libkernel_sys.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ The following eight candidate spellings are absent from that aerolib revision. T

- [statfs and futimesat](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) are not console-verified. `statfs` shares libkernel's host-volume statistics and incomplete guest mount identity fields with `_fstatfs`; archive-only paths throw because they have no host volume lookup. On Linux, FIFO and socket paths report their containing filesystem; this special-file behaviour is not independently measured on console. `futimesat` shares the existing timestamp implementation; descriptor-relative paths use the same host directory paths and guest-root limitations as `openat`, including Windows rename-after-open limits. A null path is `EFAULT`, as FreeBSD 11.4 `kern_utimesat`, not Linux's descriptor-only extension.
- [symlinkat](../../../core/libs/prx/libkernel_sys/Export.cpp) shares `symlink`'s target restrictions: absolute targets throw; a parent-directory target throws when the host would not follow it to the guest path it names. Descriptor-relative destination paths have the same guest-root limitations as `mkdirat`; Windows requires symbolic-link privileges. Console behaviour is unverified.
- [sceKernelGetPhysPageSize](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) returns `PS5_PAGE_SIZE` (16 KiB), the guest page granularity used by libkernel's direct-memory and mapping implementation. The export has no arguments. Console behaviour is unverified
- [sceKernelGetPhysPageSize](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) - heap blocks the guest `malloc` hands out are not exercised by the test: a PS5 reports them as 4 KiB, and here they answer `EINVAL` if the registry holds them as flexible memory. The prototype `(const void* address, uint64_t* pageSize)` and the answers come from a payload on firmware 13.590.001. It reports 2 MiB for a 2 MiB granule lying wholly inside one direct-memory mapping and one protection range whose physical offset is 2 MiB aligned (the rule of `GuestPageTableUsage`), else 4 KiB; also 4 KiB for stacks, images and globals. Flexible, anonymous, file and reserved ranges, NULL and unmapped addresses are `EINVAL` (errno 22), then a null `pageSize` is `EINVAL` and an unwritable one `EFAULT` (errno 14); success leaves errno alone. Unmeasured: a granule covered by two adjacent mappings or memory types
- [revoke](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) - a character device throws, because device revocation is not modelled. Other paths follow FreeBSD 11 `sys_revoke`: path errors as `stat` reports them, and `EINVAL` for anything that is not a character device. Guest paths have no `/dev`, so `/dev/null` and the like fail with `ENOENT`, not as devices

- [sceKernelGetUpdVersion](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) - runs the same code as `sysKernelGetUpdVersion` on firmware 13.42 ([disassembly](https://github.com/boykopovar/AnyPS5/pull/10671#issuecomment-6398286973)), so it forwards there: a null output is refused with `SCE_KERNEL_ERROR_EINVAL`, and otherwise the 4 bytes come from `machdep.upd_version`, whose retail value is unmeasured (see libkernel's entry)
Expand Down
Loading