My Project
emulate.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
18 
19 #ifndef __CPUI_EMULATE__
20 #define __CPUI_EMULATE__
21 
22 #include "memstate.hh"
23 #include "translate.hh"
24 
25 class Emulate; // Forward declaration
26 
37 class BreakTable {
38 public:
39  virtual ~BreakTable(void) {};
40 
46  virtual void setEmulate(Emulate *emu)=0;
47 
55  virtual bool doPcodeOpBreak(PcodeOpRaw *curop)=0;
56 
64  virtual bool doAddressBreak(const Address &addr)=0;
65 };
66 
78 protected:
80 public:
81  BreakCallBack(void);
82  virtual ~BreakCallBack(void) {}
83  virtual bool pcodeCallback(PcodeOpRaw *op);
84  virtual bool addressCallback(const Address &addr);
85  void setEmulate(Emulate *emu);
86 };
87 
91 
92 {
93  emulate = (Emulate *)0;
94 }
95 
103 
104 {
105  return true;
106 }
107 
115 inline bool BreakCallBack::addressCallback(const Address &addr)
116 
117 {
118  return true;
119 }
120 
124 
125 {
126  emulate = emu;
127 }
128 
138  Emulate *emulate;
139  Translate *trans;
140  map<Address,BreakCallBack *> addresscallback;
141  map<uintb,BreakCallBack *> pcodecallback;
142 public:
144  void registerPcodeCallback(const string &nm,BreakCallBack *func);
145  void registerAddressCallback(const Address &addr,BreakCallBack *func);
146  virtual void setEmulate(Emulate *emu);
147  virtual bool doPcodeOpBreak(PcodeOpRaw *curop);
148  virtual bool doAddressBreak(const Address &addr);
149 };
150 
155 
156 {
157  emulate = (Emulate *)0;
158  trans = t;
159 }
160 
168 class Emulate {
169 protected:
170  bool emu_halted;
172  virtual void executeUnary(void)=0;
173  virtual void executeBinary(void)=0;
174  virtual void executeLoad(void)=0;
175  virtual void executeStore(void)=0;
176 
182  virtual void executeBranch(void)=0;
183 
189  virtual bool executeCbranch(void)=0;
190  virtual void executeBranchind(void)=0;
191  virtual void executeCall(void)=0;
192  virtual void executeCallind(void)=0;
193  virtual void executeCallother(void)=0;
194  virtual void executeMultiequal(void)=0;
195  virtual void executeIndirect(void)=0;
196  virtual void executeSegmentOp(void)=0;
197  virtual void executeCpoolRef(void)=0;
198  virtual void executeNew(void)=0;
199  virtual void fallthruOp(void)=0;
200 public:
201  Emulate(void) { emu_halted = true; currentBehave = (OpBehavior *)0; }
202  virtual ~Emulate(void) {}
203  void setHalt(bool val);
204  bool getHalt(void) const;
205  virtual void setExecuteAddress(const Address &addr)=0;
206  virtual Address getExecuteAddress(void) const=0;
207  void executeCurrentOp(void);
208 };
209 
214 inline void Emulate::setHalt(bool val)
215 
216 {
217  emu_halted = val;
218 }
219 
224 inline bool Emulate::getHalt(void) const
225 
226 {
227  return emu_halted;
228 }
229 
242 
243 class EmulateMemory : public Emulate {
244 protected:
247  virtual void executeUnary(void);
248  virtual void executeBinary(void);
249  virtual void executeLoad(void);
250  virtual void executeStore(void);
251  virtual void executeBranch(void);
252  virtual bool executeCbranch(void);
253  virtual void executeBranchind(void);
254  virtual void executeCall(void);
255  virtual void executeCallind(void);
256  virtual void executeCallother(void);
257  virtual void executeMultiequal(void);
258  virtual void executeIndirect(void);
259  virtual void executeSegmentOp(void);
260  virtual void executeCpoolRef(void);
261  virtual void executeNew(void);
262 public:
264  EmulateMemory(MemoryState *mem) { memstate = mem; currentOp = (PcodeOpRaw *)0; }
265  MemoryState *getMemoryState(void) const;
266 };
267 
270 
271 {
272  return memstate;
273 }
274 
278 class PcodeEmitCache : public PcodeEmit {
279  vector<PcodeOpRaw *> &opcache;
280  vector<VarnodeData *> &varcache;
281  const vector<OpBehavior *> &inst;
282  uintm uniq;
283  VarnodeData *createVarnode(const VarnodeData *var);
284 public:
285  PcodeEmitCache(vector<PcodeOpRaw *> &ocache,vector<VarnodeData *> &vcache,
286  const vector<OpBehavior *> &in,uintb uniqReserve);
287  virtual void dump(const Address &addr,OpCode opc,VarnodeData *outvar,VarnodeData *vars,int4 isize);
288 };
289 
297  Translate *trans;
298  vector<PcodeOpRaw *> opcache;
299  vector<VarnodeData *> varcache;
300  vector<OpBehavior *> inst;
301  BreakTable *breaktable;
302  Address current_address;
303  bool instruction_start;
304  int4 current_op;
305  int4 instruction_length;
306  void clearCache(void);
307  void createInstruction(const Address &addr);
308  void establishOp(void);
309 protected:
310  virtual void fallthruOp(void);
311  virtual void executeBranch(void);
312  virtual void executeCallother(void);
313 public:
315  ~EmulatePcodeCache(void);
316  bool isInstructionStart(void) const;
317  int4 numCurrentOps(void) const;
318  int4 getCurrentOpIndex(void) const;
319  PcodeOpRaw *getOpByIndex(int4 i) const;
320  virtual void setExecuteAddress(const Address &addr);
321  virtual Address getExecuteAddress(void) const;
322  void executeInstruction(void);
323 };
324 
331 
332 {
333  return instruction_start;
334 }
335 
338 inline int4 EmulatePcodeCache::numCurrentOps(void) const
339 
340 {
341  return opcache.size();
342 }
343 
348 
349 {
350  return current_op;
351 }
352 
358 
359 {
360  return opcache[i];
361 }
362 
365 
366 {
367  return current_address;
368 }
369 
548 #endif
bool getHalt(void) const
Get the halt state of the emulator.
Definition: emulate.hh:224
int4 numCurrentOps(void) const
Return number of pcode ops in translation of current instruction.
Definition: emulate.hh:338
A basic instantiation of a breakpoint table.
Definition: emulate.hh:137
OpCode
The op-code defining a specific p-code operation (PcodeOp)
Definition: opcodes.hh:35
Abstract class for emitting pcode to an application.
Definition: translate.hh:76
void setEmulate(Emulate *emu)
Associate a particular emulator with this breakpoint.
Definition: emulate.hh:123
A collection of breakpoints for the emulator.
Definition: emulate.hh:37
A pcode-based emulator interface.
Definition: emulate.hh:168
EmulateMemory(MemoryState *mem)
Construct given a memory state.
Definition: emulate.hh:264
A breakpoint object.
Definition: emulate.hh:77
int4 getCurrentOpIndex(void) const
Get the index of current pcode op within current instruction.
Definition: emulate.hh:347
BreakTableCallBack(Translate *t)
Basic breaktable constructor.
Definition: emulate.hh:154
PcodeOpRaw * getOpByIndex(int4 i) const
Get pcode op in current instruction translation by index.
Definition: emulate.hh:357
virtual bool doAddressBreak(const Address &addr)=0
Invoke any breakpoints associated with this machine address.
MemoryState * getMemoryState(void) const
Get the emulator&#39;s memory state.
Definition: emulate.hh:269
Class encapsulating the action/behavior of specific pcode opcodes.
Definition: opbehavior.hh:42
A low-level representation of a single pcode operation.
Definition: pcoderaw.hh:94
PcodeOpRaw * currentOp
Current op to execute.
Definition: emulate.hh:246
Emulate(void)
generic emulator constructor
Definition: emulate.hh:201
virtual bool addressCallback(const Address &addr)
Call back method for address based breakpoints.
Definition: emulate.hh:115
virtual bool pcodeCallback(PcodeOpRaw *op)
Call back method for pcode based breakpoints.
Definition: emulate.hh:102
An abstract Emulate class using a MemoryState object as the backing machine state.
Definition: emulate.hh:243
A SLEIGH based implementation of the Emulate interface.
Definition: emulate.hh:296
A low-level machine address for labelling bytes and data.
Definition: address.hh:46
void setHalt(bool val)
Set the halt state of the emulator.
Definition: emulate.hh:214
The interface to a translation engine for a processor.
Definition: translate.hh:293
OpBehavior * currentBehave
Behavior of the next op to execute.
Definition: emulate.hh:171
virtual Address getExecuteAddress(void) const
Get current execution address.
Definition: emulate.hh:364
MemoryState * memstate
The memory state of the emulator.
Definition: emulate.hh:245
bool emu_halted
Set to true if the emulator is halted.
Definition: emulate.hh:170
Classes for keeping track of memory state during emulation.
All storage/state for a pcode machine.
Definition: memstate.hh:148
P-code emitter that dumps its raw Varnodes and PcodeOps to an in memory cache.
Definition: emulate.hh:278
virtual bool doPcodeOpBreak(PcodeOpRaw *curop)=0
Invoke any breakpoints associated with this particular pcodeop.
Classes for disassembly and pcode generation.
bool isInstructionStart(void) const
Return true if we are at an instruction start.
Definition: emulate.hh:330
virtual void setEmulate(Emulate *emu)=0
Associate a particular emulator with breakpoints in this table.
Data defining a specific memory location.
Definition: pcoderaw.hh:33
Emulate * emulate
The emulator currently associated with this breakpoint.
Definition: emulate.hh:79
BreakCallBack(void)
Generic breakpoint constructor.
Definition: emulate.hh:90