2 * Copyright 2016, Rashmica Gupta, IBM Corp.
4 * This traverses the kernel virtual memory and dumps the pages that are in
5 * the hash pagetable, along with their flags to
6 * /sys/kernel/debug/kernel_hash_pagetable.
8 * If radix is enabled then there is no hash page table and so no debugfs file
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; version 2
16 #include <linux/debugfs.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <asm/fixmap.h>
23 #include <asm/pgtable.h>
24 #include <linux/const.h>
26 #include <asm/pgalloc.h>
27 #include <asm/plpar_wrappers.h>
28 #include <linux/memblock.h>
29 #include <asm/firmware.h>
33 const struct addr_marker
*marker
;
34 unsigned long start_address
;
40 unsigned long start_address
;
44 static struct addr_marker address_markers
[] = {
45 { 0, "Start of kernel VM" },
46 { 0, "vmalloc() Area" },
47 { 0, "vmalloc() End" },
48 { 0, "isa I/O start" },
50 { 0, "phb I/O start" },
52 { 0, "I/O remap start" },
53 { 0, "I/O remap end" },
54 { 0, "vmemmap start" },
67 static const struct flag_info v_flag_array
[] = {
70 .val
= SLB_VSID_B_256M
,
72 .clear
= "ssize: 1T ",
74 .mask
= HPTE_V_SECONDARY
,
75 .val
= HPTE_V_SECONDARY
,
84 .mask
= HPTE_V_BOLTED
,
91 static const struct flag_info r_flag_array
[] = {
93 .mask
= HPTE_R_PP0
| HPTE_R_PP
,
97 .mask
= HPTE_R_PP0
| HPTE_R_PP
,
101 .mask
= HPTE_R_PP0
| HPTE_R_PP
,
105 .mask
= HPTE_R_PP0
| HPTE_R_PP
,
109 .mask
= HPTE_R_PP0
| HPTE_R_PP
,
113 .mask
= HPTE_R_KEY_HI
| HPTE_R_KEY_LO
,
114 .val
= HPTE_R_KEY_HI
| HPTE_R_KEY_LO
,
147 static int calculate_pagesize(struct pg_state
*st
, int ps
, char s
[])
149 static const char units
[] = "BKMGTPE";
150 const char *unit
= units
;
152 while (ps
> 9 && unit
[1]) {
156 seq_printf(st
->seq
, " %s_ps: %i%c\t", s
, 1<<ps
, *unit
);
160 static void dump_flag_info(struct pg_state
*st
, const struct flag_info
161 *flag
, u64 pte
, int num
)
165 for (i
= 0; i
< num
; i
++, flag
++) {
166 const char *s
= NULL
;
169 /* flag not defined so don't check it */
172 /* Some 'flags' are actually values */
174 val
= pte
& flag
->val
;
176 val
= val
>> flag
->shift
;
177 seq_printf(st
->seq
, " %s:%llx", flag
->set
, val
);
179 if ((pte
& flag
->mask
) == flag
->val
)
184 seq_printf(st
->seq
, " %s", s
);
189 static void dump_hpte_info(struct pg_state
*st
, unsigned long ea
, u64 v
, u64 r
,
190 unsigned long rpn
, int bps
, int aps
, unsigned long lp
)
194 while (ea
>= st
->marker
[1].start_address
) {
196 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
198 seq_printf(st
->seq
, "0x%lx:\t", ea
);
199 seq_printf(st
->seq
, "AVPN:%llx\t", HPTE_V_AVPN_VAL(v
));
200 dump_flag_info(st
, v_flag_array
, v
, ARRAY_SIZE(v_flag_array
));
201 seq_printf(st
->seq
, " rpn: %lx\t", rpn
);
202 dump_flag_info(st
, r_flag_array
, r
, ARRAY_SIZE(r_flag_array
));
204 calculate_pagesize(st
, bps
, "base");
205 aps_index
= calculate_pagesize(st
, aps
, "actual");
207 seq_printf(st
->seq
, "LP enc: %lx", lp
);
208 seq_puts(st
->seq
, "\n");
212 static int native_find(unsigned long ea
, int psize
, bool primary
, u64
*v
, u64
215 struct hash_pte
*hptep
;
216 unsigned long hash
, vsid
, vpn
, hpte_group
, want_v
, hpte_v
;
217 int i
, ssize
= mmu_kernel_ssize
;
218 unsigned long shift
= mmu_psize_defs
[psize
].shift
;
221 vsid
= get_kernel_vsid(ea
, ssize
);
222 vpn
= hpt_vpn(ea
, vsid
, ssize
);
223 hash
= hpt_hash(vpn
, shift
, ssize
);
224 want_v
= hpte_encode_avpn(vpn
, psize
, ssize
);
226 /* to check in the secondary hash table, we invert the hash */
229 hpte_group
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
230 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
231 hptep
= htab_address
+ hpte_group
;
232 hpte_v
= be64_to_cpu(hptep
->v
);
234 if (HPTE_V_COMPARE(hpte_v
, want_v
) && (hpte_v
& HPTE_V_VALID
)) {
236 *v
= be64_to_cpu(hptep
->v
);
237 *r
= be64_to_cpu(hptep
->r
);
245 #ifdef CONFIG_PPC_PSERIES
246 static int pseries_find(unsigned long ea
, int psize
, bool primary
, u64
*v
, u64
*r
)
248 struct hash_pte ptes
[4];
249 unsigned long vsid
, vpn
, hash
, hpte_group
, want_v
;
250 int i
, j
, ssize
= mmu_kernel_ssize
;
252 unsigned long shift
= mmu_psize_defs
[psize
].shift
;
255 vsid
= get_kernel_vsid(ea
, ssize
);
256 vpn
= hpt_vpn(ea
, vsid
, ssize
);
257 hash
= hpt_hash(vpn
, shift
, ssize
);
258 want_v
= hpte_encode_avpn(vpn
, psize
, ssize
);
260 /* to check in the secondary hash table, we invert the hash */
263 hpte_group
= ((hash
& htab_hash_mask
) * HPTES_PER_GROUP
) & ~0x7UL
;
264 /* see if we can find an entry in the hpte with this hash */
265 for (i
= 0; i
< HPTES_PER_GROUP
; i
+= 4, hpte_group
+= 4) {
266 lpar_rc
= plpar_pte_read_4(0, hpte_group
, (void *)ptes
);
268 if (lpar_rc
!= H_SUCCESS
)
270 for (j
= 0; j
< 4; j
++) {
271 if (HPTE_V_COMPARE(ptes
[j
].v
, want_v
) &&
272 (ptes
[j
].v
& HPTE_V_VALID
)) {
284 static void decode_r(int bps
, unsigned long r
, unsigned long *rpn
, int *aps
,
285 unsigned long *lp_bits
)
287 struct mmu_psize_def entry
;
288 unsigned long arpn
, mask
, lp
;
289 int penc
= -2, idx
= 0, shift
;
292 * The LP field has 8 bits. Depending on the actual page size, some of
293 * these bits are concatenated with the APRN to get the RPN. The rest
294 * of the bits in the LP field is the LP value and is an encoding for
295 * the base page size and the actual page size.
297 * - find the mmu entry for our base page size
298 * - go through all page encodings and use the associated mask to
299 * find an encoding that matches our encoding in the LP field.
301 arpn
= (r
& HPTE_R_RPN
) >> HPTE_R_RPN_SHIFT
;
304 entry
= mmu_psize_defs
[bps
];
305 while (idx
< MMU_PAGE_COUNT
) {
306 penc
= entry
.penc
[idx
];
307 if ((penc
!= -1) && (mmu_psize_defs
[idx
].shift
)) {
308 shift
= mmu_psize_defs
[idx
].shift
- HPTE_R_RPN_SHIFT
;
309 mask
= (0x1 << (shift
)) - 1;
310 if ((lp
& mask
) == penc
) {
311 *aps
= mmu_psize_to_shift(idx
);
312 *lp_bits
= lp
& mask
;
313 *rpn
= arpn
>> shift
;
321 static int base_hpte_find(unsigned long ea
, int psize
, bool primary
, u64
*v
,
324 #ifdef CONFIG_PPC_PSERIES
325 if (firmware_has_feature(FW_FEATURE_LPAR
))
326 return pseries_find(ea
, psize
, primary
, v
, r
);
328 return native_find(ea
, psize
, primary
, v
, r
);
331 static unsigned long hpte_find(struct pg_state
*st
, unsigned long ea
, int psize
)
335 unsigned long rpn
, lp_bits
;
336 int base_psize
= 0, actual_psize
= 0;
338 if (ea
<= PAGE_OFFSET
)
341 /* Look in primary table */
342 slot
= base_hpte_find(ea
, psize
, true, &v
, &r
);
344 /* Look in secondary table */
346 slot
= base_hpte_find(ea
, psize
, true, &v
, &r
);
353 * We found an entry in the hash page table:
354 * - check that this has the same base page
355 * - find the actual page size
358 base_psize
= mmu_psize_to_shift(psize
);
360 if ((v
& HPTE_V_LARGE
) == HPTE_V_LARGE
) {
361 decode_r(psize
, r
, &rpn
, &actual_psize
, &lp_bits
);
363 /* 4K actual page size */
365 rpn
= (r
& HPTE_R_RPN
) >> HPTE_R_RPN_SHIFT
;
366 /* In this case there are no LP bits */
370 * We didn't find a matching encoding, so the PTE we found isn't for
373 if (actual_psize
== -1)
376 dump_hpte_info(st
, ea
, v
, r
, rpn
, base_psize
, actual_psize
, lp_bits
);
380 static void walk_pte(struct pg_state
*st
, pmd_t
*pmd
, unsigned long start
)
382 pte_t
*pte
= pte_offset_kernel(pmd
, 0);
383 unsigned long addr
, pteval
, psize
;
386 for (i
= 0; i
< PTRS_PER_PTE
; i
++, pte
++) {
387 addr
= start
+ i
* PAGE_SIZE
;
388 pteval
= pte_val(*pte
);
390 if (addr
< VMALLOC_END
)
391 psize
= mmu_vmalloc_psize
;
393 psize
= mmu_io_psize
;
394 #ifdef CONFIG_PPC_64K_PAGES
395 /* check for secret 4K mappings */
396 if (((pteval
& H_PAGE_COMBO
) == H_PAGE_COMBO
) ||
397 ((pteval
& H_PAGE_4K_PFN
) == H_PAGE_4K_PFN
))
398 psize
= mmu_io_psize
;
400 /* check for hashpte */
401 status
= hpte_find(st
, addr
, psize
);
403 if (((pteval
& H_PAGE_HASHPTE
) != H_PAGE_HASHPTE
)
405 /* found a hpte that is not in the linux page tables */
406 seq_printf(st
->seq
, "page probably bolted before linux"
407 " pagetables were set: addr:%lx, pteval:%lx\n",
413 static void walk_pmd(struct pg_state
*st
, pud_t
*pud
, unsigned long start
)
415 pmd_t
*pmd
= pmd_offset(pud
, 0);
419 for (i
= 0; i
< PTRS_PER_PMD
; i
++, pmd
++) {
420 addr
= start
+ i
* PMD_SIZE
;
423 walk_pte(st
, pmd
, addr
);
427 static void walk_pud(struct pg_state
*st
, pgd_t
*pgd
, unsigned long start
)
429 pud_t
*pud
= pud_offset(pgd
, 0);
433 for (i
= 0; i
< PTRS_PER_PUD
; i
++, pud
++) {
434 addr
= start
+ i
* PUD_SIZE
;
437 walk_pmd(st
, pud
, addr
);
441 static void walk_pagetables(struct pg_state
*st
)
443 pgd_t
*pgd
= pgd_offset_k(0UL);
448 * Traverse the linux pagetable structure and dump pages that are in
449 * the hash pagetable.
451 for (i
= 0; i
< PTRS_PER_PGD
; i
++, pgd
++) {
452 addr
= KERN_VIRT_START
+ i
* PGDIR_SIZE
;
455 walk_pud(st
, pgd
, addr
);
460 static void walk_linearmapping(struct pg_state
*st
)
465 * Traverse the linear mapping section of virtual memory and dump pages
466 * that are in the hash pagetable.
468 unsigned long psize
= 1 << mmu_psize_defs
[mmu_linear_psize
].shift
;
470 for (addr
= PAGE_OFFSET
; addr
< PAGE_OFFSET
+
471 memblock_phys_mem_size(); addr
+= psize
)
472 hpte_find(st
, addr
, mmu_linear_psize
);
475 static void walk_vmemmap(struct pg_state
*st
)
477 #ifdef CONFIG_SPARSEMEM_VMEMMAP
478 struct vmemmap_backing
*ptr
= vmemmap_list
;
481 * Traverse the vmemmaped memory and dump pages that are in the hash
485 hpte_find(st
, ptr
->virt_addr
, mmu_vmemmap_psize
);
488 seq_puts(st
->seq
, "---[ vmemmap end ]---\n");
492 static void populate_markers(void)
494 address_markers
[0].start_address
= PAGE_OFFSET
;
495 address_markers
[1].start_address
= VMALLOC_START
;
496 address_markers
[2].start_address
= VMALLOC_END
;
497 address_markers
[3].start_address
= ISA_IO_BASE
;
498 address_markers
[4].start_address
= ISA_IO_END
;
499 address_markers
[5].start_address
= PHB_IO_BASE
;
500 address_markers
[6].start_address
= PHB_IO_END
;
501 address_markers
[7].start_address
= IOREMAP_BASE
;
502 address_markers
[8].start_address
= IOREMAP_END
;
503 #ifdef CONFIG_PPC_STD_MMU_64
504 address_markers
[9].start_address
= H_VMEMMAP_BASE
;
506 address_markers
[9].start_address
= VMEMMAP_BASE
;
510 static int ptdump_show(struct seq_file
*m
, void *v
)
512 struct pg_state st
= {
514 .start_address
= PAGE_OFFSET
,
515 .marker
= address_markers
,
518 * Traverse the 0xc, 0xd and 0xf areas of the kernel virtual memory and
519 * dump pages that are in the hash pagetable.
521 walk_linearmapping(&st
);
522 walk_pagetables(&st
);
527 static int ptdump_open(struct inode
*inode
, struct file
*file
)
529 return single_open(file
, ptdump_show
, NULL
);
532 static const struct file_operations ptdump_fops
= {
536 .release
= single_release
,
539 static int ptdump_init(void)
541 struct dentry
*debugfs_file
;
543 if (!radix_enabled()) {
545 debugfs_file
= debugfs_create_file("kernel_hash_pagetable",
546 0400, NULL
, NULL
, &ptdump_fops
);
547 return debugfs_file
? 0 : -ENOMEM
;
551 device_initcall(ptdump_init
);