-
Notifications
You must be signed in to change notification settings - Fork 43
/
thumb2.c
652 lines (548 loc) · 17 KB
/
thumb2.c
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
/*
Copyright (c) 2013, Jurriaan Bremer
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the darm developer(s) nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "darm.h"
#include "darm-internal.h"
#include "thumb-tbl.h"
#include "thumb2-tbl.h"
#include "thumb2.h"
#define BITMSK_8 ((1 << 8) - 1)
#define ROR(val, rotate) (((val) >> (rotate)) | ((val) << (32 - (rotate))))
#define SIGN_EXTEND32(v, len) (((int32_t)(v) << (32 - len)) >> (32 - len))
void thumb2_parse_reg(darm_t *d, uint16_t w, uint16_t w2);
void thumb2_parse_imm(darm_t *d, uint16_t w, uint16_t w2);
void thumb2_parse_flag(darm_t *d, uint16_t w, uint16_t w2);
void thumb2_parse_misc(darm_t *d, uint16_t w, uint16_t w2);
// 12 -> 32 bit expansion function
// See manual for this
// We don't care about the carry for the moment (should we?)
uint32_t thumb_expand_imm(uint16_t imm12)
{
uint32_t value;
imm12 &= 0xfff;
if((imm12 & 0xc00) == 0) {
switch ((imm12 & 0x300) >> 8) {
case 0:
value = imm12 & 0xff;
break;
case 1:
value = ((imm12 & 0xff) << 16) | (imm12 & 0xff);
break;
case 2:
value = ((imm12 & 0xff) << 24) | ((imm12 & 0xff) << 8);
break;
case 3:
imm12 &= 0xff;
value = imm12 | (imm12 << 8) | (imm12 << 16) | (imm12 << 24);
break;
}
}
else {
uint32_t unrotated = 0x80 | (imm12 & 0x7F);
value = ROR(unrotated, (imm12 & 0xF80) >> 7);
}
return value;
}
void thumb2_decode_immshift(darm_t *d, uint8_t type, uint8_t imm5)
{
switch (type) {
case 0:
d->shift_type = S_LSL;
d->shift = imm5;
break;
case 1:
d->shift_type = S_LSR;
d->shift = imm5 == 0 ? 32 : imm5;
break;
case 2:
d->shift_type = S_ASR;
d->shift = imm5 == 0 ? 32 : imm5;
break;
case 3:
d->shift_type = S_ROR;
d->shift = imm5 == 0 ? 1 : imm5; // RRX! :)
break;
default:
d->shift_type = S_INVLD;
break;
}
}
// Parse the register instruction type
void thumb2_parse_reg(darm_t *d, uint16_t w, uint16_t w2)
{
switch (d->instr_type) {
case T_THUMB2_NO_REG:
break;
case T_THUMB2_RT_REG:
d->Rt = (w2 >> 12) & b1111;
break;
case T_THUMB2_RT_RT2_REG:
d->Rt = (w2 >> 12) & b1111;
d->Rt2 = (w2 >> 8) & b1111;
break;
case T_THUMB2_RM_REG:
d->Rm = (w & b1111);
break;
case T_THUMB2_RD_REG:
d->Rd = (w2 >> 8) & b1111;
break;
case T_THUMB2_RD_RM_REG:
d->Rd = (w2 >> 8) & b1111;
d->Rm = w2 & b1111;
break;
case T_THUMB2_RN_REG:
d->Rn = w & b1111;
break;
case T_THUMB2_RN_RT_REG:
d->Rn = w & b1111;
d->Rt = (w2 >> 12) & b1111;
break;
case T_THUMB2_RN_RT_RT2_REG:
d->Rn = w & b1111;
d->Rt = (w2 >> 12) & b1111;
d->Rt2 = (w2 >> 8) & b1111;
break;
case T_THUMB2_RN_RM_REG:
d->Rn = w & b1111;
d->Rm = w2 & b1111;
break;
case T_THUMB2_RN_RM_RT_REG:
d->Rn = w & b1111;
d->Rm = w2 & b1111;
d->Rt = (w2 >> 12) & b1111;
break;
case T_THUMB2_RN_RD_REG:
d->Rn = w & b1111;
d->Rd = (w2 >> 8) & b1111;
break;
case T_THUMB2_RN_RD_RT_REG:
d->Rn = (w & b1111);
d->Rd = (w2 & b1111);
d->Rt = (w2 >> 12) & b1111;
break;
case T_THUMB2_RN_RD_RT_RT2_REG:
d->Rn = (w & b1111);
d->Rd = (w2 & b1111);
d->Rt = (w2 >> 12) & b1111;
d->Rt2 = (w2 >> 8) & b1111;
break;
case T_THUMB2_RN_RD_RM_REG:
d->Rn = w & b1111;
d->Rm = w2 & b1111;
d->Rd = (w2 >> 8) & b1111;
break;
case T_THUMB2_RN_RD_RM_RA_REG:
d->Rn = w & b1111;
d->Rm = w2 & b1111;
d->Rd = (w2 >> 8) & b1111;
d->Ra = (w2 >> 12) & b1111;
break;
default:
break;
}
}
// Parse the immediate instruction type
void thumb2_parse_imm(darm_t *d, uint16_t w, uint16_t w2)
{
d->I = B_SET;
switch (d->instr_imm_type) {
case T_THUMB2_NO_IMM:
d->I = B_UNSET;
break;
case T_THUMB2_IMM12:
d->imm = w2 & 0xfff;
break;
case T_THUMB2_IMM8:
d->imm = w2 & 0xff;
break;
case T_THUMB2_IMM2:
// 2 bit immediate
d->imm = (w2 >> 4) & b11;
d->shift = d->imm;
d->shift_type = S_LSL;
break;
case T_THUMB2_IMM2_IMM3:
// 2 and 3 bit immediates
// (imm3:imm2)
d->imm = ((w2 >> 10) & b11100) | ((w2 >> 6) & b11);
break;
case T_THUMB2_IMM1_IMM3_IMM8:
// 1, 3 and 8 bit immediates
// i:imm3:imm8 -> imm12 -> imm32
// if bits 9:8 == '10' then zero extend, otherwise thumb expand
if((w & 0x300) == 0x200) {
d->imm = ((w & 0x400) << 1) | ((w2 & 0x7000) >> 4) | (w2 & 0xff);
}
else {
d->imm = ((w & 0x400) << 1) | ((w2 & 0x7000) >> 4) | (w2 & 0xff);
d->imm = thumb_expand_imm(d->imm);
}
break;
default:
break;
}
}
// Parse the flag instruction type
void thumb2_parse_flag(darm_t *d, uint16_t w, uint16_t w2)
{
switch (d->instr_flag_type) {
case T_THUMB2_NO_FLAG:
break;
case T_THUMB2_ROTATE_FLAG:
// Rotate field
d->rotate = (w2 >> 1) & b11000;
break;
case T_THUMB2_U_FLAG:
// U flag
d->U = (w >> 7) & 1 ? B_SET : B_UNSET;
break;
case T_THUMB2_WUP_FLAG:
// W, U and P flags
d->W = (w2 >> 8) & 1 ? B_SET : B_UNSET;
d->U = (w2 >> 9) & 1 ? B_SET : B_UNSET;
d->P = (w2 >> 10) & 1 ? B_SET : B_UNSET;
break;
case T_THUMB2_TYPE_FLAG:
// Type field
// This is always a T_THUMB2_IMM2_IMM3 type
thumb2_decode_immshift(d, (w2 >> 4) & 3, d->imm);
break;
case T_THUMB2_REGLIST_FLAG:
// Reglist field
d->reglist = w2 & 0xffff;
break;
case T_THUMB2_WP_REGLIST_FLAG:
// Reglist field and W, P flags
d->reglist = w2 & 0xffff;
d->W = (w >> 5) & 1 ? B_SET : B_UNSET;
//d->P = (w >> 15) & 1 ? B_SET : B_UNSET;
break;
case T_THUMB2_S_FLAG:
// S flag
d->S = (w >> 4) & 1 ? B_SET : B_UNSET;
break;
case T_THUMB2_S_TYPE_FLAG:
// S flag and type field
d->S = (w >> 4) & 1 ? B_SET : B_UNSET;
thumb2_decode_immshift(d, (w2 >> 4) & 3, d->imm);
break;
default:
break;
}
}
// Parse misc instruction cases
void thumb2_parse_misc(darm_t *d, uint16_t w, uint16_t w2)
{
switch (d->instr) {
case I_B:
d->I = B_SET;
d->S = (w >> 10) & 1 ? B_SET : B_UNSET;
if ((w2 & 0x1000) == 0) {
// T3
// sign_extend(S:J2:J1:imm6:imm11:0, 32)
d->imm =
((w & 0x400) << 10) |
((w2 & 0x800) << 8) |
((w2 & 0x2000) << 5) |
((w & 0x3F) << 12) |
((w2 & 0x7ff) << 1);
d->imm = SIGN_EXTEND32(d->imm, 21);
d->cond = (w >> 6) & b1111;
}
else {
// T4
// I1 = not(J1 xor S);
// I2 = not(J2 xor S);
// imm32 = sign_extend(S:I1:I2:imm10:imm11:0, 32)
d->imm =
((w & 0x400) << 14) |
(((~(w2 >> 13) ^ (w >> 10)) & 1) << 23) |
((~((w2 >> 11) ^ (w >> 10)) & 1) << 22) |
((w & 0x3FF) << 12) |
((w2 & 0x7FF) << 1);
d->imm = SIGN_EXTEND32(d->imm, 25);
}
break;
case I_BL: case I_BLX:
d->I = B_SET;
d->S = (w >> 10) & 1 ? B_SET : B_UNSET;
if ((w2 & 0x1000) == 0) {
// BLX
// I1 = not(J1 xor S); I2 = not(J2 xor S); imm32 = sign_extend(S:I1:I2:imm10H:imm10L:00, 32)
d->imm =
((w & 0x400) << 14) |
((~((w2 >> 13) ^ (w >> 10)) & 1) << 23) |
((~((w2 >> 11) ^ (w >> 10)) & 1) << 22) |
((w & 0x3FF) << 12) |
((w2 & 0x7FE) << 1);
d->imm = SIGN_EXTEND32(d->imm, 25);
d->H = (w & 1) ? B_SET : B_UNSET;
}
else {
// BL
// I1 = not(J1 xor S); I2 = not(J2 xor S); imm32 = sign_extend(S:I1:I2:imm10:imm11:0, 32)
d->imm =
((w & 0x400) << 14) |
(((~((w2 >> 13) ^ (w >> 10))) & 1) << 23) |
((~((w2 >> 11) ^ (w >> 10)) & 1) << 22) |
((w & 0x3FF) << 12) |
((w2 & 0x7FF) << 1);
d->imm = SIGN_EXTEND32(d->imm, 25);
}
break;
case I_BFC: case I_BFI:
d->lsb = d->imm & 0x1f;
d->msb = w2 & 0x1f;
d->width = d->msb + 1 - d->lsb;
break;
case I_LSL: case I_LSR: case I_ASR: case I_ROR:
if(d->I == B_SET) {
d->shift = d->imm;
d->shift_type = ((w2 >> 4) & b11);
}
break;
case I_MOVW: case I_MOVT:
d->imm =
((w & b1111) << 12) |
((w & 0x400) << 1) |
((w2 & 0x7000) >> 4) |
(w2 & 0xff);
break;
// preserve PC in Rd for further use (?)
case I_CMP: case I_CMN: case I_TEQ: case I_TST:
d->Rd = PC;
break;
// option field
case I_DBG: case I_DMB: case I_DSB: case I_ISB:
d->option = w2 & b1111;
break;
// co-proc data processing
// TODO: not implemented
//case I_CPD: case I_CPD2:
//break;
// co-proc load/store memory
case I_LDC: case I_LDC2:
case I_STC: case I_STC2:
d->P = (w >> 8) & 1 ? B_SET : B_UNSET;
d->U = (w >> 7) & 1 ? B_SET : B_UNSET;
d->D = (w >> 6) & 1 ? B_SET : B_UNSET;
d->W = (w >> 5) & 1 ? B_SET : B_UNSET;
// literal or immediate
d->Rn = (w & b1111) == b1111 ? R_INVLD : (w & b1111);
d->I = B_SET;
d->imm = (w2 & 0xff) << 2;
d->coproc = (w2 >> 8) & b1111;
d->CRd = (w2 >> 12) & b1111;
break;
// Weird Rd offset
case I_STREX:
d->Rd = (w2 >> 8) & b1111;
d->imm = (w2 & 0xff) << 2;
break;
// zero-extend corner case with '00' appended
case I_LDRD: case I_LDREX: case I_STRD:
d->imm = (w2 & 0xff) << 2;
d->W = (w >> 5) & 1 ? B_SET : B_UNSET;
d->U = (w >> 7) & 1 ? B_SET : B_UNSET;
d->P = (w >> 8) & 1 ? B_SET : B_UNSET;
break;
// Catch some pop/push inconsistencies
case I_POP: case I_PUSH:
// no flags, TODO fixup
if(w == 0xf85d || w == 0xf84d) {
break;
}
// P flag
if(w == 0xe8bd) {
d->P = (w2 >> 15) & 1 ? B_SET : B_UNSET;
}
d->M = (w2 >> 14) & 1 ? B_SET : B_UNSET;
break;
// co-processor move
case I_MCR: case I_MCR2:
case I_MRC: case I_MRC2:
d->CRm = w2 & b1111;
d->CRn = w & b1111;
d->coproc = (w2 >> 8) & b1111;
d->Rt = (w2 >> 12) & b1111;
d->opc1 = (w >> 5) & b111;
d->opc2 = (w2 >> 5) & b111;
break;
// co-proc move 2 reg
case I_MCRR: case I_MCRR2:
case I_MRRC: case I_MRRC2:
d->coproc = (w2 >> 8) & b1111;
d->Rt = (w2 >> 12) & b1111;
d->opc1 = (w2 >> 4) & b1111;
d->CRm = (w2 & b1111);
d->Rt2 = w & b1111;
break;
case I_MSR:
d->I = B_SET;
d->Rn = w & b1111;
d->mask = d->imm = (w2 >> 10) & b11;
break;
case I_PKH:
// S flag and immediate already set
d->T = (w2 >> 4) & 1;
thumb2_decode_immshift(d, (w2 >> 4) & 2, d->imm);
break;
case I_PLI:
d->Rt = R_INVLD;
d->P = B_INVLD;
d->W = B_INVLD;
if(d->Rn == b1111) {
d->Rn = R_INVLD;
d->imm = w2 & 0xfff;
d->U = ((w >> 7) & 1) ? B_SET : B_UNSET;
}
break;
case I_PLD:
d->Rt = R_INVLD;
d->P = B_INVLD;
if(d->Rn == b1111) {
d->Rn = d->Rm = R_INVLD;
d->imm = w2 & 0xfff;
d->shift_type = S_INVLD;
d->shift = 0;
d->U = (w >> 7) & 1 ? B_SET : B_UNSET;
}
d->W = (w & b1111) != b1111 ? ((w >> 5) & 1) : B_INVLD;
break;
case I_SBFX: case I_UBFX:
d->lsb = d->imm;
d->width = (w2 & 0x1f) + 1;
break;
// N, M flags
case I_SMLABB: case I_SMLABT: case I_SMLATB: case I_SMLATT:
case I_SMULBB: case I_SMULBT: case I_SMULTB: case I_SMULTT:
d->N = (w2 >> 5) & 1 ? B_SET : B_UNSET;
// fall-through
case I_SMLAD: case I_SMLAW:
case I_SMLSD: case I_SMUAD:
case I_SMULW: case I_SMUSD:
d->M = (w2 >> 4) & 1 ? B_SET : B_UNSET;
if(d->Ra == b1111) {
d->Ra = R_INVLD;
}
break;
// N, M, Rdhi, Rdlo flags
case I_SMLALBB: case I_SMLALBT: case I_SMLALTB: case I_SMLALTT:
d->N = (w2 >> 5) & 1 ? B_SET : B_UNSET;
// fall-through
case I_SMLSLD: case I_SMLALD:
d->M = (w2 >> 4) & 1 ? B_SET : B_UNSET;
// fall-through
case I_SMLAL: case I_SMULL:
case I_UMAAL: case I_UMLAL: case I_UMULL:
d->RdHi = (w2 >> 8) & b1111;
d->RdLo = (w2 >> 12) & b1111;
break;
case I_SMMLA: case I_SMMLS: case I_SMMUL:
d->R = (w2 >> 4) & 1 ? B_SET : B_UNSET;
break;
case I_SSAT: case I_USAT:
thumb2_decode_immshift(d, (w >> 4) & 2, d->imm);
d->sat_imm = w2 & 0x1f;
break;
case I_SSAT16: case I_USAT16:
d->sat_imm = w2 & 0xf;
break;
case I_STM: case I_STMDB:
d->W = (w >> 5) & 1 ? B_SET : B_UNSET;
d->M = (w2 >> 14) & 1 ? B_SET : B_UNSET;
break;
case I_TBB: case I_TBH:
d->H = (w2 >> 4) & 1 ? B_SET : B_UNSET;
break;
default:
break;
}
}
static int thumb2_disasm(darm_t *d, uint16_t w, uint16_t w2)
{
d->instr = thumb2_decode_instruction(d, w, w2);
if(d->instr == I_INVLD) {
return -1;
}
thumb2_parse_reg(d, w, w2);
thumb2_parse_imm(d, w, w2);
thumb2_parse_flag(d, w, w2);
thumb2_parse_misc(d, w, w2);
d->instr_type = T_INVLD;
return 0;
}
// placeholder function for printing out thumb2 instructions
// This is here until the format string problem is resolved
// TODO: This lacks a lot of functionality, for debug only, replace with better function
char *darm_thumb2_str(darm_t *d)
{
int index=0, offset=0;
static char stringbuf[512];
for (int i = 0; i < THUMB2_INSTRUCTION_COUNT; i++) {
if(d->instr == thumb2_instr_labels[i]) {
index = i;
break;
}
}
offset += sprintf(stringbuf + offset, "%s",
thumb2_instruction_strings[index]);
if(d->Rd != R_INVLD) {
offset += sprintf(stringbuf+offset, "rd%i,", d->Rd);
}
if(d->Rt != R_INVLD) {
offset += sprintf(stringbuf+offset, "rt%i,", d->Rt2);
}
if(d->Rt2 != R_INVLD) {
offset += sprintf(stringbuf+offset, "rt2%i,", d->Rt);
}
if(d->Rn != R_INVLD) {
offset += sprintf(stringbuf+offset, "rn%i,", d->Rn);
}
if(d->Rm != R_INVLD) {
offset += sprintf(stringbuf+offset, "rm%i ", d->Rm);
}
if(d->I == B_SET) {
offset += sprintf(stringbuf+offset, "#0x%x", d->imm);
}
return stringbuf;
}
int darm_thumb2_disasm(darm_t *d, uint16_t w, uint16_t w2)
{
darm_init(d);
d->w = (w << 16) | w2;
// we set all conditional flags to "execute always" by default, as most
// thumb instructions don't feature a conditional flag
d->cond = C_AL;
switch (w >> 11) {
case b11101: case b11110: case b11111:
return thumb2_disasm(d, w, w2);
default:
return -1;
}
}