ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
Plugin.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
15#include "etiss/CPUArch.h"
16#include "etiss/CPUCore.h"
17#include "etiss/jit/ReturnCode.h"
18#include <fstream>
19
20using namespace etiss::plugin::errorInjection;
21
22BlockAccurateHandler::Error::Error(std::string reg, unsigned errorid, etiss::uint64 time_ps, etiss::uintMax xor_,
23 etiss::uintMax and_, etiss::uintMax or_)
24 : reg(reg), errorid(errorid), time_ps(time_ps), xor_(xor_), and_(and_), or_(or_)
25{
26}
27
29{
30 next_time_ps = (etiss::uint64)(etiss::int64)-1;
31 last_time_ps = 0;
32}
34
39
40void BlockAccurateHandler::add(etiss::uint64 time_ps, unsigned errorid, std::string register_, etiss::uintMax xor_,
41 etiss::uintMax and_, etiss::uintMax or_)
42{
43 errors_.push_back(BlockAccurateHandler::Error(register_, errorid, time_ps, xor_, and_, or_));
45 if (!errors_.empty())
46 {
47 next_time_ps = errors_.front().time_ps;
48 }
49 else
50 {
51 next_time_ps = (etiss::uint64)(etiss::int64)-1;
52 }
53}
54
56{
59 {
60 for (auto iter = errors_.begin(); iter != errors_.end() && iter->time_ps <= cpu->cpuTime_ps;)
61 {
62 auto f = plugin_core_->getStruct()->findName(iter->reg);
63 if (!f)
64 {
65 return ETISS_RETURNCODE_GENERALERROR;
66 }
67 switch (f->width_)
68 {
69 case 8:
70 {
71 etiss::uint8 val = (etiss::uint8)f->read();
72 val = (etiss::uint8)(((val ^ iter->xor_) & iter->and_) | iter->or_);
73 f->write(val);
74 break;
75 }
76 case 16:
77 {
78 etiss::uint16 val = (etiss::uint16)f->read();
79 val = (etiss::uint16)(((val ^ iter->xor_) & iter->and_) | iter->or_);
80 f->write(val);
81 break;
82 }
83 case 32:
84 {
85 etiss::uint32 val = (etiss::uint32)f->read();
86 std::cout << iter->reg << ": old:" << val;
87 val = (etiss::uint32)(((val ^ iter->xor_) & iter->and_) | iter->or_);
88 std::cout << "new: " << val << std::endl;
89 f->write(val);
90 break;
91 }
92 case 64:
93 {
94 etiss::uint64 val = (etiss::uint64)f->read();
95 val = (etiss::uint64)(((val ^ iter->xor_) & iter->and_) | iter->or_);
96 f->write(val);
97 break;
98 }
99 default:
100 etiss::log(etiss::ERROR, "BlockAccurateHandler: invalid register width");
101 }
102
103 std::stringstream ss;
104 ss << "BlockAccurateHandler: injected error (id: " << iter->errorid << ")";
105 etiss::log(etiss::INFO, ss.str());
106 errors_.erase(iter++);
107 }
108 if (!errors_.empty())
109 {
110 next_time_ps = errors_.front().time_ps;
111 }
112 else
113 {
114 next_time_ps = (etiss::uint64)(etiss::int64)-1;
115 }
116 }
117
118 return etiss::RETURNCODE::NOERROR;
119}
120
121void BlockAccurateHandler::parseFile(std::string filename, std::string reg)
122{
123 std::ifstream file(filename);
124 if (!file)
125 {
126 std::cout << "failed to load error definition file " << filename << std::endl;
127 return;
128 }
129
130 while (file.good())
131 {
132 std::string line;
133 std::getline(file, line);
134 std::stringstream ls(line);
135
136 while (ls.peek() == ' ' || ls.peek() == '\t')
137 {
138 char c;
139 ls >> c;
140 }
141
142 if (ls.peek() == '#')
143 continue;
144
145 etiss::uint64 tmp_cf;
146 ls >> tmp_cf; // get time in ns
147 tmp_cf = tmp_cf * 1000; // ns to ps
148 if (ls.fail())
149 {
150 printf("Invalid line in error definition file\n");
151 continue;
152 }
153 char semicolon;
154 ls >> semicolon;
155 if (ls.fail() || semicolon != ';')
156 {
157 printf("Invalid line in error definition file\n");
158 continue;
159 }
160 unsigned tmp_f;
161 ls >> tmp_f; // get flipped bit
162 if (ls.fail())
163 {
164 printf("Invalid line in error definition file\n");
165 continue;
166 }
167 ls >> semicolon;
168 bool eidv = false;
169 unsigned errorid = 0;
170 if (!ls.fail() && semicolon == ';')
171 { // parse errorid
172 ls >> errorid;
173 eidv = !ls.fail();
174 }
175
176 static unsigned global_errorid_max = 1024;
177
178 errors_.push_back(BlockAccurateHandler::Error(reg, eidv ? errorid : global_errorid_max++, tmp_cf,
179 ((etiss::uintMax)1) << tmp_f));
180
181 std::cout << reg << ": Error ";
182 if (eidv)
183 {
184 std::cout << errorid << " ";
185 }
186 std::cout << "scheduled for time (ps)" << tmp_cf << "; bit " << tmp_f << std::endl;
187 }
188
190
191 if (!errors_.empty())
192 {
193 next_time_ps = errors_.front().time_ps;
194 }
195 else
196 {
197 next_time_ps = static_cast<etiss::uint64>(-1);
198 }
199}
200
202{
203 return "BlockAccurateHandler";
204}
205
207{
208 cpu = cpu_;
209 system = system_;
210 arch = arch_;
211}
213{
214 cpu = nullptr;
215 system = nullptr;
216 arch = nullptr;
217}
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:35
__device__ __2f16 float c
the interface to translate instructions of and processor architecture
Definition CPUArch.h:116
virtual std::shared_ptr< VirtualStruct > getStruct()
Get the virtual structure of this CPUCore instance.
Definition CPUCore.h:122
CPUCore * plugin_core_
holds a pointer to the associated CPUCore instance.
Definition Plugin.h:160
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:22
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:40
virtual etiss::int32 execute()
call to apply errors
Definition Plugin.cpp:55
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:206
virtual void cleanup()
this function is called after cpu execution loop (etiss::CPUCore::execute) finished.
Definition Plugin.cpp:212
void parseFile(std::string filename, std::string reg)
reads a file and adds the errors.
Definition Plugin.cpp:121
@ INFO
Definition Misc.h:87
@ ERROR
Definition Misc.h:85
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition Misc.cpp:94
int printf(__constant const char *st,...) __attribute__((format(printf
basic cpu state structure needed for execution of any cpu architecture.
Definition CPU.h:51
etiss_uint64 cpuTime_ps
simulation time of cpu
Definition CPU.h:59
memory access and time synchronization functions.
Definition System.h:40