xref: /linux/tools/testing/selftests/bpf/progs/verifier_tailcall_jit.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1e5bdd6a8SEduard Zingerman // SPDX-License-Identifier: GPL-2.0
2e5bdd6a8SEduard Zingerman #include <linux/bpf.h>
3e5bdd6a8SEduard Zingerman #include <bpf/bpf_helpers.h>
4e5bdd6a8SEduard Zingerman #include "bpf_misc.h"
5e5bdd6a8SEduard Zingerman 
6e5bdd6a8SEduard Zingerman int main(void);
7e5bdd6a8SEduard Zingerman 
8e5bdd6a8SEduard Zingerman struct {
9e5bdd6a8SEduard Zingerman 	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
10e5bdd6a8SEduard Zingerman 	__uint(max_entries, 1);
11e5bdd6a8SEduard Zingerman 	__uint(key_size, sizeof(__u32));
12e5bdd6a8SEduard Zingerman 	__array(values, void (void));
13e5bdd6a8SEduard Zingerman } jmp_table SEC(".maps") = {
14e5bdd6a8SEduard Zingerman 	.values = {
15e5bdd6a8SEduard Zingerman 		[0] = (void *) &main,
16e5bdd6a8SEduard Zingerman 	},
17e5bdd6a8SEduard Zingerman };
18e5bdd6a8SEduard Zingerman 
19e5bdd6a8SEduard Zingerman __noinline __auxiliary
sub(void)20e5bdd6a8SEduard Zingerman static __naked int sub(void)
21e5bdd6a8SEduard Zingerman {
22e5bdd6a8SEduard Zingerman 	asm volatile (
23e5bdd6a8SEduard Zingerman 	"r2 = %[jmp_table] ll;"
24e5bdd6a8SEduard Zingerman 	"r3 = 0;"
25e5bdd6a8SEduard Zingerman 	"call 12;"
26e5bdd6a8SEduard Zingerman 	"exit;"
27e5bdd6a8SEduard Zingerman 	:
28e5bdd6a8SEduard Zingerman 	: __imm_addr(jmp_table)
29e5bdd6a8SEduard Zingerman 	: __clobber_all);
30e5bdd6a8SEduard Zingerman }
31e5bdd6a8SEduard Zingerman 
32e5bdd6a8SEduard Zingerman __success
33e5bdd6a8SEduard Zingerman __arch_x86_64
34e5bdd6a8SEduard Zingerman /* program entry for main(), regular function prologue */
35e5bdd6a8SEduard Zingerman __jited("	endbr64")
36e5bdd6a8SEduard Zingerman __jited("	nopl	(%rax,%rax)")
37e5bdd6a8SEduard Zingerman __jited("	xorq	%rax, %rax")
38e5bdd6a8SEduard Zingerman __jited("	pushq	%rbp")
39e5bdd6a8SEduard Zingerman __jited("	movq	%rsp, %rbp")
40e5bdd6a8SEduard Zingerman /* tail call prologue for program:
41e5bdd6a8SEduard Zingerman  * - establish memory location for tail call counter at &rbp[-8];
42e5bdd6a8SEduard Zingerman  * - spill tail_call_cnt_ptr at &rbp[-16];
43e5bdd6a8SEduard Zingerman  * - expect tail call counter to be passed in rax;
44e5bdd6a8SEduard Zingerman  * - for entry program rax is a raw counter, value < 33;
45e5bdd6a8SEduard Zingerman  * - for tail called program rax is tail_call_cnt_ptr (value > 33).
46e5bdd6a8SEduard Zingerman  */
47e5bdd6a8SEduard Zingerman __jited("	endbr64")
48e5bdd6a8SEduard Zingerman __jited("	cmpq	$0x21, %rax")
49e5bdd6a8SEduard Zingerman __jited("	ja	L0")
50e5bdd6a8SEduard Zingerman __jited("	pushq	%rax")
51e5bdd6a8SEduard Zingerman __jited("	movq	%rsp, %rax")
52e5bdd6a8SEduard Zingerman __jited("	jmp	L1")
53e5bdd6a8SEduard Zingerman __jited("L0:	pushq	%rax")			/* rbp[-8]  = rax         */
54e5bdd6a8SEduard Zingerman __jited("L1:	pushq	%rax")			/* rbp[-16] = rax         */
55e5bdd6a8SEduard Zingerman /* on subprogram call restore rax to be tail_call_cnt_ptr from rbp[-16]
56e5bdd6a8SEduard Zingerman  * (cause original rax might be clobbered by this point)
57e5bdd6a8SEduard Zingerman  */
58e5bdd6a8SEduard Zingerman __jited("	movq	-0x10(%rbp), %rax")
59e5bdd6a8SEduard Zingerman __jited("	callq	0x{{.*}}")		/* call to sub()          */
60e5bdd6a8SEduard Zingerman __jited("	xorl	%eax, %eax")
61e5bdd6a8SEduard Zingerman __jited("	leave")
62*c52a1e6eSEduard Zingerman __jited("	{{(retq|jmp	0x)}}")		/* return or jump to rethunk */
63e5bdd6a8SEduard Zingerman __jited("...")
64e5bdd6a8SEduard Zingerman /* subprogram entry for sub(), regular function prologue */
65e5bdd6a8SEduard Zingerman __jited("	endbr64")
66e5bdd6a8SEduard Zingerman __jited("	nopl	(%rax,%rax)")
67e5bdd6a8SEduard Zingerman __jited("	nopl	(%rax)")
68e5bdd6a8SEduard Zingerman __jited("	pushq	%rbp")
69e5bdd6a8SEduard Zingerman __jited("	movq	%rsp, %rbp")
70e5bdd6a8SEduard Zingerman /* tail call prologue for subprogram address of tail call counter
71e5bdd6a8SEduard Zingerman  * stored at rbp[-16].
72e5bdd6a8SEduard Zingerman  */
73e5bdd6a8SEduard Zingerman __jited("	endbr64")
74e5bdd6a8SEduard Zingerman __jited("	pushq	%rax")			/* rbp[-8]  = rax          */
75e5bdd6a8SEduard Zingerman __jited("	pushq	%rax")			/* rbp[-16] = rax          */
76e5bdd6a8SEduard Zingerman __jited("	movabsq	${{.*}}, %rsi")		/* r2 = &jmp_table         */
77e5bdd6a8SEduard Zingerman __jited("	xorl	%edx, %edx")		/* r3 = 0                  */
78e5bdd6a8SEduard Zingerman /* bpf_tail_call implementation:
79e5bdd6a8SEduard Zingerman  * - load tail_call_cnt_ptr from rbp[-16];
80e5bdd6a8SEduard Zingerman  * - if *tail_call_cnt_ptr < 33, increment it and jump to target;
81e5bdd6a8SEduard Zingerman  * - otherwise do nothing.
82e5bdd6a8SEduard Zingerman  */
83e5bdd6a8SEduard Zingerman __jited("	movq	-0x10(%rbp), %rax")
84e5bdd6a8SEduard Zingerman __jited("	cmpq	$0x21, (%rax)")
85e5bdd6a8SEduard Zingerman __jited("	jae	L0")
86e5bdd6a8SEduard Zingerman __jited("	nopl	(%rax,%rax)")
87e5bdd6a8SEduard Zingerman __jited("	addq	$0x1, (%rax)")		/* *tail_call_cnt_ptr += 1 */
88e5bdd6a8SEduard Zingerman __jited("	popq	%rax")
89e5bdd6a8SEduard Zingerman __jited("	popq	%rax")
90e5bdd6a8SEduard Zingerman __jited("	jmp	{{.*}}")		/* jump to tail call tgt   */
91e5bdd6a8SEduard Zingerman __jited("L0:	leave")
92*c52a1e6eSEduard Zingerman __jited("	{{(retq|jmp	0x)}}")		/* return or jump to rethunk */
93e5bdd6a8SEduard Zingerman SEC("tc")
main(void)94e5bdd6a8SEduard Zingerman __naked int main(void)
95e5bdd6a8SEduard Zingerman {
96e5bdd6a8SEduard Zingerman 	asm volatile (
97e5bdd6a8SEduard Zingerman 	"call %[sub];"
98e5bdd6a8SEduard Zingerman 	"r0 = 0;"
99e5bdd6a8SEduard Zingerman 	"exit;"
100e5bdd6a8SEduard Zingerman 	:
101e5bdd6a8SEduard Zingerman 	: __imm(sub)
102e5bdd6a8SEduard Zingerman 	: __clobber_all);
103e5bdd6a8SEduard Zingerman }
104e5bdd6a8SEduard Zingerman 
105e5bdd6a8SEduard Zingerman char __license[] SEC("license") = "GPL";
106