hashpagetable.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2016, Rashmica Gupta, IBM Corp.
  4. *
  5. * This traverses the kernel virtual memory and dumps the pages that are in
  6. * the hash pagetable, along with their flags to
  7. * /sys/kernel/debug/kernel_hash_pagetable.
  8. *
  9. * If radix is enabled then there is no hash page table and so no debugfs file
  10. * is generated.
  11. */
  12. #include <linux/debugfs.h>
  13. #include <linux/fs.h>
  14. #include <linux/io.h>
  15. #include <linux/mm.h>
  16. #include <linux/sched.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/const.h>
  19. #include <asm/page.h>
  20. #include <asm/plpar_wrappers.h>
  21. #include <linux/memblock.h>
  22. #include <asm/firmware.h>
  23. #include <asm/pgalloc.h>
  24. struct pg_state {
  25. struct seq_file *seq;
  26. const struct addr_marker *marker;
  27. unsigned long start_address;
  28. unsigned int level;
  29. u64 current_flags;
  30. };
  31. struct addr_marker {
  32. unsigned long start_address;
  33. const char *name;
  34. };
  35. static struct addr_marker address_markers[] = {
  36. { 0, "Start of kernel VM" },
  37. { 0, "vmalloc() Area" },
  38. { 0, "vmalloc() End" },
  39. { 0, "isa I/O start" },
  40. { 0, "isa I/O end" },
  41. { 0, "phb I/O start" },
  42. { 0, "phb I/O end" },
  43. { 0, "I/O remap start" },
  44. { 0, "I/O remap end" },
  45. { 0, "vmemmap start" },
  46. { -1, NULL },
  47. };
  48. struct flag_info {
  49. u64 mask;
  50. u64 val;
  51. const char *set;
  52. const char *clear;
  53. bool is_val;
  54. int shift;
  55. };
  56. static const struct flag_info v_flag_array[] = {
  57. {
  58. .mask = SLB_VSID_B,
  59. .val = SLB_VSID_B_256M,
  60. .set = "ssize: 256M",
  61. .clear = "ssize: 1T ",
  62. }, {
  63. .mask = HPTE_V_SECONDARY,
  64. .val = HPTE_V_SECONDARY,
  65. .set = "secondary",
  66. .clear = "primary ",
  67. }, {
  68. .mask = HPTE_V_VALID,
  69. .val = HPTE_V_VALID,
  70. .set = "valid ",
  71. .clear = "invalid",
  72. }, {
  73. .mask = HPTE_V_BOLTED,
  74. .val = HPTE_V_BOLTED,
  75. .set = "bolted",
  76. .clear = "",
  77. }
  78. };
  79. static const struct flag_info r_flag_array[] = {
  80. {
  81. .mask = HPTE_R_PP0 | HPTE_R_PP,
  82. .val = PP_RWXX,
  83. .set = "prot:RW--",
  84. }, {
  85. .mask = HPTE_R_PP0 | HPTE_R_PP,
  86. .val = PP_RWRX,
  87. .set = "prot:RWR-",
  88. }, {
  89. .mask = HPTE_R_PP0 | HPTE_R_PP,
  90. .val = PP_RWRW,
  91. .set = "prot:RWRW",
  92. }, {
  93. .mask = HPTE_R_PP0 | HPTE_R_PP,
  94. .val = PP_RXRX,
  95. .set = "prot:R-R-",
  96. }, {
  97. .mask = HPTE_R_PP0 | HPTE_R_PP,
  98. .val = PP_RXXX,
  99. .set = "prot:R---",
  100. }, {
  101. .mask = HPTE_R_KEY_HI | HPTE_R_KEY_LO,
  102. .val = HPTE_R_KEY_HI | HPTE_R_KEY_LO,
  103. .set = "key",
  104. .clear = "",
  105. .is_val = true,
  106. }, {
  107. .mask = HPTE_R_R,
  108. .val = HPTE_R_R,
  109. .set = "ref",
  110. .clear = " ",
  111. }, {
  112. .mask = HPTE_R_C,
  113. .val = HPTE_R_C,
  114. .set = "changed",
  115. .clear = " ",
  116. }, {
  117. .mask = HPTE_R_N,
  118. .val = HPTE_R_N,
  119. .set = "no execute",
  120. }, {
  121. .mask = HPTE_R_WIMG,
  122. .val = HPTE_R_W,
  123. .set = "writethru",
  124. }, {
  125. .mask = HPTE_R_WIMG,
  126. .val = HPTE_R_I,
  127. .set = "no cache",
  128. }, {
  129. .mask = HPTE_R_WIMG,
  130. .val = HPTE_R_G,
  131. .set = "guarded",
  132. }
  133. };
  134. static int calculate_pagesize(struct pg_state *st, int ps, char s[])
  135. {
  136. static const char units[] = "BKMGTPE";
  137. const char *unit = units;
  138. while (ps > 9 && unit[1]) {
  139. ps -= 10;
  140. unit++;
  141. }
  142. seq_printf(st->seq, " %s_ps: %i%c\t", s, 1<<ps, *unit);
  143. return ps;
  144. }
  145. static void dump_flag_info(struct pg_state *st, const struct flag_info
  146. *flag, u64 pte, int num)
  147. {
  148. unsigned int i;
  149. for (i = 0; i < num; i++, flag++) {
  150. const char *s = NULL;
  151. u64 val;
  152. /* flag not defined so don't check it */
  153. if (flag->mask == 0)
  154. continue;
  155. /* Some 'flags' are actually values */
  156. if (flag->is_val) {
  157. val = pte & flag->val;
  158. if (flag->shift)
  159. val = val >> flag->shift;
  160. seq_printf(st->seq, " %s:%llx", flag->set, val);
  161. } else {
  162. if ((pte & flag->mask) == flag->val)
  163. s = flag->set;
  164. else
  165. s = flag->clear;
  166. if (s)
  167. seq_printf(st->seq, " %s", s);
  168. }
  169. }
  170. }
  171. static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r,
  172. unsigned long rpn, int bps, int aps, unsigned long lp)
  173. {
  174. int aps_index;
  175. while (ea >= st->marker[1].start_address) {
  176. st->marker++;
  177. seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
  178. }
  179. seq_printf(st->seq, "0x%lx:\t", ea);
  180. seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v));
  181. dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array));
  182. seq_printf(st->seq, " rpn: %lx\t", rpn);
  183. dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array));
  184. calculate_pagesize(st, bps, "base");
  185. aps_index = calculate_pagesize(st, aps, "actual");
  186. if (aps_index != 2)
  187. seq_printf(st->seq, "LP enc: %lx", lp);
  188. seq_putc(st->seq, '\n');
  189. }
  190. static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64
  191. *r)
  192. {
  193. struct hash_pte *hptep;
  194. unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v;
  195. int i, ssize = mmu_kernel_ssize;
  196. unsigned long shift = mmu_psize_defs[psize].shift;
  197. /* calculate hash */
  198. vsid = get_kernel_vsid(ea, ssize);
  199. vpn = hpt_vpn(ea, vsid, ssize);
  200. hash = hpt_hash(vpn, shift, ssize);
  201. want_v = hpte_encode_avpn(vpn, psize, ssize);
  202. /* to check in the secondary hash table, we invert the hash */
  203. if (!primary)
  204. hash = ~hash;
  205. hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  206. for (i = 0; i < HPTES_PER_GROUP; i++) {
  207. hptep = htab_address + hpte_group;
  208. hpte_v = be64_to_cpu(hptep->v);
  209. if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
  210. /* HPTE matches */
  211. *v = be64_to_cpu(hptep->v);
  212. *r = be64_to_cpu(hptep->r);
  213. return 0;
  214. }
  215. ++hpte_group;
  216. }
  217. return -1;
  218. }
  219. static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 *r)
  220. {
  221. struct {
  222. unsigned long v;
  223. unsigned long r;
  224. } ptes[4];
  225. unsigned long vsid, vpn, hash, hpte_group, want_v;
  226. int i, j, ssize = mmu_kernel_ssize;
  227. long lpar_rc = 0;
  228. unsigned long shift = mmu_psize_defs[psize].shift;
  229. /* calculate hash */
  230. vsid = get_kernel_vsid(ea, ssize);
  231. vpn = hpt_vpn(ea, vsid, ssize);
  232. hash = hpt_hash(vpn, shift, ssize);
  233. want_v = hpte_encode_avpn(vpn, psize, ssize);
  234. /* to check in the secondary hash table, we invert the hash */
  235. if (!primary)
  236. hash = ~hash;
  237. hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  238. /* see if we can find an entry in the hpte with this hash */
  239. for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
  240. lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
  241. if (lpar_rc)
  242. continue;
  243. for (j = 0; j < 4; j++) {
  244. if (HPTE_V_COMPARE(ptes[j].v, want_v) &&
  245. (ptes[j].v & HPTE_V_VALID)) {
  246. /* HPTE matches */
  247. *v = ptes[j].v;
  248. *r = ptes[j].r;
  249. return 0;
  250. }
  251. }
  252. }
  253. return -1;
  254. }
  255. static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps,
  256. unsigned long *lp_bits)
  257. {
  258. struct mmu_psize_def entry;
  259. unsigned long arpn, mask, lp;
  260. int penc = -2, idx = 0, shift;
  261. /*.
  262. * The LP field has 8 bits. Depending on the actual page size, some of
  263. * these bits are concatenated with the APRN to get the RPN. The rest
  264. * of the bits in the LP field is the LP value and is an encoding for
  265. * the base page size and the actual page size.
  266. *
  267. * - find the mmu entry for our base page size
  268. * - go through all page encodings and use the associated mask to
  269. * find an encoding that matches our encoding in the LP field.
  270. */
  271. arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
  272. lp = arpn & 0xff;
  273. entry = mmu_psize_defs[bps];
  274. while (idx < MMU_PAGE_COUNT) {
  275. penc = entry.penc[idx];
  276. if ((penc != -1) && (mmu_psize_defs[idx].shift)) {
  277. shift = mmu_psize_defs[idx].shift - HPTE_R_RPN_SHIFT;
  278. mask = (0x1 << (shift)) - 1;
  279. if ((lp & mask) == penc) {
  280. *aps = mmu_psize_to_shift(idx);
  281. *lp_bits = lp & mask;
  282. *rpn = arpn >> shift;
  283. return;
  284. }
  285. }
  286. idx++;
  287. }
  288. }
  289. static int base_hpte_find(unsigned long ea, int psize, bool primary, u64 *v,
  290. u64 *r)
  291. {
  292. if (IS_ENABLED(CONFIG_PPC_PSERIES) && firmware_has_feature(FW_FEATURE_LPAR))
  293. return pseries_find(ea, psize, primary, v, r);
  294. return native_find(ea, psize, primary, v, r);
  295. }
  296. static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize)
  297. {
  298. unsigned long slot;
  299. u64 v = 0, r = 0;
  300. unsigned long rpn, lp_bits;
  301. int base_psize = 0, actual_psize = 0;
  302. if (ea < PAGE_OFFSET)
  303. return -1;
  304. /* Look in primary table */
  305. slot = base_hpte_find(ea, psize, true, &v, &r);
  306. /* Look in secondary table */
  307. if (slot == -1)
  308. slot = base_hpte_find(ea, psize, false, &v, &r);
  309. /* No entry found */
  310. if (slot == -1)
  311. return -1;
  312. /*
  313. * We found an entry in the hash page table:
  314. * - check that this has the same base page
  315. * - find the actual page size
  316. * - find the RPN
  317. */
  318. base_psize = mmu_psize_to_shift(psize);
  319. if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) {
  320. decode_r(psize, r, &rpn, &actual_psize, &lp_bits);
  321. } else {
  322. /* 4K actual page size */
  323. actual_psize = 12;
  324. rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
  325. /* In this case there are no LP bits */
  326. lp_bits = -1;
  327. }
  328. /*
  329. * We didn't find a matching encoding, so the PTE we found isn't for
  330. * this address.
  331. */
  332. if (actual_psize == -1)
  333. return -1;
  334. dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits);
  335. return 0;
  336. }
  337. static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
  338. {
  339. pte_t *pte = pte_offset_kernel(pmd, 0);
  340. unsigned long addr, pteval, psize;
  341. int i, status;
  342. for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
  343. addr = start + i * PAGE_SIZE;
  344. pteval = pte_val(*pte);
  345. if (addr < VMALLOC_END)
  346. psize = mmu_vmalloc_psize;
  347. else
  348. psize = mmu_io_psize;
  349. /* check for secret 4K mappings */
  350. if (IS_ENABLED(CONFIG_PPC_64K_PAGES) &&
  351. ((pteval & H_PAGE_COMBO) == H_PAGE_COMBO ||
  352. (pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN))
  353. psize = mmu_io_psize;
  354. /* check for hashpte */
  355. status = hpte_find(st, addr, psize);
  356. if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE)
  357. && (status != -1)) {
  358. /* found a hpte that is not in the linux page tables */
  359. seq_printf(st->seq, "page probably bolted before linux"
  360. " pagetables were set: addr:%lx, pteval:%lx\n",
  361. addr, pteval);
  362. }
  363. }
  364. }
  365. static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
  366. {
  367. pmd_t *pmd = pmd_offset(pud, 0);
  368. unsigned long addr;
  369. unsigned int i;
  370. for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
  371. addr = start + i * PMD_SIZE;
  372. if (!pmd_none(*pmd))
  373. /* pmd exists */
  374. walk_pte(st, pmd, addr);
  375. }
  376. }
  377. static void walk_pud(struct pg_state *st, p4d_t *p4d, unsigned long start)
  378. {
  379. pud_t *pud = pud_offset(p4d, 0);
  380. unsigned long addr;
  381. unsigned int i;
  382. for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
  383. addr = start + i * PUD_SIZE;
  384. if (!pud_none(*pud))
  385. /* pud exists */
  386. walk_pmd(st, pud, addr);
  387. }
  388. }
  389. static void walk_p4d(struct pg_state *st, pgd_t *pgd, unsigned long start)
  390. {
  391. p4d_t *p4d = p4d_offset(pgd, 0);
  392. unsigned long addr;
  393. unsigned int i;
  394. for (i = 0; i < PTRS_PER_P4D; i++, p4d++) {
  395. addr = start + i * P4D_SIZE;
  396. if (!p4d_none(*p4d))
  397. /* p4d exists */
  398. walk_pud(st, p4d, addr);
  399. }
  400. }
  401. static void walk_pagetables(struct pg_state *st)
  402. {
  403. pgd_t *pgd = pgd_offset_k(0UL);
  404. unsigned int i;
  405. unsigned long addr;
  406. /*
  407. * Traverse the linux pagetable structure and dump pages that are in
  408. * the hash pagetable.
  409. */
  410. for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
  411. addr = KERN_VIRT_START + i * PGDIR_SIZE;
  412. if (!pgd_none(*pgd))
  413. /* pgd exists */
  414. walk_p4d(st, pgd, addr);
  415. }
  416. }
  417. static void walk_linearmapping(struct pg_state *st)
  418. {
  419. unsigned long addr;
  420. /*
  421. * Traverse the linear mapping section of virtual memory and dump pages
  422. * that are in the hash pagetable.
  423. */
  424. unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift;
  425. for (addr = PAGE_OFFSET; addr < PAGE_OFFSET +
  426. memblock_end_of_DRAM(); addr += psize)
  427. hpte_find(st, addr, mmu_linear_psize);
  428. }
  429. static void walk_vmemmap(struct pg_state *st)
  430. {
  431. struct vmemmap_backing *ptr = vmemmap_list;
  432. if (!IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
  433. return;
  434. /*
  435. * Traverse the vmemmaped memory and dump pages that are in the hash
  436. * pagetable.
  437. */
  438. while (ptr->list) {
  439. hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize);
  440. ptr = ptr->list;
  441. }
  442. seq_puts(st->seq, "---[ vmemmap end ]---\n");
  443. }
  444. static void populate_markers(void)
  445. {
  446. address_markers[0].start_address = PAGE_OFFSET;
  447. address_markers[1].start_address = VMALLOC_START;
  448. address_markers[2].start_address = VMALLOC_END;
  449. address_markers[3].start_address = ISA_IO_BASE;
  450. address_markers[4].start_address = ISA_IO_END;
  451. address_markers[5].start_address = PHB_IO_BASE;
  452. address_markers[6].start_address = PHB_IO_END;
  453. address_markers[7].start_address = IOREMAP_BASE;
  454. address_markers[8].start_address = IOREMAP_END;
  455. address_markers[9].start_address = H_VMEMMAP_START;
  456. }
  457. static int ptdump_show(struct seq_file *m, void *v)
  458. {
  459. struct pg_state st = {
  460. .seq = m,
  461. .start_address = PAGE_OFFSET,
  462. .marker = address_markers,
  463. };
  464. /*
  465. * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and
  466. * dump pages that are in the hash pagetable.
  467. */
  468. walk_linearmapping(&st);
  469. walk_pagetables(&st);
  470. walk_vmemmap(&st);
  471. return 0;
  472. }
  473. DEFINE_SHOW_ATTRIBUTE(ptdump);
  474. static int ptdump_init(void)
  475. {
  476. if (!radix_enabled()) {
  477. populate_markers();
  478. debugfs_create_file("kernel_hash_pagetable", 0400, NULL, NULL,
  479. &ptdump_fops);
  480. }
  481. return 0;
  482. }
  483. device_initcall(ptdump_init);