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