xref: /openbmc/linux/arch/s390/mm/dump_pagetables.c (revision c819e2cf)
1 #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 
8 static unsigned long max_addr;
9 
10 struct addr_marker {
11 	unsigned long start_address;
12 	const char *name;
13 };
14 
15 enum address_markers_idx {
16 	IDENTITY_NR = 0,
17 	KERNEL_START_NR,
18 	KERNEL_END_NR,
19 	VMEMMAP_NR,
20 	VMALLOC_NR,
21 #ifdef CONFIG_64BIT
22 	MODULES_NR,
23 #endif
24 };
25 
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"},
32 #ifdef CONFIG_64BIT
33 	[MODULES_NR]	  = {0, "Modules Area"},
34 #endif
35 	{ -1, NULL }
36 };
37 
38 struct pg_state {
39 	int level;
40 	unsigned int current_prot;
41 	unsigned long start_address;
42 	unsigned long current_address;
43 	const struct addr_marker *marker;
44 };
45 
46 static void print_prot(struct seq_file *m, unsigned int pr, int level)
47 {
48 	static const char * const level_name[] =
49 		{ "ASCE", "PGD", "PUD", "PMD", "PTE" };
50 
51 	seq_printf(m, "%s ", level_name[level]);
52 	if (pr & _PAGE_INVALID) {
53 		seq_printf(m, "I\n");
54 		return;
55 	}
56 	seq_printf(m, "%s", pr & _PAGE_PROTECT ? "RO " : "RW ");
57 	seq_putc(m, '\n');
58 }
59 
60 static void note_page(struct seq_file *m, struct pg_state *st,
61 		     unsigned int new_prot, int level)
62 {
63 	static const char units[] = "KMGTPE";
64 	int width = sizeof(unsigned long) * 2;
65 	const char *unit = units;
66 	unsigned int prot, cur;
67 	unsigned long delta;
68 
69 	/*
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.
73 	 */
74 	prot = new_prot;
75 	cur = st->current_prot;
76 
77 	if (!st->level) {
78 		/* First entry */
79 		st->current_prot = new_prot;
80 		st->level = level;
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]) {
91 			delta >>= 10;
92 			unit++;
93 		}
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) {
97 			st->marker++;
98 			seq_printf(m, "---[ %s ]---\n", st->marker->name);
99 		}
100 		st->start_address = st->current_address;
101 		st->current_prot = new_prot;
102 		st->level = level;
103 	}
104 }
105 
106 /*
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.
113  */
114 static void walk_pte_level(struct seq_file *m, struct pg_state *st,
115 			   pmd_t *pmd, unsigned long addr)
116 {
117 	unsigned int prot;
118 	pte_t *pte;
119 	int i;
120 
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);
126 		addr += PAGE_SIZE;
127 	}
128 }
129 
130 #ifdef CONFIG_64BIT
131 #define _PMD_PROT_MASK _SEGMENT_ENTRY_PROTECT
132 #else
133 #define _PMD_PROT_MASK 0
134 #endif
135 
136 static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
137 			   pud_t *pud, unsigned long addr)
138 {
139 	unsigned int prot;
140 	pmd_t *pmd;
141 	int i;
142 
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);
150 			} else
151 				walk_pte_level(m, st, pmd, addr);
152 		} else
153 			note_page(m, st, _PAGE_INVALID, 3);
154 		addr += PMD_SIZE;
155 	}
156 }
157 
158 #ifdef CONFIG_64BIT
159 #define _PUD_PROT_MASK _REGION3_ENTRY_RO
160 #else
161 #define _PUD_PROT_MASK 0
162 #endif
163 
164 static void walk_pud_level(struct seq_file *m, struct pg_state *st,
165 			   pgd_t *pgd, unsigned long addr)
166 {
167 	unsigned int prot;
168 	pud_t *pud;
169 	int i;
170 
171 	for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
172 		st->current_address = addr;
173 		pud = pud_offset(pgd, addr);
174 		if (!pud_none(*pud))
175 			if (pud_large(*pud)) {
176 				prot = pud_val(*pud) & _PUD_PROT_MASK;
177 				note_page(m, st, prot, 2);
178 			} else
179 				walk_pmd_level(m, st, pud, addr);
180 		else
181 			note_page(m, st, _PAGE_INVALID, 2);
182 		addr += PUD_SIZE;
183 	}
184 }
185 
186 static void walk_pgd_level(struct seq_file *m)
187 {
188 	unsigned long addr = 0;
189 	struct pg_state st;
190 	pgd_t *pgd;
191 	int i;
192 
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);
197 		if (!pgd_none(*pgd))
198 			walk_pud_level(m, &st, pgd, addr);
199 		else
200 			note_page(m, &st, _PAGE_INVALID, 1);
201 		addr += PGDIR_SIZE;
202 	}
203 	/* Flush out the last page */
204 	st.current_address = max_addr;
205 	note_page(m, &st, 0, 0);
206 }
207 
208 static int ptdump_show(struct seq_file *m, void *v)
209 {
210 	walk_pgd_level(m);
211 	return 0;
212 }
213 
214 static int ptdump_open(struct inode *inode, struct file *filp)
215 {
216 	return single_open(filp, ptdump_show, NULL);
217 }
218 
219 static const struct file_operations ptdump_fops = {
220 	.open		= ptdump_open,
221 	.read		= seq_read,
222 	.llseek		= seq_lseek,
223 	.release	= single_release,
224 };
225 
226 static int pt_dump_init(void)
227 {
228 	/*
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.
232 	 */
233 #ifdef CONFIG_32BIT
234 	max_addr = 1UL << 31;
235 #else
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;
239 #endif
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);
243 	return 0;
244 }
245 device_initcall(pt_dump_init);
246