[APT] Implement new 3DS check, [OS] Fix mem allog bug & impl more of HID

This commit is contained in:
wheremyfoodat 2022-09-25 03:35:40 +03:00
parent c4cb20f846
commit 0aaf1c317d
14 changed files with 143 additions and 60 deletions

View file

@ -20,7 +20,17 @@ namespace KernelHandles {
MinServiceHandle = APT,
MaxServiceHandle = NDM,
GSPSharedMemHandle = MaxServiceHandle + 1 // Handle for the GSP shared memory
GSPSharedMemHandle = MaxServiceHandle + 1, // Handle for the GSP shared memory
HIDSharedMemHandle,
MinSharedMemHandle = GSPSharedMemHandle,
MaxSharedMemHandle = HIDSharedMemHandle,
HIDEvent0,
HIDEvent1,
HIDEvent2,
HIDEvent3,
HIDEvent4
};
// Returns whether "handle" belongs to one of the OS services
@ -28,6 +38,11 @@ namespace KernelHandles {
return handle >= MinServiceHandle && handle <= MaxServiceHandle;
}
// Returns whether "handle" belongs to one of the OS services' shared memory areas
static constexpr bool isSharedMemHandle(Handle handle) {
return handle >= MinSharedMemHandle && handle <= MaxSharedMemHandle;
}
// Returns the name of a handle as a string based on the given handle
static const char* getServiceName(Handle handle) {
switch (handle) {
@ -36,6 +51,7 @@ namespace KernelHandles {
case FS: return "FS";
case GPU: return "GPU";
case LCD: return "LCD";
case NDM: return "NDM";
default: return "Unknown";
}
}

View file

@ -86,6 +86,7 @@ class Kernel {
MAKE_LOG_FUNCTION(log, kernelLogger)
MAKE_LOG_FUNCTION(logSVC, svcLogger)
MAKE_LOG_FUNCTION(logDebugString, debugStringLogger)
// SVC implementations
void arbitrateAddress();

View file

@ -19,6 +19,7 @@ namespace Log {
// Our loggers here. Enable/disable by toggling the template param
static Logger<false> kernelLogger;
static Logger<true> debugStringLogger; // Enables output for the outputDebugString SVC
static Logger<false> svcLogger;
static Logger<false> gpuLogger;

View file

@ -1,9 +1,11 @@
#pragma once
#include <array>
#include <bitset>
#include <fstream>
#include <optional>
#include <vector>
#include "helpers.hpp"
#include "handles.hpp"
namespace PhysicalAddrs {
enum : u32 {
@ -68,19 +70,33 @@ namespace KernelMemoryTypes {
MemoryInfo(u32 baseAddr, u32 size, u32 perms, u32 state) : baseAddr(baseAddr), size(size)
, perms(perms), state(state) {}
};
// Shared memory block for HID, GSP:GPU etc
struct SharedMemoryBlock {
u32 paddr; // Physical address of this block's memory
u32 size; // Size of block
u32 handle; // The handle of the shared memory block
bool mapped; // Has this block been mapped at least once?
SharedMemoryBlock(u32 paddr, u32 size, u32 handle) : paddr(paddr), size(size), handle(handle), mapped(false) {}
};
}
class Memory {
u8* fcram;
u64& cpuTicks; // Reference to the CPU tick counter
using SharedMemoryBlock = KernelMemoryTypes::SharedMemoryBlock;
// Our dynarmic core uses page tables for reads and writes with 4096 byte pages
std::vector<uintptr_t> readTable, writeTable;
// This tracks our OS' memory allocations
std::vector<KernelMemoryTypes::MemoryInfo> memoryInfo;
// This tracks our physical memory reservations when the memory is not actually mapped to a vaddr
std::vector<KernelMemoryTypes::MemoryInfo> lockedMemoryInfo;
std::array<SharedMemoryBlock, 2> sharedMemBlocks = {
SharedMemoryBlock(0, 0x1000, KernelHandles::GSPSharedMemHandle), // GSP shared memory
SharedMemoryBlock(0, 0x1000, KernelHandles::HIDSharedMemHandle) // HID shared memory
};
static constexpr u32 pageShift = 12;
static constexpr u32 pageSize = 1 << pageShift;
@ -99,7 +115,6 @@ class Memory {
public:
u16 kernelVersion = 0;
u32 usedUserMemory = 0;
std::optional<int> gspMemIndex; // Index of GSP shared mem in lockedMemoryInfo or nullopt if it's already reserved
Memory(u64& cpuTicks);
void reset();
@ -139,12 +154,12 @@ public:
// For internal use:
// Reserve FCRAM linearly starting from physical address "paddr" (paddr == 0 is NOT special) with a size of "size"
// Without actually mapping the memory to a vaddr
// r, w, x: Permissions for the reserved memory
// Returns the index of the allocation in lockedMemoryInfo if allocation succeeded and nullopt if it failed
std::optional<int> reserveMemory(u32 paddr, u32 size, bool r, bool w, bool x);
// Returns true if the reservation succeeded and false if not
bool reserveMemory(u32 paddr, u32 size);
// Map GSP shared memory to virtual address vaddr with permissions "myPerms"
// Map a shared memory block to virtual address vaddr with permissions "myPerms"
// The kernel has a second permission parameter in MapMemoryBlock but not sure what's used for
// TODO: Find out
void mapGSPSharedMemory(u32 vaddr, u32 myPerms, u32 otherPerms);
// Returns a pointer to the FCRAM block used for the memory if allocation succeeded
u8* mapSharedMemory(Handle handle, u32 vaddr, u32 myPerms, u32 otherPerms);
};

View file

@ -11,6 +11,7 @@ class APTService {
// Service commands
void getLockHandle(u32 messagePointer);
void checkNew3DS(u32 messagePointer);
public:
APTService(Memory& mem) : mem(mem) {}

View file

@ -7,6 +7,8 @@
class HIDService {
Handle handle = KernelHandles::HID;
Memory& mem;
u8* sharedMem; // Pointer to HID shared memory
MAKE_LOG_FUNCTION(log, hidLogger)
// Service commands
@ -16,4 +18,11 @@ public:
HIDService(Memory& mem) : mem(mem) {}
void reset();
void handleSyncRequest(u32 messagePointer);
void setSharedMem(u8* ptr) {
sharedMem = ptr;
if (ptr != nullptr) { // Zero-fill shared memory in case the process tries to read stale service data or vice versa
std::memset(ptr, 0xff, 0x2b0);
}
}
};

View file

@ -34,6 +34,9 @@ public:
// Forward a SendSyncRequest IPC message to the service with the respective handle
void sendCommandToService(u32 messagePointer, Handle handle);
// Wrappers for communicating with certain services
void requestGPUInterrupt(GPUInterrupt type) { gsp_gpu.requestInterrupt(type); }
void setGSPSharedMem(u8* ptr) { gsp_gpu.setSharedMem(ptr); }
void setHIDSharedMem(u8* ptr) { hid.setSharedMem(ptr); }
};