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.
 
 
 
 
 
 

461 lines
12 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2008 by Spencer Oliver *
  3. * spen@spen-soft.co.uk *
  4. * *
  5. * Copyright (C) 2008 by David T.L. Wong *
  6. * *
  7. * Copyright (C) 2007,2008 Øyvind Harboe *
  8. * oyvind.harboe@zylin.com *
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. * This program is distributed in the hope that it will be useful, *
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  18. * GNU General Public License for more details. *
  19. * *
  20. * You should have received a copy of the GNU General Public License *
  21. * along with this program; if not, write to the *
  22. * Free Software Foundation, Inc., *
  23. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  24. ***************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "mips32.h"
  29. char* mips32_core_reg_list[] =
  30. {
  31. "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
  32. "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
  33. "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  34. "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra",
  35. "status", "lo", "hi", "badvaddr", "cause", "pc"
  36. };
  37. mips32_core_reg_t mips32_core_reg_list_arch_info[MIPS32NUMCOREREGS] =
  38. {
  39. {0, NULL, NULL},
  40. {1, NULL, NULL},
  41. {2, NULL, NULL},
  42. {3, NULL, NULL},
  43. {4, NULL, NULL},
  44. {5, NULL, NULL},
  45. {6, NULL, NULL},
  46. {7, NULL, NULL},
  47. {8, NULL, NULL},
  48. {9, NULL, NULL},
  49. {10, NULL, NULL},
  50. {11, NULL, NULL},
  51. {12, NULL, NULL},
  52. {13, NULL, NULL},
  53. {14, NULL, NULL},
  54. {15, NULL, NULL},
  55. {16, NULL, NULL},
  56. {17, NULL, NULL},
  57. {18, NULL, NULL},
  58. {19, NULL, NULL},
  59. {20, NULL, NULL},
  60. {21, NULL, NULL},
  61. {22, NULL, NULL},
  62. {23, NULL, NULL},
  63. {24, NULL, NULL},
  64. {25, NULL, NULL},
  65. {26, NULL, NULL},
  66. {27, NULL, NULL},
  67. {28, NULL, NULL},
  68. {29, NULL, NULL},
  69. {30, NULL, NULL},
  70. {31, NULL, NULL},
  71. {32, NULL, NULL},
  72. {33, NULL, NULL},
  73. {34, NULL, NULL},
  74. {35, NULL, NULL},
  75. {36, NULL, NULL},
  76. {37, NULL, NULL},
  77. };
  78. /* number of mips dummy fp regs fp0 - fp31 + fsr and fir
  79. * we also add 18 unknown registers to handle gdb requests */
  80. #define MIPS32NUMFPREGS 34 + 18
  81. uint8_t mips32_gdb_dummy_fp_value[] = {0, 0, 0, 0};
  82. reg_t mips32_gdb_dummy_fp_reg =
  83. {
  84. "GDB dummy floating-point register", mips32_gdb_dummy_fp_value, 0, 1, 32, NULL, 0, NULL, 0
  85. };
  86. int mips32_core_reg_arch_type = -1;
  87. int mips32_get_core_reg(reg_t *reg)
  88. {
  89. int retval;
  90. mips32_core_reg_t *mips32_reg = reg->arch_info;
  91. target_t *target = mips32_reg->target;
  92. mips32_common_t *mips32_target = target->arch_info;
  93. if (target->state != TARGET_HALTED)
  94. {
  95. return ERROR_TARGET_NOT_HALTED;
  96. }
  97. retval = mips32_target->read_core_reg(target, mips32_reg->num);
  98. return retval;
  99. }
  100. int mips32_set_core_reg(reg_t *reg, uint8_t *buf)
  101. {
  102. mips32_core_reg_t *mips32_reg = reg->arch_info;
  103. target_t *target = mips32_reg->target;
  104. uint32_t value = buf_get_u32(buf, 0, 32);
  105. if (target->state != TARGET_HALTED)
  106. {
  107. return ERROR_TARGET_NOT_HALTED;
  108. }
  109. buf_set_u32(reg->value, 0, 32, value);
  110. reg->dirty = 1;
  111. reg->valid = 1;
  112. return ERROR_OK;
  113. }
  114. int mips32_read_core_reg(struct target_s *target, int num)
  115. {
  116. uint32_t reg_value;
  117. mips32_core_reg_t *mips_core_reg;
  118. /* get pointers to arch-specific information */
  119. mips32_common_t *mips32 = target->arch_info;
  120. if ((num < 0) || (num >= MIPS32NUMCOREREGS))
  121. return ERROR_INVALID_ARGUMENTS;
  122. mips_core_reg = mips32->core_cache->reg_list[num].arch_info;
  123. reg_value = mips32->core_regs[num];
  124. buf_set_u32(mips32->core_cache->reg_list[num].value, 0, 32, reg_value);
  125. mips32->core_cache->reg_list[num].valid = 1;
  126. mips32->core_cache->reg_list[num].dirty = 0;
  127. return ERROR_OK;
  128. }
  129. int mips32_write_core_reg(struct target_s *target, int num)
  130. {
  131. uint32_t reg_value;
  132. mips32_core_reg_t *mips_core_reg;
  133. /* get pointers to arch-specific information */
  134. mips32_common_t *mips32 = target->arch_info;
  135. if ((num < 0) || (num >= MIPS32NUMCOREREGS))
  136. return ERROR_INVALID_ARGUMENTS;
  137. reg_value = buf_get_u32(mips32->core_cache->reg_list[num].value, 0, 32);
  138. mips_core_reg = mips32->core_cache->reg_list[num].arch_info;
  139. mips32->core_regs[num] = reg_value;
  140. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", num , reg_value);
  141. mips32->core_cache->reg_list[num].valid = 1;
  142. mips32->core_cache->reg_list[num].dirty = 0;
  143. return ERROR_OK;
  144. }
  145. int mips32_invalidate_core_regs(target_t *target)
  146. {
  147. /* get pointers to arch-specific information */
  148. mips32_common_t *mips32 = target->arch_info;
  149. int i;
  150. for (i = 0; i < mips32->core_cache->num_regs; i++)
  151. {
  152. mips32->core_cache->reg_list[i].valid = 0;
  153. mips32->core_cache->reg_list[i].dirty = 0;
  154. }
  155. return ERROR_OK;
  156. }
  157. int mips32_get_gdb_reg_list(target_t *target, reg_t **reg_list[], int *reg_list_size)
  158. {
  159. /* get pointers to arch-specific information */
  160. mips32_common_t *mips32 = target->arch_info;
  161. int i;
  162. /* include floating point registers */
  163. *reg_list_size = MIPS32NUMCOREREGS + MIPS32NUMFPREGS;
  164. *reg_list = malloc(sizeof(reg_t*) * (*reg_list_size));
  165. for (i = 0; i < MIPS32NUMCOREREGS; i++)
  166. {
  167. (*reg_list)[i] = &mips32->core_cache->reg_list[i];
  168. }
  169. /* add dummy floating points regs */
  170. for (i = MIPS32NUMCOREREGS; i < (MIPS32NUMCOREREGS + MIPS32NUMFPREGS); i++)
  171. {
  172. (*reg_list)[i] = &mips32_gdb_dummy_fp_reg;
  173. }
  174. return ERROR_OK;
  175. }
  176. int mips32_save_context(target_t *target)
  177. {
  178. int i;
  179. /* get pointers to arch-specific information */
  180. mips32_common_t *mips32 = target->arch_info;
  181. mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
  182. /* read core registers */
  183. mips32_pracc_read_regs(ejtag_info, mips32->core_regs);
  184. for (i = 0; i < MIPS32NUMCOREREGS; i++)
  185. {
  186. if (!mips32->core_cache->reg_list[i].valid)
  187. {
  188. mips32->read_core_reg(target, i);
  189. }
  190. }
  191. return ERROR_OK;
  192. }
  193. int mips32_restore_context(target_t *target)
  194. {
  195. int i;
  196. /* get pointers to arch-specific information */
  197. mips32_common_t *mips32 = target->arch_info;
  198. mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
  199. for (i = 0; i < MIPS32NUMCOREREGS; i++)
  200. {
  201. if (mips32->core_cache->reg_list[i].dirty)
  202. {
  203. mips32->write_core_reg(target, i);
  204. }
  205. }
  206. /* write core regs */
  207. mips32_pracc_write_regs(ejtag_info, mips32->core_regs);
  208. return ERROR_OK;
  209. }
  210. int mips32_arch_state(struct target_s *target)
  211. {
  212. mips32_common_t *mips32 = target->arch_info;
  213. if (mips32->common_magic != MIPS32_COMMON_MAGIC)
  214. {
  215. LOG_ERROR("BUG: called for a non-MIPS32 target");
  216. exit(-1);
  217. }
  218. LOG_USER("target halted due to %s, pc: 0x%8.8" PRIx32 "",
  219. Jim_Nvp_value2name_simple(nvp_target_debug_reason, target->debug_reason)->name ,
  220. buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32));
  221. return ERROR_OK;
  222. }
  223. reg_cache_t *mips32_build_reg_cache(target_t *target)
  224. {
  225. /* get pointers to arch-specific information */
  226. mips32_common_t *mips32 = target->arch_info;
  227. int num_regs = MIPS32NUMCOREREGS;
  228. reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
  229. reg_cache_t *cache = malloc(sizeof(reg_cache_t));
  230. reg_t *reg_list = malloc(sizeof(reg_t) * num_regs);
  231. mips32_core_reg_t *arch_info = malloc(sizeof(mips32_core_reg_t) * num_regs);
  232. int i;
  233. if (mips32_core_reg_arch_type == -1)
  234. mips32_core_reg_arch_type = register_reg_arch_type(mips32_get_core_reg, mips32_set_core_reg);
  235. register_init_dummy(&mips32_gdb_dummy_fp_reg);
  236. /* Build the process context cache */
  237. cache->name = "mips32 registers";
  238. cache->next = NULL;
  239. cache->reg_list = reg_list;
  240. cache->num_regs = num_regs;
  241. (*cache_p) = cache;
  242. mips32->core_cache = cache;
  243. for (i = 0; i < num_regs; i++)
  244. {
  245. arch_info[i] = mips32_core_reg_list_arch_info[i];
  246. arch_info[i].target = target;
  247. arch_info[i].mips32_common = mips32;
  248. reg_list[i].name = mips32_core_reg_list[i];
  249. reg_list[i].size = 32;
  250. reg_list[i].value = calloc(1, 4);
  251. reg_list[i].dirty = 0;
  252. reg_list[i].valid = 0;
  253. reg_list[i].bitfield_desc = NULL;
  254. reg_list[i].num_bitfields = 0;
  255. reg_list[i].arch_type = mips32_core_reg_arch_type;
  256. reg_list[i].arch_info = &arch_info[i];
  257. }
  258. return cache;
  259. }
  260. int mips32_init_arch_info(target_t *target, mips32_common_t *mips32, jtag_tap_t *tap)
  261. {
  262. target->arch_info = mips32;
  263. mips32->common_magic = MIPS32_COMMON_MAGIC;
  264. /* has breakpoint/watchpint unit been scanned */
  265. mips32->bp_scanned = 0;
  266. mips32->data_break_list = NULL;
  267. mips32->ejtag_info.tap = tap;
  268. mips32->read_core_reg = mips32_read_core_reg;
  269. mips32->write_core_reg = mips32_write_core_reg;
  270. return ERROR_OK;
  271. }
  272. int mips32_register_commands(struct command_context_s *cmd_ctx)
  273. {
  274. return ERROR_OK;
  275. }
  276. int mips32_run_algorithm(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_params, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
  277. {
  278. /*TODO*/
  279. return ERROR_OK;
  280. }
  281. int mips32_examine(struct target_s *target)
  282. {
  283. mips32_common_t *mips32 = target->arch_info;
  284. if (!target_was_examined(target))
  285. {
  286. target_set_examined(target);
  287. /* we will configure later */
  288. mips32->bp_scanned = 0;
  289. mips32->num_inst_bpoints = 0;
  290. mips32->num_data_bpoints = 0;
  291. mips32->num_inst_bpoints_avail = 0;
  292. mips32->num_data_bpoints_avail = 0;
  293. }
  294. return ERROR_OK;
  295. }
  296. int mips32_configure_break_unit(struct target_s *target)
  297. {
  298. /* get pointers to arch-specific information */
  299. mips32_common_t *mips32 = target->arch_info;
  300. int retval;
  301. uint32_t dcr, bpinfo;
  302. int i;
  303. if (mips32->bp_scanned)
  304. return ERROR_OK;
  305. /* get info about breakpoint support */
  306. if ((retval = target_read_u32(target, EJTAG_DCR, &dcr)) != ERROR_OK)
  307. return retval;
  308. if (dcr & (1 << 16))
  309. {
  310. /* get number of inst breakpoints */
  311. if ((retval = target_read_u32(target, EJTAG_IBS, &bpinfo)) != ERROR_OK)
  312. return retval;
  313. mips32->num_inst_bpoints = (bpinfo >> 24) & 0x0F;
  314. mips32->num_inst_bpoints_avail = mips32->num_inst_bpoints;
  315. mips32->inst_break_list = calloc(mips32->num_inst_bpoints, sizeof(mips32_comparator_t));
  316. for (i = 0; i < mips32->num_inst_bpoints; i++)
  317. {
  318. mips32->inst_break_list[i].reg_address = EJTAG_IBA1 + (0x100 * i);
  319. }
  320. /* clear IBIS reg */
  321. if ((retval = target_write_u32(target, EJTAG_IBS, 0)) != ERROR_OK)
  322. return retval;
  323. }
  324. if (dcr & (1 << 17))
  325. {
  326. /* get number of data breakpoints */
  327. if ((retval = target_read_u32(target, EJTAG_DBS, &bpinfo)) != ERROR_OK)
  328. return retval;
  329. mips32->num_data_bpoints = (bpinfo >> 24) & 0x0F;
  330. mips32->num_data_bpoints_avail = mips32->num_data_bpoints;
  331. mips32->data_break_list = calloc(mips32->num_data_bpoints, sizeof(mips32_comparator_t));
  332. for (i = 0; i < mips32->num_data_bpoints; i++)
  333. {
  334. mips32->data_break_list[i].reg_address = EJTAG_DBA1 + (0x100 * i);
  335. }
  336. /* clear DBIS reg */
  337. if ((retval = target_write_u32(target, EJTAG_DBS, 0)) != ERROR_OK)
  338. return retval;
  339. }
  340. LOG_DEBUG("DCR 0x%" PRIx32 " numinst %i numdata %i", dcr, mips32->num_inst_bpoints, mips32->num_data_bpoints);
  341. mips32->bp_scanned = 1;
  342. return ERROR_OK;
  343. }
  344. int mips32_enable_interrupts(struct target_s *target, int enable)
  345. {
  346. int retval;
  347. int update = 0;
  348. uint32_t dcr;
  349. /* read debug control register */
  350. if ((retval = target_read_u32(target, EJTAG_DCR, &dcr)) != ERROR_OK)
  351. return retval;
  352. if (enable)
  353. {
  354. if (!(dcr & (1 << 4)))
  355. {
  356. /* enable interrupts */
  357. dcr |= (1 << 4);
  358. update = 1;
  359. }
  360. }
  361. else
  362. {
  363. if (dcr & (1 << 4))
  364. {
  365. /* disable interrupts */
  366. dcr &= ~(1 << 4);
  367. update = 1;
  368. }
  369. }
  370. if (update)
  371. {
  372. if ((retval = target_write_u32(target, EJTAG_DCR, dcr)) != ERROR_OK)
  373. return retval;
  374. }
  375. return ERROR_OK;
  376. }