1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2016, Rashmica Gupta, IBM Corp. 4 * 5 * This traverses the kernel pagetables and dumps the 6 * information about the used sections of memory to 7 * /sys/kernel/debug/kernel_pagetables. 8 * 9 * Derived from the arm64 implementation: 10 * Copyright (c) 2014, The Linux Foundation, Laura Abbott. 11 * (C) Copyright 2008 Intel Corporation, Arjan van de Ven. 12 */ 13 #include <linux/debugfs.h> 14 #include <linux/fs.h> 15 #include <linux/hugetlb.h> 16 #include <linux/io.h> 17 #include <linux/mm.h> 18 #include <linux/highmem.h> 19 #include <linux/sched.h> 20 #include <linux/seq_file.h> 21 #include <asm/fixmap.h> 22 #include <asm/pgtable.h> 23 #include <linux/const.h> 24 #include <asm/page.h> 25 #include <asm/pgalloc.h> 26 27 #include "ptdump.h" 28 29 #ifdef CONFIG_PPC32 30 #define KERN_VIRT_START PAGE_OFFSET 31 #endif 32 33 /* 34 * To visualise what is happening, 35 * 36 * - PTRS_PER_P** = how many entries there are in the corresponding P** 37 * - P**_SHIFT = how many bits of the address we use to index into the 38 * corresponding P** 39 * - P**_SIZE is how much memory we can access through the table - not the 40 * size of the table itself. 41 * P**={PGD, PUD, PMD, PTE} 42 * 43 * 44 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a 45 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to 46 * a page. 47 * 48 * In the case where there are only 3 levels, the PUD is folded into the 49 * PGD: every PUD has only one entry which points to the PMD. 50 * 51 * The page dumper groups page table entries of the same type into a single 52 * description. It uses pg_state to track the range information while 53 * iterating over the PTE entries. When the continuity is broken it then 54 * dumps out a description of the range - ie PTEs that are virtually contiguous 55 * with the same PTE flags are chunked together. This is to make it clear how 56 * different areas of the kernel virtual memory are used. 57 * 58 */ 59 struct pg_state { 60 struct seq_file *seq; 61 const struct addr_marker *marker; 62 unsigned long start_address; 63 unsigned long start_pa; 64 unsigned long last_pa; 65 unsigned int level; 66 u64 current_flags; 67 bool check_wx; 68 unsigned long wx_pages; 69 }; 70 71 struct addr_marker { 72 unsigned long start_address; 73 const char *name; 74 }; 75 76 static struct addr_marker address_markers[] = { 77 { 0, "Start of kernel VM" }, 78 { 0, "vmalloc() Area" }, 79 { 0, "vmalloc() End" }, 80 #ifdef CONFIG_PPC64 81 { 0, "isa I/O start" }, 82 { 0, "isa I/O end" }, 83 { 0, "phb I/O start" }, 84 { 0, "phb I/O end" }, 85 { 0, "I/O remap start" }, 86 { 0, "I/O remap end" }, 87 { 0, "vmemmap start" }, 88 #else 89 { 0, "Early I/O remap start" }, 90 { 0, "Early I/O remap end" }, 91 #ifdef CONFIG_NOT_COHERENT_CACHE 92 { 0, "Consistent mem start" }, 93 { 0, "Consistent mem end" }, 94 #endif 95 #ifdef CONFIG_HIGHMEM 96 { 0, "Highmem PTEs start" }, 97 { 0, "Highmem PTEs end" }, 98 #endif 99 { 0, "Fixmap start" }, 100 { 0, "Fixmap end" }, 101 #endif 102 #ifdef CONFIG_KASAN 103 { 0, "kasan shadow mem start" }, 104 { 0, "kasan shadow mem end" }, 105 #endif 106 { -1, NULL }, 107 }; 108 109 #define pt_dump_seq_printf(m, fmt, args...) \ 110 ({ \ 111 if (m) \ 112 seq_printf(m, fmt, ##args); \ 113 }) 114 115 #define pt_dump_seq_putc(m, c) \ 116 ({ \ 117 if (m) \ 118 seq_putc(m, c); \ 119 }) 120 121 static void dump_flag_info(struct pg_state *st, const struct flag_info 122 *flag, u64 pte, int num) 123 { 124 unsigned int i; 125 126 for (i = 0; i < num; i++, flag++) { 127 const char *s = NULL; 128 u64 val; 129 130 /* flag not defined so don't check it */ 131 if (flag->mask == 0) 132 continue; 133 /* Some 'flags' are actually values */ 134 if (flag->is_val) { 135 val = pte & flag->val; 136 if (flag->shift) 137 val = val >> flag->shift; 138 pt_dump_seq_printf(st->seq, " %s:%llx", flag->set, val); 139 } else { 140 if ((pte & flag->mask) == flag->val) 141 s = flag->set; 142 else 143 s = flag->clear; 144 if (s) 145 pt_dump_seq_printf(st->seq, " %s", s); 146 } 147 st->current_flags &= ~flag->mask; 148 } 149 if (st->current_flags != 0) 150 pt_dump_seq_printf(st->seq, " unknown flags:%llx", st->current_flags); 151 } 152 153 static void dump_addr(struct pg_state *st, unsigned long addr) 154 { 155 static const char units[] = "KMGTPE"; 156 const char *unit = units; 157 unsigned long delta; 158 159 #ifdef CONFIG_PPC64 160 #define REG "0x%016lx" 161 #else 162 #define REG "0x%08lx" 163 #endif 164 165 pt_dump_seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1); 166 if (st->start_pa == st->last_pa && st->start_address + PAGE_SIZE != addr) { 167 pt_dump_seq_printf(st->seq, "[" REG "]", st->start_pa); 168 delta = PAGE_SIZE >> 10; 169 } else { 170 pt_dump_seq_printf(st->seq, " " REG " ", st->start_pa); 171 delta = (addr - st->start_address) >> 10; 172 } 173 /* Work out what appropriate unit to use */ 174 while (!(delta & 1023) && unit[1]) { 175 delta >>= 10; 176 unit++; 177 } 178 pt_dump_seq_printf(st->seq, "%9lu%c", delta, *unit); 179 180 } 181 182 static void note_prot_wx(struct pg_state *st, unsigned long addr) 183 { 184 if (!st->check_wx) 185 return; 186 187 if (!((st->current_flags & pgprot_val(PAGE_KERNEL_X)) == pgprot_val(PAGE_KERNEL_X))) 188 return; 189 190 WARN_ONCE(1, "powerpc/mm: Found insecure W+X mapping at address %p/%pS\n", 191 (void *)st->start_address, (void *)st->start_address); 192 193 st->wx_pages += (addr - st->start_address) / PAGE_SIZE; 194 } 195 196 static void note_page(struct pg_state *st, unsigned long addr, 197 unsigned int level, u64 val) 198 { 199 u64 flag = val & pg_level[level].mask; 200 u64 pa = val & PTE_RPN_MASK; 201 202 /* At first no level is set */ 203 if (!st->level) { 204 st->level = level; 205 st->current_flags = flag; 206 st->start_address = addr; 207 st->start_pa = pa; 208 st->last_pa = pa; 209 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); 210 /* 211 * Dump the section of virtual memory when: 212 * - the PTE flags from one entry to the next differs. 213 * - we change levels in the tree. 214 * - the address is in a different section of memory and is thus 215 * used for a different purpose, regardless of the flags. 216 * - the pa of this page is not adjacent to the last inspected page 217 */ 218 } else if (flag != st->current_flags || level != st->level || 219 addr >= st->marker[1].start_address || 220 (pa != st->last_pa + PAGE_SIZE && 221 (pa != st->start_pa || st->start_pa != st->last_pa))) { 222 223 /* Check the PTE flags */ 224 if (st->current_flags) { 225 note_prot_wx(st, addr); 226 dump_addr(st, addr); 227 228 /* Dump all the flags */ 229 if (pg_level[st->level].flag) 230 dump_flag_info(st, pg_level[st->level].flag, 231 st->current_flags, 232 pg_level[st->level].num); 233 234 pt_dump_seq_putc(st->seq, '\n'); 235 } 236 237 /* 238 * Address indicates we have passed the end of the 239 * current section of virtual memory 240 */ 241 while (addr >= st->marker[1].start_address) { 242 st->marker++; 243 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); 244 } 245 st->start_address = addr; 246 st->start_pa = pa; 247 st->last_pa = pa; 248 st->current_flags = flag; 249 st->level = level; 250 } else { 251 st->last_pa = pa; 252 } 253 } 254 255 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) 256 { 257 pte_t *pte = pte_offset_kernel(pmd, 0); 258 unsigned long addr; 259 unsigned int i; 260 261 for (i = 0; i < PTRS_PER_PTE; i++, pte++) { 262 addr = start + i * PAGE_SIZE; 263 note_page(st, addr, 4, pte_val(*pte)); 264 265 } 266 } 267 268 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) 269 { 270 pmd_t *pmd = pmd_offset(pud, 0); 271 unsigned long addr; 272 unsigned int i; 273 274 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { 275 addr = start + i * PMD_SIZE; 276 if (!pmd_none(*pmd) && !pmd_is_leaf(*pmd)) 277 /* pmd exists */ 278 walk_pte(st, pmd, addr); 279 else 280 note_page(st, addr, 3, pmd_val(*pmd)); 281 } 282 } 283 284 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) 285 { 286 pud_t *pud = pud_offset(pgd, 0); 287 unsigned long addr; 288 unsigned int i; 289 290 for (i = 0; i < PTRS_PER_PUD; i++, pud++) { 291 addr = start + i * PUD_SIZE; 292 if (!pud_none(*pud) && !pud_is_leaf(*pud)) 293 /* pud exists */ 294 walk_pmd(st, pud, addr); 295 else 296 note_page(st, addr, 2, pud_val(*pud)); 297 } 298 } 299 300 static void walk_pagetables(struct pg_state *st) 301 { 302 pgd_t *pgd = pgd_offset_k(0UL); 303 unsigned int i; 304 unsigned long addr; 305 306 addr = st->start_address; 307 308 /* 309 * Traverse the linux pagetable structure and dump pages that are in 310 * the hash pagetable. 311 */ 312 for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) { 313 if (!pgd_none(*pgd) && !pgd_is_leaf(*pgd)) 314 /* pgd exists */ 315 walk_pud(st, pgd, addr); 316 else 317 note_page(st, addr, 1, pgd_val(*pgd)); 318 } 319 } 320 321 static void populate_markers(void) 322 { 323 int i = 0; 324 325 address_markers[i++].start_address = PAGE_OFFSET; 326 address_markers[i++].start_address = VMALLOC_START; 327 address_markers[i++].start_address = VMALLOC_END; 328 #ifdef CONFIG_PPC64 329 address_markers[i++].start_address = ISA_IO_BASE; 330 address_markers[i++].start_address = ISA_IO_END; 331 address_markers[i++].start_address = PHB_IO_BASE; 332 address_markers[i++].start_address = PHB_IO_END; 333 address_markers[i++].start_address = IOREMAP_BASE; 334 address_markers[i++].start_address = IOREMAP_END; 335 /* What is the ifdef about? */ 336 #ifdef CONFIG_PPC_BOOK3S_64 337 address_markers[i++].start_address = H_VMEMMAP_START; 338 #else 339 address_markers[i++].start_address = VMEMMAP_BASE; 340 #endif 341 #else /* !CONFIG_PPC64 */ 342 address_markers[i++].start_address = ioremap_bot; 343 address_markers[i++].start_address = IOREMAP_TOP; 344 #ifdef CONFIG_NOT_COHERENT_CACHE 345 address_markers[i++].start_address = IOREMAP_TOP; 346 address_markers[i++].start_address = IOREMAP_TOP + 347 CONFIG_CONSISTENT_SIZE; 348 #endif 349 #ifdef CONFIG_HIGHMEM 350 address_markers[i++].start_address = PKMAP_BASE; 351 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP); 352 #endif 353 address_markers[i++].start_address = FIXADDR_START; 354 address_markers[i++].start_address = FIXADDR_TOP; 355 #ifdef CONFIG_KASAN 356 address_markers[i++].start_address = KASAN_SHADOW_START; 357 address_markers[i++].start_address = KASAN_SHADOW_END; 358 #endif 359 #endif /* CONFIG_PPC64 */ 360 } 361 362 static int ptdump_show(struct seq_file *m, void *v) 363 { 364 struct pg_state st = { 365 .seq = m, 366 .marker = address_markers, 367 }; 368 369 if (radix_enabled()) 370 st.start_address = PAGE_OFFSET; 371 else 372 st.start_address = KERN_VIRT_START; 373 374 /* Traverse kernel page tables */ 375 walk_pagetables(&st); 376 note_page(&st, 0, 0, 0); 377 return 0; 378 } 379 380 381 static int ptdump_open(struct inode *inode, struct file *file) 382 { 383 return single_open(file, ptdump_show, NULL); 384 } 385 386 static const struct file_operations ptdump_fops = { 387 .open = ptdump_open, 388 .read = seq_read, 389 .llseek = seq_lseek, 390 .release = single_release, 391 }; 392 393 static void build_pgtable_complete_mask(void) 394 { 395 unsigned int i, j; 396 397 for (i = 0; i < ARRAY_SIZE(pg_level); i++) 398 if (pg_level[i].flag) 399 for (j = 0; j < pg_level[i].num; j++) 400 pg_level[i].mask |= pg_level[i].flag[j].mask; 401 } 402 403 #ifdef CONFIG_PPC_DEBUG_WX 404 void ptdump_check_wx(void) 405 { 406 struct pg_state st = { 407 .seq = NULL, 408 .marker = address_markers, 409 .check_wx = true, 410 }; 411 412 if (radix_enabled()) 413 st.start_address = PAGE_OFFSET; 414 else 415 st.start_address = KERN_VIRT_START; 416 417 walk_pagetables(&st); 418 419 if (st.wx_pages) 420 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", 421 st.wx_pages); 422 else 423 pr_info("Checked W+X mappings: passed, no W+X pages found\n"); 424 } 425 #endif 426 427 static int ptdump_init(void) 428 { 429 struct dentry *debugfs_file; 430 431 populate_markers(); 432 build_pgtable_complete_mask(); 433 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL, 434 NULL, &ptdump_fops); 435 return debugfs_file ? 0 : -ENOMEM; 436 } 437 device_initcall(ptdump_init); 438