blob: 51a6f92da2bf1670d1855f64bd5fb74789c98e19 [file] [log] [blame]
Kees Cook8609d1b2015-11-19 17:07:55 -08001#include <linux/debugfs.h>
2#include <linux/module.h>
3#include <linux/seq_file.h>
4#include <asm/pgtable.h>
5
6static int ptdump_show(struct seq_file *m, void *v)
7{
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +01008 ptdump_walk_pgd_level_debugfs(m, NULL, false);
Kees Cook8609d1b2015-11-19 17:07:55 -08009 return 0;
10}
11
12static int ptdump_open(struct inode *inode, struct file *filp)
13{
14 return single_open(filp, ptdump_show, NULL);
15}
16
17static const struct file_operations ptdump_fops = {
18 .owner = THIS_MODULE,
19 .open = ptdump_open,
20 .read = seq_read,
21 .llseek = seq_lseek,
22 .release = single_release,
23};
24
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +010025static int ptdump_show_curknl(struct seq_file *m, void *v)
26{
27 if (current->mm->pgd) {
28 down_read(&current->mm->mmap_sem);
29 ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, false);
30 up_read(&current->mm->mmap_sem);
31 }
32 return 0;
33}
34
35static int ptdump_open_curknl(struct inode *inode, struct file *filp)
36{
37 return single_open(filp, ptdump_show_curknl, NULL);
38}
39
40static const struct file_operations ptdump_curknl_fops = {
41 .owner = THIS_MODULE,
42 .open = ptdump_open_curknl,
43 .read = seq_read,
44 .llseek = seq_lseek,
45 .release = single_release,
46};
47
48#ifdef CONFIG_PAGE_TABLE_ISOLATION
49static struct dentry *pe_curusr;
50
51static int ptdump_show_curusr(struct seq_file *m, void *v)
52{
53 if (current->mm->pgd) {
54 down_read(&current->mm->mmap_sem);
55 ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, true);
56 up_read(&current->mm->mmap_sem);
57 }
58 return 0;
59}
60
61static int ptdump_open_curusr(struct inode *inode, struct file *filp)
62{
63 return single_open(filp, ptdump_show_curusr, NULL);
64}
65
66static const struct file_operations ptdump_curusr_fops = {
67 .owner = THIS_MODULE,
68 .open = ptdump_open_curusr,
69 .read = seq_read,
70 .llseek = seq_lseek,
71 .release = single_release,
72};
73#endif
74
Andy Lutomirski116fef62018-01-31 07:56:22 -080075#if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
76extern pgd_t *efi_pgd;
77static struct dentry *pe_efi;
78
79static int ptdump_show_efi(struct seq_file *m, void *v)
80{
81 if (efi_pgd)
82 ptdump_walk_pgd_level_debugfs(m, efi_pgd, false);
83 return 0;
84}
85
86static int ptdump_open_efi(struct inode *inode, struct file *filp)
87{
88 return single_open(filp, ptdump_show_efi, NULL);
89}
90
91static const struct file_operations ptdump_efi_fops = {
92 .owner = THIS_MODULE,
93 .open = ptdump_open_efi,
94 .read = seq_read,
95 .llseek = seq_lseek,
96 .release = single_release,
97};
98#endif
99
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +0100100static struct dentry *dir, *pe_knl, *pe_curknl;
Kees Cook8609d1b2015-11-19 17:07:55 -0800101
102static int __init pt_dump_debug_init(void)
103{
Borislav Petkov75298aa12017-12-04 15:08:04 +0100104 dir = debugfs_create_dir("page_tables", NULL);
105 if (!dir)
Kees Cook8609d1b2015-11-19 17:07:55 -0800106 return -ENOMEM;
107
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +0100108 pe_knl = debugfs_create_file("kernel", 0400, dir, NULL,
109 &ptdump_fops);
110 if (!pe_knl)
Borislav Petkov75298aa12017-12-04 15:08:04 +0100111 goto err;
Thomas Gleixnera4b51ef2017-12-04 15:08:06 +0100112
113 pe_curknl = debugfs_create_file("current_kernel", 0400,
114 dir, NULL, &ptdump_curknl_fops);
115 if (!pe_curknl)
116 goto err;
117
118#ifdef CONFIG_PAGE_TABLE_ISOLATION
119 pe_curusr = debugfs_create_file("current_user", 0400,
120 dir, NULL, &ptdump_curusr_fops);
121 if (!pe_curusr)
122 goto err;
123#endif
Andy Lutomirski116fef62018-01-31 07:56:22 -0800124
125#if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
126 pe_efi = debugfs_create_file("efi", 0400, dir, NULL, &ptdump_efi_fops);
127 if (!pe_efi)
128 goto err;
129#endif
130
Kees Cook8609d1b2015-11-19 17:07:55 -0800131 return 0;
Borislav Petkov75298aa12017-12-04 15:08:04 +0100132err:
133 debugfs_remove_recursive(dir);
134 return -ENOMEM;
Kees Cook8609d1b2015-11-19 17:07:55 -0800135}
136
137static void __exit pt_dump_debug_exit(void)
138{
Borislav Petkov75298aa12017-12-04 15:08:04 +0100139 debugfs_remove_recursive(dir);
Kees Cook8609d1b2015-11-19 17:07:55 -0800140}
141
142module_init(pt_dump_debug_init);
143module_exit(pt_dump_debug_exit);
144MODULE_LICENSE("GPL");
145MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
146MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");