Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOfficialFloW committed Jan 23, 2021
0 parents commit 48cd1bd
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
40 changes: 40 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 2.8)

if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(DEFINED ENV{VITASDK})
set(CMAKE_TOOLCHAIN_FILE "$ENV{VITASDK}/share/vita.toolchain.cmake" CACHE PATH "toolchain file")
else()
message(FATAL_ERROR "Please define VITASDK to point to your SDK path!")
endif()
endif()

project(kubridge)
include("${VITASDK}/share/vita.cmake" REQUIRED)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-q -Wall -O3 -nostdlib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")

add_executable(kubridge
main.c
)

target_link_libraries(kubridge
taihenForKernel_stub
taihenModuleUtils_stub
SceSysclibForDriver_stub
SceSysmemForDriver_stub
SceThreadmgrForDriver_stub
)

vita_create_self(kubridge.skprx kubridge CONFIG exports.yml UNSAFE)

vita_create_stubs(stubs kubridge ${CMAKE_SOURCE_DIR}/exports.yml KERNEL)

install(DIRECTORY ${CMAKE_BINARY_DIR}/stubs/
DESTINATION lib
FILES_MATCHING PATTERN "*.a"
)

install(FILES kubridge.h
DESTINATION include
)
15 changes: 15 additions & 0 deletions exports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
kubridge:
attributes: 0
version:
major: 1
minor: 0
main:
start: module_start
stop: module_stop
modules:
kubridge:
syscall: true
functions:
- kuKernelAllocMemBlock
- kuKernelFlushCaches
- kuKernelCpuUnrestrictedMemcpy
59 changes: 59 additions & 0 deletions kubridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef __KUBRDIGE_H__
#define __KUBRDIGE_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <psp2/types.h>
#include <psp2/kernel/sysmem.h>

typedef struct SceKernelAddrPair {
uint32_t addr; //!< Address
uint32_t length; //!< Length
} SceKernelAddrPair;

typedef struct SceKernelPaddrList {
uint32_t size; //!< sizeof(SceKernelPaddrList)
uint32_t list_size; //!< Size in elements of the list array
uint32_t ret_length; //!< Total physical size of the memory pairs
uint32_t ret_count; //!< Number of elements of list filled by ksceKernelGetPaddrList
SceKernelAddrPair *list; //!< Array of physical addresses and their lengths pairs
} SceKernelPaddrList;

typedef struct SceKernelAllocMemBlockKernelOpt {
SceSize size; //!< sizeof(SceKernelAllocMemBlockKernelOpt)
SceUInt32 field_4;
SceUInt32 attr; //!< OR of SceKernelAllocMemBlockAttr
SceUInt32 field_C;
SceUInt32 paddr;
SceSize alignment;
SceUInt32 extraLow;
SceUInt32 extraHigh;
SceUInt32 mirror_blockid;
SceUID pid;
SceKernelPaddrList *paddr_list;
SceUInt32 field_2C;
SceUInt32 field_30;
SceUInt32 field_34;
SceUInt32 field_38;
SceUInt32 field_3C;
SceUInt32 field_40;
SceUInt32 field_44;
SceUInt32 field_48;
SceUInt32 field_4C;
SceUInt32 field_50;
SceUInt32 field_54;
} SceKernelAllocMemBlockKernelOpt;

SceUID kuKernelAllocMemBlock(const char *name, SceKernelMemBlockType type, SceSize size, SceKernelAllocMemBlockKernelOpt *opt);

void kuKernelFlushCaches(const void *ptr, SceSize len);

int kuKernelCpuUnrestrictedMemcpy(void *dst, const void *src, SceSize len);

#ifdef __cplusplus
}
#endif

#endif
84 changes: 84 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <psp2kern/kernel/cpu.h>
#include <psp2kern/kernel/modulemgr.h>
#include <psp2kern/kernel/sysmem.h>
#include <psp2kern/kernel/threadmgr.h>
#include <taihen.h>

int module_get_export_func(SceUID pid, const char *modname, uint32_t libnid, uint32_t funcnid, uintptr_t *func);

