Skip to content

Commit f616549

Browse files
puranjaymohanAlexei Starovoitov
authored andcommitted
selftests: bpf: Add tests for signed loads from arena
Add tests for loading 8, 16, and 32 bits with sign extension from arena, also verify that exception handling is working correctly and correct assembly is being generated by the x86 and arm64 JITs. Signed-off-by: Puranjay Mohan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent eab2a71 commit f616549

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

tools/testing/selftests/bpf/progs/verifier_ldsx.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
#include <linux/bpf.h>
44
#include <bpf/bpf_helpers.h>
55
#include "bpf_misc.h"
6+
#include "bpf_arena_common.h"
67

78
#if (defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86) || \
89
(defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64) || \
910
defined(__TARGET_ARCH_arm) || defined(__TARGET_ARCH_s390) || \
1011
defined(__TARGET_ARCH_loongarch)) && \
1112
__clang_major__ >= 18
1213

14+
struct {
15+
__uint(type, BPF_MAP_TYPE_ARENA);
16+
__uint(map_flags, BPF_F_MMAPABLE);
17+
__uint(max_entries, 1);
18+
} arena SEC(".maps");
19+
1320
SEC("socket")
1421
__description("LDSX, S8")
1522
__success __success_unpriv __retval(-2)
@@ -256,6 +263,175 @@ __naked void ldsx_ctx_8(void)
256263
: __clobber_all);
257264
}
258265

