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 * To visualise what is happening,
32 * - PTRS_PER_P** = how many entries there are in the corresponding P**
33 * - P**_SHIFT = how many bits of the address we use to index into the
35 * - P**_SIZE is how much memory we can access through the table - not the
36 * size of the table itself.
37 * P**={PGD, PUD, PMD, PTE}
40 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
41 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
44 * In the case where there are only 3 levels, the PUD is folded into the
45 * PGD: every PUD has only one entry which points to the PMD.
47 * The page dumper groups page table entries of the same type into a single
48 * description. It uses pg_state to track the range information while
49 * iterating over the PTE entries. When the continuity is broken it then
50 * dumps out a description of the range - ie PTEs that are virtually contiguous
51 * with the same PTE flags are chunked together. This is to make it clear how
52 * different areas of the kernel virtual memory are used.
57 const struct addr_marker *marker;
58 unsigned long start_address;
64 unsigned long start_address;
68 static struct addr_marker address_markers[] = {
69 { 0, "Start of kernel VM" },
70 { 0, "vmalloc() Area" },
71 { 0, "vmalloc() End" },
72 { 0, "isa I/O start" },
74 { 0, "phb I/O start" },
76 { 0, "I/O remap start" },
77 { 0, "I/O remap end" },
78 { 0, "vmemmap start" },
91 static const struct flag_info flag_array[] = {
93 #ifdef CONFIG_PPC_STD_MMU_64
94 .mask = _PAGE_PRIVILEGED,
118 .mask = _PAGE_PRESENT,
119 .val = _PAGE_PRESENT,
123 #ifdef CONFIG_PPC_STD_MMU_64
124 .mask = H_PAGE_HASHPTE,
125 .val = H_PAGE_HASHPTE,
127 .mask = _PAGE_HASHPTE,
128 .val = _PAGE_HASHPTE,
133 #ifndef CONFIG_PPC_STD_MMU_64
134 .mask = _PAGE_GUARDED,
135 .val = _PAGE_GUARDED,
145 .mask = _PAGE_ACCESSED,
146 .val = _PAGE_ACCESSED,
150 #ifndef CONFIG_PPC_STD_MMU_64
151 .mask = _PAGE_WRITETHRU,
152 .val = _PAGE_WRITETHRU,
153 .set = "write through",
157 .mask = _PAGE_NO_CACHE,
158 .val = _PAGE_NO_CACHE,
162 #ifdef CONFIG_PPC_BOOK3S_64
167 #ifdef CONFIG_PPC_64K_PAGES
168 .mask = H_PAGE_COMBO,
172 .mask = H_PAGE_4K_PFN,
173 .val = H_PAGE_4K_PFN,
177 .mask = H_PAGE_F_GIX,
181 .shift = H_PAGE_F_GIX_SHIFT,
183 .mask = H_PAGE_F_SECOND,
184 .val = H_PAGE_F_SECOND,
188 .mask = _PAGE_SPECIAL,
189 .val = _PAGE_SPECIAL,
194 struct pgtable_level {
195 const struct flag_info *flag;
200 static struct pgtable_level pg_level[] = {
204 .num = ARRAY_SIZE(flag_array),
207 .num = ARRAY_SIZE(flag_array),
210 .num = ARRAY_SIZE(flag_array),
213 .num = ARRAY_SIZE(flag_array),
217 static void dump_flag_info(struct pg_state *st, const struct flag_info
218 *flag, u64 pte, int num)
222 for (i = 0; i < num; i++, flag++) {
223 const char *s = NULL;
226 /* flag not defined so don't check it */
229 /* Some 'flags' are actually values */
231 val = pte & flag->val;
233 val = val >> flag->shift;
234 seq_printf(st->seq, " %s:%llx", flag->set, val);
236 if ((pte & flag->mask) == flag->val)
241 seq_printf(st->seq, " %s", s);
243 st->current_flags &= ~flag->mask;
245 if (st->current_flags != 0)
246 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
249 static void dump_addr(struct pg_state *st, unsigned long addr)
251 static const char units[] = "KMGTPE";
252 const char *unit = units;
255 seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
256 delta = (addr - st->start_address) >> 10;
257 /* Work out what appropriate unit to use */
258 while (!(delta & 1023) && unit[1]) {
262 seq_printf(st->seq, "%9lu%c", delta, *unit);
266 static void note_page(struct pg_state *st, unsigned long addr,
267 unsigned int level, u64 val)
269 u64 flag = val & pg_level[level].mask;
270 /* At first no level is set */
273 st->current_flags = flag;
274 st->start_address = addr;
275 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
277 * Dump the section of virtual memory when:
278 * - the PTE flags from one entry to the next differs.
279 * - we change levels in the tree.
280 * - the address is in a different section of memory and is thus
281 * used for a different purpose, regardless of the flags.
283 } else if (flag != st->current_flags || level != st->level ||
284 addr >= st->marker[1].start_address) {
286 /* Check the PTE flags */
287 if (st->current_flags) {
290 /* Dump all the flags */
291 if (pg_level[st->level].flag)
292 dump_flag_info(st, pg_level[st->level].flag,
294 pg_level[st->level].num);
296 seq_puts(st->seq, "\n");
300 * Address indicates we have passed the end of the
301 * current section of virtual memory
303 while (addr >= st->marker[1].start_address) {
305 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
307 st->start_address = addr;
308 st->current_flags = flag;
313 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
315 pte_t *pte = pte_offset_kernel(pmd, 0);
319 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
320 addr = start + i * PAGE_SIZE;
321 note_page(st, addr, 4, pte_val(*pte));
326 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
328 pmd_t *pmd = pmd_offset(pud, 0);
332 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
333 addr = start + i * PMD_SIZE;
336 walk_pte(st, pmd, addr);
338 note_page(st, addr, 3, pmd_val(*pmd));
342 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
344 pud_t *pud = pud_offset(pgd, 0);
348 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
349 addr = start + i * PUD_SIZE;
352 walk_pmd(st, pud, addr);
354 note_page(st, addr, 2, pud_val(*pud));
358 static void walk_pagetables(struct pg_state *st)
360 pgd_t *pgd = pgd_offset_k(0UL);
365 * Traverse the linux pagetable structure and dump pages that are in
366 * the hash pagetable.
368 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
369 addr = KERN_VIRT_START + i * PGDIR_SIZE;
372 walk_pud(st, pgd, addr);
374 note_page(st, addr, 1, pgd_val(*pgd));
378 static void populate_markers(void)
380 address_markers[0].start_address = PAGE_OFFSET;
381 address_markers[1].start_address = VMALLOC_START;
382 address_markers[2].start_address = VMALLOC_END;
383 address_markers[3].start_address = ISA_IO_BASE;
384 address_markers[4].start_address = ISA_IO_END;
385 address_markers[5].start_address = PHB_IO_BASE;
386 address_markers[6].start_address = PHB_IO_END;
387 address_markers[7].start_address = IOREMAP_BASE;
388 address_markers[8].start_address = IOREMAP_END;
389 #ifdef CONFIG_PPC_STD_MMU_64
390 address_markers[9].start_address = H_VMEMMAP_BASE;
392 address_markers[9].start_address = VMEMMAP_BASE;
396 static int ptdump_show(struct seq_file *m, void *v)
398 struct pg_state st = {
400 .start_address = KERN_VIRT_START,
401 .marker = address_markers,
403 /* Traverse kernel page tables */
404 walk_pagetables(&st);
405 note_page(&st, 0, 0, 0);
410 static int ptdump_open(struct inode *inode, struct file *file)
412 return single_open(file, ptdump_show, NULL);
415 static const struct file_operations ptdump_fops = {
419 .release = single_release,
422 static void build_pgtable_complete_mask(void)
426 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
427 if (pg_level[i].flag)
428 for (j = 0; j < pg_level[i].num; j++)
429 pg_level[i].mask |= pg_level[i].flag[j].mask;
432 static int ptdump_init(void)
434 struct dentry *debugfs_file;
437 build_pgtable_complete_mask();
438 debugfs_file = debugfs_create_file("kernel_pagetables", 0400, NULL,
440 return debugfs_file ? 0 : -ENOMEM;
442 device_initcall(ptdump_init);