Skip to content

Commit 204c628

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
bpf: Add helper macro bpf_addr_space_cast()
Introduce helper macro bpf_addr_space_cast() that emits: rX = rX instruction with off = BPF_ADDR_SPACE_CAST and encodes dest and src address_space-s into imm32. It's useful with older LLVM that doesn't emit this insn automatically. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 2e7ba4f commit 204c628

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,49 @@ l_true: \
343343
asm volatile("%[reg]=%[reg]"::[reg]"r"((short)var))
344344
#endif
345345

346+
/* emit instruction:
347+
* rX = rX .off = BPF_ADDR_SPACE_CAST .imm32 = (dst_as << 16) | src_as
348+
*/
349+
#ifndef bpf_addr_space_cast
350+
#define bpf_addr_space_cast(var, dst_as, src_as)\
351+
asm volatile(".byte 0xBF; \
352+
.ifc %[reg], r0; \
353+
.byte 0x00; \
354+
.endif; \
355+
.ifc %[reg], r1; \
356+
.byte 0x11; \
357+
.endif; \
358+
.ifc %[reg], r2; \
359+
.byte 0x22; \
360+
.endif; \
361+
.ifc %[reg], r3; \
362+
.byte 0x33; \
363+
.endif; \
364+
.ifc %[reg], r4; \
365+
.byte 0x44; \
366+
.endif; \
367+
.ifc %[reg], r5; \
368+
.byte 0x55; \
369+
.endif; \
370+
.ifc %[reg], r6; \
371+
.byte 0x66; \
372+
.endif; \
373+
.ifc %[reg], r7; \
374+
.byte 0x77; \
375+
.endif; \
376+
.ifc %[reg], r8; \
377+
.byte 0x88; \
378+
.endif; \
379+
.ifc %[reg], r9; \
380+
.byte 0x99; \
381+
.endif; \
382+
.short %[off]; \
383+
.long %[as]" \
384+
: [reg]"+r"(var) \
385+
: [off]"i"(BPF_ADDR_SPACE_CAST) \
386+
, [as]"i"((dst_as << 16) | src_as));
387+
#endif
388+
346389
/* Description
347390
* Assert that a conditional expression is true.
348391
* Returns

0 commit comments

Comments
 (0)