Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 2 | #include <linux/debugfs.h> |
Sai Praneeth | 3ede341 | 2018-03-12 09:43:54 +0000 | [diff] [blame] | 3 | #include <linux/efi.h> |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 4 | #include <linux/module.h> |
| 5 | #include <linux/seq_file.h> |
| 6 | #include <asm/pgtable.h> |
| 7 | |
| 8 | static int ptdump_show(struct seq_file *m, void *v) |
| 9 | { |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 10 | ptdump_walk_pgd_level_debugfs(m, NULL, false); |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 11 | return 0; |
| 12 | } |
| 13 | |
Yangtao Li | 6848ac7 | 2018-11-19 10:43:34 -0500 | [diff] [blame] | 14 | DEFINE_SHOW_ATTRIBUTE(ptdump); |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 15 | |
Yangtao Li | 6848ac7 | 2018-11-19 10:43:34 -0500 | [diff] [blame] | 16 | static int ptdump_curknl_show(struct seq_file *m, void *v) |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 17 | { |
| 18 | if (current->mm->pgd) { |
| 19 | down_read(¤t->mm->mmap_sem); |
| 20 | ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, false); |
| 21 | up_read(¤t->mm->mmap_sem); |
| 22 | } |
| 23 | return 0; |
| 24 | } |
| 25 | |
Yangtao Li | 6848ac7 | 2018-11-19 10:43:34 -0500 | [diff] [blame] | 26 | DEFINE_SHOW_ATTRIBUTE(ptdump_curknl); |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 27 | |
| 28 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
| 29 | static struct dentry *pe_curusr; |
| 30 | |
Yangtao Li | 6848ac7 | 2018-11-19 10:43:34 -0500 | [diff] [blame] | 31 | static int ptdump_curusr_show(struct seq_file *m, void *v) |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 32 | { |
| 33 | if (current->mm->pgd) { |
| 34 | down_read(¤t->mm->mmap_sem); |
| 35 | ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, true); |
| 36 | up_read(¤t->mm->mmap_sem); |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | |
Yangtao Li | 6848ac7 | 2018-11-19 10:43:34 -0500 | [diff] [blame] | 41 | DEFINE_SHOW_ATTRIBUTE(ptdump_curusr); |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 42 | #endif |
| 43 | |
Andy Lutomirski | 116fef6 | 2018-01-31 07:56:22 -0800 | [diff] [blame] | 44 | #if defined(CONFIG_EFI) && defined(CONFIG_X86_64) |
Andy Lutomirski | 116fef6 | 2018-01-31 07:56:22 -0800 | [diff] [blame] | 45 | static struct dentry *pe_efi; |
| 46 | |
Yangtao Li | 6848ac7 | 2018-11-19 10:43:34 -0500 | [diff] [blame] | 47 | static int ptdump_efi_show(struct seq_file *m, void *v) |
Andy Lutomirski | 116fef6 | 2018-01-31 07:56:22 -0800 | [diff] [blame] | 48 | { |
Sai Praneeth | 3ede341 | 2018-03-12 09:43:54 +0000 | [diff] [blame] | 49 | if (efi_mm.pgd) |
| 50 | ptdump_walk_pgd_level_debugfs(m, efi_mm.pgd, false); |
Andy Lutomirski | 116fef6 | 2018-01-31 07:56:22 -0800 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Yangtao Li | 6848ac7 | 2018-11-19 10:43:34 -0500 | [diff] [blame] | 54 | DEFINE_SHOW_ATTRIBUTE(ptdump_efi); |
Andy Lutomirski | 116fef6 | 2018-01-31 07:56:22 -0800 | [diff] [blame] | 55 | #endif |
| 56 | |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 57 | static struct dentry *dir, *pe_knl, *pe_curknl; |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 58 | |
| 59 | static int __init pt_dump_debug_init(void) |
| 60 | { |
Borislav Petkov | 75298aa1 | 2017-12-04 15:08:04 +0100 | [diff] [blame] | 61 | dir = debugfs_create_dir("page_tables", NULL); |
| 62 | if (!dir) |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 63 | return -ENOMEM; |
| 64 | |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 65 | pe_knl = debugfs_create_file("kernel", 0400, dir, NULL, |
| 66 | &ptdump_fops); |
| 67 | if (!pe_knl) |
Borislav Petkov | 75298aa1 | 2017-12-04 15:08:04 +0100 | [diff] [blame] | 68 | goto err; |
Thomas Gleixner | a4b51ef | 2017-12-04 15:08:06 +0100 | [diff] [blame] | 69 | |
| 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 Lutomirski | 116fef6 | 2018-01-31 07:56:22 -0800 | [diff] [blame] | 81 | |
| 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 Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 88 | return 0; |
Borislav Petkov | 75298aa1 | 2017-12-04 15:08:04 +0100 | [diff] [blame] | 89 | err: |
| 90 | debugfs_remove_recursive(dir); |
| 91 | return -ENOMEM; |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void __exit pt_dump_debug_exit(void) |
| 95 | { |
Borislav Petkov | 75298aa1 | 2017-12-04 15:08:04 +0100 | [diff] [blame] | 96 | debugfs_remove_recursive(dir); |
Kees Cook | 8609d1b | 2015-11-19 17:07:55 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | module_init(pt_dump_debug_init); |
| 100 | module_exit(pt_dump_debug_exit); |
| 101 | MODULE_LICENSE("GPL"); |
| 102 | MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>"); |
| 103 | MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables"); |