xref: /linux/arch/s390/mm/dump_pagetables.c (revision 57d7f939e7bdd746992f5c318a78697ba837c523)
1e76e82d7SHeiko Carstens #include <linux/seq_file.h>
2e76e82d7SHeiko Carstens #include <linux/debugfs.h>
3e76e82d7SHeiko Carstens #include <linux/module.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 	}
52*57d7f939SMartin Schwidefsky 	seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
53*57d7f939SMartin 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);
120*57d7f939SMartin Schwidefsky 		prot = pte_val(*pte) &
121*57d7f939SMartin 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)) {
139*57d7f939SMartin Schwidefsky 				prot = pmd_val(*pmd) &
140*57d7f939SMartin Schwidefsky 					(_SEGMENT_ENTRY_PROTECT |
141*57d7f939SMartin 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,
152e76e82d7SHeiko Carstens 			   pgd_t *pgd, 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;
160e76e82d7SHeiko Carstens 		pud = pud_offset(pgd, addr);
161e76e82d7SHeiko Carstens 		if (!pud_none(*pud))
16218da2369SHeiko Carstens 			if (pud_large(*pud)) {
163*57d7f939SMartin Schwidefsky 				prot = pud_val(*pud) &
164*57d7f939SMartin Schwidefsky 					(_REGION_ENTRY_PROTECT |
165*57d7f939SMartin 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 
175e76e82d7SHeiko Carstens static void walk_pgd_level(struct seq_file *m)
176e76e82d7SHeiko Carstens {
177e76e82d7SHeiko Carstens 	unsigned long addr = 0;
178e76e82d7SHeiko Carstens 	struct pg_state st;
179e76e82d7SHeiko Carstens 	pgd_t *pgd;
180e76e82d7SHeiko Carstens 	int i;
181e76e82d7SHeiko Carstens 
182e76e82d7SHeiko Carstens 	memset(&st, 0, sizeof(st));
183e76e82d7SHeiko Carstens 	for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
184e76e82d7SHeiko Carstens 		st.current_address = addr;
185e76e82d7SHeiko Carstens 		pgd = pgd_offset_k(addr);
186e76e82d7SHeiko Carstens 		if (!pgd_none(*pgd))
187e76e82d7SHeiko Carstens 			walk_pud_level(m, &st, pgd, addr);
188e76e82d7SHeiko Carstens 		else
189e76e82d7SHeiko Carstens 			note_page(m, &st, _PAGE_INVALID, 1);
190e76e82d7SHeiko Carstens 		addr += PGDIR_SIZE;
191e76e82d7SHeiko Carstens 	}
192e76e82d7SHeiko Carstens 	/* Flush out the last page */
193e76e82d7SHeiko Carstens 	st.current_address = max_addr;
194e76e82d7SHeiko Carstens 	note_page(m, &st, 0, 0);
195e76e82d7SHeiko Carstens }
196e76e82d7SHeiko Carstens 
197e76e82d7SHeiko Carstens static int ptdump_show(struct seq_file *m, void *v)
198e76e82d7SHeiko Carstens {
199e76e82d7SHeiko Carstens 	walk_pgd_level(m);
200e76e82d7SHeiko Carstens 	return 0;
201e76e82d7SHeiko Carstens }
202e76e82d7SHeiko Carstens 
203e76e82d7SHeiko Carstens static int ptdump_open(struct inode *inode, struct file *filp)
204e76e82d7SHeiko Carstens {
205e76e82d7SHeiko Carstens 	return single_open(filp, ptdump_show, NULL);
206e76e82d7SHeiko Carstens }
207e76e82d7SHeiko Carstens 
208e76e82d7SHeiko Carstens static const struct file_operations ptdump_fops = {
209e76e82d7SHeiko Carstens 	.open		= ptdump_open,
210e76e82d7SHeiko Carstens 	.read		= seq_read,
211e76e82d7SHeiko Carstens 	.llseek		= seq_lseek,
212e76e82d7SHeiko Carstens 	.release	= single_release,
213e76e82d7SHeiko Carstens };
214e76e82d7SHeiko Carstens 
215e76e82d7SHeiko Carstens static int pt_dump_init(void)
216e76e82d7SHeiko Carstens {
217e76e82d7SHeiko Carstens 	/*
218e76e82d7SHeiko Carstens 	 * Figure out the maximum virtual address being accessible with the
219e76e82d7SHeiko Carstens 	 * kernel ASCE. We need this to keep the page table walker functions
220e76e82d7SHeiko Carstens 	 * from accessing non-existent entries.
221e76e82d7SHeiko Carstens 	 */
222e76e82d7SHeiko Carstens 	max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
223e76e82d7SHeiko Carstens 	max_addr = 1UL << (max_addr * 11 + 31);
224c972cc60SHeiko Carstens 	address_markers[MODULES_NR].start_address = MODULES_VADDR;
225e76e82d7SHeiko Carstens 	address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
226e76e82d7SHeiko Carstens 	address_markers[VMALLOC_NR].start_address = VMALLOC_START;
227e76e82d7SHeiko Carstens 	debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
228e76e82d7SHeiko Carstens 	return 0;
229e76e82d7SHeiko Carstens }
230e76e82d7SHeiko Carstens device_initcall(pt_dump_init);
231