xref: /linux/arch/s390/mm/dump_pagetables.c (revision 1aea9b3f921003f0880f0676ae85d87c9f1cb4a2)
1e76e82d7SHeiko Carstens #include <linux/seq_file.h>
2e76e82d7SHeiko Carstens #include <linux/debugfs.h>
3549f2bf5SHeiko Carstens #include <linux/sched.h>
4e76e82d7SHeiko Carstens #include <linux/mm.h>
5e76e82d7SHeiko Carstens #include <asm/sections.h>
6e76e82d7SHeiko Carstens #include <asm/pgtable.h>
7e76e82d7SHeiko Carstens 
8e76e82d7SHeiko Carstens static unsigned long max_addr;
9e76e82d7SHeiko Carstens 
10e76e82d7SHeiko Carstens struct addr_marker {
11e76e82d7SHeiko Carstens 	unsigned long start_address;
12e76e82d7SHeiko Carstens 	const char *name;
13e76e82d7SHeiko Carstens };
14e76e82d7SHeiko Carstens 
15e76e82d7SHeiko Carstens enum address_markers_idx {
16e76e82d7SHeiko Carstens 	IDENTITY_NR = 0,
17e76e82d7SHeiko Carstens 	KERNEL_START_NR,
18e76e82d7SHeiko Carstens 	KERNEL_END_NR,
19e76e82d7SHeiko Carstens 	VMEMMAP_NR,
20e76e82d7SHeiko Carstens 	VMALLOC_NR,
21c972cc60SHeiko Carstens 	MODULES_NR,
22e76e82d7SHeiko Carstens };
23e76e82d7SHeiko Carstens 
24e76e82d7SHeiko Carstens static struct addr_marker address_markers[] = {
25e76e82d7SHeiko Carstens 	[IDENTITY_NR]	  = {0, "Identity Mapping"},
26e76e82d7SHeiko Carstens 	[KERNEL_START_NR] = {(unsigned long)&_stext, "Kernel Image Start"},
27e76e82d7SHeiko Carstens 	[KERNEL_END_NR]	  = {(unsigned long)&_end, "Kernel Image End"},
28e76e82d7SHeiko Carstens 	[VMEMMAP_NR]	  = {0, "vmemmap Area"},
29e76e82d7SHeiko Carstens 	[VMALLOC_NR]	  = {0, "vmalloc Area"},
30c972cc60SHeiko Carstens 	[MODULES_NR]	  = {0, "Modules Area"},
31e76e82d7SHeiko Carstens 	{ -1, NULL }
32e76e82d7SHeiko Carstens };
33e76e82d7SHeiko Carstens 
34e76e82d7SHeiko Carstens struct pg_state {
35e76e82d7SHeiko Carstens 	int level;
36e76e82d7SHeiko Carstens 	unsigned int current_prot;
37e76e82d7SHeiko Carstens 	unsigned long start_address;
38e76e82d7SHeiko Carstens 	unsigned long current_address;
39e76e82d7SHeiko Carstens 	const struct addr_marker *marker;
40e76e82d7SHeiko Carstens };
41e76e82d7SHeiko Carstens 
42e76e82d7SHeiko Carstens static void print_prot(struct seq_file *m, unsigned int pr, int level)
43e76e82d7SHeiko Carstens {
44e76e82d7SHeiko Carstens 	static const char * const level_name[] =
45e76e82d7SHeiko Carstens 		{ "ASCE", "PGD", "PUD", "PMD", "PTE" };
46e76e82d7SHeiko Carstens 
47e76e82d7SHeiko Carstens 	seq_printf(m, "%s ", level_name[level]);
481819ed1fSHeiko Carstens 	if (pr & _PAGE_INVALID) {
49e76e82d7SHeiko Carstens 		seq_printf(m, "I\n");
501819ed1fSHeiko Carstens 		return;
511819ed1fSHeiko Carstens 	}
5257d7f939SMartin Schwidefsky 	seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
5357d7f939SMartin Schwidefsky 	seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
54e76e82d7SHeiko Carstens }
55e76e82d7SHeiko Carstens 
56e76e82d7SHeiko Carstens static void note_page(struct seq_file *m, struct pg_state *st,
57e76e82d7SHeiko Carstens 		     unsigned int new_prot, int level)
58e76e82d7SHeiko Carstens {
59e76e82d7SHeiko Carstens 	static const char units[] = "KMGTPE";
60e76e82d7SHeiko Carstens 	int width = sizeof(unsigned long) * 2;
61e76e82d7SHeiko Carstens 	const char *unit = units;
62e76e82d7SHeiko Carstens 	unsigned int prot, cur;
63e76e82d7SHeiko Carstens 	unsigned long delta;
64e76e82d7SHeiko Carstens 
65e76e82d7SHeiko Carstens 	/*
66e76e82d7SHeiko Carstens 	 * If we have a "break" in the series, we need to flush the state
67e76e82d7SHeiko Carstens 	 * that we have now. "break" is either changing perms, levels or
68e76e82d7SHeiko Carstens 	 * address space marker.
69e76e82d7SHeiko Carstens 	 */
70e76e82d7SHeiko Carstens 	prot = new_prot;
71e76e82d7SHeiko Carstens 	cur = st->current_prot;
72e76e82d7SHeiko Carstens 
73e76e82d7SHeiko Carstens 	if (!st->level) {
74e76e82d7SHeiko Carstens 		/* First entry */
75e76e82d7SHeiko Carstens 		st->current_prot = new_prot;
76e76e82d7SHeiko Carstens 		st->level = level;
77e76e82d7SHeiko Carstens 		st->marker = address_markers;
78e76e82d7SHeiko Carstens 		seq_printf(m, "---[ %s ]---\n", st->marker->name);
79e76e82d7SHeiko Carstens 	} else if (prot != cur || level != st->level ||
80e76e82d7SHeiko Carstens 		   st->current_address >= st->marker[1].start_address) {
81e76e82d7SHeiko Carstens 		/* Print the actual finished series */
82e76e82d7SHeiko Carstens 		seq_printf(m, "0x%0*lx-0x%0*lx",
83e76e82d7SHeiko Carstens 			   width, st->start_address,
84e76e82d7SHeiko Carstens 			   width, st->current_address);
85e76e82d7SHeiko Carstens 		delta = (st->current_address - st->start_address) >> 10;
86e76e82d7SHeiko Carstens 		while (!(delta & 0x3ff) && unit[1]) {
87e76e82d7SHeiko Carstens 			delta >>= 10;
88e76e82d7SHeiko Carstens 			unit++;
89e76e82d7SHeiko Carstens 		}
90e76e82d7SHeiko Carstens 		seq_printf(m, "%9lu%c ", delta, *unit);
91e76e82d7SHeiko Carstens 		print_prot(m, st->current_prot, st->level);
92e76e82d7SHeiko Carstens 		if (st->current_address >= st->marker[1].start_address) {
93e76e82d7SHeiko Carstens 			st->marker++;
94e76e82d7SHeiko Carstens 			seq_printf(m, "---[ %s ]---\n", st->marker->name);
95e76e82d7SHeiko Carstens 		}
96e76e82d7SHeiko Carstens 		st->start_address = st->current_address;
97e76e82d7SHeiko Carstens 		st->current_prot = new_prot;
98e76e82d7SHeiko Carstens 		st->level = level;
99e76e82d7SHeiko Carstens 	}
100e76e82d7SHeiko Carstens }
101e76e82d7SHeiko Carstens 
102e76e82d7SHeiko Carstens /*
103e5098611SMartin Schwidefsky  * The actual page table walker functions. In order to keep the
104e5098611SMartin Schwidefsky  * implementation of print_prot() short, we only check and pass
105e5098611SMartin Schwidefsky  * _PAGE_INVALID and _PAGE_PROTECT flags to note_page() if a region,
106e5098611SMartin Schwidefsky  * segment or page table entry is invalid or read-only.
107e5098611SMartin Schwidefsky  * After all it's just a hint that the current level being walked
108e5098611SMartin Schwidefsky  * contains an invalid or read-only entry.
109e76e82d7SHeiko Carstens  */
110e76e82d7SHeiko Carstens static void walk_pte_level(struct seq_file *m, struct pg_state *st,
111e76e82d7SHeiko Carstens 			   pmd_t *pmd, unsigned long addr)
112e76e82d7SHeiko Carstens {
113e76e82d7SHeiko Carstens 	unsigned int prot;
114e76e82d7SHeiko Carstens 	pte_t *pte;
115e76e82d7SHeiko Carstens 	int i;
116e76e82d7SHeiko Carstens 
117e76e82d7SHeiko Carstens 	for (i = 0; i < PTRS_PER_PTE && addr < max_addr; i++) {
118e76e82d7SHeiko Carstens 		st->current_address = addr;
119e76e82d7SHeiko Carstens 		pte = pte_offset_kernel(pmd, addr);
12057d7f939SMartin Schwidefsky 		prot = pte_val(*pte) &
12157d7f939SMartin Schwidefsky 			(_PAGE_PROTECT | _PAGE_INVALID | _PAGE_NOEXEC);
122e76e82d7SHeiko Carstens 		note_page(m, st, prot, 4);
123e76e82d7SHeiko Carstens 		addr += PAGE_SIZE;
124e76e82d7SHeiko Carstens 	}
125e76e82d7SHeiko Carstens }
126e76e82d7SHeiko Carstens 
127e76e82d7SHeiko Carstens static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
128e76e82d7SHeiko Carstens 			   pud_t *pud, unsigned long addr)
129e76e82d7SHeiko Carstens {
130e76e82d7SHeiko Carstens 	unsigned int prot;
131e76e82d7SHeiko Carstens 	pmd_t *pmd;
132e76e82d7SHeiko Carstens 	int i;
133e76e82d7SHeiko Carstens 
134e76e82d7SHeiko Carstens 	for (i = 0; i < PTRS_PER_PMD && addr < max_addr; i++) {
135e76e82d7SHeiko Carstens 		st->current_address = addr;
136e76e82d7SHeiko Carstens 		pmd = pmd_offset(pud, addr);
137e76e82d7SHeiko Carstens 		if (!pmd_none(*pmd)) {
138e76e82d7SHeiko Carstens 			if (pmd_large(*pmd)) {
13957d7f939SMartin Schwidefsky 				prot = pmd_val(*pmd) &
14057d7f939SMartin Schwidefsky 					(_SEGMENT_ENTRY_PROTECT |
14157d7f939SMartin Schwidefsky 					 _SEGMENT_ENTRY_NOEXEC);
142e76e82d7SHeiko Carstens 				note_page(m, st, prot, 3);
143e76e82d7SHeiko Carstens 			} else
144e76e82d7SHeiko Carstens 				walk_pte_level(m, st, pmd, addr);
145e76e82d7SHeiko Carstens 		} else
146e76e82d7SHeiko Carstens 			note_page(m, st, _PAGE_INVALID, 3);
147e76e82d7SHeiko Carstens 		addr += PMD_SIZE;
148e76e82d7SHeiko Carstens 	}
149e76e82d7SHeiko Carstens }
150e76e82d7SHeiko Carstens 
151e76e82d7SHeiko Carstens static void walk_pud_level(struct seq_file *m, struct pg_state *st,
152*1aea9b3fSMartin Schwidefsky 			   p4d_t *p4d, unsigned long addr)
153e76e82d7SHeiko Carstens {
15418da2369SHeiko Carstens 	unsigned int prot;
155e76e82d7SHeiko Carstens 	pud_t *pud;
156e76e82d7SHeiko Carstens 	int i;
157e76e82d7SHeiko Carstens 
158e76e82d7SHeiko Carstens 	for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
159e76e82d7SHeiko Carstens 		st->current_address = addr;
160*1aea9b3fSMartin Schwidefsky 		pud = pud_offset(p4d, addr);
161e76e82d7SHeiko Carstens 		if (!pud_none(*pud))
16218da2369SHeiko Carstens 			if (pud_large(*pud)) {
16357d7f939SMartin Schwidefsky 				prot = pud_val(*pud) &
16457d7f939SMartin Schwidefsky 					(_REGION_ENTRY_PROTECT |
16557d7f939SMartin Schwidefsky 					 _REGION_ENTRY_NOEXEC);
16618da2369SHeiko Carstens 				note_page(m, st, prot, 2);
16718da2369SHeiko Carstens 			} else
168e76e82d7SHeiko Carstens 				walk_pmd_level(m, st, pud, addr);
169e76e82d7SHeiko Carstens 		else
170e76e82d7SHeiko Carstens 			note_page(m, st, _PAGE_INVALID, 2);
171e76e82d7SHeiko Carstens 		addr += PUD_SIZE;
172e76e82d7SHeiko Carstens 	}
173e76e82d7SHeiko Carstens }
174e76e82d7SHeiko Carstens 
175*1aea9b3fSMartin Schwidefsky static void walk_p4d_level(struct seq_file *m, struct pg_state *st,
176*1aea9b3fSMartin Schwidefsky 			   pgd_t *pgd, unsigned long addr)
177*1aea9b3fSMartin Schwidefsky {
178*1aea9b3fSMartin Schwidefsky 	p4d_t *p4d;
179*1aea9b3fSMartin Schwidefsky 	int i;
180*1aea9b3fSMartin Schwidefsky 
181*1aea9b3fSMartin Schwidefsky 	for (i = 0; i < PTRS_PER_P4D && addr < max_addr; i++) {
182*1aea9b3fSMartin Schwidefsky 		st->current_address = addr;
183*1aea9b3fSMartin Schwidefsky 		p4d = p4d_offset(pgd, addr);
184*1aea9b3fSMartin Schwidefsky 		if (!p4d_none(*p4d))
185*1aea9b3fSMartin Schwidefsky 			walk_pud_level(m, st, p4d, addr);
186*1aea9b3fSMartin Schwidefsky 		else
187*1aea9b3fSMartin Schwidefsky 			note_page(m, st, _PAGE_INVALID, 2);
188*1aea9b3fSMartin Schwidefsky 		addr += P4D_SIZE;
189*1aea9b3fSMartin Schwidefsky 	}
190*1aea9b3fSMartin Schwidefsky }
191*1aea9b3fSMartin Schwidefsky 
192e76e82d7SHeiko Carstens static void walk_pgd_level(struct seq_file *m)
193e76e82d7SHeiko Carstens {
194e76e82d7SHeiko Carstens 	unsigned long addr = 0;
195e76e82d7SHeiko Carstens 	struct pg_state st;
196e76e82d7SHeiko Carstens 	pgd_t *pgd;
197e76e82d7SHeiko Carstens 	int i;
198e76e82d7SHeiko Carstens 
199e76e82d7SHeiko Carstens 	memset(&st, 0, sizeof(st));
200e76e82d7SHeiko Carstens 	for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
201e76e82d7SHeiko Carstens 		st.current_address = addr;
202e76e82d7SHeiko Carstens 		pgd = pgd_offset_k(addr);
203e76e82d7SHeiko Carstens 		if (!pgd_none(*pgd))
204*1aea9b3fSMartin Schwidefsky 			walk_p4d_level(m, &st, pgd, addr);
205e76e82d7SHeiko Carstens 		else
206e76e82d7SHeiko Carstens 			note_page(m, &st, _PAGE_INVALID, 1);
207e76e82d7SHeiko Carstens 		addr += PGDIR_SIZE;
208549f2bf5SHeiko Carstens 		cond_resched();
209e76e82d7SHeiko Carstens 	}
210e76e82d7SHeiko Carstens 	/* Flush out the last page */
211e76e82d7SHeiko Carstens 	st.current_address = max_addr;
212e76e82d7SHeiko Carstens 	note_page(m, &st, 0, 0);
213e76e82d7SHeiko Carstens }
214e76e82d7SHeiko Carstens 
215e76e82d7SHeiko Carstens static int ptdump_show(struct seq_file *m, void *v)
216e76e82d7SHeiko Carstens {
217e76e82d7SHeiko Carstens 	walk_pgd_level(m);
218e76e82d7SHeiko Carstens 	return 0;
219e76e82d7SHeiko Carstens }
220e76e82d7SHeiko Carstens 
221e76e82d7SHeiko Carstens static int ptdump_open(struct inode *inode, struct file *filp)
222e76e82d7SHeiko Carstens {
223e76e82d7SHeiko Carstens 	return single_open(filp, ptdump_show, NULL);
224e76e82d7SHeiko Carstens }
225e76e82d7SHeiko Carstens 
226e76e82d7SHeiko Carstens static const struct file_operations ptdump_fops = {
227e76e82d7SHeiko Carstens 	.open		= ptdump_open,
228e76e82d7SHeiko Carstens 	.read		= seq_read,
229e76e82d7SHeiko Carstens 	.llseek		= seq_lseek,
230e76e82d7SHeiko Carstens 	.release	= single_release,
231e76e82d7SHeiko Carstens };
232e76e82d7SHeiko Carstens 
233e76e82d7SHeiko Carstens static int pt_dump_init(void)
234e76e82d7SHeiko Carstens {
235e76e82d7SHeiko Carstens 	/*
236e76e82d7SHeiko Carstens 	 * Figure out the maximum virtual address being accessible with the
237e76e82d7SHeiko Carstens 	 * kernel ASCE. We need this to keep the page table walker functions
238e76e82d7SHeiko Carstens 	 * from accessing non-existent entries.
239e76e82d7SHeiko Carstens 	 */
240e76e82d7SHeiko Carstens 	max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
241e76e82d7SHeiko Carstens 	max_addr = 1UL << (max_addr * 11 + 31);
242c972cc60SHeiko Carstens 	address_markers[MODULES_NR].start_address = MODULES_VADDR;
243e76e82d7SHeiko Carstens 	address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
244e76e82d7SHeiko Carstens 	address_markers[VMALLOC_NR].start_address = VMALLOC_START;
245e76e82d7SHeiko Carstens 	debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
246e76e82d7SHeiko Carstens 	return 0;
247e76e82d7SHeiko Carstens }
248e76e82d7SHeiko Carstens device_initcall(pt_dump_init);
249