|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <vmlinux.h> |
| 4 | +#include <bpf/bpf_helpers.h> |
| 5 | +#include "bpf_misc.h" |
| 6 | +#include "bpf_experimental.h" |
| 7 | + |
| 8 | +/* From include/linux/filter.h */ |
| 9 | +#define MAX_BPF_STACK 512 |
| 10 | + |
| 11 | +#if defined(__TARGET_ARCH_x86) |
| 12 | + |
| 13 | +SEC("kprobe") |
| 14 | +__description("Private stack, single prog") |
| 15 | +__success |
| 16 | +__arch_x86_64 |
| 17 | +__jited(" movabsq $0x{{.*}}, %r9") |
| 18 | +__jited(" addq %gs:0x{{.*}}, %r9") |
| 19 | +__jited(" movl $0x2a, %edi") |
| 20 | +__jited(" movq %rdi, -0x100(%r9)") |
| 21 | +__naked void private_stack_single_prog(void) |
| 22 | +{ |
| 23 | + asm volatile ( |
| 24 | + "r1 = 42;" |
| 25 | + "*(u64 *)(r10 - 256) = r1;" |
| 26 | + "r0 = 0;" |
| 27 | + "exit;" |
| 28 | + : |
| 29 | + : |
| 30 | + : __clobber_all); |
| 31 | +} |
| 32 | + |
| 33 | +__used |
| 34 | +__naked static void cumulative_stack_depth_subprog(void) |
| 35 | +{ |
| 36 | + asm volatile ( |
| 37 | + "r1 = 41;" |
| 38 | + "*(u64 *)(r10 - 32) = r1;" |
| 39 | + "call %[bpf_get_smp_processor_id];" |
| 40 | + "exit;" |
| 41 | + :: __imm(bpf_get_smp_processor_id) |
| 42 | + : __clobber_all); |
| 43 | +} |
| 44 | + |
| 45 | +SEC("kprobe") |
| 46 | +__description("Private stack, subtree > MAX_BPF_STACK") |
| 47 | +__success |
| 48 | +__arch_x86_64 |
| 49 | +/* private stack fp for the main prog */ |
| 50 | +__jited(" movabsq $0x{{.*}}, %r9") |
| 51 | +__jited(" addq %gs:0x{{.*}}, %r9") |
| 52 | +__jited(" movl $0x2a, %edi") |
| 53 | +__jited(" movq %rdi, -0x200(%r9)") |
| 54 | +__jited(" pushq %r9") |
| 55 | +__jited(" callq 0x{{.*}}") |
| 56 | +__jited(" popq %r9") |
| 57 | +__jited(" xorl %eax, %eax") |
| 58 | +__naked void private_stack_nested_1(void) |
| 59 | +{ |
| 60 | + asm volatile ( |
| 61 | + "r1 = 42;" |
| 62 | + "*(u64 *)(r10 - %[max_bpf_stack]) = r1;" |
| 63 | + "call cumulative_stack_depth_subprog;" |
| 64 | + "r0 = 0;" |
| 65 | + "exit;" |
| 66 | + : |
| 67 | + : __imm_const(max_bpf_stack, MAX_BPF_STACK) |
| 68 | + : __clobber_all); |
| 69 | +} |
| 70 | + |
| 71 | +SEC("kprobe") |
| 72 | +__description("Private stack, subtree > MAX_BPF_STACK") |
| 73 | +__success |
| 74 | +__arch_x86_64 |
| 75 | +/* private stack fp for the subprog */ |
| 76 | +__jited(" addq $0x20, %r9") |
| 77 | +__naked void private_stack_nested_2(void) |
| 78 | +{ |
| 79 | + asm volatile ( |
| 80 | + "r1 = 42;" |
| 81 | + "*(u64 *)(r10 - %[max_bpf_stack]) = r1;" |
| 82 | + "call cumulative_stack_depth_subprog;" |
| 83 | + "r0 = 0;" |
| 84 | + "exit;" |
| 85 | + : |
| 86 | + : __imm_const(max_bpf_stack, MAX_BPF_STACK) |
| 87 | + : __clobber_all); |
| 88 | +} |
| 89 | + |
| 90 | +SEC("raw_tp") |
| 91 | +__description("No private stack, nested") |
| 92 | +__success |
| 93 | +__arch_x86_64 |
| 94 | +__jited(" subq $0x8, %rsp") |
| 95 | +__naked void no_private_stack_nested(void) |
| 96 | +{ |
| 97 | + asm volatile ( |
| 98 | + "r1 = 42;" |
| 99 | + "*(u64 *)(r10 - 8) = r1;" |
| 100 | + "call cumulative_stack_depth_subprog;" |
| 101 | + "r0 = 0;" |
| 102 | + "exit;" |
| 103 | + : |
| 104 | + : |
| 105 | + : __clobber_all); |
| 106 | +} |
| 107 | + |
| 108 | +__naked __noinline __used |
| 109 | +static unsigned long loop_callback(void) |
| 110 | +{ |
| 111 | + asm volatile ( |
| 112 | + "call %[bpf_get_prandom_u32];" |
| 113 | + "r1 = 42;" |
| 114 | + "*(u64 *)(r10 - 512) = r1;" |
| 115 | + "call cumulative_stack_depth_subprog;" |
| 116 | + "r0 = 0;" |
| 117 | + "exit;" |
| 118 | + : |
| 119 | + : __imm(bpf_get_prandom_u32) |
| 120 | + : __clobber_common); |
| 121 | +} |
| 122 | + |
| 123 | +SEC("raw_tp") |
| 124 | +__description("Private stack, callback") |
| 125 | +__success |
| 126 | +__arch_x86_64 |
| 127 | +/* for func loop_callback */ |
| 128 | +__jited("func #1") |
| 129 | +__jited(" endbr64") |
| 130 | +__jited(" nopl (%rax,%rax)") |
| 131 | +__jited(" nopl (%rax)") |
| 132 | +__jited(" pushq %rbp") |
| 133 | +__jited(" movq %rsp, %rbp") |
| 134 | +__jited(" endbr64") |
| 135 | +__jited(" movabsq $0x{{.*}}, %r9") |
| 136 | +__jited(" addq %gs:0x{{.*}}, %r9") |
| 137 | +__jited(" pushq %r9") |
| 138 | +__jited(" callq") |
| 139 | +__jited(" popq %r9") |
| 140 | +__jited(" movl $0x2a, %edi") |
| 141 | +__jited(" movq %rdi, -0x200(%r9)") |
| 142 | +__jited(" pushq %r9") |
| 143 | +__jited(" callq") |
| 144 | +__jited(" popq %r9") |
| 145 | +__naked void private_stack_callback(void) |
| 146 | +{ |
| 147 | + asm volatile ( |
| 148 | + "r1 = 1;" |
| 149 | + "r2 = %[loop_callback];" |
| 150 | + "r3 = 0;" |
| 151 | + "r4 = 0;" |
| 152 | + "call %[bpf_loop];" |
| 153 | + "r0 = 0;" |
| 154 | + "exit;" |
| 155 | + : |
| 156 | + : __imm_ptr(loop_callback), |
| 157 | + __imm(bpf_loop) |
| 158 | + : __clobber_common); |
| 159 | +} |
| 160 | + |
| 161 | +SEC("fentry/bpf_fentry_test9") |
| 162 | +__description("Private stack, exception in main prog") |
| 163 | +__success __retval(0) |
| 164 | +__arch_x86_64 |
| 165 | +__jited(" pushq %r9") |
| 166 | +__jited(" callq") |
| 167 | +__jited(" popq %r9") |
| 168 | +int private_stack_exception_main_prog(void) |
| 169 | +{ |
| 170 | + asm volatile ( |
| 171 | + "r1 = 42;" |
| 172 | + "*(u64 *)(r10 - 512) = r1;" |
| 173 | + ::: __clobber_common); |
| 174 | + |
| 175 | + bpf_throw(0); |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +__used static int subprog_exception(void) |
| 180 | +{ |
| 181 | + bpf_throw(0); |
| 182 | + return 0; |
| 183 | +} |
| 184 | + |
| 185 | +SEC("fentry/bpf_fentry_test9") |
| 186 | +__description("Private stack, exception in subprog") |
| 187 | +__success __retval(0) |
| 188 | +__arch_x86_64 |
| 189 | +__jited(" movq %rdi, -0x200(%r9)") |
| 190 | +__jited(" pushq %r9") |
| 191 | +__jited(" callq") |
| 192 | +__jited(" popq %r9") |
| 193 | +int private_stack_exception_sub_prog(void) |
| 194 | +{ |
| 195 | + asm volatile ( |
| 196 | + "r1 = 42;" |
| 197 | + "*(u64 *)(r10 - 512) = r1;" |
| 198 | + "call subprog_exception;" |
| 199 | + ::: __clobber_common); |
| 200 | + |
| 201 | + return 0; |
| 202 | +} |
| 203 | + |
| 204 | +#else |
| 205 | + |
| 206 | +SEC("kprobe") |
| 207 | +__description("private stack is not supported, use a dummy test") |
| 208 | +__success |
| 209 | +int dummy_test(void) |
| 210 | +{ |
| 211 | + return 0; |
| 212 | +} |
| 213 | + |
| 214 | +#endif |
| 215 | + |
| 216 | +char _license[] SEC("license") = "GPL"; |
0 commit comments