From nobody Mon Feb 9 02:55:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497075; cv=none; d=zohomail.com; s=zohoarc; b=W1N0hGIBfK8J9m9YCjbvUKvX0/wVMaI12JDc3cgywAX1gscdanceYIyGVXtQHC75HJ8W78Gth8+gpoE5Lpwq5qBkaMdFvT3/3hNUPHsS3eTUczMawrjKnDrLC1bYTZKnfTFG0+4b8hdNNDW5D//gja54OrZGxoVV4KdElRoH0DI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497075; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1ysiNB3jN3pm4xSzw9lWbP2QG1HmR4QbzZmy+B07iUo=; b=g5XGP/1uM8oIWjYwl1lpjYXmbiuYKmzRgzTG3WG594N1G7LUo7PtCfH8gs9MZ8mdW4KCEgRnPebRM/yVzE5G9WXc/bn58U1+wqYK4CFE0gi1pLGzNWSPg5yT4Cwo7stzRXWWLkwvEBxGsL2vvEKenfuCVUbBs7l7vc0aNWQ9xvw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1749497075850401.157528485312; Mon, 9 Jun 2025 12:24:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi64-0007sh-M6; Mon, 09 Jun 2025 15:23:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi62-0007s8-5R for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:50 -0400 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi5y-0002Qu-FL for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:49 -0400 Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-311e2cc157bso3389116a91.2 for ; Mon, 09 Jun 2025 12:23:45 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497024; x=1750101824; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1ysiNB3jN3pm4xSzw9lWbP2QG1HmR4QbzZmy+B07iUo=; b=XAR4DZZXBWbaspo3RunzOrEQkXGwruo58jGK6UQzDvClJYLkX2iblINVZyA9iewwiX UT5jsM4O6Srskv5OHUyzjmAQ83Y8fKXEnmuBvNPws802zLVzS5pGP7xUA1g/aYmBAeVG pdVFvxau9tDqqFu83JnrQdV/4//YkKG9Op6EzH0+0ua5TwiZXDIVxFebhuuzHfl6VkRB t2GKwe2HdGW87wcKTuiSDXq2kX3pjHp0TRsW/3lopjVFrmfBRdW0KhKFmn8e0d5I2bJD /c5LGxq9HnwWPkW7/QH69HlGAsfaUa+yNfY56FZDZeWT+5LoJ/maxeYoxmGLUkImVVwZ xN/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497024; x=1750101824; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1ysiNB3jN3pm4xSzw9lWbP2QG1HmR4QbzZmy+B07iUo=; b=D67Cf3rCmipeJqDM2R8fbNHaut3+aH+s4ADSPBZpuL8pHFAUg5Wgz9niLubgUgILBF MgSiPPJE1ci7yKz7+dozhlPrIplz11hH5YkJ1SbpX1OFLdHTStJTD/FEY75IdRdvQdVa WHhCYGG9iDBtHeky8Uwnc9A+HDgNXBrrJMD6792xqjains2EnQylNgk1EdUaGj7uyZUK xOkdN1v8Cv9lpnD12qAPLklV4HSDsNef7RN71i8fj8RSbIkaURIDoO73DxaR3YGFqnU7 saw6tGTwFwPSptzxmLkCfPtwfVKCOf8lgB0w/rZo0grmbNwzxOj9hVFmzBHSWlwQ3FwK bPaQ== X-Gm-Message-State: AOJu0YwA8tANK4A2Mjr26K+kZumwRFlnuoFehDIDPVTDK7RXR34Wg1sa VD4ZAa2xxCYj0kr+lcEQKQYUIauDuzkeuzLDJRsqRi6pySV2+ASrQ3F4ERUlEqlS X-Gm-Gg: ASbGncszfF2E2ZBTG9LkzyEnvflyap7zw0x4IOTrkchw0Sntbcd44vZUHk0isy3bRsn seCpaH8qV5nk4k8ijHTmsFgp8d6oxng1kw1HHHWyJu/rZz5o6Uqmo0IlvaJaEqCjUHmwmkiCyfN STccsiV/QzZ3qIPWB9caePWZFd/qLFtlZx+GghW0dfhm7JWPYOrYhN/rfDKRlJWiBTOVZh+Vtf6 at6ImziVltqy/1Ii4eL8ZYHyKNNqL1KKcntHqOfcZJBHYd7CcGWb6n+e9NdfQKwNMcrvhaXVx9F XyzxBBWSwcS/P8nxoUq27wpiMNMIBJ3lbzIQKi7R8xCMbXigVk8= X-Google-Smtp-Source: AGHT+IGPHenvDzAFSS4Ezrgkwx/qzXH/dr5io5aeKoUtsDjYIsunK8DnnsClDUTpYsG2Wn6L9z/nFQ== X-Received: by 2002:a17:90b:52ce:b0:311:a314:c2e0 with SMTP id 98e67ed59e1d1-31346d4dd5bmr21107498a91.20.1749497024107; Mon, 09 Jun 2025 12:23:44 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , novafacing , Julian Ganz Subject: [PATCH v10 1/8] gdbstub: Expose gdb_write_register function to consumers of gdbstub Date: Mon, 9 Jun 2025 12:23:35 -0700 Message-ID: <20250609192342.316156-2-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102b; envelope-from=rowanbhart@gmail.com; helo=mail-pj1-x102b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497076835116600 From: novafacing This patch exposes the gdb_write_register function from gdbstub/gdbstub.c via the exec/gdbstub.h header file to support use in plugins to write register contents. Reviewed-by: Alex Benn=C3=A9e Reviewed-by: Julian Ganz Reviewed-by: Pierrick Bouvier Signed-off-by: Rowan Hart --- gdbstub/gdbstub.c | 2 +- include/exec/gdbstub.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gdbstub/gdbstub.c b/gdbstub/gdbstub.c index 565f6b33a9..5846e481be 100644 --- a/gdbstub/gdbstub.c +++ b/gdbstub/gdbstub.c @@ -534,7 +534,7 @@ int gdb_read_register(CPUState *cpu, GByteArray *buf, i= nt reg) return 0; } =20 -static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) +int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) { GDBRegisterState *r; =20 diff --git a/include/exec/gdbstub.h b/include/exec/gdbstub.h index 0675b0b646..a16c0051ce 100644 --- a/include/exec/gdbstub.h +++ b/include/exec/gdbstub.h @@ -124,6 +124,20 @@ const GDBFeature *gdb_find_static_feature(const char *= xmlname); */ int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg); =20 +/** + * gdb_write_register() - Write a register associated with a CPU. + * @cpu: The CPU associated with the register. + * @buf: The buffer that the register contents will be set to. + * @reg: The register's number returned by gdb_find_feature_register(). + * + * The size of @buf must be at least the size of the register being + * written. + * + * Return: The number of written bytes, or 0 if an error occurred (for + * example, an unknown register was provided). + */ +int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg); + /** * typedef GDBRegDesc - a register description from gdbstub */ --=20 2.49.0 From nobody Mon Feb 9 02:55:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497179; cv=none; d=zohomail.com; s=zohoarc; b=kpY1aSv6vp18LE6HjAKlt0PDaH+8aPyYT0lqSFZPVJ28c1IUjv94rQci5OoX4CfDCqDmiTECB8vwYkAfilpfjk6GR2o1zxOvx2GqoEUpMa+t1APgSOjV7gc+nKHezUpPVkyLcrywVVTuizFESUa5E9Xc3GZUub6mX4vm8V79QxU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497179; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=DkzAOT9KPhbXdwiYkwrjtSXHJRVHCh65cjh/wF+xrzM=; b=dCyq4cv0+I2HCc9I2CBMTectFNdzHikiJBgLqDQp5vIXmEQ3kbjhYIxEvfKo4GLQ27VfDBni/6QlG5JArYCIX9KroK9NKOcLaPzi35M8iDZcsZIByAgmWjJ3Y7hW6/PuAxZGaByc+YPeC/Qg3FBArfDeIuLTSjyaVEBtMHh9atM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1749497179082165.36461293503476; Mon, 9 Jun 2025 12:26:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi6K-0007yJ-85; Mon, 09 Jun 2025 15:24:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi6A-0007v7-Gm for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:59 -0400 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi63-0002Qy-Jt for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:58 -0400 Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-3138b2f0249so1018210a91.2 for ; Mon, 09 Jun 2025 12:23:48 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497025; x=1750101825; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DkzAOT9KPhbXdwiYkwrjtSXHJRVHCh65cjh/wF+xrzM=; b=jy6V1mHQTXQXL5t6tWQZrMmD8C3gNlVay0Upeo1gCjtN4TeeceKadCcsNEFJHirpld bhDpVl5GLtOAqOFGyjQd4aA8JelzrGM3I3BLe6rLPeU1kk87U68WFi46dQEP4lvylrzT mZi9GyZOEa7Y7/2SL70mX4nbuoLPCMXgJx1YcbUI8wtCbUMw26vui6B3oWa3GD6/AlFI 9VaHwwBqqTaubwNHzwtRGJsxaGSWN7WQQYd2ZUN6mSt/ynBF+m4NNleDIGxgtCzbmUee uQwT2rEcNL3bUGcEBXtSj0XB38FK72EjbNz2dvSVefS31VmkLbFHEq68F7xACuiAtXZQ HE3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497025; x=1750101825; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DkzAOT9KPhbXdwiYkwrjtSXHJRVHCh65cjh/wF+xrzM=; b=Ax2vzRylaUj29lGmsUy54TEYWxC5p0uU7h+Rv95UGCII2cJIMbDh8B6wL02pqarDNo jkgJd5JQl3Oh5Z34VAnfcuqvqRqs+2MYyWY/K2eYfBKduaMw2f/2mk+XfTwbp5eUBQbO pwULHWKO+5PndKNyTyP7W6UY8onFegJHqjYJSN9yeloFQW5yzmchauVfms+xQsFbJy4F P/49ilHlAhmkwU8RtCTUDPpMNuxnM0xne2SUut1pQzzXAku0dVghRadvnnlkWpdWBfYr xTVVUqXblTaNmbne4zbQTS9TtH8P03w11wg8S9QhRQKZ6jedVCqZvXHgZXfumFoNfdbo to3g== X-Gm-Message-State: AOJu0YyjCXDVGMKbebybcyuowiu0ef5TmkFI/C0M8Qgp94b8FARZGDU6 fxHazp2ClArAshv8McGbw2kAx+iOfEcHZ5UvOb+cPy2JkYwkHQMRzryhJH9otTyo X-Gm-Gg: ASbGncuf138xmKk+n4R2rpCTraJNKZ/qLmfOTyiTa/KPbWzoYhlxc6jX3FkveBHmmid MOkEE6HWW2QCKYgBuI/lfmqsQrjvAAHBfhfYFdmjgWEkfBxvfpBCUNagU/mUt+bEjZlgB98Mk6L I1v8IN+yfh0eXqy1a9Xzu+sw2XMdKjLVJYzLgDTenLingOu/ttMiSmAVkEeA5ouRpGb9ARWKHA2 9Is5vFsnL7NpoSlMolpChI+Wg4CWm2tENESHW0rqbw50fLm0XDdusQe8K+lqxThpcPWheQV0dfX ZG2weq20Y5p0yvPjx6i1Dw1vt/gm5ovs5AY9ZvtS4xIYbT3mDiHJtlaw05eNsg== X-Google-Smtp-Source: AGHT+IH+lioVgAIzxFPFBDM1KuAiFdLLJje1Cqxh8//98quc1UAneREJJ33jKG+wUtvFkWVSsyFRxg== X-Received: by 2002:a17:90b:164a:b0:301:9f62:a944 with SMTP id 98e67ed59e1d1-3134769f73dmr21816151a91.33.1749497024938; Mon, 09 Jun 2025 12:23:44 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , novafacing Subject: [PATCH v10 2/8] plugins: Add register write API Date: Mon, 9 Jun 2025 12:23:36 -0700 Message-ID: <20250609192342.316156-3-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102b; envelope-from=rowanbhart@gmail.com; helo=mail-pj1-x102b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497179855116600 Content-Type: text/plain; charset="utf-8" From: novafacing This patch adds a function to the plugins API to allow plugins to write register contents. It also moves the qemu_plugin_read_register function so all the register-related functions are grouped together in the file. Reviewed-by: Pierrick Bouvier Signed-off-by: Rowan Hart --- include/qemu/qemu-plugin.h | 54 ++++++++++++++++++++++++++------------ plugins/api.c | 26 +++++++++++++----- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 3a850aa216..cfe1692ecb 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -871,7 +871,8 @@ struct qemu_plugin_register; /** * typedef qemu_plugin_reg_descriptor - register descriptions * - * @handle: opaque handle for retrieving value with qemu_plugin_read_regis= ter + * @handle: opaque handle for retrieving value with qemu_plugin_read_regis= ter or + * writing value with qemu_plugin_write_register * @name: register name * @feature: optional feature descriptor, can be NULL */ @@ -893,6 +894,41 @@ typedef struct { QEMU_PLUGIN_API GArray *qemu_plugin_get_registers(void); =20 +/** + * qemu_plugin_read_register() - read register for current vCPU + * + * @handle: a @qemu_plugin_reg_handle handle + * @buf: A GByteArray for the data owned by the plugin + * + * This function is only available in a context that register read access = is + * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. + * + * Returns the size of the read register. The content of @buf is in target= byte + * order. On failure returns -1. + */ +QEMU_PLUGIN_API +int qemu_plugin_read_register(struct qemu_plugin_register *handle, + GByteArray *buf); + +/** + * qemu_plugin_write_register() - write register for current vCPU + * + * @handle: a @qemu_plugin_reg_handle handle + * @buf: A GByteArray for the data owned by the plugin + * + * This function is only available in a context that register write access= is + * explicitly requested via the QEMU_PLUGIN_CB_RW_REGS flag. + * + * The size of @buf must be at least the size of the requested register. + * Attempting to write a register with @buf smaller than the register size + * will result in a crash or other undesired behavior. + * + * Returns the number of bytes written. On failure returns 0. + */ +QEMU_PLUGIN_API +int qemu_plugin_write_register(struct qemu_plugin_register *handle, + GByteArray *buf); + /** * qemu_plugin_read_memory_vaddr() - read from memory using a virtual addr= ess * @@ -915,22 +951,6 @@ QEMU_PLUGIN_API bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len); =20 -/** - * qemu_plugin_read_register() - read register for current vCPU - * - * @handle: a @qemu_plugin_reg_handle handle - * @buf: A GByteArray for the data owned by the plugin - * - * This function is only available in a context that register read access = is - * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. - * - * Returns the size of the read register. The content of @buf is in target= byte - * order. On failure returns -1. - */ -QEMU_PLUGIN_API -int qemu_plugin_read_register(struct qemu_plugin_register *handle, - GByteArray *buf); - /** * qemu_plugin_scoreboard_new() - alloc a new scoreboard * diff --git a/plugins/api.c b/plugins/api.c index 3c9d4832e9..6514f2c76a 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -433,6 +433,25 @@ GArray *qemu_plugin_get_registers(void) return create_register_handles(regs); } =20 +int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray= *buf) +{ + g_assert(current_cpu); + + return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1); +} + +int qemu_plugin_write_register(struct qemu_plugin_register *reg, + GByteArray *buf) +{ + g_assert(current_cpu); + + if (buf->len =3D=3D 0 || qemu_plugin_get_cb_flags() !=3D QEMU_PLUGIN_C= B_RW_REGS) { + return -1; + } + + return gdb_write_register(current_cpu, buf->data, GPOINTER_TO_INT(reg)= - 1); +} + bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t= len) { g_assert(current_cpu); @@ -453,13 +472,6 @@ bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByt= eArray *data, size_t len) return true; } =20 -int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray= *buf) -{ - g_assert(current_cpu); - - return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1); -} - struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_s= ize) { return plugin_scoreboard_new(element_size); --=20 2.49.0 From nobody Mon Feb 9 02:55:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497187; cv=none; d=zohomail.com; s=zohoarc; b=XMX54sMvpz03mH+8eoYN+do/dfnMDTfr41suEjAwECEc+yXnKTNKH+O/rkU0XWfvI4MYJ3h3a9/mnPS/S6YN6byH1mdI+VEptmXyscWBF5bkzZJ6TDiJw9fJb92V3eMeUctKq5Op2dvpKMEuQ+ZSUXdJbHVbuQ13kFkdW6iGDAQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497187; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wJSraaVGwKFPMPq56zo7N8lheZJz4Mz0YwSZtDSPAJo=; b=gc3a75zi4HX0b/wI273JvEmeGl3jcRGo6T/XAIBbKRXbYBQ9ztQwq39+gnFiTIyMwppPDmrTMf5fhRe9VqUqEycWQ28CQ4zrxo2TSIssFdmaZzIT5ruG/IsxX3q2hqYOH2LjrO0plrNuuek2uf+lD1cIQKNooJ8siHUWOrosafQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174949718719282.17527358208281; Mon, 9 Jun 2025 12:26:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi6Q-00081z-9c; Mon, 09 Jun 2025 15:24:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi64-0007t0-UG for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:53 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi60-0002R5-Ma for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:52 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2351227b098so35513325ad.2 for ; Mon, 09 Jun 2025 12:23:47 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497026; x=1750101826; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wJSraaVGwKFPMPq56zo7N8lheZJz4Mz0YwSZtDSPAJo=; b=h080FJ1QpfNLLOX0FqWwqJuhJtoKg2thgnThE5KarEM84/L9rVckrF93AcDrSomb7t NTBmPXRZvahD+NDuVQVcodZ9aHEeFsiNxWl7M83PlZgoJZmTAOecAXPRQI+9diujli80 bX3kPeMSGvtasavYDoH6HNxQt2qdAKVpHXrVIduPnTzJELh/lPeQMeN7AbouhzfnYjCG /yjkAN0ltyQJvzpyW999jc+9bljSXAnQBiKN+dLdjK8ty2ZhSfznyuK4etYPdy4Tlg8o EkkInQEoAbQtEj9SOdUy/7f/6r7oaqloPRvK7T+qB58RDL1ZHJRlq029yJDMi6kqHJGW lGqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497026; x=1750101826; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wJSraaVGwKFPMPq56zo7N8lheZJz4Mz0YwSZtDSPAJo=; b=l5GjbfCEDiBzcI/hSlJpWqSow6zS062FAP2h3cJyZmEhtXU8egj2b5+/DpFUv259Eq eUW4DZ/MatqQJ+o37K2DHP6d2AtDVxy20cus1Ha2zCc/CvoR4Dxdl5MHeTYo89qAMXro nl9FaU28GIW5sjM4OaZlwrVZI/6tSzi3C8sGa/UaooZ/J3iQdJNRtalatEU5xHoWTddQ vrje+7zpV2QUdqbvWHwXvg/1LeQnGU2hjCnX9t6L6VyFuOs5rP8d55SxcAxQIy+hUIui fNd7mifxy/l95W0aol0avsgs/xMN1WBPvGdy0U9pCk5YPXCLPVCPn/B2v9ItzluBUkMM dNug== X-Gm-Message-State: AOJu0Yx5VLBeY49S2ym+KCcMXvh1c8tbqYh5PK6+LL7//Mmy7ptlMDuN 0p7KKM8n8vYfztX9rXd0Zyr943WSPNda/Csz1X+ACsoIzppYgDksijvyC+s4aQW/fF4= X-Gm-Gg: ASbGncvEHdS2xZusyjmL9zslfLHzVGNyAvBCpxn/g+xEFXu+JV1iqNCO/X3ojjqr/SC gnglzn9TyM7nC0FL2LG25aGl88yc3bsVeGmWVvuh4LbofrwPaHPAUDt0ro5sSygqRw12tGZwoGq VXY5beGCfiGNuQyIMlWJUPvFOt/I30bORMX5IfOr0XS86C5Jv+ceZVsszG8eMc8hT0uzs73Uon5 K5GUjcpp2eu6KQmLkjUvbsdnx6hd/UcOSOo3XVXVZFhNAJ7sr9SbQU96xkuKtfEN+e+dN/b9RxJ ij1OKxpI1Q/vdiJUZmNM0Ljft7Gke/XUaJW9jb1NrU+L4FOKGt8= X-Google-Smtp-Source: AGHT+IFQOxlBJlGR/lqdsMylUgjvi/t9euh52bwBdLBKJUAa9Gw4kvHzayBlYdWMKH4e64vlpB1ulQ== X-Received: by 2002:a17:90a:e7c1:b0:311:b3e7:fb31 with SMTP id 98e67ed59e1d1-31346c4c5admr22535862a91.0.1749497025805; Mon, 09 Jun 2025 12:23:45 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , Rowan Hart Subject: [PATCH v10 3/8] plugins: Add enforcement of QEMU_PLUGIN_CB flags in register R/W callbacks Date: Mon, 9 Jun 2025 12:23:37 -0700 Message-ID: <20250609192342.316156-4-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::629; envelope-from=rowanbhart@gmail.com; helo=mail-pl1-x629.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497188170116600 Content-Type: text/plain; charset="utf-8" This patch adds functionality to enforce the requested QEMU_PLUGIN_CB_ flags level passed when registering a callback function using the plugins API. Each time a callback is about to be invoked, a thread-local variable will be updated with the level that callback requested. Then, called API functions (in particular, the register read and write API) will call qemu_plugin_get_cb_flags() to check the level is at least the level they require. Signed-off-by: Rowan Hart --- accel/tcg/plugin-gen.c | 30 +++++++++++++++++++++++++ include/hw/core/cpu.h | 1 + include/qemu/plugin.h | 6 +++++ include/qemu/qemu-plugin.h | 19 +++++++++++----- plugins/api.c | 4 ++++ plugins/core.c | 45 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 6 deletions(-) diff --git a/accel/tcg/plugin-gen.c b/accel/tcg/plugin-gen.c index c1da753894..9920381a84 100644 --- a/accel/tcg/plugin-gen.c +++ b/accel/tcg/plugin-gen.c @@ -117,10 +117,20 @@ static TCGv_i32 gen_cpu_index(void) static void gen_udata_cb(struct qemu_plugin_regular_cb *cb) { TCGv_i32 cpu_index =3D gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags =3D + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags =3D tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags =3D tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_gen_call2(cb->f.vcpu_udata, cb->info, NULL, tcgv_i32_temp(cpu_index), tcgv_ptr_temp(tcg_constant_ptr(cb->userp))); + tcg_gen_st_i32(clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_temp_free_i32(cpu_index); + tcg_temp_free_i32(flags); + tcg_temp_free_i32(clear_flags); } =20 static TCGv_ptr gen_plugin_u64_ptr(qemu_plugin_u64 entry) @@ -173,10 +183,20 @@ static void gen_udata_cond_cb(struct qemu_plugin_cond= itional_cb *cb) tcg_gen_ld_i64(val, ptr, 0); tcg_gen_brcondi_i64(cond, val, cb->imm, after_cb); TCGv_i32 cpu_index =3D gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags =3D + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags =3D tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags =3D tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_gen_call2(cb->f.vcpu_udata, cb->info, NULL, tcgv_i32_temp(cpu_index), tcgv_ptr_temp(tcg_constant_ptr(cb->userp))); + tcg_gen_st_i32(clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_temp_free_i32(cpu_index); + tcg_temp_free_i32(flags); + tcg_temp_free_i32(clear_flags); gen_set_label(after_cb); =20 tcg_temp_free_i64(val); @@ -210,12 +230,22 @@ static void gen_mem_cb(struct qemu_plugin_regular_cb = *cb, qemu_plugin_meminfo_t meminfo, TCGv_i64 addr) { TCGv_i32 cpu_index =3D gen_cpu_index(); + enum qemu_plugin_cb_flags cb_flags =3D + tcg_call_to_qemu_plugin_cb_flags(cb->info->flags); + TCGv_i32 flags =3D tcg_constant_i32(cb_flags); + TCGv_i32 clear_flags =3D tcg_constant_i32(QEMU_PLUGIN_CB_NO_REGS); + tcg_gen_st_i32(flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_gen_call4(cb->f.vcpu_mem, cb->info, NULL, tcgv_i32_temp(cpu_index), tcgv_i32_temp(tcg_constant_i32(meminfo)), tcgv_i64_temp(addr), tcgv_ptr_temp(tcg_constant_ptr(cb->userp))); + tcg_gen_st_i32(clear_flags, tcg_env, + offsetof(CPUState, neg.plugin_cb_flags) - sizeof(CPUState)); tcg_temp_free_i32(cpu_index); + tcg_temp_free_i32(flags); + tcg_temp_free_i32(clear_flags); } =20 static void inject_cb(struct qemu_plugin_dyn_cb *cb) diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 1e87f7d393..d3cc9a5224 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -368,6 +368,7 @@ typedef struct CPUNegativeOffsetState { GArray *plugin_mem_cbs; uint64_t plugin_mem_value_low; uint64_t plugin_mem_value_high; + int32_t plugin_cb_flags; #endif IcountDecr icount_decr; bool can_do_io; diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h index 9726a9ebf3..0ea79170f7 100644 --- a/include/qemu/plugin.h +++ b/include/qemu/plugin.h @@ -209,6 +209,12 @@ void qemu_plugin_user_prefork_lock(void); */ void qemu_plugin_user_postfork(bool is_child); =20 +enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags(int flags); + +void qemu_plugin_set_cb_flags(CPUState *cpu, enum qemu_plugin_cb_flags fla= gs); + +enum qemu_plugin_cb_flags qemu_plugin_get_cb_flags(void); + #else /* !CONFIG_PLUGIN */ =20 static inline void qemu_plugin_add_opts(void) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index cfe1692ecb..9c9ebf6ce0 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -254,9 +254,6 @@ typedef struct { * @QEMU_PLUGIN_CB_NO_REGS: callback does not access the CPU's regs * @QEMU_PLUGIN_CB_R_REGS: callback reads the CPU's regs * @QEMU_PLUGIN_CB_RW_REGS: callback reads and writes the CPU's regs - * - * Note: currently QEMU_PLUGIN_CB_RW_REGS is unused, plugins cannot change - * system register state. */ enum qemu_plugin_cb_flags { QEMU_PLUGIN_CB_NO_REGS, @@ -901,7 +898,12 @@ GArray *qemu_plugin_get_registers(void); * @buf: A GByteArray for the data owned by the plugin * * This function is only available in a context that register read access = is - * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag. + * explicitly requested via the QEMU_PLUGIN_CB_R_REGS flag, if called insi= de a + * callback that can be registered with a qemu_plugin_cb_flags argument. T= his + * function can also be used in any callback context that does not use a f= lags + * argument, such as in a callback registered with + * qemu_plugin_register_vcpu_init_cb(), except for callbacks registered wi= th + * qemu_plugin_register_atexit_cb() and qemu_plugin_register_flush_cb(). * * Returns the size of the read register. The content of @buf is in target= byte * order. On failure returns -1. @@ -916,8 +918,13 @@ int qemu_plugin_read_register(struct qemu_plugin_regis= ter *handle, * @handle: a @qemu_plugin_reg_handle handle * @buf: A GByteArray for the data owned by the plugin * - * This function is only available in a context that register write access= is - * explicitly requested via the QEMU_PLUGIN_CB_RW_REGS flag. + * This function is only available in a context that register read access = is + * explicitly requested via the QEMU_PLUGIN_CB_RW_REGS flag, if called ins= ide a + * callback that can be registered with a qemu_plugin_cb_flags argument. T= his + * function can also be used in any callback context that does not use a f= lags + * argument, such as in a callback registered with + * qemu_plugin_register_vcpu_init_cb(), except for callbacks registered wi= th + * qemu_plugin_register_atexit_cb() and qemu_plugin_register_flush_cb(). * * The size of @buf must be at least the size of the requested register. * Attempting to write a register with @buf smaller than the register size diff --git a/plugins/api.c b/plugins/api.c index 6514f2c76a..3f04399c26 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -437,6 +437,10 @@ int qemu_plugin_read_register(struct qemu_plugin_regis= ter *reg, GByteArray *buf) { g_assert(current_cpu); =20 + if (qemu_plugin_get_cb_flags() =3D=3D QEMU_PLUGIN_CB_NO_REGS) { + return -1; + } + return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1); } =20 diff --git a/plugins/core.c b/plugins/core.c index eb9281fe54..26c97b3179 100644 --- a/plugins/core.c +++ b/plugins/core.c @@ -15,6 +15,7 @@ #include "qemu/lockable.h" #include "qemu/option.h" #include "qemu/plugin.h" +#include "qemu/qemu-plugin.h" #include "qemu/queue.h" #include "qemu/rcu_queue.h" #include "qemu/rcu.h" @@ -266,7 +267,9 @@ static void qemu_plugin_vcpu_init__async(CPUState *cpu,= run_on_cpu_data unused) plugin_grow_scoreboards__locked(cpu); qemu_rec_mutex_unlock(&plugin.lock); =20 + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_RW_REGS); plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_INIT); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); } =20 void qemu_plugin_vcpu_init_hook(CPUState *cpu) @@ -279,7 +282,9 @@ void qemu_plugin_vcpu_exit_hook(CPUState *cpu) { bool success; =20 + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_RW_REGS); plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); =20 assert(cpu->cpu_index !=3D UNASSIGNED_CPU_INDEX); qemu_rec_mutex_lock(&plugin.lock); @@ -367,6 +372,7 @@ void plugin_register_dyn_cb__udata(GArray **arr, static TCGHelperInfo info[3] =3D { [QEMU_PLUGIN_CB_NO_REGS].flags =3D TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags =3D TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags =3D 0, /* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) @@ -396,6 +402,7 @@ void plugin_register_dyn_cond_cb__udata(GArray **arr, static TCGHelperInfo info[3] =3D { [QEMU_PLUGIN_CB_NO_REGS].flags =3D TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags =3D TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags =3D 0, /* * Match qemu_plugin_vcpu_udata_cb_t: * void (*)(uint32_t, void *) @@ -434,6 +441,7 @@ void plugin_register_vcpu_mem_cb(GArray **arr, static TCGHelperInfo info[3] =3D { [QEMU_PLUGIN_CB_NO_REGS].flags =3D TCG_CALL_NO_RWG, [QEMU_PLUGIN_CB_R_REGS].flags =3D TCG_CALL_NO_WG, + [QEMU_PLUGIN_CB_RW_REGS].flags =3D 0, /* * Match qemu_plugin_vcpu_mem_cb_t: * void (*)(uint32_t, qemu_plugin_meminfo_t, uint64_t, void *) @@ -473,7 +481,9 @@ void qemu_plugin_tb_trans_cb(CPUState *cpu, struct qemu= _plugin_tb *tb) QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) { qemu_plugin_vcpu_tb_trans_cb_t func =3D cb->f.vcpu_tb_trans; =20 + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_RW_REGS); func(cb->ctx->id, tb); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); } } =20 @@ -498,7 +508,9 @@ qemu_plugin_vcpu_syscall(CPUState *cpu, int64_t num, ui= nt64_t a1, uint64_t a2, QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) { qemu_plugin_vcpu_syscall_cb_t func =3D cb->f.vcpu_syscall; =20 + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_RW_REGS); func(cb->ctx->id, cpu->cpu_index, num, a1, a2, a3, a4, a5, a6, a7,= a8); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); } } =20 @@ -520,7 +532,9 @@ void qemu_plugin_vcpu_syscall_ret(CPUState *cpu, int64_= t num, int64_t ret) QLIST_FOREACH_SAFE_RCU(cb, &plugin.cb_lists[ev], entry, next) { qemu_plugin_vcpu_syscall_ret_cb_t func =3D cb->f.vcpu_syscall_ret; =20 + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_RW_REGS); func(cb->ctx->id, cpu->cpu_index, num, ret); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); } } =20 @@ -528,14 +542,18 @@ void qemu_plugin_vcpu_idle_cb(CPUState *cpu) { /* idle and resume cb may be called before init, ignore in this case */ if (cpu->cpu_index < plugin.num_vcpus) { + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_RW_REGS); plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_IDLE); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); } } =20 void qemu_plugin_vcpu_resume_cb(CPUState *cpu) { if (cpu->cpu_index < plugin.num_vcpus) { + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_RW_REGS); plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_RESUME); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); } } =20 @@ -615,9 +633,13 @@ void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t v= addr, switch (cb->type) { case PLUGIN_CB_MEM_REGULAR: if (rw & cb->regular.rw) { + qemu_plugin_set_cb_flags(cpu, + tcg_call_to_qemu_plugin_cb_flags(cb->regular.info->fla= gs)); + cb->regular.f.vcpu_mem(cpu->cpu_index, make_plugin_meminfo(oi, rw), vaddr, cb->regular.userp); + qemu_plugin_set_cb_flags(cpu, QEMU_PLUGIN_CB_NO_REGS); } break; case PLUGIN_CB_INLINE_ADD_U64: @@ -760,3 +782,26 @@ void plugin_scoreboard_free(struct qemu_plugin_scorebo= ard *score) g_array_free(score->data, TRUE); g_free(score); } + +enum qemu_plugin_cb_flags tcg_call_to_qemu_plugin_cb_flags(int flags) +{ + if (flags & TCG_CALL_NO_RWG) { + return QEMU_PLUGIN_CB_NO_REGS; + } else if (flags & TCG_CALL_NO_WG) { + return QEMU_PLUGIN_CB_R_REGS; + } else { + return QEMU_PLUGIN_CB_RW_REGS; + } +} + +void qemu_plugin_set_cb_flags(CPUState *cpu, enum qemu_plugin_cb_flags fla= gs) +{ + assert(cpu); + current_cpu->neg.plugin_cb_flags =3D flags; +} + +enum qemu_plugin_cb_flags qemu_plugin_get_cb_flags(void) +{ + assert(current_cpu); + return current_cpu->neg.plugin_cb_flags; +} --=20 2.49.0 From nobody Mon Feb 9 02:55:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497096; cv=none; d=zohomail.com; s=zohoarc; b=fE2b2v8FJskwoXv73myYIMaK6xIHB0g0EkZtLECRFz3ync6zyzoZPIdlODjOs7lcAp2iEnfppN3Ki2zMO4+Wmz0rH6g17InwW9yUfuJDlPDyQf31fixhuj5kdEc0V4t1W0695voO2LCLiUbOx5JrluJn6oG4mwBO6OuxDsFaCtw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497096; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Xl2CsCbMIdd7IA6Ry113lx92Qe9OahNuvq02EUlL3YI=; b=aXc4b44K0L5uLSCCpdnCfCNJ7+BtQStIZrEpAzzL4W42gPtV8zjDyRFL37Io5gexY00Lvh1c9Ixusea818ybop34T5+MzenBM2MZkUDxe3x223w92BCrQuGyGPsL8J6lmshhDONJGhyM40CJTLioKZV8pioMLxLiOHm3N9piMpI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1749497096211135.0858519733132; Mon, 9 Jun 2025 12:24:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi6B-0007v5-1F; Mon, 09 Jun 2025 15:23:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi64-0007sv-Sb for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:52 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi61-0002R9-DX for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:51 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-311d5fdf1f0so3840703a91.1 for ; Mon, 09 Jun 2025 12:23:48 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497026; x=1750101826; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Xl2CsCbMIdd7IA6Ry113lx92Qe9OahNuvq02EUlL3YI=; b=IuqFCPN+9PkGWAXnrAnGU8HGRDkfCTO4SQ5KPjFt7KYVOP3SipUL9qWh8zvxBG8nnQ JUZzb+Ga5hR0s/C2jLSCDbP1vYmPRU775fPla0fi7pvJctJoX9i07cflrjgfTD9AKmyL NE8d5AwE6kbY9dwBAbmIeNG4cVkmUeQbG1eHkm0J1/3Yu2JHnSa7dEnVUzUGFQbGtGTQ OVGp0Frf8Hsh2IBNFAmnwlLyqJvKQjaD4ZOftfJaOSEgr04vz6tVNXsaeoMMQODUhftu mVTnz3N4hqUuzuN7Nk9YipuiCNjIZsX5HEsYMQYPmRFovkvVEVe5KQFa93ikikxQSqpC WtdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497026; x=1750101826; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xl2CsCbMIdd7IA6Ry113lx92Qe9OahNuvq02EUlL3YI=; b=am/SSB+lDP4QcuKsXv+jG86omTxDuNRzA9QB6EFYUfilUxYOfBt3zImILc8YpxGD+3 3KY3Tvl8L2+9/CuQ8HwHvkzBtx8Sumab1HCD/dvSOE+LtgYFl1IWftko9FttW0ANW7ug SnJ9F+1GNy7/Gn3T52eJs88PQAuMaiOwPoBZAMHhaK0h0BDectvDeC/jEHnDjoJRxfuP bIrmM53le+D3p1RMw3tjstYkJpFx4F7V6tW9PfFY50IHSJJnsKj2W7UUxbLHAYujB4Z/ X9pKZo5EWgmAJyPxgBUpy2EH21fRR8MdvG4QsQ1ndC4itotmi+9SxuZMSxo/L9fKk/E1 lZ8w== X-Gm-Message-State: AOJu0YwhsbqvQQqkPjszi4zoQL/GHNYLGma9JQ2Z5XorIz1i/iR4DXNf jOF6HhOCdU409u7U6db95gFtR1Ko10EfWOBJEXAlKPMSDXxmB+ZfDu1+uZN8P2v/ X-Gm-Gg: ASbGncvSUNZVlMO82USU7hEysmfE36ogYVgzi8MYgHBNMQUVItyvbTTx+o5psumB5qt 6e1L9Plr7hd8Moh5xwHlS7SJmaFls50hMN128E+QuU4rIyUN7DURc/wJ08JGzPnk76nqp7OoXG1 MxNUf2yXrrL7mMrVPHtMf5H1V1X6UTDsrKKVqpJyjxf0hZg0SOX33Y8W9EjLWxpYEDTUbPOXsr+ CCWmMiR/Q0UNKYBbx9aAo9Wv65Q+2HDpT+lrr9RIApr8AcUsWJaCoIIvA2+9pQuV7n3CFs39Kuv Z0f/L5YPevwKVYo/FUpIIOJn5bMsAUzW1mAhScKIxBW/uocs8U9ZJHnvQmbnsg== X-Google-Smtp-Source: AGHT+IGawz0/JCwXfpQ7l06DeQfD0DcT7TfXTZs/v6rB3KxNDck3lX+320YpJ0R7Brp9UrvTdDMAxA== X-Received: by 2002:a17:90b:17ca:b0:311:b0d3:855 with SMTP id 98e67ed59e1d1-31346af9a73mr19788076a91.2.1749497026449; Mon, 09 Jun 2025 12:23:46 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , novafacing Subject: [PATCH v10 4/8] plugins: Add memory virtual address write API Date: Mon, 9 Jun 2025 12:23:38 -0700 Message-ID: <20250609192342.316156-5-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=rowanbhart@gmail.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497096863116600 Content-Type: text/plain; charset="utf-8" From: novafacing This patch adds functions to the plugins API to allow reading and writing memory via virtual addresses. These functions only permit doing so on the current CPU, because there is no way to ensure consistency if plugins are allowed to read or write to other CPUs that aren't currently in the context of the plugin. Reviewed-by: Pierrick Bouvier Signed-off-by: Rowan Hart --- include/qemu/qemu-plugin.h | 21 +++++++++++++++++++++ plugins/api.c | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 9c9ebf6ce0..4167c46c2a 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -958,6 +958,27 @@ QEMU_PLUGIN_API bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len); =20 +/** + * qemu_plugin_write_memory_vaddr() - write to memory using a virtual addr= ess + * + * @addr: A virtual address to write to + * @data: A byte array containing the data to write + * + * The contents of @data will be written to memory starting at the virtual + * address @addr. + * + * This function does not guarantee consistency of writes, nor does it ens= ure + * that pending writes are flushed either before or after the write takes = place, + * so callers should take care to only call this function in vCPU context = (i.e. + * in callbacks) and avoid depending on the existence of data written usin= g this + * function which may be overwritten afterward. + * + * Returns true on success and false on failure. + */ +QEMU_PLUGIN_API +bool qemu_plugin_write_memory_vaddr(uint64_t addr, + GByteArray *data); + /** * qemu_plugin_scoreboard_new() - alloc a new scoreboard * diff --git a/plugins/api.c b/plugins/api.c index 3f04399c26..1f64a9ea64 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -476,6 +476,24 @@ bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByt= eArray *data, size_t len) return true; } =20 +bool qemu_plugin_write_memory_vaddr(uint64_t addr, GByteArray *data) +{ + g_assert(current_cpu); + + if (data->len =3D=3D 0) { + return false; + } + + int result =3D cpu_memory_rw_debug(current_cpu, addr, data->data, + data->len, true); + + if (result < 0) { + return false; + } + + return true; +} + struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_s= ize) { return plugin_scoreboard_new(element_size); --=20 2.49.0 From nobody Mon Feb 9 02:55:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497169; cv=none; d=zohomail.com; s=zohoarc; b=dlRSVEMrkBezjqMu6Vktc+T/WkzMAdtpnIDVprzOAmnzLUsa2cx5LLvdnN/mKl9Nk28dle9Y+DGsAoyipRKgZKWPDP3ynErOOfU/q74a4P+Gd+0HmSLkWPgjVyxY/XvylfTurs4s6a9NDWRr1XWxKzm6djnacqgkfQAsZ8DDMmc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497169; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=97wbn1fyVd354+uZeRkmCwxgLDzBnsPU4MLpqSMDiPM=; b=ZcRqOK37b6E9VqTvY7UVs+Ug7TCcsXTuB/q3Be0lnucSvDZX2Y9JbOMdAZ8c08b4eIuxO6tQSyj4ESuj4atMaRtxnzSwtWFst5eX/NjFlrw45lAm09gkjFiF6/hyV/W3IBlMo/Eq8RQQ4g4v4nDu+/nBQ4fPCJyOs795HuM/sJY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1749497169347124.17655711389762; Mon, 9 Jun 2025 12:26:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi6E-0007wf-9t; Mon, 09 Jun 2025 15:24:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi69-0007uJ-Gy for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:58 -0400 Received: from mail-pg1-x530.google.com ([2607:f8b0:4864:20::530]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi61-0002RF-JM for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:57 -0400 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-b2f645eba5dso1893467a12.1 for ; Mon, 09 Jun 2025 12:23:49 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497027; x=1750101827; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=97wbn1fyVd354+uZeRkmCwxgLDzBnsPU4MLpqSMDiPM=; b=N7pmbJe40BJ1TUs5D6qToTS5CqdF91dUj8/ksQkuwWVqyLDLeJqqfiroUh7WB3pOGE hlbxWEcYa5q+7SAm7/0+0oigQgxagz8oA+HfRUMjFEFFlGzLuLcAG2T1+IKXnsUfytAg TPW2ie2hRjcjA75lV8oK3rz/kSlTLsM27ysaI9x2ExMpPxLdmwO5zbl5Gkzyyiyg4lgX vD836iJYGNUOEg5LwM9wP0rk3jeajReem9d/cJjoEQWR52k4ZX+9CIS/muhvIu/mmJz+ JD8HqFL+OroFEJnCN7g7r12j/mriK/g8+5SV/4JvmUgD7t8atv+gdhJ3MFK0Ks9U+P6D kcIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497027; x=1750101827; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=97wbn1fyVd354+uZeRkmCwxgLDzBnsPU4MLpqSMDiPM=; b=BIQnisDT2SN9K1lcXiXEOjNzSLWa8+RhTpNj9ITYbCQA2N7kOduxGf84xtsGySA00l XmdgJT0A/TQxumTdpQmULgQp+1kU6f8YUDqSVGEnwSOuBgpaMcIDz3V98MovPr0q+y1B rs7ZiAB06F+MPclQ5G+Xz3g2TbHUSnU2Qla1W2X8IuT+CugiiSsxws7hjWf6VrPD/1vO 7lwgoB8yr1OWfMbAcSXsQSVoRD5pnnXCeIl/Uv+G86CjvoqqAkVp2xv2rJLT2auMxoAl X6hr1gl2+qnHs0El52+/Y/vp+crO+brjXlGmPhj1ZlyZ9vmmP5qwsn2DtaeQMgw8vC5X o1Tg== X-Gm-Message-State: AOJu0YzsPHPsm1Q8eLj2h0/62AKci5VJ1AVNmyjSLPCW5kHvhGsUwdCu 7/5J3SsffFGlMFAGduaQO+pDRrV9AoJId+3BjUEdMP+upRPX/MQnSV95CsIaDfHrlIY= X-Gm-Gg: ASbGncvrmc9kuI4RsToCuKpuUo4Kkg2MGUY7oEJ2Y68PEcCkNGreH0ya37VMaWeJ7dM AzXSUA9PpCgtp49403TCDicA6cwFtaVQ4evNAEpJYmd2WQ9pxH1PB46TOZ/mQ1hAFfYYULeDoET eylwxU6Sv9zLmE7aVAxa7J6untbtbIa34W8yiT5dkJQlsxvCVCk4onHev5tmxP4WozJnypqKR/Z yTrS5+GWy8qfztuNx1Y0/Hpd2NAqBJcyawdIXuAFmp66LTWcZT79D7yewjDI7Tgn+HvqWuTVNwN tHir48aUKlYOutnyggpaLbeLc7CdDgS3bk+DMOsli7fVEhTghGZ2suEqGH5qHg== X-Google-Smtp-Source: AGHT+IEi8XGFwvK5BY/lD7vVVDQw1xSW2QOdi4LoDQcytV+s0zu/hg70TFevmDFl03LM6O9NKUKKtA== X-Received: by 2002:a17:90b:17c1:b0:30e:3737:7c87 with SMTP id 98e67ed59e1d1-3139dfe2b7emr776499a91.5.1749497027245; Mon, 09 Jun 2025 12:23:47 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , novafacing Subject: [PATCH v10 5/8] plugins: Add memory hardware address read/write API Date: Mon, 9 Jun 2025 12:23:39 -0700 Message-ID: <20250609192342.316156-6-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::530; envelope-from=rowanbhart@gmail.com; helo=mail-pg1-x530.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497171802116600 Content-Type: text/plain; charset="utf-8" From: novafacing This patch adds functions to the plugins API to allow plugins to read and write memory via hardware addresses. The functions use the current address space of the current CPU in order to avoid exposing address space information to users. A later patch may want to add a function to permit a specified address space, for example to facilitate architecture-specific plugins that want to operate on them, for example reading ARM secure memory. Reviewed-by: Pierrick Bouvier Signed-off-by: Rowan Hart --- include/qemu/qemu-plugin.h | 93 ++++++++++++++++++++++++++++++++++++ plugins/api.c | 97 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 4167c46c2a..5eecdccc67 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -979,6 +979,99 @@ QEMU_PLUGIN_API bool qemu_plugin_write_memory_vaddr(uint64_t addr, GByteArray *data); =20 +/** + * enum qemu_plugin_hwaddr_operation_result - result of a memory operation + * + * @QEMU_PLUGIN_HWADDR_OPERATION_OK: hwaddr operation succeeded + * @QEMU_PLUGIN_HWADDR_OPERATION_ERROR: unexpected error occurred + * @QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR: error in memory device + * @QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED: permission error + * @QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS: address was invalid + * @QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE: invalid address sp= ace + */ +enum qemu_plugin_hwaddr_operation_result { + QEMU_PLUGIN_HWADDR_OPERATION_OK, + QEMU_PLUGIN_HWADDR_OPERATION_ERROR, + QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR, + QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED, + QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS, + QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE, +}; + +/** + * qemu_plugin_read_memory_hwaddr() - read from memory using a hardware ad= dress + * + * @addr: The physical address to read from + * @data: A byte array to store data into + * @len: The number of bytes to read, starting from @addr + * + * @len bytes of data is read from the current memory space for the current + * vCPU starting at @addr and stored into @data. If @data is not large eno= ugh to + * hold @len bytes, it will be expanded to the necessary size, reallocatin= g if + * necessary. @len must be greater than 0. + * + * This function does not ensure writes are flushed prior to reading, so + * callers should take care when calling this function in plugin callbacks= to + * avoid attempting to read data which may not yet be written and should u= se + * the memory callback API instead. + * + * This function is only valid for softmmu targets. + * + * Returns a qemu_plugin_hwaddr_operation_result indicating the result of = the + * operation. + */ +QEMU_PLUGIN_API +enum qemu_plugin_hwaddr_operation_result +qemu_plugin_read_memory_hwaddr(uint64_t addr, GByteArray *data, size_t len= ); + +/** + * qemu_plugin_write_memory_hwaddr() - write to memory using a hardware ad= dress + * + * @addr: A physical address to write to + * @data: A byte array containing the data to write + * + * The contents of @data will be written to memory starting at the hardware + * address @addr in the current address space for the current vCPU. + * + * This function does not guarantee consistency of writes, nor does it ens= ure + * that pending writes are flushed either before or after the write takes = place, + * so callers should take care when calling this function in plugin callba= cks to + * avoid depending on the existence of data written using this function wh= ich + * may be overwritten afterward. In addition, this function requires that = the + * pages containing the address are not locked. Practically, this means th= at you + * should not write instruction memory in a current translation block insi= de a + * callback registered with qemu_plugin_register_vcpu_tb_trans_cb. + * + * You can, for example, write instruction memory in a current translation= block + * in a callback registered with qemu_plugin_register_vcpu_tb_exec_cb, alt= hough + * be aware that the write will not be flushed until after the translation= block + * has finished executing. In general, this function should be used to wr= ite + * data memory or to patch code at a known address, not in a current trans= lation + * block. + * + * This function is only valid for softmmu targets. + * + * Returns a qemu_plugin_hwaddr_operation_result indicating the result of = the + * operation. + */ +QEMU_PLUGIN_API +enum qemu_plugin_hwaddr_operation_result +qemu_plugin_write_memory_hwaddr(uint64_t addr, GByteArray *data); + +/** + * qemu_plugin_translate_vaddr() - translate virtual address for current v= CPU + * + * @vaddr: virtual address to translate + * @hwaddr: pointer to store the physical address + * + * This function is only valid in vCPU context (i.e. in callbacks) and is = only + * valid for softmmu targets. + * + * Returns true on success and false on failure. + */ +QEMU_PLUGIN_API +bool qemu_plugin_translate_vaddr(uint64_t vaddr, uint64_t *hwaddr); + /** * qemu_plugin_scoreboard_new() - alloc a new scoreboard * diff --git a/plugins/api.c b/plugins/api.c index 1f64a9ea64..eac04cc1f6 100644 --- a/plugins/api.c +++ b/plugins/api.c @@ -39,6 +39,7 @@ #include "qemu/main-loop.h" #include "qemu/plugin.h" #include "qemu/log.h" +#include "system/memory.h" #include "tcg/tcg.h" #include "exec/gdbstub.h" #include "exec/target_page.h" @@ -494,6 +495,102 @@ bool qemu_plugin_write_memory_vaddr(uint64_t addr, GB= yteArray *data) return true; } =20 +enum qemu_plugin_hwaddr_operation_result +qemu_plugin_read_memory_hwaddr(hwaddr addr, GByteArray *data, size_t len) +{ +#ifdef CONFIG_SOFTMMU + if (len =3D=3D 0) { + return QEMU_PLUGIN_HWADDR_OPERATION_ERROR; + } + + g_assert(current_cpu); + + + int as_idx =3D cpu_asidx_from_attrs(current_cpu, MEMTXATTRS_UNSPECIFIE= D); + AddressSpace *as =3D cpu_get_address_space(current_cpu, as_idx); + + if (as =3D=3D NULL) { + return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE; + } + + g_byte_array_set_size(data, len); + MemTxResult res =3D address_space_rw(as, addr, + MEMTXATTRS_UNSPECIFIED, data->data, + data->len, false); + + switch (res) { + case MEMTX_OK: + return QEMU_PLUGIN_HWADDR_OPERATION_OK; + case MEMTX_ERROR: + return QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR; + case MEMTX_DECODE_ERROR: + return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS; + case MEMTX_ACCESS_ERROR: + return QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED; + default: + return QEMU_PLUGIN_HWADDR_OPERATION_ERROR; + } +#else + return QEMU_PLUGIN_HWADDR_OPERATION_ERROR; +#endif +} + +enum qemu_plugin_hwaddr_operation_result +qemu_plugin_write_memory_hwaddr(hwaddr addr, GByteArray *data) +{ +#ifdef CONFIG_SOFTMMU + if (data->len =3D=3D 0) { + return QEMU_PLUGIN_HWADDR_OPERATION_ERROR; + } + + g_assert(current_cpu); + + int as_idx =3D cpu_asidx_from_attrs(current_cpu, MEMTXATTRS_UNSPECIFIE= D); + AddressSpace *as =3D cpu_get_address_space(current_cpu, as_idx); + + if (as =3D=3D NULL) { + return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS_SPACE; + } + + MemTxResult res =3D address_space_rw(as, addr, + MEMTXATTRS_UNSPECIFIED, data->data, + data->len, true); + switch (res) { + case MEMTX_OK: + return QEMU_PLUGIN_HWADDR_OPERATION_OK; + case MEMTX_ERROR: + return QEMU_PLUGIN_HWADDR_OPERATION_DEVICE_ERROR; + case MEMTX_DECODE_ERROR: + return QEMU_PLUGIN_HWADDR_OPERATION_INVALID_ADDRESS; + case MEMTX_ACCESS_ERROR: + return QEMU_PLUGIN_HWADDR_OPERATION_ACCESS_DENIED; + default: + return QEMU_PLUGIN_HWADDR_OPERATION_ERROR; + } +#else + return QEMU_PLUGIN_HWADDR_OPERATION_ERROR; +#endif +} + +bool qemu_plugin_translate_vaddr(uint64_t vaddr, uint64_t *hwaddr) +{ +#ifdef CONFIG_SOFTMMU + g_assert(current_cpu); + + uint64_t res =3D cpu_get_phys_page_debug(current_cpu, vaddr); + + if (res =3D=3D (uint64_t)-1) { + return false; + } + + *hwaddr =3D res | (vaddr & ~TARGET_PAGE_MASK); + + return true; +#else + return false; +#endif +} + struct qemu_plugin_scoreboard *qemu_plugin_scoreboard_new(size_t element_s= ize) { return plugin_scoreboard_new(element_size); --=20 2.49.0 From nobody Mon Feb 9 02:55:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497159; cv=none; d=zohomail.com; s=zohoarc; b=CDbKamZY11HbbNwDW1M2bMbFE/5Aswq4FPrvWJ+XyDSdt5NaiNZdpcLPtx5pWjbMJuR+DRTUTqHf9gZiBNun5xkiehx9pO8rFd4VbYWMhhZZb9/ke6EKn67YEG1khvSsihUDPlQWOu/GGximj1n2DkGiOKKW24f2d5Q9lBrw5Ho= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497159; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2ap6I1EkC90RWD1sn5nrnpOFuEPRFLgHmwY9jqJf898=; b=BsrJe1A46xoiiDOxSV6dQcpYkswupI9EUUWXzn7LlprtH6dvKlSZk1a+IvCjqVsc1lA5IE8QXANP78LX16GwoYb4sAWX4Gu/8fq9tqvbW/pqURHMAckMS6eDNCrs8afty9FtWQWf2ELcI6Rq0WiLql6fLx0YnVHyg04LCq9RUE4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1749497159359887.3558557602636; Mon, 9 Jun 2025 12:25:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi6M-0007yN-Kt; Mon, 09 Jun 2025 15:24:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi66-0007tw-Q1 for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:54 -0400 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi61-0002RP-Uo for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:54 -0400 Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-306b6ae4fb2so4089203a91.3 for ; Mon, 09 Jun 2025 12:23:49 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497028; x=1750101828; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2ap6I1EkC90RWD1sn5nrnpOFuEPRFLgHmwY9jqJf898=; b=KlXKhZJuYz/u8qmUPXj6jB2Y+4loakCV+Plw+Td1XN+EaUjzXlBduk9nOpQSvyMWgU arRiHXSMySOg54zPRSC89CVRHoXvnDrqXX3injp/qG1PDwwh6veKqca7c3sXNJLL6eud duuPQZ3dUVN4NaSrchheZ2JGjCRCRwfS5SGpoIWj8bfKcxrzLyo6s3daAkpvbhk9fKGt Vzq66fmQ6NrhkFbFHBjkh4eFwRVKIY29jXXduOf9WMi8VdeIx/67iPOXjTGtlXRXdEWT yx9RcF/y9SJgnr6/LoFWk5aXUbFSy9mDowESjWxGenRoiAlQGVaiLFM9X+jvtaNDdenr ApCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497028; x=1750101828; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2ap6I1EkC90RWD1sn5nrnpOFuEPRFLgHmwY9jqJf898=; b=mHPP7z2kBNKVaNikcL8jyV9VhlPpYRQ42ZmOpZo7nFjH+iwkKDpAwMhB4KihcSEtg9 O8KC3sUL6s16lX0fS5qCO/SONRAEbFpRL3jSmaIXOwZ2lWRErs9OeWnqJBUuoDaUIs/k j0BHBmGTpX2ny+UZC7DYaZVA4Y6X3tb1RfU5aXfWuLgNtxcLguensoZL0unWwxghTAA2 3cyjhftJ8mcjRmgl5JE174ET1OhU2GyoxP/ATgFuOsv/tEdPt9xFb1FlsJdsxwCrvuf5 l1n+0CwAnQW4pbLtcFbYX9gVKU1zSUGjbCku1Ze522WR9DkRaejLOk2ADkjJyHf52SUZ S8og== X-Gm-Message-State: AOJu0YzWUgZdLwLekcT3eSDP4AJJkfdVXYWlMfv+WlDsf2w6gQWBBln8 jSgv6qYvkVD8F+1EoGEOt51uVX9lIuxZHwCNc7WOMdR1ngT+blfzYjrLq46RXYDRHx0= X-Gm-Gg: ASbGncud9IfuhxCeAfXgg7NID18axeGHWabif2IBMoU8EvS2Xwws2OymJ/oI/ZhSSBt DvKnuCiTNf/yr1g5R1EaFxWg7Q3bLkogVZpzp8u3TUB4ZOLxLUwKgsiZ7u/jPV9D8KML2EGNzJk cMoMJnyt/VWzsNjjeXN0+oRUChSUtP8wSRnHcVdBWm3PQqj+4Hd/7pED17hyFr06K/cZuxIu7xW qAO2kdcgQl0DtMVIXyHryhqkOz7EmRAnTKD+6wl5T61CKtDbPSH9B1nr5RAmtbotoUeU0gm7KO3 DDaLPTl+cPcGJ3KzahfR7+YNf79hd+Buwv7HgvOeasCbN36II1A= X-Google-Smtp-Source: AGHT+IGgAizrw3BHnNbUrAa5WsOPHH9gfyNXHR4767b98duf6zVKbMMA5fZzxg7SF6/lV7kbnaxiWw== X-Received: by 2002:a17:90b:52cf:b0:313:28f1:fc1b with SMTP id 98e67ed59e1d1-31347301ddbmr23597628a91.9.1749497027946; Mon, 09 Jun 2025 12:23:47 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , novafacing Subject: [PATCH v10 6/8] plugins: Add patcher plugin and test Date: Mon, 9 Jun 2025 12:23:40 -0700 Message-ID: <20250609192342.316156-7-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102b; envelope-from=rowanbhart@gmail.com; helo=mail-pj1-x102b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497161635116600 Content-Type: text/plain; charset="utf-8" From: novafacing This patch adds a plugin that exercises the virtual and hardware memory read-write API functions added in a previous patch. The plugin takes a target and patch byte sequence, and will overwrite any instruction matching the target byte sequence with the patch. Signed-off-by: Rowan Hart --- tests/tcg/Makefile.target | 1 + tests/tcg/plugins/meson.build | 2 +- tests/tcg/plugins/patch.c | 297 ++++++++++++++++++++++ tests/tcg/x86_64/Makefile.softmmu-target | 32 ++- tests/tcg/x86_64/system/patch-target.c | 27 ++ tests/tcg/x86_64/system/validate-patch.py | 39 +++ 6 files changed, 392 insertions(+), 6 deletions(-) create mode 100644 tests/tcg/plugins/patch.c create mode 100644 tests/tcg/x86_64/system/patch-target.c create mode 100755 tests/tcg/x86_64/system/validate-patch.py diff --git a/tests/tcg/Makefile.target b/tests/tcg/Makefile.target index 95ff76ea44..4b709a9d18 100644 --- a/tests/tcg/Makefile.target +++ b/tests/tcg/Makefile.target @@ -176,6 +176,7 @@ RUN_TESTS+=3D$(EXTRA_RUNS) # Some plugins need additional arguments above the default to fully # exercise things. We can define them on a per-test basis here. run-plugin-%-with-libmem.so: PLUGIN_ARGS=3D$(COMMA)inline=3Dtrue +run-plugin-%-with-libpatch.so: PLUGIN_ARGS=3D$(COMMA)target=3Dffffffff$(CO= MMA)patch=3D00000000 =20 ifeq ($(filter %-softmmu, $(TARGET)),) run-%: % diff --git a/tests/tcg/plugins/meson.build b/tests/tcg/plugins/meson.build index 41f02f2c7f..163042e601 100644 --- a/tests/tcg/plugins/meson.build +++ b/tests/tcg/plugins/meson.build @@ -1,6 +1,6 @@ t =3D [] if get_option('plugins') - foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall'] + foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall',= 'patch'] if host_os =3D=3D 'windows' t +=3D shared_module(i, files(i + '.c') + '../../../contrib/plugins/= win32_linker.c', include_directories: '../../../include/qemu', diff --git a/tests/tcg/plugins/patch.c b/tests/tcg/plugins/patch.c new file mode 100644 index 0000000000..6e83418b85 --- /dev/null +++ b/tests/tcg/plugins/patch.c @@ -0,0 +1,297 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This plugin patches instructions matching a pattern to a different + * instruction as they execute + * + */ + +#include "glib.h" +#include "glibconfig.h" + +#include +#include +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + +static bool use_hwaddr; +static bool debug_insns; +static GByteArray *target_data; +static GByteArray *patch_data; + +/** + * Parse a string of hexadecimal digits into a GByteArray. The string must= be + * even length + */ +static GByteArray *str_to_bytes(const char *str) +{ + GByteArray *bytes =3D g_byte_array_new(); + char byte[3] =3D {0}; + size_t len =3D strlen(str); + guint8 value =3D 0; + + if (len % 2 !=3D 0) { + g_byte_array_free(bytes, true); + return NULL; + } + + for (size_t i =3D 0; i < len; i +=3D 2) { + byte[0] =3D str[i]; + byte[1] =3D str[i + 1]; + value =3D (guint8)g_ascii_strtoull(byte, NULL, 16); + g_byte_array_append(bytes, &value, 1); + } + + return bytes; +} + +static void patch_hwaddr(unsigned int vcpu_index, void *userdata) +{ + uint64_t addr =3D (uint64_t)userdata; + GString *str =3D g_string_new(NULL); + g_string_printf(str, "patching: @0x%" + PRIx64 "\n", + addr); + qemu_plugin_outs(str->str); + g_string_free(str, true); + + enum qemu_plugin_hwaddr_operation_result result =3D + qemu_plugin_write_memory_hwaddr(addr, patch_data); + + + if (result !=3D QEMU_PLUGIN_HWADDR_OPERATION_OK) { + GString *errmsg =3D g_string_new(NULL); + g_string_printf(errmsg, "Failed to write memory: %d\n", result); + qemu_plugin_outs(errmsg->str); + g_string_free(errmsg, true); + return; + } + + GByteArray *read_data =3D g_byte_array_new(); + + result =3D qemu_plugin_read_memory_hwaddr(addr, read_data, + patch_data->len); + + qemu_plugin_outs("Reading memory...\n"); + + if (result !=3D QEMU_PLUGIN_HWADDR_OPERATION_OK) { + GString *errmsg =3D g_string_new(NULL); + g_string_printf(errmsg, "Failed to read memory: %d\n", result); + qemu_plugin_outs(errmsg->str); + g_string_free(errmsg, true); + return; + } + + if (memcmp(patch_data->data, read_data->data, patch_data->len) !=3D 0)= { + qemu_plugin_outs("Failed to read back written data\n"); + } + + qemu_plugin_outs("Success!\n"); + + return; +} + +static void patch_vaddr(unsigned int vcpu_index, void *userdata) +{ + uint64_t addr =3D (uint64_t)userdata; + uint64_t hwaddr =3D 0; + if (!qemu_plugin_translate_vaddr(addr, &hwaddr)) { + qemu_plugin_outs("Failed to translate vaddr\n"); + return; + } + GString *str =3D g_string_new(NULL); + g_string_printf(str, "patching: @0x%" + PRIx64 " hw: @0x%" PRIx64 "\n", + addr, hwaddr); + qemu_plugin_outs(str->str); + g_string_free(str, true); + + qemu_plugin_outs("Writing memory (vaddr)...\n"); + + if (!qemu_plugin_write_memory_vaddr(addr, patch_data)) { + qemu_plugin_outs("Failed to write memory\n"); + return; + } + + qemu_plugin_outs("Reading memory (vaddr)...\n"); + + + GByteArray *read_data =3D g_byte_array_new(); + + if (!qemu_plugin_read_memory_vaddr(addr, read_data, patch_data->len)) { + qemu_plugin_outs("Failed to read memory\n"); + return; + } + + if (memcmp(patch_data->data, read_data->data, patch_data->len) !=3D 0)= { + qemu_plugin_outs("Failed to read back written data\n"); + } + + qemu_plugin_outs("Success!\n"); + + return; +} + +static void debug_disas(unsigned int vcpu_index, void *userdata) +{ + GString *debug_info =3D (GString *)userdata; + qemu_plugin_outs(debug_info->str); +} + +static void debug_print_newline(unsigned int vcpu_index, void *userdata) +{ + qemu_plugin_outs("\n"); +} + +/* + * Callback on translation of a translation block. + */ +static void vcpu_tb_trans_cb(qemu_plugin_id_t id, struct qemu_plugin_tb *t= b) +{ + uint64_t addr =3D 0; + GByteArray *insn_data =3D g_byte_array_new(); + for (size_t i =3D 0; i < qemu_plugin_tb_n_insns(tb); i++) { + struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(tb, i); + + if (use_hwaddr) { + uint64_t vaddr =3D qemu_plugin_insn_vaddr(insn); + if (!qemu_plugin_translate_vaddr(vaddr, &addr)) { + qemu_plugin_outs("Failed to translate vaddr\n"); + continue; + } + } else { + addr =3D qemu_plugin_insn_vaddr(insn); + } + + g_byte_array_set_size(insn_data, qemu_plugin_insn_size(insn)); + qemu_plugin_insn_data(insn, insn_data->data, insn_data->len); + + if (insn_data->len >=3D target_data->len && + !memcmp(insn_data->data, target_data->data, + MIN(target_data->len, insn_data->len))) { + if (use_hwaddr) { + qemu_plugin_register_vcpu_tb_exec_cb(tb, patch_hwaddr, + QEMU_PLUGIN_CB_NO_REG= S, + (void *)addr); + } else { + qemu_plugin_register_vcpu_tb_exec_cb(tb, patch_vaddr, + QEMU_PLUGIN_CB_NO_REG= S, + (void *)addr); + } + } + } + for (size_t i =3D 0; i < qemu_plugin_tb_n_insns(tb); i++) { + struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(tb, i); + uint64_t vaddr =3D qemu_plugin_insn_vaddr(insn); + uint64_t hwaddr =3D (uint64_t)qemu_plugin_insn_haddr(insn); + uint64_t translated_hwaddr =3D 0; + if (!qemu_plugin_translate_vaddr(vaddr, &translated_hwaddr)) { + qemu_plugin_outs("Failed to translate vaddr\n"); + continue; + } + char *disas =3D qemu_plugin_insn_disas(insn); + GString *str =3D g_string_new(NULL); + g_string_printf(str, + "vaddr: 0x%" PRIx64 " hwaddr: 0x%" PRIx64 + " translated: 0x%" PRIx64 " : %s\n", + vaddr, hwaddr, translated_hwaddr, disas); + g_free(disas); + if (debug_insns) { + qemu_plugin_register_vcpu_insn_exec_cb(insn, debug_disas, + QEMU_PLUGIN_CB_NO_REGS, + str); + } + + } + + if (debug_insns) { + qemu_plugin_register_vcpu_tb_exec_cb(tb, debug_print_newline, + QEMU_PLUGIN_CB_NO_REGS, + NULL); + } + + g_byte_array_free(insn_data, true); +} + +static void usage(void) +{ + fprintf(stderr, "Usage: ,target=3D,patch=3D" + "[,use_hwaddr=3D]" + "[,debug_insns=3D]\n"); +} + +/* + * Called when the plugin is installed + */ +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, + const qemu_info_t *info, int ar= gc, + char **argv) +{ + + use_hwaddr =3D true; + debug_insns =3D false; + target_data =3D NULL; + patch_data =3D NULL; + + if (argc > 4) { + usage(); + return -1; + } + + for (size_t i =3D 0; i < argc; i++) { + char *opt =3D argv[i]; + g_auto(GStrv) tokens =3D g_strsplit(opt, "=3D", 2); + if (g_strcmp0(tokens[0], "use_hwaddr") =3D=3D 0) { + if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &use_hwaddr)= ) { + fprintf(stderr, + "Failed to parse boolean argument use_hwaddr\n"); + return -1; + } + } else if (g_strcmp0(tokens[0], "debug_insns") =3D=3D 0) { + if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &debug_insns= )) { + fprintf(stderr, + "Failed to parse boolean argument debug_insns\n"); + return -1; + } + } else if (g_strcmp0(tokens[0], "target") =3D=3D 0) { + target_data =3D str_to_bytes(tokens[1]); + if (!target_data) { + fprintf(stderr, + "Failed to parse target bytes.\n"); + return -1; + } + } else if (g_strcmp0(tokens[0], "patch") =3D=3D 0) { + patch_data =3D str_to_bytes(tokens[1]); + if (!patch_data) { + fprintf(stderr, "Failed to parse patch bytes.\n"); + return -1; + } + } else { + fprintf(stderr, "Unknown argument: %s\n", tokens[0]); + usage(); + return -1; + } + } + + if (!target_data) { + fprintf(stderr, "target argument is required\n"); + usage(); + return -1; + } + + if (!patch_data) { + fprintf(stderr, "patch argument is required\n"); + usage(); + return -1; + } + + if (target_data->len !=3D patch_data->len) { + fprintf(stderr, "Target and patch data must be the same length\n"); + return -1; + } + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans_cb); + + return 0; +} diff --git a/tests/tcg/x86_64/Makefile.softmmu-target b/tests/tcg/x86_64/Ma= kefile.softmmu-target index ef6bcb4dc7..8d3a067c33 100644 --- a/tests/tcg/x86_64/Makefile.softmmu-target +++ b/tests/tcg/x86_64/Makefile.softmmu-target @@ -7,18 +7,27 @@ # =20 I386_SYSTEM_SRC=3D$(SRC_PATH)/tests/tcg/i386/system -X64_SYSTEM_SRC=3D$(SRC_PATH)/tests/tcg/x86_64/system +X86_64_SYSTEM_SRC=3D$(SRC_PATH)/tests/tcg/x86_64/system =20 # These objects provide the basic boot code and helper functions for all t= ests CRT_OBJS=3Dboot.o =20 -CRT_PATH=3D$(X64_SYSTEM_SRC) -LINK_SCRIPT=3D$(X64_SYSTEM_SRC)/kernel.ld +X86_64_TEST_C_SRCS=3D$(wildcard $(X86_64_SYSTEM_SRC)/*.c) +X86_64_TEST_S_SRCS=3D + +X86_64_C_TESTS =3D $(patsubst $(X86_64_SYSTEM_SRC)/%.c, %, $(X86_64_TEST_C= _SRCS)) +X86_64_S_TESTS =3D $(patsubst $(X86_64_SYSTEM_SRC)/%.S, %, $(X86_64_TEST_S= _SRCS)) + +X86_64_TESTS =3D $(X86_64_C_TESTS) +X86_64_TESTS +=3D $(X86_64_S_TESTS) + +CRT_PATH=3D$(X86_64_SYSTEM_SRC) +LINK_SCRIPT=3D$(X86_64_SYSTEM_SRC)/kernel.ld LDFLAGS=3D-Wl,-T$(LINK_SCRIPT) -Wl,-melf_x86_64 CFLAGS+=3D-nostdlib -ggdb -O0 $(MINILIB_INC) LDFLAGS+=3D-static -nostdlib $(CRT_OBJS) $(MINILIB_OBJS) -lgcc =20 -TESTS+=3D$(MULTIARCH_TESTS) +TESTS+=3D$(X86_64_TESTS) $(MULTIARCH_TESTS) EXTRA_RUNS+=3D$(MULTIARCH_RUNS) =20 # building head blobs @@ -27,11 +36,24 @@ EXTRA_RUNS+=3D$(MULTIARCH_RUNS) %.o: $(CRT_PATH)/%.S $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -Wa,--noexecstack -c $< -o $@ =20 -# Build and link the tests +# Build and link the multiarch tests %: %.c $(LINK_SCRIPT) $(CRT_OBJS) $(MINILIB_OBJS) $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) =20 +# Build and link the arch tests +%: $(X86_64_SYSTEM_SRC)/%.c $(LINK_SCRIPT) $(CRT_OBJS) $(MINILIB_OBJS) + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) + memory: CFLAGS+=3D-DCHECK_UNALIGNED=3D1 +patch-target: CFLAGS+=3D-O0 =20 # Running QEMU_OPTS+=3D-device isa-debugcon,chardev=3Doutput -device isa-debug-exit,= iobase=3D0xf4,iosize=3D0x4 -kernel + +# Add patch-target to ADDITIONAL_PLUGINS_TESTS +ADDITIONAL_PLUGINS_TESTS +=3D patch-target + +run-plugin-patch-target-with-libpatch.so: \ + PLUGIN_ARGS=3D$(COMMA)target=3Dffc0$(COMMA)patch=3D9090$(COMMA)use_hwaddr= =3Dtrue$(COMMA)debug_insns=3Dfalse +run-plugin-patch-target-with-libpatch.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=3D$(X86_64_SYSTEM_SRC)/validate-patch.py $@.o= ut \ No newline at end of file diff --git a/tests/tcg/x86_64/system/patch-target.c b/tests/tcg/x86_64/syst= em/patch-target.c new file mode 100644 index 0000000000..8a7c0a0ae8 --- /dev/null +++ b/tests/tcg/x86_64/system/patch-target.c @@ -0,0 +1,27 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This test target increments a value 100 times. The patcher converts the + * inc instruction to a nop, so it only increments the value once. + * + */ +#include + +int main(void) +{ + ml_printf("Running test...\n"); +#if defined(__x86_64__) + ml_printf("Testing insn memory read/write...\n"); + unsigned int x =3D 0; + for (int i =3D 0; i < 100; i++) { + asm volatile ( + "inc %[x]" + : [x] "+a" (x) + ); + } + ml_printf("Value: %d\n", x); +#else + #error "This test is only valid for x86_64 architecture." +#endif + return 0; +} diff --git a/tests/tcg/x86_64/system/validate-patch.py b/tests/tcg/x86_64/s= ystem/validate-patch.py new file mode 100755 index 0000000000..700950eae5 --- /dev/null +++ b/tests/tcg/x86_64/system/validate-patch.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# +# validate-patch.py: check the patch applies +# +# This program takes two inputs: +# - the plugin output +# - the binary output +# +# Copyright (C) 2024 +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import sys +from argparse import ArgumentParser + +def main() -> None: + """ + Process the arguments, injest the program and plugin out and + verify they match up and report if they do not. + """ + parser =3D ArgumentParser(description=3D"Validate patch") + parser.add_argument('test_output', + help=3D"The output from the test itself") + parser.add_argument('plugin_output', + help=3D"The output from plugin") + args =3D parser.parse_args() + + with open(args.test_output, 'r') as f: + test_data =3D f.read() + with open(args.plugin_output, 'r') as f: + plugin_data =3D f.read() + if "Value: 1" in test_data: + sys.exit(0) + else: + sys.exit(1) + +if __name__ =3D=3D "__main__": + main() + --=20 2.49.0 From nobody Mon Feb 9 02:55:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497116; cv=none; d=zohomail.com; s=zohoarc; b=VXseMfZrYZZMRNeRCgYi0gzmhA71n3AkZEmIa/BWl4uD2jO6YX9nU/buGwBTFxE82kObOCvK001IjxZpz7nKpDeSGeEILSE4QhbcS6OS0kZdg6W9d8srs8BHCHAAVyX7oYoX/0sc51C0pxP/HmZGE8KbT96IBPEGK+H/+NenwEM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497116; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Z0nlYktoATC6j3lpIaHBr3lSUsxpIXbgJCeWazuE310=; b=ackyWST+9RFID7+KRq7M7r+hDP0tUsvL2KG8lfC18Eraj+GWg0efcj6Y+rWXSAvYQGlh1p5W2WyRHLRt7BeybYjyFD8Mpp7MwdzyyGgfMG/3pOnKBE7E3leLt4hf4QtKSiHKh5aEZLkuOFTDCZ/W0pq/2ttEpjb3tOdAiem4Fog= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1749497116298364.2883519944934; Mon, 9 Jun 2025 12:25:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi6F-0007x9-Tm; Mon, 09 Jun 2025 15:24:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi6B-0007vh-9E for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:59 -0400 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi62-0002Ra-SC for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:59 -0400 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-b1fde81de05so2406906a12.1 for ; Mon, 09 Jun 2025 12:23:50 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497029; x=1750101829; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z0nlYktoATC6j3lpIaHBr3lSUsxpIXbgJCeWazuE310=; b=HN9v2+WAUHNhfETJOkL/A+9tvpG1+nfrkonRT2oytZVatYSQNMy3Q8Q0HcAsfGJ9Eg J5jCjdi4n8ex7Fqm7Dh7pfNBMNvfS6mPuIhFetddKmz5szoTQEFO/NKzy6RpeAnzTqe/ qp3IHZ6VfUAJGeblOYXKM0pVhjz8z1QHATomJjJiYyxKAN9fB6BULSXxKb7UL8fdcM83 p8dgzOrQL9M+LtHgAc886ZSCvXaqnCkqOGvWDAuGE7El7fTGnfFqxjHGdx5AsWi1G4y2 G8UnPeIMebJNHAtKMTuz3CzTLe6PomxMqVIi7EX5o96bo39i6bEeP+BeF1bKldDbbJDa OA2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497029; x=1750101829; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z0nlYktoATC6j3lpIaHBr3lSUsxpIXbgJCeWazuE310=; b=EQqnb/miRkrc+WX17izFOihszYZzK+ZUeSRa0gz9WGEwSsL2p7XEbDAnFJwdEOqu/d FbTQmE1N264HbwzGJzopak3J6xhkqzbbZSK2EFztlSiB20kkeP+tXQRT50vxxkaMetoo sOaJkCkSx4m1SrHKsxt7IXi33ZmU2uWD4jHZve0wdTx9axU/81UG0vy+Vel6sgWdNczQ XjN2wso4Foa8C/U154aBLZNDbO1G1SRjap+bj4QWd0ju+kPiFnbB1BetGigQEw7srZET xMUv6AB8zYVlQC99IWKpODCNrRsPgxyo2ecL4FXsvOMmvAdP5GuOZxDLWMoFAhDkzWdz TaqA== X-Gm-Message-State: AOJu0Yx5czA1Z6vzW6X4Z7cO4LCbideroFVn7d/5RiOLDnZJc54RHUAk lAj/UbssOBV+RO4bgLfdqGpvH8iz3AjLQAb8IY1zMwTcbnAmYNMt2dYw0b6wiydH X-Gm-Gg: ASbGncvuQcCIqiLSPo3Rp5nP1YU/JSCeWkdahoOeUyeI76981Utj0HGNieLDf1AY+WN 5ZuSt/k54TP05aD6s84sUiwQr1B1gctsuaww1Ad0CRoyHULA9jiI34l+2sTKx9IYFjnjPwhEFPU oTynSfTvEivNHg9woFeQ3fmsGd2nWgGo1OmtejA/6H7hkxkejVKT5fazNhHrJiHYl4wYHKFfxLT hbSHbwoCugCJdlCizwraTiOFCB2THOPVKKv+EcRDaMXakMcgGjeuysFquq3kmSwphbtvG3GBLvx gRM1G8hpOmSX4Sv0pWriwslFLYhtNwhSIpzCEG2AP+4cclCBBHIwqXS/P7kwUw== X-Google-Smtp-Source: AGHT+IFcOxaj0CtCk9LTBIGhB+H7YyjNSagZlFLkKpSQH4aLPc6qiB6oFbRKAx1lMJNEzNdiUxGRCA== X-Received: by 2002:a17:90b:164a:b0:301:9f62:a944 with SMTP id 98e67ed59e1d1-3134769f73dmr21816353a91.33.1749497028681; Mon, 09 Jun 2025 12:23:48 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , novafacing Subject: [PATCH v10 7/8] plugins: Add hypercalls plugin and test Date: Mon, 9 Jun 2025 12:23:41 -0700 Message-ID: <20250609192342.316156-8-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52a; envelope-from=rowanbhart@gmail.com; helo=mail-pg1-x52a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497117747116600 Content-Type: text/plain; charset="utf-8" From: novafacing This patch adds a plugin that implements a simple form of hypercalls from guest code to the plugin by using the register read API. It accepts only one hypercall, which writes a magic value to guest memory. Signed-off-by: Rowan Hart --- tests/tcg/Makefile.target | 1 + tests/tcg/plugins/hypercalls.c | 547 ++++++++++++++++++ tests/tcg/plugins/meson.build | 2 +- tests/tcg/x86_64/Makefile.softmmu-target | 6 +- tests/tcg/x86_64/system/hypercalls-target.c | 40 ++ .../tcg/x86_64/system/validate-hypercalls.py | 40 ++ 6 files changed, 634 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/plugins/hypercalls.c create mode 100644 tests/tcg/x86_64/system/hypercalls-target.c create mode 100755 tests/tcg/x86_64/system/validate-hypercalls.py diff --git a/tests/tcg/Makefile.target b/tests/tcg/Makefile.target index 4b709a9d18..5ac9638102 100644 --- a/tests/tcg/Makefile.target +++ b/tests/tcg/Makefile.target @@ -177,6 +177,7 @@ RUN_TESTS+=3D$(EXTRA_RUNS) # exercise things. We can define them on a per-test basis here. run-plugin-%-with-libmem.so: PLUGIN_ARGS=3D$(COMMA)inline=3Dtrue run-plugin-%-with-libpatch.so: PLUGIN_ARGS=3D$(COMMA)target=3Dffffffff$(CO= MMA)patch=3D00000000 +run-plugin-%-with-libhypercalls.so: PLUGIN_ARGS=3D$(COMMA)ignore_unsupport= ed=3Dtrue =20 ifeq ($(filter %-softmmu, $(TARGET)),) run-%: % diff --git a/tests/tcg/plugins/hypercalls.c b/tests/tcg/plugins/hypercalls.c new file mode 100644 index 0000000000..3214bb8ee5 --- /dev/null +++ b/tests/tcg/plugins/hypercalls.c @@ -0,0 +1,547 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This plugin implements a simple hypercall interface for guests (both sy= stem + * and user mode) to call certain operations from the host. + */ +#include "glib.h" +#include "glibconfig.h" +#include +#include +#include +#include +#include +#include +#include + +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + +#define AARCH64_N_HYPERCALL_INSNS (28) +#define AARCH64_HYPERCALL_INSN_LEN (4) +#define AARCH64_HYPERCALL_MAX (AARCH64_N_HYPERCALL_INSNS) +#define ARM_N_HYPERCALL_INSNS (12) +#define ARM_HYPERCALL_INSN_LEN (4) +#define ARM_HYPERCALL_MAX (ARM_N_HYPERCALL_INSNS) +#define X86_HYPERCALL_INSN_LEN (2) +#define X86_HYPERCALL_VALUE_BASE (0x4711) +#define X86_HYPERCALL_MAX (0x10000) +#define N_HYPERCALL_ARGS (4) + +static bool ignore_unsupported; + +static struct qemu_plugin_register *get_register(const char *name); +static uint64_t byte_array_to_uint64(GByteArray *buf); + +enum HypercallInsnType { + CONSTANT, + CALLBACK, +}; + + +/* + * Checks an instruction and returns its hypercall number, if it is + * a hypercall instruction, or -1 if it is not. Called at execution + * time. + */ +typedef int32_t (*hypercall_nr_cb)(GByteArray *); + +/* + * Checks an instruction and returns whether it is a hypercall, or -1 if i= t is + * not. Called at execution time. + */ +typedef bool (*is_hypercall_cb)(GByteArray *); + +/* + * Specifies a Hypercall for an architecture: + * + * - Architecture name + * - Whether it is enabled + * - The hypercall instruction + * - The register names to pass the hypercall # and args + */ +struct HypercallSpec { + const bool enabled; + const char *name; + const bool le; + const char *args[N_HYPERCALL_ARGS]; + const hypercall_nr_cb hypercall_nr_cb; + const is_hypercall_cb is_hypercall_cb; +}; + +static int32_t aarch64_hypercall_nr_cb(GByteArray *insn) +{ + if (insn->len !=3D AARCH64_HYPERCALL_INSN_LEN) { + return -1; + } + + static const uint8_t + hypercall_insns[AARCH64_N_HYPERCALL_INSNS][AARCH64_HYPERCALL_INSN_LEN]= =3D { + { 0xaa, 0x4, 0x0, 0x84 }, + { 0xaa, 0x5, 0x0, 0xa5 }, + { 0xaa, 0x6, 0x0, 0xc6 }, + { 0xaa, 0x7, 0x0, 0xe7 }, + { 0xaa, 0x8, 0x1, 0x8 }, + { 0xaa, 0x9, 0x1, 0x29 }, + { 0xaa, 0xa, 0x1, 0x4a }, + { 0xaa, 0xb, 0x1, 0x6b }, + { 0xaa, 0xc, 0x1, 0x8c }, + { 0xaa, 0xd, 0x1, 0xad }, + { 0xaa, 0xe, 0x1, 0xce }, + { 0xaa, 0xf, 0x1, 0xef }, + { 0xaa, 0x10, 0x2, 0x10 }, + { 0xaa, 0x11, 0x2, 0x31 }, + { 0xaa, 0x12, 0x2, 0x52 }, + { 0xaa, 0x13, 0x2, 0x73 }, + { 0xaa, 0x14, 0x2, 0x94 }, + { 0xaa, 0x15, 0x2, 0xb5 }, + { 0xaa, 0x16, 0x2, 0xd6 }, + { 0xaa, 0x17, 0x2, 0xf7 }, + { 0xaa, 0x18, 0x3, 0x18 }, + { 0xaa, 0x19, 0x3, 0x39 }, + { 0xaa, 0x1a, 0x3, 0x5a }, + { 0xaa, 0x1b, 0x3, 0x7b }, + { 0xaa, 0x1c, 0x3, 0x9c }, + { 0xaa, 0x1d, 0x3, 0xbd }, + { 0xaa, 0x1e, 0x3, 0xde }, + { 0xaa, 0x1f, 0x3, 0xff }, + }; + + for (int32_t i =3D 0; i < AARCH64_N_HYPERCALL_INSNS; i++) { + if (!memcmp(hypercall_insns[i], insn->data, insn->len)) { + return i; + } + } + return -1; +} + +static bool aarch64_is_hypercall_cb(GByteArray *insn) +{ + return aarch64_hypercall_nr_cb(insn) < 0; +} + + +static int32_t aarch64_be_hypercall_nr_cb(GByteArray *insn) +{ + if (insn->len !=3D AARCH64_HYPERCALL_INSN_LEN) { + return -1; + } + + static const uint8_t + hypercall_insns[AARCH64_N_HYPERCALL_INSNS][AARCH64_HYPERCALL_INSN_LEN]= =3D { + {0x84, 0x0, 0x4, 0xaa}, + {0xa5, 0x0, 0x5, 0xaa}, + {0xc6, 0x0, 0x6, 0xaa}, + {0xe7, 0x0, 0x7, 0xaa}, + {0x8, 0x1, 0x8, 0xaa}, + {0x29, 0x1, 0x9, 0xaa}, + {0x4a, 0x1, 0xa, 0xaa}, + {0x6b, 0x1, 0xb, 0xaa}, + {0x8c, 0x1, 0xc, 0xaa}, + {0xad, 0x1, 0xd, 0xaa}, + {0xce, 0x1, 0xe, 0xaa}, + {0xef, 0x1, 0xf, 0xaa}, + {0x10, 0x2, 0x10, 0xaa}, + {0x31, 0x2, 0x11, 0xaa}, + {0x52, 0x2, 0x12, 0xaa}, + {0x73, 0x2, 0x13, 0xaa}, + {0x94, 0x2, 0x14, 0xaa}, + {0xb5, 0x2, 0x15, 0xaa}, + {0xd6, 0x2, 0x16, 0xaa}, + {0xf7, 0x2, 0x17, 0xaa}, + {0x18, 0x3, 0x18, 0xaa}, + {0x39, 0x3, 0x19, 0xaa}, + {0x5a, 0x3, 0x1a, 0xaa}, + {0x7b, 0x3, 0x1b, 0xaa}, + {0x9c, 0x3, 0x1c, 0xaa}, + {0xbd, 0x3, 0x1d, 0xaa}, + {0xde, 0x3, 0x1e, 0xaa}, + {0xff, 0x3, 0x1f, 0xaa}, + }; + + for (int32_t i =3D 0; i < AARCH64_N_HYPERCALL_INSNS; i++) { + if (!memcmp(hypercall_insns[i], insn->data, insn->len)) { + return i; + } + } + return -1; +} + +static bool aarch64_be_is_hypercall_cb(GByteArray *insn) +{ + return aarch64_be_hypercall_nr_cb(insn) < 0; +} + + +static int32_t arm_hypercall_nr_cb(GByteArray *insn) +{ + if (insn->len !=3D ARM_HYPERCALL_INSN_LEN) { + return -1; + } + + static const uint8_t + hypercall_insns[ARM_N_HYPERCALL_INSNS][ARM_HYPERCALL_INSN_LEN] =3D { + { 0xe1, 0x84, 0x40, 0x4 }, + { 0xe1, 0x85, 0x50, 0x5 }, + { 0xe1, 0x86, 0x60, 0x6 }, + { 0xe1, 0x87, 0x70, 0x7 }, + { 0xe1, 0x88, 0x80, 0x8 }, + { 0xe1, 0x89, 0x90, 0x9 }, + { 0xe1, 0x8a, 0xa0, 0xa }, + { 0xe1, 0x8b, 0xb0, 0xb }, + { 0xe1, 0x8c, 0xc0, 0xc }, + { 0xe1, 0x8d, 0xd0, 0xd }, + { 0xe1, 0x8e, 0xe0, 0xe }, + { 0xe1, 0x8f, 0xf0, 0xf }, + }; + + for (int32_t i =3D 0; i < ARM_N_HYPERCALL_INSNS; i++) { + if (!memcmp(hypercall_insns[i], insn->data, insn->len)) { + return i; + } + } + return -1; +} + +static bool arm_is_hypercall_cb(GByteArray *insn) +{ + return arm_hypercall_nr_cb(insn) < 0; +} + +static int32_t arm_be_hypercall_nr_cb(GByteArray *insn) +{ + if (insn->len !=3D ARM_HYPERCALL_INSN_LEN) { + return -1; + } + + static const uint8_t + hypercall_insns[ARM_N_HYPERCALL_INSNS][ARM_HYPERCALL_INSN_LEN] =3D { + {0x4, 0x40, 0x84, 0xe1}, + {0x5, 0x50, 0x85, 0xe1}, + {0x6, 0x60, 0x86, 0xe1}, + {0x7, 0x70, 0x87, 0xe1}, + {0x8, 0x80, 0x88, 0xe1}, + {0x9, 0x90, 0x89, 0xe1}, + {0xa, 0xa0, 0x8a, 0xe1}, + {0xb, 0xb0, 0x8b, 0xe1}, + {0xc, 0xc0, 0x8c, 0xe1}, + {0xd, 0xd0, 0x8d, 0xe1}, + {0xe, 0xe0, 0x8e, 0xe1}, + {0xf, 0xf0, 0x8f, 0xe1}, + }; + + for (int32_t i =3D 0; i < ARM_N_HYPERCALL_INSNS; i++) { + if (!memcmp(hypercall_insns[i], insn->data, insn->len)) { + return i; + } + } + return -1; +} + +static bool arm_be_is_hypercall_cb(GByteArray *insn) +{ + return arm_be_hypercall_nr_cb(insn) < 0; +} + +static int32_t x86_64_hypercall_nr_cb(GByteArray *insn) +{ + if (insn->len !=3D X86_HYPERCALL_INSN_LEN) { + return -1; + } + + uint8_t cpuid[] =3D { 0x0f, 0xa2 }; + if (!memcmp(cpuid, insn->data, insn->len)) { + GByteArray *reg =3D g_byte_array_new(); + qemu_plugin_read_register(get_register("rax"), reg); + uint64_t value =3D byte_array_to_uint64(reg); + g_byte_array_free(reg, true); + + if (!(value & X86_HYPERCALL_VALUE_BASE)) { + return -1; + } + + value =3D (value >> 16) & 0xffff; + + if (value >=3D X86_HYPERCALL_MAX) { + return -1; + } + + return (int32_t)value; + } + + return -1; +} + +static bool x86_64_is_hypercall_cb(GByteArray *insn) +{ + if (insn->len !=3D X86_HYPERCALL_INSN_LEN) { + return false; + } + + uint8_t cpuid[] =3D { 0x0f, 0xa2 }; + if (!memcmp(cpuid, insn->data, insn->len)) { + return true; + } + + return false; +} + +static int32_t i386_hypercall_nr_cb(GByteArray *insn) +{ + if (insn->len !=3D X86_HYPERCALL_INSN_LEN) { + return -1; + } + + uint8_t cpuid[] =3D { 0x0f, 0xa2 }; + if (!memcmp(cpuid, insn->data, insn->len)) { + GByteArray *reg =3D g_byte_array_new(); + qemu_plugin_read_register(get_register("eax"), reg); + uint64_t value =3D byte_array_to_uint64(reg); + g_byte_array_free(reg, true); + + if (!(value & X86_HYPERCALL_VALUE_BASE)) { + return -1; + } + + value =3D (value >> 16) & 0xffff; + + if (value >=3D X86_HYPERCALL_MAX) { + return -1; + } + return (int32_t)value; + } + + return -1; + +} + +static bool i386_is_hypercall_cb(GByteArray *insn) +{ + if (insn->len !=3D X86_HYPERCALL_INSN_LEN) { + return false; + } + + uint8_t cpuid[] =3D { 0x0f, 0xa2 }; + if (!memcmp(cpuid, insn->data, insn->len)) { + return true; + } + + return false; + +} + +static const struct HypercallSpec *hypercall_spec; + +static const struct HypercallSpec hypercall_specs[] =3D { + { true, "aarch64", true, { + "x0", "x1", "x2", "x3", + }, aarch64_hypercall_nr_cb, aarch64_is_hypercall_cb + }, + { true, "aarch64_be", false, { + "x0", "x1", "x2", "x3", + }, aarch64_be_hypercall_nr_cb, aarch64_be_is_hypercall_cb + }, + { true, "arm", true, { + "r0", "r1", "r2", "r3", + }, arm_hypercall_nr_cb, arm_is_hypercall_cb + }, + { true, "armeb", false, { + "r0", "r1", "r2", "r3" + }, arm_be_hypercall_nr_cb, arm_be_is_hypercall_cb + }, + { true, "i386", true, { + "edi", "esi", "edx", "ecx" + }, i386_hypercall_nr_cb, i386_is_hypercall_cb + }, + { true, "x86_64", true, { + "rdi", "rsi", "rdx", "rcx" + + }, x86_64_hypercall_nr_cb, x86_64_is_hypercall_cb + }, + { false, NULL, .le =3D false, {NULL, NULL, NULL, NULL}, NULL}, +}; + +static GArray *hypercall_insns; + +/* + * Returns a handle to a register with a given name, or NULL if there is no + * such register. + */ +static struct qemu_plugin_register *get_register(const char *name) +{ + GArray *registers =3D qemu_plugin_get_registers(); + + struct qemu_plugin_register *handle =3D NULL; + + qemu_plugin_reg_descriptor *reg_descriptors =3D + (qemu_plugin_reg_descriptor *)registers->data; + + for (size_t i =3D 0; i < registers->len; i++) { + if (!strcmp(reg_descriptors[i].name, name)) { + handle =3D reg_descriptors[i].handle; + } + } + + g_array_free(registers, true); + + return handle; +} + +/* + * Transforms a byte array with at most 8 entries into a uint64_t + * depending on the target machine's endianness. + */ +static uint64_t byte_array_to_uint64(GByteArray *buf) +{ + uint64_t value =3D 0; + if (hypercall_spec->le) { + for (int i =3D 0; i < buf->len && i < sizeof(uint64_t); i++) { + value |=3D ((uint64_t)buf->data[i]) << (i * 8); + } + } else { + for (int i =3D 0; i < buf->len && i < sizeof(uint64_t); i++) { + value |=3D ((uint64_t)buf->data[i]) << ((buf->len - 1 - i) * 8= ); + } + } + return value; +} + +/* + * Handle a "hypercall" instruction, which has some special meaning for th= is + * plugin. + */ +static void hypercall(unsigned int vcpu_index, void *userdata) +{ + GByteArray *insn_data =3D (GByteArray *)userdata; + int32_t hypercall_nr =3D hypercall_spec->hypercall_nr_cb(insn_data); + + if (hypercall_nr < 0) { + return; + } + + uint64_t args[N_HYPERCALL_ARGS] =3D {0}; + GByteArray *buf =3D g_byte_array_new(); + for (size_t i =3D 0; i < N_HYPERCALL_ARGS; i++) { + g_byte_array_set_size(buf, 0); + struct qemu_plugin_register *reg =3D + get_register(hypercall_spec->args[i]); + qemu_plugin_read_register(reg, buf); + args[i] =3D byte_array_to_uint64(buf); + } + g_byte_array_free(buf, true); + + switch (hypercall_nr) { + /* + * The write hypercall (#0x01) tells the plugin to write random bytes + * of a given size into the memory of the emulated system at a particu= lar + * vaddr + */ + case 1: { + GByteArray *data =3D g_byte_array_new(); + g_byte_array_set_size(data, args[1]); + for (uint64_t i =3D 0; i < args[1]; i++) { + data->data[i] =3D (uint8_t)g_random_int(); + } + qemu_plugin_write_memory_vaddr(args[0], data); + break; + } + default: + break; + } +} + +/* + * Callback on translation of a translation block. + */ +static void vcpu_tb_trans_cb(qemu_plugin_id_t id, struct qemu_plugin_tb *t= b) +{ + for (size_t i =3D 0; i < qemu_plugin_tb_n_insns(tb); i++) { + struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(tb, i); + GByteArray *insn_data =3D g_byte_array_new(); + size_t insn_len =3D qemu_plugin_insn_size(insn); + g_byte_array_set_size(insn_data, insn_len); + qemu_plugin_insn_data(insn, insn_data->data, insn_data->len); + + if (hypercall_spec->is_hypercall_cb(insn_data)) { + g_array_append_val(hypercall_insns, insn_data); + qemu_plugin_register_vcpu_insn_exec_cb(insn, hypercall, + QEMU_PLUGIN_CB_R_REGS, + (void *)insn_data); + } else { + g_byte_array_free(insn_data, true); + } + + } +} + +static void atexit_cb(qemu_plugin_id_t id, void *userdata) +{ + for (size_t i =3D 0; i < hypercall_insns->len; i++) { + g_byte_array_free(g_array_index(hypercall_insns, GByteArray *, i), + true); + } + + g_array_free(hypercall_insns, true); +} + +static void usage(void) +{ + fprintf(stderr, + "Usage: ,[ignore_unsupported=3D]"); +} + +/* + * Called when the plugin is installed + */ +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, + const qemu_info_t *info, int ar= gc, + char **argv) +{ + if (argc > 1) { + usage(); + return -1; + } + + for (size_t i =3D 0; i < argc; i++) { + char *opt =3D argv[i]; + g_auto(GStrv) tokens =3D g_strsplit(opt, "=3D", 2); + if (g_strcmp0(tokens[0], "ignore_unsupported") =3D=3D 0) { + if (!qemu_plugin_bool_parse(tokens[0], + tokens[1], &ignore_unsupported)) { + fprintf(stderr, + "Failed to parse argument ignore_unsupported\n"); + return -1; + } + } else { + fprintf(stderr, "Unknown argument: %s\n", tokens[0]); + usage(); + return -1; + } + } + + + hypercall_spec =3D &hypercall_specs[0]; + + while (hypercall_spec->name !=3D NULL) { + if (!strcmp(hypercall_spec->name, info->target_name)) { + break; + } + hypercall_spec++; + } + + if (hypercall_spec->name =3D=3D NULL || !hypercall_spec->enabled) { + qemu_plugin_outs("Error: no hypercall spec."); + if (ignore_unsupported) { + return 0; + } + return -1; + } + + hypercall_insns =3D g_array_new(true, true, sizeof(GByteArray *)); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans_cb); + qemu_plugin_register_atexit_cb(id, atexit_cb, NULL); + + return 0; +} diff --git a/tests/tcg/plugins/meson.build b/tests/tcg/plugins/meson.build index 163042e601..909bf3005a 100644 --- a/tests/tcg/plugins/meson.build +++ b/tests/tcg/plugins/meson.build @@ -1,6 +1,6 @@ t =3D [] if get_option('plugins') - foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall',= 'patch'] + foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall',= 'hypercalls', 'patch'] if host_os =3D=3D 'windows' t +=3D shared_module(i, files(i + '.c') + '../../../contrib/plugins/= win32_linker.c', include_directories: '../../../include/qemu', diff --git a/tests/tcg/x86_64/Makefile.softmmu-target b/tests/tcg/x86_64/Ma= kefile.softmmu-target index 8d3a067c33..8cb2a19461 100644 --- a/tests/tcg/x86_64/Makefile.softmmu-target +++ b/tests/tcg/x86_64/Makefile.softmmu-target @@ -46,14 +46,18 @@ EXTRA_RUNS+=3D$(MULTIARCH_RUNS) =20 memory: CFLAGS+=3D-DCHECK_UNALIGNED=3D1 patch-target: CFLAGS+=3D-O0 +hypercalls-target: CFLAGS+=3D-O0 =20 # Running QEMU_OPTS+=3D-device isa-debugcon,chardev=3Doutput -device isa-debug-exit,= iobase=3D0xf4,iosize=3D0x4 -kernel =20 # Add patch-target to ADDITIONAL_PLUGINS_TESTS ADDITIONAL_PLUGINS_TESTS +=3D patch-target +ADDITIONAL_PLUGINS_TESTS +=3D hypercalls-target =20 run-plugin-patch-target-with-libpatch.so: \ PLUGIN_ARGS=3D$(COMMA)target=3Dffc0$(COMMA)patch=3D9090$(COMMA)use_hwaddr= =3Dtrue$(COMMA)debug_insns=3Dfalse run-plugin-patch-target-with-libpatch.so: \ - CHECK_PLUGIN_OUTPUT_COMMAND=3D$(X86_64_SYSTEM_SRC)/validate-patch.py $@.o= ut \ No newline at end of file + CHECK_PLUGIN_OUTPUT_COMMAND=3D$(X86_64_SYSTEM_SRC)/validate-patch.py $@.o= ut +run-plugin-hypercalls-target-with-libhypercalls.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=3D$(X86_64_SYSTEM_SRC)/validate-hypercalls.py= $@.out diff --git a/tests/tcg/x86_64/system/hypercalls-target.c b/tests/tcg/x86_64= /system/hypercalls-target.c new file mode 100644 index 0000000000..acb6d695f2 --- /dev/null +++ b/tests/tcg/x86_64/system/hypercalls-target.c @@ -0,0 +1,40 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This test target invokes a hypercall to write the value 0x1337 to a + * variable. + * + */ +#include +#include +#include + +#define _hypercall(num, arg0, arg1, arg2, arg3) \ + unsigned int a __attribute__((unused)) =3D 0; \ + unsigned int b __attribute__((unused)) =3D 0; \ + unsigned int c __attribute__((unused)) =3D 0; \ + unsigned int d __attribute__((unused)) =3D 0; \ + __asm__ __volatile__("cpuid\n\t" \ + : "=3Da"(a), "=3Db"(b), "=3Dc"(c), "=3Dd"(d) = \ + : "a"(num), "D"(arg0), "S"(arg1), \ + "d"(arg2), "c"(arg3)); + +#define hypercall(num, arg0, arg1, arg2, arg3) \ + { \ + unsigned int __num =3D 0x4711 | (num << 16); \ + _hypercall(__num, arg0, arg1, arg2, arg3); \ + } + +int main(void) +{ + uint16_t value =3D 0; + + for (size_t i =3D 0; i < 1000000; i++) { + hypercall(1, &value, sizeof(value), 0, 0); + if (value =3D=3D 0x1337) { + ml_printf("Victory!\n"); + return 0; + } + } + return 0; +} diff --git a/tests/tcg/x86_64/system/validate-hypercalls.py b/tests/tcg/x86= _64/system/validate-hypercalls.py new file mode 100755 index 0000000000..6e7c980706 --- /dev/null +++ b/tests/tcg/x86_64/system/validate-hypercalls.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# +# validate-patch.py: check the patch applies +# +# This program takes two inputs: +# - the plugin output +# - the binary output +# +# Copyright (C) 2024 +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import sys +from argparse import ArgumentParser + +def main() -> None: + """ + Process the arguments, injest the program and plugin out and + verify they match up and report if they do not. + """ + parser =3D ArgumentParser(description=3D"Validate patch") + parser.add_argument('test_output', + help=3D"The output from the test itself") + parser.add_argument('plugin_output', + help=3D"The output from plugin") + args =3D parser.parse_args() + + with open(args.test_output, 'r') as f: + test_data =3D f.read() + with open(args.plugin_output, 'r') as f: + plugin_data =3D f.read() + + if "Victory" in test_data: + sys.exit(0) + else: + sys.exit(1) + +if __name__ =3D=3D "__main__": + main() + --=20 2.49.0 From nobody Mon Feb 9 02:55:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1749497170; cv=none; d=zohomail.com; s=zohoarc; b=Nza+fbv+s0rkdL361xz35rmAqOA48VkE6Kk0hH7wODPzqy8dWKEaL+onPgh3B3XGVUgwZzVW7ISDp/KkCPo65lF27EUbn+Cl9F0E+au1MAYdgLw1Y1YU0mTlxzqtZIGh3bk0hlye5jg0HKUquybGXadC0aes9G4Uv+ktUof98cw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749497170; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QaQPhhHbCamu4uXY+C/FHHXcY6DZYaSMK3JVJyC1xcc=; b=nnoKXLJwaz0U14hlHPE5KimUi1D7Z1zEMmrmcksHuF9RXUXl9+woxuRFEyiWooo6dTPmZjI/W4rykMkgnmxMmCXhfs0SXRDCpgSERwwD1Ec2Ijpk57zCpM7QkQnTstyXICj2IRDe4h1BnEcJrIIjK7NO4OjrNHq844DkTI/2duQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1749497170744106.74867310846685; Mon, 9 Jun 2025 12:26:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOi6I-0007xW-AR; Mon, 09 Jun 2025 15:24:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uOi6B-0007vv-EB for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:59 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uOi64-0002Ri-KJ for qemu-devel@nongnu.org; Mon, 09 Jun 2025 15:23:59 -0400 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-312e747d2d8so4072981a91.0 for ; Mon, 09 Jun 2025 12:23:51 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3134b044ef2sm6057658a91.3.2025.06.09.12.23.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jun 2025 12:23:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749497029; x=1750101829; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QaQPhhHbCamu4uXY+C/FHHXcY6DZYaSMK3JVJyC1xcc=; b=Mch1PvDMr3pVTeYfIYtiMd01slxUsZK2rrY/ePvHjzs+X2NJoHmrU6AlLoMCUQC9HT TVNlJgBD93XMHk7B9smEHhNKJ+tJ1zkbh3aefOoi5lk0XysN6gb6u0afu6MNT2o82Vex UNGe9n8uLXsnawoN83zFpeXSy26B5a7Oc5OZP3k6QTxpgIkn2CQW695Qj+9jDMteifuC DQ7bci6TSs+TGAb8JCPgjSQNGCMKnI8Ks9XWIVzGIx8Oqn6oEdqCdvU+KQKxGgHji//q KcdscBDyf2MkWztctjfsdFuMYvUaqRyR1Fp/Ytvz70HizJW18K7ppnv+TTMnW7depN4y MSMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749497029; x=1750101829; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QaQPhhHbCamu4uXY+C/FHHXcY6DZYaSMK3JVJyC1xcc=; b=wfoPEk7Uei2Ao/hrvJooR0BMbC1Jk1S1ylJqnj8cp2EICK7eQYI2aFjBKD9347S0oA h7bLozZXWiHYaD0UjoyeH49HrMoRvNy7wc+ZJi6LhKiYttxCVu0W14ZOHb6JpPB7awBi zUZXiDwXUaLo6TK9tK155Xm/vt/68PUFlwRDW6/hA4JQCYiQjOBwAn31bYhb64Yton6i pH/dbEaQje5Pk0OIMFpvO3QUdPbBevrhg2zCfPpCGOXiT7KPReruzHKB0Du3hEHoAsub vgGSQ1IRC8phcp0W7zQrvDsEQGU03yjBGpJFMeqa+ivE92E1lZ/bRQQcTMtQ4dX4nVfR PT/g== X-Gm-Message-State: AOJu0YxZf57RNmbHWua8HjBEJG5o+iQIhj8pVGbmQ/sxHcZ5SoFsvZl7 1T4nBbD5OqmY+9QH8ve5j5urItcxgwYHcxOE9NzxFT5lh672wTiAuBDdjcLL8+c1 X-Gm-Gg: ASbGncsTKlSAYnDCGA0ahfk56ru4561nEzgoVfurls9gAJCVl7i1vKXKjflgSx5a2su Pknv4H9Ea8MuAxGEpd8qA9fT4vKJDf8WUk03asCK74zRk1Xd1WF45Tgh6yNLCXntGZJvHS2uQI7 Pk0w7gNPXjoMIycafGbMwNT0gLxGFGTafX7CoOmSSZtDHxMFFRrzZI+1pIWPYqP2uvM3LLgt5ON seeAfPMjyjxnVJrgLYFKyGGcCKEv2zPIhidVVM5MaUfsYv4wQAEfaVWPO/aBgWt9c9HMX57x7cj TIxFXiy1IjE/3QYn/u9KHQ7fB8gazXveV+YJ2Yzu+zUc9mlYKV9bSfv+hMIKj1eudKbkHl2+ X-Google-Smtp-Source: AGHT+IFvNTGo/vEhg/QZV8d6HPL7CP2k5Zz64RO5dAZ9c0LYUXK7bwk95posZjnYSbMCFRTDf7w8cw== X-Received: by 2002:a17:90b:5291:b0:2fa:42f3:e3e4 with SMTP id 98e67ed59e1d1-3139dfaee85mr859239a91.3.1749497029281; Mon, 09 Jun 2025 12:23:49 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Zhao Liu , Mahmoud Mandour , Marcel Apfelbaum , Alexandre Iooss , Richard Henderson , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eduardo Habkost , Yanan Wang , novafacing Subject: [PATCH v10 8/8] plugins: Update plugin version and add notes Date: Mon, 9 Jun 2025 12:23:42 -0700 Message-ID: <20250609192342.316156-9-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250609192342.316156-1-rowanbhart@gmail.com> References: <20250609192342.316156-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=rowanbhart@gmail.com; helo=mail-pj1-x102f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1749497171601116600 Content-Type: text/plain; charset="utf-8" From: novafacing This patch updates the plugin version to gate new APIs and adds notes describing what has been added. Signed-off-by: Rowan Hart --- include/qemu/qemu-plugin.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h index 5eecdccc67..c450106af1 100644 --- a/include/qemu/qemu-plugin.h +++ b/include/qemu/qemu-plugin.h @@ -65,11 +65,18 @@ typedef uint64_t qemu_plugin_id_t; * * version 4: * - added qemu_plugin_read_memory_vaddr + * + * version 5: + * - added qemu_plugin_write_memory_vaddr + * - added qemu_plugin_read_memory_hwaddr + * - added qemu_plugin_write_memory_hwaddr + * - added qemu_plugin_write_register + * - added qemu_plugin_translate_vaddr */ =20 extern QEMU_PLUGIN_EXPORT int qemu_plugin_version; =20 -#define QEMU_PLUGIN_VERSION 4 +#define QEMU_PLUGIN_VERSION 5 =20 /** * struct qemu_info_t - system information for plugins --=20 2.49.0