Skip to content
This repository has been archived by the owner on Oct 13, 2019. It is now read-only.

Commit

Permalink
JIT
Browse files Browse the repository at this point in the history
  • Loading branch information
CTurt committed Jan 3, 2016
1 parent a9e15a8 commit e732672
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/jit/Makefile
@@ -0,0 +1,41 @@
LIBPS4 := $(PS4SDK)/libPS4

TEXT := 0x926200000
DATA := 0x926300000

CC := gcc
AS := gcc
OBJCOPY := objcopy
ODIR := build
SDIR := source
IDIRS := -I$(LIBPS4)/include -I. -Iinclude
LDIRS := -L$(LIBPS4) -L. -Llib
CFLAGS := $(IDIRS) -O2 -std=c11 -fno-builtin -nostartfiles -nostdlib -Wall -masm=intel -march=btver2 -mtune=btver2 -m64 -mabi=sysv -mcmodel=large -DTEXT_ADDRESS=$(TEXT) -DDATA_ADDRESS=$(DATA)
SFLAGS := -nostartfiles -nostdlib -march=btver2 -mtune=btver2
LFLAGS := $(LDIRS) -Xlinker -T $(LIBPS4)/linker.x -Wl,--build-id=none -Ttext=$(TEXT) -Tdata=$(DATA)
CFILES := $(wildcard $(SDIR)/*.c)
SFILES := $(wildcard $(SDIR)/*.s)
OBJS := $(patsubst $(SDIR)/%.c, $(ODIR)/%.o, $(CFILES)) $(patsubst $(SDIR)/%.s, $(ODIR)/%.o, $(SFILES))

LIBS := -lPS4

TARGET = $(shell basename $(CURDIR)).bin

$(TARGET): $(ODIR) $(OBJS)
$(CC) $(LIBPS4)/crt0.s $(ODIR)/*.o -o temp.t $(CFLAGS) $(LFLAGS) $(LIBS)
$(OBJCOPY) -O binary temp.t $(TARGET)
rm -f temp.t

$(ODIR)/%.o: $(SDIR)/%.c
$(CC) -c -o $@ $< $(CFLAGS)

$(ODIR)/%.o: $(SDIR)/%.s
$(AS) -c -o $@ $< $(SFLAGS)

$(ODIR):
@mkdir $@

.PHONY: clean

clean:
rm -f $(TARGET) $(ODIR)/*.o
57 changes: 57 additions & 0 deletions examples/jit/source/main.c
@@ -0,0 +1,57 @@
#include "ps4.h"

int sock;

#define printf(format, ...)\
do {\
char buffer[512];\
int size = sprintf(buffer, format, ##__VA_ARGS__);\
sceNetSend(sock, buffer, size, 0);\
} while(0)

int _main(void) {
initKernel();

initLibc();
initNetwork();
initJIT();


struct sockaddr_in server;

server.sin_len = sizeof(server);
server.sin_family = AF_INET;
server.sin_addr.s_addr = IP(192, 168, 0, 4);
server.sin_port = sceNetHtons(9023);
memset(server.sin_zero, 0, sizeof(server.sin_zero));

sock = sceNetSocket("debug", AF_INET, SOCK_STREAM, 0);
sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server));


void *executableAddress = NULL;
void *writableAddress = NULL;
void *dataAddress = NULL;

allocateJIT(PAGE_SIZE, &executableAddress, &writableAddress);
dataAddress = malloc(PAGE_SIZE);

printf("Executable address: %p\n", executableAddress);
printf("Writable address: %p\n", writableAddress);
printf("Data address: %p\n", dataAddress);

sceNetSocketClose(sock);


// writableAddress and executableAddress are 2 different virtual mappings, with different protections, but which point to the same physical memory


//unsigned char ret[] = { 0xc3 };
unsigned char loop[] = { 0xeb, 0xfe };
memcpy(writableAddress, loop, sizeof(loop));

((void (*)())executableAddress)();


return 0;
}
9 changes: 9 additions & 0 deletions libPS4/include/jit.h
@@ -0,0 +1,9 @@
#pragma once

extern int (*sceKernelJitCreateSharedMemory)(int flags, size_t size, int protection, int *destinationHandle);
extern int (*sceKernelJitCreateAliasOfSharedMemory)(int handle, int protection, int *destinationHandle);
extern int (*sceKernelJitMapSharedMemory)(int handle, int protection, void **destination);

void initJIT(void);

void allocateJIT(size_t size, void **executableAddress, void **writableAddress);
1 change: 1 addition & 0 deletions libPS4/include/ps4.h
Expand Up @@ -19,3 +19,4 @@
#include "camera.h"
#include "pad.h"
#include "graphics.h"
#include "jit.h"
31 changes: 31 additions & 0 deletions libPS4/source/jit.c
@@ -0,0 +1,31 @@
#include "kernel.h"
#include "module.h"
#include "memory.h"

#include "jit.h"

int (*sceKernelJitCreateSharedMemory)(int flags, size_t size, int protection, int *destinationHandle);
int (*sceKernelJitCreateAliasOfSharedMemory)(int handle, int protection, int *destinationHandle);
int (*sceKernelJitMapSharedMemory)(int handle, int protection, void **destination);

void initJIT(void) {
int libkernel;
loadModule("libkernel.sprx", &libkernel);

RESOLVE(libkernel, sceKernelJitCreateSharedMemory);
RESOLVE(libkernel, sceKernelJitCreateAliasOfSharedMemory);
RESOLVE(libkernel, sceKernelJitMapSharedMemory);
}

void allocateJIT(size_t size, void **executableAddress, void **writableAddress) {
int executableHandle;
int writableHandle;

sceKernelJitCreateSharedMemory(0, size, PROT_CPU_READ | PROT_CPU_WRITE | PROT_CPU_EXEC, &executableHandle);
sceKernelJitCreateAliasOfSharedMemory(executableHandle, PROT_CPU_READ | PROT_CPU_WRITE, &writableHandle);

*executableAddress = mmap(NULL, size, PROT_CPU_READ | PROT_CPU_EXEC, MAP_SHARED, executableHandle, 0);

//sceKernelJitMapSharedMemory(writableHandle, PROT_CPU_READ | PROT_CPU_WRITE, writableAddress);
*writableAddress = mmap(NULL, size, PROT_CPU_READ | PROT_CPU_WRITE, MAP_PRIVATE | MAP_TYPE, writableHandle, 0);
}

0 comments on commit e732672

Please sign in to comment.