From nobody Sun Nov 24 05:43:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 172647742030274.03011337277042; Mon, 16 Sep 2024 02:03:40 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 34B251A9A; Mon, 16 Sep 2024 05:03:39 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id A9E4C127A; Mon, 16 Sep 2024 05:03:16 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 09FE011EC; Mon, 16 Sep 2024 05:03:14 -0400 (EDT) Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 3DD6A11A6 for ; Mon, 16 Sep 2024 05:03:13 -0400 (EDT) Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-42cb9a0c300so35639915e9.0 for ; Mon, 16 Sep 2024 02:03:13 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378e73e846asm6625976f8f.41.2024.09.16.02.03.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 02:03:11 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id D70C75FCD7; Mon, 16 Sep 2024 09:54:01 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1726477392; x=1727082192; darn=lists.libvirt.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=B68/UAkmHFNWKQn2wg+UZm8jcMQdmSX3ck9PnfQqapo=; b=aUU3OQ+TxsBGJXIEW2AhzvF5ChZGCm2Kp8x+IXlKC1ij98C5ObUjfV8DykCveopD3j DoHPQyOGX0nCCLXCsO/8gi0FbMhjW6MBCOuD4SSSMlTPSiRkpmU1nwtxl+4sb003DBQ0 sWpDPlVgaT+pe5W6/75DkXhGMmzt58HZbf8kQ5qXM2d/uLNiB0pka4JGtOuz+VKd2dn0 lcfAQRTLBTqwmPT8ar4eulQsCC0EEQ/b7l/HxcQZr++N53twXxkallZrxOaSr+9AZIOT crRPFHVO0tRvJd/A2taDbZVo+hhEtmnRG//OpKMm2CChtHfA3NEqglouGcSnW2iOdmKu RPzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726477392; x=1727082192; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B68/UAkmHFNWKQn2wg+UZm8jcMQdmSX3ck9PnfQqapo=; b=RLhBN/9t0ojC5wKihs975uNztatz5wmS3n1odRXxvUi4/gnqYyYEVeqjpdyFeAxb8C z9gqz86t2panZYGf8pM4or4D1RmkQdPg8fuWUMXN9tOX8ZJs0TZ8100c0949yhjaBjsA k9namkTBGeZgGg4mePW9c+96De7F3GWmDYj/9Yky+hIXXdgm4aRpl2TesgTAw4hBJXYe QvvqezVR07rqgC4VQgjYpuKL/Ut0YuBpkV/h3zPMbNy+yXrsLT2FKcY/1KW09rFfw4o/ km5niSyLEyOF060JDsXjs5KV/gi45searEWSI3c2U/zKhUe4hF/pwhzOqwncYfVFI/1g +Y+g== X-Forwarded-Encrypted: i=1; AJvYcCX8aPj2xlL2dLCajcSNVETji3odQhwytr2dmw4aMHq1SvveoPxvYVa3XTJR199li3OyihlXwA==@lists.libvirt.org X-Gm-Message-State: AOJu0YyYMOgpWafK4JGPaUuuGxdNchL0bJJ6gPM/gU6UXMpZTBivgGKh gGpH+3Nd6eu3sbCkrVEx7CdrORXvt/Bbsebw8EyalN1l/GVLQo0obXxQCoUDM0c= X-Google-Smtp-Source: AGHT+IFpmSQCy7cM22BZM0ggb3oZsZrzR7HeGFrmduUy88rI6FzJ/fEMUpYb3IWB99wrfa1aG4c0PA== X-Received: by 2002:a05:600c:5104:b0:42c:b2fa:1c0a with SMTP id 5b1f17b1804b1-42cdb566287mr110441505e9.23.1726477391840; Mon, 16 Sep 2024 02:03:11 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v2 16/18] plugins: add plugin API to read guest memory Date: Mon, 16 Sep 2024 09:53:58 +0100 Message-Id: <20240916085400.1046925-17-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20240916085400.1046925-1-alex.bennee@linaro.org> References: <20240916085400.1046925-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: TVVBFPBWLDGIEIMGCRNWXQZVEGHUSUIG X-Message-ID-Hash: TVVBFPBWLDGIEIMGCRNWXQZVEGHUSUIG X-MailFrom: alex.bennee@linaro.org X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0 CC: qemu-s390x@nongnu.org, Thomas Huth , Richard Henderson , qemu-ppc@nongnu.org, Ilya Leoshkevich , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Marcel Apfelbaum , Alexandre Iooss , Eduardo Habkost , Daniel Henrique Barboza , Yanan Wang , Nicholas Piggin , David Hildenbrand , devel@lists.libvirt.org, Mahmoud Mandour , Zhao Liu , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Pierrick Bouvier , Rowan Hart X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1726477422259116600 Content-Type: text/plain; charset="utf-8" From: Rowan Hart Signed-off-by: Rowan Hart Reviewed-by: Pierrick Bouvier Message-Id: <20240827215329.248434-2-rowanbhart@gmail.com> [AJB: tweaked cpu_memory_rw_debug call] Signed-off-by: Alex Benn=C3=A9e --- vAJB: - explicit bool for cpu_memory_rw_debug v2 - fix alignment --- include/qemu/qemu-plugin.h | 32 +++++++++++++++++++++++++++++++- plugins/api.c | 20 ++++++++++++++++++++ plugins/qemu-plugins.symbols | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 649ce89815..622c9a0232 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -57,11 +57,19 @@ typedef uint64_t qemu_plugin_id_t; * - Remove qemu_plugin_register_vcpu_{tb, insn, mem}_exec_inline. * Those functions are replaced by *_per_vcpu variants, which guarantee * thread-safety for operations. + * + * version 3: + * - modified arguments and return value of qemu_plugin_insn_data to copy + * the data into a user-provided buffer instead of returning a pointer + * to the data. + * + * version 4: + * - added qemu_plugin_read_memory_vaddr */ =20 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; =20 -#define QEMU_PLUGIN_VERSION 3 +#define QEMU_PLUGIN_VERSION 4 =20 /** * struct qemu_info_t - system information for plugins @@ -884,6 +892,28 @@ typedef struct { QEMU_PLUGIN_API GArray *qemu_plugin_get_registers(void); =20 +/** + * qemu_plugin_read_memory_vaddr() - read from memory using a virtual addr= ess + * + * @addr: A virtual address to read from + * @data: A byte array to store data into + * @len: The number of bytes to read, starting from @addr + * + * @len bytes of data is read starting at @addr and stored into @data. If = @data + * is not large enough to hold @len bytes, it will be expanded to the nece= ssary + * size, reallocating if necessary. @len must be greater than 0. + * + * This function does not ensure writes are flushed prior to reading, so + * callers should take care when calling this function in plugin callbacks= to + * avoid attempting to read data which may not yet be written and should u= se + * the memory callback API instead. + * + * Returns true on success and false on failure. + */ +QEMU_PLUGIN_API +bool qemu_plugin_read_memory_vaddr(uint64_t addr, + GByteArray *data, size_t len); + /** * qemu_plugin_read_register() - read register for current vCPU * diff --git a/plugins/api.c b/plugins/api.c index 3316d4a04d..24ea64e2de 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -560,6 +560,26 @@ GArray *qemu_plugin_get_registers(void) return create_register_handles(regs); } =20 +bool qemu_plugin_read_memory_vaddr(vaddr addr, GByteArray *data, size_t le= n) +{ + g_assert(current_cpu); + + if (len =3D=3D 0) { + return false; + } + + g_byte_array_set_size(data, len); + + int result =3D cpu_memory_rw_debug(current_cpu, addr, data->data, + data->len, false); + + if (result < 0) { + return false; + } + + return true; +} + int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray= *buf) { g_assert(current_cpu); diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols index eed9d8abd9..032661f9ea 100644 --- a/plugins/qemu-plugins.symbols +++ b/plugins/qemu-plugins.symbols @@ -21,6 +21,7 @@ qemu_plugin_num_vcpus; qemu_plugin_outs; qemu_plugin_path_to_binary; + qemu_plugin_read_memory_vaddr; qemu_plugin_read_register; qemu_plugin_register_atexit_cb; qemu_plugin_register_flush_cb; --=20 2.39.5