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