Repository navigation
Expand file tree
/
Copy patharm_data_processing.c
More file actions
450 lines (414 loc) · 13.8 KB
/
Copy patharm_data_processing.c
File metadata and controls
450 lines (414 loc) · 13.8 KB
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
/*
Armator - simulateur de jeu d'instruction ARMv5T � but p�dagogique
Copyright (C) 2011 Guillaume Huard
Ce programme est libre, vous pouvez le redistribuer et/ou le modifier selon les
termes de la Licence Publique G�n�rale GNU publi�e par la Free Software
Foundation (version 2 ou bien toute autre version ult�rieure choisie par vous).
Ce programme est distribu� car potentiellement utile, mais SANS AUCUNE
GARANTIE, ni explicite ni implicite, y compris les garanties de
commercialisation ou d'adaptation dans un but sp�cifique. Reportez-vous � la
Licence Publique G�n�rale GNU pour plus de d�tails.
Vous devez avoir re�u une copie de la Licence Publique G�n�rale GNU en m�me
temps que ce programme ; si ce n'est pas le cas, �crivez � la Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
�tats-Unis.
Contact: Guillaume.Huard@imag.fr
B�timent IMAG
700 avenue centrale, domaine universitaire
38401 Saint Martin d'H�res
*/
#include "arm_data_processing.h"
#include "arm_exception.h"
#include "arm_constants.h"
#include "arm_branch_other.h"
#include "util.h"
#include "debug.h"
/* data processing information bits */
#define S 20
/* data processing instructions */
#define AND 0
#define EOR 1
#define SUB 2
#define RSB 3
#define ADD 4
#define ADC 5
#define SBC 6
#define RSC 7
#define TST 8
#define TEQ 9
#define CMP 10
#define CMN 11
#define ORR 12
#define MOV 13
#define BIC 14
#define MVN 15
/* immediate msr's information bit */
#define R 22
int add_carry(int32_t a, int32_t b, int32_t *result) {
return __builtin_add_overflow(a, b, result) || (*result < a);
}
int add_overflow(int32_t a, int32_t b, int32_t *result) {
return __builtin_add_overflow(a, b, result);
}
int sub_carry(int32_t a, int32_t b, int32_t *result) {
return __builtin_sub_overflow(a, b, result) || (*result > a);
}
int sub_overflow(int32_t a, int32_t b, int32_t *result) {
return __builtin_sub_overflow(a, b, result);
}
int data_processing(arm_core p, uint8_t S_bit, uint8_t opcode, uint8_t Rd,
uint32_t Rn_value, uint32_t shifter_operand, uint8_t shifter_carry_out) {
/* Read current flag */
uint32_t current_CPSR = arm_read_cpsr(p);
uint8_t N_bit = get_bit(current_CPSR, N);
uint8_t Z_bit = get_bit(current_CPSR, Z);
uint8_t C_bit = get_bit(current_CPSR, C);
uint8_t V_bit = get_bit(current_CPSR, V);
uint32_t new_CPSR;
int change_Rd, result, tmp;
/* CALCULATE RESULT BASED OPCODE */
switch (opcode) {
case AND:
result = Rn_value & shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 1;
break;
case EOR:
result = Rn_value ^ shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 1;
break;
case SUB:
result = Rn_value - shifter_operand;
C_bit = sub_carry(Rn_value, shifter_operand, &result);
V_bit = sub_overflow(Rn_value, shifter_operand, &result);
change_Rd = 1;
break;
case RSB:
result = shifter_operand - Rn_value;
C_bit = sub_carry(shifter_operand, Rn_value, &result);
V_bit = sub_overflow(shifter_operand, Rn_value, &result);
change_Rd = 1;
break;
case ADD:
result = Rn_value + shifter_operand;
C_bit = add_carry(Rn_value, shifter_operand, &result);
V_bit = add_overflow(Rn_value, shifter_operand, &result);
change_Rd = 1;
break;
case ADC:
tmp = Rn_value + shifter_operand;
result = tmp + C_bit;
C_bit = add_carry(Rn_value, shifter_operand, &tmp) ||
add_carry(tmp, C_bit, &result);
V_bit = add_overflow(Rn_value, shifter_operand, &tmp) ||
add_overflow(tmp, C_bit, &result);
change_Rd = 1;
break;
case SBC:
tmp = Rn_value - shifter_operand;
result = tmp - !C_bit;
C_bit = sub_carry(Rn_value, shifter_operand, &tmp) ||
sub_carry(tmp, !C_bit, &result);
V_bit = sub_overflow(Rn_value, shifter_operand, &tmp) ||
sub_overflow(tmp, !C_bit, &result);
change_Rd = 1;
break;
case RSC:
tmp = shifter_operand - Rn_value;
result = tmp - !C_bit;
C_bit = sub_carry(shifter_operand, Rn_value, &tmp) ||
sub_carry(tmp, !C_bit, &result);
V_bit = sub_overflow(shifter_operand, Rn_value, &tmp) ||
sub_overflow(tmp, !C_bit, &result);
change_Rd = 1;
break;
case TST:
result = Rn_value & shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 0;
break;
case TEQ:
result = Rn_value ^ shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 0;
break;
case CMP:
result = Rn_value - shifter_operand;
C_bit = sub_carry(Rn_value, shifter_operand, &result);
V_bit = sub_overflow(Rn_value, shifter_operand, &result);
change_Rd = 0;
break;
case CMN:
result = Rn_value + shifter_operand;
C_bit = add_carry(Rn_value, shifter_operand, &result);
V_bit = add_overflow(Rn_value, shifter_operand, &result);
change_Rd = 0;
break;
case ORR:
result = Rn_value | shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 1;
break;
case MOV:
result = shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 1;
break;
case BIC:
result = Rn_value & ~shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 1;
break;
default: // MVN
result = ~shifter_operand;
C_bit = shifter_carry_out;
change_Rd = 1;
break;
}
// Get the 2 other flags
N_bit = get_bit(result, 31);
Z_bit = (result == 0);
// Write result in Rd if there is a need to
if (change_Rd)
arm_write_register(p, Rd, result);
/* CHANGE CPSR ACCORDING TO S */
if (S_bit && Rd == 15) {
if (arm_current_mode_has_spsr(p)) {
arm_write_cpsr(p, arm_read_spsr(p));
return SUCCESSFULLY_DECODED;
} else {
warning("UNPREDICTABLE (Attempt to use SPSR but current mode does not have SPSR)\n");
return UNDEFINED_INSTRUCTION;
}
} else if (S_bit) {
// Write to CPSR
new_CPSR = ((current_CPSR & 0x0FFFFFFF) |
((uint32_t)N_bit << N) | ((uint32_t)Z_bit << Z) |
((uint32_t)C_bit << C) | ((uint32_t)V_bit << V));
arm_write_cpsr(p, new_CPSR);
return SUCCESSFULLY_DECODED;
} else
return SUCCESSFULLY_DECODED;
}
/* Decoding functions for different classes of instructions */
int arm_data_processing_shift(arm_core p, uint32_t ins) {
/* Read current flag */
uint8_t C_bit = get_bit(arm_read_cpsr(p), C);
/* Decode instruction */
uint8_t S_bit = get_bit(ins, S);
uint8_t opcode = get_bits(ins, 24, 21);
uint8_t Rn = get_bits(ins, 19, 16);
uint32_t Rn_value = arm_read_register(p, Rn);
uint8_t Rd = get_bits(ins, 15, 12);
uint16_t shifter_operand_code = get_bits(ins, 11, 0);
uint8_t shift_imm = get_bits(ins, 11, 7);
uint8_t Rm = get_bits(ins, 3, 0);
uint32_t Rm_value = arm_read_register(p, Rm);
uint8_t Rs = get_bits(ins, 11, 8);
uint32_t Rs_value = arm_read_register(p, Rs);
uint8_t Rs_valueLB = get_bits(Rs_value, 7, 0);; // least significant byte
uint32_t shifter_operand;
uint8_t shifter_carry_out;
/* CALCULATE THE SHIFTER OPERAND */
if (get_bits(shifter_operand_code, 6, 4) == 0b000) {
// lsl by immediate
if (Rm == 15 || Rn == 15) // weird use of r15
Rm_value = arm_read_register(p, 15) + 4;
if (shift_imm == 0) { /* Register operand */
shifter_operand = Rm_value;
shifter_carry_out = C_bit;
} else { /* shift_imm > 0 */
shifter_operand = Rm_value << shift_imm;
shifter_carry_out = get_bit(Rm_value, 32 - shift_imm);
}
} else if (get_bits(shifter_operand_code, 7, 4) == 0b0001) {
// lsl by register
if (Rd == 15 || Rm == 15 || Rn == 15 || Rs == 15) {
warning("UNPREDICTABLE (Attempt to specify PC as register in shift by register)\n");
return UNDEFINED_INSTRUCTION;
}
if (Rs_valueLB == 0) {
shifter_operand = Rm_value;
shifter_carry_out = C_bit;
} else if (Rs_valueLB < 32) {
shifter_operand = Rm_value << Rs_valueLB;
shifter_carry_out = get_bit(Rm_value, 32 - Rs_valueLB);
} else if (Rs_valueLB == 32) {
shifter_operand = 0;
shifter_carry_out = get_bit(Rm_value, 0);
} else { /* Rs[7:0] > 32 */
shifter_operand = 0;
shifter_carry_out = 0;
}
} else if (get_bits(shifter_operand_code, 6, 4) == 0b010) {
// lsr by immediate
if (Rm == 15 || Rn == 15) // weird use of r15
Rm_value = arm_read_register(p, 15) + 4;
if (shift_imm == 0) {
shifter_operand = 0;
shifter_carry_out = get_bit(Rm_value, 31);
} else { /* shift_imm > 0 */
shifter_operand = Rm_value >> shift_imm;
shifter_carry_out = get_bit(Rm_value, shift_imm - 1);
}
} else if (get_bits(shifter_operand_code, 7, 4) == 0b0011) {
// lsr by register
if (Rd == 15 || Rm == 15 || Rn == 15 || Rs == 15) {
warning("UNPREDICTABLE (Attempt to specify PC as register in shift by register)\n");
return UNDEFINED_INSTRUCTION;
}
if (Rs_valueLB == 0) {
shifter_operand = Rm_value;
shifter_carry_out = C_bit;
} else if (Rs_valueLB < 32) {
shifter_operand = Rm_value >> Rs_valueLB;
shifter_carry_out = get_bit(Rm_value, Rs_valueLB - 1);
} else if (Rs_valueLB == 32) {
shifter_operand = 0;
shifter_carry_out = get_bit(Rm_value, 31);
} else { /* Rs[7:0] > 32 */
shifter_operand = 0;
shifter_carry_out = 0;
}
} else if (get_bits(shifter_operand_code, 6, 4) == 0b100) {
// asr by immediate
if (Rm == 15 || Rn == 15) // weird use of r15
Rm_value = arm_read_register(p, 15) + 4;
if (shift_imm == 0) {
if (get_bit(Rm_value, 31) == 0) {
shifter_operand = 0;
shifter_carry_out = 0;
} else { /* Rm[31] == 1 */
shifter_operand = 0xFFFFFFFF;
shifter_carry_out = 1;
}
} else { /* shift_imm > 0 */
shifter_operand = asr(Rm_value, shift_imm);
shifter_carry_out = get_bit(Rm_value, shift_imm - 1);
}
} else if (get_bits(shifter_operand_code, 7, 4) == 0b0101) {
// asr by register
if (Rd == 15 || Rm == 15 || Rn == 15 || Rs == 15) {
warning("UNPREDICTABLE (Attempt to specify PC as register in shift by register)\n");
return UNDEFINED_INSTRUCTION;
}
if (Rs_valueLB == 0) {
shifter_operand = Rm_value;
shifter_carry_out = C_bit;
} else if (Rs_valueLB < 32) {
shifter_operand = asr(Rm_value, Rs_valueLB);
shifter_carry_out = get_bit(Rm_value, Rs_valueLB - 1);
} else { /* Rs_valueLB >= 32 */
if (get_bit(Rm_value, 31) == 0) {
shifter_operand = 0;
shifter_carry_out = 0;
} else { /* Rm[31] == 1 */
shifter_operand = 0xFFFFFFFF;
shifter_carry_out = 1;
}
}
} else if (get_bits(shifter_operand_code, 6, 4) == 0b110) {
// rotate right by immediate
if (Rm == 15 || Rn == 15) // weird use of r15
Rm_value = arm_read_register(p, 15) + 4;
if (shift_imm == 0) { /* rotate right with extend */
shifter_operand = ((uint32_t)C_bit << 31) | (Rm_value >> 1);
shifter_carry_out = get_bit(Rm_value, 0);
} else { /* shift_imm > 0 */
shifter_operand = ror(Rm_value, shift_imm);
shifter_carry_out = get_bit(Rm_value, shift_imm - 1);
}
} else if (get_bits(shifter_operand_code, 7, 4) == 0b0111) {
// rotate right by register
if (Rd == 15 || Rm == 15 || Rn == 15 || Rs == 15) {
warning("UNPREDICTABLE (Attempt to specify PC as register in shift by register)\n");
return UNDEFINED_INSTRUCTION;
}
if (Rs_valueLB == 0) {
shifter_operand = Rm_value;
shifter_carry_out = C_bit;
} else if (get_bits(Rs_value, 4, 0) == 0) {
shifter_operand = Rm_value;
shifter_carry_out = get_bit(Rm_value, 31);
} else { /* Rs[4:0] > 0 */
shifter_operand = ror(Rm_value, get_bits(Rs_value, 4, 0));
shifter_carry_out = get_bit(Rm_value, get_bits(Rs_value, 4, 0) - 1);
}
} else
return UNDEFINED_INSTRUCTION;
return data_processing(p, S_bit, opcode, Rd, Rn_value, shifter_operand, shifter_carry_out);
}
int arm_data_processing_immediate(arm_core p, uint32_t ins) {
/* Read current flag */
uint8_t C_bit = get_bit(arm_read_cpsr(p), C);
/* Decode instruction */
uint8_t S_bit = get_bit(ins, S);
uint8_t opcode = get_bits(ins, 24, 21);
uint8_t Rn = get_bits(ins, 19, 16);
uint32_t Rn_value = arm_read_register(p, Rn);
uint8_t Rd = get_bits(ins, 15, 12);
uint8_t rotate_imm = get_bits(ins, 11, 8);
uint32_t immed_byte = get_bits(ins, 7, 0);
uint32_t shifter_operand;
uint8_t shifter_carry_out;
/* CACULATE SHIFTER OPERAND */
shifter_operand = ror(immed_byte, rotate_imm * 2);
if (rotate_imm == 0)
shifter_carry_out = C_bit;
else /* rotate_imm != 0 */
shifter_carry_out = get_bit(shifter_operand, 31);
return data_processing(p, S_bit, opcode, Rd, Rn_value, shifter_operand, shifter_carry_out);
}
int arm_msr_immediate(arm_core p, uint32_t ins) {
uint8_t R_bit = get_bit(ins, R);
uint8_t field_mask = get_bits(ins, 19, 16);
uint8_t rotate_imm = get_bits(ins, 11, 8);
uint32_t immed_byte = get_bits(ins, 7, 0);
/* Operand is an rotation of an immediate byte */
uint32_t operand = ror(immed_byte, rotate_imm * 2);
if (operand & UnallocMask) {
warning("UNPREDICTABLE (Attempt to set reserved bits)\n");
return UNDEFINED_INSTRUCTION;
}
/* Choose what byte to modfiy according to field_mask */
uint32_t byte_mask = 0;
if (get_bit(field_mask, 0))
byte_mask |= 0x000000FF;
if (get_bit(field_mask, 1))
byte_mask |= 0x0000FF00;
if (get_bit(field_mask, 2))
byte_mask |= 0x00FF0000;
if (get_bit(field_mask, 3))
byte_mask |= 0xFF000000;
uint32_t mask;
if (R_bit == 0) {
// Change CPSR according to operand, byte_mask and mode masks..
if (arm_in_a_privileged_mode(p)) {
if (operand & StateMask) {
warning("UNPREDICTABLE (Attempt to set non-ARM execution state)\n");
return UNDEFINED_INSTRUCTION;
} else
mask = byte_mask & (UserMask | PrivMask);
} else
mask = byte_mask & UserMask;
arm_write_cpsr(p, (arm_read_cpsr(p) & ~mask) | (operand & mask));
return SUCCESSFULLY_DECODED;
} else { // R == 1
// Change SPSR according to operand, byte_mask and mode masks..
if (arm_current_mode_has_spsr(p)) {
mask = byte_mask & (UserMask | PrivMask | StateMask);
arm_write_spsr(p, (arm_read_spsr(p) & ~mask) | (operand & mask));
return SUCCESSFULLY_DECODED;
} else {
warning("UNPREDICTABLE (Attempt to use SPSR but current mode does not have SPSR)\n");
return UNDEFINED_INSTRUCTION;
}
}
}
int arm_data_processing_immediate_msr(arm_core p, uint32_t ins) {
if (get_bits(ins, 24, 23) == 0b10 && get_bits(ins, 21, 20) == 0b10)
return arm_msr_immediate(p, ins);
else
return arm_data_processing_immediate(p, ins);
}