ring0.c 840 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. This illustrates operations which are only possible in ring 0.
  3. https://stackoverflow.com/questions/7415515/how-to-access-the-control-registers-cr0-cr2-cr3-from-a-program-getting-segmenta/7419306#7419306
  4. It only works for x86_64.
  5. Then try to run this on userland and see the process be killed:
  6. /ring0.out
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include "ring0.h"
  11. static int myinit(void)
  12. {
  13. #if defined(__x86_64__) || defined(__i386__)
  14. Ring0Regs ring0_regs;
  15. ring0_get_control_regs(&ring0_regs);
  16. pr_info("cr0 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr0);
  17. pr_info("cr2 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr2);
  18. pr_info("cr3 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr3);
  19. #endif
  20. return 0;
  21. }
  22. static void myexit(void) {}
  23. module_init(myinit)
  24. module_exit(myexit)
  25. MODULE_LICENSE("GPL");