Skip to content

armgit / PS5LM

Public Imported from GitHub
0

llama.cpp on a jailbroken PS5: run any GGUF model natively on the console, starting with Qwen 3.8

Build: mbedTLS with threads, and tools that leave through _Exit

Mert Cobanov8be06411bd8c
diff --git a/ps5/CMakeLists.txt b/ps5/CMakeLists.txt
index 27c0426..cf0eacd 100644
--- a/ps5/CMakeLists.txt
+++ b/ps5/CMakeLists.txt
@@ -12,6 +12,15 @@ set(LLAMA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third_party/llama.cpp")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
add_subdirectory(${LLAMA_DIR} llama.cpp)
+# The tools leave through _Exit when main returns, skipping Sony's exit() and
+# the static destructors it runs (compat/exit.cpp, issue #3).
+foreach(tool llama-cli llama-server llama-bench llama-completion)
+ if(TARGET ${tool})
+ target_sources(${tool} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/compat/exit.cpp")
+ target_link_options(${tool} PRIVATE "LINKER:--wrap=main")
+ endif()
+endforeach()
+
# Files the payload carries inside itself (the pages and the CA bundle), as
# byte arrays in a generated header. Reconfigures when one of them changes.
function(ps5lm_embed out_header)
diff --git a/ps5/compat/exit.cpp b/ps5/compat/exit.cpp
new file mode 100644
index 0000000..b824f0c
--- /dev/null
+++ b/ps5/compat/exit.cpp
@@ -0,0 +1,31 @@
+// llama.cpp's tools leave through _Exit once main returns (issue #3).
+//
+// After main, the SDK's start code unloads the Sony modules the payload loaded
+// and calls Sony's exit() (ps5-payload-dev/sdk, crt/crt.c), which then runs
+// every C++ static destructor registered with __cxa_atexit while other threads
+// may still run. "[SceLibc] A heap error is detected" shows up at exit, after
+// the work is done, which points at this phase. Native PS5 apps never return
+// from main (ProsperoAI,
+// ps5-native-app-boilerplate); a payload can end with _Exit, as SDK payloads
+// did until 2024 (sdk commit f19dd3e).
+//
+// ps5/CMakeLists.txt links this into the tools with --wrap=main: the start
+// code's call to main lands here, and __real_main is the tool's own main.
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "log.h"
+
+#include <cstdio>
+#include <cstdlib>
+
+extern "C" int __real_main(int argc, char ** argv, char ** envp);
+extern "C" int __wrap_main(int argc, char ** argv, char ** envp);
+
+int __wrap_main(int argc, char ** argv, char ** envp) {
+ const int rc = __real_main(argc, argv, envp);
+ // llama.cpp logs through a worker thread: drain it, then stdio.
+ common_log_flush(common_log_main());
+ fflush(nullptr);
+ _Exit(rc);
+}
diff --git a/scripts/build-llama.sh b/scripts/build-llama.sh
index e4e9e5f..1fd4011 100755
--- a/scripts/build-llama.sh
+++ b/scripts/build-llama.sh
@@ -18,9 +18,8 @@ if [ ! -f "$llama/CMakeLists.txt" ]; then
echo "build-llama: run git submodule update --init first" >&2
exit 1
fi
-if [ ! -f "$mbedtls/lib/libmbedtls.a" ]; then
- "$root/scripts/build-mbedtls.sh"
-fi
+# Incremental: rebuilds mbedTLS only when its version or configuration changed.
+"$root/scripts/build-mbedtls.sh"
# The PS5 changes to llama.cpp live in patches/ and go onto the submodule's
# working tree; a patch that is already applied is skipped.
diff --git a/scripts/build-mbedtls.sh b/scripts/build-mbedtls.sh
index 49db41b..95ff9c2 100755
--- a/scripts/build-mbedtls.sh
+++ b/scripts/build-mbedtls.sh
@@ -26,6 +26,12 @@ fi
[ "$actual" = "$SHA256" ] || { echo "build-mbedtls: checksum mismatch" >&2; exit 1; }
[ -d "$src" ] || tar xjf "$tarball" -C "$deps"
+# ps5lm downloads over 16 connections at once, and TLS 1.3 handshakes share
+# PSA's global key store, which is only thread-safe with these two (mbedTLS
+# docs/architecture/psa-thread-safety; pacbrew's PS5 package turns them on too).
+python3 "$src/scripts/config.py" -f "$src/include/mbedtls/mbedtls_config.h" set MBEDTLS_THREADING_C
+python3 "$src/scripts/config.py" -f "$src/include/mbedtls/mbedtls_config.h" set MBEDTLS_THREADING_PTHREAD
+
"$PS5_PAYLOAD_SDK/bin/prospero-cmake" -S "$src" -B "$deps/mbedtls-build" -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="$out" \