From nobody Thu Dec 18 07:59:50 2025 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1733472068; cv=none; d=zohomail.com; s=zohoarc; b=keZfu903/U+4v/1JqKTup5HOPjkzgqEmazGPQqgxhAmr3ObrSimlKfAWEW7meuWNqRJ2IhTAh5QENE1IVYq5J4K32merR/JaLHU7xlEQf+DDk5tvQ8HqXGQGVI9Hw1UeE+qouNEK5wOQ7Hub7epnF0jpVsfnrvLGsyVlHBkWYtA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1733472068; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cip0LPHBoehxlgnIZ9C55yYW9ZMranVZsujGiSNdgNE=; b=cSnSG7EatJqS9Ofn4VB/RLvaDacyHDqX6EaYcjH2x6RBQ540/PEwJmBE0H2p8k/HWB3eVteHunRfAkzI93CqGaEbBm6E9Y/hxzDJb7GWe0lKx1pUBChXH7smSJgAxO0YS+65yIWDkc6r1rrU4EqpBk1sW6+BlP6LakOfrRwjnm4= 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 1733472068560932.7374250449187; Fri, 6 Dec 2024 00:01:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tJTG5-0007em-D9; Fri, 06 Dec 2024 03:00:17 -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 1tJTG2-0007bq-73 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 03:00:14 -0500 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tJTFz-0005mc-Ft for qemu-devel@nongnu.org; Fri, 06 Dec 2024 03:00:13 -0500 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-7252b7326f4so1521433b3a.2 for ; Fri, 06 Dec 2024 00:00:10 -0800 (PST) Received: from chaos.lan ([50.39.253.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-215f8e5f601sm23527525ad.73.2024.12.06.00.00.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Dec 2024 00:00:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733472009; x=1734076809; darn=nongnu.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=cip0LPHBoehxlgnIZ9C55yYW9ZMranVZsujGiSNdgNE=; b=gvYV6u3jFjm51ljvOKzhApKFBk8cbrkAmJ+IzgEnYLwPpAZQXkkbz3KCUHck1rveOs bPC+P0LNKVnhDHUdU9c7fZWCs5RvC0CcEhinSsNomumlawZYBdLGMq+YnYeBAho3A5X0 OhUN858jKkbHgf17snAQjdJdPT7K5MDoon/eV4h0rSWtlNzcy+hS98g9pAcyO332aMNt qYQkRhN20WuA8eib/4jwc37h84c2W6nXe9YmZabNQ0janCU1P3dfNyl4HLfpbq7P1dkk f2LUKBepq4zNlr10a7S3MXIh0BYA9X7+H+3OeRxGEoOeS8GM3q5QDlpGIPKuOM9z5iXl vC2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733472009; x=1734076809; 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=cip0LPHBoehxlgnIZ9C55yYW9ZMranVZsujGiSNdgNE=; b=R8QN9olgz2oSst0c77EPSMHvf8hOjnsvXuKDZkwT8pCwUVzdqdYqiaOIUyk7yZGyAT MF4BKydeqGdwC3fAE7h9JDEpXftUKKqx+VFnGb7yDrhE++hmbkvfEWKj1A5NyGm0a0/6 nYZ68Oya/v1ZSm1DIgCc/lJOFAGqCkrnlQ6pBAPdpx4OeYmNYWmz47ot/yMyrZGoC3Cz YDOrtOUyfc2kgwTrLefMgcg1upd1MD2T3v6NqppozOu0ID4ubMJUuVOpoVa+U2k0l9rq wOZp7Lhkq25z3BEFvZgmKzM5C5MEn1eDGKF8u4I7xOw5mjFjgu6aBoO+XE/XsskTJrXH o9wQ== X-Gm-Message-State: AOJu0YzQPoibHMXkWwZp+WqTThT/7Jo8fQP4tg6PkU2JoiZJPm3SewnO wXqX8HCKvanewlMMAbUbmhmEu1pjXmn6H9M/KKU0ijU4d3fVkxEQ014dyCck6mg= X-Gm-Gg: ASbGncsN6OnQJjYMrUe3Fit+61GAoOenF55ALx7zoYObCjfvD5+xEb992bcpRDAz8cd BAnjxUyjBCwBL2ioM8sTzPdSy4h4Q2+asqIrYHaj2a8FyBjSjf6q1R+Zz3YUsYEEjG9esLbUQey gQSdPlfwm+99sUarSXwaThQ4tIolah3CNGNNmErMkwdE834UcO9FdFiig8D3jU8EOaMOls0P+yt XStTahiken5RB9iaHkrhprgr9YsyqN9cWtj6fm2y/JNpyU= X-Google-Smtp-Source: AGHT+IFeaI/+19fft7Y2bFyT8MXQ9gsNU9IbLO9oT4FUPqczxYi60aa2fiGlXb7b+ErgqcpvYcvZiA== X-Received: by 2002:a17:903:11c8:b0:215:4a31:47d8 with SMTP id d9443c01a7336-21614d1ef5emr29395825ad.9.1733472008636; Fri, 06 Dec 2024 00:00:08 -0800 (PST) From: Rowan Hart To: qemu-devel@nongnu.org Cc: Pierrick Bouvier , Alexandre Iooss , =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Mahmoud Mandour , novafacing Subject: [PATCH 2/2] Add plugin API functions for register R/W, hwaddr R/W, vaddr W Date: Fri, 6 Dec 2024 00:00:04 -0800 Message-ID: <20241206080005.775275-3-rowanbhart@gmail.com> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241206080005.775275-1-rowanbhart@gmail.com> References: <20241206080005.775275-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::435; envelope-from=rowanbhart@gmail.com; helo=mail-pf1-x435.google.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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 @gmail.com) X-ZM-MESSAGEID: 1733472072363116600 Content-Type: text/plain; charset="utf-8" From: novafacing --- include/qemu/qemu-plugin.h | 116 +++++++++++++++++++++++++++++++++---- plugins/api.c | 66 ++++++++++++++++++++- 2 files changed, 168 insertions(+), 14 deletions(-) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 0fba36ae02..b812593e7f 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -65,11 +65,18 @@ typedef uint64_t qemu_plugin_id_t; * * version 4: * - added qemu_plugin_read_memory_vaddr + * + * version 5: + * - added qemu_plugin_write_memory_vaddr + * - added qemu_plugin_read_memory_hwaddr + * - added qemu_plugin_write_memory_hwaddr + * - added qemu_plugin_write_register + * */ =20 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; =20 -#define QEMU_PLUGIN_VERSION 4 +#define QEMU_PLUGIN_VERSION 5 =20 /** * struct qemu_info_t - system information for plugins @@ -255,8 +262,6 @@ typedef struct { * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs * - * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change - * system register state. */ enum qemu_plugin_cb_flags { QEMU_PLUGIN_CB_NO_REGS, @@ -893,6 +898,41 @@ typedef struct { QEMU_PLUGIN_API GArray *qemu_plugin_get_registers(void); =20 +/** + * qemu_plugin_read_register() - read register for current vCPU + * + * @handle: a @qemu_plugin_reg_handle handle + * @buf: A GByteArray for the data owned by the plugin + * + * This function is only available in a context that register read access = is + * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. + * + * Returns the size of the read register. The content of @buf is in target= byte + * order. On failure returns -1. + */ +QEMU_PLUGIN_API +int qemu_plugin_read_register(struct qemu_plugin_register *handle, + GByteArray *buf); + +/** + * qemu_plugin_write_register() - write register for current vCPU + * + * @handle: a @qemu_plugin_reg_handle handle + * @buf: A GByteArray for the data owned by the plugin + * + * This function is only available in a context that register write access= is + * explicitly requested via the QEMU_PLUGIN_CB_W_REGS flag. + * + * The size of @buf must be at least the size of the requested register. + * Attempting to write a register with @buf smaller than the register size + * will result in a crash or other undesired behavior. + * + * Returns the number of bytes written. On failure returns 0. + */ +QEMU_PLUGIN_API +int qemu_plugin_write_register(struct qemu_plugin_register *handle, + GByteArray *buf); + /** * qemu_plugin_read_memory_vaddr() - read from memory using a virtual addr= ess * @@ -916,20 +956,72 @@ bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len); =20 /** - * qemu_plugin_read_register() - read register for current vCPU + * qemu_plugin_write_memory_vaddr() - write to memory using a virtual addr= ess * - * @handle: a @qemu_plugin_reg_handle handle - * @buf: A GByteArray for the data owned by the plugin + * @addr: A virtual address to write to=20 + * @data: A byte array containing the data to write * - * This function is only available in a context that register read access = is - * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. + * The contents of @data will be written to memory starting at the virtual + * address @addr. * - * Returns the size of the read register. The content of @buf is in target= byte - * order. On failure returns -1. + * This function does not guarantee consistency of writes, nor does it ens= ure + * that pending writes are flushed either before or after the write takes + * place, so callers should take care when calling this function in plugin + * callbacks to avoid depending on the existence of data written using this + * function which may be overwritten afterward. + * + * Returns true on success and false on failure. */ QEMU_PLUGIN_API -int qemu_plugin_read_register(struct qemu_plugin_register *handle, - GByteArray *buf); +bool qemu_plugin_write_memory_vaddr(uint64_t addr, + GByteArray *data); + +/** + * qemu_plugin_read_memory_vaddr() - read from memory using a hardware add= ress + * + * @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. + * + * This function is only valid for softmmu targets. + * + * Returns true on success and false on failure. + */ +QEMU_PLUGIN_API +bool qemu_plugin_read_memory_hwaddr(uint64_t addr, + GByteArray *data, size_t len); + +/** + * qemu_plugin_write_memory_vaddr() - write to memory using a hardware add= ress + * + * @addr: A virtual address to write to=20 + * @data: A byte array containing the data to write + * + * The contents of @data will be written to memory starting at the hardware + * address @addr. + * + * This function does not guarantee consistency of writes, nor does it ens= ure + * that pending writes are flushed either before or after the write takes + * place, so callers should take care when calling this function in plugin + * callbacks to avoid depending on the existence of data written using this + * function which may be overwritten afterward. + * + * This function is only valid for softmmu targets. + * + * Returns true on success and false on failure. + */ +QEMU_PLUGIN_API +bool qemu_plugin_write_memory_hwaddr(uint64_t addr, + GByteArray *data); =20 /** * qemu_plugin_scoreboard_new() - alloc a new scoreboard diff --git a/plugins/api.c b/plugins/api.c index 24ea64e2de..4a84cf4dfe 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -560,6 +560,24 @@ GArray *qemu_plugin_get_registers(void) return create_register_handles(regs); } =20 +int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray= *buf) +{ + g_assert(current_cpu); + + return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1); +} + +int qemu_plugin_write_register(struct qemu_plugin_register *reg, GByteArra= y *buf) +{ + g_assert(current_cpu); + + if (buf->len =3D=3D 0) { + return 0; + } + + return gdb_write_register(current_cpu, buf->data, GPOINTER_TO_INT(reg)= - 1); +} + bool qemu_plugin_read_memory_vaddr(vaddr addr, GByteArray *data, size_t le= n) { g_assert(current_cpu); @@ -580,13 +598,57 @@ bool qemu_plugin_read_memory_vaddr(vaddr addr, GByteA= rray *data, size_t len) return true; } =20 -int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray= *buf) +bool qemu_plugin_write_memory_vaddr(vaddr addr, GByteArray *data) { g_assert(current_cpu); =20 - return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1); + if (data->len =3D=3D 0) { + return false; + } + + int result =3D cpu_memory_rw_debug(current_cpu, addr, data->data, + data->len, true); + + if (result < 0) { + return false; + } + + return true; +} + +bool qemu_plugin_read_memory_hwaddr(hwaddr addr, GByteArray *data, size_t = len) +{ +#ifdef CONFIG_SOFTMMU + if (len =3D=3D 0) { + return false; + } + + g_byte_array_set_size(data, len); + + cpu_physical_memory_rw(addr, data->data, data->len, false); + + return true; +#else + return false; +#endif } =20 +bool qemu_plugin_write_memory_hwaddr(hwaddr addr, GByteArray *data) +{ +#ifdef CONFIG_SOFTMMU + if (data->len =3D=3D 0) { + return false; + } + + cpu_physical_memory_rw(addr, data->data, data->len, true); + + return true; +#else + return false; +#endif +} + + struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_s= ize) { return plugin_scoreboard_new(element_size); --=20 2.46.1