blob: 5a22a119a74c87b4b5b54e114701b3c6eed233e6 [file] [log] [blame]
Laura Abbottc9465b42014-11-26 00:28:39 +00001/*
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>
Mark Rutland764011c2015-01-22 18:20:36 +000017#include <linux/errno.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000018#include <linux/fs.h>
Mark Brown284be282015-01-22 20:52:10 +000019#include <linux/io.h>
Mark Rutland764011c2015-01-22 18:20:36 +000020#include <linux/init.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000021#include <linux/mm.h>
22#include <linux/sched.h>
23#include <linux/seq_file.h>
24
25#include <asm/fixmap.h>
Mark Rutland764011c2015-01-22 18:20:36 +000026#include <asm/memory.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000027#include <asm/pgtable.h>
Mark Rutland764011c2015-01-22 18:20:36 +000028#include <asm/pgtable-hwdef.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000029
30#define LOWEST_ADDR (UL(0xffffffffffffffff) << VA_BITS)
31
32struct addr_marker {
33 unsigned long start_address;
34 const char *name;
35};
36
37enum address_markers_idx {
38 VMALLOC_START_NR = 0,
39 VMALLOC_END_NR,
40#ifdef CONFIG_SPARSEMEM_VMEMMAP
41 VMEMMAP_START_NR,
42 VMEMMAP_END_NR,
43#endif
Laura Abbottc9465b42014-11-26 00:28:39 +000044 FIXADDR_START_NR,
45 FIXADDR_END_NR,
Mark Rutlandaa03c422015-01-22 18:20:35 +000046 PCI_START_NR,
47 PCI_END_NR,
Laura Abbottc9465b42014-11-26 00:28:39 +000048 MODULES_START_NR,
49 MODUELS_END_NR,
50 KERNEL_SPACE_NR,
51};
52
53static struct addr_marker address_markers[] = {
54 { VMALLOC_START, "vmalloc() Area" },
55 { VMALLOC_END, "vmalloc() End" },
56#ifdef CONFIG_SPARSEMEM_VMEMMAP
57 { 0, "vmemmap start" },
58 { 0, "vmemmap end" },
59#endif
Laura Abbottc9465b42014-11-26 00:28:39 +000060 { FIXADDR_START, "Fixmap start" },
61 { FIXADDR_TOP, "Fixmap end" },
Mark Rutlandaa03c422015-01-22 18:20:35 +000062 { PCI_IO_START, "PCI I/O start" },
63 { PCI_IO_END, "PCI I/O end" },
Laura Abbottc9465b42014-11-26 00:28:39 +000064 { MODULES_VADDR, "Modules start" },
65 { MODULES_END, "Modules end" },
66 { PAGE_OFFSET, "Kernel Mapping" },
67 { -1, NULL },
68};
69
Jeremy Linton202e41a2015-10-07 12:00:23 -050070/*
71 * The page dumper groups page table entries of the same type into a single
72 * description. It uses pg_state to track the range information while
73 * iterating over the pte entries. When the continuity is broken it then
74 * dumps out a description of the range.
75 */
Laura Abbottc9465b42014-11-26 00:28:39 +000076struct pg_state {
77 struct seq_file *seq;
78 const struct addr_marker *marker;
79 unsigned long start_address;
80 unsigned level;
81 u64 current_prot;
82};
83
84struct prot_bits {
85 u64 mask;
86 u64 val;
87 const char *set;
88 const char *clear;
89};
90
91static const struct prot_bits pte_bits[] = {
92 {
93 .mask = PTE_USER,
94 .val = PTE_USER,
95 .set = "USR",
96 .clear = " ",
97 }, {
98 .mask = PTE_RDONLY,
99 .val = PTE_RDONLY,
100 .set = "ro",
101 .clear = "RW",
102 }, {
103 .mask = PTE_PXN,
104 .val = PTE_PXN,
105 .set = "NX",
106 .clear = "x ",
107 }, {
108 .mask = PTE_SHARED,
109 .val = PTE_SHARED,
110 .set = "SHD",
111 .clear = " ",
112 }, {
113 .mask = PTE_AF,
114 .val = PTE_AF,
115 .set = "AF",
116 .clear = " ",
117 }, {
118 .mask = PTE_NG,
119 .val = PTE_NG,
120 .set = "NG",
121 .clear = " ",
122 }, {
Jeremy Linton202e41a2015-10-07 12:00:23 -0500123 .mask = PTE_CONT,
124 .val = PTE_CONT,
125 .set = "CON",
126 .clear = " ",
127 }, {
128 .mask = PTE_TABLE_BIT,
129 .val = PTE_TABLE_BIT,
130 .set = " ",
131 .clear = "BLK",
132 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000133 .mask = PTE_UXN,
134 .val = PTE_UXN,
135 .set = "UXN",
136 }, {
137 .mask = PTE_ATTRINDX_MASK,
138 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
139 .set = "DEVICE/nGnRnE",
140 }, {
141 .mask = PTE_ATTRINDX_MASK,
142 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
143 .set = "DEVICE/nGnRE",
144 }, {
145 .mask = PTE_ATTRINDX_MASK,
146 .val = PTE_ATTRINDX(MT_DEVICE_GRE),
147 .set = "DEVICE/GRE",
148 }, {
149 .mask = PTE_ATTRINDX_MASK,
150 .val = PTE_ATTRINDX(MT_NORMAL_NC),
151 .set = "MEM/NORMAL-NC",
152 }, {
153 .mask = PTE_ATTRINDX_MASK,
154 .val = PTE_ATTRINDX(MT_NORMAL),
155 .set = "MEM/NORMAL",
156 }
157};
158
159struct pg_level {
160 const struct prot_bits *bits;
161 size_t num;
162 u64 mask;
163};
164
165static struct pg_level pg_level[] = {
166 {
167 }, { /* pgd */
168 .bits = pte_bits,
169 .num = ARRAY_SIZE(pte_bits),
170 }, { /* pud */
171 .bits = pte_bits,
172 .num = ARRAY_SIZE(pte_bits),
173 }, { /* pmd */
174 .bits = pte_bits,
175 .num = ARRAY_SIZE(pte_bits),
176 }, { /* pte */
177 .bits = pte_bits,
178 .num = ARRAY_SIZE(pte_bits),
179 },
180};
181
182static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
183 size_t num)
184{
185 unsigned i;
186
187 for (i = 0; i < num; i++, bits++) {
188 const char *s;
189
190 if ((st->current_prot & bits->mask) == bits->val)
191 s = bits->set;
192 else
193 s = bits->clear;
194
195 if (s)
196 seq_printf(st->seq, " %s", s);
197 }
198}
199
200static void note_page(struct pg_state *st, unsigned long addr, unsigned level,
201 u64 val)
202{
203 static const char units[] = "KMGTPE";
204 u64 prot = val & pg_level[level].mask;
205
Laura Abbottc9465b42014-11-26 00:28:39 +0000206 if (!st->level) {
207 st->level = level;
208 st->current_prot = prot;
209 st->start_address = addr;
210 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
211 } else if (prot != st->current_prot || level != st->level ||
212 addr >= st->marker[1].start_address) {
213 const char *unit = units;
214 unsigned long delta;
215
216 if (st->current_prot) {
Jeremy Linton202e41a2015-10-07 12:00:23 -0500217 seq_printf(st->seq, "0x%016lx-0x%016lx ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000218 st->start_address, addr);
219
220 delta = (addr - st->start_address) >> 10;
221 while (!(delta & 1023) && unit[1]) {
222 delta >>= 10;
223 unit++;
224 }
225 seq_printf(st->seq, "%9lu%c", delta, *unit);
226 if (pg_level[st->level].bits)
227 dump_prot(st, pg_level[st->level].bits,
228 pg_level[st->level].num);
229 seq_puts(st->seq, "\n");
230 }
231
232 if (addr >= st->marker[1].start_address) {
233 st->marker++;
234 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
235 }
236
237 st->start_address = addr;
238 st->current_prot = prot;
239 st->level = level;
240 }
241
242 if (addr >= st->marker[1].start_address) {
243 st->marker++;
244 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
245 }
246
247}
248
249static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
250{
251 pte_t *pte = pte_offset_kernel(pmd, 0);
252 unsigned long addr;
253 unsigned i;
254
255 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
256 addr = start + i * PAGE_SIZE;
257 note_page(st, addr, 4, pte_val(*pte));
258 }
259}
260
261static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
262{
263 pmd_t *pmd = pmd_offset(pud, 0);
264 unsigned long addr;
265 unsigned i;
266
267 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
268 addr = start + i * PMD_SIZE;
Mark Rutlanda1c76572015-01-27 16:36:30 +0000269 if (pmd_none(*pmd) || pmd_sect(*pmd)) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000270 note_page(st, addr, 3, pmd_val(*pmd));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000271 } else {
272 BUG_ON(pmd_bad(*pmd));
Laura Abbottc9465b42014-11-26 00:28:39 +0000273 walk_pte(st, pmd, addr);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000274 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000275 }
276}
277
278static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
279{
280 pud_t *pud = pud_offset(pgd, 0);
281 unsigned long addr;
282 unsigned i;
283
284 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
285 addr = start + i * PUD_SIZE;
Mark Rutlanda1c76572015-01-27 16:36:30 +0000286 if (pud_none(*pud) || pud_sect(*pud)) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000287 note_page(st, addr, 2, pud_val(*pud));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000288 } else {
289 BUG_ON(pud_bad(*pud));
Laura Abbottc9465b42014-11-26 00:28:39 +0000290 walk_pmd(st, pud, addr);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000291 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000292 }
293}
294
295static void walk_pgd(struct pg_state *st, struct mm_struct *mm, unsigned long start)
296{
Mark Rutland35545f0c2014-12-05 12:34:54 +0000297 pgd_t *pgd = pgd_offset(mm, 0UL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000298 unsigned i;
299 unsigned long addr;
300
301 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
302 addr = start + i * PGDIR_SIZE;
Mark Rutlanda1c76572015-01-27 16:36:30 +0000303 if (pgd_none(*pgd)) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000304 note_page(st, addr, 1, pgd_val(*pgd));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000305 } else {
306 BUG_ON(pgd_bad(*pgd));
Laura Abbottc9465b42014-11-26 00:28:39 +0000307 walk_pud(st, pgd, addr);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000308 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000309 }
310}
311
312static int ptdump_show(struct seq_file *m, void *v)
313{
314 struct pg_state st = {
315 .seq = m,
316 .marker = address_markers,
317 };
318
319 walk_pgd(&st, &init_mm, LOWEST_ADDR);
320
321 note_page(&st, 0, 0, 0);
322 return 0;
323}
324
325static int ptdump_open(struct inode *inode, struct file *file)
326{
327 return single_open(file, ptdump_show, NULL);
328}
329
330static const struct file_operations ptdump_fops = {
331 .open = ptdump_open,
332 .read = seq_read,
333 .llseek = seq_lseek,
334 .release = single_release,
335};
336
337static int ptdump_init(void)
338{
339 struct dentry *pe;
340 unsigned i, j;
341
342 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
343 if (pg_level[i].bits)
344 for (j = 0; j < pg_level[i].num; j++)
345 pg_level[i].mask |= pg_level[i].bits[j].mask;
346
Jungseung Lee326a7802015-05-04 11:33:48 +0100347#ifdef CONFIG_SPARSEMEM_VMEMMAP
Laura Abbottc9465b42014-11-26 00:28:39 +0000348 address_markers[VMEMMAP_START_NR].start_address =
349 (unsigned long)virt_to_page(PAGE_OFFSET);
350 address_markers[VMEMMAP_END_NR].start_address =
351 (unsigned long)virt_to_page(high_memory);
Jungseung Lee326a7802015-05-04 11:33:48 +0100352#endif
Laura Abbottc9465b42014-11-26 00:28:39 +0000353
354 pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL,
355 &ptdump_fops);
356 return pe ? 0 : -ENOMEM;
357}
358device_initcall(ptdump_init);