2 * Copyright 2016, Rashmica Gupta, IBM Corp.
4 * This traverses the kernel pagetables and dumps the
5 * information about the used sections of memory to
6 * /sys/kernel/debug/kernel_pagetables.
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.
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
17 #include <linux/debugfs.h>
21 #include <linux/sched.h>
22 #include <linux/seq_file.h>
23 #include <asm/fixmap.h>
24 #include <asm/pgtable.h>
25 #include <linux/const.h>
27 #include <asm/pgalloc.h>
30 #define KERN_VIRT_START 0
34 * To visualise what is happening,
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
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}
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
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.
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.
61 const struct addr_marker *marker;
62 unsigned long start_address;
63 unsigned long start_pa;
64 unsigned long last_pa;
70 unsigned long start_address;
74 static struct addr_marker address_markers[] = {
75 { 0, "Start of kernel VM" },
76 { 0, "vmalloc() Area" },
77 { 0, "vmalloc() End" },
79 { 0, "isa I/O start" },
81 { 0, "phb I/O start" },
83 { 0, "I/O remap start" },
84 { 0, "I/O remap end" },
85 { 0, "vmemmap start" },
87 { 0, "Early I/O remap start" },
88 { 0, "Early I/O remap end" },
89 #ifdef CONFIG_NOT_COHERENT_CACHE
90 { 0, "Consistent mem start" },
91 { 0, "Consistent mem end" },
94 { 0, "Highmem PTEs start" },
95 { 0, "Highmem PTEs end" },
97 { 0, "Fixmap start" },
112 static const struct flag_info flag_array[] = {
114 #ifdef CONFIG_PPC_STD_MMU_64
115 .mask = _PAGE_PRIVILEGED,
144 .mask = _PAGE_PRESENT,
145 .val = _PAGE_PRESENT,
149 #ifdef CONFIG_PPC_STD_MMU_64
150 .mask = H_PAGE_HASHPTE,
151 .val = H_PAGE_HASHPTE,
153 .mask = _PAGE_HASHPTE,
154 .val = _PAGE_HASHPTE,
159 #ifndef CONFIG_PPC_STD_MMU_64
160 .mask = _PAGE_GUARDED,
161 .val = _PAGE_GUARDED,
171 .mask = _PAGE_ACCESSED,
172 .val = _PAGE_ACCESSED,
176 #ifndef CONFIG_PPC_STD_MMU_64
177 .mask = _PAGE_WRITETHRU,
178 .val = _PAGE_WRITETHRU,
179 .set = "write through",
183 #ifndef CONFIG_PPC_BOOK3S_64
184 .mask = _PAGE_NO_CACHE,
185 .val = _PAGE_NO_CACHE,
190 .mask = _PAGE_NON_IDEMPOTENT,
191 .val = _PAGE_NON_IDEMPOTENT,
192 .set = "non-idempotent",
195 .mask = _PAGE_TOLERANT,
196 .val = _PAGE_TOLERANT,
201 #ifdef CONFIG_PPC_BOOK3S_64
206 #ifdef CONFIG_PPC_64K_PAGES
207 .mask = H_PAGE_COMBO,
211 .mask = H_PAGE_4K_PFN,
212 .val = H_PAGE_4K_PFN,
216 .mask = H_PAGE_F_GIX,
220 .shift = H_PAGE_F_GIX_SHIFT,
222 .mask = H_PAGE_F_SECOND,
223 .val = H_PAGE_F_SECOND,
227 .mask = _PAGE_SPECIAL,
228 .val = _PAGE_SPECIAL,
231 .mask = _PAGE_SHARED,
237 struct pgtable_level {
238 const struct flag_info *flag;
243 static struct pgtable_level pg_level[] = {
247 .num = ARRAY_SIZE(flag_array),
250 .num = ARRAY_SIZE(flag_array),
253 .num = ARRAY_SIZE(flag_array),
256 .num = ARRAY_SIZE(flag_array),
260 static void dump_flag_info(struct pg_state *st, const struct flag_info
261 *flag, u64 pte, int num)
265 for (i = 0; i < num; i++, flag++) {
266 const char *s = NULL;
269 /* flag not defined so don't check it */
272 /* Some 'flags' are actually values */
274 val = pte & flag->val;
276 val = val >> flag->shift;
277 seq_printf(st->seq, " %s:%llx", flag->set, val);
279 if ((pte & flag->mask) == flag->val)
284 seq_printf(st->seq, " %s", s);
286 st->current_flags &= ~flag->mask;
288 if (st->current_flags != 0)
289 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
292 static void dump_addr(struct pg_state *st, unsigned long addr)
294 static const char units[] = "KMGTPE";
295 const char *unit = units;
299 seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
300 seq_printf(st->seq, "0x%016lx ", st->start_pa);
302 seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
303 seq_printf(st->seq, "0x%08lx ", st->start_pa);
306 delta = (addr - st->start_address) >> 10;
307 /* Work out what appropriate unit to use */
308 while (!(delta & 1023) && unit[1]) {
312 seq_printf(st->seq, "%9lu%c", delta, *unit);
316 static void note_page(struct pg_state *st, unsigned long addr,
317 unsigned int level, u64 val)
319 u64 flag = val & pg_level[level].mask;
320 u64 pa = val & PTE_RPN_MASK;
322 /* At first no level is set */
325 st->current_flags = flag;
326 st->start_address = addr;
329 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
331 * Dump the section of virtual memory when:
332 * - the PTE flags from one entry to the next differs.
333 * - we change levels in the tree.
334 * - the address is in a different section of memory and is thus
335 * used for a different purpose, regardless of the flags.
336 * - the pa of this page is not adjacent to the last inspected page
338 } else if (flag != st->current_flags || level != st->level ||
339 addr >= st->marker[1].start_address ||
340 pa != st->last_pa + PAGE_SIZE) {
342 /* Check the PTE flags */
343 if (st->current_flags) {
346 /* Dump all the flags */
347 if (pg_level[st->level].flag)
348 dump_flag_info(st, pg_level[st->level].flag,
350 pg_level[st->level].num);
352 seq_puts(st->seq, "\n");
356 * Address indicates we have passed the end of the
357 * current section of virtual memory
359 while (addr >= st->marker[1].start_address) {
361 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
363 st->start_address = addr;
366 st->current_flags = flag;
373 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
375 pte_t *pte = pte_offset_kernel(pmd, 0);
379 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
380 addr = start + i * PAGE_SIZE;
381 note_page(st, addr, 4, pte_val(*pte));
386 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
388 pmd_t *pmd = pmd_offset(pud, 0);
392 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
393 addr = start + i * PMD_SIZE;
396 walk_pte(st, pmd, addr);
398 note_page(st, addr, 3, pmd_val(*pmd));
402 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
404 pud_t *pud = pud_offset(pgd, 0);
408 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
409 addr = start + i * PUD_SIZE;
412 walk_pmd(st, pud, addr);
414 note_page(st, addr, 2, pud_val(*pud));
418 static void walk_pagetables(struct pg_state *st)
420 pgd_t *pgd = pgd_offset_k(0UL);
425 * Traverse the linux pagetable structure and dump pages that are in
426 * the hash pagetable.
428 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
429 addr = KERN_VIRT_START + i * PGDIR_SIZE;
432 walk_pud(st, pgd, addr);
434 note_page(st, addr, 1, pgd_val(*pgd));
438 static void populate_markers(void)
442 address_markers[i++].start_address = PAGE_OFFSET;
443 address_markers[i++].start_address = VMALLOC_START;
444 address_markers[i++].start_address = VMALLOC_END;
446 address_markers[i++].start_address = ISA_IO_BASE;
447 address_markers[i++].start_address = ISA_IO_END;
448 address_markers[i++].start_address = PHB_IO_BASE;
449 address_markers[i++].start_address = PHB_IO_END;
450 address_markers[i++].start_address = IOREMAP_BASE;
451 address_markers[i++].start_address = IOREMAP_END;
452 #ifdef CONFIG_PPC_STD_MMU_64
453 address_markers[i++].start_address = H_VMEMMAP_BASE;
455 address_markers[i++].start_address = VMEMMAP_BASE;
457 #else /* !CONFIG_PPC64 */
458 address_markers[i++].start_address = ioremap_bot;
459 address_markers[i++].start_address = IOREMAP_TOP;
460 #ifdef CONFIG_NOT_COHERENT_CACHE
461 address_markers[i++].start_address = IOREMAP_TOP;
462 address_markers[i++].start_address = IOREMAP_TOP +
463 CONFIG_CONSISTENT_SIZE;
465 #ifdef CONFIG_HIGHMEM
466 address_markers[i++].start_address = PKMAP_BASE;
467 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
469 address_markers[i++].start_address = FIXADDR_START;
470 address_markers[i++].start_address = FIXADDR_TOP;
471 #endif /* CONFIG_PPC64 */
474 static int ptdump_show(struct seq_file *m, void *v)
476 struct pg_state st = {
478 .start_address = KERN_VIRT_START,
479 .marker = address_markers,
481 /* Traverse kernel page tables */
482 walk_pagetables(&st);
483 note_page(&st, 0, 0, 0);
488 static int ptdump_open(struct inode *inode, struct file *file)
490 return single_open(file, ptdump_show, NULL);
493 static const struct file_operations ptdump_fops = {
497 .release = single_release,
500 static void build_pgtable_complete_mask(void)
504 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
505 if (pg_level[i].flag)
506 for (j = 0; j < pg_level[i].num; j++)
507 pg_level[i].mask |= pg_level[i].flag[j].mask;
510 static int ptdump_init(void)
512 struct dentry *debugfs_file;
515 build_pgtable_complete_mask();
516 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
518 return debugfs_file ? 0 : -ENOMEM;
520 device_initcall(ptdump_init);