arm64: update stale PAGE_OFFSET comment
[deliverable/linux.git] / arch / arm64 / mm / dump.c
CommitLineData
c9465b4e
LA
1/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * Debug helper to dump the current kernel pagetables of the system
4 * so that we can see what the various memory ranges are set to.
5 *
6 * Derived from x86 and arm implementation:
7 * (C) Copyright 2008 Intel Corporation
8 *
9 * Author: Arjan van de Ven <arjan@linux.intel.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; version 2
14 * of the License.
15 */
16#include <linux/debugfs.h>
764011ca 17#include <linux/errno.h>
c9465b4e 18#include <linux/fs.h>
284be285 19#include <linux/io.h>
764011ca 20#include <linux/init.h>
c9465b4e
LA
21#include <linux/mm.h>
22#include <linux/sched.h>
23#include <linux/seq_file.h>
24
25#include <asm/fixmap.h>
d8fc68a0 26#include <asm/kasan.h>
764011ca 27#include <asm/memory.h>
c9465b4e 28#include <asm/pgtable.h>
764011ca 29#include <asm/pgtable-hwdef.h>
c9465b4e 30
c9465b4e
LA
31struct addr_marker {
32 unsigned long start_address;
33 const char *name;
34};
35
c8f8cca4 36static const struct addr_marker address_markers[] = {
d8fc68a0
AB
37#ifdef CONFIG_KASAN
38 { KASAN_SHADOW_START, "Kasan shadow start" },
39 { KASAN_SHADOW_END, "Kasan shadow end" },
40#endif
c8f8cca4
AB
41 { MODULES_VADDR, "Modules start" },
42 { MODULES_END, "Modules end" },
43 { VMALLOC_START, "vmalloc() Area" },
44 { VMALLOC_END, "vmalloc() End" },
45 { FIXADDR_START, "Fixmap start" },
46 { FIXADDR_TOP, "Fixmap end" },
47 { PCI_IO_START, "PCI I/O start" },
48 { PCI_IO_END, "PCI I/O end" },
3e1907d5 49#ifdef CONFIG_SPARSEMEM_VMEMMAP
c8f8cca4
AB
50 { VMEMMAP_START, "vmemmap start" },
51 { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" },
3e1907d5 52#endif
c8f8cca4
AB
53 { PAGE_OFFSET, "Linear Mapping" },
54 { -1, NULL },
c9465b4e
LA
55};
56
202e41a1
JL
57/*
58 * The page dumper groups page table entries of the same type into a single
59 * description. It uses pg_state to track the range information while
60 * iterating over the pte entries. When the continuity is broken it then
61 * dumps out a description of the range.
62 */
c9465b4e
LA
63struct pg_state {
64 struct seq_file *seq;
65 const struct addr_marker *marker;
66 unsigned long start_address;
67 unsigned level;
68 u64 current_prot;
69};
70
71struct prot_bits {
72 u64 mask;
73 u64 val;
74 const char *set;
75 const char *clear;
76};
77
78static const struct prot_bits pte_bits[] = {
79 {
d7e9d594
LA
80 .mask = PTE_VALID,
81 .val = PTE_VALID,
82 .set = " ",
83 .clear = "F",
84 }, {
c9465b4e
LA
85 .mask = PTE_USER,
86 .val = PTE_USER,
87 .set = "USR",
88 .clear = " ",
89 }, {
90 .mask = PTE_RDONLY,
91 .val = PTE_RDONLY,
92 .set = "ro",
93 .clear = "RW",
94 }, {
95 .mask = PTE_PXN,
96 .val = PTE_PXN,
97 .set = "NX",
98 .clear = "x ",
99 }, {
100 .mask = PTE_SHARED,
101 .val = PTE_SHARED,
102 .set = "SHD",
103 .clear = " ",
104 }, {
105 .mask = PTE_AF,
106 .val = PTE_AF,
107 .set = "AF",
108 .clear = " ",
109 }, {
110 .mask = PTE_NG,
111 .val = PTE_NG,
112 .set = "NG",
113 .clear = " ",
202e41a1
JL
114 }, {
115 .mask = PTE_CONT,
116 .val = PTE_CONT,
117 .set = "CON",
118 .clear = " ",
119 }, {
120 .mask = PTE_TABLE_BIT,
121 .val = PTE_TABLE_BIT,
122 .set = " ",
123 .clear = "BLK",
c9465b4e
LA
124 }, {
125 .mask = PTE_UXN,
126 .val = PTE_UXN,
127 .set = "UXN",
128 }, {
129 .mask = PTE_ATTRINDX_MASK,
130 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
131 .set = "DEVICE/nGnRnE",
132 }, {
133 .mask = PTE_ATTRINDX_MASK,
134 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
135 .set = "DEVICE/nGnRE",
136 }, {
137 .mask = PTE_ATTRINDX_MASK,
138 .val = PTE_ATTRINDX(MT_DEVICE_GRE),
139 .set = "DEVICE/GRE",
140 }, {
141 .mask = PTE_ATTRINDX_MASK,
142 .val = PTE_ATTRINDX(MT_NORMAL_NC),
143 .set = "MEM/NORMAL-NC",
144 }, {
145 .mask = PTE_ATTRINDX_MASK,
146 .val = PTE_ATTRINDX(MT_NORMAL),
147 .set = "MEM/NORMAL",
148 }
149};
150
151struct pg_level {
152 const struct prot_bits *bits;
153 size_t num;
154 u64 mask;
155};
156
157static struct pg_level pg_level[] = {
158 {
159 }, { /* pgd */
160 .bits = pte_bits,
161 .num = ARRAY_SIZE(pte_bits),
162 }, { /* pud */
163 .bits = pte_bits,
164 .num = ARRAY_SIZE(pte_bits),
165 }, { /* pmd */
166 .bits = pte_bits,
167 .num = ARRAY_SIZE(pte_bits),
168 }, { /* pte */
169 .bits = pte_bits,
170 .num = ARRAY_SIZE(pte_bits),
171 },
172};
173
174static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
175 size_t num)
176{
177 unsigned i;
178
179 for (i = 0; i < num; i++, bits++) {
180 const char *s;
181
182 if ((st->current_prot & bits->mask) == bits->val)
183 s = bits->set;
184 else
185 s = bits->clear;
186
187 if (s)
188 seq_printf(st->seq, " %s", s);
189 }
190}
191
192static void note_page(struct pg_state *st, unsigned long addr, unsigned level,
193 u64 val)
194{
195 static const char units[] = "KMGTPE";
196 u64 prot = val & pg_level[level].mask;
197
c9465b4e
LA
198 if (!st->level) {
199 st->level = level;
200 st->current_prot = prot;
201 st->start_address = addr;
202 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
203 } else if (prot != st->current_prot || level != st->level ||
204 addr >= st->marker[1].start_address) {
205 const char *unit = units;
206 unsigned long delta;
207
208 if (st->current_prot) {
202e41a1 209 seq_printf(st->seq, "0x%016lx-0x%016lx ",
c9465b4e
LA
210 st->start_address, addr);
211
212 delta = (addr - st->start_address) >> 10;
213 while (!(delta & 1023) && unit[1]) {
214 delta >>= 10;
215 unit++;
216 }
217 seq_printf(st->seq, "%9lu%c", delta, *unit);
218 if (pg_level[st->level].bits)
219 dump_prot(st, pg_level[st->level].bits,
220 pg_level[st->level].num);
221 seq_puts(st->seq, "\n");
222 }
223
224 if (addr >= st->marker[1].start_address) {
225 st->marker++;
226 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
227 }
228
229 st->start_address = addr;
230 st->current_prot = prot;
231 st->level = level;
232 }
233
234 if (addr >= st->marker[1].start_address) {
235 st->marker++;
236 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
237 }
238
239}
240
241static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
242{
243 pte_t *pte = pte_offset_kernel(pmd, 0);
244 unsigned long addr;
245 unsigned i;
246
247 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
248 addr = start + i * PAGE_SIZE;
249 note_page(st, addr, 4, pte_val(*pte));
250 }
251}
252
253static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
254{
255 pmd_t *pmd = pmd_offset(pud, 0);
256 unsigned long addr;
257 unsigned i;
258
259 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
260 addr = start + i * PMD_SIZE;
a1c76574 261 if (pmd_none(*pmd) || pmd_sect(*pmd)) {
c9465b4e 262 note_page(st, addr, 3, pmd_val(*pmd));
a1c76574
MR
263 } else {
264 BUG_ON(pmd_bad(*pmd));
c9465b4e 265 walk_pte(st, pmd, addr);
a1c76574 266 }
c9465b4e
LA
267 }
268}
269
270static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
271{
272 pud_t *pud = pud_offset(pgd, 0);
273 unsigned long addr;
274 unsigned i;
275
276 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
277 addr = start + i * PUD_SIZE;
a1c76574 278 if (pud_none(*pud) || pud_sect(*pud)) {
c9465b4e 279 note_page(st, addr, 2, pud_val(*pud));
a1c76574
MR
280 } else {
281 BUG_ON(pud_bad(*pud));
c9465b4e 282 walk_pmd(st, pud, addr);
a1c76574 283 }
c9465b4e
LA
284 }
285}
286
287static void walk_pgd(struct pg_state *st, struct mm_struct *mm, unsigned long start)
288{
35545f0c 289 pgd_t *pgd = pgd_offset(mm, 0UL);
c9465b4e
LA
290 unsigned i;
291 unsigned long addr;
292
293 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
294 addr = start + i * PGDIR_SIZE;
a1c76574 295 if (pgd_none(*pgd)) {
c9465b4e 296 note_page(st, addr, 1, pgd_val(*pgd));
a1c76574
MR
297 } else {
298 BUG_ON(pgd_bad(*pgd));
c9465b4e 299 walk_pud(st, pgd, addr);
a1c76574 300 }
c9465b4e
LA
301 }
302}
303
304static int ptdump_show(struct seq_file *m, void *v)
305{
306 struct pg_state st = {
307 .seq = m,
308 .marker = address_markers,
309 };
310
cc30e6b9 311 walk_pgd(&st, &init_mm, VA_START);
c9465b4e
LA
312
313 note_page(&st, 0, 0, 0);
314 return 0;
315}
316
317static int ptdump_open(struct inode *inode, struct file *file)
318{
319 return single_open(file, ptdump_show, NULL);
320}
321
322static const struct file_operations ptdump_fops = {
323 .open = ptdump_open,
324 .read = seq_read,
325 .llseek = seq_lseek,
326 .release = single_release,
327};
328
329static int ptdump_init(void)
330{
331 struct dentry *pe;
332 unsigned i, j;
333
334 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
335 if (pg_level[i].bits)
336 for (j = 0; j < pg_level[i].num; j++)
337 pg_level[i].mask |= pg_level[i].bits[j].mask;
338
c9465b4e
LA
339 pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL,
340 &ptdump_fops);
341 return pe ? 0 : -ENOMEM;
342}
343device_initcall(ptdump_init);
This page took 0.093363 seconds and 5 git commands to generate.