blob: 9c3e75df21804bdefec8587550e53c8b3fd8920e [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>
Ard Biesheuveld8fc68a2016-04-22 18:48:04 +020026#include <asm/kasan.h>
Mark Rutland764011c2015-01-22 18:20:36 +000027#include <asm/memory.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000028#include <asm/pgtable.h>
Mark Rutland764011c2015-01-22 18:20:36 +000029#include <asm/pgtable-hwdef.h>
Mark Rutland4674fdb2016-05-31 14:49:01 +010030#include <asm/ptdump.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000031
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020032static const struct addr_marker address_markers[] = {
Ard Biesheuveld8fc68a2016-04-22 18:48:04 +020033#ifdef CONFIG_KASAN
34 { KASAN_SHADOW_START, "Kasan shadow start" },
35 { KASAN_SHADOW_END, "Kasan shadow end" },
36#endif
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020037 { MODULES_VADDR, "Modules start" },
38 { MODULES_END, "Modules end" },
39 { VMALLOC_START, "vmalloc() Area" },
40 { VMALLOC_END, "vmalloc() End" },
41 { FIXADDR_START, "Fixmap start" },
42 { FIXADDR_TOP, "Fixmap end" },
43 { PCI_IO_START, "PCI I/O start" },
44 { PCI_IO_END, "PCI I/O end" },
Ard Biesheuvel3e1907d2016-03-30 16:46:00 +020045#ifdef CONFIG_SPARSEMEM_VMEMMAP
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020046 { VMEMMAP_START, "vmemmap start" },
47 { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" },
Ard Biesheuvel3e1907d2016-03-30 16:46:00 +020048#endif
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020049 { PAGE_OFFSET, "Linear Mapping" },
50 { -1, NULL },
Laura Abbottc9465b42014-11-26 00:28:39 +000051};
52
Jeremy Linton202e41a2015-10-07 12:00:23 -050053/*
54 * The page dumper groups page table entries of the same type into a single
55 * description. It uses pg_state to track the range information while
56 * iterating over the pte entries. When the continuity is broken it then
57 * dumps out a description of the range.
58 */
Laura Abbottc9465b42014-11-26 00:28:39 +000059struct pg_state {
60 struct seq_file *seq;
61 const struct addr_marker *marker;
62 unsigned long start_address;
63 unsigned level;
64 u64 current_prot;
65};
66
67struct prot_bits {
68 u64 mask;
69 u64 val;
70 const char *set;
71 const char *clear;
72};
73
74static const struct prot_bits pte_bits[] = {
75 {
Laura Abbottd7e9d592016-02-05 16:24:48 -080076 .mask = PTE_VALID,
77 .val = PTE_VALID,
78 .set = " ",
79 .clear = "F",
80 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +000081 .mask = PTE_USER,
82 .val = PTE_USER,
83 .set = "USR",
84 .clear = " ",
85 }, {
86 .mask = PTE_RDONLY,
87 .val = PTE_RDONLY,
88 .set = "ro",
89 .clear = "RW",
90 }, {
91 .mask = PTE_PXN,
92 .val = PTE_PXN,
93 .set = "NX",
94 .clear = "x ",
95 }, {
96 .mask = PTE_SHARED,
97 .val = PTE_SHARED,
98 .set = "SHD",
99 .clear = " ",
100 }, {
101 .mask = PTE_AF,
102 .val = PTE_AF,
103 .set = "AF",
104 .clear = " ",
105 }, {
106 .mask = PTE_NG,
107 .val = PTE_NG,
108 .set = "NG",
109 .clear = " ",
110 }, {
Jeremy Linton202e41a2015-10-07 12:00:23 -0500111 .mask = PTE_CONT,
112 .val = PTE_CONT,
113 .set = "CON",
114 .clear = " ",
115 }, {
116 .mask = PTE_TABLE_BIT,
117 .val = PTE_TABLE_BIT,
118 .set = " ",
119 .clear = "BLK",
120 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000121 .mask = PTE_UXN,
122 .val = PTE_UXN,
123 .set = "UXN",
124 }, {
125 .mask = PTE_ATTRINDX_MASK,
126 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
127 .set = "DEVICE/nGnRnE",
128 }, {
129 .mask = PTE_ATTRINDX_MASK,
130 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
131 .set = "DEVICE/nGnRE",
132 }, {
133 .mask = PTE_ATTRINDX_MASK,
134 .val = PTE_ATTRINDX(MT_DEVICE_GRE),
135 .set = "DEVICE/GRE",
136 }, {
137 .mask = PTE_ATTRINDX_MASK,
138 .val = PTE_ATTRINDX(MT_NORMAL_NC),
139 .set = "MEM/NORMAL-NC",
140 }, {
141 .mask = PTE_ATTRINDX_MASK,
142 .val = PTE_ATTRINDX(MT_NORMAL),
143 .set = "MEM/NORMAL",
144 }
145};
146
147struct pg_level {
148 const struct prot_bits *bits;
Mark Rutland48dd73c2016-05-31 14:49:02 +0100149 const char *name;
Laura Abbottc9465b42014-11-26 00:28:39 +0000150 size_t num;
151 u64 mask;
152};
153
154static struct pg_level pg_level[] = {
155 {
156 }, { /* pgd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100157 .name = "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000158 .bits = pte_bits,
159 .num = ARRAY_SIZE(pte_bits),
160 }, { /* pud */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100161 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000162 .bits = pte_bits,
163 .num = ARRAY_SIZE(pte_bits),
164 }, { /* pmd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100165 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000166 .bits = pte_bits,
167 .num = ARRAY_SIZE(pte_bits),
168 }, { /* pte */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100169 .name = "PTE",
Laura Abbottc9465b42014-11-26 00:28:39 +0000170 .bits = pte_bits,
171 .num = ARRAY_SIZE(pte_bits),
172 },
173};
174
175static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
176 size_t num)
177{
178 unsigned i;
179
180 for (i = 0; i < num; i++, bits++) {
181 const char *s;
182
183 if ((st->current_prot & bits->mask) == bits->val)
184 s = bits->set;
185 else
186 s = bits->clear;
187
188 if (s)
189 seq_printf(st->seq, " %s", s);
190 }
191}
192
193static void note_page(struct pg_state *st, unsigned long addr, unsigned level,
194 u64 val)
195{
196 static const char units[] = "KMGTPE";
197 u64 prot = val & pg_level[level].mask;
198
Laura Abbottc9465b42014-11-26 00:28:39 +0000199 if (!st->level) {
200 st->level = level;
201 st->current_prot = prot;
202 st->start_address = addr;
203 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
204 } else if (prot != st->current_prot || level != st->level ||
205 addr >= st->marker[1].start_address) {
206 const char *unit = units;
207 unsigned long delta;
208
209 if (st->current_prot) {
Jeremy Linton202e41a2015-10-07 12:00:23 -0500210 seq_printf(st->seq, "0x%016lx-0x%016lx ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000211 st->start_address, addr);
212
213 delta = (addr - st->start_address) >> 10;
214 while (!(delta & 1023) && unit[1]) {
215 delta >>= 10;
216 unit++;
217 }
Mark Rutland48dd73c2016-05-31 14:49:02 +0100218 seq_printf(st->seq, "%9lu%c %s", delta, *unit,
219 pg_level[st->level].name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000220 if (pg_level[st->level].bits)
221 dump_prot(st, pg_level[st->level].bits,
222 pg_level[st->level].num);
223 seq_puts(st->seq, "\n");
224 }
225
226 if (addr >= st->marker[1].start_address) {
227 st->marker++;
228 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
229 }
230
231 st->start_address = addr;
232 st->current_prot = prot;
233 st->level = level;
234 }
235
236 if (addr >= st->marker[1].start_address) {
237 st->marker++;
238 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
239 }
240
241}
242
243static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
244{
Catalin Marinasa93a4d62014-12-05 12:34:54 +0000245 pte_t *pte = pte_offset_kernel(pmd, 0UL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000246 unsigned long addr;
247 unsigned i;
248
249 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
250 addr = start + i * PAGE_SIZE;
251 note_page(st, addr, 4, pte_val(*pte));
252 }
253}
254
255static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
256{
Catalin Marinasa93a4d62014-12-05 12:34:54 +0000257 pmd_t *pmd = pmd_offset(pud, 0UL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000258 unsigned long addr;
259 unsigned i;
260
261 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
262 addr = start + i * PMD_SIZE;
Mark Rutlanda1c76572015-01-27 16:36:30 +0000263 if (pmd_none(*pmd) || pmd_sect(*pmd)) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000264 note_page(st, addr, 3, pmd_val(*pmd));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000265 } else {
266 BUG_ON(pmd_bad(*pmd));
Laura Abbottc9465b42014-11-26 00:28:39 +0000267 walk_pte(st, pmd, addr);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000268 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000269 }
270}
271
272static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
273{
Catalin Marinasa93a4d62014-12-05 12:34:54 +0000274 pud_t *pud = pud_offset(pgd, 0UL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000275 unsigned long addr;
276 unsigned i;
277
278 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
279 addr = start + i * PUD_SIZE;
Mark Rutlanda1c76572015-01-27 16:36:30 +0000280 if (pud_none(*pud) || pud_sect(*pud)) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000281 note_page(st, addr, 2, pud_val(*pud));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000282 } else {
283 BUG_ON(pud_bad(*pud));
Laura Abbottc9465b42014-11-26 00:28:39 +0000284 walk_pmd(st, pud, addr);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000285 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000286 }
287}
288
Mark Rutland4674fdb2016-05-31 14:49:01 +0100289static void walk_pgd(struct pg_state *st, struct mm_struct *mm,
290 unsigned long start)
Laura Abbottc9465b42014-11-26 00:28:39 +0000291{
Mark Rutland35545f0c2014-12-05 12:34:54 +0000292 pgd_t *pgd = pgd_offset(mm, 0UL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000293 unsigned i;
294 unsigned long addr;
295
296 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
297 addr = start + i * PGDIR_SIZE;
Mark Rutlanda1c76572015-01-27 16:36:30 +0000298 if (pgd_none(*pgd)) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000299 note_page(st, addr, 1, pgd_val(*pgd));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000300 } else {
301 BUG_ON(pgd_bad(*pgd));
Laura Abbottc9465b42014-11-26 00:28:39 +0000302 walk_pud(st, pgd, addr);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000303 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000304 }
305}
306
307static int ptdump_show(struct seq_file *m, void *v)
308{
Mark Rutland4674fdb2016-05-31 14:49:01 +0100309 struct ptdump_info *info = m->private;
Laura Abbottc9465b42014-11-26 00:28:39 +0000310 struct pg_state st = {
311 .seq = m,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100312 .marker = info->markers,
Laura Abbottc9465b42014-11-26 00:28:39 +0000313 };
314
Mark Rutland4674fdb2016-05-31 14:49:01 +0100315 walk_pgd(&st, info->mm, info->base_addr);
Laura Abbottc9465b42014-11-26 00:28:39 +0000316
317 note_page(&st, 0, 0, 0);
318 return 0;
319}
320
321static int ptdump_open(struct inode *inode, struct file *file)
322{
Mark Rutland4674fdb2016-05-31 14:49:01 +0100323 return single_open(file, ptdump_show, inode->i_private);
Laura Abbottc9465b42014-11-26 00:28:39 +0000324}
325
326static const struct file_operations ptdump_fops = {
327 .open = ptdump_open,
328 .read = seq_read,
329 .llseek = seq_lseek,
330 .release = single_release,
331};
332
Mark Rutland4674fdb2016-05-31 14:49:01 +0100333int ptdump_register(struct ptdump_info *info, const char *name)
Laura Abbottc9465b42014-11-26 00:28:39 +0000334{
335 struct dentry *pe;
336 unsigned i, j;
337
338 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
339 if (pg_level[i].bits)
340 for (j = 0; j < pg_level[i].num; j++)
341 pg_level[i].mask |= pg_level[i].bits[j].mask;
342
Mark Rutland4674fdb2016-05-31 14:49:01 +0100343 pe = debugfs_create_file(name, 0400, NULL, info, &ptdump_fops);
Laura Abbottc9465b42014-11-26 00:28:39 +0000344 return pe ? 0 : -ENOMEM;
345}
Mark Rutland4674fdb2016-05-31 14:49:01 +0100346
347static struct ptdump_info kernel_ptdump_info = {
348 .mm = &init_mm,
349 .markers = address_markers,
350 .base_addr = VA_START,
351};
352
353static int ptdump_init(void)
354{
355 return ptdump_register(&kernel_ptdump_info, "kernel_page_tables");
356}
Laura Abbottc9465b42014-11-26 00:28:39 +0000357device_initcall(ptdump_init);