powerpc/mm/hash64: Map all the kernel regions in the same 0xc range
[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" },
103#endif
8eb07b18
RG
104 { -1, NULL },
105};
106
8eb07b18
RG
107static void dump_flag_info(struct pg_state *st, const struct flag_info
108 *flag, u64 pte, int num)
109{
110 unsigned int i;
111
112 for (i = 0; i < num; i++, flag++) {
113 const char *s = NULL;
114 u64 val;
115
116 /* flag not defined so don't check it */
117 if (flag->mask == 0)
118 continue;
119 /* Some 'flags' are actually values */
120 if (flag->is_val) {
121 val = pte & flag->val;
122 if (flag->shift)
123 val = val >> flag->shift;
124 seq_printf(st->seq, " %s:%llx", flag->set, val);
125 } else {
126 if ((pte & flag->mask) == flag->val)
127 s = flag->set;
128 else
129 s = flag->clear;
130 if (s)
131 seq_printf(st->seq, " %s", s);
132 }
133 st->current_flags &= ~flag->mask;
134 }
135 if (st->current_flags != 0)
136 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
137}
138
139static void dump_addr(struct pg_state *st, unsigned long addr)
140{
141 static const char units[] = "KMGTPE";
142 const char *unit = units;
143 unsigned long delta;
144
78a18dbf 145#ifdef CONFIG_PPC64
cabe8138 146#define REG "0x%016lx"
78a18dbf 147#else
cabe8138 148#define REG "0x%08lx"
78a18dbf 149#endif
aaa22952 150
cabe8138
CL
151 seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1);
152 if (st->start_pa == st->last_pa && st->start_address + PAGE_SIZE != addr) {
153 seq_printf(st->seq, "[" REG "]", st->start_pa);
154 delta = PAGE_SIZE >> 10;
155 } else {
156 seq_printf(st->seq, " " REG " ", st->start_pa);
157 delta = (addr - st->start_address) >> 10;
158 }
8eb07b18
RG
159 /* Work out what appropriate unit to use */
160 while (!(delta & 1023) && unit[1]) {
161 delta >>= 10;
162 unit++;
163 }
164 seq_printf(st->seq, "%9lu%c", delta, *unit);
165
166}
167
168static void note_page(struct pg_state *st, unsigned long addr,
169 unsigned int level, u64 val)
170{
171 u64 flag = val & pg_level[level].mask;
aaa22952
OH
172 u64 pa = val & PTE_RPN_MASK;
173
8eb07b18
RG
174 /* At first no level is set */
175 if (!st->level) {
176 st->level = level;
177 st->current_flags = flag;
178 st->start_address = addr;
aaa22952
OH
179 st->start_pa = pa;
180 st->last_pa = pa;
8eb07b18
RG
181 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
182 /*
183 * Dump the section of virtual memory when:
184 * - the PTE flags from one entry to the next differs.
185 * - we change levels in the tree.
186 * - the address is in a different section of memory and is thus
187 * used for a different purpose, regardless of the flags.
aaa22952 188 * - the pa of this page is not adjacent to the last inspected page
8eb07b18
RG
189 */
190 } else if (flag != st->current_flags || level != st->level ||
aaa22952 191 addr >= st->marker[1].start_address ||
cabe8138
CL
192 (pa != st->last_pa + PAGE_SIZE &&
193 (pa != st->start_pa || st->start_pa != st->last_pa))) {
8eb07b18
RG
194
195 /* Check the PTE flags */
196 if (st->current_flags) {
197 dump_addr(st, addr);
198
199 /* Dump all the flags */
200 if (pg_level[st->level].flag)
201 dump_flag_info(st, pg_level[st->level].flag,
202 st->current_flags,
203 pg_level[st->level].num);
204
aae85e3c 205 seq_putc(st->seq, '\n');
8eb07b18
RG
206 }
207
208 /*
209 * Address indicates we have passed the end of the
210 * current section of virtual memory
211 */
212 while (addr >= st->marker[1].start_address) {
213 st->marker++;
214 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
215 }
216 st->start_address = addr;
aaa22952
OH
217 st->start_pa = pa;
218 st->last_pa = pa;
8eb07b18
RG
219 st->current_flags = flag;
220 st->level = level;
aaa22952
OH
221 } else {
222 st->last_pa = pa;
8eb07b18
RG
223 }
224}
225
226static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
227{
228 pte_t *pte = pte_offset_kernel(pmd, 0);
229 unsigned long addr;
230 unsigned int i;
231
232 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
233 addr = start + i * PAGE_SIZE;
234 note_page(st, addr, 4, pte_val(*pte));
235
236 }
237}
238
239static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
240{
241 pmd_t *pmd = pmd_offset(pud, 0);
242 unsigned long addr;
243 unsigned int i;
244
245 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
246 addr = start + i * PMD_SIZE;
bfb9956a 247 if (!pmd_none(*pmd) && !pmd_huge(*pmd))
8eb07b18
RG
248 /* pmd exists */
249 walk_pte(st, pmd, addr);
250 else
251 note_page(st, addr, 3, pmd_val(*pmd));
252 }
253}
254
255static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
256{
257 pud_t *pud = pud_offset(pgd, 0);
258 unsigned long addr;
259 unsigned int i;
260
261 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
262 addr = start + i * PUD_SIZE;
bfb9956a 263 if (!pud_none(*pud) && !pud_huge(*pud))
8eb07b18
RG
264 /* pud exists */
265 walk_pmd(st, pud, addr);
266 else
267 note_page(st, addr, 2, pud_val(*pud));
268 }
269}
270
271static void walk_pagetables(struct pg_state *st)
272{
273 pgd_t *pgd = pgd_offset_k(0UL);
274 unsigned int i;
275 unsigned long addr;
276
0d923962
ME
277 addr = st->start_address;
278
8eb07b18
RG
279 /*
280 * Traverse the linux pagetable structure and dump pages that are in
281 * the hash pagetable.
282 */
0d923962 283 for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
bfb9956a 284 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
8eb07b18
RG
285 /* pgd exists */
286 walk_pud(st, pgd, addr);
287 else
288 note_page(st, addr, 1, pgd_val(*pgd));
289 }
290}
291
292static void populate_markers(void)
293{
6c01bbd2
CL
294 int i = 0;
295
296 address_markers[i++].start_address = PAGE_OFFSET;
297 address_markers[i++].start_address = VMALLOC_START;
298 address_markers[i++].start_address = VMALLOC_END;
299#ifdef CONFIG_PPC64
300 address_markers[i++].start_address = ISA_IO_BASE;
301 address_markers[i++].start_address = ISA_IO_END;
302 address_markers[i++].start_address = PHB_IO_BASE;
303 address_markers[i++].start_address = PHB_IO_END;
304 address_markers[i++].start_address = IOREMAP_BASE;
305 address_markers[i++].start_address = IOREMAP_END;
0034d395 306 /* What is the ifdef about? */
4e003747 307#ifdef CONFIG_PPC_BOOK3S_64
0034d395 308 address_markers[i++].start_address = H_VMEMMAP_START;
8eb07b18 309#else
6c01bbd2
CL
310 address_markers[i++].start_address = VMEMMAP_BASE;
311#endif
312#else /* !CONFIG_PPC64 */
313 address_markers[i++].start_address = ioremap_bot;
314 address_markers[i++].start_address = IOREMAP_TOP;
315#ifdef CONFIG_NOT_COHERENT_CACHE
316 address_markers[i++].start_address = IOREMAP_TOP;
317 address_markers[i++].start_address = IOREMAP_TOP +
318 CONFIG_CONSISTENT_SIZE;
319#endif
320#ifdef CONFIG_HIGHMEM
321 address_markers[i++].start_address = PKMAP_BASE;
322 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
8eb07b18 323#endif
6c01bbd2
CL
324 address_markers[i++].start_address = FIXADDR_START;
325 address_markers[i++].start_address = FIXADDR_TOP;
326#endif /* CONFIG_PPC64 */
8eb07b18
RG
327}
328
329static int ptdump_show(struct seq_file *m, void *v)
330{
331 struct pg_state st = {
332 .seq = m,
8eb07b18
RG
333 .marker = address_markers,
334 };
0d923962
ME
335
336 if (radix_enabled())
337 st.start_address = PAGE_OFFSET;
338 else
339 st.start_address = KERN_VIRT_START;
340
8eb07b18
RG
341 /* Traverse kernel page tables */
342 walk_pagetables(&st);
343 note_page(&st, 0, 0, 0);
344 return 0;
345}
346
347
348static int ptdump_open(struct inode *inode, struct file *file)
349{
350 return single_open(file, ptdump_show, NULL);
351}
352
353static const struct file_operations ptdump_fops = {
354 .open = ptdump_open,
355 .read = seq_read,
356 .llseek = seq_lseek,
357 .release = single_release,
358};
359
360static void build_pgtable_complete_mask(void)
361{
362 unsigned int i, j;
363
364 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
365 if (pg_level[i].flag)
366 for (j = 0; j < pg_level[i].num; j++)
367 pg_level[i].mask |= pg_level[i].flag[j].mask;
368}
369
370static int ptdump_init(void)
371{
372 struct dentry *debugfs_file;
373
374 populate_markers();
375 build_pgtable_complete_mask();
2505820f 376 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
8eb07b18
RG
377 NULL, &ptdump_fops);
378 return debugfs_file ? 0 : -ENOMEM;
379}
380device_initcall(ptdump_init);