Add initial support of Thread

This commit is contained in:
rkx1209 2018-07-16 22:36:53 +09:00
parent 968a95b3d5
commit 389dd73ca1
10 changed files with 495 additions and 458 deletions

View file

@ -12,8 +12,9 @@ void Init() {
cpu_engine = Interpreter::get_instance ();
cpu_engine->Init ();
PC = 0x0;
//PC = 0x30f0;
SP = 0x3100000;
Thread *main_thread = ThreadManager::Create();
X(1) = main_thread->handle; // Assign main thread handle to X1
SYSR.tpidrro_el[0] = tls_base;
SYSR.tczid_el[0] = 0x4; //FIXME: calclulate at runtime
Memory::AddMemmap (tls_base, tls_size);

View file

@ -9,7 +9,7 @@ bool DeepTrace;
void Init() {
ARMv8::Init ();
SVC::Init ();
// TODO: Thread::Init()
ThreadManager::Init();
}
void Run() {

View file

@ -213,14 +213,14 @@ def generateCaller(qname, fname, func):
yield '%s* %s;' % (rest[0][0], tn)
params.append(tn)
yield AFTER, 'if(%s != nullptr)' % tn
yield AFTER, '\tresp->SetMove(%i, IPC::NewHandle((IpcService *)%s));' % (objOff, tn)
yield AFTER, '\tresp->SetMove(%i, NewHandle((IpcService *)%s));' % (objOff, tn)
objOff += 1
elif type == 'IpcService':
tn = tempname()
yield 'IpcService *%s;' % tn
params.append(tn)
yield AFTER, 'if(%s != nullptr)' % tn
yield AFTER, '\tresp->SetCopy(%i, IPC::NewHandle(%s));' % (hndOff, tn)
yield AFTER, '\tresp->SetCopy(%i, NewHandle(%s));' % (hndOff, tn)
hndOff += 1
elif type == 'pid':
assert False

11
Ipc.cpp
View file

@ -81,27 +81,18 @@ void IpcMessage::SetErrorCode() {
namespace IPC {
static uint32_t handle_id;
static SmService sm;
std::unordered_map<std::string, IpcService *> services;
bool is_domainobj = false;
std::unordered_map<uint32_t, IpcService *> handles;
//std::unordered_map<uint32_t, KObject *> handles;
#define SERVICE(str, iface) do { services[str] = new iface(); } while(0)
void InitIPC() {
sm.Initialize();
//handle_id = 0xde00; // XXX: Magic number?
handle_id = 0xde01; // XXX: Initial thread should have 0xde00 handle in TLS (See. ThreadManager::create in Mephisito)
SERVICE_MAPPING(); // From IpcStubs.hpp
}
uint32_t NewHandle(IpcService *srv) {
handles[handle_id] = srv;
ns_print("New Handle 0x%x\n",handle_id);
return handle_id++;
}
uint32_t ConnectToPort(std::string name) {
if (name != "sm:") {
ns_abort("Attempt to connect to unknown service\n");

View file

@ -4,6 +4,8 @@
Nsemu *Nsemu::inst = nullptr;
static std::thread cpu_thread;
uint32_t handle_id;
std::unordered_map<uint32_t, KObject *> handles;
static void LoadNso(Nsemu *nsemu, string path) {
Nso nso (path);
@ -23,6 +25,7 @@ bool Nsemu::BootUp(const std::string& path) {
Memory::InitMemmap (this);
LoadNso (this, path);
IPC::InitIPC();
handle_id = 0xde00; // XXX: Magic number?
cpu_thread = std::thread (CpuThread);
/* Run cpu */
cpu_thread.join ();

18
Thread.cpp Normal file
View file

@ -0,0 +1,18 @@
#include "Nsemu.hpp"
namespace ThreadManager {
std::unordered_map<uint32_t, Thread *> threads;
unsigned long thread_id;
void Init() {
thread_id = 0;
}
Thread *Create() {
Thread *thread = new Thread();
threads[thread_id++] = thread;
thread->handle = NewHandle(thread);
return thread;
}
};

View file

@ -1,10 +1,5 @@
#ifndef _IPC_HPP
#define _IPC_HPP
class KObject {
public:
virtual ~KObject() {}
virtual void Close() {}
};
class IpcMessage {
public:
@ -107,7 +102,7 @@ private:
bool is_domainobj;
};
class IpcService {
class IpcService : public KObject {
public:
IpcService() : handle(0xf000){}
virtual uint32_t Dispatch(IpcMessage *req, IpcMessage *resp) { return 0; }
@ -120,19 +115,17 @@ class IUnknown : public IpcService {
namespace IPC {
extern std::unordered_map<std::string, IpcService*> services;
extern std::unordered_map<uint32_t, IpcService *> handles;
//extern std::unordered_map<uint32_t, KObject *> handles;
extern bool is_domainobj;
void InitIPC();
uint32_t NewHandle(IpcService *srv);
template<typename T>
T GetHandle(uint32_t handle) {
if (handles.find(handle) == handles.end()) {
return nullptr;
}
IpcService *srv = handles[handle];
KObject *srv = handles[handle];
return static_cast<T>(srv);
}

File diff suppressed because it is too large Load diff

View file

@ -31,13 +31,29 @@ 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"
#include "ARMv8/Interpreter.hpp"
#include "ARMv8/MMU.hpp"
class KObject {
public:
KObject() {}
};
extern uint32_t handle_id;
extern std::unordered_map<uint32_t, KObject *> handles;
template<typename T> uint32_t NewHandle(T* obj) {
handles[handle_id] = (KObject *)obj;
ns_print("New Handle 0x%x\n", handle_id);
return handle_id++;
}
#include "Ipc.hpp"
#include "Thread.hpp"
/* Global NSEMU singleton class .*/
class Nsemu {
private:

16
include/Thread.hpp Normal file
View file

@ -0,0 +1,16 @@
#ifndef _THREAD_HPP
#define _THREAD_HPP
class KObject;
class Thread : public KObject {
public:
uint32_t handle;
Thread() : KObject() { }
};
namespace ThreadManager {
void Init();
Thread *Create();
}
#endif