blob: 59424937e52b8839c4fb4504aedbc93584b4304e [file] [log] [blame]
Russell King1fd15b82013-10-23 16:13:02 +01001/*
2 * Debug helper to dump the current kernel pagetables of the system
3 * so that we can see what the various memory ranges are set to.
4 *
5 * Derived from x86 implementation:
6 * (C) Copyright 2008 Intel Corporation
7 *
8 * Author: Arjan van de Ven <arjan@linux.intel.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; version 2
13 * of the License.
14 */
15#include <linux/debugfs.h>
16#include <linux/fs.h>
17#include <linux/mm.h>
18#include <linux/seq_file.h>
19
20#include <asm/fixmap.h>
21#include <asm/pgtable.h>
22
23struct addr_marker {
24 unsigned long start_address;
25 const char *name;
26};
27
28static struct addr_marker address_markers[] = {
29 { MODULES_VADDR, "Modules" },
30 { PAGE_OFFSET, "Kernel Mapping" },
31 { 0, "vmalloc() Area" },
32 { VMALLOC_END, "vmalloc() End" },
33 { FIXADDR_START, "Fixmap Area" },
34 { CONFIG_VECTORS_BASE, "Vectors" },
35 { CONFIG_VECTORS_BASE + PAGE_SIZE * 2, "Vectors End" },
36 { -1, NULL },
37};
38
39struct pg_state {
40 struct seq_file *seq;
41 const struct addr_marker *marker;
42 unsigned long start_address;
43 unsigned level;
44 u64 current_prot;
45};
46
47struct prot_bits {
48 u64 mask;
49 u64 val;
50 const char *set;
51 const char *clear;
52};
53
54static const struct prot_bits pte_bits[] = {
55 {
56 .mask = L_PTE_USER,
57 .val = L_PTE_USER,
58 .set = "USR",
59 .clear = " ",
60 }, {
61 .mask = L_PTE_RDONLY,
62 .val = L_PTE_RDONLY,
63 .set = "ro",
64 .clear = "RW",
65 }, {
66 .mask = L_PTE_XN,
67 .val = L_PTE_XN,
68 .set = "NX",
69 .clear = "x ",
70 }, {
71 .mask = L_PTE_SHARED,
72 .val = L_PTE_SHARED,
73 .set = "SHD",
74 .clear = " ",
75 }, {
76 .mask = L_PTE_MT_MASK,
77 .val = L_PTE_MT_UNCACHED,
78 .set = "SO/UNCACHED",
79 }, {
80 .mask = L_PTE_MT_MASK,
81 .val = L_PTE_MT_BUFFERABLE,
82 .set = "MEM/BUFFERABLE/WC",
83 }, {
84 .mask = L_PTE_MT_MASK,
85 .val = L_PTE_MT_WRITETHROUGH,
86 .set = "MEM/CACHED/WT",
87 }, {
88 .mask = L_PTE_MT_MASK,
89 .val = L_PTE_MT_WRITEBACK,
90 .set = "MEM/CACHED/WBRA",
91#ifndef CONFIG_ARM_LPAE
92 }, {
93 .mask = L_PTE_MT_MASK,
94 .val = L_PTE_MT_MINICACHE,
95 .set = "MEM/MINICACHE",
96#endif
97 }, {
98 .mask = L_PTE_MT_MASK,
99 .val = L_PTE_MT_WRITEALLOC,
100 .set = "MEM/CACHED/WBWA",
101 }, {
102 .mask = L_PTE_MT_MASK,
103 .val = L_PTE_MT_DEV_SHARED,
104 .set = "DEV/SHARED",
105#ifndef CONFIG_ARM_LPAE
106 }, {
107 .mask = L_PTE_MT_MASK,
108 .val = L_PTE_MT_DEV_NONSHARED,
109 .set = "DEV/NONSHARED",
110#endif
111 }, {
112 .mask = L_PTE_MT_MASK,
113 .val = L_PTE_MT_DEV_WC,
114 .set = "DEV/WC",
115 }, {
116 .mask = L_PTE_MT_MASK,
117 .val = L_PTE_MT_DEV_CACHED,
118 .set = "DEV/CACHED",
119 },
120};
121
122static const struct prot_bits section_bits[] = {
Kees Cookfff00db2014-03-31 22:20:34 +0100123#ifdef CONFIG_ARM_LPAE
Russell King1fd15b82013-10-23 16:13:02 +0100124 {
125 .mask = PMD_SECT_USER,
126 .val = PMD_SECT_USER,
127 .set = "USR",
128 }, {
Steven Capperded94772014-07-18 16:16:15 +0100129 .mask = L_PMD_SECT_RDONLY,
130 .val = L_PMD_SECT_RDONLY,
Russell King1fd15b82013-10-23 16:13:02 +0100131 .set = "ro",
132 .clear = "RW",
Kees Cookfff00db2014-03-31 22:20:34 +0100133#elif __LINUX_ARM_ARCH__ >= 6
134 {
135 .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
136 .val = PMD_SECT_APX | PMD_SECT_AP_WRITE,
137 .set = " ro",
138 }, {
139 .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
140 .val = PMD_SECT_AP_WRITE,
141 .set = " RW",
142 }, {
143 .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
144 .val = PMD_SECT_AP_READ,
145 .set = "USR ro",
146 }, {
147 .mask = PMD_SECT_APX | PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
148 .val = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
149 .set = "USR RW",
150#else /* ARMv4/ARMv5 */
151 /* These are approximate */
152 {
153 .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
154 .val = 0,
155 .set = " ro",
156 }, {
157 .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
158 .val = PMD_SECT_AP_WRITE,
159 .set = " RW",
160 }, {
161 .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
162 .val = PMD_SECT_AP_READ,
163 .set = "USR ro",
164 }, {
165 .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
166 .val = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE,
167 .set = "USR RW",
Russell King1fd15b82013-10-23 16:13:02 +0100168#endif
169 }, {
170 .mask = PMD_SECT_XN,
171 .val = PMD_SECT_XN,
172 .set = "NX",
173 .clear = "x ",
174 }, {
175 .mask = PMD_SECT_S,
176 .val = PMD_SECT_S,
177 .set = "SHD",
178 .clear = " ",
179 },
180};
181
182struct pg_level {
183 const struct prot_bits *bits;
184 size_t num;
185 u64 mask;
186};
187
188static struct pg_level pg_level[] = {
189 {
190 }, { /* pgd */
191 }, { /* pud */
192 }, { /* pmd */
193 .bits = section_bits,
194 .num = ARRAY_SIZE(section_bits),
195 }, { /* pte */
196 .bits = pte_bits,
197 .num = ARRAY_SIZE(pte_bits),
198 },
199};
200
201static void dump_prot(struct pg_state *st, const struct prot_bits *bits, size_t num)
202{
203 unsigned i;
204
205 for (i = 0; i < num; i++, bits++) {
206 const char *s;
207
208 if ((st->current_prot & bits->mask) == bits->val)
209 s = bits->set;
210 else
211 s = bits->clear;
212
213 if (s)
214 seq_printf(st->seq, " %s", s);
215 }
216}
217
218static void note_page(struct pg_state *st, unsigned long addr, unsigned level, u64 val)
219{
220 static const char units[] = "KMGTPE";
221 u64 prot = val & pg_level[level].mask;
222
223 if (addr < USER_PGTABLES_CEILING)
224 return;
225
226 if (!st->level) {
227 st->level = level;
228 st->current_prot = prot;
229 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
230 } else if (prot != st->current_prot || level != st->level ||
231 addr >= st->marker[1].start_address) {
232 const char *unit = units;
233 unsigned long delta;
234
235 if (st->current_prot) {
236 seq_printf(st->seq, "0x%08lx-0x%08lx ",
237 st->start_address, addr);
238
239 delta = (addr - st->start_address) >> 10;
240 while (!(delta & 1023) && unit[1]) {
241 delta >>= 10;
242 unit++;
243 }
244 seq_printf(st->seq, "%9lu%c", delta, *unit);
245 if (pg_level[st->level].bits)
246 dump_prot(st, pg_level[st->level].bits, pg_level[st->level].num);
247 seq_printf(st->seq, "\n");
248 }
249
250 if (addr >= st->marker[1].start_address) {
251 st->marker++;
252 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
253 }
254 st->start_address = addr;
255 st->current_prot = prot;
256 st->level = level;
257 }
258}
259
260static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
261{
262 pte_t *pte = pte_offset_kernel(pmd, 0);
263 unsigned long addr;
264 unsigned i;
265
266 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
267 addr = start + i * PAGE_SIZE;
268 note_page(st, addr, 4, pte_val(*pte));
269 }
270}
271
272static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
273{
274 pmd_t *pmd = pmd_offset(pud, 0);
275 unsigned long addr;
276 unsigned i;
277
278 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
279 addr = start + i * PMD_SIZE;
280 if (pmd_none(*pmd) || pmd_large(*pmd) || !pmd_present(*pmd))
281 note_page(st, addr, 3, pmd_val(*pmd));
282 else
283 walk_pte(st, pmd, addr);
Kees Cookcd91b2f2014-02-14 20:19:03 +0100284
285 if (SECTION_SIZE < PMD_SIZE && pmd_large(pmd[1]))
286 note_page(st, addr + SECTION_SIZE, 3, pmd_val(pmd[1]));
Russell King1fd15b82013-10-23 16:13:02 +0100287 }
288}
289
290static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
291{
292 pud_t *pud = pud_offset(pgd, 0);
293 unsigned long addr;
294 unsigned i;
295
296 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
297 addr = start + i * PUD_SIZE;
298 if (!pud_none(*pud)) {
299 walk_pmd(st, pud, addr);
300 } else {
301 note_page(st, addr, 2, pud_val(*pud));
302 }
303 }
304}
305
306static void walk_pgd(struct seq_file *m)
307{
308 pgd_t *pgd = swapper_pg_dir;
309 struct pg_state st;
310 unsigned long addr;
311 unsigned i, pgdoff = USER_PGTABLES_CEILING / PGDIR_SIZE;
312
313 memset(&st, 0, sizeof(st));
314 st.seq = m;
315 st.marker = address_markers;
316
317 pgd += pgdoff;
318
319 for (i = pgdoff; i < PTRS_PER_PGD; i++, pgd++) {
320 addr = i * PGDIR_SIZE;
321 if (!pgd_none(*pgd)) {
322 walk_pud(&st, pgd, addr);
323 } else {
324 note_page(&st, addr, 1, pgd_val(*pgd));
325 }
326 }
327
328 note_page(&st, 0, 0, 0);
329}
330
331static int ptdump_show(struct seq_file *m, void *v)
332{
333 walk_pgd(m);
334 return 0;
335}
336
337static int ptdump_open(struct inode *inode, struct file *file)
338{
339 return single_open(file, ptdump_show, NULL);
340}
341
342static const struct file_operations ptdump_fops = {
343 .open = ptdump_open,
344 .read = seq_read,
345 .llseek = seq_lseek,
346 .release = single_release,
347};
348
349static int ptdump_init(void)
350{
351 struct dentry *pe;
352 unsigned i, j;
353
354 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
355 if (pg_level[i].bits)
356 for (j = 0; j < pg_level[i].num; j++)
357 pg_level[i].mask |= pg_level[i].bits[j].mask;
358
359 address_markers[2].start_address = VMALLOC_START;
360
361 pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL,
362 &ptdump_fops);
363 return pe ? 0 : -ENOMEM;
364}
365__initcall(ptdump_init);