From nobody Tue May 14 09:50:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yadro.com ARC-Seal: i=1; a=rsa-sha256; t=1676891652; cv=none; d=zohomail.com; s=zohoarc; b=C5umnqnX7rJMJwQ8SuY2T45K1oNUeOqMVNwPDshsMp/seOjJSyapn3USRRENe1ZqZf588TDhDbTUwE+qeLB1SGX02BXg+dJXFO7se+bs5KgVkn0Rx+dRJ3CQ+hE6vGXgSvwZJo1FlvcmnjI7GHJ5Az5zXd5OUed4eKnDgPMCgHM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676891652; h=Content-Type:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=qOtY70Fv5qwcFnq0rOBRTwPRsOrtZJ7g90Nrr2otxA0=; b=bZ6rl3et01D+QH0MJnIeuNcLDDW+4lbGRs0KNR1g0KINh2s+blX2MlhxF4PNv3WOGqZdI8+DYcwy+cM2roZbn987u2Mg91j53LEw7/rkVIy0CWq1erjBqZYbvDixgb8UzMNwmFiq/W+Yu+y5jInFHV9wa3QVjnOe+fCIM+D362g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1676891651849185.41954335928096; Mon, 20 Feb 2023 03:14:11 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pU477-00075T-2Z; Mon, 20 Feb 2023 06:13:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pU473-0006y8-PQ for qemu-devel@nongnu.org; Mon, 20 Feb 2023 06:13:43 -0500 Received: from mta-02.yadro.com ([89.207.88.252] helo=mta-01.yadro.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pU470-0006ZF-FT for qemu-devel@nongnu.org; Mon, 20 Feb 2023 06:13:41 -0500 Received: from mta-01.yadro.com (localhost.localdomain [127.0.0.1]) by mta-01.yadro.com (Proxmox) with ESMTP id B9270341D0B; Mon, 20 Feb 2023 14:13:33 +0300 (MSK) Received: from T-EXCH-08.corp.yadro.com (unknown [172.17.10.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mta-01.yadro.com (Proxmox) with ESMTPS id ABCA1341BC0; Mon, 20 Feb 2023 14:13:33 +0300 (MSK) Received: from T-EXCH-08.corp.yadro.com (172.17.11.58) by T-EXCH-08.corp.yadro.com (172.17.11.58) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.9; Mon, 20 Feb 2023 14:13:33 +0300 Received: from T-EXCH-08.corp.yadro.com ([172.17.11.58]) by T-EXCH-08.corp.yadro.com ([172.17.11.58]) with mapi id 15.02.1118.009; Mon, 20 Feb 2023 14:13:33 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yadro.com; h=cc :cc:content-type:content-type:date:from:from:message-id :mime-version:reply-to:subject:subject:to:to; s=mta-01; bh=qOtY7 0Fv5qwcFnq0rOBRTwPRsOrtZJ7g90Nrr2otxA0=; b=aHPSQjkC0b/ZD5JLCFUbP 8Bbt7oSLd+iG39fGG81mNbGGVcoeAK/dQpue9znpFlNOfrnOUCBpK7ytSYINDAI7 SWZ7xlofrTJsLfTqXYPaO0fsJ9RRvPTaoeNf2eBnz72k2zLVFCfeFy8Nd/RV5z6S F2qkgRT+Ksqg73kg7IpRRU= From: Mikhail Tyutin To: "qemu-devel@nongnu.org" CC: "alex.bennee@linaro.org" , Peter Maydell , "erdnaxe@crans.org" , "ma.mandourr@gmail.com" Subject: [PATCH v2] TCG plugin API extension to read guest memory content by an address Thread-Topic: [PATCH v2] TCG plugin API extension to read guest memory content by an address Thread-Index: AdlFGzFjtCgLNbPNTzSXOtdOOq1PfA== Date: Mon, 20 Feb 2023 11:13:33 +0000 Message-ID: <5c50db42136d4a908b261c66b132b043@yadro.com> Accept-Language: en-US Content-Language: ru-RU X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.178.120.230] Content-Type: multipart/alternative; boundary="_000_5c50db42136d4a908b261c66b132b043yadrocom_" MIME-Version: 1.0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=89.207.88.252; envelope-from=m.tyutin@yadro.com; helo=mta-01.yadro.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yadro.com) X-ZM-MESSAGEID: 1676891654955100003 --_000_5c50db42136d4a908b261c66b132b043yadrocom_ Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" TCG plugin API extension to read guest memory content. qemu_plugin_vcpu_rea= d_phys_mem() function can be used by TCG plugin inside of qemu_plugin_vcpu_mem_cb_t call= back to adjust received address according to internal memory mappings and read content of = guest memory. Works for both user-level and system-level emulation modes. What's new in v2: * Increment QEMU_PLUGIN_VERSION instead of custom define * Example of qemu_plugin_vcpu_read_phys_mem() usage in memtrace.c * Use cpu_memory_rw_debug() to get memory content in both user-level and system-level emulation modes Signed-off-by: Mikhail Tyutin Signed-off-by: Aleksey Titov --- contrib/plugins/Makefile | 1 + contrib/plugins/memtrace.c | 76 ++++++++++++++++++++++++++++++++++++ include/qemu/qemu-plugin.h | 18 ++++++++- plugins/api.c | 16 ++++++++ plugins/qemu-plugins.symbols | 1 + 5 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 contrib/plugins/memtrace.c diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile index 23e0396687..cf27554616 100644 --- a/contrib/plugins/Makefile +++ b/contrib/plugins/Makefile @@ -21,6 +21,7 @@ NAMES +=3D lockstep NAMES +=3D hwprofile NAMES +=3D cache NAMES +=3D drcov +NAMES +=3D memtrace SONAMES :=3D $(addsuffix .so,$(addprefix lib,$(NAMES))) diff --git a/contrib/plugins/memtrace.c b/contrib/plugins/memtrace.c new file mode 100644 index 0000000000..16c1553f47 --- /dev/null +++ b/contrib/plugins/memtrace.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2023, Mikhail Tyutin + * + * Log all memory accesses including content of the access. + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include + +#include + + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + + +static void vcpu_mem_access(uint32_t vcpuIndex, qemu_plugin_meminfo_t memI= nfo, + uint64_t vaddr, void *userdata) +{ + unsigned size =3D 1 << qemu_plugin_mem_size_shift(memInfo); + char* memContent =3D g_malloc(size); + unsigned i; + GString* logLine =3D g_string_new(NULL); + + qemu_plugin_vcpu_read_phys_mem(vcpuIndex, vaddr, memContent, size); + + if(qemu_plugin_mem_is_store(memInfo)) { + g_string_append(logLine, "mem store"); + } + else { + g_string_append(logLine, "mem load"); + } + + g_string_append_printf(logLine, " at 0x%08"PRIx64, vaddr); + + g_string_append_printf(logLine, " size=3D%u content=3D{",size); + for (i =3D 0; i < size; i++) { + if (i !=3D 0) { + g_string_append(logLine, " "); + } + g_string_append_printf(logLine, "%02hhx", memContent[i]); + } + g_string_append(logLine, "}\n"); + + qemu_plugin_outs(logLine->str); + + g_string_free(logLine, TRUE); + g_free(memContent); +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + size_t n_insns; + size_t i; + + n_insns =3D qemu_plugin_tb_n_insns(tb); + for (i =3D 0; i < n_insns; i++) { + struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(tb, i); + + qemu_plugin_register_vcpu_mem_cb(insn, + vcpu_mem_access, + QEMU_PLUGIN_CB_NO_REGS, + QEMU_PLUGIN_MEM_RW, + 0); + } +} + +QEMU_PLUGIN_EXPORT +int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, + int argc, char **argv) +{ + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + + return 0; +} diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index d0e9d03adf..866282cf7d 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -51,7 +51,7 @@ typedef uint64_t qemu_plugin_id_t; extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; -#define QEMU_PLUGIN_VERSION 1 +#define QEMU_PLUGIN_VERSION 2 /** * struct qemu_info_t - system information for plugins @@ -625,4 +625,20 @@ uint64_t qemu_plugin_end_code(void); */ uint64_t qemu_plugin_entry_code(void); +/** + * qemu_plugin_vcpu_read_phys_mem() - reads guest's memory content + * + * @vcpu_index: vcpu index + * @addr: guest's virtual address + * @buf: destination buffer to read data to + * @len: number of bytes to read + * + * Adjusts address according to internal memory mapping and reads + * content of guest memory. + */ +void qemu_plugin_vcpu_read_phys_mem(unsigned int vcpu_index, + uint64_t addr, + void *buf, + uint64_t len); + #endif /* QEMU_QEMU_PLUGIN_H */ diff --git a/plugins/api.c b/plugins/api.c index 2078b16edb..af4f177229 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -442,3 +442,19 @@ uint64_t qemu_plugin_entry_code(void) #endif return entry; } + +void qemu_plugin_vcpu_read_phys_mem(unsigned int vcpu_index, + uint64_t addr, + void *buf, + uint64_t len) { + CPUClass *cc; + CPUState *cpu; + + cpu =3D qemu_get_cpu(vcpu_index); + cc =3D CPU_GET_CLASS(cpu); + if (cc->memory_rw_debug) { + cc->memory_rw_debug(cpu, addr, buf, len, false); + } else { + cpu_memory_rw_debug(cpu, addr, buf, len, false); + } +} \ No newline at end of file diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols index 71f6c90549..f0ce8c730f 100644 --- a/plugins/qemu-plugins.symbols +++ b/plugins/qemu-plugins.symbols @@ -42,4 +42,5 @@ qemu_plugin_tb_vaddr; qemu_plugin_uninstall; qemu_plugin_vcpu_for_each; + qemu_plugin_vcpu_read_phys_mem; }; -- 2.34.1 --_000_5c50db42136d4a908b261c66b132b043yadrocom_ Content-Type: text/html; charset="koi8-r" Content-Transfer-Encoding: quoted-printable

TCG plugin API extension to rea= d guest memory content. qemu_plugin_vcpu_read_phys_mem()<= /p>

function can be used by TCG plu= gin inside of qemu_plugin_vcpu_mem_cb_t callback to adjust

received address according to i= nternal memory mappings and read content of guest memory.=

Works for both user-level and s= ystem-level emulation modes.

 

What's new in v2:

* Increment QEMU_PLUGIN_VERSION= instead of custom define

* Example of qemu_plugin_vcpu_r= ead_phys_mem() usage in memtrace.c

* Use cpu_memory_rw_debug() to = get memory content in both user-level and

  system-level emulation m= odes

 

Signed-off-by: Mikhail Tyutin &= lt;m.tyutin@yadro.com>

Signed-off-by: Aleksey Titov &l= t;a.titov@yadro.com>

---

contrib/plugins/Makefile &= nbsp;   |  1 +

contrib/plugins/memtrace.c = ;  | 76 ++++++++++++&#= 43;++++++++++++++&#= 43;++++++++

include/qemu/qemu-plugin.h = ;  | 18 ++++++++-

plugins/api.c   =              | = 16 ++++++++

plugins/qemu-plugins.symbols |&= nbsp; 1 +

5 files changed, 111 insertions= (+), 1 deletion(-)

create mode 100644 contrib/plug= ins/memtrace.c

 

diff --git a/contrib/plugins/Ma= kefile b/contrib/plugins/Makefile

index 23e0396687..cf27554616 10= 0644

--- a/contrib/plugins/Makefile<= o:p>

+++ b/contrib/plugi= ns/Makefile

@@ -21,6 +21,7 @@ NAMES = 3;=3D lockstep

NAMES +=3D hwprofile

NAMES +=3D cache=

NAMES +=3D drcov=

+NAMES +=3D memtrace

 SONAMES :=3D $(addsuffix = .so,$(addprefix lib,$(NAMES)))

diff --git a/contrib/plugins/me= mtrace.c b/contrib/plugins/memtrace.c

new file mode 100644=

index 0000000000..16c1553f47

--- /dev/null=

+++ b/contrib/plugi= ns/memtrace.c

@@ -0,0 +1,76 @@=

+/*

+ * Copyright (C) 2023, Mik= hail Tyutin <m.tyutin@yadro.com>

+ *

+ * Log all memory accesses= including content of the access.

+ *

+ * License: GNU GPL, versi= on 2 or later.

+ *   See the COP= YING file in the top-level directory.

+ */

+

+#include <glib.h>

+

+#include <qemu-plugin.h= >

+

+

+QEMU_PLUGIN_EXPORT int qem= u_plugin_version =3D QEMU_PLUGIN_VERSION;

+

+

+static void vcpu_mem_acces= s(uint32_t vcpuIndex, qemu_plugin_meminfo_t memInfo,

+    &n= bsp;            = ;           uint64_t vadd= r, void *userdata)

+{

+    unsigne= d size =3D 1 << qemu_plugin_mem_size_shift(memInfo);

+    char* m= emContent =3D g_malloc(size);

+    unsigne= d i;

+    GString= * logLine =3D g_string_new(NULL);

+

+    qemu_pl= ugin_vcpu_read_phys_mem(vcpuIndex, vaddr, memContent, size);

+

+    if(qemu= _plugin_mem_is_store(memInfo)) {

+    &n= bsp;   g_string_append(logLine, "mem store");

+    }<= /o:p>

+    else {<= o:p>

+    &n= bsp;   g_string_append(logLine, "mem load");=

+    }<= /o:p>

+

+    g_strin= g_append_printf(logLine, " at 0x%08"PRIx64, vaddr);

+

+    g_strin= g_append_printf(logLine, " size=3D%u content=3D{",size);

+    for (i = =3D 0; i < size; i++) {

+    &n= bsp;   if (i !=3D 0) {

+    &n= bsp;       g_string_append(logLine, " &q= uot;);

+    &n= bsp;   }

+    &n= bsp;   g_string_append_printf(logLine, "%02hhx", memCon= tent[i]);

+    }<= /o:p>

+    g_strin= g_append(logLine, "}\n");

+

+    qemu_pl= ugin_outs(logLine->str);

+

+    g_strin= g_free(logLine, TRUE);

+    g_free(= memContent);

+}

+

+static void vcpu_tb_trans(= qemu_plugin_id_t id, struct qemu_plugin_tb *tb)

+{

+    size_t = n_insns;

+    size_t = i;

+

+    n_insns= =3D qemu_plugin_tb_n_insns(tb);

+    for (i = =3D 0; i < n_insns; i++) {

+    &n= bsp;   struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(= tb, i);

+

+    &n= bsp;   qemu_plugin_register_vcpu_mem_cb(insn,

+    &n= bsp;            = ;            &n= bsp;       vcpu_mem_access,=

+    &n= bsp;            = ;            &n= bsp;       QEMU_PLUGIN_CB_NO_REGS,=

+    &n= bsp;            = ;            &n= bsp;       QEMU_PLUGIN_MEM_RW,

+    &n= bsp;            = ;            &n= bsp;       0);

+    }<= /o:p>

+}

+

+QEMU_PLUGIN_EXPORT

+int qemu_plugin_install(qe= mu_plugin_id_t id, const qemu_info_t *info,

+    &n= bsp;            = ;       int argc, char **argv)

+{

+    qemu_pl= ugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);

+

+    return = 0;

+}

diff --git a/include/qemu/qemu-= plugin.h b/include/qemu/qemu-plugin.h

index d0e9d03adf..866282cf7d 10= 0644

--- a/include/qemu/qemu-plugin.= h

+++ b/include/qemu/= qemu-plugin.h

@@ -51,7 +51,7 @@ typedef u= int64_t qemu_plugin_id_t;

 extern QEMU_PLUGIN_EXPORT= int qemu_plugin_version;

-#define QEMU_PLUGIN_VERSION 1<= o:p>

+#define QEMU_PLUGIN_VERSIO= N 2

 /**

  * struct qemu_info_t - s= ystem information for plugins

@@ -625,4 +625,20 @@ uint64= _t qemu_plugin_end_code(void);

  */

uint64_t qemu_plugin_entry_code= (void);

+/**

+ * qemu_plugin_vcpu_read_p= hys_mem() - reads guest's memory content

+ *

+ * @vcpu_index: vcpu index=

+ * @addr: guest's virtual = address

+ * @buf: destination buffe= r to read data to

+ * @len: number of bytes t= o read

+ *

+ * Adjusts address accordi= ng to internal memory mapping and reads

+ * content of guest memory= .

+ */

+void qemu_plugin_vcpu_read= _phys_mem(unsigned int vcpu_index,

+    &n= bsp;            = ;            &n= bsp;      uint64_t addr,

+    &n= bsp;            = ;            &n= bsp;      void *buf,

+    &n= bsp;            = ;             &= nbsp;     uint64_t len);

+

#endif /* QEMU_QEMU_PLUGIN_H */=

diff --git a/plugins/api.c b/pl= ugins/api.c

index 2078b16edb..af4f177229 10= 0644

--- a/plugins/api.c<= /span>

+++ b/plugins/api.c=

@@ -442,3 +442,19 @@ uint64= _t qemu_plugin_entry_code(void)

#endif

     return= entry;

}

