basic_asm.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _SELFTESTS_POWERPC_BASIC_ASM_H
  3. #define _SELFTESTS_POWERPC_BASIC_ASM_H
  4. #include <ppc-asm.h>
  5. #include <asm/unistd.h>
  6. #ifdef __powerpc64__
  7. #define PPC_LL ld
  8. #define PPC_STL std
  9. #define PPC_STLU stdu
  10. #else
  11. #define PPC_LL lwz
  12. #define PPC_STL stw
  13. #define PPC_STLU stwu
  14. #endif
  15. #define LOAD_REG_IMMEDIATE(reg, expr) \
  16. lis reg, (expr)@highest; \
  17. ori reg, reg, (expr)@higher; \
  18. rldicr reg, reg, 32, 31; \
  19. oris reg, reg, (expr)@high; \
  20. ori reg, reg, (expr)@l;
  21. /*
  22. * Note: These macros assume that variables being stored on the stack are
  23. * sizeof(long), while this is usually the case it may not always be the
  24. * case for each use case.
  25. */
  26. #ifdef __powerpc64__
  27. // ABIv2
  28. #if defined(_CALL_ELF) && _CALL_ELF == 2
  29. #define STACK_FRAME_MIN_SIZE 32
  30. #define STACK_FRAME_TOC_POS 24
  31. #define __STACK_FRAME_PARAM(_param) (32 + ((_param)*8))
  32. #define __STACK_FRAME_LOCAL(_num_params, _var_num) \
  33. ((STACK_FRAME_PARAM(_num_params)) + ((_var_num)*8))
  34. #else // ABIv1 below
  35. #define STACK_FRAME_MIN_SIZE 112
  36. #define STACK_FRAME_TOC_POS 40
  37. #define __STACK_FRAME_PARAM(i) (48 + ((i)*8))
  38. /*
  39. * Caveat: if a function passed more than 8 doublewords, the caller will have
  40. * made more space... which would render the 112 incorrect.
  41. */
  42. #define __STACK_FRAME_LOCAL(_num_params, _var_num) \
  43. (112 + ((_var_num)*8))
  44. #endif // ABIv2
  45. // Common 64-bit
  46. #define STACK_FRAME_LR_POS 16
  47. #define STACK_FRAME_CR_POS 8
  48. #else // 32-bit below
  49. #define STACK_FRAME_MIN_SIZE 16
  50. #define STACK_FRAME_LR_POS 4
  51. #define __STACK_FRAME_PARAM(_param) (STACK_FRAME_MIN_SIZE + ((_param)*4))
  52. #define __STACK_FRAME_LOCAL(_num_params, _var_num) \
  53. ((STACK_FRAME_PARAM(_num_params)) + ((_var_num)*4))
  54. #endif // __powerpc64__
  55. /* Parameter x saved to the stack */
  56. #define STACK_FRAME_PARAM(var) __STACK_FRAME_PARAM(var)
  57. /* Local variable x saved to the stack after x parameters */
  58. #define STACK_FRAME_LOCAL(num_params, var) \
  59. __STACK_FRAME_LOCAL(num_params, var)
  60. /*
  61. * It is very important to note here that _extra is the extra amount of
  62. * stack space needed. This space can be accessed using STACK_FRAME_PARAM()
  63. * or STACK_FRAME_LOCAL() macros.
  64. *
  65. * r1 and r2 are not defined in ppc-asm.h (instead they are defined as sp
  66. * and toc). Kernel programmers tend to prefer rX even for r1 and r2, hence
  67. * %1 and %r2. r0 is defined in ppc-asm.h and therefore %r0 gets
  68. * preprocessed incorrectly, hence r0.
  69. */
  70. #define PUSH_BASIC_STACK(_extra) \
  71. mflr r0; \
  72. PPC_STL r0, STACK_FRAME_LR_POS(%r1); \
  73. PPC_STLU %r1, -(((_extra + 15) & ~15) + STACK_FRAME_MIN_SIZE)(%r1);
  74. #define POP_BASIC_STACK(_extra) \
  75. addi %r1, %r1, (((_extra + 15) & ~15) + STACK_FRAME_MIN_SIZE); \
  76. PPC_LL r0, STACK_FRAME_LR_POS(%r1); \
  77. mtlr r0;
  78. .macro OP_REGS op, reg_width, start_reg, end_reg, base_reg, base_reg_offset=0, skip=0
  79. .set i, \start_reg
  80. .rept (\end_reg - \start_reg + 1)
  81. \op i, (\reg_width * (i - \skip) + \base_reg_offset)(\base_reg)
  82. .set i, i + 1
  83. .endr
  84. .endm
  85. #endif /* _SELFTESTS_POWERPC_BASIC_ASM_H */