bpf_jit_asm.S 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * bpf_jib_asm.S: Packet/header access helper functions for MIPS/MIPS64 BPF
  3. * compiler.
  4. *
  5. * Copyright (C) 2015 Imagination Technologies Ltd.
  6. * Author: Markos Chandras <markos.chandras@imgtec.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; version 2 of the License.
  11. */
  12. #include <asm/asm.h>
  13. #include <asm/regdef.h>
  14. #include "bpf_jit.h"
  15. /* ABI
  16. *
  17. * r_skb_hl skb header length
  18. * r_skb_data skb data
  19. * r_off(a1) offset register
  20. * r_A BPF register A
  21. * r_X PF register X
  22. * r_skb(a0) *skb
  23. * r_M *scratch memory
  24. * r_skb_le skb length
  25. * r_s0 Scratch register 0
  26. * r_s1 Scratch register 1
  27. *
  28. * On entry:
  29. * a0: *skb
  30. * a1: offset (imm or imm + X)
  31. *
  32. * All non-BPF-ABI registers are free for use. On return, we only
  33. * care about r_ret. The BPF-ABI registers are assumed to remain
  34. * unmodified during the entire filter operation.
  35. */
  36. #define skb a0
  37. #define offset a1
  38. #define SKF_LL_OFF (-0x200000) /* Can't include linux/filter.h in assembly */
  39. /* We know better :) so prevent assembler reordering etc */
  40. .set noreorder
  41. #define is_offset_negative(TYPE) \
  42. /* If offset is negative we have more work to do */ \
  43. slti t0, offset, 0; \
  44. bgtz t0, bpf_slow_path_##TYPE##_neg; \
  45. /* Be careful what follows in DS. */
  46. #define is_offset_in_header(SIZE, TYPE) \
  47. /* Reading from header? */ \
  48. addiu $r_s0, $r_skb_hl, -SIZE; \
  49. slt t0, $r_s0, offset; \
  50. bgtz t0, bpf_slow_path_##TYPE; \
  51. LEAF(sk_load_word)
  52. is_offset_negative(word)
  53. FEXPORT(sk_load_word_positive)
  54. is_offset_in_header(4, word)
  55. /* Offset within header boundaries */
  56. PTR_ADDU t1, $r_skb_data, offset
  57. .set reorder
  58. lw $r_A, 0(t1)
  59. .set noreorder
  60. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  61. # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
  62. wsbh t0, $r_A
  63. rotr $r_A, t0, 16
  64. # else
  65. sll t0, $r_A, 24
  66. srl t1, $r_A, 24
  67. srl t2, $r_A, 8
  68. or t0, t0, t1
  69. andi t2, t2, 0xff00
  70. andi t1, $r_A, 0xff00
  71. or t0, t0, t2
  72. sll t1, t1, 8
  73. or $r_A, t0, t1
  74. # endif
  75. #endif
  76. jr $r_ra
  77. move $r_ret, zero
  78. END(sk_load_word)
  79. LEAF(sk_load_half)
  80. is_offset_negative(half)
  81. FEXPORT(sk_load_half_positive)
  82. is_offset_in_header(2, half)
  83. /* Offset within header boundaries */
  84. PTR_ADDU t1, $r_skb_data, offset
  85. .set reorder
  86. lh $r_A, 0(t1)
  87. .set noreorder
  88. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  89. # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
  90. wsbh t0, $r_A
  91. seh $r_A, t0
  92. # else
  93. sll t0, $r_A, 24
  94. andi t1, $r_A, 0xff00
  95. sra t0, t0, 16
  96. srl t1, t1, 8
  97. or $r_A, t0, t1
  98. # endif
  99. #endif
  100. jr $r_ra
  101. move $r_ret, zero
  102. END(sk_load_half)
  103. LEAF(sk_load_byte)
  104. is_offset_negative(byte)
  105. FEXPORT(sk_load_byte_positive)
  106. is_offset_in_header(1, byte)
  107. /* Offset within header boundaries */
  108. PTR_ADDU t1, $r_skb_data, offset
  109. lb $r_A, 0(t1)
  110. jr $r_ra
  111. move $r_ret, zero
  112. END(sk_load_byte)
  113. /*
  114. * call skb_copy_bits:
  115. * (prototype in linux/skbuff.h)
  116. *
  117. * int skb_copy_bits(sk_buff *skb, int offset, void *to, int len)
  118. *
  119. * o32 mandates we leave 4 spaces for argument registers in case
  120. * the callee needs to use them. Even though we don't care about
  121. * the argument registers ourselves, we need to allocate that space
  122. * to remain ABI compliant since the callee may want to use that space.
  123. * We also allocate 2 more spaces for $r_ra and our return register (*to).
  124. *
  125. * n64 is a bit different. The *caller* will allocate the space to preserve
  126. * the arguments. So in 64-bit kernels, we allocate the 4-arg space for no
  127. * good reason but it does not matter that much really.
  128. *
  129. * (void *to) is returned in r_s0
  130. *
  131. */
  132. #define bpf_slow_path_common(SIZE) \
  133. /* Quick check. Are we within reasonable boundaries? */ \
  134. LONG_ADDIU $r_s1, $r_skb_len, -SIZE; \
  135. sltu $r_s0, offset, $r_s1; \
  136. beqz $r_s0, fault; \
  137. /* Load 4th argument in DS */ \
  138. LONG_ADDIU a3, zero, SIZE; \
  139. PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
  140. PTR_LA t0, skb_copy_bits; \
  141. PTR_S $r_ra, (5 * SZREG)($r_sp); \
  142. /* Assign low slot to a2 */ \
  143. move a2, $r_sp; \
  144. jalr t0; \
  145. /* Reset our destination slot (DS but it's ok) */ \
  146. INT_S zero, (4 * SZREG)($r_sp); \
  147. /* \
  148. * skb_copy_bits returns 0 on success and -EFAULT \
  149. * on error. Our data live in a2. Do not bother with \
  150. * our data if an error has been returned. \
  151. */ \
  152. /* Restore our frame */ \
  153. PTR_L $r_ra, (5 * SZREG)($r_sp); \
  154. INT_L $r_s0, (4 * SZREG)($r_sp); \
  155. bltz v0, fault; \
  156. PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
  157. move $r_ret, zero; \
  158. NESTED(bpf_slow_path_word, (6 * SZREG), $r_sp)
  159. bpf_slow_path_common(4)
  160. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  161. # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
  162. wsbh t0, $r_s0
  163. jr $r_ra
  164. rotr $r_A, t0, 16
  165. # else
  166. sll t0, $r_s0, 24
  167. srl t1, $r_s0, 24
  168. srl t2, $r_s0, 8
  169. or t0, t0, t1
  170. andi t2, t2, 0xff00
  171. andi t1, $r_s0, 0xff00
  172. or t0, t0, t2
  173. sll t1, t1, 8
  174. jr $r_ra
  175. or $r_A, t0, t1
  176. # endif
  177. #else
  178. jr $r_ra
  179. move $r_A, $r_s0
  180. #endif
  181. END(bpf_slow_path_word)
  182. NESTED(bpf_slow_path_half, (6 * SZREG), $r_sp)
  183. bpf_slow_path_common(2)
  184. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  185. # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
  186. jr $r_ra
  187. wsbh $r_A, $r_s0
  188. # else
  189. sll t0, $r_s0, 8
  190. andi t1, $r_s0, 0xff00
  191. andi t0, t0, 0xff00
  192. srl t1, t1, 8
  193. jr $r_ra
  194. or $r_A, t0, t1
  195. # endif
  196. #else
  197. jr $r_ra
  198. move $r_A, $r_s0
  199. #endif
  200. END(bpf_slow_path_half)
  201. NESTED(bpf_slow_path_byte, (6 * SZREG), $r_sp)
  202. bpf_slow_path_common(1)
  203. jr $r_ra
  204. move $r_A, $r_s0
  205. END(bpf_slow_path_byte)
  206. /*
  207. * Negative entry points
  208. */
  209. .macro bpf_is_end_of_data
  210. li t0, SKF_LL_OFF
  211. /* Reading link layer data? */
  212. slt t1, offset, t0
  213. bgtz t1, fault
  214. /* Be careful what follows in DS. */
  215. .endm
  216. /*
  217. * call skb_copy_bits:
  218. * (prototype in linux/filter.h)
  219. *
  220. * void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  221. * int k, unsigned int size)
  222. *
  223. * see above (bpf_slow_path_common) for ABI restrictions
  224. */
  225. #define bpf_negative_common(SIZE) \
  226. PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
  227. PTR_LA t0, bpf_internal_load_pointer_neg_helper; \
  228. PTR_S $r_ra, (5 * SZREG)($r_sp); \
  229. jalr t0; \
  230. li a2, SIZE; \
  231. PTR_L $r_ra, (5 * SZREG)($r_sp); \
  232. /* Check return pointer */ \
  233. beqz v0, fault; \
  234. PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
  235. /* Preserve our pointer */ \
  236. move $r_s0, v0; \
  237. /* Set return value */ \
  238. move $r_ret, zero; \
  239. bpf_slow_path_word_neg:
  240. bpf_is_end_of_data
  241. NESTED(sk_load_word_negative, (6 * SZREG), $r_sp)
  242. bpf_negative_common(4)
  243. jr $r_ra
  244. lw $r_A, 0($r_s0)
  245. END(sk_load_word_negative)
  246. bpf_slow_path_half_neg:
  247. bpf_is_end_of_data
  248. NESTED(sk_load_half_negative, (6 * SZREG), $r_sp)
  249. bpf_negative_common(2)
  250. jr $r_ra
  251. lhu $r_A, 0($r_s0)
  252. END(sk_load_half_negative)
  253. bpf_slow_path_byte_neg:
  254. bpf_is_end_of_data
  255. NESTED(sk_load_byte_negative, (6 * SZREG), $r_sp)
  256. bpf_negative_common(1)
  257. jr $r_ra
  258. lbu $r_A, 0($r_s0)
  259. END(sk_load_byte_negative)
  260. fault:
  261. jr $r_ra
  262. addiu $r_ret, zero, 1