Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / arch / x86 / mm / dump_pagetables.c
1 /*
2  * Debug helper to dump the current kernel pagetables of the system
3  * so that we can see what the various memory ranges are set to.
4  *
5  * (C) Copyright 2008 Intel Corporation
6  *
7  * Author: Arjan van de Ven <arjan@linux.intel.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2
12  * of the License.
13  */
14
15 #include <linux/debugfs.h>
16 #include <linux/kasan.h>
17 #include <linux/mm.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/seq_file.h>
21
22 #include <asm/pgtable.h>
23
24 /*
25  * The dumper groups pagetable entries of the same type into one, and for
26  * that it needs to keep some state when walking, and flush this state
27  * when a "break" in the continuity is found.
28  */
29 struct pg_state {
30         int level;
31         pgprot_t current_prot;
32         unsigned long start_address;
33         unsigned long current_address;
34         const struct addr_marker *marker;
35         unsigned long lines;
36         bool to_dmesg;
37         bool check_wx;
38         unsigned long wx_pages;
39 };
40
41 struct addr_marker {
42         unsigned long start_address;
43         const char *name;
44         unsigned long max_lines;
45 };
46
47 /* Address space markers hints */
48
49 #ifdef CONFIG_X86_64
50
51 enum address_markers_idx {
52         USER_SPACE_NR = 0,
53         KERNEL_SPACE_NR,
54         LOW_KERNEL_NR,
55         VMALLOC_START_NR,
56         VMEMMAP_START_NR,
57 #ifdef CONFIG_KASAN
58         KASAN_SHADOW_START_NR,
59         KASAN_SHADOW_END_NR,
60 #endif
61         CPU_ENTRY_AREA_NR,
62 #ifdef CONFIG_X86_ESPFIX64
63         ESPFIX_START_NR,
64 #endif
65 #ifdef CONFIG_EFI
66         EFI_END_NR,
67 #endif
68         HIGH_KERNEL_NR,
69         MODULES_VADDR_NR,
70         MODULES_END_NR,
71         FIXADDR_START_NR,
72         END_OF_SPACE_NR,
73 };
74
75 static struct addr_marker address_markers[] = {
76         [USER_SPACE_NR]         = { 0,                  "User Space" },
77         [KERNEL_SPACE_NR]       = { (1UL << 63),        "Kernel Space" },
78         [LOW_KERNEL_NR]         = { 0UL,                "Low Kernel Mapping" },
79         [VMALLOC_START_NR]      = { 0UL,                "vmalloc() Area" },
80         [VMEMMAP_START_NR]      = { 0UL,                "Vmemmap" },
81 #ifdef CONFIG_KASAN
82         [KASAN_SHADOW_START_NR] = { KASAN_SHADOW_START, "KASAN shadow" },
83         [KASAN_SHADOW_END_NR]   = { KASAN_SHADOW_END,   "KASAN shadow end" },
84 #endif
85         [CPU_ENTRY_AREA_NR]     = { CPU_ENTRY_AREA_BASE,"CPU entry Area" },
86 #ifdef CONFIG_X86_ESPFIX64
87         [ESPFIX_START_NR]       = { ESPFIX_BASE_ADDR,   "ESPfix Area", 16 },
88 #endif
89 #ifdef CONFIG_EFI
90         [EFI_END_NR]            = { EFI_VA_END,         "EFI Runtime Services" },
91 #endif
92         [HIGH_KERNEL_NR]        = { __START_KERNEL_map, "High Kernel Mapping" },
93         [MODULES_VADDR_NR]      = { MODULES_VADDR,      "Modules" },
94         [MODULES_END_NR]        = { MODULES_END,        "End Modules" },
95         [FIXADDR_START_NR]      = { FIXADDR_START,      "Fixmap Area" },
96         [END_OF_SPACE_NR]       = { -1,                 NULL }
97 };
98
99 #else /* CONFIG_X86_64 */
100
101 enum address_markers_idx {
102         USER_SPACE_NR = 0,
103         KERNEL_SPACE_NR,
104         VMALLOC_START_NR,
105         VMALLOC_END_NR,
106 #ifdef CONFIG_HIGHMEM
107         PKMAP_BASE_NR,
108 #endif
109         CPU_ENTRY_AREA_NR,
110         FIXADDR_START_NR,
111         END_OF_SPACE_NR,
112 };
113
114 static struct addr_marker address_markers[] = {
115         [USER_SPACE_NR]         = { 0,                  "User Space" },
116         [KERNEL_SPACE_NR]       = { PAGE_OFFSET,        "Kernel Mapping" },
117         [VMALLOC_START_NR]      = { 0UL,                "vmalloc() Area" },
118         [VMALLOC_END_NR]        = { 0UL,                "vmalloc() End" },
119 #ifdef CONFIG_HIGHMEM
120         [PKMAP_BASE_NR]         = { 0UL,                "Persistent kmap() Area" },
121 #endif
122         [CPU_ENTRY_AREA_NR]     = { 0UL,                "CPU entry area" },
123         [FIXADDR_START_NR]      = { 0UL,                "Fixmap area" },
124         [END_OF_SPACE_NR]       = { -1,                 NULL }
125 };
126
127 #endif /* !CONFIG_X86_64 */
128
129 /* Multipliers for offsets within the PTEs */
130 #define PTE_LEVEL_MULT (PAGE_SIZE)
131 #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
132 #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
133 #define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
134 #define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT)
135
136 #define pt_dump_seq_printf(m, to_dmesg, fmt, args...)           \
137 ({                                                              \
138         if (to_dmesg)                                   \
139                 printk(KERN_INFO fmt, ##args);                  \
140         else                                                    \
141                 if (m)                                          \
142                         seq_printf(m, fmt, ##args);             \
143 })
144
145 #define pt_dump_cont_printf(m, to_dmesg, fmt, args...)          \
146 ({                                                              \
147         if (to_dmesg)                                   \
148                 printk(KERN_CONT fmt, ##args);                  \
149         else                                                    \
150                 if (m)                                          \
151                         seq_printf(m, fmt, ##args);             \
152 })
153
154 /*
155  * Print a readable form of a pgprot_t to the seq_file
156  */
157 static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg)
158 {
159         pgprotval_t pr = pgprot_val(prot);
160         static const char * const level_name[] =
161                 { "cr3", "pgd", "p4d", "pud", "pmd", "pte" };
162
163         if (!(pr & _PAGE_PRESENT)) {
164                 /* Not present */
165                 pt_dump_cont_printf(m, dmsg, "                              ");
166         } else {
167                 if (pr & _PAGE_USER)
168                         pt_dump_cont_printf(m, dmsg, "USR ");
169                 else
170                         pt_dump_cont_printf(m, dmsg, "    ");
171                 if (pr & _PAGE_RW)
172                         pt_dump_cont_printf(m, dmsg, "RW ");
173                 else
174                         pt_dump_cont_printf(m, dmsg, "ro ");
175                 if (pr & _PAGE_PWT)
176                         pt_dump_cont_printf(m, dmsg, "PWT ");
177                 else
178                         pt_dump_cont_printf(m, dmsg, "    ");
179                 if (pr & _PAGE_PCD)
180                         pt_dump_cont_printf(m, dmsg, "PCD ");
181                 else
182                         pt_dump_cont_printf(m, dmsg, "    ");
183
184                 /* Bit 7 has a different meaning on level 3 vs 4 */
185                 if (level <= 4 && pr & _PAGE_PSE)
186                         pt_dump_cont_printf(m, dmsg, "PSE ");
187                 else
188                         pt_dump_cont_printf(m, dmsg, "    ");
189                 if ((level == 5 && pr & _PAGE_PAT) ||
190                     ((level == 4 || level == 3) && pr & _PAGE_PAT_LARGE))
191                         pt_dump_cont_printf(m, dmsg, "PAT ");
192                 else
193                         pt_dump_cont_printf(m, dmsg, "    ");
194                 if (pr & _PAGE_GLOBAL)
195                         pt_dump_cont_printf(m, dmsg, "GLB ");
196                 else
197                         pt_dump_cont_printf(m, dmsg, "    ");
198                 if (pr & _PAGE_NX)
199                         pt_dump_cont_printf(m, dmsg, "NX ");
200                 else
201                         pt_dump_cont_printf(m, dmsg, "x  ");
202         }
203         pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
204 }
205
206 /*
207  * On 64 bits, sign-extend the 48 bit address to 64 bit
208  */
209 static unsigned long normalize_addr(unsigned long u)
210 {
211         int shift;
212         if (!IS_ENABLED(CONFIG_X86_64))
213                 return u;
214
215         shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
216         return (signed long)(u << shift) >> shift;
217 }
218
219 /*
220  * This function gets called on a break in a continuous series
221  * of PTE entries; the next one is different so we need to
222  * print what we collected so far.
223  */
224 static void note_page(struct seq_file *m, struct pg_state *st,
225                       pgprot_t new_prot, int level)
226 {
227         pgprotval_t prot, cur;
228         static const char units[] = "BKMGTPE";
229
230         /*
231          * If we have a "break" in the series, we need to flush the state that
232          * we have now. "break" is either changing perms, levels or
233          * address space marker.
234          */
235         prot = pgprot_val(new_prot);
236         cur = pgprot_val(st->current_prot);
237
238         if (!st->level) {
239                 /* First entry */
240                 st->current_prot = new_prot;
241                 st->level = level;
242                 st->marker = address_markers;
243                 st->lines = 0;
244                 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
245                                    st->marker->name);
246         } else if (prot != cur || level != st->level ||
247                    st->current_address >= st->marker[1].start_address) {
248                 const char *unit = units;
249                 unsigned long delta;
250                 int width = sizeof(unsigned long) * 2;
251                 pgprotval_t pr = pgprot_val(st->current_prot);
252
253                 if (st->check_wx && (pr & _PAGE_RW) && !(pr & _PAGE_NX)) {
254                         WARN_ONCE(1,
255                                   "x86/mm: Found insecure W+X mapping at address %p/%pS\n",
256                                   (void *)st->start_address,
257                                   (void *)st->start_address);
258                         st->wx_pages += (st->current_address -
259                                          st->start_address) / PAGE_SIZE;
260                 }
261
262                 /*
263                  * Now print the actual finished series
264                  */
265                 if (!st->marker->max_lines ||
266                     st->lines < st->marker->max_lines) {
267                         pt_dump_seq_printf(m, st->to_dmesg,
268                                            "0x%0*lx-0x%0*lx   ",
269                                            width, st->start_address,
270                                            width, st->current_address);
271
272                         delta = st->current_address - st->start_address;
273                         while (!(delta & 1023) && unit[1]) {
274                                 delta >>= 10;
275                                 unit++;
276                         }
277                         pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
278                                             delta, *unit);
279                         printk_prot(m, st->current_prot, st->level,
280                                     st->to_dmesg);
281                 }
282                 st->lines++;
283
284                 /*
285                  * We print markers for special areas of address space,
286                  * such as the start of vmalloc space etc.
287                  * This helps in the interpretation.
288                  */
289                 if (st->current_address >= st->marker[1].start_address) {
290                         if (st->marker->max_lines &&
291                             st->lines > st->marker->max_lines) {
292                                 unsigned long nskip =
293                                         st->lines - st->marker->max_lines;
294                                 pt_dump_seq_printf(m, st->to_dmesg,
295                                                    "... %lu entr%s skipped ... \n",
296                                                    nskip,
297                                                    nskip == 1 ? "y" : "ies");
298                         }
299                         st->marker++;
300                         st->lines = 0;
301                         pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
302                                            st->marker->name);
303                 }
304
305                 st->start_address = st->current_address;
306                 st->current_prot = new_prot;
307                 st->level = level;
308         }
309 }
310
311 static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr, unsigned long P)
312 {
313         int i;
314         pte_t *start;
315         pgprotval_t prot;
316
317         start = (pte_t *)pmd_page_vaddr(addr);
318         for (i = 0; i < PTRS_PER_PTE; i++) {
319                 prot = pte_flags(*start);
320                 st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT);
321                 note_page(m, st, __pgprot(prot), 5);
322                 start++;
323         }
324 }
325 #ifdef CONFIG_KASAN
326
327 /*
328  * This is an optimization for KASAN=y case. Since all kasan page tables
329  * eventually point to the kasan_zero_page we could call note_page()
330  * right away without walking through lower level page tables. This saves
331  * us dozens of seconds (minutes for 5-level config) while checking for
332  * W+X mapping or reading kernel_page_tables debugfs file.
333  */
334 static inline bool kasan_page_table(struct seq_file *m, struct pg_state *st,
335                                 void *pt)
336 {
337         if (__pa(pt) == __pa(kasan_zero_pmd) ||
338 #ifdef CONFIG_X86_5LEVEL
339             __pa(pt) == __pa(kasan_zero_p4d) ||
340 #endif
341             __pa(pt) == __pa(kasan_zero_pud)) {
342                 pgprotval_t prot = pte_flags(kasan_zero_pte[0]);
343                 note_page(m, st, __pgprot(prot), 5);
344                 return true;
345         }
346         return false;
347 }
348 #else
349 static inline bool kasan_page_table(struct seq_file *m, struct pg_state *st,
350                                 void *pt)
351 {
352         return false;
353 }
354 #endif
355
356 #if PTRS_PER_PMD > 1
357
358 static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr, unsigned long P)
359 {
360         int i;
361         pmd_t *start, *pmd_start;
362         pgprotval_t prot;
363
364         pmd_start = start = (pmd_t *)pud_page_vaddr(addr);
365         for (i = 0; i < PTRS_PER_PMD; i++) {
366                 st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT);
367                 if (!pmd_none(*start)) {
368                         if (pmd_large(*start) || !pmd_present(*start)) {
369                                 prot = pmd_flags(*start);
370                                 note_page(m, st, __pgprot(prot), 4);
371                         } else if (!kasan_page_table(m, st, pmd_start)) {
372                                 walk_pte_level(m, st, *start,
373                                                P + i * PMD_LEVEL_MULT);
374                         }
375                 } else
376                         note_page(m, st, __pgprot(0), 4);
377                 start++;
378         }
379 }
380
381 #else
382 #define walk_pmd_level(m,s,a,p) walk_pte_level(m,s,__pmd(pud_val(a)),p)
383 #define pud_large(a) pmd_large(__pmd(pud_val(a)))
384 #define pud_none(a)  pmd_none(__pmd(pud_val(a)))
385 #endif
386
387 #if PTRS_PER_PUD > 1
388
389 static void walk_pud_level(struct seq_file *m, struct pg_state *st, p4d_t addr, unsigned long P)
390 {
391         int i;
392         pud_t *start, *pud_start;
393         pgprotval_t prot;
394         pud_t *prev_pud = NULL;
395
396         pud_start = start = (pud_t *)p4d_page_vaddr(addr);
397
398         for (i = 0; i < PTRS_PER_PUD; i++) {
399                 st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
400                 if (!pud_none(*start)) {
401                         if (pud_large(*start) || !pud_present(*start)) {
402                                 prot = pud_flags(*start);
403                                 note_page(m, st, __pgprot(prot), 3);
404                         } else if (!kasan_page_table(m, st, pud_start)) {
405                                 walk_pmd_level(m, st, *start,
406                                                P + i * PUD_LEVEL_MULT);
407                         }
408                 } else
409                         note_page(m, st, __pgprot(0), 3);
410
411                 prev_pud = start;
412                 start++;
413         }
414 }
415
416 #else
417 #define walk_pud_level(m,s,a,p) walk_pmd_level(m,s,__pud(p4d_val(a)),p)
418 #define p4d_large(a) pud_large(__pud(p4d_val(a)))
419 #define p4d_none(a)  pud_none(__pud(p4d_val(a)))
420 #endif
421
422 #if PTRS_PER_P4D > 1
423
424 static void walk_p4d_level(struct seq_file *m, struct pg_state *st, pgd_t addr, unsigned long P)
425 {
426         int i;
427         p4d_t *start, *p4d_start;
428         pgprotval_t prot;
429
430         p4d_start = start = (p4d_t *)pgd_page_vaddr(addr);
431
432         for (i = 0; i < PTRS_PER_P4D; i++) {
433                 st->current_address = normalize_addr(P + i * P4D_LEVEL_MULT);
434                 if (!p4d_none(*start)) {
435                         if (p4d_large(*start) || !p4d_present(*start)) {
436                                 prot = p4d_flags(*start);
437                                 note_page(m, st, __pgprot(prot), 2);
438                         } else if (!kasan_page_table(m, st, p4d_start)) {
439                                 walk_pud_level(m, st, *start,
440                                                P + i * P4D_LEVEL_MULT);
441                         }
442                 } else
443                         note_page(m, st, __pgprot(0), 2);
444
445                 start++;
446         }
447 }
448
449 #else
450 #define walk_p4d_level(m,s,a,p) walk_pud_level(m,s,__p4d(pgd_val(a)),p)
451 #define pgd_large(a) p4d_large(__p4d(pgd_val(a)))
452 #define pgd_none(a)  p4d_none(__p4d(pgd_val(a)))
453 #endif
454
455 static inline bool is_hypervisor_range(int idx)
456 {
457 #ifdef CONFIG_X86_64
458         /*
459          * ffff800000000000 - ffff87ffffffffff is reserved for
460          * the hypervisor.
461          */
462         return  (idx >= pgd_index(__PAGE_OFFSET) - 16) &&
463                 (idx <  pgd_index(__PAGE_OFFSET));
464 #else
465         return false;
466 #endif
467 }
468
469 static void ptdump_walk_pgd_level_core(struct seq_file *m, pgd_t *pgd,
470                                        bool checkwx)
471 {
472 #ifdef CONFIG_X86_64
473         pgd_t *start = (pgd_t *) &init_top_pgt;
474 #else
475         pgd_t *start = swapper_pg_dir;
476 #endif
477         pgprotval_t prot;
478         int i;
479         struct pg_state st = {};
480
481         if (pgd) {
482                 start = pgd;
483                 st.to_dmesg = true;
484         }
485
486         st.check_wx = checkwx;
487         if (checkwx)
488                 st.wx_pages = 0;
489
490         for (i = 0; i < PTRS_PER_PGD; i++) {
491                 st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
492                 if (!pgd_none(*start) && !is_hypervisor_range(i)) {
493                         if (pgd_large(*start) || !pgd_present(*start)) {
494                                 prot = pgd_flags(*start);
495                                 note_page(m, &st, __pgprot(prot), 1);
496                         } else {
497                                 walk_p4d_level(m, &st, *start,
498                                                i * PGD_LEVEL_MULT);
499                         }
500                 } else
501                         note_page(m, &st, __pgprot(0), 1);
502
503                 cond_resched();
504                 start++;
505         }
506
507         /* Flush out the last page */
508         st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT);
509         note_page(m, &st, __pgprot(0), 0);
510         if (!checkwx)
511                 return;
512         if (st.wx_pages)
513                 pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n",
514                         st.wx_pages);
515         else
516                 pr_info("x86/mm: Checked W+X mappings: passed, no W+X pages found.\n");
517 }
518
519 void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd)
520 {
521         ptdump_walk_pgd_level_core(m, pgd, false);
522 }
523 EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level);
524
525 void ptdump_walk_pgd_level_checkwx(void)
526 {
527         ptdump_walk_pgd_level_core(NULL, NULL, true);
528 }
529
530 static int __init pt_dump_init(void)
531 {
532         /*
533          * Various markers are not compile-time constants, so assign them
534          * here.
535          */
536 #ifdef CONFIG_X86_64
537         address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET;
538         address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
539         address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START;
540 #endif
541 #ifdef CONFIG_X86_32
542         address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
543         address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
544 # ifdef CONFIG_HIGHMEM
545         address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
546 # endif
547         address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
548         address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE;
549 #endif
550         return 0;
551 }
552 __initcall(pt_dump_init);