From 40e0bf4db7bf42f984a56dcd307d646e2873d5a0 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 15 Oct 2018 06:02:04 -0700 Subject: [PATCH 1/5] Refactor service IPC to support domains. --- nx/include/switch/kernel/ipc.h | 77 +++++++++++++++++++++++++-------- nx/include/switch/services/sm.h | 61 ++++++++++++++++++++++++-- 2 files changed, 117 insertions(+), 21 deletions(-) diff --git a/nx/include/switch/kernel/ipc.h b/nx/include/switch/kernel/ipc.h index f9243754..6d016078 100644 --- a/nx/include/switch/kernel/ipc.h +++ b/nx/include/switch/kernel/ipc.h @@ -63,6 +63,13 @@ typedef struct { u32 Pad[2]; } DomainMessageHeader; +/// IPC domain response header. +typedef struct { + u32 NumObjectIds; + u32 Pad[3]; +} DomainResponseHeader; + + typedef struct { size_t NumSend; // A size_t NumRecv; // B @@ -363,12 +370,16 @@ typedef struct { Handle Handles[IPC_MAX_OBJECTS]; ///< Handles. bool WasHandleCopied[IPC_MAX_OBJECTS]; ///< true if the handle was moved, false if it was copied. - bool IsDomainMessage; ///< true if the the message is a Domain message. - DomainMessageType MessageType; ///< Type of the domain message. - u32 MessageLength; ///< Size of rawdata (for domain messages). - u32 ThisObjectId; ///< Object ID to call the command on (for domain messages). - size_t NumObjectIds; ///< Number of object IDs (for domain messages). - u32 ObjectIds[IPC_MAX_OBJECTS]; ///< Object IDs (for domain messages). + bool IsDomainRequest; ///< true if the the message is a Domain message. + DomainMessageType InMessageType; ///< Type of the domain message. + u32 InMessageLength; ///< Size of rawdata (for domain messages). + u32 InThisObjectId; ///< Object ID to call the command on (for domain messages). + size_t InNumObjectIds; ///< Number of object IDs (for domain messages). + u32 InObjectIds[IPC_MAX_OBJECTS]; ///< Object IDs (for domain messages). + + bool IsDomainResponse; ///< true if the the message is a Domain response. + size_t OutNumObjectIds; ///< Number of object IDs (for domain responses). + u32 OutObjectIds[IPC_MAX_OBJECTS]; ///< Object IDs (for domain responses). size_t NumBuffers; ///< Number of buffers in the response. void* Buffers[IPC_MAX_BUFFERS]; ///< Pointers to the buffers. @@ -399,7 +410,8 @@ static inline Result ipcParse(IpcParsedCommand* r) { u32 ctrl1 = *buf++; size_t i; - r->IsDomainMessage = false; + r->IsDomainRequest = false; + r->IsDomainResponse = false; r->CommandType = (IpcCommandType) (ctrl0 & 0xffff); r->HasPid = false; @@ -656,11 +668,11 @@ static inline void* ipcPrepareHeaderForDomain(IpcCommand* cmd, size_t sizeof_raw } /** - * @brief Parse an IPC command response into an IPC parsed command structure (domain version). + * @brief Parse an IPC command request into an IPC parsed command structure (domain version). * @param IPC parsed command structure to fill in. * @return Result code. */ -static inline Result ipcParseForDomain(IpcParsedCommand* r) { +static inline Result ipcParseDomainRequest(IpcParsedCommand* r) { Result rc = ipcParse(r); DomainMessageHeader *hdr; u32 *object_ids; @@ -671,22 +683,51 @@ static inline Result ipcParseForDomain(IpcParsedCommand* r) { object_ids = (u32*)(((uintptr_t) hdr) + sizeof(DomainMessageHeader) + hdr->Length); r->Raw = (void*)(((uintptr_t) r->Raw) + sizeof(DomainMessageHeader)); - r->IsDomainMessage = true; - r->MessageType = (DomainMessageType)(hdr->Type); - switch (r->MessageType) { + r->IsDomainRequest = true; + r->InMessageType = (DomainMessageType)(hdr->Type); + switch (r->InMessageType) { case DomainMessageType_SendMessage: case DomainMessageType_Close: break; default: return MAKERESULT(Module_Libnx, LibnxError_DomainMessageUnknownType); } - r->ThisObjectId = hdr->ThisObjectId; - r->NumObjectIds = hdr->NumObjectIds > 8 ? 8 : hdr->NumObjectIds; - if ((uintptr_t)object_ids + sizeof(u32) * r->NumObjectIds - (uintptr_t)armGetTls() >= 0x100) { + + r->InThisObjectId = hdr->ThisObjectId; + r->InNumObjectIds = hdr->NumObjectIds > 8 ? 8 : hdr->NumObjectIds; + if ((uintptr_t)object_ids + sizeof(u32) * r->InNumObjectIds - (uintptr_t)armGetTls() >= 0x100) { return MAKERESULT(Module_Libnx, LibnxError_DomainMessageTooManyObjectIds); } - for(size_t i = 0; i < r->NumObjectIds; i++) - r->ObjectIds[i] = object_ids[i]; + for(size_t i = 0; i < r->InNumObjectIds; i++) + r->InObjectIds[i] = object_ids[i]; + + return rc; +} + +/** + * @brief Parse an IPC command response into an IPC parsed command structure (domain version). + * @param IPC parsed command structure to fill in. + * @return Result code. + */ +static inline Result ipcParseDomainResponse(IpcParsedCommand* r, size_t sizeof_raw) { + Result rc = ipcParse(r); + DomainResponseHeader *hdr; + u32 *object_ids; + if(R_FAILED(rc)) + return rc; + + hdr = (DomainResponseHeader*) r->Raw; + r->Raw = (void*)(((uintptr_t) r->Raw) + sizeof(DomainResponseHeader)); + object_ids = (u32*)((((uintptr_t) r->Raw) + sizeof_raw + 3) & ~3); + + r->IsDomainResponse = true; + + r->OutNumObjectIds = hdr->NumObjectIds > 8 ? 8 : hdr->NumObjectIds; + if ((uintptr_t)object_ids + sizeof(u32) * r->OutNumObjectIds - (uintptr_t)armGetTls() >= 0x100) { + return MAKERESULT(Module_Libnx, LibnxError_DomainMessageTooManyObjectIds); + } + for(size_t i = 0; i < r->OutNumObjectIds; i++) + r->OutObjectIds[i] = object_ids[i]; return rc; } @@ -704,7 +745,7 @@ static inline Result ipcCloseObjectById(Handle session, u32 object_id) { ipcInitialize(&c); hdr = (DomainMessageHeader*)ipcPrepareHeader(&c, sizeof(DomainMessageHeader)); - hdr->Type = 2; + hdr->Type = DomainMessageType_Close; hdr->NumObjectIds = 0; hdr->Length = 0; hdr->ThisObjectId = object_id; diff --git a/nx/include/switch/services/sm.h b/nx/include/switch/services/sm.h index 2c78ec04..b9f467cc 100644 --- a/nx/include/switch/services/sm.h +++ b/nx/include/switch/services/sm.h @@ -16,7 +16,7 @@ typedef enum { ServiceType_Normal, ///< Normal service. ServiceType_Domain, ///< Domain. ServiceType_DomainSubservice, ///< Domain subservice; - ServiceType_Override ///< Service overriden in the homebrew environment. + ServiceType_Override, ///< Service overriden in the homebrew environment. } ServiceType; /// Service object structure. @@ -113,15 +113,39 @@ static inline void serviceCreateDomainSubservice(Service* s, Service* parent, u3 s->object_id = object_id; } +/** + * @brief Creates a subservice object from a parent service. + * @param[out] s Service object. + * @param[in] parent Parent service, possibly a domain or domain subservice. + * @param[in] r Parsed IPC command containing handles/object IDs to create subservice from. + * @param[in] i The index of the handle/object ID to create subservice from. + */ +static inline void serviceCreateSubservice(Service* s, Service* parent, IpcParsedCommand* r, int i) { + if (r->IsDomainResponse) { + return serviceCreateDomainSubservice(s, parent, r->OutObjectIds[i]); + } else { + return serviceCreate(s, r->Handles[i]); + } +} + /** * @brief Converts a regular service to a domain. * @param[in] s Service object. * @return Result code. */ static inline Result serviceConvertToDomain(Service* s) { - Result rc = ipcConvertSessionToDomain(s->handle, &s->object_id); - if(R_SUCCEEDED(rc)) + Result rc = 0; + if (serviceIsOverride(s)) { + rc = ipcCloneSession(s->handle, 1, &s->handle); + if (R_FAILED(rc)) { + return rc; + } + s->type = ServiceType_Normal; + } + rc = ipcConvertSessionToDomain(s->handle, &s->object_id); + if (R_SUCCEEDED(rc)) { s->type = ServiceType_Domain; + } return rc; } @@ -153,6 +177,37 @@ static inline void serviceClose(Service* s) { s->type = ServiceType_Uninitialized; } + + +/** + * @brief Prepares the header of an IPC command structure for a service. + * @param s Service to prepare message header for + * @param cmd IPC command structure. + * @param sizeof_raw Size in bytes of the raw data structure to embed inside the IPC request + * @return Pointer to the raw embedded data structure in the request, ready to be filled out. + */ +static inline void* serviceIpcPrepareHeader(Service* s, IpcCommand* cmd, size_t sizeof_raw) { + if (serviceIsDomain(s) || serviceIsDomainSubservice(s)) { + return ipcPrepareHeaderForDomain(cmd, sizeof_raw, serviceGetObjectId(s)); + } else { + return ipcPrepareHeader(cmd, sizeof_raw); + } +} + +/** + * @brief Parse an IPC command response into an IPC parsed command structure for a service. + * @param s Service to prepare message header for + * @param r IPC parsed command structure to fill in. + * @return Result code. + */ +static inline Result serviceIpcParse(Service* s, IpcParsedCommand* r, size_t sizeof_raw) { + if (serviceIsDomain(s) || serviceIsDomainSubservice(s)) { + return ipcParseDomainResponse(r, sizeof_raw); + } else { + return ipcParse(r); + } +} + /** * @brief Initializes SM. * @return Result code. From d67ae02bc58487e8708d0f3bb7cd3fe51edbc044 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 15 Oct 2018 06:02:13 -0700 Subject: [PATCH 2/5] Add domain support to fsp-srv --- nx/source/services/fs.c | 392 ++++++++++++++++++++++------------------ 1 file changed, 218 insertions(+), 174 deletions(-) diff --git a/nx/source/services/fs.c b/nx/source/services/fs.c index f954ea94..2967b0c4 100644 --- a/nx/source/services/fs.c +++ b/nx/source/services/fs.c @@ -19,6 +19,10 @@ Result fsInitialize(void) return 0; Result rc = smGetService(&g_fsSrv, "fsp-srv"); + + if (R_SUCCEEDED(rc)) { + rc = serviceConvertToDomain(&g_fsSrv); + } if (R_SUCCEEDED(rc)) { IpcCommand c; @@ -31,7 +35,7 @@ Result fsInitialize(void) u64 unk; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; @@ -41,12 +45,13 @@ Result fsInitialize(void) if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -75,7 +80,7 @@ Result fsOpenBisStorage(FsStorage* out, u32 PartitionId) { u32 PartitionId; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 12; @@ -85,17 +90,18 @@ Result fsOpenBisStorage(FsStorage* out, u32 PartitionId) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -116,7 +122,7 @@ Result fsOpenBisFileSystem(FsFileSystem* out, u32 PartitionId, const char* strin u32 PartitionId; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 11; @@ -126,17 +132,18 @@ Result fsOpenBisFileSystem(FsFileSystem* out, u32 PartitionId, const char* strin if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -152,7 +159,7 @@ Result fsMountSdcard(FsFileSystem* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 18; @@ -161,17 +168,18 @@ Result fsMountSdcard(FsFileSystem* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -189,7 +197,7 @@ Result fsMountSaveData(FsFileSystem* out, u8 inval, FsSave *save) { FsSave save; } PACKED *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 51; @@ -200,17 +208,18 @@ Result fsMountSaveData(FsFileSystem* out, u8 inval, FsSave *save) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -228,7 +237,7 @@ Result fsMountSystemSaveData(FsFileSystem* out, u8 inval, FsSave *save) { FsSave save; } PACKED *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 52; @@ -239,17 +248,18 @@ Result fsMountSystemSaveData(FsFileSystem* out, u8 inval, FsSave *save) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -272,13 +282,13 @@ Result fsOpenSaveDataIterator(FsSaveDataIterator* out, s32 SaveDataSpaceId) { } *raw2; if (SaveDataSpaceId == FsSaveDataSpaceId_All) { - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 60; } else { - raw2 = ipcPrepareHeader(&c, sizeof(*raw2)); + raw2 = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw2)); raw2->magic = SFCI_MAGIC; raw2->cmd_id = 61; @@ -289,17 +299,18 @@ Result fsOpenSaveDataIterator(FsSaveDataIterator* out, s32 SaveDataSpaceId) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -315,7 +326,7 @@ Result fsOpenDataStorageByCurrentProcess(FsStorage* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 200; @@ -324,17 +335,18 @@ Result fsOpenDataStorageByCurrentProcess(FsStorage* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -350,7 +362,7 @@ Result fsOpenDeviceOperator(FsDeviceOperator* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 400; @@ -359,17 +371,18 @@ Result fsOpenDeviceOperator(FsDeviceOperator* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -385,7 +398,7 @@ Result fsOpenSdCardDetectionEventNotifier(FsEventNotifier* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 500; @@ -394,17 +407,18 @@ Result fsOpenSdCardDetectionEventNotifier(FsEventNotifier* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -453,7 +467,7 @@ Result fsOpenFileSystemWithId(FsFileSystem* out, u64 titleId, FsFileSystemType f u64 titleId; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 8; @@ -467,7 +481,7 @@ Result fsOpenFileSystemWithId(FsFileSystem* out, u64 titleId, FsFileSystemType f u32 fsType; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -478,17 +492,18 @@ Result fsOpenFileSystemWithId(FsFileSystem* out, u64 titleId, FsFileSystemType f if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsSrv, &r, 0); } } @@ -509,7 +524,7 @@ Result fsFsCreateFile(FsFileSystem* fs, const char* path, size_t size, int flags u32 flags; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -521,12 +536,13 @@ Result fsFsCreateFile(FsFileSystem* fs, const char* path, size_t size, int flags if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -544,7 +560,7 @@ Result fsFsDeleteFile(FsFileSystem* fs, const char* path) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; @@ -553,12 +569,13 @@ Result fsFsDeleteFile(FsFileSystem* fs, const char* path) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -576,7 +593,7 @@ Result fsFsCreateDirectory(FsFileSystem* fs, const char* path) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 2; @@ -585,12 +602,13 @@ Result fsFsCreateDirectory(FsFileSystem* fs, const char* path) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -608,7 +626,7 @@ Result fsFsDeleteDirectory(FsFileSystem* fs, const char* path) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 3; @@ -617,12 +635,13 @@ Result fsFsDeleteDirectory(FsFileSystem* fs, const char* path) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -640,7 +659,7 @@ Result fsFsDeleteDirectoryRecursively(FsFileSystem* fs, const char* path) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 4; @@ -649,12 +668,13 @@ Result fsFsDeleteDirectoryRecursively(FsFileSystem* fs, const char* path) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -673,7 +693,7 @@ Result fsFsRenameFile(FsFileSystem* fs, const char* path0, const char* path1) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 5; @@ -682,12 +702,13 @@ Result fsFsRenameFile(FsFileSystem* fs, const char* path0, const char* path1) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -706,7 +727,7 @@ Result fsFsRenameDirectory(FsFileSystem* fs, const char* path0, const char* path u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 6; @@ -715,12 +736,13 @@ Result fsFsRenameDirectory(FsFileSystem* fs, const char* path0, const char* path if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -738,7 +760,7 @@ Result fsFsGetEntryType(FsFileSystem* fs, const char* path, FsEntryType* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 7; @@ -747,13 +769,14 @@ Result fsFsGetEntryType(FsFileSystem* fs, const char* path, FsEntryType* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u32 type; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -776,7 +799,7 @@ Result fsFsOpenFile(FsFileSystem* fs, const char* path, int flags, FsFile* out) u32 flags; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 8; @@ -786,17 +809,18 @@ Result fsFsOpenFile(FsFileSystem* fs, const char* path, int flags, FsFile* out) if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &fs->s, &r, 0); } } @@ -814,7 +838,7 @@ Result fsFsOpenDirectory(FsFileSystem* fs, const char* path, int flags, FsDir* o u32 flags; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 9; @@ -824,17 +848,18 @@ Result fsFsOpenDirectory(FsFileSystem* fs, const char* path, int flags, FsDir* o if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &fs->s, &r, 0); } } @@ -850,7 +875,7 @@ Result fsFsCommit(FsFileSystem* fs) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 10; @@ -859,12 +884,13 @@ Result fsFsCommit(FsFileSystem* fs) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -882,7 +908,7 @@ Result fsFsGetFreeSpace(FsFileSystem* fs, const char* path, u64* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 11; @@ -891,13 +917,14 @@ Result fsFsGetFreeSpace(FsFileSystem* fs, const char* path, u64* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 space; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -919,7 +946,7 @@ Result fsFsGetTotalSpace(FsFileSystem* fs, const char* path, u64* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 12; @@ -928,13 +955,14 @@ Result fsFsGetTotalSpace(FsFileSystem* fs, const char* path, u64* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 space; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -956,7 +984,7 @@ Result fsFsCleanDirectoryRecursively(FsFileSystem* fs, const char* path) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&fs->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 13; @@ -965,12 +993,13 @@ Result fsFsCleanDirectoryRecursively(FsFileSystem* fs, const char* path) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&fs->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -996,7 +1025,7 @@ Result fsFileRead(FsFile* f, u64 off, void* buf, size_t len, size_t* out) { u64 read_size; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&f->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -1008,13 +1037,14 @@ Result fsFileRead(FsFile* f, u64 off, void* buf, size_t len, size_t* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 bytes_read; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&f->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -1039,7 +1069,7 @@ Result fsFileWrite(FsFile* f, u64 off, const void* buf, size_t len) { u64 write_size; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&f->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; @@ -1051,12 +1081,13 @@ Result fsFileWrite(FsFile* f, u64 off, const void* buf, size_t len) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&f->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -1073,7 +1104,7 @@ Result fsFileFlush(FsFile* f) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&f->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 2; @@ -1082,12 +1113,13 @@ Result fsFileFlush(FsFile* f) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&f->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -1105,7 +1137,7 @@ Result fsFileSetSize(FsFile* f, u64 sz) { u64 size; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&f->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 3; @@ -1115,12 +1147,13 @@ Result fsFileSetSize(FsFile* f, u64 sz) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&f->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -1137,7 +1170,7 @@ Result fsFileGetSize(FsFile* f, u64* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&f->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 4; @@ -1146,13 +1179,14 @@ Result fsFileGetSize(FsFile* f, u64* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 size; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&f->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc) && out) *out = resp->size; @@ -1181,7 +1215,7 @@ Result fsDirRead(FsDir* d, u64 inval, size_t* total_entries, size_t max_entries, u64 inval; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&d->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -1191,13 +1225,14 @@ Result fsDirRead(FsDir* d, u64 inval, size_t* total_entries, size_t max_entries, if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 total_entries; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&d->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -1218,7 +1253,7 @@ Result fsDirGetEntryCount(FsDir* d, u64* count) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&d->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; @@ -1227,13 +1262,14 @@ Result fsDirGetEntryCount(FsDir* d, u64* count) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 count; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&d->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc) && count) *count = resp->count; @@ -1255,7 +1291,7 @@ Result fsStorageRead(FsStorage* s, u64 off, void* buf, size_t len) { u64 read_size; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&s->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -1266,12 +1302,13 @@ Result fsStorageRead(FsStorage* s, u64 off, void* buf, size_t len) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&s->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -1291,7 +1328,7 @@ Result fsStorageWrite(FsStorage* s, u64 off, const void* buf, size_t len) { u64 write_size; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&s->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; @@ -1302,12 +1339,13 @@ Result fsStorageWrite(FsStorage* s, u64 off, const void* buf, size_t len) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&s->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -1324,7 +1362,7 @@ Result fsStorageFlush(FsStorage* s) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&s->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 2; @@ -1333,12 +1371,13 @@ Result fsStorageFlush(FsStorage* s) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&s->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -1356,7 +1395,7 @@ Result fsStorageSetSize(FsStorage* s, u64 sz) { u64 size; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&s->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 3; @@ -1366,12 +1405,13 @@ Result fsStorageSetSize(FsStorage* s, u64 sz) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&s->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } @@ -1388,7 +1428,7 @@ Result fsStorageGetSize(FsStorage* s, u64* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&s->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 4; @@ -1397,13 +1437,14 @@ Result fsStorageGetSize(FsStorage* s, u64* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 size; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&s->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc) && out) *out = resp->size; @@ -1427,7 +1468,7 @@ Result fsSaveDataIteratorRead(FsSaveDataIterator *s, FsSaveDataInfo* buf, size_t u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&s->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -1436,13 +1477,14 @@ Result fsSaveDataIteratorRead(FsSaveDataIterator *s, FsSaveDataInfo* buf, size_t if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u64 total_entries; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&s->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -1468,7 +1510,7 @@ Result fsEventNotifierGetEventHandle(FsEventNotifier* e, Handle* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&e->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -1477,12 +1519,13 @@ Result fsEventNotifierGetEventHandle(FsEventNotifier* e, Handle* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&e->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -1508,7 +1551,7 @@ Result fsDeviceOperatorIsSdCardInserted(FsDeviceOperator* d, bool* out) { u64 cmd_id; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&d->s, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; @@ -1517,13 +1560,14 @@ Result fsDeviceOperatorIsSdCardInserted(FsDeviceOperator* d, bool* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u8 is_inserted; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&d->s, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; From 86a25e1edddce7ec8961ef81ef91e0c2ed7e2d22 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 15 Oct 2018 06:08:39 -0700 Subject: [PATCH 3/5] Make fsp-ldr use domains --- nx/source/services/fsldr.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/nx/source/services/fsldr.c b/nx/source/services/fsldr.c index 146ab137..7ea45287 100644 --- a/nx/source/services/fsldr.c +++ b/nx/source/services/fsldr.c @@ -21,6 +21,10 @@ Result fsldrInitialize(void) { return 0; Result rc = smGetService(&g_fsldrSrv, "fsp-ldr"); + + if (R_SUCCEEDED(rc)) { + rc = serviceConvertToDomain(&g_fsldrSrv); + } if (R_SUCCEEDED(rc) && kernelAbove400()) { rc = fsldrSetCurrentProcess(); @@ -47,7 +51,7 @@ Result fsldrOpenCodeFileSystem(u64 tid, const char *path, FsFileSystem* out) { u64 tid; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsldrSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 0; raw->tid = tid; @@ -57,17 +61,18 @@ Result fsldrOpenCodeFileSystem(u64 tid, const char *path, FsFileSystem* out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsldrSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; if (R_SUCCEEDED(rc)) { - serviceCreate(&out->s, r.Handles[0]); + serviceCreateSubservice(&out->s, &g_fsldrSrv, &r, 0); } } @@ -84,7 +89,7 @@ Result fsldrIsArchivedProgram(u64 pid, bool *out) { u64 pid; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsldrSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 1; raw->pid = pid; @@ -93,13 +98,14 @@ Result fsldrIsArchivedProgram(u64 pid, bool *out) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; u8 is_archived; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsldrSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; @@ -122,7 +128,7 @@ Result fsldrSetCurrentProcess(void) { u64 unk; } *raw; - raw = ipcPrepareHeader(&c, sizeof(*raw)); + raw = serviceIpcPrepareHeader(&g_fsldrSrv, &c, sizeof(*raw)); raw->magic = SFCI_MAGIC; raw->cmd_id = 2; @@ -132,12 +138,13 @@ Result fsldrSetCurrentProcess(void) { if (R_SUCCEEDED(rc)) { IpcParsedCommand r; - ipcParse(&r); - struct { u64 magic; u64 result; - } *resp = r.Raw; + } *resp; + + serviceIpcParse(&g_fsldrSrv, &r, sizeof(*resp)); + resp = r.Raw; rc = resp->result; } From fba43b0f103aaae62958e4e7e5f079824740b0d8 Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 17 Oct 2018 00:32:47 +0200 Subject: [PATCH 4/5] nvBufferCreate: use separate is_cpu_cacheable/is_gpu_cacheable parameters --- nx/include/switch/nvidia/buffer.h | 5 +++-- nx/source/nvidia/buffer.c | 15 ++++++++------- nx/source/nvidia/gpu/cmd_list.c | 2 +- nx/source/nvidia/gpu/zcull_ctx.c | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/nx/include/switch/nvidia/buffer.h b/nx/include/switch/nvidia/buffer.h index 6a85b2dc..5557dd54 100644 --- a/nx/include/switch/nvidia/buffer.h +++ b/nx/include/switch/nvidia/buffer.h @@ -13,14 +13,15 @@ typedef struct NvBuffer { NvAddressSpace* addr_space; NvKind kind; bool has_init; - bool is_cacheable; + bool is_cpu_cacheable; + bool is_gpu_cacheable; } NvBuffer; Result nvBufferInit(void); u32 nvBufferGetNvmapFd(void); void nvBufferExit(void); -Result nvBufferCreate(NvBuffer* m, size_t size, u32 align, bool is_cacheable, NvKind kind, NvAddressSpace* as); +Result nvBufferCreate(NvBuffer* m, size_t size, u32 align, bool is_cpu_cacheable, bool is_gpu_cacheable, NvKind kind, NvAddressSpace* as); void nvBufferFree(NvBuffer* m); void* nvBufferGetCpuAddr(NvBuffer* m); diff --git a/nx/source/nvidia/buffer.c b/nx/source/nvidia/buffer.c index af38dfb7..dfd149e6 100644 --- a/nx/source/nvidia/buffer.c +++ b/nx/source/nvidia/buffer.c @@ -43,7 +43,7 @@ u32 nvBufferGetNvmapFd(void) { } Result nvBufferCreate( - NvBuffer* m, size_t size, u32 align, bool is_cacheable, NvKind kind, + NvBuffer* m, size_t size, u32 align, bool is_cpu_cacheable, bool is_gpu_cacheable, NvKind kind, NvAddressSpace* as) { Result rc; @@ -51,7 +51,8 @@ Result nvBufferCreate( size = (size + align - 1) & ~(align - 1); m->has_init = true; - m->is_cacheable = is_cacheable; + m->is_cpu_cacheable = is_cpu_cacheable; + m->is_gpu_cacheable = is_gpu_cacheable; m->size = size; m->fd = -1; m->cpu_addr = memalign(align, size); @@ -67,16 +68,16 @@ Result nvBufferCreate( if (R_SUCCEEDED(rc)) rc = nvioctlNvmap_Alloc(g_nvmap_fd, m->fd, - 0, is_cacheable ? 1 : 0, align, kind, m->cpu_addr); + 0, is_cpu_cacheable ? 1 : 0, align, kind, m->cpu_addr); - if (R_SUCCEEDED(rc) && !is_cacheable) { + if (R_SUCCEEDED(rc) && !is_cpu_cacheable) { armDCacheFlush(m->cpu_addr, m->size); svcSetMemoryAttribute(m->cpu_addr, m->size, 8, 8); } if (R_SUCCEEDED(rc)) rc = nvAddressSpaceMapBuffer(as, m->fd, - is_cacheable ? NvMapBufferFlags_IsCacheable : 0, NvKind_Pitch, &m->gpu_addr); + is_gpu_cacheable ? NvMapBufferFlags_IsCacheable : 0, NvKind_Pitch, &m->gpu_addr); if (R_FAILED(rc)) nvBufferFree(m); @@ -105,7 +106,7 @@ void nvBufferFree(NvBuffer* m) } if (m->cpu_addr) { - if (!m->is_cacheable) + if (!m->is_cpu_cacheable) svcSetMemoryAttribute(m->cpu_addr, m->size, 8, 0); free(m->cpu_addr); @@ -125,7 +126,7 @@ iova_t nvBufferGetGpuAddr(NvBuffer* m) { Result nvBufferMapAsTexture(NvBuffer* m, NvKind kind) { return nvAddressSpaceMapBuffer(m->addr_space, m->fd, - m->is_cacheable ? NvMapBufferFlags_IsCacheable : 0, kind, &m->gpu_addr_texture); + m->is_gpu_cacheable ? NvMapBufferFlags_IsCacheable : 0, kind, &m->gpu_addr_texture); } iova_t nvBufferGetGpuAddrTexture(NvBuffer* m) { diff --git a/nx/source/nvidia/gpu/cmd_list.c b/nx/source/nvidia/gpu/cmd_list.c index ff6fc50a..06cf3099 100644 --- a/nx/source/nvidia/gpu/cmd_list.c +++ b/nx/source/nvidia/gpu/cmd_list.c @@ -22,7 +22,7 @@ Result nvCmdListCreate(NvCmdList* c, NvGpu* parent, size_t max_cmds) Result rc; rc = nvBufferCreate( - &c->buffer, max_cmds * 4, 0x1000, NvKind_Pitch, false, + &c->buffer, max_cmds * 4, 0x1000, NvKind_Pitch, false, false, &parent->addr_space); if (R_SUCCEEDED(rc)) { diff --git a/nx/source/nvidia/gpu/zcull_ctx.c b/nx/source/nvidia/gpu/zcull_ctx.c index 7c3110df..ae080bd8 100644 --- a/nx/source/nvidia/gpu/zcull_ctx.c +++ b/nx/source/nvidia/gpu/zcull_ctx.c @@ -25,7 +25,7 @@ Result nvZcullContextCreate(NvZcullContext* z, NvGpu* parent) z->parent = parent; rc = nvBufferCreate( - &z->ctx_buf, nvInfoGetZcullCtxSize(), 0x20000, NvKind_Pitch, true, + &z->ctx_buf, nvInfoGetZcullCtxSize(), 0x20000, NvKind_Pitch, false, true, &parent->addr_space); if (R_SUCCEEDED(rc)) From 052fb343976a927a5e7e1e9762bc430718ea0272 Mon Sep 17 00:00:00 2001 From: TuxSH Date: Fri, 21 Sep 2018 13:54:41 +0200 Subject: [PATCH 5/5] Reduce usage of malloc in select and poll. Fix poll not acceping -1 fds --- nx/source/runtime/devices/socket.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/nx/source/runtime/devices/socket.c b/nx/source/runtime/devices/socket.c index 676b2633..43a12a03 100644 --- a/nx/source/runtime/devices/socket.c +++ b/nx/source/runtime/devices/socket.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,8 @@ #include "services/nifm.h" #include "result.h" +__attribute__((weak)) size_t __nx_pollfd_sb_max_fds = 64; + int _convert_errno(int bsdErrno); __thread int h_errno; @@ -261,7 +264,10 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc ++numfds; } - pollinfo = (struct pollfd*)malloc(numfds * sizeof(struct pollfd)); + if(numfds <= __nx_pollfd_sb_max_fds) + pollinfo = (struct pollfd *)alloca(numfds * sizeof(struct pollfd)); + else + pollinfo = (struct pollfd *)malloc(numfds * sizeof(struct pollfd)); if(pollinfo == NULL) { errno = ENOMEM; return -1; @@ -271,7 +277,11 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc if((readfds && FD_ISSET(i, readfds)) || (writefds && FD_ISSET(i, writefds)) || (exceptfds && FD_ISSET(i, exceptfds))) { - pollinfo[j].fd = i; + pollinfo[j].fd = _socketGetFd(i); + if(pollinfo[j].fd == -1) { + rc = -1; + goto cleanup; + } pollinfo[j].events = 0; pollinfo[j].revents = 0; @@ -285,14 +295,12 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc } if(timeout) - rc = poll(pollinfo, numfds, timeout->tv_sec*1000 + timeout->tv_usec/1000); + rc = _socketParseBsdResult(NULL, bsdPoll(pollinfo, numfds, timeout->tv_sec*1000 + timeout->tv_usec/1000)); else - rc = poll(pollinfo, numfds, -1); + rc = _socketParseBsdResult(NULL, bsdPoll(pollinfo, numfds, -1)); - if(rc < 0) { - free(pollinfo); - return rc; - } + if(rc < 0) + goto cleanup; for(i = 0, j = 0, rc = 0; i < nfds; ++i) { found = 0; @@ -328,8 +336,9 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struc } } - free(pollinfo); - +cleanup: + if(numfds > __nx_pollfd_sb_max_fds) + free(pollinfo); return rc; } @@ -343,7 +352,10 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) { return -1; } - fds2 = (struct pollfd *)malloc(nfds * sizeof(struct pollfd)); + if(nfds <= __nx_pollfd_sb_max_fds) + fds2 = (struct pollfd *)alloca(nfds * sizeof(struct pollfd)); + else + fds2 = (struct pollfd *)malloc(nfds * sizeof(struct pollfd)); if(fds2 == NULL) { errno = ENOMEM; return -1; @@ -352,10 +364,14 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) { for(nfds_t i = 0; i < nfds; i++) { fds2[i].events = fds[i].events; fds2[i].revents = fds[i].revents; - fds2[i].fd = _socketGetFd(fds[i].fd); - if(fds2[i].fd == -1) { - ret = -1; - break; + if(fds[i].fd < 0) { + fds2[i].fd = -1; + } else { + fds2[i].fd = _socketGetFd(fds[i].fd); + if(fds2[i].fd == -1) { + ret = -1; + break; + } } } @@ -368,7 +384,8 @@ int poll(struct pollfd *fds, nfds_t nfds, int timeout) { } } - free(fds2); + if(nfds > __nx_pollfd_sb_max_fds) + free(fds2); return ret; } @@ -1617,4 +1634,4 @@ struct servent *getservbyport(int a, const char *s) { (void)a; (void)s; h_errno struct servent *getservent(void) { h_errno = NO_RECOVERY; errno = ENOSYS; return NULL; } void sethostent(int a) { (void)a;} void setnetent(int a) { (void)a;} -void setprotoent(int a) { (void)a; } \ No newline at end of file +void setprotoent(int a) { (void)a; }