dump.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Debug helper to dump the current kernel pagetables of the system
  3. * so that we can see what the various memory ranges are set to.
  4. *
  5. * Derived from x86 implementation:
  6. * (C) Copyright 2008 Intel Corporation
  7. *
  8. * Author: Arjan van de Ven <arjan@linux.intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; version 2
  13. * of the License.
  14. */
  15. #include <linux/debugfs.h>
  16. #include <linux/fs.h>
  17. #include <linux/mm.h>
  18. #include <linux/seq_file.h>
  19. #include <asm/fixmap.h>
  20. #include <asm/pgtable.h>
  21. struct addr_marker {
  22. unsigned long start_address;
  23. const char *name;
  24. };
  25. static struct addr_marker address_markers[] = {
  26. { MODULES_VADDR, "Modules" },
  27. { PAGE_OFFSET, "Kernel Mapping" },
  28. { 0, "vmalloc() Area" },
  29. { VMALLOC_END, "vmalloc() End" },
  30. { FIXADDR_START, "Fixmap Area" },
  31. { CONFIG_VECTORS_BASE, "Vectors" },
  32. { CONFIG_VECTORS_BASE + PAGE_SIZE * 2, "Vectors End" },
  33. { -1, NULL },
  34. };
  35. struct pg_state {
  36. struct seq_file *seq;
  37. const struct addr_marker *marker;
  38. unsigned long start_address;
  39. unsigned level;
  40. u64 current_prot;
  41. };
  42. struct prot_bits {
  43. u64 mask;
  44. u64 val;
  45. const char *set;
  46. const char *clear;
  47. };
  48. static const struct prot_bits pte_bits[] = {
  49. {
  50. .mask = L_PTE_USER,
  51. .val = L_PTE_USER,
  52. .set = "USR",
  53. .clear = " ",
  54. }, {
  55. .mask = L_PTE_RDONLY,
  56. .val = L_PTE_RDONLY,
  57. .set = "ro",
  58. .clear = "RW",
  59. }, {
  60. .mask = L_PTE_XN,
  61. .val = L_PTE_XN,
  62. .set = "NX",
  63. .clear = "x ",
  64. }, {
  65. .mask = L_PTE_SHARED,
  66. .val = L_PTE_SHARED,
  67. .set = "SHD",
  68. .clear = " ",
  69. }, {
  70. .mask = L_PTE_MT_MASK,
  71. .val = L_PTE_MT_UNCACHED,
  72. .set = "SO/UNCACHED",
  73. }, {
  74. .mask = L_PTE_MT_MASK,
  75. .val = L_PTE_MT_BUFFERABLE,
  76. .set = "MEM/BUFFERABLE/WC",
  77. }, {
  78. .mask = L_PTE_MT_MASK,
  79. .val = L_PTE_MT_WRITETHROUGH,
  80. .set = "MEM/CACHED/WT",
  81. }, {
  82. .mask = L_PTE_MT_MASK,
  83. .val = L_PTE_MT_WRITEBACK,
  84. .set = "MEM/CACHED/WBRA",
  85. #ifndef CONFIG_ARM_LPAE
  86. }, {
  87. .mask = L_PTE_MT_MASK,
  88. .val = L_PTE_MT_MINICACHE,
  89. .set = "MEM/MINICACHE",
  90. #endif
  91. }, {
  92. .mask = L_PTE_MT_MASK,
  93. .val = L_PTE_MT_WRITEALLOC,
  94. .set = "MEM/CACHED/WBWA",
  95. }, {
  96. .mask = L_PTE_MT_MASK,
  97. .val = L_PTE_MT_DEV_SHARED,
  98. .set = "DEV/SHARED",
  99. #ifndef CONFIG_ARM_LPAE
  100. }, {
  101. .mask = L_PTE_MT_MASK,
  102. .val = L_PTE_MT_DEV_NONSHARED,
  103. .set = "DEV/NONSHARED",
  104. #endif
  105. }, {
  106. .mask = L_PTE_MT_MASK,
  107. .val = L_PTE_MT_DEV_WC,
  108. .set = "DEV/WC",
  109. }, {
  110. .mask = L_PTE_MT_MASK,
  111. .val = L_PTE_MT_DEV_CACHED,
  112. .set = "DEV/CACHED",
  113. },
  114. };
  115. static const struct prot_bits section_bits[] = {
  116. #ifdef CONFIG_ARM_LPAE
  117. {
  118. .mask = PMD_SECT_USER,
  119. .val = PMD_SECT_USER,
  120. .set = "USR",
  121. }, {
  122. .mask = L_PMD_SECT_RDONLY | PMD_SECT_AP2,
  123. .val = L_PMD_SECT_RDONLY | PMD_SECT_AP2,
  124. .set = "ro",
  125. .clear = "RW",
  126. #elif __LINUX_ARM_ARCH__ >= 6
  127. {
  128. .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  129. .val = PMD_SECT_APX | PMD_SECT_AP_WRITE,
  130. .set = " ro",
  131. }, {
  132. .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  133. .val = PMD_SECT_AP_WRITE,
  134. .set = " RW",
  135. }, {
  136. .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  137. .val = PMD_SECT_AP_READ,
  138. .set = "USR ro",
  139. }, {
  140. .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  141. .val = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  142. .set = "USR RW",
  143. #else /* ARMv4/ARMv5 */
  144. /* These are approximate */
  145. {
  146. .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  147. .val = 0,
  148. .set = " ro",
  149. }, {
  150. .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  151. .val = PMD_SECT_AP_WRITE,
  152. .set = " RW",
  153. }, {
  154. .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  155. .val = PMD_SECT_AP_READ,
  156. .set = "USR ro",
  157. }, {
  158. .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  159. .val = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
  160. .set = "USR RW",
  161. #endif
  162. }, {
  163. .mask = PMD_SECT_XN,
  164. .val = PMD_SECT_XN,
  165. .set = "NX",
  166. .clear = "x ",
  167. }, {
  168. .mask = PMD_SECT_S,
  169. .val = PMD_SECT_S,
  170. .set = "SHD",
  171. .clear = " ",
  172. },
  173. };
  174. struct pg_level {
  175. const struct prot_bits *bits;
  176. size_t num;
  177. u64 mask;
  178. };
  179. static struct pg_level pg_level[] = {
  180. {
  181. }, { /* pgd */
  182. }, { /* pud */
  183. }, { /* pmd */
  184. .bits = section_bits,
  185. .num = ARRAY_SIZE(section_bits),
  186. }, { /* pte */
  187. .bits = pte_bits,
  188. .num = ARRAY_SIZE(pte_bits),
  189. },
  190. };
  191. static void dump_prot(struct pg_state *st, const struct prot_bits *bits, size_t num)
  192. {
  193. unsigned i;
  194. for (i = 0; i < num; i++, bits++) {
  195. const char *s;
  196. if ((st->current_prot & bits->mask) == bits->val)
  197. s = bits->set;
  198. else
  199. s = bits->clear;
  200. if (s)
  201. seq_printf(st->seq, " %s", s);
  202. }
  203. }
  204. static void note_page(struct pg_state *st, unsigned long addr, unsigned level, u64 val)
  205. {
  206. static const char units[] = "KMGTPE";
  207. u64 prot = val & pg_level[level].mask;
  208. if (!st->level) {
  209. st->level = level;
  210. st->current_prot = prot;
  211. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  212. } else if (prot != st->current_prot || level != st->level ||
  213. addr >= st->marker[1].start_address) {
  214. const char *unit = units;
  215. unsigned long delta;
  216. if (st->current_prot) {
  217. seq_printf(st->seq, "0x%08lx-0x%08lx ",
  218. st->start_address, addr);
  219. delta = (addr - st->start_address) >> 10;
  220. while (!(delta & 1023) && unit[1]) {
  221. delta >>= 10;
  222. unit++;
  223. }
  224. seq_printf(st->seq, "%9lu%c", delta, *unit);
  225. if (pg_level[st->level].bits)
  226. dump_prot(st, pg_level[st->level].bits, pg_level[st->level].num);
  227. seq_printf(st->seq, "\n");
  228. }
  229. if (addr >= st->marker[1].start_address) {
  230. st->marker++;
  231. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  232. }
  233. st->start_address = addr;
  234. st->current_prot = prot;
  235. st->level = level;
  236. }
  237. }
  238. static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
  239. {
  240. pte_t *pte = pte_offset_kernel(pmd, 0);
  241. unsigned long addr;
  242. unsigned i;
  243. for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
  244. addr = start + i * PAGE_SIZE;
  245. note_page(st, addr, 4, pte_val(*pte));
  246. }
  247. }
  248. static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
  249. {
  250. pmd_t *pmd = pmd_offset(pud, 0);
  251. unsigned long addr;
  252. unsigned i;
  253. for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
  254. addr = start + i * PMD_SIZE;
  255. if (pmd_none(*pmd) || pmd_large(*pmd) || !pmd_present(*pmd))
  256. note_page(st, addr, 3, pmd_val(*pmd));
  257. else
  258. walk_pte(st, pmd, addr);
  259. if (SECTION_SIZE < PMD_SIZE && pmd_large(pmd[1]))
  260. note_page(st, addr + SECTION_SIZE, 3, pmd_val(pmd[1]));
  261. }
  262. }
  263. static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
  264. {
  265. pud_t *pud = pud_offset(pgd, 0);
  266. unsigned long addr;
  267. unsigned i;
  268. for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
  269. addr = start + i * PUD_SIZE;
  270. if (!pud_none(*pud)) {
  271. walk_pmd(st, pud, addr);
  272. } else {
  273. note_page(st, addr, 2, pud_val(*pud));
  274. }
  275. }
  276. }
  277. static void walk_pgd(struct seq_file *m)
  278. {
  279. pgd_t *pgd = swapper_pg_dir;
  280. struct pg_state st;
  281. unsigned long addr;
  282. unsigned i;
  283. memset(&st, 0, sizeof(st));
  284. st.seq = m;
  285. st.marker = address_markers;
  286. for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
  287. addr = i * PGDIR_SIZE;
  288. if (!pgd_none(*pgd)) {
  289. walk_pud(&st, pgd, addr);
  290. } else {
  291. note_page(&st, addr, 1, pgd_val(*pgd));
  292. }
  293. }
  294. note_page(&st, 0, 0, 0);
  295. }
  296. static int ptdump_show(struct seq_file *m, void *v)
  297. {
  298. walk_pgd(m);
  299. return 0;
  300. }
  301. static int ptdump_open(struct inode *inode, struct file *file)
  302. {
  303. return single_open(file, ptdump_show, NULL);
  304. }
  305. static const struct file_operations ptdump_fops = {
  306. .open = ptdump_open,
  307. .read = seq_read,
  308. .llseek = seq_lseek,
  309. .release = single_release,
  310. };
  311. static int ptdump_init(void)
  312. {
  313. struct dentry *pe;
  314. unsigned i, j;
  315. for (i = 0; i < ARRAY_SIZE(pg_level); i++)
  316. if (pg_level[i].bits)
  317. for (j = 0; j < pg_level[i].num; j++)
  318. pg_level[i].mask |= pg_level[i].bits[j].mask;
  319. address_markers[2].start_address = VMALLOC_START;
  320. pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL,
  321. &ptdump_fops);
  322. return pe ? 0 : -ENOMEM;
  323. }
  324. __initcall(ptdump_init);