blob: 807dc634bbd2464a993f33524cb495f9df2ba9f2 [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
Anshuman Khandualc048ddf2020-09-04 14:00:59 +053044 { BPF_JIT_REGION_START, "BPF start" },
45 { BPF_JIT_REGION_END, "BPF end" },
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020046 { MODULES_VADDR, "Modules start" },
47 { MODULES_END, "Modules end" },
Will Deacon4733c7c2018-09-05 15:12:27 +010048 { VMALLOC_START, "vmalloc() area" },
49 { VMALLOC_END, "vmalloc() end" },
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020050 { 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 Biesheuvel3e1907d2016-03-30 16:46:00 +020054#ifdef CONFIG_SPARSEMEM_VMEMMAP
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020055 { VMEMMAP_START, "vmemmap start" },
56 { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" },
Ard Biesheuvel3e1907d2016-03-30 16:46:00 +020057#endif
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020058 { -1, NULL },
Laura Abbottc9465b42014-11-26 00:28:39 +000059};
60
Laura Abbottae5d1cf2016-10-27 09:27:32 -070061#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 Linton202e41a12015-10-07 12:00:23 -050073/*
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 Abbottc9465b42014-11-26 00:28:39 +000079struct pg_state {
Steven Price102f45f2020-02-03 17:36:29 -080080 struct ptdump_state ptdump;
Laura Abbottc9465b42014-11-26 00:28:39 +000081 struct seq_file *seq;
82 const struct addr_marker *marker;
83 unsigned long start_address;
Steven Price102f45f2020-02-03 17:36:29 -080084 int level;
Laura Abbottc9465b42014-11-26 00:28:39 +000085 u64 current_prot;
Laura Abbott1404d6f2016-10-27 09:27:34 -070086 bool check_wx;
87 unsigned long wx_pages;
88 unsigned long uxn_pages;
Laura Abbottc9465b42014-11-26 00:28:39 +000089};
90
91struct prot_bits {
92 u64 mask;
93 u64 val;
94 const char *set;
95 const char *clear;
96};
97
98static const struct prot_bits pte_bits[] = {
99 {
Laura Abbottd7e9d592016-02-05 16:24:48 -0800100 .mask = PTE_VALID,
101 .val = PTE_VALID,
102 .set = " ",
103 .clear = "F",
104 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000105 .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 Linton202e41a12015-10-07 12:00:23 -0500135 .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 Abbottc9465b42014-11-26 00:28:39 +0000145 .mask = PTE_UXN,
146 .val = PTE_UXN,
147 .set = "UXN",
Mark Browncba779d2019-11-21 13:51:32 +0000148 .clear = " ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000149 }, {
Mark Brownde48bb32020-03-16 16:50:53 +0000150 .mask = PTE_GP,
151 .val = PTE_GP,
152 .set = "GP",
153 .clear = " ",
154 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000155 .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 Marinas0178dc72019-11-27 09:51:13 +0000174 }, {
175 .mask = PTE_ATTRINDX_MASK,
176 .val = PTE_ATTRINDX(MT_NORMAL_TAGGED),
177 .set = "MEM/NORMAL-TAGGED",
Laura Abbottc9465b42014-11-26 00:28:39 +0000178 }
179};
180
181struct pg_level {
182 const struct prot_bits *bits;
Mark Rutland48dd73c2016-05-31 14:49:02 +0100183 const char *name;
Laura Abbottc9465b42014-11-26 00:28:39 +0000184 size_t num;
185 u64 mask;
186};
187
188static struct pg_level pg_level[] = {
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800189 { /* pgd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100190 .name = "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000191 .bits = pte_bits,
192 .num = ARRAY_SIZE(pte_bits),
Steven Price102f45f2020-02-03 17:36:29 -0800193 }, { /* p4d */
194 .name = "P4D",
195 .bits = pte_bits,
196 .num = ARRAY_SIZE(pte_bits),
Laura Abbottc9465b42014-11-26 00:28:39 +0000197 }, { /* pud */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100198 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000199 .bits = pte_bits,
200 .num = ARRAY_SIZE(pte_bits),
201 }, { /* pmd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100202 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000203 .bits = pte_bits,
204 .num = ARRAY_SIZE(pte_bits),
205 }, { /* pte */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100206 .name = "PTE",
Laura Abbottc9465b42014-11-26 00:28:39 +0000207 .bits = pte_bits,
208 .num = ARRAY_SIZE(pte_bits),
209 },
210};
211
212static 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 Abbottae5d1cf2016-10-27 09:27:32 -0700226 pt_dump_seq_printf(st->seq, " %s", s);
Laura Abbottc9465b42014-11-26 00:28:39 +0000227 }
228}
229
Laura Abbott1404d6f2016-10-27 09:27:34 -0700230static 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
244static 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 Price102f45f2020-02-03 17:36:29 -0800259static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
Steven Price99395ee2020-06-01 21:50:01 -0700260 u64 val)
Laura Abbottc9465b42014-11-26 00:28:39 +0000261{
Steven Price102f45f2020-02-03 17:36:29 -0800262 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
Laura Abbottc9465b42014-11-26 00:28:39 +0000263 static const char units[] = "KMGTPE";
Steven Price102f45f2020-02-03 17:36:29 -0800264 u64 prot = 0;
265
266 if (level >= 0)
267 prot = val & pg_level[level].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000268
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800269 if (st->level == -1) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000270 st->level = level;
271 st->current_prot = prot;
272 st->start_address = addr;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700273 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000274 } 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 Abbott1404d6f2016-10-27 09:27:34 -0700280 note_prot_uxn(st, addr);
281 note_prot_wx(st, addr);
Steven Price9c7869c2020-02-03 17:36:34 -0800282 }
283
284 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000285 st->start_address, addr);
286
Steven Price9c7869c2020-02-03 17:36:34 -0800287 delta = (addr - st->start_address) >> 10;
288 while (!(delta & 1023) && unit[1]) {
289 delta >>= 10;
290 unit++;
Laura Abbottc9465b42014-11-26 00:28:39 +0000291 }
Steven Price9c7869c2020-02-03 17:36:34 -0800292 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 Abbottc9465b42014-11-26 00:28:39 +0000298
299 if (addr >= st->marker[1].start_address) {
300 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700301 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000302 }
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 Abbottae5d1cf2016-10-27 09:27:32 -0700311 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000312 }
313
314}
315
Steven Price102f45f2020-02-03 17:36:29 -0800316void ptdump_walk(struct seq_file *s, struct ptdump_info *info)
Laura Abbottc9465b42014-11-26 00:28:39 +0000317{
Steven Price102f45f2020-02-03 17:36:29 -0800318 unsigned long end = ~0UL;
319 struct pg_state st;
Laura Abbottc9465b42014-11-26 00:28:39 +0000320
Steven Price102f45f2020-02-03 17:36:29 -0800321 if (info->base_addr < TASK_SIZE_64)
322 end = TASK_SIZE_64;
Laura Abbottc9465b42014-11-26 00:28:39 +0000323
Steven Price102f45f2020-02-03 17:36:29 -0800324 st = (struct pg_state){
325 .seq = s,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100326 .marker = info->markers,
Steven Price102f45f2020-02-03 17:36:29 -0800327 .ptdump = {
328 .note_page = note_page,
329 .range = (struct ptdump_range[]){
330 {info->base_addr, end},
331 {0, 0}
332 }
333 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000334 };
335
Steven Pricee47690d2020-02-03 17:36:42 -0800336 ptdump_walk_pgd(&st.ptdump, info->mm, NULL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000337}
338
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700339static void ptdump_initialize(void)
Laura Abbottc9465b42014-11-26 00:28:39 +0000340{
Laura Abbottc9465b42014-11-26 00:28:39 +0000341 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 Abbottc9465b42014-11-26 00:28:39 +0000347}
Mark Rutland4674fdb2016-05-31 14:49:01 +0100348
349static struct ptdump_info kernel_ptdump_info = {
350 .mm = &init_mm,
351 .markers = address_markers,
Steve Capper14c127c2019-08-07 16:55:14 +0100352 .base_addr = PAGE_OFFSET,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100353};
354
Laura Abbott1404d6f2016-10-27 09:27:34 -0700355void 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 Pricef8f0d0b2020-02-03 17:36:38 -0800363 .level = -1,
Laura Abbott1404d6f2016-10-27 09:27:34 -0700364 .check_wx = true,
Steven Price102f45f2020-02-03 17:36:29 -0800365 .ptdump = {
366 .note_page = note_page,
367 .range = (struct ptdump_range[]) {
368 {PAGE_OFFSET, ~0UL},
369 {0, 0}
370 }
371 }
Laura Abbott1404d6f2016-10-27 09:27:34 -0700372 };
373
Steven Pricee47690d2020-02-03 17:36:42 -0800374 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
Steven Price102f45f2020-02-03 17:36:29 -0800375
Laura Abbott1404d6f2016-10-27 09:27:34 -0700376 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 Rutland4674fdb2016-05-31 14:49:01 +0100383static int ptdump_init(void)
384{
Mark Rutland77ad4ce2019-08-14 14:28:48 +0100385 address_markers[PAGE_END_NR].start_address = PAGE_END;
Steve Capper99426e52019-08-07 16:55:16 +0100386#ifdef CONFIG_KASAN
387 address_markers[KASAN_START_NR].start_address = KASAN_SHADOW_START;
388#endif
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700389 ptdump_initialize();
Greg Kroah-Hartmane2a2e562019-01-22 15:41:11 +0100390 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
391 return 0;
Mark Rutland4674fdb2016-05-31 14:49:01 +0100392}
Laura Abbottc9465b42014-11-26 00:28:39 +0000393device_initcall(ptdump_init);