Add initial support of IPC message exchange

This commit is contained in:
rkx1209 2018-04-06 18:09:46 +09:00
parent da5e13ca0d
commit 423fa2c8b8
7 changed files with 146 additions and 12 deletions

View file

@ -21,8 +21,8 @@ int Interpreter::SingleStep() {
void Interpreter::Run() {
debug_print ("Running with Interpreter\n");
static uint64_t counter = 0;
//uint64_t estimate = 3415400, mx = 1000;
uint64_t estimate = 0, mx = 3420000;
uint64_t estimate = 3415400, mx = 1000;
//uint64_t estimate = 0, mx = 3420000;
while (Cpu::GetState () == Cpu::State::Running) {
if (GdbStub::enabled) {
if (GdbStub::cont) {
@ -36,7 +36,7 @@ void Interpreter::Run() {
}
}
} else {
if (counter >= estimate){
if (counter >= estimate){
Cpu::DumpMachine ();
}
if (counter >= estimate + mx)

95
Ipc.cpp
View file

@ -1,7 +1,82 @@
/* nsemu - LGPL - Copyright 2018 rkx1209<rkx1209dev@gmail.com> */
#include "Nsemu.hpp"
void IpcMessage::ParseMessage() {
uint32_t *buf = (uint32_t *) raw_ptr;
type = buf[0] & 0xFFFF;
x_cnt = (buf[0] >> 16) & 0xF;
a_cnt = (buf[0] >> 20) & 0xF;
b_cnt = (buf[0] >> 24) & 0xF;
w_cnt = buf[1] & 0x3FF;
has_c = ((buf[1] >> 10) & 0x3) != 0;
// domainHandle = 0;
// domainCommand = 0;
bool has_hd = (buf[1] >> 31) == 1; //Handle descriptor enabled?
int pos = 2;
if (has_hd) {
uint32_t hd = buf[pos++];
has_pid = hd & 1;
copy_cnt = (hd >> 1) & 0xf;
move_cnt = hd >> 5;
if(has_pid) {
pid = *((uint64_t *) &buf[pos]);
pos += 2;
}
copy_off = pos * 4;
pos += copy_cnt;
move_off = pos * 4;
pos += move_cnt;
}
desc_off = pos * 4;
pos += x_cnt * 2;
pos += a_cnt * 3;
pos += b_cnt * 3;
raw_off = pos * 4;
if(pos & 3)
pos += 4 - (pos & 3);
if (is_domainobj && type == 4) {
domain_cmd = buf[pos] & 0xff;
domain_handle = buf[pos + 1];
pos += 4;
}
payload_off = pos * 4;
cmd_id = GetData<uint32_t>(0);
}
uint32_t SmService::Dispatch(IpcInMessage *req, IpcOutMessage *resp) {
void IpcMessage::GenBuf(unsigned int _move_cnt, unsigned int _copy_cnt, unsigned int data_bytes) {
move_cnt = _move_cnt;
copy_cnt = _copy_cnt;
uint8_t *obuf = raw_ptr;
obuf[0] = 0;
if(move_cnt != 0 || copy_cnt != 0) {
obuf[1] = ((move_cnt != 0 && !is_domainobj) || copy_cnt != 0) ? (1U << 31) : 0;
obuf[2] = (copy_cnt << 1) | ((is_domainobj ? 0 : move_cnt) << 5);
}
auto pos = 2 + (((move_cnt != 0 && !is_domainobj) || copy_cnt != 0) ? (1 + move_cnt + copy_cnt) : 0);
auto start = pos;
if(pos & 3)
pos += 4 - (pos & 3);
if(is_domainobj) {
obuf[pos] = move_cnt;
pos += 4;
}
realdata_off = is_domainobj ? move_cnt << 2 : 0;
auto data_words = (realdata_off >> 2) + (data_bytes & 3) ? (data_bytes >> 2) + 1 : (data_bytes >> 2);
obuf[1] |= 4 + (is_domainobj ? 4 : 0) + 4 + data_words;
payload_off = pos * 4;
obuf[pos] = byte_swap32_str("SFCO");
}
void IpcMessage::SetErrorCode(uint32_t error_code) {
error_code = error_code;
if (raw_ptr) {
raw_ptr[(payload_off >> 2) + 2] = error_code;
}
}
uint32_t SmService::Dispatch(IpcMessage *req, IpcMessage *resp) {
}
uint32_t SmService::GetService(std::string name, IpcService *service) {
@ -18,6 +93,7 @@ namespace IPC {
static uint32_t handle_id;
static SmService sm;
std::unordered_map<std::string, IpcService> services;
bool is_domainobj = false;
static std::unordered_map<uint32_t, IpcService *> handles;
void Initialize() {
@ -41,6 +117,23 @@ uint32_t ConnectToPort(std::string name) {
}
uint32_t ProcMessage(IpcService *handler, uint8_t buf[]) {
uint8_t obuf[0x100];
memset(obuf, 0, 0x100);
IpcMessage req(buf, is_domainobj);
req.ParseMessage();
IpcMessage resp(obuf, is_domainobj);
uint32_t ret = 0xf601;
switch(req.type) {
case 2: //Close
break;
case 4: //Normal
ret = handler->Dispatch(&req, &resp);
break;
case 5: //Control
break;
}
return 0;
}

View file

@ -48,6 +48,17 @@ RAMBlock *FindRAMBlock(Nsemu *nsemu, uint64_t addr, size_t len) {
return nullptr;
}
std::list<std::tuple<uint64_t,uint64_t, int>> GetRegions() {
std::list<std::tuple<uint64_t,uint64_t, int>> ret;
for (int i = 0; i < sizeof(mem_map) / sizeof(RAMBlock); i++) {
uint64_t addr = mem_map[i].addr;
size_t length = mem_map[i].length;
int perm = mem_map[i].perm;
ret.push_back(make_tuple(addr, addr + length, perm));
}
return ret;
}
static bool _CopyMemEmu(void *data, uint64_t gpa, size_t len, bool load) {
void *emu_mem = (void *)&pRAM[gpa];
if (load) {

View file

@ -121,6 +121,8 @@ uint64_t UnmapMemory(uint64_t dest, uint64_t src, uint64_t size) {
}
std::tuple<uint64_t, uint64_t> QueryMemory(uint64_t meminfo, uint64_t pageinfo, uint64_t addr) {
ns_print("QueryMemory 0x%lx\n", addr);
return make_tuple(0, 0);
}

View file

@ -1,34 +1,58 @@
#ifndef _IPC_HPP
#define _IPC_HPP
class IpcInMessage {
class IpcMessage {
public:
IpcInMessage() {}
};
IpcMessage() {}
IpcMessage(uint8_t *buf, bool is_domainobj) : raw_ptr(buf), is_domainobj(is_domainobj) {}
/* IPC command structure */
unsigned int type;
unsigned int x_cnt, a_cnt, b_cnt, w_cnt, has_c;
/* Handle descriptr (if enabled)*/
unsigned int has_pid, pid, move_cnt, copy_cnt;
/* Domain */
unsigned int domain_handle, domain_cmd;
/* Data */
unsigned int cmd_id;
class IpcOutMessage {
public:
IpcOutMessage() {}
uint32_t error_code; // Use only in response message
template<typename T>
T GetData(unsigned int offset) {
return *((T *) (raw_ptr + payload_off + 8 + offset));
}
template<typename T>
T getDataPointer(uint offset) {
return (T) (raw_ptr + payload_off + 8 + offset);
}
void GenBuf(unsigned int _move_cnt, unsigned int _copy_cnt, unsigned int _data_bytes);
void SetErrorCode(uint32_t error_code);
void ParseMessage();
private:
unsigned int copy_off, move_off, desc_off, raw_off, payload_off, realdata_off;
uint8_t *raw_ptr;
bool is_domainobj;
};
class IpcService {
public:
IpcService() {}
virtual uint32_t Dispatch(IpcInMessage *req, IpcOutMessage *resp) {}
virtual uint32_t Dispatch(IpcMessage *req, IpcMessage *resp) { return 0; }
};
class SmService : public IpcService {
public:
SmService() { }
uint32_t GetService(std::string name, IpcService *service);
uint32_t Dispatch(IpcInMessage *req, IpcOutMessage *resp);
uint32_t Dispatch(IpcMessage *req, IpcMessage *resp);
};
namespace IPC {
extern std::unordered_map<std::string, IpcService> services;
extern bool is_domainobj;
void Initialize();
uint32_t NewHandle(IpcService *srv);

View file

@ -28,6 +28,7 @@ extern uint64_t heap_size;
void InitMemmap(Nsemu *nsemu);
RAMBlock *FindRAMBlock(Nsemu *nsemu, uint64_t addr, size_t len);
std::list<std::tuple<uint64_t,uint64_t, int>> GetRegions();
bool CopytoEmu(Nsemu *nsemu, void *data, uint64_t addr, size_t len);
bool CopytoEmuByName(Nsemu *nsemu, void *data, std::string name, size_t len);
bool CopyfromEmu(Nsemu *nsemu, void *data, uint64_t addr, size_t len);

View file

@ -10,7 +10,10 @@
#include <functional>
#include <iostream>
#include <string>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <memory>
#include <vector>