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