From patchwork Fri Jan 10 10:46:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13934231 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6BACBE77188 for ; Fri, 10 Jan 2025 10:47:23 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tWCXf-00029o-Mr; Fri, 10 Jan 2025 05:47:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWCXR-00028W-Tc for qemu-devel@nongnu.org; Fri, 10 Jan 2025 05:46:50 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWCXQ-0000wM-9T for qemu-devel@nongnu.org; Fri, 10 Jan 2025 05:46:49 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736506004; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oyJGJfV9DoJuEeQoh7mfVXEg1t8mQb3WAp/BwFtyxic=; b=QD4THjYIUyZBhKvyJqV68Q+troNQB3hv1LWITQdyEqwdw4x1cJ17CqcW0Cr7WNKkKQsAF0 wiN5mpcRaq53nuxQ5ledaEJmSLQCSBpMkG4C03HhpH6cCKRCn7baWdkyoPBqrTomZJEP6H FDPCFWm8FF/pBNU0Wbwb8DIqgVO3fSE= Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-685-5k01-Q-7PzCAGL_Zd3v4Dg-1; Fri, 10 Jan 2025 05:46:42 -0500 X-MC-Unique: 5k01-Q-7PzCAGL_Zd3v4Dg-1 X-Mimecast-MFC-AGG-ID: 5k01-Q-7PzCAGL_Zd3v4Dg Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2167141e00eso34076475ad.2 for ; Fri, 10 Jan 2025 02:46:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736506002; x=1737110802; 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=oyJGJfV9DoJuEeQoh7mfVXEg1t8mQb3WAp/BwFtyxic=; b=OC9Ux75653zO6psq68rcZeFHNERkemQjvmG8dlHbF9JAJL9RWp245avc3STr/4R7Rh GFIXWhCG/to1ok4GEuX9hPqvJwcC5EMxOOMAJIspcR06XoHQx+yqB25ZXDSm42RlmlbF jh91HtIog1nAemDIegWn1i1TgA8bm88Ag+NU0X3pwvTUtJcLLHanFTA5q/22l0dCMEAr fg993IOQjamy1bnT5JwyEbLv9afoK5eewS6vFENMrHbVZfruNrStKw4sGFrt3fglaEye g/l3VUiiqGkRC6zWyBommf+cPWnLDgCGfrgVaVR9tW/ojMQakZMj8378JSMergLXrHFk yAeA== X-Forwarded-Encrypted: i=1; AJvYcCXD+1AMts9wD7kvqqF2T9Q/qYuzugid+E+fHUo8zoBL0x9dsfof9NdZNpzATU8dwXl/E5N6lpg7FpHp@nongnu.org X-Gm-Message-State: AOJu0YyBzKM0eRtV2HBcvpUOuyZvU/xRocDf/vzVM8FMpbqNfIH9x+Dn lFPWHWf95XUFXa2c840uSd97CzOoz9NuvYhWL8/aIbM/iHZ1ASqU05HJkWMoufM/OviKKYbkndO Z0iGq0c02WJ+n0NBFdG/L7UKuSvAn5TGEDULl/Cgrx5U6uZRIKOGy X-Gm-Gg: ASbGncvQnkf1xpkVWApk4bhLySEDGm03qItztpRz4rProvqXhGqNM/tUUIDEQ5zur/d PI5fxyYbxxRYGR4LSPCiw95EiiOOZBewXWHx9xPagEZUCE+V4DGxoe2JA8bW3dMLR+MdscZTKL+ p3SalMYqpIl/CuY/ebf6v51OsgfpHoW+Qg8kPzkvkaBI9bKn+Akwx1OivdVMHEzgNYOdKzPcUXJ Vr9NlGh1YCuZ8sznw9sldzXz3acoD6AlytK8dwQx6UvaYYGs/yfJ5P7yHaF5y1OGMAjzPmQ1y4x qMk= X-Received: by 2002:a17:903:2806:b0:218:a43c:571e with SMTP id d9443c01a7336-21a83f69672mr126592675ad.28.1736506001725; Fri, 10 Jan 2025 02:46:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IFneGfhH27MvTMw6o6doQR+60+5nYtZLkyI89b9sqtyqiKbyG3P2Bg2YhbO7jjFSnNqq/hwWg== X-Received: by 2002:a17:903:2806:b0:218:a43c:571e with SMTP id d9443c01a7336-21a83f69672mr126592475ad.28.1736506001401; Fri, 10 Jan 2025 02:46:41 -0800 (PST) Received: from localhost.localdomain ([115.96.31.209]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-21a9f10ddbesm11722255ad.2.2025.01.10.02.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2025 02:46:40 -0800 (PST) From: Ani Sinha To: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Gerd Hoffmann , Fabiano Rosas , Laurent Vivier , Paolo Bonzini Cc: armbru@redhat.com, Ani Sinha , qemu-devel@nongnu.org Subject: [PATCH v5 1/3] libqos/fw_cfg: refactor file directory iteraton to make it more reusable Date: Fri, 10 Jan 2025 16:16:16 +0530 Message-ID: <20250110104619.267564-2-anisinha@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250110104619.267564-1-anisinha@redhat.com> References: <20250110104619.267564-1-anisinha@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=anisinha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.436, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org fw-cfg file directory iteration code can be used by other functions that may want to implement fw-cfg file operations. Refactor it into a smaller helper so that it can be reused. No functional change. Signed-off-by: Ani Sinha Reviewed-by: Fabiano Rosas --- tests/qtest/libqos/fw_cfg.c | 62 ++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/tests/qtest/libqos/fw_cfg.c b/tests/qtest/libqos/fw_cfg.c index 89f053ccac..b178d0b1b8 100644 --- a/tests/qtest/libqos/fw_cfg.c +++ b/tests/qtest/libqos/fw_cfg.c @@ -60,6 +60,38 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key) qtest_writew(fw_cfg->qts, fw_cfg->base, key); } +static bool +find_pdir_entry(QFWCFG *fw_cfg, const char *filename, + uint16_t *sel, uint32_t *size) +{ + g_autofree unsigned char *filesbuf = NULL; + uint32_t count; + size_t dsize; + FWCfgFile *pdir_entry; + uint32_t i; + bool found = false; + + *size = 0; + *sel = 0; + + qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, &count, sizeof(count)); + count = be32_to_cpu(count); + dsize = sizeof(uint32_t) + count * sizeof(struct fw_cfg_file); + filesbuf = g_malloc(dsize); + qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, filesbuf, dsize); + pdir_entry = (FWCfgFile *)(filesbuf + sizeof(uint32_t)); + for (i = 0; i < count; ++i, ++pdir_entry) { + if (!strcmp(pdir_entry->name, filename)) { + *size = be32_to_cpu(pdir_entry->size); + *sel = be16_to_cpu(pdir_entry->select); + found = true; + break; + } + } + + return found; +} + /* * The caller need check the return value. When the return value is * nonzero, it means that some bytes have been transferred. @@ -75,32 +107,18 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key) size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename, void *data, size_t buflen) { - uint32_t count; - uint32_t i; - unsigned char *filesbuf = NULL; - size_t dsize; - FWCfgFile *pdir_entry; size_t filesize = 0; + uint32_t len; + uint16_t sel; - qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, &count, sizeof(count)); - count = be32_to_cpu(count); - dsize = sizeof(uint32_t) + count * sizeof(struct fw_cfg_file); - filesbuf = g_malloc(dsize); - qfw_cfg_get(fw_cfg, FW_CFG_FILE_DIR, filesbuf, dsize); - pdir_entry = (FWCfgFile *)(filesbuf + sizeof(uint32_t)); - for (i = 0; i < count; ++i, ++pdir_entry) { - if (!strcmp(pdir_entry->name, filename)) { - uint32_t len = be32_to_cpu(pdir_entry->size); - uint16_t sel = be16_to_cpu(pdir_entry->select); - filesize = len; - if (len > buflen) { - len = buflen; - } - qfw_cfg_get(fw_cfg, sel, data, len); - break; + if (find_pdir_entry(fw_cfg, filename, &sel, &len)) { + filesize = len; + if (len > buflen) { + len = buflen; } + qfw_cfg_get(fw_cfg, sel, data, len); } - g_free(filesbuf); + return filesize; } From patchwork Fri Jan 10 10:46:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13934233 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A1264E7719C for ; Fri, 10 Jan 2025 10:47:42 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tWCXw-0002V2-8Q; Fri, 10 Jan 2025 05:47:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWCXV-000298-CH for qemu-devel@nongnu.org; Fri, 10 Jan 2025 05:46:55 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWCXT-0000wa-HP for qemu-devel@nongnu.org; Fri, 10 Jan 2025 05:46:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736506010; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GZMMh3Ct3D59i/vZcmkBqIRDug6lODH51ZMZv/tlh6U=; b=ZIE5k+ncpJUdHHgKcF0EQm7vPNBFtxmtTzMnRwV9Ijtmkt4CTssLPab3KiKWKtAXdyru1B +WiZ2hAwJr1ktfaWV0LoCQ68rvx9ZlD1TknRrDaqvDmskarpxwg8HdukiSnWCDUtBDHbnB /uy7i3CAh/gAlxoEbV4B8lvUjy5oQK4= Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-169-LvuEjwU1OVGwUF9HwqMqRQ-1; Fri, 10 Jan 2025 05:46:47 -0500 X-MC-Unique: LvuEjwU1OVGwUF9HwqMqRQ-1 X-Mimecast-MFC-AGG-ID: LvuEjwU1OVGwUF9HwqMqRQ Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-216750b679eso26279115ad.1 for ; Fri, 10 Jan 2025 02:46:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736506006; x=1737110806; 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=GZMMh3Ct3D59i/vZcmkBqIRDug6lODH51ZMZv/tlh6U=; b=JHUprbwTmmmxhpm1anfQWDhZKyckbqjlbHFzaAN+xthLYSn1KXU9xd86gtZ+6a79/8 j2pc4d7Ko/Vg+n54Y/yeBObJXXNZdOstY0mqhonSS6FpmGh7Dznfv6qtbaRsJh+ZQKT5 FQ/cSZZlW7fceGtKvy7evNeshb/hNpWT9Mg/JiuJNQIbBe5afyNWAlCcmkXmOBNQrndA l/Dp/XC4hJi2WE+mSwWytG/u6e2cgKIrwyF5Kvyy4Ktscav/Twk06jRdgeTdRt3PRX/V nScsDk20yMY4JM+n7k3OL3ws2Vd/nZoG12wYgFfZ054eUs6h5MnYiPJMdxAJDU/cIMnI 7LmQ== X-Forwarded-Encrypted: i=1; AJvYcCXl8iQB/ZstzAIGZQI1VT6tU2XPHqHkgDYDiH70zQ/cowuD8l5e76MpVKzspzmgIVtv1SuG8exqk+YE@nongnu.org X-Gm-Message-State: AOJu0Yw7KCJQRZL68jne0vwfujFKS4SXy9NtMFwJNTeo3Xvzm4kvGkMP xaaOVqedZRKG/rZbdhE6nv7/umYaX7T97j3Q1PQj4Z9BKX3oI5quw0i7ktuEM9RZ3QQMr/hl/wm FNAOosH55+BT3Snv/y/NL/J0RbgXdHUbvsj9NeTK4HSIJ96+7r049 X-Gm-Gg: ASbGnct2/FFCAqMMH2DssA06r+EXrxuRYWV4oxS9HJ3Zq5HobukxRL2zPUXzY/k+73Y Lxp4Fnznacj+9633ttsBrLC14h4SuYt4huwb9AiwZsTGv0cHmVigptypqj40do+w+NOEgE9/meo PJMBHdNyR+iBJRR2LY2DOZyK4JAkhUsk4/UqY2rg3K/rNLQJgFiq/WJt6/Www8FDNj5ahNUrLXx eBisQX8JeIRrcGBZNSxlCKjAsopF47uFEzxNEFDu8dy+BLRZHrgIzKgG1XGBGFZEzxrlLq2sDD9 +cA= X-Received: by 2002:a17:902:d4c4:b0:215:5ea2:6543 with SMTP id d9443c01a7336-21a83f6400cmr161720695ad.28.1736506005608; Fri, 10 Jan 2025 02:46:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlMtGtmFqVLCLlCNDtxuvluWrUbeDJHdsWl3c7mXOV2ZkVnwMt8IP7SF8F0QG5FwaGUaKRNg== X-Received: by 2002:a17:902:d4c4:b0:215:5ea2:6543 with SMTP id d9443c01a7336-21a83f6400cmr161720375ad.28.1736506005203; Fri, 10 Jan 2025 02:46:45 -0800 (PST) Received: from localhost.localdomain ([115.96.31.209]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-21a9f10ddbesm11722255ad.2.2025.01.10.02.46.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2025 02:46:44 -0800 (PST) From: Ani Sinha To: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Gerd Hoffmann , Fabiano Rosas , Laurent Vivier , Paolo Bonzini Cc: armbru@redhat.com, Ani Sinha , qemu-devel@nongnu.org Subject: [PATCH v5 2/3] tests/qtest/libqos: add DMA support for writing and reading fw_cfg files Date: Fri, 10 Jan 2025 16:16:17 +0530 Message-ID: <20250110104619.267564-3-anisinha@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250110104619.267564-1-anisinha@redhat.com> References: <20250110104619.267564-1-anisinha@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=anisinha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.436, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org At present, the libqos/fw_cfg.c library does not support the modern DMA interface which is required to write to the fw_cfg files. It only uses the IO interface. Implement read and write methods based on DMA. This will enable developers to write tests that writes to the fw_cfg file(s). The structure of the code is taken from edk2 fw_cfg implementation. It has been tested by writing a qtest that writes to a fw_cfg file. This test will be part of a future patch series. Signed-off-by: Ani Sinha --- tests/qtest/libqos/fw_cfg.c | 142 ++++++++++++++++++++++++++++++++++++ tests/qtest/libqos/fw_cfg.h | 6 +- 2 files changed, 147 insertions(+), 1 deletion(-) diff --git a/tests/qtest/libqos/fw_cfg.c b/tests/qtest/libqos/fw_cfg.c index b178d0b1b8..7c86f0fecf 100644 --- a/tests/qtest/libqos/fw_cfg.c +++ b/tests/qtest/libqos/fw_cfg.c @@ -14,6 +14,8 @@ #include "qemu/osdep.h" #include "fw_cfg.h" +#include "malloc-pc.h" +#include "libqos-malloc.h" #include "../libqtest.h" #include "qemu/bswap.h" #include "hw/nvram/fw_cfg.h" @@ -60,6 +62,63 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key) qtest_writew(fw_cfg->qts, fw_cfg->base, key); } +static void +qfw_cfg_dma_transfer(QFWCFG *fw_cfg, QOSState *qs, void *address, + uint32_t length, uint32_t control) +{ + FWCfgDmaAccess access; + uint32_t addr; + uint64_t guest_access_addr; + uint64_t gaddr; + + /* create a data buffer in guest memory */ + gaddr = guest_alloc(&qs->alloc, length); + + if (control & FW_CFG_DMA_CTL_WRITE) { + qtest_bufwrite(fw_cfg->qts, gaddr, address, length); + } + access.address = cpu_to_be64(gaddr); + access.length = cpu_to_be32(length); + access.control = cpu_to_be32(control); + + /* now create a separate buffer in guest memory for 'access' */ + guest_access_addr = guest_alloc(&qs->alloc, sizeof(access)); + qtest_bufwrite(fw_cfg->qts, guest_access_addr, &access, sizeof(access)); + + /* write lower 32 bits of address */ + addr = cpu_to_be32((uint32_t)(uintptr_t)guest_access_addr); + qtest_outl(fw_cfg->qts, fw_cfg->base + 8, addr); + + /* write upper 32 bits of address */ + addr = cpu_to_be32((uint32_t)(uintptr_t)(guest_access_addr >> 32)); + qtest_outl(fw_cfg->qts, fw_cfg->base + 4, addr); + + g_assert(!(be32_to_cpu(access.control) & FW_CFG_DMA_CTL_ERROR)); + + if (control & FW_CFG_DMA_CTL_READ) { + qtest_bufread(fw_cfg->qts, gaddr, address, length); + } + + guest_free(&qs->alloc, guest_access_addr); + guest_free(&qs->alloc, gaddr); +} + +static void +qfw_cfg_write_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key, + void *buf, uint32_t len) +{ + qfw_cfg_select(fw_cfg, key); + qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_WRITE); +} + +static void +qfw_cfg_read_entry(QFWCFG *fw_cfg, QOSState *qs, uint16_t key, + void *buf, uint32_t len) +{ + qfw_cfg_select(fw_cfg, key); + qfw_cfg_dma_transfer(fw_cfg, qs, buf, len, FW_CFG_DMA_CTL_READ); +} + static bool find_pdir_entry(QFWCFG *fw_cfg, const char *filename, uint16_t *sel, uint32_t *size) @@ -122,6 +181,89 @@ size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename, return filesize; } +/* + * The caller need check the return value. When the return value is + * nonzero, it means that some bytes have been transferred. + * + * If the fw_cfg file in question is smaller than the allocated & passed-in + * buffer, then the first len bytes were read. + * + * If the fw_cfg file in question is larger than the passed-in + * buffer, then the return value explains how much was actually read. + * + * It is illegal to call this function if fw_cfg does not support DMA + * interface. The caller should ensure that DMA is supported before + * calling this function. + * + * Passed QOSState pointer qs must be initialized. qs->alloc must also be + * properly initialized. + */ +size_t qfw_cfg_read_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename, + void *data, size_t buflen) +{ + uint32_t len = 0; + uint16_t sel; + uint32_t id; + + g_assert(qs); + g_assert(filename); + g_assert(data); + g_assert(buflen); + /* check if DMA is supported since we use DMA for read */ + id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID); + g_assert(id & FW_CFG_VERSION_DMA); + + if (find_pdir_entry(fw_cfg, filename, &sel, &len)) { + if (len > buflen) { + len = buflen; + } + qfw_cfg_read_entry(fw_cfg, qs, sel, data, len); + } + + return len; +} + +/* + * The caller need check the return value. When the return value is + * nonzero, it means that some bytes have been transferred. + * + * If the fw_cfg file in question is smaller than the allocated & passed-in + * buffer, then the buffer has been partially written. + * + * If the fw_cfg file in question is larger than the passed-in + * buffer, then the return value explains how much was actually written. + * + * It is illegal to call this function if fw_cfg does not support DMA + * interface. The caller should ensure that DMA is supported before + * calling this function. + * + * Passed QOSState pointer qs must be initialized. qs->alloc must also be + * properly initialized. + */ +size_t qfw_cfg_write_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename, + void *data, size_t buflen) +{ + uint32_t len = 0; + uint16_t sel; + uint32_t id; + + g_assert(qs); + g_assert(filename); + g_assert(data); + g_assert(buflen); + /* write operation is only valid if DMA is supported */ + id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID); + g_assert(id & FW_CFG_VERSION_DMA); + + if (find_pdir_entry(fw_cfg, filename, &sel, &len)) { + if (len > buflen) { + len = buflen; + } + qfw_cfg_write_entry(fw_cfg, qs, sel, data, len); + } + return len; +} + static void mm_fw_cfg_read(QFWCFG *fw_cfg, void *data, size_t len) { uint8_t *ptr = data; diff --git a/tests/qtest/libqos/fw_cfg.h b/tests/qtest/libqos/fw_cfg.h index b0456a15df..6d6ff09725 100644 --- a/tests/qtest/libqos/fw_cfg.h +++ b/tests/qtest/libqos/fw_cfg.h @@ -14,6 +14,7 @@ #define LIBQOS_FW_CFG_H #include "../libqtest.h" +#include "libqos.h" typedef struct QFWCFG QFWCFG; @@ -33,7 +34,10 @@ uint32_t qfw_cfg_get_u32(QFWCFG *fw_cfg, uint16_t key); uint64_t qfw_cfg_get_u64(QFWCFG *fw_cfg, uint16_t key); size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename, void *data, size_t buflen); - +size_t qfw_cfg_write_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename, + void *data, size_t buflen); +size_t qfw_cfg_read_file(QFWCFG *fw_cfg, QOSState *qs, const char *filename, + void *data, size_t buflen); QFWCFG *mm_fw_cfg_init(QTestState *qts, uint64_t base); void mm_fw_cfg_uninit(QFWCFG *fw_cfg); QFWCFG *io_fw_cfg_init(QTestState *qts, uint16_t base); From patchwork Fri Jan 10 10:46:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13934232 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CE35DE7719C for ; Fri, 10 Jan 2025 10:47:38 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tWCXh-00029z-Bh; Fri, 10 Jan 2025 05:47:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWCXW-000299-Hd for qemu-devel@nongnu.org; Fri, 10 Jan 2025 05:46:55 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tWCXU-0000wf-R0 for qemu-devel@nongnu.org; Fri, 10 Jan 2025 05:46:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736506011; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v0sEEaRcxSx+BsOOhEQYP9sA3DNrT8vp0mZwhjIi2oQ=; b=ibZGFeZB7c0rxmU/r9V+e8+PSQkJsRc3a4GS8UVz0erJCc58NeMsMfMMXD4NbfB2vEHkCK sDNUR4at3/uNNIyp5AdqdWp5BiP4bRVNCk45U9feHO+Mj+R8GQ53V2AUswvgJ42m+F02vm oeUmwjdIWezDGfQmf/copytOsbcC1xE= Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-267-0LtEmmlJO0yIdKDrDlDqFA-1; Fri, 10 Jan 2025 05:46:50 -0500 X-MC-Unique: 0LtEmmlJO0yIdKDrDlDqFA-1 X-Mimecast-MFC-AGG-ID: 0LtEmmlJO0yIdKDrDlDqFA Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2163c2f32fdso50993565ad.2 for ; Fri, 10 Jan 2025 02:46:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736506009; x=1737110809; 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=v0sEEaRcxSx+BsOOhEQYP9sA3DNrT8vp0mZwhjIi2oQ=; b=JX0jQNfeFaLhJyuHhum8rOWHY5HWIVaYNlhszQ32WVw3OGnh9p4QkbeaHSkmAQq4WS F15rBDJu4+JCrqFLz/IxbbnV5vEvIJuwQZynQ8pZYACD6MRAy5qp/9RjpSrCEKpc+L0s Jvnc8DhDLODNtF1+M7kO22R3mTyu9r91rJsM81mc72ZbPkHVbiMzC1atsyT8EE2wuFk6 0L2yg8OvB1cTpc/A5tchhYPRZ0+Bx3ZBixyJwzOpicrTa1cXEFSCjH9KDtkvElYmkifg wC3wjY5hCFPlzqXY0w6t8hYP+TF4wsjUL5QBdpTCd9kTg59K06vU1+rRqQvRnpsPYXu6 qWCg== X-Forwarded-Encrypted: i=1; AJvYcCWjOvmFS9dZqLR0Yr4j9OfhOLTgaYj+SPm71VXbq+Jk76+1F5auno//onFNdPLoPI+TL2btMBaU0q4I@nongnu.org X-Gm-Message-State: AOJu0YwLJ/jcNlr52digvQglyV+Iyzxs0GH1q5/URHBEM5nmrxM0T5EQ KMuhQmSA82FMOuFp4MjrRcgPdvlAwaoOh2u1mULulEjhe999GmHpzy0GT4e3xs7WZ4n0s4X9KZH ylSn8yso6NZZzsDp3qfx/mNr9j+/dhD+MAh9Kw5HMxympUWakZwrL X-Gm-Gg: ASbGncusbTy1c41WD4S0lilTImlv7XhsCpB910RmvjxRjSZ9u7rb2sh5DuU/TNCQX8E ofegcz7waF8pXGEAmeEWGNu2oMV/FrBm4eRR//dS0gWu/MAhQvHdpOH9GdsamJ74tUK0CnWb2TT FSTbZEPgeMv7g9TsvD/QKw4Uq5eu2SmVso0906DE4U5yvkVRjeYBK6O9279iLwddMshrgN2DT7m f4/ND+XtgVAXv29adLa5NrIuLG9wJgi96CNevTciW1oJG9E5aHZtF/Xexgfx9+F6WR+scxZ2/FH lHg= X-Received: by 2002:a17:902:e746:b0:216:3633:36e7 with SMTP id d9443c01a7336-21a83f7103amr147737065ad.26.1736506009057; Fri, 10 Jan 2025 02:46:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IHYVoncP3/flh2uNY8wX31NHpf2uzMxo7YNPROrqSgR+EfmldhyeVHSYzUZsoRbkl/XSnKo8g== X-Received: by 2002:a17:902:e746:b0:216:3633:36e7 with SMTP id d9443c01a7336-21a83f7103amr147736895ad.26.1736506008781; Fri, 10 Jan 2025 02:46:48 -0800 (PST) Received: from localhost.localdomain ([115.96.31.209]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-21a9f10ddbesm11722255ad.2.2025.01.10.02.46.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2025 02:46:48 -0800 (PST) From: Ani Sinha To: Fabiano Rosas , Laurent Vivier , Paolo Bonzini , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Ani Sinha Cc: armbru@redhat.com, qemu-devel@nongnu.org Subject: [PATCH v5 3/3] tests/qtest/vmcoreinfo: add a unit test to exercize basic vmcoreinfo function Date: Fri, 10 Jan 2025 16:16:18 +0530 Message-ID: <20250110104619.267564-4-anisinha@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250110104619.267564-1-anisinha@redhat.com> References: <20250110104619.267564-1-anisinha@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=anisinha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.436, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org A new qtest is written that exercizes the fw-cfg DMA based read and write ops to write values into vmcoreinfo fw-cfg file and read them back and varify that they are the same. Signed-off-by: Ani Sinha --- MAINTAINERS | 2 + tests/qtest/meson.build | 1 + tests/qtest/vmcoreinfo-test.c | 90 +++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 tests/qtest/vmcoreinfo-test.c diff --git a/MAINTAINERS b/MAINTAINERS index 2101b51217..4723d413ab 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3005,6 +3005,7 @@ F: include/system/device_tree.h Dump S: Supported M: Marc-André Lureau +R: Ani Sinha F: dump/ F: hw/misc/vmcoreinfo.c F: include/hw/misc/vmcoreinfo.h @@ -3015,6 +3016,7 @@ F: qapi/dump.json F: scripts/dump-guest-memory.py F: stubs/dump.c F: docs/specs/vmcoreinfo.rst +F: tests/qtest/vmcoreinfo-test.c Error reporting M: Markus Armbruster diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index c5a70021c5..9804451e25 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -57,6 +57,7 @@ qtests_i386 = \ (config_all_devices.has_key('CONFIG_AHCI_ICH9') ? ['tco-test'] : []) + \ (config_all_devices.has_key('CONFIG_FDC_ISA') ? ['fdc-test'] : []) + \ (config_all_devices.has_key('CONFIG_I440FX') ? ['fw_cfg-test'] : []) + \ + (config_all_devices.has_key('CONFIG_FW_CFG_DMA') ? ['vmcoreinfo-test'] : []) + \ (config_all_devices.has_key('CONFIG_I440FX') ? ['i440fx-test'] : []) + \ (config_all_devices.has_key('CONFIG_I440FX') ? ['ide-test'] : []) + \ (config_all_devices.has_key('CONFIG_I440FX') ? ['numa-test'] : []) + \ diff --git a/tests/qtest/vmcoreinfo-test.c b/tests/qtest/vmcoreinfo-test.c new file mode 100644 index 0000000000..dcf3b5ae05 --- /dev/null +++ b/tests/qtest/vmcoreinfo-test.c @@ -0,0 +1,90 @@ +/* + * qtest vmcoreinfo test case + * + * Copyright Red Hat. 2025. + * + * Authors: + * Ani Sinha + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qemu/units.h" +#include "libqos/libqos-pc.h" +#include "libqtest.h" +#include "standard-headers/linux/qemu_fw_cfg.h" +#include "libqos/fw_cfg.h" +#include "qemu/bswap.h" +#include "hw/misc/vmcoreinfo.h" + +static void test_vmcoreinfo_write_basic(void) +{ + QFWCFG *fw_cfg; + QOSState *qs; + FWCfgVMCoreInfo info; + size_t filesize; + uint16_t guest_format; + uint16_t host_format; + uint32_t size; + uint64_t paddr; + + qs = qtest_pc_boot("-device vmcoreinfo"); + fw_cfg = pc_fw_cfg_init(qs->qts); + + memset(&info, 0 , sizeof(info)); + /* read vmcoreinfo and read back the host format */ + filesize = qfw_cfg_read_file(fw_cfg, qs, FW_CFG_VMCOREINFO_FILENAME, + &info, sizeof(info)); + g_assert_cmpint(filesize, ==, sizeof(info)); + + host_format = le16_to_cpu(info.host_format); + g_assert_cmpint(host_format, ==, FW_CFG_VMCOREINFO_FORMAT_ELF); + + memset(&info, 0 , sizeof(info)); + info.guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF); + info.size = cpu_to_le32(1 * MiB); + info.paddr = cpu_to_le64(0xffffff00); + info.host_format = cpu_to_le16(host_format); + + /* write the values to the host */ + filesize = qfw_cfg_write_file(fw_cfg, qs, FW_CFG_VMCOREINFO_FILENAME, + &info, sizeof(info)); + g_assert_cmpint(filesize, ==, sizeof(info)); + + memset(&info, 0 , sizeof(info)); + + /* now read back the values we wrote and compare that they are the same */ + filesize = qfw_cfg_read_file(fw_cfg, qs, FW_CFG_VMCOREINFO_FILENAME, + &info, sizeof(info)); + g_assert_cmpint(filesize, ==, sizeof(info)); + + size = le32_to_cpu(info.size); + paddr = le64_to_cpu(info.paddr); + guest_format = le16_to_cpu(info.guest_format); + + g_assert_cmpint(size, ==, 1 * MiB); + g_assert_cmpint(paddr, ==, 0xffffff00); + g_assert_cmpint(guest_format, ==, FW_CFG_VMCOREINFO_FORMAT_ELF); + + pc_fw_cfg_uninit(fw_cfg); + qtest_shutdown(qs); +} + +int main(int argc, char **argv) +{ + const char *arch = qtest_get_arch(); + + g_test_init(&argc, &argv, NULL); + + if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) { + /* skip for non-x86 */ + exit(EXIT_SUCCESS); + } + + qtest_add_func("vmcoreinfo/basic-write", + test_vmcoreinfo_write_basic); + + return g_test_run(); +}