x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / arch / powerpc / mm / dump_linuxpagetables.c
blobe1b6c8bf062714909a10a8660332ba89c4c77411
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>
31 * To visualise what is happening,
33 * - PTRS_PER_P** = how many entries there are in the corresponding P**
34 * - P**_SHIFT = how many bits of the address we use to index into the
35 * corresponding P**
36 * - P**_SIZE is how much memory we can access through the table - not the
37 * size of the table itself.
38 * P**={PGD, PUD, PMD, PTE}
41 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
42 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
43 * a page.
45 * In the case where there are only 3 levels, the PUD is folded into the
46 * PGD: every PUD has only one entry which points to the PMD.
48 * The page dumper groups page table entries of the same type into a single
49 * description. It uses pg_state to track the range information while
50 * iterating over the PTE entries. When the continuity is broken it then
51 * dumps out a description of the range - ie PTEs that are virtually contiguous
52 * with the same PTE flags are chunked together. This is to make it clear how
53 * different areas of the kernel virtual memory are used.
56 struct pg_state {
57 struct seq_file *seq;
58 const struct addr_marker *marker;
59 unsigned long start_address;
60 unsigned long start_pa;
61 unsigned long last_pa;
62 unsigned int level;
63 u64 current_flags;
66 struct addr_marker {
67 unsigned long start_address;
68 const char *name;
71 static struct addr_marker address_markers[] = {
72 { 0, "Start of kernel VM" },
73 { 0, "vmalloc() Area" },
74 { 0, "vmalloc() End" },
75 { 0, "isa I/O start" },
76 { 0, "isa I/O end" },
77 { 0, "phb I/O start" },
78 { 0, "phb I/O end" },
79 { 0, "I/O remap start" },
80 { 0, "I/O remap end" },
81 { 0, "vmemmap start" },
82 { -1, NULL },
85 struct flag_info {
86 u64 mask;
87 u64 val;
88 const char *set;
89 const char *clear;
90 bool is_val;
91 int shift;
94 static const struct flag_info flag_array[] = {
96 #ifdef CONFIG_PPC_STD_MMU_64
97 .mask = _PAGE_PRIVILEGED,
98 .val = 0,
99 #else
100 .mask = _PAGE_USER,
101 .val = _PAGE_USER,
102 #endif
103 .set = "user",
104 .clear = " ",
105 }, {
106 .mask = _PAGE_RW,
107 .val = _PAGE_RW,
108 .set = "rw",
109 .clear = "ro",
110 }, {
111 .mask = _PAGE_EXEC,
112 .val = _PAGE_EXEC,
113 .set = " X ",
114 .clear = " ",
115 }, {
116 .mask = _PAGE_PTE,
117 .val = _PAGE_PTE,
118 .set = "pte",
119 .clear = " ",
120 }, {
121 .mask = _PAGE_PRESENT,
122 .val = _PAGE_PRESENT,
123 .set = "present",
124 .clear = " ",
125 }, {
126 #ifdef CONFIG_PPC_STD_MMU_64
127 .mask = H_PAGE_HASHPTE,
128 .val = H_PAGE_HASHPTE,
129 #else
130 .mask = _PAGE_HASHPTE,
131 .val = _PAGE_HASHPTE,
132 #endif
133 .set = "hpte",
134 .clear = " ",
135 }, {
136 #ifndef CONFIG_PPC_STD_MMU_64
137 .mask = _PAGE_GUARDED,
138 .val = _PAGE_GUARDED,
139 .set = "guarded",
140 .clear = " ",
141 }, {
142 #endif
143 .mask = _PAGE_DIRTY,
144 .val = _PAGE_DIRTY,
145 .set = "dirty",
146 .clear = " ",
147 }, {
148 .mask = _PAGE_ACCESSED,
149 .val = _PAGE_ACCESSED,
150 .set = "accessed",
151 .clear = " ",
152 }, {
153 #ifndef CONFIG_PPC_STD_MMU_64
154 .mask = _PAGE_WRITETHRU,
155 .val = _PAGE_WRITETHRU,
156 .set = "write through",
157 .clear = " ",
158 }, {
159 #endif
160 .mask = _PAGE_NO_CACHE,
161 .val = _PAGE_NO_CACHE,
162 .set = "no cache",
163 .clear = " ",
164 }, {
165 #ifdef CONFIG_PPC_BOOK3S_64
166 .mask = H_PAGE_BUSY,
167 .val = H_PAGE_BUSY,
168 .set = "busy",
169 }, {
170 #ifdef CONFIG_PPC_64K_PAGES
171 .mask = H_PAGE_COMBO,
172 .val = H_PAGE_COMBO,
173 .set = "combo",
174 }, {
175 .mask = H_PAGE_4K_PFN,
176 .val = H_PAGE_4K_PFN,
177 .set = "4K_pfn",
178 }, {
179 #endif
180 .mask = H_PAGE_F_GIX,
181 .val = H_PAGE_F_GIX,
182 .set = "f_gix",
183 .is_val = true,
184 .shift = H_PAGE_F_GIX_SHIFT,
185 }, {
186 .mask = H_PAGE_F_SECOND,
187 .val = H_PAGE_F_SECOND,
188 .set = "f_second",
189 }, {
190 #endif
191 .mask = _PAGE_SPECIAL,
192 .val = _PAGE_SPECIAL,
193 .set = "special",
197 struct pgtable_level {
198 const struct flag_info *flag;
199 size_t num;
200 u64 mask;
203 static struct pgtable_level pg_level[] = {
205 }, { /* pgd */
206 .flag = flag_array,
207 .num = ARRAY_SIZE(flag_array),
208 }, { /* pud */
209 .flag = flag_array,
210 .num = ARRAY_SIZE(flag_array),
211 }, { /* pmd */
212 .flag = flag_array,
213 .num = ARRAY_SIZE(flag_array),
214 }, { /* pte */
215 .flag = flag_array,
216 .num = ARRAY_SIZE(flag_array),
220 static void dump_flag_info(struct pg_state *st, const struct flag_info
221 *flag, u64 pte, int num)
223 unsigned int i;
225 for (i = 0; i < num; i++, flag++) {
226 const char *s = NULL;
227 u64 val;
229 /* flag not defined so don't check it */
230 if (flag->mask == 0)
231 continue;
232 /* Some 'flags' are actually values */
233 if (flag->is_val) {
234 val = pte & flag->val;
235 if (flag->shift)
236 val = val >> flag->shift;
237 seq_printf(st->seq, " %s:%llx", flag->set, val);
238 } else {
239 if ((pte & flag->mask) == flag->val)
240 s = flag->set;
241 else
242 s = flag->clear;
243 if (s)
244 seq_printf(st->seq, " %s", s);
246 st->current_flags &= ~flag->mask;
248 if (st->current_flags != 0)
249 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
252 static void dump_addr(struct pg_state *st, unsigned long addr)
254 static const char units[] = "KMGTPE";
255 const char *unit = units;
256 unsigned long delta;
258 seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
259 seq_printf(st->seq, "0x%016lx ", st->start_pa);
261 delta = (addr - st->start_address) >> 10;
262 /* Work out what appropriate unit to use */
263 while (!(delta & 1023) && unit[1]) {
264 delta >>= 10;
265 unit++;
267 seq_printf(st->seq, "%9lu%c", delta, *unit);
271 static void note_page(struct pg_state *st, unsigned long addr,
272 unsigned int level, u64 val)
274 u64 flag = val & pg_level[level].mask;
275 u64 pa = val & PTE_RPN_MASK;
277 /* At first no level is set */
278 if (!st->level) {
279 st->level = level;
280 st->current_flags = flag;
281 st->start_address = addr;
282 st->start_pa = pa;
283 st->last_pa = pa;
284 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
286 * Dump the section of virtual memory when:
287 * - the PTE flags from one entry to the next differs.
288 * - we change levels in the tree.
289 * - the address is in a different section of memory and is thus
290 * used for a different purpose, regardless of the flags.
291 * - the pa of this page is not adjacent to the last inspected page
293 } else if (flag != st->current_flags || level != st->level ||
294 addr >= st->marker[1].start_address ||
295 pa != st->last_pa + PAGE_SIZE) {
297 /* Check the PTE flags */
298 if (st->current_flags) {
299 dump_addr(st, addr);
301 /* Dump all the flags */
302 if (pg_level[st->level].flag)
303 dump_flag_info(st, pg_level[st->level].flag,
304 st->current_flags,
305 pg_level[st->level].num);
307 seq_puts(st->seq, "\n");
311 * Address indicates we have passed the end of the
312 * current section of virtual memory
314 while (addr >= st->marker[1].start_address) {
315 st->marker++;
316 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
318 st->start_address = addr;
319 st->start_pa = pa;
320 st->last_pa = pa;
321 st->current_flags = flag;
322 st->level = level;
323 } else {
324 st->last_pa = pa;
328 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
330 pte_t *pte = pte_offset_kernel(pmd, 0);
331 unsigned long addr;
332 unsigned int i;
334 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
335 addr = start + i * PAGE_SIZE;
336 note_page(st, addr, 4, pte_val(*pte));
341 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
343 pmd_t *pmd = pmd_offset(pud, 0);
344 unsigned long addr;
345 unsigned int i;
347 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
348 addr = start + i * PMD_SIZE;
349 if (!pmd_none(*pmd) && !pmd_huge(*pmd))
350 /* pmd exists */
351 walk_pte(st, pmd, addr);
352 else
353 note_page(st, addr, 3, pmd_val(*pmd));
357 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
359 pud_t *pud = pud_offset(pgd, 0);
360 unsigned long addr;
361 unsigned int i;
363 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
364 addr = start + i * PUD_SIZE;
365 if (!pud_none(*pud) && !pud_huge(*pud))
366 /* pud exists */
367 walk_pmd(st, pud, addr);
368 else
369 note_page(st, addr, 2, pud_val(*pud));
373 static void walk_pagetables(struct pg_state *st)
375 pgd_t *pgd = pgd_offset_k(0UL);
376 unsigned int i;
377 unsigned long addr;
380 * Traverse the linux pagetable structure and dump pages that are in
381 * the hash pagetable.
383 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
384 addr = KERN_VIRT_START + i * PGDIR_SIZE;
385 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
386 /* pgd exists */
387 walk_pud(st, pgd, addr);
388 else
389 note_page(st, addr, 1, pgd_val(*pgd));
393 static void populate_markers(void)
395 address_markers[0].start_address = PAGE_OFFSET;
396 address_markers[1].start_address = VMALLOC_START;
397 address_markers[2].start_address = VMALLOC_END;
398 address_markers[3].start_address = ISA_IO_BASE;
399 address_markers[4].start_address = ISA_IO_END;
400 address_markers[5].start_address = PHB_IO_BASE;
401 address_markers[6].start_address = PHB_IO_END;
402 address_markers[7].start_address = IOREMAP_BASE;
403 address_markers[8].start_address = IOREMAP_END;
404 #ifdef CONFIG_PPC_STD_MMU_64
405 address_markers[9].start_address = H_VMEMMAP_BASE;
406 #else
407 address_markers[9].start_address = VMEMMAP_BASE;
408 #endif
411 static int ptdump_show(struct seq_file *m, void *v)
413 struct pg_state st = {
414 .seq = m,
415 .start_address = KERN_VIRT_START,
416 .marker = address_markers,
418 /* Traverse kernel page tables */
419 walk_pagetables(&st);
420 note_page(&st, 0, 0, 0);
421 return 0;
425 static int ptdump_open(struct inode *inode, struct file *file)
427 return single_open(file, ptdump_show, NULL);
430 static const struct file_operations ptdump_fops = {
431 .open = ptdump_open,
432 .read = seq_read,
433 .llseek = seq_lseek,
434 .release = single_release,
437 static void build_pgtable_complete_mask(void)
439 unsigned int i, j;
441 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
442 if (pg_level[i].flag)
443 for (j = 0; j < pg_level[i].num; j++)
444 pg_level[i].mask |= pg_level[i].flag[j].mask;
447 static int ptdump_init(void)
449 struct dentry *debugfs_file;
451 populate_markers();
452 build_pgtable_complete_mask();
453 debugfs_file = debugfs_create_file("kernel_pagetables", 0400, NULL,
454 NULL, &ptdump_fops);
455 return debugfs_file ? 0 : -ENOMEM;
457 device_initcall(ptdump_init);