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 | |
Jeremy Linton | 202e41a | 2015-10-07 12:00:23 -0500 | [diff] [blame] | 70 | /* |
| 71 | * The page dumper groups page table entries of the same type into a single |
| 72 | * description. It uses pg_state to track the range information while |
| 73 | * iterating over the pte entries. When the continuity is broken it then |
| 74 | * dumps out a description of the range. |
| 75 | */ |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 76 | struct pg_state { |
| 77 | struct seq_file *seq; |
| 78 | const struct addr_marker *marker; |
| 79 | unsigned long start_address; |
| 80 | unsigned level; |
| 81 | u64 current_prot; |
| 82 | }; |
| 83 | |
| 84 | struct prot_bits { |
| 85 | u64 mask; |
| 86 | u64 val; |
| 87 | const char *set; |
| 88 | const char *clear; |
| 89 | }; |
| 90 | |
| 91 | static const struct prot_bits pte_bits[] = { |
| 92 | { |
| 93 | .mask = PTE_USER, |
| 94 | .val = PTE_USER, |
| 95 | .set = "USR", |
| 96 | .clear = " ", |
| 97 | }, { |
| 98 | .mask = PTE_RDONLY, |
| 99 | .val = PTE_RDONLY, |
| 100 | .set = "ro", |
| 101 | .clear = "RW", |
| 102 | }, { |
| 103 | .mask = PTE_PXN, |
| 104 | .val = PTE_PXN, |
| 105 | .set = "NX", |
| 106 | .clear = "x ", |
| 107 | }, { |
| 108 | .mask = PTE_SHARED, |
| 109 | .val = PTE_SHARED, |
| 110 | .set = "SHD", |
| 111 | .clear = " ", |
| 112 | }, { |
| 113 | .mask = PTE_AF, |
| 114 | .val = PTE_AF, |
| 115 | .set = "AF", |
| 116 | .clear = " ", |
| 117 | }, { |
| 118 | .mask = PTE_NG, |
| 119 | .val = PTE_NG, |
| 120 | .set = "NG", |
| 121 | .clear = " ", |
| 122 | }, { |
Jeremy Linton | 202e41a | 2015-10-07 12:00:23 -0500 | [diff] [blame] | 123 | .mask = PTE_CONT, |
| 124 | .val = PTE_CONT, |
| 125 | .set = "CON", |
| 126 | .clear = " ", |
| 127 | }, { |
| 128 | .mask = PTE_TABLE_BIT, |
| 129 | .val = PTE_TABLE_BIT, |
| 130 | .set = " ", |
| 131 | .clear = "BLK", |
| 132 | }, { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 133 | .mask = PTE_UXN, |
| 134 | .val = PTE_UXN, |
| 135 | .set = "UXN", |
| 136 | }, { |
| 137 | .mask = PTE_ATTRINDX_MASK, |
| 138 | .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE), |
| 139 | .set = "DEVICE/nGnRnE", |
| 140 | }, { |
| 141 | .mask = PTE_ATTRINDX_MASK, |
| 142 | .val = PTE_ATTRINDX(MT_DEVICE_nGnRE), |
| 143 | .set = "DEVICE/nGnRE", |
| 144 | }, { |
| 145 | .mask = PTE_ATTRINDX_MASK, |
| 146 | .val = PTE_ATTRINDX(MT_DEVICE_GRE), |
| 147 | .set = "DEVICE/GRE", |
| 148 | }, { |
| 149 | .mask = PTE_ATTRINDX_MASK, |
| 150 | .val = PTE_ATTRINDX(MT_NORMAL_NC), |
| 151 | .set = "MEM/NORMAL-NC", |
| 152 | }, { |
| 153 | .mask = PTE_ATTRINDX_MASK, |
| 154 | .val = PTE_ATTRINDX(MT_NORMAL), |
| 155 | .set = "MEM/NORMAL", |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | struct pg_level { |
| 160 | const struct prot_bits *bits; |
| 161 | size_t num; |
| 162 | u64 mask; |
| 163 | }; |
| 164 | |
| 165 | static struct pg_level pg_level[] = { |
| 166 | { |
| 167 | }, { /* pgd */ |
| 168 | .bits = pte_bits, |
| 169 | .num = ARRAY_SIZE(pte_bits), |
| 170 | }, { /* pud */ |
| 171 | .bits = pte_bits, |
| 172 | .num = ARRAY_SIZE(pte_bits), |
| 173 | }, { /* pmd */ |
| 174 | .bits = pte_bits, |
| 175 | .num = ARRAY_SIZE(pte_bits), |
| 176 | }, { /* pte */ |
| 177 | .bits = pte_bits, |
| 178 | .num = ARRAY_SIZE(pte_bits), |
| 179 | }, |
| 180 | }; |
| 181 | |
| 182 | static void dump_prot(struct pg_state *st, const struct prot_bits *bits, |
| 183 | size_t num) |
| 184 | { |
| 185 | unsigned i; |
| 186 | |
| 187 | for (i = 0; i < num; i++, bits++) { |
| 188 | const char *s; |
| 189 | |
| 190 | if ((st->current_prot & bits->mask) == bits->val) |
| 191 | s = bits->set; |
| 192 | else |
| 193 | s = bits->clear; |
| 194 | |
| 195 | if (s) |
| 196 | seq_printf(st->seq, " %s", s); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static void note_page(struct pg_state *st, unsigned long addr, unsigned level, |
| 201 | u64 val) |
| 202 | { |
| 203 | static const char units[] = "KMGTPE"; |
| 204 | u64 prot = val & pg_level[level].mask; |
| 205 | |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 206 | if (!st->level) { |
| 207 | st->level = level; |
| 208 | st->current_prot = prot; |
| 209 | st->start_address = addr; |
| 210 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 211 | } else if (prot != st->current_prot || level != st->level || |
| 212 | addr >= st->marker[1].start_address) { |
| 213 | const char *unit = units; |
| 214 | unsigned long delta; |
| 215 | |
| 216 | if (st->current_prot) { |
Jeremy Linton | 202e41a | 2015-10-07 12:00:23 -0500 | [diff] [blame] | 217 | seq_printf(st->seq, "0x%016lx-0x%016lx ", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 218 | st->start_address, addr); |
| 219 | |
| 220 | delta = (addr - st->start_address) >> 10; |
| 221 | while (!(delta & 1023) && unit[1]) { |
| 222 | delta >>= 10; |
| 223 | unit++; |
| 224 | } |
| 225 | seq_printf(st->seq, "%9lu%c", delta, *unit); |
| 226 | if (pg_level[st->level].bits) |
| 227 | dump_prot(st, pg_level[st->level].bits, |
| 228 | pg_level[st->level].num); |
| 229 | seq_puts(st->seq, "\n"); |
| 230 | } |
| 231 | |
| 232 | if (addr >= st->marker[1].start_address) { |
| 233 | st->marker++; |
| 234 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 235 | } |
| 236 | |
| 237 | st->start_address = addr; |
| 238 | st->current_prot = prot; |
| 239 | st->level = level; |
| 240 | } |
| 241 | |
| 242 | if (addr >= st->marker[1].start_address) { |
| 243 | st->marker++; |
| 244 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) |
| 250 | { |
| 251 | pte_t *pte = pte_offset_kernel(pmd, 0); |
| 252 | unsigned long addr; |
| 253 | unsigned i; |
| 254 | |
| 255 | for (i = 0; i < PTRS_PER_PTE; i++, pte++) { |
| 256 | addr = start + i * PAGE_SIZE; |
| 257 | note_page(st, addr, 4, pte_val(*pte)); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) |
| 262 | { |
| 263 | pmd_t *pmd = pmd_offset(pud, 0); |
| 264 | unsigned long addr; |
| 265 | unsigned i; |
| 266 | |
| 267 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { |
| 268 | addr = start + i * PMD_SIZE; |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 269 | if (pmd_none(*pmd) || pmd_sect(*pmd)) { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 270 | note_page(st, addr, 3, pmd_val(*pmd)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 271 | } else { |
| 272 | BUG_ON(pmd_bad(*pmd)); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 273 | walk_pte(st, pmd, addr); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 274 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) |
| 279 | { |
| 280 | pud_t *pud = pud_offset(pgd, 0); |
| 281 | unsigned long addr; |
| 282 | unsigned i; |
| 283 | |
| 284 | for (i = 0; i < PTRS_PER_PUD; i++, pud++) { |
| 285 | addr = start + i * PUD_SIZE; |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 286 | if (pud_none(*pud) || pud_sect(*pud)) { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 287 | note_page(st, addr, 2, pud_val(*pud)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 288 | } else { |
| 289 | BUG_ON(pud_bad(*pud)); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 290 | walk_pmd(st, pud, addr); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 291 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | static void walk_pgd(struct pg_state *st, struct mm_struct *mm, unsigned long start) |
| 296 | { |
Mark Rutland | 35545f0c | 2014-12-05 12:34:54 +0000 | [diff] [blame] | 297 | pgd_t *pgd = pgd_offset(mm, 0UL); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 298 | unsigned i; |
| 299 | unsigned long addr; |
| 300 | |
| 301 | for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { |
| 302 | addr = start + i * PGDIR_SIZE; |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 303 | if (pgd_none(*pgd)) { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 304 | note_page(st, addr, 1, pgd_val(*pgd)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 305 | } else { |
| 306 | BUG_ON(pgd_bad(*pgd)); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 307 | walk_pud(st, pgd, addr); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 308 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | static int ptdump_show(struct seq_file *m, void *v) |
| 313 | { |
| 314 | struct pg_state st = { |
| 315 | .seq = m, |
| 316 | .marker = address_markers, |
| 317 | }; |
| 318 | |
| 319 | walk_pgd(&st, &init_mm, LOWEST_ADDR); |
| 320 | |
| 321 | note_page(&st, 0, 0, 0); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int ptdump_open(struct inode *inode, struct file *file) |
| 326 | { |
| 327 | return single_open(file, ptdump_show, NULL); |
| 328 | } |
| 329 | |
| 330 | static const struct file_operations ptdump_fops = { |
| 331 | .open = ptdump_open, |
| 332 | .read = seq_read, |
| 333 | .llseek = seq_lseek, |
| 334 | .release = single_release, |
| 335 | }; |
| 336 | |
| 337 | static int ptdump_init(void) |
| 338 | { |
| 339 | struct dentry *pe; |
| 340 | unsigned i, j; |
| 341 | |
| 342 | for (i = 0; i < ARRAY_SIZE(pg_level); i++) |
| 343 | if (pg_level[i].bits) |
| 344 | for (j = 0; j < pg_level[i].num; j++) |
| 345 | pg_level[i].mask |= pg_level[i].bits[j].mask; |
| 346 | |
Jungseung Lee | 326a780 | 2015-05-04 11:33:48 +0100 | [diff] [blame] | 347 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 348 | address_markers[VMEMMAP_START_NR].start_address = |
| 349 | (unsigned long)virt_to_page(PAGE_OFFSET); |
| 350 | address_markers[VMEMMAP_END_NR].start_address = |
| 351 | (unsigned long)virt_to_page(high_memory); |
Jungseung Lee | 326a780 | 2015-05-04 11:33:48 +0100 | [diff] [blame] | 352 | #endif |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 353 | |
| 354 | pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, |
| 355 | &ptdump_fops); |
| 356 | return pe ? 0 : -ENOMEM; |
| 357 | } |
| 358 | device_initcall(ptdump_init); |