From 15c7d8170669cadce685471fea48c215687d6622 Mon Sep 17 00:00:00 2001
From: bunnei <ericbunnie@gmail.com>
Date: Sun, 1 Jun 2014 20:48:29 -0400
Subject: [PATCH] svc: cleaned up function_wrappers, updated various SVCs to
 make use of pointer arguments

---
 src/core/hle/function_wrappers.h | 37 +++++++++++++++++++-------------
 src/core/hle/svc.cpp             | 34 +++++++++++++++--------------
 2 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/src/core/hle/function_wrappers.h b/src/core/hle/function_wrappers.h
index 7eb62fc48b..65e3164c4b 100644
--- a/src/core/hle/function_wrappers.h
+++ b/src/core/hle/function_wrappers.h
@@ -33,7 +33,9 @@ template<u32 func()> void WrapU_V() {
 }
 
 template<int func(void *, const char *)> void WrapI_VC() {
-    u32 retval = func(Memory::GetPointer(PARAM(0)), Memory::GetCharPointer(PARAM(1)));
+    u32 param_1 = 0;
+    u32 retval = func(&param_1, Memory::GetCharPointer(PARAM(1)));
+    Core::g_app_core->SetReg(1, param_1);
     RETURN(retval);
 }
 
@@ -48,7 +50,8 @@ template<int func(const char *, int, int, u32)> void WrapI_CIIU() {
 }
 
 template<int func(int, const char *, u32, void *, void *, u32, int)> void WrapI_ICUVVUI() {
-    u32 retval = func(PARAM(0), Memory::GetCharPointer(PARAM(1)), PARAM(2), Memory::GetPointer(PARAM(3)),Memory::GetPointer(PARAM(4)), PARAM(5), PARAM(6) );
+    u32 retval = func(PARAM(0), Memory::GetCharPointer(PARAM(1)), PARAM(2), 
+        Memory::GetPointer(PARAM(3)),Memory::GetPointer(PARAM(4)), PARAM(5), PARAM(6));
     RETURN(retval);
 }
 
@@ -394,8 +397,8 @@ template<u32 func(const char *)> void WrapU_C() {
 
 template<u32 func(const char *, const char *, const char *, u32)> void WrapU_CCCU() {
     u32 retval = func(Memory::GetCharPointer(PARAM(0)),
-            Memory::GetCharPointer(PARAM(1)), Memory::GetCharPointer(PARAM(2)),
-            PARAM(3));
+        Memory::GetCharPointer(PARAM(1)), Memory::GetCharPointer(PARAM(2)),
+        PARAM(3));
     RETURN(retval);
 }
 
@@ -431,7 +434,7 @@ template<int func(const char *, u32, u32)> void WrapI_CUU() {
 
 template<int func(const char *, u32, u32, u32)> void WrapI_CUUU() {
     int retval = func(Memory::GetCharPointer(PARAM(0)), PARAM(1), PARAM(2),
-            PARAM(3));
+        PARAM(3));
     RETURN(retval);
 }
 
@@ -442,7 +445,7 @@ template<int func(const char *, const char*, int, int)> void WrapI_CCII() {
 
 template<int func(const char *, u32, u32, int, u32, u32)> void WrapI_CUUIUU() {
     int retval = func(Memory::GetCharPointer(PARAM(0)), PARAM(1), PARAM(2),
-            PARAM(3), PARAM(4), PARAM(5));
+        PARAM(3), PARAM(4), PARAM(5));
     RETURN(retval);
 }
 
@@ -454,7 +457,7 @@ template<int func(const char *, int, int, u32, int, int)> void WrapI_CIIUII() {
 
 template<int func(const char *, int, u32, u32, u32)> void WrapI_CIUUU() {
     int retval = func(Memory::GetCharPointer(PARAM(0)), PARAM(1), PARAM(2),
-            PARAM(3), PARAM(4));
+        PARAM(3), PARAM(4));
     RETURN(retval);
 }
 
@@ -554,18 +557,18 @@ template<int func(const char *, u32, int, u32)> void WrapI_CUIU() {
 
 template<void func(u32, const char *, u32, int, u32)> void WrapV_UCUIU() {
     func(PARAM(0), Memory::GetCharPointer(PARAM(1)), PARAM(2), PARAM(3),
-            PARAM(4));
+        PARAM(4));
 }
 
 template<int func(u32, const char *, u32, int, u32)> void WrapI_UCUIU() {
     int retval = func(PARAM(0), Memory::GetCharPointer(PARAM(1)), PARAM(2),
-            PARAM(3), PARAM(4));
+        PARAM(3), PARAM(4));
     RETURN(retval);
 }
 
 template<void func(const char *, u32, int, int, u32)> void WrapV_CUIIU() {
     func(Memory::GetCharPointer(PARAM(0)), PARAM(1), PARAM(2), PARAM(3),
-            PARAM(4));
+        PARAM(4));
 }
 
 template<int func(const char *, u32, int, int, u32)> void WrapI_CUIIU() {
@@ -689,8 +692,8 @@ template<int func(int, u32, u32)> void WrapI_IUU() {
 }
 
 template<u32 func(u32, u32, u32, u32, u32, u32, u32)> void WrapU_UUUUUUU() {
-  u32 retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4), PARAM(5), PARAM(6));
-  RETURN(retval);
+    u32 retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4), PARAM(5), PARAM(6));
+    RETURN(retval);
 }
 
 template<int func(u32, int, u32, u32)> void WrapI_UIUU() {
@@ -734,7 +737,9 @@ template<int func(int, const char *, u32, void *, int, int, int)> void WrapI_ICU
 }
 
 template<int func(void*, u32)> void WrapI_VU(){
-    u32 retval = func(Memory::GetPointer(PARAM(0)), PARAM(1));
+    u32 param_1 = 0;
+    u32 retval = func(&param_1, PARAM(1));
+    Core::g_app_core->SetReg(1, param_1);
     RETURN(retval);
 }
 
