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