Hopefully we're good to start sending commands to services now

This commit is contained in:
wheremyfoodat 2022-09-18 00:21:32 +03:00
parent 208c18356b
commit 6154a360c6
10 changed files with 110 additions and 32 deletions

View file

@ -48,12 +48,13 @@ set(SOURCE_FILES src/main.cpp src/emulator.cpp src/core/CPU/cpu_dynarmic.cpp src
set(KERNEL_SOURCE_FILES src/core/kernel/kernel.cpp src/core/kernel/resource_limits.cpp
src/core/kernel/memory_management.cpp src/core/kernel/ports.cpp
)
set(SERVICE_SOURCE_FILES src/core/services/service_manager.cpp)
set(SERVICE_SOURCE_FILES src/core/services/service_manager.cpp src/core/services/apt.cpp)
set(HEADER_FILES include/emulator.hpp include/helpers.hpp include/opengl.hpp include/termcolor.hpp
include/cpu.hpp include/cpu_dynarmic.hpp include/memory.hpp include/kernel/kernel.hpp
include/dynarmic_cp15.hpp include/kernel/resource_limits.hpp include/kernel/kernel_types.hpp
include/kernel/config_mem.hpp include/services/service_manager.hpp
include/kernel/config_mem.hpp include/services/service_manager.hpp include/services/apt.hpp
include/kernel/handles.hpp
)
set(THIRD_PARTY_SOURCE_FILES third_party/imgui/imgui.cpp

View file

@ -0,0 +1,31 @@
#pragma once
#include "helpers.hpp"
using Handle = u32;
namespace KernelHandles {
enum : u32 {
Max = 0xFFFF7FFF, // Max handle the kernel can automagically allocate
// Hardcoded handles
CurrentThread = 0xFFFF8000, // Used by the original kernel
CurrentProcess = 0xFFFF8001, // Used by the original kernel
APT = 0xFFFF8002, // App Title something service?
MinServiceHandle = APT,
MaxServiceHandle = APT
};
// Returns whether "handle" belongs to one of the OS services
static constexpr bool isServiceHandle(Handle handle) {
return handle >= MinServiceHandle && handle <= MaxServiceHandle;
}
// Returns the name of a handle as a string based on the given handle
static const char* getServiceName(Handle handle) {
switch (handle) {
case APT: return "APT";
default: return "Unknown";
}
}
}

View file

@ -40,7 +40,7 @@ class Kernel {
}
Handle makeObject(KernelObjectType type) {
if (handleCounter == std::numeric_limits<Handle>::max()) [[unlikely]] {
if (handleCounter > KernelHandles::Max) [[unlikely]] {
Helpers::panic("Hlep we somehow created enough kernel objects to overflow this thing");
}
@ -52,6 +52,7 @@ class Kernel {
Handle makeProcess();
Handle makePort(const char* name);
Handle makeSession(Handle port);
std::optional<Handle> getPortHandle(const char* name);
void deleteObjectData(KernelObject& object);

View file

@ -1,4 +1,5 @@
#pragma once
#include "handles.hpp"
#include "helpers.hpp"
namespace SVCResult {
@ -13,13 +14,6 @@ namespace SVCResult {
};
}
namespace KernelHandles {
enum : u32 {
CurrentThread = 0xFFFF8000,
CurrentProcess = 0xFFFF8001
};
}
enum class KernelObjectType : u8 {
Port, Process, ResourceLimit, Session, Dummy
};
@ -44,8 +38,6 @@ enum ResourceTypes {
CPU_TIME = 9
};
using Handle = u32;
struct ResourceLimits {
Handle handle;

11
include/services/apt.hpp Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include "helpers.hpp"
#include "kernel_types.hpp"
class APTService {
Handle handle = KernelHandles::APT;
public:
void reset();
void handleSyncRequest(u32 messagePointer);
};

View file

@ -2,11 +2,14 @@
#include <array>
#include "helpers.hpp"
#include "memory.hpp"
#include "services/apt.hpp"
class ServiceManager {
std::array<u32, 16>& regs;
Memory& mem;
APTService apt;
// "srv:" commands
void getServiceHandle(u32 messagePointer);
void registerClient(u32 messagePointer);
@ -14,5 +17,8 @@ class ServiceManager {
public:
ServiceManager(std::array<u32, 16>& regs, Memory& mem);
void reset();
void handleSyncRequest(u32 TLSPointer);
void handleSyncRequest(u32 messagePointer);
// Forward a SendSyncRequest IPC message to the service with the respective handle
void sendCommandToService(u32 messagePointer, Handle handle);
};

View file

@ -41,14 +41,14 @@ KernelObject* Kernel::getProcessFromPID(Handle handle) {
}
void Kernel::deleteObjectData(KernelObject& object) {
// Resource limit and dummy objects do not allocate heap data, so we don't delete anything
if (object.type == KernelObjectType::ResourceLimit || object.type == KernelObjectType::Dummy) {
using enum KernelObjectType;
// Resource limit, service and dummy objects do not allocate heap data, so we don't delete anything
if (object.data == nullptr || object.type == ResourceLimit || object.type == Dummy) {
return;
}
if (object.data != nullptr) {
delete object.data;
}
delete object.data;
}
void Kernel::reset() {

View file

@ -91,11 +91,21 @@ void Kernel::connectToPort() {
}
// Result SendSyncRequest(Handle session)
// Send an IPC message to a port (typically "srv:") or a service
void Kernel::sendSyncRequest() {
const auto handle = regs[0];
const auto session = getObject(handle, KernelObjectType::Session);
printf("SendSyncRequest(session handle = %d)\n", handle);
u32 messagePointer = getTLSPointer() + 0x80; // The message is stored starting at TLS+0x80
printf("SendSyncRequest(session handle = %X)\n", handle);
// The sync request is being sent at a service rather than whatever port, so have the service manager intercept it
if (KernelHandles::isServiceHandle(handle)) {
serviceManager.sendCommandToService(messagePointer, handle);
regs[0] = SVCResult::Success;
return;
}
const auto session = getObject(handle, KernelObjectType::Session);
if (session == nullptr) [[unlikely]] {
Helpers::panic("SendSyncRequest: Invalid session handle");
regs[0] = SVCResult::BadHandle;
@ -107,7 +117,7 @@ void Kernel::sendSyncRequest() {
const auto portData = static_cast<PortData*>(port.data);
if (portData->type == PortType::ServiceManager) { // Special-case SendSyncRequest targetting "srv:"
serviceManager.handleSyncRequest(getTLSPointer());
serviceManager.handleSyncRequest(messagePointer);
} else {
Helpers::panic("SendSyncRequest targetting port %s\n", portData->name);
}

View file

@ -0,0 +1,7 @@
#include "services/apt.hpp"
void APTService::reset() {}
void APTService::handleSyncRequest(u32 messagePointer) {
Helpers::panic("APT service requested");
}

View file

@ -3,7 +3,7 @@
ServiceManager::ServiceManager(std::array<u32, 16>& regs, Memory& mem) : regs(regs), mem(mem) {}
void ServiceManager::reset() {
apt.reset();
}
// Match IPC messages to a "srv:" command based on their header
@ -26,12 +26,16 @@ namespace Commands {
};
}
namespace Result {
enum : u32 {
Success = 0
};
}
// Handle an IPC message issued using the SendSyncRequest SVC
// The parameters are stored in thread-local storage in this format: https://www.3dbrew.org/wiki/IPC#Message_Structure
// TLSPointer: The base pointer for this thread's thread-local storage
void ServiceManager::handleSyncRequest(u32 TLSPointer) {
// The message is stored at TLS+0x80 in this format: https://www.3dbrew.org/wiki/IPC#Message_Structure
const u32 messagePointer = TLSPointer + 0x80;
// messagePointer: The base pointer for the IPC message
void ServiceManager::handleSyncRequest(u32 messagePointer) {
const u32 header = mem.read32(messagePointer);
switch (header) {
@ -41,19 +45,34 @@ void ServiceManager::handleSyncRequest(u32 TLSPointer) {
}
}
// https://www.3dbrew.org/wiki/SRV:RegisterClient
void ServiceManager::registerClient(u32 messagePointer) {
printf("srv: registerClient (Stubbed)\n");
mem.write32(messagePointer + 4, Result::Success);
}
// https://www.3dbrew.org/wiki/SRV:GetServiceHandle
void ServiceManager::getServiceHandle(u32 messagePointer) {
printf("srv: getServiceHandle\n");
u32 nameLength = mem.read32(messagePointer + 12);
u32 flags = mem.read32(messagePointer + 16);
u32 handle = 0;
std::string myBalls;
myBalls.resize(8);
std::string service = mem.readString(messagePointer + 4, 8);
printf("srv: getServiceHandle (Service: %s, nameLength: %d, flags: %d)\n", service.c_str(), nameLength, flags);
for (int i = 0; i < 8; i++) {
myBalls[i] = mem.read8(messagePointer + 4 + i);
if (service == "APT:S") {
handle = KernelHandles::APT;
} else {
Helpers::panic("srv: GetServiceHandle with unknown service %s", service.c_str());
}
std::cout << "Requested handle for service " << myBalls << "\n";
mem.write32(messagePointer + 4, Result::Success);
mem.write32(messagePointer + 12, handle);
}
void ServiceManager::sendCommandToService(u32 messagePointer, Handle handle) {
switch (handle) {
case KernelHandles::APT: apt.handleSyncRequest(messagePointer); break;
default: Helpers::panic("Sent IPC message to unknown service %08X\n", handle);
}
}