powerpc/mm: Dump linux pagetables
[linux-2.6-block.git] / arch / powerpc / mm / dump_linuxpagetables.c
CommitLineData
8eb07b18
RG
1/*
2 * Copyright 2016, Rashmica Gupta, IBM Corp.
3 *
4 * This traverses the kernel pagetables and dumps the
5 * information about the used sections of memory to
6 * /sys/kernel/debug/kernel_pagetables.
7 *
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.
11 *
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.
16 */
17#include <linux/debugfs.h>
18#include <linux/fs.h>
19#include <linux/io.h>
20#include <linux/mm.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>
26#include <asm/page.h>
27#include <asm/pgalloc.h>
28
29/*
30 * To visualise what is happening,
31 *
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
34 * corresponding P**
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}
38 *
39 *
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
42 * a page.
43 *
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.
46 *
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.
53 *
54 */
55struct pg_state {
56 struct seq_file *seq;
57 const struct addr_marker *marker;
58 unsigned long start_address;
59 unsigned int level;
60 u64 current_flags;
61};
62
63struct addr_marker {
64 unsigned long start_address;
65 const char *name;
66};
67
68static 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" },
73 { 0, "isa I/O end" },
74 { 0, "phb I/O start" },
75 { 0, "phb I/O end" },
76 { 0, "I/O remap start" },
77 { 0, "I/O remap end" },
78 { 0, "vmemmap start" },
79 { -1, NULL },
80};
81
82struct flag_info {
83 u64 mask;
84 u64 val;
85 const char *set;
86 const char *clear;
87 bool is_val;
88 int shift;
89};
90
91static const struct flag_info flag_array[] = {
92 {
93#ifdef CONFIG_PPC_STD_MMU_64
94 .mask = _PAGE_PRIVILEGED,
95 .val = 0,
96#else
97 .mask = _PAGE_USER,
98 .val = _PAGE_USER,
99#endif
100 .set = "user",
101 .clear = " ",
102 }, {
103 .mask = _PAGE_RW,
104 .val = _PAGE_RW,
105 .set = "rw",
106 .clear = "ro",
107 }, {
108 .mask = _PAGE_EXEC,
109 .val = _PAGE_EXEC,
110 .set = " X ",
111 .clear = " ",
112 }, {
113 .mask = _PAGE_PTE,
114 .val = _PAGE_PTE,
115 .set = "pte",
116 .clear = " ",
117 }, {
118 .mask = _PAGE_PRESENT,
119 .val = _PAGE_PRESENT,
120 .set = "present",
121 .clear = " ",
122 }, {
123#ifdef CONFIG_PPC_STD_MMU_64
124 .mask = H_PAGE_HASHPTE,
125 .val = H_PAGE_HASHPTE,
126#else
127 .mask = _PAGE_HASHPTE,
128 .val = _PAGE_HASHPTE,
129#endif
130 .set = "hpte",
131 .clear = " ",
132 }, {
133#ifndef CONFIG_PPC_STD_MMU_64
134 .mask = _PAGE_GUARDED,
135 .val = _PAGE_GUARDED,
136 .set = "guarded",
137 .clear = " ",
138 }, {
139#endif
140 .mask = _PAGE_DIRTY,
141 .val = _PAGE_DIRTY,
142 .set = "dirty",
143 .clear = " ",
144 }, {
145 .mask = _PAGE_ACCESSED,
146 .val = _PAGE_ACCESSED,
147 .set = "accessed",
148 .clear = " ",
149 }, {
150#ifndef CONFIG_PPC_STD_MMU_64
151 .mask = _PAGE_WRITETHRU,
152 .val = _PAGE_WRITETHRU,
153 .set = "write through",
154 .clear = " ",
155 }, {
156#endif
157 .mask = _PAGE_NO_CACHE,
158 .val = _PAGE_NO_CACHE,
159 .set = "no cache",
160 .clear = " ",
161 }, {
162 .mask = H_PAGE_BUSY,
163 .val = H_PAGE_BUSY,
164 .set = "busy",
165 }, {
166#ifdef CONFIG_PPC_64K_PAGES
167 .mask = H_PAGE_COMBO,
168 .val = H_PAGE_COMBO,
169 .set = "combo",
170 }, {
171 .mask = H_PAGE_4K_PFN,
172 .val = H_PAGE_4K_PFN,
173 .set = "4K_pfn",
174 }, {
175#endif
176 .mask = H_PAGE_F_GIX,
177 .val = H_PAGE_F_GIX,
178 .set = "f_gix",
179 .is_val = true,
180 .shift = H_PAGE_F_GIX_SHIFT,
181 }, {
182 .mask = H_PAGE_F_SECOND,
183 .val = H_PAGE_F_SECOND,
184 .set = "f_second",
185 }, {
186 .mask = _PAGE_SPECIAL,
187 .val = _PAGE_SPECIAL,
188 .set = "special",
189 }
190};
191
192struct pgtable_level {
193 const struct flag_info *flag;
194 size_t num;
195 u64 mask;
196};
197
198static struct pgtable_level pg_level[] = {
199 {
200 }, { /* pgd */
201 .flag = flag_array,
202 .num = ARRAY_SIZE(flag_array),
203 }, { /* pud */
204 .flag = flag_array,
205 .num = ARRAY_SIZE(flag_array),
206 }, { /* pmd */
207 .flag = flag_array,
208 .num = ARRAY_SIZE(flag_array),
209 }, { /* pte */
210 .flag = flag_array,
211 .num = ARRAY_SIZE(flag_array),
212 },
213};
214
215static void dump_flag_info(struct pg_state *st, const struct flag_info
216 *flag, u64 pte, int num)
217{
218 unsigned int i;
219
220 for (i = 0; i < num; i++, flag++) {
221 const char *s = NULL;
222 u64 val;
223
224 /* flag not defined so don't check it */
225 if (flag->mask == 0)
226 continue;
227 /* Some 'flags' are actually values */
228 if (flag->is_val) {
229 val = pte & flag->val;
230 if (flag->shift)
231 val = val >> flag->shift;
232 seq_printf(st->seq, " %s:%llx", flag->set, val);
233 } else {
234 if ((pte & flag->mask) == flag->val)
235 s = flag->set;
236 else
237 s = flag->clear;
238 if (s)
239 seq_printf(st->seq, " %s", s);
240 }
241 st->current_flags &= ~flag->mask;
242 }
243 if (st->current_flags != 0)
244 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
245}
246
247static void dump_addr(struct pg_state *st, unsigned long addr)
248{
249 static const char units[] = "KMGTPE";
250 const char *unit = units;
251 unsigned long delta;
252
253 seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
254 delta = (addr - st->start_address) >> 10;
255 /* Work out what appropriate unit to use */
256 while (!(delta & 1023) && unit[1]) {
257 delta >>= 10;
258 unit++;
259 }
260 seq_printf(st->seq, "%9lu%c", delta, *unit);
261
262}
263
264static void note_page(struct pg_state *st, unsigned long addr,
265 unsigned int level, u64 val)
266{
267 u64 flag = val & pg_level[level].mask;
268 /* At first no level is set */
269 if (!st->level) {
270 st->level = level;
271 st->current_flags = flag;
272 st->start_address = addr;
273 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
274 /*
275 * Dump the section of virtual memory when:
276 * - the PTE flags from one entry to the next differs.
277 * - we change levels in the tree.
278 * - the address is in a different section of memory and is thus
279 * used for a different purpose, regardless of the flags.
280 */
281 } else if (flag != st->current_flags || level != st->level ||
282 addr >= st->marker[1].start_address) {
283
284 /* Check the PTE flags */
285 if (st->current_flags) {
286 dump_addr(st, addr);
287
288 /* Dump all the flags */
289 if (pg_level[st->level].flag)
290 dump_flag_info(st, pg_level[st->level].flag,
291 st->current_flags,
292 pg_level[st->level].num);
293
294 seq_puts(st->seq, "\n");
295 }
296
297 /*
298 * Address indicates we have passed the end of the
299 * current section of virtual memory
300 */
301 while (addr >= st->marker[1].start_address) {
302 st->marker++;
303 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
304 }
305 st->start_address = addr;
306 st->current_flags = flag;
307 st->level = level;
308 }
309}
310
311static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
312{
313 pte_t *pte = pte_offset_kernel(pmd, 0);
314 unsigned long addr;
315 unsigned int i;
316
317 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
318 addr = start + i * PAGE_SIZE;
319 note_page(st, addr, 4, pte_val(*pte));
320
321 }
322}
323
324static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
325{
326 pmd_t *pmd = pmd_offset(pud, 0);
327 unsigned long addr;
328 unsigned int i;
329
330 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
331 addr = start + i * PMD_SIZE;
332 if (!pmd_none(*pmd))
333 /* pmd exists */
334 walk_pte(st, pmd, addr);
335 else
336 note_page(st, addr, 3, pmd_val(*pmd));
337 }
338}
339
340static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
341{
342 pud_t *pud = pud_offset(pgd, 0);
343 unsigned long addr;
344 unsigned int i;
345
346 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
347 addr = start + i * PUD_SIZE;
348 if (!pud_none(*pud))
349 /* pud exists */
350 walk_pmd(st, pud, addr);
351 else
352 note_page(st, addr, 2, pud_val(*pud));
353 }
354}
355
356static void walk_pagetables(struct pg_state *st)
357{
358 pgd_t *pgd = pgd_offset_k(0UL);
359 unsigned int i;
360 unsigned long addr;
361
362 /*
363 * Traverse the linux pagetable structure and dump pages that are in
364 * the hash pagetable.
365 */
366 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
367 addr = KERN_VIRT_START + i * PGDIR_SIZE;
368 if (!pgd_none(*pgd))
369 /* pgd exists */
370 walk_pud(st, pgd, addr);
371 else
372 note_page(st, addr, 1, pgd_val(*pgd));
373 }
374}
375
376static void populate_markers(void)
377{
378 address_markers[0].start_address = PAGE_OFFSET;
379 address_markers[1].start_address = VMALLOC_START;
380 address_markers[2].start_address = VMALLOC_END;
381 address_markers[3].start_address = ISA_IO_BASE;
382 address_markers[4].start_address = ISA_IO_END;
383 address_markers[5].start_address = PHB_IO_BASE;
384 address_markers[6].start_address = PHB_IO_END;
385 address_markers[7].start_address = IOREMAP_BASE;
386 address_markers[8].start_address = IOREMAP_END;
387#ifdef CONFIG_PPC_STD_MMU_64
388 address_markers[9].start_address = H_VMEMMAP_BASE;
389#else
390 address_markers[9].start_address = VMEMMAP_BASE;
391#endif
392}
393
394static int ptdump_show(struct seq_file *m, void *v)
395{
396 struct pg_state st = {
397 .seq = m,
398 .start_address = KERN_VIRT_START,
399 .marker = address_markers,
400 };
401 /* Traverse kernel page tables */
402 walk_pagetables(&st);
403 note_page(&st, 0, 0, 0);
404 return 0;
405}
406
407
408static int ptdump_open(struct inode *inode, struct file *file)
409{
410 return single_open(file, ptdump_show, NULL);
411}
412
413static const struct file_operations ptdump_fops = {
414 .open = ptdump_open,
415 .read = seq_read,
416 .llseek = seq_lseek,
417 .release = single_release,
418};
419
420static void build_pgtable_complete_mask(void)
421{
422 unsigned int i, j;
423
424 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
425 if (pg_level[i].flag)
426 for (j = 0; j < pg_level[i].num; j++)
427 pg_level[i].mask |= pg_level[i].flag[j].mask;
428}
429
430static int ptdump_init(void)
431{
432 struct dentry *debugfs_file;
433
434 populate_markers();
435 build_pgtable_complete_mask();
436 debugfs_file = debugfs_create_file("kernel_pagetables", 0400, NULL,
437 NULL, &ptdump_fops);
438 return debugfs_file ? 0 : -ENOMEM;
439}
440device_initcall(ptdump_init);