Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Debug helper to dump the current kernel pagetables of the system |
| 3 | * so that we can see what the various memory ranges are set to. |
| 4 | * |
| 5 | * (C) Copyright 2008 Intel Corporation |
| 6 | * |
| 7 | * Author: Arjan van de Ven <arjan@linux.intel.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; version 2 |
| 12 | * of the License. |
| 13 | */ |
| 14 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 15 | #include <linux/debugfs.h> |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 16 | #include <linux/kasan.h> |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 17 | #include <linux/mm.h> |
Paul Gortmaker | 84e629b | 2016-07-13 20:18:54 -0400 | [diff] [blame] | 18 | #include <linux/init.h> |
Andrey Ryabinin | 146fbb76 | 2017-02-10 12:54:05 +0300 | [diff] [blame] | 19 | #include <linux/sched.h> |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 21 | |
| 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 { |
| 30 | int level; |
| 31 | pgprot_t current_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 32 | pgprotval_t effective_prot; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 33 | unsigned long start_address; |
| 34 | unsigned long current_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; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct addr_marker { |
| 43 | unsigned long start_address; |
| 44 | const char *name; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 45 | unsigned long max_lines; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 48 | /* Address space markers hints */ |
| 49 | |
| 50 | #ifdef CONFIG_X86_64 |
| 51 | |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 52 | enum address_markers_idx { |
| 53 | USER_SPACE_NR = 0, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 54 | KERNEL_SPACE_NR, |
| 55 | LOW_KERNEL_NR, |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 56 | #if defined(CONFIG_MODIFY_LDT_SYSCALL) && defined(CONFIG_X86_5LEVEL) |
| 57 | LDT_NR, |
| 58 | #endif |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 59 | VMALLOC_START_NR, |
| 60 | VMEMMAP_START_NR, |
Andrey Ryabinin | 025205f | 2017-02-14 13:08:39 +0300 | [diff] [blame] | 61 | #ifdef CONFIG_KASAN |
| 62 | KASAN_SHADOW_START_NR, |
| 63 | KASAN_SHADOW_END_NR, |
| 64 | #endif |
Thomas Gleixner | f207890 | 2018-01-04 13:01:40 +0100 | [diff] [blame] | 65 | CPU_ENTRY_AREA_NR, |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 66 | #if defined(CONFIG_MODIFY_LDT_SYSCALL) && !defined(CONFIG_X86_5LEVEL) |
| 67 | LDT_NR, |
| 68 | #endif |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 69 | #ifdef CONFIG_X86_ESPFIX64 |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 70 | ESPFIX_START_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 71 | #endif |
| 72 | #ifdef CONFIG_EFI |
| 73 | EFI_END_NR, |
| 74 | #endif |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 75 | HIGH_KERNEL_NR, |
| 76 | MODULES_VADDR_NR, |
| 77 | MODULES_END_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 78 | FIXADDR_START_NR, |
| 79 | END_OF_SPACE_NR, |
| 80 | }; |
| 81 | |
| 82 | static struct addr_marker address_markers[] = { |
| 83 | [USER_SPACE_NR] = { 0, "User Space" }, |
| 84 | [KERNEL_SPACE_NR] = { (1UL << 63), "Kernel Space" }, |
| 85 | [LOW_KERNEL_NR] = { 0UL, "Low Kernel Mapping" }, |
| 86 | [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" }, |
| 87 | [VMEMMAP_START_NR] = { 0UL, "Vmemmap" }, |
| 88 | #ifdef CONFIG_KASAN |
Kirill A. Shutemov | 09e61a7 | 2018-02-14 14:16:55 +0300 | [diff] [blame] | 89 | /* |
| 90 | * These fields get initialized with the (dynamic) |
| 91 | * KASAN_SHADOW_{START,END} values in pt_dump_init(). |
| 92 | */ |
| 93 | [KASAN_SHADOW_START_NR] = { 0UL, "KASAN shadow" }, |
| 94 | [KASAN_SHADOW_END_NR] = { 0UL, "KASAN shadow end" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 95 | #endif |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 96 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
Kirill A. Shutemov | 5c7919b | 2018-02-14 14:16:52 +0300 | [diff] [blame] | 97 | [LDT_NR] = { 0UL, "LDT remap" }, |
Andy Lutomirski | f55f050 | 2017-12-12 07:56:45 -0800 | [diff] [blame] | 98 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 99 | [CPU_ENTRY_AREA_NR] = { CPU_ENTRY_AREA_BASE,"CPU entry Area" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 100 | #ifdef CONFIG_X86_ESPFIX64 |
| 101 | [ESPFIX_START_NR] = { ESPFIX_BASE_ADDR, "ESPfix Area", 16 }, |
| 102 | #endif |
| 103 | #ifdef CONFIG_EFI |
| 104 | [EFI_END_NR] = { EFI_VA_END, "EFI Runtime Services" }, |
| 105 | #endif |
| 106 | [HIGH_KERNEL_NR] = { __START_KERNEL_map, "High Kernel Mapping" }, |
| 107 | [MODULES_VADDR_NR] = { MODULES_VADDR, "Modules" }, |
| 108 | [MODULES_END_NR] = { MODULES_END, "End Modules" }, |
| 109 | [FIXADDR_START_NR] = { FIXADDR_START, "Fixmap Area" }, |
| 110 | [END_OF_SPACE_NR] = { -1, NULL } |
| 111 | }; |
| 112 | |
| 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 |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 123 | CPU_ENTRY_AREA_NR, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 124 | FIXADDR_START_NR, |
| 125 | END_OF_SPACE_NR, |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 128 | static struct addr_marker address_markers[] = { |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 129 | [USER_SPACE_NR] = { 0, "User Space" }, |
| 130 | [KERNEL_SPACE_NR] = { PAGE_OFFSET, "Kernel Mapping" }, |
| 131 | [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" }, |
| 132 | [VMALLOC_END_NR] = { 0UL, "vmalloc() End" }, |
| 133 | #ifdef CONFIG_HIGHMEM |
| 134 | [PKMAP_BASE_NR] = { 0UL, "Persistent kmap() Area" }, |
Andrey Ryabinin | 025205f | 2017-02-14 13:08:39 +0300 | [diff] [blame] | 135 | #endif |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 136 | [CPU_ENTRY_AREA_NR] = { 0UL, "CPU entry area" }, |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 137 | [FIXADDR_START_NR] = { 0UL, "Fixmap area" }, |
| 138 | [END_OF_SPACE_NR] = { -1, NULL } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
Thomas Gleixner | 146122e | 2017-12-20 18:07:42 +0100 | [diff] [blame] | 141 | #endif /* !CONFIG_X86_64 */ |
| 142 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 143 | /* Multipliers for offsets within the PTEs */ |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 144 | #define PTE_LEVEL_MULT (PAGE_SIZE) |
| 145 | #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT) |
| 146 | #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT) |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 147 | #define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT) |
Juergen Gross | 84bbabc | 2017-04-12 16:36:34 +0200 | [diff] [blame] | 148 | #define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 149 | |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 150 | #define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \ |
| 151 | ({ \ |
| 152 | if (to_dmesg) \ |
| 153 | printk(KERN_INFO fmt, ##args); \ |
| 154 | else \ |
| 155 | if (m) \ |
| 156 | seq_printf(m, fmt, ##args); \ |
| 157 | }) |
| 158 | |
| 159 | #define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \ |
| 160 | ({ \ |
| 161 | if (to_dmesg) \ |
| 162 | printk(KERN_CONT fmt, ##args); \ |
| 163 | else \ |
| 164 | if (m) \ |
| 165 | seq_printf(m, fmt, ##args); \ |
| 166 | }) |
| 167 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 168 | /* |
| 169 | * Print a readable form of a pgprot_t to the seq_file |
| 170 | */ |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 171 | static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 172 | { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 173 | pgprotval_t pr = pgprot_val(prot); |
| 174 | static const char * const level_name[] = |
Kirill A. Shutemov | 45dcd20 | 2017-07-17 01:59:48 +0300 | [diff] [blame] | 175 | { "cr3", "pgd", "p4d", "pud", "pmd", "pte" }; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 176 | |
Thomas Gleixner | c053449 | 2017-12-16 01:14:39 +0100 | [diff] [blame] | 177 | if (!(pr & _PAGE_PRESENT)) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 178 | /* Not present */ |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 179 | pt_dump_cont_printf(m, dmsg, " "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 180 | } else { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 181 | if (pr & _PAGE_USER) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 182 | pt_dump_cont_printf(m, dmsg, "USR "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 183 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 184 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 185 | if (pr & _PAGE_RW) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 186 | pt_dump_cont_printf(m, dmsg, "RW "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 187 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 188 | pt_dump_cont_printf(m, dmsg, "ro "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 189 | if (pr & _PAGE_PWT) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 190 | pt_dump_cont_printf(m, dmsg, "PWT "); |
H. Peter Anvin | fe770bf0 | 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_PCD) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 194 | pt_dump_cont_printf(m, dmsg, "PCD "); |
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, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 197 | |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 198 | /* Bit 7 has a different meaning on level 3 vs 4 */ |
Kirill A. Shutemov | 45dcd20 | 2017-07-17 01:59:48 +0300 | [diff] [blame] | 199 | if (level <= 4 && pr & _PAGE_PSE) |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 200 | pt_dump_cont_printf(m, dmsg, "PSE "); |
| 201 | else |
| 202 | pt_dump_cont_printf(m, dmsg, " "); |
Kirill A. Shutemov | 45dcd20 | 2017-07-17 01:59:48 +0300 | [diff] [blame] | 203 | if ((level == 5 && pr & _PAGE_PAT) || |
| 204 | ((level == 4 || level == 3) && pr & _PAGE_PAT_LARGE)) |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 205 | pt_dump_cont_printf(m, dmsg, "PAT "); |
Juergen Gross | f439c429c3 | 2014-11-03 14:02:01 +0100 | [diff] [blame] | 206 | else |
| 207 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 208 | if (pr & _PAGE_GLOBAL) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 209 | pt_dump_cont_printf(m, dmsg, "GLB "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 210 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 211 | pt_dump_cont_printf(m, dmsg, " "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 212 | if (pr & _PAGE_NX) |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 213 | pt_dump_cont_printf(m, dmsg, "NX "); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 214 | else |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 215 | pt_dump_cont_printf(m, dmsg, "x "); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 216 | } |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 217 | 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] | 218 | } |
| 219 | |
| 220 | /* |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 221 | * On 64 bits, sign-extend the 48 bit address to 64 bit |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 222 | */ |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 223 | static unsigned long normalize_addr(unsigned long u) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 224 | { |
Kirill A. Shutemov | 3a366f7 | 2017-07-17 01:59:47 +0300 | [diff] [blame] | 225 | int shift; |
| 226 | if (!IS_ENABLED(CONFIG_X86_64)) |
| 227 | return u; |
| 228 | |
| 229 | shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); |
| 230 | return (signed long)(u << shift) >> shift; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
| 234 | * This function gets called on a break in a continuous series |
| 235 | * of PTE entries; the next one is different so we need to |
| 236 | * print what we collected so far. |
| 237 | */ |
| 238 | static void note_page(struct seq_file *m, struct pg_state *st, |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 239 | pgprot_t new_prot, pgprotval_t new_eff, int level) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 240 | { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 241 | pgprotval_t prot, cur, eff; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 242 | static const char units[] = "BKMGTPE"; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * 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] | 246 | * we have now. "break" is either changing perms, levels or |
| 247 | * address space marker. |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 248 | */ |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 249 | prot = pgprot_val(new_prot); |
| 250 | cur = pgprot_val(st->current_prot); |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 251 | eff = st->effective_prot; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 252 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 253 | if (!st->level) { |
| 254 | /* First entry */ |
| 255 | st->current_prot = new_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 256 | st->effective_prot = new_eff; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 257 | st->level = level; |
| 258 | st->marker = address_markers; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 259 | st->lines = 0; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 260 | pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n", |
| 261 | st->marker->name); |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 262 | } else if (prot != cur || new_eff != eff || level != st->level || |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 263 | st->current_address >= st->marker[1].start_address) { |
| 264 | const char *unit = units; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 265 | unsigned long delta; |
Yinghai Lu | 6424fb3 | 2009-04-13 23:51:46 -0700 | [diff] [blame] | 266 | int width = sizeof(unsigned long) * 2; |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 267 | |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 268 | if (st->check_wx && (eff & _PAGE_RW) && !(eff & _PAGE_NX)) { |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 269 | WARN_ONCE(1, |
| 270 | "x86/mm: Found insecure W+X mapping at address %p/%pS\n", |
| 271 | (void *)st->start_address, |
| 272 | (void *)st->start_address); |
| 273 | st->wx_pages += (st->current_address - |
| 274 | st->start_address) / PAGE_SIZE; |
| 275 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 276 | |
| 277 | /* |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 278 | * Now print the actual finished series |
| 279 | */ |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 280 | if (!st->marker->max_lines || |
| 281 | st->lines < st->marker->max_lines) { |
| 282 | pt_dump_seq_printf(m, st->to_dmesg, |
| 283 | "0x%0*lx-0x%0*lx ", |
| 284 | width, st->start_address, |
| 285 | width, st->current_address); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 286 | |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 287 | delta = st->current_address - st->start_address; |
| 288 | while (!(delta & 1023) && unit[1]) { |
| 289 | delta >>= 10; |
| 290 | unit++; |
| 291 | } |
| 292 | pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ", |
| 293 | delta, *unit); |
| 294 | printk_prot(m, st->current_prot, st->level, |
| 295 | st->to_dmesg); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 296 | } |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 297 | st->lines++; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 298 | |
| 299 | /* |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 300 | * We print markers for special areas of address space, |
| 301 | * such as the start of vmalloc space etc. |
| 302 | * This helps in the interpretation. |
| 303 | */ |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 304 | if (st->current_address >= st->marker[1].start_address) { |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 305 | if (st->marker->max_lines && |
| 306 | st->lines > st->marker->max_lines) { |
| 307 | unsigned long nskip = |
| 308 | st->lines - st->marker->max_lines; |
| 309 | pt_dump_seq_printf(m, st->to_dmesg, |
| 310 | "... %lu entr%s skipped ... \n", |
| 311 | nskip, |
| 312 | nskip == 1 ? "y" : "ies"); |
| 313 | } |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 314 | st->marker++; |
H. Peter Anvin | 3891a04 | 2014-04-29 16:46:09 -0700 | [diff] [blame] | 315 | st->lines = 0; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 316 | pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n", |
| 317 | st->marker->name); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 320 | st->start_address = st->current_address; |
| 321 | st->current_prot = new_prot; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 322 | st->effective_prot = new_eff; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 323 | st->level = level; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 324 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 327 | static inline pgprotval_t effective_prot(pgprotval_t prot1, pgprotval_t prot2) |
| 328 | { |
| 329 | return (prot1 & prot2 & (_PAGE_USER | _PAGE_RW)) | |
| 330 | ((prot1 | prot2) & _PAGE_NX); |
| 331 | } |
| 332 | |
| 333 | static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr, |
| 334 | pgprotval_t eff_in, unsigned long P) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 335 | { |
| 336 | int i; |
| 337 | pte_t *start; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 338 | pgprotval_t prot, eff; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 339 | |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 340 | start = (pte_t *)pmd_page_vaddr(addr); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 341 | for (i = 0; i < PTRS_PER_PTE; i++) { |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 342 | prot = pte_flags(*start); |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 343 | eff = effective_prot(eff_in, prot); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 344 | st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT); |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 345 | note_page(m, st, __pgprot(prot), eff, 5); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 346 | start++; |
| 347 | } |
| 348 | } |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 349 | #ifdef CONFIG_KASAN |
| 350 | |
| 351 | /* |
| 352 | * This is an optimization for KASAN=y case. Since all kasan page tables |
| 353 | * eventually point to the kasan_zero_page we could call note_page() |
| 354 | * right away without walking through lower level page tables. This saves |
| 355 | * us dozens of seconds (minutes for 5-level config) while checking for |
| 356 | * W+X mapping or reading kernel_page_tables debugfs file. |
| 357 | */ |
| 358 | static inline bool kasan_page_table(struct seq_file *m, struct pg_state *st, |
| 359 | void *pt) |
| 360 | { |
| 361 | if (__pa(pt) == __pa(kasan_zero_pmd) || |
Kirill A. Shutemov | 91f606a | 2018-02-14 21:25:41 +0300 | [diff] [blame] | 362 | (pgtable_l5_enabled && __pa(pt) == __pa(kasan_zero_p4d)) || |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 363 | __pa(pt) == __pa(kasan_zero_pud)) { |
| 364 | pgprotval_t prot = pte_flags(kasan_zero_pte[0]); |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 365 | note_page(m, st, __pgprot(prot), 0, 5); |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 366 | return true; |
| 367 | } |
| 368 | return false; |
| 369 | } |
| 370 | #else |
| 371 | static inline bool kasan_page_table(struct seq_file *m, struct pg_state *st, |
| 372 | void *pt) |
| 373 | { |
| 374 | return false; |
| 375 | } |
| 376 | #endif |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 377 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 378 | #if PTRS_PER_PMD > 1 |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 379 | |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 380 | static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr, |
| 381 | pgprotval_t eff_in, unsigned long P) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 382 | { |
| 383 | int i; |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 384 | pmd_t *start, *pmd_start; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 385 | pgprotval_t prot, eff; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 386 | |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 387 | pmd_start = start = (pmd_t *)pud_page_vaddr(addr); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 388 | for (i = 0; i < PTRS_PER_PMD; i++) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 389 | st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 390 | if (!pmd_none(*start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 391 | prot = pmd_flags(*start); |
| 392 | eff = effective_prot(eff_in, prot); |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 393 | if (pmd_large(*start) || !pmd_present(*start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 394 | note_page(m, st, __pgprot(prot), eff, 4); |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 395 | } else if (!kasan_page_table(m, st, pmd_start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 396 | walk_pte_level(m, st, *start, eff, |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 397 | P + i * PMD_LEVEL_MULT); |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 398 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 399 | } else |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 400 | note_page(m, st, __pgprot(0), 0, 4); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 401 | start++; |
| 402 | } |
| 403 | } |
| 404 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 405 | #else |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 406 | #define walk_pmd_level(m,s,a,e,p) walk_pte_level(m,s,__pmd(pud_val(a)),e,p) |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 407 | #define pud_large(a) pmd_large(__pmd(pud_val(a))) |
| 408 | #define pud_none(a) pmd_none(__pmd(pud_val(a))) |
| 409 | #endif |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 410 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 411 | #if PTRS_PER_PUD > 1 |
| 412 | |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 413 | static void walk_pud_level(struct seq_file *m, struct pg_state *st, p4d_t addr, |
| 414 | pgprotval_t eff_in, unsigned long P) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 415 | { |
| 416 | int i; |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 417 | pud_t *start, *pud_start; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 418 | pgprotval_t prot, eff; |
Andrey Ryabinin | 243b72a | 2017-02-14 13:08:38 +0300 | [diff] [blame] | 419 | pud_t *prev_pud = NULL; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 420 | |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 421 | pud_start = start = (pud_t *)p4d_page_vaddr(addr); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 422 | |
| 423 | for (i = 0; i < PTRS_PER_PUD; i++) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 424 | st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT); |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 425 | if (!pud_none(*start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 426 | prot = pud_flags(*start); |
| 427 | eff = effective_prot(eff_in, prot); |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 428 | if (pud_large(*start) || !pud_present(*start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 429 | note_page(m, st, __pgprot(prot), eff, 3); |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 430 | } else if (!kasan_page_table(m, st, pud_start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 431 | walk_pmd_level(m, st, *start, eff, |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 432 | P + i * PUD_LEVEL_MULT); |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 433 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 434 | } else |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 435 | note_page(m, st, __pgprot(0), 0, 3); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 436 | |
Andrey Ryabinin | 243b72a | 2017-02-14 13:08:38 +0300 | [diff] [blame] | 437 | prev_pud = start; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 438 | start++; |
| 439 | } |
| 440 | } |
| 441 | |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 442 | #else |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 443 | #define walk_pud_level(m,s,a,e,p) walk_pmd_level(m,s,__pud(p4d_val(a)),e,p) |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 444 | #define p4d_large(a) pud_large(__pud(p4d_val(a))) |
| 445 | #define p4d_none(a) pud_none(__pud(p4d_val(a))) |
| 446 | #endif |
| 447 | |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 448 | static void walk_p4d_level(struct seq_file *m, struct pg_state *st, pgd_t addr, |
| 449 | pgprotval_t eff_in, unsigned long P) |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 450 | { |
| 451 | int i; |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 452 | p4d_t *start, *p4d_start; |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 453 | pgprotval_t prot, eff; |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 454 | |
Kirill A. Shutemov | c65e774 | 2018-02-14 14:16:53 +0300 | [diff] [blame] | 455 | if (PTRS_PER_P4D == 1) |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 456 | return walk_pud_level(m, st, __p4d(pgd_val(addr)), eff_in, P); |
Kirill A. Shutemov | c65e774 | 2018-02-14 14:16:53 +0300 | [diff] [blame] | 457 | |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 458 | p4d_start = start = (p4d_t *)pgd_page_vaddr(addr); |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 459 | |
| 460 | for (i = 0; i < PTRS_PER_P4D; i++) { |
| 461 | st->current_address = normalize_addr(P + i * P4D_LEVEL_MULT); |
| 462 | if (!p4d_none(*start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 463 | prot = p4d_flags(*start); |
| 464 | eff = effective_prot(eff_in, prot); |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 465 | if (p4d_large(*start) || !p4d_present(*start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 466 | note_page(m, st, __pgprot(prot), eff, 2); |
Andrey Ryabinin | 04b6702 | 2017-07-24 18:25:58 +0300 | [diff] [blame] | 467 | } else if (!kasan_page_table(m, st, p4d_start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 468 | walk_pud_level(m, st, *start, eff, |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 469 | P + i * P4D_LEVEL_MULT); |
| 470 | } |
| 471 | } else |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 472 | note_page(m, st, __pgprot(0), 0, 2); |
Kirill A. Shutemov | fdd3d8c | 2017-03-28 13:48:06 +0300 | [diff] [blame] | 473 | |
| 474 | start++; |
| 475 | } |
| 476 | } |
| 477 | |
Kirill A. Shutemov | c65e774 | 2018-02-14 14:16:53 +0300 | [diff] [blame] | 478 | #define pgd_large(a) (pgtable_l5_enabled ? pgd_large(a) : p4d_large(__p4d(pgd_val(a)))) |
| 479 | #define pgd_none(a) (pgtable_l5_enabled ? pgd_none(a) : p4d_none(__p4d(pgd_val(a)))) |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 480 | |
Boris Ostrovsky | f4e342c | 2015-11-05 13:56:35 -0500 | [diff] [blame] | 481 | static inline bool is_hypervisor_range(int idx) |
| 482 | { |
Borislav Petkov | b176862 | 2016-02-18 21:00:41 +0100 | [diff] [blame] | 483 | #ifdef CONFIG_X86_64 |
Boris Ostrovsky | f4e342c | 2015-11-05 13:56:35 -0500 | [diff] [blame] | 484 | /* |
| 485 | * ffff800000000000 - ffff87ffffffffff is reserved for |
| 486 | * the hypervisor. |
| 487 | */ |
Borislav Petkov | b176862 | 2016-02-18 21:00:41 +0100 | [diff] [blame] | 488 | return (idx >= pgd_index(__PAGE_OFFSET) - 16) && |
| 489 | (idx < pgd_index(__PAGE_OFFSET)); |
Boris Ostrovsky | f4e342c | 2015-11-05 13:56:35 -0500 | [diff] [blame] | 490 | #else |
Borislav Petkov | b176862 | 2016-02-18 21:00:41 +0100 | [diff] [blame] | 491 | return false; |
Boris Ostrovsky | f4e342c | 2015-11-05 13:56:35 -0500 | [diff] [blame] | 492 | #endif |
Borislav Petkov | b176862 | 2016-02-18 21:00:41 +0100 | [diff] [blame] | 493 | } |
Boris Ostrovsky | f4e342c | 2015-11-05 13:56:35 -0500 | [diff] [blame] | 494 | |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 495 | 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] | 496 | bool checkwx, bool dmesg) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 497 | { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 498 | #ifdef CONFIG_X86_64 |
Kirill A. Shutemov | 65ade2f | 2017-06-06 14:31:27 +0300 | [diff] [blame] | 499 | pgd_t *start = (pgd_t *) &init_top_pgt; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 500 | #else |
| 501 | pgd_t *start = swapper_pg_dir; |
| 502 | #endif |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 503 | pgprotval_t prot, eff; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 504 | int i; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 505 | struct pg_state st = {}; |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 506 | |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 507 | if (pgd) { |
| 508 | start = pgd; |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 509 | st.to_dmesg = dmesg; |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 510 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 511 | |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 512 | st.check_wx = checkwx; |
| 513 | if (checkwx) |
| 514 | st.wx_pages = 0; |
| 515 | |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 516 | for (i = 0; i < PTRS_PER_PGD; i++) { |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 517 | st.current_address = normalize_addr(i * PGD_LEVEL_MULT); |
Boris Ostrovsky | f4e342c | 2015-11-05 13:56:35 -0500 | [diff] [blame] | 518 | if (!pgd_none(*start) && !is_hypervisor_range(i)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 519 | prot = pgd_flags(*start); |
| 520 | #ifdef CONFIG_X86_PAE |
| 521 | eff = _PAGE_USER | _PAGE_RW; |
| 522 | #else |
| 523 | eff = prot; |
| 524 | #endif |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 525 | if (pgd_large(*start) || !pgd_present(*start)) { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 526 | note_page(m, &st, __pgprot(prot), eff, 1); |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 527 | } else { |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 528 | walk_p4d_level(m, &st, *start, eff, |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 529 | i * PGD_LEVEL_MULT); |
Toshi Kani | da25e62 | 2015-09-17 12:24:19 -0600 | [diff] [blame] | 530 | } |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 531 | } else |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 532 | note_page(m, &st, __pgprot(0), 0, 1); |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 533 | |
Andrey Ryabinin | 146fbb76 | 2017-02-10 12:54:05 +0300 | [diff] [blame] | 534 | cond_resched(); |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 535 | start++; |
| 536 | } |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 537 | |
| 538 | /* Flush out the last page */ |
| 539 | st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT); |
Jan Beulich | 672c0ae | 2018-02-23 01:27:37 -0700 | [diff] [blame] | 540 | note_page(m, &st, __pgprot(0), 0, 0); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 541 | if (!checkwx) |
| 542 | return; |
| 543 | if (st.wx_pages) |
| 544 | pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n", |
| 545 | st.wx_pages); |
| 546 | else |
| 547 | 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] | 548 | } |
| 549 | |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 550 | void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd) |
| 551 | { |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 552 | ptdump_walk_pgd_level_core(m, pgd, false, true); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 553 | } |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 554 | |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 555 | void ptdump_walk_pgd_level_debugfs(struct seq_file *m, pgd_t *pgd, bool user) |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 556 | { |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 557 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
| 558 | if (user && static_cpu_has(X86_FEATURE_PTI)) |
| 559 | pgd = kernel_to_user_pgdp(pgd); |
| 560 | #endif |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 561 | ptdump_walk_pgd_level_core(m, pgd, false, false); |
| 562 | } |
| 563 | EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs); |
| 564 | |
| 565 | static void ptdump_walk_user_pgd_level_checkwx(void) |
| 566 | { |
| 567 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
| 568 | pgd_t *pgd = (pgd_t *) &init_top_pgt; |
| 569 | |
| 570 | if (!static_cpu_has(X86_FEATURE_PTI)) |
| 571 | return; |
| 572 | |
| 573 | pr_info("x86/mm: Checking user space page tables\n"); |
| 574 | pgd = kernel_to_user_pgdp(pgd); |
| 575 | ptdump_walk_pgd_level_core(NULL, pgd, true, false); |
| 576 | #endif |
| 577 | } |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 578 | |
| 579 | void ptdump_walk_pgd_level_checkwx(void) |
| 580 | { |
Thomas Gleixner | b4bf4f9 | 2017-12-04 15:08:05 +0100 | [diff] [blame] | 581 | ptdump_walk_pgd_level_core(NULL, NULL, true, false); |
| 582 | ptdump_walk_user_pgd_level_checkwx(); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 583 | } |
| 584 | |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 585 | static int __init pt_dump_init(void) |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 586 | { |
Thomas Garnier | 0483e1f | 2016-06-21 17:47:02 -0700 | [diff] [blame] | 587 | /* |
| 588 | * Various markers are not compile-time constants, so assign them |
| 589 | * here. |
| 590 | */ |
| 591 | #ifdef CONFIG_X86_64 |
| 592 | address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET; |
| 593 | address_markers[VMALLOC_START_NR].start_address = VMALLOC_START; |
| 594 | address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START; |
Kirill A. Shutemov | 5c7919b | 2018-02-14 14:16:52 +0300 | [diff] [blame] | 595 | #ifdef CONFIG_MODIFY_LDT_SYSCALL |
| 596 | address_markers[LDT_NR].start_address = LDT_BASE_ADDR; |
| 597 | #endif |
Kirill A. Shutemov | 09e61a7 | 2018-02-14 14:16:55 +0300 | [diff] [blame] | 598 | #ifdef CONFIG_KASAN |
| 599 | address_markers[KASAN_SHADOW_START_NR].start_address = KASAN_SHADOW_START; |
| 600 | address_markers[KASAN_SHADOW_END_NR].start_address = KASAN_SHADOW_END; |
| 601 | #endif |
Thomas Garnier | 0483e1f | 2016-06-21 17:47:02 -0700 | [diff] [blame] | 602 | #endif |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 603 | #ifdef CONFIG_X86_32 |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 604 | address_markers[VMALLOC_START_NR].start_address = VMALLOC_START; |
| 605 | address_markers[VMALLOC_END_NR].start_address = VMALLOC_END; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 606 | # ifdef CONFIG_HIGHMEM |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 607 | address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 608 | # endif |
Andres Salomon | 92851e2 | 2010-07-20 15:19:46 -0700 | [diff] [blame] | 609 | address_markers[FIXADDR_START_NR].start_address = FIXADDR_START; |
Thomas Gleixner | 92a0f81 | 2017-12-20 18:51:31 +0100 | [diff] [blame] | 610 | address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE; |
H. Peter Anvin | fe770bf0 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 611 | #endif |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 612 | return 0; |
| 613 | } |
Arjan van de Ven | 926e539 | 2008-04-17 17:40:45 +0200 | [diff] [blame] | 614 | __initcall(pt_dump_init); |