OpenOCD
arm_simulator.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2008 by Hongtao Zheng *
8  * hontor@126.com *
9  ***************************************************************************/
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 #include "arm.h"
16 #include "armv4_5.h"
17 #include "arm_disassembler.h"
18 #include "arm_simulator.h"
19 #include <helper/binarybuffer.h>
20 #include "register.h"
21 #include <helper/log.h>
22 
23 static uint32_t arm_shift(uint8_t shift, uint32_t rm,
24  uint32_t shift_amount, uint8_t *carry)
25 {
26  uint32_t return_value = 0;
27  shift_amount &= 0xff;
28 
29  if (shift == 0x0) { /* LSL */
30  if ((shift_amount > 0) && (shift_amount <= 32)) {
31  return_value = rm << shift_amount;
32  *carry = rm >> (32 - shift_amount);
33  } else if (shift_amount > 32) {
34  return_value = 0x0;
35  *carry = 0x0;
36  } else /* (shift_amount == 0) */
37  return_value = rm;
38  } else if (shift == 0x1) { /* LSR */
39  if ((shift_amount > 0) && (shift_amount <= 32)) {
40  return_value = rm >> shift_amount;
41  *carry = (rm >> (shift_amount - 1)) & 1;
42  } else if (shift_amount > 32) {
43  return_value = 0x0;
44  *carry = 0x0;
45  } else /* (shift_amount == 0) */
46  return_value = rm;
47  } else if (shift == 0x2) { /* ASR */
48  if ((shift_amount > 0) && (shift_amount <= 32)) {
49  /* C right shifts of unsigned values are guaranteed to
50  * be logical (shift in zeroes); simulate an arithmetic
51  * shift (shift in signed-bit) by adding the sign bit
52  * manually
53  */
54  return_value = rm >> shift_amount;
55  if (rm & 0x80000000)
56  return_value |= 0xffffffff << (32 - shift_amount);
57  } else if (shift_amount > 32) {
58  if (rm & 0x80000000) {
59  return_value = 0xffffffff;
60  *carry = 0x1;
61  } else {
62  return_value = 0x0;
63  *carry = 0x0;
64  }
65  } else /* (shift_amount == 0) */
66  return_value = rm;
67  } else if (shift == 0x3) { /* ROR */
68  if (shift_amount == 0)
69  return_value = rm;
70  else {
71  shift_amount = shift_amount % 32;
72  return_value = (rm >> shift_amount) | (rm << (32 - shift_amount));
73  *carry = (return_value >> 31) & 0x1;
74  }
75  } else if (shift == 0x4) { /* RRX */
76  return_value = rm >> 1;
77  if (*carry)
78  rm |= 0x80000000;
79  *carry = rm & 0x1;
80  }
81 
82  return return_value;
83 }
84 
85 
86 static uint32_t arm_shifter_operand(struct arm_sim_interface *sim,
87  int variant, union arm_shifter_operand shifter_operand,
88  uint8_t *shifter_carry_out)
89 {
90  uint32_t return_value;
91  int instruction_size;
92 
93  if (sim->get_state(sim) == ARM_STATE_ARM)
94  instruction_size = 4;
95  else
96  instruction_size = 2;
97 
98  *shifter_carry_out = sim->get_cpsr(sim, 29, 1);
99 
100  if (variant == 0) /* 32-bit immediate */
101  return_value = shifter_operand.immediate.immediate;
102  else if (variant == 1) {/* immediate shift */
103  uint32_t rm = sim->get_reg_mode(sim, shifter_operand.immediate_shift.rm);
104 
105  /* adjust RM in case the PC is being read */
106  if (shifter_operand.immediate_shift.rm == 15)
107  rm += 2 * instruction_size;
108 
109  return_value = arm_shift(shifter_operand.immediate_shift.shift,
110  rm, shifter_operand.immediate_shift.shift_imm,
111  shifter_carry_out);
112  } else if (variant == 2) { /* register shift */
113  uint32_t rm = sim->get_reg_mode(sim, shifter_operand.register_shift.rm);
114  uint32_t rs = sim->get_reg_mode(sim, shifter_operand.register_shift.rs);
115 
116  /* adjust RM in case the PC is being read */
117  if (shifter_operand.register_shift.rm == 15)
118  rm += 2 * instruction_size;
119 
120  return_value = arm_shift(shifter_operand.immediate_shift.shift,
121  rm, rs, shifter_carry_out);
122  } else {
123  LOG_ERROR("BUG: shifter_operand.variant not 0, 1 or 2");
124  return_value = 0xffffffff;
125  }
126 
127  return return_value;
128 }
129 
130 static int pass_condition(uint32_t cpsr, uint32_t opcode)
131 {
132  switch ((opcode & 0xf0000000) >> 28) {
133  case 0x0: /* EQ */
134  if (cpsr & 0x40000000)
135  return 1;
136  else
137  return 0;
138  case 0x1: /* NE */
139  if (!(cpsr & 0x40000000))
140  return 1;
141  else
142  return 0;
143  case 0x2: /* CS */
144  if (cpsr & 0x20000000)
145  return 1;
146  else
147  return 0;
148  case 0x3: /* CC */
149  if (!(cpsr & 0x20000000))
150  return 1;
151  else
152  return 0;
153  case 0x4: /* MI */
154  if (cpsr & 0x80000000)
155  return 1;
156  else
157  return 0;
158  case 0x5: /* PL */
159  if (!(cpsr & 0x80000000))
160  return 1;
161  else
162  return 0;
163  case 0x6: /* VS */
164  if (cpsr & 0x10000000)
165  return 1;
166  else
167  return 0;
168  case 0x7: /* VC */
169  if (!(cpsr & 0x10000000))
170  return 1;
171  else
172  return 0;
173  case 0x8: /* HI */
174  if ((cpsr & 0x20000000) && !(cpsr & 0x40000000))
175  return 1;
176  else
177  return 0;
178  case 0x9: /* LS */
179  if (!(cpsr & 0x20000000) || (cpsr & 0x40000000))
180  return 1;
181  else
182  return 0;
183  case 0xa: /* GE */
184  if (((cpsr & 0x80000000) && (cpsr & 0x10000000))
185  || (!(cpsr & 0x80000000) && !(cpsr & 0x10000000)))
186  return 1;
187  else
188  return 0;
189  case 0xb: /* LT */
190  if (((cpsr & 0x80000000) && !(cpsr & 0x10000000))
191  || (!(cpsr & 0x80000000) && (cpsr & 0x10000000)))
192  return 1;
193  else
194  return 0;
195  case 0xc: /* GT */
196  if (!(cpsr & 0x40000000) &&
197  (((cpsr & 0x80000000) && (cpsr & 0x10000000))
198  || (!(cpsr & 0x80000000) && !(cpsr & 0x10000000))))
199  return 1;
200  else
201  return 0;
202  case 0xd: /* LE */
203  if ((cpsr & 0x40000000) ||
204  ((cpsr & 0x80000000) && !(cpsr & 0x10000000))
205  || (!(cpsr & 0x80000000) && (cpsr & 0x10000000)))
206  return 1;
207  else
208  return 0;
209  case 0xe:
210  case 0xf:
211  return 1;
212  }
213 
214  LOG_ERROR("BUG: should never get here");
215  return 0;
216 }
217 
218 static int thumb_pass_branch_condition(uint32_t cpsr, uint16_t opcode)
219 {
220  return pass_condition(cpsr, (opcode & 0x0f00) << 20);
221 }
222 
223 /* simulate a single step (if possible)
224  * if the dry_run_pc argument is provided, no state is changed,
225  * but the new pc is stored in the variable pointed at by the argument
226  */
228  uint32_t *dry_run_pc, struct arm_sim_interface *sim)
229 {
230  uint32_t current_pc = sim->get_reg(sim, 15);
231  struct arm_instruction instruction;
232  int instruction_size;
233  int retval = ERROR_OK;
234 
235  if (sim->get_state(sim) == ARM_STATE_ARM) {
236  uint32_t opcode;
237 
238  /* get current instruction, and identify it */
239  retval = target_read_u32(target, current_pc, &opcode);
240  if (retval != ERROR_OK)
241  return retval;
242  retval = arm_evaluate_opcode(opcode, current_pc, &instruction);
243  if (retval != ERROR_OK)
244  return retval;
245  instruction_size = 4;
246 
247  /* check condition code (for all instructions) */
248  if (!pass_condition(sim->get_cpsr(sim, 0, 32), opcode)) {
249  if (dry_run_pc)
250  *dry_run_pc = current_pc + instruction_size;
251  else
252  sim->set_reg(sim, 15, current_pc + instruction_size);
253 
254  return ERROR_OK;
255  }
256  } else {
257  uint16_t opcode;
258 
259  retval = target_read_u16(target, current_pc, &opcode);
260  if (retval != ERROR_OK)
261  return retval;
262  retval = thumb_evaluate_opcode(opcode, current_pc, &instruction);
263  if (retval != ERROR_OK)
264  return retval;
265  instruction_size = 2;
266 
267  /* check condition code (only for branch (1) instructions) */
268  if ((opcode & 0xf000) == 0xd000
270  sim->get_cpsr(sim, 0, 32), opcode)) {
271  if (dry_run_pc)
272  *dry_run_pc = current_pc + instruction_size;
273  else
274  sim->set_reg(sim, 15, current_pc + instruction_size);
275 
276  return ERROR_OK;
277  }
278 
279  /* Deal with 32-bit BL/BLX */
280  if ((opcode & 0xf800) == 0xf000) {
281  uint32_t high = instruction.info.b_bl_bx_blx.target_address;
282  retval = target_read_u16(target, current_pc+2, &opcode);
283  if (retval != ERROR_OK)
284  return retval;
285  retval = thumb_evaluate_opcode(opcode, current_pc, &instruction);
286  if (retval != ERROR_OK)
287  return retval;
288  instruction.info.b_bl_bx_blx.target_address += high;
289  }
290  }
291 
292  /* examine instruction type */
293 
294  /* branch instructions */
295  if ((instruction.type >= ARM_B) && (instruction.type <= ARM_BLX)) {
296  uint32_t target_address;
297 
298  if (instruction.info.b_bl_bx_blx.reg_operand == -1)
299  target_address = instruction.info.b_bl_bx_blx.target_address;
300  else {
301  target_address = sim->get_reg_mode(sim,
302  instruction.info.b_bl_bx_blx.reg_operand);
303  if (instruction.info.b_bl_bx_blx.reg_operand == 15)
304  target_address += 2 * instruction_size;
305  }
306 
307  if (dry_run_pc) {
308  *dry_run_pc = target_address & ~1;
309  return ERROR_OK;
310  } else {
311  if (instruction.type == ARM_B)
312  sim->set_reg(sim, 15, target_address);
313  else if (instruction.type == ARM_BL) {
314  uint32_t old_pc = sim->get_reg(sim, 15);
315  int t = (sim->get_state(sim) == ARM_STATE_THUMB);
316  sim->set_reg_mode(sim, 14, old_pc + 4 + t);
317  sim->set_reg(sim, 15, target_address);
318  } else if (instruction.type == ARM_BX) {
319  if (target_address & 0x1)
320  sim->set_state(sim, ARM_STATE_THUMB);
321  else
322  sim->set_state(sim, ARM_STATE_ARM);
323  sim->set_reg(sim, 15, target_address & 0xfffffffe);
324  } else if (instruction.type == ARM_BLX) {
325  uint32_t old_pc = sim->get_reg(sim, 15);
326  int t = (sim->get_state(sim) == ARM_STATE_THUMB);
327  sim->set_reg_mode(sim, 14, old_pc + 4 + t);
328 
329  if (target_address & 0x1)
330  sim->set_state(sim, ARM_STATE_THUMB);
331  else
332  sim->set_state(sim, ARM_STATE_ARM);
333  sim->set_reg(sim, 15, target_address & 0xfffffffe);
334  }
335 
336  return ERROR_OK;
337  }
338  }
339  /* data processing instructions, except compare instructions (CMP, CMN, TST, TEQ) */
340  else if (((instruction.type >= ARM_AND) && (instruction.type <= ARM_RSC))
341  || ((instruction.type >= ARM_ORR) && (instruction.type <= ARM_MVN))) {
342  uint32_t rd, rn, shifter_operand;
343  uint8_t c = sim->get_cpsr(sim, 29, 1);
344  uint8_t carry_out;
345 
346  rd = 0x0;
347  /* ARM_MOV and ARM_MVN does not use Rn */
348  if ((instruction.type != ARM_MOV) && (instruction.type != ARM_MVN))
349  rn = sim->get_reg_mode(sim, instruction.info.data_proc.rn);
350  else
351  rn = 0;
352 
353  shifter_operand = arm_shifter_operand(sim,
354  instruction.info.data_proc.variant,
355  instruction.info.data_proc.shifter_operand,
356  &carry_out);
357 
358  /* adjust Rn in case the PC is being read */
359  if (instruction.info.data_proc.rn == 15)
360  rn += 2 * instruction_size;
361 
362  if (instruction.type == ARM_AND)
363  rd = rn & shifter_operand;
364  else if (instruction.type == ARM_EOR)
365  rd = rn ^ shifter_operand;
366  else if (instruction.type == ARM_SUB)
367  rd = rn - shifter_operand;
368  else if (instruction.type == ARM_RSB)
369  rd = shifter_operand - rn;
370  else if (instruction.type == ARM_ADD)
371  rd = rn + shifter_operand;
372  else if (instruction.type == ARM_ADC)
373  rd = rn + shifter_operand + (c & 1);
374  else if (instruction.type == ARM_SBC)
375  rd = rn - shifter_operand - (c & 1) ? 0 : 1;
376  else if (instruction.type == ARM_RSC)
377  rd = shifter_operand - rn - (c & 1) ? 0 : 1;
378  else if (instruction.type == ARM_ORR)
379  rd = rn | shifter_operand;
380  else if (instruction.type == ARM_BIC)
381  rd = rn & ~(shifter_operand);
382  else if (instruction.type == ARM_MOV)
383  rd = shifter_operand;
384  else if (instruction.type == ARM_MVN)
385  rd = ~shifter_operand;
386  else
387  LOG_WARNING("unhandled instruction type");
388 
389  if (dry_run_pc) {
390  if (instruction.info.data_proc.rd == 15)
391  *dry_run_pc = rd & ~1;
392  else
393  *dry_run_pc = current_pc + instruction_size;
394 
395  return ERROR_OK;
396  } else {
397  if (instruction.info.data_proc.rd == 15) {
398  sim->set_reg_mode(sim, 15, rd & ~1);
399  if (rd & 1)
400  sim->set_state(sim, ARM_STATE_THUMB);
401  else
402  sim->set_state(sim, ARM_STATE_ARM);
403  return ERROR_OK;
404  }
405  sim->set_reg_mode(sim, instruction.info.data_proc.rd, rd);
406  LOG_WARNING("no updating of flags yet");
407  }
408  }
409  /* compare instructions (CMP, CMN, TST, TEQ) */
410  else if ((instruction.type >= ARM_TST) && (instruction.type <= ARM_CMN)) {
411  if (dry_run_pc) {
412  *dry_run_pc = current_pc + instruction_size;
413  return ERROR_OK;
414  } else
415  LOG_WARNING("no updating of flags yet");
416  }
417  /* load register instructions */
418  else if ((instruction.type >= ARM_LDR) && (instruction.type <= ARM_LDRSH)) {
419  uint32_t load_address = 0, modified_address = 0, load_value = 0;
420  uint32_t rn = sim->get_reg_mode(sim, instruction.info.load_store.rn);
421 
422  /* adjust Rn in case the PC is being read */
423  if (instruction.info.load_store.rn == 15)
424  rn += 2 * instruction_size;
425 
426  if (instruction.info.load_store.offset_mode == 0) {
427  if (instruction.info.load_store.u)
428  modified_address = rn + instruction.info.load_store.offset.offset;
429  else
430  modified_address = rn - instruction.info.load_store.offset.offset;
431  } else if (instruction.info.load_store.offset_mode == 1) {
432  uint32_t offset;
433  uint32_t rm = sim->get_reg_mode(sim,
434  instruction.info.load_store.offset.reg.rm);
435  uint8_t shift = instruction.info.load_store.offset.reg.shift;
436  uint8_t shift_imm = instruction.info.load_store.offset.reg.shift_imm;
437  uint8_t carry = sim->get_cpsr(sim, 29, 1);
438 
439  offset = arm_shift(shift, rm, shift_imm, &carry);
440 
441  if (instruction.info.load_store.u)
442  modified_address = rn + offset;
443  else
444  modified_address = rn - offset;
445  } else
446  LOG_ERROR("BUG: offset_mode neither 0 (offset) nor 1 (scaled register)");
447 
448  if (instruction.info.load_store.index_mode == 0) {
449  /* offset mode
450  * we load from the modified address, but don't change
451  * the base address register
452  */
453  load_address = modified_address;
454  modified_address = rn;
455  } else if (instruction.info.load_store.index_mode == 1) {
456  /* pre-indexed mode
457  * we load from the modified address, and write it
458  * back to the base address register
459  */
460  load_address = modified_address;
461  } else if (instruction.info.load_store.index_mode == 2) {
462  /* post-indexed mode
463  * we load from the unmodified address, and write the
464  * modified address back
465  */
466  load_address = rn;
467  }
468 
469  if ((!dry_run_pc) || (instruction.info.load_store.rd == 15)) {
470  retval = target_read_u32(target, load_address, &load_value);
471  if (retval != ERROR_OK)
472  return retval;
473  }
474 
475  if (dry_run_pc) {
476  if (instruction.info.load_store.rd == 15)
477  *dry_run_pc = load_value & ~1;
478  else
479  *dry_run_pc = current_pc + instruction_size;
480  return ERROR_OK;
481  } else {
482  if ((instruction.info.load_store.index_mode == 1) ||
483  (instruction.info.load_store.index_mode == 2))
484  sim->set_reg_mode(sim,
485  instruction.info.load_store.rn,
486  modified_address);
487 
488  if (instruction.info.load_store.rd == 15) {
489  sim->set_reg_mode(sim, 15, load_value & ~1);
490  if (load_value & 1)
491  sim->set_state(sim, ARM_STATE_THUMB);
492  else
493  sim->set_state(sim, ARM_STATE_ARM);
494  return ERROR_OK;
495  }
496  sim->set_reg_mode(sim, instruction.info.load_store.rd, load_value);
497  }
498  }
499  /* load multiple instruction */
500  else if (instruction.type == ARM_LDM) {
501  int i;
502  uint32_t rn = sim->get_reg_mode(sim, instruction.info.load_store_multiple.rn);
503  uint32_t load_values[16];
504  int bits_set = 0;
505 
506  for (i = 0; i < 16; i++) {
507  if (instruction.info.load_store_multiple.register_list & (1 << i))
508  bits_set++;
509  }
510 
511  switch (instruction.info.load_store_multiple.addressing_mode) {
512  case 0: /* Increment after */
513  /* rn = rn; */
514  break;
515  case 1: /* Increment before */
516  rn = rn + 4;
517  break;
518  case 2: /* Decrement after */
519  rn = rn - (bits_set * 4) + 4;
520  break;
521  case 3: /* Decrement before */
522  rn = rn - (bits_set * 4);
523  break;
524  }
525 
526  for (i = 0; i < 16; i++) {
527  if (instruction.info.load_store_multiple.register_list & (1 << i)) {
528  if ((!dry_run_pc) || (i == 15))
529  target_read_u32(target, rn, &load_values[i]);
530  rn += 4;
531  }
532  }
533 
534  if (dry_run_pc) {
535  if (instruction.info.load_store_multiple.register_list & 0x8000) {
536  *dry_run_pc = load_values[15] & ~1;
537  return ERROR_OK;
538  }
539  } else {
540  int update_cpsr = 0;
541 
542  if (instruction.info.load_store_multiple.s) {
543  if (instruction.info.load_store_multiple.register_list & 0x8000)
544  update_cpsr = 1;
545  }
546 
547  for (i = 0; i < 16; i++) {
548  if (instruction.info.load_store_multiple.register_list & (1 << i)) {
549  if (i == 15) {
550  uint32_t val = load_values[i];
551  sim->set_reg_mode(sim, i, val & ~1);
552  if (val & 1)
553  sim->set_state(sim, ARM_STATE_THUMB);
554  else
555  sim->set_state(sim, ARM_STATE_ARM);
556  } else
557  sim->set_reg_mode(sim, i, load_values[i]);
558  }
559  }
560 
561  if (update_cpsr) {
562  uint32_t spsr = sim->get_reg_mode(sim, 16);
563  sim->set_reg(sim, ARMV4_5_CPSR, spsr);
564  }
565 
566  /* base register writeback */
567  if (instruction.info.load_store_multiple.w)
568  sim->set_reg_mode(sim, instruction.info.load_store_multiple.rn, rn);
569 
570 
571  if (instruction.info.load_store_multiple.register_list & 0x8000)
572  return ERROR_OK;
573  }
574  }
575  /* store multiple instruction */
576  else if (instruction.type == ARM_STM) {
577  int i;
578 
579  if (dry_run_pc) {
580  /* STM wont affect PC (advance by instruction size */
581  } else {
582  uint32_t rn = sim->get_reg_mode(sim,
583  instruction.info.load_store_multiple.rn);
584  int bits_set = 0;
585 
586  for (i = 0; i < 16; i++) {
587  if (instruction.info.load_store_multiple.register_list & (1 << i))
588  bits_set++;
589  }
590 
591  switch (instruction.info.load_store_multiple.addressing_mode) {
592  case 0: /* Increment after */
593  /* rn = rn; */
594  break;
595  case 1: /* Increment before */
596  rn = rn + 4;
597  break;
598  case 2: /* Decrement after */
599  rn = rn - (bits_set * 4) + 4;
600  break;
601  case 3: /* Decrement before */
602  rn = rn - (bits_set * 4);
603  break;
604  }
605 
606  for (i = 0; i < 16; i++) {
607  if (instruction.info.load_store_multiple.register_list & (1 << i)) {
608  target_write_u32(target, rn, sim->get_reg_mode(sim, i));
609  rn += 4;
610  }
611  }
612 
613  /* base register writeback */
614  if (instruction.info.load_store_multiple.w)
615  sim->set_reg_mode(sim,
616  instruction.info.load_store_multiple.rn, rn);
617 
618  }
619  } else if (!dry_run_pc) {
620  /* the instruction wasn't handled, but we're supposed to simulate it
621  */
622  LOG_ERROR("Unimplemented instruction, could not simulate it.");
623  return ERROR_FAIL;
624  }
625 
626  if (dry_run_pc) {
627  *dry_run_pc = current_pc + instruction_size;
628  return ERROR_OK;
629  } else {
630  sim->set_reg(sim, 15, current_pc + instruction_size);
631  return ERROR_OK;
632  }
633 
634 }
635 
636 static uint32_t armv4_5_get_reg(struct arm_sim_interface *sim, int reg)
637 {
638  struct arm *arm = (struct arm *)sim->user_data;
639 
640  return buf_get_u32(arm->core_cache->reg_list[reg].value, 0, 32);
641 }
642 
643 static void armv4_5_set_reg(struct arm_sim_interface *sim, int reg, uint32_t value)
644 {
645  struct arm *arm = (struct arm *)sim->user_data;
646 
647  buf_set_u32(arm->core_cache->reg_list[reg].value, 0, 32, value);
648 }
649 
650 static uint32_t armv4_5_get_reg_mode(struct arm_sim_interface *sim, int reg)
651 {
652  struct arm *arm = (struct arm *)sim->user_data;
653 
655  arm->core_mode, reg).value, 0, 32);
656 }
657 
658 static void armv4_5_set_reg_mode(struct arm_sim_interface *sim, int reg, uint32_t value)
659 {
660  struct arm *arm = (struct arm *)sim->user_data;
661 
663  arm->core_mode, reg).value, 0, 32, value);
664 }
665 
666 static uint32_t armv4_5_get_cpsr(struct arm_sim_interface *sim, int pos, int bits)
667 {
668  struct arm *arm = (struct arm *)sim->user_data;
669 
670  return buf_get_u32(arm->cpsr->value, pos, bits);
671 }
672 
673 static enum arm_state armv4_5_get_state(struct arm_sim_interface *sim)
674 {
675  struct arm *arm = (struct arm *)sim->user_data;
676 
677  return arm->core_state;
678 }
679 
680 static void armv4_5_set_state(struct arm_sim_interface *sim, enum arm_state mode)
681 {
682  struct arm *arm = (struct arm *)sim->user_data;
683 
684  arm->core_state = mode;
685 }
686 
687 static enum arm_mode armv4_5_get_mode(struct arm_sim_interface *sim)
688 {
689  struct arm *arm = (struct arm *)sim->user_data;
690 
691  return arm->core_mode;
692 }
693 
694 int arm_simulate_step(struct target *target, uint32_t *dry_run_pc)
695 {
696  struct arm *arm = target_to_arm(target);
697  struct arm_sim_interface sim;
698 
699  sim.user_data = arm;
700  sim.get_reg = &armv4_5_get_reg;
701  sim.set_reg = &armv4_5_set_reg;
704  sim.get_cpsr = &armv4_5_get_cpsr;
705  sim.get_mode = &armv4_5_get_mode;
708 
709  return arm_simulate_step_core(target, dry_run_pc, &sim);
710 }
Holds the interface to ARM cores.
arm_mode
Represent state of an ARM core.
Definition: arm.h:82
static struct arm * target_to_arm(const struct target *target)
Convert target handle to generic ARM target state handle.
Definition: arm.h:261
arm_state
The PSR "T" and "J" bits define the mode of "classic ARM" cores.
Definition: arm.h:150
@ ARM_STATE_THUMB
Definition: arm.h:152
@ ARM_STATE_ARM
Definition: arm.h:151
int arm_evaluate_opcode(uint32_t opcode, uint32_t address, struct arm_instruction *instruction)
int thumb_evaluate_opcode(uint16_t opcode, uint32_t address, struct arm_instruction *instruction)
@ ARM_RSB
@ ARM_BIC
@ ARM_ADD
@ ARM_STM
@ ARM_SBC
@ ARM_RSC
@ ARM_BX
@ ARM_BL
@ ARM_B
@ ARM_MOV
@ ARM_TST
@ ARM_LDR
@ ARM_AND
@ ARM_BLX
@ ARM_ADC
@ ARM_LDM
@ ARM_EOR
@ ARM_LDRSH
@ ARM_SUB
@ ARM_MVN
@ ARM_ORR
@ ARM_CMN
static void armv4_5_set_reg(struct arm_sim_interface *sim, int reg, uint32_t value)
static int thumb_pass_branch_condition(uint32_t cpsr, uint16_t opcode)
static void armv4_5_set_reg_mode(struct arm_sim_interface *sim, int reg, uint32_t value)
int arm_simulate_step(struct target *target, uint32_t *dry_run_pc)
static uint32_t arm_shift(uint8_t shift, uint32_t rm, uint32_t shift_amount, uint8_t *carry)
Definition: arm_simulator.c:23
static uint32_t armv4_5_get_reg_mode(struct arm_sim_interface *sim, int reg)
static uint32_t arm_shifter_operand(struct arm_sim_interface *sim, int variant, union arm_shifter_operand shifter_operand, uint8_t *shifter_carry_out)
Definition: arm_simulator.c:86
static uint32_t armv4_5_get_cpsr(struct arm_sim_interface *sim, int pos, int bits)
static void armv4_5_set_state(struct arm_sim_interface *sim, enum arm_state mode)
static int pass_condition(uint32_t cpsr, uint32_t opcode)
static int arm_simulate_step_core(struct target *target, uint32_t *dry_run_pc, struct arm_sim_interface *sim)
static enum arm_mode armv4_5_get_mode(struct arm_sim_interface *sim)
static uint32_t armv4_5_get_reg(struct arm_sim_interface *sim, int reg)
static enum arm_state armv4_5_get_state(struct arm_sim_interface *sim)
enum arm_mode mode
Definition: armv4_5.c:281
#define ARMV4_5_CORE_REG_MODE(cache, mode, num)
Definition: armv4_5.h:32
@ ARMV4_5_CPSR
Definition: armv4_5.h:36
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
const char * rs
Definition: ecos.c:480
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define ERROR_OK
Definition: log.h:168
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
union arm_shifter_operand shifter_operand
struct arm_load_store_multiple_instr load_store_multiple
unsigned int instruction_size
enum arm_instruction_type type
struct arm_b_bl_bx_blx_instr b_bl_bx_blx
union arm_instruction::@72 info
struct arm_load_store_instr load_store
struct arm_data_proc_instr data_proc
uint32_t(* get_reg_mode)(struct arm_sim_interface *sim, int reg)
Definition: arm_simulator.h:17
enum arm_mode(* get_mode)(struct arm_sim_interface *sim)
Definition: arm_simulator.h:22
void(* set_reg)(struct arm_sim_interface *sim, int reg, uint32_t value)
Definition: arm_simulator.h:16
uint32_t(* get_cpsr)(struct arm_sim_interface *sim, int pos, int bits)
Definition: arm_simulator.h:19
uint32_t(* get_reg)(struct arm_sim_interface *sim, int reg)
Definition: arm_simulator.h:15
void(* set_state)(struct arm_sim_interface *sim, enum arm_state mode)
Definition: arm_simulator.h:21
void(* set_reg_mode)(struct arm_sim_interface *sim, int reg, uint32_t value)
Definition: arm_simulator.h:18
enum arm_state(* get_state)(struct arm_sim_interface *sim)
Definition: arm_simulator.h:20
Represents a generic ARM core, with standard application registers.
Definition: arm.h:175
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:196
struct reg * cpsr
Handle to the CPSR/xPSR; valid in all core modes.
Definition: arm.h:184
struct reg_cache * core_cache
Definition: arm.h:178
enum arm_state core_state
Record the current core state: ARM, Thumb, or otherwise.
Definition: arm.h:199
struct reg * reg_list
Definition: register.h:147
Definition: register.h:111
uint8_t * value
Definition: register.h:122
Definition: target.h:119
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2650
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2583
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2559
struct arm_shifter_operand::@68 immediate_shift
struct arm_shifter_operand::@69 register_shift
uint8_t offset[4]
Definition: vdebug.c:9