Refactor Common

This refactored common by:
* Moving out as many constants to class/function local scopes from being declared in `common`
* Spacing out common and any function to which a constant was moved out to
* Fixing comments here and there
In addition, some naming inconsistencies were fixed as well.
This commit is contained in:
◱ PixelyIon
2020-03-25 23:29:37 +05:30
committed by ◱ PixelyIon
parent 618353c1fa
commit 773ee25e5a
44 changed files with 906 additions and 813 deletions

View File

@ -23,12 +23,12 @@ namespace skyline::kernel::ipc {
handleDesc = reinterpret_cast<HandleDescriptor *>(pointer);
pointer += sizeof(HandleDescriptor) + (handleDesc->sendPid ? sizeof(u64) : 0);
for (uint index = 0; handleDesc->copyCount > index; index++) {
copyHandles.push_back(*reinterpret_cast<handle_t *>(pointer));
pointer += sizeof(handle_t);
copyHandles.push_back(*reinterpret_cast<KHandle *>(pointer));
pointer += sizeof(KHandle);
}
for (uint index = 0; handleDesc->moveCount > index; index++) {
moveHandles.push_back(*reinterpret_cast<handle_t *>(pointer));
pointer += sizeof(handle_t);
moveHandles.push_back(*reinterpret_cast<KHandle *>(pointer));
pointer += sizeof(KHandle);
}
}
@ -69,7 +69,9 @@ namespace skyline::kernel::ipc {
pointer += sizeof(BufferDescriptorABW);
}
u64 padding = ((((reinterpret_cast<u64>(pointer) - reinterpret_cast<u64>(tls)) - 1U) & ~(constant::IpcPaddingSum - 1U)) + constant::IpcPaddingSum + (reinterpret_cast<u64>(tls) - reinterpret_cast<u64>(pointer))); // Calculate the amount of padding at the front
constexpr auto ipcPaddingSum = 0x10; // The sum of the padding surrounding the data payload
u64 padding = ((((reinterpret_cast<u64>(pointer) - reinterpret_cast<u64>(tls)) - 1U) & ~(ipcPaddingSum - 1U)) + ipcPaddingSum + (reinterpret_cast<u64>(tls) - reinterpret_cast<u64>(pointer))); // Calculate the amount of padding at the front
pointer += padding;
if (isDomain && (header->type == CommandType::Request)) {
@ -84,24 +86,26 @@ namespace skyline::kernel::ipc {
pointer += domain->payloadSz;
for (uint index = 0; domain->inputCount > index; index++) {
domainObjects.push_back(*reinterpret_cast<handle_t *>(pointer));
pointer += sizeof(handle_t);
domainObjects.push_back(*reinterpret_cast<KHandle *>(pointer));
pointer += sizeof(KHandle);
}
} else {
payload = reinterpret_cast<PayloadHeader *>(pointer);
pointer += sizeof(PayloadHeader);
cmdArg = pointer;
cmdArgSz = (header->rawSize * sizeof(u32)) - (constant::IpcPaddingSum + sizeof(PayloadHeader));
cmdArgSz = (header->rawSize * sizeof(u32)) - (ipcPaddingSum + sizeof(PayloadHeader));
pointer += cmdArgSz;
}
payloadOffset = cmdArg;
if (payload->magic != constant::SfciMagic && header->type != CommandType::Control)
constexpr auto sfciMagic = 0x49434653; //!< SFCI in reverse, present in received IPC messages
if (payload->magic != sfciMagic && header->type != CommandType::Control)
state.logger->Debug("Unexpected Magic in PayloadHeader: 0x{:X}", u32(payload->magic));
pointer += constant::IpcPaddingSum - padding;
pointer += ipcPaddingSum - padding;
if (header->cFlag == BufferCFlag::SingleDescriptor) {
auto bufC = reinterpret_cast<BufferDescriptorC *>(pointer);
@ -135,10 +139,14 @@ namespace skyline::kernel::ipc {
void IpcResponse::WriteResponse() {
auto tls = state.process->GetPointer<u8>(state.thread->tls);
u8 *pointer = tls;
memset(tls, 0, constant::TlsIpcSize);
constexpr auto tlsIpcSize = 0x100; // The size of the IPC command buffer in a TLS slot
memset(tls, 0, tlsIpcSize);
constexpr auto ipcPaddingSum = 0x10; // The sum of the padding surrounding the data payload
auto header = reinterpret_cast<CommandHeader *>(pointer);
header->rawSize = static_cast<u32>((sizeof(PayloadHeader) + argVec.size() + (domainObjects.size() * sizeof(handle_t)) + constant::IpcPaddingSum + (isDomain ? sizeof(DomainHeaderRequest) : 0)) / sizeof(u32)); // Size is in 32-bit units because Nintendo
header->rawSize = static_cast<u32>((sizeof(PayloadHeader) + argVec.size() + (domainObjects.size() * sizeof(KHandle)) + ipcPaddingSum + (isDomain ? sizeof(DomainHeaderRequest) : 0)) / sizeof(u32)); // Size is in 32-bit units because Nintendo
header->handleDesc = (!copyHandles.empty() || !moveHandles.empty());
pointer += sizeof(CommandHeader);
@ -149,17 +157,17 @@ namespace skyline::kernel::ipc {
pointer += sizeof(HandleDescriptor);
for (unsigned int copyHandle : copyHandles) {
*reinterpret_cast<handle_t *>(pointer) = copyHandle;
pointer += sizeof(handle_t);
*reinterpret_cast<KHandle *>(pointer) = copyHandle;
pointer += sizeof(KHandle);
}
for (unsigned int moveHandle : moveHandles) {
*reinterpret_cast<handle_t *>(pointer) = moveHandle;
pointer += sizeof(handle_t);
*reinterpret_cast<KHandle *>(pointer) = moveHandle;
pointer += sizeof(KHandle);
}
}
u64 padding = ((((reinterpret_cast<u64>(pointer) - reinterpret_cast<u64>(tls)) - 1U) & ~(constant::IpcPaddingSum - 1U)) + constant::IpcPaddingSum + (reinterpret_cast<u64>(tls) - reinterpret_cast<u64>(pointer))); // Calculate the amount of padding at the front
u64 padding = ((((reinterpret_cast<u64>(pointer) - reinterpret_cast<u64>(tls)) - 1U) & ~(ipcPaddingSum - 1U)) + ipcPaddingSum + (reinterpret_cast<u64>(tls) - reinterpret_cast<u64>(pointer))); // Calculate the amount of padding at the front
pointer += padding;
if (isDomain) {
@ -168,19 +176,22 @@ namespace skyline::kernel::ipc {
pointer += sizeof(DomainHeaderResponse);
}
constexpr auto sfcoMagic = 0x4F434653; // SFCO in reverse, IPC Response Magic
auto payload = reinterpret_cast<PayloadHeader *>(pointer);
payload->magic = constant::SfcoMagic;
payload->magic = sfcoMagic;
payload->version = 1;
payload->value = errorCode;
pointer += sizeof(PayloadHeader);
if (!argVec.empty())
memcpy(pointer, argVec.data(), argVec.size());
pointer += argVec.size();
if (isDomain) {
for (auto &domainObject : domainObjects) {
*reinterpret_cast<handle_t *>(pointer) = domainObject;
pointer += sizeof(handle_t);
*reinterpret_cast<KHandle *>(pointer) = domainObject;
pointer += sizeof(KHandle);
}
}