Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
8eb07b18 RG |
2 | /* |
3 | * Copyright 2016, Rashmica Gupta, IBM Corp. | |
4 | * | |
5 | * This traverses the kernel pagetables and dumps the | |
6 | * information about the used sections of memory to | |
7 | * /sys/kernel/debug/kernel_pagetables. | |
8 | * | |
9 | * Derived from the arm64 implementation: | |
10 | * Copyright (c) 2014, The Linux Foundation, Laura Abbott. | |
11 | * (C) Copyright 2008 Intel Corporation, Arjan van de Ven. | |
8eb07b18 RG |
12 | */ |
13 | #include <linux/debugfs.h> | |
14 | #include <linux/fs.h> | |
bfb9956a | 15 | #include <linux/hugetlb.h> |
8eb07b18 RG |
16 | #include <linux/io.h> |
17 | #include <linux/mm.h> | |
462951cd | 18 | #include <linux/highmem.h> |
8eb07b18 RG |
19 | #include <linux/sched.h> |
20 | #include <linux/seq_file.h> | |
21 | #include <asm/fixmap.h> | |
22 | #include <asm/pgtable.h> | |
23 | #include <linux/const.h> | |
24 | #include <asm/page.h> | |
25 | #include <asm/pgalloc.h> | |
26 | ||
e66c3209 | 27 | #include "ptdump.h" |
97026b5a | 28 | |
6c01bbd2 | 29 | #ifdef CONFIG_PPC32 |
453d87f6 | 30 | #define KERN_VIRT_START PAGE_OFFSET |
6c01bbd2 CL |
31 | #endif |
32 | ||
8eb07b18 RG |
33 | /* |
34 | * To visualise what is happening, | |
35 | * | |
36 | * - PTRS_PER_P** = how many entries there are in the corresponding P** | |
37 | * - P**_SHIFT = how many bits of the address we use to index into the | |
38 | * corresponding P** | |
39 | * - P**_SIZE is how much memory we can access through the table - not the | |
40 | * size of the table itself. | |
41 | * P**={PGD, PUD, PMD, PTE} | |
42 | * | |
43 | * | |
44 | * Each entry of the PGD points to a PUD. Each entry of a PUD points to a | |
45 | * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to | |
46 | * a page. | |
47 | * | |
48 | * In the case where there are only 3 levels, the PUD is folded into the | |
49 | * PGD: every PUD has only one entry which points to the PMD. | |
50 | * | |
51 | * The page dumper groups page table entries of the same type into a single | |
52 | * description. It uses pg_state to track the range information while | |
53 | * iterating over the PTE entries. When the continuity is broken it then | |
54 | * dumps out a description of the range - ie PTEs that are virtually contiguous | |
55 | * with the same PTE flags are chunked together. This is to make it clear how | |
56 | * different areas of the kernel virtual memory are used. | |
57 | * | |
58 | */ | |
59 | struct pg_state { | |
60 | struct seq_file *seq; | |
61 | const struct addr_marker *marker; | |
62 | unsigned long start_address; | |
aaa22952 OH |
63 | unsigned long start_pa; |
64 | unsigned long last_pa; | |
8eb07b18 RG |
65 | unsigned int level; |
66 | u64 current_flags; | |
453d87f6 RC |
67 | bool check_wx; |
68 | unsigned long wx_pages; | |
8eb07b18 RG |
69 | }; |
70 | ||
71 | struct addr_marker { | |
72 | unsigned long start_address; | |
73 | const char *name; | |
74 | }; | |
75 | ||
76 | static struct addr_marker address_markers[] = { | |
77 | { 0, "Start of kernel VM" }, | |
78 | { 0, "vmalloc() Area" }, | |
79 | { 0, "vmalloc() End" }, | |
6c01bbd2 | 80 | #ifdef CONFIG_PPC64 |
8eb07b18 RG |
81 | { 0, "isa I/O start" }, |
82 | { 0, "isa I/O end" }, | |
83 | { 0, "phb I/O start" }, | |
84 | { 0, "phb I/O end" }, | |
85 | { 0, "I/O remap start" }, | |
86 | { 0, "I/O remap end" }, | |
87 | { 0, "vmemmap start" }, | |
6c01bbd2 CL |
88 | #else |
89 | { 0, "Early I/O remap start" }, | |
90 | { 0, "Early I/O remap end" }, | |
91 | #ifdef CONFIG_NOT_COHERENT_CACHE | |
92 | { 0, "Consistent mem start" }, | |
93 | { 0, "Consistent mem end" }, | |
94 | #endif | |
95 | #ifdef CONFIG_HIGHMEM | |
96 | { 0, "Highmem PTEs start" }, | |
97 | { 0, "Highmem PTEs end" }, | |
98 | #endif | |
99 | { 0, "Fixmap start" }, | |
100 | { 0, "Fixmap end" }, | |
b4abe38f CL |
101 | #endif |
102 | #ifdef CONFIG_KASAN | |
103 | { 0, "kasan shadow mem start" }, | |
104 | { 0, "kasan shadow mem end" }, | |
6c01bbd2 | 105 | #endif |
8eb07b18 RG |
106 | { -1, NULL }, |
107 | }; | |
108 | ||
5f18cbdb RC |
109 | #define pt_dump_seq_printf(m, fmt, args...) \ |
110 | ({ \ | |
111 | if (m) \ | |
112 | seq_printf(m, fmt, ##args); \ | |
113 | }) | |
114 | ||
115 | #define pt_dump_seq_putc(m, c) \ | |
116 | ({ \ | |
117 | if (m) \ | |
118 | seq_putc(m, c); \ | |
119 | }) | |
120 | ||
8eb07b18 RG |
121 | static void dump_flag_info(struct pg_state *st, const struct flag_info |
122 | *flag, u64 pte, int num) | |
123 | { | |
124 | unsigned int i; | |
125 | ||
126 | for (i = 0; i < num; i++, flag++) { | |
127 | const char *s = NULL; | |
128 | u64 val; | |
129 | ||
130 | /* flag not defined so don't check it */ | |
131 | if (flag->mask == 0) | |
132 | continue; | |
133 | /* Some 'flags' are actually values */ | |
134 | if (flag->is_val) { | |
135 | val = pte & flag->val; | |
136 | if (flag->shift) | |
137 | val = val >> flag->shift; | |
5f18cbdb | 138 | pt_dump_seq_printf(st->seq, " %s:%llx", flag->set, val); |
8eb07b18 RG |
139 | } else { |
140 | if ((pte & flag->mask) == flag->val) | |
141 | s = flag->set; | |
142 | else | |
143 | s = flag->clear; | |
144 | if (s) | |
5f18cbdb | 145 | pt_dump_seq_printf(st->seq, " %s", s); |
8eb07b18 RG |
146 | } |
147 | st->current_flags &= ~flag->mask; | |
148 | } | |
149 | if (st->current_flags != 0) | |
5f18cbdb | 150 | pt_dump_seq_printf(st->seq, " unknown flags:%llx", st->current_flags); |
8eb07b18 RG |
151 | } |
152 | ||
153 | static void dump_addr(struct pg_state *st, unsigned long addr) | |
154 | { | |
155 | static const char units[] = "KMGTPE"; | |
156 | const char *unit = units; | |
157 | unsigned long delta; | |
158 | ||
78a18dbf | 159 | #ifdef CONFIG_PPC64 |
cabe8138 | 160 | #define REG "0x%016lx" |
78a18dbf | 161 | #else |
cabe8138 | 162 | #define REG "0x%08lx" |
78a18dbf | 163 | #endif |
aaa22952 | 164 | |
5f18cbdb | 165 | pt_dump_seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1); |
cabe8138 | 166 | if (st->start_pa == st->last_pa && st->start_address + PAGE_SIZE != addr) { |
5f18cbdb | 167 | pt_dump_seq_printf(st->seq, "[" REG "]", st->start_pa); |
cabe8138 CL |
168 | delta = PAGE_SIZE >> 10; |
169 | } else { | |
5f18cbdb | 170 | pt_dump_seq_printf(st->seq, " " REG " ", st->start_pa); |
cabe8138 CL |
171 | delta = (addr - st->start_address) >> 10; |
172 | } | |
8eb07b18 RG |
173 | /* Work out what appropriate unit to use */ |
174 | while (!(delta & 1023) && unit[1]) { | |
175 | delta >>= 10; | |
176 | unit++; | |
177 | } | |
5f18cbdb | 178 | pt_dump_seq_printf(st->seq, "%9lu%c", delta, *unit); |
8eb07b18 RG |
179 | |
180 | } | |
181 | ||
453d87f6 RC |
182 | static void note_prot_wx(struct pg_state *st, unsigned long addr) |
183 | { | |
184 | if (!st->check_wx) | |
185 | return; | |
186 | ||
187 | if (!((st->current_flags & pgprot_val(PAGE_KERNEL_X)) == pgprot_val(PAGE_KERNEL_X))) | |
188 | return; | |
189 | ||
190 | WARN_ONCE(1, "powerpc/mm: Found insecure W+X mapping at address %p/%pS\n", | |
191 | (void *)st->start_address, (void *)st->start_address); | |
192 | ||
193 | st->wx_pages += (addr - st->start_address) / PAGE_SIZE; | |
194 | } | |
195 | ||
8eb07b18 RG |
196 | static void note_page(struct pg_state *st, unsigned long addr, |
197 | unsigned int level, u64 val) | |
198 | { | |
199 | u64 flag = val & pg_level[level].mask; | |
aaa22952 OH |
200 | u64 pa = val & PTE_RPN_MASK; |
201 | ||
8eb07b18 RG |
202 | /* At first no level is set */ |
203 | if (!st->level) { | |
204 | st->level = level; | |
205 | st->current_flags = flag; | |
206 | st->start_address = addr; | |
aaa22952 OH |
207 | st->start_pa = pa; |
208 | st->last_pa = pa; | |
5f18cbdb | 209 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
8eb07b18 RG |
210 | /* |
211 | * Dump the section of virtual memory when: | |
212 | * - the PTE flags from one entry to the next differs. | |
213 | * - we change levels in the tree. | |
214 | * - the address is in a different section of memory and is thus | |
215 | * used for a different purpose, regardless of the flags. | |
aaa22952 | 216 | * - the pa of this page is not adjacent to the last inspected page |
8eb07b18 RG |
217 | */ |
218 | } else if (flag != st->current_flags || level != st->level || | |
aaa22952 | 219 | addr >= st->marker[1].start_address || |
cabe8138 CL |
220 | (pa != st->last_pa + PAGE_SIZE && |
221 | (pa != st->start_pa || st->start_pa != st->last_pa))) { | |
8eb07b18 RG |
222 | |
223 | /* Check the PTE flags */ | |
224 | if (st->current_flags) { | |
453d87f6 | 225 | note_prot_wx(st, addr); |
8eb07b18 RG |
226 | dump_addr(st, addr); |
227 | ||
228 | /* Dump all the flags */ | |
229 | if (pg_level[st->level].flag) | |
230 | dump_flag_info(st, pg_level[st->level].flag, | |
231 | st->current_flags, | |
232 | pg_level[st->level].num); | |
233 | ||
5f18cbdb | 234 | pt_dump_seq_putc(st->seq, '\n'); |
8eb07b18 RG |
235 | } |
236 | ||
237 | /* | |
238 | * Address indicates we have passed the end of the | |
239 | * current section of virtual memory | |
240 | */ | |
241 | while (addr >= st->marker[1].start_address) { | |
242 | st->marker++; | |
5f18cbdb | 243 | pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name); |
8eb07b18 RG |
244 | } |
245 | st->start_address = addr; | |
aaa22952 OH |
246 | st->start_pa = pa; |
247 | st->last_pa = pa; | |
8eb07b18 RG |
248 | st->current_flags = flag; |
249 | st->level = level; | |
aaa22952 OH |
250 | } else { |
251 | st->last_pa = pa; | |
8eb07b18 RG |
252 | } |
253 | } | |
254 | ||
255 | static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start) | |
256 | { | |
257 | pte_t *pte = pte_offset_kernel(pmd, 0); | |
258 | unsigned long addr; | |
259 | unsigned int i; | |
260 | ||
261 | for (i = 0; i < PTRS_PER_PTE; i++, pte++) { | |
262 | addr = start + i * PAGE_SIZE; | |
263 | note_page(st, addr, 4, pte_val(*pte)); | |
264 | ||
265 | } | |
266 | } | |
267 | ||
268 | static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start) | |
269 | { | |
270 | pmd_t *pmd = pmd_offset(pud, 0); | |
271 | unsigned long addr; | |
272 | unsigned int i; | |
273 | ||
274 | for (i = 0; i < PTRS_PER_PMD; i++, pmd++) { | |
275 | addr = start + i * PMD_SIZE; | |
bfb9956a | 276 | if (!pmd_none(*pmd) && !pmd_huge(*pmd)) |
8eb07b18 RG |
277 | /* pmd exists */ |
278 | walk_pte(st, pmd, addr); | |
279 | else | |
280 | note_page(st, addr, 3, pmd_val(*pmd)); | |
281 | } | |
282 | } | |
283 | ||
284 | static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start) | |
285 | { | |
286 | pud_t *pud = pud_offset(pgd, 0); | |
287 | unsigned long addr; | |
288 | unsigned int i; | |
289 | ||
290 | for (i = 0; i < PTRS_PER_PUD; i++, pud++) { | |
291 | addr = start + i * PUD_SIZE; | |
bfb9956a | 292 | if (!pud_none(*pud) && !pud_huge(*pud)) |
8eb07b18 RG |
293 | /* pud exists */ |
294 | walk_pmd(st, pud, addr); | |
295 | else | |
296 | note_page(st, addr, 2, pud_val(*pud)); | |
297 | } | |
298 | } | |
299 | ||
300 | static void walk_pagetables(struct pg_state *st) | |
301 | { | |
302 | pgd_t *pgd = pgd_offset_k(0UL); | |
303 | unsigned int i; | |
304 | unsigned long addr; | |
305 | ||
0d923962 ME |
306 | addr = st->start_address; |
307 | ||
8eb07b18 RG |
308 | /* |
309 | * Traverse the linux pagetable structure and dump pages that are in | |
310 | * the hash pagetable. | |
311 | */ | |
0d923962 | 312 | for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) { |
bfb9956a | 313 | if (!pgd_none(*pgd) && !pgd_huge(*pgd)) |
8eb07b18 RG |
314 | /* pgd exists */ |
315 | walk_pud(st, pgd, addr); | |
316 | else | |
317 | note_page(st, addr, 1, pgd_val(*pgd)); | |
318 | } | |
319 | } | |
320 | ||
321 | static void populate_markers(void) | |
322 | { | |
6c01bbd2 CL |
323 | int i = 0; |
324 | ||
325 | address_markers[i++].start_address = PAGE_OFFSET; | |
326 | address_markers[i++].start_address = VMALLOC_START; | |
327 | address_markers[i++].start_address = VMALLOC_END; | |
328 | #ifdef CONFIG_PPC64 | |
329 | address_markers[i++].start_address = ISA_IO_BASE; | |
330 | address_markers[i++].start_address = ISA_IO_END; | |
331 | address_markers[i++].start_address = PHB_IO_BASE; | |
332 | address_markers[i++].start_address = PHB_IO_END; | |
333 | address_markers[i++].start_address = IOREMAP_BASE; | |
334 | address_markers[i++].start_address = IOREMAP_END; | |
0034d395 | 335 | /* What is the ifdef about? */ |
4e003747 | 336 | #ifdef CONFIG_PPC_BOOK3S_64 |
0034d395 | 337 | address_markers[i++].start_address = H_VMEMMAP_START; |
8eb07b18 | 338 | #else |
6c01bbd2 CL |
339 | address_markers[i++].start_address = VMEMMAP_BASE; |
340 | #endif | |
341 | #else /* !CONFIG_PPC64 */ | |
342 | address_markers[i++].start_address = ioremap_bot; | |
343 | address_markers[i++].start_address = IOREMAP_TOP; | |
344 | #ifdef CONFIG_NOT_COHERENT_CACHE | |
345 | address_markers[i++].start_address = IOREMAP_TOP; | |
346 | address_markers[i++].start_address = IOREMAP_TOP + | |
347 | CONFIG_CONSISTENT_SIZE; | |
348 | #endif | |
349 | #ifdef CONFIG_HIGHMEM | |
350 | address_markers[i++].start_address = PKMAP_BASE; | |
351 | address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP); | |
8eb07b18 | 352 | #endif |
6c01bbd2 CL |
353 | address_markers[i++].start_address = FIXADDR_START; |
354 | address_markers[i++].start_address = FIXADDR_TOP; | |
b4abe38f CL |
355 | #ifdef CONFIG_KASAN |
356 | address_markers[i++].start_address = KASAN_SHADOW_START; | |
357 | address_markers[i++].start_address = KASAN_SHADOW_END; | |
358 | #endif | |
6c01bbd2 | 359 | #endif /* CONFIG_PPC64 */ |
8eb07b18 RG |
360 | } |
361 | ||
362 | static int ptdump_show(struct seq_file *m, void *v) | |
363 | { | |
364 | struct pg_state st = { | |
365 | .seq = m, | |
8eb07b18 RG |
366 | .marker = address_markers, |
367 | }; | |
0d923962 ME |
368 | |
369 | if (radix_enabled()) | |
370 | st.start_address = PAGE_OFFSET; | |
371 | else | |
372 | st.start_address = KERN_VIRT_START; | |
373 | ||
8eb07b18 RG |
374 | /* Traverse kernel page tables */ |
375 | walk_pagetables(&st); | |
376 | note_page(&st, 0, 0, 0); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | ||
381 | static int ptdump_open(struct inode *inode, struct file *file) | |
382 | { | |
383 | return single_open(file, ptdump_show, NULL); | |
384 | } | |
385 | ||
386 | static const struct file_operations ptdump_fops = { | |
387 | .open = ptdump_open, | |
388 | .read = seq_read, | |
389 | .llseek = seq_lseek, | |
390 | .release = single_release, | |
391 | }; | |
392 | ||
393 | static void build_pgtable_complete_mask(void) | |
394 | { | |
395 | unsigned int i, j; | |
396 | ||
397 | for (i = 0; i < ARRAY_SIZE(pg_level); i++) | |
398 | if (pg_level[i].flag) | |
399 | for (j = 0; j < pg_level[i].num; j++) | |
400 | pg_level[i].mask |= pg_level[i].flag[j].mask; | |
401 | } | |
402 | ||
453d87f6 RC |
403 | #ifdef CONFIG_PPC_DEBUG_WX |
404 | void ptdump_check_wx(void) | |
405 | { | |
406 | struct pg_state st = { | |
407 | .seq = NULL, | |
408 | .marker = address_markers, | |
409 | .check_wx = true, | |
410 | }; | |
411 | ||
412 | if (radix_enabled()) | |
413 | st.start_address = PAGE_OFFSET; | |
414 | else | |
415 | st.start_address = KERN_VIRT_START; | |
416 | ||
417 | walk_pagetables(&st); | |
418 | ||
419 | if (st.wx_pages) | |
420 | pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", | |
421 | st.wx_pages); | |
422 | else | |
423 | pr_info("Checked W+X mappings: passed, no W+X pages found\n"); | |
424 | } | |
425 | #endif | |
426 | ||
8eb07b18 RG |
427 | static int ptdump_init(void) |
428 | { | |
429 | struct dentry *debugfs_file; | |
430 | ||
431 | populate_markers(); | |
432 | build_pgtable_complete_mask(); | |
2505820f | 433 | debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL, |
8eb07b18 RG |
434 | NULL, &ptdump_fops); |
435 | return debugfs_file ? 0 : -ENOMEM; | |
436 | } | |
437 | device_initcall(ptdump_init); |