[{"id":3241545,"web_url":"http://patchwork.ozlabs.org/comment/3241545/","msgid":"<52cac44e-a467-4748-8c5b-c9c47f5b0f79@daynix.com>","list_archive_url":null,"date":"2024-01-04T12:05:22","subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","submitter":{"id":84745,"url":"http://patchwork.ozlabs.org/api/people/84745/","name":"Akihiko Odaki","email":"akihiko.odaki@daynix.com"},"content":"On 2024/01/04 2:33, Alex Bennée wrote:\n> We can only request a list of registers once the vCPU has been\n> initialised so the user needs to use either call the get function on\n> vCPU initialisation or during the translation phase.\n> \n> We don't expose the reg number to the plugin instead hiding it behind\n> an opaque handle. This allows for a bit of future proofing should the\n> internals need to be changed while also being hashed against the\n> CPUClass so we can handle different register sets per-vCPU in\n> hetrogenous situations.\n> \n> Having an internal state within the plugins also allows us to expand\n> the interface in future (for example providing callbacks on register\n> change if the translator can track changes).\n> \n> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1706\n> Cc: Akihiko Odaki <akihiko.odaki@daynix.com>\n> Based-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>\n> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>\n> \n> ---\n> v3\n>    - also g_intern_string the register name\n>    - make get_registers documentation a bit less verbose\n> v2\n>    - use new get whole list api, and expose upwards\n> \n> vAJB:\n> \n> The main difference to Akikio's version is hiding the gdb register\n> detail from the plugin for the reasons described above.\n> ---\n>   include/qemu/qemu-plugin.h   |  51 +++++++++++++++++-\n>   plugins/api.c                | 102 +++++++++++++++++++++++++++++++++++\n>   plugins/qemu-plugins.symbols |   2 +\n>   3 files changed, 153 insertions(+), 2 deletions(-)\n> \n> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h\n> index 4daab6efd29..95380895f81 100644\n> --- a/include/qemu/qemu-plugin.h\n> +++ b/include/qemu/qemu-plugin.h\n> @@ -11,6 +11,7 @@\n>   #ifndef QEMU_QEMU_PLUGIN_H\n>   #define QEMU_QEMU_PLUGIN_H\n>   \n> +#include <glib.h>\n>   #include <inttypes.h>\n>   #include <stdbool.h>\n>   #include <stddef.h>\n> @@ -227,8 +228,8 @@ struct qemu_plugin_insn;\n>    * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n>    * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n>    *\n> - * Note: currently unused, plugins cannot read or change system\n> - * register state.\n> + * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n> + * system register state.\n>    */\n>   enum qemu_plugin_cb_flags {\n>       QEMU_PLUGIN_CB_NO_REGS,\n> @@ -708,4 +709,50 @@ uint64_t qemu_plugin_end_code(void);\n>   QEMU_PLUGIN_API\n>   uint64_t qemu_plugin_entry_code(void);\n>   \n> +/** struct qemu_plugin_register - Opaque handle for register access */\n> +struct qemu_plugin_register;\n\nJust in case you missed my comment for the earlier version:\n\nWhat about identifying a register with an index in an array returned\nby qemu_plugin_get_registers(). That saves troubles having the handle\nmember in qemu_plugin_reg_descriptor.\n\nRegards,\nAkihiko Odaki","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=daynix-com.20230601.gappssmtp.com\n header.i=@daynix-com.20230601.gappssmtp.com header.a=rsa-sha256\n header.s=20230601 header.b=3DxkuBAW;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4T5QKr6WdYz23dg\n\tfor <incoming@patchwork.ozlabs.org>; Thu,  4 Jan 2024 23:06:24 +1100 (AEDT)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1rLMTm-0006k3-Ht; Thu, 04 Jan 2024 07:05:42 -0500","from eggs.gnu.org ([2001:470:142:3::10])\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <akihiko.odaki@daynix.com>)\n id 1rLMTj-0006ic-C7\n for qemu-devel@nongnu.org; Thu, 04 Jan 2024 07:05:40 -0500","from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <akihiko.odaki@daynix.com>)\n id 1rLMTf-0000GE-EF\n for qemu-devel@nongnu.org; Thu, 04 Jan 2024 07:05:38 -0500","by mail-pg1-x536.google.com with SMTP id\n 41be03b00d2f7-5cde7afa1d7so263734a12.1\n for <qemu-devel@nongnu.org>; Thu, 04 Jan 2024 04:05:33 -0800 (PST)","from ?IPV6:2400:4050:a840:1e00:9ac7:6d57:2b16:6932?\n ([2400:4050:a840:1e00:9ac7:6d57:2b16:6932])\n by smtp.gmail.com with ESMTPSA id\n c1-20020a63a441000000b005ce472f2d0fsm12383424pgp.66.2024.01.04.04.05.23\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Thu, 04 Jan 2024 04:05:32 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1704369932; x=1704974732;\n darn=nongnu.org;\n h=content-transfer-encoding:in-reply-to:from:references:cc:to\n :content-language:subject:user-agent:mime-version:date:message-id\n :from:to:cc:subject:date:message-id:reply-to;\n bh=1JfSYgvG+gN3IuIgW3mKT13qe2Rfz3AaTGl7a0v25sQ=;\n b=3DxkuBAWTHVazoQPmHwXyxjcEtY2ZMGv21rAc3ZBHBY8ryqdJMNHc1rvG9T206WBxw\n HY6Uxr41uypCWmd2PxyCXfd+QBMH/SkhWP5Gfx/rSDZiuSj3ri5x9Ug1z63ASwBX1n4j\n 2W1V/NrHeWXm0KNShqRVmJ9IIBzv04li+QXFApBf2Qs5L7uPiOE+HmXkjCrHTWMOvZC7\n oh55eyz7p0UPx1TPihiK8+WTegorHDuhOsZibIAPCUiJcoeUftKWajl8jYWzMsyRegpp\n ebUuaR3VtLLHXxrBOR/9c/HnPeqIfoLB/xPWIsnxdY79Z3DIJHOOWVb1u5MLZGmCPgPr\n QShg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1704369932; x=1704974732;\n h=content-transfer-encoding:in-reply-to:from:references:cc:to\n :content-language:subject:user-agent:mime-version:date:message-id\n :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;\n bh=1JfSYgvG+gN3IuIgW3mKT13qe2Rfz3AaTGl7a0v25sQ=;\n b=KQAGDpJHlwk6cbMwyb7+TxAakvp/XMIojASYRPzNAYfHoFI8cFFXInHYF0T3sGuG9I\n vnuerval62rBmebetB9v2nhFQetmVkdAce70SWbr9UCnt9FIfYLD6bdC7m/RvwweUtUz\n aSMCAArQ2CuKPGDEWdK5VsD4UgAhbsukRZzAMtLnbFQJpp/a9lDTm9BNMKIE6hZHdZBy\n 7mxcESnbL4lTcSinz01doYP06uxegpy4lMchQ7D5MX00u0hrXXP2vUefFzKiCr1k8tRi\n 7GsWqUbG1uv1y6f6XMnGEg5A1lR5nWyc5fLXyDX/18KmjkYHW5dXRwLWXdNvpceSPPNQ\n eswg==","X-Gm-Message-State":"AOJu0YzSMHLpWaHwPVi4gPFMDtz5oxymCBzEfRuDgU7nQtFZiaAi24L8\n f7YO23vY21bX/3ONRibmi5UwMKh3Zhdt6Q==","X-Google-Smtp-Source":"\n AGHT+IF2iS1huYrRTsdmtHUlZNPJuYf+UOYa/O57oEjcrmBp+rJUuvidSwvdw5+1d7IxIqlfD1OWKQ==","X-Received":"by 2002:a05:6a20:430d:b0:198:f146:b2da with SMTP id\n h13-20020a056a20430d00b00198f146b2damr405894pzk.93.1704369932451;\n Thu, 04 Jan 2024 04:05:32 -0800 (PST)","Message-ID":"<52cac44e-a467-4748-8c5b-c9c47f5b0f79@daynix.com>","Date":"Thu, 4 Jan 2024 21:05:22 +0900","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","Content-Language":"en-US","To":"=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>,\n qemu-devel@nongnu.org","Cc":"qemu-s390x@nongnu.org, qemu-ppc@nongnu.org,\n Richard Henderson <richard.henderson@linaro.org>,\n Song Gao <gaosong@loongson.cn>,\n =?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>,\n David Hildenbrand <david@redhat.com>, Aurelien Jarno <aurelien@aurel32.net>,\n Yoshinori Sato <ysato@users.sourceforge.jp>,\n Yanan Wang <wangyanan55@huawei.com>, Bin Meng <bin.meng@windriver.com>,\n Laurent Vivier <lvivier@redhat.com>, Michael Rolnik <mrolnik@gmail.com>,\n Alexandre Iooss <erdnaxe@crans.org>, David Woodhouse <dwmw2@infradead.org>,\n Laurent Vivier <laurent@vivier.eu>, Paolo Bonzini <pbonzini@redhat.com>,\n Brian Cain <bcain@quicinc.com>,\n Daniel Henrique Barboza <danielhb413@gmail.com>,\n Beraldo Leal <bleal@redhat.com>, Paul Durrant <paul@xen.org>,\n Mahmoud Mandour <ma.mandourr@gmail.com>, Thomas Huth <thuth@redhat.com>,\n Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Cleber Rosa <crosa@redhat.com>,\n kvm@vger.kernel.org, Peter Maydell <peter.maydell@linaro.org>,\n Wainer dos Santos Moschetta <wainersm@redhat.com>, qemu-arm@nongnu.org,\n Weiwei Li <liwei1518@gmail.com>,\n =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= <philmd@linaro.org>,\n John Snow <jsnow@redhat.com>,\n Daniel Henrique Barboza <dbarboza@ventanamicro.com>,\n Nicholas Piggin <npiggin@gmail.com>, Palmer Dabbelt <palmer@dabbelt.com>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,\n Ilya Leoshkevich <iii@linux.ibm.com>,\n =?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@kaod.org>,\n \"Edgar E. Iglesias\" <edgar.iglesias@gmail.com>,\n Eduardo Habkost <eduardo@habkost.net>,\n Pierrick Bouvier <pierrick.bouvier@linaro.org>, qemu-riscv@nongnu.org,\n Alistair Francis <alistair.francis@wdc.com>","References":"<20240103173349.398526-1-alex.bennee@linaro.org>\n <20240103173349.398526-39-alex.bennee@linaro.org>","From":"Akihiko Odaki <akihiko.odaki@daynix.com>","In-Reply-To":"<20240103173349.398526-39-alex.bennee@linaro.org>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"8bit","Received-SPF":"none client-ip=2607:f8b0:4864:20::536;\n envelope-from=akihiko.odaki@daynix.com; helo=mail-pg1-x536.google.com","X-Spam_score_int":"-18","X-Spam_score":"-1.9","X-Spam_bar":"-","X-Spam_report":"(-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,\n SPF_NONE=0.001,\n T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3241564,"web_url":"http://patchwork.ozlabs.org/comment/3241564/","msgid":"<87cyuhguf4.fsf@draig.linaro.org>","list_archive_url":null,"date":"2024-01-04T12:22:23","subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","submitter":{"id":39532,"url":"http://patchwork.ozlabs.org/api/people/39532/","name":"Alex Bennée","email":"alex.bennee@linaro.org"},"content":"Akihiko Odaki <akihiko.odaki@daynix.com> writes:\n\n> On 2024/01/04 2:33, Alex Bennée wrote:\n>> We can only request a list of registers once the vCPU has been\n>> initialised so the user needs to use either call the get function on\n>> vCPU initialisation or during the translation phase.\n>> We don't expose the reg number to the plugin instead hiding it\n>> behind\n>> an opaque handle. This allows for a bit of future proofing should the\n>> internals need to be changed while also being hashed against the\n>> CPUClass so we can handle different register sets per-vCPU in\n>> hetrogenous situations.\n>> Having an internal state within the plugins also allows us to expand\n>> the interface in future (for example providing callbacks on register\n>> change if the translator can track changes).\n>> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1706\n>> Cc: Akihiko Odaki <akihiko.odaki@daynix.com>\n>> Based-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>\n>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>\n>> ---\n>> v3\n>>    - also g_intern_string the register name\n>>    - make get_registers documentation a bit less verbose\n>> v2\n>>    - use new get whole list api, and expose upwards\n>> vAJB:\n>> The main difference to Akikio's version is hiding the gdb register\n>> detail from the plugin for the reasons described above.\n>> ---\n>>   include/qemu/qemu-plugin.h   |  51 +++++++++++++++++-\n>>   plugins/api.c                | 102 +++++++++++++++++++++++++++++++++++\n>>   plugins/qemu-plugins.symbols |   2 +\n>>   3 files changed, 153 insertions(+), 2 deletions(-)\n>> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h\n>> index 4daab6efd29..95380895f81 100644\n>> --- a/include/qemu/qemu-plugin.h\n>> +++ b/include/qemu/qemu-plugin.h\n>> @@ -11,6 +11,7 @@\n>>   #ifndef QEMU_QEMU_PLUGIN_H\n>>   #define QEMU_QEMU_PLUGIN_H\n>>   +#include <glib.h>\n>>   #include <inttypes.h>\n>>   #include <stdbool.h>\n>>   #include <stddef.h>\n>> @@ -227,8 +228,8 @@ struct qemu_plugin_insn;\n>>    * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n>>    * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n>>    *\n>> - * Note: currently unused, plugins cannot read or change system\n>> - * register state.\n>> + * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n>> + * system register state.\n>>    */\n>>   enum qemu_plugin_cb_flags {\n>>       QEMU_PLUGIN_CB_NO_REGS,\n>> @@ -708,4 +709,50 @@ uint64_t qemu_plugin_end_code(void);\n>>   QEMU_PLUGIN_API\n>>   uint64_t qemu_plugin_entry_code(void);\n>>   +/** struct qemu_plugin_register - Opaque handle for register\n>> access */\n>> +struct qemu_plugin_register;\n>\n> Just in case you missed my comment for the earlier version:\n>\n> What about identifying a register with an index in an array returned\n> by qemu_plugin_get_registers(). That saves troubles having the handle\n> member in qemu_plugin_reg_descriptor.\n\nThe handle gets de-referenced internally in the plugin api and\nadditional checking could be added there. If we pass an index then we'd\nend up having to track the index assigned during get_registers as well\nas account for a potential skew in the index value if the register\nlayout varies between vCPUs (although I admit this is future proofing\nfor potential heterogeneous models).\n\nThe concept of opaque handle == pointer is fairly common in the QEMU\ncode base. We are not making it hard for a plugin author to bypass this\n\"protection\", just making it clear if you do so your violating the\nprinciple of the API.\n\n>\n> Regards,\n> Akihiko Odaki","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256\n header.s=google header.b=CENhCJFD;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4T5Qhw1M1Mz1ydb\n\tfor <incoming@patchwork.ozlabs.org>; Thu,  4 Jan 2024 23:22:56 +1100 (AEDT)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1rLMk3-0004TF-3N; Thu, 04 Jan 2024 07:22:31 -0500","from eggs.gnu.org ([2001:470:142:3::10])\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <alex.bennee@linaro.org>)\n id 1rLMk1-0004T7-HA\n for qemu-devel@nongnu.org; Thu, 04 Jan 2024 07:22:29 -0500","from mail-wm1-x335.google.com ([2a00:1450:4864:20::335])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <alex.bennee@linaro.org>)\n id 1rLMjz-0003gx-HN\n for qemu-devel@nongnu.org; Thu, 04 Jan 2024 07:22:29 -0500","by mail-wm1-x335.google.com with SMTP id\n 5b1f17b1804b1-40e3485f0e1so3250875e9.3\n for <qemu-devel@nongnu.org>; Thu, 04 Jan 2024 04:22:26 -0800 (PST)","from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id\n k9-20020a05600c1c8900b0040d772030c2sm5545801wms.44.2024.01.04.04.22.24\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Thu, 04 Jan 2024 04:22:24 -0800 (PST)","from draig (localhost [IPv6:::1])\n by draig.lan (Postfix) with ESMTP id B05F75F92F;\n Thu,  4 Jan 2024 12:22:23 +0000 (GMT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=linaro.org; s=google; t=1704370945; x=1704975745; darn=nongnu.org;\n h=content-transfer-encoding:mime-version:message-id:date:user-agent\n :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=aVYIwMJOT/YdMTd5+YIOmWGlpHzauE+h8CsP+z7S6+8=;\n b=CENhCJFDWrR+I2mE6kOLyn3T2r6wHX7z5JldbRBVa4JrwtdyTuV7OrGU9+5wNGXTf0\n +tSOX4OsqZOd+TZz3c9f2gTAwnEn2Ld1dzXuDwV2OZ4pV50SdfB2nCPB+G09t1xGqrUN\n V3HgEiTTkoPnQ+Ww9woKIKOEEmOxQ58njTbzEsQqzeNBLXjHAhnYWzWbshZbOB8qi4HN\n CpIJlGKf+aUVQwA6HNiQwnV8iilwL9HjfYcLoXZIygldPoh2tFXNQa689HRbOrgTBb0r\n zJZEpwfiEOxhUfGW9IgCOqZAaISU2i2pEU614hYSkRk3wM/MKIeLj5t+Apr/BG1HxABH\n eAaw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1704370945; x=1704975745;\n h=content-transfer-encoding:mime-version:message-id:date:user-agent\n :references:in-reply-to:subject:cc:to:from:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=aVYIwMJOT/YdMTd5+YIOmWGlpHzauE+h8CsP+z7S6+8=;\n b=eQNkDGQVE+fBc5JM4G0jV3b9OTLkNcXvTgtEBJREsD3nXjKvw7ExbIt4I3olOwmFyC\n pRX+bqnHCyPfyP5cuMuc+cAfidhDkz0Z7JbAftMK/hDv5T9/sI0dYjqiIyIELTs+2IRj\n hAF+9IG8r5T0iHdiv5F5AcMLLn+R0syEHXeifyRVeaKi3kANXZHVzLXtTprhEI+cOFG8\n WubHA8/3Pv5ewR0LC7rQLAr34TvNNrfrr9BXxiJYailq1Az82pbA4b0Cr6jHA0H2K70m\n E+84xGFe4onNHaxQhznEVqiOzn+r9AD85r6brxFgPIuhHZu1LG1nzcTNsUTqFZfJFV+Y\n B1Nw==","X-Gm-Message-State":"AOJu0YxNAPkEzLD5jLSAWZLMInPmI5EFLk3jQd++a8eSzl0548/R0Qt1\n CmJGsSG0nL6csE7AJHGuPzdri9txnwmH2A==","X-Google-Smtp-Source":"\n AGHT+IESZ6CAtWXu3+NTv00JGxGiUQOBGWvfU2/hoP3T/WPXVHRBP2upx0cmIZsx1q9LTKnhMAQE1Q==","X-Received":"by 2002:a05:600c:d6:b0:40d:7607:a9a7 with SMTP id\n u22-20020a05600c00d600b0040d7607a9a7mr289270wmm.99.1704370944510;\n Thu, 04 Jan 2024 04:22:24 -0800 (PST)","From":"=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>","To":"Akihiko Odaki <akihiko.odaki@daynix.com>","Cc":"qemu-devel@nongnu.org,  qemu-s390x@nongnu.org,  qemu-ppc@nongnu.org,\n Richard Henderson <richard.henderson@linaro.org>,\n  Song Gao <gaosong@loongson.cn>,\n  =?utf-8?q?Marc-Andr=C3=A9?= Lureau <marcandre.lureau@redhat.com>,\n David Hildenbrand <david@redhat.com>,  Aurelien Jarno <aurelien@aurel32.net>,\n  Yoshinori Sato <ysato@users.sourceforge.jp>,\n Yanan Wang <wangyanan55@huawei.com>,  Bin Meng <bin.meng@windriver.com>,\n Laurent Vivier <lvivier@redhat.com>,  Michael Rolnik <mrolnik@gmail.com>,\n Alexandre Iooss <erdnaxe@crans.org>,  David Woodhouse <dwmw2@infradead.org>,\n  Laurent Vivier <laurent@vivier.eu>,  Paolo Bonzini <pbonzini@redhat.com>,\n  Brian Cain <bcain@quicinc.com>,\n  Daniel Henrique Barboza <danielhb413@gmail.com>,\n  Beraldo Leal <bleal@redhat.com>,  Paul Durrant <paul@xen.org>,\n  Mahmoud Mandour <ma.mandourr@gmail.com>,  Thomas Huth <thuth@redhat.com>,\n  Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Cleber Rosa <crosa@redhat.com>,\n  kvm@vger.kernel.org,  Peter Maydell <peter.maydell@linaro.org>,\n  Wainer dos Santos Moschetta <wainersm@redhat.com>,  qemu-arm@nongnu.org,\n  Weiwei Li <liwei1518@gmail.com>,\n  Philippe =?utf-8?q?Mathieu-Daud=C3=A9?= <philmd@linaro.org>,\n  John Snow <jsnow@redhat.com>,\n  Daniel Henrique Barboza <dbarboza@ventanamicro.com>,\n  Nicholas Piggin <npiggin@gmail.com>,  Palmer Dabbelt <palmer@dabbelt.com>,\n  Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,\n  Ilya Leoshkevich <iii@linux.ibm.com>,\n =?utf-8?q?C=C3=A9dric?= Le Goater <clg@kaod.org>,\n  \"Edgar E. Iglesias\" <edgar.iglesias@gmail.com>,\n  Eduardo Habkost <eduardo@habkost.net>,\n Pierrick Bouvier <pierrick.bouvier@linaro.org>,  qemu-riscv@nongnu.org,\n Alistair Francis <alistair.francis@wdc.com>","Subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","In-Reply-To":"<52cac44e-a467-4748-8c5b-c9c47f5b0f79@daynix.com> (Akihiko\n Odaki's message of \"Thu, 4 Jan 2024 21:05:22 +0900\")","References":"<20240103173349.398526-1-alex.bennee@linaro.org>\n <20240103173349.398526-39-alex.bennee@linaro.org>\n <52cac44e-a467-4748-8c5b-c9c47f5b0f79@daynix.com>","User-Agent":"mu4e 1.11.27; emacs 29.1","Date":"Thu, 04 Jan 2024 12:22:23 +0000","Message-ID":"<87cyuhguf4.fsf@draig.linaro.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","Received-SPF":"pass client-ip=2a00:1450:4864:20::335;\n envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x335.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,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,\n T_SCC_BODY_TEXT_LINE=-0.01 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":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3241612,"web_url":"http://patchwork.ozlabs.org/comment/3241612/","msgid":"<4cf878c6-0adf-4c97-b404-446d8a3dabf1@daynix.com>","list_archive_url":null,"date":"2024-01-04T13:22:39","subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","submitter":{"id":84745,"url":"http://patchwork.ozlabs.org/api/people/84745/","name":"Akihiko Odaki","email":"akihiko.odaki@daynix.com"},"content":"On 2024/01/04 21:22, Alex Bennée wrote:\n> Akihiko Odaki <akihiko.odaki@daynix.com> writes:\n> \n>> On 2024/01/04 2:33, Alex Bennée wrote:\n>>> We can only request a list of registers once the vCPU has been\n>>> initialised so the user needs to use either call the get function on\n>>> vCPU initialisation or during the translation phase.\n>>> We don't expose the reg number to the plugin instead hiding it\n>>> behind\n>>> an opaque handle. This allows for a bit of future proofing should the\n>>> internals need to be changed while also being hashed against the\n>>> CPUClass so we can handle different register sets per-vCPU in\n>>> hetrogenous situations.\n>>> Having an internal state within the plugins also allows us to expand\n>>> the interface in future (for example providing callbacks on register\n>>> change if the translator can track changes).\n>>> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1706\n>>> Cc: Akihiko Odaki <akihiko.odaki@daynix.com>\n>>> Based-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>\n>>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>\n>>> ---\n>>> v3\n>>>     - also g_intern_string the register name\n>>>     - make get_registers documentation a bit less verbose\n>>> v2\n>>>     - use new get whole list api, and expose upwards\n>>> vAJB:\n>>> The main difference to Akikio's version is hiding the gdb register\n>>> detail from the plugin for the reasons described above.\n>>> ---\n>>>    include/qemu/qemu-plugin.h   |  51 +++++++++++++++++-\n>>>    plugins/api.c                | 102 +++++++++++++++++++++++++++++++++++\n>>>    plugins/qemu-plugins.symbols |   2 +\n>>>    3 files changed, 153 insertions(+), 2 deletions(-)\n>>> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h\n>>> index 4daab6efd29..95380895f81 100644\n>>> --- a/include/qemu/qemu-plugin.h\n>>> +++ b/include/qemu/qemu-plugin.h\n>>> @@ -11,6 +11,7 @@\n>>>    #ifndef QEMU_QEMU_PLUGIN_H\n>>>    #define QEMU_QEMU_PLUGIN_H\n>>>    +#include <glib.h>\n>>>    #include <inttypes.h>\n>>>    #include <stdbool.h>\n>>>    #include <stddef.h>\n>>> @@ -227,8 +228,8 @@ struct qemu_plugin_insn;\n>>>     * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n>>>     * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n>>>     *\n>>> - * Note: currently unused, plugins cannot read or change system\n>>> - * register state.\n>>> + * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n>>> + * system register state.\n>>>     */\n>>>    enum qemu_plugin_cb_flags {\n>>>        QEMU_PLUGIN_CB_NO_REGS,\n>>> @@ -708,4 +709,50 @@ uint64_t qemu_plugin_end_code(void);\n>>>    QEMU_PLUGIN_API\n>>>    uint64_t qemu_plugin_entry_code(void);\n>>>    +/** struct qemu_plugin_register - Opaque handle for register\n>>> access */\n>>> +struct qemu_plugin_register;\n>>\n>> Just in case you missed my comment for the earlier version:\n>>\n>> What about identifying a register with an index in an array returned\n>> by qemu_plugin_get_registers(). That saves troubles having the handle\n>> member in qemu_plugin_reg_descriptor.\n> \n> The handle gets de-referenced internally in the plugin api and\n> additional checking could be added there. If we pass an index then we'd\n> end up having to track the index assigned during get_registers as well\n> as account for a potential skew in the index value if the register\n> layout varies between vCPUs (although I admit this is future proofing\n> for potential heterogeneous models).\n> \n> The concept of opaque handle == pointer is fairly common in the QEMU\n> code base. We are not making it hard for a plugin author to bypass this\n> \"protection\", just making it clear if you do so your violating the\n> principle of the API.\n\nNow I get the idea. Indeed index values are not guaranteed to be stable \nacross CPUs.\n\nWhy don't you pass gdb_reg_num as is then? qemu_plugin_register has the \nname member, but it's unused so gdb_reg_num is effectively the only \nmember we need. You can even cast gdb_reg_num to (struct \nqemu_plugin_register *), but I don't think pointers are more opaque or \nfuture-proof than integers.","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=daynix-com.20230601.gappssmtp.com\n header.i=@daynix-com.20230601.gappssmtp.com header.a=rsa-sha256\n header.s=20230601 header.b=1bv073tZ;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4T5S376lPvz1ydb\n\tfor <incoming@patchwork.ozlabs.org>; Fri,  5 Jan 2024 00:23:47 +1100 (AEDT)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1rLNgV-0007Hr-56; Thu, 04 Jan 2024 08:22:55 -0500","from eggs.gnu.org ([2001:470:142:3::10])\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <akihiko.odaki@daynix.com>)\n id 1rLNgS-0007Ge-TQ\n for qemu-devel@nongnu.org; Thu, 04 Jan 2024 08:22:52 -0500","from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <akihiko.odaki@daynix.com>)\n id 1rLNgQ-0006Nl-7T\n for qemu-devel@nongnu.org; Thu, 04 Jan 2024 08:22:52 -0500","by mail-pf1-x435.google.com with SMTP id\n d2e1a72fcca58-6d9e62ff056so345649b3a.1\n for <qemu-devel@nongnu.org>; Thu, 04 Jan 2024 05:22:49 -0800 (PST)","from ?IPV6:2400:4050:a840:1e00:9ac7:6d57:2b16:6932?\n ([2400:4050:a840:1e00:9ac7:6d57:2b16:6932])\n by smtp.gmail.com with ESMTPSA id\n a25-20020a634d19000000b005c6617b52e6sm24378620pgb.5.2024.01.04.05.22.40\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Thu, 04 Jan 2024 05:22:48 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1704374568; x=1704979368;\n darn=nongnu.org;\n h=content-transfer-encoding:in-reply-to:from:references:cc:to\n :content-language:subject:user-agent:mime-version:date:message-id\n :from:to:cc:subject:date:message-id:reply-to;\n bh=+UaUoSEUxULvKOz2jhQuoYpr4rLktnoBGVluoHHYMOU=;\n b=1bv073tZz+lRFULvmgTlv/p2kj//bEO6qfFPNrSt8Aw+wvsExnWycEluG3lr1Y/Taf\n 67KetNaXmQsUnGTbTWBHFe5tr3iZb6xbdFllngNrarqaRLhkA/jVGWfyy4MlTbuGubfL\n ACfDkTArPFcHJ/azShjoMH4hZsTQULnwDcjYaJ4E3up0nRccqSLqKHZO8u9a7/8ZHDYE\n Jf1zlC/jbDFpogsl37LxbK0VRxczrAHpqyRgZoo0sp5f/2C47WQNuLUu8sRzIL6Q8Ert\n z4yKQC2pONUlKr1WjQWRRX7LhGuhSN3r35XsbFQLfnbAy99cRhlW2OzkNJ6bNh0bXLLr\n L3Hw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1704374568; x=1704979368;\n h=content-transfer-encoding:in-reply-to:from:references:cc:to\n :content-language:subject:user-agent:mime-version:date:message-id\n :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;\n bh=+UaUoSEUxULvKOz2jhQuoYpr4rLktnoBGVluoHHYMOU=;\n b=ARwKqs00Hpxk45g0g0JVB2KBzslKWN7rZYwcLTuFAonBm8O8L28IqOHWNybLQfjbMN\n uhbVFtvnLZV/35ZtnDJxvh/ci8kjZnK/fTkVD1qpZDTiSZ2BKd1RvhcNRv0sshQtaTN5\n A2hZADxcrp4uhVZlJtU1NnKCvp4mYyYSZw7TtNrbS2154NoT6laDQlpfq6TaQccD79wd\n H14oSwd6UNB0D6PAIj7HeePKECC10JOcF82Spz3piEh3FwBR9Dlfw5HoCwQHZ9Mppu8g\n hanIY/qWyYpoL1NI3y+mFVvWgXsZ0haI2hSOinUdStmp1Np0LnqMbZ/5IDRBrw1Q/hoi\n 4AzQ==","X-Gm-Message-State":"AOJu0YzOSU08V7eMxZ5hO6p8eHgDDDHxYax3EW4IG2v3SvBpS469Oklg\n uLIZeIWgTo8gmjdJBIVxsDyMVuvGdYaroA==","X-Google-Smtp-Source":"\n AGHT+IFokc8I4M12wpRZbGFseRS03dNmYQJFm1uTthsqLJ9zrTgE2OKJgGw72Fa3MExoY1OmaCb7cA==","X-Received":"by 2002:a05:6a20:1445:b0:199:247a:1044 with SMTP id\n a5-20020a056a20144500b00199247a1044mr201672pzi.17.1704374568632;\n Thu, 04 Jan 2024 05:22:48 -0800 (PST)","Message-ID":"<4cf878c6-0adf-4c97-b404-446d8a3dabf1@daynix.com>","Date":"Thu, 4 Jan 2024 22:22:39 +0900","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","Content-Language":"en-US","To":"=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>","Cc":"qemu-devel@nongnu.org, qemu-s390x@nongnu.org, qemu-ppc@nongnu.org,\n Richard Henderson <richard.henderson@linaro.org>,\n Song Gao <gaosong@loongson.cn>,\n =?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>,\n David Hildenbrand <david@redhat.com>, Aurelien Jarno <aurelien@aurel32.net>,\n Yoshinori Sato <ysato@users.sourceforge.jp>,\n Yanan Wang <wangyanan55@huawei.com>, Bin Meng <bin.meng@windriver.com>,\n Laurent Vivier <lvivier@redhat.com>, Michael Rolnik <mrolnik@gmail.com>,\n Alexandre Iooss <erdnaxe@crans.org>, David Woodhouse <dwmw2@infradead.org>,\n Laurent Vivier <laurent@vivier.eu>, Paolo Bonzini <pbonzini@redhat.com>,\n Brian Cain <bcain@quicinc.com>,\n Daniel Henrique Barboza <danielhb413@gmail.com>,\n Beraldo Leal <bleal@redhat.com>, Paul Durrant <paul@xen.org>,\n Mahmoud Mandour <ma.mandourr@gmail.com>, Thomas Huth <thuth@redhat.com>,\n Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Cleber Rosa <crosa@redhat.com>,\n kvm@vger.kernel.org, Peter Maydell <peter.maydell@linaro.org>,\n Wainer dos Santos Moschetta <wainersm@redhat.com>, qemu-arm@nongnu.org,\n Weiwei Li <liwei1518@gmail.com>,\n =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= <philmd@linaro.org>,\n John Snow <jsnow@redhat.com>,\n Daniel Henrique Barboza <dbarboza@ventanamicro.com>,\n Nicholas Piggin <npiggin@gmail.com>, Palmer Dabbelt <palmer@dabbelt.com>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,\n Ilya Leoshkevich <iii@linux.ibm.com>,\n =?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@kaod.org>,\n \"Edgar E. Iglesias\" <edgar.iglesias@gmail.com>,\n Eduardo Habkost <eduardo@habkost.net>,\n Pierrick Bouvier <pierrick.bouvier@linaro.org>, qemu-riscv@nongnu.org,\n Alistair Francis <alistair.francis@wdc.com>","References":"<20240103173349.398526-1-alex.bennee@linaro.org>\n <20240103173349.398526-39-alex.bennee@linaro.org>\n <52cac44e-a467-4748-8c5b-c9c47f5b0f79@daynix.com>\n <87cyuhguf4.fsf@draig.linaro.org>","From":"Akihiko Odaki <akihiko.odaki@daynix.com>","In-Reply-To":"<87cyuhguf4.fsf@draig.linaro.org>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"8bit","Received-SPF":"none client-ip=2607:f8b0:4864:20::435;\n envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x435.google.com","X-Spam_score_int":"-18","X-Spam_score":"-1.9","X-Spam_bar":"-","X-Spam_report":"(-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,\n SPF_NONE=0.001,\n T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3243894,"web_url":"http://patchwork.ozlabs.org/comment/3243894/","msgid":"<6c22ed26-7906-47a2-ab66-57d545ef59f5@linaro.org>","list_archive_url":null,"date":"2024-01-09T15:05:12","subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","submitter":{"id":85798,"url":"http://patchwork.ozlabs.org/api/people/85798/","name":"Pierrick Bouvier","email":"pierrick.bouvier@linaro.org"},"content":"On 1/3/24 21:33, Alex Bennée wrote:\n> We can only request a list of registers once the vCPU has been\n> initialised so the user needs to use either call the get function on\n> vCPU initialisation or during the translation phase.\n> \n> We don't expose the reg number to the plugin instead hiding it behind\n> an opaque handle. This allows for a bit of future proofing should the\n> internals need to be changed while also being hashed against the\n> CPUClass so we can handle different register sets per-vCPU in\n> hetrogenous situations.\n> \n> Having an internal state within the plugins also allows us to expand\n> the interface in future (for example providing callbacks on register\n> change if the translator can track changes).\n> \n> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1706\n> Cc: Akihiko Odaki <akihiko.odaki@daynix.com>\n> Based-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>\n> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>\n> \n> ---\n> v3\n>    - also g_intern_string the register name\n>    - make get_registers documentation a bit less verbose\n> v2\n>    - use new get whole list api, and expose upwards\n> \n> vAJB:\n> \n> The main difference to Akikio's version is hiding the gdb register\n> detail from the plugin for the reasons described above.\n> ---\n>   include/qemu/qemu-plugin.h   |  51 +++++++++++++++++-\n>   plugins/api.c                | 102 +++++++++++++++++++++++++++++++++++\n>   plugins/qemu-plugins.symbols |   2 +\n>   3 files changed, 153 insertions(+), 2 deletions(-)\n> \n> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h\n> index 4daab6efd29..95380895f81 100644\n> --- a/include/qemu/qemu-plugin.h\n> +++ b/include/qemu/qemu-plugin.h\n> @@ -11,6 +11,7 @@\n>   #ifndef QEMU_QEMU_PLUGIN_H\n>   #define QEMU_QEMU_PLUGIN_H\n>   \n> +#include <glib.h>\n>   #include <inttypes.h>\n>   #include <stdbool.h>\n>   #include <stddef.h>\n> @@ -227,8 +228,8 @@ struct qemu_plugin_insn;\n>    * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n>    * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n>    *\n> - * Note: currently unused, plugins cannot read or change system\n> - * register state.\n> + * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n> + * system register state.\n>    */\n>   enum qemu_plugin_cb_flags {\n>       QEMU_PLUGIN_CB_NO_REGS,\n> @@ -708,4 +709,50 @@ uint64_t qemu_plugin_end_code(void);\n>   QEMU_PLUGIN_API\n>   uint64_t qemu_plugin_entry_code(void);\n>   \n> +/** struct qemu_plugin_register - Opaque handle for register access */\n> +struct qemu_plugin_register;\n> +\n> +/**\n> + * typedef qemu_plugin_reg_descriptor - register descriptions\n> + *\n> + * @handle: opaque handle for retrieving value with qemu_plugin_read_register\n> + * @name: register name\n> + * @feature: optional feature descriptor, can be NULL\n> + */\n> +typedef struct {\n> +    struct qemu_plugin_register *handle;\n> +    const char *name;\n> +    const char *feature;\n> +} qemu_plugin_reg_descriptor;\n> +\n> +/**\n> + * qemu_plugin_get_registers() - return register list for vCPU\n> + * @vcpu_index: vcpu to query\n> + *\n> + * Returns a GArray of qemu_plugin_reg_descriptor or NULL. Caller\n> + * frees the array (but not the const strings).\n> + *\n> + * Should be used from a qemu_plugin_register_vcpu_init_cb() callback\n> + * after the vCPU is initialised.\n> + */\n> +GArray * qemu_plugin_get_registers(unsigned int vcpu_index);\n> +\n> +/**\n> + * qemu_plugin_read_register() - read register\n> + *\n> + * @vcpu: vcpu index\n> + * @handle: a @qemu_plugin_reg_handle handle\n> + * @buf: A GByteArray for the data owned by the plugin\n> + *\n> + * This function is only available in a context that register read access is\n> + * explicitly requested.\n> + *\n> + * Returns the size of the read register. The content of @buf is in target byte\n> + * order. On failure returns -1\n> + */\n> +int qemu_plugin_read_register(unsigned int vcpu,\n> +                              struct qemu_plugin_register *handle,\n> +                              GByteArray *buf);\n> +\n> +\n>   #endif /* QEMU_QEMU_PLUGIN_H */\n> diff --git a/plugins/api.c b/plugins/api.c\n> index ac39cdea0b3..f8905325c43 100644\n> --- a/plugins/api.c\n> +++ b/plugins/api.c\n> @@ -8,6 +8,7 @@\n>    *\n>    *  qemu_plugin_tb\n>    *  qemu_plugin_insn\n> + *  qemu_plugin_register\n>    *\n>    * Which can then be passed back into the API to do additional things.\n>    * As such all the public functions in here are exported in\n> @@ -35,10 +36,12 @@\n>    */\n>   \n>   #include \"qemu/osdep.h\"\n> +#include \"qemu/main-loop.h\"\n>   #include \"qemu/plugin.h\"\n>   #include \"qemu/log.h\"\n>   #include \"tcg/tcg.h\"\n>   #include \"exec/exec-all.h\"\n> +#include \"exec/gdbstub.h\"\n>   #include \"exec/ram_addr.h\"\n>   #include \"disas/disas.h\"\n>   #include \"plugin.h\"\n> @@ -435,3 +438,102 @@ uint64_t qemu_plugin_entry_code(void)\n>   #endif\n>       return entry;\n>   }\n> +\n> +/*\n> + * Register handles\n> + *\n> + * The plugin infrastructure keeps hold of these internal data\n> + * structures which are presented to plugins as opaque handles. They\n> + * are global to the system and therefor additions to the hash table\n> + * must be protected by the @reg_handle_lock.\n> + *\n> + * In order to future proof for up-coming heterogeneous work we want\n> + * different entries for each CPU type while sharing them in the\n> + * common case of multiple cores of the same type.\n> + */\n> +\n> +static QemuMutex reg_handle_lock;\n> +\n> +struct qemu_plugin_register {\n> +    const char *name;\n> +    int gdb_reg_num;\n> +};\n> +\n> +static GHashTable *reg_handles; /* hash table of PluginReg */\n> +\n> +/* Generate a stable key - would xxhash be overkill? */\n> +static gpointer cpu_plus_reg_to_key(CPUState *cs, int gdb_regnum)\n> +{\n> +    uintptr_t key = (uintptr_t) cs->cc;\n> +    key ^= gdb_regnum;\n> +    return GUINT_TO_POINTER(key);\n> +}\n> +\n> +/*\n> + * Create register handles.\n> + *\n> + * We need to create a handle for each register so the plugin\n> + * infrastructure can call gdbstub to read a register. We also\n> + * construct a result array with those handles and some ancillary data\n> + * the plugin might find useful.\n> + */\n> +\n> +static GArray * create_register_handles(CPUState *cs, GArray *gdbstub_regs) {\n> +    GArray *find_data = g_array_new(true, true, sizeof(qemu_plugin_reg_descriptor));\n> +\n> +    WITH_QEMU_LOCK_GUARD(&reg_handle_lock) {\n> +\n> +        if (!reg_handles) {\n> +            reg_handles = g_hash_table_new(g_direct_hash, g_direct_equal);\n> +        }\n> +\n> +        for (int i=0; i < gdbstub_regs->len; i++) {\n> +            GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);\n> +            gpointer key = cpu_plus_reg_to_key(cs, grd->gdb_reg);\n> +            struct qemu_plugin_register *val = g_hash_table_lookup(reg_handles, key);\n> +\n> +            /* Doesn't exist, create one */\n> +            if (!val) {\n> +                val = g_new0(struct qemu_plugin_register, 1);\n> +                val->gdb_reg_num = grd->gdb_reg;\n> +                val->name = g_intern_string(grd->name);\n> +\n> +                g_hash_table_insert(reg_handles, key, val);\n> +            }\n> +\n> +            /* Create a record for the plugin */\n> +            qemu_plugin_reg_descriptor desc = {\n> +                .handle = val,\n> +                .name = val->name,\n> +                .feature = g_intern_string(grd->feature_name)\n> +            };\n> +            g_array_append_val(find_data, desc);\n> +        }\n> +    }\n> +\n> +    return find_data;\n> +}\n> +\n> +GArray * qemu_plugin_get_registers(unsigned int vcpu)\n> +{\n> +    CPUState *cs = qemu_get_cpu(vcpu);\n> +    if (cs) {\n> +        g_autoptr(GArray) regs = gdb_get_register_list(cs);\n> +        return regs->len ? create_register_handles(cs, regs) : NULL;\n> +    } else {\n> +        return NULL;\n> +    }\n> +}\n\nWould that be useful to cache the returned value on plugin runtime side \n(per vcpu)? This way, a plugin could call qemu_plugin_get_registers as \nmany time as it wants without having to pay for the creation of the array.\n\nIn more, could we return a hashtable (indexed by regname string) instead \nof an array?\n\nWith both changes, a plugin could simply perform a lookup in table \nreturned by qemu_plugin_get_registers without having to keep anything on \nits side.\n\n> +\n> +int qemu_plugin_read_register(unsigned int vcpu, struct qemu_plugin_register *reg, GByteArray *buf)\n> +{\n> +    CPUState *cs = qemu_get_cpu(vcpu);\n> +    /* assert with debugging on? */\n> +    return gdb_read_register(cs, buf, reg->gdb_reg_num);\n> +}\n> +\n> +static void __attribute__((__constructor__)) qemu_api_init(void)\n> +{\n> +    qemu_mutex_init(&reg_handle_lock);\n> +\n> +}\n> diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols\n> index 71f6c90549d..6963585c1ea 100644\n> --- a/plugins/qemu-plugins.symbols\n> +++ b/plugins/qemu-plugins.symbols\n> @@ -3,6 +3,7 @@\n>     qemu_plugin_end_code;\n>     qemu_plugin_entry_code;\n>     qemu_plugin_get_hwaddr;\n> +  qemu_plugin_get_registers;\n>     qemu_plugin_hwaddr_device_name;\n>     qemu_plugin_hwaddr_is_io;\n>     qemu_plugin_hwaddr_phys_addr;\n> @@ -20,6 +21,7 @@\n>     qemu_plugin_n_vcpus;\n>     qemu_plugin_outs;\n>     qemu_plugin_path_to_binary;\n> +  qemu_plugin_read_register;\n>     qemu_plugin_register_atexit_cb;\n>     qemu_plugin_register_flush_cb;\n>     qemu_plugin_register_vcpu_exit_cb;","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256\n header.s=google header.b=PkF/ZtDV;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4T8Z4Z4PTCz1yPf\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 10 Jan 2024 02:05:50 +1100 (AEDT)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1rNDfZ-0000A1-Nw; Tue, 09 Jan 2024 10:05:33 -0500","from eggs.gnu.org ([2001:470:142:3::10])\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <pierrick.bouvier@linaro.org>)\n id 1rNDfX-0008Vv-02\n for qemu-devel@nongnu.org; Tue, 09 Jan 2024 10:05:31 -0500","from mail-wr1-x435.google.com ([2a00:1450:4864:20::435])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <pierrick.bouvier@linaro.org>)\n id 1rNDfS-0001fs-NG\n for qemu-devel@nongnu.org; Tue, 09 Jan 2024 10:05:30 -0500","by mail-wr1-x435.google.com with SMTP id\n ffacd0b85a97d-3373a30af67so2901665f8f.0\n for <qemu-devel@nongnu.org>; Tue, 09 Jan 2024 07:05:26 -0800 (PST)","from [192.168.1.24] ([102.35.208.160])\n by smtp.gmail.com with ESMTPSA id\n d7-20020adfe887000000b0033672971fabsm2609637wrm.115.2024.01.09.07.05.14\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Tue, 09 Jan 2024 07:05:24 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=linaro.org; s=google; t=1704812725; x=1705417525; darn=nongnu.org;\n h=content-transfer-encoding:in-reply-to:from:references:cc:to\n :content-language:subject:user-agent:mime-version:date:message-id\n :from:to:cc:subject:date:message-id:reply-to;\n bh=OdLIh7YhTDuZWsjEAOESckJCOOUpBDjEHB+IHlWuUz0=;\n b=PkF/ZtDVcHKZIybcvNQPVEgYO2RVQnhFH1gTD/g3TeNgvd23+hPHd5k5ySA9ij5Mtt\n u2njT0JKl+oLFs4ZNFR4XhL+LT0ePLhW70SmJq8TPCMOcbzqrHNnt8N7L3OGdk8kAni7\n wIrnIklHesQAOwG/vVcF+o7WzJok2aj3W0allCG14Ewp1i7DZiVzfU0QN99W8UOKux3l\n 9p3++oGBbUShBJ0gPK9xJJbfgw39Y/8VpLfsw9YcwXF+TRoFgyd99BZDU7oryumkGC6l\n eeRZ1IN27WlUiLwCcseIEAFue6EXo/j9qyXj7lzzydcF67PKnlPDuNisCaoGt4WySGCW\n XmnA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1704812725; x=1705417525;\n h=content-transfer-encoding:in-reply-to:from:references:cc:to\n :content-language:subject:user-agent:mime-version:date:message-id\n :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;\n bh=OdLIh7YhTDuZWsjEAOESckJCOOUpBDjEHB+IHlWuUz0=;\n b=YHTjpR6E6YnnYFoZWqlK5dks3vy8wV+/F04gMIMMdxy7vuRAGQPbPRSBtOKUcc2uyn\n Dg1117BVZ3y7++E9/iSE5jNhLfZRkaY95VCiVhx+4Uc9Z2n6FeIxt/GvhAXSr4b5Nu4Y\n CUksAQ3vC6OYVKcVaYS/tjGe/idfPpP1rG2JJAN8jKGKQrhpvXMQmYluet0Guvm295/y\n VlhFVcZeS/3EQKXT5KfCBA9rQm82OJcMNjN9m8JrK5X82Rnxd1RYQ+YA/GCVyf9H5ZX0\n R3ULqrhX7mY16JM9SNpP7EXCV3uFxIzAj4ECRJqU7HhYQMpjYkgLGG7uCyoTiCH87QR5\n RQAQ==","X-Gm-Message-State":"AOJu0YyqH3bwxTwcXqU7FHKwwu+iVCLYPHAFZuN1uhAtvuWbhPQ8piMr\n 8sj9PxiPCqqkWZ1D0qafQSc1bCRSbrRvOw==","X-Google-Smtp-Source":"\n AGHT+IGLZvzPXlrjqFLT0I9xL4CCZMxYp9AqmpILXYKgOZeO+8smk9/WWmznsyFpJpAbVXRJ0Ews6Q==","X-Received":"by 2002:a05:6000:400c:b0:336:7b76:2b4a with SMTP id\n cp12-20020a056000400c00b003367b762b4amr552081wrb.181.1704812724926;\n Tue, 09 Jan 2024 07:05:24 -0800 (PST)","Message-ID":"<6c22ed26-7906-47a2-ab66-57d545ef59f5@linaro.org>","Date":"Tue, 9 Jan 2024 19:05:12 +0400","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","Content-Language":"en-US","To":"=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>,\n qemu-devel@nongnu.org","Cc":"qemu-s390x@nongnu.org, qemu-ppc@nongnu.org,\n Richard Henderson <richard.henderson@linaro.org>,\n Song Gao <gaosong@loongson.cn>,\n =?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>,\n David Hildenbrand <david@redhat.com>, Aurelien Jarno <aurelien@aurel32.net>,\n Yoshinori Sato <ysato@users.sourceforge.jp>,\n Yanan Wang <wangyanan55@huawei.com>, Bin Meng <bin.meng@windriver.com>,\n Laurent Vivier <lvivier@redhat.com>, Michael Rolnik <mrolnik@gmail.com>,\n Alexandre Iooss <erdnaxe@crans.org>, David Woodhouse <dwmw2@infradead.org>,\n Laurent Vivier <laurent@vivier.eu>, Paolo Bonzini <pbonzini@redhat.com>,\n Brian Cain <bcain@quicinc.com>,\n Daniel Henrique Barboza <danielhb413@gmail.com>,\n Beraldo Leal <bleal@redhat.com>, Paul Durrant <paul@xen.org>,\n Mahmoud Mandour <ma.mandourr@gmail.com>, Thomas Huth <thuth@redhat.com>,\n Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Cleber Rosa <crosa@redhat.com>,\n kvm@vger.kernel.org, Peter Maydell <peter.maydell@linaro.org>,\n Wainer dos Santos Moschetta <wainersm@redhat.com>, qemu-arm@nongnu.org,\n Weiwei Li <liwei1518@gmail.com>,\n =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= <philmd@linaro.org>,\n John Snow <jsnow@redhat.com>,\n Daniel Henrique Barboza <dbarboza@ventanamicro.com>,\n Nicholas Piggin <npiggin@gmail.com>, Palmer Dabbelt <palmer@dabbelt.com>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,\n Ilya Leoshkevich <iii@linux.ibm.com>,\n =?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@kaod.org>,\n \"Edgar E. Iglesias\" <edgar.iglesias@gmail.com>,\n Eduardo Habkost <eduardo@habkost.net>, qemu-riscv@nongnu.org,\n Alistair Francis <alistair.francis@wdc.com>,\n Akihiko Odaki <akihiko.odaki@daynix.com>","References":"<20240103173349.398526-1-alex.bennee@linaro.org>\n <20240103173349.398526-39-alex.bennee@linaro.org>","From":"Pierrick Bouvier <pierrick.bouvier@linaro.org>","In-Reply-To":"<20240103173349.398526-39-alex.bennee@linaro.org>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"base64","Received-SPF":"pass client-ip=2a00:1450:4864:20::435;\n envelope-from=pierrick.bouvier@linaro.org; helo=mail-wr1-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,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,\n T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3245323,"web_url":"http://patchwork.ozlabs.org/comment/3245323/","msgid":"<87il40f3qg.fsf@draig.linaro.org>","list_archive_url":null,"date":"2024-01-11T12:34:31","subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","submitter":{"id":39532,"url":"http://patchwork.ozlabs.org/api/people/39532/","name":"Alex Bennée","email":"alex.bennee@linaro.org"},"content":"Pierrick Bouvier <pierrick.bouvier@linaro.org> writes:\n\n> On 1/3/24 21:33, Alex Bennée wrote:\n>> We can only request a list of registers once the vCPU has been\n>> initialised so the user needs to use either call the get function on\n>> vCPU initialisation or during the translation phase.\n>> We don't expose the reg number to the plugin instead hiding it\n>> behind\n>> an opaque handle. This allows for a bit of future proofing should the\n>> internals need to be changed while also being hashed against the\n>> CPUClass so we can handle different register sets per-vCPU in\n>> hetrogenous situations.\n>> Having an internal state within the plugins also allows us to expand\n>> the interface in future (for example providing callbacks on register\n>> change if the translator can track changes).\n>> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1706\n>> Cc: Akihiko Odaki <akihiko.odaki@daynix.com>\n>> Based-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>\n>> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>\n>> ---\n>> v3\n>>    - also g_intern_string the register name\n>>    - make get_registers documentation a bit less verbose\n>> v2\n>>    - use new get whole list api, and expose upwards\n>> vAJB:\n>> The main difference to Akikio's version is hiding the gdb register\n>> detail from the plugin for the reasons described above.\n>> ---\n>>   include/qemu/qemu-plugin.h   |  51 +++++++++++++++++-\n>>   plugins/api.c                | 102 +++++++++++++++++++++++++++++++++++\n>>   plugins/qemu-plugins.symbols |   2 +\n>>   3 files changed, 153 insertions(+), 2 deletions(-)\n>> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h\n>> index 4daab6efd29..95380895f81 100644\n>> --- a/include/qemu/qemu-plugin.h\n>> +++ b/include/qemu/qemu-plugin.h\n>> @@ -11,6 +11,7 @@\n>>   #ifndef QEMU_QEMU_PLUGIN_H\n>>   #define QEMU_QEMU_PLUGIN_H\n>>   +#include <glib.h>\n>>   #include <inttypes.h>\n>>   #include <stdbool.h>\n>>   #include <stddef.h>\n>> @@ -227,8 +228,8 @@ struct qemu_plugin_insn;\n>>    * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n>>    * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n>>    *\n>> - * Note: currently unused, plugins cannot read or change system\n>> - * register state.\n>> + * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n>> + * system register state.\n>>    */\n>>   enum qemu_plugin_cb_flags {\n>>       QEMU_PLUGIN_CB_NO_REGS,\n>> @@ -708,4 +709,50 @@ uint64_t qemu_plugin_end_code(void);\n>>   QEMU_PLUGIN_API\n>>   uint64_t qemu_plugin_entry_code(void);\n>>   +/** struct qemu_plugin_register - Opaque handle for register\n>> access */\n>> +struct qemu_plugin_register;\n>> +\n>> +/**\n>> + * typedef qemu_plugin_reg_descriptor - register descriptions\n>> + *\n>> + * @handle: opaque handle for retrieving value with qemu_plugin_read_register\n>> + * @name: register name\n>> + * @feature: optional feature descriptor, can be NULL\n>> + */\n>> +typedef struct {\n>> +    struct qemu_plugin_register *handle;\n>> +    const char *name;\n>> +    const char *feature;\n>> +} qemu_plugin_reg_descriptor;\n>> +\n>> +/**\n>> + * qemu_plugin_get_registers() - return register list for vCPU\n>> + * @vcpu_index: vcpu to query\n>> + *\n>> + * Returns a GArray of qemu_plugin_reg_descriptor or NULL. Caller\n>> + * frees the array (but not the const strings).\n>> + *\n>> + * Should be used from a qemu_plugin_register_vcpu_init_cb() callback\n>> + * after the vCPU is initialised.\n>> + */\n>> +GArray * qemu_plugin_get_registers(unsigned int vcpu_index);\n>> +\n>> +/**\n>> + * qemu_plugin_read_register() - read register\n>> + *\n>> + * @vcpu: vcpu index\n>> + * @handle: a @qemu_plugin_reg_handle handle\n>> + * @buf: A GByteArray for the data owned by the plugin\n>> + *\n>> + * This function is only available in a context that register read access is\n>> + * explicitly requested.\n>> + *\n>> + * Returns the size of the read register. The content of @buf is in target byte\n>> + * order. On failure returns -1\n>> + */\n>> +int qemu_plugin_read_register(unsigned int vcpu,\n>> +                              struct qemu_plugin_register *handle,\n>> +                              GByteArray *buf);\n>> +\n>> +\n>>   #endif /* QEMU_QEMU_PLUGIN_H */\n>> diff --git a/plugins/api.c b/plugins/api.c\n>> index ac39cdea0b3..f8905325c43 100644\n>> --- a/plugins/api.c\n>> +++ b/plugins/api.c\n>> @@ -8,6 +8,7 @@\n>>    *\n>>    *  qemu_plugin_tb\n>>    *  qemu_plugin_insn\n>> + *  qemu_plugin_register\n>>    *\n>>    * Which can then be passed back into the API to do additional things.\n>>    * As such all the public functions in here are exported in\n>> @@ -35,10 +36,12 @@\n>>    */\n>>     #include \"qemu/osdep.h\"\n>> +#include \"qemu/main-loop.h\"\n>>   #include \"qemu/plugin.h\"\n>>   #include \"qemu/log.h\"\n>>   #include \"tcg/tcg.h\"\n>>   #include \"exec/exec-all.h\"\n>> +#include \"exec/gdbstub.h\"\n>>   #include \"exec/ram_addr.h\"\n>>   #include \"disas/disas.h\"\n>>   #include \"plugin.h\"\n>> @@ -435,3 +438,102 @@ uint64_t qemu_plugin_entry_code(void)\n>>   #endif\n>>       return entry;\n>>   }\n>> +\n>> +/*\n>> + * Register handles\n>> + *\n>> + * The plugin infrastructure keeps hold of these internal data\n>> + * structures which are presented to plugins as opaque handles. They\n>> + * are global to the system and therefor additions to the hash table\n>> + * must be protected by the @reg_handle_lock.\n>> + *\n>> + * In order to future proof for up-coming heterogeneous work we want\n>> + * different entries for each CPU type while sharing them in the\n>> + * common case of multiple cores of the same type.\n>> + */\n>> +\n>> +static QemuMutex reg_handle_lock;\n>> +\n>> +struct qemu_plugin_register {\n>> +    const char *name;\n>> +    int gdb_reg_num;\n>> +};\n>> +\n>> +static GHashTable *reg_handles; /* hash table of PluginReg */\n>> +\n>> +/* Generate a stable key - would xxhash be overkill? */\n>> +static gpointer cpu_plus_reg_to_key(CPUState *cs, int gdb_regnum)\n>> +{\n>> +    uintptr_t key = (uintptr_t) cs->cc;\n>> +    key ^= gdb_regnum;\n>> +    return GUINT_TO_POINTER(key);\n>> +}\n>> +\n>> +/*\n>> + * Create register handles.\n>> + *\n>> + * We need to create a handle for each register so the plugin\n>> + * infrastructure can call gdbstub to read a register. We also\n>> + * construct a result array with those handles and some ancillary data\n>> + * the plugin might find useful.\n>> + */\n>> +\n>> +static GArray * create_register_handles(CPUState *cs, GArray *gdbstub_regs) {\n>> +    GArray *find_data = g_array_new(true, true, sizeof(qemu_plugin_reg_descriptor));\n>> +\n>> +    WITH_QEMU_LOCK_GUARD(&reg_handle_lock) {\n>> +\n>> +        if (!reg_handles) {\n>> +            reg_handles = g_hash_table_new(g_direct_hash, g_direct_equal);\n>> +        }\n>> +\n>> +        for (int i=0; i < gdbstub_regs->len; i++) {\n>> +            GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);\n>> +            gpointer key = cpu_plus_reg_to_key(cs, grd->gdb_reg);\n>> +            struct qemu_plugin_register *val = g_hash_table_lookup(reg_handles, key);\n>> +\n>> +            /* Doesn't exist, create one */\n>> +            if (!val) {\n>> +                val = g_new0(struct qemu_plugin_register, 1);\n>> +                val->gdb_reg_num = grd->gdb_reg;\n>> +                val->name = g_intern_string(grd->name);\n>> +\n>> +                g_hash_table_insert(reg_handles, key, val);\n>> +            }\n>> +\n>> +            /* Create a record for the plugin */\n>> +            qemu_plugin_reg_descriptor desc = {\n>> +                .handle = val,\n>> +                .name = val->name,\n>> +                .feature = g_intern_string(grd->feature_name)\n>> +            };\n>> +            g_array_append_val(find_data, desc);\n>> +        }\n>> +    }\n>> +\n>> +    return find_data;\n>> +}\n>> +\n>> +GArray * qemu_plugin_get_registers(unsigned int vcpu)\n>> +{\n>> +    CPUState *cs = qemu_get_cpu(vcpu);\n>> +    if (cs) {\n>> +        g_autoptr(GArray) regs = gdb_get_register_list(cs);\n>> +        return regs->len ? create_register_handles(cs, regs) : NULL;\n>> +    } else {\n>> +        return NULL;\n>> +    }\n>> +}\n>\n> Would that be useful to cache the returned value on plugin runtime\n> side (per vcpu)? This way, a plugin could call\n> qemu_plugin_get_registers as many time as it wants without having to\n> pay for the creation of the array.\n\nI suspect plugins would still want to work out what it needed ahead of\ntime and the cpu list should be static from vcpu_init onwards.\n\n> In more, could we return a hashtable (indexed by regname string)\n> instead of an array?\n\nHmm maybe. Arrays do benefit from being easy to cleanup and we would\nstill need an internal hash table to track stuff we don't want to expose\nto the plugin.\n\nWe can always change it later if there is a compelling use case.\n\n> With both changes, a plugin could simply perform a lookup in table\n> returned by qemu_plugin_get_registers without having to keep anything\n> on its side.\n>\n>> +\n>> +int qemu_plugin_read_register(unsigned int vcpu, struct qemu_plugin_register *reg, GByteArray *buf)\n>> +{\n>> +    CPUState *cs = qemu_get_cpu(vcpu);\n>> +    /* assert with debugging on? */\n>> +    return gdb_read_register(cs, buf, reg->gdb_reg_num);\n>> +}\n>> +\n>> +static void __attribute__((__constructor__)) qemu_api_init(void)\n>> +{\n>> +    qemu_mutex_init(&reg_handle_lock);\n>> +\n>> +}\n>> diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols\n>> index 71f6c90549d..6963585c1ea 100644\n>> --- a/plugins/qemu-plugins.symbols\n>> +++ b/plugins/qemu-plugins.symbols\n>> @@ -3,6 +3,7 @@\n>>     qemu_plugin_end_code;\n>>     qemu_plugin_entry_code;\n>>     qemu_plugin_get_hwaddr;\n>> +  qemu_plugin_get_registers;\n>>     qemu_plugin_hwaddr_device_name;\n>>     qemu_plugin_hwaddr_is_io;\n>>     qemu_plugin_hwaddr_phys_addr;\n>> @@ -20,6 +21,7 @@\n>>     qemu_plugin_n_vcpus;\n>>     qemu_plugin_outs;\n>>     qemu_plugin_path_to_binary;\n>> +  qemu_plugin_read_register;\n>>     qemu_plugin_register_atexit_cb;\n>>     qemu_plugin_register_flush_cb;\n>>     qemu_plugin_register_vcpu_exit_cb;","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256\n header.s=google header.b=CIKqtRhY;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4T9kdt1w71z1yPm\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 11 Jan 2024 23:35:14 +1100 (AEDT)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1rNuGw-0005Ps-6p; Thu, 11 Jan 2024 07:34:58 -0500","from eggs.gnu.org ([2001:470:142:3::10])\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <alex.bennee@linaro.org>)\n id 1rNuGe-0005Iz-EF\n for qemu-devel@nongnu.org; Thu, 11 Jan 2024 07:34:49 -0500","from mail-wm1-x332.google.com ([2a00:1450:4864:20::332])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <alex.bennee@linaro.org>)\n id 1rNuGa-0006JS-Cw\n for qemu-devel@nongnu.org; Thu, 11 Jan 2024 07:34:40 -0500","by mail-wm1-x332.google.com with SMTP id\n 5b1f17b1804b1-40e6297a00fso399215e9.3\n for <qemu-devel@nongnu.org>; Thu, 11 Jan 2024 04:34:33 -0800 (PST)","from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id\n x9-20020adfec09000000b00336b8461a5esm1126877wrn.88.2024.01.11.04.34.31\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Thu, 11 Jan 2024 04:34:31 -0800 (PST)","from draig (localhost [IPv6:::1])\n by draig.lan (Postfix) with ESMTP id 4752F5F7AD;\n Thu, 11 Jan 2024 12:34:31 +0000 (GMT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=linaro.org; s=google; t=1704976472; x=1705581272; darn=nongnu.org;\n h=content-transfer-encoding:mime-version:message-id:date:user-agent\n :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=Laj5dgyw/ZFfRFO13kyKlguGgpT3a+jIHEPtaFCGeuY=;\n b=CIKqtRhYUxf0ar8m2qkZooMjiLl3pcsBtA80qZzzc/rq6KSJm66mzlBEIexjIKglow\n Sx3fjF2KOcS3HcCN0vtIHXe5G4/BLK/tQLdl8SYtZ+q4AsAA5mRzm9XmMunzXK+ZZWm2\n SteeefFzcjqXDbLaA7OgFDp4oRmQJFYg/lT4Ss86g6Kq422zWVgAgVm14JbvBdUgq16T\n W3NzTeIuP6Ai3Z3iD1u6jlxy198rrZh32tMHSlPP4ZqveBiILlN6GKbB4RiAFT5Xp7w7\n +my/f9Ocn19u6/gJHBJTpUTNylVEg1D2x5pF0zK/+DbZ6NvFaGu1RaOog7G0mP6sODqh\n DSXg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1704976472; x=1705581272;\n h=content-transfer-encoding:mime-version:message-id:date:user-agent\n :references:in-reply-to:subject:cc:to:from:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=Laj5dgyw/ZFfRFO13kyKlguGgpT3a+jIHEPtaFCGeuY=;\n b=TuD2GlPd/POM1fHYeaq6LvG/Z+eU8MT/ZfNj6eXaENPhhm6NXnrsAl4aQ+LdtnjTlk\n 0ef+MdKKZecp7RWBYqv3gZTXoq5vlCS0e/ihnLoGPnHljWHLScRhMUvrbOpV9jS5OzYO\n Cs389JzSEFfcrJOoE0/ak/3xNL9R2VqQ4+DHK0aGIJxGJ3rYAek8KbV7FWROzWkhx+hA\n 9l0Fu5Q+rE+DQrShu8jiLQCqLNZDIUrr3kl7TR96qOgEw5IPppsGSAyPtoroAYuaI/5V\n qGzBqnSZflufNpYuuQnK7OsNqQ+KTXRSb/Ec+tt5LmAFVuc3mSqFx7V22/J0W0Pf9yc3\n kAPQ==","X-Gm-Message-State":"AOJu0Yy2MX0hy1zqzsUH3GASBml3dHLVlJhXBhKjc1kgqcdpLa7C6a1V\n lY5RevfnXXkXIDQnOdJgrMuSSltTKmRyzg==","X-Google-Smtp-Source":"\n AGHT+IHCqvU083WGqGcVqRzQLj+PUoocXsxh/YK9sBx/8GpEXqP1xY2y/M7R8Oui7TCF0nRIj3X61w==","X-Received":"by 2002:a7b:c8c8:0:b0:40e:540a:aefb with SMTP id\n f8-20020a7bc8c8000000b0040e540aaefbmr274151wml.26.1704976472020;\n Thu, 11 Jan 2024 04:34:32 -0800 (PST)","From":"=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>","To":"Pierrick Bouvier <pierrick.bouvier@linaro.org>","Cc":"qemu-devel@nongnu.org,  qemu-s390x@nongnu.org,  qemu-ppc@nongnu.org,\n Richard Henderson <richard.henderson@linaro.org>,\n  Song Gao <gaosong@loongson.cn>,\n  =?utf-8?q?Marc-Andr=C3=A9?= Lureau <marcandre.lureau@redhat.com>,\n David Hildenbrand <david@redhat.com>,  Aurelien Jarno <aurelien@aurel32.net>,\n  Yoshinori Sato <ysato@users.sourceforge.jp>,\n Yanan Wang <wangyanan55@huawei.com>,  Bin Meng <bin.meng@windriver.com>,\n Laurent Vivier <lvivier@redhat.com>,  Michael Rolnik <mrolnik@gmail.com>,\n Alexandre Iooss <erdnaxe@crans.org>,  David Woodhouse <dwmw2@infradead.org>,\n  Laurent Vivier <laurent@vivier.eu>,  Paolo Bonzini <pbonzini@redhat.com>,\n  Brian Cain <bcain@quicinc.com>,\n  Daniel Henrique Barboza <danielhb413@gmail.com>,\n  Beraldo Leal <bleal@redhat.com>,  Paul Durrant <paul@xen.org>,\n  Mahmoud Mandour <ma.mandourr@gmail.com>,  Thomas Huth <thuth@redhat.com>,\n  Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Cleber Rosa <crosa@redhat.com>,\n  kvm@vger.kernel.org,  Peter Maydell <peter.maydell@linaro.org>,\n  Wainer dos Santos Moschetta <wainersm@redhat.com>,  qemu-arm@nongnu.org,\n  Weiwei Li <liwei1518@gmail.com>,\n  Philippe =?utf-8?q?Mathieu-Daud=C3=A9?= <philmd@linaro.org>,\n  John Snow <jsnow@redhat.com>,\n  Daniel Henrique Barboza <dbarboza@ventanamicro.com>,\n  Nicholas Piggin <npiggin@gmail.com>,  Palmer Dabbelt <palmer@dabbelt.com>,\n  Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,\n  Ilya Leoshkevich <iii@linux.ibm.com>,\n =?utf-8?q?C=C3=A9dric?= Le Goater <clg@kaod.org>,\n  \"Edgar E. Iglesias\" <edgar.iglesias@gmail.com>,\n  Eduardo Habkost <eduardo@habkost.net>, qemu-riscv@nongnu.org,\n  Alistair Francis <alistair.francis@wdc.com>,\n Akihiko Odaki <akihiko.odaki@daynix.com>","Subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","In-Reply-To":"<6c22ed26-7906-47a2-ab66-57d545ef59f5@linaro.org> (Pierrick\n Bouvier's message of \"Tue, 9 Jan 2024 19:05:12 +0400\")","References":"<20240103173349.398526-1-alex.bennee@linaro.org>\n <20240103173349.398526-39-alex.bennee@linaro.org>\n <6c22ed26-7906-47a2-ab66-57d545ef59f5@linaro.org>","User-Agent":"mu4e 1.11.27; emacs 29.1","Date":"Thu, 11 Jan 2024 12:34:31 +0000","Message-ID":"<87il40f3qg.fsf@draig.linaro.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","Received-SPF":"pass client-ip=2a00:1450:4864:20::332;\n envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x332.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,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,\n T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}},{"id":3245718,"web_url":"http://patchwork.ozlabs.org/comment/3245718/","msgid":"<1a8ddba6-cbe5-4db4-b1bc-71f96bd498e2@linaro.org>","list_archive_url":null,"date":"2024-01-12T03:49:07","subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","submitter":{"id":85798,"url":"http://patchwork.ozlabs.org/api/people/85798/","name":"Pierrick Bouvier","email":"pierrick.bouvier@linaro.org"},"content":"On 1/3/24 21:33, Alex Bennée wrote:\n> We can only request a list of registers once the vCPU has been\n> initialised so the user needs to use either call the get function on\n> vCPU initialisation or during the translation phase.\n> \n> We don't expose the reg number to the plugin instead hiding it behind\n> an opaque handle. This allows for a bit of future proofing should the\n> internals need to be changed while also being hashed against the\n> CPUClass so we can handle different register sets per-vCPU in\n> hetrogenous situations.\n> \n> Having an internal state within the plugins also allows us to expand\n> the interface in future (for example providing callbacks on register\n> change if the translator can track changes).\n> \n> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1706\n> Cc: Akihiko Odaki <akihiko.odaki@daynix.com>\n> Based-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>\n> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>\n> \n> ---\n> v3\n>    - also g_intern_string the register name\n>    - make get_registers documentation a bit less verbose\n> v2\n>    - use new get whole list api, and expose upwards\n> \n> vAJB:\n> \n> The main difference to Akikio's version is hiding the gdb register\n> detail from the plugin for the reasons described above.\n> ---\n>   include/qemu/qemu-plugin.h   |  51 +++++++++++++++++-\n>   plugins/api.c                | 102 +++++++++++++++++++++++++++++++++++\n>   plugins/qemu-plugins.symbols |   2 +\n>   3 files changed, 153 insertions(+), 2 deletions(-)\n> \n> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h\n> index 4daab6efd29..95380895f81 100644\n> --- a/include/qemu/qemu-plugin.h\n> +++ b/include/qemu/qemu-plugin.h\n> @@ -11,6 +11,7 @@\n>   #ifndef QEMU_QEMU_PLUGIN_H\n>   #define QEMU_QEMU_PLUGIN_H\n>   \n> +#include <glib.h>\n>   #include <inttypes.h>\n>   #include <stdbool.h>\n>   #include <stddef.h>\n> @@ -227,8 +228,8 @@ struct qemu_plugin_insn;\n>    * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs\n>    * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs\n>    *\n> - * Note: currently unused, plugins cannot read or change system\n> - * register state.\n> + * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change\n> + * system register state.\n>    */\n>   enum qemu_plugin_cb_flags {\n>       QEMU_PLUGIN_CB_NO_REGS,\n> @@ -708,4 +709,50 @@ uint64_t qemu_plugin_end_code(void);\n>   QEMU_PLUGIN_API\n>   uint64_t qemu_plugin_entry_code(void);\n>   \n> +/** struct qemu_plugin_register - Opaque handle for register access */\n> +struct qemu_plugin_register;\n> +\n> +/**\n> + * typedef qemu_plugin_reg_descriptor - register descriptions\n> + *\n> + * @handle: opaque handle for retrieving value with qemu_plugin_read_register\n> + * @name: register name\n> + * @feature: optional feature descriptor, can be NULL\n> + */\n> +typedef struct {\n> +    struct qemu_plugin_register *handle;\n> +    const char *name;\n> +    const char *feature;\n> +} qemu_plugin_reg_descriptor;\n> +\n> +/**\n> + * qemu_plugin_get_registers() - return register list for vCPU\n> + * @vcpu_index: vcpu to query\n> + *\n> + * Returns a GArray of qemu_plugin_reg_descriptor or NULL. Caller\n> + * frees the array (but not the const strings).\n> + *\n> + * Should be used from a qemu_plugin_register_vcpu_init_cb() callback\n> + * after the vCPU is initialised.\n> + */\n> +GArray * qemu_plugin_get_registers(unsigned int vcpu_index);\n> +\n> +/**\n> + * qemu_plugin_read_register() - read register\n> + *\n> + * @vcpu: vcpu index\n> + * @handle: a @qemu_plugin_reg_handle handle\n> + * @buf: A GByteArray for the data owned by the plugin\n> + *\n> + * This function is only available in a context that register read access is\n> + * explicitly requested.\n> + *\n> + * Returns the size of the read register. The content of @buf is in target byte\n> + * order. On failure returns -1\n> + */\n> +int qemu_plugin_read_register(unsigned int vcpu,\n> +                              struct qemu_plugin_register *handle,\n> +                              GByteArray *buf);\n> +\n> +\n>   #endif /* QEMU_QEMU_PLUGIN_H */\n> diff --git a/plugins/api.c b/plugins/api.c\n> index ac39cdea0b3..f8905325c43 100644\n> --- a/plugins/api.c\n> +++ b/plugins/api.c\n> @@ -8,6 +8,7 @@\n>    *\n>    *  qemu_plugin_tb\n>    *  qemu_plugin_insn\n> + *  qemu_plugin_register\n>    *\n>    * Which can then be passed back into the API to do additional things.\n>    * As such all the public functions in here are exported in\n> @@ -35,10 +36,12 @@\n>    */\n>   \n>   #include \"qemu/osdep.h\"\n> +#include \"qemu/main-loop.h\"\n>   #include \"qemu/plugin.h\"\n>   #include \"qemu/log.h\"\n>   #include \"tcg/tcg.h\"\n>   #include \"exec/exec-all.h\"\n> +#include \"exec/gdbstub.h\"\n>   #include \"exec/ram_addr.h\"\n>   #include \"disas/disas.h\"\n>   #include \"plugin.h\"\n> @@ -435,3 +438,102 @@ uint64_t qemu_plugin_entry_code(void)\n>   #endif\n>       return entry;\n>   }\n> +\n> +/*\n> + * Register handles\n> + *\n> + * The plugin infrastructure keeps hold of these internal data\n> + * structures which are presented to plugins as opaque handles. They\n> + * are global to the system and therefor additions to the hash table\n> + * must be protected by the @reg_handle_lock.\n> + *\n> + * In order to future proof for up-coming heterogeneous work we want\n> + * different entries for each CPU type while sharing them in the\n> + * common case of multiple cores of the same type.\n> + */\n> +\n> +static QemuMutex reg_handle_lock;\n> +\n> +struct qemu_plugin_register {\n> +    const char *name;\n> +    int gdb_reg_num;\n> +};\n> +\n> +static GHashTable *reg_handles; /* hash table of PluginReg */\n> +\n> +/* Generate a stable key - would xxhash be overkill? */\n> +static gpointer cpu_plus_reg_to_key(CPUState *cs, int gdb_regnum)\n> +{\n> +    uintptr_t key = (uintptr_t) cs->cc;\n> +    key ^= gdb_regnum;\n> +    return GUINT_TO_POINTER(key);\n> +}\n> +\n> +/*\n> + * Create register handles.\n> + *\n> + * We need to create a handle for each register so the plugin\n> + * infrastructure can call gdbstub to read a register. We also\n> + * construct a result array with those handles and some ancillary data\n> + * the plugin might find useful.\n> + */\n> +\n> +static GArray * create_register_handles(CPUState *cs, GArray *gdbstub_regs) {\n> +    GArray *find_data = g_array_new(true, true, sizeof(qemu_plugin_reg_descriptor));\n> +\n> +    WITH_QEMU_LOCK_GUARD(&reg_handle_lock) {\n> +\n> +        if (!reg_handles) {\n> +            reg_handles = g_hash_table_new(g_direct_hash, g_direct_equal);\n> +        }\n> +\n> +        for (int i=0; i < gdbstub_regs->len; i++) {\n> +            GDBRegDesc *grd = &g_array_index(gdbstub_regs, GDBRegDesc, i);\n> +            gpointer key = cpu_plus_reg_to_key(cs, grd->gdb_reg);\n> +            struct qemu_plugin_register *val = g_hash_table_lookup(reg_handles, key);\n> +\n> +            /* Doesn't exist, create one */\n> +            if (!val) {\n> +                val = g_new0(struct qemu_plugin_register, 1);\n> +                val->gdb_reg_num = grd->gdb_reg;\n> +                val->name = g_intern_string(grd->name);\n> +\n> +                g_hash_table_insert(reg_handles, key, val);\n> +            }\n> +\n> +            /* Create a record for the plugin */\n> +            qemu_plugin_reg_descriptor desc = {\n> +                .handle = val,\n> +                .name = val->name,\n> +                .feature = g_intern_string(grd->feature_name)\n> +            };\n> +            g_array_append_val(find_data, desc);\n> +        }\n> +    }\n> +\n> +    return find_data;\n> +}\n> +\n> +GArray * qemu_plugin_get_registers(unsigned int vcpu)\n> +{\n> +    CPUState *cs = qemu_get_cpu(vcpu);\n> +    if (cs) {\n> +        g_autoptr(GArray) regs = gdb_get_register_list(cs);\n> +        return regs->len ? create_register_handles(cs, regs) : NULL;\n> +    } else {\n> +        return NULL;\n> +    }\n> +}\n> +\n> +int qemu_plugin_read_register(unsigned int vcpu, struct qemu_plugin_register *reg, GByteArray *buf)\n> +{\n> +    CPUState *cs = qemu_get_cpu(vcpu);\n> +    /* assert with debugging on? */\n> +    return gdb_read_register(cs, buf, reg->gdb_reg_num);\n> +}\n> +\n> +static void __attribute__((__constructor__)) qemu_api_init(void)\n> +{\n> +    qemu_mutex_init(&reg_handle_lock);\n> +\n> +}\n> diff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols\n> index 71f6c90549d..6963585c1ea 100644\n> --- a/plugins/qemu-plugins.symbols\n> +++ b/plugins/qemu-plugins.symbols\n> @@ -3,6 +3,7 @@\n>     qemu_plugin_end_code;\n>     qemu_plugin_entry_code;\n>     qemu_plugin_get_hwaddr;\n> +  qemu_plugin_get_registers;\n>     qemu_plugin_hwaddr_device_name;\n>     qemu_plugin_hwaddr_is_io;\n>     qemu_plugin_hwaddr_phys_addr;\n> @@ -20,6 +21,7 @@\n>     qemu_plugin_n_vcpus;\n>     qemu_plugin_outs;\n>     qemu_plugin_path_to_binary;\n> +  qemu_plugin_read_register;\n>     qemu_plugin_register_atexit_cb;\n>     qemu_plugin_register_flush_cb;\n>     qemu_plugin_register_vcpu_exit_cb;\n\nReviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>","headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256\n header.s=google header.b=oI8KaQtS;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4TB6xs4Gbgz1yPy\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 12 Jan 2024 14:50:25 +1100 (AEDT)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1rO8Y5-0006wo-HF; Thu, 11 Jan 2024 22:49:37 -0500","from eggs.gnu.org ([2001:470:142:3::10])\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <pierrick.bouvier@linaro.org>)\n id 1rO8Y4-0006wZ-Qk\n for qemu-devel@nongnu.org; Thu, 11 Jan 2024 22:49:36 -0500","from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <pierrick.bouvier@linaro.org>)\n id 1rO8Xz-0002QX-Ed\n for qemu-devel@nongnu.org; Thu, 11 Jan 2024 22:49:35 -0500","by mail-wr1-x42b.google.com with SMTP id\n ffacd0b85a97d-3368ac0f74dso4098738f8f.0\n for <qemu-devel@nongnu.org>; Thu, 11 Jan 2024 19:49:22 -0800 (PST)","from [192.168.1.24] ([102.35.208.160])\n by smtp.gmail.com with ESMTPSA id\n bk14-20020a0560001d8e00b003375c072fbcsm143193wrb.100.2024.01.11.19.49.10\n (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n Thu, 11 Jan 2024 19:49:20 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=linaro.org; s=google; t=1705031361; x=1705636161; darn=nongnu.org;\n h=content-transfer-encoding:in-reply-to:from:content-language\n :references:cc:to:subject:user-agent:mime-version:date:message-id\n :from:to:cc:subject:date:message-id:reply-to;\n bh=bnLMr6T9vQtB9zohSG+Ww3f+r9f9DHbIMkNIc6agMA4=;\n b=oI8KaQtS5ogDX6VCZRF+Qkq622Ui+FB5Z1oiHqnC5lws6AKMks5SL6RvvNSCGNBMLd\n lAAulvxz1vLehvCAL+UdxNXkR0sSO8MKnzUFe6Tmbd2OSAwyfmG0puViNX6lF16hqvty\n t/HDR1+VmFrt6ErFi4n2c+VLmtbUNshVdXvZAlMK+BzWLBoDPG5hvAI0AshaEQsvR3UE\n lHuP0LgakuEA/1IFW8Ralr0mhviajWrhWjoAnTarOq/Iu26ssyG72BP3pMx+oDw5/AE+\n p4jNg96a2duKlOhQshJagUGu0sEEsDFdEljwjKy4+pbWFLBJ3DTWVY/VyCdDAB8wfQXI\n zhUQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1705031361; x=1705636161;\n h=content-transfer-encoding:in-reply-to:from:content-language\n :references:cc:to:subject:user-agent:mime-version:date:message-id\n :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;\n bh=bnLMr6T9vQtB9zohSG+Ww3f+r9f9DHbIMkNIc6agMA4=;\n b=JpfkkjNkss+uF5qSKpcGrmWsupHWs2lmLKVwxZ/1JFynRKvzNpSEtcFgQc2NUdOUel\n 9fJtkwJX7UjVw3jyzzgqL3GW+oQPZWVk0YJHAQTmKlVxlJu+czV0x48XEi4V49L/Uk+u\n 5l1jWThGXDpgRlH8s8Ev1Nq4G46+QTQp83vRPW1+/rjvkcMUNlXrXA59cb1ieGZ2+1Un\n zl/kmYWVGRflT2JCRN5ttVYvE6w03SW+xzLp6Vxen9YoIoA/x2cqG0fCS7IElulRxZQ8\n Ot0dUxOJxDQgCT991pkTGJ5CztdfnTibxr1sVJUCwSEqwteytUsrLBbnUpjh85lRpZ99\n 8PFw==","X-Gm-Message-State":"AOJu0YwLAFHTO6LyBvrbM2LX2hHWOo8AlZylbu7OraRvrui4w9wISUrv\n GiKoGU5/eI/MWku5t6n568XaNelmjB+ofg==","X-Google-Smtp-Source":"\n AGHT+IG7fJ9mDTJtOI7JOT6xSEnpJbMoYs7biiehaATm8v2v0+sP2Y2WEfCENGSAUaHP/owM3Y/0rg==","X-Received":"by 2002:a05:6000:1211:b0:337:4a09:b7df with SMTP id\n e17-20020a056000121100b003374a09b7dfmr342633wrx.35.1705031361001;\n Thu, 11 Jan 2024 19:49:21 -0800 (PST)","Message-ID":"<1a8ddba6-cbe5-4db4-b1bc-71f96bd498e2@linaro.org>","Date":"Fri, 12 Jan 2024 07:49:07 +0400","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH v2 38/43] plugins: add an API to read registers","To":"=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>,\n qemu-devel@nongnu.org","Cc":"qemu-s390x@nongnu.org, qemu-ppc@nongnu.org,\n Richard Henderson <richard.henderson@linaro.org>,\n Song Gao <gaosong@loongson.cn>,\n =?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>,\n David Hildenbrand <david@redhat.com>, Aurelien Jarno <aurelien@aurel32.net>,\n Yoshinori Sato <ysato@users.sourceforge.jp>,\n Yanan Wang <wangyanan55@huawei.com>, Bin Meng <bin.meng@windriver.com>,\n Laurent Vivier <lvivier@redhat.com>, Michael Rolnik <mrolnik@gmail.com>,\n Alexandre Iooss <erdnaxe@crans.org>, David Woodhouse <dwmw2@infradead.org>,\n Laurent Vivier <laurent@vivier.eu>, Paolo Bonzini <pbonzini@redhat.com>,\n Brian Cain <bcain@quicinc.com>,\n Daniel Henrique Barboza <danielhb413@gmail.com>,\n Beraldo Leal <bleal@redhat.com>, Paul Durrant <paul@xen.org>,\n Mahmoud Mandour <ma.mandourr@gmail.com>, Thomas Huth <thuth@redhat.com>,\n Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Cleber Rosa <crosa@redhat.com>,\n kvm@vger.kernel.org, Peter Maydell <peter.maydell@linaro.org>,\n Wainer dos Santos Moschetta <wainersm@redhat.com>, qemu-arm@nongnu.org,\n Weiwei Li <liwei1518@gmail.com>,\n =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= <philmd@linaro.org>,\n John Snow <jsnow@redhat.com>,\n Daniel Henrique Barboza <dbarboza@ventanamicro.com>,\n Nicholas Piggin <npiggin@gmail.com>, Palmer Dabbelt <palmer@dabbelt.com>,\n Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,\n Ilya Leoshkevich <iii@linux.ibm.com>,\n =?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@kaod.org>,\n \"Edgar E. Iglesias\" <edgar.iglesias@gmail.com>,\n Eduardo Habkost <eduardo@habkost.net>, qemu-riscv@nongnu.org,\n Alistair Francis <alistair.francis@wdc.com>,\n Akihiko Odaki <akihiko.odaki@daynix.com>","References":"<20240103173349.398526-1-alex.bennee@linaro.org>\n <20240103173349.398526-39-alex.bennee@linaro.org>","Content-Language":"en-US","From":"Pierrick Bouvier <pierrick.bouvier@linaro.org>","In-Reply-To":"<20240103173349.398526-39-alex.bennee@linaro.org>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"base64","Received-SPF":"pass client-ip=2a00:1450:4864:20::42b;\n envelope-from=pierrick.bouvier@linaro.org; helo=mail-wr1-x42b.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,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,\n T_SCC_BODY_TEXT_LINE=-0.01 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":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"}}]