ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
Plugin.cpp
Go to the documentation of this file.
1 
53 #include "etiss/CPUArch.h"
54 #include "etiss/CPUCore.h"
55 #include <fstream>
56 
57 using namespace etiss::plugin::errorInjection;
58 
59 BlockAccurateHandler::Error::Error(std::string reg, unsigned errorid, etiss::uint64 time_ps, etiss::uintMax xor_,
60  etiss::uintMax and_, etiss::uintMax or_)
61  : reg(reg), errorid(errorid), time_ps(time_ps), xor_(xor_), and_(and_), or_(or_)
62 {
63 }
64 
66 {
68  last_time_ps = 0;
69 }
71 
73 {
74  return o1.time_ps < o2.time_ps;
75 }
76 
77 void BlockAccurateHandler::add(etiss::uint64 time_ps, unsigned errorid, std::string register_, etiss::uintMax xor_,
78  etiss::uintMax and_, etiss::uintMax or_)
79 {
80  errors_.push_back(BlockAccurateHandler::Error(register_, errorid, time_ps, xor_, and_, or_));
82  if (!errors_.empty())
83  {
84  next_time_ps = errors_.front().time_ps;
85  }
86  else
87  {
89  }
90 }
91 
93 {
96  {
97  for (auto iter = errors_.begin(); iter != errors_.end() && iter->time_ps <= cpu->cpuTime_ps;)
98  {
99  auto f = plugin_core_->getStruct()->findName(iter->reg);
100  if (!f)
101  {
102  return ETISS_RETURNCODE_GENERALERROR;
103  }
104  switch (f->width_)
105  {
106  case 8:
107  {
108  etiss::uint8 val = (etiss::uint8)f->read();
109  val = (etiss::uint8)(((val ^ iter->xor_) & iter->and_) | iter->or_);
110  f->write(val);
111  break;
112  }
113  case 16:
114  {
115  etiss::uint16 val = (etiss::uint16)f->read();
116  val = (etiss::uint16)(((val ^ iter->xor_) & iter->and_) | iter->or_);
117  f->write(val);
118  break;
119  }
120  case 32:
121  {
122  etiss::uint32 val = (etiss::uint32)f->read();
123  std::cout << iter->reg << ": old:" << val;
124  val = (etiss::uint32)(((val ^ iter->xor_) & iter->and_) | iter->or_);
125  std::cout << "new: " << val << std::endl;
126  f->write(val);
127  break;
128  }
129  case 64:
130  {
131  etiss::uint64 val = (etiss::uint64)f->read();
132  val = (etiss::uint64)(((val ^ iter->xor_) & iter->and_) | iter->or_);
133  f->write(val);
134  break;
135  }
136  default:
137  etiss::log(etiss::ERROR, "BlockAccurateHandler: invalid register width");
138  }
139 
140  std::stringstream ss;
141  ss << "BlockAccurateHandler: injected error (id: " << iter->errorid << ")";
142  etiss::log(etiss::INFO, ss.str());
143  errors_.erase(iter++);
144  }
145  if (!errors_.empty())
146  {
147  next_time_ps = errors_.front().time_ps;
148  }
149  else
150  {
152  }
153  }
154 
156 }
157 
158 void BlockAccurateHandler::parseFile(std::string filename, std::string reg)
159 {
160  std::ifstream file(filename);
161  if (!file)
162  {
163  std::cout << "failed to load error definition file " << filename << std::endl;
164  return;
165  }
166 
167  while (file.good())
168  {
169  std::string line;
170  std::getline(file, line);
171  std::stringstream ls(line);
172 
173  while (ls.peek() == ' ' || ls.peek() == '\t')
174  {
175  char c;
176  ls >> c;
177  }
178 
179  if (ls.peek() == '#')
180  continue;
181 
182  etiss::uint64 tmp_cf;
183  ls >> tmp_cf; // get time in ns
184  tmp_cf = tmp_cf * 1000; // ns to ps
185  if (ls.fail())
186  {
187  printf("Invalid line in error definition file\n");
188  continue;
189  }
190  char semicolon;
191  ls >> semicolon;
192  if (ls.fail() || semicolon != ';')
193  {
194  printf("Invalid line in error definition file\n");
195  continue;
196  }
197  unsigned tmp_f;
198  ls >> tmp_f; // get flipped bit
199  if (ls.fail())
200  {
201  printf("Invalid line in error definition file\n");
202  continue;
203  }
204  ls >> semicolon;
205  bool eidv = false;
206  unsigned errorid = 0;
207  if (!ls.fail() && semicolon == ';')
208  { // parse errorid
209  ls >> errorid;
210  eidv = !ls.fail();
211  }
212 
213  static unsigned global_errorid_max = 1024;
214 
215  errors_.push_back(BlockAccurateHandler::Error(reg, eidv ? errorid : global_errorid_max++, tmp_cf,
216  ((etiss::uintMax)1) << tmp_f));
217 
218  std::cout << reg << ": Error ";
219  if (eidv)
220  {
221  std::cout << errorid << " ";
222  }
223  std::cout << "scheduled for time (ps)" << tmp_cf << "; bit " << tmp_f << std::endl;
224  }
225 
227 
228  if (!errors_.empty())
229  {
230  next_time_ps = errors_.front().time_ps;
231  }
232  else
233  {
234  next_time_ps = static_cast<etiss::uint64>(-1);
235  }
236 }
237 
239 {
240  return "BlockAccurateHandler";
241 }
242 
244 {
245  cpu = cpu_;
246  system = system_;
247  arch = arch_;
248 }
250 {
251  cpu = nullptr;
252  system = nullptr;
253  arch = nullptr;
254 }
etiss_uint8 uint8
Definition: 386-GCC.h:76
etiss_int32 int32
Definition: 386-GCC.h:81
etiss_int64 int64
Definition: 386-GCC.h:83
etiss_uint32 uint32
Definition: 386-GCC.h:80
etiss_uint16 uint16
Definition: 386-GCC.h:78
etiss_uint64 uint64
Definition: 386-GCC.h:82
contains neccesary interfaces for instruction translation.
defines main cpu core interface
bool etiss_BlockAccurateHandler_cmp(const BlockAccurateHandler::Error &o1, const BlockAccurateHandler::Error &o2)
Definition: Plugin.cpp:72
__device__ __2f16 float c
the interface to translate instructions of and processor architecture
Definition: CPUArch.h:162
virtual std::shared_ptr< VirtualStruct > getStruct()
Get the virtual structure of this CPUCore instance.
Definition: CPUCore.h:170
CPUCore * plugin_core_
holds a pointer to the associated CPUCore instance.
Definition: Plugin.h:200
Error(std::string reg, unsigned errorid, etiss::uint64 time_ps, etiss::uintMax xor_, etiss::uintMax and_=(etiss::uintMax)(etiss::intMax) -1, etiss::uintMax or_=0)
Definition: Plugin.cpp:59
virtual std::string _getPluginName() const
Definition: Plugin.cpp:238
void add(etiss::uint64 time_ps, unsigned errorid, std::string register_, etiss::uintMax xor_, etiss::uintMax and_=(etiss::uintMax)(etiss::intMax) -1, etiss::uintMax or_=0)
schedule an error
Definition: Plugin.cpp:77
virtual etiss::int32 execute()
call to apply errors
Definition: Plugin.cpp:92
virtual void init(ETISS_CPU *cpu, ETISS_System *system, CPUArch *arch)
this function is called before the plugin is used in the cpu execution loop (etiss::CPUCore::execute)...
Definition: Plugin.cpp:243
virtual void cleanup()
this function is called after cpu execution loop (etiss::CPUCore::execute) finished.
Definition: Plugin.cpp:249
void parseFile(std::string filename, std::string reg)
reads a file and adds the errors.
Definition: Plugin.cpp:158
MM_EXPORT const int32_t NOERROR
@ INFO
Definition: Misc.h:129
@ ERROR
Definition: Misc.h:127
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition: Misc.cpp:125
int printf(__constant const char *st,...) __attribute__((format(printf
basic cpu state structure needed for execution of any cpu architecture.
Definition: CPU.h:89
etiss_uint64 cpuTime_ps
simulation time of cpu
Definition: CPU.h:97
memory access and time synchronization functions.
Definition: System.h:78