blob: 265284dc942df34fe75caa469dfc8c6c0b936c22 [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",
174 }
175};
176
177struct pg_level {
178 const struct prot_bits *bits;
Mark Rutland48dd73c2016-05-31 14:49:02 +0100179 const char *name;
Laura Abbottc9465b42014-11-26 00:28:39 +0000180 size_t num;
181 u64 mask;
182};
183
184static struct pg_level pg_level[] = {
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800185 { /* pgd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100186 .name = "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000187 .bits = pte_bits,
188 .num = ARRAY_SIZE(pte_bits),
Steven Price102f45f2020-02-03 17:36:29 -0800189 }, { /* p4d */
190 .name = "P4D",
191 .bits = pte_bits,
192 .num = ARRAY_SIZE(pte_bits),
Laura Abbottc9465b42014-11-26 00:28:39 +0000193 }, { /* pud */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100194 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000195 .bits = pte_bits,
196 .num = ARRAY_SIZE(pte_bits),
197 }, { /* pmd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100198 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000199 .bits = pte_bits,
200 .num = ARRAY_SIZE(pte_bits),
201 }, { /* pte */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100202 .name = "PTE",
Laura Abbottc9465b42014-11-26 00:28:39 +0000203 .bits = pte_bits,
204 .num = ARRAY_SIZE(pte_bits),
205 },
206};
207
208static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
209 size_t num)
210{
211 unsigned i;
212
213 for (i = 0; i < num; i++, bits++) {
214 const char *s;
215
216 if ((st->current_prot & bits->mask) == bits->val)
217 s = bits->set;
218 else
219 s = bits->clear;
220
221 if (s)
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700222 pt_dump_seq_printf(st->seq, " %s", s);
Laura Abbottc9465b42014-11-26 00:28:39 +0000223 }
224}
225
Laura Abbott1404d6f2016-10-27 09:27:34 -0700226static void note_prot_uxn(struct pg_state *st, unsigned long addr)
227{
228 if (!st->check_wx)
229 return;
230
231 if ((st->current_prot & PTE_UXN) == PTE_UXN)
232 return;
233
234 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
235 (void *)st->start_address, (void *)st->start_address);
236
237 st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
238}
239
240static void note_prot_wx(struct pg_state *st, unsigned long addr)
241{
242 if (!st->check_wx)
243 return;
244 if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
245 return;
246 if ((st->current_prot & PTE_PXN) == PTE_PXN)
247 return;
248
249 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
250 (void *)st->start_address, (void *)st->start_address);
251
252 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
253}
254
Steven Price102f45f2020-02-03 17:36:29 -0800255static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
Steven Price99395ee2020-06-01 21:50:01 -0700256 u64 val)
Laura Abbottc9465b42014-11-26 00:28:39 +0000257{
Steven Price102f45f2020-02-03 17:36:29 -0800258 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
Laura Abbottc9465b42014-11-26 00:28:39 +0000259 static const char units[] = "KMGTPE";
Steven Price102f45f2020-02-03 17:36:29 -0800260 u64 prot = 0;
261
262 if (level >= 0)
263 prot = val & pg_level[level].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000264
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800265 if (st->level == -1) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000266 st->level = level;
267 st->current_prot = prot;
268 st->start_address = addr;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700269 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000270 } else if (prot != st->current_prot || level != st->level ||
271 addr >= st->marker[1].start_address) {
272 const char *unit = units;
273 unsigned long delta;
274
275 if (st->current_prot) {
Laura Abbott1404d6f2016-10-27 09:27:34 -0700276 note_prot_uxn(st, addr);
277 note_prot_wx(st, addr);
Steven Price9c7869c2020-02-03 17:36:34 -0800278 }
279
280 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000281 st->start_address, addr);
282
Steven Price9c7869c2020-02-03 17:36:34 -0800283 delta = (addr - st->start_address) >> 10;
284 while (!(delta & 1023) && unit[1]) {
285 delta >>= 10;
286 unit++;
Laura Abbottc9465b42014-11-26 00:28:39 +0000287 }
Steven Price9c7869c2020-02-03 17:36:34 -0800288 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
289 pg_level[st->level].name);
290 if (st->current_prot && pg_level[st->level].bits)
291 dump_prot(st, pg_level[st->level].bits,
292 pg_level[st->level].num);
293 pt_dump_seq_puts(st->seq, "\n");
Laura Abbottc9465b42014-11-26 00:28:39 +0000294
295 if (addr >= st->marker[1].start_address) {
296 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700297 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000298 }
299
300 st->start_address = addr;
301 st->current_prot = prot;
302 st->level = level;
303 }
304
305 if (addr >= st->marker[1].start_address) {
306 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700307 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000308 }
309
310}
311
Steven Price102f45f2020-02-03 17:36:29 -0800312void ptdump_walk(struct seq_file *s, struct ptdump_info *info)
Laura Abbottc9465b42014-11-26 00:28:39 +0000313{
Steven Price102f45f2020-02-03 17:36:29 -0800314 unsigned long end = ~0UL;
315 struct pg_state st;
Laura Abbottc9465b42014-11-26 00:28:39 +0000316
Steven Price102f45f2020-02-03 17:36:29 -0800317 if (info->base_addr < TASK_SIZE_64)
318 end = TASK_SIZE_64;
Laura Abbottc9465b42014-11-26 00:28:39 +0000319
Steven Price102f45f2020-02-03 17:36:29 -0800320 st = (struct pg_state){
321 .seq = s,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100322 .marker = info->markers,
Steven Price102f45f2020-02-03 17:36:29 -0800323 .ptdump = {
324 .note_page = note_page,
325 .range = (struct ptdump_range[]){
326 {info->base_addr, end},
327 {0, 0}
328 }
329 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000330 };
331
Steven Pricee47690d2020-02-03 17:36:42 -0800332 ptdump_walk_pgd(&st.ptdump, info->mm, NULL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000333}
334
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700335static void ptdump_initialize(void)
Laura Abbottc9465b42014-11-26 00:28:39 +0000336{
Laura Abbottc9465b42014-11-26 00:28:39 +0000337 unsigned i, j;
338
339 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
340 if (pg_level[i].bits)
341 for (j = 0; j < pg_level[i].num; j++)
342 pg_level[i].mask |= pg_level[i].bits[j].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000343}
Mark Rutland4674fdb2016-05-31 14:49:01 +0100344
345static struct ptdump_info kernel_ptdump_info = {
346 .mm = &init_mm,
347 .markers = address_markers,
Steve Capper14c127c2019-08-07 16:55:14 +0100348 .base_addr = PAGE_OFFSET,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100349};
350
Laura Abbott1404d6f2016-10-27 09:27:34 -0700351void ptdump_check_wx(void)
352{
353 struct pg_state st = {
354 .seq = NULL,
355 .marker = (struct addr_marker[]) {
356 { 0, NULL},
357 { -1, NULL},
358 },
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800359 .level = -1,
Laura Abbott1404d6f2016-10-27 09:27:34 -0700360 .check_wx = true,
Steven Price102f45f2020-02-03 17:36:29 -0800361 .ptdump = {
362 .note_page = note_page,
363 .range = (struct ptdump_range[]) {
364 {PAGE_OFFSET, ~0UL},
365 {0, 0}
366 }
367 }
Laura Abbott1404d6f2016-10-27 09:27:34 -0700368 };
369
Steven Pricee47690d2020-02-03 17:36:42 -0800370 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
Steven Price102f45f2020-02-03 17:36:29 -0800371
Laura Abbott1404d6f2016-10-27 09:27:34 -0700372 if (st.wx_pages || st.uxn_pages)
373 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
374 st.wx_pages, st.uxn_pages);
375 else
376 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
377}
378
Mark Rutland4674fdb2016-05-31 14:49:01 +0100379static int ptdump_init(void)
380{
Mark Rutland77ad4ce2019-08-14 14:28:48 +0100381 address_markers[PAGE_END_NR].start_address = PAGE_END;
Steve Capper99426e52019-08-07 16:55:16 +0100382#ifdef CONFIG_KASAN
383 address_markers[KASAN_START_NR].start_address = KASAN_SHADOW_START;
384#endif
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700385 ptdump_initialize();
Greg Kroah-Hartmane2a2e562019-01-22 15:41:11 +0100386 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
387 return 0;
Mark Rutland4674fdb2016-05-31 14:49:01 +0100388}
Laura Abbottc9465b42014-11-26 00:28:39 +0000389device_initcall(ptdump_init);