Skip to content

Commit

Permalink
Merge pull request #27 from SonicMastr/v2.1
Browse files Browse the repository at this point in the history
Support for PSP Vita
  • Loading branch information
Brugarolas authored Jan 8, 2024
2 parents a3f7279 + c329ddd commit 2eae201
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,17 @@ else
ifeq (GNU/kFreeBSD,$(TARGET_SYS))
TARGET_XLIBS+= -ldl
endif
ifeq (PSP2,$(TARGET_SYS))
TARGET_XLIBS+= -ldl -ltaihen_stub -lSceKernelModulemgr_stub -lSceSblSsMgr_stub -lSceLibKernel_stub -lSceSysmodule_stub
endif
endif
endif
endif

ifneq ($(HOST_SYS),$(TARGET_SYS))
ifeq (PSP2,$(TARGET_SYS))
HOST_XCFLAGS+= -DLUAJIT_OS=LUAJIT_OS_PSP2 -DLUAJIT_USE_SYSMALLOC
else
ifeq (Windows,$(TARGET_SYS))
HOST_XCFLAGS+= -malign-double -DLUAJIT_OS=LUAJIT_OS_WINDOWS
else
Expand All @@ -373,6 +379,7 @@ ifneq ($(HOST_SYS),$(TARGET_SYS))
endif
endif
endif
endif
endif

ifneq (,$(CCDEBUG))
Expand Down Expand Up @@ -571,6 +578,9 @@ endif
ifeq (PS3,$(TARGET_SYS))
BUILDMODE= static
endif
ifeq (PSP2,$(TARGET_SYS))
BUILDMODE= static
endif

