1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun #include <linux/debugfs.h>
3*4882a593Smuzhiyun #include <linux/efi.h>
4*4882a593Smuzhiyun #include <linux/module.h>
5*4882a593Smuzhiyun #include <linux/seq_file.h>
6*4882a593Smuzhiyun #include <linux/pgtable.h>
7*4882a593Smuzhiyun
ptdump_show(struct seq_file * m,void * v)8*4882a593Smuzhiyun static int ptdump_show(struct seq_file *m, void *v)
9*4882a593Smuzhiyun {
10*4882a593Smuzhiyun ptdump_walk_pgd_level_debugfs(m, &init_mm, false);
11*4882a593Smuzhiyun return 0;
12*4882a593Smuzhiyun }
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(ptdump);
15*4882a593Smuzhiyun
ptdump_curknl_show(struct seq_file * m,void * v)16*4882a593Smuzhiyun static int ptdump_curknl_show(struct seq_file *m, void *v)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun if (current->mm->pgd)
19*4882a593Smuzhiyun ptdump_walk_pgd_level_debugfs(m, current->mm, false);
20*4882a593Smuzhiyun return 0;
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(ptdump_curknl);
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #ifdef CONFIG_PAGE_TABLE_ISOLATION
ptdump_curusr_show(struct seq_file * m,void * v)26*4882a593Smuzhiyun static int ptdump_curusr_show(struct seq_file *m, void *v)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun if (current->mm->pgd)
29*4882a593Smuzhiyun ptdump_walk_pgd_level_debugfs(m, current->mm, true);
30*4882a593Smuzhiyun return 0;
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(ptdump_curusr);
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
ptdump_efi_show(struct seq_file * m,void * v)37*4882a593Smuzhiyun static int ptdump_efi_show(struct seq_file *m, void *v)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun if (efi_mm.pgd)
40*4882a593Smuzhiyun ptdump_walk_pgd_level_debugfs(m, &efi_mm, false);
41*4882a593Smuzhiyun return 0;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(ptdump_efi);
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun static struct dentry *dir;
48*4882a593Smuzhiyun
pt_dump_debug_init(void)49*4882a593Smuzhiyun static int __init pt_dump_debug_init(void)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun dir = debugfs_create_dir("page_tables", NULL);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun debugfs_create_file("kernel", 0400, dir, NULL, &ptdump_fops);
54*4882a593Smuzhiyun debugfs_create_file("current_kernel", 0400, dir, NULL,
55*4882a593Smuzhiyun &ptdump_curknl_fops);
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #ifdef CONFIG_PAGE_TABLE_ISOLATION
58*4882a593Smuzhiyun debugfs_create_file("current_user", 0400, dir, NULL,
59*4882a593Smuzhiyun &ptdump_curusr_fops);
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun #if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
62*4882a593Smuzhiyun debugfs_create_file("efi", 0400, dir, NULL, &ptdump_efi_fops);
63*4882a593Smuzhiyun #endif
64*4882a593Smuzhiyun return 0;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
pt_dump_debug_exit(void)67*4882a593Smuzhiyun static void __exit pt_dump_debug_exit(void)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun debugfs_remove_recursive(dir);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun module_init(pt_dump_debug_init);
73*4882a593Smuzhiyun module_exit(pt_dump_debug_exit);
74*4882a593Smuzhiyun MODULE_LICENSE("GPL");
75*4882a593Smuzhiyun MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
76*4882a593Smuzhiyun MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");
77