diff --git a/wiiu/dynamic_libs/acp_functions.c b/wiiu/dynamic_libs/acp_functions.c new file mode 100644 index 0000000000..eb931f84b0 --- /dev/null +++ b/wiiu/dynamic_libs/acp_functions.c @@ -0,0 +1,40 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#include "os_functions.h" +#include "acp_functions.h" + +unsigned int acp_handle __attribute__((section(".data"))) = 0; + +EXPORT_DECL(void, GetMetaXml, void * _ACPMetaXml); + +void InitAcquireACP(void) +{ + OSDynLoad_Acquire("nn_acp.rpl", &acp_handle); +} + +void InitACPFunctionPointers(void) +{ + InitAcquireACP(); + OSDynLoad_FindExport(acp_handle,0,"GetMetaXml__Q2_2nn3acpFP11_ACPMetaXml",&GetMetaXml); +} diff --git a/wiiu/dynamic_libs/acp_functions.h b/wiiu/dynamic_libs/acp_functions.h new file mode 100644 index 0000000000..dc1822dc35 --- /dev/null +++ b/wiiu/dynamic_libs/acp_functions.h @@ -0,0 +1,42 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#ifndef __ACP_FUNCTIONS_H_ +#define __ACP_FUNCTIONS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +extern unsigned int acp_handle; + +void InitACPFunctionPointers(void); +void InitAcquireACP(void); + +#ifdef __cplusplus +} +#endif + +#endif // __VPAD_FUNCTIONS_H_ diff --git a/wiiu/dynamic_libs/aoc_functions.c b/wiiu/dynamic_libs/aoc_functions.c new file mode 100644 index 0000000000..e74306d9c8 --- /dev/null +++ b/wiiu/dynamic_libs/aoc_functions.c @@ -0,0 +1,58 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#include "os_functions.h" +#include "aoc_functions.h" + +unsigned int aoc_handle __attribute__((section(".data"))) = 0; + +EXPORT_DECL(s32, AOC_Initialize, void); +EXPORT_DECL(s32, AOC_Finalize, void); +EXPORT_DECL(u32, AOC_CalculateWorkBufferSize, u32 num_titles); +EXPORT_DECL(s32, AOC_ListTitle, u32 * num_titles, void * titles, u32 max_titles, void * buffer, u32 buffer_size); +EXPORT_DECL(s32, AOC_OpenTitle, char* aoc_path, void * title, void * buffer, u32 buffer_size); +EXPORT_DECL(s32, AOC_CloseTitle, void * title); +EXPORT_DECL(s32, AOC_DeleteContent, u64 title_id, u16 contentIndexes[], u32 numberOfContent, void* buffer, u32 buffer_size); +EXPORT_DECL(s32, AOC_GetPurchaseInfo, u32 * bResult, u64 title_id, u16 contentIndexes[], u32 numberOfContent, void * buffer, u32 buffer_size); + +void InitAcquireAoc(void) +{ + OSDynLoad_Acquire("nn_aoc.rpl", &aoc_handle); +} + +void InitAocFunctionPointers(void) +{ + InitAcquireAoc(); + if(aoc_handle == 0) + return; + + //! assigning those is not mandatory and it does not always work to load them + OSDynLoad_FindExport(aoc_handle, 0, "AOC_Initialize", &AOC_Initialize); + OSDynLoad_FindExport(aoc_handle, 0, "AOC_Finalize", &AOC_Finalize); + OSDynLoad_FindExport(aoc_handle, 0, "AOC_CalculateWorkBufferSize", &AOC_CalculateWorkBufferSize); + OSDynLoad_FindExport(aoc_handle, 0, "AOC_ListTitle", &AOC_ListTitle); + OSDynLoad_FindExport(aoc_handle, 0, "AOC_OpenTitle", &AOC_OpenTitle); + OSDynLoad_FindExport(aoc_handle, 0, "AOC_CloseTitle", &AOC_CloseTitle); + OSDynLoad_FindExport(aoc_handle, 0, "AOC_DeleteContent", &AOC_DeleteContent); + OSDynLoad_FindExport(aoc_handle, 0, "AOC_GetPurchaseInfo", &AOC_GetPurchaseInfo); +} diff --git a/wiiu/dynamic_libs/aoc_functions.h b/wiiu/dynamic_libs/aoc_functions.h new file mode 100644 index 0000000000..efca771ab0 --- /dev/null +++ b/wiiu/dynamic_libs/aoc_functions.h @@ -0,0 +1,58 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#ifndef __AOC_FUNCTIONS_H_ +#define __AOC_FUNCTIONS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +extern unsigned int aoc_handle; + +#define AOC_TITLE_SIZE 104 + +typedef struct { + u64 title_ID; + u32 group_ID; + u16 version; + char path[88]; +} AOC_TitleListType; + + +void InitAocFunctionPointers(void); +void InitAcquireAoc(void); + +extern s32 (* AOC_Initialize)(void); +extern s32 (* AOC_Finalize)(void); +extern u32 (* AOC_CalculateWorkBufferSize)(u32 num_titles); +extern s32 (* AOC_ListTitle)(u32 * num_titles, void * titles, u32 max_titles, void * buffer, u32 buffer_size); +extern s32 (* AOC_OpenTitle)(char* aoc_path, void * title, void * buffer, u32 buffer_size); +extern s32 (* AOC_CloseTitle)(void * title); +extern s32 (* AOC_DeleteContent)(u64 title_id, u16 contentIndexes[], u32 numberOfContent, void * buffer, u32 buffer_size); +extern s32 (* AOC_GetPurchaseInfo)(u32 * bResult, u64 title_id, u16 contentIndexes[], u32 numberOfContent, void * buffer, u32 buffer_size); +#ifdef __cplusplus +} +#endif + +#endif // __AOC_FUNCTIONS_H_ diff --git a/wiiu/dynamic_libs/curl_functions.c b/wiiu/dynamic_libs/curl_functions.c new file mode 100644 index 0000000000..d828cdb365 --- /dev/null +++ b/wiiu/dynamic_libs/curl_functions.c @@ -0,0 +1,54 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#include "os_functions.h" +#include "curl_functions.h" + +unsigned int libcurl_handle __attribute__((section(".data"))) = 0; + +EXPORT_DECL(CURLcode, n_curl_global_init, long flags); +EXPORT_DECL(CURL *, n_curl_easy_init, void); +EXPORT_DECL(CURLcode, n_curl_easy_setopt, CURL *curl, CURLoption option, ...); +EXPORT_DECL(CURLcode, n_curl_easy_perform, CURL *curl); +EXPORT_DECL(void, n_curl_easy_cleanup, CURL *curl); +EXPORT_DECL(CURLcode, n_curl_easy_getinfo, CURL *curl, CURLINFO info, ...); + +void InitAcquireCurl(void) +{ + OSDynLoad_Acquire("nlibcurl", &libcurl_handle); +} + +void InitCurlFunctionPointers(void) +{ + InitAcquireCurl(); + unsigned int *funcPointer = 0; + + OS_FIND_EXPORT_EX(libcurl_handle, curl_global_init, n_curl_global_init); + OS_FIND_EXPORT_EX(libcurl_handle, curl_easy_init, n_curl_easy_init); + OS_FIND_EXPORT_EX(libcurl_handle, curl_easy_setopt, n_curl_easy_setopt); + OS_FIND_EXPORT_EX(libcurl_handle, curl_easy_perform, n_curl_easy_perform); + OS_FIND_EXPORT_EX(libcurl_handle, curl_easy_cleanup, n_curl_easy_cleanup); + OS_FIND_EXPORT_EX(libcurl_handle, curl_easy_getinfo, n_curl_easy_getinfo); + + n_curl_global_init(CURL_GLOBAL_ALL); +} diff --git a/wiiu/dynamic_libs/curl_functions.h b/wiiu/dynamic_libs/curl_functions.h new file mode 100644 index 0000000000..e40740bf0b --- /dev/null +++ b/wiiu/dynamic_libs/curl_functions.h @@ -0,0 +1,51 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#ifndef __CURL_FUNCTIONS_H_ +#define __CURL_FUNCTIONS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "socket_functions.h" +typedef int socklen_t; +#include +#include + +void InitCurlFunctionPointers(void); +void InitAcquireCurl(void); + +extern CURLcode (* n_curl_global_init)(long flags); +extern CURL * (* n_curl_easy_init)(void); +extern CURLcode (* n_curl_easy_setopt)(CURL *curl, CURLoption option, ...); +extern CURLcode (* n_curl_easy_perform)(CURL *curl); +extern void (* n_curl_easy_cleanup)(CURL *curl); +extern CURLcode (* n_curl_easy_getinfo)(CURL *curl, CURLINFO info, ...); + +#ifdef __cplusplus +} +#endif + +#endif // __CURL_FUNCTIONS_H_ diff --git a/wiiu/dynamic_libs/fs_defs.h b/wiiu/dynamic_libs/fs_defs.h new file mode 100644 index 0000000000..1b1bc415f5 --- /dev/null +++ b/wiiu/dynamic_libs/fs_defs.h @@ -0,0 +1,61 @@ +#ifndef FS_DEFS_H +#define FS_DEFS_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/* FS defines and types */ +#define FS_MAX_LOCALPATH_SIZE 511 +#define FS_MAX_MOUNTPATH_SIZE 128 +#define FS_MAX_FULLPATH_SIZE (FS_MAX_LOCALPATH_SIZE + FS_MAX_MOUNTPATH_SIZE) +#define FS_MAX_ARGPATH_SIZE FS_MAX_FULLPATH_SIZE + +#define FS_STATUS_OK 0 +#define FS_RET_UNSUPPORTED_CMD 0x0400 +#define FS_RET_NO_ERROR 0x0000 +#define FS_RET_ALL_ERROR (unsigned int)(-1) + +#define FS_STAT_FLAG_IS_DIRECTORY 0x80000000 + +/* max length of file/dir name */ +#define FS_MAX_ENTNAME_SIZE 256 + +#define FS_SOURCETYPE_EXTERNAL 0 +#define FS_SOURCETYPE_HFIO 1 + +#define FS_MOUNT_SOURCE_SIZE 0x300 +#define FS_CLIENT_SIZE 0x1700 +#define FS_CMD_BLOCK_SIZE 0xA80 + +typedef struct +{ + uint32_t flag; + uint32_t permission; + uint32_t owner_id; + uint32_t group_id; + uint32_t size; + uint32_t alloc_size; + uint64_t quota_size; + uint32_t ent_id; + uint64_t ctime; + uint64_t mtime; + uint8_t attributes[48]; +} __attribute__((packed)) FSStat; + +typedef struct +{ + FSStat stat; + char name[FS_MAX_ENTNAME_SIZE]; +} FSDirEntry; + + +#ifdef __cplusplus +} +#endif + +#endif /* FS_DEFS_H */ + diff --git a/wiiu/dynamic_libs/os_types.h b/wiiu/dynamic_libs/os_types.h new file mode 100644 index 0000000000..aaa18feeab --- /dev/null +++ b/wiiu/dynamic_libs/os_types.h @@ -0,0 +1,27 @@ +#ifndef _OS_TYPES_H_ +#define _OS_TYPES_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef struct _OSCalendarTime { + int sec; + int min; + int hour; + int mday; + int mon; + int year; + int wday; + int yday; + int msec; + int usec; +} OSCalendarTime; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/wiiu/dynamic_libs/syshid_functions.c b/wiiu/dynamic_libs/syshid_functions.c new file mode 100644 index 0000000000..996d39d42e --- /dev/null +++ b/wiiu/dynamic_libs/syshid_functions.c @@ -0,0 +1,78 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#include "os_functions.h" +#include "syshid_functions.h" + +unsigned int syshid_handle __attribute__((section(".data"))) = 0; + +EXPORT_DECL(int, HIDSetup,void); +EXPORT_DECL(int, HIDTeardown,void); + +EXPORT_DECL(int, HIDAddClient,HIDClient *p_client, HIDAttachCallback attach_callback); +EXPORT_DECL(int, HIDDelClient,HIDClient *p_client); + +EXPORT_DECL(int, HIDGetDescriptor,unsigned int handle,u8 descriptor_type,u8 descriptor_index, u16 language_id, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); +EXPORT_DECL(int, HIDSetDescriptor,unsigned int handle,u8 descriptor_type,u8 descriptor_index, u16 language_id, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); + +EXPORT_DECL(int, HIDSetProtocol,unsigned int handle,u8 interface_index,u8 protocol, HIDCallback hc, void *p_user); +EXPORT_DECL(int, HIDGetProtocol,unsigned int handle,u8 interface_index,u8 * protocol, HIDCallback hc, void *p_user); + +EXPORT_DECL(int, HIDGetReport,u32 handle, u8 report_type, u8 report_id, u8 *p_buffer, u32 buffer_length, HIDCallback hc, void *p_user); +EXPORT_DECL(int, HIDSetReport,u32 handle, u8 report_type, u8 report_id, u8 *p_buffer, u32 buffer_length, HIDCallback hc, void *p_user); + +EXPORT_DECL(int, HIDSetIdle,unsigned int handle, u8 interface_index,u8 duration, HIDCallback hc, void *p_user); + +EXPORT_DECL(int, HIDRead,unsigned int handle, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); +EXPORT_DECL(int, HIDWrite,unsigned int handle, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); + +void InitAcquireSysHID(void) +{ + OSDynLoad_Acquire("nsyshid.rpl", &syshid_handle); +} + +void InitSysHIDFunctionPointers(void) +{ + InitAcquireSysHID(); + + if(syshid_handle == 0){ + return; + } + + unsigned int funcPointer = 0; + + //! assigning those is not mandatory and it does not always work to load them + OS_FIND_EXPORT(syshid_handle, HIDSetup); + OS_FIND_EXPORT(syshid_handle, HIDTeardown); + OS_FIND_EXPORT(syshid_handle, HIDAddClient); + OS_FIND_EXPORT(syshid_handle, HIDDelClient); + OS_FIND_EXPORT(syshid_handle, HIDGetDescriptor); + OS_FIND_EXPORT(syshid_handle, HIDSetDescriptor); + OS_FIND_EXPORT(syshid_handle, HIDRead); + OS_FIND_EXPORT(syshid_handle, HIDWrite); + OS_FIND_EXPORT(syshid_handle, HIDSetProtocol); + OS_FIND_EXPORT(syshid_handle, HIDGetProtocol); + OS_FIND_EXPORT(syshid_handle, HIDSetIdle); + OS_FIND_EXPORT(syshid_handle, HIDGetReport); + OS_FIND_EXPORT(syshid_handle, HIDSetReport); +} diff --git a/wiiu/dynamic_libs/syshid_functions.h b/wiiu/dynamic_libs/syshid_functions.h new file mode 100644 index 0000000000..08213b4b28 --- /dev/null +++ b/wiiu/dynamic_libs/syshid_functions.h @@ -0,0 +1,90 @@ +/**************************************************************************** + * Copyright (C) 2015 + * by Dimok + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + ***************************************************************************/ +#ifndef __SYSHID_FUNCTIONS_H_ +#define __SYSHID_FUNCTIONS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +extern unsigned int syshid_handle; + +typedef struct +{ + unsigned int handle; + unsigned int physical_device_inst; + unsigned short vid; + unsigned short pid; + unsigned char interface_index; + unsigned char sub_class; + unsigned char protocol; + + unsigned short max_packet_size_rx; + unsigned short max_packet_size_tx; + +} HIDDevice; + +typedef struct _HIDClient HIDClient; + +#define HID_DEVICE_DETACH 0 +#define HID_DEVICE_ATTACH 1 + +typedef int (*HIDAttachCallback)(HIDClient *p_hc,HIDDevice *p_hd,unsigned int attach); + +struct _HIDClient +{ + HIDClient *next; + HIDAttachCallback attach_cb; +}; + +typedef void (*HIDCallback)(unsigned int handle,int error,unsigned char *p_buffer,unsigned int bytes_transferred,void *p_user); + +void InitSysHIDFunctionPointers(void); +void InitAcquireSysHID(void); + +extern int(*HIDSetup)(void); +extern int(*HIDTeardown)(void); + +extern int(*HIDAddClient)(HIDClient *p_client, HIDAttachCallback attach_callback); +extern int(*HIDDelClient)(HIDClient *p_client); + +extern int(*HIDGetDescriptor)(unsigned int handle,u8 descriptor_type,u8 descriptor_index, u16 language_id, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); +extern int(*HIDSetDescriptor)(unsigned int handle,u8 descriptor_type,u8 descriptor_index, u16 language_id, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); + +extern int(*HIDGetReport)(u32 handle, u8 report_type, u8 report_id, u8 *p_buffer, u32 buffer_length, HIDCallback hc, void *p_user); +extern int(*HIDSetReport)(u32 handle, u8 report_type, u8 report_id, u8 *p_buffer, u32 buffer_length, HIDCallback hc, void *p_user); + +extern int(*HIDSetIdle)(unsigned int handle, u8 interface_index,u8 duration, HIDCallback hc, void *p_user); + +extern int(* HIDSetProtocol)(unsigned int handle,u8 interface_index,u8 protocol, HIDCallback hc, void *p_user); +extern int(* HIDGetProtocol)(unsigned int handle,u8 interface_index,u8 * protocol, HIDCallback hc, void *p_user); + +extern int(*HIDRead)(unsigned int handle, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); +extern int(*HIDWrite)(unsigned int handle, unsigned char *p_buffer, unsigned int buffer_length, HIDCallback hc, void *p_user); + +#ifdef __cplusplus +} +#endif + +#endif // __SYSHID_FUNCTIONS_H_ diff --git a/wiiu/system/exception_handler.c b/wiiu/system/exception_handler.c new file mode 100644 index 0000000000..b64224871f --- /dev/null +++ b/wiiu/system/exception_handler.c @@ -0,0 +1,169 @@ +#include +#include "dynamic_libs/os_functions.h" +#include "utils/logger.h" +#include "exception_handler.h" + +#define OS_EXCEPTION_MODE_GLOBAL_ALL_CORES 4 + +#define OS_EXCEPTION_DSI 2 +#define OS_EXCEPTION_ISI 3 +#define OS_EXCEPTION_PROGRAM 6 + +/* Exceptions */ +typedef struct OSContext +{ + /* OSContext identifier */ + uint32_t tag1; + uint32_t tag2; + + /* GPRs */ + uint32_t gpr[32]; + + /* Special registers */ + uint32_t cr; + uint32_t lr; + uint32_t ctr; + uint32_t xer; + + /* Initial PC and MSR */ + uint32_t srr0; + uint32_t srr1; + + /* Only valid during DSI exception */ + uint32_t exception_specific0; + uint32_t exception_specific1; + + /* There is actually a lot more here but we don't need the rest*/ +} OSContext; + +#define CPU_STACK_TRACE_DEPTH 10 +#define __stringify(rn) #rn + +#define mfspr(_rn) \ +({ register uint32_t _rval = 0; \ + asm volatile("mfspr %0," __stringify(_rn) \ + : "=r" (_rval));\ + _rval; \ +}) + +typedef struct _framerec { + struct _framerec *up; + void *lr; +} frame_rec, *frame_rec_t; + +static const char *exception_names[] = { + "DSI", + "ISI", + "PROGRAM" +}; + +static const char exception_print_formats[18][45] = { + "Exception type %s occurred!\n", // 0 + "GPR00 %08X GPR08 %08X GPR16 %08X GPR24 %08X\n", // 1 + "GPR01 %08X GPR09 %08X GPR17 %08X GPR25 %08X\n", // 2 + "GPR02 %08X GPR10 %08X GPR18 %08X GPR26 %08X\n", // 3 + "GPR03 %08X GPR11 %08X GPR19 %08X GPR27 %08X\n", // 4 + "GPR04 %08X GPR12 %08X GPR20 %08X GPR28 %08X\n", // 5 + "GPR05 %08X GPR13 %08X GPR21 %08X GPR29 %08X\n", // 6 + "GPR06 %08X GPR14 %08X GPR22 %08X GPR30 %08X\n", // 7 + "GPR07 %08X GPR15 %08X GPR23 %08X GPR31 %08X\n", // 8 + "LR %08X SRR0 %08x SRR1 %08x\n", // 9 + "DAR %08X DSISR %08X\n", // 10 + "\nSTACK DUMP:", // 11 + " --> ", // 12 + " -->\n", // 13 + "\n", // 14 + "%p", // 15 + "\nCODE DUMP:\n", // 16 + "%p: %08X %08X %08X %08X\n", // 17 +}; + +static unsigned char exception_cb(void * c, unsigned char exception_type) { + char buf[850]; + int pos = 0; + + OSContext *context = (OSContext *) c; + /* + * This part is mostly from libogc. Thanks to the devs over there. + */ + pos += sprintf(buf + pos, exception_print_formats[0], exception_names[exception_type]); + pos += sprintf(buf + pos, exception_print_formats[1], context->gpr[0], context->gpr[8], context->gpr[16], context->gpr[24]); + pos += sprintf(buf + pos, exception_print_formats[2], context->gpr[1], context->gpr[9], context->gpr[17], context->gpr[25]); + pos += sprintf(buf + pos, exception_print_formats[3], context->gpr[2], context->gpr[10], context->gpr[18], context->gpr[26]); + pos += sprintf(buf + pos, exception_print_formats[4], context->gpr[3], context->gpr[11], context->gpr[19], context->gpr[27]); + pos += sprintf(buf + pos, exception_print_formats[5], context->gpr[4], context->gpr[12], context->gpr[20], context->gpr[28]); + pos += sprintf(buf + pos, exception_print_formats[6], context->gpr[5], context->gpr[13], context->gpr[21], context->gpr[29]); + pos += sprintf(buf + pos, exception_print_formats[7], context->gpr[6], context->gpr[14], context->gpr[22], context->gpr[30]); + pos += sprintf(buf + pos, exception_print_formats[8], context->gpr[7], context->gpr[15], context->gpr[23], context->gpr[31]); + pos += sprintf(buf + pos, exception_print_formats[9], context->lr, context->srr0, context->srr1); + + //if(exception_type == OS_EXCEPTION_DSI) { + pos += sprintf(buf + pos, exception_print_formats[10], context->exception_specific1, context->exception_specific0); // this freezes + //} + + void *pc = (void*)context->srr0; + void *lr = (void*)context->lr; + void *r1 = (void*)context->gpr[1]; + register uint32_t i = 0; + register frame_rec_t l,p = (frame_rec_t)lr; + + l = p; + p = r1; + if(!p) + asm volatile("mr %0,%%r1" : "=r"(p)); + + pos += sprintf(buf + pos, exception_print_formats[11]); + + for(i = 0; i < CPU_STACK_TRACE_DEPTH-1 && p->up; p = p->up, i++) { + if(i % 4) + pos += sprintf(buf + pos, exception_print_formats[12]); + else { + if(i > 0) + pos += sprintf(buf + pos, exception_print_formats[13]); + else + pos += sprintf(buf + pos, exception_print_formats[14]); + } + + switch(i) { + case 0: + if(pc) + pos += sprintf(buf + pos, exception_print_formats[15],pc); + break; + case 1: + if(!l) + l = (frame_rec_t)mfspr(8); + pos += sprintf(buf + pos, exception_print_formats[15],(void*)l); + break; + default: + pos += sprintf(buf + pos, exception_print_formats[15],(void*)(p->up->lr)); + break; + } + } + + //if(exception_type == OS_EXCEPTION_DSI) { + uint32_t *pAdd = (uint32_t*)context->srr0; + pos += sprintf(buf + pos, exception_print_formats[16]); + // TODO by Dimok: this was actually be 3 instead of 2 lines in libogc .... but there is just no more space anymore on the screen + for (i = 0; i < 8; i += 4) + pos += sprintf(buf + pos, exception_print_formats[17], &(pAdd[i]),pAdd[i], pAdd[i+1], pAdd[i+2], pAdd[i+3]); + //} + log_print(buf); + OSFatal(buf); + return 1; +} + +static unsigned char dsi_exception_cb(void * context) { + return exception_cb(context, 0); +} +static unsigned char isi_exception_cb(void * context) { + return exception_cb(context, 1); +} +static unsigned char program_exception_cb(void * context) { + return exception_cb(context, 2); +} + +void setup_os_exceptions(void) { + OSSetExceptionCallback(OS_EXCEPTION_DSI, &dsi_exception_cb); + OSSetExceptionCallback(OS_EXCEPTION_ISI, &isi_exception_cb); + OSSetExceptionCallback(OS_EXCEPTION_PROGRAM, &program_exception_cb); +} diff --git a/wiiu/system/exception_handler.h b/wiiu/system/exception_handler.h new file mode 100644 index 0000000000..7626f92877 --- /dev/null +++ b/wiiu/system/exception_handler.h @@ -0,0 +1,14 @@ +#ifndef __EXCEPTION_HANDLER_H_ +#define __EXCEPTION_HANDLER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void setup_os_exceptions(void); + +#ifdef __cplusplus +} +#endif + +#endif