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> |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 18 | #include <linux/ptdump.h> |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 19 | #include <linux/sched.h> |
| 20 | #include <linux/seq_file.h> |
| 21 | |
| 22 | #include <asm/fixmap.h> |
Ard Biesheuvel | d8fc68a | 2016-04-22 18:48:04 +0200 | [diff] [blame] | 23 | #include <asm/kasan.h> |
Mark Rutland | 764011c | 2015-01-22 18:20:36 +0000 | [diff] [blame] | 24 | #include <asm/memory.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 | |
Steve Capper | 99426e5 | 2019-08-07 16:55:16 +0100 | [diff] [blame] | 28 | |
| 29 | enum address_markers_idx { |
| 30 | PAGE_OFFSET_NR = 0, |
Mark Rutland | 77ad4ce | 2019-08-14 14:28:48 +0100 | [diff] [blame] | 31 | PAGE_END_NR, |
Ard Biesheuvel | d8fc68a | 2016-04-22 18:48:04 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_KASAN |
Steve Capper | 99426e5 | 2019-08-07 16:55:16 +0100 | [diff] [blame] | 33 | KASAN_START_NR, |
| 34 | #endif |
| 35 | }; |
| 36 | |
| 37 | static struct addr_marker address_markers[] = { |
| 38 | { PAGE_OFFSET, "Linear Mapping start" }, |
Mark Rutland | 77ad4ce | 2019-08-14 14:28:48 +0100 | [diff] [blame] | 39 | { 0 /* PAGE_END */, "Linear Mapping end" }, |
Steve Capper | 99426e5 | 2019-08-07 16:55:16 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_KASAN |
| 41 | { 0 /* KASAN_SHADOW_START */, "Kasan shadow start" }, |
Ard Biesheuvel | d8fc68a | 2016-04-22 18:48:04 +0200 | [diff] [blame] | 42 | { KASAN_SHADOW_END, "Kasan shadow end" }, |
| 43 | #endif |
Anshuman Khandual | c048ddf | 2020-09-04 14:00:59 +0530 | [diff] [blame] | 44 | { BPF_JIT_REGION_START, "BPF start" }, |
| 45 | { BPF_JIT_REGION_END, "BPF end" }, |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 46 | { MODULES_VADDR, "Modules start" }, |
| 47 | { MODULES_END, "Modules end" }, |
Will Deacon | 4733c7c | 2018-09-05 15:12:27 +0100 | [diff] [blame] | 48 | { VMALLOC_START, "vmalloc() area" }, |
| 49 | { VMALLOC_END, "vmalloc() end" }, |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 50 | { FIXADDR_START, "Fixmap start" }, |
| 51 | { FIXADDR_TOP, "Fixmap end" }, |
| 52 | { PCI_IO_START, "PCI I/O start" }, |
| 53 | { PCI_IO_END, "PCI I/O end" }, |
Ard Biesheuvel | 3e1907d | 2016-03-30 16:46:00 +0200 | [diff] [blame] | 54 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 55 | { VMEMMAP_START, "vmemmap start" }, |
| 56 | { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" }, |
Ard Biesheuvel | 3e1907d | 2016-03-30 16:46:00 +0200 | [diff] [blame] | 57 | #endif |
Ard Biesheuvel | c8f8cca | 2016-04-22 18:48:03 +0200 | [diff] [blame] | 58 | { -1, NULL }, |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 61 | #define pt_dump_seq_printf(m, fmt, args...) \ |
| 62 | ({ \ |
| 63 | if (m) \ |
| 64 | seq_printf(m, fmt, ##args); \ |
| 65 | }) |
| 66 | |
| 67 | #define pt_dump_seq_puts(m, fmt) \ |
| 68 | ({ \ |
| 69 | if (m) \ |
| 70 | seq_printf(m, fmt); \ |
| 71 | }) |
| 72 | |
Jeremy Linton | 202e41a1 | 2015-10-07 12:00:23 -0500 | [diff] [blame] | 73 | /* |
| 74 | * The page dumper groups page table entries of the same type into a single |
| 75 | * description. It uses pg_state to track the range information while |
| 76 | * iterating over the pte entries. When the continuity is broken it then |
| 77 | * dumps out a description of the range. |
| 78 | */ |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 79 | struct pg_state { |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 80 | struct ptdump_state ptdump; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 81 | struct seq_file *seq; |
| 82 | const struct addr_marker *marker; |
| 83 | unsigned long start_address; |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 84 | int level; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 85 | u64 current_prot; |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 86 | bool check_wx; |
| 87 | unsigned long wx_pages; |
| 88 | unsigned long uxn_pages; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct prot_bits { |
| 92 | u64 mask; |
| 93 | u64 val; |
| 94 | const char *set; |
| 95 | const char *clear; |
| 96 | }; |
| 97 | |
| 98 | static const struct prot_bits pte_bits[] = { |
| 99 | { |
Laura Abbott | d7e9d59 | 2016-02-05 16:24:48 -0800 | [diff] [blame] | 100 | .mask = PTE_VALID, |
| 101 | .val = PTE_VALID, |
| 102 | .set = " ", |
| 103 | .clear = "F", |
| 104 | }, { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 105 | .mask = PTE_USER, |
| 106 | .val = PTE_USER, |
| 107 | .set = "USR", |
| 108 | .clear = " ", |
| 109 | }, { |
| 110 | .mask = PTE_RDONLY, |
| 111 | .val = PTE_RDONLY, |
| 112 | .set = "ro", |
| 113 | .clear = "RW", |
| 114 | }, { |
| 115 | .mask = PTE_PXN, |
| 116 | .val = PTE_PXN, |
| 117 | .set = "NX", |
| 118 | .clear = "x ", |
| 119 | }, { |
| 120 | .mask = PTE_SHARED, |
| 121 | .val = PTE_SHARED, |
| 122 | .set = "SHD", |
| 123 | .clear = " ", |
| 124 | }, { |
| 125 | .mask = PTE_AF, |
| 126 | .val = PTE_AF, |
| 127 | .set = "AF", |
| 128 | .clear = " ", |
| 129 | }, { |
| 130 | .mask = PTE_NG, |
| 131 | .val = PTE_NG, |
| 132 | .set = "NG", |
| 133 | .clear = " ", |
| 134 | }, { |
Jeremy Linton | 202e41a1 | 2015-10-07 12:00:23 -0500 | [diff] [blame] | 135 | .mask = PTE_CONT, |
| 136 | .val = PTE_CONT, |
| 137 | .set = "CON", |
| 138 | .clear = " ", |
| 139 | }, { |
| 140 | .mask = PTE_TABLE_BIT, |
| 141 | .val = PTE_TABLE_BIT, |
| 142 | .set = " ", |
| 143 | .clear = "BLK", |
| 144 | }, { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 145 | .mask = PTE_UXN, |
| 146 | .val = PTE_UXN, |
| 147 | .set = "UXN", |
Mark Brown | cba779d | 2019-11-21 13:51:32 +0000 | [diff] [blame] | 148 | .clear = " ", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 149 | }, { |
Mark Brown | de48bb3 | 2020-03-16 16:50:53 +0000 | [diff] [blame] | 150 | .mask = PTE_GP, |
| 151 | .val = PTE_GP, |
| 152 | .set = "GP", |
| 153 | .clear = " ", |
| 154 | }, { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 155 | .mask = PTE_ATTRINDX_MASK, |
| 156 | .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE), |
| 157 | .set = "DEVICE/nGnRnE", |
| 158 | }, { |
| 159 | .mask = PTE_ATTRINDX_MASK, |
| 160 | .val = PTE_ATTRINDX(MT_DEVICE_nGnRE), |
| 161 | .set = "DEVICE/nGnRE", |
| 162 | }, { |
| 163 | .mask = PTE_ATTRINDX_MASK, |
| 164 | .val = PTE_ATTRINDX(MT_DEVICE_GRE), |
| 165 | .set = "DEVICE/GRE", |
| 166 | }, { |
| 167 | .mask = PTE_ATTRINDX_MASK, |
| 168 | .val = PTE_ATTRINDX(MT_NORMAL_NC), |
| 169 | .set = "MEM/NORMAL-NC", |
| 170 | }, { |
| 171 | .mask = PTE_ATTRINDX_MASK, |
| 172 | .val = PTE_ATTRINDX(MT_NORMAL), |
| 173 | .set = "MEM/NORMAL", |
Catalin Marinas | 0178dc7 | 2019-11-27 09:51:13 +0000 | [diff] [blame] | 174 | }, { |
| 175 | .mask = PTE_ATTRINDX_MASK, |
| 176 | .val = PTE_ATTRINDX(MT_NORMAL_TAGGED), |
| 177 | .set = "MEM/NORMAL-TAGGED", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 178 | } |
| 179 | }; |
| 180 | |
| 181 | struct pg_level { |
| 182 | const struct prot_bits *bits; |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 183 | const char *name; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 184 | size_t num; |
| 185 | u64 mask; |
| 186 | }; |
| 187 | |
| 188 | static struct pg_level pg_level[] = { |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 189 | { /* pgd */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 190 | .name = "PGD", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 191 | .bits = pte_bits, |
| 192 | .num = ARRAY_SIZE(pte_bits), |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 193 | }, { /* p4d */ |
| 194 | .name = "P4D", |
| 195 | .bits = pte_bits, |
| 196 | .num = ARRAY_SIZE(pte_bits), |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 197 | }, { /* pud */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 198 | .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 199 | .bits = pte_bits, |
| 200 | .num = ARRAY_SIZE(pte_bits), |
| 201 | }, { /* pmd */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 202 | .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 203 | .bits = pte_bits, |
| 204 | .num = ARRAY_SIZE(pte_bits), |
| 205 | }, { /* pte */ |
Mark Rutland | 48dd73c | 2016-05-31 14:49:02 +0100 | [diff] [blame] | 206 | .name = "PTE", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 207 | .bits = pte_bits, |
| 208 | .num = ARRAY_SIZE(pte_bits), |
| 209 | }, |
| 210 | }; |
| 211 | |
| 212 | static void dump_prot(struct pg_state *st, const struct prot_bits *bits, |
| 213 | size_t num) |
| 214 | { |
| 215 | unsigned i; |
| 216 | |
| 217 | for (i = 0; i < num; i++, bits++) { |
| 218 | const char *s; |
| 219 | |
| 220 | if ((st->current_prot & bits->mask) == bits->val) |
| 221 | s = bits->set; |
| 222 | else |
| 223 | s = bits->clear; |
| 224 | |
| 225 | if (s) |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 226 | pt_dump_seq_printf(st->seq, " %s", s); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 230 | static void note_prot_uxn(struct pg_state *st, unsigned long addr) |
| 231 | { |
| 232 | if (!st->check_wx) |
| 233 | return; |
| 234 | |
| 235 | if ((st->current_prot & PTE_UXN) == PTE_UXN) |
| 236 | return; |
| 237 | |
| 238 | WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n", |
| 239 | (void *)st->start_address, (void *)st->start_address); |
| 240 | |
| 241 | st->uxn_pages += (addr - st->start_address) / PAGE_SIZE; |
| 242 | } |
| 243 | |
| 244 | static void note_prot_wx(struct pg_state *st, unsigned long addr) |
| 245 | { |
| 246 | if (!st->check_wx) |
| 247 | return; |
| 248 | if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY) |
| 249 | return; |
| 250 | if ((st->current_prot & PTE_PXN) == PTE_PXN) |
| 251 | return; |
| 252 | |
| 253 | WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n", |
| 254 | (void *)st->start_address, (void *)st->start_address); |
| 255 | |
| 256 | st->wx_pages += (addr - st->start_address) / PAGE_SIZE; |
| 257 | } |
| 258 | |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 259 | static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, |
Steven Price | 99395ee | 2020-06-01 21:50:01 -0700 | [diff] [blame] | 260 | u64 val) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 261 | { |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 262 | struct pg_state *st = container_of(pt_st, struct pg_state, ptdump); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 263 | static const char units[] = "KMGTPE"; |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 264 | u64 prot = 0; |
| 265 | |
| 266 | if (level >= 0) |
| 267 | prot = val & pg_level[level].mask; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 268 | |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 269 | if (st->level == -1) { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 270 | st->level = level; |
| 271 | st->current_prot = prot; |
| 272 | st->start_address = addr; |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 273 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 274 | } else if (prot != st->current_prot || level != st->level || |
| 275 | addr >= st->marker[1].start_address) { |
| 276 | const char *unit = units; |
| 277 | unsigned long delta; |
| 278 | |
| 279 | if (st->current_prot) { |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 280 | note_prot_uxn(st, addr); |
| 281 | note_prot_wx(st, addr); |
Steven Price | 9c7869c | 2020-02-03 17:36:34 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ", |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 285 | st->start_address, addr); |
| 286 | |
Steven Price | 9c7869c | 2020-02-03 17:36:34 -0800 | [diff] [blame] | 287 | delta = (addr - st->start_address) >> 10; |
| 288 | while (!(delta & 1023) && unit[1]) { |
| 289 | delta >>= 10; |
| 290 | unit++; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 291 | } |
Steven Price | 9c7869c | 2020-02-03 17:36:34 -0800 | [diff] [blame] | 292 | pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit, |
| 293 | pg_level[st->level].name); |
| 294 | if (st->current_prot && pg_level[st->level].bits) |
| 295 | dump_prot(st, pg_level[st->level].bits, |
| 296 | pg_level[st->level].num); |
| 297 | pt_dump_seq_puts(st->seq, "\n"); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 298 | |
| 299 | if (addr >= st->marker[1].start_address) { |
| 300 | st->marker++; |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 301 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | st->start_address = addr; |
| 305 | st->current_prot = prot; |
| 306 | st->level = level; |
| 307 | } |
| 308 | |
| 309 | if (addr >= st->marker[1].start_address) { |
| 310 | st->marker++; |
Laura Abbott | ae5d1cf | 2016-10-27 09:27:32 -0700 | [diff] [blame] | 311 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | } |
| 315 | |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 316 | void ptdump_walk(struct seq_file *s, struct ptdump_info *info) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 317 | { |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 318 | unsigned long end = ~0UL; |
| 319 | struct pg_state st; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 320 | |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 321 | if (info->base_addr < TASK_SIZE_64) |
| 322 | end = TASK_SIZE_64; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 323 | |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 324 | st = (struct pg_state){ |
| 325 | .seq = s, |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 326 | .marker = info->markers, |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 327 | .ptdump = { |
| 328 | .note_page = note_page, |
| 329 | .range = (struct ptdump_range[]){ |
| 330 | {info->base_addr, end}, |
| 331 | {0, 0} |
| 332 | } |
| 333 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 336 | ptdump_walk_pgd(&st.ptdump, info->mm, NULL); |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Laura Abbott | 4ddb9bf | 2016-10-27 09:27:31 -0700 | [diff] [blame] | 339 | static void ptdump_initialize(void) |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 340 | { |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 341 | unsigned i, j; |
| 342 | |
| 343 | for (i = 0; i < ARRAY_SIZE(pg_level); i++) |
| 344 | if (pg_level[i].bits) |
| 345 | for (j = 0; j < pg_level[i].num; j++) |
| 346 | pg_level[i].mask |= pg_level[i].bits[j].mask; |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 347 | } |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 348 | |
| 349 | static struct ptdump_info kernel_ptdump_info = { |
| 350 | .mm = &init_mm, |
| 351 | .markers = address_markers, |
Steve Capper | 14c127c | 2019-08-07 16:55:14 +0100 | [diff] [blame] | 352 | .base_addr = PAGE_OFFSET, |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 353 | }; |
| 354 | |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 355 | void ptdump_check_wx(void) |
| 356 | { |
| 357 | struct pg_state st = { |
| 358 | .seq = NULL, |
| 359 | .marker = (struct addr_marker[]) { |
| 360 | { 0, NULL}, |
| 361 | { -1, NULL}, |
| 362 | }, |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 363 | .level = -1, |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 364 | .check_wx = true, |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 365 | .ptdump = { |
| 366 | .note_page = note_page, |
| 367 | .range = (struct ptdump_range[]) { |
| 368 | {PAGE_OFFSET, ~0UL}, |
| 369 | {0, 0} |
| 370 | } |
| 371 | } |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 372 | }; |
| 373 | |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 374 | ptdump_walk_pgd(&st.ptdump, &init_mm, NULL); |
Steven Price | 102f45f | 2020-02-03 17:36:29 -0800 | [diff] [blame] | 375 | |
Laura Abbott | 1404d6f | 2016-10-27 09:27:34 -0700 | [diff] [blame] | 376 | if (st.wx_pages || st.uxn_pages) |
| 377 | pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n", |
| 378 | st.wx_pages, st.uxn_pages); |
| 379 | else |
| 380 | pr_info("Checked W+X mappings: passed, no W+X pages found\n"); |
| 381 | } |
| 382 | |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 383 | static int ptdump_init(void) |
| 384 | { |
Mark Rutland | 77ad4ce | 2019-08-14 14:28:48 +0100 | [diff] [blame] | 385 | address_markers[PAGE_END_NR].start_address = PAGE_END; |
Steve Capper | 99426e5 | 2019-08-07 16:55:16 +0100 | [diff] [blame] | 386 | #ifdef CONFIG_KASAN |
| 387 | address_markers[KASAN_START_NR].start_address = KASAN_SHADOW_START; |
| 388 | #endif |
Laura Abbott | 4ddb9bf | 2016-10-27 09:27:31 -0700 | [diff] [blame] | 389 | ptdump_initialize(); |
Greg Kroah-Hartman | e2a2e56 | 2019-01-22 15:41:11 +0100 | [diff] [blame] | 390 | ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables"); |
| 391 | return 0; |
Mark Rutland | 4674fdb | 2016-05-31 14:49:01 +0100 | [diff] [blame] | 392 | } |
Laura Abbott | c9465b4 | 2014-11-26 00:28:39 +0000 | [diff] [blame] | 393 | device_initcall(ptdump_init); |