x86_64.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef LKMC_X86_64_H
  2. #define LKMC_X86_64_H
  3. /* This and other macros may make C function calls, and therefore can destroy
  4. * non-callee saved registers. */
  5. #define LKMC_ASSERT_EQ(general1, general2) \
  6. push %rdi; \
  7. mov general2, %rdi; \
  8. push %rdi; \
  9. mov 8(%rsp), %rdi; \
  10. mov general1, %rdi; \
  11. pop %rsi; \
  12. add $8, %rsp; \
  13. mov $__LINE__, %edx; \
  14. call lkmc_assert_eq_64; \
  15. ;
  16. #define LKMC_ASSERT_EQ_32(general1, general2) \
  17. push %rdi; \
  18. mov general2, %edi; \
  19. push %rdi; \
  20. mov 8(%rsp), %rdi; \
  21. mov general1, %edi; \
  22. pop %rsi; \
  23. add $8, %rsp; \
  24. mov $__LINE__, %edx; \
  25. call lkmc_assert_eq_32; \
  26. ;
  27. #define LKMC_ASSERT_FAIL \
  28. mov $__LINE__, %edi; \
  29. call lkmc_assert_fail; \
  30. ;
  31. /* Assert that two memory arrays are the same. */
  32. #define LKMC_ASSERT_MEMCMP(label1, label2, const_size) \
  33. lea label1(%rip), %rdi; \
  34. lea label2(%rip), %rsi; \
  35. mov const_size, %rdx; \
  36. mov $__LINE__, %ecx; \
  37. call lkmc_assert_memcmp; \
  38. ;
  39. /* Function epilogue.
  40. *
  41. * https://cirosantilli.com/linux-kernel-module-cheat#x86-64-calling-convention
  42. */
  43. #define LKMC_EPILOGUE \
  44. add $8, %rsp; \
  45. pop %rbx; \
  46. pop %r12; \
  47. pop %r13; \
  48. pop %r14; \
  49. pop %r15; \
  50. pop %rbp; \
  51. mov $0, %rax; \
  52. ret; \
  53. ;
  54. /* Function prologue.
  55. *
  56. * https://cirosantilli.com/linux-kernel-module-cheat#x86-64-calling-convention
  57. */
  58. #define LKMC_PROLOGUE \
  59. .text; \
  60. .global main; \
  61. main: \
  62. push %rbp; \
  63. mov %rsp, %rbp; \
  64. push %r15; \
  65. push %r14; \
  66. push %r13; \
  67. push %r12; \
  68. push %rbx; \
  69. sub $8, %rsp; \
  70. main_after_prologue: \
  71. ;
  72. #endif