Merge tag 'linux-watchdog-4.20-rc1' of git://www.linux-watchdog.org/linux-watchdog
[sfrench/cifs-2.6.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/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 #include "dump_linuxpagetables.h"
31
32 #ifdef CONFIG_PPC32
33 #define KERN_VIRT_START 0
34 #endif
35
36 /*
37  * To visualise what is happening,
38  *
39  *  - PTRS_PER_P** = how many entries there are in the corresponding P**
40  *  - P**_SHIFT = how many bits of the address we use to index into the
41  * corresponding P**
42  *  - P**_SIZE is how much memory we can access through the table - not the
43  * size of the table itself.
44  * P**={PGD, PUD, PMD, PTE}
45  *
46  *
47  * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
48  * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
49  * a page.
50  *
51  * In the case where there are only 3 levels, the PUD is folded into the
52  * PGD: every PUD has only one entry which points to the PMD.
53  *
54  * The page dumper groups page table entries of the same type into a single
55  * description. It uses pg_state to track the range information while
56  * iterating over the PTE entries. When the continuity is broken it then
57  * dumps out a description of the range - ie PTEs that are virtually contiguous
58  * with the same PTE flags are chunked together. This is to make it clear how
59  * different areas of the kernel virtual memory are used.
60  *
61  */
62 struct pg_state {
63         struct seq_file *seq;
64         const struct addr_marker *marker;
65         unsigned long start_address;
66         unsigned long start_pa;
67         unsigned long last_pa;
68         unsigned int level;
69         u64 current_flags;
70 };
71
72 struct addr_marker {
73         unsigned long start_address;
74         const char *name;
75 };
76
77 static struct addr_marker address_markers[] = {
78         { 0,    "Start of kernel VM" },
79         { 0,    "vmalloc() Area" },
80         { 0,    "vmalloc() End" },
81 #ifdef CONFIG_PPC64
82         { 0,    "isa I/O start" },
83         { 0,    "isa I/O end" },
84         { 0,    "phb I/O start" },
85         { 0,    "phb I/O end" },
86         { 0,    "I/O remap start" },
87         { 0,    "I/O remap end" },
88         { 0,    "vmemmap start" },
89 #else
90         { 0,    "Early I/O remap start" },
91         { 0,    "Early I/O remap end" },
92 #ifdef CONFIG_NOT_COHERENT_CACHE
93         { 0,    "Consistent mem start" },
94         { 0,    "Consistent mem end" },
95 #endif
96 #ifdef CONFIG_HIGHMEM
97         { 0,    "Highmem PTEs start" },
98         { 0,    "Highmem PTEs end" },
99 #endif
100         { 0,    "Fixmap start" },
101         { 0,    "Fixmap end" },
102 #endif
103         { -1,   NULL },
104 };
105
106 static void dump_flag_info(struct pg_state *st, const struct flag_info
107                 *flag, u64 pte, int num)
108 {
109         unsigned int i;
110
111         for (i = 0; i < num; i++, flag++) {
112                 const char *s = NULL;
113                 u64 val;
114
115                 /* flag not defined so don't check it */
116                 if (flag->mask == 0)
117                         continue;
118                 /* Some 'flags' are actually values */
119                 if (flag->is_val) {
120                         val = pte & flag->val;
121                         if (flag->shift)
122                                 val = val >> flag->shift;
123                         seq_printf(st->seq, "  %s:%llx", flag->set, val);
124                 } else {
125                         if ((pte & flag->mask) == flag->val)
126                                 s = flag->set;
127                         else
128                                 s = flag->clear;
129                         if (s)
130                                 seq_printf(st->seq, "  %s", s);
131                 }
132                 st->current_flags &= ~flag->mask;
133         }
134         if (st->current_flags != 0)
135                 seq_printf(st->seq, "  unknown flags:%llx", st->current_flags);
136 }
137
138 static void dump_addr(struct pg_state *st, unsigned long addr)
139 {
140         static const char units[] = "KMGTPE";
141         const char *unit = units;
142         unsigned long delta;
143
144 #ifdef CONFIG_PPC64
145         seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
146         seq_printf(st->seq, "0x%016lx ", st->start_pa);
147 #else
148         seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
149         seq_printf(st->seq, "0x%08lx ", st->start_pa);
150 #endif
151
152         delta = (addr - st->start_address) >> 10;
153         /* Work out what appropriate unit to use */
154         while (!(delta & 1023) && unit[1]) {
155                 delta >>= 10;
156                 unit++;
157         }
158         seq_printf(st->seq, "%9lu%c", delta, *unit);
159
160 }
161
162 static void note_page(struct pg_state *st, unsigned long addr,
163                unsigned int level, u64 val)
164 {
165         u64 flag = val & pg_level[level].mask;
166         u64 pa = val & PTE_RPN_MASK;
167
168         /* At first no level is set */
169         if (!st->level) {
170                 st->level = level;
171                 st->current_flags = flag;
172                 st->start_address = addr;
173                 st->start_pa = pa;
174                 st->last_pa = pa;
175                 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
176         /*
177          * Dump the section of virtual memory when:
178          *   - the PTE flags from one entry to the next differs.
179          *   - we change levels in the tree.
180          *   - the address is in a different section of memory and is thus
181          *   used for a different purpose, regardless of the flags.
182          *   - the pa of this page is not adjacent to the last inspected page
183          */
184         } else if (flag != st->current_flags || level != st->level ||
185                    addr >= st->marker[1].start_address ||
186                    pa != st->last_pa + PAGE_SIZE) {
187
188                 /* Check the PTE flags */
189                 if (st->current_flags) {
190                         dump_addr(st, addr);
191
192                         /* Dump all the flags */
193                         if (pg_level[st->level].flag)
194                                 dump_flag_info(st, pg_level[st->level].flag,
195                                           st->current_flags,
196                                           pg_level[st->level].num);
197
198                         seq_putc(st->seq, '\n');
199                 }
200
201                 /*
202                  * Address indicates we have passed the end of the
203                  * current section of virtual memory
204                  */
205                 while (addr >= st->marker[1].start_address) {
206                         st->marker++;
207                         seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
208                 }
209                 st->start_address = addr;
210                 st->start_pa = pa;
211                 st->last_pa = pa;
212                 st->current_flags = flag;
213                 st->level = level;
214         } else {
215                 st->last_pa = pa;
216         }
217 }
218
219 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
220 {
221         pte_t *pte = pte_offset_kernel(pmd, 0);
222         unsigned long addr;
223         unsigned int i;
224
225         for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
226                 addr = start + i * PAGE_SIZE;
227                 note_page(st, addr, 4, pte_val(*pte));
228
229         }
230 }
231
232 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
233 {
234         pmd_t *pmd = pmd_offset(pud, 0);
235         unsigned long addr;
236         unsigned int i;
237
238         for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
239                 addr = start + i * PMD_SIZE;
240                 if (!pmd_none(*pmd) && !pmd_huge(*pmd))
241                         /* pmd exists */
242                         walk_pte(st, pmd, addr);
243                 else
244                         note_page(st, addr, 3, pmd_val(*pmd));
245         }
246 }
247
248 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
249 {
250         pud_t *pud = pud_offset(pgd, 0);
251         unsigned long addr;
252         unsigned int i;
253
254         for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
255                 addr = start + i * PUD_SIZE;
256                 if (!pud_none(*pud) && !pud_huge(*pud))
257                         /* pud exists */
258                         walk_pmd(st, pud, addr);
259                 else
260                         note_page(st, addr, 2, pud_val(*pud));
261         }
262 }
263
264 static void walk_pagetables(struct pg_state *st)
265 {
266         pgd_t *pgd = pgd_offset_k(0UL);
267         unsigned int i;
268         unsigned long addr;
269
270         addr = st->start_address;
271
272         /*
273          * Traverse the linux pagetable structure and dump pages that are in
274          * the hash pagetable.
275          */
276         for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
277                 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
278                         /* pgd exists */
279                         walk_pud(st, pgd, addr);
280                 else
281                         note_page(st, addr, 1, pgd_val(*pgd));
282         }
283 }
284
285 static void populate_markers(void)
286 {
287         int i = 0;
288
289         address_markers[i++].start_address = PAGE_OFFSET;
290         address_markers[i++].start_address = VMALLOC_START;
291         address_markers[i++].start_address = VMALLOC_END;
292 #ifdef CONFIG_PPC64
293         address_markers[i++].start_address = ISA_IO_BASE;
294         address_markers[i++].start_address = ISA_IO_END;
295         address_markers[i++].start_address = PHB_IO_BASE;
296         address_markers[i++].start_address = PHB_IO_END;
297         address_markers[i++].start_address = IOREMAP_BASE;
298         address_markers[i++].start_address = IOREMAP_END;
299 #ifdef CONFIG_PPC_BOOK3S_64
300         address_markers[i++].start_address =  H_VMEMMAP_BASE;
301 #else
302         address_markers[i++].start_address =  VMEMMAP_BASE;
303 #endif
304 #else /* !CONFIG_PPC64 */
305         address_markers[i++].start_address = ioremap_bot;
306         address_markers[i++].start_address = IOREMAP_TOP;
307 #ifdef CONFIG_NOT_COHERENT_CACHE
308         address_markers[i++].start_address = IOREMAP_TOP;
309         address_markers[i++].start_address = IOREMAP_TOP +
310                                              CONFIG_CONSISTENT_SIZE;
311 #endif
312 #ifdef CONFIG_HIGHMEM
313         address_markers[i++].start_address = PKMAP_BASE;
314         address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
315 #endif
316         address_markers[i++].start_address = FIXADDR_START;
317         address_markers[i++].start_address = FIXADDR_TOP;
318 #endif /* CONFIG_PPC64 */
319 }
320
321 static int ptdump_show(struct seq_file *m, void *v)
322 {
323         struct pg_state st = {
324                 .seq = m,
325                 .marker = address_markers,
326         };
327
328         if (radix_enabled())
329                 st.start_address = PAGE_OFFSET;
330         else
331                 st.start_address = KERN_VIRT_START;
332
333         /* Traverse kernel page tables */
334         walk_pagetables(&st);
335         note_page(&st, 0, 0, 0);
336         return 0;
337 }
338
339
340 static int ptdump_open(struct inode *inode, struct file *file)
341 {
342         return single_open(file, ptdump_show, NULL);
343 }
344
345 static const struct file_operations ptdump_fops = {
346         .open           = ptdump_open,
347         .read           = seq_read,
348         .llseek         = seq_lseek,
349         .release        = single_release,
350 };
351
352 static void build_pgtable_complete_mask(void)
353 {
354         unsigned int i, j;
355
356         for (i = 0; i < ARRAY_SIZE(pg_level); i++)
357                 if (pg_level[i].flag)
358                         for (j = 0; j < pg_level[i].num; j++)
359                                 pg_level[i].mask |= pg_level[i].flag[j].mask;
360 }
361
362 static int ptdump_init(void)
363 {
364         struct dentry *debugfs_file;
365
366         populate_markers();
367         build_pgtable_complete_mask();
368         debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
369                         NULL, &ptdump_fops);
370         return debugfs_file ? 0 : -ENOMEM;
371 }
372 device_initcall(ptdump_init);