266+
SEC("syscall")
267+
__description("Arena LDSX Disasm")
268+
__success
269+
__arch_x86_64
270+
__jited("movslq 0x10(%rax,%r12), %r14")
271+
__jited("movswq 0x18(%rax,%r12), %r14")
272+
__jited("movsbq 0x20(%rax,%r12), %r14")
273+
__jited("movslq 0x10(%rdi,%r12), %r15")
274+
__jited("movswq 0x18(%rdi,%r12), %r15")
275+
__jited("movsbq 0x20(%rdi,%r12), %r15")
276+
__arch_arm64
277+
__jited("add x11, x7, x28")
278+
__jited("ldrsw x21, [x11, #0x10]")
279+
__jited("add x11, x7, x28")
280+
__jited("ldrsh x21, [x11, #0x18]")
281+
__jited("add x11, x7, x28")
282+
__jited("ldrsb x21, [x11, #0x20]")
283+
__jited("add x11, x0, x28")
284+
__jited("ldrsw x22, [x11, #0x10]")
285+
__jited("add x11, x0, x28")
286+
__jited("ldrsh x22, [x11, #0x18]")
287+
__jited("add x11, x0, x28")
288+
__jited("ldrsb x22, [x11, #0x20]")
289+
__naked void arena_ldsx_disasm(void *ctx)
290+
{
291+
asm volatile (
292+
"r1 = %[arena] ll;"
293+
"r2 = 0;"
294+
"r3 = 1;"
295+
"r4 = %[numa_no_node];"
296+
"r5 = 0;"
297+
"call %[bpf_arena_alloc_pages];"
298+
"r0 = addr_space_cast(r0, 0x0, 0x1);"
299+
"r1 = r0;"
300+
"r8 = *(s32 *)(r0 + 16);"
301+
"r8 = *(s16 *)(r0 + 24);"
302+
"r8 = *(s8 *)(r0 + 32);"
303+
"r9 = *(s32 *)(r1 + 16);"
304+
"r9 = *(s16 *)(r1 + 24);"
305+
"r9 = *(s8 *)(r1 + 32);"
306+
"r0 = 0;"
307+
"exit;"
308+
:: __imm(bpf_arena_alloc_pages),
309+
__imm_addr(arena),
310+
__imm_const(numa_no_node, NUMA_NO_NODE)
311+
: __clobber_all
312+
);
313+
}
314+
315+
SEC("syscall")
316+
__description("Arena LDSX Exception")
317+
__success __retval(0)
318+
__arch_x86_64
319+
__arch_arm64
320+
__naked void arena_ldsx_exception(void *ctx)
321+
{
322+
asm volatile (
323+
"r1 = %[arena] ll;"
324+
"r0 = 0xdeadbeef;"
325+
"r0 = addr_space_cast(r0, 0x0, 0x1);"
326+
"r1 = 0x3fe;"
327+
"*(u64 *)(r0 + 0) = r1;"
328+
"r0 = *(s8 *)(r0 + 0);"
329+
"exit;"
330+
:
331+
: __imm_addr(arena)
332+
: __clobber_all
333+
);
334+
}
335+
336+
SEC("syscall")
337+
__description("Arena LDSX, S8")
338+
__success __retval(-1)
339+
__arch_x86_64
340+
__arch_arm64
341+
__naked void arena_ldsx_s8(void *ctx)
342+
{
343+
asm volatile (
344+
"r1 = %[arena] ll;"
345+
"r2 = 0;"
346+
"r3 = 1;"
347+
"r4 = %[numa_no_node];"
348+
"r5 = 0;"
349+
"call %[bpf_arena_alloc_pages];"
350+
"r0 = addr_space_cast(r0, 0x0, 0x1);"
351+
"r1 = 0x3fe;"
352+
"*(u64 *)(r0 + 0) = r1;"
353+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
354+
"r0 = *(s8 *)(r0 + 0);"
355+
#else
356+
"r0 = *(s8 *)(r0 + 7);"
357+
#endif
358+
"r0 >>= 1;"
359+
"exit;"
360+
:: __imm(bpf_arena_alloc_pages),
361+
__imm_addr(arena),
362+
__imm_const(numa_no_node, NUMA_NO_NODE)
363+
: __clobber_all
364+
);
365+
}
366+
367+
SEC("syscall")
368+
__description("Arena LDSX, S16")
369+
__success __retval(-1)
370+
__arch_x86_64
371+
__arch_arm64
372+
__naked void arena_ldsx_s16(void *ctx)
373+
{
374+
asm volatile (
375+
"r1 = %[arena] ll;"
376+
"r2 = 0;"
377+
"r3 = 1;"
378+
"r4 = %[numa_no_node];"
379+
"r5 = 0;"
380+
"call %[bpf_arena_alloc_pages];"
381+
"r0 = addr_space_cast(r0, 0x0, 0x1);"
382+
"r1 = 0x3fffe;"
383+
"*(u64 *)(r0 + 0) = r1;"
384+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
385+
"r0 = *(s16 *)(r0 + 0);"
386+
#else
387+
"r0 = *(s16 *)(r0 + 6);"
388+
#endif
389+
"r0 >>= 1;"
390+
"exit;"
391+
:: __imm(bpf_arena_alloc_pages),
392+
__imm_addr(arena),
393+
__imm_const(numa_no_node, NUMA_NO_NODE)
394+
: __clobber_all
395+
);
396+
}
397+
398+
SEC("syscall")
399+
__description("Arena LDSX, S32")
400+
__success __retval(-1)
401+
__arch_x86_64
402+
__arch_arm64
403+
__naked void arena_ldsx_s32(void *ctx)
404+
{
405+
asm volatile (
406+
"r1 = %[arena] ll;"
407+
"r2 = 0;"
408+
"r3 = 1;"
409+
"r4 = %[numa_no_node];"
410+
"r5 = 0;"
411+
"call %[bpf_arena_alloc_pages];"
412+
"r0 = addr_space_cast(r0, 0x0, 0x1);"
413+
"r1 = 0xfffffffe;"
414+
"*(u64 *)(r0 + 0) = r1;"
415+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
416+
"r0 = *(s32 *)(r0 + 0);"
417+
#else
418+
"r0 = *(s32 *)(r0 + 4);"
419+
#endif
420+
"r0 >>= 1;"
421+
"exit;"
422+
:: __imm(bpf_arena_alloc_pages),
423+
__imm_addr(arena),
424+
__imm_const(numa_no_node, NUMA_NO_NODE)
425+
: __clobber_all
426+
);
427+
}
428+
429+
/* to retain debug info for BTF generation */
430+
void kfunc_root(void)
431+
{
432+
bpf_arena_alloc_pages(0, 0, 0, 0, 0);
433+
}
434+
259435
#else
260436

261437
SEC("socket")

0 commit comments

Comments
 (0)