void (* _ksceKernelCpuDcacheWritebackInvalidateRange)(const void *ptr, SceSize len);
void (* _ksceKernelCpuIcacheInvalidateRange)(const void *ptr, SceSize len);
void (* _ksceKernelCpuIcacheAndL2WritebackInvalidateRange)(const void *ptr, SceSize len);

void kuKernelFlushCaches(const void *ptr, SceSize len) {
uintptr_t ptr_aligned;
ptr_aligned = (uintptr_t)ptr & ~0x1F;
len = (((uintptr_t)ptr + len + 0x1F) & ~0x1F) - ptr_aligned;
_ksceKernelCpuDcacheWritebackInvalidateRange((void *)ptr_aligned, len);
// _ksceKernelCpuIcacheAndL2WritebackInvalidateRange((void *)ptr_aligned, len);
_ksceKernelCpuIcacheInvalidateRange((void *)ptr_aligned, len);
}

SceUID kuKernelAllocMemBlock(const char *name, SceKernelMemBlockType type, SceSize size, SceKernelAllocMemBlockKernelOpt *opt) {
char k_name[32];
SceKernelAllocMemBlockKernelOpt k_opt;
uint32_t state;
int res;

ENTER_SYSCALL(state);

res = ksceKernelStrncpyUserToKernel(k_name, (uintptr_t)name, sizeof(k_name));
if (res < 0)
goto error;

res = ksceKernelMemcpyUserToKernel(&k_opt, (uintptr_t)opt, sizeof(k_opt));
if (res < 0)
goto error;

res = ksceKernelAllocMemBlock(k_name, type, size, &k_opt);
if (res < 0)
goto error;

res = ksceKernelCreateUserUid(ksceKernelGetProcessId(), res);

error:
EXIT_SYSCALL(state);
return res;
}

int kuKernelCpuUnrestrictedMemcpy(void *dst, const void *src, SceSize len) {
int prev_dacr;

asm volatile("mrc p15, 0, %0, c3, c0, 0" : "=r" (prev_dacr));
asm volatile("mcr p15, 0, %0, c3, c0, 0" :: "r" (0x15450FC3));

for (int i = 0; i < len; i++) {
uint32_t val;
__asm__ volatile ("ldrbt %0, [%1]" : "=r" (val) : "r" (src + i));
__asm__ volatile ("strbt %0, [%1]" :: "r" (val), "r" (dst + i));
}

asm volatile("mcr p15, 0, %0, c3, c0, 0" :: "r" (prev_dacr));
return 0;
}

void _start() __attribute__ ((weak, alias("module_start")));
int module_start(SceSize args, void *argp) {
int res;

res = module_get_export_func(KERNEL_PID, "SceSysmem", TAI_ANY_LIBRARY, 0x6BA2E51C, (uintptr_t *)&_ksceKernelCpuDcacheWritebackInvalidateRange);
if (res < 0)
module_get_export_func(KERNEL_PID, "SceSysmem", TAI_ANY_LIBRARY, 0x4F442396, (uintptr_t *)&_ksceKernelCpuDcacheWritebackInvalidateRange);
res = module_get_export_func(KERNEL_PID, "SceSysmem", TAI_ANY_LIBRARY, 0xF4C7F578, (uintptr_t *)&_ksceKernelCpuIcacheInvalidateRange);
if (res < 0)
module_get_export_func(KERNEL_PID, "SceSysmem", TAI_ANY_LIBRARY, 0x2E637B1D, (uintptr_t *)&_ksceKernelCpuIcacheInvalidateRange);
res = module_get_export_func(KERNEL_PID, "SceSysmem", TAI_ANY_LIBRARY, 0x19F17BD0, (uintptr_t *)&_ksceKernelCpuIcacheAndL2WritebackInvalidateRange);
if (res < 0)
module_get_export_func(KERNEL_PID, "SceSysmem", TAI_ANY_LIBRARY, 0x73E895EA, (uintptr_t *)&_ksceKernelCpuIcacheAndL2WritebackInvalidateRange);

return SCE_KERNEL_START_SUCCESS;
}

int module_stop(SceSize args, void *argp) {
return SCE_KERNEL_STOP_SUCCESS;
}

0 comments on commit 48cd1bd

Please sign in to comment.