1 #include <linux/seq_file.h>
2 #include <linux/debugfs.h>
3 #include <linux/module.h>
5 #include <asm/sections.h>
6 #include <asm/pgtable.h>
8 static unsigned long max_addr;
11 unsigned long start_address;
15 enum address_markers_idx {
23 static struct addr_marker address_markers[] = {
24 [IDENTITY_NR] = {0, "Identity Mapping"},
25 [KERNEL_START_NR] = {(unsigned long)&_stext, "Kernel Image Start"},
26 [KERNEL_END_NR] = {(unsigned long)&_end, "Kernel Image End"},
27 [VMEMMAP_NR] = {0, "vmemmap Area"},
28 [VMALLOC_NR] = {0, "vmalloc Area"},
34 unsigned int current_prot;
35 unsigned long start_address;
36 unsigned long current_address;
37 const struct addr_marker *marker;
40 static void print_prot(struct seq_file *m, unsigned int pr, int level)
42 static const char * const level_name[] =
43 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
45 seq_printf(m, "%s ", level_name[level]);
46 if (pr & _PAGE_INVALID)
49 seq_printf(m, "%s\n", pr & _PAGE_RO ? "RO" : "RW");
52 static void note_page(struct seq_file *m, struct pg_state *st,
53 unsigned int new_prot, int level)
55 static const char units[] = "KMGTPE";
56 int width = sizeof(unsigned long) * 2;
57 const char *unit = units;
58 unsigned int prot, cur;
62 * If we have a "break" in the series, we need to flush the state
63 * that we have now. "break" is either changing perms, levels or
64 * address space marker.
67 cur = st->current_prot;
71 st->current_prot = new_prot;
73 st->marker = address_markers;
74 seq_printf(m, "---[ %s ]---\n", st->marker->name);
75 } else if (prot != cur || level != st->level ||
76 st->current_address >= st->marker[1].start_address) {
77 /* Print the actual finished series */
78 seq_printf(m, "0x%0*lx-0x%0*lx",
79 width, st->start_address,
80 width, st->current_address);
81 delta = (st->current_address - st->start_address) >> 10;
82 while (!(delta & 0x3ff) && unit[1]) {
86 seq_printf(m, "%9lu%c ", delta, *unit);
87 print_prot(m, st->current_prot, st->level);
88 if (st->current_address >= st->marker[1].start_address) {
90 seq_printf(m, "---[ %s ]---\n", st->marker->name);
92 st->start_address = st->current_address;
93 st->current_prot = new_prot;
99 * The actual page table walker functions. In order to keep the implementation
100 * of print_prot() short, we only check and pass _PAGE_INVALID and _PAGE_RO
101 * flags to note_page() if a region, segment or page table entry is invalid or
103 * After all it's just a hint that the current level being walked contains an
104 * invalid or read-only entry.
106 static void walk_pte_level(struct seq_file *m, struct pg_state *st,
107 pmd_t *pmd, unsigned long addr)
113 for (i = 0; i < PTRS_PER_PTE && addr < max_addr; i++) {
114 st->current_address = addr;
115 pte = pte_offset_kernel(pmd, addr);
116 prot = pte_val(*pte) & (_PAGE_RO | _PAGE_INVALID);
117 note_page(m, st, prot, 4);
122 static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
123 pud_t *pud, unsigned long addr)
129 for (i = 0; i < PTRS_PER_PMD && addr < max_addr; i++) {
130 st->current_address = addr;
131 pmd = pmd_offset(pud, addr);
132 if (!pmd_none(*pmd)) {
133 if (pmd_large(*pmd)) {
134 prot = pmd_val(*pmd) & _SEGMENT_ENTRY_RO;
135 note_page(m, st, prot, 3);
137 walk_pte_level(m, st, pmd, addr);
139 note_page(m, st, _PAGE_INVALID, 3);
144 static void walk_pud_level(struct seq_file *m, struct pg_state *st,
145 pgd_t *pgd, unsigned long addr)
150 for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
151 st->current_address = addr;
152 pud = pud_offset(pgd, addr);
154 walk_pmd_level(m, st, pud, addr);
156 note_page(m, st, _PAGE_INVALID, 2);
161 static void walk_pgd_level(struct seq_file *m)
163 unsigned long addr = 0;
168 memset(&st, 0, sizeof(st));
169 for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
170 st.current_address = addr;
171 pgd = pgd_offset_k(addr);
173 walk_pud_level(m, &st, pgd, addr);
175 note_page(m, &st, _PAGE_INVALID, 1);
178 /* Flush out the last page */
179 st.current_address = max_addr;
180 note_page(m, &st, 0, 0);
183 static int ptdump_show(struct seq_file *m, void *v)
189 static int ptdump_open(struct inode *inode, struct file *filp)
191 return single_open(filp, ptdump_show, NULL);
194 static const struct file_operations ptdump_fops = {
198 .release = single_release,
201 static int pt_dump_init(void)
204 * Figure out the maximum virtual address being accessible with the
205 * kernel ASCE. We need this to keep the page table walker functions
206 * from accessing non-existent entries.
209 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
210 max_addr = 1UL << (max_addr * 11 + 31);
212 max_addr = 1UL << 31;
214 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
215 address_markers[VMALLOC_NR].start_address = VMALLOC_START;
216 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
219 device_initcall(pt_dump_init);