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.
 
 
 
 
 
 

428 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. #include "jtag.h"
  30. #include "log.h"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. char* mips32_core_reg_list[] =
  34. {
  35. "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
  36. "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
  37. "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  38. "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra",
  39. "status", "lo", "hi", "badvaddr", "cause", "pc"
  40. };
  41. mips32_core_reg_t mips32_core_reg_list_arch_info[MIPS32NUMCOREREGS] =
  42. {
  43. {0, NULL, NULL},
  44. {1, NULL, NULL},
  45. {2, NULL, NULL},
  46. {3, NULL, NULL},
  47. {4, NULL, NULL},
  48. {5, NULL, NULL},
  49. {6, NULL, NULL},
  50. {7, NULL, NULL},
  51. {8, NULL, NULL},
  52. {9, NULL, NULL},
  53. {10, NULL, NULL},
  54. {11, NULL, NULL},
  55. {12, NULL, NULL},
  56. {13, NULL, NULL},
  57. {14, NULL, NULL},
  58. {15, NULL, NULL},
  59. {16, NULL, NULL},
  60. {17, NULL, NULL},
  61. {18, NULL, NULL},
  62. {19, NULL, NULL},
  63. {20, NULL, NULL},
  64. {21, NULL, NULL},
  65. {22, NULL, NULL},
  66. {23, NULL, NULL},
  67. {24, NULL, NULL},
  68. {25, NULL, NULL},
  69. {26, NULL, NULL},
  70. {27, NULL, NULL},
  71. {28, NULL, NULL},
  72. {29, NULL, NULL},
  73. {30, NULL, NULL},
  74. {31, NULL, NULL},
  75. {32, NULL, NULL},
  76. {33, NULL, NULL},
  77. {34, NULL, NULL},
  78. {35, NULL, NULL},
  79. {36, NULL, NULL},
  80. {37, NULL, NULL},
  81. };
  82. u8 mips32_gdb_dummy_fsr_value[] = {0, 0, 0, 0};
  83. reg_t mips32_gdb_dummy_fsr_reg =
  84. {
  85. "GDB dummy floating-point status register", mips32_gdb_dummy_fsr_value, 0, 1, 32, NULL, 0, NULL, 0
  86. };
  87. u8 mips32_gdb_dummy_fir_value[] = {0, 0, 0, 0};
  88. reg_t mips32_gdb_dummy_fir_reg =
  89. {
  90. "GDB dummy floating-point register", mips32_gdb_dummy_fir_value, 0, 1, 32, NULL, 0, NULL, 0
  91. };
  92. int mips32_core_reg_arch_type = -1;
  93. int mips32_get_core_reg(reg_t *reg)
  94. {
  95. int retval;
  96. mips32_core_reg_t *mips32_reg = reg->arch_info;
  97. target_t *target = mips32_reg->target;
  98. mips32_common_t *mips32_target = target->arch_info;
  99. if (target->state != TARGET_HALTED)
  100. {
  101. return ERROR_TARGET_NOT_HALTED;
  102. }
  103. retval = mips32_target->read_core_reg(target, mips32_reg->num);
  104. return retval;
  105. }
  106. int mips32_set_core_reg(reg_t *reg, u8 *buf)
  107. {
  108. mips32_core_reg_t *mips32_reg = reg->arch_info;
  109. target_t *target = mips32_reg->target;
  110. u32 value = buf_get_u32(buf, 0, 32);
  111. if (target->state != TARGET_HALTED)
  112. {
  113. return ERROR_TARGET_NOT_HALTED;
  114. }
  115. buf_set_u32(reg->value, 0, 32, value);
  116. reg->dirty = 1;
  117. reg->valid = 1;
  118. return ERROR_OK;
  119. }
  120. int mips32_read_core_reg(struct target_s *target, int num)
  121. {
  122. u32 reg_value;
  123. mips32_core_reg_t *mips_core_reg;
  124. /* get pointers to arch-specific information */
  125. mips32_common_t *mips32 = target->arch_info;
  126. if ((num < 0) || (num >= MIPS32NUMCOREREGS))
  127. return ERROR_INVALID_ARGUMENTS;
  128. mips_core_reg = mips32->core_cache->reg_list[num].arch_info;
  129. reg_value = mips32->core_regs[num];
  130. buf_set_u32(mips32->core_cache->reg_list[num].value, 0, 32, reg_value);
  131. mips32->core_cache->reg_list[num].valid = 1;
  132. mips32->core_cache->reg_list[num].dirty = 0;
  133. return ERROR_OK;
  134. }
  135. int mips32_write_core_reg(struct target_s *target, int num)
  136. {
  137. u32 reg_value;
  138. mips32_core_reg_t *mips_core_reg;
  139. /* get pointers to arch-specific information */
  140. mips32_common_t *mips32 = target->arch_info;
  141. if ((num < 0) || (num >= MIPS32NUMCOREREGS))
  142. return ERROR_INVALID_ARGUMENTS;
  143. reg_value = buf_get_u32(mips32->core_cache->reg_list[num].value, 0, 32);
  144. mips_core_reg = mips32->core_cache->reg_list[num].arch_info;
  145. mips32->core_regs[num] = reg_value;
  146. LOG_DEBUG("write core reg %i value 0x%x", num , reg_value);
  147. mips32->core_cache->reg_list[num].valid = 1;
  148. mips32->core_cache->reg_list[num].dirty = 0;
  149. return ERROR_OK;
  150. }
  151. int mips32_invalidate_core_regs(target_t *target)
  152. {
  153. /* get pointers to arch-specific information */
  154. mips32_common_t *mips32 = target->arch_info;
  155. int i;
  156. for (i = 0; i < mips32->core_cache->num_regs; i++)
  157. {
  158. mips32->core_cache->reg_list[i].valid = 0;
  159. mips32->core_cache->reg_list[i].dirty = 0;
  160. }
  161. return ERROR_OK;
  162. }
  163. int mips32_get_gdb_reg_list(target_t *target, reg_t **reg_list[], int *reg_list_size)
  164. {
  165. /* get pointers to arch-specific information */
  166. mips32_common_t *mips32 = target->arch_info;
  167. int i;
  168. /* include fsr/fir reg */
  169. *reg_list_size = MIPS32NUMCOREREGS + 2;
  170. *reg_list = malloc(sizeof(reg_t*) * (*reg_list_size));
  171. for (i = 0; i < MIPS32NUMCOREREGS; i++)
  172. {
  173. (*reg_list)[i] = &mips32->core_cache->reg_list[i];
  174. }
  175. /* add dummy floating points regs */
  176. (*reg_list)[38] = &mips32_gdb_dummy_fsr_reg;
  177. (*reg_list)[39] = &mips32_gdb_dummy_fir_reg;
  178. return ERROR_OK;
  179. }
  180. int mips32_save_context(target_t *target)
  181. {
  182. int i;
  183. /* get pointers to arch-specific information */
  184. mips32_common_t *mips32 = target->arch_info;
  185. mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
  186. /* read core registers */
  187. mips32_pracc_read_regs(ejtag_info, mips32->core_regs);
  188. for (i = 0; i < MIPS32NUMCOREREGS; i++)
  189. {
  190. if (!mips32->core_cache->reg_list[i].valid)
  191. {
  192. mips32->read_core_reg(target, i);
  193. }
  194. }
  195. return ERROR_OK;
  196. }
  197. int mips32_restore_context(target_t *target)
  198. {
  199. int i;
  200. /* get pointers to arch-specific information */
  201. mips32_common_t *mips32 = target->arch_info;
  202. mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
  203. for (i = 0; i < MIPS32NUMCOREREGS; i++)
  204. {
  205. if (mips32->core_cache->reg_list[i].dirty)
  206. {
  207. mips32->write_core_reg(target, i);
  208. }
  209. }
  210. /* write core regs */
  211. mips32_pracc_write_regs(ejtag_info, mips32->core_regs);
  212. return ERROR_OK;
  213. }
  214. int mips32_arch_state(struct target_s *target)
  215. {
  216. mips32_common_t *mips32 = target->arch_info;
  217. if (mips32->common_magic != MIPS32_COMMON_MAGIC)
  218. {
  219. LOG_ERROR("BUG: called for a non-MIPS32 target");
  220. exit(-1);
  221. }
  222. LOG_USER("target halted due to %s, pc: 0x%8.8x",
  223. Jim_Nvp_value2name_simple( nvp_target_debug_reason, target->debug_reason )->name ,
  224. buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32));
  225. return ERROR_OK;
  226. }
  227. reg_cache_t *mips32_build_reg_cache(target_t *target)
  228. {
  229. /* get pointers to arch-specific information */
  230. mips32_common_t *mips32 = target->arch_info;
  231. int num_regs = MIPS32NUMCOREREGS;
  232. reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
  233. reg_cache_t *cache = malloc(sizeof(reg_cache_t));
  234. reg_t *reg_list = malloc(sizeof(reg_t) * num_regs);
  235. mips32_core_reg_t *arch_info = malloc(sizeof(mips32_core_reg_t) * num_regs);
  236. int i;
  237. if (mips32_core_reg_arch_type == -1)
  238. mips32_core_reg_arch_type = register_reg_arch_type(mips32_get_core_reg, mips32_set_core_reg);
  239. register_init_dummy(&mips32_gdb_dummy_fsr_reg);
  240. register_init_dummy(&mips32_gdb_dummy_fir_reg);
  241. /* Build the process context cache */
  242. cache->name = "mips32 registers";
  243. cache->next = NULL;
  244. cache->reg_list = reg_list;
  245. cache->num_regs = num_regs;
  246. (*cache_p) = cache;
  247. mips32->core_cache = cache;
  248. for (i = 0; i < num_regs; i++)
  249. {
  250. arch_info[i] = mips32_core_reg_list_arch_info[i];
  251. arch_info[i].target = target;
  252. arch_info[i].mips32_common = mips32;
  253. reg_list[i].name = mips32_core_reg_list[i];
  254. reg_list[i].size = 32;
  255. reg_list[i].value = calloc(1, 4);
  256. reg_list[i].dirty = 0;
  257. reg_list[i].valid = 0;
  258. reg_list[i].bitfield_desc = NULL;
  259. reg_list[i].num_bitfields = 0;
  260. reg_list[i].arch_type = mips32_core_reg_arch_type;
  261. reg_list[i].arch_info = &arch_info[i];
  262. }
  263. return cache;
  264. }
  265. int mips32_init_arch_info(target_t *target, mips32_common_t *mips32, int chain_pos, const char *variant)
  266. {
  267. target->arch_info = mips32;
  268. mips32->common_magic = MIPS32_COMMON_MAGIC;
  269. /* has breakpoint/watchpint unit been scanned */
  270. mips32->bp_scanned = 0;
  271. mips32->data_break_list = NULL;
  272. mips32->ejtag_info.chain_pos = chain_pos;
  273. mips32->read_core_reg = mips32_read_core_reg;
  274. mips32->write_core_reg = mips32_write_core_reg;
  275. return ERROR_OK;
  276. }
  277. int mips32_register_commands(struct command_context_s *cmd_ctx)
  278. {
  279. return ERROR_OK;
  280. }
  281. 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, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info)
  282. {
  283. /*TODO*/
  284. return ERROR_OK;
  285. }
  286. int mips32_examine(struct target_s *target)
  287. {
  288. mips32_common_t *mips32 = target->arch_info;
  289. if (!target->type->examined)
  290. {
  291. target->type->examined = 1;
  292. /* we will configure later */
  293. mips32->bp_scanned = 0;
  294. mips32->num_inst_bpoints = 0;
  295. mips32->num_data_bpoints = 0;
  296. mips32->num_inst_bpoints_avail = 0;
  297. mips32->num_data_bpoints_avail = 0;
  298. }
  299. return ERROR_OK;
  300. }
  301. int mips32_configure_break_unit(struct target_s *target)
  302. {
  303. /* get pointers to arch-specific information */
  304. mips32_common_t *mips32 = target->arch_info;
  305. int retval;
  306. u32 dcr, bpinfo;
  307. int i;
  308. if (mips32->bp_scanned)
  309. return ERROR_OK;
  310. /* get info about breakpoint support */
  311. if ((retval = target_read_u32(target, EJTAG_DCR, &dcr)) != ERROR_OK)
  312. return retval;
  313. if (dcr & (1 << 16))
  314. {
  315. /* get number of inst breakpoints */
  316. if ((retval = target_read_u32(target, EJTAG_IBS, &bpinfo)) != ERROR_OK)
  317. return retval;
  318. mips32->num_inst_bpoints = (bpinfo >> 24) & 0x0F;
  319. mips32->num_inst_bpoints_avail = mips32->num_inst_bpoints;
  320. mips32->inst_break_list = calloc(mips32->num_inst_bpoints, sizeof(mips32_comparator_t));
  321. for (i = 0; i < mips32->num_inst_bpoints; i++)
  322. {
  323. mips32->inst_break_list[i].reg_address = EJTAG_IBA1 + (0x100 * i);
  324. }
  325. /* clear IBIS reg */
  326. if ((retval = target_write_u32(target, EJTAG_IBS, 0)) != ERROR_OK)
  327. return retval;
  328. }
  329. if (dcr & (1 << 17))
  330. {
  331. /* get number of data breakpoints */
  332. if ((retval = target_read_u32(target, EJTAG_DBS, &bpinfo)) != ERROR_OK)
  333. return retval;
  334. mips32->num_data_bpoints = (bpinfo >> 24) & 0x0F;
  335. mips32->num_data_bpoints_avail = mips32->num_data_bpoints;
  336. mips32->data_break_list = calloc(mips32->num_data_bpoints, sizeof(mips32_comparator_t));
  337. for (i = 0; i < mips32->num_data_bpoints; i++)
  338. {
  339. mips32->data_break_list[i].reg_address = EJTAG_DBA1 + (0x100 * i);
  340. }
  341. /* clear DBIS reg */
  342. if ((retval = target_write_u32(target, EJTAG_DBS, 0)) != ERROR_OK)
  343. return retval;
  344. }
  345. LOG_DEBUG("DCR 0x%x numinst %i numdata %i", dcr, mips32->num_inst_bpoints, mips32->num_data_bpoints);
  346. mips32->bp_scanned = 1;
  347. return ERROR_OK;
  348. }