bpf_jit_comp32.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * BPF JIT compiler for PA-RISC (32-bit)
  4. *
  5. * Copyright (c) 2023 Helge Deller <deller@gmx.de>
  6. *
  7. * The code is based on the BPF JIT compiler for RV64 by Björn Töpel and
  8. * the BPF JIT compiler for 32-bit ARM by Shubham Bansal and Mircea Gherzan.
  9. */
  10. #include <linux/bpf.h>
  11. #include <linux/filter.h>
  12. #include <linux/libgcc.h>
  13. #include "bpf_jit.h"
  14. /*
  15. * Stack layout during BPF program execution (note: stack grows up):
  16. *
  17. * high
  18. * HPPA32 sp => +----------+ <= HPPA32 fp
  19. * | saved sp |
  20. * | saved rp |
  21. * | ... | HPPA32 callee-saved registers
  22. * | curr args|
  23. * | local var|
  24. * +----------+ <= (sp - 4 * NR_SAVED_REGISTERS)
  25. * | lo(R9) |
  26. * | hi(R9) |
  27. * | lo(FP) | JIT scratch space for BPF registers
  28. * | hi(FP) |
  29. * | ... |
  30. * +----------+ <= (sp - 4 * NR_SAVED_REGISTERS
  31. * | | - 4 * BPF_JIT_SCRATCH_REGS)
  32. * | |
  33. * | ... | BPF program stack
  34. * | |
  35. * | ... | Function call stack
  36. * | |
  37. * +----------+
  38. * low
  39. */
  40. enum {
  41. /* Stack layout - these are offsets from top of JIT scratch space. */
  42. BPF_R8_HI,
  43. BPF_R8_LO,
  44. BPF_R9_HI,
  45. BPF_R9_LO,
  46. BPF_FP_HI,
  47. BPF_FP_LO,
  48. BPF_AX_HI,
  49. BPF_AX_LO,
  50. BPF_R0_TEMP_HI,
  51. BPF_R0_TEMP_LO,
  52. BPF_JIT_SCRATCH_REGS,
  53. };
  54. /* Number of callee-saved registers stored to stack: rp, r3-r18. */
  55. #define NR_SAVED_REGISTERS (18 - 3 + 1 + 8)
  56. /* Offset from fp for BPF registers stored on stack. */
  57. #define STACK_OFFSET(k) (- (NR_SAVED_REGISTERS + k + 1))
  58. #define STACK_ALIGN FRAME_SIZE
  59. #define EXIT_PTR_LOAD(reg) hppa_ldw(-0x08, HPPA_REG_SP, reg)
  60. #define EXIT_PTR_STORE(reg) hppa_stw(reg, -0x08, HPPA_REG_SP)
  61. #define EXIT_PTR_JUMP(reg, nop) hppa_bv(HPPA_REG_ZERO, reg, nop)
  62. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
  63. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
  64. #define TMP_REG_R0 (MAX_BPF_JIT_REG + 2)
  65. static const s8 regmap[][2] = {
  66. /* Return value from in-kernel function, and exit value from eBPF. */
  67. [BPF_REG_0] = {HPPA_REG_RET0, HPPA_REG_RET1}, /* HI/LOW */
  68. /* Arguments from eBPF program to in-kernel function. */
  69. [BPF_REG_1] = {HPPA_R(3), HPPA_R(4)},
  70. [BPF_REG_2] = {HPPA_R(5), HPPA_R(6)},
  71. [BPF_REG_3] = {HPPA_R(7), HPPA_R(8)},
  72. [BPF_REG_4] = {HPPA_R(9), HPPA_R(10)},
  73. [BPF_REG_5] = {HPPA_R(11), HPPA_R(12)},
  74. [BPF_REG_6] = {HPPA_R(13), HPPA_R(14)},
  75. [BPF_REG_7] = {HPPA_R(15), HPPA_R(16)},
  76. /*
  77. * Callee-saved registers that in-kernel function will preserve.
  78. * Stored on the stack.
  79. */
  80. [BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
  81. [BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
  82. /* Read-only frame pointer to access BPF stack. Not needed. */
  83. [BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
  84. /* Temporary register for blinding constants. Stored on the stack. */
  85. [BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
  86. /*
  87. * Temporary registers used by the JIT to operate on registers stored
  88. * on the stack. Save t0 and t1 to be used as temporaries in generated
  89. * code.
  90. */
  91. [TMP_REG_1] = {HPPA_REG_T3, HPPA_REG_T2},
  92. [TMP_REG_2] = {HPPA_REG_T5, HPPA_REG_T4},
  93. /* temporary space for BPF_R0 during libgcc and millicode calls */
  94. [TMP_REG_R0] = {STACK_OFFSET(BPF_R0_TEMP_HI), STACK_OFFSET(BPF_R0_TEMP_LO)},
  95. };
  96. static s8 hi(const s8 *r)
  97. {
  98. return r[0];
  99. }
  100. static s8 lo(const s8 *r)
  101. {
  102. return r[1];
  103. }
  104. static void emit_hppa_copy(const s8 rs, const s8 rd, struct hppa_jit_context *ctx)
  105. {
  106. REG_SET_SEEN(ctx, rd);
  107. if (OPTIMIZE_HPPA && (rs == rd))
  108. return;
  109. REG_SET_SEEN(ctx, rs);
  110. emit(hppa_copy(rs, rd), ctx);
  111. }
  112. static void emit_hppa_xor(const s8 r1, const s8 r2, const s8 r3, struct hppa_jit_context *ctx)
  113. {
  114. REG_SET_SEEN(ctx, r1);
  115. REG_SET_SEEN(ctx, r2);
  116. REG_SET_SEEN(ctx, r3);
  117. if (OPTIMIZE_HPPA && (r1 == r2)) {
  118. emit(hppa_copy(HPPA_REG_ZERO, r3), ctx);
  119. } else {
  120. emit(hppa_xor(r1, r2, r3), ctx);
  121. }
  122. }
  123. static void emit_imm(const s8 rd, s32 imm, struct hppa_jit_context *ctx)
  124. {
  125. u32 lower = im11(imm);
  126. REG_SET_SEEN(ctx, rd);
  127. if (OPTIMIZE_HPPA && relative_bits_ok(imm, 14)) {
  128. emit(hppa_ldi(imm, rd), ctx);
  129. return;
  130. }
  131. emit(hppa_ldil(imm, rd), ctx);
  132. if (OPTIMIZE_HPPA && (lower == 0))
  133. return;
  134. emit(hppa_ldo(lower, rd, rd), ctx);
  135. }
  136. static void emit_imm32(const s8 *rd, s32 imm, struct hppa_jit_context *ctx)
  137. {
  138. /* Emit immediate into lower bits. */
  139. REG_SET_SEEN(ctx, lo(rd));
  140. emit_imm(lo(rd), imm, ctx);
  141. /* Sign-extend into upper bits. */
  142. REG_SET_SEEN(ctx, hi(rd));
  143. if (imm >= 0)
  144. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  145. else
  146. emit(hppa_ldi(-1, hi(rd)), ctx);
  147. }
  148. static void emit_imm64(const s8 *rd, s32 imm_hi, s32 imm_lo,
  149. struct hppa_jit_context *ctx)
  150. {
  151. emit_imm(hi(rd), imm_hi, ctx);
  152. emit_imm(lo(rd), imm_lo, ctx);
  153. }
  154. static void __build_epilogue(bool is_tail_call, struct hppa_jit_context *ctx)
  155. {
  156. const s8 *r0 = regmap[BPF_REG_0];
  157. int i;
  158. if (is_tail_call) {
  159. /*
  160. * goto *(t0 + 4);
  161. * Skips first instruction of prologue which initializes tail
  162. * call counter. Assumes t0 contains address of target program,
  163. * see emit_bpf_tail_call.
  164. */
  165. emit(hppa_ldo(1 * HPPA_INSN_SIZE, HPPA_REG_T0, HPPA_REG_T0), ctx);
  166. emit(hppa_bv(HPPA_REG_ZERO, HPPA_REG_T0, EXEC_NEXT_INSTR), ctx);
  167. /* in delay slot: */
  168. emit(hppa_copy(HPPA_REG_TCC, HPPA_REG_TCC_IN_INIT), ctx);
  169. return;
  170. }
  171. /* load epilogue function pointer and jump to it. */
  172. /* exit point is either directly below, or the outest TCC exit function */
  173. emit(EXIT_PTR_LOAD(HPPA_REG_RP), ctx);
  174. emit(EXIT_PTR_JUMP(HPPA_REG_RP, NOP_NEXT_INSTR), ctx);
  175. /* NOTE: we are 32-bit and big-endian, so return lower 32-bit value */
  176. emit_hppa_copy(lo(r0), HPPA_REG_RET0, ctx);
  177. /* Restore callee-saved registers. */
  178. for (i = 3; i <= 18; i++) {
  179. if (OPTIMIZE_HPPA && !REG_WAS_SEEN(ctx, HPPA_R(i)))
  180. continue;
  181. emit(hppa_ldw(-REG_SIZE * (8 + (i-3)), HPPA_REG_SP, HPPA_R(i)), ctx);
  182. }
  183. /* load original return pointer (stored by outest TCC function) */
  184. emit(hppa_ldw(-0x14, HPPA_REG_SP, HPPA_REG_RP), ctx);
  185. emit(hppa_bv(HPPA_REG_ZERO, HPPA_REG_RP, EXEC_NEXT_INSTR), ctx);
  186. /* in delay slot: */
  187. emit(hppa_ldw(-0x04, HPPA_REG_SP, HPPA_REG_SP), ctx);
  188. }
  189. static bool is_stacked(s8 reg)
  190. {
  191. return reg < 0;
  192. }
  193. static const s8 *bpf_get_reg64_offset(const s8 *reg, const s8 *tmp,
  194. u16 offset_sp, struct hppa_jit_context *ctx)
  195. {
  196. if (is_stacked(hi(reg))) {
  197. emit(hppa_ldw(REG_SIZE * hi(reg) - offset_sp, HPPA_REG_SP, hi(tmp)), ctx);
  198. emit(hppa_ldw(REG_SIZE * lo(reg) - offset_sp, HPPA_REG_SP, lo(tmp)), ctx);
  199. reg = tmp;
  200. }
  201. REG_SET_SEEN(ctx, hi(reg));
  202. REG_SET_SEEN(ctx, lo(reg));
  203. return reg;
  204. }
  205. static const s8 *bpf_get_reg64(const s8 *reg, const s8 *tmp,
  206. struct hppa_jit_context *ctx)
  207. {
  208. return bpf_get_reg64_offset(reg, tmp, 0, ctx);
  209. }
  210. static const s8 *bpf_get_reg64_ref(const s8 *reg, const s8 *tmp,
  211. bool must_load, struct hppa_jit_context *ctx)
  212. {
  213. if (!OPTIMIZE_HPPA)
  214. return bpf_get_reg64(reg, tmp, ctx);
  215. if (is_stacked(hi(reg))) {
  216. if (must_load)
  217. emit(hppa_ldw(REG_SIZE * hi(reg), HPPA_REG_SP, hi(tmp)), ctx);
  218. reg = tmp;
  219. }
  220. REG_SET_SEEN(ctx, hi(reg));
  221. REG_SET_SEEN(ctx, lo(reg));
  222. return reg;
  223. }
  224. static void bpf_put_reg64(const s8 *reg, const s8 *src,
  225. struct hppa_jit_context *ctx)
  226. {
  227. if (is_stacked(hi(reg))) {
  228. emit(hppa_stw(hi(src), REG_SIZE * hi(reg), HPPA_REG_SP), ctx);
  229. emit(hppa_stw(lo(src), REG_SIZE * lo(reg), HPPA_REG_SP), ctx);
  230. }
  231. }
  232. static void bpf_save_R0(struct hppa_jit_context *ctx)
  233. {
  234. bpf_put_reg64(regmap[TMP_REG_R0], regmap[BPF_REG_0], ctx);
  235. }
  236. static void bpf_restore_R0(struct hppa_jit_context *ctx)
  237. {
  238. bpf_get_reg64(regmap[TMP_REG_R0], regmap[BPF_REG_0], ctx);
  239. }
  240. static const s8 *bpf_get_reg32(const s8 *reg, const s8 *tmp,
  241. struct hppa_jit_context *ctx)
  242. {
  243. if (is_stacked(lo(reg))) {
  244. emit(hppa_ldw(REG_SIZE * lo(reg), HPPA_REG_SP, lo(tmp)), ctx);
  245. reg = tmp;
  246. }
  247. REG_SET_SEEN(ctx, lo(reg));
  248. return reg;
  249. }
  250. static const s8 *bpf_get_reg32_ref(const s8 *reg, const s8 *tmp,
  251. struct hppa_jit_context *ctx)
  252. {
  253. if (!OPTIMIZE_HPPA)
  254. return bpf_get_reg32(reg, tmp, ctx);
  255. if (is_stacked(hi(reg))) {
  256. reg = tmp;
  257. }
  258. REG_SET_SEEN(ctx, lo(reg));
  259. return reg;
  260. }
  261. static void bpf_put_reg32(const s8 *reg, const s8 *src,
  262. struct hppa_jit_context *ctx)
  263. {
  264. if (is_stacked(lo(reg))) {
  265. REG_SET_SEEN(ctx, lo(src));
  266. emit(hppa_stw(lo(src), REG_SIZE * lo(reg), HPPA_REG_SP), ctx);
  267. if (1 && !ctx->prog->aux->verifier_zext) {
  268. REG_SET_SEEN(ctx, hi(reg));
  269. emit(hppa_stw(HPPA_REG_ZERO, REG_SIZE * hi(reg), HPPA_REG_SP), ctx);
  270. }
  271. } else if (1 && !ctx->prog->aux->verifier_zext) {
  272. REG_SET_SEEN(ctx, hi(reg));
  273. emit_hppa_copy(HPPA_REG_ZERO, hi(reg), ctx);
  274. }
  275. }
  276. /* extern hppa millicode functions */
  277. extern void $$mulI(void);
  278. extern void $$divU(void);
  279. extern void $$remU(void);
  280. static void emit_call_millicode(void *func, const s8 arg0,
  281. const s8 arg1, u8 opcode, struct hppa_jit_context *ctx)
  282. {
  283. u32 func_addr;
  284. emit_hppa_copy(arg0, HPPA_REG_ARG0, ctx);
  285. emit_hppa_copy(arg1, HPPA_REG_ARG1, ctx);
  286. /* libcgcc overwrites HPPA_REG_RET0/1, save temp. in dest. */
  287. if (arg0 != HPPA_REG_RET1)
  288. bpf_save_R0(ctx);
  289. func_addr = (uintptr_t) dereference_function_descriptor(func);
  290. emit(hppa_ldil(func_addr, HPPA_REG_R31), ctx);
  291. /* skip the following be_l instruction if divisor is zero. */
  292. if (BPF_OP(opcode) == BPF_DIV || BPF_OP(opcode) == BPF_MOD) {
  293. if (BPF_OP(opcode) == BPF_DIV)
  294. emit_hppa_copy(HPPA_REG_ZERO, HPPA_REG_RET1, ctx);
  295. else
  296. emit_hppa_copy(HPPA_REG_ARG0, HPPA_REG_RET1, ctx);
  297. emit(hppa_or_cond(HPPA_REG_ARG1, HPPA_REG_ZERO, 1, 0, HPPA_REG_ZERO), ctx);
  298. }
  299. /* Note: millicode functions use r31 as return pointer instead of rp */
  300. emit(hppa_be_l(im11(func_addr) >> 2, HPPA_REG_R31, NOP_NEXT_INSTR), ctx);
  301. emit(hppa_nop(), ctx); /* this nop is needed here for delay slot */
  302. /* Note: millicode functions return result in RET1, not RET0 */
  303. emit_hppa_copy(HPPA_REG_RET1, arg0, ctx);
  304. /* restore HPPA_REG_RET0/1, temp. save in dest. */
  305. if (arg0 != HPPA_REG_RET1)
  306. bpf_restore_R0(ctx);
  307. }
  308. static void emit_call_libgcc_ll(void *func, const s8 *arg0,
  309. const s8 *arg1, u8 opcode, struct hppa_jit_context *ctx)
  310. {
  311. u32 func_addr;
  312. emit_hppa_copy(lo(arg0), HPPA_REG_ARG0, ctx);
  313. emit_hppa_copy(hi(arg0), HPPA_REG_ARG1, ctx);
  314. emit_hppa_copy(lo(arg1), HPPA_REG_ARG2, ctx);
  315. emit_hppa_copy(hi(arg1), HPPA_REG_ARG3, ctx);
  316. /* libcgcc overwrites HPPA_REG_RET0/_RET1, so keep copy of R0 on stack */
  317. if (hi(arg0) != HPPA_REG_RET0)
  318. bpf_save_R0(ctx);
  319. /* prepare stack */
  320. emit(hppa_ldo(2 * FRAME_SIZE, HPPA_REG_SP, HPPA_REG_SP), ctx);
  321. func_addr = (uintptr_t) dereference_function_descriptor(func);
  322. emit(hppa_ldil(func_addr, HPPA_REG_R31), ctx);
  323. /* zero out the following be_l instruction if divisor is 0 (and set default values) */
  324. if (BPF_OP(opcode) == BPF_DIV || BPF_OP(opcode) == BPF_MOD) {
  325. emit_hppa_copy(HPPA_REG_ZERO, HPPA_REG_RET0, ctx);
  326. if (BPF_OP(opcode) == BPF_DIV)
  327. emit_hppa_copy(HPPA_REG_ZERO, HPPA_REG_RET1, ctx);
  328. else
  329. emit_hppa_copy(HPPA_REG_ARG0, HPPA_REG_RET1, ctx);
  330. emit(hppa_or_cond(HPPA_REG_ARG2, HPPA_REG_ARG3, 1, 0, HPPA_REG_ZERO), ctx);
  331. }
  332. emit(hppa_be_l(im11(func_addr) >> 2, HPPA_REG_R31, EXEC_NEXT_INSTR), ctx);
  333. emit_hppa_copy(HPPA_REG_R31, HPPA_REG_RP, ctx);
  334. /* restore stack */
  335. emit(hppa_ldo(-2 * FRAME_SIZE, HPPA_REG_SP, HPPA_REG_SP), ctx);
  336. emit_hppa_copy(HPPA_REG_RET0, hi(arg0), ctx);
  337. emit_hppa_copy(HPPA_REG_RET1, lo(arg0), ctx);
  338. /* restore HPPA_REG_RET0/_RET1 */
  339. if (hi(arg0) != HPPA_REG_RET0)
  340. bpf_restore_R0(ctx);
  341. }
  342. static void emit_jump(s32 paoff, bool force_far,
  343. struct hppa_jit_context *ctx)
  344. {
  345. unsigned long pc, addr;
  346. /* Note: allocate 2 instructions for jumps if force_far is set. */
  347. if (relative_bits_ok(paoff - HPPA_BRANCH_DISPLACEMENT, 17)) {
  348. /* use BL,short branch followed by nop() */
  349. emit(hppa_bl(paoff - HPPA_BRANCH_DISPLACEMENT, HPPA_REG_ZERO), ctx);
  350. if (force_far)
  351. emit(hppa_nop(), ctx);
  352. return;
  353. }
  354. pc = (uintptr_t) &ctx->insns[ctx->ninsns];
  355. addr = pc + (paoff * HPPA_INSN_SIZE);
  356. emit(hppa_ldil(addr, HPPA_REG_R31), ctx);
  357. emit(hppa_be_l(im11(addr) >> 2, HPPA_REG_R31, NOP_NEXT_INSTR), ctx); // be,l,n addr(sr4,r31), %sr0, %r31
  358. }
  359. static void emit_alu_i64(const s8 *dst, s32 imm,
  360. struct hppa_jit_context *ctx, const u8 op)
  361. {
  362. const s8 *tmp1 = regmap[TMP_REG_1];
  363. const s8 *rd;
  364. if (0 && op == BPF_MOV)
  365. rd = bpf_get_reg64_ref(dst, tmp1, false, ctx);
  366. else
  367. rd = bpf_get_reg64(dst, tmp1, ctx);
  368. /* dst = dst OP imm */
  369. switch (op) {
  370. case BPF_MOV:
  371. emit_imm32(rd, imm, ctx);
  372. break;
  373. case BPF_AND:
  374. emit_imm(HPPA_REG_T0, imm, ctx);
  375. emit(hppa_and(lo(rd), HPPA_REG_T0, lo(rd)), ctx);
  376. if (imm >= 0)
  377. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  378. break;
  379. case BPF_OR:
  380. emit_imm(HPPA_REG_T0, imm, ctx);
  381. emit(hppa_or(lo(rd), HPPA_REG_T0, lo(rd)), ctx);
  382. if (imm < 0)
  383. emit_imm(hi(rd), -1, ctx);
  384. break;
  385. case BPF_XOR:
  386. emit_imm(HPPA_REG_T0, imm, ctx);
  387. emit_hppa_xor(lo(rd), HPPA_REG_T0, lo(rd), ctx);
  388. if (imm < 0) {
  389. emit_imm(HPPA_REG_T0, -1, ctx);
  390. emit_hppa_xor(hi(rd), HPPA_REG_T0, hi(rd), ctx);
  391. }
  392. break;
  393. case BPF_LSH:
  394. if (imm == 0)
  395. break;
  396. if (imm > 32) {
  397. imm -= 32;
  398. emit(hppa_zdep(lo(rd), imm, imm, hi(rd)), ctx);
  399. emit_hppa_copy(HPPA_REG_ZERO, lo(rd), ctx);
  400. } else if (imm == 32) {
  401. emit_hppa_copy(lo(rd), hi(rd), ctx);
  402. emit_hppa_copy(HPPA_REG_ZERO, lo(rd), ctx);
  403. } else {
  404. emit(hppa_shd(hi(rd), lo(rd), 32 - imm, hi(rd)), ctx);
  405. emit(hppa_zdep(lo(rd), imm, imm, lo(rd)), ctx);
  406. }
  407. break;
  408. case BPF_RSH:
  409. if (imm == 0)
  410. break;
  411. if (imm > 32) {
  412. imm -= 32;
  413. emit(hppa_shr(hi(rd), imm, lo(rd)), ctx);
  414. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  415. } else if (imm == 32) {
  416. emit_hppa_copy(hi(rd), lo(rd), ctx);
  417. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  418. } else {
  419. emit(hppa_shrpw(hi(rd), lo(rd), imm, lo(rd)), ctx);
  420. emit(hppa_shr(hi(rd), imm, hi(rd)), ctx);
  421. }
  422. break;
  423. case BPF_ARSH:
  424. if (imm == 0)
  425. break;
  426. if (imm > 32) {
  427. imm -= 32;
  428. emit(hppa_extrws(hi(rd), 31 - imm, imm, lo(rd)), ctx);
  429. emit(hppa_extrws(hi(rd), 0, 31, hi(rd)), ctx);
  430. } else if (imm == 32) {
  431. emit_hppa_copy(hi(rd), lo(rd), ctx);
  432. emit(hppa_extrws(hi(rd), 0, 31, hi(rd)), ctx);
  433. } else {
  434. emit(hppa_shrpw(hi(rd), lo(rd), imm, lo(rd)), ctx);
  435. emit(hppa_extrws(hi(rd), 31 - imm, imm, hi(rd)), ctx);
  436. }
  437. break;
  438. default:
  439. WARN_ON(1);
  440. }
  441. bpf_put_reg64(dst, rd, ctx);
  442. }
  443. static void emit_alu_i32(const s8 *dst, s32 imm,
  444. struct hppa_jit_context *ctx, const u8 op)
  445. {
  446. const s8 *tmp1 = regmap[TMP_REG_1];
  447. const s8 *rd = bpf_get_reg32(dst, tmp1, ctx);
  448. if (op == BPF_MOV)
  449. rd = bpf_get_reg32_ref(dst, tmp1, ctx);
  450. else
  451. rd = bpf_get_reg32(dst, tmp1, ctx);
  452. /* dst = dst OP imm */
  453. switch (op) {
  454. case BPF_MOV:
  455. emit_imm(lo(rd), imm, ctx);
  456. break;
  457. case BPF_ADD:
  458. emit_imm(HPPA_REG_T0, imm, ctx);
  459. emit(hppa_add(lo(rd), HPPA_REG_T0, lo(rd)), ctx);
  460. break;
  461. case BPF_SUB:
  462. emit_imm(HPPA_REG_T0, imm, ctx);
  463. emit(hppa_sub(lo(rd), HPPA_REG_T0, lo(rd)), ctx);
  464. break;
  465. case BPF_AND:
  466. emit_imm(HPPA_REG_T0, imm, ctx);
  467. emit(hppa_and(lo(rd), HPPA_REG_T0, lo(rd)), ctx);
  468. break;
  469. case BPF_OR:
  470. emit_imm(HPPA_REG_T0, imm, ctx);
  471. emit(hppa_or(lo(rd), HPPA_REG_T0, lo(rd)), ctx);
  472. break;
  473. case BPF_XOR:
  474. emit_imm(HPPA_REG_T0, imm, ctx);
  475. emit_hppa_xor(lo(rd), HPPA_REG_T0, lo(rd), ctx);
  476. break;
  477. case BPF_LSH:
  478. if (imm != 0)
  479. emit(hppa_zdep(lo(rd), imm, imm, lo(rd)), ctx);
  480. break;
  481. case BPF_RSH:
  482. if (imm != 0)
  483. emit(hppa_shr(lo(rd), imm, lo(rd)), ctx);
  484. break;
  485. case BPF_ARSH:
  486. if (imm != 0)
  487. emit(hppa_extrws(lo(rd), 31 - imm, imm, lo(rd)), ctx);
  488. break;
  489. default:
  490. WARN_ON(1);
  491. }
  492. bpf_put_reg32(dst, rd, ctx);
  493. }
  494. static void emit_alu_r64(const s8 *dst, const s8 *src,
  495. struct hppa_jit_context *ctx, const u8 op)
  496. {
  497. const s8 *tmp1 = regmap[TMP_REG_1];
  498. const s8 *tmp2 = regmap[TMP_REG_2];
  499. const s8 *rd;
  500. const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
  501. if (op == BPF_MOV)
  502. rd = bpf_get_reg64_ref(dst, tmp1, false, ctx);
  503. else
  504. rd = bpf_get_reg64(dst, tmp1, ctx);
  505. /* dst = dst OP src */
  506. switch (op) {
  507. case BPF_MOV:
  508. emit_hppa_copy(lo(rs), lo(rd), ctx);
  509. emit_hppa_copy(hi(rs), hi(rd), ctx);
  510. break;
  511. case BPF_ADD:
  512. emit(hppa_add(lo(rd), lo(rs), lo(rd)), ctx);
  513. emit(hppa_addc(hi(rd), hi(rs), hi(rd)), ctx);
  514. break;
  515. case BPF_SUB:
  516. emit(hppa_sub(lo(rd), lo(rs), lo(rd)), ctx);
  517. emit(hppa_subb(hi(rd), hi(rs), hi(rd)), ctx);
  518. break;
  519. case BPF_AND:
  520. emit(hppa_and(lo(rd), lo(rs), lo(rd)), ctx);
  521. emit(hppa_and(hi(rd), hi(rs), hi(rd)), ctx);
  522. break;
  523. case BPF_OR:
  524. emit(hppa_or(lo(rd), lo(rs), lo(rd)), ctx);
  525. emit(hppa_or(hi(rd), hi(rs), hi(rd)), ctx);
  526. break;
  527. case BPF_XOR:
  528. emit_hppa_xor(lo(rd), lo(rs), lo(rd), ctx);
  529. emit_hppa_xor(hi(rd), hi(rs), hi(rd), ctx);
  530. break;
  531. case BPF_MUL:
  532. emit_call_libgcc_ll(__muldi3, rd, rs, op, ctx);
  533. break;
  534. case BPF_DIV:
  535. emit_call_libgcc_ll(&hppa_div64, rd, rs, op, ctx);
  536. break;
  537. case BPF_MOD:
  538. emit_call_libgcc_ll(&hppa_div64_rem, rd, rs, op, ctx);
  539. break;
  540. case BPF_LSH:
  541. emit_call_libgcc_ll(__ashldi3, rd, rs, op, ctx);
  542. break;
  543. case BPF_RSH:
  544. emit_call_libgcc_ll(__lshrdi3, rd, rs, op, ctx);
  545. break;
  546. case BPF_ARSH:
  547. emit_call_libgcc_ll(__ashrdi3, rd, rs, op, ctx);
  548. break;
  549. case BPF_NEG:
  550. emit(hppa_sub(HPPA_REG_ZERO, lo(rd), lo(rd)), ctx);
  551. emit(hppa_subb(HPPA_REG_ZERO, hi(rd), hi(rd)), ctx);
  552. break;
  553. default:
  554. WARN_ON(1);
  555. }
  556. bpf_put_reg64(dst, rd, ctx);
  557. }
  558. static void emit_alu_r32(const s8 *dst, const s8 *src,
  559. struct hppa_jit_context *ctx, const u8 op)
  560. {
  561. const s8 *tmp1 = regmap[TMP_REG_1];
  562. const s8 *tmp2 = regmap[TMP_REG_2];
  563. const s8 *rd;
  564. const s8 *rs = bpf_get_reg32(src, tmp2, ctx);
  565. if (op == BPF_MOV)
  566. rd = bpf_get_reg32_ref(dst, tmp1, ctx);
  567. else
  568. rd = bpf_get_reg32(dst, tmp1, ctx);
  569. /* dst = dst OP src */
  570. switch (op) {
  571. case BPF_MOV:
  572. emit_hppa_copy(lo(rs), lo(rd), ctx);
  573. break;
  574. case BPF_ADD:
  575. emit(hppa_add(lo(rd), lo(rs), lo(rd)), ctx);
  576. break;
  577. case BPF_SUB:
  578. emit(hppa_sub(lo(rd), lo(rs), lo(rd)), ctx);
  579. break;
  580. case BPF_AND:
  581. emit(hppa_and(lo(rd), lo(rs), lo(rd)), ctx);
  582. break;
  583. case BPF_OR:
  584. emit(hppa_or(lo(rd), lo(rs), lo(rd)), ctx);
  585. break;
  586. case BPF_XOR:
  587. emit_hppa_xor(lo(rd), lo(rs), lo(rd), ctx);
  588. break;
  589. case BPF_MUL:
  590. emit_call_millicode($$mulI, lo(rd), lo(rs), op, ctx);
  591. break;
  592. case BPF_DIV:
  593. emit_call_millicode($$divU, lo(rd), lo(rs), op, ctx);
  594. break;
  595. case BPF_MOD:
  596. emit_call_millicode($$remU, lo(rd), lo(rs), op, ctx);
  597. break;
  598. case BPF_LSH:
  599. emit(hppa_subi(0x1f, lo(rs), HPPA_REG_T0), ctx);
  600. emit(hppa_mtsar(HPPA_REG_T0), ctx);
  601. emit(hppa_depwz_sar(lo(rd), lo(rd)), ctx);
  602. break;
  603. case BPF_RSH:
  604. emit(hppa_mtsar(lo(rs)), ctx);
  605. emit(hppa_shrpw_sar(lo(rd), lo(rd)), ctx);
  606. break;
  607. case BPF_ARSH: /* sign extending arithmetic shift right */
  608. // emit(hppa_beq(lo(rs), HPPA_REG_ZERO, 2), ctx);
  609. emit(hppa_subi(0x1f, lo(rs), HPPA_REG_T0), ctx);
  610. emit(hppa_mtsar(HPPA_REG_T0), ctx);
  611. emit(hppa_extrws_sar(lo(rd), lo(rd)), ctx);
  612. break;
  613. case BPF_NEG:
  614. emit(hppa_sub(HPPA_REG_ZERO, lo(rd), lo(rd)), ctx); // sub r0,rd,rd
  615. break;
  616. default:
  617. WARN_ON(1);
  618. }
  619. bpf_put_reg32(dst, rd, ctx);
  620. }
  621. static int emit_branch_r64(const s8 *src1, const s8 *src2, s32 paoff,
  622. struct hppa_jit_context *ctx, const u8 op)
  623. {
  624. int e, s = ctx->ninsns;
  625. const s8 *tmp1 = regmap[TMP_REG_1];
  626. const s8 *tmp2 = regmap[TMP_REG_2];
  627. const s8 *rs1 = bpf_get_reg64(src1, tmp1, ctx);
  628. const s8 *rs2 = bpf_get_reg64(src2, tmp2, ctx);
  629. /*
  630. * NO_JUMP skips over the rest of the instructions and the
  631. * emit_jump, meaning the BPF branch is not taken.
  632. * JUMP skips directly to the emit_jump, meaning
  633. * the BPF branch is taken.
  634. *
  635. * The fallthrough case results in the BPF branch being taken.
  636. */
  637. #define NO_JUMP(idx) (2 + (idx) - 1)
  638. #define JUMP(idx) (0 + (idx) - 1)
  639. switch (op) {
  640. case BPF_JEQ:
  641. emit(hppa_bne(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  642. emit(hppa_bne(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  643. break;
  644. case BPF_JGT:
  645. emit(hppa_bgtu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  646. emit(hppa_bltu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  647. emit(hppa_bleu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  648. break;
  649. case BPF_JLT:
  650. emit(hppa_bltu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  651. emit(hppa_bgtu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  652. emit(hppa_bgeu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  653. break;
  654. case BPF_JGE:
  655. emit(hppa_bgtu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  656. emit(hppa_bltu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  657. emit(hppa_bltu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  658. break;
  659. case BPF_JLE:
  660. emit(hppa_bltu(hi(rs1), hi(rs2), JUMP(2)), ctx);
  661. emit(hppa_bgtu(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  662. emit(hppa_bgtu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  663. break;
  664. case BPF_JNE:
  665. emit(hppa_bne(hi(rs1), hi(rs2), JUMP(1)), ctx);
  666. emit(hppa_beq(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  667. break;
  668. case BPF_JSGT:
  669. emit(hppa_bgt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  670. emit(hppa_blt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  671. emit(hppa_bleu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  672. break;
  673. case BPF_JSLT:
  674. emit(hppa_blt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  675. emit(hppa_bgt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  676. emit(hppa_bgeu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  677. break;
  678. case BPF_JSGE:
  679. emit(hppa_bgt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  680. emit(hppa_blt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  681. emit(hppa_bltu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  682. break;
  683. case BPF_JSLE:
  684. emit(hppa_blt(hi(rs1), hi(rs2), JUMP(2)), ctx);
  685. emit(hppa_bgt(hi(rs1), hi(rs2), NO_JUMP(1)), ctx);
  686. emit(hppa_bgtu(lo(rs1), lo(rs2), NO_JUMP(0)), ctx);
  687. break;
  688. case BPF_JSET:
  689. emit(hppa_and(hi(rs1), hi(rs2), HPPA_REG_T0), ctx);
  690. emit(hppa_and(lo(rs1), lo(rs2), HPPA_REG_T1), ctx);
  691. emit(hppa_bne(HPPA_REG_T0, HPPA_REG_ZERO, JUMP(1)), ctx);
  692. emit(hppa_beq(HPPA_REG_T1, HPPA_REG_ZERO, NO_JUMP(0)), ctx);
  693. break;
  694. default:
  695. WARN_ON(1);
  696. }
  697. #undef NO_JUMP
  698. #undef JUMP
  699. e = ctx->ninsns;
  700. /* Adjust for extra insns. */
  701. paoff -= (e - s);
  702. emit_jump(paoff, true, ctx);
  703. return 0;
  704. }
  705. static int emit_bcc(u8 op, u8 rd, u8 rs, int paoff, struct hppa_jit_context *ctx)
  706. {
  707. int e, s;
  708. bool far = false;
  709. int off;
  710. if (op == BPF_JSET) {
  711. /*
  712. * BPF_JSET is a special case: it has no inverse so we always
  713. * treat it as a far branch.
  714. */
  715. emit(hppa_and(rd, rs, HPPA_REG_T0), ctx);
  716. paoff -= 1; /* reduce offset due to hppa_and() above */
  717. rd = HPPA_REG_T0;
  718. rs = HPPA_REG_ZERO;
  719. op = BPF_JNE;
  720. }
  721. s = ctx->ninsns;
  722. if (!relative_bits_ok(paoff - HPPA_BRANCH_DISPLACEMENT, 12)) {
  723. op = invert_bpf_cond(op);
  724. far = true;
  725. }
  726. /*
  727. * For a far branch, the condition is negated and we jump over the
  728. * branch itself, and the three instructions from emit_jump.
  729. * For a near branch, just use paoff.
  730. */
  731. off = far ? (HPPA_BRANCH_DISPLACEMENT - 1) : paoff - HPPA_BRANCH_DISPLACEMENT;
  732. switch (op) {
  733. /* IF (dst COND src) JUMP off */
  734. case BPF_JEQ:
  735. emit(hppa_beq(rd, rs, off), ctx);
  736. break;
  737. case BPF_JGT:
  738. emit(hppa_bgtu(rd, rs, off), ctx);
  739. break;
  740. case BPF_JLT:
  741. emit(hppa_bltu(rd, rs, off), ctx);
  742. break;
  743. case BPF_JGE:
  744. emit(hppa_bgeu(rd, rs, off), ctx);
  745. break;
  746. case BPF_JLE:
  747. emit(hppa_bleu(rd, rs, off), ctx);
  748. break;
  749. case BPF_JNE:
  750. emit(hppa_bne(rd, rs, off), ctx);
  751. break;
  752. case BPF_JSGT:
  753. emit(hppa_bgt(rd, rs, off), ctx);
  754. break;
  755. case BPF_JSLT:
  756. emit(hppa_blt(rd, rs, off), ctx);
  757. break;
  758. case BPF_JSGE:
  759. emit(hppa_bge(rd, rs, off), ctx);
  760. break;
  761. case BPF_JSLE:
  762. emit(hppa_ble(rd, rs, off), ctx);
  763. break;
  764. default:
  765. WARN_ON(1);
  766. }
  767. if (far) {
  768. e = ctx->ninsns;
  769. /* Adjust for extra insns. */
  770. paoff -= (e - s);
  771. emit_jump(paoff, true, ctx);
  772. }
  773. return 0;
  774. }
  775. static int emit_branch_r32(const s8 *src1, const s8 *src2, s32 paoff,
  776. struct hppa_jit_context *ctx, const u8 op)
  777. {
  778. int e, s = ctx->ninsns;
  779. const s8 *tmp1 = regmap[TMP_REG_1];
  780. const s8 *tmp2 = regmap[TMP_REG_2];
  781. const s8 *rs1 = bpf_get_reg32(src1, tmp1, ctx);
  782. const s8 *rs2 = bpf_get_reg32(src2, tmp2, ctx);
  783. e = ctx->ninsns;
  784. /* Adjust for extra insns. */
  785. paoff -= (e - s);
  786. if (emit_bcc(op, lo(rs1), lo(rs2), paoff, ctx))
  787. return -1;
  788. return 0;
  789. }
  790. static void emit_call(bool fixed, u64 addr, struct hppa_jit_context *ctx)
  791. {
  792. const s8 *tmp = regmap[TMP_REG_1];
  793. const s8 *r0 = regmap[BPF_REG_0];
  794. const s8 *reg;
  795. const int offset_sp = 2 * STACK_ALIGN;
  796. /* prepare stack */
  797. emit(hppa_ldo(offset_sp, HPPA_REG_SP, HPPA_REG_SP), ctx);
  798. /* load R1 & R2 in registers, R3-R5 to stack. */
  799. reg = bpf_get_reg64_offset(regmap[BPF_REG_5], tmp, offset_sp, ctx);
  800. emit(hppa_stw(hi(reg), -0x48, HPPA_REG_SP), ctx);
  801. emit(hppa_stw(lo(reg), -0x44, HPPA_REG_SP), ctx);
  802. reg = bpf_get_reg64_offset(regmap[BPF_REG_4], tmp, offset_sp, ctx);
  803. emit(hppa_stw(hi(reg), -0x40, HPPA_REG_SP), ctx);
  804. emit(hppa_stw(lo(reg), -0x3c, HPPA_REG_SP), ctx);
  805. reg = bpf_get_reg64_offset(regmap[BPF_REG_3], tmp, offset_sp, ctx);
  806. emit(hppa_stw(hi(reg), -0x38, HPPA_REG_SP), ctx);
  807. emit(hppa_stw(lo(reg), -0x34, HPPA_REG_SP), ctx);
  808. reg = bpf_get_reg64_offset(regmap[BPF_REG_2], tmp, offset_sp, ctx);
  809. emit_hppa_copy(hi(reg), HPPA_REG_ARG3, ctx);
  810. emit_hppa_copy(lo(reg), HPPA_REG_ARG2, ctx);
  811. reg = bpf_get_reg64_offset(regmap[BPF_REG_1], tmp, offset_sp, ctx);
  812. emit_hppa_copy(hi(reg), HPPA_REG_ARG1, ctx);
  813. emit_hppa_copy(lo(reg), HPPA_REG_ARG0, ctx);
  814. /* backup TCC */
  815. if (REG_WAS_SEEN(ctx, HPPA_REG_TCC))
  816. emit(hppa_copy(HPPA_REG_TCC, HPPA_REG_TCC_SAVED), ctx);
  817. /*
  818. * Use ldil() to load absolute address. Don't use emit_imm as the
  819. * number of emitted instructions should not depend on the value of
  820. * addr.
  821. */
  822. emit(hppa_ldil(addr, HPPA_REG_R31), ctx);
  823. emit(hppa_be_l(im11(addr) >> 2, HPPA_REG_R31, EXEC_NEXT_INSTR), ctx);
  824. /* set return address in delay slot */
  825. emit_hppa_copy(HPPA_REG_R31, HPPA_REG_RP, ctx);
  826. /* restore TCC */
  827. if (REG_WAS_SEEN(ctx, HPPA_REG_TCC))
  828. emit(hppa_copy(HPPA_REG_TCC_SAVED, HPPA_REG_TCC), ctx);
  829. /* restore stack */
  830. emit(hppa_ldo(-offset_sp, HPPA_REG_SP, HPPA_REG_SP), ctx);
  831. /* set return value. */
  832. emit_hppa_copy(HPPA_REG_RET0, hi(r0), ctx);
  833. emit_hppa_copy(HPPA_REG_RET1, lo(r0), ctx);
  834. }
  835. static int emit_bpf_tail_call(int insn, struct hppa_jit_context *ctx)
  836. {
  837. /*
  838. * R1 -> &ctx
  839. * R2 -> &array
  840. * R3 -> index
  841. */
  842. int off;
  843. const s8 *arr_reg = regmap[BPF_REG_2];
  844. const s8 *idx_reg = regmap[BPF_REG_3];
  845. struct bpf_array bpfa;
  846. struct bpf_prog bpfp;
  847. /* get address of TCC main exit function for error case into rp */
  848. emit(EXIT_PTR_LOAD(HPPA_REG_RP), ctx);
  849. /* max_entries = array->map.max_entries; */
  850. off = offsetof(struct bpf_array, map.max_entries);
  851. BUILD_BUG_ON(sizeof(bpfa.map.max_entries) != 4);
  852. emit(hppa_ldw(off, lo(arr_reg), HPPA_REG_T1), ctx);
  853. /*
  854. * if (index >= max_entries)
  855. * goto out;
  856. */
  857. emit(hppa_bltu(lo(idx_reg), HPPA_REG_T1, 2 - HPPA_BRANCH_DISPLACEMENT), ctx);
  858. emit(EXIT_PTR_JUMP(HPPA_REG_RP, NOP_NEXT_INSTR), ctx);
  859. /*
  860. * if (--tcc < 0)
  861. * goto out;
  862. */
  863. REG_FORCE_SEEN(ctx, HPPA_REG_TCC);
  864. emit(hppa_ldo(-1, HPPA_REG_TCC, HPPA_REG_TCC), ctx);
  865. emit(hppa_bge(HPPA_REG_TCC, HPPA_REG_ZERO, 2 - HPPA_BRANCH_DISPLACEMENT), ctx);
  866. emit(EXIT_PTR_JUMP(HPPA_REG_RP, NOP_NEXT_INSTR), ctx);
  867. /*
  868. * prog = array->ptrs[index];
  869. * if (!prog)
  870. * goto out;
  871. */
  872. BUILD_BUG_ON(sizeof(bpfa.ptrs[0]) != 4);
  873. emit(hppa_sh2add(lo(idx_reg), lo(arr_reg), HPPA_REG_T0), ctx);
  874. off = offsetof(struct bpf_array, ptrs);
  875. BUILD_BUG_ON(!relative_bits_ok(off, 11));
  876. emit(hppa_ldw(off, HPPA_REG_T0, HPPA_REG_T0), ctx);
  877. emit(hppa_bne(HPPA_REG_T0, HPPA_REG_ZERO, 2 - HPPA_BRANCH_DISPLACEMENT), ctx);
  878. emit(EXIT_PTR_JUMP(HPPA_REG_RP, NOP_NEXT_INSTR), ctx);
  879. /*
  880. * tcc = temp_tcc;
  881. * goto *(prog->bpf_func + 4);
  882. */
  883. off = offsetof(struct bpf_prog, bpf_func);
  884. BUILD_BUG_ON(!relative_bits_ok(off, 11));
  885. BUILD_BUG_ON(sizeof(bpfp.bpf_func) != 4);
  886. emit(hppa_ldw(off, HPPA_REG_T0, HPPA_REG_T0), ctx);
  887. /* Epilogue jumps to *(t0 + 4). */
  888. __build_epilogue(true, ctx);
  889. return 0;
  890. }
  891. static int emit_load_r64(const s8 *dst, const s8 *src, s16 off,
  892. struct hppa_jit_context *ctx, const u8 size)
  893. {
  894. const s8 *tmp1 = regmap[TMP_REG_1];
  895. const s8 *tmp2 = regmap[TMP_REG_2];
  896. const s8 *rd = bpf_get_reg64_ref(dst, tmp1, ctx->prog->aux->verifier_zext, ctx);
  897. const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
  898. s8 srcreg;
  899. /* need to calculate address since offset does not fit in 14 bits? */
  900. if (relative_bits_ok(off, 14))
  901. srcreg = lo(rs);
  902. else {
  903. /* need to use R1 here, since addil puts result into R1 */
  904. srcreg = HPPA_REG_R1;
  905. emit(hppa_addil(off, lo(rs)), ctx);
  906. off = im11(off);
  907. }
  908. /* LDX: dst = *(size *)(src + off) */
  909. switch (size) {
  910. case BPF_B:
  911. emit(hppa_ldb(off + 0, srcreg, lo(rd)), ctx);
  912. if (!ctx->prog->aux->verifier_zext)
  913. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  914. break;
  915. case BPF_H:
  916. emit(hppa_ldh(off + 0, srcreg, lo(rd)), ctx);
  917. if (!ctx->prog->aux->verifier_zext)
  918. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  919. break;
  920. case BPF_W:
  921. emit(hppa_ldw(off + 0, srcreg, lo(rd)), ctx);
  922. if (!ctx->prog->aux->verifier_zext)
  923. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  924. break;
  925. case BPF_DW:
  926. emit(hppa_ldw(off + 0, srcreg, hi(rd)), ctx);
  927. emit(hppa_ldw(off + 4, srcreg, lo(rd)), ctx);
  928. break;
  929. }
  930. bpf_put_reg64(dst, rd, ctx);
  931. return 0;
  932. }
  933. static int emit_store_r64(const s8 *dst, const s8 *src, s16 off,
  934. struct hppa_jit_context *ctx, const u8 size,
  935. const u8 mode)
  936. {
  937. const s8 *tmp1 = regmap[TMP_REG_1];
  938. const s8 *tmp2 = regmap[TMP_REG_2];
  939. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  940. const s8 *rs = bpf_get_reg64(src, tmp2, ctx);
  941. s8 dstreg;
  942. /* need to calculate address since offset does not fit in 14 bits? */
  943. if (relative_bits_ok(off, 14))
  944. dstreg = lo(rd);
  945. else {
  946. /* need to use R1 here, since addil puts result into R1 */
  947. dstreg = HPPA_REG_R1;
  948. emit(hppa_addil(off, lo(rd)), ctx);
  949. off = im11(off);
  950. }
  951. /* ST: *(size *)(dst + off) = imm */
  952. switch (size) {
  953. case BPF_B:
  954. emit(hppa_stb(lo(rs), off + 0, dstreg), ctx);
  955. break;
  956. case BPF_H:
  957. emit(hppa_sth(lo(rs), off + 0, dstreg), ctx);
  958. break;
  959. case BPF_W:
  960. emit(hppa_stw(lo(rs), off + 0, dstreg), ctx);
  961. break;
  962. case BPF_DW:
  963. emit(hppa_stw(hi(rs), off + 0, dstreg), ctx);
  964. emit(hppa_stw(lo(rs), off + 4, dstreg), ctx);
  965. break;
  966. }
  967. return 0;
  968. }
  969. static void emit_rev16(const s8 rd, struct hppa_jit_context *ctx)
  970. {
  971. emit(hppa_extru(rd, 23, 8, HPPA_REG_T1), ctx);
  972. emit(hppa_depwz(rd, 23, 8, HPPA_REG_T1), ctx);
  973. emit(hppa_extru(HPPA_REG_T1, 31, 16, rd), ctx);
  974. }
  975. static void emit_rev32(const s8 rs, const s8 rd, struct hppa_jit_context *ctx)
  976. {
  977. emit(hppa_shrpw(rs, rs, 16, HPPA_REG_T1), ctx);
  978. emit(hppa_depwz(HPPA_REG_T1, 15, 8, HPPA_REG_T1), ctx);
  979. emit(hppa_shrpw(rs, HPPA_REG_T1, 8, rd), ctx);
  980. }
  981. static void emit_zext64(const s8 *dst, struct hppa_jit_context *ctx)
  982. {
  983. const s8 *rd;
  984. const s8 *tmp1 = regmap[TMP_REG_1];
  985. rd = bpf_get_reg64(dst, tmp1, ctx);
  986. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  987. bpf_put_reg64(dst, rd, ctx);
  988. }
  989. int bpf_jit_emit_insn(const struct bpf_insn *insn, struct hppa_jit_context *ctx,
  990. bool extra_pass)
  991. {
  992. bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
  993. BPF_CLASS(insn->code) == BPF_JMP;
  994. int s, e, paoff, i = insn - ctx->prog->insnsi;
  995. u8 code = insn->code;
  996. s16 off = insn->off;
  997. s32 imm = insn->imm;
  998. const s8 *dst = regmap[insn->dst_reg];
  999. const s8 *src = regmap[insn->src_reg];
  1000. const s8 *tmp1 = regmap[TMP_REG_1];
  1001. const s8 *tmp2 = regmap[TMP_REG_2];
  1002. if (0) printk("CLASS %03d CODE %#02x ALU64:%d BPF_SIZE %#02x "
  1003. "BPF_CODE %#02x src_reg %d dst_reg %d\n",
  1004. BPF_CLASS(code), code, (code & BPF_ALU64) ? 1:0, BPF_SIZE(code),
  1005. BPF_OP(code), insn->src_reg, insn->dst_reg);
  1006. switch (code) {
  1007. /* dst = src */
  1008. case BPF_ALU64 | BPF_MOV | BPF_X:
  1009. case BPF_ALU64 | BPF_ADD | BPF_X:
  1010. case BPF_ALU64 | BPF_ADD | BPF_K:
  1011. case BPF_ALU64 | BPF_SUB | BPF_X:
  1012. case BPF_ALU64 | BPF_SUB | BPF_K:
  1013. case BPF_ALU64 | BPF_AND | BPF_X:
  1014. case BPF_ALU64 | BPF_OR | BPF_X:
  1015. case BPF_ALU64 | BPF_XOR | BPF_X:
  1016. case BPF_ALU64 | BPF_MUL | BPF_X:
  1017. case BPF_ALU64 | BPF_MUL | BPF_K:
  1018. case BPF_ALU64 | BPF_DIV | BPF_X:
  1019. case BPF_ALU64 | BPF_DIV | BPF_K:
  1020. case BPF_ALU64 | BPF_MOD | BPF_X:
  1021. case BPF_ALU64 | BPF_MOD | BPF_K:
  1022. case BPF_ALU64 | BPF_LSH | BPF_X:
  1023. case BPF_ALU64 | BPF_RSH | BPF_X:
  1024. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1025. if (BPF_SRC(code) == BPF_K) {
  1026. emit_imm32(tmp2, imm, ctx);
  1027. src = tmp2;
  1028. }
  1029. emit_alu_r64(dst, src, ctx, BPF_OP(code));
  1030. break;
  1031. /* dst = -dst */
  1032. case BPF_ALU64 | BPF_NEG:
  1033. emit_alu_r64(dst, tmp2, ctx, BPF_OP(code));
  1034. break;
  1035. case BPF_ALU64 | BPF_MOV | BPF_K:
  1036. case BPF_ALU64 | BPF_AND | BPF_K:
  1037. case BPF_ALU64 | BPF_OR | BPF_K:
  1038. case BPF_ALU64 | BPF_XOR | BPF_K:
  1039. case BPF_ALU64 | BPF_LSH | BPF_K:
  1040. case BPF_ALU64 | BPF_RSH | BPF_K:
  1041. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1042. emit_alu_i64(dst, imm, ctx, BPF_OP(code));
  1043. break;
  1044. case BPF_ALU | BPF_MOV | BPF_X:
  1045. if (imm == 1) {
  1046. /* Special mov32 for zext. */
  1047. emit_zext64(dst, ctx);
  1048. break;
  1049. }
  1050. fallthrough;
  1051. /* dst = dst OP src */
  1052. case BPF_ALU | BPF_ADD | BPF_X:
  1053. case BPF_ALU | BPF_SUB | BPF_X:
  1054. case BPF_ALU | BPF_AND | BPF_X:
  1055. case BPF_ALU | BPF_OR | BPF_X:
  1056. case BPF_ALU | BPF_XOR | BPF_X:
  1057. case BPF_ALU | BPF_MUL | BPF_X:
  1058. case BPF_ALU | BPF_MUL | BPF_K:
  1059. case BPF_ALU | BPF_DIV | BPF_X:
  1060. case BPF_ALU | BPF_DIV | BPF_K:
  1061. case BPF_ALU | BPF_MOD | BPF_X:
  1062. case BPF_ALU | BPF_MOD | BPF_K:
  1063. case BPF_ALU | BPF_LSH | BPF_X:
  1064. case BPF_ALU | BPF_RSH | BPF_X:
  1065. case BPF_ALU | BPF_ARSH | BPF_X:
  1066. if (BPF_SRC(code) == BPF_K) {
  1067. emit_imm32(tmp2, imm, ctx);
  1068. src = tmp2;
  1069. }
  1070. emit_alu_r32(dst, src, ctx, BPF_OP(code));
  1071. break;
  1072. /* dst = dst OP imm */
  1073. case BPF_ALU | BPF_MOV | BPF_K:
  1074. case BPF_ALU | BPF_ADD | BPF_K:
  1075. case BPF_ALU | BPF_SUB | BPF_K:
  1076. case BPF_ALU | BPF_AND | BPF_K:
  1077. case BPF_ALU | BPF_OR | BPF_K:
  1078. case BPF_ALU | BPF_XOR | BPF_K:
  1079. case BPF_ALU | BPF_LSH | BPF_K:
  1080. case BPF_ALU | BPF_RSH | BPF_K:
  1081. case BPF_ALU | BPF_ARSH | BPF_K:
  1082. /*
  1083. * mul,div,mod are handled in the BPF_X case.
  1084. */
  1085. emit_alu_i32(dst, imm, ctx, BPF_OP(code));
  1086. break;
  1087. /* dst = -dst */
  1088. case BPF_ALU | BPF_NEG:
  1089. /*
  1090. * src is ignored---choose tmp2 as a dummy register since it
  1091. * is not on the stack.
  1092. */
  1093. emit_alu_r32(dst, tmp2, ctx, BPF_OP(code));
  1094. break;
  1095. /* dst = BSWAP##imm(dst) */
  1096. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1097. {
  1098. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  1099. switch (imm) {
  1100. case 16:
  1101. /* zero-extend 16 bits into 64 bits */
  1102. emit(hppa_extru(lo(rd), 31, 16, lo(rd)), ctx);
  1103. fallthrough;
  1104. case 32:
  1105. /* zero-extend 32 bits into 64 bits */
  1106. if (!ctx->prog->aux->verifier_zext)
  1107. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  1108. break;
  1109. case 64:
  1110. /* Do nothing. */
  1111. break;
  1112. default:
  1113. pr_err("bpf-jit: BPF_END imm %d invalid\n", imm);
  1114. return -1;
  1115. }
  1116. bpf_put_reg64(dst, rd, ctx);
  1117. break;
  1118. }
  1119. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1120. {
  1121. const s8 *rd = bpf_get_reg64(dst, tmp1, ctx);
  1122. switch (imm) {
  1123. case 16:
  1124. emit_rev16(lo(rd), ctx);
  1125. if (!ctx->prog->aux->verifier_zext)
  1126. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  1127. break;
  1128. case 32:
  1129. emit_rev32(lo(rd), lo(rd), ctx);
  1130. if (!ctx->prog->aux->verifier_zext)
  1131. emit_hppa_copy(HPPA_REG_ZERO, hi(rd), ctx);
  1132. break;
  1133. case 64:
  1134. /* Swap upper and lower halves, then each half. */
  1135. emit_hppa_copy(hi(rd), HPPA_REG_T0, ctx);
  1136. emit_rev32(lo(rd), hi(rd), ctx);
  1137. emit_rev32(HPPA_REG_T0, lo(rd), ctx);
  1138. break;
  1139. default:
  1140. pr_err("bpf-jit: BPF_END imm %d invalid\n", imm);
  1141. return -1;
  1142. }
  1143. bpf_put_reg64(dst, rd, ctx);
  1144. break;
  1145. }
  1146. /* JUMP off */
  1147. case BPF_JMP | BPF_JA:
  1148. paoff = hppa_offset(i, off, ctx);
  1149. emit_jump(paoff, false, ctx);
  1150. break;
  1151. /* function call */
  1152. case BPF_JMP | BPF_CALL:
  1153. {
  1154. bool fixed;
  1155. int ret;
  1156. u64 addr;
  1157. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &addr,
  1158. &fixed);
  1159. if (ret < 0)
  1160. return ret;
  1161. emit_call(fixed, addr, ctx);
  1162. break;
  1163. }
  1164. /* tail call */
  1165. case BPF_JMP | BPF_TAIL_CALL:
  1166. REG_SET_SEEN_ALL(ctx);
  1167. if (emit_bpf_tail_call(i, ctx))
  1168. return -1;
  1169. break;
  1170. /* IF (dst COND imm) JUMP off */
  1171. case BPF_JMP | BPF_JEQ | BPF_X:
  1172. case BPF_JMP | BPF_JEQ | BPF_K:
  1173. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1174. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1175. case BPF_JMP | BPF_JNE | BPF_X:
  1176. case BPF_JMP | BPF_JNE | BPF_K:
  1177. case BPF_JMP32 | BPF_JNE | BPF_X:
  1178. case BPF_JMP32 | BPF_JNE | BPF_K:
  1179. case BPF_JMP | BPF_JLE | BPF_X:
  1180. case BPF_JMP | BPF_JLE | BPF_K:
  1181. case BPF_JMP32 | BPF_JLE | BPF_X:
  1182. case BPF_JMP32 | BPF_JLE | BPF_K:
  1183. case BPF_JMP | BPF_JLT | BPF_X:
  1184. case BPF_JMP | BPF_JLT | BPF_K:
  1185. case BPF_JMP32 | BPF_JLT | BPF_X:
  1186. case BPF_JMP32 | BPF_JLT | BPF_K:
  1187. case BPF_JMP | BPF_JGE | BPF_X:
  1188. case BPF_JMP | BPF_JGE | BPF_K:
  1189. case BPF_JMP32 | BPF_JGE | BPF_X:
  1190. case BPF_JMP32 | BPF_JGE | BPF_K:
  1191. case BPF_JMP | BPF_JGT | BPF_X:
  1192. case BPF_JMP | BPF_JGT | BPF_K:
  1193. case BPF_JMP32 | BPF_JGT | BPF_X:
  1194. case BPF_JMP32 | BPF_JGT | BPF_K:
  1195. case BPF_JMP | BPF_JSLE | BPF_X:
  1196. case BPF_JMP | BPF_JSLE | BPF_K:
  1197. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1198. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1199. case BPF_JMP | BPF_JSLT | BPF_X:
  1200. case BPF_JMP | BPF_JSLT | BPF_K:
  1201. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1202. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1203. case BPF_JMP | BPF_JSGE | BPF_X:
  1204. case BPF_JMP | BPF_JSGE | BPF_K:
  1205. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1206. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1207. case BPF_JMP | BPF_JSGT | BPF_X:
  1208. case BPF_JMP | BPF_JSGT | BPF_K:
  1209. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1210. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1211. case BPF_JMP | BPF_JSET | BPF_X:
  1212. case BPF_JMP | BPF_JSET | BPF_K:
  1213. case BPF_JMP32 | BPF_JSET | BPF_X:
  1214. case BPF_JMP32 | BPF_JSET | BPF_K:
  1215. paoff = hppa_offset(i, off, ctx);
  1216. if (BPF_SRC(code) == BPF_K) {
  1217. s = ctx->ninsns;
  1218. emit_imm32(tmp2, imm, ctx);
  1219. src = tmp2;
  1220. e = ctx->ninsns;
  1221. paoff -= (e - s);
  1222. }
  1223. if (is64)
  1224. emit_branch_r64(dst, src, paoff, ctx, BPF_OP(code));
  1225. else
  1226. emit_branch_r32(dst, src, paoff, ctx, BPF_OP(code));
  1227. break;
  1228. /* function return */
  1229. case BPF_JMP | BPF_EXIT:
  1230. if (i == ctx->prog->len - 1)
  1231. break;
  1232. /* load epilogue function pointer and jump to it. */
  1233. emit(EXIT_PTR_LOAD(HPPA_REG_RP), ctx);
  1234. emit(EXIT_PTR_JUMP(HPPA_REG_RP, NOP_NEXT_INSTR), ctx);
  1235. break;
  1236. /* dst = imm64 */
  1237. case BPF_LD | BPF_IMM | BPF_DW:
  1238. {
  1239. struct bpf_insn insn1 = insn[1];
  1240. u32 upper = insn1.imm;
  1241. u32 lower = imm;
  1242. const s8 *rd = bpf_get_reg64_ref(dst, tmp1, false, ctx);
  1243. if (0 && bpf_pseudo_func(insn)) {
  1244. WARN_ON(upper); /* we are 32-bit! */
  1245. upper = 0;
  1246. lower = (uintptr_t) dereference_function_descriptor(lower);
  1247. }
  1248. emit_imm64(rd, upper, lower, ctx);
  1249. bpf_put_reg64(dst, rd, ctx);
  1250. return 1;
  1251. }
  1252. /* LDX: dst = *(size *)(src + off) */
  1253. case BPF_LDX | BPF_MEM | BPF_B:
  1254. case BPF_LDX | BPF_MEM | BPF_H:
  1255. case BPF_LDX | BPF_MEM | BPF_W:
  1256. case BPF_LDX | BPF_MEM | BPF_DW:
  1257. if (emit_load_r64(dst, src, off, ctx, BPF_SIZE(code)))
  1258. return -1;
  1259. break;
  1260. /* speculation barrier */
  1261. case BPF_ST | BPF_NOSPEC:
  1262. break;
  1263. /* ST: *(size *)(dst + off) = imm */
  1264. case BPF_ST | BPF_MEM | BPF_B:
  1265. case BPF_ST | BPF_MEM | BPF_H:
  1266. case BPF_ST | BPF_MEM | BPF_W:
  1267. case BPF_ST | BPF_MEM | BPF_DW:
  1268. case BPF_STX | BPF_MEM | BPF_B:
  1269. case BPF_STX | BPF_MEM | BPF_H:
  1270. case BPF_STX | BPF_MEM | BPF_W:
  1271. case BPF_STX | BPF_MEM | BPF_DW:
  1272. if (BPF_CLASS(code) == BPF_ST) {
  1273. emit_imm32(tmp2, imm, ctx);
  1274. src = tmp2;
  1275. }
  1276. if (emit_store_r64(dst, src, off, ctx, BPF_SIZE(code),
  1277. BPF_MODE(code)))
  1278. return -1;
  1279. break;
  1280. case BPF_STX | BPF_ATOMIC | BPF_W:
  1281. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1282. pr_info_once(
  1283. "bpf-jit: not supported: atomic operation %02x ***\n",
  1284. insn->imm);
  1285. return -EFAULT;
  1286. default:
  1287. pr_err("bpf-jit: unknown opcode %02x\n", code);
  1288. return -EINVAL;
  1289. }
  1290. return 0;
  1291. }
  1292. void bpf_jit_build_prologue(struct hppa_jit_context *ctx)
  1293. {
  1294. const s8 *tmp = regmap[TMP_REG_1];
  1295. const s8 *dst, *reg;
  1296. int stack_adjust = 0;
  1297. int i;
  1298. unsigned long addr;
  1299. int bpf_stack_adjust;
  1300. /*
  1301. * stack on hppa grows up, so if tail calls are used we need to
  1302. * allocate the maximum stack size
  1303. */
  1304. if (REG_ALL_SEEN(ctx))
  1305. bpf_stack_adjust = MAX_BPF_STACK;
  1306. else
  1307. bpf_stack_adjust = ctx->prog->aux->stack_depth;
  1308. bpf_stack_adjust = round_up(bpf_stack_adjust, STACK_ALIGN);
  1309. /* make space for callee-saved registers. */
  1310. stack_adjust += NR_SAVED_REGISTERS * REG_SIZE;
  1311. /* make space for BPF registers on stack. */
  1312. stack_adjust += BPF_JIT_SCRATCH_REGS * REG_SIZE;
  1313. /* make space for BPF stack. */
  1314. stack_adjust += bpf_stack_adjust;
  1315. /* round up for stack alignment. */
  1316. stack_adjust = round_up(stack_adjust, STACK_ALIGN);
  1317. /*
  1318. * The first instruction sets the tail-call-counter (TCC) register.
  1319. * This instruction is skipped by tail calls.
  1320. * Use a temporary register instead of a caller-saved register initially.
  1321. */
  1322. emit(hppa_ldi(MAX_TAIL_CALL_CNT, HPPA_REG_TCC_IN_INIT), ctx);
  1323. /*
  1324. * skip all initializations when called as BPF TAIL call.
  1325. */
  1326. emit(hppa_ldi(MAX_TAIL_CALL_CNT, HPPA_REG_R1), ctx);
  1327. emit(hppa_bne(HPPA_REG_TCC_IN_INIT, HPPA_REG_R1, ctx->prologue_len - 2 - HPPA_BRANCH_DISPLACEMENT), ctx);
  1328. /* set up hppa stack frame. */
  1329. emit_hppa_copy(HPPA_REG_SP, HPPA_REG_R1, ctx); // copy sp,r1 (=prev_sp)
  1330. emit(hppa_ldo(stack_adjust, HPPA_REG_SP, HPPA_REG_SP), ctx); // ldo stack_adjust(sp),sp (increase stack)
  1331. emit(hppa_stw(HPPA_REG_R1, -REG_SIZE, HPPA_REG_SP), ctx); // stw prev_sp,-0x04(sp)
  1332. emit(hppa_stw(HPPA_REG_RP, -0x14, HPPA_REG_SP), ctx); // stw rp,-0x14(sp)
  1333. REG_FORCE_SEEN(ctx, HPPA_REG_T0);
  1334. REG_FORCE_SEEN(ctx, HPPA_REG_T1);
  1335. REG_FORCE_SEEN(ctx, HPPA_REG_T2);
  1336. REG_FORCE_SEEN(ctx, HPPA_REG_T3);
  1337. REG_FORCE_SEEN(ctx, HPPA_REG_T4);
  1338. REG_FORCE_SEEN(ctx, HPPA_REG_T5);
  1339. /* save callee-save registers. */
  1340. for (i = 3; i <= 18; i++) {
  1341. if (OPTIMIZE_HPPA && !REG_WAS_SEEN(ctx, HPPA_R(i)))
  1342. continue;
  1343. emit(hppa_stw(HPPA_R(i), -REG_SIZE * (8 + (i-3)), HPPA_REG_SP), ctx); // stw ri,-save_area(sp)
  1344. }
  1345. /*
  1346. * now really set the tail call counter (TCC) register.
  1347. */
  1348. if (REG_WAS_SEEN(ctx, HPPA_REG_TCC))
  1349. emit(hppa_ldi(MAX_TAIL_CALL_CNT, HPPA_REG_TCC), ctx);
  1350. /*
  1351. * save epilogue function pointer for outer TCC call chain.
  1352. * The main TCC call stores the final RP on stack.
  1353. */
  1354. addr = (uintptr_t) &ctx->insns[ctx->epilogue_offset];
  1355. /* skip first two instructions of exit function, which jump to exit */
  1356. addr += 2 * HPPA_INSN_SIZE;
  1357. emit(hppa_ldil(addr, HPPA_REG_T2), ctx);
  1358. emit(hppa_ldo(im11(addr), HPPA_REG_T2, HPPA_REG_T2), ctx);
  1359. emit(EXIT_PTR_STORE(HPPA_REG_T2), ctx);
  1360. /* load R1 & R2 from registers, R3-R5 from stack. */
  1361. /* use HPPA_REG_R1 which holds the old stack value */
  1362. dst = regmap[BPF_REG_5];
  1363. reg = bpf_get_reg64_ref(dst, tmp, false, ctx);
  1364. if (REG_WAS_SEEN(ctx, lo(reg)) | REG_WAS_SEEN(ctx, hi(reg))) {
  1365. if (REG_WAS_SEEN(ctx, hi(reg)))
  1366. emit(hppa_ldw(-0x48, HPPA_REG_R1, hi(reg)), ctx);
  1367. if (REG_WAS_SEEN(ctx, lo(reg)))
  1368. emit(hppa_ldw(-0x44, HPPA_REG_R1, lo(reg)), ctx);
  1369. bpf_put_reg64(dst, tmp, ctx);
  1370. }
  1371. dst = regmap[BPF_REG_4];
  1372. reg = bpf_get_reg64_ref(dst, tmp, false, ctx);
  1373. if (REG_WAS_SEEN(ctx, lo(reg)) | REG_WAS_SEEN(ctx, hi(reg))) {
  1374. if (REG_WAS_SEEN(ctx, hi(reg)))
  1375. emit(hppa_ldw(-0x40, HPPA_REG_R1, hi(reg)), ctx);
  1376. if (REG_WAS_SEEN(ctx, lo(reg)))
  1377. emit(hppa_ldw(-0x3c, HPPA_REG_R1, lo(reg)), ctx);
  1378. bpf_put_reg64(dst, tmp, ctx);
  1379. }
  1380. dst = regmap[BPF_REG_3];
  1381. reg = bpf_get_reg64_ref(dst, tmp, false, ctx);
  1382. if (REG_WAS_SEEN(ctx, lo(reg)) | REG_WAS_SEEN(ctx, hi(reg))) {
  1383. if (REG_WAS_SEEN(ctx, hi(reg)))
  1384. emit(hppa_ldw(-0x38, HPPA_REG_R1, hi(reg)), ctx);
  1385. if (REG_WAS_SEEN(ctx, lo(reg)))
  1386. emit(hppa_ldw(-0x34, HPPA_REG_R1, lo(reg)), ctx);
  1387. bpf_put_reg64(dst, tmp, ctx);
  1388. }
  1389. dst = regmap[BPF_REG_2];
  1390. reg = bpf_get_reg64_ref(dst, tmp, false, ctx);
  1391. if (REG_WAS_SEEN(ctx, lo(reg)) | REG_WAS_SEEN(ctx, hi(reg))) {
  1392. if (REG_WAS_SEEN(ctx, hi(reg)))
  1393. emit_hppa_copy(HPPA_REG_ARG3, hi(reg), ctx);
  1394. if (REG_WAS_SEEN(ctx, lo(reg)))
  1395. emit_hppa_copy(HPPA_REG_ARG2, lo(reg), ctx);
  1396. bpf_put_reg64(dst, tmp, ctx);
  1397. }
  1398. dst = regmap[BPF_REG_1];
  1399. reg = bpf_get_reg64_ref(dst, tmp, false, ctx);
  1400. if (REG_WAS_SEEN(ctx, lo(reg)) | REG_WAS_SEEN(ctx, hi(reg))) {
  1401. if (REG_WAS_SEEN(ctx, hi(reg)))
  1402. emit_hppa_copy(HPPA_REG_ARG1, hi(reg), ctx);
  1403. if (REG_WAS_SEEN(ctx, lo(reg)))
  1404. emit_hppa_copy(HPPA_REG_ARG0, lo(reg), ctx);
  1405. bpf_put_reg64(dst, tmp, ctx);
  1406. }
  1407. /* Set up BPF frame pointer. */
  1408. dst = regmap[BPF_REG_FP];
  1409. reg = bpf_get_reg64_ref(dst, tmp, false, ctx);
  1410. if (REG_WAS_SEEN(ctx, lo(reg)) | REG_WAS_SEEN(ctx, hi(reg))) {
  1411. if (REG_WAS_SEEN(ctx, lo(reg)))
  1412. emit(hppa_ldo(-REG_SIZE * (NR_SAVED_REGISTERS + BPF_JIT_SCRATCH_REGS),
  1413. HPPA_REG_SP, lo(reg)), ctx);
  1414. if (REG_WAS_SEEN(ctx, hi(reg)))
  1415. emit_hppa_copy(HPPA_REG_ZERO, hi(reg), ctx);
  1416. bpf_put_reg64(dst, tmp, ctx);
  1417. }
  1418. emit(hppa_nop(), ctx);
  1419. }
  1420. void bpf_jit_build_epilogue(struct hppa_jit_context *ctx)
  1421. {
  1422. __build_epilogue(false, ctx);
  1423. }