ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
SelectiveSysWrapper.cpp
Go to the documentation of this file.
2 #include "etiss/ETISS.h"
3 
4 namespace etiss
5 {
6 namespace plugin
7 {
8 
9 struct WrapSystem
10 {
13  std::vector<void *> jitHandles;
14 };
15 
17 {
18  auto ret = new WrapSystem();
19  ret->sys.handle = system->handle;
20  ret->orig = system;
21 
22  auto jit = ((CPUCore *)cpu->_etiss_private_handle_)->getJIT();
23  std::set<std::string> headers;
24  headers.insert(etiss::jitFiles());
25  std::string jitErr;
26 
27  auto makeWrapper = [&](void *funcPtr, void *handle, bool isDbg, bool isSync, bool isIRead)
28  {
29  std::string funcAddr = std::to_string((uintptr_t)funcPtr);
30  std::string params, cast, call;
31  params += "void *handle";
32  cast += "void*";
33  call += "(void*)" + std::to_string((uintptr_t)handle) + "ull";
34  if (!isDbg)
35  {
36  params += ", ETISS_CPU *cpu";
37  cast += ", ETISS_CPU*";
38  call += ", cpu";
39  }
40  if (!isSync)
41  {
42  params += ", etiss_uint64 addr";
43  cast += ", etiss_uint64";
44  call += ", addr";
45  if (!isIRead)
46  {
47  params += ", etiss_uint8 *buffer";
48  cast += ", etiss_uint8*";
49  call += ", buffer";
50  }
51  params += ", etiss_uint32 length";
52  cast += ", etiss_uint32";
53  call += ", length";
54  }
55 
56  std::string code = "#include \"etiss/jit/System.h\"\n";
57  code += "etiss_int32 wrapper(" + params + "){\n";
58  code += " return ((etiss_int32 (*)(" + cast + "))" + funcAddr + ")(" + call + ");\n}\n";
59 
60  void *jitHandle = jit->translate(code, headers, {}, {}, jitErr, false);
61  ret->jitHandles.push_back(jitHandle);
62  return jit->getFunction(jitHandle, "wrapper", jitErr);
63  };
64 
65  ETISS_System wrapInfo = getWrapInfo(system);
66  ret->sys = *system;
67  if (wrapInfo.iread)
68  {
69  ret->sys.iread =
70  (decltype(ret->sys.iread))makeWrapper((void *)wrapInfo.iread, wrapInfo.handle, false, false, true);
71  }
72  if (wrapInfo.iwrite)
73  {
74  ret->sys.iwrite =
75  (decltype(ret->sys.iwrite))makeWrapper((void *)wrapInfo.iwrite, wrapInfo.handle, false, false, false);
76  }
77  if (wrapInfo.dread)
78  {
79  ret->sys.dread =
80  (decltype(ret->sys.dread))makeWrapper((void *)wrapInfo.dread, wrapInfo.handle, false, false, false);
81  }
82  if (wrapInfo.dwrite)
83  {
84  ret->sys.dwrite =
85  (decltype(ret->sys.dwrite))makeWrapper((void *)wrapInfo.dwrite, wrapInfo.handle, false, false, false);
86  }
87  if (wrapInfo.dbg_read)
88  {
89  ret->sys.dbg_read =
90  (decltype(ret->sys.dbg_read))makeWrapper((void *)wrapInfo.dbg_read, wrapInfo.handle, true, false, false);
91  }
92  if (wrapInfo.dbg_write)
93  {
94  ret->sys.dbg_write =
95  (decltype(ret->sys.dbg_write))makeWrapper((void *)wrapInfo.dbg_write, wrapInfo.handle, true, false, false);
96  }
97  if (wrapInfo.syncTime)
98  {
99  ret->sys.syncTime =
100  (decltype(ret->sys.syncTime))makeWrapper((void *)wrapInfo.syncTime, wrapInfo.handle, false, true, false);
101  }
102 
103  return (ETISS_System *)ret;
104 }
105 
107 {
108  auto wrapSys = (WrapSystem *)system;
109  auto jit = ((CPUCore *)cpu->_etiss_private_handle_)->getJIT();
110  for (auto h : wrapSys->jitHandles)
111  {
112  jit->free(h);
113  }
114  ETISS_System *orig = (wrapSys)->orig;
115  delete wrapSys;
116  return orig;
117 }
118 
119 } // namespace plugin
120 
121 } // namespace etiss
Header file of the ETISS library.
CPUCore is responsible for the simulation of a CPU core in ETISS.
Definition: CPUCore.h:113
ETISS_System * unwrap(ETISS_CPU *cpu, ETISS_System *system) final
undo wrap function call this function will be called AFTER etiss::Plugin::cleanup
virtual ETISS_System getWrapInfo(ETISS_System *origSystem)=0
Defines which System functions to wrap.
ETISS_System * wrap(ETISS_CPU *cpu, ETISS_System *system) final
change/wrap the passed system structure.
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition: Benchmark.h:53
std::string jitFiles()
Get ETISS JIT files path.
Definition: Misc.cpp:592
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c-base.h:62
basic cpu state structure needed for execution of any cpu architecture.
Definition: CPU.h:89
void * _etiss_private_handle_
private helper handle for plugins
Definition: CPU.h:107
memory access and time synchronization functions.
Definition: System.h:78
etiss_int32(* dwrite)(void *handle, ETISS_CPU *cpu, etiss_uint64 addr, etiss_uint8 *buffer, etiss_uint32 length)
write data
Definition: System.h:97
etiss_int32(* iread)(void *handle, ETISS_CPU *cpu, etiss_uint64 addr, etiss_uint32 length)
used to simulate an instruction fetch.
Definition: System.h:84
void * handle
custom handle that will be passed to the functions of this structure
Definition: System.h:116
etiss_int32(* dbg_write)(void *handle, etiss_uint64 addr, etiss_uint8 *buffer, etiss_uint32 length)
direct debug write
Definition: System.h:108
etiss_int32(* dbg_read)(void *handle, etiss_uint64 addr, etiss_uint8 *buffer, etiss_uint32 length)
direct debug read
Definition: System.h:104
etiss_int32(* dread)(void *handle, ETISS_CPU *cpu, etiss_uint64 addr, etiss_uint8 *buffer, etiss_uint32 length)
read data
Definition: System.h:93
void(* syncTime)(void *handle, ETISS_CPU *cpu)
called after a block to synchronize the time
Definition: System.h:114
etiss_int32(* iwrite)(void *handle, ETISS_CPU *cpu, etiss_uint64 addr, etiss_uint8 *buffer, etiss_uint32 length)
write instruction data over instruction bus
Definition: System.h:88
std::vector< void * > jitHandles