backdoor: instruction-based guest-to-QEMU backdoor communication channel
cover backdoor: instruction-based guest-to-QEMU backdoor communication channel
Provides the ability for the guest to communicate with user-provided code inside QEMU itself.
This backdoor communication channel is based on using instructions, as opposed to methods based on MMIO/PIO or virtproxy/virtagent.
Then, these instructions are redirected to a set of user-provided routines, which are expected to be in the static library "libbackdoor.a", identified by the "--with-backdoor" configuration parameter.
The advantages of this method are:
* Minimal setup for the guest. Can be used with a simple library using LD_PRELOAD, or simply adding calls to the macros in "backdoor/guest.h".
* Low-overhead. Using instructions does not go through any OS abstraction, not even for the setup of the communication channel.
* Non-intrusive wrt OS. It can be used on any OS without prior modifications.
When possible, this will overload unused/meaningless instructions in order to let the modified guest application to run natively without errors (e.g., using the CPUID instruction in x86 systems).
---
Lluís Vilanova (6):
[arm m68k] move helpers.h to helper.h
backdoor: handle config-time activation
backdoor: declare host-side backdoor helpers
backdoor: declare guest-side interface macros
backdoor: [i386] provide and implement intruction-based backdoor interface
backdoor: add a simple example
.gitignore | 1 Makefile.target | 40 +++ backdoor/examples/print/README | 13 + backdoor/examples/print/guest/Makefile | 7 backdoor/examples/print/guest/test.c | 33 ++ backdoor/examples/print/host/Makefile | 13 + backdoor/examples/print/host/printcb.c | 36 +++ backdoor/guest.h | 54 ++++ backdoor/helper.h | 21 + configure | 21 + target-arm/helper.c | 2 target-arm/helper.h | 450 ++++++++++++++++++++++++++++++++ target-arm/helpers.h | 450 -------------------------------- target-arm/iwmmxt_helper.c | 2 target-arm/neon_helper.c | 2 target-arm/op_helper.c | 2 target-arm/translate.c | 6 target-i386/cpuid.c | 27 ++ target-i386/helper.h | 4 target-i386/translate.c | 4 target-m68k/helper.c | 2 target-m68k/helper.h | 54 ++++ target-m68k/helpers.h | 54 ---- target-m68k/op_helper.c | 2 target-m68k/translate.c | 6 25 files changed, 786 insertions(+), 520 deletions(-) create mode 100644 backdoor/examples/print/README create mode 100644 backdoor/examples/print/guest/Makefile create mode 100644 backdoor/examples/print/guest/test.c create mode 100644 backdoor/examples/print/host/Makefile create mode 100644 backdoor/examples/print/host/printcb.c create mode 100644 backdoor/guest.h create mode 100644 backdoor/helper.h create mode 100644 target-arm/helper.h delete mode 100644 target-arm/helpers.h create mode 100644 target-m68k/helper.h delete mode 100644 target-m68k/helpers.h
PATCH 1/6 move helpers.h to helper.h
This provides a consistent naming scheme across all targets.
--- target-arm/helper.c | 2 target-arm/helper.h | 450 ++++++++++++++++++++++++++++++++++++++++++++ target-arm/helpers.h | 450 -------------------------------------------- target-arm/iwmmxt_helper.c | 2 target-arm/neon_helper.c | 2 target-arm/op_helper.c | 2 target-arm/translate.c | 6 - target-m68k/helper.c | 2 target-m68k/helper.h | 54 +++++ target-m68k/helpers.h | 54 ----- target-m68k/op_helper.c | 2 target-m68k/translate.c | 6 - 12 files changed, 516 insertions(+), 516 deletions(-) create mode 100644 target-arm/helper.h delete mode 100644 target-arm/helpers.h create mode 100644 target-m68k/helper.h delete mode 100644 target-m68k/helpers.h
target-arm/helper.c +1 -1
@@ -5,7 +5,7 @@#include "cpu.h" #include "exec-all.h" #include "gdbstub.h" -#include "helpers.h" +#include "helper.h" #include "qemu-common.h" #include "host-utils.h" #if !defined(CONFIG_USER_ONLY )
target-arm/helper.h new file +450 -0
450 lines not on this page · raw message
target-arm/helpers.h deleted +0 -450
450 lines not on this page · raw message
target-arm/iwmmxt_helper.c +1 -1
@@ -24,7 +24,7 @@#include "cpu.h" #include "exec-all.h" -#include "helpers.h" +#include "helper.h" /* iwMMXt macros extracted from GNU gdb. */
target-arm/neon_helper.c +1 -1
@@ -11,7 +11,7 @@#include "cpu.h" #include "exec-all.h" -#include "helpers.h" +#include "helper.h" #define SIGNBIT (uint32_t)0x80000000#define SIGNBIT64 ((uint64_t)1 << 63)
target-arm/op_helper.c +1 -1
@@ -17,7 +17,7 @@* License along with this library; if not, see <http://www.gnu.org/licenses/>. */ #include "exec.h" -#include "helpers.h" +#include "helper.h" #define SIGNBIT (uint32_t)0x80000000#define SIGNBIT64 ((uint64_t)1 << 63)
target-arm/translate.c +3 -3
@@ -30,9 +30,9 @@#include "tcg-op.h" #include "qemu-log.h" -#include "helpers.h" +#include "helper.h" #define GEN_HELPER 1 -#include "helpers.h" +#include "helper.h" #define ENABLE_ARCH_5J 0#define ENABLE_ARCH_6 arm_feature(env, ARM_FEATURE_V6)
@@ -120,7 +120,7 @@ void arm_translate_init(void)#endif #define GEN_HELPER 2 -#include "helpers.h" +#include "helper.h" }static int num_temps;
target-m68k/helper.c +1 -1
@@ -27,7 +27,7 @@#include "qemu-common.h" #include "gdbstub.h" -#include "helpers.h" +#include "helper.h" #define SIGNBIT (1u << 31)
target-m68k/helper.h new file +54 -0
54 lines not on this page · raw message
target-m68k/helpers.h deleted +0 -54
54 lines not on this page · raw message
target-m68k/op_helper.c +1 -1
@@ -17,7 +17,7 @@* License along with this library; if not, see <http://www.gnu.org/licenses/>. */ #include "exec.h" -#include "helpers.h" +#include "helper.h" #if defined(CONFIG_USER_ONLY )
target-m68k/translate.c +3 -3
@@ -30,9 +30,9 @@#include "tcg-op.h" #include "qemu-log.h" -#include "helpers.h" +#include "helper.h" #define GEN_HELPER 1 -#include "helpers.h" +#include "helper.h" //#define DEBUG_DISPATCH 1
@@ -111,7 +111,7 @@ void m68k_tcg_init(void) store_dummy =tcg_global_mem_new (TCG_AREG0 ,-8 ,"NULL" );#define GEN_HELPER 2 -#include "helpers.h" +#include "helper.h" }static inline void qemu_assert (int cond,const char *msg)
PATCH 2/6 backdoor: handle config-time activation
In reply to LluísAdd a '--with-backdoor' configuration option pointing to user-provided backdoor callback implementation.
Make is invoked on the user-provided directory, which must build a static library containing, at least, the implementation of the backdoor helpers.
--- Makefile.target | 40 ++++++++++++++++++++++++++++++++++++---- configure | 21 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-)
Makefile.target +36 -4
@@ -29,7 +29,7 @@ QEMU_PROG=qemu-system-$(TARGET_ARCH2)$(EXESUF) endif endif -PROGS=$(QEMU_PROG) +PROGS=$(QEMU_PROG)-prepare $(QEMU_PROG) ifndef CONFIG_HAIKU LIBS+=-lm
@@ -323,8 +323,40 @@ endif # CONFIG_SOFTMMU obj-y += $(addprefix ../, $(trace-obj-y)) obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o -$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) - $(call LINK,$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y)) + +.PHONY: force +force: + +######################################################### +# backdoor communication channel +ifdef CONFIG_BACKDOOR +VPATH := $(VPATH):$(BACKDOOR_PATH) + +LIBBACKDOOR_LIB = libbackdoor/libbackdoor.a +LIBBACKDOOR_CLEAN = libbackdoor-clean + +libbackdoor/Makefile: + $(call quiet-command, mkdir -p libbackdoor, " CREAT $(TARGET_DIR)$@") + $(call quiet-command, rm -f libbackdoor/Makefile) + $(call quiet-command, ln -s $(BACKDOOR_PATH)/Makefile libbackdoor/Makefile) + +libbackdoor/libbackdoor.a: libbackdoor/Makefile force + $(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libbackdoor \ + QEMU_CFLAGS="$(QEMU_CFLAGS) -I../target-$(TARGET_BASE_ARCH)" \ + TARGET_DIR=$(TARGET_DIR)libbackdoor/ VPATH=$(VPATH) \ + SRC_PATH=$(SRC_PATH) V="$(V)" libbackdoor.a) + +libbackdoor-clean: + $(MAKE) $(SUBDIR_MAKEFLAGS) -C $(LIBBACKDOOR_DIR) \ + VPATH=$(VPATH) SRC_PATH=$(SRC_PATH) V="$(V)" clean || true +endif + + + +$(QEMU_PROG)-prepare: $(GENERATED_HEADERS) $(LIBBACKDOOR_LIB) $(QEMU_PROG) + +$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y) $(LIBBACKDOOR_LIB) + $(call LINK,$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y)) $(LIBBACKDOOR_LIB) gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/feature_to_c.sh
@@ -336,7 +368,7 @@ hmp-commands.h: $(SRC_PATH)/hmp-commands.hx qmp-commands.h: $(SRC_PATH)/qmp-commands.hx $(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@") -clean: +clean: $(LIBBACKDOOR_CLEAN) rm -f *.o *.a *~ $(PROGS) nwfpe/*.o fpu/*.o rm -f *.d */*.d tcg/*.o ide/*.o rm -f hmp-commands.h qmp-commands.h gdbstub-xml.c
configure +21 -0
@@ -331,6 +331,7 @@ zero_malloc="" trace_backend="nop" trace_file="trace" spice="" +backdoor="" # OS specific if check_define __linux__ ; then
@@ -740,6 +741,17 @@ for opt do ;; --*dir) ;; + --with-backdoor=*) backdoor="$optarg" + if test ! -f "$backdoor/Makefile"; then + echo + echo "Error: cannot make into '$backdoor'" + echo "Please choose a directory where I can run 'make'" + echo + exit 1 + else + backdoor=`readlink -f $backdoor` + fi + ;; *) echo "ERROR: unknown option $opt"; show_help="yes" ;; esac
@@ -2320,6 +2332,9 @@ echo "vhost-net support $vhost_net" echo "Trace backend $trace_backend" echo "Trace output file $trace_file-<pid>" echo "spice support $spice" +if test -n "$backdoor"; then + echo "Backdoor comm. $backdoor" +fi if test $sdl_too_old = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -2579,6 +2594,12 @@ if test "$spice" = "yes" ; then echo "CONFIG_SPICE=y" >> $config_host_mak fi +if test -n "$backdoor"; then + echo "CONFIG_BACKDOOR=y" >> $config_host_mak + echo "BACKDOOR_PATH=$backdoor" >> $config_host_mak + rm -rf *-{bsd-usr,darwin-user,linux-user,softmmu}/libbackdoor/ +fi + # XXX: suppress that if [ "$bsd" = "yes" ] ; then echo "CONFIG_BSD=y" >> $config_host_mak
PATCH 3/6 backdoor: declare host-side backdoor helpers
In reply to LluísThese helpers must be implemented by the user on "libbackdoor.a".
--- backdoor/helper.h | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-) create mode 100644 backdoor/helper.h
backdoor/helper.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* Helpers for instruction-based backdoor communication. +* +* Copyright (c) 2010 Lluís Vilanova <[email protected]> +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +DEF_HELPER_1 (backdoor_i8, void, i32); +DEF_HELPER_2 (backdoor_i8_v32, void, i32, i32);
PATCH 4/6 backdoor: declare guest-side interface macros
In reply to LluísHeader for the user to include when compiling guest applications that want to communicate with QEMU through backdoor instructions.
--- backdoor/guest.h | 33 +++++++++++++++++++++++++++++++++ 1 files changed, 33 insertions(+), 0 deletions(-) create mode 100644 backdoor/guest.h
backdoor/guest.h new file +33 -0
@@ -0,0 +1,33 @@ +/* +* Guest-side interface for instruction-based backdoor communication. +* +* Copyright (c) 2010 Lluís Vilanova <[email protected]> +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef BACKDOOR__GUEST_H +#define BACKDOOR__GUEST_H + +/* Backdoor macro names define the types of its arguments: +* +* - i8: immediate of 8 bits +* - v32: value of 32 bits +*/ + + +#error Undefined instruction-based backdoor interface for guest architecture + + +#endif /* BACKDOOR__GUEST_H */
PATCH 5/6 backdoor: [i386] provide and implement intruction-based backdoor interface
In reply to LluísTake the unused CPUID 0x40001xxx range as the backdoor instruction.
--- backdoor/guest.h | 21 +++++++++++++++++++++ target-i386/cpuid.c | 27 +++++++++++++++++++++++++++ target-i386/helper.h | 4 ++++ target-i386/translate.c | 4 ++++ 4 files changed, 56 insertions(+), 0 deletions(-)
backdoor/guest.h +21 -0
@@ -26,8 +26,29 @@* - v32: value of 32 bits */ +#include <stdint.h> + +#if __i386__ || __i486__ || __x86_64__ + +#define _BACKDOOR (t, i8, v32) \ + ({ \ + uint32_t eax, ebx, ecx, edx; \ + uint32_t index = (uint32_t)0x40001000 + (t<<8) + (uint8_t)i8; \ + uint32_t count = (uint32_t)v32; \ + asm volatile ("cpuid" \ + : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) \ + : "0"(index), "2"(count) \ + ); \ + eax; \ + }) + +#define BACKDOOR_i8 (i) _BACKDOOR(0, i, 0) +#define BACKDOOR_i8_v32 (i, v) _BACKDOOR(1, i, v) + +#else #error Undefined instruction-based backdoor interface for guest architecture +#endif #endif /* BACKDOOR__GUEST_H */
target-i386/cpuid.c +27 -0
@@ -27,6 +27,9 @@#include "qemu-option.h" #include "qemu-config.h" +#include "helper.h" + +/* feature flags taken from "Intel Processor Identification and the CPUID * Instruction" and AMD's "CPUID Specification". In cases of disagreement * between feature naming conventions, aliases may be added.
@@ -991,6 +994,30 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) { +#if defined(CONFIG_BACKDOOR) /* synched with "backdoor/guest.h" */ + if ((index & ~0xfff) == 0x40001000) { + int type = (index >> 8) & 0xf; +uint8_t i8 = index &0x0ff ; +uint32_t v32 = count; +switch (type) { +case 0 : +helper_backdoor_i8 (i8); +break ; +case 1 : +helper_backdoor_i8_v32 (i8, v32); +break ; +default : +printf ("invalid backdoor request\n" ); +abort (); + } + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; +return ; + } +#endif +/* test if maximum index reached */ if (index &0x80000000 ) {if (index > env->cpuid_xlevel)
target-i386/helper.h +4 -0
@@ -217,4 +217,8 @@ DEF_HELPER_2(rclq, tl, tl, tl) DEF_HELPER_2(rcrq, tl, tl, tl)#endif +#if defined(CONFIG_BACKDOOR ) +#include "backdoor/helper.h" +#endif +#include "def-helper.h"
target-i386/translate.c +4 -0
@@ -6941,6 +6941,10 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)gen_op_set_cc_op (s->cc_op);gen_jmp_im (pc_start - s->cs_base);gen_helper_cpuid (); +#if defined(CONFIG_BACKDOOR ) +gen_jmp_im (s->pc); +gen_eob (s); +#endif break ;case 0xf4 :/* hlt */ if (s->cpl !=0 ) {
PATCH 6/6 backdoor: add a simple example
In reply to LluísProvides a guest application that exercices the instruction-based backdoor communication, as well as a backdoor callback implementation that prints the guest requests.
--- .gitignore | 1 + backdoor/examples/print/README | 13 ++++++++++++ backdoor/examples/print/guest/Makefile | 7 ++++++ backdoor/examples/print/guest/test.c | 33 +++++++++++++++++++++++++++++ backdoor/examples/print/host/Makefile | 13 ++++++++++++ backdoor/examples/print/host/printcb.c | 36 ++++++++++++++++++++++++++++++++ 6 files changed, 103 insertions(+), 0 deletions(-) create mode 100644 backdoor/examples/print/README create mode 100644 backdoor/examples/print/guest/Makefile create mode 100644 backdoor/examples/print/guest/test.c create mode 100644 backdoor/examples/print/host/Makefile create mode 100644 backdoor/examples/print/host/printcb.c
.gitignore +1 -0
@@ -58,3 +58,4 @@ pc-bios/optionrom/multiboot.bin pc-bios/optionrom/multiboot.raw .stgit-* cscope.* +backdoor/examples/print/guest/test
backdoor/examples/print/README new file +13 -0
@@ -0,0 +1,13 @@ +This example simply defines instruction-based backdoors to print their +arguments, along with a guest example code that makes use of backdoor +instructions. + +To compile the host (quemu) run: + /path/to/qemu/configure --with-backdoor=/path/to/qemu/backdoor/examples/print/host/ + make + +To compile the guest program run: + make -C /path/to/qemu/backdoor/examples/print/guest/ + +Now you can run it with: + /path/to/qemu/i386-linux-user/qemu-i386 /path/to/qemu/backdoor/examples/print/guest/test
backdoor/examples/print/guest/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +CFLAGS += -I../../../../ +PROGS = test + +all: $(PROGS) + +clean: + rm -f $(PROGS)
backdoor/examples/print/guest/test.c new file +33 -0
@@ -0,0 +1,33 @@ +/* +* Sample guest program exercising instruction-based backdoor communication. +* +* Copyright (c) 2010 Lluís Vilanova <[email protected]> +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#include <stdio.h> + +#include "backdoor/guest.h" + +int main () +{ +int i =23 ; +printf ("i8 1\n" ); +BACKDOOR_i8 (0x01 ); +printf ("i8_v32 1 32\n" ); +BACKDOOR_i8_v32 (0x01 ,32 ); +printf ("i8_v32 2 i (%d)\n" , i); +BACKDOOR_i8_v32 (0x02 , i); +}
backdoor/examples/print/host/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# Makefile for user-provided backdoor code + +include $(SRC_PATH)/config-host.mak +include $(SRC_PATH)/rules.mak +include $(SRC_PATH)/Makefile.objs + +objs = printcb.o + +libbackdoor.a: $(objs) + $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@") + +clean: + rm -f libbackdoor.a $(objs)
backdoor/examples/print/host/printcb.c new file +36 -0
@@ -0,0 +1,36 @@ +/* +* Sample user-defined callbacks for backdoor communication. +* +* Copyright (c) 2010 Lluís Vilanova <[email protected]> +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +#include <stdio.h> + +#include "cpu.h" +#include "helper.h" + + +void +helper_backdoor_i8 (uint32_t imm) +{ +printf ("backdoor_i8: %u\n" , imm); +} + +void +helper_backdoor_i8_v32 (uint32_t imm,uint32_t value) +{ +printf ("backdoor_i8_v32: %u %d\n" , imm, value); +}