+

+void qemu_plugin_vcpu_read= _phys_mem(unsigned int vcpu_index,

+    &n= bsp;            = ;            &n= bsp;      uint64_t addr,

+    &n= bsp;            = ;            &n= bsp;      void *buf,

+    &n= bsp;            = ;            &n= bsp;      uint64_t len) {

+    CPUClas= s *cc;

+    CPUStat= e *cpu;

+

+    cpu =3D= qemu_get_cpu(vcpu_index);

+    cc =3D = CPU_GET_CLASS(cpu);

+    if (cc-= >memory_rw_debug) {

+    &n= bsp;   cc->memory_rw_debug(cpu, addr, buf, len, false);

+    } else = {

+    &n= bsp;   cpu_memory_rw_debug(cpu, addr, buf, len, false);

+    }<= /o:p>

+}

\ No newline at end of file

diff --git a/plugins/qemu-plugi= ns.symbols b/plugins/qemu-plugins.symbols

index 71f6c90549..f0ce8c730f 10= 0644

--- a/plugins/qemu-plugins.symb= ols

+++ b/plugins/qemu-= plugins.symbols

@@ -42,4 +42,5 @@

   qemu_plugin_tb_vad= dr;

   qemu_plugin_uninst= all;

   qemu_plugin_vcpu_f= or_each;

+  qemu_plugin_vcpu_re= ad_phys_mem;

};

--

2.34.1

 

--_000_5c50db42136d4a908b261c66b132b043yadrocom_--