dump_linuxpagetables.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. #ifdef CONFIG_PPC32
  31. #define KERN_VIRT_START 0
  32. #endif
  33. /*
  34. * To visualise what is happening,
  35. *
  36. * - PTRS_PER_P** = how many entries there are in the corresponding P**
  37. * - P**_SHIFT = how many bits of the address we use to index into the
  38. * corresponding P**
  39. * - P**_SIZE is how much memory we can access through the table - not the
  40. * size of the table itself.
  41. * P**={PGD, PUD, PMD, PTE}
  42. *
  43. *
  44. * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
  45. * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
  46. * a page.
  47. *
  48. * In the case where there are only 3 levels, the PUD is folded into the
  49. * PGD: every PUD has only one entry which points to the PMD.
  50. *
  51. * The page dumper groups page table entries of the same type into a single
  52. * description. It uses pg_state to track the range information while
  53. * iterating over the PTE entries. When the continuity is broken it then
  54. * dumps out a description of the range - ie PTEs that are virtually contiguous
  55. * with the same PTE flags are chunked together. This is to make it clear how
  56. * different areas of the kernel virtual memory are used.
  57. *
  58. */
  59. struct pg_state {
  60. struct seq_file *seq;
  61. const struct addr_marker *marker;
  62. unsigned long start_address;
  63. unsigned long start_pa;
  64. unsigned long last_pa;
  65. unsigned int level;
  66. u64 current_flags;
  67. };
  68. struct addr_marker {
  69. unsigned long start_address;
  70. const char *name;
  71. };
  72. static struct addr_marker address_markers[] = {
  73. { 0, "Start of kernel VM" },
  74. { 0, "vmalloc() Area" },
  75. { 0, "vmalloc() End" },
  76. #ifdef CONFIG_PPC64
  77. { 0, "isa I/O start" },
  78. { 0, "isa I/O end" },
  79. { 0, "phb I/O start" },
  80. { 0, "phb I/O end" },
  81. { 0, "I/O remap start" },
  82. { 0, "I/O remap end" },
  83. { 0, "vmemmap start" },
  84. #else
  85. { 0, "Early I/O remap start" },
  86. { 0, "Early I/O remap end" },
  87. #ifdef CONFIG_NOT_COHERENT_CACHE
  88. { 0, "Consistent mem start" },
  89. { 0, "Consistent mem end" },
  90. #endif
  91. #ifdef CONFIG_HIGHMEM
  92. { 0, "Highmem PTEs start" },
  93. { 0, "Highmem PTEs end" },
  94. #endif
  95. { 0, "Fixmap start" },
  96. { 0, "Fixmap end" },
  97. #endif
  98. { -1, NULL },
  99. };
  100. struct flag_info {
  101. u64 mask;
  102. u64 val;
  103. const char *set;
  104. const char *clear;
  105. bool is_val;
  106. int shift;
  107. };
  108. static const struct flag_info flag_array[] = {
  109. {
  110. .mask = _PAGE_USER | _PAGE_PRIVILEGED,
  111. .val = _PAGE_USER,
  112. .set = "user",
  113. .clear = " ",
  114. }, {
  115. .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
  116. .val = _PAGE_RW,
  117. .set = "rw",
  118. }, {
  119. .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
  120. .val = _PAGE_RO,
  121. .set = "ro",
  122. }, {
  123. #if _PAGE_NA != 0
  124. .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
  125. .val = _PAGE_RO,
  126. .set = "na",
  127. }, {
  128. #endif
  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_BOOK3S_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_BOOK3S_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_BOOK3S_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. #else /* CONFIG_PPC_64K_PAGES */
  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 /* CONFIG_PPC_64K_PAGES */
  222. #endif
  223. .mask = _PAGE_SPECIAL,
  224. .val = _PAGE_SPECIAL,
  225. .set = "special",
  226. }
  227. };
  228. struct pgtable_level {
  229. const struct flag_info *flag;
  230. size_t num;
  231. u64 mask;
  232. };
  233. static struct pgtable_level pg_level[] = {
  234. {
  235. }, { /* pgd */
  236. .flag = flag_array,
  237. .num = ARRAY_SIZE(flag_array),
  238. }, { /* pud */
  239. .flag = flag_array,
  240. .num = ARRAY_SIZE(flag_array),
  241. }, { /* pmd */
  242. .flag = flag_array,
  243. .num = ARRAY_SIZE(flag_array),
  244. }, { /* pte */
  245. .flag = flag_array,
  246. .num = ARRAY_SIZE(flag_array),
  247. },
  248. };
  249. static void dump_flag_info(struct pg_state *st, const struct flag_info
  250. *flag, u64 pte, int num)
  251. {
  252. unsigned int i;
  253. for (i = 0; i < num; i++, flag++) {
  254. const char *s = NULL;
  255. u64 val;
  256. /* flag not defined so don't check it */
  257. if (flag->mask == 0)
  258. continue;
  259. /* Some 'flags' are actually values */
  260. if (flag->is_val) {
  261. val = pte & flag->val;
  262. if (flag->shift)
  263. val = val >> flag->shift;
  264. seq_printf(st->seq, " %s:%llx", flag->set, val);
  265. } else {
  266. if ((pte & flag->mask) == flag->val)
  267. s = flag->set;
  268. else
  269. s = flag->clear;
  270. if (s)
  271. seq_printf(st->seq, " %s", s);
  272. }
  273. st->current_flags &= ~flag->mask;
  274. }
  275. if (st->current_flags != 0)
  276. seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
  277. }
  278. static void dump_addr(struct pg_state *st, unsigned long addr)
  279. {
  280. static const char units[] = "KMGTPE";
  281. const char *unit = units;
  282. unsigned long delta;
  283. #ifdef CONFIG_PPC64
  284. seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
  285. seq_printf(st->seq, "0x%016lx ", st->start_pa);
  286. #else
  287. seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
  288. seq_printf(st->seq, "0x%08lx ", st->start_pa);
  289. #endif
  290. delta = (addr - st->start_address) >> 10;
  291. /* Work out what appropriate unit to use */
  292. while (!(delta & 1023) && unit[1]) {
  293. delta >>= 10;
  294. unit++;
  295. }
  296. seq_printf(st->seq, "%9lu%c", delta, *unit);
  297. }
  298. static void note_page(struct pg_state *st, unsigned long addr,
  299. unsigned int level, u64 val)
  300. {
  301. u64 flag = val & pg_level[level].mask;
  302. u64 pa = val & PTE_RPN_MASK;
  303. /* At first no level is set */
  304. if (!st->level) {
  305. st->level = level;
  306. st->current_flags = flag;
  307. st->start_address = addr;
  308. st->start_pa = pa;
  309. st->last_pa = pa;
  310. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  311. /*
  312. * Dump the section of virtual memory when:
  313. * - the PTE flags from one entry to the next differs.
  314. * - we change levels in the tree.
  315. * - the address is in a different section of memory and is thus
  316. * used for a different purpose, regardless of the flags.
  317. * - the pa of this page is not adjacent to the last inspected page
  318. */
  319. } else if (flag != st->current_flags || level != st->level ||
  320. addr >= st->marker[1].start_address ||
  321. pa != st->last_pa + PAGE_SIZE) {
  322. /* Check the PTE flags */
  323. if (st->current_flags) {
  324. dump_addr(st, addr);
  325. /* Dump all the flags */
  326. if (pg_level[st->level].flag)
  327. dump_flag_info(st, pg_level[st->level].flag,
  328. st->current_flags,
  329. pg_level[st->level].num);
  330. seq_putc(st->seq, '\n');
  331. }
  332. /*
  333. * Address indicates we have passed the end of the
  334. * current section of virtual memory
  335. */
  336. while (addr >= st->marker[1].start_address) {
  337. st->marker++;
  338. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  339. }
  340. st->start_address = addr;
  341. st->start_pa = pa;
  342. st->last_pa = pa;
  343. st->current_flags = flag;
  344. st->level = level;
  345. } else {
  346. st->last_pa = pa;
  347. }
  348. }
  349. static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
  350. {
  351. pte_t *pte = pte_offset_kernel(pmd, 0);
  352. unsigned long addr;
  353. unsigned int i;
  354. for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
  355. addr = start + i * PAGE_SIZE;
  356. note_page(st, addr, 4, pte_val(*pte));
  357. }
  358. }
  359. static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
  360. {
  361. pmd_t *pmd = pmd_offset(pud, 0);
  362. unsigned long addr;
  363. unsigned int i;
  364. for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
  365. addr = start + i * PMD_SIZE;
  366. if (!pmd_none(*pmd) && !pmd_huge(*pmd))
  367. /* pmd exists */
  368. walk_pte(st, pmd, addr);
  369. else
  370. note_page(st, addr, 3, pmd_val(*pmd));
  371. }
  372. }
  373. static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
  374. {
  375. pud_t *pud = pud_offset(pgd, 0);
  376. unsigned long addr;
  377. unsigned int i;
  378. for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
  379. addr = start + i * PUD_SIZE;
  380. if (!pud_none(*pud) && !pud_huge(*pud))
  381. /* pud exists */
  382. walk_pmd(st, pud, addr);
  383. else
  384. note_page(st, addr, 2, pud_val(*pud));
  385. }
  386. }
  387. static void walk_pagetables(struct pg_state *st)
  388. {
  389. pgd_t *pgd = pgd_offset_k(0UL);
  390. unsigned int i;
  391. unsigned long addr;
  392. addr = st->start_address;
  393. /*
  394. * Traverse the linux pagetable structure and dump pages that are in
  395. * the hash pagetable.
  396. */
  397. for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
  398. if (!pgd_none(*pgd) && !pgd_huge(*pgd))
  399. /* pgd exists */
  400. walk_pud(st, pgd, addr);
  401. else
  402. note_page(st, addr, 1, pgd_val(*pgd));
  403. }
  404. }
  405. static void populate_markers(void)
  406. {
  407. int i = 0;
  408. address_markers[i++].start_address = PAGE_OFFSET;
  409. address_markers[i++].start_address = VMALLOC_START;
  410. address_markers[i++].start_address = VMALLOC_END;
  411. #ifdef CONFIG_PPC64
  412. address_markers[i++].start_address = ISA_IO_BASE;
  413. address_markers[i++].start_address = ISA_IO_END;
  414. address_markers[i++].start_address = PHB_IO_BASE;
  415. address_markers[i++].start_address = PHB_IO_END;
  416. address_markers[i++].start_address = IOREMAP_BASE;
  417. address_markers[i++].start_address = IOREMAP_END;
  418. #ifdef CONFIG_PPC_BOOK3S_64
  419. address_markers[i++].start_address = H_VMEMMAP_BASE;
  420. #else
  421. address_markers[i++].start_address = VMEMMAP_BASE;
  422. #endif
  423. #else /* !CONFIG_PPC64 */
  424. address_markers[i++].start_address = ioremap_bot;
  425. address_markers[i++].start_address = IOREMAP_TOP;
  426. #ifdef CONFIG_NOT_COHERENT_CACHE
  427. address_markers[i++].start_address = IOREMAP_TOP;
  428. address_markers[i++].start_address = IOREMAP_TOP +
  429. CONFIG_CONSISTENT_SIZE;
  430. #endif
  431. #ifdef CONFIG_HIGHMEM
  432. address_markers[i++].start_address = PKMAP_BASE;
  433. address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
  434. #endif
  435. address_markers[i++].start_address = FIXADDR_START;
  436. address_markers[i++].start_address = FIXADDR_TOP;
  437. #endif /* CONFIG_PPC64 */
  438. }
  439. static int ptdump_show(struct seq_file *m, void *v)
  440. {
  441. struct pg_state st = {
  442. .seq = m,
  443. .marker = address_markers,
  444. };
  445. if (radix_enabled())
  446. st.start_address = PAGE_OFFSET;
  447. else
  448. st.start_address = KERN_VIRT_START;
  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);