bfc077b4cf
This patch generates a prologue for a BPF program which fetches arguments for it. With this patch, the program can have arguments as follow: SEC("lock_page=__lock_page page->flags") int lock_page(struct pt_regs *ctx, int err, unsigned long flags) { return 1; } This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx pointer. r2 is used to indicate if dereferencing was done successfully. This patch uses r6 to hold ctx (struct pt_regs) and r7 to hold stack pointer for result. Result of each arguments first store on stack: low address BPF_REG_FP - 24 ARG3 BPF_REG_FP - 16 ARG2 BPF_REG_FP - 8 ARG1 BPF_REG_FP high address Then loaded into r3, r4 and r5. The output prologue for offn(...off2(off1(reg)))) should be: r6 <- r1 // save ctx into a callee saved register r7 <- fp r7 <- r7 - stack_offset // pointer to result slot /* load r3 with the offset in pt_regs of 'reg' */ (r7) <- r3 // make slot valid r3 <- r3 + off1 // prepare to read unsafe pointer r2 <- 8 r1 <- r7 // result put onto stack call probe_read // read unsafe pointer jnei r0, 0, err // error checking r3 <- (r7) // read result r3 <- r3 + off2 // prepare to read unsafe pointer r2 <- 8 r1 <- r7 call probe_read jnei r0, 0, err ... /* load r2, r3, r4 from stack */ goto success err: r2 <- 1 /* load r3, r4, r5 with 0 */ goto usercode success: r2 <- 0 usercode: r1 <- r6 // restore ctx // original user code If all of arguments reside in register (dereferencing is not required), gen_prologue_fastpath() will be used to create fast prologue: r3 <- (r1 + offset of reg1) r4 <- (r1 + offset of reg2) r5 <- (r1 + offset of reg3) r2 <- 0 P.S. eBPF calling convention is defined as: * r0 - return value from in-kernel function, and exit value for eBPF program * r1 - r5 - arguments from eBPF program to in-kernel function * r6 - r9 - callee saved registers that in-kernel function will preserve * r10 - read-only frame pointer to access stack Committer note: At least testing if it builds and loads: # cat test_probe_arg.c struct pt_regs; __attribute__((section("lock_page=__lock_page page->flags"), used)) int func(struct pt_regs *ctx, int err, unsigned long flags) { return 1; } char _license[] __attribute__((section("license"), used)) = "GPL"; int _version __attribute__((section("version"), used)) = 0x40300; # perf record -e ./test_probe_arg.c usleep 1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.016 MB perf.data ] # perf evlist perf_bpf_probe:lock_page # Signed-off-by: He Kuang <hekuang@huawei.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Cc: Wang Nan <wangnan0@huawei.com> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1447675815-166222-11-git-send-email-wangnan0@huawei.com Signed-off-by: Wang Nan <wangnan0@huawei.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
123 lines
3.5 KiB
C
123 lines
3.5 KiB
C
/*
|
|
* Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
|
|
* Copyright (C) 2015, Huawei Inc.
|
|
*/
|
|
#ifndef __BPF_LOADER_H
|
|
#define __BPF_LOADER_H
|
|
|
|
#include <linux/compiler.h>
|
|
#include <linux/err.h>
|
|
#include <string.h>
|
|
#include <bpf/libbpf.h>
|
|
#include "probe-event.h"
|
|
#include "debug.h"
|
|
|
|
enum bpf_loader_errno {
|
|
__BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
|
|
/* Invalid config string */
|
|
BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
|
|
BPF_LOADER_ERRNO__GROUP, /* Invalid group name */
|
|
BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */
|
|
BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */
|
|
BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */
|
|
BPF_LOADER_ERRNO__CONFIG_TERM, /* Invalid config term in config term */
|
|
BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */
|
|
BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
|
|
BPF_LOADER_ERRNO__PROLOGUEOOB, /* Offset out of bound for prologue */
|
|
__BPF_LOADER_ERRNO__END,
|
|
};
|
|
|
|
struct bpf_object;
|
|
#define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
|
|
|
|
typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
|
|
int fd, void *arg);
|
|
|
|
#ifdef HAVE_LIBBPF_SUPPORT
|
|
struct bpf_object *bpf__prepare_load(const char *filename, bool source);
|
|
int bpf__strerror_prepare_load(const char *filename, bool source,
|
|
int err, char *buf, size_t size);
|
|
|
|
struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
|
|
const char *name);
|
|
|
|
void bpf__clear(void);
|
|
|
|
int bpf__probe(struct bpf_object *obj);
|
|
int bpf__unprobe(struct bpf_object *obj);
|
|
int bpf__strerror_probe(struct bpf_object *obj, int err,
|
|
char *buf, size_t size);
|
|
|
|
int bpf__load(struct bpf_object *obj);
|
|
int bpf__strerror_load(struct bpf_object *obj, int err,
|
|
char *buf, size_t size);
|
|
int bpf__foreach_tev(struct bpf_object *obj,
|
|
bpf_prog_iter_callback_t func, void *arg);
|
|
#else
|
|
static inline struct bpf_object *
|
|
bpf__prepare_load(const char *filename __maybe_unused,
|
|
bool source __maybe_unused)
|
|
{
|
|
pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
|
|
return ERR_PTR(-ENOTSUP);
|
|
}
|
|
|
|
static inline struct bpf_object *
|
|
bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
|
|
size_t obj_buf_sz __maybe_unused)
|
|
{
|
|
return ERR_PTR(-ENOTSUP);
|
|
}
|
|
|
|
static inline void bpf__clear(void) { }
|
|
|
|
static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
|
|
static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
|
|
static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
|
|
|
|
static inline int
|
|
bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
|
|
bpf_prog_iter_callback_t func __maybe_unused,
|
|
void *arg __maybe_unused)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static inline int
|
|
__bpf_strerror(char *buf, size_t size)
|
|
{
|
|
if (!size)
|
|
return 0;
|
|
strncpy(buf,
|
|
"ERROR: eBPF object loading is disabled during compiling.\n",
|
|
size);
|
|
buf[size - 1] = '\0';
|
|
return 0;
|
|
}
|
|
|
|
static inline
|
|
int bpf__strerror_prepare_load(const char *filename __maybe_unused,
|
|
bool source __maybe_unused,
|
|
int err __maybe_unused,
|
|
char *buf, size_t size)
|
|
{
|
|
return __bpf_strerror(buf, size);
|
|
}
|
|
|
|
static inline int
|
|
bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
|
|
int err __maybe_unused,
|
|
char *buf, size_t size)
|
|
{
|
|
return __bpf_strerror(buf, size);
|
|
}
|
|
|
|
static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
|
|
int err __maybe_unused,
|
|
char *buf, size_t size)
|
|
{
|
|
return __bpf_strerror(buf, size);
|
|
}
|
|
#endif
|
|
#endif
|