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