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