bpf_jit_asm.S 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. lhu $r_A, 0(t1)
  86. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  87. # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
  88. wsbh $r_A, $r_A
  89. # else
  90. sll t0, $r_A, 8
  91. srl t1, $r_A, 8
  92. andi t0, t0, 0xff00
  93. or $r_A, t0, t1
  94. # endif
  95. #endif
  96. jr $r_ra
  97. move $r_ret, zero
  98. END(sk_load_half)
  99. LEAF(sk_load_byte)
  100. is_offset_negative(byte)
  101. FEXPORT(sk_load_byte_positive)
  102. is_offset_in_header(1, byte)
  103. /* Offset within header boundaries */
  104. PTR_ADDU t1, $r_skb_data, offset
  105. lbu $r_A, 0(t1)
  106. jr $r_ra
  107. move $r_ret, zero
  108. END(sk_load_byte)
  109. /*
  110. * call skb_copy_bits:
  111. * (prototype in linux/skbuff.h)
  112. *
  113. * int skb_copy_bits(sk_buff *skb, int offset, void *to, int len)
  114. *
  115. * o32 mandates we leave 4 spaces for argument registers in case
  116. * the callee needs to use them. Even though we don't care about
  117. * the argument registers ourselves, we need to allocate that space
  118. * to remain ABI compliant since the callee may want to use that space.
  119. * We also allocate 2 more spaces for $r_ra and our return register (*to).
  120. *
  121. * n64 is a bit different. The *caller* will allocate the space to preserve
  122. * the arguments. So in 64-bit kernels, we allocate the 4-arg space for no
  123. * good reason but it does not matter that much really.
  124. *
  125. * (void *to) is returned in r_s0
  126. *
  127. */
  128. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  129. #define DS_OFFSET(SIZE) (4 * SZREG)
  130. #else
  131. #define DS_OFFSET(SIZE) ((4 * SZREG) + (4 - SIZE))
  132. #endif
  133. #define bpf_slow_path_common(SIZE) \
  134. /* Quick check. Are we within reasonable boundaries? */ \
  135. LONG_ADDIU $r_s1, $r_skb_len, -SIZE; \
  136. sltu $r_s0, offset, $r_s1; \
  137. beqz $r_s0, fault; \
  138. /* Load 4th argument in DS */ \
  139. LONG_ADDIU a3, zero, SIZE; \
  140. PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
  141. PTR_LA t0, skb_copy_bits; \
  142. PTR_S $r_ra, (5 * SZREG)($r_sp); \
  143. /* Assign low slot to a2 */ \
  144. PTR_ADDIU a2, $r_sp, DS_OFFSET(SIZE); \
  145. jalr t0; \
  146. /* Reset our destination slot (DS but it's ok) */ \
  147. INT_S zero, (4 * SZREG)($r_sp); \
  148. /* \
  149. * skb_copy_bits returns 0 on success and -EFAULT \
  150. * on error. Our data live in a2. Do not bother with \
  151. * our data if an error has been returned. \
  152. */ \
  153. /* Restore our frame */ \
  154. PTR_L $r_ra, (5 * SZREG)($r_sp); \
  155. INT_L $r_s0, (4 * SZREG)($r_sp); \
  156. bltz v0, fault; \
  157. PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
  158. move $r_ret, zero; \
  159. NESTED(bpf_slow_path_word, (6 * SZREG), $r_sp)
  160. bpf_slow_path_common(4)
  161. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  162. # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
  163. wsbh t0, $r_s0
  164. jr $r_ra
  165. rotr $r_A, t0, 16
  166. # else
  167. sll t0, $r_s0, 24
  168. srl t1, $r_s0, 24
  169. srl t2, $r_s0, 8
  170. or t0, t0, t1
  171. andi t2, t2, 0xff00
  172. andi t1, $r_s0, 0xff00
  173. or t0, t0, t2
  174. sll t1, t1, 8
  175. jr $r_ra
  176. or $r_A, t0, t1
  177. # endif
  178. #else
  179. jr $r_ra
  180. move $r_A, $r_s0
  181. #endif
  182. END(bpf_slow_path_word)
  183. NESTED(bpf_slow_path_half, (6 * SZREG), $r_sp)
  184. bpf_slow_path_common(2)
  185. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  186. # if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
  187. jr $r_ra
  188. wsbh $r_A, $r_s0
  189. # else
  190. sll t0, $r_s0, 8
  191. andi t1, $r_s0, 0xff00
  192. andi t0, t0, 0xff00
  193. srl t1, t1, 8
  194. jr $r_ra
  195. or $r_A, t0, t1
  196. # endif
  197. #else
  198. jr $r_ra
  199. move $r_A, $r_s0
  200. #endif
  201. END(bpf_slow_path_half)
  202. NESTED(bpf_slow_path_byte, (6 * SZREG), $r_sp)
  203. bpf_slow_path_common(1)
  204. jr $r_ra
  205. move $r_A, $r_s0
  206. END(bpf_slow_path_byte)
  207. /*
  208. * Negative entry points
  209. */
  210. .macro bpf_is_end_of_data
  211. li t0, SKF_LL_OFF
  212. /* Reading link layer data? */
  213. slt t1, offset, t0
  214. bgtz t1, fault
  215. /* Be careful what follows in DS. */
  216. .endm
  217. /*
  218. * call skb_copy_bits:
  219. * (prototype in linux/filter.h)
  220. *
  221. * void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb,
  222. * int k, unsigned int size)
  223. *
  224. * see above (bpf_slow_path_common) for ABI restrictions
  225. */
  226. #define bpf_negative_common(SIZE) \
  227. PTR_ADDIU $r_sp, $r_sp, -(6 * SZREG); \
  228. PTR_LA t0, bpf_internal_load_pointer_neg_helper; \
  229. PTR_S $r_ra, (5 * SZREG)($r_sp); \
  230. jalr t0; \
  231. li a2, SIZE; \
  232. PTR_L $r_ra, (5 * SZREG)($r_sp); \
  233. /* Check return pointer */ \
  234. beqz v0, fault; \
  235. PTR_ADDIU $r_sp, $r_sp, 6 * SZREG; \
  236. /* Preserve our pointer */ \
  237. move $r_s0, v0; \
  238. /* Set return value */ \
  239. move $r_ret, zero; \
  240. bpf_slow_path_word_neg:
  241. bpf_is_end_of_data
  242. NESTED(sk_load_word_negative, (6 * SZREG), $r_sp)
  243. bpf_negative_common(4)
  244. jr $r_ra
  245. lw $r_A, 0($r_s0)
  246. END(sk_load_word_negative)
  247. bpf_slow_path_half_neg:
  248. bpf_is_end_of_data
  249. NESTED(sk_load_half_negative, (6 * SZREG), $r_sp)
  250. bpf_negative_common(2)
  251. jr $r_ra
  252. lhu $r_A, 0($r_s0)
  253. END(sk_load_half_negative)
  254. bpf_slow_path_byte_neg:
  255. bpf_is_end_of_data
  256. NESTED(sk_load_byte_negative, (6 * SZREG), $r_sp)
  257. bpf_negative_common(1)
  258. jr $r_ra
  259. lbu $r_A, 0($r_s0)
  260. END(sk_load_byte_negative)
  261. fault:
  262. jr $r_ra
  263. addiu $r_ret, zero, 1