@@ -22,3 +22,8 @@ bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path)
}
void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit) {}
+
+int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path)
+{
+ return 0;
+}
@@ -99,3 +99,103 @@ void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit)
{
u_ebpf->jit = jit;
}
+
+static uint64_t gather_bytes(uint8_t a, uint8_t b, uint8_t c,
+ uint8_t d, uint8_t e)
+{
+ return ((uint64_t)a << 32) |
+ ((uint32_t)b << 24) |
+ ((uint32_t)c << 16) |
+ ((uint16_t)d << 8) |
+ e;
+}
+
+static void trash_registers(void)
+{
+ /* Overwrite all caller-save registers */
+ asm(
+ "mov $0xf0, %rax;"
+ "mov $0xf1, %rcx;"
+ "mov $0xf2, %rdx;"
+ "mov $0xf3, %rsi;"
+ "mov $0xf4, %rdi;"
+ "mov $0xf5, %r8;"
+ "mov $0xf6, %r9;"
+ "mov $0xf7, %r10;"
+ "mov $0xf8, %r11;"
+ );
+}
+
+static uint32_t sqrti(uint32_t x)
+{
+ return sqrt(x);
+}
+
+static uint64_t unwind(uint64_t i)
+{
+ return i;
+}
+
+static void register_functions(struct ubpf_vm *vm)
+{
+ ubpf_register(vm, 0, "gather_bytes", gather_bytes);
+ ubpf_register(vm, 1, "memfrob", memfrob);
+ ubpf_register(vm, 2, "trash_registers", trash_registers);
+ ubpf_register(vm, 3, "sqrti", sqrti);
+ ubpf_register(vm, 4, "strcmp_ext", strcmp);
+ ubpf_register(vm, 5, "unwind", unwind);
+ ubpf_set_unwind_function_index(vm, 5);
+}
+
+int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path)
+{
+ bool is_elf;
+ char *errmsg;
+ int ret;
+
+ if (!qemu_ubpf_read_code(u_ebpf, code_path)) {
+ error_report("Ubpf failed to read code");
+ return -1;
+ }
+
+ u_ebpf->vm = ubpf_create();
+ if (!u_ebpf->vm) {
+ error_report("Failed to create ubpf VM");
+ return -1;
+ }
+
+ register_functions(u_ebpf->vm);
+
+ /*
+ * The ELF magic corresponds to an RSH instruction with an offset,
+ * which is invalid.
+ */
+ is_elf = u_ebpf->code_len >= SELFMAG && !memcmp(u_ebpf->code,
+ ELFMAG, SELFMAG);
+
+ if (is_elf) {
+ ret = ubpf_load_elf(u_ebpf->vm, u_ebpf->code,
+ u_ebpf->code_len, &errmsg);
+ } else {
+ ret = ubpf_load(u_ebpf->vm, u_ebpf->code,
+ u_ebpf->code_len, &errmsg);
+ }
+
+ if (ret < 0) {
+ error_report("Failed to load ubpf code: %s ", errmsg);
+ free(errmsg);
+ ubpf_destroy(u_ebpf->vm);
+ return -1;
+ }
+
+ if (u_ebpf->jit) {
+ u_ebpf->fn = ubpf_compile(u_ebpf->vm, &errmsg);
+ if (u_ebpf->fn == NULL) {
+ error_report("Failed to ubpf compile: %s", errmsg);
+ free(errmsg);
+ return -1;
+ }
+ }
+
+ return 0;
+}
@@ -37,5 +37,6 @@ typedef struct UbpfState {
bool qemu_ubpf_read_code(UbpfState *u_ebpf, char *path);
bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path);
void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit);
+int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path);
#endif /* QEMU_UBPF_H */
The qemu_prepare_ubpf() can load user defined userspace ebpf binary file to Qemu userspace ebpf VM but not run it. The ebpf program will triggered in the hook point. Signed-off-by: Zhang Chen <chen.zhang@intel.com> --- ebpf/ubpf-stub.c | 5 +++ ebpf/ubpf.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++ ebpf/ubpf.h | 1 + 3 files changed, 106 insertions(+)