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