blob: beec87488e979c355961e0a7db26d2a4ae92567b [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>
18#include <linux/sched.h>
19#include <linux/seq_file.h>
20
21#include <asm/fixmap.h>
Ard Biesheuveld8fc68a2016-04-22 18:48:04 +020022#include <asm/kasan.h>
Mark Rutland764011c2015-01-22 18:20:36 +000023#include <asm/memory.h>
Laura Abbottc9465b42014-11-26 00:28:39 +000024#include <asm/pgtable.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
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020028static const struct addr_marker address_markers[] = {
Steve Capper14c127c2019-08-07 16:55:14 +010029 { PAGE_OFFSET, "Linear Mapping start" },
30 { VA_START, "Linear Mapping end" },
Ard Biesheuveld8fc68a2016-04-22 18:48:04 +020031#ifdef CONFIG_KASAN
32 { KASAN_SHADOW_START, "Kasan shadow start" },
33 { KASAN_SHADOW_END, "Kasan shadow end" },
34#endif
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020035 { MODULES_VADDR, "Modules start" },
36 { MODULES_END, "Modules end" },
Will Deacon4733c7c2018-09-05 15:12:27 +010037 { VMALLOC_START, "vmalloc() area" },
38 { VMALLOC_END, "vmalloc() end" },
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020039 { FIXADDR_START, "Fixmap start" },
40 { FIXADDR_TOP, "Fixmap end" },
41 { PCI_IO_START, "PCI I/O start" },
42 { PCI_IO_END, "PCI I/O end" },
Ard Biesheuvel3e1907d2016-03-30 16:46:00 +020043#ifdef CONFIG_SPARSEMEM_VMEMMAP
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020044 { VMEMMAP_START, "vmemmap start" },
45 { VMEMMAP_START + VMEMMAP_SIZE, "vmemmap end" },
Ard Biesheuvel3e1907d2016-03-30 16:46:00 +020046#endif
Ard Biesheuvelc8f8cca2016-04-22 18:48:03 +020047 { -1, NULL },
Laura Abbottc9465b42014-11-26 00:28:39 +000048};
49
Laura Abbottae5d1cf2016-10-27 09:27:32 -070050#define pt_dump_seq_printf(m, fmt, args...) \
51({ \
52 if (m) \
53 seq_printf(m, fmt, ##args); \
54})
55
56#define pt_dump_seq_puts(m, fmt) \
57({ \
58 if (m) \
59 seq_printf(m, fmt); \
60})
61
Jeremy Linton202e41a12015-10-07 12:00:23 -050062/*
63 * The page dumper groups page table entries of the same type into a single
64 * description. It uses pg_state to track the range information while
65 * iterating over the pte entries. When the continuity is broken it then
66 * dumps out a description of the range.
67 */
Laura Abbottc9465b42014-11-26 00:28:39 +000068struct pg_state {
69 struct seq_file *seq;
70 const struct addr_marker *marker;
71 unsigned long start_address;
72 unsigned level;
73 u64 current_prot;
Laura Abbott1404d6f2016-10-27 09:27:34 -070074 bool check_wx;
75 unsigned long wx_pages;
76 unsigned long uxn_pages;
Laura Abbottc9465b42014-11-26 00:28:39 +000077};
78
79struct prot_bits {
80 u64 mask;
81 u64 val;
82 const char *set;
83 const char *clear;
84};
85
86static const struct prot_bits pte_bits[] = {
87 {
Laura Abbottd7e9d592016-02-05 16:24:48 -080088 .mask = PTE_VALID,
89 .val = PTE_VALID,
90 .set = " ",
91 .clear = "F",
92 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +000093 .mask = PTE_USER,
94 .val = PTE_USER,
95 .set = "USR",
96 .clear = " ",
97 }, {
98 .mask = PTE_RDONLY,
99 .val = PTE_RDONLY,
100 .set = "ro",
101 .clear = "RW",
102 }, {
103 .mask = PTE_PXN,
104 .val = PTE_PXN,
105 .set = "NX",
106 .clear = "x ",
107 }, {
108 .mask = PTE_SHARED,
109 .val = PTE_SHARED,
110 .set = "SHD",
111 .clear = " ",
112 }, {
113 .mask = PTE_AF,
114 .val = PTE_AF,
115 .set = "AF",
116 .clear = " ",
117 }, {
118 .mask = PTE_NG,
119 .val = PTE_NG,
120 .set = "NG",
121 .clear = " ",
122 }, {
Jeremy Linton202e41a12015-10-07 12:00:23 -0500123 .mask = PTE_CONT,
124 .val = PTE_CONT,
125 .set = "CON",
126 .clear = " ",
127 }, {
128 .mask = PTE_TABLE_BIT,
129 .val = PTE_TABLE_BIT,
130 .set = " ",
131 .clear = "BLK",
132 }, {
Laura Abbottc9465b42014-11-26 00:28:39 +0000133 .mask = PTE_UXN,
134 .val = PTE_UXN,
135 .set = "UXN",
136 }, {
137 .mask = PTE_ATTRINDX_MASK,
138 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
139 .set = "DEVICE/nGnRnE",
140 }, {
141 .mask = PTE_ATTRINDX_MASK,
142 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
143 .set = "DEVICE/nGnRE",
144 }, {
145 .mask = PTE_ATTRINDX_MASK,
146 .val = PTE_ATTRINDX(MT_DEVICE_GRE),
147 .set = "DEVICE/GRE",
148 }, {
149 .mask = PTE_ATTRINDX_MASK,
150 .val = PTE_ATTRINDX(MT_NORMAL_NC),
151 .set = "MEM/NORMAL-NC",
152 }, {
153 .mask = PTE_ATTRINDX_MASK,
154 .val = PTE_ATTRINDX(MT_NORMAL),
155 .set = "MEM/NORMAL",
156 }
157};
158
159struct pg_level {
160 const struct prot_bits *bits;
Mark Rutland48dd73c2016-05-31 14:49:02 +0100161 const char *name;
Laura Abbottc9465b42014-11-26 00:28:39 +0000162 size_t num;
163 u64 mask;
164};
165
166static struct pg_level pg_level[] = {
167 {
168 }, { /* pgd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100169 .name = "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000170 .bits = pte_bits,
171 .num = ARRAY_SIZE(pte_bits),
172 }, { /* pud */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100173 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000174 .bits = pte_bits,
175 .num = ARRAY_SIZE(pte_bits),
176 }, { /* pmd */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100177 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
Laura Abbottc9465b42014-11-26 00:28:39 +0000178 .bits = pte_bits,
179 .num = ARRAY_SIZE(pte_bits),
180 }, { /* pte */
Mark Rutland48dd73c2016-05-31 14:49:02 +0100181 .name = "PTE",
Laura Abbottc9465b42014-11-26 00:28:39 +0000182 .bits = pte_bits,
183 .num = ARRAY_SIZE(pte_bits),
184 },
185};
186
187static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
188 size_t num)
189{
190 unsigned i;
191
192 for (i = 0; i < num; i++, bits++) {
193 const char *s;
194
195 if ((st->current_prot & bits->mask) == bits->val)
196 s = bits->set;
197 else
198 s = bits->clear;
199
200 if (s)
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700201 pt_dump_seq_printf(st->seq, " %s", s);
Laura Abbottc9465b42014-11-26 00:28:39 +0000202 }
203}
204
Laura Abbott1404d6f2016-10-27 09:27:34 -0700205static void note_prot_uxn(struct pg_state *st, unsigned long addr)
206{
207 if (!st->check_wx)
208 return;
209
210 if ((st->current_prot & PTE_UXN) == PTE_UXN)
211 return;
212
213 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
214 (void *)st->start_address, (void *)st->start_address);
215
216 st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
217}
218
219static void note_prot_wx(struct pg_state *st, unsigned long addr)
220{
221 if (!st->check_wx)
222 return;
223 if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
224 return;
225 if ((st->current_prot & PTE_PXN) == PTE_PXN)
226 return;
227
228 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
229 (void *)st->start_address, (void *)st->start_address);
230
231 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
232}
233
Laura Abbottc9465b42014-11-26 00:28:39 +0000234static void note_page(struct pg_state *st, unsigned long addr, unsigned level,
235 u64 val)
236{
237 static const char units[] = "KMGTPE";
238 u64 prot = val & pg_level[level].mask;
239
Laura Abbottc9465b42014-11-26 00:28:39 +0000240 if (!st->level) {
241 st->level = level;
242 st->current_prot = prot;
243 st->start_address = addr;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700244 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000245 } else if (prot != st->current_prot || level != st->level ||
246 addr >= st->marker[1].start_address) {
247 const char *unit = units;
248 unsigned long delta;
249
250 if (st->current_prot) {
Laura Abbott1404d6f2016-10-27 09:27:34 -0700251 note_prot_uxn(st, addr);
252 note_prot_wx(st, addr);
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700253 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
Laura Abbottc9465b42014-11-26 00:28:39 +0000254 st->start_address, addr);
255
256 delta = (addr - st->start_address) >> 10;
257 while (!(delta & 1023) && unit[1]) {
258 delta >>= 10;
259 unit++;
260 }
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700261 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
Mark Rutland48dd73c2016-05-31 14:49:02 +0100262 pg_level[st->level].name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000263 if (pg_level[st->level].bits)
264 dump_prot(st, pg_level[st->level].bits,
265 pg_level[st->level].num);
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700266 pt_dump_seq_puts(st->seq, "\n");
Laura Abbottc9465b42014-11-26 00:28:39 +0000267 }
268
269 if (addr >= st->marker[1].start_address) {
270 st->marker++;
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 }
273
274 st->start_address = addr;
275 st->current_prot = prot;
276 st->level = level;
277 }
278
279 if (addr >= st->marker[1].start_address) {
280 st->marker++;
Laura Abbottae5d1cf2016-10-27 09:27:32 -0700281 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
Laura Abbottc9465b42014-11-26 00:28:39 +0000282 }
283
284}
285
Will Deacond23c8082019-02-04 14:37:38 +0000286static void walk_pte(struct pg_state *st, pmd_t *pmdp, unsigned long start,
287 unsigned long end)
Laura Abbottc9465b42014-11-26 00:28:39 +0000288{
Will Deacond23c8082019-02-04 14:37:38 +0000289 unsigned long addr = start;
290 pte_t *ptep = pte_offset_kernel(pmdp, start);
Laura Abbottc9465b42014-11-26 00:28:39 +0000291
Will Deacond23c8082019-02-04 14:37:38 +0000292 do {
Will Deacon20a004e2018-02-15 11:14:56 +0000293 note_page(st, addr, 4, READ_ONCE(pte_val(*ptep)));
Will Deacond23c8082019-02-04 14:37:38 +0000294 } while (ptep++, addr += PAGE_SIZE, addr != end);
Laura Abbottc9465b42014-11-26 00:28:39 +0000295}
296
Will Deacond23c8082019-02-04 14:37:38 +0000297static void walk_pmd(struct pg_state *st, pud_t *pudp, unsigned long start,
298 unsigned long end)
Laura Abbottc9465b42014-11-26 00:28:39 +0000299{
Will Deacond23c8082019-02-04 14:37:38 +0000300 unsigned long next, addr = start;
301 pmd_t *pmdp = pmd_offset(pudp, start);
Laura Abbottc9465b42014-11-26 00:28:39 +0000302
Will Deacond23c8082019-02-04 14:37:38 +0000303 do {
Will Deacon20a004e2018-02-15 11:14:56 +0000304 pmd_t pmd = READ_ONCE(*pmdp);
Will Deacond23c8082019-02-04 14:37:38 +0000305 next = pmd_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000306
Will Deacon20a004e2018-02-15 11:14:56 +0000307 if (pmd_none(pmd) || pmd_sect(pmd)) {
308 note_page(st, addr, 3, pmd_val(pmd));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000309 } else {
Will Deacon20a004e2018-02-15 11:14:56 +0000310 BUG_ON(pmd_bad(pmd));
Will Deacond23c8082019-02-04 14:37:38 +0000311 walk_pte(st, pmdp, addr, next);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000312 }
Will Deacond23c8082019-02-04 14:37:38 +0000313 } while (pmdp++, addr = next, addr != end);
Laura Abbottc9465b42014-11-26 00:28:39 +0000314}
315
Will Deacond23c8082019-02-04 14:37:38 +0000316static void walk_pud(struct pg_state *st, pgd_t *pgdp, unsigned long start,
317 unsigned long end)
Laura Abbottc9465b42014-11-26 00:28:39 +0000318{
Will Deacond23c8082019-02-04 14:37:38 +0000319 unsigned long next, addr = start;
320 pud_t *pudp = pud_offset(pgdp, start);
Laura Abbottc9465b42014-11-26 00:28:39 +0000321
Will Deacond23c8082019-02-04 14:37:38 +0000322 do {
Will Deacon20a004e2018-02-15 11:14:56 +0000323 pud_t pud = READ_ONCE(*pudp);
Will Deacond23c8082019-02-04 14:37:38 +0000324 next = pud_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000325
Will Deacon20a004e2018-02-15 11:14:56 +0000326 if (pud_none(pud) || pud_sect(pud)) {
327 note_page(st, addr, 2, pud_val(pud));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000328 } else {
Will Deacon20a004e2018-02-15 11:14:56 +0000329 BUG_ON(pud_bad(pud));
Will Deacond23c8082019-02-04 14:37:38 +0000330 walk_pmd(st, pudp, addr, next);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000331 }
Will Deacond23c8082019-02-04 14:37:38 +0000332 } while (pudp++, addr = next, addr != end);
Laura Abbottc9465b42014-11-26 00:28:39 +0000333}
334
Mark Rutland4674fdb2016-05-31 14:49:01 +0100335static void walk_pgd(struct pg_state *st, struct mm_struct *mm,
336 unsigned long start)
Laura Abbottc9465b42014-11-26 00:28:39 +0000337{
Will Deacond23c8082019-02-04 14:37:38 +0000338 unsigned long end = (start < TASK_SIZE_64) ? TASK_SIZE_64 : 0;
339 unsigned long next, addr = start;
340 pgd_t *pgdp = pgd_offset(mm, start);
Laura Abbottc9465b42014-11-26 00:28:39 +0000341
Will Deacond23c8082019-02-04 14:37:38 +0000342 do {
Will Deacon20a004e2018-02-15 11:14:56 +0000343 pgd_t pgd = READ_ONCE(*pgdp);
Will Deacond23c8082019-02-04 14:37:38 +0000344 next = pgd_addr_end(addr, end);
Will Deacon20a004e2018-02-15 11:14:56 +0000345
Will Deacon20a004e2018-02-15 11:14:56 +0000346 if (pgd_none(pgd)) {
347 note_page(st, addr, 1, pgd_val(pgd));
Mark Rutlanda1c76572015-01-27 16:36:30 +0000348 } else {
Will Deacon20a004e2018-02-15 11:14:56 +0000349 BUG_ON(pgd_bad(pgd));
Will Deacond23c8082019-02-04 14:37:38 +0000350 walk_pud(st, pgdp, addr, next);
Mark Rutlanda1c76572015-01-27 16:36:30 +0000351 }
Will Deacond23c8082019-02-04 14:37:38 +0000352 } while (pgdp++, addr = next, addr != end);
Laura Abbottc9465b42014-11-26 00:28:39 +0000353}
354
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700355void ptdump_walk_pgd(struct seq_file *m, struct ptdump_info *info)
Laura Abbottc9465b42014-11-26 00:28:39 +0000356{
357 struct pg_state st = {
358 .seq = m,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100359 .marker = info->markers,
Laura Abbottc9465b42014-11-26 00:28:39 +0000360 };
361
Mark Rutland4674fdb2016-05-31 14:49:01 +0100362 walk_pgd(&st, info->mm, info->base_addr);
Laura Abbottc9465b42014-11-26 00:28:39 +0000363
364 note_page(&st, 0, 0, 0);
Laura Abbottc9465b42014-11-26 00:28:39 +0000365}
366
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700367static void ptdump_initialize(void)
Laura Abbottc9465b42014-11-26 00:28:39 +0000368{
Laura Abbottc9465b42014-11-26 00:28:39 +0000369 unsigned i, j;
370
371 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
372 if (pg_level[i].bits)
373 for (j = 0; j < pg_level[i].num; j++)
374 pg_level[i].mask |= pg_level[i].bits[j].mask;
Laura Abbottc9465b42014-11-26 00:28:39 +0000375}
Mark Rutland4674fdb2016-05-31 14:49:01 +0100376
377static struct ptdump_info kernel_ptdump_info = {
378 .mm = &init_mm,
379 .markers = address_markers,
Steve Capper14c127c2019-08-07 16:55:14 +0100380 .base_addr = PAGE_OFFSET,
Mark Rutland4674fdb2016-05-31 14:49:01 +0100381};
382
Laura Abbott1404d6f2016-10-27 09:27:34 -0700383void ptdump_check_wx(void)
384{
385 struct pg_state st = {
386 .seq = NULL,
387 .marker = (struct addr_marker[]) {
388 { 0, NULL},
389 { -1, NULL},
390 },
391 .check_wx = true,
392 };
393
Mark Rutland1d08a042017-12-13 11:45:42 +0000394 walk_pgd(&st, &init_mm, VA_START);
Laura Abbott1404d6f2016-10-27 09:27:34 -0700395 note_page(&st, 0, 0, 0);
396 if (st.wx_pages || st.uxn_pages)
397 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
398 st.wx_pages, st.uxn_pages);
399 else
400 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
401}
402
Mark Rutland4674fdb2016-05-31 14:49:01 +0100403static int ptdump_init(void)
404{
Laura Abbott4ddb9bf2016-10-27 09:27:31 -0700405 ptdump_initialize();
Greg Kroah-Hartmane2a2e562019-01-22 15:41:11 +0100406 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
407 return 0;
Mark Rutland4674fdb2016-05-31 14:49:01 +0100408}
Laura Abbottc9465b42014-11-26 00:28:39 +0000409device_initcall(ptdump_init);