m5ops.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#m5ops-instructions */
  2. #ifndef LKMC_M5OPS_H
  3. #define LKMC_M5OPS_H
  4. #if LKMC_M5OPS_ENABLE
  5. #if defined(__x86_64__)
  6. /* 0x43 is the identifier for CHECKPOINT on all archs.
  7. *
  8. * 0x040F is the magic x86 undefined instruction boilerplate to which the identifier gets added.
  9. *
  10. * D means RDI which is the first argument of a Linux C function call,
  11. * and S means RSI is the second one: these are also used by gem5 as arguments of the m5ops.
  12. */
  13. #define LKMC_M5OPS_CHECKPOINT_ASM mov $0, %rdi; mov $0, %rsi; .word 0x040F; .word 0x0043
  14. #define LKMC_M5OPS_DUMPSTATS_ASM mov $0, %rdi; mov $0, %rsi; .word 0x040F; .word 0x0041
  15. #define LKMC_M5OPS_EXIT_ASM mov $0, %rdi; .word 0x040F; .word 0x0021
  16. #define LKMC_M5OPS_FAIL_1_ASM mov $0, %rdi; mov $1, %rsi; .word 0x040F; .word 0x0022
  17. #define LKMC_M5OPS_RESETSTATS_ASM mov $0, %rdi; mov $0, %rsi; .word 0x040F; .word 0x0040
  18. #define LKMC_M5OPS_CHECKPOINT __asm__ __volatile__ (".word 0x040F; .word 0x0043;" : : "D" (0), "S" (0) :)
  19. #define LKMC_M5OPS_DUMPSTATS __asm__ __volatile__ (".word 0x040F; .word 0x0041;" : : "D" (0), "S" (0) :)
  20. #define LKMC_M5OPS_EXIT __asm__ __volatile__ (".word 0x040F; .word 0x0021;" : : "D" (0) :)
  21. #define LKMC_M5OPS_FAIL_1 __asm__ __volatile__ (".word 0x040F; .word 0x0022;" : : "D" (0), "S" (1) :)
  22. #define LKMC_M5OPS_RESETSTATS __asm__ __volatile__ (".word 0x040F; .word 0x0040;" : : "D" (0), "S" (0) :)
  23. #elif defined(__arm__)
  24. #define LKMC_M5OPS_CHECKPOINT_ASM mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x43 << 16)
  25. #define LKMC_M5OPS_DUMPSTATS_ASM mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x41 << 16)
  26. #define LKMC_M5OPS_EXIT_ASM mov r0, #0; mov r1, #0; .inst 0xEE000110 | (0x21 << 16)
  27. #define LKMC_M5OPS_FAIL_1_ASM mov r0, #0; mov r1, #0; mov r2, #1; mov r3, #0; .inst 0xEE000110 | (0x22 << 16)
  28. #define LKMC_M5OPS_RESETSTATS_ASM mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x40 << 16)
  29. #define LKMC_M5OPS_CHECKPOINT __asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x43 << 16);" : : : "r0", "r1", "r2", "r3")
  30. #define LKMC_M5OPS_DUMPSTATS __asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x41 << 16);" : : : "r0", "r1", "r2", "r3")
  31. #define LKMC_M5OPS_EXIT __asm__ __volatile__ ("mov r0, #0; mov r1, #0; .inst 0xEE000110 | (0x21 << 16);" : : : "r0", "r1" )
  32. #define LKMC_M5OPS_FAIL_1 __asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #1; mov r3, #0; .inst 0xEE000110 | (0x22 << 16);" : : : "r0", "r1", "r2", "r3")
  33. #define LKMC_M5OPS_RESETSTATS __asm__ __volatile__ ("mov r0, #0; mov r1, #0; mov r2, #0; mov r3, #0; .inst 0xEE000110 | (0x40 << 16);" : : : "r0", "r1", "r2", "r3")
  34. #elif defined(__aarch64__)
  35. #define LKMC_M5OPS_CHECKPOINT_ASM mov x0, 0; mov x1, 0; .inst 0xFF000110 | (0x43 << 16);
  36. #define LKMC_M5OPS_DUMPSTATS_ASM mov x0, 0; mov x1, 0; .inst 0xFF000110 | (0x41 << 16);
  37. #define LKMC_M5OPS_EXIT_ASM mov x0, 0; .inst 0XFF000110 | (0x21 << 16);
  38. #define LKMC_M5OPS_FAIL_1_ASM mov x0, 0; mov x1, 1; .inst 0xFF000110 | (0x22 << 16);
  39. #define LKMC_M5OPS_RESETSTATS_ASM mov x0, 0; mov x1, 0; .inst 0XFF000110 | (0x40 << 16);
  40. #define LKMC_M5OPS_CHECKPOINT __asm__ __volatile__ ("mov x0, 0; mov x1, 0; .inst 0xFF000110 | (0x43 << 16);" : : : "x0", "x1")
  41. #define LKMC_M5OPS_DUMPSTATS __asm__ __volatile__ ("mov x0, 0; mov x1, 0; .inst 0xFF000110 | (0x41 << 16);" : : : "x0", "x1")
  42. #define LKMC_M5OPS_EXIT __asm__ __volatile__ ("mov x0, 0; .inst 0XFF000110 | (0x21 << 16);" : : : "x0" )
  43. #define LKMC_M5OPS_FAIL_1 __asm__ __volatile__ ("mov x0, 0; mov x1, 1; .inst 0xFF000110 | (0x22 << 16);" : : : "x0", "x1")
  44. #define LKMC_M5OPS_RESETSTATS __asm__ __volatile__ ("mov x0, 0; mov x1, 0; .inst 0XFF000110 | (0x40 << 16);" : : : "x0", "x1")
  45. #else
  46. #error m5ops not implemented for the current arch
  47. #endif
  48. #else
  49. #define LKMC_M5OPS_CHECKPOINT_ASM
  50. #define LKMC_M5OPS_DUMPSTATS_ASM
  51. #define LKMC_M5OPS_EXIT_ASM
  52. #define LKMC_M5OPS_FAIL_1_ASM
  53. #define LKMC_M5OPS_RESETSTATS_ASM
  54. #define LKMC_M5OPS_CHECKPOINT
  55. #define LKMC_M5OPS_DUMPSTATS
  56. #define LKMC_M5OPS_EXIT
  57. #define LKMC_M5OPS_FAIL_1
  58. #define LKMC_M5OPS_RESETSTATS
  59. #endif
  60. #endif