OpenOCD
xscale.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006, 2007 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2009 Michael Schwingen *
11  * michael@schwingen.org *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include "breakpoints.h"
19 #include "xscale.h"
20 #include "target_type.h"
21 #include "arm_jtag.h"
22 #include "arm_simulator.h"
23 #include "arm_disassembler.h"
24 #include <helper/time_support.h>
25 #include <helper/string_choices.h>
26 #include "register.h"
27 #include "image.h"
28 #include "arm_opcodes.h"
29 #include "armv4_5.h"
30 
31 /*
32  * Important XScale documents available as of October 2009 include:
33  *
34  * Intel XScale® Core Developer’s Manual, January 2004
35  * Order Number: 273473-002
36  * This has a chapter detailing debug facilities, and punts some
37  * details to chip-specific microarchitecture documents.
38  *
39  * Hot-Debug for Intel XScale® Core Debug White Paper, May 2005
40  * Document Number: 273539-005
41  * Less detailed than the developer's manual, but summarizes those
42  * missing details (for most XScales) and gives LOTS of notes about
43  * debugger/handler interaction issues. Presents a simpler reset
44  * and load-handler sequence than the arch doc. (Note, OpenOCD
45  * doesn't currently support "Hot-Debug" as defined there.)
46  *
47  * Chip-specific microarchitecture documents may also be useful.
48  */
49 
50 /* forward declarations */
51 static int xscale_resume(struct target *, bool current,
52  target_addr_t address, bool handle_breakpoints, bool debug_execution);
53 static int xscale_debug_entry(struct target *);
54 static int xscale_restore_banked(struct target *);
55 static int xscale_get_reg(struct reg *reg);
56 static int xscale_set_reg(struct reg *reg, uint8_t *buf);
57 static int xscale_set_breakpoint(struct target *, struct breakpoint *);
58 static int xscale_set_watchpoint(struct target *, struct watchpoint *);
59 static int xscale_unset_breakpoint(struct target *, struct breakpoint *);
60 static int xscale_read_trace(struct target *);
61 
62 /* This XScale "debug handler" is loaded into the processor's
63  * mini-ICache, which is 2K of code writable only via JTAG.
64  */
65 static const uint8_t xscale_debug_handler[] = {
66 #include "../../contrib/loaders/debug/xscale/debug_handler.inc"
67 };
68 
69 static const char *const xscale_reg_list[] = {
70  "XSCALE_MAINID", /* 0 */
71  "XSCALE_CACHETYPE",
72  "XSCALE_CTRL",
73  "XSCALE_AUXCTRL",
74  "XSCALE_TTB",
75  "XSCALE_DAC",
76  "XSCALE_FSR",
77  "XSCALE_FAR",
78  "XSCALE_PID",
79  "XSCALE_CPACCESS",
80  "XSCALE_IBCR0", /* 10 */
81  "XSCALE_IBCR1",
82  "XSCALE_DBR0",
83  "XSCALE_DBR1",
84  "XSCALE_DBCON",
85  "XSCALE_TBREG",
86  "XSCALE_CHKPT0",
87  "XSCALE_CHKPT1",
88  "XSCALE_DCSR",
89  "XSCALE_TX",
90  "XSCALE_RX", /* 20 */
91  "XSCALE_TXRXCTRL",
92 };
93 
94 static const struct xscale_reg xscale_reg_arch_info[] = {
97  {XSCALE_CTRL, NULL},
99  {XSCALE_TTB, NULL},
100  {XSCALE_DAC, NULL},
101  {XSCALE_FSR, NULL},
102  {XSCALE_FAR, NULL},
103  {XSCALE_PID, NULL},
105  {XSCALE_IBCR0, NULL},
106  {XSCALE_IBCR1, NULL},
107  {XSCALE_DBR0, NULL},
108  {XSCALE_DBR1, NULL},
109  {XSCALE_DBCON, NULL},
110  {XSCALE_TBREG, NULL},
111  {XSCALE_CHKPT0, NULL},
112  {XSCALE_CHKPT1, NULL},
113  {XSCALE_DCSR, NULL}, /* DCSR accessed via JTAG or SW */
114  {-1, NULL}, /* TX accessed via JTAG */
115  {-1, NULL}, /* RX accessed via JTAG */
116  {-1, NULL}, /* TXRXCTRL implicit access via JTAG */
117 };
118 
119 /* convenience wrapper to access XScale specific registers */
120 static int xscale_set_reg_u32(struct reg *reg, uint32_t value)
121 {
122  uint8_t buf[4] = { 0 };
123 
124  buf_set_u32(buf, 0, 32, value);
125 
126  return xscale_set_reg(reg, buf);
127 }
128 
129 static const char xscale_not[] = "target is not an XScale";
130 
132  struct xscale_common *xscale)
133 {
134  if (xscale->common_magic != XSCALE_COMMON_MAGIC) {
136  return ERROR_TARGET_INVALID;
137  }
138  return ERROR_OK;
139 }
140 
141 static int xscale_jtag_set_instr(struct jtag_tap *tap, uint32_t new_instr, enum tap_state end_state)
142 {
143  assert(tap);
144 
145  if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
146  struct scan_field field;
147  uint8_t scratch[4] = { 0 };
148 
149  memset(&field, 0, sizeof(field));
150  field.num_bits = tap->ir_length;
151  field.out_value = scratch;
152  buf_set_u32(scratch, 0, field.num_bits, new_instr);
153 
154  jtag_add_ir_scan(tap, &field, end_state);
155  }
156 
157  return ERROR_OK;
158 }
159 
160 static int xscale_read_dcsr(struct target *target)
161 {
162  struct xscale_common *xscale = target_to_xscale(target);
163  int retval;
164  struct scan_field fields[3];
165  uint8_t field0 = 0x0;
166  uint8_t field0_check_value = 0x2;
167  uint8_t field0_check_mask = 0x7;
168  uint8_t field2 = 0x0;
169  uint8_t field2_check_value = 0x0;
170  uint8_t field2_check_mask = 0x1;
171 
173  XSCALE_SELDCSR << xscale->xscale_variant,
174  TAP_DRPAUSE);
175 
176  buf_set_u32(&field0, 1, 1, xscale->hold_rst);
177  buf_set_u32(&field0, 2, 1, xscale->external_debug_break);
178 
179  memset(&fields, 0, sizeof(fields));
180 
181  fields[0].num_bits = 3;
182  fields[0].out_value = &field0;
183  uint8_t tmp;
184  fields[0].in_value = &tmp;
185 
186  fields[1].num_bits = 32;
187  fields[1].in_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
188 
189  fields[2].num_bits = 1;
190  fields[2].out_value = &field2;
191  uint8_t tmp2;
192  fields[2].in_value = &tmp2;
193 
194  jtag_add_dr_scan(target->tap, 3, fields, TAP_DRPAUSE);
195 
196  jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
197  jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
198 
199  retval = jtag_execute_queue();
200  if (retval != ERROR_OK) {
201  LOG_ERROR("JTAG error while reading DCSR");
202  return retval;
203  }
204 
205  xscale->reg_cache->reg_list[XSCALE_DCSR].dirty = false;
206  xscale->reg_cache->reg_list[XSCALE_DCSR].valid = true;
207 
208  /* write the register with the value we just read
209  * on this second pass, only the first bit of field0 is guaranteed to be 0)
210  */
211  field0_check_mask = 0x1;
212  fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
213  fields[1].in_value = NULL;
214 
215  jtag_add_dr_scan(target->tap, 3, fields, TAP_DRPAUSE);
216 
217  /* DANGER!!! this must be here. It will make sure that the arguments
218  * to jtag_set_check_value() does not go out of scope! */
219  return jtag_execute_queue();
220 }
221 
222 
224 {
225  uint8_t *in = (uint8_t *)arg;
226  *((uint32_t *)arg) = buf_get_u32(in, 0, 32);
227 }
228 
229 static int xscale_receive(struct target *target, uint32_t *buffer, int num_words)
230 {
231  if (num_words == 0)
233 
234  struct xscale_common *xscale = target_to_xscale(target);
235  int retval = ERROR_OK;
236  enum tap_state path[3];
237  struct scan_field fields[3];
238  uint8_t *field0 = malloc(num_words * 1);
239  uint8_t field0_check_value = 0x2;
240  uint8_t field0_check_mask = 0x6;
241  uint32_t *field1 = malloc(num_words * 4);
242  uint8_t field2_check_value = 0x0;
243  uint8_t field2_check_mask = 0x1;
244  int words_done = 0;
245  int words_scheduled = 0;
246  int i;
247 
248  path[0] = TAP_DRSELECT;
249  path[1] = TAP_DRCAPTURE;
250  path[2] = TAP_DRSHIFT;
251 
252  memset(&fields, 0, sizeof(fields));
253 
254  fields[0].num_bits = 3;
255  uint8_t tmp;
256  fields[0].in_value = &tmp;
257  fields[0].check_value = &field0_check_value;
258  fields[0].check_mask = &field0_check_mask;
259 
260  fields[1].num_bits = 32;
261 
262  fields[2].num_bits = 1;
263  uint8_t tmp2;
264  fields[2].in_value = &tmp2;
265  fields[2].check_value = &field2_check_value;
266  fields[2].check_mask = &field2_check_mask;
267 
269  XSCALE_DBGTX << xscale->xscale_variant,
270  TAP_IDLE);
271  jtag_add_runtest(1, TAP_IDLE); /* ensures that we're in the TAP_IDLE state as the above
272  *could be a no-op */
273 
274  /* repeat until all words have been collected */
275  int attempts = 0;
276  while (words_done < num_words) {
277  /* schedule reads */
278  words_scheduled = 0;
279  for (i = words_done; i < num_words; i++) {
280  fields[0].in_value = &field0[i];
281 
282  jtag_add_pathmove(3, path);
283 
284  fields[1].in_value = (uint8_t *)(field1 + i);
285 
287 
289 
290  words_scheduled++;
291  }
292 
293  retval = jtag_execute_queue();
294  if (retval != ERROR_OK) {
295  LOG_ERROR("JTAG error while receiving data from debug handler");
296  break;
297  }
298 
299  /* examine results */
300  for (i = words_done; i < num_words; i++) {
301  if (!(field0[i] & 1)) {
302  /* move backwards if necessary */
303  int j;
304  for (j = i; j < num_words - 1; j++) {
305  field0[j] = field0[j + 1];
306  field1[j] = field1[j + 1];
307  }
308  words_scheduled--;
309  }
310  }
311  if (words_scheduled == 0) {
312  if (attempts++ == 1000) {
313  LOG_ERROR(
314  "Failed to receiving data from debug handler after 1000 attempts");
315  retval = ERROR_TARGET_TIMEOUT;
316  break;
317  }
318  }
319 
320  words_done += words_scheduled;
321  }
322 
323  for (i = 0; i < num_words; i++)
324  *(buffer++) = buf_get_u32((uint8_t *)&field1[i], 0, 32);
325 
326  free(field1);
327 
328  return retval;
329 }
330 
331 static int xscale_read_tx(struct target *target, int consume)
332 {
333  struct xscale_common *xscale = target_to_xscale(target);
334  enum tap_state path[3];
335  enum tap_state noconsume_path[6];
336  int retval;
337  struct scan_field fields[3];
338  uint8_t field0_in = 0x0;
339  uint8_t field0_check_value = 0x2;
340  uint8_t field0_check_mask = 0x6;
341  uint8_t field2_check_value = 0x0;
342  uint8_t field2_check_mask = 0x1;
343 
345  XSCALE_DBGTX << xscale->xscale_variant,
346  TAP_IDLE);
347 
348  path[0] = TAP_DRSELECT;
349  path[1] = TAP_DRCAPTURE;
350  path[2] = TAP_DRSHIFT;
351 
352  noconsume_path[0] = TAP_DRSELECT;
353  noconsume_path[1] = TAP_DRCAPTURE;
354  noconsume_path[2] = TAP_DREXIT1;
355  noconsume_path[3] = TAP_DRPAUSE;
356  noconsume_path[4] = TAP_DREXIT2;
357  noconsume_path[5] = TAP_DRSHIFT;
358 
359  memset(&fields, 0, sizeof(fields));
360 
361  fields[0].num_bits = 3;
362  fields[0].in_value = &field0_in;
363 
364  fields[1].num_bits = 32;
365  fields[1].in_value = xscale->reg_cache->reg_list[XSCALE_TX].value;
366 
367  fields[2].num_bits = 1;
368  uint8_t tmp;
369  fields[2].in_value = &tmp;
370 
371  // 1 second timeout
372  int64_t then = timeval_ms() + 1000;
373 
374  for (;; ) {
375  /* if we want to consume the register content (i.e. clear TX_READY),
376  * we have to go straight from Capture-DR to Shift-DR
377  * otherwise, we go from Capture-DR to Exit1-DR to Pause-DR
378  */
379  if (consume)
380  jtag_add_pathmove(3, path);
381  else
382  jtag_add_pathmove(ARRAY_SIZE(noconsume_path), noconsume_path);
383 
384  jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
385 
386  jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
387  jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
388 
389  retval = jtag_execute_queue();
390  if (retval != ERROR_OK) {
391  LOG_ERROR("JTAG error while reading TX");
392  return ERROR_TARGET_TIMEOUT;
393  }
394 
395  if (timeval_ms() > then) {
396  LOG_ERROR("time out reading TX register");
397  return ERROR_TARGET_TIMEOUT;
398  }
399  if (!((!(field0_in & 1)) && consume))
400  goto done;
402  LOG_DEBUG("waiting 100ms");
403  alive_sleep(100); /* avoid flooding the logs */
404  } else
405  keep_alive();
406  }
407 done:
408 
409  if (!(field0_in & 1))
411 
412  return ERROR_OK;
413 }
414 
415 static int xscale_write_rx(struct target *target)
416 {
417  struct xscale_common *xscale = target_to_xscale(target);
418  int retval;
419  struct scan_field fields[3];
420  uint8_t field0_out = 0x0;
421  uint8_t field0_in = 0x0;
422  uint8_t field0_check_value = 0x2;
423  uint8_t field0_check_mask = 0x6;
424  uint8_t field2 = 0x0;
425  uint8_t field2_check_value = 0x0;
426  uint8_t field2_check_mask = 0x1;
427 
429  XSCALE_DBGRX << xscale->xscale_variant,
430  TAP_IDLE);
431 
432  memset(&fields, 0, sizeof(fields));
433 
434  fields[0].num_bits = 3;
435  fields[0].out_value = &field0_out;
436  fields[0].in_value = &field0_in;
437 
438  fields[1].num_bits = 32;
439  fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_RX].value;
440 
441  fields[2].num_bits = 1;
442  fields[2].out_value = &field2;
443  uint8_t tmp;
444  fields[2].in_value = &tmp;
445 
446  // 1 second timeout
447  int64_t then = timeval_ms() + 1000;
448 
449  /* poll until rx_read is low */
450  LOG_DEBUG("polling RX");
451  for (;;) {
452  jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
453 
454  jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
455  jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
456 
457  retval = jtag_execute_queue();
458  if (retval != ERROR_OK) {
459  LOG_ERROR("JTAG error while writing RX");
460  return retval;
461  }
462 
463  if (timeval_ms() > then) {
464  LOG_ERROR("time out writing RX register");
465  return ERROR_TARGET_TIMEOUT;
466  }
467  if (!(field0_in & 1))
468  goto done;
470  LOG_DEBUG("waiting 100ms");
471  alive_sleep(100); /* avoid flooding the logs */
472  } else
473  keep_alive();
474  }
475 done:
476 
477  /* set rx_valid */
478  field2 = 0x1;
479  jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
480 
481  retval = jtag_execute_queue();
482  if (retval != ERROR_OK) {
483  LOG_ERROR("JTAG error while writing RX");
484  return retval;
485  }
486 
487  return ERROR_OK;
488 }
489 
490 /* send count elements of size byte to the debug handler */
491 static int xscale_send(struct target *target, const uint8_t *buffer, int count, int size)
492 {
493  struct xscale_common *xscale = target_to_xscale(target);
494  int retval;
495  int done_count = 0;
496 
498  XSCALE_DBGRX << xscale->xscale_variant,
499  TAP_IDLE);
500 
501  static const uint8_t t0;
502  uint8_t t1[4] = { 0 };
503  static const uint8_t t2 = 1;
504  struct scan_field fields[3] = {
505  { .num_bits = 3, .out_value = &t0 },
506  { .num_bits = 32, .out_value = t1 },
507  { .num_bits = 1, .out_value = &t2 },
508  };
509 
510  int endianness = target->endianness;
511  while (done_count++ < count) {
512  uint32_t t;
513 
514  switch (size) {
515  case 4:
516  if (endianness == TARGET_LITTLE_ENDIAN)
517  t = le_to_h_u32(buffer);
518  else
519  t = be_to_h_u32(buffer);
520  break;
521  case 2:
522  if (endianness == TARGET_LITTLE_ENDIAN)
523  t = le_to_h_u16(buffer);
524  else
525  t = be_to_h_u16(buffer);
526  break;
527  case 1:
528  t = buffer[0];
529  break;
530  default:
531  LOG_ERROR("BUG: size neither 4, 2 nor 1");
533  }
534 
535  buf_set_u32(t1, 0, 32, t);
536 
538  3,
539  fields,
540  TAP_IDLE);
541  buffer += size;
542  }
543 
544  retval = jtag_execute_queue();
545  if (retval != ERROR_OK) {
546  LOG_ERROR("JTAG error while sending data to debug handler");
547  return retval;
548  }
549 
550  return ERROR_OK;
551 }
552 
553 static int xscale_send_u32(struct target *target, uint32_t value)
554 {
555  struct xscale_common *xscale = target_to_xscale(target);
556 
557  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_RX].value, 0, 32, value);
558  return xscale_write_rx(target);
559 }
560 
561 static int xscale_write_dcsr(struct target *target, int hold_rst, int ext_dbg_brk)
562 {
563  struct xscale_common *xscale = target_to_xscale(target);
564  int retval;
565  struct scan_field fields[3];
566  uint8_t field0 = 0x0;
567  uint8_t field0_check_value = 0x2;
568  uint8_t field0_check_mask = 0x7;
569  uint8_t field2 = 0x0;
570  uint8_t field2_check_value = 0x0;
571  uint8_t field2_check_mask = 0x1;
572 
573  if (hold_rst != -1)
574  xscale->hold_rst = hold_rst;
575 
576  if (ext_dbg_brk != -1)
577  xscale->external_debug_break = ext_dbg_brk;
578 
580  XSCALE_SELDCSR << xscale->xscale_variant,
581  TAP_IDLE);
582 
583  buf_set_u32(&field0, 1, 1, xscale->hold_rst);
584  buf_set_u32(&field0, 2, 1, xscale->external_debug_break);
585 
586  memset(&fields, 0, sizeof(fields));
587 
588  fields[0].num_bits = 3;
589  fields[0].out_value = &field0;
590  uint8_t tmp;
591  fields[0].in_value = &tmp;
592 
593  fields[1].num_bits = 32;
594  fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
595 
596  fields[2].num_bits = 1;
597  fields[2].out_value = &field2;
598  uint8_t tmp2;
599  fields[2].in_value = &tmp2;
600 
601  jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
602 
603  jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
604  jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
605 
606  retval = jtag_execute_queue();
607  if (retval != ERROR_OK) {
608  LOG_ERROR("JTAG error while writing DCSR");
609  return retval;
610  }
611 
612  xscale->reg_cache->reg_list[XSCALE_DCSR].dirty = false;
613  xscale->reg_cache->reg_list[XSCALE_DCSR].valid = true;
614 
615  return ERROR_OK;
616 }
617 
618 /* parity of the number of bits 0 if even; 1 if odd. for 32 bit words */
619 static unsigned int parity(unsigned int v)
620 {
621  /* unsigned int ov = v; */
622  v ^= v >> 16;
623  v ^= v >> 8;
624  v ^= v >> 4;
625  v &= 0xf;
626  /* LOG_DEBUG("parity of 0x%x is %i", ov, (0x6996 >> v) & 1); */
627  return (0x6996 >> v) & 1;
628 }
629 
630 static int xscale_load_ic(struct target *target, uint32_t va, uint32_t buffer[8])
631 {
632  struct xscale_common *xscale = target_to_xscale(target);
633  uint8_t packet[4] = { 0 };
634  uint8_t cmd = 0;
635  int word;
636  struct scan_field fields[2];
637 
638  LOG_DEBUG("loading miniIC at 0x%8.8" PRIx32, va);
639 
640  /* LDIC into IR */
642  XSCALE_LDIC << xscale->xscale_variant,
643  TAP_IDLE);
644 
645  /* CMD is b011 to load a cacheline into the Mini ICache.
646  * Loading into the main ICache is deprecated, and unused.
647  * It's followed by three zero bits, and 27 address bits.
648  */
649  buf_set_u32(&cmd, 0, 6, 0x3);
650 
651  /* virtual address of desired cache line */
652  buf_set_u32(packet, 0, 27, va >> 5);
653 
654  memset(&fields, 0, sizeof(fields));
655 
656  fields[0].num_bits = 6;
657  fields[0].out_value = &cmd;
658 
659  fields[1].num_bits = 27;
660  fields[1].out_value = packet;
661 
662  jtag_add_dr_scan(target->tap, 2, fields, TAP_IDLE);
663 
664  /* rest of packet is a cacheline: 8 instructions, with parity */
665  fields[0].num_bits = 32;
666  fields[0].out_value = packet;
667 
668  fields[1].num_bits = 1;
669  fields[1].out_value = &cmd;
670 
671  for (word = 0; word < 8; word++) {
672  buf_set_u32(packet, 0, 32, buffer[word]);
673 
674  uint32_t value;
675  memcpy(&value, packet, sizeof(uint32_t));
676  cmd = parity(value);
677 
678  jtag_add_dr_scan(target->tap, 2, fields, TAP_IDLE);
679  }
680 
681  return jtag_execute_queue();
682 }
683 
684 static int xscale_invalidate_ic_line(struct target *target, uint32_t va)
685 {
686  struct xscale_common *xscale = target_to_xscale(target);
687  uint8_t packet[4] = { 0 };
688  uint8_t cmd = 0;
689  struct scan_field fields[2];
690 
692  XSCALE_LDIC << xscale->xscale_variant,
693  TAP_IDLE);
694 
695  /* CMD for invalidate IC line b000, bits [6:4] b000 */
696  buf_set_u32(&cmd, 0, 6, 0x0);
697 
698  /* virtual address of desired cache line */
699  buf_set_u32(packet, 0, 27, va >> 5);
700 
701  memset(&fields, 0, sizeof(fields));
702 
703  fields[0].num_bits = 6;
704  fields[0].out_value = &cmd;
705 
706  fields[1].num_bits = 27;
707  fields[1].out_value = packet;
708 
709  jtag_add_dr_scan(target->tap, 2, fields, TAP_IDLE);
710 
711  return ERROR_OK;
712 }
713 
715 {
716  struct xscale_common *xscale = target_to_xscale(target);
717  int i;
718  int retval;
719 
720  uint32_t low_reset_branch, high_reset_branch;
721 
722  for (i = 1; i < 8; i++) {
723  /* if there's a static vector specified for this exception, override */
724  if (xscale->static_high_vectors_set & (1 << i))
725  xscale->high_vectors[i] = xscale->static_high_vectors[i];
726  else {
727  retval = target_read_u32(target, 0xffff0000 + 4*i, &xscale->high_vectors[i]);
728  if (retval == ERROR_TARGET_TIMEOUT)
729  return retval;
730  if (retval != ERROR_OK) {
731  /* Some of these reads will fail as part of normal execution */
732  xscale->high_vectors[i] = ARMV4_5_B(0xfffffe, 0);
733  }
734  }
735  }
736 
737  for (i = 1; i < 8; i++) {
738  if (xscale->static_low_vectors_set & (1 << i))
739  xscale->low_vectors[i] = xscale->static_low_vectors[i];
740  else {
741  retval = target_read_u32(target, 0x0 + 4*i, &xscale->low_vectors[i]);
742  if (retval == ERROR_TARGET_TIMEOUT)
743  return retval;
744  if (retval != ERROR_OK) {
745  /* Some of these reads will fail as part of normal execution */
746  xscale->low_vectors[i] = ARMV4_5_B(0xfffffe, 0);
747  }
748  }
749  }
750 
751  /* calculate branches to debug handler */
752  low_reset_branch = (xscale->handler_address + 0x20 - 0x0 - 0x8) >> 2;
753  high_reset_branch = (xscale->handler_address + 0x20 - 0xffff0000 - 0x8) >> 2;
754 
755  xscale->low_vectors[0] = ARMV4_5_B((low_reset_branch & 0xffffff), 0);
756  xscale->high_vectors[0] = ARMV4_5_B((high_reset_branch & 0xffffff), 0);
757 
758  /* invalidate and load exception vectors in mini i-cache */
760  xscale_invalidate_ic_line(target, 0xffff0000);
761 
762  xscale_load_ic(target, 0x0, xscale->low_vectors);
763  xscale_load_ic(target, 0xffff0000, xscale->high_vectors);
764 
765  return ERROR_OK;
766 }
767 
768 static int xscale_arch_state(struct target *target)
769 {
770  struct xscale_common *xscale = target_to_xscale(target);
771  struct arm *arm = &xscale->arm;
772 
773  static const char *arch_dbg_reason[] = {
774  "", "\n(processor reset)", "\n(trace buffer full)"
775  };
776 
778  LOG_ERROR("BUG: called for a non-ARMv4/5 target");
780  }
781 
783  LOG_USER("MMU: %s, D-Cache: %s, I-Cache: %s%s",
787  arch_dbg_reason[xscale->arch_debug_reason]);
788 
789  return ERROR_OK;
790 }
791 
792 static int xscale_poll(struct target *target)
793 {
794  int retval = ERROR_OK;
795 
797  enum target_state previous_state = target->state;
798  retval = xscale_read_tx(target, 0);
799  if (retval == ERROR_OK) {
800 
801  /* there's data to read from the tx register, we entered debug state */
803 
804  /* process debug entry, fetching current mode regs */
805  retval = xscale_debug_entry(target);
806  } else if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
807  LOG_USER("error while polling TX register, reset CPU");
808  /* here we "lie" so GDB won't get stuck and a reset can be performed */
810  }
811 
812  /* debug_entry could have overwritten target state (i.e. immediate resume)
813  * don't signal event handlers in that case
814  */
815  if (target->state != TARGET_HALTED)
816  return ERROR_OK;
817 
818  /* if target was running, signal that we halted
819  * otherwise we reentered from debug execution */
820  if (previous_state == TARGET_RUNNING)
822  else
824  }
825 
826  return retval;
827 }
828 
829 static int xscale_debug_entry(struct target *target)
830 {
831  struct xscale_common *xscale = target_to_xscale(target);
832  struct arm *arm = &xscale->arm;
833  uint32_t pc;
834  uint32_t buffer[10];
835  unsigned int i;
836  int retval;
837  uint32_t moe;
838 
839  /* clear external dbg break (will be written on next DCSR read) */
840  xscale->external_debug_break = 0;
841  retval = xscale_read_dcsr(target);
842  if (retval != ERROR_OK)
843  return retval;
844 
845  /* get r0, pc, r1 to r7 and cpsr */
846  retval = xscale_receive(target, buffer, 10);
847  if (retval != ERROR_OK)
848  return retval;
849 
850  /* move r0 from buffer to register cache */
851  buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, buffer[0]);
852  arm->core_cache->reg_list[0].dirty = true;
853  arm->core_cache->reg_list[0].valid = true;
854  LOG_DEBUG("r0: 0x%8.8" PRIx32, buffer[0]);
855 
856  /* move pc from buffer to register cache */
857  buf_set_u32(arm->pc->value, 0, 32, buffer[1]);
858  arm->pc->dirty = true;
859  arm->pc->valid = true;
860  LOG_DEBUG("pc: 0x%8.8" PRIx32, buffer[1]);
861 
862  /* move data from buffer to register cache */
863  for (i = 1; i <= 7; i++) {
864  buf_set_u32(arm->core_cache->reg_list[i].value, 0, 32, buffer[1 + i]);
865  arm->core_cache->reg_list[i].dirty = true;
866  arm->core_cache->reg_list[i].valid = true;
867  LOG_DEBUG("r%i: 0x%8.8" PRIx32, i, buffer[i + 1]);
868  }
869 
870  arm_set_cpsr(arm, buffer[9]);
871  LOG_DEBUG("cpsr: 0x%8.8" PRIx32, buffer[9]);
872 
873  if (!is_arm_mode(arm->core_mode)) {
875  LOG_ERROR("cpsr contains invalid mode value - communication failure");
876  return ERROR_TARGET_FAILURE;
877  }
878  LOG_DEBUG("target entered debug state in %s mode",
880 
881  /* get banked registers, r8 to r14, and spsr if not in USR/SYS mode */
882  if (arm->spsr) {
884  buf_set_u32(arm->spsr->value, 0, 32, buffer[7]);
885  arm->spsr->dirty = false;
886  arm->spsr->valid = true;
887  } else {
888  /* r8 to r14, but no spsr */
890  }
891 
892  /* move data from buffer to right banked register in cache */
893  for (i = 8; i <= 14; i++) {
894  struct reg *r = arm_reg_current(arm, i);
895 
896  buf_set_u32(r->value, 0, 32, buffer[i - 8]);
897  r->dirty = false;
898  r->valid = true;
899  }
900 
901  /* mark xscale regs invalid to ensure they are retrieved from the
902  * debug handler if requested */
903  for (i = 0; i < xscale->reg_cache->num_regs; i++)
904  xscale->reg_cache->reg_list[i].valid = false;
905 
906  /* examine debug reason */
908  moe = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 2, 3);
909 
910  /* stored PC (for calculating fixup) */
911  pc = buf_get_u32(arm->pc->value, 0, 32);
912 
913  switch (moe) {
914  case 0x0: /* Processor reset */
917  pc -= 4;
918  break;
919  case 0x1: /* Instruction breakpoint hit */
922  pc -= 4;
923  break;
924  case 0x2: /* Data breakpoint hit */
927  pc -= 4;
928  break;
929  case 0x3: /* BKPT instruction executed */
932  pc -= 4;
933  break;
934  case 0x4: /* Ext. debug event */
937  pc -= 4;
938  break;
939  case 0x5: /* Vector trap occurred */
942  pc -= 4;
943  break;
944  case 0x6: /* Trace buffer full break */
947  pc -= 4;
948  break;
949  case 0x7: /* Reserved (may flag Hot-Debug support) */
950  default:
951  LOG_ERROR("Method of Entry is 'Reserved'");
952  exit(-1);
953  break;
954  }
955 
956  /* apply PC fixup */
957  buf_set_u32(arm->pc->value, 0, 32, pc);
958 
959  /* on the first debug entry, identify cache type */
960  if (xscale->armv4_5_mmu.armv4_5_cache.ctype == -1) {
961  uint32_t cache_type_reg;
962 
963  /* read cp15 cache type register */
965  cache_type_reg = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CACHETYPE].value,
966  0,
967  32);
968 
969  armv4_5_identify_cache(cache_type_reg, &xscale->armv4_5_mmu.armv4_5_cache);
970  }
971 
972  /* examine MMU and Cache settings
973  * read cp15 control register */
975  xscale->cp15_control_reg =
976  buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CTRL].value, 0, 32);
977  xscale->armv4_5_mmu.mmu_enabled = xscale->cp15_control_reg & 0x1U;
979  xscale->cp15_control_reg & 0x4U;
981  xscale->cp15_control_reg & 0x1000U;
982 
983  /* tracing enabled, read collected trace data */
984  if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
986 
987  /* Resume if entered debug due to buffer fill and we're still collecting
988  * trace data. Note that a debug exception due to trace buffer full
989  * can only happen in fill mode. */
991  if (--xscale->trace.fill_counter > 0)
992  xscale_resume(target, true, 0x0, true, false);
993  } else /* entered debug for other reason; reset counter */
994  xscale->trace.fill_counter = 0;
995  }
996 
997  return ERROR_OK;
998 }
999 
1000 static int xscale_halt(struct target *target)
1001 {
1002  struct xscale_common *xscale = target_to_xscale(target);
1003 
1004  LOG_DEBUG("target->state: %s",
1006 
1007  if (target->state == TARGET_HALTED) {
1008  LOG_DEBUG("target was already halted");
1009  return ERROR_OK;
1010  } else if (target->state == TARGET_UNKNOWN) {
1011  /* this must not happen for a xscale target */
1012  LOG_ERROR("target was in unknown state when halt was requested");
1013  return ERROR_TARGET_INVALID;
1014  } else if (target->state == TARGET_RESET)
1015  LOG_DEBUG("target->state == TARGET_RESET");
1016  else {
1017  /* assert external dbg break */
1018  xscale->external_debug_break = 1;
1020 
1022  }
1023 
1024  return ERROR_OK;
1025 }
1026 
1027 static int xscale_enable_single_step(struct target *target, uint32_t next_pc)
1028 {
1029  struct xscale_common *xscale = target_to_xscale(target);
1030  struct reg *ibcr0 = &xscale->reg_cache->reg_list[XSCALE_IBCR0];
1031  int retval;
1032 
1033  if (xscale->ibcr0_used) {
1034  struct breakpoint *ibcr0_bp =
1035  breakpoint_find(target, buf_get_u32(ibcr0->value, 0, 32) & 0xfffffffe);
1036 
1037  if (ibcr0_bp)
1038  xscale_unset_breakpoint(target, ibcr0_bp);
1039  else {
1040  LOG_ERROR(
1041  "BUG: xscale->ibcr0_used is set, but no breakpoint with that address found");
1042  exit(-1);
1043  }
1044  }
1045 
1046  retval = xscale_set_reg_u32(ibcr0, next_pc | 0x1);
1047  if (retval != ERROR_OK)
1048  return retval;
1049 
1050  return ERROR_OK;
1051 }
1052 
1054 {
1055  struct xscale_common *xscale = target_to_xscale(target);
1056  struct reg *ibcr0 = &xscale->reg_cache->reg_list[XSCALE_IBCR0];
1057  int retval;
1058 
1059  retval = xscale_set_reg_u32(ibcr0, 0x0);
1060  if (retval != ERROR_OK)
1061  return retval;
1062 
1063  return ERROR_OK;
1064 }
1065 
1067 {
1069 
1070  while (watchpoint) {
1071  if (!watchpoint->is_set)
1074  }
1075 }
1076 
1078 {
1080 
1081  /* set any pending breakpoints */
1082  while (breakpoint) {
1083  if (!breakpoint->is_set)
1086  }
1087 }
1088 
1089 static void xscale_free_trace_data(struct xscale_common *xscale)
1090 {
1091  struct xscale_trace_data *td = xscale->trace.data;
1092  while (td) {
1093  struct xscale_trace_data *next_td = td->next;
1094  free(td->entries);
1095  free(td);
1096  td = next_td;
1097  }
1098  xscale->trace.data = NULL;
1099 }
1100 
1101 static int xscale_resume(struct target *target, bool current,
1102  target_addr_t address, bool handle_breakpoints, bool debug_execution)
1103 {
1104  struct xscale_common *xscale = target_to_xscale(target);
1105  struct arm *arm = &xscale->arm;
1106  uint32_t current_pc;
1107  int retval;
1108  int i;
1109 
1110  LOG_DEBUG("-");
1111 
1112  if (target->state != TARGET_HALTED) {
1113  LOG_TARGET_ERROR(target, "not halted");
1114  return ERROR_TARGET_NOT_HALTED;
1115  }
1116 
1117  if (!debug_execution)
1119 
1120  /* update vector tables */
1121  retval = xscale_update_vectors(target);
1122  if (retval != ERROR_OK)
1123  return retval;
1124 
1125  /* current = true: continue on current pc, otherwise continue at <address> */
1126  if (!current)
1127  buf_set_u32(arm->pc->value, 0, 32, address);
1128 
1129  current_pc = buf_get_u32(arm->pc->value, 0, 32);
1130 
1131  /* if we're at the reset vector, we have to simulate the branch */
1132  if (current_pc == 0x0) {
1134  current_pc = buf_get_u32(arm->pc->value, 0, 32);
1135  }
1136 
1137  /* the front-end may request us not to handle breakpoints */
1138  if (handle_breakpoints) {
1139  struct breakpoint *breakpoint;
1141  buf_get_u32(arm->pc->value, 0, 32));
1142  if (breakpoint) {
1143  uint32_t next_pc;
1144  enum trace_mode saved_trace_mode;
1145 
1146  /* there's a breakpoint at the current PC, we have to step over it */
1147  LOG_DEBUG("unset breakpoint at " TARGET_ADDR_FMT "",
1148  breakpoint->address);
1150 
1151  /* calculate PC of next instruction */
1152  retval = arm_simulate_step(target, &next_pc);
1153  if (retval != ERROR_OK) {
1154  uint32_t current_opcode;
1155  target_read_u32(target, current_pc, &current_opcode);
1156  LOG_ERROR(
1157  "BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32,
1158  current_opcode);
1159  }
1160 
1161  LOG_DEBUG("enable single-step");
1163 
1164  /* restore banked registers */
1165  retval = xscale_restore_banked(target);
1166  if (retval != ERROR_OK)
1167  return retval;
1168 
1169  /* send resume request */
1170  xscale_send_u32(target, 0x30);
1171 
1172  /* send CPSR */
1174  buf_get_u32(arm->cpsr->value, 0, 32));
1175  LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32,
1176  buf_get_u32(arm->cpsr->value, 0, 32));
1177 
1178  for (i = 7; i >= 0; i--) {
1179  /* send register */
1181  buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
1182  LOG_DEBUG("writing r%i with value 0x%8.8" PRIx32,
1183  i, buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
1184  }
1185 
1186  /* send PC */
1188  buf_get_u32(arm->pc->value, 0, 32));
1189  LOG_DEBUG("writing PC with value 0x%8.8" PRIx32,
1190  buf_get_u32(arm->pc->value, 0, 32));
1191 
1192  /* disable trace data collection in xscale_debug_entry() */
1193  saved_trace_mode = xscale->trace.mode;
1194  xscale->trace.mode = XSCALE_TRACE_DISABLED;
1195 
1196  /* wait for and process debug entry */
1198 
1199  /* re-enable trace buffer, if enabled previously */
1200  xscale->trace.mode = saved_trace_mode;
1201 
1202  LOG_DEBUG("disable single-step");
1204 
1205  LOG_DEBUG("set breakpoint at " TARGET_ADDR_FMT "",
1206  breakpoint->address);
1208  }
1209  }
1210 
1211  /* enable any pending breakpoints and watchpoints */
1214 
1215  /* restore banked registers */
1216  retval = xscale_restore_banked(target);
1217  if (retval != ERROR_OK)
1218  return retval;
1219 
1220  /* send resume request (command 0x30 or 0x31)
1221  * clean the trace buffer if it is to be enabled (0x62) */
1222  if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
1223  if (xscale->trace.mode == XSCALE_TRACE_FILL) {
1224  /* If trace enabled in fill mode and starting collection of new set
1225  * of buffers, initialize buffer counter and free previous buffers */
1226  if (xscale->trace.fill_counter == 0) {
1227  xscale->trace.fill_counter = xscale->trace.buffer_fill;
1228  xscale_free_trace_data(xscale);
1229  }
1230  } else /* wrap mode; free previous buffer */
1231  xscale_free_trace_data(xscale);
1232 
1233  xscale_send_u32(target, 0x62);
1234  xscale_send_u32(target, 0x31);
1235  } else
1236  xscale_send_u32(target, 0x30);
1237 
1238  /* send CPSR */
1240  LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32,
1241  buf_get_u32(arm->cpsr->value, 0, 32));
1242 
1243  for (i = 7; i >= 0; i--) {
1244  /* send register */
1246  LOG_DEBUG("writing r%i with value 0x%8.8" PRIx32,
1247  i, buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
1248  }
1249 
1250  /* send PC */
1252  LOG_DEBUG("wrote PC with value 0x%8.8" PRIx32,
1253  buf_get_u32(arm->pc->value, 0, 32));
1254 
1256 
1257  if (!debug_execution) {
1258  /* registers are now invalid */
1262  } else {
1265  }
1266 
1267  LOG_DEBUG("target resumed");
1268 
1269  return ERROR_OK;
1270 }
1271 
1272 static int xscale_step_inner(struct target *target, bool current,
1273  uint32_t address, bool handle_breakpoints)
1274 {
1275  struct xscale_common *xscale = target_to_xscale(target);
1276  struct arm *arm = &xscale->arm;
1277  uint32_t next_pc;
1278  int retval;
1279  int i;
1280 
1282 
1283  /* calculate PC of next instruction */
1284  retval = arm_simulate_step(target, &next_pc);
1285  if (retval != ERROR_OK) {
1286  uint32_t current_opcode, current_pc;
1287  current_pc = buf_get_u32(arm->pc->value, 0, 32);
1288 
1289  target_read_u32(target, current_pc, &current_opcode);
1290  LOG_ERROR(
1291  "BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32,
1292  current_opcode);
1293  return retval;
1294  }
1295 
1296  LOG_DEBUG("enable single-step");
1297  retval = xscale_enable_single_step(target, next_pc);
1298  if (retval != ERROR_OK)
1299  return retval;
1300 
1301  /* restore banked registers */
1302  retval = xscale_restore_banked(target);
1303  if (retval != ERROR_OK)
1304  return retval;
1305 
1306  /* send resume request (command 0x30 or 0x31)
1307  * clean the trace buffer if it is to be enabled (0x62) */
1308  if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
1309  retval = xscale_send_u32(target, 0x62);
1310  if (retval != ERROR_OK)
1311  return retval;
1312  retval = xscale_send_u32(target, 0x31);
1313  if (retval != ERROR_OK)
1314  return retval;
1315  } else {
1316  retval = xscale_send_u32(target, 0x30);
1317  if (retval != ERROR_OK)
1318  return retval;
1319  }
1320 
1321  /* send CPSR */
1322  retval = xscale_send_u32(target,
1323  buf_get_u32(arm->cpsr->value, 0, 32));
1324  if (retval != ERROR_OK)
1325  return retval;
1326  LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32,
1327  buf_get_u32(arm->cpsr->value, 0, 32));
1328 
1329  for (i = 7; i >= 0; i--) {
1330  /* send register */
1331  retval = xscale_send_u32(target,
1332  buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
1333  if (retval != ERROR_OK)
1334  return retval;
1335  LOG_DEBUG("writing r%i with value 0x%8.8" PRIx32, i,
1336  buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
1337  }
1338 
1339  /* send PC */
1340  retval = xscale_send_u32(target,
1341  buf_get_u32(arm->pc->value, 0, 32));
1342  if (retval != ERROR_OK)
1343  return retval;
1344  LOG_DEBUG("wrote PC with value 0x%8.8" PRIx32,
1345  buf_get_u32(arm->pc->value, 0, 32));
1346 
1348 
1349  /* registers are now invalid */
1351 
1352  /* wait for and process debug entry */
1353  retval = xscale_debug_entry(target);
1354  if (retval != ERROR_OK)
1355  return retval;
1356 
1357  LOG_DEBUG("disable single-step");
1359  if (retval != ERROR_OK)
1360  return retval;
1361 
1363 
1364  return ERROR_OK;
1365 }
1366 
1367 static int xscale_step(struct target *target, bool current,
1368  target_addr_t address, bool handle_breakpoints)
1369 {
1370  struct arm *arm = target_to_arm(target);
1371  struct breakpoint *breakpoint = NULL;
1372 
1373  uint32_t current_pc;
1374  int retval;
1375 
1376  if (target->state != TARGET_HALTED) {
1377  LOG_TARGET_ERROR(target, "not halted");
1378  return ERROR_TARGET_NOT_HALTED;
1379  }
1380 
1381  /* current = true: continue on current pc, otherwise continue at <address> */
1382  if (!current)
1383  buf_set_u32(arm->pc->value, 0, 32, address);
1384 
1385  current_pc = buf_get_u32(arm->pc->value, 0, 32);
1386 
1387  /* if we're at the reset vector, we have to simulate the step */
1388  if (current_pc == 0x0) {
1389  retval = arm_simulate_step(target, NULL);
1390  if (retval != ERROR_OK)
1391  return retval;
1392  current_pc = buf_get_u32(arm->pc->value, 0, 32);
1393  LOG_DEBUG("current pc %" PRIx32, current_pc);
1394 
1397 
1398  return ERROR_OK;
1399  }
1400 
1401  /* the front-end may request us not to handle breakpoints */
1402  if (handle_breakpoints)
1404  buf_get_u32(arm->pc->value, 0, 32));
1405  if (breakpoint) {
1407  if (retval != ERROR_OK)
1408  return retval;
1409  }
1410 
1411  retval = xscale_step_inner(target, current, address, handle_breakpoints);
1412  if (retval != ERROR_OK)
1413  return retval;
1414 
1415  if (breakpoint)
1417 
1418  LOG_DEBUG("target stepped");
1419 
1420  return ERROR_OK;
1421 
1422 }
1423 
1424 static int xscale_assert_reset(struct target *target)
1425 {
1426  struct xscale_common *xscale = target_to_xscale(target);
1427 
1428  /* TODO: apply hw reset signal in not examined state */
1429  if (!(target_was_examined(target))) {
1430  LOG_WARNING("Reset is not asserted because the target is not examined.");
1431  LOG_WARNING("Use a reset button or power cycle the target.");
1433  }
1434 
1435  LOG_DEBUG("target->state: %s",
1437 
1438  /* assert reset */
1439  jtag_add_reset(0, 1);
1440 
1441  /* sleep 1ms, to be sure we fulfill any requirements */
1442  jtag_add_sleep(1000);
1444 
1445  /* select DCSR instruction (set endstate to R-T-I to ensure we don't
1446  * end up in T-L-R, which would reset JTAG
1447  */
1449  XSCALE_SELDCSR << xscale->xscale_variant,
1450  TAP_IDLE);
1451 
1452  /* set Hold reset, Halt mode and Trap Reset */
1453  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 30, 1, 0x1);
1454  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 16, 1, 0x1);
1455  xscale_write_dcsr(target, 1, 0);
1456 
1457  /* select BYPASS, because having DCSR selected caused problems on the PXA27x */
1460 
1462 
1463  if (target->reset_halt) {
1464  int retval = target_halt(target);
1465  if (retval != ERROR_OK)
1466  return retval;
1467  }
1468 
1469  return ERROR_OK;
1470 }
1471 
1473 {
1474  struct xscale_common *xscale = target_to_xscale(target);
1476 
1477  LOG_DEBUG("-");
1478 
1479  xscale->ibcr_available = 2;
1480  xscale->ibcr0_used = 0;
1481  xscale->ibcr1_used = 0;
1482 
1483  xscale->dbr_available = 2;
1484  xscale->dbr0_used = 0;
1485  xscale->dbr1_used = 0;
1486 
1487  /* mark all hardware breakpoints as unset */
1488  while (breakpoint) {
1489  if (breakpoint->type == BKPT_HARD)
1490  breakpoint->is_set = false;
1492  }
1493 
1494  xscale->trace.mode = XSCALE_TRACE_DISABLED;
1495  xscale_free_trace_data(xscale);
1496 
1498 
1499  /* FIXME mark hardware watchpoints got unset too. Also,
1500  * at least some of the XScale registers are invalid...
1501  */
1502 
1503  /*
1504  * REVISIT: *assumes* we had a SRST+TRST reset so the mini-icache
1505  * contents got invalidated. Safer to force that, so writing new
1506  * contents can't ever fail..
1507  */
1508  {
1509  uint32_t address;
1510  unsigned int buf_cnt;
1511  const uint8_t *buffer = xscale_debug_handler;
1512  int retval;
1513 
1514  /* release SRST */
1515  jtag_add_reset(0, 0);
1516 
1517  /* wait 300ms; 150 and 100ms were not enough */
1518  jtag_add_sleep(300*1000);
1519 
1520  jtag_add_runtest(2030, TAP_IDLE);
1522 
1523  /* set Hold reset, Halt mode and Trap Reset */
1524  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 30, 1, 0x1);
1525  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 16, 1, 0x1);
1526  xscale_write_dcsr(target, 1, 0);
1527 
1528  /* Load the debug handler into the mini-icache. Since
1529  * it's using halt mode (not monitor mode), it runs in
1530  * "Special Debug State" for access to registers, memory,
1531  * coprocessors, trace data, etc.
1532  */
1533  address = xscale->handler_address;
1534  for (unsigned int binary_size = sizeof(xscale_debug_handler);
1535  binary_size > 0;
1536  binary_size -= buf_cnt, buffer += buf_cnt) {
1537  uint32_t cache_line[8];
1538  unsigned int i;
1539 
1540  buf_cnt = binary_size;
1541  if (buf_cnt > 32)
1542  buf_cnt = 32;
1543 
1544  for (i = 0; i < buf_cnt; i += 4) {
1545  /* convert LE buffer to host-endian uint32_t */
1546  cache_line[i / 4] = le_to_h_u32(&buffer[i]);
1547  }
1548 
1549  for (; i < 32; i += 4)
1550  cache_line[i / 4] = 0xe1a08008;
1551 
1552  /* only load addresses other than the reset vectors */
1553  if ((address % 0x400) != 0x0) {
1554  retval = xscale_load_ic(target, address,
1555  cache_line);
1556  if (retval != ERROR_OK)
1557  return retval;
1558  }
1559 
1560  address += buf_cnt;
1561  }
1562 
1563  retval = xscale_load_ic(target, 0x0,
1564  xscale->low_vectors);
1565  if (retval != ERROR_OK)
1566  return retval;
1567  retval = xscale_load_ic(target, 0xffff0000,
1568  xscale->high_vectors);
1569  if (retval != ERROR_OK)
1570  return retval;
1571 
1573 
1574  jtag_add_sleep(100000);
1575 
1576  /* set Hold reset, Halt mode and Trap Reset */
1577  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 30, 1, 0x1);
1578  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 16, 1, 0x1);
1579  xscale_write_dcsr(target, 1, 0);
1580 
1581  /* clear Hold reset to let the target run (should enter debug handler) */
1582  xscale_write_dcsr(target, 0, 1);
1584 
1585  if (!target->reset_halt) {
1586  jtag_add_sleep(10000);
1587 
1588  /* we should have entered debug now */
1591 
1592  /* resume the target */
1593  xscale_resume(target, true, 0x0, true, false);
1594  }
1595  }
1596 
1597  return ERROR_OK;
1598 }
1599 
1600 static int xscale_read_core_reg(struct target *target, struct reg *r,
1601  int num, enum arm_mode mode)
1602 {
1604  LOG_ERROR("not implemented");
1605  return ERROR_OK;
1606 }
1607 
1608 static int xscale_write_core_reg(struct target *target, struct reg *r,
1609  int num, enum arm_mode mode, uint8_t *value)
1610 {
1612  LOG_ERROR("not implemented");
1613  return ERROR_OK;
1614 }
1615 
1616 static int xscale_full_context(struct target *target)
1617 {
1618  struct arm *arm = target_to_arm(target);
1619 
1620  uint32_t *buffer;
1621 
1622  int i, j;
1623 
1624  LOG_DEBUG("-");
1625 
1626  if (target->state != TARGET_HALTED) {
1627  LOG_TARGET_ERROR(target, "not halted");
1628  return ERROR_TARGET_NOT_HALTED;
1629  }
1630 
1631  buffer = malloc(4 * 8);
1632 
1633  /* iterate through processor modes (FIQ, IRQ, SVC, ABT, UND and SYS)
1634  * we can't enter User mode on an XScale (unpredictable),
1635  * but User shares registers with SYS
1636  */
1637  for (i = 1; i < 7; i++) {
1639  bool valid = true;
1640  struct reg *r;
1641 
1642  if (mode == ARM_MODE_USR)
1643  continue;
1644 
1645  /* check if there are invalid registers in the current mode
1646  */
1647  for (j = 0; valid && j <= 16; j++) {
1649  mode, j).valid)
1650  valid = false;
1651  }
1652  if (valid)
1653  continue;
1654 
1655  /* request banked registers */
1656  xscale_send_u32(target, 0x0);
1657 
1658  /* send CPSR for desired bank mode */
1659  xscale_send_u32(target, mode | 0xc0 /* I/F bits */);
1660 
1661  /* get banked registers: r8 to r14; and SPSR
1662  * except in USR/SYS mode
1663  */
1664  if (mode != ARM_MODE_SYS) {
1665  /* SPSR */
1667  mode, 16);
1668 
1670 
1671  buf_set_u32(r->value, 0, 32, buffer[7]);
1672  r->dirty = false;
1673  r->valid = true;
1674  } else
1676 
1677  /* move data from buffer to register cache */
1678  for (j = 8; j <= 14; j++) {
1680  mode, j);
1681 
1682  buf_set_u32(r->value, 0, 32, buffer[j - 8]);
1683  r->dirty = false;
1684  r->valid = true;
1685  }
1686  }
1687 
1688  free(buffer);
1689 
1690  return ERROR_OK;
1691 }
1692 
1694 {
1695  struct arm *arm = target_to_arm(target);
1696 
1697  int i, j;
1698 
1699  if (target->state != TARGET_HALTED) {
1700  LOG_TARGET_ERROR(target, "not halted");
1701  return ERROR_TARGET_NOT_HALTED;
1702  }
1703 
1704  /* iterate through processor modes (FIQ, IRQ, SVC, ABT, UND and SYS)
1705  * and check if any banked registers need to be written. Ignore
1706  * USR mode (number 0) in favor of SYS; we can't enter User mode on
1707  * an XScale (unpredictable), but they share all registers.
1708  */
1709  for (i = 1; i < 7; i++) {
1711  struct reg *r;
1712 
1713  if (mode == ARM_MODE_USR)
1714  continue;
1715 
1716  /* check if there are dirty registers in this mode */
1717  for (j = 8; j <= 14; j++) {
1719  mode, j).dirty)
1720  goto dirty;
1721  }
1722 
1723  /* if not USR/SYS, check if the SPSR needs to be written */
1724  if (mode != ARM_MODE_SYS) {
1726  mode, 16).dirty)
1727  goto dirty;
1728  }
1729 
1730  /* there's nothing to flush for this mode */
1731  continue;
1732 
1733 dirty:
1734  /* command 0x1: "send banked registers" */
1735  xscale_send_u32(target, 0x1);
1736 
1737  /* send CPSR for desired mode */
1738  xscale_send_u32(target, mode | 0xc0 /* I/F bits */);
1739 
1740  /* send r8 to r14/lr ... only FIQ needs more than r13..r14,
1741  * but this protocol doesn't understand that nuance.
1742  */
1743  for (j = 8; j <= 14; j++) {
1745  mode, j);
1746  xscale_send_u32(target, buf_get_u32(r->value, 0, 32));
1747  r->dirty = false;
1748  }
1749 
1750  /* send spsr if not in USR/SYS mode */
1751  if (mode != ARM_MODE_SYS) {
1753  mode, 16);
1754  xscale_send_u32(target, buf_get_u32(r->value, 0, 32));
1755  r->dirty = false;
1756  }
1757  }
1758 
1759  return ERROR_OK;
1760 }
1761 
1763  uint32_t size, uint32_t count, uint8_t *buffer)
1764 {
1765  struct xscale_common *xscale = target_to_xscale(target);
1766  uint32_t *buf32;
1767  uint32_t i;
1768  int retval;
1769 
1770  LOG_DEBUG("address: " TARGET_ADDR_FMT ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32,
1771  address,
1772  size,
1773  count);
1774 
1775  if (target->state != TARGET_HALTED) {
1776  LOG_TARGET_ERROR(target, "not halted");
1777  return ERROR_TARGET_NOT_HALTED;
1778  }
1779 
1780  /* sanitize arguments */
1781  if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1783 
1784  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1786 
1787  /* send memory read request (command 0x1n, n: access size) */
1788  retval = xscale_send_u32(target, 0x10 | size);
1789  if (retval != ERROR_OK)
1790  return retval;
1791 
1792  /* send base address for read request */
1793  retval = xscale_send_u32(target, address);
1794  if (retval != ERROR_OK)
1795  return retval;
1796 
1797  /* send number of requested data words */
1798  retval = xscale_send_u32(target, count);
1799  if (retval != ERROR_OK)
1800  return retval;
1801 
1802  /* receive data from target (count times 32-bit words in host endianness) */
1803  buf32 = malloc(4 * count);
1804  retval = xscale_receive(target, buf32, count);
1805  if (retval != ERROR_OK) {
1806  free(buf32);
1807  return retval;
1808  }
1809 
1810  /* extract data from host-endian buffer into byte stream */
1811  for (i = 0; i < count; i++) {
1812  switch (size) {
1813  case 4:
1814  target_buffer_set_u32(target, buffer, buf32[i]);
1815  buffer += 4;
1816  break;
1817  case 2:
1818  target_buffer_set_u16(target, buffer, buf32[i] & 0xffff);
1819  buffer += 2;
1820  break;
1821  case 1:
1822  *buffer++ = buf32[i] & 0xff;
1823  break;
1824  default:
1825  LOG_ERROR("invalid read size");
1827  }
1828  }
1829 
1830  free(buf32);
1831 
1832  /* examine DCSR, to see if Sticky Abort (SA) got set */
1833  retval = xscale_read_dcsr(target);
1834  if (retval != ERROR_OK)
1835  return retval;
1836  if (buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 5, 1) == 1) {
1837  /* clear SA bit */
1838  retval = xscale_send_u32(target, 0x60);
1839  if (retval != ERROR_OK)
1840  return retval;
1841 
1842  return ERROR_TARGET_DATA_ABORT;
1843  }
1844 
1845  return ERROR_OK;
1846 }
1847 
1849  uint32_t size, uint32_t count, uint8_t *buffer)
1850 {
1851  struct xscale_common *xscale = target_to_xscale(target);
1852 
1853  /* with MMU inactive, there are only physical addresses */
1854  if (!xscale->armv4_5_mmu.mmu_enabled)
1856 
1858  LOG_ERROR("%s: %s is not implemented. Disable MMU?",
1859  target_name(target), __func__);
1860  return ERROR_FAIL;
1861 }
1862 
1864  uint32_t size, uint32_t count, const uint8_t *buffer)
1865 {
1866  struct xscale_common *xscale = target_to_xscale(target);
1867  int retval;
1868 
1869  LOG_DEBUG("address: " TARGET_ADDR_FMT ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32,
1870  address,
1871  size,
1872  count);
1873 
1874  if (target->state != TARGET_HALTED) {
1875  LOG_TARGET_ERROR(target, "not halted");
1876  return ERROR_TARGET_NOT_HALTED;
1877  }
1878 
1879  /* sanitize arguments */
1880  if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1882 
1883  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1885 
1886  /* send memory write request (command 0x2n, n: access size) */
1887  retval = xscale_send_u32(target, 0x20 | size);
1888  if (retval != ERROR_OK)
1889  return retval;
1890 
1891  /* send base address for read request */
1892  retval = xscale_send_u32(target, address);
1893  if (retval != ERROR_OK)
1894  return retval;
1895 
1896  /* send number of requested data words to be written*/
1897  retval = xscale_send_u32(target, count);
1898  if (retval != ERROR_OK)
1899  return retval;
1900 
1901  /* extract data from host-endian buffer into byte stream */
1902 #if 0
1903  for (i = 0; i < count; i++) {
1904  switch (size) {
1905  case 4:
1907  xscale_send_u32(target, value);
1908  buffer += 4;
1909  break;
1910  case 2:
1912  xscale_send_u32(target, value);
1913  buffer += 2;
1914  break;
1915  case 1:
1916  value = *buffer;
1917  xscale_send_u32(target, value);
1918  buffer += 1;
1919  break;
1920  default:
1921  LOG_ERROR("should never get here");
1922  exit(-1);
1923  }
1924  }
1925 #endif
1926  retval = xscale_send(target, buffer, count, size);
1927  if (retval != ERROR_OK)
1928  return retval;
1929 
1930  /* examine DCSR, to see if Sticky Abort (SA) got set */
1931  retval = xscale_read_dcsr(target);
1932  if (retval != ERROR_OK)
1933  return retval;
1934  if (buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 5, 1) == 1) {
1935  /* clear SA bit */
1936  retval = xscale_send_u32(target, 0x60);
1937  if (retval != ERROR_OK)
1938  return retval;
1939 
1940  LOG_ERROR("data abort writing memory");
1941  return ERROR_TARGET_DATA_ABORT;
1942  }
1943 
1944  return ERROR_OK;
1945 }
1946 
1948  uint32_t size, uint32_t count, const uint8_t *buffer)
1949 {
1950  struct xscale_common *xscale = target_to_xscale(target);
1951 
1952  /* with MMU inactive, there are only physical addresses */
1953  if (!xscale->armv4_5_mmu.mmu_enabled)
1955 
1957  LOG_ERROR("%s: %s is not implemented. Disable MMU?",
1958  target_name(target), __func__);
1959  return ERROR_FAIL;
1960 }
1961 
1962 static int xscale_get_ttb(struct target *target, uint32_t *result)
1963 {
1964  struct xscale_common *xscale = target_to_xscale(target);
1965  uint32_t ttb;
1966  int retval;
1967 
1968  retval = xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_TTB]);
1969  if (retval != ERROR_OK)
1970  return retval;
1971  ttb = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_TTB].value, 0, 32);
1972 
1973  *result = ttb;
1974 
1975  return ERROR_OK;
1976 }
1977 
1978 static int xscale_disable_mmu_caches(struct target *target, int mmu,
1979  int d_u_cache, int i_cache)
1980 {
1981  struct xscale_common *xscale = target_to_xscale(target);
1982  uint32_t cp15_control;
1983  int retval;
1984 
1985  /* read cp15 control register */
1986  retval = xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_CTRL]);
1987  if (retval != ERROR_OK)
1988  return retval;
1989  cp15_control = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CTRL].value, 0, 32);
1990 
1991  if (mmu)
1992  cp15_control &= ~0x1U;
1993 
1994  if (d_u_cache) {
1995  /* clean DCache */
1996  retval = xscale_send_u32(target, 0x50);
1997  if (retval != ERROR_OK)
1998  return retval;
1999  retval = xscale_send_u32(target, xscale->cache_clean_address);
2000  if (retval != ERROR_OK)
2001  return retval;
2002 
2003  /* invalidate DCache */
2004  retval = xscale_send_u32(target, 0x51);
2005  if (retval != ERROR_OK)
2006  return retval;
2007 
2008  cp15_control &= ~0x4U;
2009  }
2010 
2011  if (i_cache) {
2012  /* invalidate ICache */
2013  retval = xscale_send_u32(target, 0x52);
2014  if (retval != ERROR_OK)
2015  return retval;
2016  cp15_control &= ~0x1000U;
2017  }
2018 
2019  /* write new cp15 control register */
2020  retval = xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_CTRL], cp15_control);
2021  if (retval != ERROR_OK)
2022  return retval;
2023 
2024  /* execute cpwait to ensure outstanding operations complete */
2025  return xscale_send_u32(target, 0x53);
2026 }
2027 
2028 static int xscale_enable_mmu_caches(struct target *target, int mmu,
2029  int d_u_cache, int i_cache)
2030 {
2031  struct xscale_common *xscale = target_to_xscale(target);
2032  uint32_t cp15_control;
2033  int retval;
2034 
2035  /* read cp15 control register */
2036  retval = xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_CTRL]);
2037  if (retval != ERROR_OK)
2038  return retval;
2039  cp15_control = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CTRL].value, 0, 32);
2040 
2041  if (mmu)
2042  cp15_control |= 0x1U;
2043 
2044  if (d_u_cache)
2045  cp15_control |= 0x4U;
2046 
2047  if (i_cache)
2048  cp15_control |= 0x1000U;
2049 
2050  /* write new cp15 control register */
2051  retval = xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_CTRL], cp15_control);
2052  if (retval != ERROR_OK)
2053  return retval;
2054 
2055  /* execute cpwait to ensure outstanding operations complete */
2056  return xscale_send_u32(target, 0x53);
2057 }
2058 
2060  struct breakpoint *breakpoint)
2061 {
2062  int retval;
2063  struct xscale_common *xscale = target_to_xscale(target);
2064 
2065  if (target->state != TARGET_HALTED) {
2066  LOG_TARGET_ERROR(target, "not halted");
2067  return ERROR_TARGET_NOT_HALTED;
2068  }
2069 
2070  if (breakpoint->is_set) {
2071  LOG_WARNING("breakpoint already set");
2072  return ERROR_OK;
2073  }
2074 
2075  if (breakpoint->type == BKPT_HARD) {
2076  uint32_t value = breakpoint->address | 1;
2077  if (!xscale->ibcr0_used) {
2079  xscale->ibcr0_used = 1;
2080  /* breakpoint set on first breakpoint register */
2082  } else if (!xscale->ibcr1_used) {
2084  xscale->ibcr1_used = 1;
2085  /* breakpoint set on second breakpoint register */
2087  } else {/* bug: availability previously verified in xscale_add_breakpoint() */
2088  LOG_ERROR("BUG: no hardware comparator available");
2090  }
2091  } else if (breakpoint->type == BKPT_SOFT) {
2092  if (breakpoint->length == 4) {
2093  /* keep the original instruction in target endianness */
2094  retval = target_read_memory(target, breakpoint->address, 4, 1,
2096  if (retval != ERROR_OK)
2097  return retval;
2098  /* write the bkpt instruction in target endianness
2099  *(arm7_9->arm_bkpt is host endian) */
2101  xscale->arm_bkpt);
2102  if (retval != ERROR_OK)
2103  return retval;
2104  } else {
2105  /* keep the original instruction in target endianness */
2106  retval = target_read_memory(target, breakpoint->address, 2, 1,
2108  if (retval != ERROR_OK)
2109  return retval;
2110  /* write the bkpt instruction in target endianness
2111  *(arm7_9->arm_bkpt is host endian) */
2113  xscale->thumb_bkpt);
2114  if (retval != ERROR_OK)
2115  return retval;
2116  }
2117  breakpoint->is_set = true;
2118 
2119  xscale_send_u32(target, 0x50); /* clean dcache */
2121  xscale_send_u32(target, 0x51); /* invalidate dcache */
2122  xscale_send_u32(target, 0x52); /* invalidate icache and flush fetch buffers */
2123  }
2124 
2125  return ERROR_OK;
2126 }
2127 
2129  struct breakpoint *breakpoint)
2130 {
2131  struct xscale_common *xscale = target_to_xscale(target);
2132 
2133  if ((breakpoint->type == BKPT_HARD) && (xscale->ibcr_available < 1)) {
2134  LOG_ERROR("no breakpoint unit available for hardware breakpoint");
2136  }
2137 
2138  if ((breakpoint->length != 2) && (breakpoint->length != 4)) {
2139  LOG_ERROR("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
2141  }
2142 
2143  if (breakpoint->type == BKPT_HARD)
2144  xscale->ibcr_available--;
2145 
2147 }
2148 
2150  struct breakpoint *breakpoint)
2151 {
2152  int retval;
2153  struct xscale_common *xscale = target_to_xscale(target);
2154 
2155  if (target->state != TARGET_HALTED) {
2156  LOG_TARGET_ERROR(target, "not halted");
2157  return ERROR_TARGET_NOT_HALTED;
2158  }
2159 
2160  if (!breakpoint->is_set) {
2161  LOG_WARNING("breakpoint not set");
2162  return ERROR_OK;
2163  }
2164 
2165  if (breakpoint->type == BKPT_HARD) {
2166  if (breakpoint->number == 0) {
2168  xscale->ibcr0_used = 0;
2169  } else if (breakpoint->number == 1) {
2171  xscale->ibcr1_used = 0;
2172  }
2173  breakpoint->is_set = false;
2174  } else {
2175  /* restore original instruction (kept in target endianness) */
2176  if (breakpoint->length == 4) {
2177  retval = target_write_memory(target, breakpoint->address, 4, 1,
2179  if (retval != ERROR_OK)
2180  return retval;
2181  } else {
2182  retval = target_write_memory(target, breakpoint->address, 2, 1,
2184  if (retval != ERROR_OK)
2185  return retval;
2186  }
2187  breakpoint->is_set = false;
2188 
2189  xscale_send_u32(target, 0x50); /* clean dcache */
2191  xscale_send_u32(target, 0x51); /* invalidate dcache */
2192  xscale_send_u32(target, 0x52); /* invalidate icache and flush fetch buffers */
2193  }
2194 
2195  return ERROR_OK;
2196 }
2197 
2199 {
2200  struct xscale_common *xscale = target_to_xscale(target);
2201 
2202  if (target->state != TARGET_HALTED) {
2203  LOG_TARGET_ERROR(target, "not halted");
2204  return ERROR_TARGET_NOT_HALTED;
2205  }
2206 
2207  if (breakpoint->is_set)
2209 
2210  if (breakpoint->type == BKPT_HARD)
2211  xscale->ibcr_available++;
2212 
2213  return ERROR_OK;
2214 }
2215 
2217  struct watchpoint *watchpoint)
2218 {
2219  struct xscale_common *xscale = target_to_xscale(target);
2220  uint32_t enable = 0;
2221  struct reg *dbcon = &xscale->reg_cache->reg_list[XSCALE_DBCON];
2222  uint32_t dbcon_value = buf_get_u32(dbcon->value, 0, 32);
2223 
2224  if (target->state != TARGET_HALTED) {
2225  LOG_TARGET_ERROR(target, "not halted");
2226  return ERROR_TARGET_NOT_HALTED;
2227  }
2228 
2229  switch (watchpoint->rw) {
2230  case WPT_READ:
2231  enable = 0x3;
2232  break;
2233  case WPT_ACCESS:
2234  enable = 0x2;
2235  break;
2236  case WPT_WRITE:
2237  enable = 0x1;
2238  break;
2239  default:
2240  LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
2241  }
2242 
2243  /* For watchpoint across more than one word, both DBR registers must
2244  be enlisted, with the second used as a mask. */
2245  if (watchpoint->length > 4) {
2246  if (xscale->dbr0_used || xscale->dbr1_used) {
2247  LOG_ERROR("BUG: sufficient hardware comparators unavailable");
2249  }
2250 
2251  /* Write mask value to DBR1, based on the length argument.
2252  * Address bits ignored by the comparator are those set in mask. */
2254  watchpoint->length - 1);
2255  xscale->dbr1_used = 1;
2256  enable |= 0x100; /* DBCON[M] */
2257  }
2258 
2259  if (!xscale->dbr0_used) {
2261  dbcon_value |= enable;
2262  xscale_set_reg_u32(dbcon, dbcon_value);
2264  xscale->dbr0_used = 1;
2265  } else if (!xscale->dbr1_used) {
2267  dbcon_value |= enable << 2;
2268  xscale_set_reg_u32(dbcon, dbcon_value);
2270  xscale->dbr1_used = 1;
2271  } else {
2272  LOG_ERROR("BUG: no hardware comparator available");
2274  }
2275 
2276  return ERROR_OK;
2277 }
2278 
2280  struct watchpoint *watchpoint)
2281 {
2282  struct xscale_common *xscale = target_to_xscale(target);
2283 
2284  if (xscale->dbr_available < 1) {
2285  LOG_ERROR("no more watchpoint registers available");
2287  }
2288 
2290  LOG_WARNING("xscale does not support value, mask arguments; ignoring");
2291 
2292  /* check that length is a power of two */
2293  for (uint32_t len = watchpoint->length; len != 1; len /= 2) {
2294  if (len % 2) {
2295  LOG_ERROR("xscale requires that watchpoint length is a power of two");
2297  }
2298  }
2299 
2300  if (watchpoint->length == 4) { /* single word watchpoint */
2301  xscale->dbr_available--;/* one DBR reg used */
2302  return ERROR_OK;
2303  }
2304 
2305  /* watchpoints across multiple words require both DBR registers */
2306  if (xscale->dbr_available < 2) {
2307  LOG_ERROR("insufficient watchpoint registers available");
2309  }
2310 
2311  if (watchpoint->length > watchpoint->address) {
2312  LOG_ERROR("xscale does not support watchpoints with length "
2313  "greater than address");
2315  }
2316 
2317  xscale->dbr_available = 0;
2318  return ERROR_OK;
2319 }
2320 
2322  struct watchpoint *watchpoint)
2323 {
2324  struct xscale_common *xscale = target_to_xscale(target);
2325  struct reg *dbcon = &xscale->reg_cache->reg_list[XSCALE_DBCON];
2326  uint32_t dbcon_value = buf_get_u32(dbcon->value, 0, 32);
2327 
2328  if (target->state != TARGET_HALTED) {
2329  LOG_TARGET_ERROR(target, "not halted");
2330  return ERROR_TARGET_NOT_HALTED;
2331  }
2332 
2333  if (!watchpoint->is_set) {
2334  LOG_WARNING("breakpoint not set");
2335  return ERROR_OK;
2336  }
2337 
2338  if (watchpoint->number == 0) {
2339  if (watchpoint->length > 4) {
2340  dbcon_value &= ~0x103; /* clear DBCON[M] as well */
2341  xscale->dbr1_used = 0; /* DBR1 was used for mask */
2342  } else
2343  dbcon_value &= ~0x3;
2344 
2345  xscale_set_reg_u32(dbcon, dbcon_value);
2346  xscale->dbr0_used = 0;
2347  } else if (watchpoint->number == 1) {
2348  dbcon_value &= ~0xc;
2349  xscale_set_reg_u32(dbcon, dbcon_value);
2350  xscale->dbr1_used = 0;
2351  }
2352  watchpoint->is_set = false;
2353 
2354  return ERROR_OK;
2355 }
2356 
2358 {
2359  struct xscale_common *xscale = target_to_xscale(target);
2360 
2361  if (target->state != TARGET_HALTED) {
2362  LOG_TARGET_ERROR(target, "not halted");
2363  return ERROR_TARGET_NOT_HALTED;
2364  }
2365 
2366  if (watchpoint->is_set)
2368 
2369  if (watchpoint->length > 4)
2370  xscale->dbr_available++;/* both DBR regs now available */
2371 
2372  xscale->dbr_available++;
2373 
2374  return ERROR_OK;
2375 }
2376 
2377 static int xscale_get_reg(struct reg *reg)
2378 {
2379  struct xscale_reg *arch_info = reg->arch_info;
2380  struct target *target = arch_info->target;
2381  struct xscale_common *xscale = target_to_xscale(target);
2382 
2383  /* DCSR, TX and RX are accessible via JTAG */
2384  if (strcmp(reg->name, "XSCALE_DCSR") == 0)
2385  return xscale_read_dcsr(arch_info->target);
2386  else if (strcmp(reg->name, "XSCALE_TX") == 0) {
2387  /* 1 = consume register content */
2388  return xscale_read_tx(arch_info->target, 1);
2389  } else if (strcmp(reg->name, "XSCALE_RX") == 0) {
2390  /* can't read from RX register (host -> debug handler) */
2391  return ERROR_OK;
2392  } else if (strcmp(reg->name, "XSCALE_TXRXCTRL") == 0) {
2393  /* can't (explicitly) read from TXRXCTRL register */
2394  return ERROR_OK;
2395  } else {/* Other DBG registers have to be transferred by the debug handler
2396  * send CP read request (command 0x40) */
2397  xscale_send_u32(target, 0x40);
2398 
2399  /* send CP register number */
2401 
2402  /* read register value */
2403  xscale_read_tx(target, 1);
2404  buf_cpy(xscale->reg_cache->reg_list[XSCALE_TX].value, reg->value, 32);
2405 
2406  reg->dirty = false;
2407  reg->valid = true;
2408  }
2409 
2410  return ERROR_OK;
2411 }
2412 
2413 static int xscale_set_reg(struct reg *reg, uint8_t *buf)
2414 {
2415  struct xscale_reg *arch_info = reg->arch_info;
2416  struct target *target = arch_info->target;
2417  struct xscale_common *xscale = target_to_xscale(target);
2418  uint32_t value = buf_get_u32(buf, 0, 32);
2419 
2420  /* DCSR, TX and RX are accessible via JTAG */
2421  if (strcmp(reg->name, "XSCALE_DCSR") == 0) {
2422  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 0, 32, value);
2423  return xscale_write_dcsr(arch_info->target, -1, -1);
2424  } else if (strcmp(reg->name, "XSCALE_RX") == 0) {
2425  buf_set_u32(xscale->reg_cache->reg_list[XSCALE_RX].value, 0, 32, value);
2426  return xscale_write_rx(arch_info->target);
2427  } else if (strcmp(reg->name, "XSCALE_TX") == 0) {
2428  /* can't write to TX register (debug-handler -> host) */
2429  return ERROR_OK;
2430  } else if (strcmp(reg->name, "XSCALE_TXRXCTRL") == 0) {
2431  /* can't (explicitly) write to TXRXCTRL register */
2432  return ERROR_OK;
2433  } else {/* Other DBG registers have to be transferred by the debug handler
2434  * send CP write request (command 0x41) */
2435  xscale_send_u32(target, 0x41);
2436 
2437  /* send CP register number */
2439 
2440  /* send CP register value */
2441  xscale_send_u32(target, value);
2442  buf_set_u32(reg->value, 0, 32, value);
2443  }
2444 
2445  return ERROR_OK;
2446 }
2447 
2448 static int xscale_write_dcsr_sw(struct target *target, uint32_t value)
2449 {
2450  struct xscale_common *xscale = target_to_xscale(target);
2451  struct reg *dcsr = &xscale->reg_cache->reg_list[XSCALE_DCSR];
2452  struct xscale_reg *dcsr_arch_info = dcsr->arch_info;
2453 
2454  /* send CP write request (command 0x41) */
2455  xscale_send_u32(target, 0x41);
2456 
2457  /* send CP register number */
2458  xscale_send_u32(target, dcsr_arch_info->dbg_handler_number);
2459 
2460  /* send CP register value */
2461  xscale_send_u32(target, value);
2462  buf_set_u32(dcsr->value, 0, 32, value);
2463 
2464  return ERROR_OK;
2465 }
2466 
2467 static int xscale_read_trace(struct target *target)
2468 {
2469  struct xscale_common *xscale = target_to_xscale(target);
2470  struct arm *arm = &xscale->arm;
2471  struct xscale_trace_data **trace_data_p;
2472 
2473  /* 258 words from debug handler
2474  * 256 trace buffer entries
2475  * 2 checkpoint addresses
2476  */
2477  uint32_t trace_buffer[258];
2478  int is_address[256];
2479  int i, j;
2480  unsigned int num_checkpoints = 0;
2481 
2482  if (target->state != TARGET_HALTED) {
2483  LOG_TARGET_ERROR(target, "must be stopped to read trace data");
2484  return ERROR_TARGET_NOT_HALTED;
2485  }
2486 
2487  /* send read trace buffer command (command 0x61) */
2488  xscale_send_u32(target, 0x61);
2489 
2490  /* receive trace buffer content */
2491  xscale_receive(target, trace_buffer, 258);
2492 
2493  /* parse buffer backwards to identify address entries */
2494  for (i = 255; i >= 0; i--) {
2495  /* also count number of checkpointed entries */
2496  if ((trace_buffer[i] & 0xe0) == 0xc0)
2497  num_checkpoints++;
2498 
2499  is_address[i] = 0;
2500  if (((trace_buffer[i] & 0xf0) == 0x90) ||
2501  ((trace_buffer[i] & 0xf0) == 0xd0)) {
2502  if (i > 0)
2503  is_address[--i] = 1;
2504  if (i > 0)
2505  is_address[--i] = 1;
2506  if (i > 0)
2507  is_address[--i] = 1;
2508  if (i > 0)
2509  is_address[--i] = 1;
2510  }
2511  }
2512 
2513 
2514  /* search first non-zero entry that is not part of an address */
2515  for (j = 0; (j < 256) && (trace_buffer[j] == 0) && (!is_address[j]); j++)
2516  ;
2517 
2518  if (j == 256) {
2519  LOG_DEBUG("no trace data collected");
2521  }
2522 
2523  /* account for possible partial address at buffer start (wrap mode only) */
2524  if (is_address[0]) { /* first entry is address; complete set of 4? */
2525  i = 1;
2526  while (i < 4)
2527  if (!is_address[i++])
2528  break;
2529  if (i < 4)
2530  j += i; /* partial address; can't use it */
2531  }
2532 
2533  /* if first valid entry is indirect branch, can't use that either (no address) */
2534  if (((trace_buffer[j] & 0xf0) == 0x90) || ((trace_buffer[j] & 0xf0) == 0xd0))
2535  j++;
2536 
2537  /* walk linked list to terminating entry */
2538  for (trace_data_p = &xscale->trace.data; *trace_data_p;
2539  trace_data_p = &(*trace_data_p)->next)
2540  ;
2541 
2542  *trace_data_p = malloc(sizeof(struct xscale_trace_data));
2543  (*trace_data_p)->next = NULL;
2544  (*trace_data_p)->chkpt0 = trace_buffer[256];
2545  (*trace_data_p)->chkpt1 = trace_buffer[257];
2546  (*trace_data_p)->last_instruction = buf_get_u32(arm->pc->value, 0, 32);
2547  (*trace_data_p)->entries = malloc(sizeof(struct xscale_trace_entry) * (256 - j));
2548  (*trace_data_p)->depth = 256 - j;
2549  (*trace_data_p)->num_checkpoints = num_checkpoints;
2550 
2551  for (i = j; i < 256; i++) {
2552  (*trace_data_p)->entries[i - j].data = trace_buffer[i];
2553  if (is_address[i])
2554  (*trace_data_p)->entries[i - j].type = XSCALE_TRACE_ADDRESS;
2555  else
2556  (*trace_data_p)->entries[i - j].type = XSCALE_TRACE_MESSAGE;
2557  }
2558 
2559  return ERROR_OK;
2560 }
2561 
2562 static int xscale_read_instruction(struct target *target, uint32_t pc,
2563  struct arm_instruction *instruction)
2564 {
2565  struct xscale_common *const xscale = target_to_xscale(target);
2566  int section = -1;
2567  size_t size_read;
2568  uint32_t opcode;
2569  int retval;
2570 
2571  if (!xscale->trace.image)
2573 
2574  /* search for the section the current instruction belongs to */
2575  for (unsigned int i = 0; i < xscale->trace.image->num_sections; i++) {
2576  if ((xscale->trace.image->sections[i].base_address <= pc) &&
2577  (xscale->trace.image->sections[i].base_address +
2578  xscale->trace.image->sections[i].size > pc)) {
2579  section = i;
2580  break;
2581  }
2582  }
2583 
2584  if (section == -1) {
2585  /* current instruction couldn't be found in the image */
2587  }
2588 
2589  if (xscale->trace.core_state == ARM_STATE_ARM) {
2590  uint8_t buf[4];
2591  retval = image_read_section(xscale->trace.image, section,
2592  pc - xscale->trace.image->sections[section].base_address,
2593  4, buf, &size_read);
2594  if (retval != ERROR_OK) {
2595  LOG_ERROR("error while reading instruction");
2597  }
2598  opcode = target_buffer_get_u32(target, buf);
2599  arm_evaluate_opcode(opcode, pc, instruction);
2600  } else if (xscale->trace.core_state == ARM_STATE_THUMB) {
2601  uint8_t buf[2];
2602  retval = image_read_section(xscale->trace.image, section,
2603  pc - xscale->trace.image->sections[section].base_address,
2604  2, buf, &size_read);
2605  if (retval != ERROR_OK) {
2606  LOG_ERROR("error while reading instruction");
2608  }
2609  opcode = target_buffer_get_u16(target, buf);
2610  thumb_evaluate_opcode(opcode, pc, instruction);
2611  } else {
2612  LOG_ERROR("BUG: unknown core state encountered");
2613  exit(-1);
2614  }
2615 
2616  return ERROR_OK;
2617 }
2618 
2619 /* Extract address encoded into trace data.
2620  * Write result to address referenced by argument 'target', or 0 if incomplete. */
2621 static inline void xscale_branch_address(struct xscale_trace_data *trace_data,
2622  int i, uint32_t *target)
2623 {
2624  /* if there are less than four entries prior to the indirect branch message
2625  * we can't extract the address */
2626  if (i < 4)
2627  *target = 0;
2628  else {
2629  *target = (trace_data->entries[i-1].data) | (trace_data->entries[i-2].data << 8) |
2630  (trace_data->entries[i-3].data << 16) | (trace_data->entries[i-4].data << 24);
2631  }
2632 }
2633 
2634 static inline void xscale_display_instruction(struct target *target, uint32_t pc,
2635  struct arm_instruction *instruction,
2636  struct command_invocation *cmd)
2637 {
2638  int retval = xscale_read_instruction(target, pc, instruction);
2639  if (retval == ERROR_OK)
2640  command_print(cmd, "%s", instruction->text);
2641  else
2642  command_print(cmd, "0x%8.8" PRIx32 "\t<not found in image>", pc);
2643 }
2644 
2646 {
2647  struct xscale_common *xscale = target_to_xscale(target);
2648  struct xscale_trace_data *trace_data = xscale->trace.data;
2649  int i, retval;
2650  uint32_t breakpoint_pc = 0;
2651  struct arm_instruction instruction;
2652  uint32_t current_pc = 0;/* initialized when address determined */
2653 
2654  if (!xscale->trace.image)
2655  LOG_WARNING("No trace image loaded; use 'xscale trace_image'");
2656 
2657  /* loop for each trace buffer that was loaded from target */
2658  while (trace_data) {
2659  int chkpt = 0; /* incremented as checkpointed entries found */
2660  int j;
2661 
2662  /* FIXME: set this to correct mode when trace buffer is first enabled */
2663  xscale->trace.core_state = ARM_STATE_ARM;
2664 
2665  /* loop for each entry in this trace buffer */
2666  for (i = 0; i < trace_data->depth; i++) {
2667  int exception = 0;
2668  uint32_t chkpt_reg = 0x0;
2669  uint32_t branch_target = 0;
2670  int count;
2671 
2672  /* trace entry type is upper nybble of 'message byte' */
2673  int trace_msg_type = (trace_data->entries[i].data & 0xf0) >> 4;
2674 
2675  /* Target addresses of indirect branches are written into buffer
2676  * before the message byte representing the branch. Skip past it */
2677  if (trace_data->entries[i].type == XSCALE_TRACE_ADDRESS)
2678  continue;
2679 
2680  switch (trace_msg_type) {
2681  case 0: /* Exceptions */
2682  case 1:
2683  case 2:
2684  case 3:
2685  case 4:
2686  case 5:
2687  case 6:
2688  case 7:
2689  exception = (trace_data->entries[i].data & 0x70) >> 4;
2690 
2691  /* FIXME: vector table may be at ffff0000 */
2692  branch_target = (trace_data->entries[i].data & 0xf0) >> 2;
2693  break;
2694 
2695  case 8: /* Direct Branch */
2696  break;
2697 
2698  case 9: /* Indirect Branch */
2699  xscale_branch_address(trace_data, i, &branch_target);
2700  break;
2701 
2702  case 13: /* Checkpointed Indirect Branch */
2703  xscale_branch_address(trace_data, i, &branch_target);
2704  if (trace_data->num_checkpoints == 2 && chkpt == 0)
2705  chkpt_reg = trace_data->chkpt1; /* 2 chkpts, this is
2706  *oldest */
2707  else
2708  chkpt_reg = trace_data->chkpt0; /* 1 chkpt, or 2 and
2709  *newest */
2710 
2711  chkpt++;
2712  break;
2713 
2714  case 12: /* Checkpointed Direct Branch */
2715  if (trace_data->num_checkpoints == 2 && chkpt == 0)
2716  chkpt_reg = trace_data->chkpt1; /* 2 chkpts, this is
2717  *oldest */
2718  else
2719  chkpt_reg = trace_data->chkpt0; /* 1 chkpt, or 2 and
2720  *newest */
2721 
2722  /* if no current_pc, checkpoint will be starting point */
2723  if (current_pc == 0)
2724  branch_target = chkpt_reg;
2725 
2726  chkpt++;
2727  break;
2728 
2729  case 15:/* Roll-over */
2730  break;
2731 
2732  default:/* Reserved */
2733  LOG_WARNING("trace is suspect: invalid trace message byte");
2734  continue;
2735  }
2736 
2737  /* If we don't have the current_pc yet, but we did get the branch target
2738  * (either from the trace buffer on indirect branch, or from a checkpoint reg),
2739  * then we can start displaying instructions at the next iteration, with
2740  * branch_target as the starting point.
2741  */
2742  if (current_pc == 0) {
2743  current_pc = branch_target; /* remains 0 unless branch_target *obtained */
2744  continue;
2745  }
2746 
2747  /* We have current_pc. Read and display the instructions from the image.
2748  * First, display count instructions (lower nybble of message byte). */
2749  count = trace_data->entries[i].data & 0x0f;
2750  for (j = 0; j < count; j++) {
2751  xscale_display_instruction(target, current_pc, &instruction,
2752  cmd);
2753  current_pc += xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2;
2754  }
2755 
2756  /* An additional instruction is implicitly added to count for
2757  * rollover and some exceptions: undef, swi, prefetch abort. */
2758  if ((trace_msg_type == 15) || (exception > 0 && exception < 4)) {
2759  xscale_display_instruction(target, current_pc, &instruction,
2760  cmd);
2761  current_pc += xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2;
2762  }
2763 
2764  if (trace_msg_type == 15) /* rollover */
2765  continue;
2766 
2767  if (exception) {
2768  command_print(cmd, "--- exception %i ---", exception);
2769  continue;
2770  }
2771 
2772  /* not exception or rollover; next instruction is a branch and is
2773  * not included in the count */
2774  xscale_display_instruction(target, current_pc, &instruction, cmd);
2775 
2776  /* for direct branches, extract branch destination from instruction */
2777  if ((trace_msg_type == 8) || (trace_msg_type == 12)) {
2778  retval = xscale_read_instruction(target, current_pc, &instruction);
2779  if (retval == ERROR_OK)
2780  current_pc = instruction.info.b_bl_bx_blx.target_address;
2781  else
2782  current_pc = 0; /* branch destination unknown */
2783 
2784  /* direct branch w/ checkpoint; can also get from checkpoint reg */
2785  if (trace_msg_type == 12) {
2786  if (current_pc == 0)
2787  current_pc = chkpt_reg;
2788  else if (current_pc != chkpt_reg) /* sanity check */
2789  LOG_WARNING("trace is suspect: checkpoint register "
2790  "inconsistent with address from image");
2791  }
2792 
2793  if (current_pc == 0)
2794  command_print(cmd, "address unknown");
2795 
2796  continue;
2797  }
2798 
2799  /* indirect branch; the branch destination was read from trace buffer */
2800  if ((trace_msg_type == 9) || (trace_msg_type == 13)) {
2801  current_pc = branch_target;
2802 
2803  /* sanity check (checkpoint reg is redundant) */
2804  if ((trace_msg_type == 13) && (chkpt_reg != branch_target))
2805  LOG_WARNING("trace is suspect: checkpoint register "
2806  "inconsistent with address from trace buffer");
2807  }
2808 
2809  } /* END: for (i = 0; i < trace_data->depth; i++) */
2810 
2811  breakpoint_pc = trace_data->last_instruction; /* used below */
2812  trace_data = trace_data->next;
2813 
2814  } /* END: while (trace_data) */
2815 
2816  /* Finally... display all instructions up to the value of the pc when the
2817  * debug break occurred (saved when trace data was collected from target).
2818  * This is necessary because the trace only records execution branches and 16
2819  * consecutive instructions (rollovers), so last few typically missed.
2820  */
2821  if (current_pc == 0)
2822  return ERROR_OK;/* current_pc was never found */
2823 
2824  /* how many instructions remaining? */
2825  int gap_count = (breakpoint_pc - current_pc) /
2826  (xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2);
2827 
2828  /* should never be negative or over 16, but verify */
2829  if (gap_count < 0 || gap_count > 16) {
2830  LOG_WARNING("trace is suspect: excessive gap at end of trace");
2831  return ERROR_OK;/* bail; large number or negative value no good */
2832  }
2833 
2834  /* display remaining instructions */
2835  for (i = 0; i < gap_count; i++) {
2836  xscale_display_instruction(target, current_pc, &instruction, cmd);
2837  current_pc += xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2;
2838  }
2839 
2840  return ERROR_OK;
2841 }
2842 
2843 static const struct reg_arch_type xscale_reg_type = {
2844  .get = xscale_get_reg,
2845  .set = xscale_set_reg,
2846 };
2847 
2849 {
2850  struct xscale_common *xscale = target_to_xscale(target);
2851  struct arm *arm = &xscale->arm;
2852  struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
2853  struct xscale_reg *arch_info = malloc(sizeof(xscale_reg_arch_info));
2854  int i;
2855  int num_regs = ARRAY_SIZE(xscale_reg_arch_info);
2856 
2857  (*cache_p) = arm_build_reg_cache(target, arm);
2858 
2859  (*cache_p)->next = malloc(sizeof(struct reg_cache));
2860  cache_p = &(*cache_p)->next;
2861 
2862  /* fill in values for the xscale reg cache */
2863  (*cache_p)->name = "XScale registers";
2864  (*cache_p)->next = NULL;
2865  (*cache_p)->reg_list = calloc(num_regs, sizeof(struct reg));
2866  (*cache_p)->num_regs = num_regs;
2867 
2868  for (i = 0; i < num_regs; i++) {
2869  (*cache_p)->reg_list[i].name = xscale_reg_list[i];
2870  (*cache_p)->reg_list[i].value = calloc(4, 1);
2871  (*cache_p)->reg_list[i].dirty = false;
2872  (*cache_p)->reg_list[i].valid = false;
2873  (*cache_p)->reg_list[i].size = 32;
2874  (*cache_p)->reg_list[i].arch_info = &arch_info[i];
2875  (*cache_p)->reg_list[i].type = &xscale_reg_type;
2876  (*cache_p)->reg_list[i].exist = true;
2877  arch_info[i] = xscale_reg_arch_info[i];
2878  arch_info[i].target = target;
2879  }
2880 
2881  xscale->reg_cache = (*cache_p);
2882 }
2883 
2885 {
2886  struct xscale_common *xscale = target_to_xscale(target);
2887  struct reg_cache *cache = xscale->reg_cache;
2888 
2889  for (unsigned int i = 0; i < ARRAY_SIZE(xscale_reg_arch_info); i++)
2890  free(cache->reg_list[i].value);
2891 
2892  free(cache->reg_list[0].arch_info);
2893  free(cache->reg_list);
2894  free(cache);
2895 
2896  arm_free_reg_cache(&xscale->arm);
2897 }
2898 
2899 static int xscale_init_target(struct command_context *cmd_ctx,
2900  struct target *target)
2901 {
2903  return ERROR_OK;
2904 }
2905 
2906 static void xscale_deinit_target(struct target *target)
2907 {
2908  struct xscale_common *xscale = target_to_xscale(target);
2909 
2911  free(xscale);
2912 }
2913 
2915  struct xscale_common *xscale, struct jtag_tap *tap)
2916 {
2917  struct arm *arm;
2918  uint32_t high_reset_branch, low_reset_branch;
2919  int i;
2920 
2921  arm = &xscale->arm;
2922 
2923  /* store architecture specific data */
2925 
2926  /* PXA3xx with 11 bit IR shifts the JTAG instructions */
2927  if (tap->ir_length == 11)
2928  xscale->xscale_variant = XSCALE_PXA3XX;
2929  else
2931 
2932  /* the debug handler isn't installed (and thus not running) at this time */
2933  xscale->handler_address = 0xfe000800;
2934 
2935  /* clear the vectors we keep locally for reference */
2936  memset(xscale->low_vectors, 0, sizeof(xscale->low_vectors));
2937  memset(xscale->high_vectors, 0, sizeof(xscale->high_vectors));
2938 
2939  /* no user-specified vectors have been configured yet */
2940  xscale->static_low_vectors_set = 0x0;
2941  xscale->static_high_vectors_set = 0x0;
2942 
2943  /* calculate branches to debug handler */
2944  low_reset_branch = (xscale->handler_address + 0x20 - 0x0 - 0x8) >> 2;
2945  high_reset_branch = (xscale->handler_address + 0x20 - 0xffff0000 - 0x8) >> 2;
2946 
2947  xscale->low_vectors[0] = ARMV4_5_B((low_reset_branch & 0xffffff), 0);
2948  xscale->high_vectors[0] = ARMV4_5_B((high_reset_branch & 0xffffff), 0);
2949 
2950  for (i = 1; i <= 7; i++) {
2951  xscale->low_vectors[i] = ARMV4_5_B(0xfffffe, 0);
2952  xscale->high_vectors[i] = ARMV4_5_B(0xfffffe, 0);
2953  }
2954 
2955  /* 64kB aligned region used for DCache cleaning */
2956  xscale->cache_clean_address = 0xfffe0000;
2957 
2958  xscale->hold_rst = 0;
2959  xscale->external_debug_break = 0;
2960 
2961  xscale->ibcr_available = 2;
2962  xscale->ibcr0_used = 0;
2963  xscale->ibcr1_used = 0;
2964 
2965  xscale->dbr_available = 2;
2966  xscale->dbr0_used = 0;
2967  xscale->dbr1_used = 0;
2968 
2969  LOG_INFO("%s: hardware has 2 breakpoints and 2 watchpoints",
2970  target_name(target));
2971 
2972  xscale->arm_bkpt = ARMV5_BKPT(0x0);
2973  xscale->thumb_bkpt = ARMV5_T_BKPT(0x0) & 0xffff;
2974 
2975  xscale->vector_catch = 0x1;
2976 
2977  xscale->trace.data = NULL;
2978  xscale->trace.image = NULL;
2979  xscale->trace.mode = XSCALE_TRACE_DISABLED;
2980  xscale->trace.buffer_fill = 0;
2981  xscale->trace.fill_counter = 0;
2982 
2983  /* prepare ARMv4/5 specific information */
2984  arm->arch_info = xscale;
2989 
2991 
2992  xscale->armv4_5_mmu.armv4_5_cache.ctype = -1;
2998  xscale->armv4_5_mmu.has_tiny_pages = 1;
2999  xscale->armv4_5_mmu.mmu_enabled = false;
3000 
3001  return ERROR_OK;
3002 }
3003 
3005 {
3006  struct xscale_common *xscale;
3007 
3008  if (sizeof(xscale_debug_handler) > 0x800) {
3009  LOG_ERROR("debug_handler.bin: larger than 2kb");
3010  return ERROR_FAIL;
3011  }
3012 
3013  xscale = calloc(1, sizeof(*xscale));
3014  if (!xscale)
3015  return ERROR_FAIL;
3016 
3017  return xscale_init_arch_info(target, xscale, target->tap);
3018 }
3019 
3020 COMMAND_HANDLER(xscale_handle_debug_handler_command)
3021 {
3022  struct target *target = NULL;
3023  struct xscale_common *xscale;
3024  int retval;
3025  uint32_t handler_address;
3026 
3027  if (CMD_ARGC < 2)
3029 
3030  target = get_target(CMD_ARGV[0]);
3031  if (!target) {
3032  LOG_ERROR("target '%s' not defined", CMD_ARGV[0]);
3033  return ERROR_FAIL;
3034  }
3035 
3036  xscale = target_to_xscale(target);
3037  retval = xscale_verify_pointer(CMD, xscale);
3038  if (retval != ERROR_OK)
3039  return retval;
3040 
3042 
3043  if (((handler_address >= 0x800) && (handler_address <= 0x1fef800)) ||
3044  ((handler_address >= 0xfe000800) && (handler_address <= 0xfffff800)))
3045  xscale->handler_address = handler_address;
3046  else {
3047  LOG_ERROR(
3048  "xscale debug_handler <address> must be between 0x800 and 0x1fef800 or between 0xfe000800 and 0xfffff800");
3049  return ERROR_FAIL;
3050  }
3051 
3052  return ERROR_OK;
3053 }
3054 
3055 COMMAND_HANDLER(xscale_handle_cache_clean_address_command)
3056 {
3057  struct target *target = NULL;
3058  struct xscale_common *xscale;
3059  int retval;
3060  uint32_t cache_clean_address;
3061 
3062  if (CMD_ARGC < 2)
3064 
3065  target = get_target(CMD_ARGV[0]);
3066  if (!target) {
3067  LOG_ERROR("target '%s' not defined", CMD_ARGV[0]);
3068  return ERROR_FAIL;
3069  }
3070  xscale = target_to_xscale(target);
3071  retval = xscale_verify_pointer(CMD, xscale);
3072  if (retval != ERROR_OK)
3073  return retval;
3074 
3076 
3077  if (cache_clean_address & 0xffff)
3078  LOG_ERROR("xscale cache_clean_address <address> must be 64kb aligned");
3079  else
3081 
3082  return ERROR_OK;
3083 }
3084 
3085 COMMAND_HANDLER(xscale_handle_cache_info_command)
3086 {
3088  struct xscale_common *xscale = target_to_xscale(target);
3089  int retval;
3090 
3091  retval = xscale_verify_pointer(CMD, xscale);
3092  if (retval != ERROR_OK)
3093  return retval;
3094 
3096 }
3097 
3098 static int xscale_virt2phys(struct target *target,
3099  target_addr_t virtual, target_addr_t *physical)
3100 {
3101  struct xscale_common *xscale = target_to_xscale(target);
3102  uint32_t cb;
3103 
3104  if (xscale->common_magic != XSCALE_COMMON_MAGIC) {
3106  return ERROR_TARGET_INVALID;
3107  }
3108 
3109  uint32_t ret;
3110  int retval = armv4_5_mmu_translate_va(target, &xscale->armv4_5_mmu,
3111  virtual, &cb, &ret);
3112  if (retval != ERROR_OK)
3113  return retval;
3114  *physical = ret;
3115  return ERROR_OK;
3116 }
3117 
3118 static int xscale_mmu(struct target *target, bool *enabled)
3119 {
3120  struct xscale_common *xscale = target_to_xscale(target);
3121 
3122  if (target->state != TARGET_HALTED) {
3123  LOG_TARGET_ERROR(target, "not halted");
3124  return ERROR_TARGET_NOT_HALTED;
3125  }
3126  *enabled = xscale->armv4_5_mmu.mmu_enabled;
3127  return ERROR_OK;
3128 }
3129 
3130 COMMAND_HANDLER(xscale_handle_mmu_command)
3131 {
3133  struct xscale_common *xscale = target_to_xscale(target);
3134  int retval;
3135 
3136  retval = xscale_verify_pointer(CMD, xscale);
3137  if (retval != ERROR_OK)
3138  return retval;
3139 
3140  if (target->state != TARGET_HALTED) {
3141  command_print(CMD, "Error: target must be stopped for \"%s\" command", CMD_NAME);
3142  return ERROR_TARGET_NOT_HALTED;
3143  }
3144 
3145  if (CMD_ARGC >= 1) {
3146  bool enable;
3147  COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
3148  if (enable)
3149  xscale_enable_mmu_caches(target, 1, 0, 0);
3150  else
3152  xscale->armv4_5_mmu.mmu_enabled = enable;
3153  }
3154 
3155  command_print(CMD, "mmu %s",
3156  (xscale->armv4_5_mmu.mmu_enabled) ? "enabled" : "disabled");
3157 
3158  return ERROR_OK;
3159 }
3160 
3161 COMMAND_HANDLER(xscale_handle_idcache_command)
3162 {
3164  struct xscale_common *xscale = target_to_xscale(target);
3165 
3166  int retval = xscale_verify_pointer(CMD, xscale);
3167  if (retval != ERROR_OK)
3168  return retval;
3169 
3170  if (target->state != TARGET_HALTED) {
3171  command_print(CMD, "Error: target must be stopped for \"%s\" command", CMD_NAME);
3172  return ERROR_TARGET_NOT_HALTED;
3173  }
3174 
3175  bool icache = false;
3176  if (strcmp(CMD_NAME, "icache") == 0)
3177  icache = true;
3178  if (CMD_ARGC >= 1) {
3179  bool enable;
3180  COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
3181  if (icache) {
3182  xscale->armv4_5_mmu.armv4_5_cache.i_cache_enabled = enable;
3183  if (enable)
3184  xscale_enable_mmu_caches(target, 0, 0, 1);
3185  else
3187  } else {
3188  xscale->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = enable;
3189  if (enable)
3190  xscale_enable_mmu_caches(target, 0, 1, 0);
3191  else
3193  }
3194  }
3195 
3196  bool enabled = icache ?
3199  const char *msg = enabled ? "enabled" : "disabled";
3200  command_print(CMD, "%s %s", CMD_NAME, msg);
3201 
3202  return ERROR_OK;
3203 }
3204 
3205 static const struct {
3206  char name[15];
3207  unsigned int mask;
3208 } vec_ids[] = {
3209  { "fiq", DCSR_TF, },
3210  { "irq", DCSR_TI, },
3211  { "dabt", DCSR_TD, },
3212  { "pabt", DCSR_TA, },
3213  { "swi", DCSR_TS, },
3214  { "undef", DCSR_TU, },
3215  { "reset", DCSR_TR, },
3216 };
3217 
3218 COMMAND_HANDLER(xscale_handle_vector_catch_command)
3219 {
3221  struct xscale_common *xscale = target_to_xscale(target);
3222  int retval;
3223  uint32_t dcsr_value;
3224  uint32_t catch = 0;
3225  struct reg *dcsr_reg = &xscale->reg_cache->reg_list[XSCALE_DCSR];
3226 
3227  retval = xscale_verify_pointer(CMD, xscale);
3228  if (retval != ERROR_OK)
3229  return retval;
3230 
3231  if (CMD_ARGC > 0) {
3232  if (CMD_ARGC == 1) {
3233  if (strcmp(CMD_ARGV[0], "all") == 0) {
3234  catch = DCSR_TRAP_MASK;
3235  CMD_ARGC--;
3236  } else if (strcmp(CMD_ARGV[0], "none") == 0) {
3237  catch = 0;
3238  CMD_ARGC--;
3239  }
3240  }
3241  while (CMD_ARGC-- > 0) {
3242  unsigned int i;
3243  for (i = 0; i < ARRAY_SIZE(vec_ids); i++) {
3244  if (strcmp(CMD_ARGV[CMD_ARGC], vec_ids[i].name))
3245  continue;
3246  catch |= vec_ids[i].mask;
3247  break;
3248  }
3249  if (i == ARRAY_SIZE(vec_ids)) {
3250  LOG_ERROR("No vector '%s'", CMD_ARGV[CMD_ARGC]);
3252  }
3253  }
3254  buf_set_u32(dcsr_reg->value, 0, 32,
3255  (buf_get_u32(dcsr_reg->value, 0, 32) & ~DCSR_TRAP_MASK) | catch);
3256  xscale_write_dcsr(target, -1, -1);
3257  }
3258 
3259  dcsr_value = buf_get_u32(dcsr_reg->value, 0, 32);
3260  for (unsigned int i = 0; i < ARRAY_SIZE(vec_ids); i++) {
3261  command_print(CMD, "%15s: %s", vec_ids[i].name,
3262  (dcsr_value & vec_ids[i].mask) ? "catch" : "ignore");
3263  }
3264 
3265  return ERROR_OK;
3266 }
3267 
3268 
3269 COMMAND_HANDLER(xscale_handle_vector_table_command)
3270 {
3272  struct xscale_common *xscale = target_to_xscale(target);
3273  int err = 0;
3274  int retval;
3275 
3276  retval = xscale_verify_pointer(CMD, xscale);
3277  if (retval != ERROR_OK)
3278  return retval;
3279 
3280  if (CMD_ARGC == 0) { /* print current settings */
3281  int idx;
3282 
3283  command_print(CMD, "active user-set static vectors:");
3284  for (idx = 1; idx < 8; idx++)
3285  if (xscale->static_low_vectors_set & (1 << idx))
3287  "low %d: 0x%" PRIx32,
3288  idx,
3289  xscale->static_low_vectors[idx]);
3290  for (idx = 1; idx < 8; idx++)
3291  if (xscale->static_high_vectors_set & (1 << idx))
3293  "high %d: 0x%" PRIx32,
3294  idx,
3295  xscale->static_high_vectors[idx]);
3296  return ERROR_OK;
3297  }
3298 
3299  if (CMD_ARGC != 3)
3300  err = 1;
3301  else {
3302  int idx;
3303  COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], idx);
3304  uint32_t vec;
3305  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], vec);
3306 
3307  if (idx < 1 || idx >= 8)
3308  err = 1;
3309 
3310  if (!err && strcmp(CMD_ARGV[0], "low") == 0) {
3311  xscale->static_low_vectors_set |= (1<<idx);
3312  xscale->static_low_vectors[idx] = vec;
3313  } else if (!err && (strcmp(CMD_ARGV[0], "high") == 0)) {
3314  xscale->static_high_vectors_set |= (1<<idx);
3315  xscale->static_high_vectors[idx] = vec;
3316  } else
3317  err = 1;
3318  }
3319 
3320  if (err)
3322 
3323  return ERROR_OK;
3324 }
3325 
3326 
3327 COMMAND_HANDLER(xscale_handle_trace_buffer_command)
3328 {
3330  struct xscale_common *xscale = target_to_xscale(target);
3331  uint32_t dcsr_value;
3332  int retval;
3333 
3334  retval = xscale_verify_pointer(CMD, xscale);
3335  if (retval != ERROR_OK)
3336  return retval;
3337 
3338  if (target->state != TARGET_HALTED) {
3339  command_print(CMD, "Error: target must be stopped for \"%s\" command", CMD_NAME);
3340  return ERROR_TARGET_NOT_HALTED;
3341  }
3342 
3343  if (CMD_ARGC >= 1) {
3344  if (strcmp("enable", CMD_ARGV[0]) == 0)
3345  xscale->trace.mode = XSCALE_TRACE_WRAP; /* default */
3346  else if (strcmp("disable", CMD_ARGV[0]) == 0)
3347  xscale->trace.mode = XSCALE_TRACE_DISABLED;
3348  else
3350  }
3351 
3352  if (CMD_ARGC >= 2 && xscale->trace.mode != XSCALE_TRACE_DISABLED) {
3353  if (strcmp("fill", CMD_ARGV[1]) == 0) {
3354  int buffcount = 1; /* default */
3355  if (CMD_ARGC >= 3)
3356  COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], buffcount);
3357  if (buffcount < 1) { /* invalid */
3358  command_print(CMD, "fill buffer count must be > 0");
3359  xscale->trace.mode = XSCALE_TRACE_DISABLED;
3361  }
3362  xscale->trace.buffer_fill = buffcount;
3363  xscale->trace.mode = XSCALE_TRACE_FILL;
3364  } else if (strcmp("wrap", CMD_ARGV[1]) == 0)
3365  xscale->trace.mode = XSCALE_TRACE_WRAP;
3366  else {
3367  xscale->trace.mode = XSCALE_TRACE_DISABLED;
3369  }
3370  }
3371 
3372  if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
3373  char fill_string[12];
3374  sprintf(fill_string, "fill %d", xscale->trace.buffer_fill);
3375  command_print(CMD, "trace buffer enabled (%s)",
3376  (xscale->trace.mode == XSCALE_TRACE_FILL)
3377  ? fill_string : "wrap");
3378  } else
3379  command_print(CMD, "trace buffer disabled");
3380 
3381  dcsr_value = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 0, 32);
3382  if (xscale->trace.mode == XSCALE_TRACE_FILL)
3383  xscale_write_dcsr_sw(target, (dcsr_value & 0xfffffffc) | 2);
3384  else
3385  xscale_write_dcsr_sw(target, dcsr_value & 0xfffffffc);
3386 
3387  return ERROR_OK;
3388 }
3389 
3390 COMMAND_HANDLER(xscale_handle_trace_image_command)
3391 {
3393  struct xscale_common *xscale = target_to_xscale(target);
3394  int retval;
3395 
3396  if (CMD_ARGC < 1)
3398 
3399  retval = xscale_verify_pointer(CMD, xscale);
3400  if (retval != ERROR_OK)
3401  return retval;
3402 
3403  if (xscale->trace.image) {
3404  image_close(xscale->trace.image);
3405  free(xscale->trace.image);
3406  command_print(CMD, "previously loaded image found and closed");
3407  }
3408 
3409  xscale->trace.image = malloc(sizeof(struct image));
3410  xscale->trace.image->base_address_set = false;
3411  xscale->trace.image->start_address_set = false;
3412 
3413  /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
3414  if (CMD_ARGC >= 2) {
3415  xscale->trace.image->base_address_set = true;
3416  COMMAND_PARSE_NUMBER(llong, CMD_ARGV[1], xscale->trace.image->base_address);
3417  } else
3418  xscale->trace.image->base_address_set = false;
3419 
3420  if (image_open(xscale->trace.image, CMD_ARGV[0],
3421  (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK) {
3422  free(xscale->trace.image);
3423  xscale->trace.image = NULL;
3424  return ERROR_OK;
3425  }
3426 
3427  return ERROR_OK;
3428 }
3429 
3430 COMMAND_HANDLER(xscale_handle_dump_trace_command)
3431 {
3433  struct xscale_common *xscale = target_to_xscale(target);
3434  struct xscale_trace_data *trace_data;
3435  struct fileio *file;
3436  int retval;
3437 
3438  retval = xscale_verify_pointer(CMD, xscale);
3439  if (retval != ERROR_OK)
3440  return retval;
3441 
3442  if (target->state != TARGET_HALTED) {
3443  command_print(CMD, "Error: target must be stopped for \"%s\" command", CMD_NAME);
3444  return ERROR_TARGET_NOT_HALTED;
3445  }
3446 
3447  if (CMD_ARGC < 1)
3449 
3450  trace_data = xscale->trace.data;
3451 
3452  if (!trace_data) {
3453  command_print(CMD, "no trace data collected");
3454  return ERROR_OK;
3455  }
3456 
3458  return ERROR_OK;
3459 
3460  while (trace_data) {
3461  int i;
3462 
3463  fileio_write_u32(file, trace_data->chkpt0);
3464  fileio_write_u32(file, trace_data->chkpt1);
3465  fileio_write_u32(file, trace_data->last_instruction);
3466  fileio_write_u32(file, trace_data->depth);
3467 
3468  for (i = 0; i < trace_data->depth; i++)
3469  fileio_write_u32(file, trace_data->entries[i].data |
3470  ((trace_data->entries[i].type & 0xffff) << 16));
3471 
3472  trace_data = trace_data->next;
3473  }
3474 
3475  fileio_close(file);
3476 
3477  return ERROR_OK;
3478 }
3479 
3480 COMMAND_HANDLER(xscale_handle_analyze_trace_buffer_command)
3481 {
3483  struct xscale_common *xscale = target_to_xscale(target);
3484  int retval;
3485 
3486  retval = xscale_verify_pointer(CMD, xscale);
3487  if (retval != ERROR_OK)
3488  return retval;
3489 
3491 
3492  return ERROR_OK;
3493 }
3494 
3495 COMMAND_HANDLER(xscale_handle_cp15)
3496 {
3498  struct xscale_common *xscale = target_to_xscale(target);
3499  int retval;
3500 
3501  retval = xscale_verify_pointer(CMD, xscale);
3502  if (retval != ERROR_OK)
3503  return retval;
3504 
3505  if (target->state != TARGET_HALTED) {
3506  command_print(CMD, "Error: target must be stopped for \"%s\" command", CMD_NAME);
3507  return ERROR_TARGET_NOT_HALTED;
3508  }
3509  uint32_t reg_no = 0;
3510  struct reg *reg = NULL;
3511  if (CMD_ARGC > 0) {
3512  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], reg_no);
3513  /*translate from xscale cp15 register no to openocd register*/
3514  switch (reg_no) {
3515  case 0:
3516  reg_no = XSCALE_MAINID;
3517  break;
3518  case 1:
3519  reg_no = XSCALE_CTRL;
3520  break;
3521  case 2:
3522  reg_no = XSCALE_TTB;
3523  break;
3524  case 3:
3525  reg_no = XSCALE_DAC;
3526  break;
3527  case 5:
3528  reg_no = XSCALE_FSR;
3529  break;
3530  case 6:
3531  reg_no = XSCALE_FAR;
3532  break;
3533  case 13:
3534  reg_no = XSCALE_PID;
3535  break;
3536  case 15:
3537  reg_no = XSCALE_CPACCESS;
3538  break;
3539  default:
3540  command_print(CMD, "invalid register number");
3542  }
3543  reg = &xscale->reg_cache->reg_list[reg_no];
3544 
3545  }
3546  if (CMD_ARGC == 1) {
3547  uint32_t value;
3548 
3549  /* read cp15 control register */
3551  value = buf_get_u32(reg->value, 0, 32);
3552  command_print(CMD, "%s (/%i): 0x%" PRIx32, reg->name, (int)(reg->size),
3553  value);
3554  } else if (CMD_ARGC == 2) {
3555  uint32_t value;
3557 
3558  /* send CP write request (command 0x41) */
3559  xscale_send_u32(target, 0x41);
3560 
3561  /* send CP register number */
3562  xscale_send_u32(target, reg_no);
3563 
3564  /* send CP register value */
3566 
3567  /* execute cpwait to ensure outstanding operations complete */
3568  xscale_send_u32(target, 0x53);
3569  } else
3571 
3572  return ERROR_OK;
3573 }
3574 
3575 static const struct command_registration xscale_exec_command_handlers[] = {
3576  {
3577  .name = "cache_info",
3578  .handler = xscale_handle_cache_info_command,
3579  .mode = COMMAND_EXEC,
3580  .help = "display information about CPU caches",
3581  .usage = "",
3582  },
3583  {
3584  .name = "mmu",
3585  .handler = xscale_handle_mmu_command,
3586  .mode = COMMAND_EXEC,
3587  .help = "enable or disable the MMU",
3588  .usage = "['enable'|'disable']",
3589  },
3590  {
3591  .name = "icache",
3592  .handler = xscale_handle_idcache_command,
3593  .mode = COMMAND_EXEC,
3594  .help = "display ICache state, optionally enabling or "
3595  "disabling it",
3596  .usage = "['enable'|'disable']",
3597  },
3598  {
3599  .name = "dcache",
3600  .handler = xscale_handle_idcache_command,
3601  .mode = COMMAND_EXEC,
3602  .help = "display DCache state, optionally enabling or "
3603  "disabling it",
3604  .usage = "['enable'|'disable']",
3605  },
3606  {
3607  .name = "vector_catch",
3608  .handler = xscale_handle_vector_catch_command,
3609  .mode = COMMAND_EXEC,
3610  .help = "set or display mask of vectors "
3611  "that should trigger debug entry",
3612  .usage = "['all'|'none'|'fiq'|'irq'|'dabt'|'pabt'|'swi'|'undef'|'reset']",
3613  },
3614  {
3615  .name = "vector_table",
3616  .handler = xscale_handle_vector_table_command,
3617  .mode = COMMAND_EXEC,
3618  .help = "set vector table entry in mini-ICache, "
3619  "or display current tables",
3620  .usage = "[('high'|'low') index code]",
3621  },
3622  {
3623  .name = "trace_buffer",
3624  .handler = xscale_handle_trace_buffer_command,
3625  .mode = COMMAND_EXEC,
3626  .help = "display trace buffer status, enable or disable "
3627  "tracing, and optionally reconfigure trace mode",
3628  .usage = "['enable'|'disable' ['fill' [number]|'wrap']]",
3629  },
3630  {
3631  .name = "dump_trace",
3632  .handler = xscale_handle_dump_trace_command,
3633  .mode = COMMAND_EXEC,
3634  .help = "dump content of trace buffer to file",
3635  .usage = "filename",
3636  },
3637  {
3638  .name = "analyze_trace",
3639  .handler = xscale_handle_analyze_trace_buffer_command,
3640  .mode = COMMAND_EXEC,
3641  .help = "analyze content of trace buffer",
3642  .usage = "",
3643  },
3644  {
3645  .name = "trace_image",
3646  .handler = xscale_handle_trace_image_command,
3647  .mode = COMMAND_EXEC,
3648  .help = "load image from file to address (default 0)",
3649  .usage = "filename [offset [filetype]]",
3650  },
3651  {
3652  .name = "cp15",
3653  .handler = xscale_handle_cp15,
3654  .mode = COMMAND_EXEC,
3655  .help = "Read or write coprocessor 15 register.",
3656  .usage = "register [value]",
3657  },
3659 };
3660 static const struct command_registration xscale_any_command_handlers[] = {
3661  {
3662  .name = "debug_handler",
3663  .handler = xscale_handle_debug_handler_command,
3664  .mode = COMMAND_ANY,
3665  .help = "Change address used for debug handler.",
3666  .usage = "<target> <address>",
3667  },
3668  {
3669  .name = "cache_clean_address",
3670  .handler = xscale_handle_cache_clean_address_command,
3671  .mode = COMMAND_ANY,
3672  .help = "Change address used for cleaning data cache.",
3673  .usage = "address",
3674  },
3675  {
3677  },
3679 };
3680 static const struct command_registration xscale_command_handlers[] = {
3681  {
3683  },
3684  {
3685  .name = "xscale",
3686  .mode = COMMAND_ANY,
3687  .help = "xscale command group",
3688  .usage = "",
3689  .chain = xscale_any_command_handlers,
3690  },
3692 };
3693 
3694 struct target_type xscale_target = {
3695  .name = "xscale",
3696 
3697  .poll = xscale_poll,
3698  .arch_state = xscale_arch_state,
3699 
3700  .halt = xscale_halt,
3701  .resume = xscale_resume,
3702  .step = xscale_step,
3703 
3704  .assert_reset = xscale_assert_reset,
3705  .deassert_reset = xscale_deassert_reset,
3706 
3707  /* REVISIT on some cores, allow exporting iwmmxt registers ... */
3708  .get_gdb_arch = arm_get_gdb_arch,
3709  .get_gdb_reg_list = arm_get_gdb_reg_list,
3710 
3711  .read_memory = xscale_read_memory,
3712  .read_phys_memory = xscale_read_phys_memory,
3713  .write_memory = xscale_write_memory,
3714  .write_phys_memory = xscale_write_phys_memory,
3715 
3716  .checksum_memory = arm_checksum_memory,
3717  .blank_check_memory = arm_blank_check_memory,
3718 
3719  .run_algorithm = armv4_5_run_algorithm,
3720 
3721  .add_breakpoint = xscale_add_breakpoint,
3722  .remove_breakpoint = xscale_remove_breakpoint,
3723  .add_watchpoint = xscale_add_watchpoint,
3724  .remove_watchpoint = xscale_remove_watchpoint,
3725 
3726  .commands = xscale_command_handlers,
3727  .target_create = xscale_target_create,
3728  .init_target = xscale_init_target,
3729  .deinit_target = xscale_deinit_target,
3730 
3731  .virt2phys = xscale_virt2phys,
3732  .mmu = xscale_mmu,
3733 
3734  .insn_set = armv4_5_insn_set,
3735 };
struct reg * arm_reg_current(struct arm *arm, unsigned int regnum)
Returns handle to the register currently mapped to a given number.
Definition: armv4_5.c:516
int arm_arch_state(struct target *target)
Definition: armv4_5.c:797
bool is_arm_mode(unsigned int psr_mode)
Return true iff the parameter denotes a valid ARM processor mode.
Definition: armv4_5.c:181
int armv4_5_insn_set(struct command_invocation *cmd, struct target *target, const char **insn_set)
Definition: armv4_5.c:1781
#define ARM_COMMON_MAGIC
Definition: arm.h:167
int arm_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Runs ARM code in the target to calculate a CRC32 checksum.
Definition: armv4_5.c:1551
struct reg_cache * arm_build_reg_cache(struct target *target, struct arm *arm)
Definition: armv4_5.c:660
int arm_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
Runs ARM code in the target to check whether a memory block holds all ones.
Definition: armv4_5.c:1625
const char * arm_get_gdb_arch(const struct target *target)
Definition: armv4_5.c:1220
int arm_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: armv4_5.c:1225
arm_mode
Represent state of an ARM core.
Definition: arm.h:82
@ ARM_MODE_SYS
Definition: arm.h:92
@ ARM_MODE_USR
Definition: arm.h:83
void arm_free_reg_cache(struct arm *arm)
Definition: armv4_5.c:776
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_THUMB
Definition: arm.h:153
@ ARM_STATE_ARM
Definition: arm.h:152
const struct command_registration arm_command_handlers[]
Definition: armv4_5.c:1200
int arm_init_arch_info(struct target *target, struct arm *arm)
Definition: armv4_5.c:1752
const char * arm_mode_name(unsigned int psr_mode)
Map PSR mode bits to the name of an ARM processor operating mode.
Definition: armv4_5.c:170
void arm_set_cpsr(struct arm *arm, uint32_t cpsr)
Configures host-side ARM records to reflect the specified CPSR.
Definition: armv4_5.c:452
int armv4_5_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Definition: armv4_5.c:1525
@ ARM_CORE_TYPE_STD
Definition: arm.h:46
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)
Macros used to generate various ARM or Thumb opcodes.
#define ARMV5_BKPT(im)
Definition: arm_opcodes.h:227
#define ARMV5_T_BKPT(im)
Definition: arm_opcodes.h:313
#define ARMV4_5_B(im, l)
Definition: arm_opcodes.h:117
int arm_simulate_step(struct target *target, uint32_t *dry_run_pc)
enum arm_mode mode
Definition: armv4_5.c:280
enum arm_mode armv4_5_number_to_mode(int number)
Map linear number indexing armv4_5_core_reg_map to PSR mode bits.
Definition: armv4_5.c:222
#define ARMV4_5_CORE_REG_MODE(cache, mode, num)
Definition: armv4_5.h:32
int armv4_5_identify_cache(uint32_t cache_type_reg, struct armv4_5_cache_common *cache)
Definition: armv4_5_cache.c:15
int armv4_5_handle_cache_info_command(struct command_invocation *cmd, struct armv4_5_cache_common *armv4_5_cache)
Definition: armv4_5_cache.c:68
int armv4_5_mmu_translate_va(struct target *target, struct armv4_5_mmu_common *armv4_5_mmu, uint32_t va, uint32_t *cb, uint32_t *val)
Definition: armv4_5_mmu.c:16
void * buf_cpy(const void *from, void *_to, unsigned int size)
Copies size bits out of from and into to.
Definition: binarybuffer.c:43
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
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:472
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
static void watchpoint_set(struct watchpoint *watchpoint, unsigned int number)
Definition: breakpoints.h:81
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:65
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:171
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:536
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
int fileio_write_u32(struct fileio *fileio, uint32_t data)
int fileio_close(struct fileio *fileio)
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
@ FILEIO_WRITE
Definition: helper/fileio.h:29
@ FILEIO_BINARY
Definition: helper/fileio.h:23
void image_close(struct image *image)
Definition: image.c:1210
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
Definition: image.c:1078
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:956
void jtag_add_dr_scan_check(struct jtag_tap *active, int in_num_fields, struct scan_field *in_fields, enum tap_state state)
A version of jtag_add_dr_scan() that uses the check_value/mask fields.
Definition: jtag/core.c:440
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
A reset of the TAP state machine can be requested.
Definition: jtag/core.c:770
void jtag_add_runtest(unsigned int num_cycles, enum tap_state state)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:597
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1049
void jtag_add_pathmove(unsigned int num_states, const enum tap_state *path)
Application code must assume that interfaces will implement transitions between states with different...
Definition: jtag/core.c:518
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, enum tap_state state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:452
void jtag_add_sleep(uint32_t us)
Definition: jtag/core.c:882
void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
Execute jtag queue and check value with an optional mask.
Definition: jtag/core.c:929
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, enum tap_state state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:375
void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
A simpler version of jtag_add_callback4().
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_DRCAPTURE
Definition: jtag.h:47
@ TAP_DRSELECT
Definition: jtag.h:48
@ TAP_DREXIT1
Definition: jtag.h:42
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_DREXIT2
Definition: jtag.h:41
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
intptr_t jtag_callback_data_t
Defines the type of data passed to the jtag_callback_t interface.
Definition: jtag.h:336
void alive_sleep(uint64_t ms)
Definition: log.c:478
void keep_alive(void)
Definition: log.c:437
#define LOG_USER(expr ...)
Definition: log.h:150
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_DEBUG
Definition: log.h:55
#define t0
Definition: mips32.c:192
#define t1
Definition: mips32.c:193
#define t2
Definition: mips32.c:194
struct reg_cache ** register_get_last_cache_p(struct reg_cache **first)
Definition: register.c:72
void register_cache_invalidate(struct reg_cache *cache)
Marks the contents of the register cache as invalid (and clean).
Definition: register.c:94
struct target * target
Definition: rtt/rtt.c:26
static const char * str_enabled_disabled(bool value)
union arm_instruction::@73 info
struct arm_b_bl_bx_blx_instr b_bl_bx_blx
Represents a generic ARM core, with standard application registers.
Definition: arm.h:176
int(* full_context)(struct target *target)
Retrieve all core registers, for display.
Definition: arm.h:222
void * arch_info
Definition: arm.h:252
enum arm_core_type core_type
Indicates what registers are in the ARM state core register set.
Definition: arm.h:194
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 * pc
Handle to the PC; valid in all core modes.
Definition: arm.h:182
int(* write_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode, uint8_t *value)
Definition: arm.h:227
int(* read_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode)
Retrieve a single core register.
Definition: arm.h:225
struct reg_cache * core_cache
Definition: arm.h:179
struct reg * spsr
Handle to the SPSR; valid only in core modes with an SPSR.
Definition: arm.h:188
unsigned int common_magic
Definition: arm.h:177
int(* write_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: armv4_5_mmu.h:18
int(* read_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: armv4_5_mmu.h:17
int(* get_ttb)(struct target *target, uint32_t *result)
Definition: armv4_5_mmu.h:16
int(* enable_mmu_caches)(struct target *target, int mmu, int d_u_cache, int i_cache)
Definition: armv4_5_mmu.h:21
int(* disable_mmu_caches)(struct target *target, int mmu, int d_u_cache, int i_cache)
Definition: armv4_5_mmu.h:20
struct armv4_5_cache_common armv4_5_cache
Definition: armv4_5_mmu.h:22
struct breakpoint * next
Definition: breakpoints.h:34
unsigned int length
Definition: breakpoints.h:29
uint8_t * orig_instr
Definition: breakpoints.h:33
enum breakpoint_type type
Definition: breakpoints.h:30
bool is_set
Definition: breakpoints.h:31
unsigned int number
Definition: breakpoints.h:32
target_addr_t address
Definition: breakpoints.h:27
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:239
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
Definition: command.h:252
FILE * file
Definition: helper/fileio.c:28
Definition: image.h:48
unsigned int num_sections
Definition: image.h:51
bool start_address_set
Definition: image.h:55
struct imagesection * sections
Definition: image.h:52
long long base_address
Definition: image.h:54
bool base_address_set
Definition: image.h:53
target_addr_t base_address
Definition: image.h:42
uint32_t size
Definition: image.h:43
Definition: jtag.h:101
uint8_t * cur_instr
current instruction
Definition: jtag.h:132
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
int(* get)(struct reg *reg)
Definition: register.h:152
const char * name
Definition: register.h:145
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_cache * next
Definition: register.h:146
Definition: register.h:111
bool valid
Definition: register.h:126
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
void * arch_info
Definition: register.h:140
bool dirty
Definition: register.h:124
const char * name
Definition: register.h:113
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
uint8_t * check_value
The value used to check the data scanned out.
Definition: jtag.h:96
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
uint8_t * check_mask
The mask to go with check_value.
Definition: jtag.h:98
This holds methods shared between all instances of a given target type.
Definition: target_type.h:27
const char * name
Name of this type of target.
Definition: target_type.h:32
Definition: target.h:119
struct jtag_tap * tap
Definition: target.h:122
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
enum target_endianness endianness
Definition: target.h:165
struct reg_cache * reg_cache
Definition: target.h:168
struct breakpoint * breakpoints
Definition: target.h:169
struct watchpoint * watchpoints
Definition: target.h:170
void * arch_info
Definition: target.h:174
bool reset_halt
Definition: target.h:154
uint64_t mask
Definition: breakpoints.h:44
enum watchpoint_rw rw
Definition: breakpoints.h:46
bool is_set
Definition: breakpoints.h:47
struct watchpoint * next
Definition: breakpoints.h:49
unsigned int length
Definition: breakpoints.h:43
unsigned int number
Definition: breakpoints.h:48
target_addr_t address
Definition: breakpoints.h:42
uint8_t static_low_vectors_set
Definition: xscale.h:90
uint16_t thumb_bkpt
Definition: xscale.h:110
uint32_t static_high_vectors[8]
Definition: xscale.h:93
uint32_t low_vectors[8]
Definition: xscale.h:86
int arch_debug_reason
Definition: xscale.h:116
int xscale_variant
Definition: xscale.h:125
uint32_t static_low_vectors[8]
Definition: xscale.h:92
uint32_t cache_clean_address
Definition: xscale.h:96
uint32_t cp15_control_reg
Definition: xscale.h:120
int ibcr_available
Definition: xscale.h:106
int dbr0_used
Definition: xscale.h:104
uint8_t static_high_vectors_set
Definition: xscale.h:91
int external_debug_break
Definition: xscale.h:100
int dbr1_used
Definition: xscale.h:105
uint32_t handler_address
Definition: xscale.h:83
int ibcr1_used
Definition: xscale.h:108
int dbr_available
Definition: xscale.h:103
uint32_t high_vectors[8]
Definition: xscale.h:87
int ibcr0_used
Definition: xscale.h:107
uint32_t arm_bkpt
Definition: xscale.h:109
uint8_t vector_catch
Definition: xscale.h:112
int hold_rst
Definition: xscale.h:99
struct reg_cache * reg_cache
Definition: xscale.h:80
struct armv4_5_mmu_common armv4_5_mmu
Definition: xscale.h:119
struct arm arm
Definition: xscale.h:77
unsigned int common_magic
Definition: xscale.h:74
struct xscale_trace trace
Definition: xscale.h:114
int dbg_handler_number
Definition: xscale.h:135
struct target * target
Definition: xscale.h:136
unsigned int num_checkpoints
Definition: xscale.h:54
struct xscale_trace_data * next
Definition: xscale.h:55
uint32_t chkpt0
Definition: xscale.h:51
uint32_t chkpt1
Definition: xscale.h:52
struct xscale_trace_entry * entries
Definition: xscale.h:49
uint32_t last_instruction
Definition: xscale.h:53
Definition: xscale.h:43
uint8_t data
Definition: xscale.h:44
enum xscale_trace_entry_type type
Definition: xscale.h:45
enum trace_mode mode
Definition: xscale.h:69
struct xscale_trace_data * data
Definition: xscale.h:66
int buffer_fill
Definition: xscale.h:67
enum arm_state core_state
Definition: xscale.h:70
struct image * image
Definition: xscale.h:65
int fill_counter
Definition: xscale.h:68
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1816
struct target * get_target(const char *id)
Definition: target.c:444
void target_free_all_working_areas(struct target *target)
Definition: target.c:2202
int target_halt(struct target *target)
Definition: target.c:518
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:381
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:363
int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2688
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1289
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2671
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:271
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2597
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:345
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1261
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:469
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:327
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:817
static bool target_was_examined(const struct target *target)
Definition: target.h:443
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:819
#define ERROR_TARGET_INVALID
Definition: target.h:814
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:285
@ TARGET_EVENT_HALTED
Definition: target.h:265
@ TARGET_EVENT_RESUMED
Definition: target.h:266
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:284
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:246
target_state
Definition: target.h:55
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:824
@ TARGET_LITTLE_ENDIAN
Definition: target.h:85
#define ERROR_TARGET_TIMEOUT
Definition: target.h:816
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:821
#define ERROR_TARGET_DATA_ABORT
Definition: target.h:820
#define ERROR_TARGET_FAILURE
Definition: target.h:818
int64_t timeval_ms(void)
#define ERROR_TRACE_INSTRUCTION_UNAVAILABLE
Definition: trace.h:48
#define ERROR_TRACE_IMAGE_UNAVAILABLE
Definition: trace.h:47
static uint16_t le_to_h_u16(const uint8_t *buf)
Definition: types.h:122
#define TARGET_ADDR_FMT
Definition: types.h:286
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
uint64_t target_addr_t
Definition: types.h:279
static uint16_t be_to_h_u16(const uint8_t *buf)
Definition: types.h:149
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t count[4]
Definition: vdebug.c:22
static int xscale_enable_single_step(struct target *target, uint32_t next_pc)
Definition: xscale.c:1027
static int xscale_target_create(struct target *target)
Definition: xscale.c:3004
static int xscale_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: xscale.c:3098
static int xscale_init_arch_info(struct target *target, struct xscale_common *xscale, struct jtag_tap *tap)
Definition: xscale.c:2914
static int xscale_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: xscale.c:2128
static int xscale_assert_reset(struct target *target)
Definition: xscale.c:1424
static int xscale_jtag_set_instr(struct jtag_tap *tap, uint32_t new_instr, enum tap_state end_state)
Definition: xscale.c:141
static int xscale_set_breakpoint(struct target *, struct breakpoint *)
Definition: xscale.c:2059
static int xscale_enable_mmu_caches(struct target *target, int mmu, int d_u_cache, int i_cache)
Definition: xscale.c:2028
static int xscale_load_ic(struct target *target, uint32_t va, uint32_t buffer[8])
Definition: xscale.c:630
static void xscale_display_instruction(struct target *target, uint32_t pc, struct arm_instruction *instruction, struct command_invocation *cmd)
Definition: xscale.c:2634
static int xscale_write_dcsr(struct target *target, int hold_rst, int ext_dbg_brk)
Definition: xscale.c:561
static int xscale_full_context(struct target *target)
Definition: xscale.c:1616
static void xscale_deinit_target(struct target *target)
Definition: xscale.c:2906
static int xscale_analyze_trace(struct target *target, struct command_invocation *cmd)
Definition: xscale.c:2645
static int xscale_set_watchpoint(struct target *, struct watchpoint *)
Definition: xscale.c:2216
static int xscale_verify_pointer(struct command_invocation *cmd, struct xscale_common *xscale)
Definition: xscale.c:131
static int xscale_read_tx(struct target *target, int consume)
Definition: xscale.c:331
static int xscale_arch_state(struct target *target)
Definition: xscale.c:768
static int xscale_set_reg_u32(struct reg *reg, uint32_t value)
Definition: xscale.c:120
static int xscale_deassert_reset(struct target *target)
Definition: xscale.c:1472
static int xscale_read_trace(struct target *)
Definition: xscale.c:2467
COMMAND_HANDLER(xscale_handle_debug_handler_command)
Definition: xscale.c:3020
static int xscale_mmu(struct target *target, bool *enabled)
Definition: xscale.c:3118
static int xscale_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: xscale.c:2198
static int xscale_read_dcsr(struct target *target)
Definition: xscale.c:160
static const uint8_t xscale_debug_handler[]
Definition: xscale.c:65
static int xscale_set_reg(struct reg *reg, uint8_t *buf)
Definition: xscale.c:2413
static int xscale_get_ttb(struct target *target, uint32_t *result)
Definition: xscale.c:1962
static int xscale_read_core_reg(struct target *target, struct reg *r, int num, enum arm_mode mode)
Definition: xscale.c:1600
static int xscale_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: xscale.c:1367
static unsigned int parity(unsigned int v)
Definition: xscale.c:619
static int xscale_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: xscale.c:1863
static const struct @136 vec_ids[]
static const struct command_registration xscale_any_command_handlers[]
Definition: xscale.c:3660
char name[15]
Definition: xscale.c:3206
static int xscale_disable_mmu_caches(struct target *target, int mmu, int d_u_cache, int i_cache)
Definition: xscale.c:1978
static int xscale_get_reg(struct reg *reg)
Definition: xscale.c:2377
static const struct reg_arch_type xscale_reg_type
Definition: xscale.c:2843
static const struct xscale_reg xscale_reg_arch_info[]
Definition: xscale.c:94
static void xscale_enable_breakpoints(struct target *target)
Definition: xscale.c:1077
static const struct command_registration xscale_command_handlers[]
Definition: xscale.c:3680
static int xscale_write_rx(struct target *target)
Definition: xscale.c:415
static void xscale_free_trace_data(struct xscale_common *xscale)
Definition: xscale.c:1089
static int xscale_write_dcsr_sw(struct target *target, uint32_t value)
Definition: xscale.c:2448
static void xscale_getbuf(jtag_callback_data_t arg)
Definition: xscale.c:223
static void xscale_free_reg_cache(struct target *target)
Definition: xscale.c:2884
static int xscale_receive(struct target *target, uint32_t *buffer, int num_words)
Definition: xscale.c:229
static int xscale_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: xscale.c:2321
unsigned int mask
Definition: xscale.c:3207
static int xscale_poll(struct target *target)
Definition: xscale.c:792
static void xscale_enable_watchpoints(struct target *target)
Definition: xscale.c:1066
static void xscale_build_reg_cache(struct target *target)
Definition: xscale.c:2848
static int xscale_write_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: xscale.c:1947
static const char xscale_not[]
Definition: xscale.c:129
static int xscale_send_u32(struct target *target, uint32_t value)
Definition: xscale.c:553
static int xscale_restore_banked(struct target *)
Definition: xscale.c:1693
static int xscale_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: xscale.c:2899
static const struct command_registration xscale_exec_command_handlers[]
Definition: xscale.c:3575
static int xscale_resume(struct target *, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: xscale.c:1101
static int xscale_disable_single_step(struct target *target)
Definition: xscale.c:1053
static int xscale_invalidate_ic_line(struct target *target, uint32_t va)
Definition: xscale.c:684
static int xscale_read_instruction(struct target *target, uint32_t pc, struct arm_instruction *instruction)
Definition: xscale.c:2562
static int xscale_update_vectors(struct target *target)
Definition: xscale.c:714
static int xscale_debug_entry(struct target *)
Definition: xscale.c:829
static int xscale_write_core_reg(struct target *target, struct reg *r, int num, enum arm_mode mode, uint8_t *value)
Definition: xscale.c:1608
static int xscale_read_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: xscale.c:1848
static int xscale_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: xscale.c:2279
static int xscale_send(struct target *target, const uint8_t *buffer, int count, int size)
Definition: xscale.c:491
static int xscale_unset_breakpoint(struct target *, struct breakpoint *)
Definition: xscale.c:2149
static int xscale_step_inner(struct target *target, bool current, uint32_t address, bool handle_breakpoints)
Definition: xscale.c:1272
static void xscale_branch_address(struct xscale_trace_data *trace_data, int i, uint32_t *target)
Definition: xscale.c:2621
static const char *const xscale_reg_list[]
Definition: xscale.c:69
static int xscale_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: xscale.c:1762
static int xscale_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: xscale.c:2357
static int xscale_halt(struct target *target)
Definition: xscale.c:1000
struct target_type xscale_target
Definition: xscale.c:3694
#define XSCALE_SELDCSR
Definition: xscale.h:26
#define DCSR_TRAP_MASK
Definition: xscale.h:174
#define DCSR_TD
Definition: xscale.h:171
@ XSCALE_DBG_REASON_RESET
Definition: xscale.h:34
@ XSCALE_DBG_REASON_TB_FULL
Definition: xscale.h:35
@ XSCALE_DBG_REASON_GENERIC
Definition: xscale.h:33
#define DCSR_TS
Definition: xscale.h:169
#define XSCALE_PXA3XX
Definition: xscale.h:30
#define DCSR_TF
Definition: xscale.h:173
#define DCSR_TA
Definition: xscale.h:170
#define XSCALE_LDIC
Definition: xscale.h:25
#define XSCALE_IXP4XX_PXA2XX
Definition: xscale.h:29
#define XSCALE_DBGRX
Definition: xscale.h:23
@ XSCALE_TRACE_MESSAGE
Definition: xscale.h:39
@ XSCALE_TRACE_ADDRESS
Definition: xscale.h:40
#define ERROR_XSCALE_NO_TRACE_DATA
Definition: xscale.h:164
#define DCSR_TR
Definition: xscale.h:167
#define DCSR_TI
Definition: xscale.h:172
static struct xscale_common * target_to_xscale(struct target *target)
Definition: xscale.h:129
#define XSCALE_COMMON_MAGIC
Definition: xscale.h:18
#define DCSR_TU
Definition: xscale.h:168
#define XSCALE_DBGTX
Definition: xscale.h:24
trace_mode
Definition: xscale.h:58
@ XSCALE_TRACE_DISABLED
Definition: xscale.h:59
@ XSCALE_TRACE_FILL
Definition: xscale.h:60
@ XSCALE_TRACE_WRAP
Definition: xscale.h:61
@ XSCALE_TX
Definition: xscale.h:159
@ XSCALE_FSR
Definition: xscale.h:146
@ XSCALE_CHKPT1
Definition: xscale.h:157
@ XSCALE_IBCR1
Definition: xscale.h:151
@ XSCALE_FAR
Definition: xscale.h:147
@ XSCALE_DBCON
Definition: xscale.h:154
@ XSCALE_DAC
Definition: xscale.h:145
@ XSCALE_CTRL
Definition: xscale.h:142
@ XSCALE_TBREG
Definition: xscale.h:155
@ XSCALE_RX
Definition: xscale.h:160
@ XSCALE_DBR1
Definition: xscale.h:153
@ XSCALE_CHKPT0
Definition: xscale.h:156
@ XSCALE_AUXCTRL
Definition: xscale.h:143
@ XSCALE_CPACCESS
Definition: xscale.h:149
@ XSCALE_MAINID
Definition: xscale.h:140
@ XSCALE_CACHETYPE
Definition: xscale.h:141
@ XSCALE_DBR0
Definition: xscale.h:152
@ XSCALE_TTB
Definition: xscale.h:144
@ XSCALE_IBCR0
Definition: xscale.h:150
@ XSCALE_PID
Definition: xscale.h:148
@ XSCALE_DCSR
Definition: xscale.h:158