bpf_jit64.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * bpf_jit64.h: BPF JIT compiler for PPC64
  4. *
  5. * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
  6. * IBM Corporation
  7. */
  8. #ifndef _BPF_JIT64_H
  9. #define _BPF_JIT64_H
  10. #include "bpf_jit.h"
  11. /*
  12. * Stack layout:
  13. * Ensure the top half (upto local_tmp_var) stays consistent
  14. * with our redzone usage.
  15. *
  16. * [ prev sp ] <-------------
  17. * [ nv gpr save area ] 5*8 |
  18. * [ tail_call_cnt ] 8 |
  19. * [ local_tmp_var ] 16 |
  20. * fp (r31) --> [ ebpf stack space ] upto 512 |
  21. * [ frame header ] 32/112 |
  22. * sp (r1) ---> [ stack pointer ] --------------
  23. */
  24. /* for gpr non volatile registers BPG_REG_6 to 10 */
  25. #define BPF_PPC_STACK_SAVE (5*8)
  26. /* for bpf JIT code internal usage */
  27. #define BPF_PPC_STACK_LOCALS 24
  28. /* stack frame excluding BPF stack, ensure this is quadword aligned */
  29. #define BPF_PPC_STACKFRAME (STACK_FRAME_MIN_SIZE + \
  30. BPF_PPC_STACK_LOCALS + BPF_PPC_STACK_SAVE)
  31. #ifndef __ASSEMBLY__
  32. /* BPF register usage */
  33. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
  34. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
  35. /* BPF to ppc register mappings */
  36. static const int b2p[] = {
  37. /* function return value */
  38. [BPF_REG_0] = 8,
  39. /* function arguments */
  40. [BPF_REG_1] = 3,
  41. [BPF_REG_2] = 4,
  42. [BPF_REG_3] = 5,
  43. [BPF_REG_4] = 6,
  44. [BPF_REG_5] = 7,
  45. /* non volatile registers */
  46. [BPF_REG_6] = 27,
  47. [BPF_REG_7] = 28,
  48. [BPF_REG_8] = 29,
  49. [BPF_REG_9] = 30,
  50. /* frame pointer aka BPF_REG_10 */
  51. [BPF_REG_FP] = 31,
  52. /* eBPF jit internal registers */
  53. [BPF_REG_AX] = 2,
  54. [TMP_REG_1] = 9,
  55. [TMP_REG_2] = 10
  56. };
  57. /* PPC NVR range -- update this if we ever use NVRs below r27 */
  58. #define BPF_PPC_NVR_MIN 27
  59. /*
  60. * WARNING: These can use TMP_REG_2 if the offset is not at word boundary,
  61. * so ensure that it isn't in use already.
  62. */
  63. #define PPC_BPF_LL(r, base, i) do { \
  64. if ((i) % 4) { \
  65. EMIT(PPC_RAW_LI(b2p[TMP_REG_2], (i)));\
  66. EMIT(PPC_RAW_LDX(r, base, \
  67. b2p[TMP_REG_2])); \
  68. } else \
  69. EMIT(PPC_RAW_LD(r, base, i)); \
  70. } while(0)
  71. #define PPC_BPF_STL(r, base, i) do { \
  72. if ((i) % 4) { \
  73. EMIT(PPC_RAW_LI(b2p[TMP_REG_2], (i)));\
  74. EMIT(PPC_RAW_STDX(r, base, \
  75. b2p[TMP_REG_2])); \
  76. } else \
  77. EMIT(PPC_RAW_STD(r, base, i)); \
  78. } while(0)
  79. #define PPC_BPF_STLU(r, base, i) do { EMIT(PPC_RAW_STDU(r, base, i)); } while(0)
  80. #define SEEN_FUNC 0x1000 /* might call external helpers */
  81. #define SEEN_STACK 0x2000 /* uses BPF stack */
  82. #define SEEN_TAILCALL 0x4000 /* uses tail calls */
  83. struct codegen_context {
  84. /*
  85. * This is used to track register usage as well
  86. * as calls to external helpers.
  87. * - register usage is tracked with corresponding
  88. * bits (r3-r10 and r27-r31)
  89. * - rest of the bits can be used to track other
  90. * things -- for now, we use bits 16 to 23
  91. * encoded in SEEN_* macros above
  92. */
  93. unsigned int seen;
  94. unsigned int idx;
  95. unsigned int stack_size;
  96. };
  97. #endif /* !__ASSEMBLY__ */
  98. #endif