1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2016, Rashmica Gupta, IBM Corp.
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.
9 * If radix is enabled then there is no hash page table and so no debugfs file
12 #include <linux/debugfs.h>
16 #include <linux/sched.h>
17 #include <linux/seq_file.h>
18 #include <asm/pgtable.h>
19 #include <linux/const.h>
21 #include <asm/pgalloc.h>
22 #include <asm/plpar_wrappers.h>
23 #include <linux/memblock.h>
24 #include <asm/firmware.h>
28 const struct addr_marker *marker;
29 unsigned long start_address;
35 unsigned long start_address;
39 static struct addr_marker address_markers[] = {
40 { 0, "Start of kernel VM" },
41 { 0, "vmalloc() Area" },
42 { 0, "vmalloc() End" },
43 { 0, "isa I/O start" },
45 { 0, "phb I/O start" },
47 { 0, "I/O remap start" },
48 { 0, "I/O remap end" },
49 { 0, "vmemmap start" },
62 static const struct flag_info v_flag_array[] = {
65 .val = SLB_VSID_B_256M,
67 .clear = "ssize: 1T ",
69 .mask = HPTE_V_SECONDARY,
70 .val = HPTE_V_SECONDARY,
79 .mask = HPTE_V_BOLTED,
86 static const struct flag_info r_flag_array[] = {
88 .mask = HPTE_R_PP0 | HPTE_R_PP,
92 .mask = HPTE_R_PP0 | HPTE_R_PP,
96 .mask = HPTE_R_PP0 | HPTE_R_PP,
100 .mask = HPTE_R_PP0 | HPTE_R_PP,
104 .mask = HPTE_R_PP0 | HPTE_R_PP,
108 .mask = HPTE_R_KEY_HI | HPTE_R_KEY_LO,
109 .val = HPTE_R_KEY_HI | HPTE_R_KEY_LO,
142 static int calculate_pagesize(struct pg_state *st, int ps, char s[])
144 static const char units[] = "BKMGTPE";
145 const char *unit = units;
147 while (ps > 9 && unit[1]) {
151 seq_printf(st->seq, " %s_ps: %i%c\t", s, 1<<ps, *unit);
155 static void dump_flag_info(struct pg_state *st, const struct flag_info
156 *flag, u64 pte, int num)
160 for (i = 0; i < num; i++, flag++) {
161 const char *s = NULL;
164 /* flag not defined so don't check it */
167 /* Some 'flags' are actually values */
169 val = pte & flag->val;
171 val = val >> flag->shift;
172 seq_printf(st->seq, " %s:%llx", flag->set, val);
174 if ((pte & flag->mask) == flag->val)
179 seq_printf(st->seq, " %s", s);
184 static void dump_hpte_info(struct pg_state *st, unsigned long ea, u64 v, u64 r,
185 unsigned long rpn, int bps, int aps, unsigned long lp)
189 while (ea >= st->marker[1].start_address) {
191 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
193 seq_printf(st->seq, "0x%lx:\t", ea);
194 seq_printf(st->seq, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v));
195 dump_flag_info(st, v_flag_array, v, ARRAY_SIZE(v_flag_array));
196 seq_printf(st->seq, " rpn: %lx\t", rpn);
197 dump_flag_info(st, r_flag_array, r, ARRAY_SIZE(r_flag_array));
199 calculate_pagesize(st, bps, "base");
200 aps_index = calculate_pagesize(st, aps, "actual");
202 seq_printf(st->seq, "LP enc: %lx", lp);
203 seq_putc(st->seq, '\n');
207 static int native_find(unsigned long ea, int psize, bool primary, u64 *v, u64
210 struct hash_pte *hptep;
211 unsigned long hash, vsid, vpn, hpte_group, want_v, hpte_v;
212 int i, ssize = mmu_kernel_ssize;
213 unsigned long shift = mmu_psize_defs[psize].shift;
216 vsid = get_kernel_vsid(ea, ssize);
217 vpn = hpt_vpn(ea, vsid, ssize);
218 hash = hpt_hash(vpn, shift, ssize);
219 want_v = hpte_encode_avpn(vpn, psize, ssize);
221 /* to check in the secondary hash table, we invert the hash */
224 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
225 for (i = 0; i < HPTES_PER_GROUP; i++) {
226 hptep = htab_address + hpte_group;
227 hpte_v = be64_to_cpu(hptep->v);
229 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
231 *v = be64_to_cpu(hptep->v);
232 *r = be64_to_cpu(hptep->r);
240 #ifdef CONFIG_PPC_PSERIES
241 static int pseries_find(unsigned long ea, int psize, bool primary, u64 *v, u64 *r)
243 struct hash_pte ptes[4];
244 unsigned long vsid, vpn, hash, hpte_group, want_v;
245 int i, j, ssize = mmu_kernel_ssize;
247 unsigned long shift = mmu_psize_defs[psize].shift;
250 vsid = get_kernel_vsid(ea, ssize);
251 vpn = hpt_vpn(ea, vsid, ssize);
252 hash = hpt_hash(vpn, shift, ssize);
253 want_v = hpte_encode_avpn(vpn, psize, ssize);
255 /* to check in the secondary hash table, we invert the hash */
258 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
259 /* see if we can find an entry in the hpte with this hash */
260 for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
261 lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
263 if (lpar_rc != H_SUCCESS)
265 for (j = 0; j < 4; j++) {
266 if (HPTE_V_COMPARE(ptes[j].v, want_v) &&
267 (ptes[j].v & HPTE_V_VALID)) {
279 static void decode_r(int bps, unsigned long r, unsigned long *rpn, int *aps,
280 unsigned long *lp_bits)
282 struct mmu_psize_def entry;
283 unsigned long arpn, mask, lp;
284 int penc = -2, idx = 0, shift;
287 * The LP field has 8 bits. Depending on the actual page size, some of
288 * these bits are concatenated with the APRN to get the RPN. The rest
289 * of the bits in the LP field is the LP value and is an encoding for
290 * the base page size and the actual page size.
292 * - find the mmu entry for our base page size
293 * - go through all page encodings and use the associated mask to
294 * find an encoding that matches our encoding in the LP field.
296 arpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
299 entry = mmu_psize_defs[bps];
300 while (idx < MMU_PAGE_COUNT) {
301 penc = entry.penc[idx];
302 if ((penc != -1) && (mmu_psize_defs[idx].shift)) {
303 shift = mmu_psize_defs[idx].shift - HPTE_R_RPN_SHIFT;
304 mask = (0x1 << (shift)) - 1;
305 if ((lp & mask) == penc) {
306 *aps = mmu_psize_to_shift(idx);
307 *lp_bits = lp & mask;
308 *rpn = arpn >> shift;
316 static int base_hpte_find(unsigned long ea, int psize, bool primary, u64 *v,
319 #ifdef CONFIG_PPC_PSERIES
320 if (firmware_has_feature(FW_FEATURE_LPAR))
321 return pseries_find(ea, psize, primary, v, r);
323 return native_find(ea, psize, primary, v, r);
326 static unsigned long hpte_find(struct pg_state *st, unsigned long ea, int psize)
330 unsigned long rpn, lp_bits;
331 int base_psize = 0, actual_psize = 0;
333 if (ea < PAGE_OFFSET)
336 /* Look in primary table */
337 slot = base_hpte_find(ea, psize, true, &v, &r);
339 /* Look in secondary table */
341 slot = base_hpte_find(ea, psize, false, &v, &r);
348 * We found an entry in the hash page table:
349 * - check that this has the same base page
350 * - find the actual page size
353 base_psize = mmu_psize_to_shift(psize);
355 if ((v & HPTE_V_LARGE) == HPTE_V_LARGE) {
356 decode_r(psize, r, &rpn, &actual_psize, &lp_bits);
358 /* 4K actual page size */
360 rpn = (r & HPTE_R_RPN) >> HPTE_R_RPN_SHIFT;
361 /* In this case there are no LP bits */
365 * We didn't find a matching encoding, so the PTE we found isn't for
368 if (actual_psize == -1)
371 dump_hpte_info(st, ea, v, r, rpn, base_psize, actual_psize, lp_bits);
375 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
377 pte_t *pte = pte_offset_kernel(pmd, 0);
378 unsigned long addr, pteval, psize;
381 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
382 addr = start + i * PAGE_SIZE;
383 pteval = pte_val(*pte);
385 if (addr < VMALLOC_END)
386 psize = mmu_vmalloc_psize;
388 psize = mmu_io_psize;
389 #ifdef CONFIG_PPC_64K_PAGES
390 /* check for secret 4K mappings */
391 if (((pteval & H_PAGE_COMBO) == H_PAGE_COMBO) ||
392 ((pteval & H_PAGE_4K_PFN) == H_PAGE_4K_PFN))
393 psize = mmu_io_psize;
395 /* check for hashpte */
396 status = hpte_find(st, addr, psize);
398 if (((pteval & H_PAGE_HASHPTE) != H_PAGE_HASHPTE)
400 /* found a hpte that is not in the linux page tables */
401 seq_printf(st->seq, "page probably bolted before linux"
402 " pagetables were set: addr:%lx, pteval:%lx\n",
408 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
410 pmd_t *pmd = pmd_offset(pud, 0);
414 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
415 addr = start + i * PMD_SIZE;
418 walk_pte(st, pmd, addr);
422 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
424 pud_t *pud = pud_offset(pgd, 0);
428 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
429 addr = start + i * PUD_SIZE;
432 walk_pmd(st, pud, addr);
436 static void walk_pagetables(struct pg_state *st)
438 pgd_t *pgd = pgd_offset_k(0UL);
443 * Traverse the linux pagetable structure and dump pages that are in
444 * the hash pagetable.
446 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
447 addr = KERN_VIRT_START + i * PGDIR_SIZE;
450 walk_pud(st, pgd, addr);
455 static void walk_linearmapping(struct pg_state *st)
460 * Traverse the linear mapping section of virtual memory and dump pages
461 * that are in the hash pagetable.
463 unsigned long psize = 1 << mmu_psize_defs[mmu_linear_psize].shift;
465 for (addr = PAGE_OFFSET; addr < PAGE_OFFSET +
466 memblock_end_of_DRAM(); addr += psize)
467 hpte_find(st, addr, mmu_linear_psize);
470 static void walk_vmemmap(struct pg_state *st)
472 #ifdef CONFIG_SPARSEMEM_VMEMMAP
473 struct vmemmap_backing *ptr = vmemmap_list;
476 * Traverse the vmemmaped memory and dump pages that are in the hash
480 hpte_find(st, ptr->virt_addr, mmu_vmemmap_psize);
483 seq_puts(st->seq, "---[ vmemmap end ]---\n");
487 static void populate_markers(void)
489 address_markers[0].start_address = PAGE_OFFSET;
490 address_markers[1].start_address = VMALLOC_START;
491 address_markers[2].start_address = VMALLOC_END;
492 address_markers[3].start_address = ISA_IO_BASE;
493 address_markers[4].start_address = ISA_IO_END;
494 address_markers[5].start_address = PHB_IO_BASE;
495 address_markers[6].start_address = PHB_IO_END;
496 address_markers[7].start_address = IOREMAP_BASE;
497 address_markers[8].start_address = IOREMAP_END;
498 #ifdef CONFIG_PPC_BOOK3S_64
499 address_markers[9].start_address = H_VMEMMAP_START;
501 address_markers[9].start_address = VMEMMAP_BASE;
505 static int ptdump_show(struct seq_file *m, void *v)
507 struct pg_state st = {
509 .start_address = PAGE_OFFSET,
510 .marker = address_markers,
513 * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and
514 * dump pages that are in the hash pagetable.
516 walk_linearmapping(&st);
517 walk_pagetables(&st);
522 static int ptdump_open(struct inode *inode, struct file *file)
524 return single_open(file, ptdump_show, NULL);
527 static const struct file_operations ptdump_fops = {
531 .release = single_release,
534 static int ptdump_init(void)
536 struct dentry *debugfs_file;
538 if (!radix_enabled()) {
540 debugfs_file = debugfs_create_file("kernel_hash_pagetable",
541 0400, NULL, NULL, &ptdump_fops);
542 return debugfs_file ? 0 : -ENOMEM;
546 device_initcall(ptdump_init);