Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 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> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/seq_file.h> |
| 21 | |
| 22 | #include <asm/fixmap.h> |
| 23 | #include <asm/pgtable.h> |
| 24 | |
| 25 | #define LOWEST_ADDR (UL(0xffffffffffffffff) << VA_BITS) |
| 26 | |
| 27 | struct addr_marker { |
| 28 | unsigned long start_address; |
| 29 | const char *name; |
| 30 | }; |
| 31 | |
| 32 | enum address_markers_idx { |
| 33 | VMALLOC_START_NR = 0, |
| 34 | VMALLOC_END_NR, |
| 35 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 36 | VMEMMAP_START_NR, |
| 37 | VMEMMAP_END_NR, |
| 38 | #endif |
| 39 | PCI_START_NR, |
| 40 | PCI_END_NR, |
| 41 | FIXADDR_START_NR, |
| 42 | FIXADDR_END_NR, |
| 43 | MODULES_START_NR, |
| 44 | MODUELS_END_NR, |
| 45 | KERNEL_SPACE_NR, |
| 46 | }; |
| 47 | |
| 48 | static struct addr_marker address_markers[] = { |
| 49 | { VMALLOC_START, "vmalloc() Area" }, |
| 50 | { VMALLOC_END, "vmalloc() End" }, |
| 51 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 52 | { 0, "vmemmap start" }, |
| 53 | { 0, "vmemmap end" }, |
| 54 | #endif |
| 55 | { (unsigned long) PCI_IOBASE, "PCI I/O start" }, |
| 56 | { (unsigned long) PCI_IOBASE + SZ_16M, "PCI I/O end" }, |
| 57 | { FIXADDR_START, "Fixmap start" }, |
| 58 | { FIXADDR_TOP, "Fixmap end" }, |
| 59 | { MODULES_VADDR, "Modules start" }, |
| 60 | { MODULES_END, "Modules end" }, |
| 61 | { PAGE_OFFSET, "Kernel Mapping" }, |
| 62 | { -1, NULL }, |
| 63 | }; |
| 64 | |
| 65 | struct pg_state { |
| 66 | struct seq_file *seq; |
| 67 | const struct addr_marker *marker; |
| 68 | unsigned long start_address; |
| 69 | unsigned level; |
| 70 | u64 current_prot; |
| 71 | }; |
| 72 | |
| 73 | struct prot_bits { |
| 74 | u64 mask; |
| 75 | u64 val; |
| 76 | const char *set; |
| 77 | const char *clear; |
| 78 | }; |
| 79 | |
| 80 | static const struct prot_bits pte_bits[] = { |
| 81 | { |
| 82 | .mask = PTE_USER, |
| 83 | .val = PTE_USER, |
| 84 | .set = "USR", |
| 85 | .clear = " ", |
| 86 | }, { |
| 87 | .mask = PTE_RDONLY, |
| 88 | .val = PTE_RDONLY, |
| 89 | .set = "ro", |
| 90 | .clear = "RW", |
| 91 | }, { |
| 92 | .mask = PTE_PXN, |
| 93 | .val = PTE_PXN, |
| 94 | .set = "NX", |
| 95 | .clear = "x ", |
| 96 | }, { |
| 97 | .mask = PTE_SHARED, |
| 98 | .val = PTE_SHARED, |
| 99 | .set = "SHD", |
| 100 | .clear = " ", |
| 101 | }, { |
| 102 | .mask = PTE_AF, |
| 103 | .val = PTE_AF, |
| 104 | .set = "AF", |
| 105 | .clear = " ", |
| 106 | }, { |
| 107 | .mask = PTE_NG, |
| 108 | .val = PTE_NG, |
| 109 | .set = "NG", |
| 110 | .clear = " ", |
| 111 | }, { |
| 112 | .mask = PTE_UXN, |
| 113 | .val = PTE_UXN, |
| 114 | .set = "UXN", |
| 115 | }, { |
| 116 | .mask = PTE_ATTRINDX_MASK, |
| 117 | .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE), |
| 118 | .set = "DEVICE/nGnRnE", |
| 119 | }, { |
| 120 | .mask = PTE_ATTRINDX_MASK, |
| 121 | .val = PTE_ATTRINDX(MT_DEVICE_nGnRE), |
| 122 | .set = "DEVICE/nGnRE", |
| 123 | }, { |
| 124 | .mask = PTE_ATTRINDX_MASK, |
| 125 | .val = PTE_ATTRINDX(MT_DEVICE_GRE), |
| 126 | .set = "DEVICE/GRE", |
| 127 | }, { |
| 128 | .mask = PTE_ATTRINDX_MASK, |
| 129 | .val = PTE_ATTRINDX(MT_NORMAL_NC), |
| 130 | .set = "MEM/NORMAL-NC", |
| 131 | }, { |
| 132 | .mask = PTE_ATTRINDX_MASK, |
| 133 | .val = PTE_ATTRINDX(MT_NORMAL), |
| 134 | .set = "MEM/NORMAL", |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | struct pg_level { |
| 139 | const struct prot_bits *bits; |
| 140 | size_t num; |
| 141 | u64 mask; |
| 142 | }; |
| 143 | |
| 144 | static struct pg_level pg_level[] = { |
| 145 | { |
| 146 | }, { /* pgd */ |
| 147 | .bits = pte_bits, |
| 148 | .num = ARRAY_SIZE(pte_bits), |
| 149 | }, { /* pud */ |
| 150 | .bits = pte_bits, |
| 151 | .num = ARRAY_SIZE(pte_bits), |
| 152 | }, { /* pmd */ |
| 153 | .bits = pte_bits, |
| 154 | .num = ARRAY_SIZE(pte_bits), |
| 155 | }, { /* pte */ |
| 156 | .bits = pte_bits, |
| 157 | .num = ARRAY_SIZE(pte_bits), |
| 158 | }, |
| 159 | }; |
| 160 | |
| 161 | static void dump_prot(struct pg_state *st, const struct prot_bits *bits, |
| 162 | size_t num) |
| 163 | { |
| 164 | unsigned i; |
| 165 | |
| 166 | for (i = 0; i < num; i++, bits++) { |
| 167 | const char *s; |
| 168 | |
| 169 | if ((st->current_prot & bits->mask) == bits->val) |
| 170 | s = bits->set; |
| 171 | else |
| 172 | s = bits->clear; |
| 173 | |
| 174 | if (s) |
| 175 | seq_printf(st->seq, " %s", s); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static void note_page(struct pg_state *st, unsigned long addr, unsigned level, |
| 180 | u64 val) |
| 181 | { |
| 182 | static const char units[] = "KMGTPE"; |
| 183 | u64 prot = val & pg_level[level].mask; |
| 184 | |
| 185 | if (addr < LOWEST_ADDR) |
| 186 | return; |
| 187 | |
| 188 | if (!st->level) { |
| 189 | st->level = level; |
| 190 | st->current_prot = prot; |
| 191 | st->start_address = addr; |
| 192 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 193 | } else if (prot != st->current_prot || level != st->level || |
| 194 | addr >= st->marker[1].start_address) { |
| 195 | const char *unit = units; |
| 196 | unsigned long delta; |
| 197 | |
| 198 | if (st->current_prot) { |
| 199 | seq_printf(st->seq, "0x%16lx-0x%16lx ", |
| 200 | st->start_address, addr); |
| 201 | |
| 202 | delta = (addr - st->start_address) >> 10; |
| 203 | while (!(delta & 1023) && unit[1]) { |
| 204 | delta >>= 10; |
| 205 | unit++; |
| 206 | } |
| 207 | seq_printf(st->seq, "%9lu%c", delta, *unit); |
| 208 | if (pg_level[st->level].bits) |
| 209 | dump_prot(st, pg_level[st->level].bits, |
| 210 | pg_level[st->level].num); |
| 211 | seq_puts(st->seq, "\n"); |
| 212 | } |
| 213 | |
| 214 | if (addr >= st->marker[1].start_address) { |
| 215 | st->marker++; |
| 216 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 217 | } |
| 218 | |
| 219 | st->start_address = addr; |
| 220 | st->current_prot = prot; |
| 221 | st->level = level; |
| 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 | } |
| 230 | |
| 231 | static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) |
| 232 | { |
| 233 | pte_t *pte = pte_offset_kernel(pmd, 0); |
| 234 | unsigned long addr; |
| 235 | unsigned i; |
| 236 | |
| 237 | for (i = 0; i < PTRS_PER_PTE; i++, pte++) { |
| 238 | addr = start + i * PAGE_SIZE; |
| 239 | note_page(st, addr, 4, pte_val(*pte)); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) |
| 244 | { |
| 245 | pmd_t *pmd = pmd_offset(pud, 0); |
| 246 | unsigned long addr; |
| 247 | unsigned i; |
| 248 | |
| 249 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { |
| 250 | addr = start + i * PMD_SIZE; |
| 251 | if (pmd_none(*pmd) || pmd_sect(*pmd) || pmd_bad(*pmd)) |
| 252 | note_page(st, addr, 3, pmd_val(*pmd)); |
| 253 | else |
| 254 | walk_pte(st, pmd, addr); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) |
| 259 | { |
| 260 | pud_t *pud = pud_offset(pgd, 0); |
| 261 | unsigned long addr; |
| 262 | unsigned i; |
| 263 | |
| 264 | for (i = 0; i < PTRS_PER_PUD; i++, pud++) { |
| 265 | addr = start + i * PUD_SIZE; |
| 266 | if (pud_none(*pud) || pud_sect(*pud) || pud_bad(*pud)) |
| 267 | note_page(st, addr, 2, pud_val(*pud)); |
| 268 | else |
| 269 | walk_pmd(st, pud, addr); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static void walk_pgd(struct pg_state *st, struct mm_struct *mm, unsigned long start) |
| 274 | { |
| 275 | pgd_t *pgd = pgd_offset(mm, 0); |
| 276 | unsigned i; |
| 277 | unsigned long addr; |
| 278 | |
| 279 | for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { |
| 280 | addr = start + i * PGDIR_SIZE; |
| 281 | if (pgd_none(*pgd) || pgd_bad(*pgd)) |
| 282 | note_page(st, addr, 1, pgd_val(*pgd)); |
| 283 | else |
| 284 | walk_pud(st, pgd, addr); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static int ptdump_show(struct seq_file *m, void *v) |
| 289 | { |
| 290 | struct pg_state st = { |
| 291 | .seq = m, |
| 292 | .marker = address_markers, |
| 293 | }; |
| 294 | |
| 295 | walk_pgd(&st, &init_mm, LOWEST_ADDR); |
| 296 | |
| 297 | note_page(&st, 0, 0, 0); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int ptdump_open(struct inode *inode, struct file *file) |
| 302 | { |
| 303 | return single_open(file, ptdump_show, NULL); |
| 304 | } |
| 305 | |
| 306 | static const struct file_operations ptdump_fops = { |
| 307 | .open = ptdump_open, |
| 308 | .read = seq_read, |
| 309 | .llseek = seq_lseek, |
| 310 | .release = single_release, |
| 311 | }; |
| 312 | |
| 313 | static int ptdump_init(void) |
| 314 | { |
| 315 | struct dentry *pe; |
| 316 | unsigned i, j; |
| 317 | |
| 318 | for (i = 0; i < ARRAY_SIZE(pg_level); i++) |
| 319 | if (pg_level[i].bits) |
| 320 | for (j = 0; j < pg_level[i].num; j++) |
| 321 | pg_level[i].mask |= pg_level[i].bits[j].mask; |
| 322 | |
| 323 | address_markers[VMEMMAP_START_NR].start_address = |
| 324 | (unsigned long)virt_to_page(PAGE_OFFSET); |
| 325 | address_markers[VMEMMAP_END_NR].start_address = |
| 326 | (unsigned long)virt_to_page(high_memory); |
| 327 | |
| 328 | pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, |
| 329 | &ptdump_fops); |
| 330 | return pe ? 0 : -ENOMEM; |
| 331 | } |
| 332 | device_initcall(ptdump_init); |