dump_linuxpagetables.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/io.h>
  20. #include <linux/mm.h>
  21. #include <linux/sched.h>
  22. #include <linux/seq_file.h>
  23. #include <asm/fixmap.h>
  24. #include <asm/pgtable.h>
  25. #include <linux/const.h>
  26. #include <asm/page.h>
  27. #include <asm/pgalloc.h>
  28. /*
  29. * To visualise what is happening,
  30. *
  31. * - PTRS_PER_P** = how many entries there are in the corresponding P**
  32. * - P**_SHIFT = how many bits of the address we use to index into the
  33. * corresponding P**
  34. * - P**_SIZE is how much memory we can access through the table - not the
  35. * size of the table itself.
  36. * P**={PGD, PUD, PMD, PTE}
  37. *
  38. *
  39. * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
  40. * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
  41. * a page.
  42. *
  43. * In the case where there are only 3 levels, the PUD is folded into the
  44. * PGD: every PUD has only one entry which points to the PMD.
  45. *
  46. * The page dumper groups page table entries of the same type into a single
  47. * description. It uses pg_state to track the range information while
  48. * iterating over the PTE entries. When the continuity is broken it then
  49. * dumps out a description of the range - ie PTEs that are virtually contiguous
  50. * with the same PTE flags are chunked together. This is to make it clear how
  51. * different areas of the kernel virtual memory are used.
  52. *
  53. */
  54. struct pg_state {
  55. struct seq_file *seq;
  56. const struct addr_marker *marker;
  57. unsigned long start_address;
  58. unsigned int level;
  59. u64 current_flags;
  60. };
  61. struct addr_marker {
  62. unsigned long start_address;
  63. const char *name;
  64. };
  65. static struct addr_marker address_markers[] = {
  66. { 0, "Start of kernel VM" },
  67. { 0, "vmalloc() Area" },
  68. { 0, "vmalloc() End" },
  69. { 0, "isa I/O start" },
  70. { 0, "isa I/O end" },
  71. { 0, "phb I/O start" },
  72. { 0, "phb I/O end" },
  73. { 0, "I/O remap start" },
  74. { 0, "I/O remap end" },
  75. { 0, "vmemmap start" },
  76. { -1, NULL },
  77. };
  78. struct flag_info {
  79. u64 mask;
  80. u64 val;
  81. const char *set;
  82. const char *clear;
  83. bool is_val;
  84. int shift;
  85. };
  86. static const struct flag_info flag_array[] = {
  87. {
  88. #ifdef CONFIG_PPC_STD_MMU_64
  89. .mask = _PAGE_PRIVILEGED,
  90. .val = 0,
  91. #else
  92. .mask = _PAGE_USER,
  93. .val = _PAGE_USER,
  94. #endif
  95. .set = "user",
  96. .clear = " ",
  97. }, {
  98. .mask = _PAGE_RW,
  99. .val = _PAGE_RW,
  100. .set = "rw",
  101. .clear = "ro",
  102. }, {
  103. .mask = _PAGE_EXEC,
  104. .val = _PAGE_EXEC,
  105. .set = " X ",
  106. .clear = " ",
  107. }, {
  108. .mask = _PAGE_PTE,
  109. .val = _PAGE_PTE,
  110. .set = "pte",
  111. .clear = " ",
  112. }, {
  113. .mask = _PAGE_PRESENT,
  114. .val = _PAGE_PRESENT,
  115. .set = "present",
  116. .clear = " ",
  117. }, {
  118. #ifdef CONFIG_PPC_STD_MMU_64
  119. .mask = H_PAGE_HASHPTE,
  120. .val = H_PAGE_HASHPTE,
  121. #else
  122. .mask = _PAGE_HASHPTE,
  123. .val = _PAGE_HASHPTE,
  124. #endif
  125. .set = "hpte",
  126. .clear = " ",
  127. }, {
  128. #ifndef CONFIG_PPC_STD_MMU_64
  129. .mask = _PAGE_GUARDED,
  130. .val = _PAGE_GUARDED,
  131. .set = "guarded",
  132. .clear = " ",
  133. }, {
  134. #endif
  135. .mask = _PAGE_DIRTY,
  136. .val = _PAGE_DIRTY,
  137. .set = "dirty",
  138. .clear = " ",
  139. }, {
  140. .mask = _PAGE_ACCESSED,
  141. .val = _PAGE_ACCESSED,
  142. .set = "accessed",
  143. .clear = " ",
  144. }, {
  145. #ifndef CONFIG_PPC_STD_MMU_64
  146. .mask = _PAGE_WRITETHRU,
  147. .val = _PAGE_WRITETHRU,
  148. .set = "write through",
  149. .clear = " ",
  150. }, {
  151. #endif
  152. .mask = _PAGE_NO_CACHE,
  153. .val = _PAGE_NO_CACHE,
  154. .set = "no cache",
  155. .clear = " ",
  156. }, {
  157. #ifdef CONFIG_PPC_BOOK3S_64
  158. .mask = H_PAGE_BUSY,
  159. .val = H_PAGE_BUSY,
  160. .set = "busy",
  161. }, {
  162. #ifdef CONFIG_PPC_64K_PAGES
  163. .mask = H_PAGE_COMBO,
  164. .val = H_PAGE_COMBO,
  165. .set = "combo",
  166. }, {
  167. .mask = H_PAGE_4K_PFN,
  168. .val = H_PAGE_4K_PFN,
  169. .set = "4K_pfn",
  170. }, {
  171. #endif
  172. .mask = H_PAGE_F_GIX,
  173. .val = H_PAGE_F_GIX,
  174. .set = "f_gix",
  175. .is_val = true,
  176. .shift = H_PAGE_F_GIX_SHIFT,
  177. }, {
  178. .mask = H_PAGE_F_SECOND,
  179. .val = H_PAGE_F_SECOND,
  180. .set = "f_second",
  181. }, {
  182. #endif
  183. .mask = _PAGE_SPECIAL,
  184. .val = _PAGE_SPECIAL,
  185. .set = "special",
  186. }
  187. };
  188. struct pgtable_level {
  189. const struct flag_info *flag;
  190. size_t num;
  191. u64 mask;
  192. };
  193. static struct pgtable_level pg_level[] = {
  194. {
  195. }, { /* pgd */
  196. .flag = flag_array,
  197. .num = ARRAY_SIZE(flag_array),
  198. }, { /* pud */
  199. .flag = flag_array,
  200. .num = ARRAY_SIZE(flag_array),
  201. }, { /* pmd */
  202. .flag = flag_array,
  203. .num = ARRAY_SIZE(flag_array),
  204. }, { /* pte */
  205. .flag = flag_array,
  206. .num = ARRAY_SIZE(flag_array),
  207. },
  208. };
  209. static void dump_flag_info(struct pg_state *st, const struct flag_info
  210. *flag, u64 pte, int num)
  211. {
  212. unsigned int i;
  213. for (i = 0; i < num; i++, flag++) {
  214. const char *s = NULL;
  215. u64 val;
  216. /* flag not defined so don't check it */
  217. if (flag->mask == 0)
  218. continue;
  219. /* Some 'flags' are actually values */
  220. if (flag->is_val) {
  221. val = pte & flag->val;
  222. if (flag->shift)
  223. val = val >> flag->shift;
  224. seq_printf(st->seq, " %s:%llx", flag->set, val);
  225. } else {
  226. if ((pte & flag->mask) == flag->val)
  227. s = flag->set;
  228. else
  229. s = flag->clear;
  230. if (s)
  231. seq_printf(st->seq, " %s", s);
  232. }
  233. st->current_flags &= ~flag->mask;
  234. }
  235. if (st->current_flags != 0)
  236. seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
  237. }
  238. static void dump_addr(struct pg_state *st, unsigned long addr)
  239. {
  240. static const char units[] = "KMGTPE";
  241. const char *unit = units;
  242. unsigned long delta;
  243. seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
  244. delta = (addr - st->start_address) >> 10;
  245. /* Work out what appropriate unit to use */
  246. while (!(delta & 1023) && unit[1]) {
  247. delta >>= 10;
  248. unit++;
  249. }
  250. seq_printf(st->seq, "%9lu%c", delta, *unit);
  251. }
  252. static void note_page(struct pg_state *st, unsigned long addr,
  253. unsigned int level, u64 val)
  254. {
  255. u64 flag = val & pg_level[level].mask;
  256. /* At first no level is set */
  257. if (!st->level) {
  258. st->level = level;
  259. st->current_flags = flag;
  260. st->start_address = addr;
  261. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  262. /*
  263. * Dump the section of virtual memory when:
  264. * - the PTE flags from one entry to the next differs.
  265. * - we change levels in the tree.
  266. * - the address is in a different section of memory and is thus
  267. * used for a different purpose, regardless of the flags.
  268. */
  269. } else if (flag != st->current_flags || level != st->level ||
  270. addr >= st->marker[1].start_address) {
  271. /* Check the PTE flags */
  272. if (st->current_flags) {
  273. dump_addr(st, addr);
  274. /* Dump all the flags */
  275. if (pg_level[st->level].flag)
  276. dump_flag_info(st, pg_level[st->level].flag,
  277. st->current_flags,
  278. pg_level[st->level].num);
  279. seq_puts(st->seq, "\n");
  280. }
  281. /*
  282. * Address indicates we have passed the end of the
  283. * current section of virtual memory
  284. */
  285. while (addr >= st->marker[1].start_address) {
  286. st->marker++;
  287. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  288. }
  289. st->start_address = addr;
  290. st->current_flags = flag;
  291. st->level = level;
  292. }
  293. }
  294. static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
  295. {
  296. pte_t *pte = pte_offset_kernel(pmd, 0);
  297. unsigned long addr;
  298. unsigned int i;
  299. for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
  300. addr = start + i * PAGE_SIZE;
  301. note_page(st, addr, 4, pte_val(*pte));
  302. }
  303. }
  304. static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
  305. {
  306. pmd_t *pmd = pmd_offset(pud, 0);
  307. unsigned long addr;
  308. unsigned int i;
  309. for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
  310. addr = start + i * PMD_SIZE;
  311. if (!pmd_none(*pmd))
  312. /* pmd exists */
  313. walk_pte(st, pmd, addr);
  314. else
  315. note_page(st, addr, 3, pmd_val(*pmd));
  316. }
  317. }
  318. static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
  319. {
  320. pud_t *pud = pud_offset(pgd, 0);
  321. unsigned long addr;
  322. unsigned int i;
  323. for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
  324. addr = start + i * PUD_SIZE;
  325. if (!pud_none(*pud))
  326. /* pud exists */
  327. walk_pmd(st, pud, addr);
  328. else
  329. note_page(st, addr, 2, pud_val(*pud));
  330. }
  331. }
  332. static void walk_pagetables(struct pg_state *st)
  333. {
  334. pgd_t *pgd = pgd_offset_k(0UL);
  335. unsigned int i;
  336. unsigned long addr;
  337. /*
  338. * Traverse the linux pagetable structure and dump pages that are in
  339. * the hash pagetable.
  340. */
  341. for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
  342. addr = KERN_VIRT_START + i * PGDIR_SIZE;
  343. if (!pgd_none(*pgd))
  344. /* pgd exists */
  345. walk_pud(st, pgd, addr);
  346. else
  347. note_page(st, addr, 1, pgd_val(*pgd));
  348. }
  349. }
  350. static void populate_markers(void)
  351. {
  352. address_markers[0].start_address = PAGE_OFFSET;
  353. address_markers[1].start_address = VMALLOC_START;
  354. address_markers[2].start_address = VMALLOC_END;
  355. address_markers[3].start_address = ISA_IO_BASE;
  356. address_markers[4].start_address = ISA_IO_END;
  357. address_markers[5].start_address = PHB_IO_BASE;
  358. address_markers[6].start_address = PHB_IO_END;
  359. address_markers[7].start_address = IOREMAP_BASE;
  360. address_markers[8].start_address = IOREMAP_END;
  361. #ifdef CONFIG_PPC_STD_MMU_64
  362. address_markers[9].start_address = H_VMEMMAP_BASE;
  363. #else
  364. address_markers[9].start_address = VMEMMAP_BASE;
  365. #endif
  366. }
  367. static int ptdump_show(struct seq_file *m, void *v)
  368. {
  369. struct pg_state st = {
  370. .seq = m,
  371. .start_address = KERN_VIRT_START,
  372. .marker = address_markers,
  373. };
  374. /* Traverse kernel page tables */
  375. walk_pagetables(&st);
  376. note_page(&st, 0, 0, 0);
  377. return 0;
  378. }
  379. static int ptdump_open(struct inode *inode, struct file *file)
  380. {
  381. return single_open(file, ptdump_show, NULL);
  382. }
  383. static const struct file_operations ptdump_fops = {
  384. .open = ptdump_open,
  385. .read = seq_read,
  386. .llseek = seq_lseek,
  387. .release = single_release,
  388. };
  389. static void build_pgtable_complete_mask(void)
  390. {
  391. unsigned int i, j;
  392. for (i = 0; i < ARRAY_SIZE(pg_level); i++)
  393. if (pg_level[i].flag)
  394. for (j = 0; j < pg_level[i].num; j++)
  395. pg_level[i].mask |= pg_level[i].flag[j].mask;
  396. }
  397. static int ptdump_init(void)
  398. {
  399. struct dentry *debugfs_file;
  400. populate_markers();
  401. build_pgtable_complete_mask();
  402. debugfs_file = debugfs_create_file("kernel_pagetables", 0400, NULL,
  403. NULL, &ptdump_fops);
  404. return debugfs_file ? 0 : -ENOMEM;
  405. }
  406. device_initcall(ptdump_init);