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 {
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"},
33 [MODULES_NR] = {0, "Modules Area"},
40 unsigned int current_prot;
41 unsigned long start_address;
42 unsigned long current_address;
43 const struct addr_marker *marker;
46 static void print_prot(struct seq_file *m, unsigned int pr, int level)
48 static const char * const level_name[] =
49 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
51 seq_printf(m, "%s ", level_name[level]);
52 if (pr & _PAGE_INVALID) {
56 seq_printf(m, "%s", pr & _PAGE_PROTECT ? "RO " : "RW ");
60 static void note_page(struct seq_file *m, struct pg_state *st,
61 unsigned int new_prot, int level)
63 static const char units[] = "KMGTPE";
64 int width = sizeof(unsigned long) * 2;
65 const char *unit = units;
66 unsigned int prot, cur;
70 * If we have a "break" in the series, we need to flush the state
71 * that we have now. "break" is either changing perms, levels or
72 * address space marker.
75 cur = st->current_prot;
79 st->current_prot = new_prot;
81 st->marker = address_markers;
82 seq_printf(m, "---[ %s ]---\n", st->marker->name);
83 } else if (prot != cur || level != st->level ||
84 st->current_address >= st->marker[1].start_address) {
85 /* Print the actual finished series */
86 seq_printf(m, "0x%0*lx-0x%0*lx",
87 width, st->start_address,
88 width, st->current_address);
89 delta = (st->current_address - st->start_address) >> 10;
90 while (!(delta & 0x3ff) && unit[1]) {
94 seq_printf(m, "%9lu%c ", delta, *unit);
95 print_prot(m, st->current_prot, st->level);
96 if (st->current_address >= st->marker[1].start_address) {
98 seq_printf(m, "---[ %s ]---\n", st->marker->name);
100 st->start_address = st->current_address;
101 st->current_prot = new_prot;
107 * The actual page table walker functions. In order to keep the
108 * implementation of print_prot() short, we only check and pass
109 * _PAGE_INVALID and _PAGE_PROTECT flags to note_page() if a region,
110 * segment or page table entry is invalid or read-only.
111 * After all it's just a hint that the current level being walked
112 * contains an invalid or read-only entry.
114 static void walk_pte_level(struct seq_file *m, struct pg_state *st,
115 pmd_t *pmd, unsigned long addr)
121 for (i = 0; i < PTRS_PER_PTE && addr < max_addr; i++) {
122 st->current_address = addr;
123 pte = pte_offset_kernel(pmd, addr);
124 prot = pte_val(*pte) & (_PAGE_PROTECT | _PAGE_INVALID);
125 note_page(m, st, prot, 4);
131 #define _PMD_PROT_MASK _SEGMENT_ENTRY_PROTECT
133 #define _PMD_PROT_MASK 0
136 static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
137 pud_t *pud, unsigned long addr)
143 for (i = 0; i < PTRS_PER_PMD && addr < max_addr; i++) {
144 st->current_address = addr;
145 pmd = pmd_offset(pud, addr);
146 if (!pmd_none(*pmd)) {
147 if (pmd_large(*pmd)) {
148 prot = pmd_val(*pmd) & _PMD_PROT_MASK;
149 note_page(m, st, prot, 3);
151 walk_pte_level(m, st, pmd, addr);
153 note_page(m, st, _PAGE_INVALID, 3);
159 #define _PUD_PROT_MASK _REGION3_ENTRY_RO
161 #define _PUD_PROT_MASK 0
164 static void walk_pud_level(struct seq_file *m, struct pg_state *st,
165 pgd_t *pgd, unsigned long addr)
171 for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
172 st->current_address = addr;
173 pud = pud_offset(pgd, addr);
175 if (pud_large(*pud)) {
176 prot = pud_val(*pud) & _PUD_PROT_MASK;
177 note_page(m, st, prot, 2);
179 walk_pmd_level(m, st, pud, addr);
181 note_page(m, st, _PAGE_INVALID, 2);
186 static void walk_pgd_level(struct seq_file *m)
188 unsigned long addr = 0;
193 memset(&st, 0, sizeof(st));
194 for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
195 st.current_address = addr;
196 pgd = pgd_offset_k(addr);
198 walk_pud_level(m, &st, pgd, addr);
200 note_page(m, &st, _PAGE_INVALID, 1);
203 /* Flush out the last page */
204 st.current_address = max_addr;
205 note_page(m, &st, 0, 0);
208 static int ptdump_show(struct seq_file *m, void *v)
214 static int ptdump_open(struct inode *inode, struct file *filp)
216 return single_open(filp, ptdump_show, NULL);
219 static const struct file_operations ptdump_fops = {
223 .release = single_release,
226 static int pt_dump_init(void)
229 * Figure out the maximum virtual address being accessible with the
230 * kernel ASCE. We need this to keep the page table walker functions
231 * from accessing non-existent entries.
234 max_addr = 1UL << 31;
236 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
237 max_addr = 1UL << (max_addr * 11 + 31);
238 address_markers[MODULES_NR].start_address = MODULES_VADDR;
240 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
241 address_markers[VMALLOC_NR].start_address = VMALLOC_START;
242 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
245 device_initcall(pt_dump_init);