Heiko Carstens | e76e82d | 2012-10-04 14:46:12 +0200 | [diff] [blame] | 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, |
Heiko Carstens | c972cc6 | 2012-10-05 16:52:18 +0200 | [diff] [blame] | 21 | #ifdef CONFIG_64BIT |
| 22 | MODULES_NR, |
| 23 | #endif |
Heiko Carstens | e76e82d | 2012-10-04 14:46:12 +0200 | [diff] [blame] | 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"}, |
Heiko Carstens | c972cc6 | 2012-10-05 16:52:18 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_64BIT |
| 33 | [MODULES_NR] = {0, "Modules Area"}, |
| 34 | #endif |
Heiko Carstens | e76e82d | 2012-10-04 14:46:12 +0200 | [diff] [blame] | 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 | pud_t *pud; |
| 154 | int i; |
| 155 | |
| 156 | for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) { |
| 157 | st->current_address = addr; |
| 158 | pud = pud_offset(pgd, addr); |
| 159 | if (!pud_none(*pud)) |
| 160 | walk_pmd_level(m, st, pud, addr); |
| 161 | else |
| 162 | note_page(m, st, _PAGE_INVALID, 2); |
| 163 | addr += PUD_SIZE; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void walk_pgd_level(struct seq_file *m) |
| 168 | { |
| 169 | unsigned long addr = 0; |
| 170 | struct pg_state st; |
| 171 | pgd_t *pgd; |
| 172 | int i; |
| 173 | |
| 174 | memset(&st, 0, sizeof(st)); |
| 175 | for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) { |
| 176 | st.current_address = addr; |
| 177 | pgd = pgd_offset_k(addr); |
| 178 | if (!pgd_none(*pgd)) |
| 179 | walk_pud_level(m, &st, pgd, addr); |
| 180 | else |
| 181 | note_page(m, &st, _PAGE_INVALID, 1); |
| 182 | addr += PGDIR_SIZE; |
| 183 | } |
| 184 | /* Flush out the last page */ |
| 185 | st.current_address = max_addr; |
| 186 | note_page(m, &st, 0, 0); |
| 187 | } |
| 188 | |
| 189 | static int ptdump_show(struct seq_file *m, void *v) |
| 190 | { |
| 191 | walk_pgd_level(m); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int ptdump_open(struct inode *inode, struct file *filp) |
| 196 | { |
| 197 | return single_open(filp, ptdump_show, NULL); |
| 198 | } |
| 199 | |
| 200 | static const struct file_operations ptdump_fops = { |
| 201 | .open = ptdump_open, |
| 202 | .read = seq_read, |
| 203 | .llseek = seq_lseek, |
| 204 | .release = single_release, |
| 205 | }; |
| 206 | |
| 207 | static int pt_dump_init(void) |
| 208 | { |
| 209 | /* |
| 210 | * Figure out the maximum virtual address being accessible with the |
| 211 | * kernel ASCE. We need this to keep the page table walker functions |
| 212 | * from accessing non-existent entries. |
| 213 | */ |
Heiko Carstens | c972cc6 | 2012-10-05 16:52:18 +0200 | [diff] [blame] | 214 | #ifdef CONFIG_32BIT |
| 215 | max_addr = 1UL << 31; |
| 216 | #else |
Heiko Carstens | e76e82d | 2012-10-04 14:46:12 +0200 | [diff] [blame] | 217 | max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2; |
| 218 | max_addr = 1UL << (max_addr * 11 + 31); |
Heiko Carstens | c972cc6 | 2012-10-05 16:52:18 +0200 | [diff] [blame] | 219 | address_markers[MODULES_NR].start_address = MODULES_VADDR; |
Heiko Carstens | e76e82d | 2012-10-04 14:46:12 +0200 | [diff] [blame] | 220 | #endif |
| 221 | address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap; |
| 222 | address_markers[VMALLOC_NR].start_address = VMALLOC_START; |
| 223 | debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops); |
| 224 | return 0; |
| 225 | } |
| 226 | device_initcall(pt_dump_init); |