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