blob: 9bc4066c5bf33a72401905790277fc5aa78900bc [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,
Andrey Konovalov0fea6e92020-12-22 12:02:06 -080032#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
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" },
Andrey Konovalov0fea6e92020-12-22 12:02:06 -080040#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
Steve Capper99426e52019-08-07 16:55:16 +010041 { 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 Biesheuvelc8f8cca2016-04-22 18:48:03 +020052 { VMEMMAP_START, "vmemmap start" },
53 { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" },
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020054 { -1, NULL },
Laura Abbottc9465b42014-11-26 00:28:39 +000055};
56
Laura Abbottae5d1cf2016-10-27 09:27:32 -070057#define pt_dump_seq_printf(m, fmt, args...) \
58({ \
59 if (m) \
60 seq_printf(m, fmt, ##args); \
61})
62
63#define pt_dump_seq_puts(m, fmt) \
64({ \
65 if (m) \
66 seq_printf(m, fmt); \
67})
68
Jeremy Linton202e41a12015-10-07 12:00:23 -050069/*
70 * The page dumper groups page table entries of the same type into a single
71 * description. It uses pg_state to track the range information while
72 * iterating over the pte entries. When the continuity is broken it then
73 * dumps out a description of the range.
74 */
Laura Abbottc9465b42014-11-26 00:28:39 +000075struct pg_state {
Steven Price102f45f2020-02-03 17:36:29 -080076 struct ptdump_state ptdump;
Laura Abbottc9465b42014-11-26 00:28:39 +000077 struct seq_file *seq;
78 const struct addr_marker *marker;
79 unsigned long start_address;
Steven Price102f45f2020-02-03 17:36:29 -080080 int level;
Laura Abbottc9465b42014-11-26 00:28:39 +000081 u64 current_prot;
Laura Abbott1404d6f2016-10-27 09:27:34 -070082 bool check_wx;
83 unsigned long wx_pages;
84 unsigned long uxn_pages;
Laura Abbottc9465b42014-11-26 00:28:39 +000085};
86
87struct prot_bits {
88 u64 mask;
89 u64 val;
90 const char *set;
91 const char *clear;
92};
93
94static const struct prot_bits pte_bits[] = {
95 {
Laura Abbottd7e9d592016-02-05 16:24:48 -080096 .mask = PTE_VALID,
97 .val = PTE_VALID,
98 .set = " ",
99 .clear = "F",
100 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000101 .mask = PTE_USER,
102 .val = PTE_USER,
103 .set = "USR",
104 .clear = " ",
105 }, {
106 .mask = PTE_RDONLY,
107 .val = PTE_RDONLY,
108 .set = "ro",
109 .clear = "RW",
110 }, {
111 .mask = PTE_PXN,
112 .val = PTE_PXN,
113 .set = "NX",
114 .clear = "x ",
115 }, {
116 .mask = PTE_SHARED,
117 .val = PTE_SHARED,
118 .set = "SHD",
119 .clear = " ",
120 }, {
121 .mask = PTE_AF,
122 .val = PTE_AF,
123 .set = "AF",
124 .clear = " ",
125 }, {
126 .mask = PTE_NG,
127 .val = PTE_NG,
128 .set = "NG",
129 .clear = " ",
130 }, {
Jeremy Linton202e41a12015-10-07 12:00:23 -0500131 .mask = PTE_CONT,
132 .val = PTE_CONT,
133 .set = "CON",
134 .clear = " ",
135 }, {
136 .mask = PTE_TABLE_BIT,
137 .val = PTE_TABLE_BIT,
138 .set = " ",
139 .clear = "BLK",
140 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000141 .mask = PTE_UXN,
142 .val = PTE_UXN,
143 .set = "UXN",
Mark Browncba779d2019-11-21 13:51:32 +0000144 .clear = " ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000145 }, {
Mark Brownde48bb32020-03-16 16:50:53 +0000146 .mask = PTE_GP,
147 .val = PTE_GP,
148 .set = "GP",
149 .clear = " ",
150 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000151 .mask = PTE_ATTRINDX_MASK,
152 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
153 .set = "DEVICE/nGnRnE",
154 }, {
155 .mask = PTE_ATTRINDX_MASK,
156 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
157 .set = "DEVICE/nGnRE",
158 }, {
159 .mask = PTE_ATTRINDX_MASK,
Laura Abbottc9465b42014-11-26 00:28:39 +0000160 .val = PTE_ATTRINDX(MT_NORMAL_NC),
161 .set = "MEM/NORMAL-NC",
162 }, {
163 .mask = PTE_ATTRINDX_MASK,
164 .val = PTE_ATTRINDX(MT_NORMAL),
165 .set = "MEM/NORMAL",
Catalin Marinas0178dc72019-11-27 09:51:13 +0000166 }, {
167 .mask = PTE_ATTRINDX_MASK,
168 .val = PTE_ATTRINDX(MT_NORMAL_TAGGED),
169 .set = "MEM/NORMAL-TAGGED",
Laura Abbottc9465b42014-11-26 00:28:39 +0000170 }
171};
172
173struct pg_level {
174 const struct prot_bits *bits;
Mark Rutland48dd73c2016-05-31 14:49:02 +0100175 const char *name;
Laura Abbottc9465b42014-11-26 00:28:39 +0000176 size_t num;
177 u64 mask;
178};
179
180static struct pg_level pg_level[] = {
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800181 { /* pgd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100182 .name = "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000183 .bits = pte_bits,
184 .num = ARRAY_SIZE(pte_bits),
Steven Price102f45f2020-02-03 17:36:29 -0800185 }, { /* p4d */
186 .name = "P4D",
187 .bits = pte_bits,
188 .num = ARRAY_SIZE(pte_bits),
Laura Abbottc9465b42014-11-26 00:28:39 +0000189 }, { /* pud */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100190 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000191 .bits = pte_bits,
192 .num = ARRAY_SIZE(pte_bits),
193 }, { /* pmd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100194 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000195 .bits = pte_bits,
196 .num = ARRAY_SIZE(pte_bits),
197 }, { /* pte */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100198 .name = "PTE",
Laura Abbottc9465b42014-11-26 00:28:39 +0000199 .bits = pte_bits,
200 .num = ARRAY_SIZE(pte_bits),
201 },
202};
203
204static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
205 size_t num)
206{
207 unsigned i;
208
209 for (i = 0; i < num; i++, bits++) {
210 const char *s;
211
212 if ((st->current_prot & bits->mask) == bits->val)
213 s = bits->set;
214 else
215 s = bits->clear;
216
217 if (s)
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700218 pt_dump_seq_printf(st->seq, " %s", s);
Laura Abbottc9465b42014-11-26 00:28:39 +0000219 }
220}
221
Laura Abbott1404d6f2016-10-27 09:27:34 -0700222static void note_prot_uxn(struct pg_state *st, unsigned long addr)
223{
224 if (!st->check_wx)
225 return;
226
227 if ((st->current_prot & PTE_UXN) == PTE_UXN)
228 return;
229
230 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
231 (void *)st->start_address, (void *)st->start_address);
232
233 st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
234}
235
236static void note_prot_wx(struct pg_state *st, unsigned long addr)
237{
238 if (!st->check_wx)
239 return;
240 if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
241 return;
242 if ((st->current_prot & PTE_PXN) == PTE_PXN)
243 return;
244
245 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
246 (void *)st->start_address, (void *)st->start_address);
247
248 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
249}
250
Steven Price102f45f2020-02-03 17:36:29 -0800251static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
Steven Price99395ee2020-06-01 21:50:01 -0700252 u64 val)
Laura Abbottc9465b42014-11-26 00:28:39 +0000253{
Steven Price102f45f2020-02-03 17:36:29 -0800254 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
Laura Abbottc9465b42014-11-26 00:28:39 +0000255 static const char units[] = "KMGTPE";
Steven Price102f45f2020-02-03 17:36:29 -0800256 u64 prot = 0;
257
258 if (level >= 0)
259 prot = val & pg_level[level].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000260
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800261 if (st->level == -1) {
Laura Abbottc9465b42014-11-26 00:28:39 +0000262 st->level = level;
263 st->current_prot = prot;
264 st->start_address = addr;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700265 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000266 } else if (prot != st->current_prot || level != st->level ||
267 addr >= st->marker[1].start_address) {
268 const char *unit = units;
269 unsigned long delta;
270
271 if (st->current_prot) {
Laura Abbott1404d6f2016-10-27 09:27:34 -0700272 note_prot_uxn(st, addr);
273 note_prot_wx(st, addr);
Steven Price9c7869c2020-02-03 17:36:34 -0800274 }
275
276 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000277 st->start_address, addr);
278
Steven Price9c7869c2020-02-03 17:36:34 -0800279 delta = (addr - st->start_address) >> 10;
280 while (!(delta & 1023) && unit[1]) {
281 delta >>= 10;
282 unit++;
Laura Abbottc9465b42014-11-26 00:28:39 +0000283 }
Steven Price9c7869c2020-02-03 17:36:34 -0800284 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
285 pg_level[st->level].name);
286 if (st->current_prot && pg_level[st->level].bits)
287 dump_prot(st, pg_level[st->level].bits,
288 pg_level[st->level].num);
289 pt_dump_seq_puts(st->seq, "\n");
Laura Abbottc9465b42014-11-26 00:28:39 +0000290
291 if (addr >= st->marker[1].start_address) {
292 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700293 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000294 }
295
296 st->start_address = addr;
297 st->current_prot = prot;
298 st->level = level;
299 }
300
301 if (addr >= st->marker[1].start_address) {
302 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700303 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000304 }
305
306}
307
Steven Price102f45f2020-02-03 17:36:29 -0800308void ptdump_walk(struct seq_file *s, struct ptdump_info *info)
Laura Abbottc9465b42014-11-26 00:28:39 +0000309{
Steven Price102f45f2020-02-03 17:36:29 -0800310 unsigned long end = ~0UL;
311 struct pg_state st;
Laura Abbottc9465b42014-11-26 00:28:39 +0000312
Steven Price102f45f2020-02-03 17:36:29 -0800313 if (info->base_addr < TASK_SIZE_64)
314 end = TASK_SIZE_64;
Laura Abbottc9465b42014-11-26 00:28:39 +0000315
Steven Price102f45f2020-02-03 17:36:29 -0800316 st = (struct pg_state){
317 .seq = s,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100318 .marker = info->markers,
Hailong Liub9ba6802021-02-02 23:07:49 +0800319 .level = -1,
Steven Price102f45f2020-02-03 17:36:29 -0800320 .ptdump = {
321 .note_page = note_page,
322 .range = (struct ptdump_range[]){
323 {info->base_addr, end},
324 {0, 0}
325 }
326 }
Laura Abbottc9465b42014-11-26 00:28:39 +0000327 };
328
Steven Pricee47690d2020-02-03 17:36:42 -0800329 ptdump_walk_pgd(&st.ptdump, info->mm, NULL);
Laura Abbottc9465b42014-11-26 00:28:39 +0000330}
331
Jisheng Zhanga7dcf582021-03-30 13:54:49 +0800332static void __init ptdump_initialize(void)
Laura Abbottc9465b42014-11-26 00:28:39 +0000333{
Laura Abbottc9465b42014-11-26 00:28:39 +0000334 unsigned i, j;
335
336 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
337 if (pg_level[i].bits)
338 for (j = 0; j < pg_level[i].num; j++)
339 pg_level[i].mask |= pg_level[i].bits[j].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000340}
Mark Rutland4674fdb2016-05-31 14:49:01 +0100341
342static struct ptdump_info kernel_ptdump_info = {
343 .mm = &init_mm,
344 .markers = address_markers,
Steve Capper14c127c2019-08-07 16:55:14 +0100345 .base_addr = PAGE_OFFSET,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100346};
347
Laura Abbott1404d6f2016-10-27 09:27:34 -0700348void ptdump_check_wx(void)
349{
350 struct pg_state st = {
351 .seq = NULL,
352 .marker = (struct addr_marker[]) {
353 { 0, NULL},
354 { -1, NULL},
355 },
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800356 .level = -1,
Laura Abbott1404d6f2016-10-27 09:27:34 -0700357 .check_wx = true,
Steven Price102f45f2020-02-03 17:36:29 -0800358 .ptdump = {
359 .note_page = note_page,
360 .range = (struct ptdump_range[]) {
361 {PAGE_OFFSET, ~0UL},
362 {0, 0}
363 }
364 }
Laura Abbott1404d6f2016-10-27 09:27:34 -0700365 };
366
Steven Pricee47690d2020-02-03 17:36:42 -0800367 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
Steven Price102f45f2020-02-03 17:36:29 -0800368
Laura Abbott1404d6f2016-10-27 09:27:34 -0700369 if (st.wx_pages || st.uxn_pages)
370 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
371 st.wx_pages, st.uxn_pages);
372 else
373 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
374}
375
Jisheng Zhanga7dcf582021-03-30 13:54:49 +0800376static int __init ptdump_init(void)
Mark Rutland4674fdb2016-05-31 14:49:01 +0100377{
Mark Rutland77ad4ce2019-08-14 14:28:48 +0100378 address_markers[PAGE_END_NR].start_address = PAGE_END;
Andrey Konovalov0fea6e92020-12-22 12:02:06 -0800379#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
Steve Capper99426e52019-08-07 16:55:16 +0100380 address_markers[KASAN_START_NR].start_address = KASAN_SHADOW_START;
381#endif
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700382 ptdump_initialize();
Greg Kroah-Hartmane2a2e562019-01-22 15:41:11 +0100383 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
384 return 0;
Mark Rutland4674fdb2016-05-31 14:49:01 +0100385}
Laura Abbottc9465b42014-11-26 00:28:39 +0000386device_initcall(ptdump_init);