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