feat(libkernel): implement scePthreadTestcancel and pthread_testcancel (#834)

This commit is contained in:
Adrià
2026-10-07 00:43:32 +02:00
committed by GitHub
parent 23ba389799
commit ab9437e34d
4 changed files with 27 additions and 0 deletions
@@ -16,6 +16,7 @@ int APS5_VABI scePthreadJoin(Pthread thread, void** retval);
int APS5_VABI scePthreadRename(Pthread thread, const char* name);
Pthread APS5_VABI scePthreadSelf();
int APS5_VABI scePthreadSetcancelstate(int state, int* old_state);
void APS5_VABI scePthreadTestcancel();
int APS5_VABI scePthreadSetprio(Pthread thread, int prio);
int APS5_VABI scePthreadGetprio(Pthread thread, int* prio);
}
@@ -93,6 +94,10 @@ int APS5_VABI pthread_setschedparam_nid_postfix(Pthread thread, int policy, cons
return PosixThread::ToErrno(scePthreadSetprio(thread, param->sched_priority));
}
void APS5_VABI pthread_testcancel_nid_postfix(void) {
scePthreadTestcancel();
}
void APS5_VABI pthread_yield_nid_postfix(void) {
std::this_thread::yield();
}
@@ -502,6 +502,9 @@ int APS5_VABI scePthreadSetcanceltype(int type, int* old_type) {
return SCE_OK;
}
void APS5_VABI scePthreadTestcancel() {
}
int APS5_VABI scePthreadSetprio(Pthread thread, int prio) {
if (!thread) return SCE_KERNEL_ERROR_EINVAL;
thread->priority.store(prio, std::memory_order_relaxed);
+18
View File
@@ -8,6 +8,10 @@ int APS5_VABI scePthreadJoin(Pthread thread, void** retval);
int APS5_VABI scePthreadDetach(Pthread thread);
void APS5_VABI scePthreadExit(void* retval);
Pthread APS5_VABI scePthreadSelf();
void APS5_VABI scePthreadTestcancel();
void APS5_VABI pthread_testcancel_nid_postfix(void);
int APS5_VABI scePthreadSetcancelstate(int state, int* old_state);
int APS5_VABI scePthreadSetcanceltype(int type, int* old_type);
int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr);
int APS5_VABI scePthreadMutexattrDestroy(PthreadMutexattr* attr);
int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type);
@@ -38,6 +42,8 @@ static constexpr int SCE_OK = 0;
static constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016;
static constexpr int SCE_KERNEL_ERROR_EPERM = 0x80020001;
static constexpr int MUTEX_TYPE_RECURSIVE = 2;
static constexpr int PTHREAD_CANCEL_ENABLE = 0;
static constexpr int PTHREAD_CANCEL_ASYNCHRONOUS = 2;
static constexpr std::intptr_t WorkerRetval = 0x1234;
static void Require(bool value) { if (!value) std::abort(); }
@@ -48,6 +54,7 @@ struct WorkerContext {
bool workerStackReported = false;
PthreadMutex* mutex = nullptr;
int unlockResult = 0;
bool testcancelReturned = false;
};
static void* APS5_VABI Worker(void* arg) {
@@ -56,6 +63,13 @@ static void* APS5_VABI Worker(void* arg) {
int local = 0;
context.workerStackReported = StackContains(context.selfFromWorker, &local);
context.unlockResult = scePthreadMutexUnlock(context.mutex);
int oldState = -1;
int oldType = -1;
context.testcancelReturned = scePthreadSetcancelstate(PTHREAD_CANCEL_ENABLE, &oldState) == SCE_OK &&
scePthreadSetcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldType) == SCE_OK;
scePthreadTestcancel();
pthread_testcancel_nid_postfix();
context.testcancelReturned = context.testcancelReturned && oldState == PTHREAD_CANCEL_ENABLE;
scePthreadExit(reinterpret_cast<void*>(WorkerRetval));
return nullptr;
}
@@ -92,6 +106,10 @@ int main() {
Require(context.selfFromWorker != mainSelf);
Require(context.unlockResult == SCE_KERNEL_ERROR_EPERM);
Require(context.workerStackReported);
Require(context.testcancelReturned);
scePthreadTestcancel();
pthread_testcancel_nid_postfix();
Require(scePthreadMutexUnlock(&mutex) == SCE_OK);
Require(scePthreadMutexDestroy(&mutex) == SCE_OK);
+1
View File
@@ -165,6 +165,7 @@ Throughout the project, every function at every stage either **does exactly what
- [Additional content](../../core/libs/prx/libSceAppContent/Export.cpp) (libSceAppContent) is not installed or mounted: `sceAppContentAddcontMount` answers `NOT_FOUND` for every entitlement label and `sceAppContentAddcontUnmount` for every mount point, as no add-content mount is ever handed out
- [sceKernelInstallExceptionHandler](../../core/libs/prx/libkernel/System/src/Exception.cpp) records the handler but nothing delivers to it: a host fault still ends the process, and `sceKernelRaiseException` is not implemented. PPSA12544's PS5Util module (Unity) installs a SIGUSR1 handler that reads `uc_mcontext.mc_rsp` (offset 0xf8 of the context) and raises SIGUSR1 on other threads to stop them for garbage collection, so delivery must run the handler on the target thread with a FreeBSD `ucontext`
- [libSceUlt queues](../../core/libs/prx/libSceUlt/Export.cpp) use preallocated host storage instead of the supplied guest work area; its existing size formula and the 512-byte object layouts remain unverified. Waiting-queue pool thread limits and waiter priority ordering are not implemented. Finalization wakes blocked queue callers with `ULT_ERROR_STATE`.
- [Thread cancellation](../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) is not modelled: `scePthreadCancel` throws, so no cancel request is ever pending and `scePthreadTestcancel` and `pthread_testcancel` return without acting; the cancel state and type set by `scePthreadSetcancelstate` and `scePthreadSetcanceltype` are stored and not used
- [MIMG `tfe` and `lwe`](../../core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp) never write the status VGPR after the data. On hardware it is written only when a texel fetch hits an unmapped page of a partially resident texture, or, for `lwe` on sample/gather, when the LOD is below the T# `MIN_LOD_WARN`; texture memory is always resident here, and `lwe` on sample, gather and `image_get_lod` throws.
- [Image atomics](../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) on 32_SINT and 32_FLOAT surfaces act on the raw dword through an R32_UINT storage view; their float results are assumed to match those measured on 32_UINT surfaces. Other formats throw.
- [`ds_wrxchg2_rtn_b32` and `ds_wrxchg2st64_rtn_b32`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit atomic swaps per invocation. RDNA2 hardware (measured on gfx1035) performs every lane's first exchange before any lane's second one, so when lanes of the same wave or of different waves overlap addresses, the result can differ from hardware. `offset0 == offset1` throws, although on hardware it is two exchanges in order.