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