Skip to content

Commit

Permalink
(3DS) - remove provileged services enabling code, it should be done by
Browse files Browse the repository at this point in the history
the frontend instead.
- dynamically allocate the recompiler cache, this will allow using the
recompiler even when the .bss section is relocated far from the .text
section (for example when using the hombrew loader).
  • Loading branch information
aliaspider committed Jan 16, 2016
1 parent 0c840ff commit f72db18
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 117 deletions.
11 changes: 4 additions & 7 deletions Makefile.libretro
Expand Up @@ -142,20 +142,17 @@ else ifeq ($(platform), ctr)
CC = $(DEVKITARM)/bin/arm-none-eabi-gcc$(EXE_EXT)
CXX = $(DEVKITARM)/bin/arm-none-eabi-g++$(EXE_EXT)
AR = $(DEVKITARM)/bin/arm-none-eabi-ar$(EXE_EXT)
CFLAGS += -DARM11 -D_3DS -DNO_OS -DNO_DYLIB
CFLAGS += -march=armv6k -mtune=mpcore -mfloat-abi=hard -marm -mfpu=vfp
CFLAGS += -DARM11 -D_3DS -DNO_OS -DNO_DYLIB -DNO_SOCKET
CFLAGS += -march=armv6k -mtune=mpcore -mfloat-abi=hard -marm -mfpu=vfp -mtp=soft
CFLAGS += -Wall -mword-relocations
CFLAGS += -fomit-frame-pointer -ffast-math
CFLAGS += -I$(CTRULIB)/include -I$(DEVKITPRO)/portlibs/armv6k/include -Ifrontend/3ds
CFLAGS += -Ifrontend/3ds
CFLAGS += -Werror=implicit-function-declaration

OBJS += frontend/3ds/3ds_utils.o


# CFLAGS += -DPCSX
# BUILTIN_GPU = unai
USE_DYNAREC = 1
DRC_CACHE_BASE = 0
DRC_CACHE_BASE = 1
ARCH = arm

STATIC_LINKING = 1
Expand Down
74 changes: 0 additions & 74 deletions frontend/3ds/3ds_utils.c

This file was deleted.

60 changes: 55 additions & 5 deletions frontend/3ds/3ds_utils.h
@@ -1,16 +1,66 @@
#ifndef _3DS_UTILS_H
#define _3DS_UTILS_H

void ctr_invalidate_ICache(void);
void ctr_flush_DCache(void);
#include <stdio.h>

void ctr_flush_invalidate_cache(void);
#define MEMOP_PROT 6
#define MEMOP_MAP 4
#define MEMOP_UNMAP 5

int ctr_svchack_init(void);
void* linearMemAlign(size_t size, size_t alignment);
void linearFree(void* mem);

int32_t svcDuplicateHandle(uint32_t* out, uint32_t original);
int32_t svcCloseHandle(uint32_t handle);
int32_t svcControlMemory(void* addr_out, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm);
int32_t svcControlProcessMemory(uint32_t process, void* addr0, void* addr1, uint32_t size, uint32_t op, uint32_t perm);

int32_t svcCreateThread(int32_t* thread, void *(*entrypoint)(void*), void* arg, void* stack_top, int32_t thread_priority, int32_t processor_id);
int32_t svcWaitSynchronization(int32_t handle, int64_t nanoseconds);
void svcExitThread(void) __attribute__((noreturn));

int32_t svcBackdoor(int32_t (*callback)(void));

#include <stdio.h>
#define DEBUG_HOLD() do{printf("%s@%s:%d.\n",__FUNCTION__, __FILE__, __LINE__);fflush(stdout);wait_for_input();}while(0)

void wait_for_input(void);

extern __attribute__((weak)) int __ctr_svchax;

typedef int32_t (*ctr_callback_type)(void);

static inline void ctr_invalidate_ICache_kernel(void)
{
__asm__ volatile(
"cpsid aif\n\t"
"mov r0, #0\n\t"
"mcr p15, 0, r0, c7, c5, 0\n\t");
}

static inline void ctr_flush_DCache_kernel(void)
{
__asm__ volatile(
"cpsid aif\n\t"
"mov r0, #0\n\t"
"mcr p15, 0, r0, c7, c10, 0\n\t");
}

static inline void ctr_invalidate_ICache(void)
{
svcBackdoor((ctr_callback_type)ctr_invalidate_ICache_kernel);
}

static inline void ctr_flush_DCache(void)
{
svcBackdoor((ctr_callback_type)ctr_flush_DCache_kernel);
}


static inline void ctr_flush_invalidate_cache(void)
{
ctr_flush_DCache();
ctr_invalidate_ICache();
}


#endif // _3DS_UTILS_H
16 changes: 8 additions & 8 deletions frontend/3ds/pthread.h
Expand Up @@ -2,18 +2,18 @@
#ifndef _3DS_PTHREAD_WRAP__
#define _3DS_PTHREAD_WRAP__

#include "3ds.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "3ds_utils.h"

#define CTR_PTHREAD_STACK_SIZE 0x10000

typedef struct
{
Handle handle;
u32* stack;
int32_t handle;
uint32_t* stack;
}pthread_t;
typedef int pthread_attr_t;

