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 | c8f8cca | 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 | c8f8cca | 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 | c8f8cca | 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 | c8f8cca | 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; |
| 153 | size_t num; |
| 154 | u64 mask; |
| 155 | }; |
| 156 | |
| 157 | static struct pg_level pg_level[] = { |
| 158 | { |
| 159 | }, { /* pgd */ |
| 160 | .bits = pte_bits, |
| 161 | .num = ARRAY_SIZE(pte_bits), |
| 162 | }, { /* pud */ |
| 163 | .bits = pte_bits, |
| 164 | .num = ARRAY_SIZE(pte_bits), |
| 165 | }, { /* pmd */ |
| 166 | .bits = pte_bits, |
| 167 | .num = ARRAY_SIZE(pte_bits), |
| 168 | }, { /* pte */ |
| 169 | .bits = pte_bits, |
| 170 | .num = ARRAY_SIZE(pte_bits), |
| 171 | }, |
| 172 | }; |
| 173 | |
| 174 | static void dump_prot(struct pg_state *st, const struct prot_bits *bits, |
| 175 | size_t num) |
| 176 | { |
| 177 | unsigned i; |
| 178 | |
| 179 | for (i = 0; i < num; i++, bits++) { |
| 180 | const char *s; |
| 181 | |
| 182 | if ((st->current_prot & bits->mask) == bits->val) |
| 183 | s = bits->set; |
| 184 | else |
| 185 | s = bits->clear; |
| 186 | |
| 187 | if (s) |
| 188 | seq_printf(st->seq, " %s", s); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void note_page(struct pg_state *st, unsigned long addr, unsigned level, |
| 193 | u64 val) |
| 194 | { |
| 195 | static const char units[] = "KMGTPE"; |
| 196 | u64 prot = val & pg_level[level].mask; |
| 197 | |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 198 | if (!st->level) { |
| 199 | st->level = level; |
| 200 | st->current_prot = prot; |
| 201 | st->start_address = addr; |
| 202 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 203 | } else if (prot != st->current_prot || level != st->level || |
| 204 | addr >= st->marker[1].start_address) { |
| 205 | const char *unit = units; |
| 206 | unsigned long delta; |
| 207 | |
| 208 | if (st->current_prot) { |
Jeremy Linton | 202e41a1 | 2015-10-07 12:00:23 -0500 | [diff] [blame] | 209 | seq_printf(st->seq, "0x%016lx-0x%016lx ", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 210 | st->start_address, addr); |
| 211 | |
| 212 | delta = (addr - st->start_address) >> 10; |
| 213 | while (!(delta & 1023) && unit[1]) { |
| 214 | delta >>= 10; |
| 215 | unit++; |
| 216 | } |
| 217 | seq_printf(st->seq, "%9lu%c", delta, *unit); |
| 218 | if (pg_level[st->level].bits) |
| 219 | dump_prot(st, pg_level[st->level].bits, |
| 220 | pg_level[st->level].num); |
| 221 | seq_puts(st->seq, "\n"); |
| 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 | st->start_address = addr; |
| 230 | st->current_prot = prot; |
| 231 | st->level = level; |
| 232 | } |
| 233 | |
| 234 | if (addr >= st->marker[1].start_address) { |
| 235 | st->marker++; |
| 236 | seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) |
| 242 | { |
| 243 | pte_t *pte = pte_offset_kernel(pmd, 0); |
| 244 | unsigned long addr; |
| 245 | unsigned i; |
| 246 | |
| 247 | for (i = 0; i < PTRS_PER_PTE; i++, pte++) { |
| 248 | addr = start + i * PAGE_SIZE; |
| 249 | note_page(st, addr, 4, pte_val(*pte)); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) |
| 254 | { |
| 255 | pmd_t *pmd = pmd_offset(pud, 0); |
| 256 | unsigned long addr; |
| 257 | unsigned i; |
| 258 | |
| 259 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { |
| 260 | addr = start + i * PMD_SIZE; |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 261 | if (pmd_none(*pmd) || pmd_sect(*pmd)) { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 262 | note_page(st, addr, 3, pmd_val(*pmd)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 263 | } else { |
| 264 | BUG_ON(pmd_bad(*pmd)); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 265 | walk_pte(st, pmd, addr); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 266 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 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; |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 278 | if (pud_none(*pud) || pud_sect(*pud)) { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 279 | note_page(st, addr, 2, pud_val(*pud)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 280 | } else { |
| 281 | BUG_ON(pud_bad(*pud)); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 282 | walk_pmd(st, pud, addr); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 283 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | static void walk_pgd(struct pg_state *st, struct mm_struct *mm, unsigned long start) |
| 288 | { |
Mark Rutland | 35545f0c | 2014-12-05 12:34:54 +0000 | [diff] [blame] | 289 | pgd_t *pgd = pgd_offset(mm, 0UL); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 290 | unsigned i; |
| 291 | unsigned long addr; |
| 292 | |
| 293 | for (i = 0; i < PTRS_PER_PGD; i++, pgd++) { |
| 294 | addr = start + i * PGDIR_SIZE; |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 295 | if (pgd_none(*pgd)) { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 296 | note_page(st, addr, 1, pgd_val(*pgd)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 297 | } else { |
| 298 | BUG_ON(pgd_bad(*pgd)); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 299 | walk_pud(st, pgd, addr); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 300 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | static int ptdump_show(struct seq_file *m, void *v) |
| 305 | { |
| 306 | struct pg_state st = { |
| 307 | .seq = m, |
| 308 | .marker = address_markers, |
| 309 | }; |
| 310 | |
Kefeng Wang | cc30e6b | 2016-01-30 15:55:21 +0800 | [diff] [blame] | 311 | walk_pgd(&st, &init_mm, VA_START); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 312 | |
| 313 | note_page(&st, 0, 0, 0); |
| 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 | |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 339 | pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, |
| 340 | &ptdump_fops); |
| 341 | return pe ? 0 : -ENOMEM; |
| 342 | } |
| 343 | device_initcall(ptdump_init); |