1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|
//===-- lldb_EmulateInstructionARM.h ------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef lldb_EmulateInstructionARM_h_
#define lldb_EmulateInstructionARM_h_
#include "lldb/Core/EmulateInstruction.h"
#include "lldb/Core/Error.h"
#include "Plugins/Process/Utility/ARMDefines.h"
namespace lldb_private {
// ITSession - Keep track of the IT Block progression.
class ITSession
{
public:
ITSession() : ITCounter(0), ITState(0) {}
~ITSession() {}
// InitIT - Initializes ITCounter/ITState.
bool InitIT(unsigned short bits7_0);
// ITAdvance - Updates ITCounter/ITState as IT Block progresses.
void ITAdvance();
// InITBlock - Returns true if we're inside an IT Block.
bool InITBlock();
// LastInITBlock - Returns true if we're the last instruction inside an IT Block.
bool LastInITBlock();
// GetCond - Gets condition bits for the current thumb instruction.
uint32_t GetCond();
private:
uint32_t ITCounter; // Possible values: 0, 1, 2, 3, 4.
uint32_t ITState; // A2.5.2 Consists of IT[7:5] and IT[4:0] initially.
};
class EmulateInstructionARM : public EmulateInstruction
{
public:
typedef enum
{
eEncodingA1,
eEncodingA2,
eEncodingA3,
eEncodingA4,
eEncodingA5,
eEncodingT1,
eEncodingT2,
eEncodingT3,
eEncodingT4,
eEncodingT5
} ARMEncoding;
static void
Initialize ();
static void
Terminate ();
virtual const char *
GetPluginName()
{
return "EmulateInstructionARM";
}
virtual const char *
GetShortPluginName()
{
return "lldb.emulate-instruction.arm";
}
virtual uint32_t
GetPluginVersion()
{
return 1;
}
virtual void
GetPluginCommandHelp (const char *command, Stream *strm)
{
}
virtual lldb_private::Error
ExecutePluginCommand (Args &command, Stream *strm)
{
Error error;
error.SetErrorString("no plug-in commands are supported");
return error;
}
virtual Log *
EnablePluginLogging (Stream *strm, Args &command)
{
return NULL;
}
enum Mode
{
eModeInvalid,
eModeARM,
eModeThumb
};
EmulateInstructionARM (void *baton,
ReadMemory read_mem_callback,
WriteMemory write_mem_callback,
ReadRegister read_reg_callback,
WriteRegister write_reg_callback) :
EmulateInstruction (lldb::eByteOrderLittle, // Byte order for ARM
4, // Address size in byte
baton,
read_mem_callback,
write_mem_callback,
read_reg_callback,
write_reg_callback),
m_arm_isa (0),
m_inst_mode (eModeInvalid),
m_inst_cpsr (0),
m_it_session ()
{
}
virtual bool
SetArchitecture (const ArchSpec &arch);
virtual bool
ReadInstruction ();
virtual bool
EvaluateInstruction ();
uint32_t
ArchVersion();
bool
ConditionPassed ();
uint32_t
CurrentCond ();
// InITBlock - Returns true if we're in Thumb mode and inside an IT Block.
bool InITBlock();
// LastInITBlock - Returns true if we're in Thumb mode and the last instruction inside an IT Block.
bool LastInITBlock();
bool
BranchWritePC(const Context &context, uint32_t addr);
bool
BXWritePC(Context &context, uint32_t addr);
bool
LoadWritePC(Context &context, uint32_t addr);
bool
ALUWritePC(Context &context, uint32_t addr);
Mode
CurrentInstrSet();
bool
SelectInstrSet(Mode arm_or_thumb);
bool
WriteBits32Unknown (int n);
bool
WriteBits32UnknownToMemory (lldb::addr_t address);
bool
UnalignedSupport();
typedef struct
{
uint32_t result;
uint8_t carry_out;
uint8_t overflow;
} AddWithCarryResult;
AddWithCarryResult
AddWithCarry(uint32_t x, uint32_t y, uint8_t carry_in);
// Helper method to read the content of an ARM core register.
uint32_t
ReadCoreReg (uint32_t regnum, bool *success);
// See A8.6.96 MOV (immediate) Operation.
// Default arguments are specified for carry and overflow parameters, which means
// not to update the respective flags even if setflags is true.
bool
WriteCoreRegOptionalFlags (Context &context,
const uint32_t result,
const uint32_t Rd,
bool setflags,
const uint32_t carry = ~0u,
const uint32_t overflow = ~0u);
bool
WriteCoreReg (Context &context,
const uint32_t result,
const uint32_t Rd)
{
// Don't set the flags.
return WriteCoreRegOptionalFlags(context, result, Rd, false);
}
// See A8.6.35 CMP (immediate) Operation.
// Default arguments are specified for carry and overflow parameters, which means
// not to update the respective flags.
bool
WriteFlags (Context &context,
const uint32_t result,
const uint32_t carry = ~0u,
const uint32_t overflow = ~0u);
inline uint64_t
MemARead (EmulateInstruction::Context &context,
lldb::addr_t address,
uint32_t size,
uint64_t fail_value,
bool *success_ptr)
{
// This is a stub function corresponding to "MemA[]" in the ARM manual pseudocode, for
// aligned reads from memory. Since we are not trying to write a full hardware simulator, and since
// we are running in User mode (rather than Kernel mode) and therefore won't have access to many of the
// system registers we would need in order to fully implement this function, we will just call
// ReadMemoryUnsigned from here. In the future, if we decide we do need to do more faithful emulation of
// the hardware, we can update this function appropriately.
return ReadMemoryUnsigned (context, address, size, fail_value, success_ptr);
}
inline bool
MemAWrite (EmulateInstruction::Context &context,
lldb::addr_t address,
uint64_t data_val,
uint32_t size)
{
// This is a stub function corresponding to "MemA[]" in the ARM manual pseudocode, for
// aligned writes to memory. Since we are not trying to write a full hardware simulator, and since
// we are running in User mode (rather than Kernel mode) and therefore won't have access to many of the
// system registers we would need in order to fully implement this function, we will just call
// WriteMemoryUnsigned from here. In the future, if we decide we do need to do more faithful emulation of
// the hardware, we can update this function appropriately.
return WriteMemoryUnsigned (context, address, data_val, size);
}
inline uint64_t
MemURead (EmulateInstruction::Context &context,
lldb::addr_t address,
uint32_t size,
uint64_t fail_value,
bool *success_ptr)
{
// This is a stub function corresponding to "MemU[]" in the ARM manual pseudocode, for
// unaligned reads from memory. Since we are not trying to write a full hardware simulator, and since
// we are running in User mode (rather than Kernel mode) and therefore won't have access to many of the
// system registers we would need in order to fully implement this function, we will just call
// ReadMemoryUnsigned from here. In the future, if we decide we do need to do more faithful emulation of
// the hardware, we can update this function appropriately.
return ReadMemoryUnsigned (context, address, size, fail_value, success_ptr);
}
inline bool
MemUWrite (EmulateInstruction::Context &context,
lldb::addr_t address,
uint64_t data_val,
uint32_t size)
{
// This is a stub function corresponding to "MemU[]" in the ARM manual pseudocode, for
// unaligned writes to memory. Since we are not trying to write a full hardware simulator, and since
// we are running in User mode (rather than Kernel mode) and therefore won't have access to many of the
// system registers we would need in order to fully implement this function, we will just call
// WriteMemoryUnsigned from here. In the future, if we decide we do need to do more faithful emulation of
// the hardware, we can update this function appropriately.
return WriteMemoryUnsigned (context, address, data_val, size);
}
protected:
// Typedef for the callback function used during the emulation.
// Pass along (ARMEncoding)encoding as the callback data.
typedef enum
{
eSize16,
eSize32
} ARMInstrSize;
typedef struct
{
uint32_t mask;
uint32_t value;
uint32_t variants;
EmulateInstructionARM::ARMEncoding encoding;
ARMInstrSize size;
bool (EmulateInstructionARM::*callback) (EmulateInstructionARM::ARMEncoding encoding);
const char *name;
} ARMOpcode;
static ARMOpcode*
GetARMOpcodeForInstruction (const uint32_t opcode);
static ARMOpcode*
GetThumbOpcodeForInstruction (const uint32_t opcode);
// A8.6.123 PUSH
bool
EmulatePUSH (ARMEncoding encoding);
// A8.6.122 POP
bool
EmulatePOP (ARMEncoding encoding);
// A8.6.8 ADD (SP plus immediate)
bool
EmulateADDRdSPImm (ARMEncoding encoding);
// A8.6.97 MOV (register) -- Rd == r7|ip and Rm == sp
bool
EmulateMOVRdSP (ARMEncoding encoding);
// A8.6.97 MOV (register) -- move from r8-r15 to r0-r7
bool
EmulateMOVLowHigh (ARMEncoding encoding);
// A8.6.59 LDR (literal)
bool
EmulateLDRRtPCRelative (ARMEncoding encoding);
// A8.6.8 ADD (SP plus immediate)
bool
EmulateADDSPImm (ARMEncoding encoding);
// A8.6.9 ADD (SP plus register)
bool
EmulateADDSPRm (ARMEncoding encoding);
// A8.6.23 BL, BLX (immediate)
bool
EmulateBLXImmediate (ARMEncoding encoding);
// A8.6.24 BLX (register)
bool
EmulateBLXRm (ARMEncoding encoding);
// A8.6.25 BX
bool
EmulateBXRm (ARMEncoding encoding);
// A8.6.212 SUB (immediate, ARM) -- Rd == r7 and Rm == ip
bool
EmulateSUBR7IPImm (ARMEncoding encoding);
// A8.6.215 SUB (SP minus immediate) -- Rd == ip
bool
EmulateSUBIPSPImm (ARMEncoding encoding);
// A8.6.215 SUB (SP minus immediate)
bool
EmulateSUBSPImm (ARMEncoding encoding);
// A8.6.194 STR (immediate, ARM) -- Rn == sp
bool
EmulateSTRRtSP (ARMEncoding encoding);
// A8.6.355 VPUSH
bool
EmulateVPUSH (ARMEncoding encoding);
// A8.6.354 VPOP
bool
EmulateVPOP (ARMEncoding encoding);
// A8.6.218 SVC (previously SWI)
bool
EmulateSVC (ARMEncoding encoding);
// A8.6.50 IT
bool
EmulateIT (ARMEncoding encoding);
// A8.6.16 B
bool
EmulateB (ARMEncoding encoding);
// A8.6.27 CBNZ, CBZ
bool
EmulateCB (ARMEncoding encoding);
// A8.6.226 TBB, TBH
bool
EmulateTB (ARMEncoding encoding);
// A8.6.5 ADD (immediate, ARM)
bool
EmulateADDImmARM (ARMEncoding encoding);
// A8.6.6 ADD (register)
bool
EmulateADDReg (ARMEncoding encoding);
// A8.6.97 MOV (register)
bool
EmulateMOVRdRm (ARMEncoding encoding);
// A8.6.96 MOV (immediate)
bool
EmulateMOVRdImm (ARMEncoding encoding);
// A8.6.35 CMP (immediate)
bool
EmulateCMPImm (ARMEncoding encoding);
// A8.6.36 CMP (register)
bool
EmulateCMPReg (ARMEncoding encoding);
// A8.6.14 ASR (immediate)
bool
EmulateASRImm (ARMEncoding encoding);
// A8.6.15 ASR (register)
bool
EmulateASRReg (ARMEncoding encoding);
// A8.6.88 LSL (immediate)
bool
EmulateLSLImm (ARMEncoding encoding);
// A8.6.89 LSL (register)
bool
EmulateLSLReg (ARMEncoding encoding);
// A8.6.90 LSR (immediate)
bool
EmulateLSRImm (ARMEncoding encoding);
// A8.6.91 LSR (register)
bool
EmulateLSRReg (ARMEncoding encoding);
// A8.6.139 ROR (immediate)
bool
EmulateRORImm (ARMEncoding encoding);
// A8.6.140 ROR (register)
bool
EmulateRORReg (ARMEncoding encoding);
// A8.6.141 RRX
bool
EmulateRRX (ARMEncoding encoding);
// Helper method for ASR, LSL, LSR, ROR (immediate), and RRX
bool
EmulateShiftImm (ARMEncoding encoding, ARM_ShifterType shift_type);
// Helper method for ASR, LSL, LSR, and ROR (register)
bool
EmulateShiftReg (ARMEncoding encoding, ARM_ShifterType shift_type);
// A8.6.53 LDM/LDMIA/LDMFD
bool
EmulateLDM (ARMEncoding encoding);
// A8.6.54 LDMDA/LDMFA
bool
EmulateLDMDA (ARMEncoding encoding);
// A8.6.55 LDMDB/LDMEA
bool
EmulateLDMDB (ARMEncoding encoding);
// A8.6.56 LDMIB/LDMED
bool
EmulateLDMIB (ARMEncoding encoding);
// A8.6.57 LDR (immediate, Thumb) -- Encoding T1
bool
EmulateLDRRtRnImm (ARMEncoding encoding);
// A8.6.188 STM/STMIA/STMEA
bool
EmulateSTM (ARMEncoding encoding);
// A8.6.189 STMDA/STMED
bool
EmulateSTMDA (ARMEncoding encoding);
// A8.6.190 STMDB/STMFD
bool
EmulateSTMDB (ARMEncoding encoding);
// A8.6.191 STMIB/STMFA
bool
EmulateSTMIB (ARMEncoding encoding);
// A8.6.192 STR (immediate, Thumb)
bool
EmulateSTRThumb(ARMEncoding encoding);
// A8.6.194 STR (register)
bool
EmulateSTRRegister (ARMEncoding encoding);
// A8.6.195 STRB (immediate, Thumb)
bool
EmulateSTRBThumb (ARMEncoding encoding);
// A8.6.1 ADC (immediate)
bool
EmulateADCImm (ARMEncoding encoding);
// A8.6.2 ADC (Register)
bool
EmulateADCReg (ARMEncoding encoding);
// A8.6.10 ADR
bool
EmulateADR (ARMEncoding encoding);
// A8.6.11 AND (immediate)
bool
EmulateANDImm (ARMEncoding encoding);
// A8.6.12 AND (register)
bool
EmulateANDReg (ARMEncoding encoding);
// A8.6.19 BIC (immediate) - Encoding A1
bool
EmulateBICImmediate (ARMEncoding encoding);
// A8.6.20 BIC (register) - Encoding T1, A1
bool
EmulateBICRegister (ARMEncoding encoding);
// A8.6.26 BXJ
bool
EmulateBXJ (ARMEncoding encoding);
// A8.6.32 CMN (immediate)
bool
EmulateCMNImm (ARMEncoding encoding);
// A8.6.33 CMN (register)
bool
EmulateCMNReg (ARMEncoding encoding);
// A8.6.44 EOR (immediate)
bool
EmulateEORImm (ARMEncoding encoding);
// A8.6.45 EOR (register)
bool
EmulateEORReg (ARMEncoding encoding);
// A8.6.58 LDR (immediate, ARM) - Encoding A1
bool
EmulateLDRImmediateARM (ARMEncoding encoding);
// A8.6.60 LDR (register) - Encoding T1, T2, A1
bool
EmulateLDRRegister (ARMEncoding encoding);
// A8.6.61 LDRB (immediate, Thumb) - Encoding T1, T2
bool
EmulateLDRBImmediate (ARMEncoding encoding);
// A8.6.63 LDRB (literal) - Encoding T1
bool
EmulateLDRBLiteral (ARMEncoding encoding);
// A8.6.64 LDRB (register) - Encoding T1
bool
EmulateLDRBRegister (ARMEncoding encoding);
// A8.6.73 LDRH (immediate, Thumb) - Encoding T1, T2
bool
EmulateLDRHImmediateThumb (ARMEncoding encoding);
// A8.6.75 LDRH (literal) - Encoding T1
bool
EmulateLDRHLiteral (ARMEncoding encoding);
// A8.6.76 LDRH (register) - Encoding T1, T2
bool
EmulateLDRHRegister (ARMEncoding encoding);
// A8.6.78 LDRSB (immediate) - Encoding T1
bool
EmulateLDRSBImmediate (ARMEncoding encoding);
// A8.6.79 LDRSB (literal) - Encoding T1
bool
EmulateLDRSBLiteral (ARMEncoding encoding);
// A8.6.80 LDRSB (register) - Encoding T1, T2
bool
EmulateLDRSBRegister (ARMEncoding encoding);
// A8.6.82 LDRSH (immediate) - Encoding T1
bool
EmulateLDRSHImmediate (ARMEncoding encoding);
// A8.6.83 LDRSH (literal) - Encoding T1
bool
EmulateLDRSHLiteral (ARMEncoding encoding);
// A8.6.84 LDRSH (register) - Encoding T1, T2
bool
EmulateLDRSHRegister (ARMEncoding encoding);
// A8.6.105 MUL
bool
EmulateMUL (ARMEncoding encoding);
// A8.6.106 MVN (immediate)
bool
EmulateMVNImm (ARMEncoding encoding);
// A8.6.107 MVN (register)
bool
EmulateMVNReg (ARMEncoding encoding);
// A8.6.113 ORR (immediate)
bool
EmulateORRImm (ARMEncoding encoding);
// A8.6.114 ORR (register)
bool
EmulateORRReg (ARMEncoding encoding);
// A8.6.117 PLD (immediate, literal) - Encoding T1, T2, T3, A1
bool
EmulatePLDImmediate (ARMEncoding encoding);
// A8.6.119 PLI (immediate,literal) - Encoding T3, A1
bool
EmulatePLIImmediate (ARMEncoding encoding);
// A8.6.120 PLI (register) - Encoding T1, A1
bool
EmulatePLIRegister (ARMEncoding encoding);
// A8.6.141 RSB (immediate)
bool
EmulateRSBImm (ARMEncoding encoding);
// A8.6.142 RSB (register)
bool
EmulateRSBReg (ARMEncoding encoding);
// A8.6.144 RSC (immediate)
bool
EmulateRSCImm (ARMEncoding encoding);
// A8.6.145 RSC (register)
bool
EmulateRSCReg (ARMEncoding encoding);
// A8.6.150 SBC (immediate)
bool
EmulateSBCImm (ARMEncoding encoding);
// A8.6.151 SBC (register)
bool
EmulateSBCReg (ARMEncoding encoding);
// A8.6.211 SUB (immediate, Thumb) - Encoding T1, T2
bool
EmulateSUBImmThumb (ARMEncoding encoding);
// A8.6.212 SUB (immediate, ARM) - Encoding A1
bool
EmulateSUBImmARM (ARMEncoding encoding);
// A8.6.222 SXTB - Encoding T1
bool
EmulateSXTB (ARMEncoding encoding);
// A8.6.224 SXTH - EncodingT1
bool
EmulateSXTH (ARMEncoding encoding);
// A8.6.227 TEQ (immediate) - Encoding A1
bool
EmulateTEQImm (ARMEncoding encoding);
// A8.6.228 TEQ (register) - Encoding A1
bool
EmulateTEQReg (ARMEncoding encoding);
// A8.6.230 TST (immediate) - Encoding A1
bool
EmulateTSTImm (ARMEncoding encoding);
// A8.6.231 TST (register) - Encoding T1, A1
bool
EmulateTSTReg (ARMEncoding encoding);
// A8.6.262 UXTB - Encoding T1
bool
EmulateUXTB (ARMEncoding encoding);
// A8.6.264 UXTH - Encoding T1
bool
EmulateUXTH (ARMEncoding encoding);
// B6.1.8 RFE
bool
EmulateRFE (ARMEncoding encoding);
uint32_t m_arm_isa;
Mode m_inst_mode;
uint32_t m_inst_cpsr;
uint32_t m_new_inst_cpsr; // This can get updated by the opcode.
ITSession m_it_session;
};
} // namespace lldb_private
#endif // lldb_EmulateInstructionARM_h_
|