]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/x86/mm/dump_pagetables.c
Merge branches 'pm-core', 'pm-qos', 'pm-domains' and 'pm-opp'
[linux.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/mm.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/seq_file.h>
20
21 #include <asm/kasan.h>
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 /* indices for address_markers; keep sync'd w/ address_markers below */
48 enum address_markers_idx {
49         USER_SPACE_NR = 0,
50 #ifdef CONFIG_X86_64
51         KERNEL_SPACE_NR,
52         LOW_KERNEL_NR,
53         VMALLOC_START_NR,
54         VMEMMAP_START_NR,
55 #ifdef CONFIG_KASAN
56         KASAN_SHADOW_START_NR,
57         KASAN_SHADOW_END_NR,
58 #endif
59 # ifdef CONFIG_X86_ESPFIX64
60         ESPFIX_START_NR,
61 # endif
62         HIGH_KERNEL_NR,
63         MODULES_VADDR_NR,
64         MODULES_END_NR,
65 #else
66         KERNEL_SPACE_NR,
67         VMALLOC_START_NR,
68         VMALLOC_END_NR,
69 # ifdef CONFIG_HIGHMEM
70         PKMAP_BASE_NR,
71 # endif
72         FIXADDR_START_NR,
73 #endif
74 };
75
76 /* Address space markers hints */
77 static struct addr_marker address_markers[] = {
78         { 0, "User Space" },
79 #ifdef CONFIG_X86_64
80         { 0x8000000000000000UL, "Kernel Space" },
81         { 0/* PAGE_OFFSET */,   "Low Kernel Mapping" },
82         { 0/* VMALLOC_START */, "vmalloc() Area" },
83         { 0/* VMEMMAP_START */, "Vmemmap" },
84 #ifdef CONFIG_KASAN
85         { KASAN_SHADOW_START,   "KASAN shadow" },
86         { KASAN_SHADOW_END,     "KASAN shadow end" },
87 #endif
88 # ifdef CONFIG_X86_ESPFIX64
89         { ESPFIX_BASE_ADDR,     "ESPfix Area", 16 },
90 # endif
91 # ifdef CONFIG_EFI
92         { EFI_VA_END,           "EFI Runtime Services" },
93 # endif
94         { __START_KERNEL_map,   "High Kernel Mapping" },
95         { MODULES_VADDR,        "Modules" },
96         { MODULES_END,          "End Modules" },
97 #else
98         { PAGE_OFFSET,          "Kernel Mapping" },
99         { 0/* VMALLOC_START */, "vmalloc() Area" },
100         { 0/*VMALLOC_END*/,     "vmalloc() End" },
101 # ifdef CONFIG_HIGHMEM
102         { 0/*PKMAP_BASE*/,      "Persistent kmap() Area" },
103 # endif
104         { 0/*FIXADDR_START*/,   "Fixmap Area" },
105 #endif
106         { -1, NULL }            /* End of list */
107 };
108
109 /* Multipliers for offsets within the PTEs */
110 #define PTE_LEVEL_MULT (PAGE_SIZE)
111 #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
112 #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
113 #define PGD_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
114
115 #define pt_dump_seq_printf(m, to_dmesg, fmt, args...)           \
116 ({                                                              \
117         if (to_dmesg)                                   \
118                 printk(KERN_INFO fmt, ##args);                  \
119         else                                                    \
120                 if (m)                                          \
121                         seq_printf(m, fmt, ##args);             \
122 })
123
124 #define pt_dump_cont_printf(m, to_dmesg, fmt, args...)          \
125 ({                                                              \
126         if (to_dmesg)                                   \
127                 printk(KERN_CONT fmt, ##args);                  \
128         else                                                    \
129                 if (m)                                          \
130                         seq_printf(m, fmt, ##args);             \
131 })
132
133 /*
134  * Print a readable form of a pgprot_t to the seq_file
135  */
136 static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg)
137 {
138         pgprotval_t pr = pgprot_val(prot);
139         static const char * const level_name[] =
140                 { "cr3", "pgd", "pud", "pmd", "pte" };
141
142         if (!pgprot_val(prot)) {
143                 /* Not present */
144                 pt_dump_cont_printf(m, dmsg, "                              ");
145         } else {
146                 if (pr & _PAGE_USER)
147                         pt_dump_cont_printf(m, dmsg, "USR ");
148                 else
149                         pt_dump_cont_printf(m, dmsg, "    ");
150                 if (pr & _PAGE_RW)
151                         pt_dump_cont_printf(m, dmsg, "RW ");
152                 else
153                         pt_dump_cont_printf(m, dmsg, "ro ");
154                 if (pr & _PAGE_PWT)
155                         pt_dump_cont_printf(m, dmsg, "PWT ");
156                 else
157                         pt_dump_cont_printf(m, dmsg, "    ");
158                 if (pr & _PAGE_PCD)
159                         pt_dump_cont_printf(m, dmsg, "PCD ");
160                 else
161                         pt_dump_cont_printf(m, dmsg, "    ");
162
163                 /* Bit 7 has a different meaning on level 3 vs 4 */
164                 if (level <= 3 && pr & _PAGE_PSE)
165                         pt_dump_cont_printf(m, dmsg, "PSE ");
166                 else
167                         pt_dump_cont_printf(m, dmsg, "    ");
168                 if ((level == 4 && pr & _PAGE_PAT) ||
169                     ((level == 3 || level == 2) && pr & _PAGE_PAT_LARGE))
170                         pt_dump_cont_printf(m, dmsg, "PAT ");
171                 else
172                         pt_dump_cont_printf(m, dmsg, "    ");
173                 if (pr & _PAGE_GLOBAL)
174                         pt_dump_cont_printf(m, dmsg, "GLB ");
175                 else
176                         pt_dump_cont_printf(m, dmsg, "    ");
177                 if (pr & _PAGE_NX)
178                         pt_dump_cont_printf(m, dmsg, "NX ");
179                 else
180                         pt_dump_cont_printf(m, dmsg, "x  ");
181         }
182         pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
183 }
184
185 /*
186  * On 64 bits, sign-extend the 48 bit address to 64 bit
187  */
188 static unsigned long normalize_addr(unsigned long u)
189 {
190 #ifdef CONFIG_X86_64
191         return (signed long)(u << 16) >> 16;
192 #else
193         return u;
194 #endif
195 }
196
197 /*
198  * This function gets called on a break in a continuous series
199  * of PTE entries; the next one is different so we need to
200  * print what we collected so far.
201  */
202 static void note_page(struct seq_file *m, struct pg_state *st,
203                       pgprot_t new_prot, int level)
204 {
205         pgprotval_t prot, cur;
206         static const char units[] = "BKMGTPE";
207
208         /*
209          * If we have a "break" in the series, we need to flush the state that
210          * we have now. "break" is either changing perms, levels or
211          * address space marker.
212          */
213         prot = pgprot_val(new_prot);
214         cur = pgprot_val(st->current_prot);
215
216         if (!st->level) {
217                 /* First entry */
218                 st->current_prot = new_prot;
219                 st->level = level;
220                 st->marker = address_markers;
221                 st->lines = 0;
222                 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
223                                    st->marker->name);
224         } else if (prot != cur || level != st->level ||
225                    st->current_address >= st->marker[1].start_address) {
226                 const char *unit = units;
227                 unsigned long delta;
228                 int width = sizeof(unsigned long) * 2;
229                 pgprotval_t pr = pgprot_val(st->current_prot);
230
231                 if (st->check_wx && (pr & _PAGE_RW) && !(pr & _PAGE_NX)) {
232                         WARN_ONCE(1,
233                                   "x86/mm: Found insecure W+X mapping at address %p/%pS\n",
234                                   (void *)st->start_address,
235                                   (void *)st->start_address);
236                         st->wx_pages += (st->current_address -
237                                          st->start_address) / PAGE_SIZE;
238                 }
239
240                 /*
241                  * Now print the actual finished series
242                  */
243                 if (!st->marker->max_lines ||
244                     st->lines < st->marker->max_lines) {
245                         pt_dump_seq_printf(m, st->to_dmesg,
246                                            "0x%0*lx-0x%0*lx   ",
247                                            width, st->start_address,
248                                            width, st->current_address);
249
250                         delta = st->current_address - st->start_address;
251                         while (!(delta & 1023) && unit[1]) {
252                                 delta >>= 10;
253                                 unit++;
254                         }
255                         pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
256                                             delta, *unit);
257                         printk_prot(m, st->current_prot, st->level,
258                                     st->to_dmesg);
259                 }
260                 st->lines++;
261
262                 /*
263                  * We print markers for special areas of address space,
264                  * such as the start of vmalloc space etc.
265                  * This helps in the interpretation.
266                  */
267                 if (st->current_address >= st->marker[1].start_address) {
268                         if (st->marker->max_lines &&
269                             st->lines > st->marker->max_lines) {
270                                 unsigned long nskip =
271                                         st->lines - st->marker->max_lines;
272                                 pt_dump_seq_printf(m, st->to_dmesg,
273                                                    "... %lu entr%s skipped ... \n",
274                                                    nskip,
275                                                    nskip == 1 ? "y" : "ies");
276                         }
277                         st->marker++;
278                         st->lines = 0;
279                         pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
280                                            st->marker->name);
281                 }
282
283                 st->start_address = st->current_address;
284                 st->current_prot = new_prot;
285                 st->level = level;
286         }
287 }
288
289 static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr,
290                                                         unsigned long P)
291 {
292         int i;
293         pte_t *start;
294         pgprotval_t prot;
295
296         start = (pte_t *) pmd_page_vaddr(addr);
297         for (i = 0; i < PTRS_PER_PTE; i++) {
298                 prot = pte_flags(*start);
299                 st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT);
300                 note_page(m, st, __pgprot(prot), 4);
301                 start++;
302         }
303 }
304
305 #if PTRS_PER_PMD > 1
306
307 static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr,
308                                                         unsigned long P)
309 {
310         int i;
311         pmd_t *start;
312         pgprotval_t prot;
313
314         start = (pmd_t *) pud_page_vaddr(addr);
315         for (i = 0; i < PTRS_PER_PMD; i++) {
316                 st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT);
317                 if (!pmd_none(*start)) {
318                         if (pmd_large(*start) || !pmd_present(*start)) {
319                                 prot = pmd_flags(*start);
320                                 note_page(m, st, __pgprot(prot), 3);
321                         } else {
322                                 walk_pte_level(m, st, *start,
323                                                P + i * PMD_LEVEL_MULT);
324                         }
325                 } else
326                         note_page(m, st, __pgprot(0), 3);
327                 start++;
328         }
329 }
330
331 #else
332 #define walk_pmd_level(m,s,a,p) walk_pte_level(m,s,__pmd(pud_val(a)),p)
333 #define pud_large(a) pmd_large(__pmd(pud_val(a)))
334 #define pud_none(a)  pmd_none(__pmd(pud_val(a)))
335 #endif
336
337 #if PTRS_PER_PUD > 1
338
339 /*
340  * This is an optimization for CONFIG_DEBUG_WX=y + CONFIG_KASAN=y
341  * KASAN fills page tables with the same values. Since there is no
342  * point in checking page table more than once we just skip repeated
343  * entries. This saves us dozens of seconds during boot.
344  */
345 static bool pud_already_checked(pud_t *prev_pud, pud_t *pud, bool checkwx)
346 {
347         return checkwx && prev_pud && (pud_val(*prev_pud) == pud_val(*pud));
348 }
349
350 static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
351                                                         unsigned long P)
352 {
353         int i;
354         pud_t *start;
355         pgprotval_t prot;
356         pud_t *prev_pud = NULL;
357
358         start = (pud_t *) pgd_page_vaddr(addr);
359
360         for (i = 0; i < PTRS_PER_PUD; i++) {
361                 st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
362                 if (!pud_none(*start) &&
363                     !pud_already_checked(prev_pud, start, st->check_wx)) {
364                         if (pud_large(*start) || !pud_present(*start)) {
365                                 prot = pud_flags(*start);
366                                 note_page(m, st, __pgprot(prot), 2);
367                         } else {
368                                 walk_pmd_level(m, st, *start,
369                                                P + i * PUD_LEVEL_MULT);
370                         }
371                 } else
372                         note_page(m, st, __pgprot(0), 2);
373
374                 prev_pud = start;
375                 start++;
376         }
377 }
378
379 #else
380 #define walk_pud_level(m,s,a,p) walk_pmd_level(m,s,__pud(pgd_val(a)),p)
381 #define pgd_large(a) pud_large(__pud(pgd_val(a)))
382 #define pgd_none(a)  pud_none(__pud(pgd_val(a)))
383 #endif
384
385 static inline bool is_hypervisor_range(int idx)
386 {
387 #ifdef CONFIG_X86_64
388         /*
389          * ffff800000000000 - ffff87ffffffffff is reserved for
390          * the hypervisor.
391          */
392         return  (idx >= pgd_index(__PAGE_OFFSET) - 16) &&
393                 (idx <  pgd_index(__PAGE_OFFSET));
394 #else
395         return false;
396 #endif
397 }
398
399 static void ptdump_walk_pgd_level_core(struct seq_file *m, pgd_t *pgd,
400                                        bool checkwx)
401 {
402 #ifdef CONFIG_X86_64
403         pgd_t *start = (pgd_t *) &init_level4_pgt;
404 #else
405         pgd_t *start = swapper_pg_dir;
406 #endif
407         pgprotval_t prot;
408         int i;
409         struct pg_state st = {};
410
411         if (pgd) {
412                 start = pgd;
413                 st.to_dmesg = true;
414         }
415
416         st.check_wx = checkwx;
417         if (checkwx)
418                 st.wx_pages = 0;
419
420         for (i = 0; i < PTRS_PER_PGD; i++) {
421                 st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
422                 if (!pgd_none(*start) && !is_hypervisor_range(i)) {
423                         if (pgd_large(*start) || !pgd_present(*start)) {
424                                 prot = pgd_flags(*start);
425                                 note_page(m, &st, __pgprot(prot), 1);
426                         } else {
427                                 walk_pud_level(m, &st, *start,
428                                                i * PGD_LEVEL_MULT);
429                         }
430                 } else
431                         note_page(m, &st, __pgprot(0), 1);
432
433                 cond_resched();
434                 start++;
435         }
436
437         /* Flush out the last page */
438         st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT);
439         note_page(m, &st, __pgprot(0), 0);
440         if (!checkwx)
441                 return;
442         if (st.wx_pages)
443                 pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n",
444                         st.wx_pages);
445         else
446                 pr_info("x86/mm: Checked W+X mappings: passed, no W+X pages found.\n");
447 }
448
449 void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd)
450 {
451         ptdump_walk_pgd_level_core(m, pgd, false);
452 }
453 EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level);
454
455 void ptdump_walk_pgd_level_checkwx(void)
456 {
457         ptdump_walk_pgd_level_core(NULL, NULL, true);
458 }
459
460 static int __init pt_dump_init(void)
461 {
462         /*
463          * Various markers are not compile-time constants, so assign them
464          * here.
465          */
466 #ifdef CONFIG_X86_64
467         address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET;
468         address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
469         address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START;
470 #endif
471 #ifdef CONFIG_X86_32
472         address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
473         address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
474 # ifdef CONFIG_HIGHMEM
475         address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
476 # endif
477         address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
478 #endif
479
480         return 0;
481 }
482 __initcall(pt_dump_init);