Russell King | 1fd15b8 | 2013-10-23 16:13:02 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | struct addr_marker { |
| 24 | unsigned long start_address; |
| 25 | const char *name; |
| 26 | }; |
| 27 | |
| 28 | static 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 | |
| 39 | struct 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 | |
| 47 | struct prot_bits { |
| 48 | u64 mask; |
| 49 | u64 val; |
| 50 | const char *set; |
| 51 | const char *clear; |
| 52 | }; |
| 53 | |
| 54 | static 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 | |
| 122 | static const struct prot_bits section_bits[] = { |
| 123 | #ifndef CONFIG_ARM_LPAE |
| 124 | /* These are approximate */ |
| 125 | { |
| 126 | .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, |
| 127 | .val = 0, |
| 128 | .set = " ro", |
| 129 | }, { |
| 130 | .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, |
| 131 | .val = PMD_SECT_AP_WRITE, |
| 132 | .set = " RW", |
| 133 | }, { |
| 134 | .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, |
| 135 | .val = PMD_SECT_AP_READ, |
| 136 | .set = "USR ro", |
| 137 | }, { |
| 138 | .mask = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, |
| 139 | .val = PMD_SECT_AP_READ | PMD_SECT_AP_WRITE, |
| 140 | .set = "USR RW", |
| 141 | #else |
| 142 | { |
| 143 | .mask = PMD_SECT_USER, |
| 144 | .val = PMD_SECT_USER, |
| 145 | .set = "USR", |
| 146 | }, { |
| 147 | .mask = PMD_SECT_RDONLY, |
| 148 | .val = PMD_SECT_RDONLY, |
| 149 | .set = "ro", |
| 150 | .clear = "RW", |
| 151 | #endif |
| 152 | }, { |
| 153 | .mask = PMD_SECT_XN, |
| 154 | .val = PMD_SECT_XN, |
| 155 | .set = "NX", |
| 156 | .clear = "x ", |
| 157 | }, { |
| 158 | .mask = PMD_SECT_S, |
| 159 | .val = PMD_SECT_S, |
| 160 | .set = "SHD", |
| 161 | .clear = " ", |
| 162 | }, |
| 163 | }; |
| 164 | |
| 165 | struct pg_level { |
| 166 | const struct prot_bits *bits; |
| 167 | size_t num; |
| 168 | u64 mask; |
| 169 | }; |
| 170 | |
| 171 | static struct pg_level pg_level[] = { |
| 172 | { |
| 173 | }, { /* pgd */ |
| 174 | }, { /* pud */ |
| 175 | }, { /* pmd */ |
| 176 | .bits = section_bits, |
| 177 | .num = ARRAY_SIZE(section_bits), |
| 178 | }, { /* pte */ |
| 179 | .bits = pte_bits, |
| 180 | .num = ARRAY_SIZE(pte_bits), |
| 181 | }, |
| 182 | }; |
| 183 | |
| 184 | static void dump_prot(struct pg_state *st, const struct prot_bits *bits, size_t num) |
| 185 | { |
| 186 | unsigned i; |
| 187 | |
| 188 | for (i = 0; i < num; i++, bits++) { |
| 189 | const char *s; |
| 190 | |
| 191 | if ((st->current_prot & bits->mask) == bits->val) |
| 192 | s = bits->set; |
| 193 | else |
| 194 | s = bits->clear; |
| 195 | |
| 196 | if (s) |
| 197 | seq_printf(st->seq, " %s", s); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void note_page(struct pg_state *st, unsigned long addr, unsigned level, u64 val) |
| 202 | { |
| 203 | static const char units[] = "KMGTPE"; |
| 204 | u64 prot = val & pg_level[level].mask; |
| 205 | |
| 206 | if (addr < USER_PGTABLES_CEILING) |
| 207 | return; |
| 208 | |
| 209 | if (!st->level) { |
| 210 | st->level = level; |
| 211 | st->current_prot = prot; |
| 212 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 213 | } else if (prot != st->current_prot || level != st->level || |
| 214 | addr >= st->marker[1].start_address) { |
| 215 | const char *unit = units; |
| 216 | unsigned long delta; |
| 217 | |
| 218 | if (st->current_prot) { |
| 219 | seq_printf(st->seq, "0x%08lx-0x%08lx ", |
| 220 | st->start_address, addr); |
| 221 | |
| 222 | delta = (addr - st->start_address) >> 10; |
| 223 | while (!(delta & 1023) && unit[1]) { |
| 224 | delta >>= 10; |
| 225 | unit++; |
| 226 | } |
| 227 | seq_printf(st->seq, "%9lu%c", delta, *unit); |
| 228 | if (pg_level[st->level].bits) |
| 229 | dump_prot(st, pg_level[st->level].bits, pg_level[st->level].num); |
| 230 | seq_printf(st->seq, "\n"); |
| 231 | } |
| 232 | |
| 233 | if (addr >= st->marker[1].start_address) { |
| 234 | st->marker++; |
| 235 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 236 | } |
| 237 | st->start_address = addr; |
| 238 | st->current_prot = prot; |
| 239 | st->level = level; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) |
| 244 | { |
| 245 | pte_t *pte = pte_offset_kernel(pmd, 0); |
| 246 | 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 | |
| 255 | static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) |
| 256 | { |
| 257 | pmd_t *pmd = pmd_offset(pud, 0); |
| 258 | unsigned long addr; |
| 259 | unsigned i; |
| 260 | |
| 261 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { |
| 262 | addr = start + i * PMD_SIZE; |
| 263 | if (pmd_none(*pmd) || pmd_large(*pmd) || !pmd_present(*pmd)) |
| 264 | note_page(st, addr, 3, pmd_val(*pmd)); |
| 265 | else |
| 266 | walk_pte(st, pmd, addr); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static 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; |
| 278 | if (!pud_none(*pud)) { |
| 279 | walk_pmd(st, pud, addr); |
| 280 | } else { |
| 281 | note_page(st, addr, 2, pud_val(*pud)); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | static void walk_pgd(struct seq_file *m) |
| 287 | { |
| 288 | pgd_t *pgd = swapper_pg_dir; |
| 289 | struct pg_state st; |
| 290 | unsigned long addr; |
| 291 | unsigned i, pgdoff = USER_PGTABLES_CEILING / PGDIR_SIZE; |
| 292 | |
| 293 | memset(&st, 0, sizeof(st)); |
| 294 | st.seq = m; |
| 295 | st.marker = address_markers; |
| 296 | |
| 297 | pgd += pgdoff; |
| 298 | |
| 299 | for (i = pgdoff; i < PTRS_PER_PGD; i++, pgd++) { |
| 300 | addr = i * PGDIR_SIZE; |
| 301 | if (!pgd_none(*pgd)) { |
| 302 | walk_pud(&st, pgd, addr); |
| 303 | } else { |
| 304 | note_page(&st, addr, 1, pgd_val(*pgd)); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | note_page(&st, 0, 0, 0); |
| 309 | } |
| 310 | |
| 311 | static int ptdump_show(struct seq_file *m, void *v) |
| 312 | { |
| 313 | walk_pgd(m); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int ptdump_open(struct inode *inode, struct file *file) |
| 318 | { |
| 319 | return single_open(file, ptdump_show, NULL); |
| 320 | } |
| 321 | |
| 322 | static const struct file_operations ptdump_fops = { |
| 323 | .open = ptdump_open, |
| 324 | .read = seq_read, |
| 325 | .llseek = seq_lseek, |
| 326 | .release = single_release, |
| 327 | }; |
| 328 | |
| 329 | static 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 | |
| 339 | address_markers[2].start_address = VMALLOC_START; |
| 340 | |
| 341 | pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, |
| 342 | &ptdump_fops); |
| 343 | return pe ? 0 : -ENOMEM; |
| 344 | } |
| 345 | __initcall(ptdump_init); |