/* * Minimal BPF JIT image disassembler * * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for * debugging or verification purposes. * * There is no Makefile. Compile with * * `gcc -Wall -O2 bpf_jit_disasm.c -o bpf_jit_disasm -lopcodes -lbfd -ldl` * * or similar. * * To get the disassembly of the JIT code, do the following: * * 1) `echo 2 > /proc/sys/net/core/bpf_jit_enable` * 2) Load a BPF filter (e.g. `tcpdump -p -n -s 0 -i eth1 host 192.168.20.0/24`) * 3) Run e.g. `bpf_jit_disasm -o` to read out the last JIT code * * Copyright 2013 Daniel Borkmann * Licensed under the GNU General Public License, version 2.0 (GPLv2) * * Note: this is part of the Linux kernel and can be found under tools/net/. */ #include #include #include #include #include #include #include #include #include #include #include static void get_exec_path(char *tpath, size_t size) { char *path; ssize_t len; snprintf(tpath, size, "/proc/%d/exe", (int) getpid()); tpath[size - 1] = 0; path = strdup(tpath); assert(path); len = readlink(path, tpath, size); tpath[len] = 0; free(path); } static void get_asm_insns(uint8_t *image, size_t len, unsigned long base, int opcodes) { int count, i, pc = 0; char tpath[256]; struct disassemble_info info; disassembler_ftype disassemble; bfd *bfdf; memset(tpath, 0, sizeof(tpath)); get_exec_path(tpath, sizeof(tpath)); bfdf = bfd_openr(tpath, NULL); assert(bfdf); assert(bfd_check_format(bfdf, bfd_object)); init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf); info.arch = bfd_get_arch(bfdf); info.mach = bfd_get_mach(bfdf); info.buffer = image; info.buffer_length = len; disassemble_init_for_target(&info); disassemble = disassembler(bfdf); assert(disassemble); do { printf("%4x:\t", pc); count = disassemble(pc, &info); if (opcodes) { printf("\n\t"); for (i = 0; i < count; ++i) printf("%02x ", (uint8_t) image[pc + i]); } printf("\n"); pc += count; } while(count > 0 && pc < len); bfd_close(bfdf); } static char *get_klog_buff(int *klen) { int ret, len = klogctl(10, NULL, 0); char *buff = malloc(len); assert(buff && klen); ret = klogctl(3, buff, len); assert(ret >= 0); *klen = ret; return buff; } static void put_klog_buff(char *buff) { free(buff); } static int get_last_jit_image(char *haystack, size_t hlen, uint8_t *image, size_t ilen, unsigned long *base) { char *ptr, *pptr, *tmp; off_t off = 0; int ret, flen, proglen, pass, ulen = 0; regmatch_t pmatch[1]; regex_t regex; if (hlen == 0) return 0; ret = regcomp(®ex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ " "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED); assert(ret == 0); ptr = haystack; while (1) { ret = regexec(®ex, ptr, 1, pmatch, 0); if (ret == 0) { ptr += pmatch[0].rm_eo; off += pmatch[0].rm_eo; assert(off < hlen); } else break; } ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so); ret = sscanf(ptr, "flen=%d proglen=%d pass=%d image=%lx", &flen, &proglen, &pass, base); if (ret != 4) return 0; tmp = ptr = haystack + off; while ((ptr = strtok(tmp, "\n")) != NULL && ulen < ilen) { tmp = NULL; if (!strstr(ptr, "JIT code")) continue; pptr = ptr; while ((ptr = strstr(pptr, ":"))) pptr = ptr + 1; ptr = pptr; do { image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16); if (ptr == pptr || ulen >= ilen) { ulen--; break; } ptr = pptr; } while (1); } assert(ulen == proglen); printf("%d bytes emitted from JIT compiler (pass:%d, flen:%d)\n", proglen, pass, flen); printf("%lx + :\n", *base); regfree(®ex); return ulen; } int main(int argc, char **argv) { int len, klen, opcodes = 0; char *kbuff; unsigned long base; uint8_t image[4096]; if (argc > 1) { if (!strncmp("-o", argv[argc - 1], 2)) { opcodes = 1; } else { printf("usage: bpf_jit_disasm [-o: show opcodes]\n"); exit(0); } } bfd_init(); memset(image, 0, sizeof(image)); kbuff = get_klog_buff(&klen); len = get_last_jit_image(kbuff, klen, image, sizeof(image), &base); if (len > 0 && base > 0) get_asm_insns(image, len, base, opcodes); put_klog_buff(kbuff); return 0; }
authorValentin Rothberg <valentinrothberg@gmail.com>2016-10-05 07:57:26 +0200
committerMichael Ellerman <mpe@ellerman.id.au>2016-10-27 21:52:59 +1100
commit39715bf972ed4fee18fe5409609a971fb16b1771 (patch)
tree1d423c94bc8d3199a42f16b6296c9d96e929982b /Documentation
parent09b7e37b18eecc1e347f4b1a3bc863f32801f634 (diff)
powerpc/process: Fix CONFIG_ALIVEC typo in restore_tm_state()
It should be ALTIVEC, not ALIVEC. Cyril explains: If a thread performs a transaction with altivec and then gets preempted for whatever reason, this bug may cause the kernel to not re-enable altivec when that thread runs again. This will result in an altivec unavailable fault, when that fault happens inside a user transaction the kernel has no choice but to enable altivec and doom the transaction. The result is that transactions using altivec may get aborted more often than they should. The difficulty in catching this with a selftest is my deliberate use of the word may above. Optimisations to avoid FPU/altivec/VSX faults mean that the kernel will always leave them on for 255 switches. This code prevents the kernel turning it off if it got to the 256th switch (and userspace was transactional). Fixes: dc16b553c949 ("powerpc: Always restore FPU/VEC/VSX if hardware transactional memory in use") Reviewed-by: Cyril Bur <cyrilbur@gmail.com> Signed-off-by: Valentin Rothberg <valentinrothberg@gmail.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Diffstat (limited to 'Documentation')