From nobody Mon Feb 9 22:38:52 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505301830522501.2610645967675; Wed, 13 Sep 2017 04:23:50 -0700 (PDT) Received: from localhost ([::1]:41657 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5ll-0001pi-I6 for importer@patchew.org; Wed, 13 Sep 2017 07:23:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56272) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5ko-0001Al-MJ for qemu-devel@nongnu.org; Wed, 13 Sep 2017 07:22:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ds5kl-0001O3-Eh for qemu-devel@nongnu.org; Wed, 13 Sep 2017 07:22:50 -0400 Received: from roura.ac.upc.es ([147.83.33.10]:47411) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ds5kk-0001NR-VU for qemu-devel@nongnu.org; Wed, 13 Sep 2017 07:22:47 -0400 Received: from correu-1.ac.upc.es (correu-1.ac.upc.es [147.83.30.91]) by roura.ac.upc.es (8.13.8/8.13.8) with ESMTP id v8DBMhZ8010459; Wed, 13 Sep 2017 13:22:43 +0200 Received: from localhost (unknown [132.68.137.204]) by correu-1.ac.upc.es (Postfix) with ESMTPSA id 1DD5E96A; Wed, 13 Sep 2017 13:22:38 +0200 (CEST) From: =?utf-8?b?TGx1w61z?= Vilanova To: qemu-devel@nongnu.org Date: Wed, 13 Sep 2017 14:22:37 +0300 Message-Id: <150530175686.10902.13435538263886289800.stgit@frigg.lan> X-Mailer: git-send-email 2.14.1 In-Reply-To: <150529642278.10902.18234057937634437857.stgit@frigg.lan> References: <150529642278.10902.18234057937634437857.stgit@frigg.lan> User-Agent: StGit/0.18 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by roura.ac.upc.es id v8DBMhZ8010459 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x [fuzzy] X-Received-From: 147.83.33.10 Subject: [Qemu-devel] [PATCH v6 22/22] instrument: Add API to manipulate guest memory X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Emilio G. Cota" , Markus Armbruster , Stefan Hajnoczi , =?UTF-8?q?Llu=C3=ADs=20Vilanova?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 It includes access to the guest's memory and vCPU registers. Signed-off-by: Llu=C3=ADs Vilanova --- instrument/Makefile.objs | 1=20 instrument/qemu-instr/state.h | 104 +++++++++++++++++++++++++++++++++++++= ++++ instrument/state.c | 73 +++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 instrument/qemu-instr/state.h create mode 100644 instrument/state.c diff --git a/instrument/Makefile.objs b/instrument/Makefile.objs index d7e6c760c3..ee482bdb45 100644 --- a/instrument/Makefile.objs +++ b/instrument/Makefile.objs @@ -5,3 +5,4 @@ target-obj-$(CONFIG_INSTRUMENT) +=3D load.o target-obj-$(CONFIG_INSTRUMENT) +=3D qmp.o target-obj-$(CONFIG_INSTRUMENT) +=3D control.o target-obj-$(CONFIG_INSTRUMENT) +=3D trace.o +target-obj-$(CONFIG_INSTRUMENT) +=3D state.o diff --git a/instrument/qemu-instr/state.h b/instrument/qemu-instr/state.h new file mode 100644 index 0000000000..0ae6255fe5 --- /dev/null +++ b/instrument/qemu-instr/state.h @@ -0,0 +1,104 @@ +/* + * Interface for accessing guest state. + * + * Copyright (C) 2012-2017 Llu=C3=ADs Vilanova + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#ifndef QI__STATE_H +#define QI__STATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + + +/** + * qi_mem_read_virt: + * @vcpu: CPU to use for address translation. + * @vaddr: Starting virtual address to read from. + * @size: Number of bytes to read. + * @buf: Buffer to write into. + * + * Read contents from virtual memory. + * + * Returns: Whether the range of virtual addresses to read could be transl= ated. + * + * Warning: Even on error, some of the destination buffer might have been + * modified. + * + * Precondition: The output buffer has at least "size" bytes. + */ +bool qi_mem_read_virt(QICPU vcpu, uint64_t vaddr, size_t size, void *buf); + +/** + * qi_mem_write_virt: + * @vcpu: CPU to use for address translation. + * @vaddr: Starting virtual address to write into. + * @size: Number of bytes to write. + * @buf: Buffer with the contents to write from. + * + * Write contents into virtual memory. + * + * Returns: Whether the range of virtual addresses to write could be trans= lated. + * + * Warning: Even on error, some of the destination memory might have been + * modified. + * Precondition: The input buffer has at least "size" bytes. + */ +bool qi_mem_write_virt(QICPU vcpu, uint64_t vaddr, size_t size, void *buf); + +/** + * qi_mem_virt_to_phys: + * @vcpu: CPU to use for address translation. + * @vaddr: Virtual address to translate. + * @paddr: Pointer to output physical address. + * + * Translate a virtual address into a physical address. + * + * Returns: Whether the address could be translated. + */ +bool qi_mem_virt_to_phys(QICPU vcpu, uint64_t vaddr, uint64_t *paddr); + +/** + * qi_mem_read_phys: + * @paddr: Starting physical address to read from. + * @size: Number of bytes to read. + * @buf: Buffer to write into. + * + * Read contents from physical memory. + * + * Returns: Whether the range of physical addresses is valid. + * + * Warning: Even on error, some of the destination buffer might have been + * modified. + * Precondition: The output buffer has at least "size" bytes. + */ +bool qi_mem_read_phys(uint64_t paddr, size_t size, void *buf); + +/** + * qi_mem_write_phys: + * @paddr: Starting physical address to write into. + * @size: Number of bytes to write. + * @buf: Buffer with the contents to write from. + * + * Write contents into virtual memory. + * + * Returns: Whether the range of physical addresses is valid. + * + * Warning: Even on error, some of the destination memory might have been + * modified. + * + * Precondition: The input buffer has at least "size" bytes. + */ +bool qi_mem_write_phys(uint64_t paddr, size_t size, void *buf); + +#ifdef __cplusplus +} +#endif + +#endif /* QI__STATE_H */ diff --git a/instrument/state.c b/instrument/state.c new file mode 100644 index 0000000000..e76fd5fbcd --- /dev/null +++ b/instrument/state.c @@ -0,0 +1,73 @@ +/* + * Interface for accessing guest state. + * + * Copyright (C) 2012-2017 Llu=C3=ADs Vilanova + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" + +#include "qemu/compiler.h" +#include "cpu.h" +#include "exec/cpu-all.h" +#include "instrument/control.h" +#include "instrument/error.h" +#include "instrument/qemu-instr/state.h" + + +SYM_PUBLIC bool qi_mem_read_virt(QICPU vcpu, uint64_t vaddr, + size_t size, void *buf) +{ + CPUState *vcpu_ =3D instr_cpu_from_qicpu(vcpu); + ERROR_IF_RET(!instr_get_state(), false, "called outside instrumentatio= n"); + ERROR_IF_RET(!vcpu_, false, "invalid QICPU"); + return cpu_memory_rw_debug(vcpu_, vaddr, buf, size, 0) =3D=3D 0; +} + +SYM_PUBLIC bool qi_mem_write_virt(QICPU vcpu, uint64_t vaddr, + size_t size, void *buf) +{ + CPUState *vcpu_ =3D instr_cpu_from_qicpu(vcpu); + ERROR_IF_RET(!instr_get_state(), false, "called outside instrumentatio= n"); + ERROR_IF_RET(!vcpu_, false, "invalid QICPU"); + return cpu_memory_rw_debug(vcpu_, vaddr, buf, size, 1) =3D=3D 0; +} + +SYM_PUBLIC bool qi_mem_virt_to_phys(QICPU vcpu, uint64_t vaddr, uint64_t *= paddr) +{ + CPUState *vcpu_ =3D instr_cpu_from_qicpu(vcpu); + ERROR_IF_RET(!instr_get_state(), false, "called outside instrumentatio= n"); + ERROR_IF_RET(!vcpu_, false, "invalid QICPU"); + +#if defined(CONFIG_USER_ONLY) + *paddr =3D vaddr; + return true; +#else + *paddr =3D cpu_get_phys_page_debug(vcpu_, vaddr); + return *paddr !=3D -1; +#endif +} + +SYM_PUBLIC bool qi_mem_read_phys(uint64_t paddr, size_t size, void *buf) +{ + ERROR_IF_RET(!instr_get_state(), false, "called outside instrumentatio= n"); +#if defined(CONFIG_USER_ONLY) + return cpu_memory_rw_debug(NULL, paddr, buf, size, 0) =3D=3D 0; +#else + cpu_physical_memory_read(paddr, buf, size); + return true; +#endif +} + +SYM_PUBLIC bool qi_mem_write_phys(uint64_t paddr, size_t size, void *buf) +{ + ERROR_IF_RET(!instr_get_state(), false, "called outside instrumentatio= n"); +#if defined(CONFIG_USER_ONLY) + return cpu_memory_rw_debug(NULL, paddr, buf, size, 1) =3D=3D 0; +#else + cpu_physical_memory_write(paddr, buf, size); + return true; +#endif +}