Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 4 | * Debug helper to dump the current kernel pagetables of the system |
| 5 | * so that we can see what the various memory ranges are set to. |
| 6 | * |
| 7 | * Derived from x86 and arm implementation: |
| 8 | * (C) Copyright 2008 Intel Corporation |
| 9 | * |
| 10 | * Author: Arjan van de Ven <arjan@linux.intel.com> |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/debugfs.h> |
Mark Rutland | 764011c | 2015-01-22 18:20:36 +0000 | [diff] [blame] | 13 | #include <linux/errno.h> |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 14 | #include <linux/fs.h> |
Mark Brown | 284be28 | 2015-01-22 20:52:10 +0000 | [diff] [blame] | 15 | #include <linux/io.h> |
Mark Rutland | 764011c | 2015-01-22 18:20:36 +0000 | [diff] [blame] | 16 | #include <linux/init.h> |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/seq_file.h> |
| 20 | |
| 21 | #include <asm/fixmap.h> |
Ard Biesheuvel | d8fc68a | 2016-04-22 18:48:04 +0200 | [diff] [blame] | 22 | #include <asm/kasan.h> |
Mark Rutland | 764011c | 2015-01-22 18:20:36 +0000 | [diff] [blame] | 23 | #include <asm/memory.h> |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 24 | #include <asm/pgtable.h> |
Mark Rutland | 764011c | 2015-01-22 18:20:36 +0000 | [diff] [blame] | 25 | #include <asm/pgtable-hwdef.h> |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 26 | #include <asm/ptdump.h> |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 27 | |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 28 | static const struct addr_marker address_markers[] = { |
Steve Capper | 14c127c | 2019-08-07 16:55:14 +0100 | [diff] [blame^] | 29 | { PAGE_OFFSET, "Linear Mapping start" }, |
| 30 | { VA_START, "Linear Mapping end" }, |
Ard Biesheuvel | d8fc68a | 2016-04-22 18:48:04 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_KASAN |
| 32 | { KASAN_SHADOW_START, "Kasan shadow start" }, |
| 33 | { KASAN_SHADOW_END, "Kasan shadow end" }, |
| 34 | #endif |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 35 | { MODULES_VADDR, "Modules start" }, |
| 36 | { MODULES_END, "Modules end" }, |
Will Deacon | 4733c7c | 2018-09-05 15:12:27 +0100 | [diff] [blame] | 37 | { VMALLOC_START, "vmalloc() area" }, |
| 38 | { VMALLOC_END, "vmalloc() end" }, |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 39 | { FIXADDR_START, "Fixmap start" }, |
| 40 | { FIXADDR_TOP, "Fixmap end" }, |
| 41 | { PCI_IO_START, "PCI I/O start" }, |
| 42 | { PCI_IO_END, "PCI I/O end" }, |
Ard Biesheuvel | 3e1907d | 2016-03-30 16:46:00 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 44 | { VMEMMAP_START, "vmemmap start" }, |
| 45 | { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" }, |
Ard Biesheuvel | 3e1907d | 2016-03-30 16:46:00 +0200 | [diff] [blame] | 46 | #endif |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 47 | { -1, NULL }, |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 50 | #define pt_dump_seq_printf(m, fmt, args...) \ |
| 51 | ({ \ |
| 52 | if (m) \ |
| 53 | seq_printf(m, fmt, ##args); \ |
| 54 | }) |
| 55 | |
| 56 | #define pt_dump_seq_puts(m, fmt) \ |
| 57 | ({ \ |
| 58 | if (m) \ |
| 59 | seq_printf(m, fmt); \ |
| 60 | }) |
| 61 | |
Jeremy Linton | 202e41a1 | 2015-10-07 12:00:23 -0500 | [diff] [blame] | 62 | /* |
| 63 | * The page dumper groups page table entries of the same type into a single |
| 64 | * description. It uses pg_state to track the range information while |
| 65 | * iterating over the pte entries. When the continuity is broken it then |
| 66 | * dumps out a description of the range. |
| 67 | */ |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 68 | struct pg_state { |
| 69 | struct seq_file *seq; |
| 70 | const struct addr_marker *marker; |
| 71 | unsigned long start_address; |
| 72 | unsigned level; |
| 73 | u64 current_prot; |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 74 | bool check_wx; |
| 75 | unsigned long wx_pages; |
| 76 | unsigned long uxn_pages; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | struct prot_bits { |
| 80 | u64 mask; |
| 81 | u64 val; |
| 82 | const char *set; |
| 83 | const char *clear; |
| 84 | }; |
| 85 | |
| 86 | static const struct prot_bits pte_bits[] = { |
| 87 | { |
Laura Abbott | d7e9d59 | 2016-02-05 16:24:48 -0800 | [diff] [blame] | 88 | .mask = PTE_VALID, |
| 89 | .val = PTE_VALID, |
| 90 | .set = " ", |
| 91 | .clear = "F", |
| 92 | }, { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 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 | 202e41a1 | 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; |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 161 | const char *name; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 162 | size_t num; |
| 163 | u64 mask; |
| 164 | }; |
| 165 | |
| 166 | static struct pg_level pg_level[] = { |
| 167 | { |
| 168 | }, { /* pgd */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 169 | .name = "PGD", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 170 | .bits = pte_bits, |
| 171 | .num = ARRAY_SIZE(pte_bits), |
| 172 | }, { /* pud */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 173 | .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 174 | .bits = pte_bits, |
| 175 | .num = ARRAY_SIZE(pte_bits), |
| 176 | }, { /* pmd */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 177 | .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 178 | .bits = pte_bits, |
| 179 | .num = ARRAY_SIZE(pte_bits), |
| 180 | }, { /* pte */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 181 | .name = "PTE", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 182 | .bits = pte_bits, |
| 183 | .num = ARRAY_SIZE(pte_bits), |
| 184 | }, |
| 185 | }; |
| 186 | |
| 187 | static void dump_prot(struct pg_state *st, const struct prot_bits *bits, |
| 188 | size_t num) |
| 189 | { |
| 190 | unsigned i; |
| 191 | |
| 192 | for (i = 0; i < num; i++, bits++) { |
| 193 | const char *s; |
| 194 | |
| 195 | if ((st->current_prot & bits->mask) == bits->val) |
| 196 | s = bits->set; |
| 197 | else |
| 198 | s = bits->clear; |
| 199 | |
| 200 | if (s) |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 201 | pt_dump_seq_printf(st->seq, " %s", s); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 205 | static void note_prot_uxn(struct pg_state *st, unsigned long addr) |
| 206 | { |
| 207 | if (!st->check_wx) |
| 208 | return; |
| 209 | |
| 210 | if ((st->current_prot & PTE_UXN) == PTE_UXN) |
| 211 | return; |
| 212 | |
| 213 | WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n", |
| 214 | (void *)st->start_address, (void *)st->start_address); |
| 215 | |
| 216 | st->uxn_pages += (addr - st->start_address) / PAGE_SIZE; |
| 217 | } |
| 218 | |
| 219 | static void note_prot_wx(struct pg_state *st, unsigned long addr) |
| 220 | { |
| 221 | if (!st->check_wx) |
| 222 | return; |
| 223 | if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY) |
| 224 | return; |
| 225 | if ((st->current_prot & PTE_PXN) == PTE_PXN) |
| 226 | return; |
| 227 | |
| 228 | WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n", |
| 229 | (void *)st->start_address, (void *)st->start_address); |
| 230 | |
| 231 | st->wx_pages += (addr - st->start_address) / PAGE_SIZE; |
| 232 | } |
| 233 | |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 234 | static void note_page(struct pg_state *st, unsigned long addr, unsigned level, |
| 235 | u64 val) |
| 236 | { |
| 237 | static const char units[] = "KMGTPE"; |
| 238 | u64 prot = val & pg_level[level].mask; |
| 239 | |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 240 | if (!st->level) { |
| 241 | st->level = level; |
| 242 | st->current_prot = prot; |
| 243 | st->start_address = addr; |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 244 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 245 | } else if (prot != st->current_prot || level != st->level || |
| 246 | addr >= st->marker[1].start_address) { |
| 247 | const char *unit = units; |
| 248 | unsigned long delta; |
| 249 | |
| 250 | if (st->current_prot) { |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 251 | note_prot_uxn(st, addr); |
| 252 | note_prot_wx(st, addr); |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 253 | pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 254 | st->start_address, addr); |
| 255 | |
| 256 | delta = (addr - st->start_address) >> 10; |
| 257 | while (!(delta & 1023) && unit[1]) { |
| 258 | delta >>= 10; |
| 259 | unit++; |
| 260 | } |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 261 | pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit, |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 262 | pg_level[st->level].name); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 263 | if (pg_level[st->level].bits) |
| 264 | dump_prot(st, pg_level[st->level].bits, |
| 265 | pg_level[st->level].num); |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 266 | pt_dump_seq_puts(st->seq, "\n"); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (addr >= st->marker[1].start_address) { |
| 270 | st->marker++; |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 271 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | st->start_address = addr; |
| 275 | st->current_prot = prot; |
| 276 | st->level = level; |
| 277 | } |
| 278 | |
| 279 | if (addr >= st->marker[1].start_address) { |
| 280 | st->marker++; |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 281 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | } |
| 285 | |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 286 | static void walk_pte(struct pg_state *st, pmd_t *pmdp, unsigned long start, |
| 287 | unsigned long end) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 288 | { |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 289 | unsigned long addr = start; |
| 290 | pte_t *ptep = pte_offset_kernel(pmdp, start); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 291 | |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 292 | do { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 293 | note_page(st, addr, 4, READ_ONCE(pte_val(*ptep))); |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 294 | } while (ptep++, addr += PAGE_SIZE, addr != end); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 297 | static void walk_pmd(struct pg_state *st, pud_t *pudp, unsigned long start, |
| 298 | unsigned long end) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 299 | { |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 300 | unsigned long next, addr = start; |
| 301 | pmd_t *pmdp = pmd_offset(pudp, start); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 302 | |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 303 | do { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 304 | pmd_t pmd = READ_ONCE(*pmdp); |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 305 | next = pmd_addr_end(addr, end); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 306 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 307 | if (pmd_none(pmd) || pmd_sect(pmd)) { |
| 308 | note_page(st, addr, 3, pmd_val(pmd)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 309 | } else { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 310 | BUG_ON(pmd_bad(pmd)); |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 311 | walk_pte(st, pmdp, addr, next); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 312 | } |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 313 | } while (pmdp++, addr = next, addr != end); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 316 | static void walk_pud(struct pg_state *st, pgd_t *pgdp, unsigned long start, |
| 317 | unsigned long end) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 318 | { |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 319 | unsigned long next, addr = start; |
| 320 | pud_t *pudp = pud_offset(pgdp, start); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 321 | |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 322 | do { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 323 | pud_t pud = READ_ONCE(*pudp); |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 324 | next = pud_addr_end(addr, end); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 325 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 326 | if (pud_none(pud) || pud_sect(pud)) { |
| 327 | note_page(st, addr, 2, pud_val(pud)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 328 | } else { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 329 | BUG_ON(pud_bad(pud)); |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 330 | walk_pmd(st, pudp, addr, next); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 331 | } |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 332 | } while (pudp++, addr = next, addr != end); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 335 | static void walk_pgd(struct pg_state *st, struct mm_struct *mm, |
| 336 | unsigned long start) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 337 | { |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 338 | unsigned long end = (start < TASK_SIZE_64) ? TASK_SIZE_64 : 0; |
| 339 | unsigned long next, addr = start; |
| 340 | pgd_t *pgdp = pgd_offset(mm, start); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 341 | |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 342 | do { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 343 | pgd_t pgd = READ_ONCE(*pgdp); |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 344 | next = pgd_addr_end(addr, end); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 345 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 346 | if (pgd_none(pgd)) { |
| 347 | note_page(st, addr, 1, pgd_val(pgd)); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 348 | } else { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 349 | BUG_ON(pgd_bad(pgd)); |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 350 | walk_pud(st, pgdp, addr, next); |
Mark Rutland | a1c7657 | 2015-01-27 16:36:30 +0000 | [diff] [blame] | 351 | } |
Will Deacon | d23c808 | 2019-02-04 14:37:38 +0000 | [diff] [blame] | 352 | } while (pgdp++, addr = next, addr != end); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Laura Abbott | 4ddb9bf | 2016-10-27 09:27:31 -0700 | [diff] [blame] | 355 | void ptdump_walk_pgd(struct seq_file *m, struct ptdump_info *info) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 356 | { |
| 357 | struct pg_state st = { |
| 358 | .seq = m, |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 359 | .marker = info->markers, |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 360 | }; |
| 361 | |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 362 | walk_pgd(&st, info->mm, info->base_addr); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 363 | |
| 364 | note_page(&st, 0, 0, 0); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Laura Abbott | 4ddb9bf | 2016-10-27 09:27:31 -0700 | [diff] [blame] | 367 | static void ptdump_initialize(void) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 368 | { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 369 | unsigned i, j; |
| 370 | |
| 371 | for (i = 0; i < ARRAY_SIZE(pg_level); i++) |
| 372 | if (pg_level[i].bits) |
| 373 | for (j = 0; j < pg_level[i].num; j++) |
| 374 | pg_level[i].mask |= pg_level[i].bits[j].mask; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 375 | } |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 376 | |
| 377 | static struct ptdump_info kernel_ptdump_info = { |
| 378 | .mm = &init_mm, |
| 379 | .markers = address_markers, |
Steve Capper | 14c127c | 2019-08-07 16:55:14 +0100 | [diff] [blame^] | 380 | .base_addr = PAGE_OFFSET, |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 381 | }; |
| 382 | |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 383 | void ptdump_check_wx(void) |
| 384 | { |
| 385 | struct pg_state st = { |
| 386 | .seq = NULL, |
| 387 | .marker = (struct addr_marker[]) { |
| 388 | { 0, NULL}, |
| 389 | { -1, NULL}, |
| 390 | }, |
| 391 | .check_wx = true, |
| 392 | }; |
| 393 | |
Mark Rutland | 1d08a04 | 2017-12-13 11:45:42 +0000 | [diff] [blame] | 394 | walk_pgd(&st, &init_mm, VA_START); |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 395 | note_page(&st, 0, 0, 0); |
| 396 | if (st.wx_pages || st.uxn_pages) |
| 397 | pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n", |
| 398 | st.wx_pages, st.uxn_pages); |
| 399 | else |
| 400 | pr_info("Checked W+X mappings: passed, no W+X pages found\n"); |
| 401 | } |
| 402 | |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 403 | static int ptdump_init(void) |
| 404 | { |
Laura Abbott | 4ddb9bf | 2016-10-27 09:27:31 -0700 | [diff] [blame] | 405 | ptdump_initialize(); |
Greg Kroah-Hartman | e2a2e56 | 2019-01-22 15:41:11 +0100 | [diff] [blame] | 406 | ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables"); |
| 407 | return 0; |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 408 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 409 | device_initcall(ptdump_init); |