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