@@ -749,7 +754,9 @@ template<int func(void*, u32, void*, int)> void WrapI_VUVI(){
 }
 
 template<int func(void*, u32, u32, u32, u32, u32)> void WrapI_VUUUUU(){
-    u32 retval = func(NULL, PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4));
+    u32 param_1 = 0;
+    u32 retval = func(&param_1, PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4));
+    Core::g_app_core->SetReg(1, param_1);
     RETURN(retval);
 }
 
@@ -763,7 +770,7 @@ template<int func(void*, void*, u32, u32, s64)> void WrapI_VVUUS64() {
     RETURN(retval);
 }
 
-// TODO(bunne): Is this correct? Probably not
+// TODO(bunnei): Is this correct? Probably not
 template<int func(u32, u32, u32, u32, s64)> void WrapI_UUUUS64() {
     int retval = func(PARAM(5), PARAM(1), PARAM(2), PARAM(3), (((u64)PARAM(4) << 32) | PARAM(0)));
     RETURN(retval);
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index 5ba42973a4..11b77e2c51 100644
--- a/src/core/hle/svc.cpp
+++ b/src/core/hle/svc.cpp
@@ -34,8 +34,8 @@ enum MapMemoryPermission {
 };
 
 /// Map application or GSP heap memory
-Result ControlMemory(void* _outaddr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) {
-    u32 virtual_address = 0x00000000;
+Result ControlMemory(void* _out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) {
+    u32* out_addr = (u32*)_out_addr;
 
     DEBUG_LOG(SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=%08X, permissions=0x%08X", 
         operation, addr0, addr1, size, permissions);
@@ -44,21 +44,18 @@ Result ControlMemory(void* _outaddr, u32 operation, u32 addr0, u32 addr1, u32 si
 
     // Map normal heap memory
     case MEMORY_OPERATION_HEAP:
-        virtual_address = Memory::MapBlock_Heap(size, operation, permissions);
+        *out_addr = Memory::MapBlock_Heap(size, operation, permissions);
         break;
 
     // Map GSP heap memory
     case MEMORY_OPERATION_GSP_HEAP:
-        virtual_address = Memory::MapBlock_HeapGSP(size, operation, permissions);
+        *out_addr = Memory::MapBlock_HeapGSP(size, operation, permissions);
         break;
 
     // Unknown ControlMemory operation
     default:
         ERROR_LOG(SVC, "unknown operation=0x%08X", operation);
     }
-
-    Core::g_app_core->SetReg(1, virtual_address);
-
     return 0;
 }
 
@@ -80,10 +77,14 @@ Result MapMemoryBlock(Handle memblock, u32 addr, u32 mypermissions, u32 otherper
 
 /// Connect to an OS service given the port name, returns the handle to the port to out
 Result ConnectToPort(void* _out, const char* port_name) {
+    Handle* out = (Handle*)_out;
     Service::Interface* service = Service::g_manager->FetchFromPortName(port_name);
+
     DEBUG_LOG(SVC, "called port_name=%s", port_name);
     _assert_msg_(KERNEL, service, "called, but service is not implemented!");
-    Core::g_app_core->SetReg(1, service->GetHandle());
+
+    *out = service->GetHandle();
+
     return 0;
 }
 
@@ -209,12 +210,13 @@ void OutputDebugString(const char* string) {
 }
 
 /// Get resource limit
-Result GetResourceLimit(void* resource_limit, Handle process) {
+Result GetResourceLimit(void* _resource_limit, Handle process) {
     // With regards to proceess values:
     // 0xFFFF8001 is a handle alias for the current KProcess, and 0xFFFF8000 is a handle alias for 
     // the current KThread.
+    Handle* resource_limit = (Handle*)_resource_limit;
+    *resource_limit = 0xDEADBEEF;
     ERROR_LOG(SVC, "(UNIMPLEMENTED) called process=0x%08X", process);
-    Core::g_app_core->SetReg(1, 0xDEADBEEF);
     return 0;
 }
 
@@ -253,10 +255,10 @@ Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 p
 
 /// Create a mutex
 Result CreateMutex(void* _mutex, u32 initial_locked) {
-    Handle mutex = Kernel::CreateMutex((initial_locked != 0));
-    Core::g_app_core->SetReg(1, mutex);
+    Handle* mutex = (Handle*)_mutex;
+    *mutex = Kernel::CreateMutex((initial_locked != 0));
     DEBUG_LOG(SVC, "called initial_locked=%s : created handle=0x%08X", 
-        initial_locked ? "true" : "false", mutex);
+        initial_locked ? "true" : "false", *mutex);
     return 0;
 }
 
@@ -282,10 +284,10 @@ Result QueryMemory(void *_info, void *_out, u32 addr) {
 
 /// Create an event
 Result CreateEvent(void* _event, u32 reset_type) {
-    Handle evt = Kernel::CreateEvent((ResetType)reset_type);
-    Core::g_app_core->SetReg(1, evt);
+    Handle* evt = (Handle*)_event;
+    *evt = Kernel::CreateEvent((ResetType)reset_type);
     DEBUG_LOG(SVC, "called reset_type=0x%08X : created handle=0x%08X", 
-        reset_type, evt);
+        reset_type, *evt);
     return 0;
 }