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