dump_linuxpagetables.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright 2016, Rashmica Gupta, IBM Corp.
  3. *
  4. * This traverses the kernel pagetables and dumps the
  5. * information about the used sections of memory to
  6. * /sys/kernel/debug/kernel_pagetables.
  7. *
  8. * Derived from the arm64 implementation:
  9. * Copyright (c) 2014, The Linux Foundation, Laura Abbott.
  10. * (C) Copyright 2008 Intel Corporation, Arjan van de Ven.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; version 2
  15. * of the License.
  16. */
  17. #include <linux/debugfs.h>
  18. #include <linux/fs.h>
  19. #include <linux/hugetlb.h>
  20. #include <linux/io.h>
  21. #include <linux/mm.h>
  22. #include <linux/highmem.h>
  23. #include <linux/sched.h>
  24. #include <linux/seq_file.h>
  25. #include <asm/fixmap.h>
  26. #include <asm/pgtable.h>
  27. #include <linux/const.h>
  28. #include <asm/page.h>
  29. #include <asm/pgalloc.h>
  30. #include "dump_linuxpagetables.h"
  31. #ifdef CONFIG_PPC32
  32. #define KERN_VIRT_START 0
  33. #endif
  34. /*
  35. * To visualise what is happening,
  36. *
  37. * - PTRS_PER_P** = how many entries there are in the corresponding P**
  38. * - P**_SHIFT = how many bits of the address we use to index into the
  39. * corresponding P**
  40. * - P**_SIZE is how much memory we can access through the table - not the
  41. * size of the table itself.
  42. * P**={PGD, PUD, PMD, PTE}
  43. *
  44. *
  45. * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
  46. * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
  47. * a page.
  48. *
  49. * In the case where there are only 3 levels, the PUD is folded into the
  50. * PGD: every PUD has only one entry which points to the PMD.
  51. *
  52. * The page dumper groups page table entries of the same type into a single
  53. * description. It uses pg_state to track the range information while
  54. * iterating over the PTE entries. When the continuity is broken it then
  55. * dumps out a description of the range - ie PTEs that are virtually contiguous
  56. * with the same PTE flags are chunked together. This is to make it clear how
  57. * different areas of the kernel virtual memory are used.
  58. *
  59. */
  60. struct pg_state {
  61. struct seq_file *seq;
  62. const struct addr_marker *marker;
  63. unsigned long start_address;
  64. unsigned long start_pa;
  65. unsigned long last_pa;
  66. unsigned int level;
  67. u64 current_flags;
  68. };
  69. struct addr_marker {
  70. unsigned long start_address;
  71. const char *name;
  72. };
  73. static struct addr_marker address_markers[] = {
  74. { 0, "Start of kernel VM" },
  75. { 0, "vmalloc() Area" },
  76. { 0, "vmalloc() End" },
  77. #ifdef CONFIG_PPC64
  78. { 0, "isa I/O start" },
  79. { 0, "isa I/O end" },
  80. { 0, "phb I/O start" },
  81. { 0, "phb I/O end" },
  82. { 0, "I/O remap start" },
  83. { 0, "I/O remap end" },
  84. { 0, "vmemmap start" },
  85. #else
  86. { 0, "Early I/O remap start" },
  87. { 0, "Early I/O remap end" },
  88. #ifdef CONFIG_NOT_COHERENT_CACHE
  89. { 0, "Consistent mem start" },
  90. { 0, "Consistent mem end" },
  91. #endif
  92. #ifdef CONFIG_HIGHMEM
  93. { 0, "Highmem PTEs start" },
  94. { 0, "Highmem PTEs end" },
  95. #endif
  96. { 0, "Fixmap start" },
  97. { 0, "Fixmap end" },
  98. #endif
  99. { -1, NULL },
  100. };
  101. static void dump_flag_info(struct pg_state *st, const struct flag_info
  102. *flag, u64 pte, int num)
  103. {
  104. unsigned int i;
  105. for (i = 0; i < num; i++, flag++) {
  106. const char *s = NULL;
  107. u64 val;
  108. /* flag not defined so don't check it */
  109. if (flag->mask == 0)
  110. continue;
  111. /* Some 'flags' are actually values */
  112. if (flag->is_val) {
  113. val = pte & flag->val;
  114. if (flag->shift)
  115. val = val >> flag->shift;
  116. seq_printf(st->seq, " %s:%llx", flag->set, val);
  117. } else {
  118. if ((pte & flag->mask) == flag->val)
  119. s = flag->set;
  120. else
  121. s = flag->clear;
  122. if (s)
  123. seq_printf(st->seq, " %s", s);
  124. }
  125. st->current_flags &= ~flag->mask;
  126. }
  127. if (st->current_flags != 0)
  128. seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
  129. }
  130. static void dump_addr(struct pg_state *st, unsigned long addr)
  131. {
  132. static const char units[] = "KMGTPE";
  133. const char *unit = units;
  134. unsigned long delta;
  135. #ifdef CONFIG_PPC64
  136. seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
  137. seq_printf(st->seq, "0x%016lx ", st->start_pa);
  138. #else
  139. seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
  140. seq_printf(st->seq, "0x%08lx ", st->start_pa);
  141. #endif
  142. delta = (addr - st->start_address) >> 10;
  143. /* Work out what appropriate unit to use */
  144. while (!(delta & 1023) && unit[1]) {
  145. delta >>= 10;
  146. unit++;
  147. }
  148. seq_printf(st->seq, "%9lu%c", delta, *unit);
  149. }
  150. static void note_page(struct pg_state *st, unsigned long addr,
  151. unsigned int level, u64 val)
  152. {
  153. u64 flag = val & pg_level[level].mask;
  154. u64 pa = val & PTE_RPN_MASK;
  155. /* At first no level is set */
  156. if (!st->level) {
  157. st->level = level;
  158. st->current_flags = flag;
  159. st->start_address = addr;
  160. st->start_pa = pa;
  161. st->last_pa = pa;
  162. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  163. /*
  164. * Dump the section of virtual memory when:
  165. * - the PTE flags from one entry to the next differs.
  166. * - we change levels in the tree.
  167. * - the address is in a different section of memory and is thus
  168. * used for a different purpose, regardless of the flags.
  169. * - the pa of this page is not adjacent to the last inspected page
  170. */
  171. } else if (flag != st->current_flags || level != st->level ||
  172. addr >= st->marker[1].start_address ||
  173. pa != st->last_pa + PAGE_SIZE) {
  174. /* Check the PTE flags */
  175. if (st->current_flags) {
  176. dump_addr(st, addr);
  177. /* Dump all the flags */
  178. if (pg_level[st->level].flag)
  179. dump_flag_info(st, pg_level[st->level].flag,
  180. st->current_flags,
  181. pg_level[st->level].num);
  182. seq_putc(st->seq, '\n');
  183. }
  184. /*
  185. * Address indicates we have passed the end of the
  186. * current section of virtual memory
  187. */
  188. while (addr >= st->marker[1].start_address) {
  189. st->marker++;
  190. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  191. }
  192. st->start_address = addr;
  193. st->start_pa = pa;
  194. st->last_pa = pa;
  195. st->current_flags = flag;
  196. st->level = level;
  197. } else {
  198. st->last_pa = pa;
  199. }
  200. }
  201. static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
  202. {
  203. pte_t *pte = pte_offset_kernel(pmd, 0);
  204. unsigned long addr;
  205. unsigned int i;
  206. for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
  207. addr = start + i * PAGE_SIZE;
  208. note_page(st, addr, 4, pte_val(*pte));
  209. }
  210. }
  211. static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
  212. {
  213. pmd_t *pmd = pmd_offset(pud, 0);
  214. unsigned long addr;
  215. unsigned int i;
  216. for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
  217. addr = start + i * PMD_SIZE;
  218. if (!pmd_none(*pmd) && !pmd_huge(*pmd))
  219. /* pmd exists */
  220. walk_pte(st, pmd, addr);
  221. else
  222. note_page(st, addr, 3, pmd_val(*pmd));
  223. }
  224. }
  225. static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
  226. {
  227. pud_t *pud = pud_offset(pgd, 0);
  228. unsigned long addr;
  229. unsigned int i;
  230. for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
  231. addr = start + i * PUD_SIZE;
  232. if (!pud_none(*pud) && !pud_huge(*pud))
  233. /* pud exists */
  234. walk_pmd(st, pud, addr);
  235. else
  236. note_page(st, addr, 2, pud_val(*pud));
  237. }
  238. }
  239. static void walk_pagetables(struct pg_state *st)
  240. {
  241. pgd_t *pgd = pgd_offset_k(0UL);
  242. unsigned int i;
  243. unsigned long addr;
  244. addr = st->start_address;
  245. /*
  246. * Traverse the linux pagetable structure and dump pages that are in
  247. * the hash pagetable.
  248. */
  249. for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
  250. if (!pgd_none(*pgd) && !pgd_huge(*pgd))
  251. /* pgd exists */
  252. walk_pud(st, pgd, addr);
  253. else
  254. note_page(st, addr, 1, pgd_val(*pgd));
  255. }
  256. }
  257. static void populate_markers(void)
  258. {
  259. int i = 0;
  260. address_markers[i++].start_address = PAGE_OFFSET;
  261. address_markers[i++].start_address = VMALLOC_START;
  262. address_markers[i++].start_address = VMALLOC_END;
  263. #ifdef CONFIG_PPC64
  264. address_markers[i++].start_address = ISA_IO_BASE;
  265. address_markers[i++].start_address = ISA_IO_END;
  266. address_markers[i++].start_address = PHB_IO_BASE;
  267. address_markers[i++].start_address = PHB_IO_END;
  268. address_markers[i++].start_address = IOREMAP_BASE;
  269. address_markers[i++].start_address = IOREMAP_END;
  270. #ifdef CONFIG_PPC_BOOK3S_64
  271. address_markers[i++].start_address = H_VMEMMAP_BASE;
  272. #else
  273. address_markers[i++].start_address = VMEMMAP_BASE;
  274. #endif
  275. #else /* !CONFIG_PPC64 */
  276. address_markers[i++].start_address = ioremap_bot;
  277. address_markers[i++].start_address = IOREMAP_TOP;
  278. #ifdef CONFIG_NOT_COHERENT_CACHE
  279. address_markers[i++].start_address = IOREMAP_TOP;
  280. address_markers[i++].start_address = IOREMAP_TOP +
  281. CONFIG_CONSISTENT_SIZE;
  282. #endif
  283. #ifdef CONFIG_HIGHMEM
  284. address_markers[i++].start_address = PKMAP_BASE;
  285. address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
  286. #endif
  287. address_markers[i++].start_address = FIXADDR_START;
  288. address_markers[i++].start_address = FIXADDR_TOP;
  289. #endif /* CONFIG_PPC64 */
  290. }
  291. static int ptdump_show(struct seq_file *m, void *v)
  292. {
  293. struct pg_state st = {
  294. .seq = m,
  295. .marker = address_markers,
  296. };
  297. if (radix_enabled())
  298. st.start_address = PAGE_OFFSET;
  299. else
  300. st.start_address = KERN_VIRT_START;
  301. /* Traverse kernel page tables */
  302. walk_pagetables(&st);
  303. note_page(&st, 0, 0, 0);
  304. return 0;
  305. }
  306. static int ptdump_open(struct inode *inode, struct file *file)
  307. {
  308. return single_open(file, ptdump_show, NULL);
  309. }
  310. static const struct file_operations ptdump_fops = {
  311. .open = ptdump_open,
  312. .read = seq_read,
  313. .llseek = seq_lseek,
  314. .release = single_release,
  315. };
  316. static void build_pgtable_complete_mask(void)
  317. {
  318. unsigned int i, j;
  319. for (i = 0; i < ARRAY_SIZE(pg_level); i++)
  320. if (pg_level[i].flag)
  321. for (j = 0; j < pg_level[i].num; j++)
  322. pg_level[i].mask |= pg_level[i].flag[j].mask;
  323. }
  324. static int ptdump_init(void)
  325. {
  326. struct dentry *debugfs_file;
  327. populate_markers();
  328. build_pgtable_complete_mask();
  329. debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
  330. NULL, &ptdump_fops);
  331. return debugfs_file ? 0 : -ENOMEM;
  332. }
  333. device_initcall(ptdump_init);