74ff2bff4ea01b9154aba21a003eb9cb7abb7ec3
[linux-2.6-block.git] / arch / powerpc / mm / ptdump / ptdump.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2016, Rashmica Gupta, IBM Corp.
4  *
5  * This traverses the kernel pagetables and dumps the
6  * information about the used sections of memory to
7  * /sys/kernel/debug/kernel_pagetables.
8  *
9  * Derived from the arm64 implementation:
10  * Copyright (c) 2014, The Linux Foundation, Laura Abbott.
11  * (C) Copyright 2008 Intel Corporation, Arjan van de Ven.
12  */
13 #include <linux/debugfs.h>
14 #include <linux/fs.h>
15 #include <linux/hugetlb.h>
16 #include <linux/io.h>
17 #include <linux/mm.h>
18 #include <linux/highmem.h>
19 #include <linux/sched.h>
20 #include <linux/seq_file.h>
21 #include <asm/fixmap.h>
22 #include <asm/pgtable.h>
23 #include <linux/const.h>
24 #include <asm/page.h>
25 #include <asm/pgalloc.h>
26
27 #include "ptdump.h"
28
29 #ifdef CONFIG_PPC32
30 #define KERN_VIRT_START 0
31 #endif
32
33 /*
34  * To visualise what is happening,
35  *
36  *  - PTRS_PER_P** = how many entries there are in the corresponding P**
37  *  - P**_SHIFT = how many bits of the address we use to index into the
38  * corresponding P**
39  *  - P**_SIZE is how much memory we can access through the table - not the
40  * size of the table itself.
41  * P**={PGD, PUD, PMD, PTE}
42  *
43  *
44  * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
45  * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
46  * a page.
47  *
48  * In the case where there are only 3 levels, the PUD is folded into the
49  * PGD: every PUD has only one entry which points to the PMD.
50  *
51  * The page dumper groups page table entries of the same type into a single
52  * description. It uses pg_state to track the range information while
53  * iterating over the PTE entries. When the continuity is broken it then
54  * dumps out a description of the range - ie PTEs that are virtually contiguous
55  * with the same PTE flags are chunked together. This is to make it clear how
56  * different areas of the kernel virtual memory are used.
57  *
58  */
59 struct pg_state {
60         struct seq_file *seq;
61         const struct addr_marker *marker;
62         unsigned long start_address;
63         unsigned long start_pa;
64         unsigned long last_pa;
65         unsigned int level;
66         u64 current_flags;
67         bool check_wx;
68         unsigned long wx_pages;
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 #ifdef CONFIG_KASAN
103         { 0,    "kasan shadow mem start" },
104         { 0,    "kasan shadow mem end" },
105 #endif
106         { -1,   NULL },
107 };
108
109 #define pt_dump_seq_printf(m, fmt, args...)     \
110 ({                                              \
111         if (m)                                  \
112                 seq_printf(m, fmt, ##args);     \
113 })
114
115 #define pt_dump_seq_putc(m, c)          \
116 ({                                      \
117         if (m)                          \
118                 seq_putc(m, c);         \
119 })
120
121 static void dump_flag_info(struct pg_state *st, const struct flag_info
122                 *flag, u64 pte, int num)
123 {
124         unsigned int i;
125
126         for (i = 0; i < num; i++, flag++) {
127                 const char *s = NULL;
128                 u64 val;
129
130                 /* flag not defined so don't check it */
131                 if (flag->mask == 0)
132                         continue;
133                 /* Some 'flags' are actually values */
134                 if (flag->is_val) {
135                         val = pte & flag->val;
136                         if (flag->shift)
137                                 val = val >> flag->shift;
138                         pt_dump_seq_printf(st->seq, "  %s:%llx", flag->set, val);
139                 } else {
140                         if ((pte & flag->mask) == flag->val)
141                                 s = flag->set;
142                         else
143                                 s = flag->clear;
144                         if (s)
145                                 pt_dump_seq_printf(st->seq, "  %s", s);
146                 }
147                 st->current_flags &= ~flag->mask;
148         }
149         if (st->current_flags != 0)
150                 pt_dump_seq_printf(st->seq, "  unknown flags:%llx", st->current_flags);
151 }
152
153 static void dump_addr(struct pg_state *st, unsigned long addr)
154 {
155         static const char units[] = "KMGTPE";
156         const char *unit = units;
157         unsigned long delta;
158
159 #ifdef CONFIG_PPC64
160 #define REG             "0x%016lx"
161 #else
162 #define REG             "0x%08lx"
163 #endif
164
165         pt_dump_seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1);
166         if (st->start_pa == st->last_pa && st->start_address + PAGE_SIZE != addr) {
167                 pt_dump_seq_printf(st->seq, "[" REG "]", st->start_pa);
168                 delta = PAGE_SIZE >> 10;
169         } else {
170                 pt_dump_seq_printf(st->seq, " " REG " ", st->start_pa);
171                 delta = (addr - st->start_address) >> 10;
172         }
173         /* Work out what appropriate unit to use */
174         while (!(delta & 1023) && unit[1]) {
175                 delta >>= 10;
176                 unit++;
177         }
178         pt_dump_seq_printf(st->seq, "%9lu%c", delta, *unit);
179
180 }
181
182 static void note_prot_wx(struct pg_state *st, unsigned long addr)
183 {
184         if (!st->check_wx)
185                 return;
186
187         if (!((st->current_flags & pgprot_val(PAGE_KERNEL_X)) == pgprot_val(PAGE_KERNEL_X)))
188                 return;
189
190         WARN_ONCE(1, "powerpc/mm: Found insecure W+X mapping at address %p/%pS\n",
191                   (void *)st->start_address, (void *)st->start_address);
192
193         st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
194 }
195
196 static void note_page(struct pg_state *st, unsigned long addr,
197                unsigned int level, u64 val)
198 {
199         u64 flag = val & pg_level[level].mask;
200         u64 pa = val & PTE_RPN_MASK;
201
202         /* At first no level is set */
203         if (!st->level) {
204                 st->level = level;
205                 st->current_flags = flag;
206                 st->start_address = addr;
207                 st->start_pa = pa;
208                 st->last_pa = pa;
209                 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
210         /*
211          * Dump the section of virtual memory when:
212          *   - the PTE flags from one entry to the next differs.
213          *   - we change levels in the tree.
214          *   - the address is in a different section of memory and is thus
215          *   used for a different purpose, regardless of the flags.
216          *   - the pa of this page is not adjacent to the last inspected page
217          */
218         } else if (flag != st->current_flags || level != st->level ||
219                    addr >= st->marker[1].start_address ||
220                    (pa != st->last_pa + PAGE_SIZE &&
221                     (pa != st->start_pa || st->start_pa != st->last_pa))) {
222
223                 /* Check the PTE flags */
224                 if (st->current_flags) {
225                         note_prot_wx(st, addr);
226                         dump_addr(st, addr);
227
228                         /* Dump all the flags */
229                         if (pg_level[st->level].flag)
230                                 dump_flag_info(st, pg_level[st->level].flag,
231                                           st->current_flags,
232                                           pg_level[st->level].num);
233
234                         pt_dump_seq_putc(st->seq, '\n');
235                 }
236
237                 /*
238                  * Address indicates we have passed the end of the
239                  * current section of virtual memory
240                  */
241                 while (addr >= st->marker[1].start_address) {
242                         st->marker++;
243                         pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
244                 }
245                 st->start_address = addr;
246                 st->start_pa = pa;
247                 st->last_pa = pa;
248                 st->current_flags = flag;
249                 st->level = level;
250         } else {
251                 st->last_pa = pa;
252         }
253 }
254
255 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
256 {
257         pte_t *pte = pte_offset_kernel(pmd, 0);
258         unsigned long addr;
259         unsigned int i;
260
261         for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
262                 addr = start + i * PAGE_SIZE;
263                 note_page(st, addr, 4, pte_val(*pte));
264
265         }
266 }
267
268 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
269 {
270         pmd_t *pmd = pmd_offset(pud, 0);
271         unsigned long addr;
272         unsigned int i;
273
274         for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
275                 addr = start + i * PMD_SIZE;
276                 if (!pmd_none(*pmd) && !pmd_is_leaf(*pmd))
277                         /* pmd exists */
278                         walk_pte(st, pmd, addr);
279                 else
280                         note_page(st, addr, 3, pmd_val(*pmd));
281         }
282 }
283
284 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
285 {
286         pud_t *pud = pud_offset(pgd, 0);
287         unsigned long addr;
288         unsigned int i;
289
290         for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
291                 addr = start + i * PUD_SIZE;
292                 if (!pud_none(*pud) && !pud_is_leaf(*pud))
293                         /* pud exists */
294                         walk_pmd(st, pud, addr);
295                 else
296                         note_page(st, addr, 2, pud_val(*pud));
297         }
298 }
299
300 static void walk_pagetables(struct pg_state *st)
301 {
302         unsigned int i;
303         unsigned long addr = st->start_address & PGDIR_MASK;
304         pgd_t *pgd = pgd_offset_k(addr);
305
306         /*
307          * Traverse the linux pagetable structure and dump pages that are in
308          * the hash pagetable.
309          */
310         for (i = pgd_index(addr); i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
311                 if (!pgd_none(*pgd) && !pgd_is_leaf(*pgd))
312                         /* pgd exists */
313                         walk_pud(st, pgd, addr);
314                 else
315                         note_page(st, addr, 1, pgd_val(*pgd));
316         }
317 }
318
319 static void populate_markers(void)
320 {
321         int i = 0;
322
323         address_markers[i++].start_address = PAGE_OFFSET;
324         address_markers[i++].start_address = VMALLOC_START;
325         address_markers[i++].start_address = VMALLOC_END;
326 #ifdef CONFIG_PPC64
327         address_markers[i++].start_address = ISA_IO_BASE;
328         address_markers[i++].start_address = ISA_IO_END;
329         address_markers[i++].start_address = PHB_IO_BASE;
330         address_markers[i++].start_address = PHB_IO_END;
331         address_markers[i++].start_address = IOREMAP_BASE;
332         address_markers[i++].start_address = IOREMAP_END;
333         /* What is the ifdef about? */
334 #ifdef CONFIG_PPC_BOOK3S_64
335         address_markers[i++].start_address =  H_VMEMMAP_START;
336 #else
337         address_markers[i++].start_address =  VMEMMAP_BASE;
338 #endif
339 #else /* !CONFIG_PPC64 */
340         address_markers[i++].start_address = ioremap_bot;
341         address_markers[i++].start_address = IOREMAP_TOP;
342 #ifdef CONFIG_NOT_COHERENT_CACHE
343         address_markers[i++].start_address = IOREMAP_TOP;
344         address_markers[i++].start_address = IOREMAP_TOP +
345                                              CONFIG_CONSISTENT_SIZE;
346 #endif
347 #ifdef CONFIG_HIGHMEM
348         address_markers[i++].start_address = PKMAP_BASE;
349         address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
350 #endif
351         address_markers[i++].start_address = FIXADDR_START;
352         address_markers[i++].start_address = FIXADDR_TOP;
353 #ifdef CONFIG_KASAN
354         address_markers[i++].start_address = KASAN_SHADOW_START;
355         address_markers[i++].start_address = KASAN_SHADOW_END;
356 #endif
357 #endif /* CONFIG_PPC64 */
358 }
359
360 static int ptdump_show(struct seq_file *m, void *v)
361 {
362         struct pg_state st = {
363                 .seq = m,
364                 .marker = address_markers,
365         };
366
367         if (radix_enabled())
368                 st.start_address = PAGE_OFFSET;
369         else
370                 st.start_address = KERN_VIRT_START;
371
372         /* Traverse kernel page tables */
373         walk_pagetables(&st);
374         note_page(&st, 0, 0, 0);
375         return 0;
376 }
377
378
379 static int ptdump_open(struct inode *inode, struct file *file)
380 {
381         return single_open(file, ptdump_show, NULL);
382 }
383
384 static const struct file_operations ptdump_fops = {
385         .open           = ptdump_open,
386         .read           = seq_read,
387         .llseek         = seq_lseek,
388         .release        = single_release,
389 };
390
391 static void build_pgtable_complete_mask(void)
392 {
393         unsigned int i, j;
394
395         for (i = 0; i < ARRAY_SIZE(pg_level); i++)
396                 if (pg_level[i].flag)
397                         for (j = 0; j < pg_level[i].num; j++)
398                                 pg_level[i].mask |= pg_level[i].flag[j].mask;
399 }
400
401 #ifdef CONFIG_PPC_DEBUG_WX
402 void ptdump_check_wx(void)
403 {
404         struct pg_state st = {
405                 .seq = NULL,
406                 .marker = address_markers,
407                 .check_wx = true,
408         };
409
410         if (radix_enabled())
411                 st.start_address = PAGE_OFFSET;
412         else
413                 st.start_address = KERN_VIRT_START;
414
415         walk_pagetables(&st);
416
417         if (st.wx_pages)
418                 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n",
419                         st.wx_pages);
420         else
421                 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
422 }
423 #endif
424
425 static int ptdump_init(void)
426 {
427         struct dentry *debugfs_file;
428
429         populate_markers();
430         build_pgtable_complete_mask();
431         debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
432                         NULL, &ptdump_fops);
433         return debugfs_file ? 0 : -ENOMEM;
434 }
435 device_initcall(ptdump_init);