dump_linuxpagetables.c 12 KB

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