bpf_jit.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. * Just-In-Time compiler for BPF filters on MIPS
  3. *
  4. * Copyright (c) 2014 Imagination Technologies Ltd.
  5. * Author: Markos Chandras <markos.chandras@imgtec.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/compiler.h>
  13. #include <linux/errno.h>
  14. #include <linux/filter.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/kconfig.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <asm/bitops.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/cpu-features.h>
  25. #include <asm/uasm.h>
  26. #include "bpf_jit.h"
  27. /* ABI
  28. *
  29. * s0 1st scratch register
  30. * s1 2nd scratch register
  31. * s2 offset register
  32. * s3 BPF register A
  33. * s4 BPF register X
  34. * s5 *skb
  35. * s6 *scratch memory
  36. *
  37. * On entry (*bpf_func)(*skb, *filter)
  38. * a0 = MIPS_R_A0 = skb;
  39. * a1 = MIPS_R_A1 = filter;
  40. *
  41. * Stack
  42. * ...
  43. * M[15]
  44. * M[14]
  45. * M[13]
  46. * ...
  47. * M[0] <-- r_M
  48. * saved reg k-1
  49. * saved reg k-2
  50. * ...
  51. * saved reg 0 <-- r_sp
  52. * <no argument area>
  53. *
  54. * Packet layout
  55. *
  56. * <--------------------- len ------------------------>
  57. * <--skb-len(r_skb_hl)-->< ----- skb->data_len ------>
  58. * ----------------------------------------------------
  59. * | skb->data |
  60. * ----------------------------------------------------
  61. */
  62. #define RSIZE (sizeof(unsigned long))
  63. #define ptr typeof(unsigned long)
  64. /* ABI specific return values */
  65. #ifdef CONFIG_32BIT /* O32 */
  66. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  67. #define r_err MIPS_R_V1
  68. #define r_val MIPS_R_V0
  69. #else /* CONFIG_CPU_LITTLE_ENDIAN */
  70. #define r_err MIPS_R_V0
  71. #define r_val MIPS_R_V1
  72. #endif
  73. #else /* N64 */
  74. #define r_err MIPS_R_V0
  75. #define r_val MIPS_R_V0
  76. #endif
  77. #define r_ret MIPS_R_V0
  78. /*
  79. * Use 2 scratch registers to avoid pipeline interlocks.
  80. * There is no overhead during epilogue and prologue since
  81. * any of the $s0-$s6 registers will only be preserved if
  82. * they are going to actually be used.
  83. */
  84. #define r_s0 MIPS_R_S0 /* scratch reg 1 */
  85. #define r_s1 MIPS_R_S1 /* scratch reg 2 */
  86. #define r_off MIPS_R_S2
  87. #define r_A MIPS_R_S3
  88. #define r_X MIPS_R_S4
  89. #define r_skb MIPS_R_S5
  90. #define r_M MIPS_R_S6
  91. #define r_tmp_imm MIPS_R_T6 /* No need to preserve this */
  92. #define r_tmp MIPS_R_T7 /* No need to preserve this */
  93. #define r_zero MIPS_R_ZERO
  94. #define r_sp MIPS_R_SP
  95. #define r_ra MIPS_R_RA
  96. #define SCRATCH_OFF(k) (4 * (k))
  97. /* JIT flags */
  98. #define SEEN_CALL (1 << BPF_MEMWORDS)
  99. #define SEEN_SREG_SFT (BPF_MEMWORDS + 1)
  100. #define SEEN_SREG_BASE (1 << SEEN_SREG_SFT)
  101. #define SEEN_SREG(x) (SEEN_SREG_BASE << (x))
  102. #define SEEN_S0 SEEN_SREG(0)
  103. #define SEEN_S1 SEEN_SREG(1)
  104. #define SEEN_OFF SEEN_SREG(2)
  105. #define SEEN_A SEEN_SREG(3)
  106. #define SEEN_X SEEN_SREG(4)
  107. #define SEEN_SKB SEEN_SREG(5)
  108. #define SEEN_MEM SEEN_SREG(6)
  109. /* Arguments used by JIT */
  110. #define ARGS_USED_BY_JIT 2 /* only applicable to 64-bit */
  111. #define SBIT(x) (1 << (x)) /* Signed version of BIT() */
  112. /**
  113. * struct jit_ctx - JIT context
  114. * @skf: The sk_filter
  115. * @prologue_bytes: Number of bytes for prologue
  116. * @idx: Instruction index
  117. * @flags: JIT flags
  118. * @offsets: Instruction offsets
  119. * @target: Memory location for the compiled filter
  120. */
  121. struct jit_ctx {
  122. const struct bpf_prog *skf;
  123. unsigned int prologue_bytes;
  124. u32 idx;
  125. u32 flags;
  126. u32 *offsets;
  127. u32 *target;
  128. };
  129. static inline int optimize_div(u32 *k)
  130. {
  131. /* power of 2 divides can be implemented with right shift */
  132. if (!(*k & (*k-1))) {
  133. *k = ilog2(*k);
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx);
  139. /* Simply emit the instruction if the JIT memory space has been allocated */
  140. #define emit_instr(ctx, func, ...) \
  141. do { \
  142. if ((ctx)->target != NULL) { \
  143. u32 *p = &(ctx)->target[ctx->idx]; \
  144. uasm_i_##func(&p, ##__VA_ARGS__); \
  145. } \
  146. (ctx)->idx++; \
  147. } while (0)
  148. /*
  149. * Similar to emit_instr but it must be used when we need to emit
  150. * 32-bit or 64-bit instructions
  151. */
  152. #define emit_long_instr(ctx, func, ...) \
  153. do { \
  154. if ((ctx)->target != NULL) { \
  155. u32 *p = &(ctx)->target[ctx->idx]; \
  156. UASM_i_##func(&p, ##__VA_ARGS__); \
  157. } \
  158. (ctx)->idx++; \
  159. } while (0)
  160. /* Determine if immediate is within the 16-bit signed range */
  161. static inline bool is_range16(s32 imm)
  162. {
  163. return !(imm >= SBIT(15) || imm < -SBIT(15));
  164. }
  165. static inline void emit_addu(unsigned int dst, unsigned int src1,
  166. unsigned int src2, struct jit_ctx *ctx)
  167. {
  168. emit_instr(ctx, addu, dst, src1, src2);
  169. }
  170. static inline void emit_nop(struct jit_ctx *ctx)
  171. {
  172. emit_instr(ctx, nop);
  173. }
  174. /* Load a u32 immediate to a register */
  175. static inline void emit_load_imm(unsigned int dst, u32 imm, struct jit_ctx *ctx)
  176. {
  177. if (ctx->target != NULL) {
  178. /* addiu can only handle s16 */
  179. if (!is_range16(imm)) {
  180. u32 *p = &ctx->target[ctx->idx];
  181. uasm_i_lui(&p, r_tmp_imm, (s32)imm >> 16);
  182. p = &ctx->target[ctx->idx + 1];
  183. uasm_i_ori(&p, dst, r_tmp_imm, imm & 0xffff);
  184. } else {
  185. u32 *p = &ctx->target[ctx->idx];
  186. uasm_i_addiu(&p, dst, r_zero, imm);
  187. }
  188. }
  189. ctx->idx++;
  190. if (!is_range16(imm))
  191. ctx->idx++;
  192. }
  193. static inline void emit_or(unsigned int dst, unsigned int src1,
  194. unsigned int src2, struct jit_ctx *ctx)
  195. {
  196. emit_instr(ctx, or, dst, src1, src2);
  197. }
  198. static inline void emit_ori(unsigned int dst, unsigned src, u32 imm,
  199. struct jit_ctx *ctx)
  200. {
  201. if (imm >= BIT(16)) {
  202. emit_load_imm(r_tmp, imm, ctx);
  203. emit_or(dst, src, r_tmp, ctx);
  204. } else {
  205. emit_instr(ctx, ori, dst, src, imm);
  206. }
  207. }
  208. static inline void emit_daddiu(unsigned int dst, unsigned int src,
  209. int imm, struct jit_ctx *ctx)
  210. {
  211. /*
  212. * Only used for stack, so the imm is relatively small
  213. * and it fits in 15-bits
  214. */
  215. emit_instr(ctx, daddiu, dst, src, imm);
  216. }
  217. static inline void emit_addiu(unsigned int dst, unsigned int src,
  218. u32 imm, struct jit_ctx *ctx)
  219. {
  220. if (!is_range16(imm)) {
  221. emit_load_imm(r_tmp, imm, ctx);
  222. emit_addu(dst, r_tmp, src, ctx);
  223. } else {
  224. emit_instr(ctx, addiu, dst, src, imm);
  225. }
  226. }
  227. static inline void emit_and(unsigned int dst, unsigned int src1,
  228. unsigned int src2, struct jit_ctx *ctx)
  229. {
  230. emit_instr(ctx, and, dst, src1, src2);
  231. }
  232. static inline void emit_andi(unsigned int dst, unsigned int src,
  233. u32 imm, struct jit_ctx *ctx)
  234. {
  235. /* If imm does not fit in u16 then load it to register */
  236. if (imm >= BIT(16)) {
  237. emit_load_imm(r_tmp, imm, ctx);
  238. emit_and(dst, src, r_tmp, ctx);
  239. } else {
  240. emit_instr(ctx, andi, dst, src, imm);
  241. }
  242. }
  243. static inline void emit_xor(unsigned int dst, unsigned int src1,
  244. unsigned int src2, struct jit_ctx *ctx)
  245. {
  246. emit_instr(ctx, xor, dst, src1, src2);
  247. }
  248. static inline void emit_xori(ptr dst, ptr src, u32 imm, struct jit_ctx *ctx)
  249. {
  250. /* If imm does not fit in u16 then load it to register */
  251. if (imm >= BIT(16)) {
  252. emit_load_imm(r_tmp, imm, ctx);
  253. emit_xor(dst, src, r_tmp, ctx);
  254. } else {
  255. emit_instr(ctx, xori, dst, src, imm);
  256. }
  257. }
  258. static inline void emit_stack_offset(int offset, struct jit_ctx *ctx)
  259. {
  260. emit_long_instr(ctx, ADDIU, r_sp, r_sp, offset);
  261. }
  262. static inline void emit_subu(unsigned int dst, unsigned int src1,
  263. unsigned int src2, struct jit_ctx *ctx)
  264. {
  265. emit_instr(ctx, subu, dst, src1, src2);
  266. }
  267. static inline void emit_neg(unsigned int reg, struct jit_ctx *ctx)
  268. {
  269. emit_subu(reg, r_zero, reg, ctx);
  270. }
  271. static inline void emit_sllv(unsigned int dst, unsigned int src,
  272. unsigned int sa, struct jit_ctx *ctx)
  273. {
  274. emit_instr(ctx, sllv, dst, src, sa);
  275. }
  276. static inline void emit_sll(unsigned int dst, unsigned int src,
  277. unsigned int sa, struct jit_ctx *ctx)
  278. {
  279. /* sa is 5-bits long */
  280. if (sa >= BIT(5))
  281. /* Shifting >= 32 results in zero */
  282. emit_jit_reg_move(dst, r_zero, ctx);
  283. else
  284. emit_instr(ctx, sll, dst, src, sa);
  285. }
  286. static inline void emit_srlv(unsigned int dst, unsigned int src,
  287. unsigned int sa, struct jit_ctx *ctx)
  288. {
  289. emit_instr(ctx, srlv, dst, src, sa);
  290. }
  291. static inline void emit_srl(unsigned int dst, unsigned int src,
  292. unsigned int sa, struct jit_ctx *ctx)
  293. {
  294. /* sa is 5-bits long */
  295. if (sa >= BIT(5))
  296. /* Shifting >= 32 results in zero */
  297. emit_jit_reg_move(dst, r_zero, ctx);
  298. else
  299. emit_instr(ctx, srl, dst, src, sa);
  300. }
  301. static inline void emit_slt(unsigned int dst, unsigned int src1,
  302. unsigned int src2, struct jit_ctx *ctx)
  303. {
  304. emit_instr(ctx, slt, dst, src1, src2);
  305. }
  306. static inline void emit_sltu(unsigned int dst, unsigned int src1,
  307. unsigned int src2, struct jit_ctx *ctx)
  308. {
  309. emit_instr(ctx, sltu, dst, src1, src2);
  310. }
  311. static inline void emit_sltiu(unsigned dst, unsigned int src,
  312. unsigned int imm, struct jit_ctx *ctx)
  313. {
  314. /* 16 bit immediate */
  315. if (!is_range16((s32)imm)) {
  316. emit_load_imm(r_tmp, imm, ctx);
  317. emit_sltu(dst, src, r_tmp, ctx);
  318. } else {
  319. emit_instr(ctx, sltiu, dst, src, imm);
  320. }
  321. }
  322. /* Store register on the stack */
  323. static inline void emit_store_stack_reg(ptr reg, ptr base,
  324. unsigned int offset,
  325. struct jit_ctx *ctx)
  326. {
  327. emit_long_instr(ctx, SW, reg, offset, base);
  328. }
  329. static inline void emit_store(ptr reg, ptr base, unsigned int offset,
  330. struct jit_ctx *ctx)
  331. {
  332. emit_instr(ctx, sw, reg, offset, base);
  333. }
  334. static inline void emit_load_stack_reg(ptr reg, ptr base,
  335. unsigned int offset,
  336. struct jit_ctx *ctx)
  337. {
  338. emit_long_instr(ctx, LW, reg, offset, base);
  339. }
  340. static inline void emit_load(unsigned int reg, unsigned int base,
  341. unsigned int offset, struct jit_ctx *ctx)
  342. {
  343. emit_instr(ctx, lw, reg, offset, base);
  344. }
  345. static inline void emit_load_byte(unsigned int reg, unsigned int base,
  346. unsigned int offset, struct jit_ctx *ctx)
  347. {
  348. emit_instr(ctx, lb, reg, offset, base);
  349. }
  350. static inline void emit_half_load(unsigned int reg, unsigned int base,
  351. unsigned int offset, struct jit_ctx *ctx)
  352. {
  353. emit_instr(ctx, lh, reg, offset, base);
  354. }
  355. static inline void emit_mul(unsigned int dst, unsigned int src1,
  356. unsigned int src2, struct jit_ctx *ctx)
  357. {
  358. emit_instr(ctx, mul, dst, src1, src2);
  359. }
  360. static inline void emit_div(unsigned int dst, unsigned int src,
  361. struct jit_ctx *ctx)
  362. {
  363. if (ctx->target != NULL) {
  364. u32 *p = &ctx->target[ctx->idx];
  365. uasm_i_divu(&p, dst, src);
  366. p = &ctx->target[ctx->idx + 1];
  367. uasm_i_mflo(&p, dst);
  368. }
  369. ctx->idx += 2; /* 2 insts */
  370. }
  371. static inline void emit_mod(unsigned int dst, unsigned int src,
  372. struct jit_ctx *ctx)
  373. {
  374. if (ctx->target != NULL) {
  375. u32 *p = &ctx->target[ctx->idx];
  376. uasm_i_divu(&p, dst, src);
  377. p = &ctx->target[ctx->idx + 1];
  378. uasm_i_mflo(&p, dst);
  379. }
  380. ctx->idx += 2; /* 2 insts */
  381. }
  382. static inline void emit_dsll(unsigned int dst, unsigned int src,
  383. unsigned int sa, struct jit_ctx *ctx)
  384. {
  385. emit_instr(ctx, dsll, dst, src, sa);
  386. }
  387. static inline void emit_dsrl32(unsigned int dst, unsigned int src,
  388. unsigned int sa, struct jit_ctx *ctx)
  389. {
  390. emit_instr(ctx, dsrl32, dst, src, sa);
  391. }
  392. static inline void emit_wsbh(unsigned int dst, unsigned int src,
  393. struct jit_ctx *ctx)
  394. {
  395. emit_instr(ctx, wsbh, dst, src);
  396. }
  397. /* load pointer to register */
  398. static inline void emit_load_ptr(unsigned int dst, unsigned int src,
  399. int imm, struct jit_ctx *ctx)
  400. {
  401. /* src contains the base addr of the 32/64-pointer */
  402. emit_long_instr(ctx, LW, dst, imm, src);
  403. }
  404. /* load a function pointer to register */
  405. static inline void emit_load_func(unsigned int reg, ptr imm,
  406. struct jit_ctx *ctx)
  407. {
  408. if (config_enabled(CONFIG_64BIT)) {
  409. /* At this point imm is always 64-bit */
  410. emit_load_imm(r_tmp, (u64)imm >> 32, ctx);
  411. emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
  412. emit_ori(r_tmp, r_tmp_imm, (imm >> 16) & 0xffff, ctx);
  413. emit_dsll(r_tmp_imm, r_tmp, 16, ctx); /* left shift by 16 */
  414. emit_ori(reg, r_tmp_imm, imm & 0xffff, ctx);
  415. } else {
  416. emit_load_imm(reg, imm, ctx);
  417. }
  418. }
  419. /* Move to real MIPS register */
  420. static inline void emit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
  421. {
  422. emit_long_instr(ctx, ADDU, dst, src, r_zero);
  423. }
  424. /* Move to JIT (32-bit) register */
  425. static inline void emit_jit_reg_move(ptr dst, ptr src, struct jit_ctx *ctx)
  426. {
  427. emit_addu(dst, src, r_zero, ctx);
  428. }
  429. /* Compute the immediate value for PC-relative branches. */
  430. static inline u32 b_imm(unsigned int tgt, struct jit_ctx *ctx)
  431. {
  432. if (ctx->target == NULL)
  433. return 0;
  434. /*
  435. * We want a pc-relative branch. We only do forward branches
  436. * so tgt is always after pc. tgt is the instruction offset
  437. * we want to jump to.
  438. * Branch on MIPS:
  439. * I: target_offset <- sign_extend(offset)
  440. * I+1: PC += target_offset (delay slot)
  441. *
  442. * ctx->idx currently points to the branch instruction
  443. * but the offset is added to the delay slot so we need
  444. * to subtract 4.
  445. */
  446. return ctx->offsets[tgt] -
  447. (ctx->idx * 4 - ctx->prologue_bytes) - 4;
  448. }
  449. static inline void emit_bcond(int cond, unsigned int reg1, unsigned int reg2,
  450. unsigned int imm, struct jit_ctx *ctx)
  451. {
  452. if (ctx->target != NULL) {
  453. u32 *p = &ctx->target[ctx->idx];
  454. switch (cond) {
  455. case MIPS_COND_EQ:
  456. uasm_i_beq(&p, reg1, reg2, imm);
  457. break;
  458. case MIPS_COND_NE:
  459. uasm_i_bne(&p, reg1, reg2, imm);
  460. break;
  461. case MIPS_COND_ALL:
  462. uasm_i_b(&p, imm);
  463. break;
  464. default:
  465. pr_warn("%s: Unhandled branch conditional: %d\n",
  466. __func__, cond);
  467. }
  468. }
  469. ctx->idx++;
  470. }
  471. static inline void emit_b(unsigned int imm, struct jit_ctx *ctx)
  472. {
  473. emit_bcond(MIPS_COND_ALL, r_zero, r_zero, imm, ctx);
  474. }
  475. static inline void emit_jalr(unsigned int link, unsigned int reg,
  476. struct jit_ctx *ctx)
  477. {
  478. emit_instr(ctx, jalr, link, reg);
  479. }
  480. static inline void emit_jr(unsigned int reg, struct jit_ctx *ctx)
  481. {
  482. emit_instr(ctx, jr, reg);
  483. }
  484. static inline u16 align_sp(unsigned int num)
  485. {
  486. /* Double word alignment for 32-bit, quadword for 64-bit */
  487. unsigned int align = config_enabled(CONFIG_64BIT) ? 16 : 8;
  488. num = (num + (align - 1)) & -align;
  489. return num;
  490. }
  491. static bool is_load_to_a(u16 inst)
  492. {
  493. switch (inst) {
  494. case BPF_LD | BPF_W | BPF_LEN:
  495. case BPF_LD | BPF_W | BPF_ABS:
  496. case BPF_LD | BPF_H | BPF_ABS:
  497. case BPF_LD | BPF_B | BPF_ABS:
  498. return true;
  499. default:
  500. return false;
  501. }
  502. }
  503. static void save_bpf_jit_regs(struct jit_ctx *ctx, unsigned offset)
  504. {
  505. int i = 0, real_off = 0;
  506. u32 sflags, tmp_flags;
  507. /* Adjust the stack pointer */
  508. emit_stack_offset(-align_sp(offset), ctx);
  509. if (ctx->flags & SEEN_CALL) {
  510. /* Argument save area */
  511. if (config_enabled(CONFIG_64BIT))
  512. /* Bottom of current frame */
  513. real_off = align_sp(offset) - RSIZE;
  514. else
  515. /* Top of previous frame */
  516. real_off = align_sp(offset) + RSIZE;
  517. emit_store_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
  518. emit_store_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx);
  519. real_off = 0;
  520. }
  521. tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
  522. /* sflags is essentially a bitmap */
  523. while (tmp_flags) {
  524. if ((sflags >> i) & 0x1) {
  525. emit_store_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
  526. ctx);
  527. real_off += RSIZE;
  528. }
  529. i++;
  530. tmp_flags >>= 1;
  531. }
  532. /* save return address */
  533. if (ctx->flags & SEEN_CALL) {
  534. emit_store_stack_reg(r_ra, r_sp, real_off, ctx);
  535. real_off += RSIZE;
  536. }
  537. /* Setup r_M leaving the alignment gap if necessary */
  538. if (ctx->flags & SEEN_MEM) {
  539. if (real_off % (RSIZE * 2))
  540. real_off += RSIZE;
  541. emit_long_instr(ctx, ADDIU, r_M, r_sp, real_off);
  542. }
  543. }
  544. static void restore_bpf_jit_regs(struct jit_ctx *ctx,
  545. unsigned int offset)
  546. {
  547. int i, real_off = 0;
  548. u32 sflags, tmp_flags;
  549. if (ctx->flags & SEEN_CALL) {
  550. if (config_enabled(CONFIG_64BIT))
  551. /* Bottom of current frame */
  552. real_off = align_sp(offset) - RSIZE;
  553. else
  554. /* Top of previous frame */
  555. real_off = align_sp(offset) + RSIZE;
  556. emit_load_stack_reg(MIPS_R_A0, r_sp, real_off, ctx);
  557. emit_load_stack_reg(MIPS_R_A1, r_sp, real_off + RSIZE, ctx);
  558. real_off = 0;
  559. }
  560. tmp_flags = sflags = ctx->flags >> SEEN_SREG_SFT;
  561. /* sflags is a bitmap */
  562. i = 0;
  563. while (tmp_flags) {
  564. if ((sflags >> i) & 0x1) {
  565. emit_load_stack_reg(MIPS_R_S0 + i, r_sp, real_off,
  566. ctx);
  567. real_off += RSIZE;
  568. }
  569. i++;
  570. tmp_flags >>= 1;
  571. }
  572. /* restore return address */
  573. if (ctx->flags & SEEN_CALL)
  574. emit_load_stack_reg(r_ra, r_sp, real_off, ctx);
  575. /* Restore the sp and discard the scrach memory */
  576. emit_stack_offset(align_sp(offset), ctx);
  577. }
  578. static unsigned int get_stack_depth(struct jit_ctx *ctx)
  579. {
  580. int sp_off = 0;
  581. /* How may s* regs do we need to preserved? */
  582. sp_off += hweight32(ctx->flags >> SEEN_SREG_SFT) * RSIZE;
  583. if (ctx->flags & SEEN_MEM)
  584. sp_off += 4 * BPF_MEMWORDS; /* BPF_MEMWORDS are 32-bit */
  585. if (ctx->flags & SEEN_CALL)
  586. /*
  587. * The JIT code make calls to external functions using 2
  588. * arguments. Therefore, for o32 we don't need to allocate
  589. * space because we don't care if the argumetns are lost
  590. * across calls. We do need however to preserve incoming
  591. * arguments but the space is already allocated for us by
  592. * the caller. On the other hand, for n64, we need to allocate
  593. * this space ourselves. We need to preserve $ra as well.
  594. */
  595. sp_off += config_enabled(CONFIG_64BIT) ?
  596. (ARGS_USED_BY_JIT + 1) * RSIZE : RSIZE;
  597. /*
  598. * Subtract the bytes for the last registers since we only care about
  599. * the location on the stack pointer.
  600. */
  601. return sp_off - RSIZE;
  602. }
  603. static void build_prologue(struct jit_ctx *ctx)
  604. {
  605. u16 first_inst = ctx->skf->insns[0].code;
  606. int sp_off;
  607. /* Calculate the total offset for the stack pointer */
  608. sp_off = get_stack_depth(ctx);
  609. save_bpf_jit_regs(ctx, sp_off);
  610. if (ctx->flags & SEEN_SKB)
  611. emit_reg_move(r_skb, MIPS_R_A0, ctx);
  612. if (ctx->flags & SEEN_X)
  613. emit_jit_reg_move(r_X, r_zero, ctx);
  614. /* Do not leak kernel data to userspace */
  615. if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst)))
  616. emit_jit_reg_move(r_A, r_zero, ctx);
  617. }
  618. static void build_epilogue(struct jit_ctx *ctx)
  619. {
  620. unsigned int sp_off;
  621. /* Calculate the total offset for the stack pointer */
  622. sp_off = get_stack_depth(ctx);
  623. restore_bpf_jit_regs(ctx, sp_off);
  624. /* Return */
  625. emit_jr(r_ra, ctx);
  626. emit_nop(ctx);
  627. }
  628. static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset)
  629. {
  630. u8 ret;
  631. int err;
  632. err = skb_copy_bits(skb, offset, &ret, 1);
  633. return (u64)err << 32 | ret;
  634. }
  635. static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset)
  636. {
  637. u16 ret;
  638. int err;
  639. err = skb_copy_bits(skb, offset, &ret, 2);
  640. return (u64)err << 32 | ntohs(ret);
  641. }
  642. static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
  643. {
  644. u32 ret;
  645. int err;
  646. err = skb_copy_bits(skb, offset, &ret, 4);
  647. return (u64)err << 32 | ntohl(ret);
  648. }
  649. static int build_body(struct jit_ctx *ctx)
  650. {
  651. void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
  652. const struct bpf_prog *prog = ctx->skf;
  653. const struct sock_filter *inst;
  654. unsigned int i, off, load_order, condt;
  655. u32 k, b_off __maybe_unused;
  656. for (i = 0; i < prog->len; i++) {
  657. u16 code;
  658. inst = &(prog->insns[i]);
  659. pr_debug("%s: code->0x%02x, jt->0x%x, jf->0x%x, k->0x%x\n",
  660. __func__, inst->code, inst->jt, inst->jf, inst->k);
  661. k = inst->k;
  662. code = bpf_anc_helper(inst);
  663. if (ctx->target == NULL)
  664. ctx->offsets[i] = ctx->idx * 4;
  665. switch (code) {
  666. case BPF_LD | BPF_IMM:
  667. /* A <- k ==> li r_A, k */
  668. ctx->flags |= SEEN_A;
  669. emit_load_imm(r_A, k, ctx);
  670. break;
  671. case BPF_LD | BPF_W | BPF_LEN:
  672. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
  673. /* A <- len ==> lw r_A, offset(skb) */
  674. ctx->flags |= SEEN_SKB | SEEN_A;
  675. off = offsetof(struct sk_buff, len);
  676. emit_load(r_A, r_skb, off, ctx);
  677. break;
  678. case BPF_LD | BPF_MEM:
  679. /* A <- M[k] ==> lw r_A, offset(M) */
  680. ctx->flags |= SEEN_MEM | SEEN_A;
  681. emit_load(r_A, r_M, SCRATCH_OFF(k), ctx);
  682. break;
  683. case BPF_LD | BPF_W | BPF_ABS:
  684. /* A <- P[k:4] */
  685. load_order = 2;
  686. goto load;
  687. case BPF_LD | BPF_H | BPF_ABS:
  688. /* A <- P[k:2] */
  689. load_order = 1;
  690. goto load;
  691. case BPF_LD | BPF_B | BPF_ABS:
  692. /* A <- P[k:1] */
  693. load_order = 0;
  694. load:
  695. /* the interpreter will deal with the negative K */
  696. if ((int)k < 0)
  697. return -ENOTSUPP;
  698. emit_load_imm(r_off, k, ctx);
  699. load_common:
  700. /*
  701. * We may got here from the indirect loads so
  702. * return if offset is negative.
  703. */
  704. emit_slt(r_s0, r_off, r_zero, ctx);
  705. emit_bcond(MIPS_COND_NE, r_s0, r_zero,
  706. b_imm(prog->len, ctx), ctx);
  707. emit_reg_move(r_ret, r_zero, ctx);
  708. ctx->flags |= SEEN_CALL | SEEN_OFF | SEEN_S0 |
  709. SEEN_SKB | SEEN_A;
  710. emit_load_func(r_s0, (ptr)load_func[load_order],
  711. ctx);
  712. emit_reg_move(MIPS_R_A0, r_skb, ctx);
  713. emit_jalr(MIPS_R_RA, r_s0, ctx);
  714. /* Load second argument to delay slot */
  715. emit_reg_move(MIPS_R_A1, r_off, ctx);
  716. /* Check the error value */
  717. if (config_enabled(CONFIG_64BIT)) {
  718. /* Get error code from the top 32-bits */
  719. emit_dsrl32(r_s0, r_val, 0, ctx);
  720. /* Branch to 3 instructions ahead */
  721. emit_bcond(MIPS_COND_NE, r_s0, r_zero, 3 << 2,
  722. ctx);
  723. } else {
  724. /* Branch to 3 instructions ahead */
  725. emit_bcond(MIPS_COND_NE, r_err, r_zero, 3 << 2,
  726. ctx);
  727. }
  728. emit_nop(ctx);
  729. /* We are good */
  730. emit_b(b_imm(i + 1, ctx), ctx);
  731. emit_jit_reg_move(r_A, r_val, ctx);
  732. /* Return with error */
  733. emit_b(b_imm(prog->len, ctx), ctx);
  734. emit_reg_move(r_ret, r_zero, ctx);
  735. break;
  736. case BPF_LD | BPF_W | BPF_IND:
  737. /* A <- P[X + k:4] */
  738. load_order = 2;
  739. goto load_ind;
  740. case BPF_LD | BPF_H | BPF_IND:
  741. /* A <- P[X + k:2] */
  742. load_order = 1;
  743. goto load_ind;
  744. case BPF_LD | BPF_B | BPF_IND:
  745. /* A <- P[X + k:1] */
  746. load_order = 0;
  747. load_ind:
  748. ctx->flags |= SEEN_OFF | SEEN_X;
  749. emit_addiu(r_off, r_X, k, ctx);
  750. goto load_common;
  751. case BPF_LDX | BPF_IMM:
  752. /* X <- k */
  753. ctx->flags |= SEEN_X;
  754. emit_load_imm(r_X, k, ctx);
  755. break;
  756. case BPF_LDX | BPF_MEM:
  757. /* X <- M[k] */
  758. ctx->flags |= SEEN_X | SEEN_MEM;
  759. emit_load(r_X, r_M, SCRATCH_OFF(k), ctx);
  760. break;
  761. case BPF_LDX | BPF_W | BPF_LEN:
  762. /* X <- len */
  763. ctx->flags |= SEEN_X | SEEN_SKB;
  764. off = offsetof(struct sk_buff, len);
  765. emit_load(r_X, r_skb, off, ctx);
  766. break;
  767. case BPF_LDX | BPF_B | BPF_MSH:
  768. /* the interpreter will deal with the negative K */
  769. if ((int)k < 0)
  770. return -ENOTSUPP;
  771. /* X <- 4 * (P[k:1] & 0xf) */
  772. ctx->flags |= SEEN_X | SEEN_CALL | SEEN_S0 | SEEN_SKB;
  773. /* Load offset to a1 */
  774. emit_load_func(r_s0, (ptr)jit_get_skb_b, ctx);
  775. /*
  776. * This may emit two instructions so it may not fit
  777. * in the delay slot. So use a0 in the delay slot.
  778. */
  779. emit_load_imm(MIPS_R_A1, k, ctx);
  780. emit_jalr(MIPS_R_RA, r_s0, ctx);
  781. emit_reg_move(MIPS_R_A0, r_skb, ctx); /* delay slot */
  782. /* Check the error value */
  783. if (config_enabled(CONFIG_64BIT)) {
  784. /* Top 32-bits of $v0 on 64-bit */
  785. emit_dsrl32(r_s0, r_val, 0, ctx);
  786. emit_bcond(MIPS_COND_NE, r_s0, r_zero,
  787. 3 << 2, ctx);
  788. } else {
  789. emit_bcond(MIPS_COND_NE, r_err, r_zero,
  790. 3 << 2, ctx);
  791. }
  792. /* No need for delay slot */
  793. /* We are good */
  794. /* X <- P[1:K] & 0xf */
  795. emit_andi(r_X, r_val, 0xf, ctx);
  796. /* X << 2 */
  797. emit_b(b_imm(i + 1, ctx), ctx);
  798. emit_sll(r_X, r_X, 2, ctx); /* delay slot */
  799. /* Return with error */
  800. emit_b(b_imm(prog->len, ctx), ctx);
  801. emit_load_imm(r_ret, 0, ctx); /* delay slot */
  802. break;
  803. case BPF_ST:
  804. /* M[k] <- A */
  805. ctx->flags |= SEEN_MEM | SEEN_A;
  806. emit_store(r_A, r_M, SCRATCH_OFF(k), ctx);
  807. break;
  808. case BPF_STX:
  809. /* M[k] <- X */
  810. ctx->flags |= SEEN_MEM | SEEN_X;
  811. emit_store(r_X, r_M, SCRATCH_OFF(k), ctx);
  812. break;
  813. case BPF_ALU | BPF_ADD | BPF_K:
  814. /* A += K */
  815. ctx->flags |= SEEN_A;
  816. emit_addiu(r_A, r_A, k, ctx);
  817. break;
  818. case BPF_ALU | BPF_ADD | BPF_X:
  819. /* A += X */
  820. ctx->flags |= SEEN_A | SEEN_X;
  821. emit_addu(r_A, r_A, r_X, ctx);
  822. break;
  823. case BPF_ALU | BPF_SUB | BPF_K:
  824. /* A -= K */
  825. ctx->flags |= SEEN_A;
  826. emit_addiu(r_A, r_A, -k, ctx);
  827. break;
  828. case BPF_ALU | BPF_SUB | BPF_X:
  829. /* A -= X */
  830. ctx->flags |= SEEN_A | SEEN_X;
  831. emit_subu(r_A, r_A, r_X, ctx);
  832. break;
  833. case BPF_ALU | BPF_MUL | BPF_K:
  834. /* A *= K */
  835. /* Load K to scratch register before MUL */
  836. ctx->flags |= SEEN_A | SEEN_S0;
  837. emit_load_imm(r_s0, k, ctx);
  838. emit_mul(r_A, r_A, r_s0, ctx);
  839. break;
  840. case BPF_ALU | BPF_MUL | BPF_X:
  841. /* A *= X */
  842. ctx->flags |= SEEN_A | SEEN_X;
  843. emit_mul(r_A, r_A, r_X, ctx);
  844. break;
  845. case BPF_ALU | BPF_DIV | BPF_K:
  846. /* A /= k */
  847. if (k == 1)
  848. break;
  849. if (optimize_div(&k)) {
  850. ctx->flags |= SEEN_A;
  851. emit_srl(r_A, r_A, k, ctx);
  852. break;
  853. }
  854. ctx->flags |= SEEN_A | SEEN_S0;
  855. emit_load_imm(r_s0, k, ctx);
  856. emit_div(r_A, r_s0, ctx);
  857. break;
  858. case BPF_ALU | BPF_MOD | BPF_K:
  859. /* A %= k */
  860. if (k == 1 || optimize_div(&k)) {
  861. ctx->flags |= SEEN_A;
  862. emit_jit_reg_move(r_A, r_zero, ctx);
  863. } else {
  864. ctx->flags |= SEEN_A | SEEN_S0;
  865. emit_load_imm(r_s0, k, ctx);
  866. emit_mod(r_A, r_s0, ctx);
  867. }
  868. break;
  869. case BPF_ALU | BPF_DIV | BPF_X:
  870. /* A /= X */
  871. ctx->flags |= SEEN_X | SEEN_A;
  872. /* Check if r_X is zero */
  873. emit_bcond(MIPS_COND_EQ, r_X, r_zero,
  874. b_imm(prog->len, ctx), ctx);
  875. emit_load_imm(r_val, 0, ctx); /* delay slot */
  876. emit_div(r_A, r_X, ctx);
  877. break;
  878. case BPF_ALU | BPF_MOD | BPF_X:
  879. /* A %= X */
  880. ctx->flags |= SEEN_X | SEEN_A;
  881. /* Check if r_X is zero */
  882. emit_bcond(MIPS_COND_EQ, r_X, r_zero,
  883. b_imm(prog->len, ctx), ctx);
  884. emit_load_imm(r_val, 0, ctx); /* delay slot */
  885. emit_mod(r_A, r_X, ctx);
  886. break;
  887. case BPF_ALU | BPF_OR | BPF_K:
  888. /* A |= K */
  889. ctx->flags |= SEEN_A;
  890. emit_ori(r_A, r_A, k, ctx);
  891. break;
  892. case BPF_ALU | BPF_OR | BPF_X:
  893. /* A |= X */
  894. ctx->flags |= SEEN_A;
  895. emit_ori(r_A, r_A, r_X, ctx);
  896. break;
  897. case BPF_ALU | BPF_XOR | BPF_K:
  898. /* A ^= k */
  899. ctx->flags |= SEEN_A;
  900. emit_xori(r_A, r_A, k, ctx);
  901. break;
  902. case BPF_ANC | SKF_AD_ALU_XOR_X:
  903. case BPF_ALU | BPF_XOR | BPF_X:
  904. /* A ^= X */
  905. ctx->flags |= SEEN_A;
  906. emit_xor(r_A, r_A, r_X, ctx);
  907. break;
  908. case BPF_ALU | BPF_AND | BPF_K:
  909. /* A &= K */
  910. ctx->flags |= SEEN_A;
  911. emit_andi(r_A, r_A, k, ctx);
  912. break;
  913. case BPF_ALU | BPF_AND | BPF_X:
  914. /* A &= X */
  915. ctx->flags |= SEEN_A | SEEN_X;
  916. emit_and(r_A, r_A, r_X, ctx);
  917. break;
  918. case BPF_ALU | BPF_LSH | BPF_K:
  919. /* A <<= K */
  920. ctx->flags |= SEEN_A;
  921. emit_sll(r_A, r_A, k, ctx);
  922. break;
  923. case BPF_ALU | BPF_LSH | BPF_X:
  924. /* A <<= X */
  925. ctx->flags |= SEEN_A | SEEN_X;
  926. emit_sllv(r_A, r_A, r_X, ctx);
  927. break;
  928. case BPF_ALU | BPF_RSH | BPF_K:
  929. /* A >>= K */
  930. ctx->flags |= SEEN_A;
  931. emit_srl(r_A, r_A, k, ctx);
  932. break;
  933. case BPF_ALU | BPF_RSH | BPF_X:
  934. ctx->flags |= SEEN_A | SEEN_X;
  935. emit_srlv(r_A, r_A, r_X, ctx);
  936. break;
  937. case BPF_ALU | BPF_NEG:
  938. /* A = -A */
  939. ctx->flags |= SEEN_A;
  940. emit_neg(r_A, ctx);
  941. break;
  942. case BPF_JMP | BPF_JA:
  943. /* pc += K */
  944. emit_b(b_imm(i + k + 1, ctx), ctx);
  945. emit_nop(ctx);
  946. break;
  947. case BPF_JMP | BPF_JEQ | BPF_K:
  948. /* pc += ( A == K ) ? pc->jt : pc->jf */
  949. condt = MIPS_COND_EQ | MIPS_COND_K;
  950. goto jmp_cmp;
  951. case BPF_JMP | BPF_JEQ | BPF_X:
  952. ctx->flags |= SEEN_X;
  953. /* pc += ( A == X ) ? pc->jt : pc->jf */
  954. condt = MIPS_COND_EQ | MIPS_COND_X;
  955. goto jmp_cmp;
  956. case BPF_JMP | BPF_JGE | BPF_K:
  957. /* pc += ( A >= K ) ? pc->jt : pc->jf */
  958. condt = MIPS_COND_GE | MIPS_COND_K;
  959. goto jmp_cmp;
  960. case BPF_JMP | BPF_JGE | BPF_X:
  961. ctx->flags |= SEEN_X;
  962. /* pc += ( A >= X ) ? pc->jt : pc->jf */
  963. condt = MIPS_COND_GE | MIPS_COND_X;
  964. goto jmp_cmp;
  965. case BPF_JMP | BPF_JGT | BPF_K:
  966. /* pc += ( A > K ) ? pc->jt : pc->jf */
  967. condt = MIPS_COND_GT | MIPS_COND_K;
  968. goto jmp_cmp;
  969. case BPF_JMP | BPF_JGT | BPF_X:
  970. ctx->flags |= SEEN_X;
  971. /* pc += ( A > X ) ? pc->jt : pc->jf */
  972. condt = MIPS_COND_GT | MIPS_COND_X;
  973. jmp_cmp:
  974. /* Greater or Equal */
  975. if ((condt & MIPS_COND_GE) ||
  976. (condt & MIPS_COND_GT)) {
  977. if (condt & MIPS_COND_K) { /* K */
  978. ctx->flags |= SEEN_S0 | SEEN_A;
  979. emit_sltiu(r_s0, r_A, k, ctx);
  980. } else { /* X */
  981. ctx->flags |= SEEN_S0 | SEEN_A |
  982. SEEN_X;
  983. emit_sltu(r_s0, r_A, r_X, ctx);
  984. }
  985. /* A < (K|X) ? r_scrach = 1 */
  986. b_off = b_imm(i + inst->jf + 1, ctx);
  987. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off,
  988. ctx);
  989. emit_nop(ctx);
  990. /* A > (K|X) ? scratch = 0 */
  991. if (condt & MIPS_COND_GT) {
  992. /* Checking for equality */
  993. ctx->flags |= SEEN_S0 | SEEN_A | SEEN_X;
  994. if (condt & MIPS_COND_K)
  995. emit_load_imm(r_s0, k, ctx);
  996. else
  997. emit_jit_reg_move(r_s0, r_X,
  998. ctx);
  999. b_off = b_imm(i + inst->jf + 1, ctx);
  1000. emit_bcond(MIPS_COND_EQ, r_A, r_s0,
  1001. b_off, ctx);
  1002. emit_nop(ctx);
  1003. /* Finally, A > K|X */
  1004. b_off = b_imm(i + inst->jt + 1, ctx);
  1005. emit_b(b_off, ctx);
  1006. emit_nop(ctx);
  1007. } else {
  1008. /* A >= (K|X) so jump */
  1009. b_off = b_imm(i + inst->jt + 1, ctx);
  1010. emit_b(b_off, ctx);
  1011. emit_nop(ctx);
  1012. }
  1013. } else {
  1014. /* A == K|X */
  1015. if (condt & MIPS_COND_K) { /* K */
  1016. ctx->flags |= SEEN_S0 | SEEN_A;
  1017. emit_load_imm(r_s0, k, ctx);
  1018. /* jump true */
  1019. b_off = b_imm(i + inst->jt + 1, ctx);
  1020. emit_bcond(MIPS_COND_EQ, r_A, r_s0,
  1021. b_off, ctx);
  1022. emit_nop(ctx);
  1023. /* jump false */
  1024. b_off = b_imm(i + inst->jf + 1,
  1025. ctx);
  1026. emit_bcond(MIPS_COND_NE, r_A, r_s0,
  1027. b_off, ctx);
  1028. emit_nop(ctx);
  1029. } else { /* X */
  1030. /* jump true */
  1031. ctx->flags |= SEEN_A | SEEN_X;
  1032. b_off = b_imm(i + inst->jt + 1,
  1033. ctx);
  1034. emit_bcond(MIPS_COND_EQ, r_A, r_X,
  1035. b_off, ctx);
  1036. emit_nop(ctx);
  1037. /* jump false */
  1038. b_off = b_imm(i + inst->jf + 1, ctx);
  1039. emit_bcond(MIPS_COND_NE, r_A, r_X,
  1040. b_off, ctx);
  1041. emit_nop(ctx);
  1042. }
  1043. }
  1044. break;
  1045. case BPF_JMP | BPF_JSET | BPF_K:
  1046. ctx->flags |= SEEN_S0 | SEEN_S1 | SEEN_A;
  1047. /* pc += (A & K) ? pc -> jt : pc -> jf */
  1048. emit_load_imm(r_s1, k, ctx);
  1049. emit_and(r_s0, r_A, r_s1, ctx);
  1050. /* jump true */
  1051. b_off = b_imm(i + inst->jt + 1, ctx);
  1052. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
  1053. emit_nop(ctx);
  1054. /* jump false */
  1055. b_off = b_imm(i + inst->jf + 1, ctx);
  1056. emit_b(b_off, ctx);
  1057. emit_nop(ctx);
  1058. break;
  1059. case BPF_JMP | BPF_JSET | BPF_X:
  1060. ctx->flags |= SEEN_S0 | SEEN_X | SEEN_A;
  1061. /* pc += (A & X) ? pc -> jt : pc -> jf */
  1062. emit_and(r_s0, r_A, r_X, ctx);
  1063. /* jump true */
  1064. b_off = b_imm(i + inst->jt + 1, ctx);
  1065. emit_bcond(MIPS_COND_NE, r_s0, r_zero, b_off, ctx);
  1066. emit_nop(ctx);
  1067. /* jump false */
  1068. b_off = b_imm(i + inst->jf + 1, ctx);
  1069. emit_b(b_off, ctx);
  1070. emit_nop(ctx);
  1071. break;
  1072. case BPF_RET | BPF_A:
  1073. ctx->flags |= SEEN_A;
  1074. if (i != prog->len - 1)
  1075. /*
  1076. * If this is not the last instruction
  1077. * then jump to the epilogue
  1078. */
  1079. emit_b(b_imm(prog->len, ctx), ctx);
  1080. emit_reg_move(r_ret, r_A, ctx); /* delay slot */
  1081. break;
  1082. case BPF_RET | BPF_K:
  1083. /*
  1084. * It can emit two instructions so it does not fit on
  1085. * the delay slot.
  1086. */
  1087. emit_load_imm(r_ret, k, ctx);
  1088. if (i != prog->len - 1) {
  1089. /*
  1090. * If this is not the last instruction
  1091. * then jump to the epilogue
  1092. */
  1093. emit_b(b_imm(prog->len, ctx), ctx);
  1094. emit_nop(ctx);
  1095. }
  1096. break;
  1097. case BPF_MISC | BPF_TAX:
  1098. /* X = A */
  1099. ctx->flags |= SEEN_X | SEEN_A;
  1100. emit_jit_reg_move(r_X, r_A, ctx);
  1101. break;
  1102. case BPF_MISC | BPF_TXA:
  1103. /* A = X */
  1104. ctx->flags |= SEEN_A | SEEN_X;
  1105. emit_jit_reg_move(r_A, r_X, ctx);
  1106. break;
  1107. /* AUX */
  1108. case BPF_ANC | SKF_AD_PROTOCOL:
  1109. /* A = ntohs(skb->protocol */
  1110. ctx->flags |= SEEN_SKB | SEEN_OFF | SEEN_A;
  1111. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  1112. protocol) != 2);
  1113. off = offsetof(struct sk_buff, protocol);
  1114. emit_half_load(r_A, r_skb, off, ctx);
  1115. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  1116. /* This needs little endian fixup */
  1117. if (cpu_has_wsbh) {
  1118. /* R2 and later have the wsbh instruction */
  1119. emit_wsbh(r_A, r_A, ctx);
  1120. } else {
  1121. /* Get first byte */
  1122. emit_andi(r_tmp_imm, r_A, 0xff, ctx);
  1123. /* Shift it */
  1124. emit_sll(r_tmp, r_tmp_imm, 8, ctx);
  1125. /* Get second byte */
  1126. emit_srl(r_tmp_imm, r_A, 8, ctx);
  1127. emit_andi(r_tmp_imm, r_tmp_imm, 0xff, ctx);
  1128. /* Put everyting together in r_A */
  1129. emit_or(r_A, r_tmp, r_tmp_imm, ctx);
  1130. }
  1131. #endif
  1132. break;
  1133. case BPF_ANC | SKF_AD_CPU:
  1134. ctx->flags |= SEEN_A | SEEN_OFF;
  1135. /* A = current_thread_info()->cpu */
  1136. BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info,
  1137. cpu) != 4);
  1138. off = offsetof(struct thread_info, cpu);
  1139. /* $28/gp points to the thread_info struct */
  1140. emit_load(r_A, 28, off, ctx);
  1141. break;
  1142. case BPF_ANC | SKF_AD_IFINDEX:
  1143. /* A = skb->dev->ifindex */
  1144. ctx->flags |= SEEN_SKB | SEEN_A | SEEN_S0;
  1145. off = offsetof(struct sk_buff, dev);
  1146. /* Load *dev pointer */
  1147. emit_load_ptr(r_s0, r_skb, off, ctx);
  1148. /* error (0) in the delay slot */
  1149. emit_bcond(MIPS_COND_EQ, r_s0, r_zero,
  1150. b_imm(prog->len, ctx), ctx);
  1151. emit_reg_move(r_ret, r_zero, ctx);
  1152. BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
  1153. ifindex) != 4);
  1154. off = offsetof(struct net_device, ifindex);
  1155. emit_load(r_A, r_s0, off, ctx);
  1156. break;
  1157. case BPF_ANC | SKF_AD_MARK:
  1158. ctx->flags |= SEEN_SKB | SEEN_A;
  1159. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
  1160. off = offsetof(struct sk_buff, mark);
  1161. emit_load(r_A, r_skb, off, ctx);
  1162. break;
  1163. case BPF_ANC | SKF_AD_RXHASH:
  1164. ctx->flags |= SEEN_SKB | SEEN_A;
  1165. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
  1166. off = offsetof(struct sk_buff, hash);
  1167. emit_load(r_A, r_skb, off, ctx);
  1168. break;
  1169. case BPF_ANC | SKF_AD_VLAN_TAG:
  1170. case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
  1171. ctx->flags |= SEEN_SKB | SEEN_S0 | SEEN_A;
  1172. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  1173. vlan_tci) != 2);
  1174. off = offsetof(struct sk_buff, vlan_tci);
  1175. emit_half_load(r_s0, r_skb, off, ctx);
  1176. if (code == (BPF_ANC | SKF_AD_VLAN_TAG)) {
  1177. emit_andi(r_A, r_s0, (u16)~VLAN_TAG_PRESENT, ctx);
  1178. } else {
  1179. emit_andi(r_A, r_s0, VLAN_TAG_PRESENT, ctx);
  1180. /* return 1 if present */
  1181. emit_sltu(r_A, r_zero, r_A, ctx);
  1182. }
  1183. break;
  1184. case BPF_ANC | SKF_AD_PKTTYPE:
  1185. ctx->flags |= SEEN_SKB;
  1186. emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
  1187. /* Keep only the last 3 bits */
  1188. emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
  1189. #ifdef __BIG_ENDIAN_BITFIELD
  1190. /* Get the actual packet type to the lower 3 bits */
  1191. emit_srl(r_A, r_A, 5, ctx);
  1192. #endif
  1193. break;
  1194. case BPF_ANC | SKF_AD_QUEUE:
  1195. ctx->flags |= SEEN_SKB | SEEN_A;
  1196. BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
  1197. queue_mapping) != 2);
  1198. BUILD_BUG_ON(offsetof(struct sk_buff,
  1199. queue_mapping) > 0xff);
  1200. off = offsetof(struct sk_buff, queue_mapping);
  1201. emit_half_load(r_A, r_skb, off, ctx);
  1202. break;
  1203. default:
  1204. pr_debug("%s: Unhandled opcode: 0x%02x\n", __FILE__,
  1205. inst->code);
  1206. return -1;
  1207. }
  1208. }
  1209. /* compute offsets only during the first pass */
  1210. if (ctx->target == NULL)
  1211. ctx->offsets[i] = ctx->idx * 4;
  1212. return 0;
  1213. }
  1214. int bpf_jit_enable __read_mostly;
  1215. void bpf_jit_compile(struct bpf_prog *fp)
  1216. {
  1217. struct jit_ctx ctx;
  1218. unsigned int alloc_size, tmp_idx;
  1219. if (!bpf_jit_enable)
  1220. return;
  1221. memset(&ctx, 0, sizeof(ctx));
  1222. ctx.offsets = kcalloc(fp->len, sizeof(*ctx.offsets), GFP_KERNEL);
  1223. if (ctx.offsets == NULL)
  1224. return;
  1225. ctx.skf = fp;
  1226. if (build_body(&ctx))
  1227. goto out;
  1228. tmp_idx = ctx.idx;
  1229. build_prologue(&ctx);
  1230. ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
  1231. /* just to complete the ctx.idx count */
  1232. build_epilogue(&ctx);
  1233. alloc_size = 4 * ctx.idx;
  1234. ctx.target = module_alloc(alloc_size);
  1235. if (ctx.target == NULL)
  1236. goto out;
  1237. /* Clean it */
  1238. memset(ctx.target, 0, alloc_size);
  1239. ctx.idx = 0;
  1240. /* Generate the actual JIT code */
  1241. build_prologue(&ctx);
  1242. build_body(&ctx);
  1243. build_epilogue(&ctx);
  1244. /* Update the icache */
  1245. flush_icache_range((ptr)ctx.target, (ptr)(ctx.target + ctx.idx));
  1246. if (bpf_jit_enable > 1)
  1247. /* Dump JIT code */
  1248. bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
  1249. fp->bpf_func = (void *)ctx.target;
  1250. fp->jited = true;
  1251. out:
  1252. kfree(ctx.offsets);
  1253. }
  1254. void bpf_jit_free(struct bpf_prog *fp)
  1255. {
  1256. if (fp->jited)
  1257. module_free(NULL, fp->bpf_func);
  1258. bpf_prog_unlock_free(fp);
  1259. }