1 /***************************************************************************
2 * Copyright (C) 2016 by Matthias Welwarsky *
3 * matthias.welwarsky@sysgo.com *
5 * Copyright (C) ST-Ericsson SA 2011 michel.jaouen@stericsson.com *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 ***************************************************************************/
25 #include <helper/binarybuffer.h>
26 #include <helper/command.h>
28 #include "jtag/interface.h"
31 #include "armv7a_mmu.h"
32 #include "arm_opcodes.h"
35 #define SCTLR_BIT_AFE (1 << 29)
37 /* V7 method VA TO PA */
38 int armv7a_mmu_translate_va_pa(struct target *target, uint32_t va,
39 target_addr_t *val, int meminfo)
41 int retval = ERROR_FAIL;
42 struct armv7a_common *armv7a = target_to_armv7a(target);
43 struct arm_dpm *dpm = armv7a->arm.dpm;
44 uint32_t virt = va & ~0xfff, value;
45 uint32_t NOS, NS, INNER, OUTER, SS;
47 retval = dpm->prepare(dpm);
48 if (retval != ERROR_OK)
50 /* mmu must be enable in order to get a correct translation
51 * use VA to PA CP15 register for conversion */
52 retval = dpm->instr_write_data_r0(dpm,
53 ARMV4_5_MCR(15, 0, 0, 7, 8, 0),
55 if (retval != ERROR_OK)
57 retval = dpm->instr_read_data_r0(dpm,
58 ARMV4_5_MRC(15, 0, 0, 7, 4, 0),
60 if (retval != ERROR_OK)
63 /* decode memory attribute */
64 SS = (value >> 1) & 1;
67 LOG_ERROR("Super section found with no-64 bit address support");
71 NOS = (value >> 10) & 1; /* Not Outer shareable */
72 NS = (value >> 9) & 1; /* Non secure */
73 INNER = (value >> 4) & 0x7;
74 OUTER = (value >> 2) & 0x3;
77 /* PAR[31:24] contains PA[31:24] */
78 *val = value & 0xff000000;
79 /* PAR [23:16] contains PA[39:32] */
80 *val |= (target_addr_t)(value & 0x00ff0000) << 16;
81 /* PA[23:12] is the same as VA[23:12] */
82 *val |= (va & 0xffffff);
84 *val = (value & ~0xfff) + (va & 0xfff);
87 LOG_INFO("%" PRIx32 " : %" TARGET_PRIxADDR " %s outer shareable %s secured %s super section",
89 NOS == 1 ? "not" : " ",
91 SS == 0 ? "not" : "");
94 LOG_INFO("outer: Non-Cacheable");
97 LOG_INFO("outer: Write-Back, Write-Allocate");
100 LOG_INFO("outer: Write-Through, No Write-Allocate");
103 LOG_INFO("outer: Write-Back, no Write-Allocate");
108 LOG_INFO("inner: Non-Cacheable");
111 LOG_INFO("inner: Strongly-ordered");
114 LOG_INFO("inner: Device");
117 LOG_INFO("inner: Write-Back, Write-Allocate");
120 LOG_INFO("inner: Write-Through");
123 LOG_INFO("inner: Write-Back, no Write-Allocate");
126 LOG_INFO("inner: %" PRIx32 " ???", INNER);
136 static const char *desc_bits_to_string(bool c_bit, bool b_bit, bool s_bit, bool ap2, int ap10, bool afe)
138 static char bits_string[64];
144 bool priv = !(ap10 & 2);
145 len = snprintf(bits_string, sizeof(bits_string), "%s%s%s access%s: %s%s",
146 s_bit ? "S " : "", c_bit ? "C " : "", b_bit ? "B " : "",
147 priv ? "(priv)" : "", acc_r ? "R" : "N", acc_w ? "W " : "O ");
149 bool priv_acc_w = !ap2;
150 bool priv_acc_r = true;
151 bool unpriv_acc_w = priv_acc_w;
152 bool unpriv_acc_r = priv_acc_r;
156 priv_acc_r = priv_acc_w = false;
157 unpriv_acc_r = unpriv_acc_w = false;
160 unpriv_acc_r = unpriv_acc_w = false;
163 unpriv_acc_w = false;
169 len = snprintf(bits_string, sizeof(bits_string), "%s%s%s access(priv): %s%s access(unpriv): %s%s",
170 s_bit ? "S " : "", c_bit ? "C " : "", b_bit ? "B " : "", priv_acc_r ? "R" : "N", priv_acc_w ? "W" : "O",
171 unpriv_acc_r ? "R" : "N", unpriv_acc_w ? "W" : "O");
174 if (len >= sizeof(bits_string))
180 static const char *l2_desc_bits_to_string(uint32_t l2_desc, bool afe)
182 bool c_bit = !!(l2_desc & (1 << 3));
183 bool b_bit = !!(l2_desc & (1 << 2));
184 bool s_bit = !!(l2_desc & (1 << 10));
185 bool ap2 = !!(l2_desc & (1 << 9));
186 int ap10 = (l2_desc >> 4) & 3;
188 return desc_bits_to_string(c_bit, b_bit, s_bit, ap2, ap10, afe);
191 static const char *l1_desc_bits_to_string(uint32_t l1_desc, bool afe)
193 bool c_bit = !!(l1_desc & (1 << 3));
194 bool b_bit = !!(l1_desc & (1 << 2));
195 bool s_bit = !!(l1_desc & (1 << 16));
196 bool ap2 = !!(l1_desc & (1 << 15));
197 int ap10 = (l1_desc >> 10) & 3;
199 return desc_bits_to_string(c_bit, b_bit, s_bit, ap2, ap10, afe);
202 COMMAND_HANDLER(armv7a_mmu_dump_table)
204 struct target *target = get_current_target(CMD_CTX);
205 struct cortex_a_common *cortex_a = target_to_cortex_a(target);
206 struct armv7a_common *armv7a = target_to_armv7a(target);
207 struct armv7a_mmu_common *mmu = &armv7a->armv7a_mmu;
208 struct armv7a_cache_common *cache = &mmu->armv7a_cache;
209 uint32_t *first_lvl_ptbl;
214 int max_pt_idx = 4095;
218 return ERROR_COMMAND_SYNTAX_ERROR;
220 if (!strcmp(CMD_ARGV[0], "addr")) {
222 return ERROR_COMMAND_SYNTAX_ERROR;
224 COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[1], ttb);
227 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], max_pt_idx);
229 if (max_pt_idx < 1 || max_pt_idx > 4096)
230 return ERROR_COMMAND_ARGUMENT_INVALID;
234 if (mmu->cached != 1) {
235 LOG_ERROR("TTB not cached!");
239 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], ttbidx);
240 if (ttbidx < 0 || ttbidx > 1)
241 return ERROR_COMMAND_ARGUMENT_INVALID;
243 ttb = mmu->ttbr[ttbidx] & mmu->ttbr_mask[ttbidx];
246 int ttbcr_n = mmu->ttbcr & 0x7;
247 max_pt_idx = 0x0fff >> ttbcr_n;
251 LOG_USER("Page Directory at (phys): %8.8" TARGET_PRIxADDR, ttb);
253 first_lvl_ptbl = malloc(sizeof(uint32_t)*(max_pt_idx+1));
254 if (first_lvl_ptbl == NULL)
258 * this may or may not be necessary depending on whether
259 * the table walker is configured to use the cache or not.
261 cache->flush_all_data_cache(target);
263 retval = mmu->read_physical_memory(target, ttb, 4, max_pt_idx+1, (uint8_t *)first_lvl_ptbl);
264 if (retval != ERROR_OK) {
265 LOG_ERROR("Failed to read first-level page table!");
269 afe = !!(cortex_a->cp15_control_reg & SCTLR_BIT_AFE);
271 for (pt_idx = 0; pt_idx <= max_pt_idx;) {
272 uint32_t first_lvl_descriptor = target_buffer_get_u32(target,
273 (uint8_t *)&first_lvl_ptbl[pt_idx]);
275 LOG_DEBUG("L1 desc[%8.8"PRIx32"]: %8.8"PRIx32, pt_idx << 20, first_lvl_descriptor);
277 /* skip empty entries in the first level table */
278 if ((first_lvl_descriptor & 3) == 0) {
281 if ((first_lvl_descriptor & 0x40002) == 2) {
282 /* section descriptor */
283 uint32_t va_range = 1024*1024-1; /* 1MB range */
284 uint32_t va_start = pt_idx << 20;
285 uint32_t va_end = va_start + va_range;
287 uint32_t pa_start = (first_lvl_descriptor & 0xfff00000);
288 uint32_t pa_end = pa_start + va_range;
290 LOG_USER("SECT: VA[%8.8"PRIx32" -- %8.8"PRIx32"]: PA[%8.8"PRIx32" -- %8.8"PRIx32"] %s",
291 va_start, va_end, pa_start, pa_end, l1_desc_bits_to_string(first_lvl_descriptor, afe));
294 if ((first_lvl_descriptor & 0x40002) == 0x40002) {
295 /* supersection descriptor */
296 uint32_t va_range = 16*1024*1024-1; /* 16MB range */
297 uint32_t va_start = pt_idx << 20;
298 uint32_t va_end = va_start + va_range;
300 uint32_t pa_start = (first_lvl_descriptor & 0xff000000);
301 uint32_t pa_end = pa_start + va_range;
303 LOG_USER("SSCT: VA[%8.8"PRIx32" -- %8.8"PRIx32"]: PA[%8.8"PRIx32" -- %8.8"PRIx32"] %s",
304 va_start, va_end, pa_start, pa_end, l1_desc_bits_to_string(first_lvl_descriptor, afe));
306 /* skip next 15 entries, they're duplicating the first entry */
309 target_addr_t second_lvl_ptbl = first_lvl_descriptor & 0xfffffc00;
310 uint32_t second_lvl_descriptor;
314 /* page table, always 1KB long */
316 retval = mmu->read_physical_memory(target, second_lvl_ptbl,
317 4, 256, (uint8_t *)pt2);
318 if (retval != ERROR_OK) {
319 LOG_ERROR("Failed to read second-level page table!");
323 for (pt2_idx = 0; pt2_idx < 256; ) {
324 second_lvl_descriptor = target_buffer_get_u32(target,
325 (uint8_t *)&pt2[pt2_idx]);
327 if ((second_lvl_descriptor & 3) == 0) {
331 if ((second_lvl_descriptor & 3) == 1) {
333 uint32_t va_range = 64*1024-1; /* 64KB range */
334 uint32_t va_start = (pt_idx << 20) + (pt2_idx << 12);
335 uint32_t va_end = va_start + va_range;
337 uint32_t pa_start = (second_lvl_descriptor & 0xffff0000);
338 uint32_t pa_end = pa_start + va_range;
340 LOG_USER("LPGE: VA[%8.8"PRIx32" -- %8.8"PRIx32"]: PA[%8.8"PRIx32" -- %8.8"PRIx32"] %s",
341 va_start, va_end, pa_start, pa_end, l2_desc_bits_to_string(second_lvl_descriptor, afe));
346 uint32_t va_range = 4*1024-1; /* 4KB range */
347 uint32_t va_start = (pt_idx << 20) + (pt2_idx << 12);
348 uint32_t va_end = va_start + va_range;
350 uint32_t pa_start = (second_lvl_descriptor & 0xfffff000);
351 uint32_t pa_end = pa_start + va_range;
353 LOG_USER("SPGE: VA[%8.8"PRIx32" -- %8.8"PRIx32"]: PA[%8.8"PRIx32" -- %8.8"PRIx32"] %s",
354 va_start, va_end, pa_start, pa_end, l2_desc_bits_to_string(second_lvl_descriptor, afe));
364 free(first_lvl_ptbl);
368 static const struct command_registration armv7a_mmu_group_handlers[] = {
371 .handler = armv7a_mmu_dump_table,
373 .help = "dump translation table 0, 1 or from <address>",
374 .usage = "(0|1|addr <address> [num_entries])",
376 COMMAND_REGISTRATION_DONE
379 const struct command_registration armv7a_mmu_command_handlers[] = {
383 .help = "mmu command group",
385 .chain = armv7a_mmu_group_handlers,
387 COMMAND_REGISTRATION_DONE