GNU Linux-libre 4.14.302-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                 .mask   = _PAGE_RW | _PAGE_RO,
127                 .val    = _PAGE_RO,
128                 .set    = "ro",
129                 .clear  = "rw",
130         }, {
131                 .mask   = _PAGE_EXEC,
132                 .val    = _PAGE_EXEC,
133                 .set    = " X ",
134                 .clear  = "   ",
135         }, {
136                 .mask   = _PAGE_PTE,
137                 .val    = _PAGE_PTE,
138                 .set    = "pte",
139                 .clear  = "   ",
140         }, {
141                 .mask   = _PAGE_PRESENT,
142                 .val    = _PAGE_PRESENT,
143                 .set    = "present",
144                 .clear  = "       ",
145         }, {
146 #ifdef CONFIG_PPC_STD_MMU_64
147                 .mask   = H_PAGE_HASHPTE,
148                 .val    = H_PAGE_HASHPTE,
149 #else
150                 .mask   = _PAGE_HASHPTE,
151                 .val    = _PAGE_HASHPTE,
152 #endif
153                 .set    = "hpte",
154                 .clear  = "    ",
155         }, {
156 #ifndef CONFIG_PPC_STD_MMU_64
157                 .mask   = _PAGE_GUARDED,
158                 .val    = _PAGE_GUARDED,
159                 .set    = "guarded",
160                 .clear  = "       ",
161         }, {
162 #endif
163                 .mask   = _PAGE_DIRTY,
164                 .val    = _PAGE_DIRTY,
165                 .set    = "dirty",
166                 .clear  = "     ",
167         }, {
168                 .mask   = _PAGE_ACCESSED,
169                 .val    = _PAGE_ACCESSED,
170                 .set    = "accessed",
171                 .clear  = "        ",
172         }, {
173 #ifndef CONFIG_PPC_STD_MMU_64
174                 .mask   = _PAGE_WRITETHRU,
175                 .val    = _PAGE_WRITETHRU,
176                 .set    = "write through",
177                 .clear  = "             ",
178         }, {
179 #endif
180 #ifndef CONFIG_PPC_BOOK3S_64
181                 .mask   = _PAGE_NO_CACHE,
182                 .val    = _PAGE_NO_CACHE,
183                 .set    = "no cache",
184                 .clear  = "        ",
185         }, {
186 #else
187                 .mask   = _PAGE_NON_IDEMPOTENT,
188                 .val    = _PAGE_NON_IDEMPOTENT,
189                 .set    = "non-idempotent",
190                 .clear  = "              ",
191         }, {
192                 .mask   = _PAGE_TOLERANT,
193                 .val    = _PAGE_TOLERANT,
194                 .set    = "tolerant",
195                 .clear  = "        ",
196         }, {
197 #endif
198 #ifdef CONFIG_PPC_BOOK3S_64
199                 .mask   = H_PAGE_BUSY,
200                 .val    = H_PAGE_BUSY,
201                 .set    = "busy",
202         }, {
203 #ifdef CONFIG_PPC_64K_PAGES
204                 .mask   = H_PAGE_COMBO,
205                 .val    = H_PAGE_COMBO,
206                 .set    = "combo",
207         }, {
208                 .mask   = H_PAGE_4K_PFN,
209                 .val    = H_PAGE_4K_PFN,
210                 .set    = "4K_pfn",
211         }, {
212 #endif
213                 .mask   = H_PAGE_F_GIX,
214                 .val    = H_PAGE_F_GIX,
215                 .set    = "f_gix",
216                 .is_val = true,
217                 .shift  = H_PAGE_F_GIX_SHIFT,
218         }, {
219                 .mask   = H_PAGE_F_SECOND,
220                 .val    = H_PAGE_F_SECOND,
221                 .set    = "f_second",
222         }, {
223 #endif
224                 .mask   = _PAGE_SPECIAL,
225                 .val    = _PAGE_SPECIAL,
226                 .set    = "special",
227         }, {
228                 .mask   = _PAGE_SHARED,
229                 .val    = _PAGE_SHARED,
230                 .set    = "shared",
231         }
232 };
233
234 struct pgtable_level {
235         const struct flag_info *flag;
236         size_t num;
237         u64 mask;
238 };
239
240 static struct pgtable_level pg_level[] = {
241         {
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),
254         },
255 };
256
257 static 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                 /* 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);
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
289 static 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         /* Work out what appropriate unit to use */
305         while (!(delta & 1023) && unit[1]) {
306                 delta >>= 10;
307                 unit++;
308         }
309         seq_printf(st->seq, "%9lu%c", delta, *unit);
310
311 }
312
313 static 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         /* 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);
327         /*
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
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                 /* Check the PTE flags */
340                 if (st->current_flags) {
341                         dump_addr(st, addr);
342
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);
348
349                         seq_putc(st->seq, '\n');
350                 }
351
352                 /*
353                  * Address indicates we have passed the end of the
354                  * current section of virtual memory
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
370 static 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
383 static 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                         /* pmd exists */
393                         walk_pte(st, pmd, addr);
394                 else
395                         note_page(st, addr, 3, pmd_val(*pmd));
396         }
397 }
398
399 static 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                         /* pud exists */
409                         walk_pmd(st, pud, addr);
410                 else
411                         note_page(st, addr, 2, pud_val(*pud));
412         }
413 }
414
415 static 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         addr = st->start_address;
422
423         /*
424          * Traverse the linux pagetable structure and dump pages that are in
425          * the hash pagetable.
426          */
427         for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
428                 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
429                         /* pgd exists */
430                         walk_pud(st, pgd, addr);
431                 else
432                         note_page(st, addr, 1, pgd_val(*pgd));
433         }
434 }
435
436 static void populate_markers(void)
437 {
438         int i = 0;
439
440         address_markers[i++].start_address = PAGE_OFFSET;
441         address_markers[i++].start_address = VMALLOC_START;
442         address_markers[i++].start_address = VMALLOC_END;
443 #ifdef CONFIG_PPC64
444         address_markers[i++].start_address = ISA_IO_BASE;
445         address_markers[i++].start_address = ISA_IO_END;
446         address_markers[i++].start_address = PHB_IO_BASE;
447         address_markers[i++].start_address = PHB_IO_END;
448         address_markers[i++].start_address = IOREMAP_BASE;
449         address_markers[i++].start_address = IOREMAP_END;
450 #ifdef CONFIG_PPC_STD_MMU_64
451         address_markers[i++].start_address =  H_VMEMMAP_BASE;
452 #else
453         address_markers[i++].start_address =  VMEMMAP_BASE;
454 #endif
455 #else /* !CONFIG_PPC64 */
456         address_markers[i++].start_address = ioremap_bot;
457         address_markers[i++].start_address = IOREMAP_TOP;
458 #ifdef CONFIG_NOT_COHERENT_CACHE
459         address_markers[i++].start_address = IOREMAP_TOP;
460         address_markers[i++].start_address = IOREMAP_TOP +
461                                              CONFIG_CONSISTENT_SIZE;
462 #endif
463 #ifdef CONFIG_HIGHMEM
464         address_markers[i++].start_address = PKMAP_BASE;
465         address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
466 #endif
467         address_markers[i++].start_address = FIXADDR_START;
468         address_markers[i++].start_address = FIXADDR_TOP;
469 #endif /* CONFIG_PPC64 */
470 }
471
472 static int ptdump_show(struct seq_file *m, void *v)
473 {
474         struct pg_state st = {
475                 .seq = m,
476                 .marker = address_markers,
477         };
478
479         if (radix_enabled())
480                 st.start_address = PAGE_OFFSET;
481         else
482                 st.start_address = KERN_VIRT_START;
483
484         /* Traverse kernel page tables */
485         walk_pagetables(&st);
486         note_page(&st, 0, 0, 0);
487         return 0;
488 }
489
490
491 static int ptdump_open(struct inode *inode, struct file *file)
492 {
493         return single_open(file, ptdump_show, NULL);
494 }
495
496 static const struct file_operations ptdump_fops = {
497         .open           = ptdump_open,
498         .read           = seq_read,
499         .llseek         = seq_lseek,
500         .release        = single_release,
501 };
502
503 static void build_pgtable_complete_mask(void)
504 {
505         unsigned int i, j;
506
507         for (i = 0; i < ARRAY_SIZE(pg_level); i++)
508                 if (pg_level[i].flag)
509                         for (j = 0; j < pg_level[i].num; j++)
510                                 pg_level[i].mask |= pg_level[i].flag[j].mask;
511 }
512
513 static int ptdump_init(void)
514 {
515         struct dentry *debugfs_file;
516
517         populate_markers();
518         build_pgtable_complete_mask();
519         debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
520                         NULL, &ptdump_fops);
521         return debugfs_file ? 0 : -ENOMEM;
522 }
523 device_initcall(ptdump_init);