Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 2 | /* |
| 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 Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 11 | #include <linux/debugfs.h> |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 12 | #include <linux/kasan.h> |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 13 | #include <linux/mm.h> |
Paul Gortmaker | 84e629b | 2016-07-13 20:18:54 -0400 | [diff] [blame] | 14 | #include <linux/init.h> |
Andrey Ryabinin | 146fbb76 | 2017-02-10 12:54:05 +0300 | [diff] [blame] | 15 | #include <linux/sched.h> |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 16 | #include <linux/seq_file.h> |
Joerg Roedel | d6ef1f1 | 2018-04-17 15:27:16 +0200 | [diff] [blame] | 17 | #include <linux/highmem.h> |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 18 | #include <linux/pci.h> |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 19 | #include <linux/ptdump.h> |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 20 | |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 21 | #include <asm/e820/types.h> |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 22 | |
| 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 | */ |
| 28 | struct pg_state { |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 29 | struct ptdump_state ptdump; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 30 | int level; |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 31 | pgprotval_t current_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 32 | pgprotval_t effective_prot; |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 33 | pgprotval_t prot_levels[5]; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 34 | unsigned long start_address; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 35 | const struct addr_marker *marker; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 36 | unsigned long lines; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 37 | bool to_dmesg; |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 38 | bool check_wx; |
| 39 | unsigned long wx_pages; |
Steven Price | 74d2aaa | 2020-02-03 17:36:07 -0800 | [diff] [blame] | 40 | struct seq_file *seq; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct addr_marker { |
| 44 | unsigned long start_address; |
| 45 | const char *name; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 46 | unsigned long max_lines; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 49 | /* Address space markers hints */ |
| 50 | |
| 51 | #ifdef CONFIG_X86_64 |
| 52 | |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 53 | enum address_markers_idx { |
| 54 | USER_SPACE_NR = 0, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 55 | KERNEL_SPACE_NR, |
Kirill A. Shutemov | 254eb55 | 2018-11-30 23:23:28 +0300 | [diff] [blame] | 56 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 57 | LDT_NR, |
| 58 | #endif |
Kirill A. Shutemov | 254eb55 | 2018-11-30 23:23:28 +0300 | [diff] [blame] | 59 | LOW_KERNEL_NR, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 60 | VMALLOC_START_NR, |
| 61 | VMEMMAP_START_NR, |
Andrey Ryabinin | 025205f | 2017-02-14 13:08:39 +0300 | [diff] [blame] | 62 | #ifdef CONFIG_KASAN |
| 63 | KASAN_SHADOW_START_NR, |
| 64 | KASAN_SHADOW_END_NR, |
| 65 | #endif |
Thomas Gleixner | f207890 | 2018-01-04 13:01:40 +0100 | [diff] [blame] | 66 | CPU_ENTRY_AREA_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 67 | #ifdef CONFIG_X86_ESPFIX64 |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 68 | ESPFIX_START_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 69 | #endif |
| 70 | #ifdef CONFIG_EFI |
| 71 | EFI_END_NR, |
| 72 | #endif |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 73 | HIGH_KERNEL_NR, |
| 74 | MODULES_VADDR_NR, |
| 75 | MODULES_END_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 76 | FIXADDR_START_NR, |
| 77 | END_OF_SPACE_NR, |
| 78 | }; |
| 79 | |
| 80 | static 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. Shutemov | 09e61a7 | 2018-02-14 14:16:55 +0300 | [diff] [blame] | 87 | /* |
| 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 Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 93 | #endif |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 94 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
Kirill A. Shutemov | 5c7919b | 2018-02-14 14:16:52 +0300 | [diff] [blame] | 95 | [LDT_NR] = { 0UL, "LDT remap" }, |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 96 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 97 | [CPU_ENTRY_AREA_NR] = { CPU_ENTRY_AREA_BASE,"CPU entry Area" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 98 | #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 Roedel | 4e8537e | 2018-07-18 11:41:08 +0200 | [diff] [blame] | 111 | #define INIT_PGD ((pgd_t *) &init_top_pgt) |
| 112 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 113 | #else /* CONFIG_X86_64 */ |
| 114 | |
| 115 | enum address_markers_idx { |
| 116 | USER_SPACE_NR = 0, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 117 | KERNEL_SPACE_NR, |
| 118 | VMALLOC_START_NR, |
| 119 | VMALLOC_END_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 120 | #ifdef CONFIG_HIGHMEM |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 121 | PKMAP_BASE_NR, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 122 | #endif |
Joerg Roedel | f3e48e5 | 2018-07-18 11:41:10 +0200 | [diff] [blame] | 123 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 124 | LDT_NR, |
| 125 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 126 | CPU_ENTRY_AREA_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 127 | FIXADDR_START_NR, |
| 128 | END_OF_SPACE_NR, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 131 | static struct addr_marker address_markers[] = { |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 132 | [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 Ryabinin | 025205f | 2017-02-14 13:08:39 +0300 | [diff] [blame] | 138 | #endif |
Joerg Roedel | f3e48e5 | 2018-07-18 11:41:10 +0200 | [diff] [blame] | 139 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 140 | [LDT_NR] = { 0UL, "LDT remap" }, |
| 141 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 142 | [CPU_ENTRY_AREA_NR] = { 0UL, "CPU entry area" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 143 | [FIXADDR_START_NR] = { 0UL, "Fixmap area" }, |
| 144 | [END_OF_SPACE_NR] = { -1, NULL } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 145 | }; |
| 146 | |
Joerg Roedel | 4e8537e | 2018-07-18 11:41:08 +0200 | [diff] [blame] | 147 | #define INIT_PGD (swapper_pg_dir) |
| 148 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 149 | #endif /* !CONFIG_X86_64 */ |
| 150 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 151 | /* Multipliers for offsets within the PTEs */ |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 152 | #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. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 155 | #define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT) |
Juergen Gross | 84bbabc | 2017-04-12 16:36:34 +0200 | [diff] [blame] | 156 | #define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 157 | |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 158 | #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 Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 176 | /* |
| 177 | * Print a readable form of a pgprot_t to the seq_file |
| 178 | */ |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 179 | static void printk_prot(struct seq_file *m, pgprotval_t pr, int level, bool dmsg) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 180 | { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 181 | static const char * const level_name[] = |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 182 | { "pgd", "p4d", "pud", "pmd", "pte" }; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 183 | |
Thomas Gleixner | c053449 | 2017-12-16 01:14:39 +0100 | [diff] [blame] | 184 | if (!(pr & _PAGE_PRESENT)) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 185 | /* Not present */ |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 186 | pt_dump_cont_printf(m, dmsg, " "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 187 | } else { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 188 | if (pr & _PAGE_USER) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 189 | pt_dump_cont_printf(m, dmsg, "USR "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 190 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 191 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 192 | if (pr & _PAGE_RW) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 193 | pt_dump_cont_printf(m, dmsg, "RW "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 194 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 195 | pt_dump_cont_printf(m, dmsg, "ro "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 196 | if (pr & _PAGE_PWT) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 197 | pt_dump_cont_printf(m, dmsg, "PWT "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 198 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 199 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 200 | if (pr & _PAGE_PCD) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 201 | pt_dump_cont_printf(m, dmsg, "PCD "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 202 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 203 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 204 | |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 205 | /* Bit 7 has a different meaning on level 3 vs 4 */ |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 206 | if (level <= 3 && pr & _PAGE_PSE) |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 207 | pt_dump_cont_printf(m, dmsg, "PSE "); |
| 208 | else |
| 209 | pt_dump_cont_printf(m, dmsg, " "); |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 210 | if ((level == 4 && pr & _PAGE_PAT) || |
| 211 | ((level == 3 || level == 2) && pr & _PAGE_PAT_LARGE)) |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 212 | pt_dump_cont_printf(m, dmsg, "PAT "); |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 213 | else |
| 214 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 215 | if (pr & _PAGE_GLOBAL) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 216 | pt_dump_cont_printf(m, dmsg, "GLB "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 217 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 218 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 219 | if (pr & _PAGE_NX) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 220 | pt_dump_cont_printf(m, dmsg, "NX "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 221 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 222 | pt_dump_cont_printf(m, dmsg, "x "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 223 | } |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 224 | pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 227 | static void note_wx(struct pg_state *st, unsigned long addr) |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 228 | { |
| 229 | unsigned long npages; |
| 230 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 231 | npages = (addr - st->start_address) / PAGE_SIZE; |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 232 | |
| 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 Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 239 | addr <= PAGE_OFFSET + BIOS_END) { |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 240 | 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 Gleixner | 510bb96 | 2019-04-15 10:46:07 +0200 | [diff] [blame] | 246 | WARN_ONCE(__supported_pte_mask & _PAGE_NX, |
| 247 | "x86/mm: Found insecure W+X mapping at address %pS\n", |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 248 | (void *)st->start_address); |
| 249 | } |
| 250 | |
Steven Price | 1494e0c | 2020-06-01 21:49:58 -0700 | [diff] [blame] | 251 | static void effective_prot(struct ptdump_state *pt_st, int level, u64 val) |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 252 | { |
Steven Price | 1494e0c | 2020-06-01 21:49:58 -0700 | [diff] [blame] | 253 | 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 Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 269 | /* |
| 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 Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 274 | static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, |
Steven Price | 99395ee | 2020-06-01 21:50:01 -0700 | [diff] [blame] | 275 | u64 val) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 276 | { |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 277 | 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 Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 280 | static const char units[] = "BKMGTPE"; |
Steven Price | 74d2aaa | 2020-02-03 17:36:07 -0800 | [diff] [blame] | 281 | struct seq_file *m = st->seq; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 282 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 283 | new_prot = val & PTE_FLAGS_MASK; |
Steven Price | 1494e0c | 2020-06-01 21:49:58 -0700 | [diff] [blame] | 284 | if (!val) |
| 285 | new_eff = 0; |
| 286 | else |
| 287 | new_eff = st->prot_levels[level]; |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 288 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 289 | /* |
| 290 | * If we have a "break" in the series, we need to flush the state that |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 291 | * we have now. "break" is either changing perms, levels or |
| 292 | * address space marker. |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 293 | */ |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 294 | cur = st->current_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 295 | eff = st->effective_prot; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 296 | |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 297 | if (st->level == -1) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 298 | /* First entry */ |
| 299 | st->current_prot = new_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 300 | st->effective_prot = new_eff; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 301 | st->level = level; |
| 302 | st->marker = address_markers; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 303 | st->lines = 0; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 304 | pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n", |
| 305 | st->marker->name); |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 306 | } else if (new_prot != cur || new_eff != eff || level != st->level || |
| 307 | addr >= st->marker[1].start_address) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 308 | const char *unit = units; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 309 | unsigned long delta; |
Yinghai Lu | 6424fb3 | 2009-04-13 23:51:46 -0700 | [diff] [blame] | 310 | int width = sizeof(unsigned long) * 2; |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 311 | |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 312 | if (st->check_wx && (eff & _PAGE_RW) && !(eff & _PAGE_NX)) |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 313 | note_wx(st, addr); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 314 | |
| 315 | /* |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 316 | * Now print the actual finished series |
| 317 | */ |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 318 | 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 Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 323 | width, addr); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 324 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 325 | delta = addr - st->start_address; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 326 | 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 Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 334 | } |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 335 | st->lines++; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 336 | |
| 337 | /* |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 338 | * 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 Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 342 | if (addr >= st->marker[1].start_address) { |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 343 | 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 Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 352 | st->marker++; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 353 | st->lines = 0; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 354 | pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n", |
| 355 | st->marker->name); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 358 | st->start_address = addr; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 359 | st->current_prot = new_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 360 | st->effective_prot = new_eff; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 361 | st->level = level; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 362 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 365 | static void ptdump_walk_pgd_level_core(struct seq_file *m, |
| 366 | struct mm_struct *mm, pgd_t *pgd, |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 367 | bool checkwx, bool dmesg) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 368 | { |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 369 | const struct ptdump_range ptdump_ranges[] = { |
| 370 | #ifdef CONFIG_X86_64 |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 371 | {0, PTRS_PER_PGD * PGD_LEVEL_MULT / 2}, |
Juergen Gross | bba42af | 2020-02-21 11:38:51 +0100 | [diff] [blame] | 372 | {GUARD_HOLE_END_ADDR, ~0UL}, |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 373 | #else |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 374 | {0, ~0UL}, |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 375 | #endif |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 376 | {0, 0} |
| 377 | }; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 378 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 379 | struct pg_state st = { |
| 380 | .ptdump = { |
| 381 | .note_page = note_page, |
Steven Price | 1494e0c | 2020-06-01 21:49:58 -0700 | [diff] [blame] | 382 | .effective_prot = effective_prot, |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 383 | .range = ptdump_ranges |
| 384 | }, |
Steven Price | f8f0d0b | 2020-02-03 17:36:38 -0800 | [diff] [blame] | 385 | .level = -1, |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 386 | .to_dmesg = dmesg, |
| 387 | .check_wx = checkwx, |
| 388 | .seq = m |
| 389 | }; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 390 | |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 391 | ptdump_walk_pgd(&st.ptdump, mm, pgd); |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame] | 392 | |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 393 | 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 Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Steven Price | e455248 | 2020-02-03 17:36:11 -0800 | [diff] [blame] | 402 | void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm) |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 403 | { |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 404 | ptdump_walk_pgd_level_core(m, mm, mm->pgd, false, true); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 405 | } |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 406 | |
Steven Price | c5cfae1 | 2020-02-03 17:36:16 -0800 | [diff] [blame] | 407 | void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm, |
| 408 | bool user) |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 409 | { |
Steven Price | c5cfae1 | 2020-02-03 17:36:16 -0800 | [diff] [blame] | 410 | pgd_t *pgd = mm->pgd; |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 411 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
Borislav Petkov | 28e3ace | 2019-03-29 20:00:38 +0100 | [diff] [blame] | 412 | if (user && boot_cpu_has(X86_FEATURE_PTI)) |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 413 | pgd = kernel_to_user_pgdp(pgd); |
| 414 | #endif |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 415 | ptdump_walk_pgd_level_core(m, mm, pgd, false, false); |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 416 | } |
| 417 | EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs); |
| 418 | |
Joerg Roedel | d878efc | 2018-08-08 13:16:40 +0200 | [diff] [blame] | 419 | void ptdump_walk_user_pgd_level_checkwx(void) |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 420 | { |
| 421 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
Joerg Roedel | 4e8537e | 2018-07-18 11:41:08 +0200 | [diff] [blame] | 422 | pgd_t *pgd = INIT_PGD; |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 423 | |
Joerg Roedel | d878efc | 2018-08-08 13:16:40 +0200 | [diff] [blame] | 424 | if (!(__supported_pte_mask & _PAGE_NX) || |
Borislav Petkov | 28e3ace | 2019-03-29 20:00:38 +0100 | [diff] [blame] | 425 | !boot_cpu_has(X86_FEATURE_PTI)) |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 426 | return; |
| 427 | |
| 428 | pr_info("x86/mm: Checking user space page tables\n"); |
| 429 | pgd = kernel_to_user_pgdp(pgd); |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 430 | ptdump_walk_pgd_level_core(NULL, &init_mm, pgd, true, false); |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 431 | #endif |
| 432 | } |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 433 | |
| 434 | void ptdump_walk_pgd_level_checkwx(void) |
| 435 | { |
Steven Price | e47690d | 2020-02-03 17:36:42 -0800 | [diff] [blame] | 436 | ptdump_walk_pgd_level_core(NULL, &init_mm, INIT_PGD, true, false); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 439 | static int __init pt_dump_init(void) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 440 | { |
Thomas Garnier | 0483e1f | 2016-06-21 17:47:02 -0700 | [diff] [blame] | 441 | /* |
| 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. Shutemov | 5c7919b | 2018-02-14 14:16:52 +0300 | [diff] [blame] | 449 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 450 | address_markers[LDT_NR].start_address = LDT_BASE_ADDR; |
| 451 | #endif |
Kirill A. Shutemov | 09e61a7 | 2018-02-14 14:16:55 +0300 | [diff] [blame] | 452 | #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 Garnier | 0483e1f | 2016-06-21 17:47:02 -0700 | [diff] [blame] | 456 | #endif |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 457 | #ifdef CONFIG_X86_32 |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 458 | address_markers[VMALLOC_START_NR].start_address = VMALLOC_START; |
| 459 | address_markers[VMALLOC_END_NR].start_address = VMALLOC_END; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 460 | # ifdef CONFIG_HIGHMEM |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 461 | address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 462 | # endif |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 463 | address_markers[FIXADDR_START_NR].start_address = FIXADDR_START; |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 464 | address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE; |
Joerg Roedel | f3e48e5 | 2018-07-18 11:41:10 +0200 | [diff] [blame] | 465 | # ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 466 | address_markers[LDT_NR].start_address = LDT_BASE_ADDR; |
| 467 | # endif |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 468 | #endif |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 469 | return 0; |
| 470 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 471 | __initcall(pt_dump_init); |