Expand All @@ -23,8 +23,8 @@ static inline int pthread_create(pthread_t *thread,

thread->stack = linearMemAlign(CTR_PTHREAD_STACK_SIZE, 8);

svcCreateThread(&thread->handle, (ThreadFunc)start_routine,arg,
(u32*)((u32)thread->stack + CTR_PTHREAD_STACK_SIZE),
svcCreateThread(&thread->handle, start_routine, arg,
(uint32_t*)((uint32_t)thread->stack + CTR_PTHREAD_STACK_SIZE),
0x25, 1);

return 1;
Expand Down
69 changes: 55 additions & 14 deletions frontend/3ds/sys/mman.h
Expand Up @@ -5,28 +5,58 @@
extern "C" {
#endif

#include "3ds.h"
#include "stdlib.h"
#include "stdio.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <malloc.h>

#define PROT_READ MEMPERM_READ
#define PROT_WRITE MEMPERM_WRITE
#define PROT_EXEC MEMPERM_EXECUTE
#include "3ds_utils.h"

#define PROT_READ 0b001
#define PROT_WRITE 0b010
#define PROT_EXEC 0b100
#define MAP_PRIVATE 2
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20

#define MAP_FAILED ((void *)-1)

static void* dynarec_cache = NULL;
static void* dynarec_cache_mapping = NULL;

static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
{
(void)addr;
(void)prot;
(void)flags;
(void)fd;
(void)offset;

void* addr_out;

if((prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) &&
(flags == (MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS)))
{
if(__ctr_svchax)
{
/* this hack works only for pcsx_rearmed */
uint32_t currentHandle;

if(!dynarec_cache)
dynarec_cache = memalign(0x1000, len);

svcDuplicateHandle(&currentHandle, 0xFFFF8001);
svcControlProcessMemory(currentHandle, addr, dynarec_cache,
len, MEMOP_MAP, prot);
svcCloseHandle(currentHandle);
dynarec_cache_mapping = addr;
return addr;
}
else
{
printf("tried to mmap RWX pages without svcControlProcessMemory access !\n");
return MAP_FAILED;
}

}

addr_out = malloc(len);
if(!addr_out)
return MAP_FAILED;
Expand All @@ -36,13 +66,11 @@ static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, of

static inline int mprotect(void *addr, size_t len, int prot)
{
extern int ctr_svchack_init_success;

if(ctr_svchack_init_success)
if(__ctr_svchax)
{
uint32_t currentHandle;
svcDuplicateHandle(&currentHandle, 0xFFFF8001);
svcControlProcessMemory(currentHandle, (u32)addr, 0x0,
svcControlProcessMemory(currentHandle, addr, NULL,
len, MEMOP_PROT, prot);
svcCloseHandle(currentHandle);
return 0;
Expand All @@ -54,7 +82,20 @@ static inline int mprotect(void *addr, size_t len, int prot)

static inline int munmap(void *addr, size_t len)
{
free(addr);
if((addr == dynarec_cache_mapping) && __ctr_svchax)
{
uint32_t currentHandle;
svcDuplicateHandle(&currentHandle, 0xFFFF8001);
svcControlProcessMemory(currentHandle,
dynarec_cache, dynarec_cache_mapping,
len, MEMOP_UNMAP, 0b111);
svcCloseHandle(currentHandle);
dynarec_cache_mapping = NULL;

}
else
free(addr);

return 0;
}

Expand Down
15 changes: 6 additions & 9 deletions frontend/libretro.c
Expand Up @@ -29,9 +29,7 @@
#include "libretro.h"

#ifdef _3DS
#include "3ds.h"
#include "3ds/3ds_utils.h"
int ctr_svchack_init_success = 0;
#endif

static retro_video_refresh_t video_cb;
Expand Down Expand Up @@ -194,7 +192,7 @@ void* pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
(void)is_fixed;
(void)addr;

if (ctr_svchack_init_success)
if (__ctr_svchax)
{
psx_map_t* custom_map = custom_psx_maps;

Expand All @@ -207,7 +205,7 @@ void* pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
custom_map->buffer = malloc(size + 0x1000);
ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;

if(svcControlMemory(&tmp, custom_map->target_map, ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
if(svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
{
SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
exit(1);
Expand All @@ -225,7 +223,7 @@ void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
{
(void)tag;

if (ctr_svchack_init_success)
if (__ctr_svchax)
{
psx_map_t* custom_map = custom_psx_maps;

Expand All @@ -237,7 +235,7 @@ void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)

ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;

svcControlMemory(&tmp, custom_map->target_map, ptr_aligned, size, MEMOP_UNMAP, 0x3);
svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0x3);

free(custom_map->buffer);
custom_map->buffer = NULL;
Expand Down Expand Up @@ -1139,7 +1137,7 @@ static void update_variables(bool in_flight)
R3000Acpu *prev_cpu = psxCpu;

#ifdef _3DS
if(!ctr_svchack_init_success)
if(!__ctr_svchax)
Config.Cpu = CPU_INTERPRETER;
else
#endif
Expand Down Expand Up @@ -1385,14 +1383,13 @@ void retro_init(void)
bool found_bios = false;

#ifdef _3DS
ctr_svchack_init_success = ctr_svchack_init();
psxMapHook = pl_3ds_mmap;
psxUnmapHook = pl_3ds_munmap;
#endif
ret = emu_core_preinit();
#ifdef _3DS
/* emu_core_preinit sets the cpu to dynarec */
if(!ctr_svchack_init_success)
if(!__ctr_svchax)
Config.Cpu = CPU_INTERPRETER;
#endif
ret |= emu_core_init();
Expand Down

0 comments on commit f72db18

Please sign in to comment.