Skip to content

Commit

Permalink
Refactor exception handling
Browse files Browse the repository at this point in the history
Refactored exception handling system.

Added support for undefined instruction exceptions.

Deprecated abort handler entrypoints and behaviour.
  • Loading branch information
bythos14 committed Oct 29, 2022
1 parent 97b502d commit 52d0e46
Show file tree
Hide file tree
Showing 9 changed files with 543 additions and 415 deletions.
10 changes: 2 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ if(DEFINED EXCEPTION_SAFETY)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DEXCEPTION_SAFETY=${EXCEPTION_SAFETY}")
endif(DEFINED EXCEPTION_SAFETY)

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/exceptions_user.h
COMMAND arm-vita-eabi-as ${CMAKE_CURRENT_SOURCE_DIR}/src/exceptions_user.S -o exceptions_user.o
COMMAND arm-vita-eabi-objcopy -O binary exceptions_user.o exceptions_user.bin
COMMAND xxd -i exceptions_user.bin ${CMAKE_CURRENT_BINARY_DIR}/exceptions_user.h
COMMAND rm -f exceptions_user.o exceptions_user.bin
DEPENDS src/exceptions_user.S)

add_executable(kubridge
src/main.c
src/exceptions.c
src/exceptions.S
${CMAKE_CURRENT_BINARY_DIR}/exceptions_user.h
src/exceptions_bootstrap.S
src/mprotect.c
src/rwx_patch.S
)
Expand All @@ -48,6 +41,7 @@ target_link_libraries(kubridge
SceThreadmgrForDriver_stub
SceDebugForDriver_stub
SceSysrootForKernel_stub
SceProcessmgrForDriver_stub
)

vita_create_self(kubridge.skprx kubridge CONFIG exports.yml UNSAFE)
Expand Down
8 changes: 6 additions & 2 deletions exports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ kubridge:
- kuKernelAllocMemBlock: 0x2EF7C290
- kuKernelFlushCaches: 0x38B70744
- kuKernelCpuUnrestrictedMemcpy: 0x91D9CABC
- kuKernelRegisterAbortHandler: 0x799f5648
- kuKernelReleaseAbortHandler: 0xb188292d
- kuKernelMemProtect: 0x566d2af1
- kuKernelMemReserve: 0xced3608c
- kuKernelMemCommit: 0x9c0cd758
- kuKernelMemDecommit: 0x9bdf5881
- kuKernelRegisterExceptionHandler: 0xf9025d19
- kuKernelReleaseExceptionHandler: 0xdf9f3bed

