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.
 
 
 
 
 
 

374 lines
10 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "arm7_9_common.h"
  24. #include "log.h"
  25. #include "command.h"
  26. #include "armv4_5_mmu.h"
  27. #include "target.h"
  28. #include <stdlib.h>
  29. u32 armv4mmu_translate_va(target_t *target, armv4_5_mmu_common_t *armv4_5_mmu, u32 va, int *type, u32 *cb, int *domain, u32 *ap);
  30. int armv4_5_mmu_read_physical(target_t *target, armv4_5_mmu_common_t *armv4_5_mmu, u32 address, u32 size, u32 count, u8 *buffer);
  31. int armv4_5_mmu_write_physical(target_t *target, armv4_5_mmu_common_t *armv4_5_mmu, u32 address, u32 size, u32 count, u8 *buffer);
  32. char* armv4_5_mmu_page_type_names[] =
  33. {
  34. "section", "large page", "small page", "tiny page"
  35. };
  36. u32 armv4_5_mmu_translate_va(target_t *target, armv4_5_mmu_common_t *armv4_5_mmu, u32 va, int *type, u32 *cb, int *domain, u32 *ap)
  37. {
  38. u32 first_lvl_descriptor = 0x0;
  39. u32 second_lvl_descriptor = 0x0;
  40. u32 ttb = armv4_5_mmu->get_ttb(target);
  41. armv4_5_mmu_read_physical(target, armv4_5_mmu,
  42. (ttb & 0xffffc000) | ((va & 0xfff00000) >> 18),
  43. 4, 1, (u8*)&first_lvl_descriptor);
  44. first_lvl_descriptor = target_buffer_get_u32(target, (u8*)&first_lvl_descriptor);
  45. LOG_DEBUG("1st lvl desc: %8.8x", first_lvl_descriptor);
  46. if ((first_lvl_descriptor & 0x3) == 0)
  47. {
  48. *type = -1;
  49. LOG_ERROR("Address translation failure");
  50. return ERROR_TARGET_TRANSLATION_FAULT;
  51. }
  52. if (!armv4_5_mmu->has_tiny_pages && ((first_lvl_descriptor & 0x3) == 3))
  53. {
  54. *type = -1;
  55. LOG_ERROR("Address translation failure");
  56. return ERROR_TARGET_TRANSLATION_FAULT;
  57. }
  58. /* domain is always specified in bits 8-5 */
  59. *domain = (first_lvl_descriptor & 0x1e0) >> 5;
  60. if ((first_lvl_descriptor & 0x3) == 2)
  61. {
  62. /* section descriptor */
  63. *type = ARMV4_5_SECTION;
  64. *cb = (first_lvl_descriptor & 0xc) >> 2;
  65. *ap = (first_lvl_descriptor & 0xc00) >> 10;
  66. return (first_lvl_descriptor & 0xfff00000) | (va & 0x000fffff);
  67. }
  68. if ((first_lvl_descriptor & 0x3) == 1)
  69. {
  70. /* coarse page table */
  71. armv4_5_mmu_read_physical(target, armv4_5_mmu,
  72. (first_lvl_descriptor & 0xfffffc00) | ((va & 0x000ff000) >> 10),
  73. 4, 1, (u8*)&second_lvl_descriptor);
  74. }
  75. else if ((first_lvl_descriptor & 0x3) == 3)
  76. {
  77. /* fine page table */
  78. armv4_5_mmu_read_physical(target, armv4_5_mmu,
  79. (first_lvl_descriptor & 0xfffff000) | ((va & 0x000ffc00) >> 8),
  80. 4, 1, (u8*)&second_lvl_descriptor);
  81. }
  82. second_lvl_descriptor = target_buffer_get_u32(target, (u8*)&second_lvl_descriptor);
  83. LOG_DEBUG("2nd lvl desc: %8.8x", second_lvl_descriptor);
  84. if ((second_lvl_descriptor & 0x3) == 0)
  85. {
  86. *type = -1;
  87. LOG_ERROR("Address translation failure");
  88. return ERROR_TARGET_TRANSLATION_FAULT;
  89. }
  90. /* cacheable/bufferable is always specified in bits 3-2 */
  91. *cb = (second_lvl_descriptor & 0xc) >> 2;
  92. if ((second_lvl_descriptor & 0x3) == 1)
  93. {
  94. /* large page descriptor */
  95. *type = ARMV4_5_LARGE_PAGE;
  96. *ap = (second_lvl_descriptor & 0xff0) >> 4;
  97. return (second_lvl_descriptor & 0xffff0000) | (va & 0x0000ffff);
  98. }
  99. if ((second_lvl_descriptor & 0x3) == 2)
  100. {
  101. /* small page descriptor */
  102. *type = ARMV4_5_SMALL_PAGE;
  103. *ap = (second_lvl_descriptor & 0xff0) >> 4;
  104. return (second_lvl_descriptor & 0xfffff000) | (va & 0x00000fff);
  105. }
  106. if ((second_lvl_descriptor & 0x3) == 3)
  107. {
  108. /* tiny page descriptor */
  109. *type = ARMV4_5_TINY_PAGE;
  110. *ap = (second_lvl_descriptor & 0x30) >> 4;
  111. return (second_lvl_descriptor & 0xfffffc00) | (va & 0x000003ff);
  112. }
  113. /* should not happen */
  114. *type = -1;
  115. LOG_ERROR("Address translation failure");
  116. return ERROR_TARGET_TRANSLATION_FAULT;
  117. }
  118. int armv4_5_mmu_read_physical(target_t *target, armv4_5_mmu_common_t *armv4_5_mmu, u32 address, u32 size, u32 count, u8 *buffer)
  119. {
  120. int retval;
  121. if (target->state != TARGET_HALTED)
  122. return ERROR_TARGET_NOT_HALTED;
  123. /* disable MMU and data (or unified) cache */
  124. armv4_5_mmu->disable_mmu_caches(target, 1, 1, 0);
  125. retval = armv4_5_mmu->read_memory(target, address, size, count, buffer);
  126. /* reenable MMU / cache */
  127. armv4_5_mmu->enable_mmu_caches(target, armv4_5_mmu->mmu_enabled,
  128. armv4_5_mmu->armv4_5_cache.d_u_cache_enabled,
  129. armv4_5_mmu->armv4_5_cache.i_cache_enabled);
  130. return retval;
  131. }
  132. int armv4_5_mmu_write_physical(target_t *target, armv4_5_mmu_common_t *armv4_5_mmu, u32 address, u32 size, u32 count, u8 *buffer)
  133. {
  134. int retval;
  135. if (target->state != TARGET_HALTED)
  136. return ERROR_TARGET_NOT_HALTED;
  137. /* disable MMU and data (or unified) cache */
  138. armv4_5_mmu->disable_mmu_caches(target, 1, 1, 0);
  139. retval = armv4_5_mmu->write_memory(target, address, size, count, buffer);
  140. /* reenable MMU / cache */
  141. armv4_5_mmu->enable_mmu_caches(target, armv4_5_mmu->mmu_enabled,
  142. armv4_5_mmu->armv4_5_cache.d_u_cache_enabled,
  143. armv4_5_mmu->armv4_5_cache.i_cache_enabled);
  144. return retval;
  145. }
  146. int armv4_5_mmu_handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc, target_t *target, armv4_5_mmu_common_t *armv4_5_mmu)
  147. {
  148. u32 va;
  149. u32 pa;
  150. int type;
  151. u32 cb;
  152. int domain;
  153. u32 ap;
  154. if (target->state != TARGET_HALTED)
  155. {
  156. command_print(cmd_ctx, "target must be stopped for \"virt2phys\" command");
  157. return ERROR_OK;
  158. }
  159. if (argc == 0)
  160. {
  161. command_print(cmd_ctx, "usage: virt2phys <virtual address>");
  162. return ERROR_OK;
  163. }
  164. if (argc == 1)
  165. {
  166. va = strtoul(args[0], NULL, 0);
  167. pa = armv4_5_mmu_translate_va(target, armv4_5_mmu, va, &type, &cb, &domain, &ap);
  168. if (type == -1)
  169. {
  170. switch (pa)
  171. {
  172. case ERROR_TARGET_TRANSLATION_FAULT:
  173. command_print(cmd_ctx, "no valid translation for 0x%8.8x", va);
  174. break;
  175. default:
  176. command_print(cmd_ctx, "unknown translation error");
  177. }
  178. return ERROR_OK;
  179. }
  180. command_print(cmd_ctx, "0x%8.8x -> 0x%8.8x, type: %s, cb: %i, domain: %i, ap: %2.2x",
  181. va, pa, armv4_5_mmu_page_type_names[type], cb, domain, ap);
  182. }
  183. return ERROR_OK;
  184. }
  185. int armv4_5_mmu_handle_md_phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc, target_t *target, armv4_5_mmu_common_t *armv4_5_mmu)
  186. {
  187. int count = 1;
  188. int size = 4;
  189. u32 address = 0;
  190. int i;
  191. char output[128];
  192. int output_len;
  193. int retval;
  194. u8 *buffer;
  195. if (target->state != TARGET_HALTED)
  196. {
  197. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  198. return ERROR_OK;
  199. }
  200. if (argc < 1)
  201. return ERROR_OK;
  202. if (argc == 2)
  203. count = strtoul(args[1], NULL, 0);
  204. address = strtoul(args[0], NULL, 0);
  205. switch (cmd[2])
  206. {
  207. case 'w':
  208. size = 4;
  209. break;
  210. case 'h':
  211. size = 2;
  212. break;
  213. case 'b':
  214. size = 1;
  215. break;
  216. default:
  217. return ERROR_OK;
  218. }
  219. buffer = calloc(count, size);
  220. if ((retval = armv4_5_mmu_read_physical(target, armv4_5_mmu, address, size, count, buffer)) != ERROR_OK)
  221. {
  222. switch (retval)
  223. {
  224. case ERROR_TARGET_UNALIGNED_ACCESS:
  225. command_print(cmd_ctx, "error: address not aligned");
  226. break;
  227. case ERROR_TARGET_NOT_HALTED:
  228. command_print(cmd_ctx, "error: target must be halted for memory accesses");
  229. break;
  230. case ERROR_TARGET_DATA_ABORT:
  231. command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
  232. break;
  233. default:
  234. command_print(cmd_ctx, "error: unknown error");
  235. }
  236. }
  237. output_len = 0;
  238. for (i = 0; i < count; i++)
  239. {
  240. if (i%8 == 0)
  241. output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
  242. switch (size)
  243. {
  244. case 4:
  245. output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
  246. break;
  247. case 2:
  248. output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
  249. break;
  250. case 1:
  251. output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
  252. break;
  253. }
  254. if ((i % 8 == 7) || (i == count - 1))
  255. {
  256. command_print(cmd_ctx, output);
  257. output_len = 0;
  258. }
  259. }
  260. free(buffer);
  261. return ERROR_OK;
  262. }
  263. int armv4_5_mmu_handle_mw_phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc, target_t *target, armv4_5_mmu_common_t *armv4_5_mmu)
  264. {
  265. u32 address = 0;
  266. u32 value = 0;
  267. int retval;
  268. u8 value_buf[4];
  269. if (target->state != TARGET_HALTED)
  270. {
  271. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  272. return ERROR_OK;
  273. }
  274. if (argc < 2)
  275. return ERROR_OK;
  276. address = strtoul(args[0], NULL, 0);
  277. value = strtoul(args[1], NULL, 0);
  278. switch (cmd[2])
  279. {
  280. case 'w':
  281. target_buffer_set_u32(target, value_buf, value);
  282. retval = armv4_5_mmu_write_physical(target, armv4_5_mmu, address, 4, 1, value_buf);
  283. break;
  284. case 'h':
  285. target_buffer_set_u16(target, value_buf, value);
  286. retval = armv4_5_mmu_write_physical(target, armv4_5_mmu, address, 2, 1, value_buf);
  287. break;
  288. case 'b':
  289. value_buf[0] = value;
  290. retval = armv4_5_mmu_write_physical(target, armv4_5_mmu, address, 1, 1, value_buf);
  291. break;
  292. default:
  293. return ERROR_OK;
  294. }
  295. switch (retval)
  296. {
  297. case ERROR_TARGET_UNALIGNED_ACCESS:
  298. command_print(cmd_ctx, "error: address not aligned");
  299. break;
  300. case ERROR_TARGET_DATA_ABORT:
  301. command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
  302. break;
  303. case ERROR_TARGET_NOT_HALTED:
  304. command_print(cmd_ctx, "error: target must be halted for memory accesses");
  305. break;
  306. case ERROR_OK:
  307. break;
  308. default:
  309. command_print(cmd_ctx, "error: unknown error");
  310. }
  311. return ERROR_OK;
  312. }