blob: ba6d1d89f9b2ae59622bb555f9e878c5439b0853 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Laura Abbottc9465b42014-11-26 00:28:39 +00002/*
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 Abbottc9465b42014-11-26 00:28:39 +000011 */
12#include <linux/debugfs.h>
Mark Rutland764011c2015-01-22 18:20:36 +000013#include <linux/errno.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000014#include <linux/fs.h>
Mark Brown284be282015-01-22 20:52:10 +000015#include <linux/io.h>
Mark Rutland764011c2015-01-22 18:20:36 +000016#include <linux/init.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000017#include <linux/mm.h>
Steven Price102f45f2020-02-03 17:36:29 -080018#include <linux/ptdump.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000019#include <linux/sched.h>
20#include <linux/seq_file.h>
21
22#include <asm/fixmap.h>
Ard Biesheuveld8fc68a2016-04-22 18:48:04 +020023#include <asm/kasan.h>
Mark Rutland764011c2015-01-22 18:20:36 +000024#include <asm/memory.h>
Mark Rutland764011c2015-01-22 18:20:36 +000025#include <asm/pgtable-hwdef.h>
Mark Rutland4674fdb2016-05-31 14:49:01 +010026#include <asm/ptdump.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000027
Steve Capper99426e52019-08-07 16:55:16 +010028
29enum address_markers_idx {
30 PAGE_OFFSET_NR = 0,
Mark Rutland77ad4ce2019-08-14 14:28:48 +010031 PAGE_END_NR,
Ard Biesheuveld8fc68a2016-04-22 18:48:04 +020032#ifdef CONFIG_KASAN
Steve Capper99426e52019-08-07 16:55:16 +010033 KASAN_START_NR,
34#endif
35};
36
37static struct addr_marker address_markers[] = {
38 { PAGE_OFFSET, "Linear Mapping start" },
Mark Rutland77ad4ce2019-08-14 14:28:48 +010039 { 0 /* PAGE_END */, "Linear Mapping end" },
Steve Capper99426e52019-08-07 16:55:16 +010040#ifdef CONFIG_KASAN
41 { 0 /* KASAN_SHADOW_START */, "Kasan shadow start" },
Ard Biesheuveld8fc68a2016-04-22 18:48:04 +020042 { KASAN_SHADOW_END, "Kasan shadow end" },
43#endif
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020044 { MODULES_VADDR, "Modules start" },
45 { MODULES_END, "Modules end" },
Will Deacon4733c7c2018-09-05 15:12:27 +010046 { VMALLOC_START, "vmalloc() area" },
47 { VMALLOC_END, "vmalloc() end" },
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020048 { FIXADDR_START, "Fixmap start" },
49 { FIXADDR_TOP, "Fixmap end" },
50 { PCI_IO_START, "PCI I/O start" },
51 { PCI_IO_END, "PCI I/O end" },
Ard Biesheuvel3e1907d2016-03-30 16:46:00 +020052#ifdef CONFIG_SPARSEMEM_VMEMMAP
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020053 { VMEMMAP_START, "vmemmap start" },
54 { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" },
Ard Biesheuvel3e1907d2016-03-30 16:46:00 +020055#endif
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020056 { -1, NULL },
Laura Abbottc9465b42014-11-26 00:28:39 +000057};
58
Laura Abbottae5d1cf2016-10-27 09:27:32 -070059#define pt_dump_seq_printf(m, fmt, args...) \
60({ \
61 if (m) \
62 seq_printf(m, fmt, ##args); \
63})
64
65#define pt_dump_seq_puts(m, fmt) \
66({ \
67 if (m) \
68 seq_printf(m, fmt); \
69})
70
Jeremy Linton202e41a12015-10-07 12:00:23 -050071/*
72 * The page dumper groups page table entries of the same type into a single
73 * description. It uses pg_state to track the range information while
74 * iterating over the pte entries. When the continuity is broken it then
75 * dumps out a description of the range.
76 */
Laura Abbottc9465b42014-11-26 00:28:39 +000077struct pg_state {
Steven Price102f45f2020-02-03 17:36:29 -080078 struct ptdump_state ptdump;
Laura Abbottc9465b42014-11-26 00:28:39 +000079 struct seq_file *seq;
80 const struct addr_marker *marker;
81 unsigned long start_address;
Steven Price102f45f2020-02-03 17:36:29 -080082 int level;
Laura Abbottc9465b42014-11-26 00:28:39 +000083 u64 current_prot;
Laura Abbott1404d6f2016-10-27 09:27:34 -070084 bool check_wx;
85 unsigned long wx_pages;
86 unsigned long uxn_pages;
Laura Abbottc9465b42014-11-26 00:28:39 +000087};
88
89struct prot_bits {
90 u64 mask;
91 u64 val;
92 const char *set;
93 const char *clear;
94};
95
96static const struct prot_bits pte_bits[] = {
97 {
Laura Abbottd7e9d592016-02-05 16:24:48 -080098 .mask = PTE_VALID,
99 .val = PTE_VALID,
100 .set = " ",
101 .clear = "F",
102 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000103 .mask = PTE_USER,
104 .val = PTE_USER,
105 .set = "USR",
106 .clear = " ",
107 }, {
108 .mask = PTE_RDONLY,
109 .val = PTE_RDONLY,
110 .set = "ro",
111 .clear = "RW",
112 }, {
113 .mask = PTE_PXN,
114 .val = PTE_PXN,
115 .set = "NX",
116 .clear = "x ",
117 }, {
118 .mask = PTE_SHARED,
119 .val = PTE_SHARED,
120 .set = "SHD",
121 .clear = " ",
122 }, {
123 .mask = PTE_AF,
124 .val = PTE_AF,
125 .set = "AF",
126 .clear = " ",
127 }, {
128 .mask = PTE_NG,
129 .val = PTE_NG,
130 .set = "NG",
131 .clear = " ",
132 }, {
Jeremy Linton202e41a12015-10-07 12:00:23 -0500133 .mask = PTE_CONT,
134 .val = PTE_CONT,
135 .set = "CON",
136 .clear = " ",
137 }, {
138 .mask = PTE_TABLE_BIT,
139 .val = PTE_TABLE_BIT,
140 .set = " ",
141 .clear = "BLK",
142 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000143 .mask = PTE_UXN,
144 .val = PTE_UXN,
145 .set = "UXN",
Mark Browncba779d2019-11-21 13:51:32 +0000146 .clear = " ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000147 }, {
Mark Brownde48bb32020-03-16 16:50:53 +0000148 .mask = PTE_GP,
149 .val = PTE_GP,
150 .set = "GP",
151 .clear = " ",
152 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000153 .mask = PTE_ATTRINDX_MASK,
154 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
155 .set = "DEVICE/nGnRnE",
156 }, {
157 .mask = PTE_ATTRINDX_MASK,
158 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
159 .set = "DEVICE/nGnRE",
160 }, {
161 .mask = PTE_ATTRINDX_MASK,
162 .val = PTE_ATTRINDX(MT_DEVICE_GRE),
163 .set = "DEVICE/GRE",
164 }, {
165 .mask = PTE_ATTRINDX_MASK,
166 .val = PTE_ATTRINDX(MT_NORMAL_NC),
167 .set = "MEM/NORMAL-NC",
168 }, {
169 .mask = PTE_ATTRINDX_MASK,
170 .val = PTE_ATTRINDX(MT_NORMAL),
171 .set = "MEM/NORMAL",
Catalin Marinas0178dc72019-11-27 09:51:13 +0000172 }, {
173 .mask = PTE_ATTRINDX_MASK,
174 .val = PTE_ATTRINDX(MT_NORMAL_TAGGED),
175 .set = "MEM/NORMAL-TAGGED",
Laura Abbottc9465b42014-11-26 00:28:39 +0000176 }
177};
178
179struct pg_level {
180 const struct prot_bits *bits;
Mark Rutland48dd73c2016-05-31 14:49:02 +0100181 const char *name;
Laura Abbottc9465b42014-11-26 00:28:39 +0000182 size_t num;
183 u64 mask;
184};
185
186static struct pg_level pg_level[] = {
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800187 { /* pgd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100188 .name = "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000189 .bits = pte_bits,
190 .num = ARRAY_SIZE(pte_bits),
Steven Price102f45f2020-02-03 17:36:29 -0800191 }, { /* p4d */
192 .name = "P4D",
193 .bits = pte_bits,
194 .num = ARRAY_SIZE(pte_bits),
Laura Abbottc9465b42014-11-26 00:28:39 +0000195 }, { /* pud */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100196 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000197 .bits = pte_bits,
198 .num = ARRAY_SIZE(pte_bits),
199 }, { /* pmd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100200 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000201 .bits = pte_bits,
202 .num = ARRAY_SIZE(pte_bits),
203 }, { /* pte */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100204 .name = "PTE",
Laura Abbottc9465b42014-11-26 00:28:39 +0000205 .bits = pte_bits,
206 .num = ARRAY_SIZE(pte_bits),
207 },
208};
209
210static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
211 size_t num)
212{
213 unsigned i;
214
215 for (i = 0; i < num; i++, bits++) {
216 const char *s;
217
218 if ((st->current_prot & bits->mask) == bits->val)
219 s = bits->set;
220 else
221 s = bits->clear;
222
223 if (s)
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700224 pt_dump_seq_printf(st->seq, " %s", s);
Laura Abbottc9465b42014-11-26 00:28:39 +0000225 }
226}
227
Laura Abbott1404d6f2016-10-27 09:27:34 -0700228static void note_prot_uxn(struct pg_state *st, unsigned long addr)
229{
230 if (!st->check_wx)
231 return;
232
233 if ((st->current_prot & PTE_UXN) == PTE_UXN)
234 return;
235
236 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
237 (void *)st->start_address, (void *)st->start_address);
238
239 st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
240}
241
242static void note_prot_wx(struct pg_state *st, unsigned long addr)
243{
244 if (!st->check_wx)
245 return;
246 if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
247 return;
248 if ((st->current_prot & PTE_PXN) == PTE_PXN)
249 return;
250
251 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
252 (void *)st->start_address, (void *)st->start_address);
253
254 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
255}
256
Steven Price102f45f2020-02-03 17:36:29 -0800257static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
Steven Price99395ee2020-06-01 21:50:01 -0700258 u64 val)
Laura Abbottc9465b42014-11-26 00:28:39 +0000259{
Steven Price102f45f2020-02-03 17:36:29 -0800260 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
Laura Abbottc9465b42014-11-26 00:28:39 +0000261 static const char units[] = "KMGTPE";
Steven Price102f45f2020-02-03 17:36:29 -0800262 u64 prot = 0;
263
264 if (level >= 0)
265 prot = val & pg_level[level].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000266
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800267 if (st->level == -1) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000268 st->level = level;
269 st->current_prot = prot;
270 st->start_address = addr;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700271 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000272 } else if (prot != st->current_prot || level != st->level ||
273 addr >= st->marker[1].start_address) {
274 const char *unit = units;
275 unsigned long delta;
276
277 if (st->current_prot) {
Laura Abbott1404d6f2016-10-27 09:27:34 -0700278 note_prot_uxn(st, addr);
279 note_prot_wx(st, addr);
Steven Price9c7869c2020-02-03 17:36:34 -0800280 }
281
282 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000283 st->start_address, addr);
284
Steven Price9c7869c2020-02-03 17:36:34 -0800285 delta = (addr - st->start_address) >> 10;
286 while (!(delta & 1023) && unit[1]) {
287 delta >>= 10;
288 unit++;
Laura Abbottc9465b42014-11-26 00:28:39 +0000289 }
Steven Price9c7869c2020-02-03 17:36:34 -0800290 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
291 pg_level[st->level].name);
292 if (st->current_prot && pg_level[st->level].bits)
293 dump_prot(st, pg_level[st->level].bits,
294 pg_level[st->level].num);
295 pt_dump_seq_puts(st->seq, "\n");
Laura Abbottc9465b42014-11-26 00:28:39 +0000296
297 if (addr >= st->marker[1].start_address) {
298 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700299 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000300 }
301
302 st->start_address = addr;
303 st->current_prot = prot;
304 st->level = level;
305 }
306
307 if (addr >= st->marker[1].start_address) {
308 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700309 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000310 }
311
312}
313
Steven Price102f45f2020-02-03 17:36:29 -0800314void ptdump_walk(struct seq_file *s, struct ptdump_info *info)
Laura Abbottc9465b42014-11-26 00:28:39 +0000315{
Steven Price102f45f2020-02-03 17:36:29 -0800316 unsigned long end = ~0UL;
317 struct pg_state st;
Laura Abbottc9465b42014-11-26 00:28:39 +0000318
Steven Price102f45f2020-02-03 17:36:29 -0800319 if (info->base_addr < TASK_SIZE_64)
320 end = TASK_SIZE_64;
Laura Abbottc9465b42014-11-26 00:28:39 +0000321
Steven Price102f45f2020-02-03 17:36:29 -0800322 st = (struct pg_state){
323 .seq = s,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100324 .marker = info->markers,
Steven Price102f45f2020-02-03 17:36:29 -0800325 .ptdump = {
326 .note_page = note_page,
327 .range = (struct ptdump_range[]){
328 {info->base_addr, end},
329 {0, 0}
330 }
331 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000332 };
333
Steven Pricee47690d2020-02-03 17:36:42 -0800334 ptdump_walk_pgd(&st.ptdump, info->mm, NULL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000335}
336
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700337static void ptdump_initialize(void)
Laura Abbottc9465b42014-11-26 00:28:39 +0000338{
Laura Abbottc9465b42014-11-26 00:28:39 +0000339 unsigned i, j;
340
341 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
342 if (pg_level[i].bits)
343 for (j = 0; j < pg_level[i].num; j++)
344 pg_level[i].mask |= pg_level[i].bits[j].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000345}
Mark Rutland4674fdb2016-05-31 14:49:01 +0100346
347static struct ptdump_info kernel_ptdump_info = {
348 .mm = &init_mm,
349 .markers = address_markers,
Steve Capper14c127c2019-08-07 16:55:14 +0100350 .base_addr = PAGE_OFFSET,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100351};
352
Laura Abbott1404d6f2016-10-27 09:27:34 -0700353void ptdump_check_wx(void)
354{
355 struct pg_state st = {
356 .seq = NULL,
357 .marker = (struct addr_marker[]) {
358 { 0, NULL},
359 { -1, NULL},
360 },
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800361 .level = -1,
Laura Abbott1404d6f2016-10-27 09:27:34 -0700362 .check_wx = true,
Steven Price102f45f2020-02-03 17:36:29 -0800363 .ptdump = {
364 .note_page = note_page,
365 .range = (struct ptdump_range[]) {
366 {PAGE_OFFSET, ~0UL},
367 {0, 0}
368 }
369 }
Laura Abbott1404d6f2016-10-27 09:27:34 -0700370 };
371
Steven Pricee47690d2020-02-03 17:36:42 -0800372 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
Steven Price102f45f2020-02-03 17:36:29 -0800373
Laura Abbott1404d6f2016-10-27 09:27:34 -0700374 if (st.wx_pages || st.uxn_pages)
375 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
376 st.wx_pages, st.uxn_pages);
377 else
378 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
379}
380
Mark Rutland4674fdb2016-05-31 14:49:01 +0100381static int ptdump_init(void)
382{
Mark Rutland77ad4ce2019-08-14 14:28:48 +0100383 address_markers[PAGE_END_NR].start_address = PAGE_END;
Steve Capper99426e52019-08-07 16:55:16 +0100384#ifdef CONFIG_KASAN
385 address_markers[KASAN_START_NR].start_address = KASAN_SHADOW_START;
386#endif
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700387 ptdump_initialize();
Greg Kroah-Hartmane2a2e562019-01-22 15:41:11 +0100388 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
389 return 0;
Mark Rutland4674fdb2016-05-31 14:49:01 +0100390}
Laura Abbottc9465b42014-11-26 00:28:39 +0000391device_initcall(ptdump_init);