pmccntr.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#arm-pmccntr-register */
  2. #include <linux/debugfs.h>
  3. #include <linux/errno.h> /* EFAULT */
  4. #include <linux/fs.h>
  5. #include <linux/module.h>
  6. #include <linux/printk.h> /* pr_info */
  7. #include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
  8. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  9. #include <uapi/linux/stat.h> /* S_IRUSR */
  10. static struct dentry *debugfs_file;
  11. static int show(struct seq_file *m, void *v)
  12. {
  13. u32 pmccntr;
  14. #if defined(__arm__)
  15. /* Invalid aarch64 asm. */
  16. /* TODO Internal error: Oops - undefined instruction: 0 [#1] ARM */
  17. /* Enable userland access to conter. */
  18. /* PMUSERENR = 1 */
  19. /*__asm__ __volatile__ ("mcr p15, 0, %0, c9, c14, 0" :: "r"(1));*/
  20. /* TODO oops undefined instruction. */
  21. /* PMCR.E (bit 0) = 1 */
  22. /*__asm__ __volatile__ ("mcr p15, 0, %0, c9, c12, 0" :: "r"(1));*/
  23. /* TODO oops undefined instruction. */
  24. /* Enable counter. */
  25. /* PMCNTENSET.C (bit 31) = 1 */
  26. /*__asm__ __volatile__ ("mcr p15, 0, %0, c9, c12, 1" :: "r"(1 << 31));*/
  27. /* Get counter value. */
  28. __asm__ __volatile__ ("mrc p15, 0, %0, c15, c12, 1" : "=r" (pmccntr));
  29. #else
  30. pmccntr = 0;
  31. #endif
  32. seq_printf(m, "%8.8llX\n", (unsigned long long)pmccntr);
  33. return 0;
  34. }
  35. static int open(struct inode *inode, struct file *file)
  36. {
  37. return single_open(file, show, NULL);
  38. }
  39. static const struct file_operations fops = {
  40. .llseek = seq_lseek,
  41. .open = open,
  42. .owner = THIS_MODULE,
  43. .read = seq_read,
  44. .release = single_release,
  45. };
  46. static int myinit(void)
  47. {
  48. debugfs_file = debugfs_create_file("lkmc_pmccntr", S_IRUSR, NULL, NULL, &fops);
  49. if (!debugfs_file) {
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. static void myexit(void)
  55. {
  56. debugfs_remove(debugfs_file);
  57. }
  58. module_init(myinit)
  59. module_exit(myexit)
  60. MODULE_LICENSE("GPL");