1 #include <linux/seq_file.h> 2 #include <linux/debugfs.h> 3 #include <linux/module.h> 4 #include <linux/mm.h> 5 #include <asm/sections.h> 6 #include <asm/pgtable.h> 7 8 static unsigned long max_addr; 9 10 struct addr_marker { 11 unsigned long start_address; 12 const char *name; 13 }; 14 15 enum address_markers_idx { 16 IDENTITY_NR = 0, 17 KERNEL_START_NR, 18 KERNEL_END_NR, 19 VMEMMAP_NR, 20 VMALLOC_NR, 21 #ifdef CONFIG_64BIT 22 MODULES_NR, 23 #endif 24 }; 25 26 static struct addr_marker address_markers[] = { 27 [IDENTITY_NR] = {0, "Identity Mapping"}, 28 [KERNEL_START_NR] = {(unsigned long)&_stext, "Kernel Image Start"}, 29 [KERNEL_END_NR] = {(unsigned long)&_end, "Kernel Image End"}, 30 [VMEMMAP_NR] = {0, "vmemmap Area"}, 31 [VMALLOC_NR] = {0, "vmalloc Area"}, 32 #ifdef CONFIG_64BIT 33 [MODULES_NR] = {0, "Modules Area"}, 34 #endif 35 { -1, NULL } 36 }; 37 38 struct pg_state { 39 int level; 40 unsigned int current_prot; 41 unsigned long start_address; 42 unsigned long current_address; 43 const struct addr_marker *marker; 44 }; 45 46 static void print_prot(struct seq_file *m, unsigned int pr, int level) 47 { 48 static const char * const level_name[] = 49 { "ASCE", "PGD", "PUD", "PMD", "PTE" }; 50 51 seq_printf(m, "%s ", level_name[level]); 52 if (pr & _PAGE_INVALID) 53 seq_printf(m, "I\n"); 54 else 55 seq_printf(m, "%s\n", pr & _PAGE_RO ? "RO" : "RW"); 56 } 57 58 static void note_page(struct seq_file *m, struct pg_state *st, 59 unsigned int new_prot, int level) 60 { 61 static const char units[] = "KMGTPE"; 62 int width = sizeof(unsigned long) * 2; 63 const char *unit = units; 64 unsigned int prot, cur; 65 unsigned long delta; 66 67 /* 68 * If we have a "break" in the series, we need to flush the state 69 * that we have now. "break" is either changing perms, levels or 70 * address space marker. 71 */ 72 prot = new_prot; 73 cur = st->current_prot; 74 75 if (!st->level) { 76 /* First entry */ 77 st->current_prot = new_prot; 78 st->level = level; 79 st->marker = address_markers; 80 seq_printf(m, "---[ %s ]---\n", st->marker->name); 81 } else if (prot != cur || level != st->level || 82 st->current_address >= st->marker[1].start_address) { 83 /* Print the actual finished series */ 84 seq_printf(m, "0x%0*lx-0x%0*lx", 85 width, st->start_address, 86 width, st->current_address); 87 delta = (st->current_address - st->start_address) >> 10; 88 while (!(delta & 0x3ff) && unit[1]) { 89 delta >>= 10; 90 unit++; 91 } 92 seq_printf(m, "%9lu%c ", delta, *unit); 93 print_prot(m, st->current_prot, st->level); 94 if (st->current_address >= st->marker[1].start_address) { 95 st->marker++; 96 seq_printf(m, "---[ %s ]---\n", st->marker->name); 97 } 98 st->start_address = st->current_address; 99 st->current_prot = new_prot; 100 st->level = level; 101 } 102 } 103 104 /* 105 * The actual page table walker functions. In order to keep the implementation 106 * of print_prot() short, we only check and pass _PAGE_INVALID and _PAGE_RO 107 * flags to note_page() if a region, segment or page table entry is invalid or 108 * read-only. 109 * After all it's just a hint that the current level being walked contains an 110 * invalid or read-only entry. 111 */ 112 static void walk_pte_level(struct seq_file *m, struct pg_state *st, 113 pmd_t *pmd, unsigned long addr) 114 { 115 unsigned int prot; 116 pte_t *pte; 117 int i; 118 119 for (i = 0; i < PTRS_PER_PTE && addr < max_addr; i++) { 120 st->current_address = addr; 121 pte = pte_offset_kernel(pmd, addr); 122 prot = pte_val(*pte) & (_PAGE_RO | _PAGE_INVALID); 123 note_page(m, st, prot, 4); 124 addr += PAGE_SIZE; 125 } 126 } 127 128 static void walk_pmd_level(struct seq_file *m, struct pg_state *st, 129 pud_t *pud, unsigned long addr) 130 { 131 unsigned int prot; 132 pmd_t *pmd; 133 int i; 134 135 for (i = 0; i < PTRS_PER_PMD && addr < max_addr; i++) { 136 st->current_address = addr; 137 pmd = pmd_offset(pud, addr); 138 if (!pmd_none(*pmd)) { 139 if (pmd_large(*pmd)) { 140 prot = pmd_val(*pmd) & _SEGMENT_ENTRY_RO; 141 note_page(m, st, prot, 3); 142 } else 143 walk_pte_level(m, st, pmd, addr); 144 } else 145 note_page(m, st, _PAGE_INVALID, 3); 146 addr += PMD_SIZE; 147 } 148 } 149 150 static void walk_pud_level(struct seq_file *m, struct pg_state *st, 151 pgd_t *pgd, unsigned long addr) 152 { 153 unsigned int prot; 154 pud_t *pud; 155 int i; 156 157 for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) { 158 st->current_address = addr; 159 pud = pud_offset(pgd, addr); 160 if (!pud_none(*pud)) 161 if (pud_large(*pud)) { 162 prot = pud_val(*pud) & _PAGE_RO; 163 note_page(m, st, prot, 2); 164 } else 165 walk_pmd_level(m, st, pud, addr); 166 else 167 note_page(m, st, _PAGE_INVALID, 2); 168 addr += PUD_SIZE; 169 } 170 } 171 172 static void walk_pgd_level(struct seq_file *m) 173 { 174 unsigned long addr = 0; 175 struct pg_state st; 176 pgd_t *pgd; 177 int i; 178 179 memset(&st, 0, sizeof(st)); 180 for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) { 181 st.current_address = addr; 182 pgd = pgd_offset_k(addr); 183 if (!pgd_none(*pgd)) 184 walk_pud_level(m, &st, pgd, addr); 185 else 186 note_page(m, &st, _PAGE_INVALID, 1); 187 addr += PGDIR_SIZE; 188 } 189 /* Flush out the last page */ 190 st.current_address = max_addr; 191 note_page(m, &st, 0, 0); 192 } 193 194 static int ptdump_show(struct seq_file *m, void *v) 195 { 196 walk_pgd_level(m); 197 return 0; 198 } 199 200 static int ptdump_open(struct inode *inode, struct file *filp) 201 { 202 return single_open(filp, ptdump_show, NULL); 203 } 204 205 static const struct file_operations ptdump_fops = { 206 .open = ptdump_open, 207 .read = seq_read, 208 .llseek = seq_lseek, 209 .release = single_release, 210 }; 211 212 static int pt_dump_init(void) 213 { 214 /* 215 * Figure out the maximum virtual address being accessible with the 216 * kernel ASCE. We need this to keep the page table walker functions 217 * from accessing non-existent entries. 218 */ 219 #ifdef CONFIG_32BIT 220 max_addr = 1UL << 31; 221 #else 222 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2; 223 max_addr = 1UL << (max_addr * 11 + 31); 224 address_markers[MODULES_NR].start_address = MODULES_VADDR; 225 #endif 226 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap; 227 address_markers[VMALLOC_NR].start_address = VMALLOC_START; 228 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops); 229 return 0; 230 } 231 device_initcall(pt_dump_init); 232