dump_linuxpagetables.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. .mask = _PAGE_USER | _PAGE_PRIVILEGED,
  110. .val = _PAGE_USER,
  111. .set = "user",
  112. .clear = " ",
  113. }, {
  114. .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
  115. .val = _PAGE_RW,
  116. .set = "rw",
  117. }, {
  118. .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
  119. .val = _PAGE_RO,
  120. .set = "ro",
  121. }, {
  122. #if _PAGE_NA != 0
  123. .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
  124. .val = _PAGE_RO,
  125. .set = "na",
  126. }, {
  127. #endif
  128. .mask = _PAGE_EXEC,
  129. .val = _PAGE_EXEC,
  130. .set = " X ",
  131. .clear = " ",
  132. }, {
  133. .mask = _PAGE_PTE,
  134. .val = _PAGE_PTE,
  135. .set = "pte",
  136. .clear = " ",
  137. }, {
  138. .mask = _PAGE_PRESENT,
  139. .val = _PAGE_PRESENT,
  140. .set = "present",
  141. .clear = " ",
  142. }, {
  143. #ifdef CONFIG_PPC_BOOK3S_64
  144. .mask = H_PAGE_HASHPTE,
  145. .val = H_PAGE_HASHPTE,
  146. #else
  147. .mask = _PAGE_HASHPTE,
  148. .val = _PAGE_HASHPTE,
  149. #endif
  150. .set = "hpte",
  151. .clear = " ",
  152. }, {
  153. #ifndef CONFIG_PPC_BOOK3S_64
  154. .mask = _PAGE_GUARDED,
  155. .val = _PAGE_GUARDED,
  156. .set = "guarded",
  157. .clear = " ",
  158. }, {
  159. #endif
  160. .mask = _PAGE_DIRTY,
  161. .val = _PAGE_DIRTY,
  162. .set = "dirty",
  163. .clear = " ",
  164. }, {
  165. .mask = _PAGE_ACCESSED,
  166. .val = _PAGE_ACCESSED,
  167. .set = "accessed",
  168. .clear = " ",
  169. }, {
  170. #ifndef CONFIG_PPC_BOOK3S_64
  171. .mask = _PAGE_WRITETHRU,
  172. .val = _PAGE_WRITETHRU,
  173. .set = "write through",
  174. .clear = " ",
  175. }, {
  176. #endif
  177. #ifndef CONFIG_PPC_BOOK3S_64
  178. .mask = _PAGE_NO_CACHE,
  179. .val = _PAGE_NO_CACHE,
  180. .set = "no cache",
  181. .clear = " ",
  182. }, {
  183. #else
  184. .mask = _PAGE_NON_IDEMPOTENT,
  185. .val = _PAGE_NON_IDEMPOTENT,
  186. .set = "non-idempotent",
  187. .clear = " ",
  188. }, {
  189. .mask = _PAGE_TOLERANT,
  190. .val = _PAGE_TOLERANT,
  191. .set = "tolerant",
  192. .clear = " ",
  193. }, {
  194. #endif
  195. #ifdef CONFIG_PPC_BOOK3S_64
  196. .mask = H_PAGE_BUSY,
  197. .val = H_PAGE_BUSY,
  198. .set = "busy",
  199. }, {
  200. #ifdef CONFIG_PPC_64K_PAGES
  201. .mask = H_PAGE_COMBO,
  202. .val = H_PAGE_COMBO,
  203. .set = "combo",
  204. }, {
  205. .mask = H_PAGE_4K_PFN,
  206. .val = H_PAGE_4K_PFN,
  207. .set = "4K_pfn",
  208. }, {
  209. #else /* CONFIG_PPC_64K_PAGES */
  210. .mask = H_PAGE_F_GIX,
  211. .val = H_PAGE_F_GIX,
  212. .set = "f_gix",
  213. .is_val = true,
  214. .shift = H_PAGE_F_GIX_SHIFT,
  215. }, {
  216. .mask = H_PAGE_F_SECOND,
  217. .val = H_PAGE_F_SECOND,
  218. .set = "f_second",
  219. }, {
  220. #endif /* CONFIG_PPC_64K_PAGES */
  221. #endif
  222. .mask = _PAGE_SPECIAL,
  223. .val = _PAGE_SPECIAL,
  224. .set = "special",
  225. }
  226. };
  227. struct pgtable_level {
  228. const struct flag_info *flag;
  229. size_t num;
  230. u64 mask;
  231. };
  232. static struct pgtable_level pg_level[] = {
  233. {
  234. }, { /* pgd */
  235. .flag = flag_array,
  236. .num = ARRAY_SIZE(flag_array),
  237. }, { /* pud */
  238. .flag = flag_array,
  239. .num = ARRAY_SIZE(flag_array),
  240. }, { /* pmd */
  241. .flag = flag_array,
  242. .num = ARRAY_SIZE(flag_array),
  243. }, { /* pte */
  244. .flag = flag_array,
  245. .num = ARRAY_SIZE(flag_array),
  246. },
  247. };
  248. static void dump_flag_info(struct pg_state *st, const struct flag_info
  249. *flag, u64 pte, int num)
  250. {
  251. unsigned int i;
  252. for (i = 0; i < num; i++, flag++) {
  253. const char *s = NULL;
  254. u64 val;
  255. /* flag not defined so don't check it */
  256. if (flag->mask == 0)
  257. continue;
  258. /* Some 'flags' are actually values */
  259. if (flag->is_val) {
  260. val = pte & flag->val;
  261. if (flag->shift)
  262. val = val >> flag->shift;
  263. seq_printf(st->seq, " %s:%llx", flag->set, val);
  264. } else {
  265. if ((pte & flag->mask) == flag->val)
  266. s = flag->set;
  267. else
  268. s = flag->clear;
  269. if (s)
  270. seq_printf(st->seq, " %s", s);
  271. }
  272. st->current_flags &= ~flag->mask;
  273. }
  274. if (st->current_flags != 0)
  275. seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
  276. }
  277. static void dump_addr(struct pg_state *st, unsigned long addr)
  278. {
  279. static const char units[] = "KMGTPE";
  280. const char *unit = units;
  281. unsigned long delta;
  282. #ifdef CONFIG_PPC64
  283. seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
  284. seq_printf(st->seq, "0x%016lx ", st->start_pa);
  285. #else
  286. seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
  287. seq_printf(st->seq, "0x%08lx ", st->start_pa);
  288. #endif
  289. delta = (addr - st->start_address) >> 10;
  290. /* Work out what appropriate unit to use */
  291. while (!(delta & 1023) && unit[1]) {
  292. delta >>= 10;
  293. unit++;
  294. }
  295. seq_printf(st->seq, "%9lu%c", delta, *unit);
  296. }
  297. static void note_page(struct pg_state *st, unsigned long addr,
  298. unsigned int level, u64 val)
  299. {
  300. u64 flag = val & pg_level[level].mask;
  301. u64 pa = val & PTE_RPN_MASK;
  302. /* At first no level is set */
  303. if (!st->level) {
  304. st->level = level;
  305. st->current_flags = flag;
  306. st->start_address = addr;
  307. st->start_pa = pa;
  308. st->last_pa = pa;
  309. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  310. /*
  311. * Dump the section of virtual memory when:
  312. * - the PTE flags from one entry to the next differs.
  313. * - we change levels in the tree.
  314. * - the address is in a different section of memory and is thus
  315. * used for a different purpose, regardless of the flags.
  316. * - the pa of this page is not adjacent to the last inspected page
  317. */
  318. } else if (flag != st->current_flags || level != st->level ||
  319. addr >= st->marker[1].start_address ||
  320. pa != st->last_pa + PAGE_SIZE) {
  321. /* Check the PTE flags */
  322. if (st->current_flags) {
  323. dump_addr(st, addr);
  324. /* Dump all the flags */
  325. if (pg_level[st->level].flag)
  326. dump_flag_info(st, pg_level[st->level].flag,
  327. st->current_flags,
  328. pg_level[st->level].num);
  329. seq_putc(st->seq, '\n');
  330. }
  331. /*
  332. * Address indicates we have passed the end of the
  333. * current section of virtual memory
  334. */
  335. while (addr >= st->marker[1].start_address) {
  336. st->marker++;
  337. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  338. }
  339. st->start_address = addr;
  340. st->start_pa = pa;
  341. st->last_pa = pa;
  342. st->current_flags = flag;
  343. st->level = level;
  344. } else {
  345. st->last_pa = pa;
  346. }
  347. }
  348. static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
  349. {
  350. pte_t *pte = pte_offset_kernel(pmd, 0);
  351. unsigned long addr;
  352. unsigned int i;
  353. for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
  354. addr = start + i * PAGE_SIZE;
  355. note_page(st, addr, 4, pte_val(*pte));
  356. }
  357. }
  358. static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
  359. {
  360. pmd_t *pmd = pmd_offset(pud, 0);
  361. unsigned long addr;
  362. unsigned int i;
  363. for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
  364. addr = start + i * PMD_SIZE;
  365. if (!pmd_none(*pmd) && !pmd_huge(*pmd))
  366. /* pmd exists */
  367. walk_pte(st, pmd, addr);
  368. else
  369. note_page(st, addr, 3, pmd_val(*pmd));
  370. }
  371. }
  372. static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
  373. {
  374. pud_t *pud = pud_offset(pgd, 0);
  375. unsigned long addr;
  376. unsigned int i;
  377. for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
  378. addr = start + i * PUD_SIZE;
  379. if (!pud_none(*pud) && !pud_huge(*pud))
  380. /* pud exists */
  381. walk_pmd(st, pud, addr);
  382. else
  383. note_page(st, addr, 2, pud_val(*pud));
  384. }
  385. }
  386. static void walk_pagetables(struct pg_state *st)
  387. {
  388. pgd_t *pgd = pgd_offset_k(0UL);
  389. unsigned int i;
  390. unsigned long addr;
  391. /*
  392. * Traverse the linux pagetable structure and dump pages that are in
  393. * the hash pagetable.
  394. */
  395. for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
  396. addr = KERN_VIRT_START + i * PGDIR_SIZE;
  397. if (!pgd_none(*pgd) && !pgd_huge(*pgd))
  398. /* pgd exists */
  399. walk_pud(st, pgd, addr);
  400. else
  401. note_page(st, addr, 1, pgd_val(*pgd));
  402. }
  403. }
  404. static void populate_markers(void)
  405. {
  406. int i = 0;
  407. address_markers[i++].start_address = PAGE_OFFSET;
  408. address_markers[i++].start_address = VMALLOC_START;
  409. address_markers[i++].start_address = VMALLOC_END;
  410. #ifdef CONFIG_PPC64
  411. address_markers[i++].start_address = ISA_IO_BASE;
  412. address_markers[i++].start_address = ISA_IO_END;
  413. address_markers[i++].start_address = PHB_IO_BASE;
  414. address_markers[i++].start_address = PHB_IO_END;
  415. address_markers[i++].start_address = IOREMAP_BASE;
  416. address_markers[i++].start_address = IOREMAP_END;
  417. #ifdef CONFIG_PPC_BOOK3S_64
  418. address_markers[i++].start_address = H_VMEMMAP_BASE;
  419. #else
  420. address_markers[i++].start_address = VMEMMAP_BASE;
  421. #endif
  422. #else /* !CONFIG_PPC64 */
  423. address_markers[i++].start_address = ioremap_bot;
  424. address_markers[i++].start_address = IOREMAP_TOP;
  425. #ifdef CONFIG_NOT_COHERENT_CACHE
  426. address_markers[i++].start_address = IOREMAP_TOP;
  427. address_markers[i++].start_address = IOREMAP_TOP +
  428. CONFIG_CONSISTENT_SIZE;
  429. #endif
  430. #ifdef CONFIG_HIGHMEM
  431. address_markers[i++].start_address = PKMAP_BASE;
  432. address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
  433. #endif
  434. address_markers[i++].start_address = FIXADDR_START;
  435. address_markers[i++].start_address = FIXADDR_TOP;
  436. #endif /* CONFIG_PPC64 */
  437. }
  438. static int ptdump_show(struct seq_file *m, void *v)
  439. {
  440. struct pg_state st = {
  441. .seq = m,
  442. .start_address = KERN_VIRT_START,
  443. .marker = address_markers,
  444. };
  445. /* Traverse kernel page tables */
  446. walk_pagetables(&st);
  447. note_page(&st, 0, 0, 0);
  448. return 0;
  449. }
  450. static int ptdump_open(struct inode *inode, struct file *file)
  451. {
  452. return single_open(file, ptdump_show, NULL);
  453. }
  454. static const struct file_operations ptdump_fops = {
  455. .open = ptdump_open,
  456. .read = seq_read,
  457. .llseek = seq_lseek,
  458. .release = single_release,
  459. };
  460. static void build_pgtable_complete_mask(void)
  461. {
  462. unsigned int i, j;
  463. for (i = 0; i < ARRAY_SIZE(pg_level); i++)
  464. if (pg_level[i].flag)
  465. for (j = 0; j < pg_level[i].num; j++)
  466. pg_level[i].mask |= pg_level[i].flag[j].mask;
  467. }
  468. static int ptdump_init(void)
  469. {
  470. struct dentry *debugfs_file;
  471. populate_markers();
  472. build_pgtable_complete_mask();
  473. debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
  474. NULL, &ptdump_fops);
  475. return debugfs_file ? 0 : -ENOMEM;
  476. }
  477. device_initcall(ptdump_init);