blob: 5a46b1d7e57852ed44d17bc14aed56377041718a [file] [log] [blame]
Heiko Carstense76e82d2012-10-04 14:46:12 +02001#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
8static unsigned long max_addr;
9
10struct addr_marker {
11 unsigned long start_address;
12 const char *name;
13};
14
15enum address_markers_idx {
16 IDENTITY_NR = 0,
17 KERNEL_START_NR,
18 KERNEL_END_NR,
19 VMEMMAP_NR,
20 VMALLOC_NR,
Heiko Carstensc972cc62012-10-05 16:52:18 +020021 MODULES_NR,
Heiko Carstense76e82d2012-10-04 14:46:12 +020022};
23
24static struct addr_marker address_markers[] = {
25 [IDENTITY_NR] = {0, "Identity Mapping"},
26 [KERNEL_START_NR] = {(unsigned long)&_stext, "Kernel Image Start"},
27 [KERNEL_END_NR] = {(unsigned long)&_end, "Kernel Image End"},
28 [VMEMMAP_NR] = {0, "vmemmap Area"},
29 [VMALLOC_NR] = {0, "vmalloc Area"},
Heiko Carstensc972cc62012-10-05 16:52:18 +020030 [MODULES_NR] = {0, "Modules Area"},
Heiko Carstense76e82d2012-10-04 14:46:12 +020031 { -1, NULL }
32};
33
34struct pg_state {
35 int level;
36 unsigned int current_prot;
37 unsigned long start_address;
38 unsigned long current_address;
39 const struct addr_marker *marker;
40};
41
42static void print_prot(struct seq_file *m, unsigned int pr, int level)
43{
44 static const char * const level_name[] =
45 { "ASCE", "PGD", "PUD", "PMD", "PTE" };
46
47 seq_printf(m, "%s ", level_name[level]);
Heiko Carstens1819ed12013-02-16 11:47:27 +010048 if (pr & _PAGE_INVALID) {
Heiko Carstense76e82d2012-10-04 14:46:12 +020049 seq_printf(m, "I\n");
Heiko Carstens1819ed12013-02-16 11:47:27 +010050 return;
51 }
Martin Schwidefsky57d7f932016-03-22 10:54:24 +010052 seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
53 seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
Heiko Carstense76e82d2012-10-04 14:46:12 +020054}
55
56static void note_page(struct seq_file *m, struct pg_state *st,
57 unsigned int new_prot, int level)
58{
59 static const char units[] = "KMGTPE";
60 int width = sizeof(unsigned long) * 2;
61 const char *unit = units;
62 unsigned int prot, cur;
63 unsigned long delta;
64
65 /*
66 * If we have a "break" in the series, we need to flush the state
67 * that we have now. "break" is either changing perms, levels or
68 * address space marker.
69 */
70 prot = new_prot;
71 cur = st->current_prot;
72
73 if (!st->level) {
74 /* First entry */
75 st->current_prot = new_prot;
76 st->level = level;
77 st->marker = address_markers;
78 seq_printf(m, "---[ %s ]---\n", st->marker->name);
79 } else if (prot != cur || level != st->level ||
80 st->current_address >= st->marker[1].start_address) {
81 /* Print the actual finished series */
82 seq_printf(m, "0x%0*lx-0x%0*lx",
83 width, st->start_address,
84 width, st->current_address);
85 delta = (st->current_address - st->start_address) >> 10;
86 while (!(delta & 0x3ff) && unit[1]) {
87 delta >>= 10;
88 unit++;
89 }
90 seq_printf(m, "%9lu%c ", delta, *unit);
91 print_prot(m, st->current_prot, st->level);
92 if (st->current_address >= st->marker[1].start_address) {
93 st->marker++;
94 seq_printf(m, "---[ %s ]---\n", st->marker->name);
95 }
96 st->start_address = st->current_address;
97 st->current_prot = new_prot;
98 st->level = level;
99 }
100}
101
102/*
Martin Schwidefskye5098612013-07-23 20:57:57 +0200103 * The actual page table walker functions. In order to keep the
104 * implementation of print_prot() short, we only check and pass
105 * _PAGE_INVALID and _PAGE_PROTECT flags to note_page() if a region,
106 * segment or page table entry is invalid or read-only.
107 * After all it's just a hint that the current level being walked
108 * contains an invalid or read-only entry.
Heiko Carstense76e82d2012-10-04 14:46:12 +0200109 */
110static void walk_pte_level(struct seq_file *m, struct pg_state *st,
111 pmd_t *pmd, unsigned long addr)
112{
113 unsigned int prot;
114 pte_t *pte;
115 int i;
116
117 for (i = 0; i < PTRS_PER_PTE && addr < max_addr; i++) {
118 st->current_address = addr;
119 pte = pte_offset_kernel(pmd, addr);
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100120 prot = pte_val(*pte) &
121 (_PAGE_PROTECT | _PAGE_INVALID | _PAGE_NOEXEC);
Heiko Carstense76e82d2012-10-04 14:46:12 +0200122 note_page(m, st, prot, 4);
123 addr += PAGE_SIZE;
124 }
125}
126
127static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
128 pud_t *pud, unsigned long addr)
129{
130 unsigned int prot;
131 pmd_t *pmd;
132 int i;
133
134 for (i = 0; i < PTRS_PER_PMD && addr < max_addr; i++) {
135 st->current_address = addr;
136 pmd = pmd_offset(pud, addr);
137 if (!pmd_none(*pmd)) {
138 if (pmd_large(*pmd)) {
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100139 prot = pmd_val(*pmd) &
140 (_SEGMENT_ENTRY_PROTECT |
141 _SEGMENT_ENTRY_NOEXEC);
Heiko Carstense76e82d2012-10-04 14:46:12 +0200142 note_page(m, st, prot, 3);
143 } else
144 walk_pte_level(m, st, pmd, addr);
145 } else
146 note_page(m, st, _PAGE_INVALID, 3);
147 addr += PMD_SIZE;
148 }
149}
150
151static void walk_pud_level(struct seq_file *m, struct pg_state *st,
152 pgd_t *pgd, unsigned long addr)
153{
Heiko Carstens18da2362012-10-08 09:18:26 +0200154 unsigned int prot;
Heiko Carstense76e82d2012-10-04 14:46:12 +0200155 pud_t *pud;
156 int i;
157
158 for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
159 st->current_address = addr;
160 pud = pud_offset(pgd, addr);
161 if (!pud_none(*pud))
Heiko Carstens18da2362012-10-08 09:18:26 +0200162 if (pud_large(*pud)) {
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100163 prot = pud_val(*pud) &
164 (_REGION_ENTRY_PROTECT |
165 _REGION_ENTRY_NOEXEC);
Heiko Carstens18da2362012-10-08 09:18:26 +0200166 note_page(m, st, prot, 2);
167 } else
168 walk_pmd_level(m, st, pud, addr);
Heiko Carstense76e82d2012-10-04 14:46:12 +0200169 else
170 note_page(m, st, _PAGE_INVALID, 2);
171 addr += PUD_SIZE;
172 }
173}
174
175static void walk_pgd_level(struct seq_file *m)
176{
177 unsigned long addr = 0;
178 struct pg_state st;
179 pgd_t *pgd;
180 int i;
181
182 memset(&st, 0, sizeof(st));
183 for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
184 st.current_address = addr;
185 pgd = pgd_offset_k(addr);
186 if (!pgd_none(*pgd))
187 walk_pud_level(m, &st, pgd, addr);
188 else
189 note_page(m, &st, _PAGE_INVALID, 1);
190 addr += PGDIR_SIZE;
191 }
192 /* Flush out the last page */
193 st.current_address = max_addr;
194 note_page(m, &st, 0, 0);
195}
196
197static int ptdump_show(struct seq_file *m, void *v)
198{
199 walk_pgd_level(m);
200 return 0;
201}
202
203static int ptdump_open(struct inode *inode, struct file *filp)
204{
205 return single_open(filp, ptdump_show, NULL);
206}
207
208static const struct file_operations ptdump_fops = {
209 .open = ptdump_open,
210 .read = seq_read,
211 .llseek = seq_lseek,
212 .release = single_release,
213};
214
215static int pt_dump_init(void)
216{
217 /*
218 * Figure out the maximum virtual address being accessible with the
219 * kernel ASCE. We need this to keep the page table walker functions
220 * from accessing non-existent entries.
221 */
Heiko Carstense76e82d2012-10-04 14:46:12 +0200222 max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
223 max_addr = 1UL << (max_addr * 11 + 31);
Heiko Carstensc972cc62012-10-05 16:52:18 +0200224 address_markers[MODULES_NR].start_address = MODULES_VADDR;
Heiko Carstense76e82d2012-10-04 14:46:12 +0200225 address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
226 address_markers[VMALLOC_NR].start_address = VMALLOC_START;
227 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
228 return 0;
229}
230device_initcall(pt_dump_init);