Add initial stub code for IPC support

This commit is contained in:
rkx1209 2018-04-04 18:10:24 +09:00
parent 34a21912ae
commit da5e13ca0d
8 changed files with 130 additions and 2 deletions

View file

@ -50,4 +50,8 @@ void DumpJson(FILE *fp) {
file_print (fp, "},\n");
}
uint64_t GetTls() {
return SYSR.tpidrro_el[0];
}
}

View file

@ -45,6 +45,26 @@ void ReadBytes(uint64_t gva, uint8_t *ptr, int size) {
}
}
std::string ReadString(uint64_t gva) {
uint64_t gpa = gva;
uint64_t mx_size = (1 << 32);
char byte;
int sz = 0;
do {
byte = (char)ReadU8(gpa + sz);
sz++;
} while (sz < mx_size && byte != '\0');
if (sz >= mx_size) {
ns_abort("Can not find any string from addr 0x%lx\n", gva);
}
char *bytes = new char[sz];
for (int i = 0; i < sz; i++) {
bytes[i] = (char)ReadU8(gpa + i);
}
std::string str = std::string(bytes);
delete[] bytes;
return str;
}
void WriteBytes(uint64_t gva, uint8_t *ptr, int size) {
uint64_t gpa = gva;
for (int i = 0; i < size; i++) {

47
Ipc.cpp Normal file
View file

@ -0,0 +1,47 @@
/* nsemu - LGPL - Copyright 2018 rkx1209<rkx1209dev@gmail.com> */
#include "Nsemu.hpp"
uint32_t SmService::Dispatch(IpcInMessage *req, IpcOutMessage *resp) {
}
uint32_t SmService::GetService(std::string name, IpcService *service) {
if (IPC::services.find(name) == IPC::services.end()) {
ns_print("Unknown service name %s\n", name.c_str());
return 0xC15; //error code
}
*service = IPC::services[name];
return 0;
}
namespace IPC {
static uint32_t handle_id;
static SmService sm;
std::unordered_map<std::string, IpcService> services;
static std::unordered_map<uint32_t, IpcService *> handles;
void Initialize() {
}
uint32_t NewHandle(IpcService *srv) {
handles[handle_id] = srv;
return handle_id++;
}
IpcService *GetHandle(uint32_t handle) {
if (handles.find(handle) == handles.end()) {
return nullptr;
}
return handles[handle];
}
uint32_t ConnectToPort(std::string name) {
if (name != "sm:") {
ns_abort("Attempt to connect to unknown service\n");
}
return NewHandle(&sm);
}
uint32_t ProcMessage(IpcService *handler, uint8_t buf[]) {
return 0;
}
};

11
Svc.cpp
View file

@ -231,10 +231,19 @@ uint64_t SignalProcessWideKey(uint64_t semaAddr, uint64_t target) {
}
std::tuple<uint64_t, uint32_t> ConnectToPort(uint64_t name) {
return make_tuple(0, 0);
ns_print("ConnectToPort\n");
return make_tuple(0, IPC::ConnectToPort(ARMv8::ReadString(name)));
}
uint64_t SendSyncRequest(uint32_t handle) {
ns_print("SendSyncRequest\n");
uint8_t msgbuf[0x100];
ARMv8::ReadBytes (ARMv8::GetTls(), msgbuf, 0x100);
auto handler = IPC::GetHandle(handle);
if (!handler) {
ns_abort ("Cannnot find session handler\n");
}
IPC::ProcMessage(handler, msgbuf);
return 0;
}

View file

@ -34,7 +34,7 @@ struct ARMv8State {
uint64_t tpidr_el[4];
};
uint64_t tpidrro_el[1];
uint64_t tczid_el[1];
uint64_t tczid_el[1];
} sysr;
};
@ -87,6 +87,8 @@ void Dump();
void DumpJson(FILE *fp);
uint64_t GetTls();
}
#endif

View file

@ -12,6 +12,7 @@ uint32_t ReadU32(const uint64_t gva);
uint64_t ReadU64(const uint64_t gva);
void ReadBytes(uint64_t gva, uint8_t *ptr, int size);
void WriteBytes(uint64_t gva, uint8_t *ptr, int size);
std::string ReadString(uint64_t gva);
void WriteU8(const uint64_t gva, uint8_t value);
void WriteU16(const uint64_t gva, uint16_t value);

44
include/Ipc.hpp Normal file
View file

@ -0,0 +1,44 @@
#ifndef _IPC_HPP
#define _IPC_HPP
class IpcInMessage {
public:
IpcInMessage() {}
};
class IpcOutMessage {
public:
IpcOutMessage() {}
};
class IpcService {
public:
IpcService() {}
virtual uint32_t Dispatch(IpcInMessage *req, IpcOutMessage *resp) {}
};
class SmService : public IpcService {
public:
SmService() { }
uint32_t GetService(std::string name, IpcService *service);
uint32_t Dispatch(IpcInMessage *req, IpcOutMessage *resp);
};
namespace IPC {
extern std::unordered_map<std::string, IpcService> services;
void Initialize();
uint32_t NewHandle(IpcService *srv);
IpcService *GetHandle(uint32_t handle);
uint32_t ConnectToPort(std::string name);
uint32_t ProcMessage(IpcService *handler, uint8_t buf[]);
};
#endif

View file

@ -21,6 +21,7 @@ using namespace std;
#include "Util.hpp"
#include "NintendoObject.hpp"
#include "Cpu.hpp"
#include "Ipc.hpp"
#include "Svc.hpp"
#include "ARMv8/ARMv8.hpp"
#include "ARMv8/Disassembler.hpp"