Linux 4.18.8
[linux/fpc-iii.git] / arch / powerpc / mm / dump_linuxpagetables.c
blob876e2a3c79f201eb1a86187814efdf35d911e00b
1 /*
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
15 * of the License.
17 #include <linux/debugfs.h>
18 #include <linux/fs.h>
19 #include <linux/hugetlb.h>
20 #include <linux/io.h>
21 #include <linux/mm.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <asm/fixmap.h>
25 #include <asm/pgtable.h>
26 #include <linux/const.h>
27 #include <asm/page.h>
28 #include <asm/pgalloc.h>
30 #ifdef CONFIG_PPC32
31 #define KERN_VIRT_START 0
32 #endif
35 * To visualise what is happening,
37 * - PTRS_PER_P** = how many entries there are in the corresponding P**
38 * - P**_SHIFT = how many bits of the address we use to index into the
39 * corresponding P**
40 * - P**_SIZE is how much memory we can access through the table - not the
41 * size of the table itself.
42 * P**={PGD, PUD, PMD, PTE}
45 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
46 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
47 * a page.
49 * In the case where there are only 3 levels, the PUD is folded into the
50 * PGD: every PUD has only one entry which points to the PMD.
52 * The page dumper groups page table entries of the same type into a single
53 * description. It uses pg_state to track the range information while
54 * iterating over the PTE entries. When the continuity is broken it then
55 * dumps out a description of the range - ie PTEs that are virtually contiguous
56 * with the same PTE flags are chunked together. This is to make it clear how
57 * different areas of the kernel virtual memory are used.
60 struct pg_state {
61 struct seq_file *seq;
62 const struct addr_marker *marker;
63 unsigned long start_address;
64 unsigned long start_pa;
65 unsigned long last_pa;
66 unsigned int level;
67 u64 current_flags;
70 struct addr_marker {
71 unsigned long start_address;
72 const char *name;
75 static struct addr_marker address_markers[] = {
76 { 0, "Start of kernel VM" },
77 { 0, "vmalloc() Area" },
78 { 0, "vmalloc() End" },
79 #ifdef CONFIG_PPC64
80 { 0, "isa I/O start" },
81 { 0, "isa I/O end" },
82 { 0, "phb I/O start" },
83 { 0, "phb I/O end" },
84 { 0, "I/O remap start" },
85 { 0, "I/O remap end" },
86 { 0, "vmemmap start" },
87 #else
88 { 0, "Early I/O remap start" },
89 { 0, "Early I/O remap end" },
90 #ifdef CONFIG_NOT_COHERENT_CACHE
91 { 0, "Consistent mem start" },
92 { 0, "Consistent mem end" },
93 #endif
94 #ifdef CONFIG_HIGHMEM
95 { 0, "Highmem PTEs start" },
96 { 0, "Highmem PTEs end" },
97 #endif
98 { 0, "Fixmap start" },
99 { 0, "Fixmap end" },
100 #endif
101 { -1, NULL },
104 struct flag_info {
105 u64 mask;
106 u64 val;
107 const char *set;
108 const char *clear;
109 bool is_val;
110 int shift;
113 static const struct flag_info flag_array[] = {
115 .mask = _PAGE_USER | _PAGE_PRIVILEGED,
116 .val = _PAGE_USER,
117 .set = "user",
118 .clear = " ",
119 }, {
120 .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
121 .val = _PAGE_RW,
122 .set = "rw",
123 }, {
124 .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
125 .val = _PAGE_RO,
126 .set = "ro",
127 }, {
128 #if _PAGE_NA != 0
129 .mask = _PAGE_RW | _PAGE_RO | _PAGE_NA,
130 .val = _PAGE_RO,
131 .set = "na",
132 }, {
133 #endif
134 .mask = _PAGE_EXEC,
135 .val = _PAGE_EXEC,
136 .set = " X ",
137 .clear = " ",
138 }, {
139 .mask = _PAGE_PTE,
140 .val = _PAGE_PTE,
141 .set = "pte",
142 .clear = " ",
143 }, {
144 .mask = _PAGE_PRESENT,
145 .val = _PAGE_PRESENT,
146 .set = "present",
147 .clear = " ",
148 }, {
149 #ifdef CONFIG_PPC_BOOK3S_64
150 .mask = H_PAGE_HASHPTE,
151 .val = H_PAGE_HASHPTE,
152 #else
153 .mask = _PAGE_HASHPTE,
154 .val = _PAGE_HASHPTE,
155 #endif
156 .set = "hpte",
157 .clear = " ",
158 }, {
159 #ifndef CONFIG_PPC_BOOK3S_64
160 .mask = _PAGE_GUARDED,
161 .val = _PAGE_GUARDED,
162 .set = "guarded",
163 .clear = " ",
164 }, {
165 #endif
166 .mask = _PAGE_DIRTY,
167 .val = _PAGE_DIRTY,
168 .set = "dirty",
169 .clear = " ",
170 }, {
171 .mask = _PAGE_ACCESSED,
172 .val = _PAGE_ACCESSED,
173 .set = "accessed",
174 .clear = " ",
175 }, {
176 #ifndef CONFIG_PPC_BOOK3S_64
177 .mask = _PAGE_WRITETHRU,
178 .val = _PAGE_WRITETHRU,
179 .set = "write through",
180 .clear = " ",
181 }, {
182 #endif
183 #ifndef CONFIG_PPC_BOOK3S_64
184 .mask = _PAGE_NO_CACHE,
185 .val = _PAGE_NO_CACHE,
186 .set = "no cache",
187 .clear = " ",
188 }, {
189 #else
190 .mask = _PAGE_NON_IDEMPOTENT,
191 .val = _PAGE_NON_IDEMPOTENT,
192 .set = "non-idempotent",
193 .clear = " ",
194 }, {
195 .mask = _PAGE_TOLERANT,
196 .val = _PAGE_TOLERANT,
197 .set = "tolerant",
198 .clear = " ",
199 }, {
200 #endif
201 #ifdef CONFIG_PPC_BOOK3S_64
202 .mask = H_PAGE_BUSY,
203 .val = H_PAGE_BUSY,
204 .set = "busy",
205 }, {
206 #ifdef CONFIG_PPC_64K_PAGES
207 .mask = H_PAGE_COMBO,
208 .val = H_PAGE_COMBO,
209 .set = "combo",
210 }, {
211 .mask = H_PAGE_4K_PFN,
212 .val = H_PAGE_4K_PFN,
213 .set = "4K_pfn",
214 }, {
215 #else /* CONFIG_PPC_64K_PAGES */
216 .mask = H_PAGE_F_GIX,
217 .val = H_PAGE_F_GIX,
218 .set = "f_gix",
219 .is_val = true,
220 .shift = H_PAGE_F_GIX_SHIFT,
221 }, {
222 .mask = H_PAGE_F_SECOND,
223 .val = H_PAGE_F_SECOND,
224 .set = "f_second",
225 }, {
226 #endif /* CONFIG_PPC_64K_PAGES */
227 #endif
228 .mask = _PAGE_SPECIAL,
229 .val = _PAGE_SPECIAL,
230 .set = "special",
234 struct pgtable_level {
235 const struct flag_info *flag;
236 size_t num;
237 u64 mask;
240 static struct pgtable_level pg_level[] = {
242 }, { /* pgd */
243 .flag = flag_array,
244 .num = ARRAY_SIZE(flag_array),
245 }, { /* pud */
246 .flag = flag_array,
247 .num = ARRAY_SIZE(flag_array),
248 }, { /* pmd */
249 .flag = flag_array,
250 .num = ARRAY_SIZE(flag_array),
251 }, { /* pte */
252 .flag = flag_array,
253 .num = ARRAY_SIZE(flag_array),
257 static void dump_flag_info(struct pg_state *st, const struct flag_info
258 *flag, u64 pte, int num)
260 unsigned int i;
262 for (i = 0; i < num; i++, flag++) {
263 const char *s = NULL;
264 u64 val;
266 /* flag not defined so don't check it */
267 if (flag->mask == 0)
268 continue;
269 /* Some 'flags' are actually values */
270 if (flag->is_val) {
271 val = pte & flag->val;
272 if (flag->shift)
273 val = val >> flag->shift;
274 seq_printf(st->seq, " %s:%llx", flag->set, val);
275 } else {
276 if ((pte & flag->mask) == flag->val)
277 s = flag->set;
278 else
279 s = flag->clear;
280 if (s)
281 seq_printf(st->seq, " %s", s);
283 st->current_flags &= ~flag->mask;
285 if (st->current_flags != 0)
286 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
289 static void dump_addr(struct pg_state *st, unsigned long addr)
291 static const char units[] = "KMGTPE";
292 const char *unit = units;
293 unsigned long delta;
295 #ifdef CONFIG_PPC64
296 seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
297 seq_printf(st->seq, "0x%016lx ", st->start_pa);
298 #else
299 seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
300 seq_printf(st->seq, "0x%08lx ", st->start_pa);
301 #endif
303 delta = (addr - st->start_address) >> 10;
304 /* Work out what appropriate unit to use */
305 while (!(delta & 1023) && unit[1]) {
306 delta >>= 10;
307 unit++;
309 seq_printf(st->seq, "%9lu%c", delta, *unit);
313 static void note_page(struct pg_state *st, unsigned long addr,
314 unsigned int level, u64 val)
316 u64 flag = val & pg_level[level].mask;
317 u64 pa = val & PTE_RPN_MASK;
319 /* At first no level is set */
320 if (!st->level) {
321 st->level = level;
322 st->current_flags = flag;
323 st->start_address = addr;
324 st->start_pa = pa;
325 st->last_pa = pa;
326 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
328 * Dump the section of virtual memory when:
329 * - the PTE flags from one entry to the next differs.
330 * - we change levels in the tree.
331 * - the address is in a different section of memory and is thus
332 * used for a different purpose, regardless of the flags.
333 * - the pa of this page is not adjacent to the last inspected page
335 } else if (flag != st->current_flags || level != st->level ||
336 addr >= st->marker[1].start_address ||
337 pa != st->last_pa + PAGE_SIZE) {
339 /* Check the PTE flags */
340 if (st->current_flags) {
341 dump_addr(st, addr);
343 /* Dump all the flags */
344 if (pg_level[st->level].flag)
345 dump_flag_info(st, pg_level[st->level].flag,
346 st->current_flags,
347 pg_level[st->level].num);
349 seq_putc(st->seq, '\n');
353 * Address indicates we have passed the end of the
354 * current section of virtual memory
356 while (addr >= st->marker[1].start_address) {
357 st->marker++;
358 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
360 st->start_address = addr;
361 st->start_pa = pa;
362 st->last_pa = pa;
363 st->current_flags = flag;
364 st->level = level;
365 } else {
366 st->last_pa = pa;
370 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
372 pte_t *pte = pte_offset_kernel(pmd, 0);
373 unsigned long addr;
374 unsigned int i;
376 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
377 addr = start + i * PAGE_SIZE;
378 note_page(st, addr, 4, pte_val(*pte));
383 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
385 pmd_t *pmd = pmd_offset(pud, 0);
386 unsigned long addr;
387 unsigned int i;
389 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
390 addr = start + i * PMD_SIZE;
391 if (!pmd_none(*pmd) && !pmd_huge(*pmd))
392 /* pmd exists */
393 walk_pte(st, pmd, addr);
394 else
395 note_page(st, addr, 3, pmd_val(*pmd));
399 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
401 pud_t *pud = pud_offset(pgd, 0);
402 unsigned long addr;
403 unsigned int i;
405 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
406 addr = start + i * PUD_SIZE;
407 if (!pud_none(*pud) && !pud_huge(*pud))
408 /* pud exists */
409 walk_pmd(st, pud, addr);
410 else
411 note_page(st, addr, 2, pud_val(*pud));
415 static void walk_pagetables(struct pg_state *st)
417 pgd_t *pgd = pgd_offset_k(0UL);
418 unsigned int i;
419 unsigned long addr;
422 * Traverse the linux pagetable structure and dump pages that are in
423 * the hash pagetable.
425 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
426 addr = KERN_VIRT_START + i * PGDIR_SIZE;
427 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
428 /* pgd exists */
429 walk_pud(st, pgd, addr);
430 else
431 note_page(st, addr, 1, pgd_val(*pgd));
435 static void populate_markers(void)
437 int i = 0;
439 address_markers[i++].start_address = PAGE_OFFSET;
440 address_markers[i++].start_address = VMALLOC_START;
441 address_markers[i++].start_address = VMALLOC_END;
442 #ifdef CONFIG_PPC64
443 address_markers[i++].start_address = ISA_IO_BASE;
444 address_markers[i++].start_address = ISA_IO_END;
445 address_markers[i++].start_address = PHB_IO_BASE;
446 address_markers[i++].start_address = PHB_IO_END;
447 address_markers[i++].start_address = IOREMAP_BASE;
448 address_markers[i++].start_address = IOREMAP_END;
449 #ifdef CONFIG_PPC_BOOK3S_64
450 address_markers[i++].start_address = H_VMEMMAP_BASE;
451 #else
452 address_markers[i++].start_address = VMEMMAP_BASE;
453 #endif
454 #else /* !CONFIG_PPC64 */
455 address_markers[i++].start_address = ioremap_bot;
456 address_markers[i++].start_address = IOREMAP_TOP;
457 #ifdef CONFIG_NOT_COHERENT_CACHE
458 address_markers[i++].start_address = IOREMAP_TOP;
459 address_markers[i++].start_address = IOREMAP_TOP +
460 CONFIG_CONSISTENT_SIZE;
461 #endif
462 #ifdef CONFIG_HIGHMEM
463 address_markers[i++].start_address = PKMAP_BASE;
464 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
465 #endif
466 address_markers[i++].start_address = FIXADDR_START;
467 address_markers[i++].start_address = FIXADDR_TOP;
468 #endif /* CONFIG_PPC64 */
471 static int ptdump_show(struct seq_file *m, void *v)
473 struct pg_state st = {
474 .seq = m,
475 .start_address = KERN_VIRT_START,
476 .marker = address_markers,
478 /* Traverse kernel page tables */
479 walk_pagetables(&st);
480 note_page(&st, 0, 0, 0);
481 return 0;
485 static int ptdump_open(struct inode *inode, struct file *file)
487 return single_open(file, ptdump_show, NULL);
490 static const struct file_operations ptdump_fops = {
491 .open = ptdump_open,
492 .read = seq_read,
493 .llseek = seq_lseek,
494 .release = single_release,
497 static void build_pgtable_complete_mask(void)
499 unsigned int i, j;
501 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
502 if (pg_level[i].flag)
503 for (j = 0; j < pg_level[i].num; j++)
504 pg_level[i].mask |= pg_level[i].flag[j].mask;
507 static int ptdump_init(void)
509 struct dentry *debugfs_file;
511 populate_markers();
512 build_pgtable_complete_mask();
513 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
514 NULL, &ptdump_fops);
515 return debugfs_file ? 0 : -ENOMEM;
517 device_initcall(ptdump_init);