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
2 changes: 1 addition & 1 deletion core/libs/prx/libkernel/Socket/src/Udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ int APS5_VABI ioctl_nid_postfix(int descriptor, std::uint64_t request, void* arg
else closeOnExecDescriptors.erase(descriptor);
return 0;
}
if (!argument) return Fail(14);
if (!argument && (request & 0x1fff0000 || !(request & 0x20000000))) return Fail(14);
if (request != 0x8004667e && request != 0x4004667f) return Fail(25);
std::lock_guard lock(socket->modeMutex);
if (request == 0x4004667f && socket->receiveShut) {
Expand Down
15 changes: 0 additions & 15 deletions core/libs/prx/libkernel_sys/Export.Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2018,11 +2018,6 @@ int APS5_VABI scePthreadMutexInitForInternalLibc(PthreadMutex* mutex, const Pthr
return SceResult(pthread_mutex_init_nid_postfix(mutex, attr));
}

int APS5_VABI scePthreadMutexIsowned(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI scePthreadMutexSetprioceiling(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
Expand Down Expand Up @@ -2139,21 +2134,11 @@ int APS5_VABI tcgetsid_nid_postfix(void) {
return 0;
}

int APS5_VABI tcsetsid_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI unmount_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI utrace_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI vfork_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
Expand Down
17 changes: 17 additions & 0 deletions core/libs/prx/libkernel_sys/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ constexpr int GuestEloop = 62;
constexpr int GuestEnametoolong = 63;
constexpr int GuestEnotempty = 66;
constexpr int GuestEoverflow = 84;
constexpr std::uint64_t TerminalSetControlling = 0x20007461;
constexpr int GuestAtFdcwd = -100;
constexpr int GuestAtRemovedir = 0x800;
constexpr std::size_t GuestPathMax = 1024;
Expand Down Expand Up @@ -1306,4 +1307,20 @@ int APS5_VABI sceKernelGetProcessType(int pid) {
return 0;
}

int APS5_VABI scePthreadMutexIsowned(PthreadMutex* mutex) {
const int owned = pthread_mutex_isowned_np_nid_postfix(mutex);
return owned == 0 ? 0 : SceKernelError(owned);
}

int APS5_VABI utrace_nid_postfix(const void* address, std::size_t length) {
(void)address;
(void)length;
return 0;
}

int APS5_VABI tcsetsid_nid_postfix(int fd, int pid) {
if (pid != getsid_nid_postfix(0)) return static_cast<int>(Fail(GuestEinval));
return ioctl_nid_postfix(fd, TerminalSetControlling, nullptr);
}

}
6 changes: 3 additions & 3 deletions core/libs/prx/libkernel_sys/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ int APS5_VABI scePthreadMain(void);
int APS5_VABI scePthreadMulti(void);
int APS5_VABI scePthreadMutexGetprioceiling(void);
int APS5_VABI scePthreadMutexInitForInternalLibc(PthreadMutex* mutex, const PthreadMutexattr* attr);
int APS5_VABI scePthreadMutexIsowned(void);
int APS5_VABI scePthreadMutexIsowned(PthreadMutex* mutex);
int APS5_VABI scePthreadMutexSetprioceiling(void);
int APS5_VABI scePthreadMutexattrGetgen(const PthreadMutexattr* attr, int* generation);
int APS5_VABI scePthreadMutexattrInitForInternalLibc(PthreadMutexattr* attr);
Expand All @@ -545,9 +545,9 @@ int APS5_VABI sysc_e00_nid_postfix(void);
int APS5_VABI sysc_m00_nid_postfix(void);
int APS5_VABI sysc_s00_nid_postfix(void);
int APS5_VABI tcgetsid_nid_postfix(void);
int APS5_VABI tcsetsid_nid_postfix(void);
int APS5_VABI tcsetsid_nid_postfix(int fd, int pid);
int APS5_VABI unmount_nid_postfix(void);
int APS5_VABI utrace_nid_postfix(void);
int APS5_VABI utrace_nid_postfix(const void* address, std::size_t length);
int APS5_VABI uuidgen_nid_postfix(GuestUuid* store, int count);
int APS5_VABI vfork_nid_postfix(void);
int APS5_VABI wait3_nid_postfix(void);
Expand Down
35 changes: 35 additions & 0 deletions core/libs/tests/GuestKernelSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "prx/libSceAmpr/include/Api.hpp"
#include "prx/libkernel/Pthread/include/Cond.hpp"
#include "prx/libkernel/Time/include/Time.hpp"
#include "prx/libkernel/Pthread/include/Mutex.hpp"
#include "prx/libkernel/File/include/File.hpp"
#include "prx/libc/include/Api.hpp"
#include "SceTypes.hpp"
Expand Down Expand Up @@ -57,6 +58,39 @@ int Error() {
return *__error_nid_postfix();
}

void ThreadAndSessionTests(int descriptor) {
PthreadMutex mutex = nullptr;
Require(scePthreadMutexIsowned(&mutex) == 0);
Require(scePthreadMutexInit(&mutex, nullptr, "isowned") == 0);
Require(scePthreadMutexIsowned(&mutex) == 0);
Require(scePthreadMutexLock(&mutex) == 0);
Require(scePthreadMutexIsowned(&mutex) == SceKernelError(1));
int fromOther = -1;
std::thread([&] { fromOther = scePthreadMutexIsowned(&mutex); }).join();
Require(fromOther == 0);
Require(scePthreadMutexUnlock(&mutex) == 0);
Require(scePthreadMutexIsowned(&mutex) == 0);
Require(scePthreadMutexDestroy(&mutex) == 0);
Require(scePthreadMutexIsowned(&mutex) == 0);

const char record[] = "trace";
*__error_nid_postfix() = 0;
Require(utrace_nid_postfix(record, sizeof(record)) == 0 && Error() == 0);
Require(utrace_nid_postfix(nullptr, 0) == 0);
Require(utrace_nid_postfix(nullptr, 4096) == 0 && Error() == 0);

const int session = getsid_nid_postfix(0);
Require(session > 0);
Require(tcsetsid_nid_postfix(descriptor, session + 1) == -1 && Error() == 22);
Require(tcsetsid_nid_postfix(-1, session + 1) == -1 && Error() == 22);
Require(tcsetsid_nid_postfix(descriptor, session) == -1 && Error() == 25);
Require(tcsetsid_nid_postfix(-1, session) == -1 && Error() == 9);
const int socketDescriptor = socket_nid_postfix(2, 2, 0);
Require(socketDescriptor >= 0);
Require(tcsetsid_nid_postfix(socketDescriptor, session) == -1 && Error() == 25);
Require(close_nid_postfix(socketDescriptor) == 0);
}

bool ChrootThrows(const std::string& path) {
try {
chroot_nid_postfix(path.c_str());
Expand Down Expand Up @@ -1213,6 +1247,7 @@ int main() {
CheckPathconf(root, file);
const int descriptor = open_nid_postfix(file.c_str(), 0, 0);
Require(descriptor >= 0);
ThreadAndSessionTests(descriptor);
KernelStatfs publicStats{};
auto* viaPublic = reinterpret_cast<unsigned char*>(&publicStats);
KernelStatfs syscallStats{};
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libkernel_sys.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
- [sched_setscheduler](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) follows FreeBSD 11.4's [`sys_sched_setscheduler`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/p1003_1b.c#L198-L225): an unreadable parameter returns `EFAULT`, a pid other than 0 and the guest's own returns `ESRCH`, and every other call returns `EPERM` whatever the policy and priority, because `kern_sched_setscheduler` needs `PRIV_SCHED_SET` before it checks them and `prison_priv_check` denies it to a jailed caller. A title is taken to run jailed; that is not measured on a console. The policy and priority are never validated or stored, so `EINVAL` for a bad policy is not modelled
- [sigreturn](../../../core/libs/prx/libkernel_sys/Export.Discovery.cpp) (libkernel_sys) follows FreeBSD 11.4's amd64 [`sys_sigreturn`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/amd64/amd64/machdep.c#L473-L576) on `setcontext`'s `ucontext_t`: a null context returns `EFAULT`, and `mc_flags` outside `_MC_FLAG_MASK` or an `rflags` differing from the current flags outside `PSL_USERCHANGE` return `EINVAL`; `mc_len` is not checked. As in [`set_fpcontext`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/amd64/amd64/machdep.c#L2349-L2370), `_MC_FPFMT_NODEV` keeps the FPU state, `_MC_FPOWNED_NONE` loads the initial one, the FPU and PCB owners load `mc_fpstate`, and other values return `EINVAL`. The rest is restored as by `setcontext`, which keeps the `%fs` and `%gs` bases FreeBSD would load. A `%cs` that is not a user selector (FreeBSD also raises `SIGBUS`) and `_MC_HASFPXSTATE` throw
- [sceKernelGetModuleInfoInternal, sceKernelGetModuleListInternal and sceKernelLoadStartModuleForSysmodule](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) answer for every loaded module, system modules included. `sceKernelGetModuleInfoInternal` fills the 0x1A8-byte `SceKernelModuleInfoEx` of a module handle the way `sceKernelGetModuleInfoFromAddr` does for an address, with [shadPS4's checks](https://github.com/shadps4-emu/shadPS4/blob/a808e76/src/core/libraries/kernel/process.cpp#L243-L258) in their order: `EFAULT` for a null pointer, `EINVAL` when `st_size` is not 0x1A8, `ESRCH` for an unknown handle. `sceKernelGetModuleListInternal` returns the same list as `sceKernelGetModuleList`, as [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd4/kernel/ps4_libkernel.pas#L852-L858) does; fpPS4 marks that as a guess, and neither is checked on a PS5. `sceKernelLoadStartModuleForSysmodule` forwards to `sceKernelLoadStartModule`
- [scePthreadMutexIsowned, utrace and tcsetsid](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) follow FreeBSD 11.4 except where stated. `scePthreadMutexIsowned` returns 0 when libkernel's `pthread_mutex_isowned_np` does and `0x80020000` plus its result (`0x80020001`) for a mutex the calling thread holds, as firmware 13.42's `libkernel_sys.sprx` was measured to do in an emulator ([#9713](https://github.com/boykopovar/AnyPS5/pull/9713)); a console run is not measured. `utrace` returns 0 without reading its record, as [`sys_utrace`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/kern_ktrace.c) does for an untraced process; that the PS5 kernel has `KTRACE` (else `ENOSYS`) is assumed. `tcsetsid` fails with `EINVAL` unless `pid` is `getsid(0)`, then issues `TIOCSCTTY`, which every guest descriptor refuses (`ENOTTY`, `EBADF` when closed), as libc's [termios.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/gen/termios.c)
- [sceKernelIsModuleLoaded](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) follows firmware 13.42's libkernel_sys: it asks `sceKernelGetModuleListInternal` for up to 0x300 modules and returns its error, then returns 0 when the first 256 bytes of one module's `sceKernelGetModuleInfo` name equal the argument, else `SCE_KERNEL_ERROR_ESRCH`. It inherits the names `sceKernelGetModuleInfo` reports, so a system library is found under its host file name, such as `libSceAjm.prx`, and the console's `libSceAjm.sprx` returns `SCE_KERNEL_ERROR_ESRCH` here. A null name throws, where the console faults in `strncmp`. Closes when: `sceKernelGetModuleInfo` reports the console's module names
- [chroot](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) follows FreeBSD 11.4's [`sys_chroot`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/vfs_syscalls.c#L776-L807) and [`pwd_chroot`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L3059-L3085) for a root caller: `EFAULT` for a null path, `ENOENT` for an empty or missing one, `ENAMETOOLONG` for a path of 1024 bytes or more or a component over 255, and `ENOTDIR` for a path that is not a directory or passes through a file. A directory that is the guest root (`/`, `/..`, or `.` after `fchdir` to a descriptor of `/`) succeeds and changes nothing, as the root stays the same directory; the guest is taken never to have changed its root, so open directory descriptors do not make it fail with `EPERM` (`kern.chroot_allow_open_directories` 1). Any other directory throws, naming the path: the guest path resolver has a fixed root and mount aliases (`/app0`, `/savedata0`) that do not move with it. On the console a title starts in a sandbox root, where `pwd_chroot` refuses with `EPERM` while a directory is open; whether that holds for titles, and whether a title holds `PRIV_VFS_CHROOT`, are unverified. Porpoise (PPSA99764) weak-imports it and calls it only as `fchdir` to its saved descriptor of `/` then `chroot(".")`, to put its sandbox root back after a jailbreak daemon replaced it ([`put_root_back`](https://github.com/elripalda/Porpoise-Dolphin-Emulator-for-PS5/blob/2ed1ab5a06995aa198f7bccd4da0f90c3fc29d39/src/porpoise_jailbreak.cpp#L303-L331))
- [Console identity](../../../core/libs/prx/libkernel_sys/Export.cpp) (libkernel_sys) - `get_authinfo` errors and capabilities are unverified on PS5, returning `EFAULT` when passed an unwritable destination pointer and following [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/396c2a09a38d22eaa02aab32c1967263dceab95f/src/core/libraries/kernel/kernel.cpp) with `EPERM` for null, and zero authority and credential words. The host models a retail unit (`IsCEX` 1, `IsGenuineCEX` 1, `IsDevKit` 0, `IsGenuineDevKit` 0, `IsTestKit` 0, `IsGenuineTestKit` 0) and Prospero (`IsProspero` 1, `IsKratos` 0). On a CFI-2014 PS5 (firmware 13.60) a payload process got 0 from `sceKernelIsProspero()`; a payload process is not a title, so the value a title gets is unmeasured.
Expand Down
Loading