blob: e1b599ecbbc26d02c2de09a589f899f0eebca01d [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Arjan van de Ven926e5392008-04-17 17:40:45 +02002/*
3 * Debug helper to dump the current kernel pagetables of the system
4 * so that we can see what the various memory ranges are set to.
5 *
6 * (C) Copyright 2008 Intel Corporation
7 *
8 * Author: Arjan van de Ven <arjan@linux.intel.com>
Arjan van de Ven926e5392008-04-17 17:40:45 +02009 */
10
H. Peter Anvinfe770bf02008-04-17 17:40:45 +020011#include <linux/debugfs.h>
Andrey Ryabinin04b67022017-07-24 18:25:58 +030012#include <linux/kasan.h>
H. Peter Anvinfe770bf02008-04-17 17:40:45 +020013#include <linux/mm.h>
Paul Gortmaker84e629b2016-07-13 20:18:54 -040014#include <linux/init.h>
Andrey Ryabinin146fbb762017-02-10 12:54:05 +030015#include <linux/sched.h>
Arjan van de Ven926e5392008-04-17 17:40:45 +020016#include <linux/seq_file.h>
Joerg Roedeld6ef1f12018-04-17 15:27:16 +020017#include <linux/highmem.h>
Thomas Gleixnerc200dac2018-10-08 21:53:48 +020018#include <linux/pci.h>
Steven Price2ae27132020-02-03 17:36:24 -080019#include <linux/ptdump.h>
Arjan van de Ven926e5392008-04-17 17:40:45 +020020
Thomas Gleixnerc200dac2018-10-08 21:53:48 +020021#include <asm/e820/types.h>
Arjan van de Ven926e5392008-04-17 17:40:45 +020022
23/*
24 * The dumper groups pagetable entries of the same type into one, and for
25 * that it needs to keep some state when walking, and flush this state
26 * when a "break" in the continuity is found.
27 */
28struct pg_state {
Steven Price2ae27132020-02-03 17:36:24 -080029 struct ptdump_state ptdump;
Arjan van de Ven926e5392008-04-17 17:40:45 +020030 int level;
Steven Price2ae27132020-02-03 17:36:24 -080031 pgprotval_t current_prot;
Jan Beulich672c0ae2018-02-23 01:27:37 -070032 pgprotval_t effective_prot;
Steven Price2ae27132020-02-03 17:36:24 -080033 pgprotval_t prot_levels[5];
Arjan van de Ven926e5392008-04-17 17:40:45 +020034 unsigned long start_address;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +020035 const struct addr_marker *marker;
H. Peter Anvin3891a042014-04-29 16:46:09 -070036 unsigned long lines;
Borislav Petkovef6bea62014-01-18 12:48:14 +010037 bool to_dmesg;
Stephen Smalleye1a58322015-10-05 12:55:20 -040038 bool check_wx;
39 unsigned long wx_pages;
Steven Price74d2aaa2020-02-03 17:36:07 -080040 struct seq_file *seq;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +020041};
42
43struct addr_marker {
44 unsigned long start_address;
45 const char *name;
H. Peter Anvin3891a042014-04-29 16:46:09 -070046 unsigned long max_lines;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +020047};
48
Thomas Gleixner146122e2017-12-20 18:07:42 +010049/* Address space markers hints */
50
51#ifdef CONFIG_X86_64
52
Andres Salomon92851e22010-07-20 15:19:46 -070053enum address_markers_idx {
54 USER_SPACE_NR = 0,
Andres Salomon92851e22010-07-20 15:19:46 -070055 KERNEL_SPACE_NR,
Kirill A. Shutemov254eb552018-11-30 23:23:28 +030056#ifdef CONFIG_MODIFY_LDT_SYSCALL
Andy Lutomirskif55f0502017-12-12 07:56:45 -080057 LDT_NR,
58#endif
Kirill A. Shutemov254eb552018-11-30 23:23:28 +030059 LOW_KERNEL_NR,
Andres Salomon92851e22010-07-20 15:19:46 -070060 VMALLOC_START_NR,
61 VMEMMAP_START_NR,
Andrey Ryabinin025205f2017-02-14 13:08:39 +030062#ifdef CONFIG_KASAN
63 KASAN_SHADOW_START_NR,
64 KASAN_SHADOW_END_NR,
65#endif
Thomas Gleixnerf2078902018-01-04 13:01:40 +010066 CPU_ENTRY_AREA_NR,
Thomas Gleixner146122e2017-12-20 18:07:42 +010067#ifdef CONFIG_X86_ESPFIX64
H. Peter Anvin3891a042014-04-29 16:46:09 -070068 ESPFIX_START_NR,
Thomas Gleixner146122e2017-12-20 18:07:42 +010069#endif
70#ifdef CONFIG_EFI
71 EFI_END_NR,
72#endif
Andres Salomon92851e22010-07-20 15:19:46 -070073 HIGH_KERNEL_NR,
74 MODULES_VADDR_NR,
75 MODULES_END_NR,
Thomas Gleixner146122e2017-12-20 18:07:42 +010076 FIXADDR_START_NR,
77 END_OF_SPACE_NR,
78};
79
80static struct addr_marker address_markers[] = {
81 [USER_SPACE_NR] = { 0, "User Space" },
82 [KERNEL_SPACE_NR] = { (1UL << 63), "Kernel Space" },
83 [LOW_KERNEL_NR] = { 0UL, "Low Kernel Mapping" },
84 [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" },
85 [VMEMMAP_START_NR] = { 0UL, "Vmemmap" },
86#ifdef CONFIG_KASAN
Kirill A. Shutemov09e61a72018-02-14 14:16:55 +030087 /*
88 * These fields get initialized with the (dynamic)
89 * KASAN_SHADOW_{START,END} values in pt_dump_init().
90 */
91 [KASAN_SHADOW_START_NR] = { 0UL, "KASAN shadow" },
92 [KASAN_SHADOW_END_NR] = { 0UL, "KASAN shadow end" },
Thomas Gleixner146122e2017-12-20 18:07:42 +010093#endif
Andy Lutomirskif55f0502017-12-12 07:56:45 -080094#ifdef CONFIG_MODIFY_LDT_SYSCALL
Kirill A. Shutemov5c7919b2018-02-14 14:16:52 +030095 [LDT_NR] = { 0UL, "LDT remap" },
Andy Lutomirskif55f0502017-12-12 07:56:45 -080096#endif
Thomas Gleixner92a0f812017-12-20 18:51:31 +010097 [CPU_ENTRY_AREA_NR] = { CPU_ENTRY_AREA_BASE,"CPU entry Area" },
Thomas Gleixner146122e2017-12-20 18:07:42 +010098#ifdef CONFIG_X86_ESPFIX64
99 [ESPFIX_START_NR] = { ESPFIX_BASE_ADDR, "ESPfix Area", 16 },
100#endif
101#ifdef CONFIG_EFI
102 [EFI_END_NR] = { EFI_VA_END, "EFI Runtime Services" },
103#endif
104 [HIGH_KERNEL_NR] = { __START_KERNEL_map, "High Kernel Mapping" },
105 [MODULES_VADDR_NR] = { MODULES_VADDR, "Modules" },
106 [MODULES_END_NR] = { MODULES_END, "End Modules" },
107 [FIXADDR_START_NR] = { FIXADDR_START, "Fixmap Area" },
108 [END_OF_SPACE_NR] = { -1, NULL }
109};
110
Joerg Roedel4e8537e2018-07-18 11:41:08 +0200111#define INIT_PGD ((pgd_t *) &init_top_pgt)
112
Thomas Gleixner146122e2017-12-20 18:07:42 +0100113#else /* CONFIG_X86_64 */
114
115enum address_markers_idx {
116 USER_SPACE_NR = 0,
Andres Salomon92851e22010-07-20 15:19:46 -0700117 KERNEL_SPACE_NR,
118 VMALLOC_START_NR,
119 VMALLOC_END_NR,
Thomas Gleixner146122e2017-12-20 18:07:42 +0100120#ifdef CONFIG_HIGHMEM
Andres Salomon92851e22010-07-20 15:19:46 -0700121 PKMAP_BASE_NR,
Andres Salomon92851e22010-07-20 15:19:46 -0700122#endif
Joerg Roedelf3e48e52018-07-18 11:41:10 +0200123#ifdef CONFIG_MODIFY_LDT_SYSCALL
124 LDT_NR,
125#endif
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100126 CPU_ENTRY_AREA_NR,
Thomas Gleixner146122e2017-12-20 18:07:42 +0100127 FIXADDR_START_NR,
128 END_OF_SPACE_NR,
Andres Salomon92851e22010-07-20 15:19:46 -0700129};
130
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200131static struct addr_marker address_markers[] = {
Thomas Gleixner146122e2017-12-20 18:07:42 +0100132 [USER_SPACE_NR] = { 0, "User Space" },
133 [KERNEL_SPACE_NR] = { PAGE_OFFSET, "Kernel Mapping" },
134 [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" },
135 [VMALLOC_END_NR] = { 0UL, "vmalloc() End" },
136#ifdef CONFIG_HIGHMEM
137 [PKMAP_BASE_NR] = { 0UL, "Persistent kmap() Area" },
Andrey Ryabinin025205f2017-02-14 13:08:39 +0300138#endif
Joerg Roedelf3e48e52018-07-18 11:41:10 +0200139#ifdef CONFIG_MODIFY_LDT_SYSCALL
140 [LDT_NR] = { 0UL, "LDT remap" },
141#endif
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100142 [CPU_ENTRY_AREA_NR] = { 0UL, "CPU entry area" },
Thomas Gleixner146122e2017-12-20 18:07:42 +0100143 [FIXADDR_START_NR] = { 0UL, "Fixmap area" },
144 [END_OF_SPACE_NR] = { -1, NULL }
Arjan van de Ven926e5392008-04-17 17:40:45 +0200145};
146
Joerg Roedel4e8537e2018-07-18 11:41:08 +0200147#define INIT_PGD (swapper_pg_dir)
148
Thomas Gleixner146122e2017-12-20 18:07:42 +0100149#endif /* !CONFIG_X86_64 */
150
Arjan van de Ven926e5392008-04-17 17:40:45 +0200151/* Multipliers for offsets within the PTEs */
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200152#define PTE_LEVEL_MULT (PAGE_SIZE)
153#define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
154#define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
Kirill A. Shutemovfdd3d8c2017-03-28 13:48:06 +0300155#define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
Juergen Gross84bbabc2017-04-12 16:36:34 +0200156#define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT)
Arjan van de Ven926e5392008-04-17 17:40:45 +0200157
Borislav Petkovef6bea62014-01-18 12:48:14 +0100158#define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \
159({ \
160 if (to_dmesg) \
161 printk(KERN_INFO fmt, ##args); \
162 else \
163 if (m) \
164 seq_printf(m, fmt, ##args); \
165})
166
167#define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \
168({ \
169 if (to_dmesg) \
170 printk(KERN_CONT fmt, ##args); \
171 else \
172 if (m) \
173 seq_printf(m, fmt, ##args); \
174})
175
Arjan van de Ven926e5392008-04-17 17:40:45 +0200176/*
177 * Print a readable form of a pgprot_t to the seq_file
178 */
Steven Price2ae27132020-02-03 17:36:24 -0800179static void printk_prot(struct seq_file *m, pgprotval_t pr, int level, bool dmsg)
Arjan van de Ven926e5392008-04-17 17:40:45 +0200180{
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200181 static const char * const level_name[] =
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800182 { "pgd", "p4d", "pud", "pmd", "pte" };
Arjan van de Ven926e5392008-04-17 17:40:45 +0200183
Thomas Gleixnerc0534492017-12-16 01:14:39 +0100184 if (!(pr & _PAGE_PRESENT)) {
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200185 /* Not present */
Juergen Grossf439c429c32014-11-03 14:02:01 +0100186 pt_dump_cont_printf(m, dmsg, " ");
Arjan van de Ven926e5392008-04-17 17:40:45 +0200187 } else {
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200188 if (pr & _PAGE_USER)
Borislav Petkovef6bea62014-01-18 12:48:14 +0100189 pt_dump_cont_printf(m, dmsg, "USR ");
Arjan van de Ven926e5392008-04-17 17:40:45 +0200190 else
Borislav Petkovef6bea62014-01-18 12:48:14 +0100191 pt_dump_cont_printf(m, dmsg, " ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200192 if (pr & _PAGE_RW)
Borislav Petkovef6bea62014-01-18 12:48:14 +0100193 pt_dump_cont_printf(m, dmsg, "RW ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200194 else
Borislav Petkovef6bea62014-01-18 12:48:14 +0100195 pt_dump_cont_printf(m, dmsg, "ro ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200196 if (pr & _PAGE_PWT)
Borislav Petkovef6bea62014-01-18 12:48:14 +0100197 pt_dump_cont_printf(m, dmsg, "PWT ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200198 else
Borislav Petkovef6bea62014-01-18 12:48:14 +0100199 pt_dump_cont_printf(m, dmsg, " ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200200 if (pr & _PAGE_PCD)
Borislav Petkovef6bea62014-01-18 12:48:14 +0100201 pt_dump_cont_printf(m, dmsg, "PCD ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200202 else
Borislav Petkovef6bea62014-01-18 12:48:14 +0100203 pt_dump_cont_printf(m, dmsg, " ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200204
Juergen Grossf439c429c32014-11-03 14:02:01 +0100205 /* Bit 7 has a different meaning on level 3 vs 4 */
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800206 if (level <= 3 && pr & _PAGE_PSE)
Juergen Grossf439c429c32014-11-03 14:02:01 +0100207 pt_dump_cont_printf(m, dmsg, "PSE ");
208 else
209 pt_dump_cont_printf(m, dmsg, " ");
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800210 if ((level == 4 && pr & _PAGE_PAT) ||
211 ((level == 3 || level == 2) && pr & _PAGE_PAT_LARGE))
Toshi Kanida25e622015-09-17 12:24:19 -0600212 pt_dump_cont_printf(m, dmsg, "PAT ");
Juergen Grossf439c429c32014-11-03 14:02:01 +0100213 else
214 pt_dump_cont_printf(m, dmsg, " ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200215 if (pr & _PAGE_GLOBAL)
Borislav Petkovef6bea62014-01-18 12:48:14 +0100216 pt_dump_cont_printf(m, dmsg, "GLB ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200217 else
Borislav Petkovef6bea62014-01-18 12:48:14 +0100218 pt_dump_cont_printf(m, dmsg, " ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200219 if (pr & _PAGE_NX)
Borislav Petkovef6bea62014-01-18 12:48:14 +0100220 pt_dump_cont_printf(m, dmsg, "NX ");
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200221 else
Borislav Petkovef6bea62014-01-18 12:48:14 +0100222 pt_dump_cont_printf(m, dmsg, "x ");
Arjan van de Ven926e5392008-04-17 17:40:45 +0200223 }
Borislav Petkovef6bea62014-01-18 12:48:14 +0100224 pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
Arjan van de Ven926e5392008-04-17 17:40:45 +0200225}
226
Steven Price2ae27132020-02-03 17:36:24 -0800227static void note_wx(struct pg_state *st, unsigned long addr)
Thomas Gleixnerc200dac2018-10-08 21:53:48 +0200228{
229 unsigned long npages;
230
Steven Price2ae27132020-02-03 17:36:24 -0800231 npages = (addr - st->start_address) / PAGE_SIZE;
Thomas Gleixnerc200dac2018-10-08 21:53:48 +0200232
233#ifdef CONFIG_PCI_BIOS
234 /*
235 * If PCI BIOS is enabled, the PCI BIOS area is forced to WX.
236 * Inform about it, but avoid the warning.
237 */
238 if (pcibios_enabled && st->start_address >= PAGE_OFFSET + BIOS_BEGIN &&
Steven Price2ae27132020-02-03 17:36:24 -0800239 addr <= PAGE_OFFSET + BIOS_END) {
Thomas Gleixnerc200dac2018-10-08 21:53:48 +0200240 pr_warn_once("x86/mm: PCI BIOS W+X mapping %lu pages\n", npages);
241 return;
242 }
243#endif
244 /* Account the WX pages */
245 st->wx_pages += npages;
Thomas Gleixner510bb962019-04-15 10:46:07 +0200246 WARN_ONCE(__supported_pte_mask & _PAGE_NX,
247 "x86/mm: Found insecure W+X mapping at address %pS\n",
Thomas Gleixnerc200dac2018-10-08 21:53:48 +0200248 (void *)st->start_address);
249}
250
Steven Price1494e0c2020-06-01 21:49:58 -0700251static void effective_prot(struct ptdump_state *pt_st, int level, u64 val)
Steven Price2ae27132020-02-03 17:36:24 -0800252{
Steven Price1494e0c2020-06-01 21:49:58 -0700253 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
254 pgprotval_t prot = val & PTE_FLAGS_MASK;
255 pgprotval_t effective;
256
257 if (level > 0) {
258 pgprotval_t higher_prot = st->prot_levels[level - 1];
259
260 effective = (higher_prot & prot & (_PAGE_USER | _PAGE_RW)) |
261 ((higher_prot | prot) & _PAGE_NX);
262 } else {
263 effective = prot;
264 }
265
266 st->prot_levels[level] = effective;
Steven Price2ae27132020-02-03 17:36:24 -0800267}
268
Arjan van de Ven926e5392008-04-17 17:40:45 +0200269/*
270 * This function gets called on a break in a continuous series
271 * of PTE entries; the next one is different so we need to
272 * print what we collected so far.
273 */
Steven Price2ae27132020-02-03 17:36:24 -0800274static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
Steven Price99395ee2020-06-01 21:50:01 -0700275 u64 val)
Arjan van de Ven926e5392008-04-17 17:40:45 +0200276{
Steven Price2ae27132020-02-03 17:36:24 -0800277 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
278 pgprotval_t new_prot, new_eff;
279 pgprotval_t cur, eff;
H. Peter Anvin3891a042014-04-29 16:46:09 -0700280 static const char units[] = "BKMGTPE";
Steven Price74d2aaa2020-02-03 17:36:07 -0800281 struct seq_file *m = st->seq;
Arjan van de Ven926e5392008-04-17 17:40:45 +0200282
Steven Price2ae27132020-02-03 17:36:24 -0800283 new_prot = val & PTE_FLAGS_MASK;
Steven Price1494e0c2020-06-01 21:49:58 -0700284 if (!val)
285 new_eff = 0;
286 else
287 new_eff = st->prot_levels[level];
Steven Price2ae27132020-02-03 17:36:24 -0800288
Arjan van de Ven926e5392008-04-17 17:40:45 +0200289 /*
290 * If we have a "break" in the series, we need to flush the state that
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200291 * we have now. "break" is either changing perms, levels or
292 * address space marker.
Arjan van de Ven926e5392008-04-17 17:40:45 +0200293 */
Steven Price2ae27132020-02-03 17:36:24 -0800294 cur = st->current_prot;
Jan Beulich672c0ae2018-02-23 01:27:37 -0700295 eff = st->effective_prot;
Arjan van de Ven926e5392008-04-17 17:40:45 +0200296
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800297 if (st->level == -1) {
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200298 /* First entry */
299 st->current_prot = new_prot;
Jan Beulich672c0ae2018-02-23 01:27:37 -0700300 st->effective_prot = new_eff;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200301 st->level = level;
302 st->marker = address_markers;
H. Peter Anvin3891a042014-04-29 16:46:09 -0700303 st->lines = 0;
Borislav Petkovef6bea62014-01-18 12:48:14 +0100304 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
305 st->marker->name);
Steven Price2ae27132020-02-03 17:36:24 -0800306 } else if (new_prot != cur || new_eff != eff || level != st->level ||
307 addr >= st->marker[1].start_address) {
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200308 const char *unit = units;
Arjan van de Ven926e5392008-04-17 17:40:45 +0200309 unsigned long delta;
Yinghai Lu6424fb32009-04-13 23:51:46 -0700310 int width = sizeof(unsigned long) * 2;
Stephen Smalleye1a58322015-10-05 12:55:20 -0400311
Thomas Gleixnerc200dac2018-10-08 21:53:48 +0200312 if (st->check_wx && (eff & _PAGE_RW) && !(eff & _PAGE_NX))
Steven Price2ae27132020-02-03 17:36:24 -0800313 note_wx(st, addr);
Arjan van de Ven926e5392008-04-17 17:40:45 +0200314
315 /*
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200316 * Now print the actual finished series
317 */
H. Peter Anvin3891a042014-04-29 16:46:09 -0700318 if (!st->marker->max_lines ||
319 st->lines < st->marker->max_lines) {
320 pt_dump_seq_printf(m, st->to_dmesg,
321 "0x%0*lx-0x%0*lx ",
322 width, st->start_address,
Steven Price2ae27132020-02-03 17:36:24 -0800323 width, addr);
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200324
Steven Price2ae27132020-02-03 17:36:24 -0800325 delta = addr - st->start_address;
H. Peter Anvin3891a042014-04-29 16:46:09 -0700326 while (!(delta & 1023) && unit[1]) {
327 delta >>= 10;
328 unit++;
329 }
330 pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
331 delta, *unit);
332 printk_prot(m, st->current_prot, st->level,
333 st->to_dmesg);
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200334 }
H. Peter Anvin3891a042014-04-29 16:46:09 -0700335 st->lines++;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200336
337 /*
Arjan van de Ven926e5392008-04-17 17:40:45 +0200338 * We print markers for special areas of address space,
339 * such as the start of vmalloc space etc.
340 * This helps in the interpretation.
341 */
Steven Price2ae27132020-02-03 17:36:24 -0800342 if (addr >= st->marker[1].start_address) {
H. Peter Anvin3891a042014-04-29 16:46:09 -0700343 if (st->marker->max_lines &&
344 st->lines > st->marker->max_lines) {
345 unsigned long nskip =
346 st->lines - st->marker->max_lines;
347 pt_dump_seq_printf(m, st->to_dmesg,
348 "... %lu entr%s skipped ... \n",
349 nskip,
350 nskip == 1 ? "y" : "ies");
351 }
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200352 st->marker++;
H. Peter Anvin3891a042014-04-29 16:46:09 -0700353 st->lines = 0;
Borislav Petkovef6bea62014-01-18 12:48:14 +0100354 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
355 st->marker->name);
Arjan van de Ven926e5392008-04-17 17:40:45 +0200356 }
357
Steven Price2ae27132020-02-03 17:36:24 -0800358 st->start_address = addr;
Arjan van de Ven926e5392008-04-17 17:40:45 +0200359 st->current_prot = new_prot;
Jan Beulich672c0ae2018-02-23 01:27:37 -0700360 st->effective_prot = new_eff;
Arjan van de Ven926e5392008-04-17 17:40:45 +0200361 st->level = level;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200362 }
Arjan van de Ven926e5392008-04-17 17:40:45 +0200363}
364
Steven Pricee47690d2020-02-03 17:36:42 -0800365static void ptdump_walk_pgd_level_core(struct seq_file *m,
366 struct mm_struct *mm, pgd_t *pgd,
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100367 bool checkwx, bool dmesg)
Arjan van de Ven926e5392008-04-17 17:40:45 +0200368{
Steven Price2ae27132020-02-03 17:36:24 -0800369 const struct ptdump_range ptdump_ranges[] = {
370#ifdef CONFIG_X86_64
Steven Price2ae27132020-02-03 17:36:24 -0800371 {0, PTRS_PER_PGD * PGD_LEVEL_MULT / 2},
Juergen Grossbba42af2020-02-21 11:38:51 +0100372 {GUARD_HOLE_END_ADDR, ~0UL},
Jan Beulich672c0ae2018-02-23 01:27:37 -0700373#else
Steven Price2ae27132020-02-03 17:36:24 -0800374 {0, ~0UL},
Jan Beulich672c0ae2018-02-23 01:27:37 -0700375#endif
Steven Price2ae27132020-02-03 17:36:24 -0800376 {0, 0}
377};
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200378
Steven Price2ae27132020-02-03 17:36:24 -0800379 struct pg_state st = {
380 .ptdump = {
381 .note_page = note_page,
Steven Price1494e0c2020-06-01 21:49:58 -0700382 .effective_prot = effective_prot,
Steven Price2ae27132020-02-03 17:36:24 -0800383 .range = ptdump_ranges
384 },
Steven Pricef8f0d0b2020-02-03 17:36:38 -0800385 .level = -1,
Steven Price2ae27132020-02-03 17:36:24 -0800386 .to_dmesg = dmesg,
387 .check_wx = checkwx,
388 .seq = m
389 };
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200390
Steven Pricee47690d2020-02-03 17:36:42 -0800391 ptdump_walk_pgd(&st.ptdump, mm, pgd);
Steven Price2ae27132020-02-03 17:36:24 -0800392
Stephen Smalleye1a58322015-10-05 12:55:20 -0400393 if (!checkwx)
394 return;
395 if (st.wx_pages)
396 pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n",
397 st.wx_pages);
398 else
399 pr_info("x86/mm: Checked W+X mappings: passed, no W+X pages found.\n");
Arjan van de Ven926e5392008-04-17 17:40:45 +0200400}
401
Steven Pricee4552482020-02-03 17:36:11 -0800402void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm)
Stephen Smalleye1a58322015-10-05 12:55:20 -0400403{
Steven Pricee47690d2020-02-03 17:36:42 -0800404 ptdump_walk_pgd_level_core(m, mm, mm->pgd, false, true);
Stephen Smalleye1a58322015-10-05 12:55:20 -0400405}
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100406
Steven Pricec5cfae12020-02-03 17:36:16 -0800407void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
408 bool user)
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100409{
Steven Pricec5cfae12020-02-03 17:36:16 -0800410 pgd_t *pgd = mm->pgd;
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +0100411#ifdef CONFIG_PAGE_TABLE_ISOLATION
Borislav Petkov28e3ace2019-03-29 20:00:38 +0100412 if (user && boot_cpu_has(X86_FEATURE_PTI))
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +0100413 pgd = kernel_to_user_pgdp(pgd);
414#endif
Steven Pricee47690d2020-02-03 17:36:42 -0800415 ptdump_walk_pgd_level_core(m, mm, pgd, false, false);
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100416}
417EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs);
418
Joerg Roedeld878efc2018-08-08 13:16:40 +0200419void ptdump_walk_user_pgd_level_checkwx(void)
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100420{
421#ifdef CONFIG_PAGE_TABLE_ISOLATION
Joerg Roedel4e8537e2018-07-18 11:41:08 +0200422 pgd_t *pgd = INIT_PGD;
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100423
Joerg Roedeld878efc2018-08-08 13:16:40 +0200424 if (!(__supported_pte_mask & _PAGE_NX) ||
Borislav Petkov28e3ace2019-03-29 20:00:38 +0100425 !boot_cpu_has(X86_FEATURE_PTI))
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100426 return;
427
428 pr_info("x86/mm: Checking user space page tables\n");
429 pgd = kernel_to_user_pgdp(pgd);
Steven Pricee47690d2020-02-03 17:36:42 -0800430 ptdump_walk_pgd_level_core(NULL, &init_mm, pgd, true, false);
Thomas Gleixnerb4bf4f92017-12-04 15:08:05 +0100431#endif
432}
Stephen Smalleye1a58322015-10-05 12:55:20 -0400433
434void ptdump_walk_pgd_level_checkwx(void)
435{
Steven Pricee47690d2020-02-03 17:36:42 -0800436 ptdump_walk_pgd_level_core(NULL, &init_mm, INIT_PGD, true, false);
Stephen Smalleye1a58322015-10-05 12:55:20 -0400437}
438
Kees Cook8609d1b2015-11-19 17:07:55 -0800439static int __init pt_dump_init(void)
Arjan van de Ven926e5392008-04-17 17:40:45 +0200440{
Thomas Garnier0483e1f2016-06-21 17:47:02 -0700441 /*
442 * Various markers are not compile-time constants, so assign them
443 * here.
444 */
445#ifdef CONFIG_X86_64
446 address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET;
447 address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
448 address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START;
Kirill A. Shutemov5c7919b2018-02-14 14:16:52 +0300449#ifdef CONFIG_MODIFY_LDT_SYSCALL
450 address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
451#endif
Kirill A. Shutemov09e61a72018-02-14 14:16:55 +0300452#ifdef CONFIG_KASAN
453 address_markers[KASAN_SHADOW_START_NR].start_address = KASAN_SHADOW_START;
454 address_markers[KASAN_SHADOW_END_NR].start_address = KASAN_SHADOW_END;
455#endif
Thomas Garnier0483e1f2016-06-21 17:47:02 -0700456#endif
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200457#ifdef CONFIG_X86_32
Andres Salomon92851e22010-07-20 15:19:46 -0700458 address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
459 address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200460# ifdef CONFIG_HIGHMEM
Andres Salomon92851e22010-07-20 15:19:46 -0700461 address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200462# endif
Andres Salomon92851e22010-07-20 15:19:46 -0700463 address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
Thomas Gleixner92a0f812017-12-20 18:51:31 +0100464 address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE;
Joerg Roedelf3e48e52018-07-18 11:41:10 +0200465# ifdef CONFIG_MODIFY_LDT_SYSCALL
466 address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
467# endif
H. Peter Anvinfe770bf02008-04-17 17:40:45 +0200468#endif
Arjan van de Ven926e5392008-04-17 17:40:45 +0200469 return 0;
470}
Arjan van de Ven926e5392008-04-17 17:40:45 +0200471__initcall(pt_dump_init);