@@ -189,6 +189,12 @@ static inline void reg_set_seen(struct bpf_jit *jit, u32 b1)
189189 _EMIT4((op) | __pcrel); \
190190})
191191
192+ #define EMIT4_PCREL_RIC (op , mask , target ) \
193+ ({ \
194+ int __rel = ((target) - jit->prg) / 2; \
195+ _EMIT4((op) | (mask) << 20 | (__rel & 0xffff)); \
196+ })
197+
192198#define _EMIT6 (op1 , op2 ) \
193199({ \
194200 if (jit->prg_buf) { \
@@ -250,17 +256,22 @@ static inline void reg_set_seen(struct bpf_jit *jit, u32 b1)
250256
251257#define EMIT6_PCREL_RILB (op , b , target ) \
252258({ \
253- int rel = (( target) - jit->prg) / 2; \
259+ unsigned int rel = (int)(( target) - jit->prg) / 2; \
254260 _EMIT6((op) | reg_high(b) << 16 | rel >> 16, rel & 0xffff);\
255261 REG_SET_SEEN(b); \
256262})
257263
258264#define EMIT6_PCREL_RIL (op , target ) \
259265({ \
260- int rel = (( target) - jit->prg) / 2; \
266+ unsigned int rel = (int)(( target) - jit->prg) / 2; \
261267 _EMIT6((op) | rel >> 16, rel & 0xffff); \
262268})
263269
270+ #define EMIT6_PCREL_RILC (op , mask , target ) \
271+ ({ \
272+ EMIT6_PCREL_RIL((op) | (mask) << 20, (target)); \
273+ })
274+
264275#define _EMIT6_IMM (op , imm ) \
265276({ \
266277 unsigned int __imm = (imm); \
@@ -322,6 +333,22 @@ static bool is_codegen_pass(struct bpf_jit *jit)
322333 return jit -> prg_buf ;
323334}
324335
336+ /*
337+ * Return whether "rel" can be encoded as a short PC-relative offset
338+ */
339+ static bool is_valid_rel (int rel )
340+ {
341+ return rel >= -65536 && rel <= 65534 ;
342+ }
343+
344+ /*
345+ * Return whether "off" can be reached using a short PC-relative offset
346+ */
347+ static bool can_use_rel (struct bpf_jit * jit , int off )
348+ {
349+ return is_valid_rel (off - jit -> prg );
350+ }
351+
325352/*
326353 * Fill whole space with illegal instructions
327354 */
@@ -525,9 +552,9 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
525552 int i , bool extra_pass )
526553{
527554 struct bpf_insn * insn = & fp -> insnsi [i ];
528- int jmp_off , last , insn_count = 1 ;
529555 u32 dst_reg = insn -> dst_reg ;
530556 u32 src_reg = insn -> src_reg ;
557+ int last , insn_count = 1 ;
531558 u32 * addrs = jit -> addrs ;
532559 s32 imm = insn -> imm ;
533560 s16 off = insn -> off ;
@@ -1071,9 +1098,17 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
10711098 /* llgf %w1,map.max_entries(%b2) */
10721099 EMIT6_DISP_LH (0xe3000000 , 0x0016 , REG_W1 , REG_0 , BPF_REG_2 ,
10731100 offsetof(struct bpf_array , map .max_entries ));
1074- /* clrj %b3,%w1,0xa,label0: if (u32)%b3 >= (u32)%w1 goto out */
1075- EMIT6_PCREL_LABEL (0xec000000 , 0x0077 , BPF_REG_3 ,
1076- REG_W1 , 0 , 0xa );
1101+ /* if ((u32)%b3 >= (u32)%w1) goto out; */
1102+ if (!is_first_pass (jit ) && can_use_rel (jit , jit -> labels [0 ])) {
1103+ /* clrj %b3,%w1,0xa,label0 */
1104+ EMIT6_PCREL_LABEL (0xec000000 , 0x0077 , BPF_REG_3 ,
1105+ REG_W1 , 0 , 0xa );
1106+ } else {
1107+ /* clr %b3,%w1 */
1108+ EMIT2 (0x1500 , BPF_REG_3 , REG_W1 );
1109+ /* brcl 0xa,label0 */
1110+ EMIT6_PCREL_RILC (0xc0040000 , 0xa , jit -> labels [0 ]);
1111+ }
10771112
10781113 /*
10791114 * if (tail_call_cnt++ > MAX_TAIL_CALL_CNT)
@@ -1088,9 +1123,16 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
10881123 EMIT4_IMM (0xa7080000 , REG_W0 , 1 );
10891124 /* laal %w1,%w0,off(%r15) */
10901125 EMIT6_DISP_LH (0xeb000000 , 0x00fa , REG_W1 , REG_W0 , REG_15 , off );
1091- /* clij %w1,MAX_TAIL_CALL_CNT,0x2,label0 */
1092- EMIT6_PCREL_IMM_LABEL (0xec000000 , 0x007f , REG_W1 ,
1093- MAX_TAIL_CALL_CNT , 0 , 0x2 );
1126+ if (!is_first_pass (jit ) && can_use_rel (jit , jit -> labels [0 ])) {
1127+ /* clij %w1,MAX_TAIL_CALL_CNT,0x2,label0 */
1128+ EMIT6_PCREL_IMM_LABEL (0xec000000 , 0x007f , REG_W1 ,
1129+ MAX_TAIL_CALL_CNT , 0 , 0x2 );
1130+ } else {
1131+ /* clfi %w1,MAX_TAIL_CALL_CNT */
1132+ EMIT6_IMM (0xc20f0000 , REG_W1 , MAX_TAIL_CALL_CNT );
1133+ /* brcl 0x2,label0 */
1134+ EMIT6_PCREL_RILC (0xc0040000 , 0x2 , jit -> labels [0 ]);
1135+ }
10941136
10951137 /*
10961138 * prog = array->ptrs[index];
@@ -1102,11 +1144,16 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
11021144 EMIT4 (0xb9160000 , REG_1 , BPF_REG_3 );
11031145 /* sllg %r1,%r1,3: %r1 *= 8 */
11041146 EMIT6_DISP_LH (0xeb000000 , 0x000d , REG_1 , REG_1 , REG_0 , 3 );
1105- /* lg %r1,prog(%b2,%r1) */
1106- EMIT6_DISP_LH (0xe3000000 , 0x0004 , REG_1 , BPF_REG_2 ,
1147+ /* ltg %r1,prog(%b2,%r1) */
1148+ EMIT6_DISP_LH (0xe3000000 , 0x0002 , REG_1 , BPF_REG_2 ,
11071149 REG_1 , offsetof(struct bpf_array , ptrs ));
1108- /* clgij %r1,0,0x8,label0 */
1109- EMIT6_PCREL_IMM_LABEL (0xec000000 , 0x007d , REG_1 , 0 , 0 , 0x8 );
1150+ if (!is_first_pass (jit ) && can_use_rel (jit , jit -> labels [0 ])) {
1151+ /* brc 0x8,label0 */
1152+ EMIT4_PCREL_RIC (0xa7040000 , 0x8 , jit -> labels [0 ]);
1153+ } else {
1154+ /* brcl 0x8,label0 */
1155+ EMIT6_PCREL_RILC (0xc0040000 , 0x8 , jit -> labels [0 ]);
1156+ }
11101157
11111158 /*
11121159 * Restore registers before calling function
@@ -1263,36 +1310,83 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
12631310 goto branch_oc ;
12641311branch_ks :
12651312 is_jmp32 = BPF_CLASS (insn -> code ) == BPF_JMP32 ;
1266- /* lgfi %w1,imm (load sign extend imm) */
1267- EMIT6_IMM (0xc0010000 , REG_W1 , imm );
1268- /* crj or cgrj %dst,%w1,mask,off */
1269- EMIT6_PCREL (0xec000000 , (is_jmp32 ? 0x0076 : 0x0064 ),
1270- dst_reg , REG_W1 , i , off , mask );
1313+ /* cfi or cgfi %dst,imm */
1314+ EMIT6_IMM (is_jmp32 ? 0xc20d0000 : 0xc20c0000 ,
1315+ dst_reg , imm );
1316+ if (!is_first_pass (jit ) &&
1317+ can_use_rel (jit , addrs [i + off + 1 ])) {
1318+ /* brc mask,off */
1319+ EMIT4_PCREL_RIC (0xa7040000 ,
1320+ mask >> 12 , addrs [i + off + 1 ]);
1321+ } else {
1322+ /* brcl mask,off */
1323+ EMIT6_PCREL_RILC (0xc0040000 ,
1324+ mask >> 12 , addrs [i + off + 1 ]);
1325+ }
12711326 break ;
12721327branch_ku :
12731328 is_jmp32 = BPF_CLASS (insn -> code ) == BPF_JMP32 ;
1274- /* lgfi %w1,imm (load sign extend imm) */
1275- EMIT6_IMM (0xc0010000 , REG_W1 , imm );
1276- /* clrj or clgrj %dst,%w1,mask,off */
1277- EMIT6_PCREL (0xec000000 , (is_jmp32 ? 0x0077 : 0x0065 ),
1278- dst_reg , REG_W1 , i , off , mask );
1329+ /* clfi or clgfi %dst,imm */
1330+ EMIT6_IMM (is_jmp32 ? 0xc20f0000 : 0xc20e0000 ,
1331+ dst_reg , imm );
1332+ if (!is_first_pass (jit ) &&
1333+ can_use_rel (jit , addrs [i + off + 1 ])) {
1334+ /* brc mask,off */
1335+ EMIT4_PCREL_RIC (0xa7040000 ,
1336+ mask >> 12 , addrs [i + off + 1 ]);
1337+ } else {
1338+ /* brcl mask,off */
1339+ EMIT6_PCREL_RILC (0xc0040000 ,
1340+ mask >> 12 , addrs [i + off + 1 ]);
1341+ }
12791342 break ;
12801343branch_xs :
12811344 is_jmp32 = BPF_CLASS (insn -> code ) == BPF_JMP32 ;
1282- /* crj or cgrj %dst,%src,mask,off */
1283- EMIT6_PCREL (0xec000000 , (is_jmp32 ? 0x0076 : 0x0064 ),
1284- dst_reg , src_reg , i , off , mask );
1345+ if (!is_first_pass (jit ) &&
1346+ can_use_rel (jit , addrs [i + off + 1 ])) {
1347+ /* crj or cgrj %dst,%src,mask,off */
1348+ EMIT6_PCREL (0xec000000 , (is_jmp32 ? 0x0076 : 0x0064 ),
1349+ dst_reg , src_reg , i , off , mask );
1350+ } else {
1351+ /* cr or cgr %dst,%src */
1352+ if (is_jmp32 )
1353+ EMIT2 (0x1900 , dst_reg , src_reg );
1354+ else
1355+ EMIT4 (0xb9200000 , dst_reg , src_reg );
1356+ /* brcl mask,off */
1357+ EMIT6_PCREL_RILC (0xc0040000 ,
1358+ mask >> 12 , addrs [i + off + 1 ]);
1359+ }
12851360 break ;
12861361branch_xu :
12871362 is_jmp32 = BPF_CLASS (insn -> code ) == BPF_JMP32 ;
1288- /* clrj or clgrj %dst,%src,mask,off */
1289- EMIT6_PCREL (0xec000000 , (is_jmp32 ? 0x0077 : 0x0065 ),
1290- dst_reg , src_reg , i , off , mask );
1363+ if (!is_first_pass (jit ) &&
1364+ can_use_rel (jit , addrs [i + off + 1 ])) {
1365+ /* clrj or clgrj %dst,%src,mask,off */
1366+ EMIT6_PCREL (0xec000000 , (is_jmp32 ? 0x0077 : 0x0065 ),
1367+ dst_reg , src_reg , i , off , mask );
1368+ } else {
1369+ /* clr or clgr %dst,%src */
1370+ if (is_jmp32 )
1371+ EMIT2 (0x1500 , dst_reg , src_reg );
1372+ else
1373+ EMIT4 (0xb9210000 , dst_reg , src_reg );
1374+ /* brcl mask,off */
1375+ EMIT6_PCREL_RILC (0xc0040000 ,
1376+ mask >> 12 , addrs [i + off + 1 ]);
1377+ }
12911378 break ;
12921379branch_oc :
1293- /* brc mask,jmp_off (branch instruction needs 4 bytes) */
1294- jmp_off = addrs [i + off + 1 ] - (addrs [i + 1 ] - 4 );
1295- EMIT4_PCREL (0xa7040000 | mask << 8 , jmp_off );
1380+ if (!is_first_pass (jit ) &&
1381+ can_use_rel (jit , addrs [i + off + 1 ])) {
1382+ /* brc mask,off */
1383+ EMIT4_PCREL_RIC (0xa7040000 ,
1384+ mask >> 12 , addrs [i + off + 1 ]);
1385+ } else {
1386+ /* brcl mask,off */
1387+ EMIT6_PCREL_RILC (0xc0040000 ,
1388+ mask >> 12 , addrs [i + off + 1 ]);
1389+ }
12961390 break ;
12971391 }
12981392 default : /* too complex, give up */
0 commit comments