# Deprecated
- kuKernelRegisterAbortHandler: 0x799f5648
- kuKernelReleaseAbortHandler: 0xb188292d
116 changes: 79 additions & 37 deletions kubridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,41 @@ extern "C" {

#define KU_KERNEL_MEM_COMMIT_ATTR_HAS_BASE (0x1)

#define KU_KERNEL_ABORT_TYPE_DATA_ABORT 0
#define KU_KERNEL_ABORT_TYPE_PREFETCH_ABORT 1

typedef struct KuKernelAbortContext
{
SceUInt32 r0;
SceUInt32 r1;
SceUInt32 r2;
SceUInt32 r3;
SceUInt32 r4;
SceUInt32 r5;
SceUInt32 r6;
SceUInt32 r7;
SceUInt32 r8;
SceUInt32 r9;
SceUInt32 r10;
SceUInt32 r11;
SceUInt32 r12;
SceUInt32 sp;
SceUInt32 lr;
SceUInt32 pc;
SceUInt64 vfpRegisters[32];
SceUInt32 SPSR;
SceUInt32 FPSCR;
SceUInt32 FPEXC;
SceUInt32 FSR;
SceUInt32 FAR;
SceUInt32 abortType;
} KuKernelAbortContext;

typedef void (*KuKernelAbortHandler)(KuKernelAbortContext *);

typedef struct KuKernelAbortHandlerOpt {
SceSize size; //!< Size of structure
} KuKernelAbortHandlerOpt;
#define KU_KERNEL_EXCEPTION_TYPE_DATA_ABORT 0
#define KU_KERNEL_EXCEPTION_TYPE_PREFETCH_ABORT 1
#define KU_KERNEL_EXCEPTION_TYPE_UNDEFINED_INSTRUCTION 2

typedef struct KuKernelExceptionContext {
SceUInt32 r0;
SceUInt32 r1;
SceUInt32 r2;
SceUInt32 r3;
SceUInt32 r4;
SceUInt32 r5;
SceUInt32 r6;
SceUInt32 r7;
SceUInt32 r8;
SceUInt32 r9;
SceUInt32 r10;
SceUInt32 r11;
SceUInt32 r12;
SceUInt32 sp;
SceUInt32 lr;
SceUInt32 pc;
SceUInt64 vfpRegisters[32];
SceUInt32 SPSR;
SceUInt32 FPSCR;
SceUInt32 FPEXC;
SceUInt32 FSR;
SceUInt32 FAR;
SceUInt32 exceptionType;
} KuKernelExceptionContext;

typedef void (*KuKernelExceptionHandler)(KuKernelExceptionContext *);

typedef struct KuKernelExceptionHandlerOpt {
SceSize size;
} KuKernelExceptionHandlerOpt;

typedef struct KuKernelMemCommitOpt {
SceSize size;
Expand Down Expand Up @@ -102,14 +102,56 @@ void kuKernelFlushCaches(const void *ptr, SceSize len);

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

int kuKernelRegisterAbortHandler(KuKernelAbortHandler pHandler, KuKernelAbortHandler *pOldHandler, KuKernelAbortHandlerOpt *pOpt);
void kuKernelReleaseAbortHandler();
int kuKernelRegisterExceptionHandler(SceUInt32 exceptionType, KuKernelExceptionHandler pHandler, KuKernelExceptionHandler *pOldHandler, KuKernelExceptionHandlerOpt *pOpt);
void kuKernelReleaseExceptionHandler(SceUInt32 exceptionType);

int kuKernelMemProtect(void *addr, SceSize len, SceUInt32 prot);
SceUID kuKernelMemReserve(void **addr, SceSize size, SceKernelMemBlockType memBlockType);
int kuKernelMemCommit(void *addr, SceSize len, SceUInt32 prot, KuKernelMemCommitOpt *pOpt);
int kuKernelMemDecommit(void *addr, SceSize len);

// Deprecated

#define KU_KERNEL_ABORT_TYPE_DATA_ABORT 0
#define KU_KERNEL_ABORT_TYPE_PREFETCH_ABORT 1

typedef struct KuKernelAbortContext
{
SceUInt32 r0;
SceUInt32 r1;
SceUInt32 r2;
SceUInt32 r3;
SceUInt32 r4;
SceUInt32 r5;
SceUInt32 r6;
SceUInt32 r7;
SceUInt32 r8;
SceUInt32 r9;
SceUInt32 r10;
SceUInt32 r11;
SceUInt32 r12;
SceUInt32 sp;
SceUInt32 lr;
SceUInt32 pc;
SceUInt64 vfpRegisters[32];
SceUInt32 SPSR;
SceUInt32 FPSCR;
SceUInt32 FPEXC;
SceUInt32 FSR;
SceUInt32 FAR;
SceUInt32 abortType;
} KuKernelAbortContext;

typedef void (*KuKernelAbortHandler)(KuKernelAbortContext *);

typedef struct KuKernelAbortHandlerOpt
{
SceSize size; //!< Size of structure
} KuKernelAbortHandlerOpt;

int kuKernelRegisterAbortHandler(KuKernelAbortHandler pHandler, KuKernelAbortHandler *pOldHandler, KuKernelAbortHandlerOpt *pOpt);
void kuKernelReleaseAbortHandler();

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 52d0e46

Please sign in to comment.