From 6c2d06f9a818b48e7a8c6acfb9fc412e63791975 Mon Sep 17 00:00:00 2001 From: plutoo Date: Mon, 11 Sep 2017 04:38:38 +0200 Subject: [PATCH] Fixing bugs. bsdInitialize currently fails with 0xe401, probably due to ipc bug --- nx/include/switch/ipc.h | 8 ++++---- nx/include/switch/kernel/tmem.h | 4 ++++ nx/include/switch/svc.h | 1 + nx/source/heap/heap.c | 7 ++++--- nx/source/kernel/svc.s | 8 ++++++++ nx/source/services/bsd.c | 2 ++ nx/source/services/sm.c | 7 ++++--- 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/nx/include/switch/ipc.h b/nx/include/switch/ipc.h index f047635d..f4c18516 100644 --- a/nx/include/switch/ipc.h +++ b/nx/include/switch/ipc.h @@ -100,23 +100,23 @@ static inline void ipcSendHandleMove(IpcCommand* cmd, Handle h) { static inline void* ipcPrepareHeader(IpcCommand* cmd, size_t sizeof_raw) { u32* buf = armGetTls(); + size_t i; *buf++ = 4 | (cmd->NumStaticIn << 16) | (cmd->NumSend << 20) | (cmd->NumRecv << 24) | (cmd->NumTransfer << 28); if (cmd->SendPid || cmd->NumHandlesCopy > 0 || cmd->NumHandlesMove > 0) { *buf++ = (sizeof_raw/4) | 0x80000000; - *buf++ = (!!cmd->SendPid) | (cmd->NumHandlesCopy << 1) | (cmd->NumHandlesMove << 1); + *buf++ = (!!cmd->SendPid) | (cmd->NumHandlesCopy << 1) | (cmd->NumHandlesMove << 5); if (cmd->SendPid) buf += 2; - buf += cmd->NumHandlesCopy; - buf += cmd->NumHandlesMove; + for (i=0; i<(cmd->NumHandlesCopy + cmd->NumHandlesMove); i++) + *buf++ = cmd->Handles[i]; } else { *buf++ = sizeof_raw/4; } - size_t i; for (i=0; iNumStaticIn; i++, buf+=2) { IpcStaticSendDescriptor* desc = (IpcStaticSendDescriptor*) buf; uintptr_t ptr = (uintptr_t) cmd->Statics[i]; diff --git a/nx/include/switch/kernel/tmem.h b/nx/include/switch/kernel/tmem.h index bbc96a3d..49822696 100644 --- a/nx/include/switch/kernel/tmem.h +++ b/nx/include/switch/kernel/tmem.h @@ -8,3 +8,7 @@ typedef enum { PERM_R = 1, PERM_RW = 3 } Permission; + +Result tmemCreate(TransferMemory* t, size_t size, Permission perm); +Result tmemClose(TransferMemory* t); + diff --git a/nx/include/switch/svc.h b/nx/include/switch/svc.h index 97acc218..03cc803f 100644 --- a/nx/include/switch/svc.h +++ b/nx/include/switch/svc.h @@ -18,6 +18,7 @@ static inline void* armGetTls(void) { return ret; } +Result svcSetHeapSize(void** out_addr, u64 size); Result svcQueryMemory(u32 *meminfo_ptr, u32 *pageinfo, u64 addr); Result svcCloseHandle(Handle handle); Result svcCreateTransferMemory(Handle* out, void* addr, size_t size, u32 perm); diff --git a/nx/source/heap/heap.c b/nx/source/heap/heap.c index 48ceee19..9409cc33 100644 --- a/nx/source/heap/heap.c +++ b/nx/source/heap/heap.c @@ -19,7 +19,6 @@ struct HeapHeader { static HeapHeader g_LastFree; void heapInit(void* base, size_t size) { - // Called by crt0. HeapHeader* hdr = (HeapHeader*) base; hdr->Next = &g_LastFree; @@ -32,8 +31,10 @@ void heapInit(void* base, size_t size) { } void heapSetup() { - static u8 g_Heap[0x20000]; - heapInit(g_Heap, sizeof(g_Heap)); + // Called by crt0. + #define HEAP_SIZE 0x20000 + static u8 g_Heap[HEAP_SIZE]; + heapInit(&g_Heap[0], HEAP_SIZE); } void* heapAllocPages(size_t size) { diff --git a/nx/source/kernel/svc.s b/nx/source/kernel/svc.s index be3d9de0..7476efd7 100644 --- a/nx/source/kernel/svc.s +++ b/nx/source/kernel/svc.s @@ -11,6 +11,14 @@ .cfi_endproc .endm +SVC_BEGIN svcSetHeapSize + str x0, [sp, #-16]! + svc 1 + ldr x2, [sp], #16 + str x1, [x2] + ret +SVC_END + SVC_BEGIN svcQueryMemory str x1, [sp, #-16]! svc 0x6 diff --git a/nx/source/services/bsd.c b/nx/source/services/bsd.c index 5533a2cd..c1011b91 100644 --- a/nx/source/services/bsd.c +++ b/nx/source/services/bsd.c @@ -33,6 +33,8 @@ Result bsdInitialize(TransferMemory* tmem) { raw->unk[3] = 0; raw->unk[4] = 0; raw->tmem_sz = tmem->Size; + + rc = ipcDispatch(g_bsdHandle); } return rc; diff --git a/nx/source/services/sm.c b/nx/source/services/sm.c index fbb848c8..b14d2567 100644 --- a/nx/source/services/sm.c +++ b/nx/source/services/sm.c @@ -5,8 +5,9 @@ static Handle g_smHandle = -1; Result smInitialize() { Result rc = svcConnectToNamedPort(&g_smHandle, "sm:"); + Handle tmp; - if (R_SUCCEEDED(rc)) { + if (R_SUCCEEDED(rc) && smGetService(&tmp, "") == 0x415) { IpcCommand c; ipcInitialize(&c); ipcSendPid(&c); @@ -47,10 +48,10 @@ Result smGetService(Handle* handle_out, const char* name) { size_t i; for (i=0; i<8; i++) { - name_encoded = (name_encoded >> 8) | (((u64) name[i]) << 56); - if (name[i] == '\0') break; + + name_encoded |= ((u64) name[i]) << (8*i); } IpcCommand c;