bpftool: Enable aarch64 ISA extensions for JIT disassembly
PATCH 1/1 bpftool: Enable aarch64 ISA extensions for JIT disassembly
The LLVM disassembler needs ISA extension features enabled to correctly decode instructions from those extensions. On aarch64, without these features, instructions like LSE atomics (e.g. ldaddal) are silently decoded as incorrect instructions and disassembly is truncated.
Use LLVMCreateDisasmCPUFeatures() with "+all" features for aarch64 targets so that the disassembler can handle any instruction the kernel JIT might emit.
Before:
int bench_trigger_uprobe(void * ctx):bpf_prog_538c6a43d1c6b84c_bench_trigger_uprobe:
; int cpu = bpf_get_smp_processor_id();
0: mov x9, x30 4: nop 8: stp x29, x30, [sp, #-16]! c: mov x29, sp 10: stp xzr, x26, [sp, #-16]! 14: mov x26, sp 18: mrs x10, SP_EL0 1c: ldr w7, [x10, #16] ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 20: and w7, w7, #0xff ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 24: lsl x7, x7, #7 28: mov x0, #-281474976710656 2c: movk x0, #32768, lsl #32 30: movk x0, #35407, lsl #16 34: add x0, x0, x7 38: mov x1, #1
; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1);
3c: mov x1, #1
After:
int bench_trigger_uprobe(void * ctx):bpf_prog_538c6a43d1c6b84c_bench_trigger_uprobe:
; int cpu = bpf_get_smp_processor_id();
0: mov x9, x30 4: nop 8: stp x29, x30, [sp, #-16]! c: mov x29, sp 10: stp xzr, x26, [sp, #-16]! 14: mov x26, sp 18: mrs x10, SP_EL0 1c: ldr w7, [x10, #16] ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 20: and w7, w7, #0xff ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 24: lsl x7, x7, #7 28: mov x0, #-281474976710656 2c: movk x0, #32768, lsl #32 30: movk x0, #35407, lsl #16 34: add x0, x0, x7 38: mov x1, #1 ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 3c: ldaddal x1, x1, [x0] ; return 0; 40: mov w7, #0 44: ldp xzr, x26, [sp], #16 48: ldp x29, x30, [sp], #16 4c: mov x0, x7 50: ret 54: nop 58: ldr x10, #8 5c: br x10
--- tools/bpf/bpftool/jit_disasm.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
tools/bpf/bpftool/jit_disasm.c +12 -1
@@ -93,7 +93,18 @@ init_context(disasm_ctx_t *ctx, const char *arch,p_err ("Failed to retrieve triple" );return -1 ; } - *ctx =LLVMCreateDisasm (triple,NULL ,0 ,NULL , symbol_lookup_callback); + +/* +* Enable all aarch64 ISA extensions so the disassembler can handle any +* instruction the kernel JIT might emit (e.g. ARM64 LSE atomics). +*/ +if (strstr (triple,"aarch64" )) + *ctx =LLVMCreateDisasmCPUFeatures (triple,"" ,"+all" , +NULL ,0 ,NULL , + symbol_lookup_callback); +else + *ctx =LLVMCreateDisasm (triple,NULL ,0 ,NULL , + symbol_lookup_callback);LLVMDisposeMessage (triple);if (!*ctx) {
base-commit: 6dd780f973816133f189efec04118c1e6b1b443d
Signature · 1 line
2.47.3
Quentin Monnet
maintainerQuoted 68 lines · Puranjay Mohan
The LLVM disassembler needs ISA extension features enabled to correctly decode instructions from those extensions. On aarch64, without these features, instructions like LSE atomics (e.g. ldaddal) are silently decoded as incorrect instructions and disassembly is truncated.
Use
LLVMCreateDisasmCPUFeatures()with "+all" features for aarch64 targets so that the disassembler can handle any instruction the kernel JIT might emit.Before:
int
bench_trigger_uprobe(void * ctx):bpf_prog_538c6a43d1c6b84c_bench_trigger_uprobe:; int cpu = bpf_get_smp_processor_id(); 0: mov x9, x30 4: nop 8: stp x29, x30, [sp, #-16]! c: mov x29, sp 10: stp xzr, x26, [sp, #-16]! 14: mov x26, sp 18: mrs x10, SP_EL0 1c: ldr w7, [x10, #16] ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 20: and w7, w7, #0xff ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 24: lsl x7, x7, #7 28: mov x0, #-281474976710656 2c: movk x0, #32768, lsl #32 30: movk x0, #35407, lsl #16 34: add x0, x0, x7 38: mov x1, #1;
__sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1);
3c: mov x1, #1After:
int
bench_trigger_uprobe(void * ctx):bpf_prog_538c6a43d1c6b84c_bench_trigger_uprobe:; int cpu = bpf_get_smp_processor_id(); 0: mov x9, x30 4: nop 8: stp x29, x30, [sp, #-16]! c: mov x29, sp 10: stp xzr, x26, [sp, #-16]! 14: mov x26, sp 18: mrs x10, SP_EL0 1c: ldr w7, [x10, #16] ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 20: and w7, w7, #0xff ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 24: lsl x7, x7, #7 28: mov x0, #-281474976710656 2c: movk x0, #32768, lsl #32 30: movk x0, #35407, lsl #16 34: add x0, x0, x7 38: mov x1, #1 ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 3c: ldaddal x1, x1, [x0] ; return 0; 40: mov w7, #0 44: ldp xzr, x26, [sp], #16 48: ldp x29, x30, [sp], #16 4c: mov x0, x7 50: ret 54: nop 58: ldr x10, #8 5c: br x10
Quoted 1 line
Signed-off-by Puranjay Mohan <[email protected]>
Thanks! The change looks OK to me, I just have a few nits below.
Quoted 9 lines
--- tools/bpf/bpftool/jit_disasm.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)tools/bpf/bpftool/jit_disasm.c +0
-0
tools/bpf/bpftool/jit_disasm.c init_context(disasm_ctx_t *ctx, const char *arch, · line 101
Quoted 10 lines
tools/bpf/bpftool/jit_disasm.c +6
-1@@ -93,7 +93,18 @@ init_context(disasm_ctx_t *ctx, const char *arch,p_err ("Failed to retrieve triple" );return -1 ; } - *ctx =LLVMCreateDisasm (triple,NULL ,0 ,NULL , symbol_lookup_callback); + +/* +* Enable all aarch64 ISA extensions so the disassembler can handle any +* instruction the kernel JIT might emit (e.g. ARM64 LSE atomics). +*/ +if (strstr (triple,"aarch64" ))
I don't think the arch name can appear in the middle of the triple, can it? So maybe just strncmp(triple, "aarch64", 7)?
tools/bpf/bpftool/jit_disasm.c init_context(disasm_ctx_t *ctx, const char *arch, · line 107
Quoted 6 lines
tools/bpf/bpftool/jit_disasm.c +6
-0+ *ctx =LLVMCreateDisasmCPUFeatures (triple,"" ,"+all" , +NULL ,0 ,NULL , + symbol_lookup_callback); + else + *ctx =LLVMCreateDisasm (triple,NULL ,0 ,NULL , + symbol_lookup_callback);
Formatting nit: The line length limit is 100 char so you could have these LLVMCreateDisasm...() calls on 2 and 1 lines (rather than 3 and 2 lines), respectively.
Thanks,
Quentin
Quoted 68 lines · Puranjay Mohan
The LLVM disassembler needs ISA extension features enabled to correctly decode instructions from those extensions. On aarch64, without these features, instructions like LSE atomics (e.g. ldaddal) are silently decoded as incorrect instructions and disassembly is truncated.
Use
LLVMCreateDisasmCPUFeatures()with "+all" features for aarch64 targets so that the disassembler can handle any instruction the kernel JIT might emit.Before:
int
bench_trigger_uprobe(void * ctx):bpf_prog_538c6a43d1c6b84c_bench_trigger_uprobe:; int cpu = bpf_get_smp_processor_id(); 0: mov x9, x30 4: nop 8: stp x29, x30, [sp, #-16]! c: mov x29, sp 10: stp xzr, x26, [sp, #-16]! 14: mov x26, sp 18: mrs x10, SP_EL0 1c: ldr w7, [x10, #16] ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 20: and w7, w7, #0xff ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 24: lsl x7, x7, #7 28: mov x0, #-281474976710656 2c: movk x0, #32768, lsl #32 30: movk x0, #35407, lsl #16 34: add x0, x0, x7 38: mov x1, #1 ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 3c: mov x1, #1After:
int
bench_trigger_uprobe(void * ctx):bpf_prog_538c6a43d1c6b84c_bench_trigger_uprobe:; int cpu = bpf_get_smp_processor_id(); 0: mov x9, x30 4: nop 8: stp x29, x30, [sp, #-16]! c: mov x29, sp 10: stp xzr, x26, [sp, #-16]! 14: mov x26, sp 18: mrs x10, SP_EL0 1c: ldr w7, [x10, #16] ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 20: and w7, w7, #0xff ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 24: lsl x7, x7, #7 28: mov x0, #-281474976710656 2c: movk x0, #32768, lsl #32 30: movk x0, #35407, lsl #16 34: add x0, x0, x7 38: mov x1, #1 ; __sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1); 3c: ldaddal x1, x1, [x0] ; return 0; 40: mov w7, #0 44: ldp xzr, x26, [sp], #16 48: ldp x29, x30, [sp], #16 4c: mov x0, x7 50: ret 54: nop 58: ldr x10, #8 5c: br x10
Quoted 1 line
Signed-off-by Puranjay Mohan <[email protected]>
Quoted 9 lines
--- tools/bpf/bpftool/jit_disasm.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)tools/bpf/bpftool/jit_disasm.c +0
-0
tools/bpf/bpftool/jit_disasm.c init_context(disasm_ctx_t *ctx, const char *arch, · line 101
Quoted 10 lines
tools/bpf/bpftool/jit_disasm.c +6
-1@@ -93,7 +93,18 @@ init_context(disasm_ctx_t *ctx, const char *arch,p_err ("Failed to retrieve triple" );return -1 ; } - *ctx =LLVMCreateDisasm (triple,NULL ,0 ,NULL , symbol_lookup_callback); + +/* +* Enable all aarch64 ISA extensions so the disassembler can handle any +* instruction the kernel JIT might emit (e.g. ARM64 LSE atomics). +*/ +if (strstr (triple,"aarch64" ))
As mentioned by Quentin, "aarch64" should be in the leading triple, so it would be great to check with that instead of strstr.
There are another variant "arm64" which is used for Apple platformas, so we can ignore them.
Quoted 11 lines
tools/bpf/bpftool/jit_disasm.c +6
-0+ *ctx =LLVMCreateDisasmCPUFeatures (triple,"" ,"+all" , +NULL ,0 ,NULL , + symbol_lookup_callback); + else + *ctx =LLVMCreateDisasm (triple,NULL ,0 ,NULL , + symbol_lookup_callback);LLVMDisposeMessage (triple);if (!*ctx) {base-commit:
6dd780f973816133f189efec04118c1e6b1b443d