Skip to content

Commit

Permalink
Adding in luajit
Browse files Browse the repository at this point in the history
  • Loading branch information
capsterx committed Nov 5, 2020
1 parent 3dd8d5a commit 154331c
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 12 deletions.
33 changes: 23 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ TARGET := solarus
BUILD := build
SOURCES := src src/entities src/audio src/hero src/movements src/graphics src/graphics/sdlrenderer \
src/graphics/glrenderer src/third_party src/third_party/hqx \
src/third_party/mojoAL src/third_party/lua src/third_party/snes_spc src/lua src/main src/core
src/third_party/mojoAL src/third_party/snes_spc src/lua src/main src/core
DATA := data
INCLUDES := include include/solarus include/solarus/core include/solarus/hero include/solarus/entities \
include/solarus/lowlevel include/solarus/lua include/solarus/movements \
include/solarus/containers include/solarus/third_party/snes_spc \
include/solarus/third_party/mojoAL/AL include/solarus/third_party/lua \
include/solarus/third_party/mojoAL/AL \
include/solarus/third_party
EXEFS_SRC := exefs_src

APP_TITLE := Solarus Engine
APP_AUTHOR := Solarus Team & carstene1ns
APP_AUTHOR := Solarus Team & capsterx
APP_VERSION := 1.6.4.
#ROMFS := romfs
ICON := solarus.jpg
Expand All @@ -60,12 +60,20 @@ CFLAGS += $(INCLUDE) -D__SWITCH__ -DCOCO_DISABLE
CXXFLAGS := $(CFLAGS) -frtti -fexceptions -std=c++11

ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

LIBS := -lSDL2_ttf -lglapi -lfreetype -lbz2 -lSDL2_image -lpng -lz -ljpeg -lSDL2 \
-lphysfs -lmodplug -lvorbisfile -lvorbis -logg \
-lwebp -lEGL -lglapi -ldrm_nouveau -lnx
#SWITCH_GUI := 1
-lwebp -lEGL -lglapi -ldrm_nouveau -lnx
ifneq ($(strip $(LUAJIT)),)
INCLUDES += src/solarus/third_party/luajit/src
LDFLAGS += -L$(TOPDIR)/src/third_party/luajit/src
LIBS += -lluajit -lnx
SOLARUS_LUA_JIT:=luajit_make
else
SOURCES += src/third_party/lua
INCLUDE += include/solarus/third_party/lua
endif


#---------------------------------------------------------------------------------
Expand Down Expand Up @@ -192,7 +200,7 @@ endif
.PHONY: $(BUILD) clean all

#---------------------------------------------------------------------------------
all: $(BUILD)
all: $(BUILD)

$(BUILD):
@[ -d $@ ] || mkdir -p $@
Expand All @@ -208,11 +216,13 @@ switch_gui:
@$(MAKE) -C Plutonium




#---------------------------------------------------------------------------------
else
.PHONY: all switch_gui
.PHONY: all switch_gui

DEPENDS := $(OFILES:.o=.d) $(SOLARUS_SWITCH_GUI)
DEPENDS := $(OFILES:.o=.d) $(SOLARUS_SWITCH_GUI)

#---------------------------------------------------------------------------------
# main targets
Expand All @@ -229,7 +239,7 @@ else
$(OUTPUT).nro : $(OUTPUT).elf
endif

$(OUTPUT).elf : $(OFILES)
$(OUTPUT).elf : $(OFILES) $(SOLARUS_LUA_JIT)

# make compiling silent
%.o: %.cpp
Expand All @@ -246,6 +256,9 @@ $(OUTPUT).elf : $(OFILES)
# $(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@
# $(NM) -CSn $@ > $(notdir $*.lst)

luajit_make:
@$(MAKE) -C $(TOPDIR)/src/third_party/luajit TARGET_SYS=Switch

launcher: $(OUTPUT).elf
nacptool --create $(EMBEDED_TITLE) $(EMBEDED_AUTHOR) $(EMBEDED_VERSION) $(TOPDIR)/$(EMBEDED_TARGET).nacp
echo built ... $(EMBEDED_TARGET).nacp
Expand Down
17 changes: 16 additions & 1 deletion src/third_party/luajit/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ CCWARN= -Wall
# as dynamic mode.
#
# Mixed mode creates a static + dynamic library and a statically linked luajit.
BUILDMODE= mixed
BUILDMODE= static
#
# Static mode creates a static library and a statically linked luajit.
#BUILDMODE= static
Expand Down Expand Up @@ -173,6 +173,18 @@ else
endif
endif


ifeq (Switch,$(TARGET_SYS))
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
HOST_CC:=$(CC)
CROSS:=$(DEVKITPRO)/devkitA64/bin/aarch64-none-elf-
BUILDMODE=static
TARGET_FLAGS:=-march=armv8-a -mtp=soft -fPIE --sysroot $(DEVKITPRO)/devkitA64/aarch64-none-elf -DLJ_NO_SYSTEM=1 -DLUAJIT_NO_LOG2=1 -DLJ_TARGET_SWITCH -I$(DEVKITPRO)/libnx/include/ -DLUAJIT_USE_SYSMALLOC -L$(DEVKITPRO)/libnx/lib -DLJ_HASPROFILE=0 -D__SWITCH__=1 -specs=$(DEVKITPRO)/libnx/switch.specs
#TARGET_FLAGS:=-march=armv8-a -mtp=soft -fPIE --sysroot $(DEVKITPRO)/devkitA64/aarch64-none-elf -DLUAJIT_SECURITY_PRNG=0 -DLJ_NO_SYSTEM=1 -DLUAJIT_NO_LOG2=1 -DLJ_TARGET_SWITCH -I$(DEVKITPRO)/libnx/include/ -DLUAJIT_USE_SYSMALLOC -L$(DEVKITPRO)/libnx/lib -DLJ_HASPROFILE=0 -D__SWITCH__=1 -specs=$(DEVKITPRO)/libnx/switch.specs
endif

