1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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>
29
30#ifdef CONFIG_PPC32
31#define KERN_VIRT_START 0
32#endif
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60struct 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;
68};
69
70struct addr_marker {
71 unsigned long start_address;
72 const char *name;
73};
74
75static 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 },
102};
103
104struct flag_info {
105 u64 mask;
106 u64 val;
107 const char *set;
108 const char *clear;
109 bool is_val;
110 int shift;
111};
112
113static const struct flag_info flag_array[] = {
114 {
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
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
227#endif
228 .mask = _PAGE_SPECIAL,
229 .val = _PAGE_SPECIAL,
230 .set = "special",
231 }
232};
233
234struct pgtable_level {
235 const struct flag_info *flag;
236 size_t num;
237 u64 mask;
238};
239
240static struct pgtable_level pg_level[] = {
241 {
242 }, {
243 .flag = flag_array,
244 .num = ARRAY_SIZE(flag_array),
245 }, {
246 .flag = flag_array,
247 .num = ARRAY_SIZE(flag_array),
248 }, {
249 .flag = flag_array,
250 .num = ARRAY_SIZE(flag_array),
251 }, {
252 .flag = flag_array,
253 .num = ARRAY_SIZE(flag_array),
254 },
255};
256
257static void dump_flag_info(struct pg_state *st, const struct flag_info
258 *flag, u64 pte, int num)
259{
260 unsigned int i;
261
262 for (i = 0; i < num; i++, flag++) {
263 const char *s = NULL;
264 u64 val;
265
266
267 if (flag->mask == 0)
268 continue;
269
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);
282 }
283 st->current_flags &= ~flag->mask;
284 }
285 if (st->current_flags != 0)
286 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
287}
288
289static void dump_addr(struct pg_state *st, unsigned long addr)
290{
291 static const char units[] = "KMGTPE";
292 const char *unit = units;
293 unsigned long delta;
294
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
302
303 delta = (addr - st->start_address) >> 10;
304
305 while (!(delta & 1023) && unit[1]) {
306 delta >>= 10;
307 unit++;
308 }
309 seq_printf(st->seq, "%9lu%c", delta, *unit);
310
311}
312
313static void note_page(struct pg_state *st, unsigned long addr,
314 unsigned int level, u64 val)
315{
316 u64 flag = val & pg_level[level].mask;
317 u64 pa = val & PTE_RPN_MASK;
318
319
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);
327
328
329
330
331
332
333
334
335 } else if (flag != st->current_flags || level != st->level ||
336 addr >= st->marker[1].start_address ||
337 pa != st->last_pa + PAGE_SIZE) {
338
339
340 if (st->current_flags) {
341 dump_addr(st, addr);
342
343
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);
348
349 seq_putc(st->seq, '\n');
350 }
351
352
353
354
355
356 while (addr >= st->marker[1].start_address) {
357 st->marker++;
358 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
359 }
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;
367 }
368}
369
370static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
371{
372 pte_t *pte = pte_offset_kernel(pmd, 0);
373 unsigned long addr;
374 unsigned int i;
375
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));
379
380 }
381}
382
383static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
384{
385 pmd_t *pmd = pmd_offset(pud, 0);
386 unsigned long addr;
387 unsigned int i;
388
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
393 walk_pte(st, pmd, addr);
394 else
395 note_page(st, addr, 3, pmd_val(*pmd));
396 }
397}
398
399static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
400{
401 pud_t *pud = pud_offset(pgd, 0);
402 unsigned long addr;
403 unsigned int i;
404
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
409 walk_pmd(st, pud, addr);
410 else
411 note_page(st, addr, 2, pud_val(*pud));
412 }
413}
414
415static void walk_pagetables(struct pg_state *st)
416{
417 pgd_t *pgd = pgd_offset_k(0UL);
418 unsigned int i;
419 unsigned long addr;
420
421
422
423
424
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
429 walk_pud(st, pgd, addr);
430 else
431 note_page(st, addr, 1, pgd_val(*pgd));
432 }
433}
434
435static void populate_markers(void)
436{
437 int i = 0;
438
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
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
469}
470
471static int ptdump_show(struct seq_file *m, void *v)
472{
473 struct pg_state st = {
474 .seq = m,
475 .start_address = KERN_VIRT_START,
476 .marker = address_markers,
477 };
478
479 walk_pagetables(&st);
480 note_page(&st, 0, 0, 0);
481 return 0;
482}
483
484
485static int ptdump_open(struct inode *inode, struct file *file)
486{
487 return single_open(file, ptdump_show, NULL);
488}
489
490static const struct file_operations ptdump_fops = {
491 .open = ptdump_open,
492 .read = seq_read,
493 .llseek = seq_lseek,
494 .release = single_release,
495};
496
497static void build_pgtable_complete_mask(void)
498{
499 unsigned int i, j;
500
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;
505}
506
507static int ptdump_init(void)
508{
509 struct dentry *debugfs_file;
510
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;
516}
517device_initcall(ptdump_init);
518