blob: c6f4982d5401403174585b558be2829715e56dd0 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Kees Cook8609d1b2015-11-19 17:07:55 -08002#include <linux/debugfs.h>
Sai Praneeth3ede3412018-03-12 09:43:54 +00003#include <linux/efi.h>
Kees Cook8609d1b2015-11-19 17:07:55 -08004#include <linux/module.h>
5#include <linux/seq_file.h>
6#include <asm/pgtable.h>
7
8static int ptdump_show(struct seq_file *m, void *v)
9{
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010010 ptdump_walk_pgd_level_debugfs(m, NULL, false);
Kees Cook8609d1b2015-11-19 17:07:55 -080011 return 0;
12}
13
Yangtao Li6848ac72018-11-19 10:43:34 -050014DEFINE_SHOW_ATTRIBUTE(ptdump);
Kees Cook8609d1b2015-11-19 17:07:55 -080015
Yangtao Li6848ac72018-11-19 10:43:34 -050016static int ptdump_curknl_show(struct seq_file *m, void *v)
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010017{
18 if (current->mm->pgd) {
19 down_read(&current->mm->mmap_sem);
20 ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, false);
21 up_read(&current->mm->mmap_sem);
22 }
23 return 0;
24}
25
Yangtao Li6848ac72018-11-19 10:43:34 -050026DEFINE_SHOW_ATTRIBUTE(ptdump_curknl);
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010027
28#ifdef CONFIG_PAGE_TABLE_ISOLATION
29static struct dentry *pe_curusr;
30
Yangtao Li6848ac72018-11-19 10:43:34 -050031static int ptdump_curusr_show(struct seq_file *m, void *v)
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010032{
33 if (current->mm->pgd) {
34 down_read(&current->mm->mmap_sem);
35 ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, true);
36 up_read(&current->mm->mmap_sem);
37 }
38 return 0;
39}
40
Yangtao Li6848ac72018-11-19 10:43:34 -050041DEFINE_SHOW_ATTRIBUTE(ptdump_curusr);
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010042#endif
43
Andy Lutomirski116fef62018-01-31 07:56:22 -080044#if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
Andy Lutomirski116fef62018-01-31 07:56:22 -080045static struct dentry *pe_efi;
46
Yangtao Li6848ac72018-11-19 10:43:34 -050047static int ptdump_efi_show(struct seq_file *m, void *v)
Andy Lutomirski116fef62018-01-31 07:56:22 -080048{
Sai Praneeth3ede3412018-03-12 09:43:54 +000049 if (efi_mm.pgd)
50 ptdump_walk_pgd_level_debugfs(m, efi_mm.pgd, false);
Andy Lutomirski116fef62018-01-31 07:56:22 -080051 return 0;
52}
53
Yangtao Li6848ac72018-11-19 10:43:34 -050054DEFINE_SHOW_ATTRIBUTE(ptdump_efi);
Andy Lutomirski116fef62018-01-31 07:56:22 -080055#endif
56
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010057static struct dentry *dir, *pe_knl, *pe_curknl;
Kees Cook8609d1b2015-11-19 17:07:55 -080058
59static int __init pt_dump_debug_init(void)
60{
Borislav Petkov75298aa12017-12-04 15:08:04 +010061 dir = debugfs_create_dir("page_tables", NULL);
62 if (!dir)
Kees Cook8609d1b2015-11-19 17:07:55 -080063 return -ENOMEM;
64
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010065 pe_knl = debugfs_create_file("kernel", 0400, dir, NULL,
66 &ptdump_fops);
67 if (!pe_knl)
Borislav Petkov75298aa12017-12-04 15:08:04 +010068 goto err;
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010069
70 pe_curknl = debugfs_create_file("current_kernel", 0400,
71 dir, NULL, &ptdump_curknl_fops);
72 if (!pe_curknl)
73 goto err;
74
75#ifdef CONFIG_PAGE_TABLE_ISOLATION
76 pe_curusr = debugfs_create_file("current_user", 0400,
77 dir, NULL, &ptdump_curusr_fops);
78 if (!pe_curusr)
79 goto err;
80#endif
Andy Lutomirski116fef62018-01-31 07:56:22 -080081
82#if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
83 pe_efi = debugfs_create_file("efi", 0400, dir, NULL, &ptdump_efi_fops);
84 if (!pe_efi)
85 goto err;
86#endif
87
Kees Cook8609d1b2015-11-19 17:07:55 -080088 return 0;
Borislav Petkov75298aa12017-12-04 15:08:04 +010089err:
90 debugfs_remove_recursive(dir);
91 return -ENOMEM;
Kees Cook8609d1b2015-11-19 17:07:55 -080092}
93
94static void __exit pt_dump_debug_exit(void)
95{
Borislav Petkov75298aa12017-12-04 15:08:04 +010096 debugfs_remove_recursive(dir);
Kees Cook8609d1b2015-11-19 17:07:55 -080097}
98
99module_init(pt_dump_debug_init);
100module_exit(pt_dump_debug_exit);
101MODULE_LICENSE("GPL");
102MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
103MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");