Xbox Cloud Gaming app for PS5 Homebrew
C
fix(ps4): resolve explicit scheduling EPERM, SO_NBIO EACCES, recursive mutex deadlock, and onion memory decoder
Rafael Abud Ferreira2afebcd2be85
diff --git a/src/app/app_core.cpp b/src/app/app_core.cppindex cd57985..fe1949c 100644--- a/src/app/app_core.cpp+++ b/src/app/app_core.cpp@@ -298,11 +298,14 @@ bool coreStartup() {bool ok = fonts.loadIcons(shell::iconFontPaths());XC_LOGI("system icon font: %s", ok ? "loaded" : "not found (drawn icons)");}+ const size_t imageBudget = (platform::platformName().find("PlayStation 4") != std::string::npos)+ ? (48u << 20)+ : (160u << 20);g_images = std::make_unique<ui::ImageCache>([] {if (g_ui) g_ui->invalidate();},- 160u << 20, platform::dataDir() + "/imgcache");+ imageBudget, platform::dataDir() + "/imgcache");g_ui = std::make_unique<ui::AppUi>(fonts, *g_images);g_ui->showSplash(ui::tr(ui::Str::SigningIn));startPriceLoop();@@ -1252,7 +1255,7 @@ bool Core::frame() {++logged;XC_LOGI("ui render %llu ms", static_cast<unsigned long long>(t1 - t0));}- std::lock_guard<std::mutex> lock(display::frameMutex());+ std::lock_guard<std::recursive_mutex> lock(display::frameMutex());if (g_ui->screen() != ui::Screen::Streaming) {display::drawRgba(canvas->data());display::present(); // paced by the next draw waiting for a free bufferdiff --git a/src/app/desktop_selftest.cpp b/src/app/desktop_selftest.cppindex 9b459cf..55ce762 100644--- a/src/app/desktop_selftest.cpp+++ b/src/app/desktop_selftest.cpp@@ -82,7 +82,7 @@ bool varied(const Shot& s) {}void drawCanvas(const ui::Canvas& c) {- std::lock_guard<std::mutex> lock(display::frameMutex());+ std::lock_guard<std::recursive_mutex> lock(display::frameMutex());display::drawRgba(c.data());display::present();}@@ -122,7 +122,7 @@ Yuv colourBars() {}void drawVideo(const Yuv& f) {- std::lock_guard<std::mutex> lock(display::frameMutex());+ std::lock_guard<std::recursive_mutex> lock(display::frameMutex());display::drawYuv420(f.y.data(), f.u.data(), f.v.data(), f.w, f.w / 2, f.w / 2, f.w, f.h, false);display::present();}@@ -351,7 +351,7 @@ int desktopSelftest(const std::string& dir) {nv12Uv[i * 2 + 1] = bars.v[i];}{- std::lock_guard<std::mutex> lock(display::frameMutex());+ std::lock_guard<std::recursive_mutex> lock(display::frameMutex());display::drawNv12(bars.y.data(), nv12Uv.data(), bars.w, bars.w, bars.w, bars.h, false);display::present();}diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cppindex 5f9ed6e..4f5e99d 100644--- a/src/app/stream_player.cpp+++ b/src/app/stream_player.cpp@@ -441,7 +441,7 @@ struct StreamPlayer::Impl {continue;}bool screenshot = saveSnapshotIfAsked(pic);- std::lock_guard<std::mutex> lock(display::frameMutex());+ std::lock_guard<std::recursive_mutex> lock(display::frameMutex());if (decodedFrames == 1) {XC_LOGI("first draw %dx%d (%s, %s)", pic.width, pic.height,pic.format == media::Picture::Format::Nv12 ? "NV12" : "YUV420P",diff --git a/src/display/display.h b/src/display/display.hindex 6f54ab5..e4e8f4d 100644--- a/src/display/display.h+++ b/src/display/display.h@@ -19,7 +19,7 @@ constexpr unsigned kHeight = 1080;bool init(bool preferGpu = true);void shutdown();// Held by whoever draws a frame (UI thread or video thread).-std::mutex& frameMutex();+std::recursive_mutex& frameMutex();void present();// Copies a linear 1920x1080 RGBA (0xAABBGGRR) image to the back bufferdiff --git a/src/display/display_null.cpp b/src/display/display_null.cppindex 27b73fc..e2b53f3 100644--- a/src/display/display_null.cpp+++ b/src/display/display_null.cpp@@ -9,8 +9,8 @@namespace xc::display {-std::mutex& frameMutex() {- static std::mutex m;+std::recursive_mutex& frameMutex() {+ static std::recursive_mutex m;return m;}bool init(bool) { return true; }diff --git a/src/display/display_ps4.cpp b/src/display/display_ps4.cppindex 89bce3f..0063906 100644--- a/src/display/display_ps4.cpp+++ b/src/display/display_ps4.cpp@@ -39,7 +39,7 @@ uint8_t* g_buffers[kBuffers]{};int g_currentBuffer = 0;int64_t g_flipMarker = 0;std::array<int64_t, kBuffers> g_bufferMarker{-1, -1, -1};-std::mutex g_frameMutex;+std::recursive_mutex g_frameMutex;std::atomic<int> g_sharpness{0};struct Overlay {@@ -280,7 +280,7 @@ void shutdown() {g_mappedMemory = nullptr;}-std::mutex& frameMutex() { return g_frameMutex; }+std::recursive_mutex& frameMutex() { return g_frameMutex; }namespace {// Picks the back buffer for the next frame: not on screen, not queued.@@ -545,7 +545,7 @@ bool lastShown(uint64_t& id, uint64_t& atUs) {return true;}bool readBackRgb(std::vector<uint8_t>& rgb, int& width, int& height) {- std::lock_guard<std::mutex> lock(g_frameMutex);+ std::lock_guard<std::recursive_mutex> lock(g_frameMutex);if (!g_buffers[g_currentBuffer]) return false;width = kWidth;height = kHeight;diff --git a/src/display/display_ps5.cpp b/src/display/display_ps5.cppindex cc60a66..bbd6ab8 100644--- a/src/display/display_ps5.cpp+++ b/src/display/display_ps5.cpp@@ -144,8 +144,8 @@ bool init(bool preferGpu) {return true;}-std::mutex& frameMutex() {- static std::mutex m;+std::recursive_mutex& frameMutex() {+ static std::recursive_mutex m;return m;}diff --git a/src/display/display_sdl.cpp b/src/display/display_sdl.cppindex 5151488..51f88d1 100644--- a/src/display/display_sdl.cpp+++ b/src/display/display_sdl.cpp@@ -66,7 +66,7 @@ Frame g_front; // the picture on screenOverlay g_overlayShown;bool g_frontPending = false, g_overlayPending = false; // not uploaded yet-std::mutex g_frameMutex; // display.h's: held around a draw and its present()+std::recursive_mutex g_frameMutex; // display.h's: held around a draw and its present()Frame g_back; // being drawn (under g_frameMutex)std::mutex g_mailboxMutex;@@ -218,7 +218,7 @@ void sdlSetFullscreen(bool on) {void sdlToggleFullscreen() { sdlSetFullscreen(!sdlIsFullscreen()); }-std::mutex& frameMutex() { return g_frameMutex; }+std::recursive_mutex& frameMutex() { return g_frameMutex; }bool init(bool preferGpu) {if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) {diff --git a/src/media/hw_decoder_ps4.cpp b/src/media/hw_decoder_ps4.cppindex 4c8c24f..f2d8be6 100644--- a/src/media/hw_decoder_ps4.cpp+++ b/src/media/hw_decoder_ps4.cpp@@ -161,7 +161,8 @@ void freeFlexible(FlexibleMem& mem) {struct DecoderAllocations {DirectMem computeBuf;- FlexibleMem decCpu;+ DirectMem decCpuDirect;+ FlexibleMem decCpuFlex;DirectMem decGpu;DirectMem decCpuGpu;DirectMem frameBuf;@@ -193,7 +194,8 @@ HwDecoder::~HwDecoder() {freeDirect(it->second.frameBuf);freeDirect(it->second.decCpuGpu);freeDirect(it->second.decGpu);- freeFlexible(it->second.decCpu);+ freeFlexible(it->second.decCpuFlex);+ freeDirect(it->second.decCpuDirect);freeDirect(it->second.computeBuf);s_allocations.erase(it);}@@ -247,7 +249,8 @@ bool HwDecoder::init(int maxWidth, int maxHeight) {frameBufferSize_ = 0;freeDirect(allocs.decCpuGpu);freeDirect(allocs.decGpu);- freeFlexible(allocs.decCpu);+ freeFlexible(allocs.decCpuFlex);+ freeDirect(allocs.decCpuDirect);freeDirect(allocs.computeBuf);};@@ -315,16 +318,23 @@ bool HwDecoder::init(int maxWidth, int maxHeight) {if (decMem.cpuSize > 0) {size_t cpuBytes = align16k(decMem.cpuSize);- void* cpuMem = nullptr;- rc = sceKernelMapNamedFlexibleMemory(&cpuMem, cpuBytes, kProtectionRw, 0, "PSBoxVdecCpu");- if (rc != 0 || !cpuMem) {- XC_LOGW("hwdec: allocate flexible memory %zu failed: 0x%08x", cpuBytes, static_cast<unsigned>(rc));- rollback();- return false;+ allocs.decCpuDirect = allocDirect(cpuBytes, kMemoryTypeOnion, "decoder cpu");+ if (allocs.decCpuDirect.addr) {+ decMem.cpu = allocs.decCpuDirect.addr;+ decMem.cpuSize = allocs.decCpuDirect.size;+ } else {+ XC_LOGW("hwdec: direct onion memory for cpu failed, falling back to flexible memory");+ void* cpuMem = nullptr;+ rc = sceKernelMapNamedFlexibleMemory(&cpuMem, cpuBytes, kProtectionRw, 0, "PSBoxVdecCpu");+ if (rc != 0 || !cpuMem) {+ XC_LOGW("hwdec: allocate flexible memory %zu failed: 0x%08x", cpuBytes, static_cast<unsigned>(rc));+ rollback();+ return false;+ }+ decMem.cpu = cpuMem;+ decMem.cpuSize = cpuBytes;+ allocs.decCpuFlex = {cpuMem, cpuBytes};}- decMem.cpu = cpuMem;- decMem.cpuSize = cpuBytes;- allocs.decCpu = {cpuMem, cpuBytes};}if (decMem.gpuSize > 0) {diff --git a/src/platform/ps4/platform_ps4.cpp b/src/platform/ps4/platform_ps4.cppindex de334fc..4cb8a86 100644--- a/src/platform/ps4/platform_ps4.cpp+++ b/src/platform/ps4/platform_ps4.cpp@@ -83,12 +83,8 @@ int ioctl(int fd, unsigned long request, ...) {arg = va_arg(ap, void*);va_end(ap);- if ((request == 0x8004667eUL || request == 0x5421 || request == FIONBIO) && arg) {- int on = *reinterpret_cast<const int*>(arg) ? 1 : 0;- int rc = sceNetSetsockopt(fd, 0xffff /*SOL_SOCKET*/, 0x1200 /*SO_NBIO*/, &on, sizeof(on));- if (rc == 0) return 0;- errno = getNetErrno(rc);- return -1;+ if (request == 0x5421) {+ request = 0x8004667eUL; // Translate Linux FIONBIO to FreeBSD/Orbis FIONBIO}// On PS4/shadPS4, socket ioctls like FIONBIO/FIONREAD must be routed to sceNetIoctl.@@ -109,7 +105,7 @@ int fcntl(int fd, int cmd, ...) {if (cmd == F_SETFL) {long flags = reinterpret_cast<intptr_t>(arg);int on = (flags & O_NONBLOCK) ? 1 : 0;- if (sceNetSetsockopt(fd, 0xffff /*SOL_SOCKET*/, 0x1200 /*SO_NBIO*/, &on, sizeof(on)) == 0) {+ if (sceNetIoctl(fd, 0x8004667eUL /*FIONBIO*/, &on) >= 0) {return 0;}} else if (cmd == F_GETFL) {@@ -149,7 +145,6 @@ int pthread_create(pthread_t* thread, const pthread_attr_t* attr,if (!attr) {if (pthread_attr_init(&customAttr) == 0) {pthread_attr_setstacksize(&customAttr, 1024 * 1024); // 1 MB stack- pthread_attr_setinheritsched(&customAttr, PTHREAD_EXPLICIT_SCHED);attr = &customAttr;createdAttr = true;}@@ -173,8 +168,8 @@ int pthread_create(pthread_t* thread, const pthread_attr_t* attr,if (rc != 0) {rc = scePthreadCreate(thread, attr, start_routine, arg, "xcloud_worker");if (rc < 0) {- rc = (-rc) & 0xFF;- if (rc == 0) rc = EAGAIN;+ uint32_t uret = static_cast<uint32_t>(rc);+ rc = ((uret & 0xFFFF0000) == 0x80020000) ? static_cast<int>(uret & 0xFFFF) : EAGAIN;}}@@ -202,7 +197,7 @@ int socket(int domain, int type, int protocol) {#endifif ((type & SOCK_NONBLOCK) || (type & 04000) || (type & 0x800) || (type & 0x4000) || (type & 0x20000000)) {int nbio = 1;- sceNetSetsockopt(s, 0xffff, 0x1200, &nbio, sizeof(nbio));+ sceNetIoctl(s, 0x8004667eUL /*FIONBIO*/, &nbio);}return s;}diff --git a/src/stream/stream_session.cpp b/src/stream/stream_session.cppindex 7794cd4..889d387 100644--- a/src/stream/stream_session.cpp+++ b/src/stream/stream_session.cpp@@ -572,6 +572,11 @@ struct StreamSession::Impl {bool start(std::string& err) {XC_LOGI("webrtc: start() calling createPeer");createPeer();+ if (!pc || failed) {+ std::lock_guard<std::mutex> lock(mutex);+ err = failReason.empty() ? "WebRTC PeerConnection initialization failed" : failReason;+ return false;+ }XC_LOGI("webrtc: calling pc->setLocalDescription");pc->setLocalDescription(rtc::Description::Type::Offer);XC_LOGI("webrtc: setLocalDescription returned; waiting for ICE gathering");