bpf_jit32.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * bpf_jit32.h: BPF JIT compiler for PPC
  3. *
  4. * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation
  5. *
  6. * Split from bpf_jit.h
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; version 2
  11. * of the License.
  12. */
  13. #ifndef _BPF_JIT32_H
  14. #define _BPF_JIT32_H
  15. #include "bpf_jit.h"
  16. #ifdef CONFIG_PPC64
  17. #define BPF_PPC_STACK_R3_OFF 48
  18. #define BPF_PPC_STACK_LOCALS 32
  19. #define BPF_PPC_STACK_BASIC (48+64)
  20. #define BPF_PPC_STACK_SAVE (18*8)
  21. #define BPF_PPC_STACKFRAME (BPF_PPC_STACK_BASIC+BPF_PPC_STACK_LOCALS+ \
  22. BPF_PPC_STACK_SAVE)
  23. #define BPF_PPC_SLOWPATH_FRAME (48+64)
  24. #else
  25. #define BPF_PPC_STACK_R3_OFF 24
  26. #define BPF_PPC_STACK_LOCALS 16
  27. #define BPF_PPC_STACK_BASIC (24+32)
  28. #define BPF_PPC_STACK_SAVE (18*4)
  29. #define BPF_PPC_STACKFRAME (BPF_PPC_STACK_BASIC+BPF_PPC_STACK_LOCALS+ \
  30. BPF_PPC_STACK_SAVE)
  31. #define BPF_PPC_SLOWPATH_FRAME (24+32)
  32. #endif
  33. #define REG_SZ (BITS_PER_LONG/8)
  34. /*
  35. * Generated code register usage:
  36. *
  37. * As normal PPC C ABI (e.g. r1=sp, r2=TOC), with:
  38. *
  39. * skb r3 (Entry parameter)
  40. * A register r4
  41. * X register r5
  42. * addr param r6
  43. * r7-r10 scratch
  44. * skb->data r14
  45. * skb headlen r15 (skb->len - skb->data_len)
  46. * m[0] r16
  47. * m[...] ...
  48. * m[15] r31
  49. */
  50. #define r_skb 3
  51. #define r_ret 3
  52. #define r_A 4
  53. #define r_X 5
  54. #define r_addr 6
  55. #define r_scratch1 7
  56. #define r_scratch2 8
  57. #define r_D 14
  58. #define r_HL 15
  59. #define r_M 16
  60. #ifndef __ASSEMBLY__
  61. /*
  62. * Assembly helpers from arch/powerpc/net/bpf_jit.S:
  63. */
  64. #define DECLARE_LOAD_FUNC(func) \
  65. extern u8 func[], func##_negative_offset[], func##_positive_offset[]
  66. DECLARE_LOAD_FUNC(sk_load_word);
  67. DECLARE_LOAD_FUNC(sk_load_half);
  68. DECLARE_LOAD_FUNC(sk_load_byte);
  69. DECLARE_LOAD_FUNC(sk_load_byte_msh);
  70. #define PPC_LBZ_OFFS(r, base, i) do { if ((i) < 32768) PPC_LBZ(r, base, i); \
  71. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  72. PPC_LBZ(r, r, IMM_L(i)); } } while(0)
  73. #define PPC_LD_OFFS(r, base, i) do { if ((i) < 32768) PPC_LD(r, base, i); \
  74. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  75. PPC_LD(r, r, IMM_L(i)); } } while(0)
  76. #define PPC_LWZ_OFFS(r, base, i) do { if ((i) < 32768) PPC_LWZ(r, base, i); \
  77. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  78. PPC_LWZ(r, r, IMM_L(i)); } } while(0)
  79. #define PPC_LHZ_OFFS(r, base, i) do { if ((i) < 32768) PPC_LHZ(r, base, i); \
  80. else { PPC_ADDIS(r, base, IMM_HA(i)); \
  81. PPC_LHZ(r, r, IMM_L(i)); } } while(0)
  82. #ifdef CONFIG_PPC64
  83. #define PPC_LL_OFFS(r, base, i) do { PPC_LD_OFFS(r, base, i); } while(0)
  84. #else
  85. #define PPC_LL_OFFS(r, base, i) do { PPC_LWZ_OFFS(r, base, i); } while(0)
  86. #endif
  87. #ifdef CONFIG_SMP
  88. #ifdef CONFIG_PPC64
  89. #define PPC_BPF_LOAD_CPU(r) \
  90. do { BUILD_BUG_ON(FIELD_SIZEOF(struct paca_struct, paca_index) != 2); \
  91. PPC_LHZ_OFFS(r, 13, offsetof(struct paca_struct, paca_index)); \
  92. } while (0)
  93. #else
  94. #define PPC_BPF_LOAD_CPU(r) \
  95. do { BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4); \
  96. PPC_LHZ_OFFS(r, (1 & ~(THREAD_SIZE - 1)), \
  97. offsetof(struct thread_info, cpu)); \
  98. } while(0)
  99. #endif
  100. #else
  101. #define PPC_BPF_LOAD_CPU(r) do { PPC_LI(r, 0); } while(0)
  102. #endif
  103. #define PPC_LHBRX_OFFS(r, base, i) \
  104. do { PPC_LI32(r, i); PPC_LHBRX(r, r, base); } while(0)
  105. #ifdef __LITTLE_ENDIAN__
  106. #define PPC_NTOHS_OFFS(r, base, i) PPC_LHBRX_OFFS(r, base, i)
  107. #else
  108. #define PPC_NTOHS_OFFS(r, base, i) PPC_LHZ_OFFS(r, base, i)
  109. #endif
  110. #define SEEN_DATAREF 0x10000 /* might call external helpers */
  111. #define SEEN_XREG 0x20000 /* X reg is used */
  112. #define SEEN_MEM 0x40000 /* SEEN_MEM+(1<<n) = use mem[n] for temporary
  113. * storage */
  114. #define SEEN_MEM_MSK 0x0ffff
  115. struct codegen_context {
  116. unsigned int seen;
  117. unsigned int idx;
  118. int pc_ret0; /* bpf index of first RET #0 instruction (if any) */
  119. };
  120. #endif
  121. #endif