1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/seq_file.h>
3 #include <linux/debugfs.h>
4 #include <linux/sched.h>
6 #include <asm/sections.h>
7 #include <asm/pgtable.h>
9 static unsigned long max_addr;
12 unsigned long start_address;
16 enum address_markers_idx {
25 static struct addr_marker address_markers[] = {
26 [IDENTITY_NR] = {0, "Identity Mapping"},
27 [KERNEL_START_NR] = {(unsigned long)&_stext, "Kernel Image Start"},
28 [KERNEL_END_NR] = {(unsigned long)&_end, "Kernel Image End"},
29 [VMEMMAP_NR] = {0, "vmemmap Area"},
30 [VMALLOC_NR] = {0, "vmalloc Area"},
31 [MODULES_NR] = {0, "Modules Area"},
37 unsigned int current_prot;
38 unsigned long start_address;
39 unsigned long current_address;
40 const struct addr_marker *marker;
43 static void print_prot(struct seq_file *m, unsigned int pr, int level)
45 static const char * const level_name[] =
46 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
48 seq_printf(m, "%s ", level_name[level]);
49 if (pr & _PAGE_INVALID) {
53 seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
54 seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
57 static void note_page(struct seq_file *m, struct pg_state *st,
58 unsigned int new_prot, int level)
60 static const char units[] = "KMGTPE";
61 int width = sizeof(unsigned long) * 2;
62 const char *unit = units;
63 unsigned int prot, cur;
67 * If we have a "break" in the series, we need to flush the state
68 * that we have now. "break" is either changing perms, levels or
69 * address space marker.
72 cur = st->current_prot;
76 st->current_prot = new_prot;
78 st->marker = address_markers;
79 seq_printf(m, "---[ %s ]---\n", st->marker->name);
80 } else if (prot != cur || level != st->level ||
81 st->current_address >= st->marker[1].start_address) {
82 /* Print the actual finished series */
83 seq_printf(m, "0x%0*lx-0x%0*lx",
84 width, st->start_address,
85 width, st->current_address);
86 delta = (st->current_address - st->start_address) >> 10;
87 while (!(delta & 0x3ff) && unit[1]) {
91 seq_printf(m, "%9lu%c ", delta, *unit);
92 print_prot(m, st->current_prot, st->level);
93 if (st->current_address >= st->marker[1].start_address) {
95 seq_printf(m, "---[ %s ]---\n", st->marker->name);
97 st->start_address = st->current_address;
98 st->current_prot = new_prot;
104 * The actual page table walker functions. In order to keep the
105 * implementation of print_prot() short, we only check and pass
106 * _PAGE_INVALID and _PAGE_PROTECT flags to note_page() if a region,
107 * segment or page table entry is invalid or read-only.
108 * After all it's just a hint that the current level being walked
109 * contains an invalid or read-only entry.
111 static void walk_pte_level(struct seq_file *m, struct pg_state *st,
112 pmd_t *pmd, unsigned long addr)
118 for (i = 0; i < PTRS_PER_PTE && addr < max_addr; i++) {
119 st->current_address = addr;
120 pte = pte_offset_kernel(pmd, addr);
121 prot = pte_val(*pte) &
122 (_PAGE_PROTECT | _PAGE_INVALID | _PAGE_NOEXEC);
123 note_page(m, st, prot, 4);
128 static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
129 pud_t *pud, unsigned long addr)
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) &
141 (_SEGMENT_ENTRY_PROTECT |
142 _SEGMENT_ENTRY_NOEXEC);
143 note_page(m, st, prot, 3);
145 walk_pte_level(m, st, pmd, addr);
147 note_page(m, st, _PAGE_INVALID, 3);
152 static void walk_pud_level(struct seq_file *m, struct pg_state *st,
153 p4d_t *p4d, unsigned long addr)
159 for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
160 st->current_address = addr;
161 pud = pud_offset(p4d, addr);
163 if (pud_large(*pud)) {
164 prot = pud_val(*pud) &
165 (_REGION_ENTRY_PROTECT |
166 _REGION_ENTRY_NOEXEC);
167 note_page(m, st, prot, 2);
169 walk_pmd_level(m, st, pud, addr);
171 note_page(m, st, _PAGE_INVALID, 2);
176 static void walk_p4d_level(struct seq_file *m, struct pg_state *st,
177 pgd_t *pgd, unsigned long addr)
182 for (i = 0; i < PTRS_PER_P4D && addr < max_addr; i++) {
183 st->current_address = addr;
184 p4d = p4d_offset(pgd, addr);
186 walk_pud_level(m, st, p4d, addr);
188 note_page(m, st, _PAGE_INVALID, 2);
193 static void walk_pgd_level(struct seq_file *m)
195 unsigned long addr = 0;
200 memset(&st, 0, sizeof(st));
201 for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
202 st.current_address = addr;
203 pgd = pgd_offset_k(addr);
205 walk_p4d_level(m, &st, pgd, addr);
207 note_page(m, &st, _PAGE_INVALID, 1);
211 /* Flush out the last page */
212 st.current_address = max_addr;
213 note_page(m, &st, 0, 0);
216 static int ptdump_show(struct seq_file *m, void *v)
222 static int ptdump_open(struct inode *inode, struct file *filp)
224 return single_open(filp, ptdump_show, NULL);
227 static const struct file_operations ptdump_fops = {
231 .release = single_release,
234 static int pt_dump_init(void)
237 * Figure out the maximum virtual address being accessible with the
238 * kernel ASCE. We need this to keep the page table walker functions
239 * from accessing non-existent entries.
241 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
242 max_addr = 1UL << (max_addr * 11 + 31);
243 address_markers[MODULES_NR].start_address = MODULES_VADDR;
244 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
245 address_markers[VMALLOC_NR].start_address = VMALLOC_START;
246 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
249 device_initcall(pt_dump_init);