You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

287 lines
7.3 KiB

  1. /*****************************************************************************
  2. * Copyright (C) 2016 by Matthias Welwarsky <matthias.welwarsky@sysgo.com> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. ****************************************************************************/
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include "target.h"
  18. #include "target_type.h"
  19. #include "arm_adi_v5.h"
  20. #include "register.h"
  21. #include <jtag/jtag.h>
  22. #define MEM_AP_COMMON_MAGIC 0x4DE4DA50
  23. struct mem_ap {
  24. int common_magic;
  25. struct adiv5_dap *dap;
  26. struct adiv5_ap *ap;
  27. int ap_num;
  28. };
  29. static int mem_ap_target_create(struct target *target, Jim_Interp *interp)
  30. {
  31. struct mem_ap *mem_ap;
  32. struct adiv5_private_config *pc;
  33. pc = (struct adiv5_private_config *)target->private_config;
  34. if (!pc)
  35. return ERROR_FAIL;
  36. if (pc->ap_num == DP_APSEL_INVALID) {
  37. LOG_ERROR("AP number not specified");
  38. return ERROR_FAIL;
  39. }
  40. mem_ap = calloc(1, sizeof(struct mem_ap));
  41. if (!mem_ap) {
  42. LOG_ERROR("Out of memory");
  43. return ERROR_FAIL;
  44. }
  45. mem_ap->ap_num = pc->ap_num;
  46. mem_ap->common_magic = MEM_AP_COMMON_MAGIC;
  47. mem_ap->dap = pc->dap;
  48. target->arch_info = mem_ap;
  49. if (!target->gdb_port_override)
  50. target->gdb_port_override = strdup("disabled");
  51. return ERROR_OK;
  52. }
  53. static int mem_ap_init_target(struct command_context *cmd_ctx, struct target *target)
  54. {
  55. LOG_DEBUG("%s", __func__);
  56. target->state = TARGET_UNKNOWN;
  57. target->debug_reason = DBG_REASON_UNDEFINED;
  58. return ERROR_OK;
  59. }
  60. static void mem_ap_deinit_target(struct target *target)
  61. {
  62. LOG_DEBUG("%s", __func__);
  63. free(target->private_config);
  64. free(target->arch_info);
  65. return;
  66. }
  67. static int mem_ap_arch_state(struct target *target)
  68. {
  69. LOG_DEBUG("%s", __func__);
  70. return ERROR_OK;
  71. }
  72. static int mem_ap_poll(struct target *target)
  73. {
  74. if (target->state == TARGET_UNKNOWN) {
  75. target->state = TARGET_RUNNING;
  76. target->debug_reason = DBG_REASON_NOTHALTED;
  77. }
  78. return ERROR_OK;
  79. }
  80. static int mem_ap_halt(struct target *target)
  81. {
  82. LOG_DEBUG("%s", __func__);
  83. target->state = TARGET_HALTED;
  84. target->debug_reason = DBG_REASON_DBGRQ;
  85. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  86. return ERROR_OK;
  87. }
  88. static int mem_ap_resume(struct target *target, int current, target_addr_t address,
  89. int handle_breakpoints, int debug_execution)
  90. {
  91. LOG_DEBUG("%s", __func__);
  92. target->state = TARGET_RUNNING;
  93. target->debug_reason = DBG_REASON_NOTHALTED;
  94. return ERROR_OK;
  95. }
  96. static int mem_ap_step(struct target *target, int current, target_addr_t address,
  97. int handle_breakpoints)
  98. {
  99. LOG_DEBUG("%s", __func__);
  100. target->state = TARGET_HALTED;
  101. target->debug_reason = DBG_REASON_DBGRQ;
  102. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  103. return ERROR_OK;
  104. }
  105. static int mem_ap_assert_reset(struct target *target)
  106. {
  107. target->state = TARGET_RESET;
  108. target->debug_reason = DBG_REASON_UNDEFINED;
  109. LOG_DEBUG("%s", __func__);
  110. return ERROR_OK;
  111. }
  112. static int mem_ap_examine(struct target *target)
  113. {
  114. struct mem_ap *mem_ap = target->arch_info;
  115. if (!target_was_examined(target)) {
  116. mem_ap->ap = dap_ap(mem_ap->dap, mem_ap->ap_num);
  117. target_set_examined(target);
  118. target->state = TARGET_UNKNOWN;
  119. target->debug_reason = DBG_REASON_UNDEFINED;
  120. return mem_ap_init(mem_ap->ap);
  121. }
  122. return ERROR_OK;
  123. }
  124. static int mem_ap_deassert_reset(struct target *target)
  125. {
  126. if (target->reset_halt) {
  127. target->state = TARGET_HALTED;
  128. target->debug_reason = DBG_REASON_DBGRQ;
  129. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  130. } else {
  131. target->state = TARGET_RUNNING;
  132. target->debug_reason = DBG_REASON_NOTHALTED;
  133. }
  134. LOG_DEBUG("%s", __func__);
  135. return ERROR_OK;
  136. }
  137. static int mem_ap_reg_get(struct reg *reg)
  138. {
  139. return ERROR_OK;
  140. }
  141. static int mem_ap_reg_set(struct reg *reg, uint8_t *buf)
  142. {
  143. return ERROR_OK;
  144. }
  145. static struct reg_arch_type mem_ap_reg_arch_type = {
  146. .get = mem_ap_reg_get,
  147. .set = mem_ap_reg_set,
  148. };
  149. const char *mem_ap_get_gdb_arch(struct target *target)
  150. {
  151. return "arm";
  152. }
  153. /*
  154. * Dummy ARM register emulation:
  155. * reg[0..15]: 32 bits, r0~r12, sp, lr, pc
  156. * reg[16..23]: 96 bits, f0~f7
  157. * reg[24]: 32 bits, fps
  158. * reg[25]: 32 bits, cpsr
  159. *
  160. * Set 'exist' only to reg[0..15], so initial response to GDB is correct
  161. */
  162. #define NUM_REGS 26
  163. #define MAX_REG_SIZE 96
  164. #define REG_EXIST(n) ((n) < 16)
  165. #define REG_SIZE(n) ((((n) >= 16) && ((n) < 24)) ? 96 : 32)
  166. struct mem_ap_alloc_reg_list {
  167. /* reg_list must be the first field */
  168. struct reg *reg_list[NUM_REGS];
  169. struct reg regs[NUM_REGS];
  170. uint8_t regs_value[MAX_REG_SIZE / 8];
  171. };
  172. static int mem_ap_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
  173. int *reg_list_size, enum target_register_class reg_class)
  174. {
  175. struct mem_ap_alloc_reg_list *mem_ap_alloc = calloc(1, sizeof(struct mem_ap_alloc_reg_list));
  176. if (!mem_ap_alloc) {
  177. LOG_ERROR("Out of memory");
  178. return ERROR_FAIL;
  179. }
  180. *reg_list = mem_ap_alloc->reg_list;
  181. *reg_list_size = NUM_REGS;
  182. struct reg *regs = mem_ap_alloc->regs;
  183. for (int i = 0; i < NUM_REGS; i++) {
  184. regs[i].number = i;
  185. regs[i].value = mem_ap_alloc->regs_value;
  186. regs[i].size = REG_SIZE(i);
  187. regs[i].exist = REG_EXIST(i);
  188. regs[i].type = &mem_ap_reg_arch_type;
  189. (*reg_list)[i] = &regs[i];
  190. }
  191. return ERROR_OK;
  192. }
  193. static int mem_ap_read_memory(struct target *target, target_addr_t address,
  194. uint32_t size, uint32_t count, uint8_t *buffer)
  195. {
  196. struct mem_ap *mem_ap = target->arch_info;
  197. LOG_DEBUG("Reading memory at physical address " TARGET_ADDR_FMT
  198. "; size %" PRIu32 "; count %" PRIu32, address, size, count);
  199. if (count == 0 || !buffer)
  200. return ERROR_COMMAND_SYNTAX_ERROR;
  201. return mem_ap_read_buf(mem_ap->ap, buffer, size, count, address);
  202. }
  203. static int mem_ap_write_memory(struct target *target, target_addr_t address,
  204. uint32_t size, uint32_t count,
  205. const uint8_t *buffer)
  206. {
  207. struct mem_ap *mem_ap = target->arch_info;
  208. LOG_DEBUG("Writing memory at physical address " TARGET_ADDR_FMT
  209. "; size %" PRIu32 "; count %" PRIu32, address, size, count);
  210. if (count == 0 || !buffer)
  211. return ERROR_COMMAND_SYNTAX_ERROR;
  212. return mem_ap_write_buf(mem_ap->ap, buffer, size, count, address);
  213. }
  214. struct target_type mem_ap_target = {
  215. .name = "mem_ap",
  216. .target_create = mem_ap_target_create,
  217. .init_target = mem_ap_init_target,
  218. .deinit_target = mem_ap_deinit_target,
  219. .examine = mem_ap_examine,
  220. .target_jim_configure = adiv5_jim_configure,
  221. .poll = mem_ap_poll,
  222. .arch_state = mem_ap_arch_state,
  223. .halt = mem_ap_halt,
  224. .resume = mem_ap_resume,
  225. .step = mem_ap_step,
  226. .assert_reset = mem_ap_assert_reset,
  227. .deassert_reset = mem_ap_deassert_reset,
  228. .get_gdb_arch = mem_ap_get_gdb_arch,
  229. .get_gdb_reg_list = mem_ap_get_gdb_reg_list,
  230. .read_memory = mem_ap_read_memory,
  231. .write_memory = mem_ap_write_memory,
  232. };