get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/1882116/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1882116,
    "url": "http://patchwork.ozlabs.org/api/patches/1882116/?format=api",
    "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/patch/20240103173349.398526-39-alex.bennee@linaro.org/",
    "project": {
        "id": 14,
        "url": "http://patchwork.ozlabs.org/api/projects/14/?format=api",
        "name": "QEMU Development",
        "link_name": "qemu-devel",
        "list_id": "qemu-devel.nongnu.org",
        "list_email": "qemu-devel@nongnu.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": "",
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    },
    "msgid": "<20240103173349.398526-39-alex.bennee@linaro.org>",
    "list_archive_url": null,
    "date": "2024-01-03T17:33:44",
    "name": "[v2,38/43] plugins: add an API to read registers",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "3999390a1d38fbb9bfba64081cc81ced120fec25",
    "submitter": {
        "id": 39532,
        "url": "http://patchwork.ozlabs.org/api/people/39532/?format=api",
        "name": "Alex Bennée",
        "email": "alex.bennee@linaro.org"
    },
    "delegate": null,
    "mbox": "http://patchwork.ozlabs.org/project/qemu-devel/patch/20240103173349.398526-39-alex.bennee@linaro.org/mbox/",
    "series": [
        {
            "id": 388742,
            "url": "http://patchwork.ozlabs.org/api/series/388742/?format=api",
            "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/list/?series=388742",
            "date": "2024-01-03T17:33:08",
            "name": "testing and plugin updates for 9.0 (pre-PR)",
            "version": 2,
            "mbox": "http://patchwork.ozlabs.org/series/388742/mbox/"
        }
    ],
    "comments": "http://patchwork.ozlabs.org/api/patches/1882116/comments/",
    "check": "pending",
    "checks": "http://patchwork.ozlabs.org/api/patches/1882116/checks/",
    "tags": {},
    "related": [],
    "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=ntRvzuBR;\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 4T4xqY3V7qz20Rq\n\tfor <incoming@patchwork.ozlabs.org>; Thu,  4 Jan 2024 04:42:01 +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 1rL5DT-0007pV-Tx; Wed, 03 Jan 2024 12:39:43 -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 1rL5DR-0007jz-Tw\n for qemu-devel@nongnu.org; Wed, 03 Jan 2024 12:39:41 -0500",
            "from mail-wm1-x330.google.com ([2a00:1450:4864:20::330])\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 1rL5D2-0000m3-Bu\n for qemu-devel@nongnu.org; Wed, 03 Jan 2024 12:39:41 -0500",
            "by mail-wm1-x330.google.com with SMTP id\n 5b1f17b1804b1-40d5a41143fso67909825e9.3\n for <qemu-devel@nongnu.org>; Wed, 03 Jan 2024 09:39:15 -0800 (PST)",
            "from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id\n df2-20020a5d5b82000000b00336be33649csm23840746wrb.9.2024.01.03.09.39.08\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Wed, 03 Jan 2024 09:39:13 -0800 (PST)",
            "from draig.lan (localhost [IPv6:::1])\n by draig.lan (Postfix) with ESMTP id 3ABB35F92F;\n Wed,  3 Jan 2024 17:33:53 +0000 (GMT)"
        ],
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=linaro.org; s=google; t=1704303555; x=1704908355; darn=nongnu.org;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=lT3w4azBfzDEu1VsOKbVUIApuL3l4Y+VxFlcwm2LC0s=;\n b=ntRvzuBRHm1nrfodMmCL7pUimLgHdVGjo2iqqFzMXfbYOeiovf40ZJtZnxMmctCW0C\n WO39VHwsI9F1J+YfsfqNYfVRzq6cvTUqvxr4FtjkYu6VjJknkx3HO+y/XLJM94Q5i64W\n cx3mzkpMCAFEg4dUexvSzTBE9Gfa91w+KggK9QuiT+JseC5Q4SLcsrtz0YruTU9qDFi5\n Lir5V3NHXxg8AqcynapZZnJ7gjcbBOWWUPA6E/1nOx7c+4ZBv4hj9hD58n3eYHazMYXh\n hcN/Q3q9ELJJhdB21/ghuoOBTgvIFVUEndpV2ABU5SC7rNFO9JLWVPbdmoRip1TLdW0s\n DSOg==",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20230601; t=1704303555; x=1704908355;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n :subject:date:message-id:reply-to;\n bh=lT3w4azBfzDEu1VsOKbVUIApuL3l4Y+VxFlcwm2LC0s=;\n b=Wi/cl6Yh1qjqCgnvhlb8WWKt83nHyCYBcoSzBu5FOSeBt7hd2qE1dLXrmW8kLzO59e\n kbhwfVgPAOh4vFRf+t9ArQq5S/bKuUSD6TIntC5jRZOYkoQ/ac22mYEMkPYOHdiSbN/M\n kOUJ986AxXdOvqprT2XeZHE4OYNkXw3/L4k6+MajuGhHSpqwHAIKeCArskYdeoc1pY3b\n ed/7c4ivCEADEBMA2l7CHe7sT7A72/nHr+pmBfYs/1/P+Z34oZ7djHwd1O+o26C9YJXB\n FP7JSJIwv7/7FuG9aAO+zlvctOSuOd3YFG9MdpnwI8IPaHt21Y3Ub00o30blp29mCgqK\n fsNw==",
        "X-Gm-Message-State": "AOJu0YyJH2q5xW9qv4gZ3N5j7zBNTg+TbJTbRzil4BhYL7I1toDkp6HV\n nKsB8PW591FiEW7qqw9rvbcTM1WnGNNpoA==",
        "X-Google-Smtp-Source": "\n AGHT+IFU+sn40l7XSEUwt4a43VDFS9eN3BSlA6WquNSO73rVVY9SbSOwOaEBjbf8E24A6FOp4w1ohg==",
        "X-Received": "by 2002:a05:600c:4e03:b0:40d:91a2:7133 with SMTP id\n b3-20020a05600c4e0300b0040d91a27133mr514787wmq.100.1704303554843;\n Wed, 03 Jan 2024 09:39:14 -0800 (PST)",
        "From": "=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>",
        "To": "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>, =?utf-8?q?Alex_Benn?=\n\t=?utf-8?q?=C3=A9e?= <alex.bennee@linaro.org>, 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>,\n Akihiko Odaki <akihiko.odaki@daynix.com>",
        "Subject": "[PATCH v2 38/43] plugins: add an API to read registers",
        "Date": "Wed,  3 Jan 2024 17:33:44 +0000",
        "Message-Id": "<20240103173349.398526-39-alex.bennee@linaro.org>",
        "X-Mailer": "git-send-email 2.39.2",
        "In-Reply-To": "<20240103173349.398526-1-alex.bennee@linaro.org>",
        "References": "<20240103173349.398526-1-alex.bennee@linaro.org>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "Received-SPF": "pass client-ip=2a00:1450:4864:20::330;\n envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x330.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"
    },
    "content": "We can only request a list of registers once the vCPU has been\ninitialised so the user needs to use either call the get function on\nvCPU initialisation or during the translation phase.\n\nWe don't expose the reg number to the plugin instead hiding it behind\nan opaque handle. This allows for a bit of future proofing should the\ninternals need to be changed while also being hashed against the\nCPUClass so we can handle different register sets per-vCPU in\nhetrogenous situations.\n\nHaving an internal state within the plugins also allows us to expand\nthe interface in future (for example providing callbacks on register\nchange if the translator can track changes).\n\nResolves: https://gitlab.com/qemu-project/qemu/-/issues/1706\nCc: Akihiko Odaki <akihiko.odaki@daynix.com>\nBased-on: <20231025093128.33116-18-akihiko.odaki@daynix.com>\nSigned-off-by: Alex Bennée <alex.bennee@linaro.org>\n\n---\nv3\n  - also g_intern_string the register name\n  - make get_registers documentation a bit less verbose\nv2\n  - use new get whole list api, and expose upwards\n\nvAJB:\n\nThe main difference to Akikio's version is hiding the gdb register\ndetail 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(-)",
    "diff": "diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h\nindex 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 */\ndiff --git a/plugins/api.c b/plugins/api.c\nindex 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+}\ndiff --git a/plugins/qemu-plugins.symbols b/plugins/qemu-plugins.symbols\nindex 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",
    "prefixes": [
        "v2",
        "38/43"
    ]
}