debug_pagetables.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <linux/debugfs.h>
  2. #include <linux/efi.h>
  3. #include <linux/module.h>
  4. #include <linux/seq_file.h>
  5. #include <asm/pgtable.h>
  6. static int ptdump_show(struct seq_file *m, void *v)
  7. {
  8. ptdump_walk_pgd_level_debugfs(m, NULL, false);
  9. return 0;
  10. }
  11. static int ptdump_open(struct inode *inode, struct file *filp)
  12. {
  13. return single_open(filp, ptdump_show, NULL);
  14. }
  15. static const struct file_operations ptdump_fops = {
  16. .owner = THIS_MODULE,
  17. .open = ptdump_open,
  18. .read = seq_read,
  19. .llseek = seq_lseek,
  20. .release = single_release,
  21. };
  22. static int ptdump_show_curknl(struct seq_file *m, void *v)
  23. {
  24. if (current->mm->pgd) {
  25. down_read(&current->mm->mmap_sem);
  26. ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, false);
  27. up_read(&current->mm->mmap_sem);
  28. }
  29. return 0;
  30. }
  31. static int ptdump_open_curknl(struct inode *inode, struct file *filp)
  32. {
  33. return single_open(filp, ptdump_show_curknl, NULL);
  34. }
  35. static const struct file_operations ptdump_curknl_fops = {
  36. .owner = THIS_MODULE,
  37. .open = ptdump_open_curknl,
  38. .read = seq_read,
  39. .llseek = seq_lseek,
  40. .release = single_release,
  41. };
  42. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  43. static struct dentry *pe_curusr;
  44. static int ptdump_show_curusr(struct seq_file *m, void *v)
  45. {
  46. if (current->mm->pgd) {
  47. down_read(&current->mm->mmap_sem);
  48. ptdump_walk_pgd_level_debugfs(m, current->mm->pgd, true);
  49. up_read(&current->mm->mmap_sem);
  50. }
  51. return 0;
  52. }
  53. static int ptdump_open_curusr(struct inode *inode, struct file *filp)
  54. {
  55. return single_open(filp, ptdump_show_curusr, NULL);
  56. }
  57. static const struct file_operations ptdump_curusr_fops = {
  58. .owner = THIS_MODULE,
  59. .open = ptdump_open_curusr,
  60. .read = seq_read,
  61. .llseek = seq_lseek,
  62. .release = single_release,
  63. };
  64. #endif
  65. #if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
  66. static struct dentry *pe_efi;
  67. static int ptdump_show_efi(struct seq_file *m, void *v)
  68. {
  69. if (efi_mm.pgd)
  70. ptdump_walk_pgd_level_debugfs(m, efi_mm.pgd, false);
  71. return 0;
  72. }
  73. static int ptdump_open_efi(struct inode *inode, struct file *filp)
  74. {
  75. return single_open(filp, ptdump_show_efi, NULL);
  76. }
  77. static const struct file_operations ptdump_efi_fops = {
  78. .owner = THIS_MODULE,
  79. .open = ptdump_open_efi,
  80. .read = seq_read,
  81. .llseek = seq_lseek,
  82. .release = single_release,
  83. };
  84. #endif
  85. static struct dentry *dir, *pe_knl, *pe_curknl;
  86. static int __init pt_dump_debug_init(void)
  87. {
  88. dir = debugfs_create_dir("page_tables", NULL);
  89. if (!dir)
  90. return -ENOMEM;
  91. pe_knl = debugfs_create_file("kernel", 0400, dir, NULL,
  92. &ptdump_fops);
  93. if (!pe_knl)
  94. goto err;
  95. pe_curknl = debugfs_create_file("current_kernel", 0400,
  96. dir, NULL, &ptdump_curknl_fops);
  97. if (!pe_curknl)
  98. goto err;
  99. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  100. pe_curusr = debugfs_create_file("current_user", 0400,
  101. dir, NULL, &ptdump_curusr_fops);
  102. if (!pe_curusr)
  103. goto err;
  104. #endif
  105. #if defined(CONFIG_EFI) && defined(CONFIG_X86_64)
  106. pe_efi = debugfs_create_file("efi", 0400, dir, NULL, &ptdump_efi_fops);
  107. if (!pe_efi)
  108. goto err;
  109. #endif
  110. return 0;
  111. err:
  112. debugfs_remove_recursive(dir);
  113. return -ENOMEM;
  114. }
  115. static void __exit pt_dump_debug_exit(void)
  116. {
  117. debugfs_remove_recursive(dir);
  118. }
  119. module_init(pt_dump_debug_init);
  120. module_exit(pt_dump_debug_exit);
  121. MODULE_LICENSE("GPL");
  122. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
  123. MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");