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 | #include <asm/pgtable.h> |
| 23 | |
| 24 | /* |
| 25 | * The dumper groups pagetable entries of the same type into one, and for |
| 26 | * that it needs to keep some state when walking, and flush this state |
| 27 | * when a "break" in the continuity is found. |
| 28 | */ |
| 29 | struct pg_state { |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 30 | struct ptdump_state ptdump; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 31 | int level; |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 32 | pgprotval_t current_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 33 | pgprotval_t effective_prot; |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 34 | pgprotval_t prot_levels[5]; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 35 | unsigned long start_address; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 36 | const struct addr_marker *marker; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 37 | unsigned long lines; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 38 | bool to_dmesg; |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 39 | bool check_wx; |
| 40 | unsigned long wx_pages; |
Steven Price | 74d2aaa | 2020-02-03 17:36:07 -0800 | [diff] [blame] | 41 | struct seq_file *seq; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct addr_marker { |
| 45 | unsigned long start_address; |
| 46 | const char *name; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 47 | unsigned long max_lines; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 50 | /* Address space markers hints */ |
| 51 | |
| 52 | #ifdef CONFIG_X86_64 |
| 53 | |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 54 | enum address_markers_idx { |
| 55 | USER_SPACE_NR = 0, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 56 | KERNEL_SPACE_NR, |
Kirill A. Shutemov | 254eb55 | 2018-11-30 23:23:28 +0300 | [diff] [blame] | 57 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 58 | LDT_NR, |
| 59 | #endif |
Kirill A. Shutemov | 254eb55 | 2018-11-30 23:23:28 +0300 | [diff] [blame] | 60 | LOW_KERNEL_NR, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 61 | VMALLOC_START_NR, |
| 62 | VMEMMAP_START_NR, |
Andrey Ryabinin | 025205f | 2017-02-14 13:08:39 +0300 | [diff] [blame] | 63 | #ifdef CONFIG_KASAN |
| 64 | KASAN_SHADOW_START_NR, |
| 65 | KASAN_SHADOW_END_NR, |
| 66 | #endif |
Thomas Gleixner | f207890 | 2018-01-04 13:01:40 +0100 | [diff] [blame] | 67 | CPU_ENTRY_AREA_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 68 | #ifdef CONFIG_X86_ESPFIX64 |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 69 | ESPFIX_START_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 70 | #endif |
| 71 | #ifdef CONFIG_EFI |
| 72 | EFI_END_NR, |
| 73 | #endif |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 74 | HIGH_KERNEL_NR, |
| 75 | MODULES_VADDR_NR, |
| 76 | MODULES_END_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 77 | FIXADDR_START_NR, |
| 78 | END_OF_SPACE_NR, |
| 79 | }; |
| 80 | |
| 81 | static struct addr_marker address_markers[] = { |
| 82 | [USER_SPACE_NR] = { 0, "User Space" }, |
| 83 | [KERNEL_SPACE_NR] = { (1UL << 63), "Kernel Space" }, |
| 84 | [LOW_KERNEL_NR] = { 0UL, "Low Kernel Mapping" }, |
| 85 | [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" }, |
| 86 | [VMEMMAP_START_NR] = { 0UL, "Vmemmap" }, |
| 87 | #ifdef CONFIG_KASAN |
Kirill A. Shutemov | 09e61a7 | 2018-02-14 14:16:55 +0300 | [diff] [blame] | 88 | /* |
| 89 | * These fields get initialized with the (dynamic) |
| 90 | * KASAN_SHADOW_{START,END} values in pt_dump_init(). |
| 91 | */ |
| 92 | [KASAN_SHADOW_START_NR] = { 0UL, "KASAN shadow" }, |
| 93 | [KASAN_SHADOW_END_NR] = { 0UL, "KASAN shadow end" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 94 | #endif |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 95 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
Kirill A. Shutemov | 5c7919b | 2018-02-14 14:16:52 +0300 | [diff] [blame] | 96 | [LDT_NR] = { 0UL, "LDT remap" }, |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 97 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 98 | [CPU_ENTRY_AREA_NR] = { CPU_ENTRY_AREA_BASE,"CPU entry Area" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 99 | #ifdef CONFIG_X86_ESPFIX64 |
| 100 | [ESPFIX_START_NR] = { ESPFIX_BASE_ADDR, "ESPfix Area", 16 }, |
| 101 | #endif |
| 102 | #ifdef CONFIG_EFI |
| 103 | [EFI_END_NR] = { EFI_VA_END, "EFI Runtime Services" }, |
| 104 | #endif |
| 105 | [HIGH_KERNEL_NR] = { __START_KERNEL_map, "High Kernel Mapping" }, |
| 106 | [MODULES_VADDR_NR] = { MODULES_VADDR, "Modules" }, |
| 107 | [MODULES_END_NR] = { MODULES_END, "End Modules" }, |
| 108 | [FIXADDR_START_NR] = { FIXADDR_START, "Fixmap Area" }, |
| 109 | [END_OF_SPACE_NR] = { -1, NULL } |
| 110 | }; |
| 111 | |
Joerg Roedel | 4e8537e | 2018-07-18 11:41:08 +0200 | [diff] [blame] | 112 | #define INIT_PGD ((pgd_t *) &init_top_pgt) |
| 113 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 114 | #else /* CONFIG_X86_64 */ |
| 115 | |
| 116 | enum address_markers_idx { |
| 117 | USER_SPACE_NR = 0, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 118 | KERNEL_SPACE_NR, |
| 119 | VMALLOC_START_NR, |
| 120 | VMALLOC_END_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 121 | #ifdef CONFIG_HIGHMEM |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 122 | PKMAP_BASE_NR, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 123 | #endif |
Joerg Roedel | f3e48e5 | 2018-07-18 11:41:10 +0200 | [diff] [blame] | 124 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 125 | LDT_NR, |
| 126 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 127 | CPU_ENTRY_AREA_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 128 | FIXADDR_START_NR, |
| 129 | END_OF_SPACE_NR, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 132 | static struct addr_marker address_markers[] = { |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 133 | [USER_SPACE_NR] = { 0, "User Space" }, |
| 134 | [KERNEL_SPACE_NR] = { PAGE_OFFSET, "Kernel Mapping" }, |
| 135 | [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" }, |
| 136 | [VMALLOC_END_NR] = { 0UL, "vmalloc() End" }, |
| 137 | #ifdef CONFIG_HIGHMEM |
| 138 | [PKMAP_BASE_NR] = { 0UL, "Persistent kmap() Area" }, |
Andrey Ryabinin | 025205f | 2017-02-14 13:08:39 +0300 | [diff] [blame] | 139 | #endif |
Joerg Roedel | f3e48e5 | 2018-07-18 11:41:10 +0200 | [diff] [blame] | 140 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 141 | [LDT_NR] = { 0UL, "LDT remap" }, |
| 142 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 143 | [CPU_ENTRY_AREA_NR] = { 0UL, "CPU entry area" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 144 | [FIXADDR_START_NR] = { 0UL, "Fixmap area" }, |
| 145 | [END_OF_SPACE_NR] = { -1, NULL } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
Joerg Roedel | 4e8537e | 2018-07-18 11:41:08 +0200 | [diff] [blame] | 148 | #define INIT_PGD (swapper_pg_dir) |
| 149 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 150 | #endif /* !CONFIG_X86_64 */ |
| 151 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 152 | /* Multipliers for offsets within the PTEs */ |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 153 | #define PTE_LEVEL_MULT (PAGE_SIZE) |
| 154 | #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT) |
| 155 | #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT) |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 156 | #define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT) |
Juergen Gross | 84bbabc | 2017-04-12 16:36:34 +0200 | [diff] [blame] | 157 | #define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 158 | |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 159 | #define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \ |
| 160 | ({ \ |
| 161 | if (to_dmesg) \ |
| 162 | printk(KERN_INFO fmt, ##args); \ |
| 163 | else \ |
| 164 | if (m) \ |
| 165 | seq_printf(m, fmt, ##args); \ |
| 166 | }) |
| 167 | |
| 168 | #define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \ |
| 169 | ({ \ |
| 170 | if (to_dmesg) \ |
| 171 | printk(KERN_CONT fmt, ##args); \ |
| 172 | else \ |
| 173 | if (m) \ |
| 174 | seq_printf(m, fmt, ##args); \ |
| 175 | }) |
| 176 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 177 | /* |
| 178 | * Print a readable form of a pgprot_t to the seq_file |
| 179 | */ |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 180 | 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] | 181 | { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 182 | static const char * const level_name[] = |
Kirill A. Shutemov | 45dcd20 | 2017-07-17 01:59:48 +0300 | [diff] [blame] | 183 | { "cr3", "pgd", "p4d", "pud", "pmd", "pte" }; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 184 | |
Thomas Gleixner | c053449 | 2017-12-16 01:14:39 +0100 | [diff] [blame] | 185 | if (!(pr & _PAGE_PRESENT)) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 186 | /* Not present */ |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 187 | pt_dump_cont_printf(m, dmsg, " "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 188 | } else { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 189 | if (pr & _PAGE_USER) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 190 | pt_dump_cont_printf(m, dmsg, "USR "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 191 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 192 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 193 | if (pr & _PAGE_RW) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 194 | pt_dump_cont_printf(m, dmsg, "RW "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 195 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 196 | pt_dump_cont_printf(m, dmsg, "ro "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 197 | if (pr & _PAGE_PWT) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 198 | pt_dump_cont_printf(m, dmsg, "PWT "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 199 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 200 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 201 | if (pr & _PAGE_PCD) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 202 | pt_dump_cont_printf(m, dmsg, "PCD "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 203 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 204 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 205 | |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 206 | /* Bit 7 has a different meaning on level 3 vs 4 */ |
Kirill A. Shutemov | 45dcd20 | 2017-07-17 01:59:48 +0300 | [diff] [blame] | 207 | if (level <= 4 && pr & _PAGE_PSE) |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 208 | pt_dump_cont_printf(m, dmsg, "PSE "); |
| 209 | else |
| 210 | pt_dump_cont_printf(m, dmsg, " "); |
Kirill A. Shutemov | 45dcd20 | 2017-07-17 01:59:48 +0300 | [diff] [blame] | 211 | if ((level == 5 && pr & _PAGE_PAT) || |
| 212 | ((level == 4 || level == 3) && pr & _PAGE_PAT_LARGE)) |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 213 | pt_dump_cont_printf(m, dmsg, "PAT "); |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 214 | else |
| 215 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 216 | if (pr & _PAGE_GLOBAL) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 217 | pt_dump_cont_printf(m, dmsg, "GLB "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 218 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 219 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 220 | if (pr & _PAGE_NX) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 221 | pt_dump_cont_printf(m, dmsg, "NX "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 222 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 223 | pt_dump_cont_printf(m, dmsg, "x "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 224 | } |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 225 | 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] | 226 | } |
| 227 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 228 | static void note_wx(struct pg_state *st, unsigned long addr) |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 229 | { |
| 230 | unsigned long npages; |
| 231 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 232 | npages = (addr - st->start_address) / PAGE_SIZE; |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 233 | |
| 234 | #ifdef CONFIG_PCI_BIOS |
| 235 | /* |
| 236 | * If PCI BIOS is enabled, the PCI BIOS area is forced to WX. |
| 237 | * Inform about it, but avoid the warning. |
| 238 | */ |
| 239 | if (pcibios_enabled && st->start_address >= PAGE_OFFSET + BIOS_BEGIN && |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 240 | addr <= PAGE_OFFSET + BIOS_END) { |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 241 | pr_warn_once("x86/mm: PCI BIOS W+X mapping %lu pages\n", npages); |
| 242 | return; |
| 243 | } |
| 244 | #endif |
| 245 | /* Account the WX pages */ |
| 246 | st->wx_pages += npages; |
Thomas Gleixner | 510bb96 | 2019-04-15 10:46:07 +0200 | [diff] [blame] | 247 | WARN_ONCE(__supported_pte_mask & _PAGE_NX, |
| 248 | "x86/mm: Found insecure W+X mapping at address %pS\n", |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 249 | (void *)st->start_address); |
| 250 | } |
| 251 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 252 | static inline pgprotval_t effective_prot(pgprotval_t prot1, pgprotval_t prot2) |
| 253 | { |
| 254 | return (prot1 & prot2 & (_PAGE_USER | _PAGE_RW)) | |
| 255 | ((prot1 | prot2) & _PAGE_NX); |
| 256 | } |
| 257 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 258 | /* |
| 259 | * This function gets called on a break in a continuous series |
| 260 | * of PTE entries; the next one is different so we need to |
| 261 | * print what we collected so far. |
| 262 | */ |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 263 | static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, |
| 264 | unsigned long val) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 265 | { |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 266 | struct pg_state *st = container_of(pt_st, struct pg_state, ptdump); |
| 267 | pgprotval_t new_prot, new_eff; |
| 268 | pgprotval_t cur, eff; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 269 | static const char units[] = "BKMGTPE"; |
Steven Price | 74d2aaa | 2020-02-03 17:36:07 -0800 | [diff] [blame] | 270 | struct seq_file *m = st->seq; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 271 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 272 | new_prot = val & PTE_FLAGS_MASK; |
| 273 | |
| 274 | if (level > 1) { |
| 275 | new_eff = effective_prot(st->prot_levels[level - 2], |
| 276 | new_prot); |
| 277 | } else { |
| 278 | new_eff = new_prot; |
| 279 | } |
| 280 | |
| 281 | if (level > 0) |
| 282 | st->prot_levels[level - 1] = new_eff; |
| 283 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 284 | /* |
| 285 | * 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] | 286 | * we have now. "break" is either changing perms, levels or |
| 287 | * address space marker. |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 288 | */ |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 289 | cur = st->current_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 290 | eff = st->effective_prot; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 291 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 292 | if (!st->level) { |
| 293 | /* First entry */ |
| 294 | st->current_prot = new_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 295 | st->effective_prot = new_eff; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 296 | st->level = level; |
| 297 | st->marker = address_markers; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 298 | st->lines = 0; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 299 | pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n", |
| 300 | st->marker->name); |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 301 | } else if (new_prot != cur || new_eff != eff || level != st->level || |
| 302 | addr >= st->marker[1].start_address) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 303 | const char *unit = units; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 304 | unsigned long delta; |
Yinghai Lu | 6424fb3 | 2009-04-13 23:51:46 -0700 | [diff] [blame] | 305 | int width = sizeof(unsigned long) * 2; |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 306 | |
Thomas Gleixner | c200dac | 2018-10-08 21:53:48 +0200 | [diff] [blame] | 307 | if (st->check_wx && (eff & _PAGE_RW) && !(eff & _PAGE_NX)) |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 308 | note_wx(st, addr); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 309 | |
| 310 | /* |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 311 | * Now print the actual finished series |
| 312 | */ |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 313 | if (!st->marker->max_lines || |
| 314 | st->lines < st->marker->max_lines) { |
| 315 | pt_dump_seq_printf(m, st->to_dmesg, |
| 316 | "0x%0*lx-0x%0*lx ", |
| 317 | width, st->start_address, |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 318 | width, addr); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 319 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 320 | delta = addr - st->start_address; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 321 | while (!(delta & 1023) && unit[1]) { |
| 322 | delta >>= 10; |
| 323 | unit++; |
| 324 | } |
| 325 | pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ", |
| 326 | delta, *unit); |
| 327 | printk_prot(m, st->current_prot, st->level, |
| 328 | st->to_dmesg); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 329 | } |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 330 | st->lines++; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 331 | |
| 332 | /* |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 333 | * We print markers for special areas of address space, |
| 334 | * such as the start of vmalloc space etc. |
| 335 | * This helps in the interpretation. |
| 336 | */ |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 337 | if (addr >= st->marker[1].start_address) { |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 338 | if (st->marker->max_lines && |
| 339 | st->lines > st->marker->max_lines) { |
| 340 | unsigned long nskip = |
| 341 | st->lines - st->marker->max_lines; |
| 342 | pt_dump_seq_printf(m, st->to_dmesg, |
| 343 | "... %lu entr%s skipped ... \n", |
| 344 | nskip, |
| 345 | nskip == 1 ? "y" : "ies"); |
| 346 | } |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 347 | st->marker++; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 348 | st->lines = 0; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 349 | pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n", |
| 350 | st->marker->name); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 353 | st->start_address = addr; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 354 | st->current_prot = new_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 355 | st->effective_prot = new_eff; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 356 | st->level = level; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 357 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 360 | static void ptdump_walk_pgd_level_core(struct seq_file *m, pgd_t *pgd, |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 361 | bool checkwx, bool dmesg) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 362 | { |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 363 | const struct ptdump_range ptdump_ranges[] = { |
| 364 | #ifdef CONFIG_X86_64 |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 365 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 366 | #define normalize_addr_shift (64 - (__VIRTUAL_MASK_SHIFT + 1)) |
| 367 | #define normalize_addr(u) ((signed long)((u) << normalize_addr_shift) >> \ |
| 368 | normalize_addr_shift) |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 369 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 370 | {0, PTRS_PER_PGD * PGD_LEVEL_MULT / 2}, |
| 371 | {normalize_addr(PTRS_PER_PGD * PGD_LEVEL_MULT / 2), ~0UL}, |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 372 | #else |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 373 | {0, ~0UL}, |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 374 | #endif |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 375 | {0, 0} |
| 376 | }; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 377 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 378 | struct pg_state st = { |
| 379 | .ptdump = { |
| 380 | .note_page = note_page, |
| 381 | .range = ptdump_ranges |
| 382 | }, |
| 383 | .to_dmesg = dmesg, |
| 384 | .check_wx = checkwx, |
| 385 | .seq = m |
| 386 | }; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 387 | |
Steven Price | 2ae2713 | 2020-02-03 17:36:24 -0800 | [diff] [blame^] | 388 | struct mm_struct fake_mm = { |
| 389 | .pgd = pgd |
| 390 | }; |
| 391 | init_rwsem(&fake_mm.mmap_sem); |
| 392 | |
| 393 | ptdump_walk_pgd(&st.ptdump, &fake_mm); |
| 394 | |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 395 | if (!checkwx) |
| 396 | return; |
| 397 | if (st.wx_pages) |
| 398 | pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n", |
| 399 | st.wx_pages); |
| 400 | else |
| 401 | 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] | 402 | } |
| 403 | |
Steven Price | e455248 | 2020-02-03 17:36:11 -0800 | [diff] [blame] | 404 | 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] | 405 | { |
Steven Price | e455248 | 2020-02-03 17:36:11 -0800 | [diff] [blame] | 406 | ptdump_walk_pgd_level_core(m, mm->pgd, false, true); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 407 | } |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 408 | |
Steven Price | c5cfae1 | 2020-02-03 17:36:16 -0800 | [diff] [blame] | 409 | void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm, |
| 410 | bool user) |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 411 | { |
Steven Price | c5cfae1 | 2020-02-03 17:36:16 -0800 | [diff] [blame] | 412 | pgd_t *pgd = mm->pgd; |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 413 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
Borislav Petkov | 28e3ace | 2019-03-29 20:00:38 +0100 | [diff] [blame] | 414 | if (user && boot_cpu_has(X86_FEATURE_PTI)) |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 415 | pgd = kernel_to_user_pgdp(pgd); |
| 416 | #endif |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 417 | ptdump_walk_pgd_level_core(m, pgd, false, false); |
| 418 | } |
| 419 | EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs); |
| 420 | |
Joerg Roedel | d878efc | 2018-08-08 13:16:40 +0200 | [diff] [blame] | 421 | void ptdump_walk_user_pgd_level_checkwx(void) |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 422 | { |
| 423 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
Joerg Roedel | 4e8537e | 2018-07-18 11:41:08 +0200 | [diff] [blame] | 424 | pgd_t *pgd = INIT_PGD; |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 425 | |
Joerg Roedel | d878efc | 2018-08-08 13:16:40 +0200 | [diff] [blame] | 426 | if (!(__supported_pte_mask & _PAGE_NX) || |
Borislav Petkov | 28e3ace | 2019-03-29 20:00:38 +0100 | [diff] [blame] | 427 | !boot_cpu_has(X86_FEATURE_PTI)) |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 428 | return; |
| 429 | |
| 430 | pr_info("x86/mm: Checking user space page tables\n"); |
| 431 | pgd = kernel_to_user_pgdp(pgd); |
| 432 | ptdump_walk_pgd_level_core(NULL, pgd, true, false); |
| 433 | #endif |
| 434 | } |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 435 | |
| 436 | void ptdump_walk_pgd_level_checkwx(void) |
| 437 | { |
Steven Price | c5cfae1 | 2020-02-03 17:36:16 -0800 | [diff] [blame] | 438 | ptdump_walk_pgd_level_core(NULL, INIT_PGD, true, false); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 441 | static int __init pt_dump_init(void) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 442 | { |
Thomas Garnier | 0483e1f | 2016-06-21 17:47:02 -0700 | [diff] [blame] | 443 | /* |
| 444 | * Various markers are not compile-time constants, so assign them |
| 445 | * here. |
| 446 | */ |
| 447 | #ifdef CONFIG_X86_64 |
| 448 | address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET; |
| 449 | address_markers[VMALLOC_START_NR].start_address = VMALLOC_START; |
| 450 | address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START; |
Kirill A. Shutemov | 5c7919b | 2018-02-14 14:16:52 +0300 | [diff] [blame] | 451 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 452 | address_markers[LDT_NR].start_address = LDT_BASE_ADDR; |
| 453 | #endif |
Kirill A. Shutemov | 09e61a7 | 2018-02-14 14:16:55 +0300 | [diff] [blame] | 454 | #ifdef CONFIG_KASAN |
| 455 | address_markers[KASAN_SHADOW_START_NR].start_address = KASAN_SHADOW_START; |
| 456 | address_markers[KASAN_SHADOW_END_NR].start_address = KASAN_SHADOW_END; |
| 457 | #endif |
Thomas Garnier | 0483e1f | 2016-06-21 17:47:02 -0700 | [diff] [blame] | 458 | #endif |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 459 | #ifdef CONFIG_X86_32 |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 460 | address_markers[VMALLOC_START_NR].start_address = VMALLOC_START; |
| 461 | address_markers[VMALLOC_END_NR].start_address = VMALLOC_END; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 462 | # ifdef CONFIG_HIGHMEM |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 463 | address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 464 | # endif |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 465 | address_markers[FIXADDR_START_NR].start_address = FIXADDR_START; |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 466 | address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE; |
Joerg Roedel | f3e48e5 | 2018-07-18 11:41:10 +0200 | [diff] [blame] | 467 | # ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 468 | address_markers[LDT_NR].start_address = LDT_BASE_ADDR; |
| 469 | # endif |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 470 | #endif |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 471 | return 0; |
| 472 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 473 | __initcall(pt_dump_init); |