Received: from eggs.gnu.org ([140.186.70.92]:58661) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R9Gxg-0003GQ-Iy for qemu-devel@nongnu.org; Thu, 29 Sep 2011 09:47:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R9Gxa-0004AV-Le for qemu-devel@nongnu.org; Thu, 29 Sep 2011 09:47:40 -0400 Received: from gw.ac.upc.edu ([147.83.30.3]:50992) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R9GxZ-0004AK-VS for qemu-devel@nongnu.org; Thu, 29 Sep 2011 09:47:34 -0400 From: =?utf-8?b?TGx1w61z?= Vilanova Date: Thu, 29 Sep 2011 15:47:33 +0200 Message-ID: <20110929134733.19559.46294.stgit@ginnungagap.bsc.es> In-Reply-To: <20110929134727.19559.54734.stgit@ginnungagap.bsc.es> References: <20110929134727.19559.54734.stgit@ginnungagap.bsc.es> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 1/5] backdoor: Add documentation List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Zhi Yong Wu Signed-off-by: Llu=C3=ADs Vilanova --- docs/backdoor.txt | 144 +++++++++++++++++++++++++++++++++++++++++++++++= ++++++ 1 files changed, 144 insertions(+), 0 deletions(-) create mode 100644 docs/backdoor.txt diff --git a/docs/backdoor.txt b/docs/backdoor.txt new file mode 100644 index 0000000..3b26b70 --- /dev/null +++ b/docs/backdoor.txt @@ -0,0 +1,144 @@ +=3D Backdoor communication channel =3D + +=3D=3D Introduction =3D=3D + +This document describes how the guest can use the backdoor communication= channel +to interact with user-provided code inside QEMU. + +The backdoor provides a lightweight and guest-initiated communication ch= annel +between code running inside the guest system and code in QEMU, including= both +QEMU in 'softmmu' and 'user' modes. + +The semantics of the backdoor channel are up to the user, who must provi= de the +implementation of the QEMU-side callbacks used when the backdoor channel= is +invoked. + +On the guest side, code can simply link against a simple library provide= d in +QEMU to interface with the backdoor channel. + +The features of this mechanism are: + +* Minimal setup for the guest. +* Independent of guest architecture. +* Works with 'softmmu' and 'user' mode. +* Low overhead; capturing memory accesses to specific addresses does not= go + through any OS abstraction, except during the setup of the communicati= on + channel. + + +=3D=3D QEMU-side code =3D=3D + +1. Create the "Makefile" to build the user-provided backdoor channel lib= rary: + + mkdir /tmp/my-backdoor-qemu + cat > /tmp/my-backdoor-qemu/Makefile < /tmp/my-backdoor-qemu/backdoor.c < + =20 + =20 + void qemu_backdoor_init(uint64_t data_size) + { + printf("+ %ld\n", data_size); + } + =20 + void qemu_backdoor(uint64_t cmd, void *data) + { + /* Perform any endianess-wise loads to interpret the data */ + uint64_t d =3D ldq_p(data); + printf("-> %x :: %x\n", cmd, *(uint64_t*)data); + } + EOF + +3. Build QEMU with the backdoor feature: + + /path/to/qemu/configure --with-backdoor=3D/tmp/my-backdoor-qemu + + +=3D=3D Guest-side code =3D=3D + +1. Compile the corresponding guest-side interface library: + + make -C /path/to/qemu-build/x86_64-linux-user/backdoor/guest + +2. Create your own application to interact with the backdoor channel: + + cat > /tmp/my-backdoor-guest.c < + #include + #include + #include + =20 + =20 + int main() + { + /* This base path is only applicable to 'user' mode */ + if (qemu_backdoor_init("/tmp/backdoor") !=3D 0) { + fprintf(stderr, "error: qemu_backdoor_init: %s\n", strerror(= errno)); + abort(); + } + =20 + /* Get a pointer to beginning of the data channel */ + uint32_t * data =3D qemu_backdoor_data(); + /* Write anything into the channel */ + *data =3D 0xcafe; + /* Invoke the channel */ + qemu_backdoor(0xbabe); + } + EOF + +3. Link your application against "libqemu-backdoor-guest.a": + + gcc -o /tmp/my-backdoor-guest /tmp/my-backdoor-guest.c /path/to/qemu= -build/x86_64-linux-user/backdoor/guest/libqemu-backdoor-guest.a + + +=3D=3D Running QEMU =3D=3D + +If you want to use QEMU's 'softmmu' mode: + + /path/to/qemu-build/x86_64-softmmu/qemu-system-x86_64 -device backdo= or + sudo /tmp/my-backdoor-guest # inside the VM + +If you want to use QEMU's 'user' mode: + + /path/to/qemu-build/x86_64-linux-user/qemu-x86_64 -backdoor /tmp/bac= kdoor /tmp/my-backdoor-guest + + +=3D=3D Implementation details =3D=3D + +The backdoor channel is composed of two channels that are handled as 'mm= ap'ed +files. The data channel is used to contain arbitrary data to communicate= back +and forth between the guest and QEMU. The control channel is used by the= guest +to signal that the data channel is ready to be used. + +When using the 'softmmu' mode, the backdoor communication channels are p= rovided +as a virtual device used through MMIO. The data channel acts as regular = memory +and the control channel intercepts all accesses to it to proxy them to t= he +user-provided backdoor library. + +When using the 'user' mode, the backdoor communication channels are prov= ided as +regular files in the host system that the guest must 'mmap' into its add= ress +space. The data channel acts as regular memory and the 'mmap' of the con= trol +channel is intercepted in QEMU to establish if it's an 'mmap' for the co= ntrol +channel file. If that's the case, the memory that QEMU allocates for the= guest +is 'mprotect'ed to intercept all accesses to it performed by the guest a= nd proxy +them to the user-provided backdoor library.