##############################################################################
# Flags and options for host and target.
##############################################################################
Expand Down Expand Up @@ -347,6 +359,9 @@ else
ifeq (GNU/kFreeBSD,$(TARGET_SYS))
TARGET_XLIBS+= -ldl
endif
ifeq (Switch,$(TARGET_SYS))
TARGET_XLIBS+= -lnx
endif
endif
endif
endif
Expand Down
93 changes: 92 additions & 1 deletion src/third_party/luajit/src/lj_mcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

/* -- OS-specific functions ----------------------------------------------- */


#if LJ_HASJIT || LJ_HASFFI

/* Define this if you want to run LuaJIT with Valgrind. */
Expand Down Expand Up @@ -119,10 +120,100 @@ static int mcode_setprot(void *p, size_t sz, int prot)
return mprotect(p, sz, prot);
}

#elif defined(LJ_TARGET_SWITCH)

#include <switch/kernel/svc.h>
#include <switch/runtime/env.h>
#define LJ_MCBOTTOM_OFFSET sizeof(void*)
void *memalign(size_t alignment, size_t size);

#define MCPROT_RW (Perm_Rw)
#define MCPROT_RX (Perm_Rx)

void * get_orig_addr(void * ptr)
{
return (char *)ptr + sizeof(MCLink);
}

void * get_orig(void * ptr)
{
void * orig;
memcpy(&orig, get_orig_addr(ptr), sizeof(void *));
return orig;
}

static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)
{
void * p = memalign(0x1000, sz);
memcpy(get_orig_addr(p), &p, sizeof(void*));

MemoryInfo info = {0};
u32 pageInfo = {0};
svcQueryMemory(&info, &pageInfo, hint);
if (info.type != MemType_Unmapped)
{
return NULL;
}
bool succeded = R_SUCCEEDED(svcMapProcessCodeMemory(envGetOwnProcessHandle(), hint, (u64)p, sz));
if (!succeded)
{
free(p);
return NULL;
}
succeded = R_SUCCEEDED(svcSetProcessMemoryPermission(envGetOwnProcessHandle(), hint, sz, prot));
if (!succeded)
{
svcUnmapProcessCodeMemory(envGetOwnProcessHandle(), hint, (u64)p, sz);
free(p);
return NULL;
}

return (void*)hint;
}

static void mcode_free(jit_State *J, void *p, size_t sz)
{
void * orig_p=get_orig_addr(p);
svcUnmapProcessCodeMemory(envGetOwnProcessHandle(), (u64)p, (u64)orig_p, sz);
free(orig_p);
}

static int mcode_setprot(void *p, size_t sz, int prot)
{
void * orig_p=get_orig_addr(p);
bool succeded=false;
// Switch lets us change from RW -> RX but not RX -> RW, unmap and map again
if (prot == MCPROT_RX)
{
succeded = R_SUCCEEDED(svcUnmapProcessCodeMemory(envGetOwnProcessHandle(), (u64)p, (u64)orig_p, sz));
if (!succeded)
{
return 1;
}

succeded = R_SUCCEEDED(svcMapProcessCodeMemory(envGetOwnProcessHandle(), (u64)p, (u64)orig_p, sz));
if (!succeded)
{
return 1;
}
}

succeded = R_SUCCEEDED(svcSetProcessMemoryPermission(envGetOwnProcessHandle(), (u64)p, sz, prot));
if (!succeded)
{
return 1;
}
return 0;
}


#else

#error "Missing OS support for explicit placement of executable memory"
#endif

#ifndef LJ_MCBOTTOM_OFFSET
#define LJ_MCBOTTOM_OFFSET 0
#endif

/* -- MCode area protection ----------------------------------------------- */
Expand Down Expand Up @@ -265,7 +356,7 @@ static void mcode_allocarea(jit_State *J)
J->szmcarea = sz;
J->mcprot = MCPROT_GEN;
J->mctop = (MCode *)((char *)J->mcarea + J->szmcarea);
J->mcbot = (MCode *)((char *)J->mcarea + sizeof(MCLink));
J->mcbot = (MCode *)((char *)J->mcarea + sizeof(MCLink) + LJ_MCBOTTOM_OFFSET);
((MCLink *)J->mcarea)->next = oldarea;
((MCLink *)J->mcarea)->size = sz;
J->szallmcarea += sz;
Expand Down
7 changes: 7 additions & 0 deletions src/third_party/luajit/src/lj_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include "lj_arch.h"
#include "lj_prng.h"

#if LJ_TARGET_SWITCH
void randomGet(void* buf, size_t len);
#endif

/* -- PRNG step function -------------------------------------------------- */

/* This implements a Tausworthe PRNG with period 2^223. Based on:
Expand Down Expand Up @@ -221,6 +225,9 @@ int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs)
}
}

#elif LJ_TARGET_SWITCH
randomGet(rs->u, sizeof(rs->u));
goto ok;
#else

/* Add an elif above for your OS with a secure PRNG seed.
Expand Down

0 comments on commit 154331c

Please sign in to comment.