ifeq (Windows,$(HOST_SYS))
MINILUA_T= host/minilua.exe
Expand Down
2 changes: 1 addition & 1 deletion src/host/buildvm_asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void emit_asm(BuildCtx *ctx)
fprintf(ctx->fp, "\n");
switch (ctx->mode) {
case BUILD_elfasm:
#if !(LJ_TARGET_PS3 || LJ_TARGET_PSVITA)
#if !(LJ_TARGET_PS3 || LJ_TARGET_PSVITA || LJ_TARGET_PSP2)
fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n");
#endif
#if LJ_TARGET_PPC && !LJ_TARGET_PS3 && !LJ_ABI_SOFTFP
Expand Down
2 changes: 1 addition & 1 deletion src/lib_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ LJLIB_CF(io_popen)
LJLIB_CF(io_tmpfile)
{
IOFileUD *iof = io_file_new(L);
#if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX
#if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX || LJ_TARGET_PSP2
iof->fp = NULL; errno = ENOSYS;
#else
iof->fp = tmpfile();
Expand Down
4 changes: 2 additions & 2 deletions src/lib_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ LJLIB_CF(os_rename)

LJLIB_CF(os_tmpname)
{
#if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX
#if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PS5 || LJ_TARGET_PSVITA || LJ_TARGET_NX || LJ_TARGET_PSP2
lj_err_caller(L, LJ_ERR_OSUNIQF);
return 0;
#else
Expand All @@ -101,7 +101,7 @@ LJLIB_CF(os_tmpname)

LJLIB_CF(os_getenv)
{
#if LJ_TARGET_CONSOLE
#if LJ_TARGET_CONSOLE || LJ_TARGET_PSP2
lua_pushnil(L);
#else
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
Expand Down
2 changes: 1 addition & 1 deletion src/lib_package.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ static int lj_cf_package_seeall(lua_State *L)
static void setpath(lua_State *L, const char *fieldname, const char *envname,
const char *def, int noenv)
{
#if LJ_TARGET_CONSOLE
#if LJ_TARGET_CONSOLE || LJ_TARGET_PSP2
const char *path = NULL;
UNUSED(envname);
#else
Expand Down
23 changes: 16 additions & 7 deletions src/lj_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@

/* Target OS. */
#define LUAJIT_OS_OTHER 0
#define LUAJIT_OS_WINDOWS 1
#define LUAJIT_OS_LINUX 2
#define LUAJIT_OS_OSX 3
#define LUAJIT_OS_BSD 4
#define LUAJIT_OS_POSIX 5
#define LUAJIT_OS_PSP2 1
#define LUAJIT_OS_WINDOWS 2
#define LUAJIT_OS_LINUX 3
#define LUAJIT_OS_OSX 4
#define LUAJIT_OS_BSD 5
#define LUAJIT_OS_POSIX 6

/* Number mode. */
#define LJ_NUMMODE_SINGLE 0 /* Single-number mode only. */
Expand Down Expand Up @@ -74,7 +75,9 @@
/* Select native OS if no target OS defined. */
#ifndef LUAJIT_OS

#if defined(_WIN32) && !defined(_XBOX_VER)
#if defined(__vita__)
#define LUAJIT_OS LUAJIT_OS_PSP2
#elif defined(_WIN32) && !defined(_XBOX_VER)
#define LUAJIT_OS LUAJIT_OS_WINDOWS
#elif defined(__linux__)
#define LUAJIT_OS LUAJIT_OS_LINUX
Expand Down Expand Up @@ -113,16 +116,20 @@
#define LJ_OS_NAME "BSD"
#elif LUAJIT_OS == LUAJIT_OS_POSIX
#define LJ_OS_NAME "POSIX"
#elif LUAJIT_OS == LUAJIT_OS_PSP2
#define LJ_OS_NAME "PSP2"
#define LUAJIT_USE_SYSMALLOC 1
#else
#define LJ_OS_NAME "Other"
#endif

#define LJ_TARGET_PSP2 (LUAJIT_OS == LUAJIT_OS_PSP2)
#define LJ_TARGET_WINDOWS (LUAJIT_OS == LUAJIT_OS_WINDOWS)
#define LJ_TARGET_LINUX (LUAJIT_OS == LUAJIT_OS_LINUX)
#define LJ_TARGET_OSX (LUAJIT_OS == LUAJIT_OS_OSX)
#define LJ_TARGET_BSD (LUAJIT_OS == LUAJIT_OS_BSD)
#define LJ_TARGET_POSIX (LUAJIT_OS > LUAJIT_OS_WINDOWS)
#define LJ_TARGET_DLOPEN LJ_TARGET_POSIX
#define LJ_TARGET_DLOPEN (LJ_TARGET_POSIX || LJ_TARGET_PSP2)

#if TARGET_OS_IPHONE
#define LJ_TARGET_IOS 1
Expand Down Expand Up @@ -594,6 +601,8 @@

#if defined(LUAJIT_DISABLE_PROFILE)
#define LJ_HASPROFILE 0
#elif LJ_TARGET_PSP2
#define LJ_HASPROFILE 0
#elif LJ_TARGET_POSIX
#define LJ_HASPROFILE 1
#define LJ_PROFILE_SIGPROF 1
Expand Down
29 changes: 29 additions & 0 deletions src/lj_ccallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ static void *callback_mcode_init(global_State *g, uint32_t *page)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#elif LJ_TARGET_PSP2

#include <psp2/kernel/sysmem.h>

#elif LJ_TARGET_POSIX

#include <sys/mman.h>
Expand All @@ -275,6 +279,21 @@ static void callback_mcode_new(CTState *cts)
p = LJ_WIN_VALLOC(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
if (!p)
lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
#elif LJ_TARGET_PSP2
{
int fail;
SceUID block = sceKernelAllocMemBlockForVM("LuaJITCallbackCodeMemBlock", sz);
if (block < 0)
fail = 1;
else if (LJ_UNLIKELY(sceKernelGetMemBlockBase(block, &p) < 0))
fail = 1;
else if (sceKernelOpenVMDomain() < 0)
fail = 1;
else
fail = 0;
if (fail)
lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
}
#elif LJ_TARGET_POSIX
p = mmap(NULL, sz, (PROT_READ|PROT_WRITE|CCPROT_CREATE), MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0);
Expand All @@ -295,6 +314,8 @@ static void callback_mcode_new(CTState *cts)
DWORD oprot;
LJ_WIN_VPROTECT(p, sz, PAGE_EXECUTE_READ, &oprot);
}
#elif LJ_TARGET_PSP2
sceKernelCloseVMDomain();
#elif LJ_TARGET_POSIX
mprotect(p, sz, (PROT_READ|PROT_EXEC));
#endif
Expand All @@ -309,6 +330,14 @@ void lj_ccallback_mcode_free(CTState *cts)
#if LJ_TARGET_WINDOWS
VirtualFree(p, 0, MEM_RELEASE);
UNUSED(sz);
#elif LJ_TARGET_PSP2
{
SceUID block;
void *base = (void *)((uintptr_t)p & ~(uintptr_t)0xFFFFF);
block = sceKernelFindMemBlockByAddr(base, 0);
sceKernelFreeMemBlock(block);
UNUSED(sz);
}
#elif LJ_TARGET_POSIX
munmap(p, sz);
#else
Expand Down
4 changes: 4 additions & 0 deletions src/lj_clib.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L)

#if LJ_TARGET_CYGWIN
#define CLIB_SOPREFIX "cyg"
#elif LJ_TARGET_PSP2
#define CLIB_SOPREFIX ""
#else
#define CLIB_SOPREFIX "lib"
#endif
Expand All @@ -50,6 +52,8 @@ LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L)
#define CLIB_SOEXT "%s.dylib"
#elif LJ_TARGET_CYGWIN
#define CLIB_SOEXT "%s.dll"
#elif LJ_TARGET_PSP2
#define CLIB_SOEXT "%s.suprx"
#else
#define CLIB_SOEXT "%s.so"
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/lj_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ typedef unsigned int uintptr_t;
#define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */
#define LJ_STACK_EXTRA (5+3*LJ_FR2) /* Extra stack space (metamethods). */

#if LJ_TARGET_PSP2
#define LJ_NUM_CBPAGE 256 /* Number of FFI callback pages. */
#else
#define LJ_NUM_CBPAGE 1 /* Number of FFI callback pages. */
#endif

/* Minimum table/buffer sizes. */
#define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */
Expand Down
7 changes: 6 additions & 1 deletion src/lj_jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,19 @@
/* Note: FMA is not set by default. */

/* -- JIT engine parameters ----------------------------------------------- */

#if LJ_TARGET_PSP2
#define JIT_P_sizemcode_DEFAULT 1024
#define JIT_P_maxmcode_DEFAULT 8192
#else
#define JIT_P_maxmcode_DEFAULT 512
#if LJ_TARGET_WINDOWS || LJ_64
/* See: https://devblogs.microsoft.com/oldnewthing/20031008-00/?p=42223 */
#define JIT_P_sizemcode_DEFAULT 64
#else
/* Could go as low as 4K, but the mmap() overhead would be rather high. */
#define JIT_P_sizemcode_DEFAULT 32
#endif
#endif

/* Optimization parameters and their defaults. Length is a char in octal! */
#define JIT_PARAMDEF(_) \
Expand Down
78 changes: 76 additions & 2 deletions src/lj_mcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@
void sys_icache_invalidate(void *start, size_t len);
#endif

#if LJ_TARGET_PSP2
#include <psp2/kernel/sysmem.h>
#endif

/* Synchronize data/instruction cache. */
void lj_mcode_sync(void *start, void *end)
{
#ifdef LUAJIT_USE_VALGRIND
VALGRIND_DISCARD_TRANSLATIONS(start, (char *)end-(char *)start);
#endif
#if LJ_TARGET_X86ORX64
#if LJ_TARGET_PSP2
SceUID block;
void *base = (void *)((uintptr_t)start & ~(uintptr_t)0xFFFFF);
block = sceKernelFindMemBlockByAddr(base, 0);
/* Removed Assertion */
sceKernelSyncVMDomain(block, start, (char *)end-(char *)start);
#elif LJ_TARGET_X86ORX64
UNUSED(start); UNUSED(end);
#elif LJ_TARGET_WINDOWS
FlushInstructionCache(GetCurrentProcess(), start, (char *)end-(char *)start);
Expand All @@ -63,7 +73,58 @@ void lj_mcode_sync(void *start, void *end)

#if LJ_HASJIT

#if LJ_TARGET_WINDOWS
#if LJ_TARGET_PSP2

#define LUAJIT_PSP2PROTECT_MCODE
#define MCPROT_RX 0
#define MCPROT_RWX 1

/*
** NYI: proper fix of the potential race condition.
** Currently there's only a dirty hack on lj_mcode_patch.
*/
static int mcode_setprot(void *p, size_t sz, int prot)
{
UNUSED(p); UNUSED(sz);
if (MCPROT_RX == prot)
return sceKernelCloseVMDomain();
else
return sceKernelOpenVMDomain();
}

static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)
{
void *p;
int fail;
SceUID block = sceKernelAllocMemBlockForVM("LuaJITCodeMemBlock", sz);
if (block < 0) {
fail = 1;
} else if (LJ_UNLIKELY(sceKernelGetMemBlockBase(block, &p) < 0)) {
sceKernelFreeMemBlock(block);
fail = 1;
} else if (mcode_setprot(p, sz, prot)) {
sceKernelFreeMemBlock(block);
fail = 1;
} else {
fail = 0;
}
if (fail) {
if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL);
p = NULL;
}
return p;
}

static void mcode_free(jit_State *J, void *p, size_t sz)
{
SceUID block;
void *base = (void *)((uintptr_t)p & ~(uintptr_t)0xFFFFF);
block = sceKernelFindMemBlockByAddr(base, 0);
/* Removed Assertion */
sceKernelFreeMemBlock(block);
}

#elif LJ_TARGET_WINDOWS

#define MCPROT_RW PAGE_READWRITE
#define MCPROT_RX PAGE_EXECUTE_READ
Expand Down Expand Up @@ -158,6 +219,12 @@ static void mcode_protect(jit_State *J, int prot)
UNUSED(J); UNUSED(prot); UNUSED(mcode_setprot);
}

#else
#if defined(LUAJIT_PSP2PROTECT_MCODE)

#define MCPROT_GEN MCPROT_RWX
#define MCPROT_RUN MCPROT_RX

#else

/* This is the default behaviour and much safer:
Expand All @@ -171,6 +238,8 @@ static void mcode_protect(jit_State *J, int prot)
#define MCPROT_GEN MCPROT_RW
#define MCPROT_RUN MCPROT_RX

#endif

/* Protection twiddling failed. Probably due to kernel security. */
static LJ_NORET LJ_NOINLINE void mcode_protfail(jit_State *J)
{
Expand Down Expand Up @@ -331,7 +400,12 @@ MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish)
#if LUAJIT_SECURITY_MCODE
if (J->mcarea == ptr)
mcode_protect(J, MCPROT_RUN);
#ifdef LUAJIT_PSP2PROTECT_MCODE
/* Restore previous state of the current (top) MCode area */
else if (LJ_UNLIKELY(mcode_setprot(NULL, 0, J->mcprot)))
#else
else if (LJ_UNLIKELY(mcode_setprot(ptr, ((MCLink *)ptr)->size, MCPROT_RUN)))
#endif
mcode_protfail(J);
#endif
return NULL;
Expand Down
9 changes: 9 additions & 0 deletions src/lj_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ extern int sceRandomGetRandomNumber(void *buf, size_t len);

#include <unistd.h>

#elif LJ_TARGET_PSP2

extern int sceKernelGetRandomNumber(void *buf, size_t len);

#elif LJ_TARGET_WINDOWS || LJ_TARGET_XBOXONE

#define WIN32_LEAN_AND_MEAN
Expand Down Expand Up @@ -185,6 +189,11 @@ int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs)
if (getentropy(rs->u, sizeof(rs->u)) == 0)
goto ok;

#elif LJ_TARGET_PSP2

if (sceKernelGetRandomNumber(rs->u, sizeof(rs->u)) == 0)
goto ok;

#elif LJ_TARGET_UWP || LJ_TARGET_XBOXONE

if (BCryptGenRandom(NULL, (PUCHAR)(rs->u), (ULONG)sizeof(rs->u),
Expand Down

0 comments on commit 2eae201

Please sign in to comment.