From patchwork Mon Jan 20 04:38:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13944763 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 3C322C02185 for ; Mon, 20 Jan 2025 04:40:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tZjZD-00049W-B7; Sun, 19 Jan 2025 23:39:15 -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 1tZjZB-00049I-JZ for qemu-devel@nongnu.org; Sun, 19 Jan 2025 23:39:13 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tZjZA-0008WI-2K for qemu-devel@nongnu.org; Sun, 19 Jan 2025 23:39:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737347950; 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=+2dgrQDHc5fzCfLKC4fH5d2T13feJHoGpN7u1WbNm/k=; b=SVW0ifTiI7vxsQK6S+XYWqmMI9DijO4COlHSp59H+/x/t5N1hptyBOWdpiHIn0krAHxvJr 8e4dktZM6nEVSSfnNePjXoypnqc6pVpjpH9/2jjuFtdBgZ5ltjtyv6EgenRtYaqYywZX7I PugvF1TVVf3gphki7OirpN0g8/bAsSs= Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-222-gvRfEmqxPXmKvsTj0pTB0Q-1; Sun, 19 Jan 2025 23:39:08 -0500 X-MC-Unique: gvRfEmqxPXmKvsTj0pTB0Q-1 X-Mimecast-MFC-AGG-ID: gvRfEmqxPXmKvsTj0pTB0Q Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2164fad3792so69720745ad.0 for ; Sun, 19 Jan 2025 20:39:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737347946; x=1737952746; 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=+2dgrQDHc5fzCfLKC4fH5d2T13feJHoGpN7u1WbNm/k=; b=ebxC4IICtX5W4JafQW7uer9TAdqxatU3qSF+iyB5CSCf1enMbmDyZ7jQFCgKytrqD6 yBZ7PmWx7UH3m0GF3mUTV8N0o9iuYE35wl40i58mC88g1nFX+jKZoH5gvgNOI8OOqRQw Geiz53c/6Ow4eq/bXKAx25Bb+DpqCUJllyt73r5G2XSxBiBiAXZw2ZBv/SzBVxe6m3+2 dHyYDLexSEPs0maClASL84DJ25Zlvj0VpTxVvbkhXcte7sQcalT64HkCQzOJZNKlRUc4 ucDHn8LlGR8yGokDu4lKkodVz37UcItJ4vpUZ6isT15SwIufnjU6z6Bd5Rqlru5YxAp5 lVeQ== X-Forwarded-Encrypted: i=1; AJvYcCWh+mw6V/UfD6N43qC4iMNchn9bGvjmhDQyf/rpU1/PPYyMjdNgOy+cNJEvMTU3ScqHX5jFgBPWrIMY@nongnu.org X-Gm-Message-State: AOJu0Yyy0xlWESh6o5Wewg9wonLe6lRJ5ADDpkdE0hiC+brKHFNTzSsE xrwbSgbaHJhfnyVPGpFnQ4V5mKfx7bMv2u3mwvCRsC7QW2UsoHvSvopBmrW8FK2YTGo+i4hXjVO jAz+41dH63Aay497rDrO799IyRx5RUVKBBzYS7KlC2n03oZOUYI20YFtXGQS2 X-Gm-Gg: ASbGnctCQWFSRx/aCvlnAr0HuHNaiERNjBCG1XXJnSurEa+rAMUALomyLdEYegvgzUH r+oh4Liz8YOKOjeDKovv1MCosA6JfA5TUZRAsKCjso5/D7Q4syZuVWYp3JWMYLCtfJoQ5zYSpee JVZUaI5p6o7n87KX4u6nC7Y+bqCUMgyV/BZKCr3dECjbSRlB96+d+tq3RSXJ1ZNF3X5VziWP7kC b90cMaJn+JqJKmdq/Ww5F56LnEwAF9p3mTX1jS0g8GO64wkIcjICqJUWhlzeoU0uKZFl72cL3VD eRtMqbuPXgP76uPE X-Received: by 2002:a17:902:d48a:b0:212:67a5:ab2d with SMTP id d9443c01a7336-21c35604a08mr216829865ad.44.1737347946403; Sun, 19 Jan 2025 20:39:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IFF8hKqkaK4fzLdyAqIIhK/M0nMI8lZRN0nyjDSxsa3utTy2xxSwtYL2OfVj3do9u6fPivrtg== X-Received: by 2002:a17:902:d48a:b0:212:67a5:ab2d with SMTP id d9443c01a7336-21c35604a08mr216829575ad.44.1737347946071; Sun, 19 Jan 2025 20:39:06 -0800 (PST) Received: from localhost.localdomain ([115.96.115.34]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-a9bcc3232a4sm5889604a12.30.2025.01.19.20.39.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jan 2025 20:39:05 -0800 (PST) From: Ani Sinha To: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Gerd Hoffmann , Fabiano Rosas , Laurent Vivier , Paolo Bonzini Cc: Ani Sinha , qemu-devel@nongnu.org Subject: [PATCH v6 1/3] libqos/fw_cfg: refactor file directory iteraton to make it more reusable Date: Mon, 20 Jan 2025 10:08:32 +0530 Message-ID: <20250120043847.954881-2-anisinha@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250120043847.954881-1-anisinha@redhat.com> References: <20250120043847.954881-1-anisinha@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=anisinha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -50 X-Spam_score: -5.1 X-Spam_bar: ----- X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996, 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_MSPIKE_H2=-0.024, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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 | 63 +++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/tests/qtest/libqos/fw_cfg.c b/tests/qtest/libqos/fw_cfg.c index 89f053ccac..22435873d9 100644 --- a/tests/qtest/libqos/fw_cfg.c +++ b/tests/qtest/libqos/fw_cfg.c @@ -60,6 +60,37 @@ 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. @@ -73,34 +104,20 @@ static void mm_fw_cfg_select(QFWCFG *fw_cfg, uint16_t key) * populated, it has received only a starting slice of the fw_cfg file. */ size_t qfw_cfg_get_file(QFWCFG *fw_cfg, const char *filename, - void *data, size_t buflen) + 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 Mon Jan 20 04:38:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13944761 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 133CEC02185 for ; Mon, 20 Jan 2025 04:39:59 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tZjZG-0004AB-0H; Sun, 19 Jan 2025 23:39:18 -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 1tZjZE-00049o-DN for qemu-devel@nongnu.org; Sun, 19 Jan 2025 23:39:16 -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 1tZjZC-00004q-Ki for qemu-devel@nongnu.org; Sun, 19 Jan 2025 23:39:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737347953; 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=sSec4nsvGyIYNNvt6WzLxqj9VgTUaOKwIDlDunuoFmY=; b=LkXEMDh3IIpR2v9FyV9udCgEdzvM9/S+ej5c2h4hxbD0S/kykCd/njF7dlkj5zZ6mPrNjn 0foOc94GD6XX/R5qBt1l0WIhSBIAKeI2qVEtewHE8BvAlOTjmQgBDD5Lh9x9RZCG7emVdc WUjWS0Bq21HlZDE4fkBIPEd1M1Q0Z2g= Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-39-2zwWEMf1MpG9Vgl-RFb0aw-1; Sun, 19 Jan 2025 23:39:10 -0500 X-MC-Unique: 2zwWEMf1MpG9Vgl-RFb0aw-1 X-Mimecast-MFC-AGG-ID: 2zwWEMf1MpG9Vgl-RFb0aw Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2163c2f32fdso125233265ad.2 for ; Sun, 19 Jan 2025 20:39:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737347950; x=1737952750; 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=sSec4nsvGyIYNNvt6WzLxqj9VgTUaOKwIDlDunuoFmY=; b=Dn8sWgJeYa1ZGZOK+lhfI12Y7Q4+tHfNXwlVU6bj9UN2xuD1huOwvv2IAiWwVZqt/2 bxbIv16Wd2AzEg0bd7GnVDbwMAHuXtN8rQx2AUmNt5FUBx1/SFBdhVGrDyD83ekbjfvL YTqWcrtnE0d6zF5GvdoavXwWIxZEAYCiwLbLAKZhK9Ldhk6pKzpi9iaaEgcj2JmG9g9K zbKMnRWIOcC4FF3DxCjUzpodVsKjBSSTWcJkAQRqDRU8ZMX3GrbuS29mHsFoG1nRhH2S ngkLq27a32k7Og9Nau7A478vaGL8rlTj34neBHDPklozWF+1aJUgHdSd00Vy2bljZ9AL o94w== X-Forwarded-Encrypted: i=1; AJvYcCU81GAW+bsJ6QhMWFgo/guXYzZ1myybJoah25Cg2k2oBCFSQqvR/XbxkjiHMWNMFWZUB7oWK/+rL6Ai@nongnu.org X-Gm-Message-State: AOJu0Yz9PN4+H1BwactW2MXWylvZkM9dxiq+zWI8dAo+vudOSl7TqIu7 01xiG8/a12uXKRc/0uP7D36+i2WFN4l8v90xliE4DAm6fIsLO4p/uJ3sKTiZCDPHIDlOsC6YRZc s39gi3RugA7OjH9owfHXdwZvTzYINzwJ8ZMw5LJwWqJLkYBKvxJtD X-Gm-Gg: ASbGnctPGZQsUPx5dhitD3gTt8L87VsEt+mVxurU0v/MqiUZSx20yErrJORoBoeZ4aS Us3CW9N7p+x+3G/WsSXCXTfv9vlOMZaigFhGLgbLCCVweapHxbAsqiRxCYWVw8lW73+gFDLPUdl OERLTBC9kt4W4LLz+rRAZBj79OkpMXlWIKC0USqvFCSENQA5+FKjAj0NgsYbyqLg2GkrQtwc9r5 aTkbzh91KwkJu1eICBN1zo6j+ZxK2SXPibXqvyPGYxqjopr684xtUXTism86KB5x5b3NJx9zbNs TippjvSsnpIWyGMX X-Received: by 2002:a05:6a21:100c:b0:1e0:d575:8d4f with SMTP id adf61e73a8af0-1eb215dadc7mr16970018637.37.1737347949696; Sun, 19 Jan 2025 20:39:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFF3hR9DRVG31HrdNWEGrggpE4g11WqV4Tf1iUTnuWcyBpMok+ZLQvnhbd9GQZNlkM93dWW2Q== X-Received: by 2002:a05:6a21:100c:b0:1e0:d575:8d4f with SMTP id adf61e73a8af0-1eb215dadc7mr16969998637.37.1737347949369; Sun, 19 Jan 2025 20:39:09 -0800 (PST) Received: from localhost.localdomain ([115.96.115.34]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-a9bcc3232a4sm5889604a12.30.2025.01.19.20.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jan 2025 20:39:08 -0800 (PST) From: Ani Sinha To: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Gerd Hoffmann , Fabiano Rosas , Laurent Vivier , Paolo Bonzini Cc: Ani Sinha , qemu-devel@nongnu.org Subject: [PATCH v6 2/3] tests/qtest/libqos: add DMA support for writing and reading fw_cfg files Date: Mon, 20 Jan 2025 10:08:33 +0530 Message-ID: <20250120043847.954881-3-anisinha@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250120043847.954881-1-anisinha@redhat.com> References: <20250120043847.954881-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: -60 X-Spam_score: -6.1 X-Spam_bar: ------ X-Spam_report: (-6.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996, 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_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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 add 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. Signed-off-by: Ani Sinha --- tests/qtest/libqos/fw_cfg.c | 139 ++++++++++++++++++++++++++++++++++++ tests/qtest/libqos/fw_cfg.h | 6 +- 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/tests/qtest/libqos/fw_cfg.c b/tests/qtest/libqos/fw_cfg.c index 22435873d9..0ab3959171 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,60 @@ 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) { @@ -121,6 +177,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 Mon Jan 20 04:38:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ani Sinha X-Patchwork-Id: 13944762 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 C8BB1C0218B for ; Mon, 20 Jan 2025 04:40:01 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tZjZM-0004AV-Bk; Sun, 19 Jan 2025 23:39:24 -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 1tZjZK-0004AL-KP for qemu-devel@nongnu.org; Sun, 19 Jan 2025 23:39:22 -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 1tZjZJ-000052-37 for qemu-devel@nongnu.org; Sun, 19 Jan 2025 23:39:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737347959; 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=vwiXxTc7xFDKV1c7kXLl1EzuQbCxBUqxjJnvRGVrIss=; b=Kh3ytsIVy8GARhJSq8qWp+AKXZviwxNAU9dJWn0ZF2zQn1evJgmJHw/YoLN55J70YfPDqE 8UlpPvnuULyL0V6DkmkgvQa24jH2wMeJSMPNaeDx12s41Nf96HbtODy9ZcEyy1kScrstZT QME+nTFs9ue9TPMZ/NLoltprQcxzErs= Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-25-63Cr7D_JNpqKYnnmC2JR7g-1; Sun, 19 Jan 2025 23:39:14 -0500 X-MC-Unique: 63Cr7D_JNpqKYnnmC2JR7g-1 X-Mimecast-MFC-AGG-ID: 63Cr7D_JNpqKYnnmC2JR7g Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-2ef9dbeb848so7591733a91.0 for ; Sun, 19 Jan 2025 20:39:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737347953; x=1737952753; 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=vwiXxTc7xFDKV1c7kXLl1EzuQbCxBUqxjJnvRGVrIss=; b=KlqPdrre9ahMSCUnEcFVkG7jsuxjU7+nRBIgTVKUCr4V1yKwdakPgqLrXF3lhAyfkJ LxOGV16BzSiVZHFwA0EmgbqRuaS4TR+beLvqWjKZW3DbR8SaE8zZcMNoC1PylOpwF6fu crPkT3MaMgbozkp/D7YF+/RQXgi3NtKxEi5qvaMkHJkIXm0ZlVCGHL2e/nvIuRmDYR0B YvaEZMJlzcUuy1MJkad4SmhFAP03vQTE748+HSxF1boJcBJg6jzyhyYd2l43d/4TpCoX dsQgE78doDa+KlkVpyrQhYNDfEHXKhaK596Wq0P7uHwEc4Rk+qIY3gJS6Z+VhkqH1Fjs qyAA== X-Gm-Message-State: AOJu0YzWZ6PsB+GOXNub4m7IUehc/E1+LkvwTPXsZbJkCFUYFVd/6qL3 cLl9uJymzhbYLNxw1URe4M4mnkufpbEjaB/zPirA401asiMtI4MJ6rBI+OXC202ohcJUU5b2JKU ThXnXNl8uY3vWM+wkH8M21/1TPj0xaD3o0Ry+jHaU5xIBUGB+uqU4 X-Gm-Gg: ASbGncvBNYdJAzMPIWD2ahrrorzrVvtLHDNrS1PytRryHc8o6gnaS9vu9ds85z+FKu8 TKBz+sWZURyoXvS4owUj2bc7Kt1+Zd/e7tKhr+j5Q++4gyyPvrRzV5FuLp/p16DV7wbpqagwCcy oUusq2V0fUCkYT7raCE+IyCvWGW2MoIxIJte+t4nm77pKFy9KMyMNdLNrR+CQ488RhtjXIk+qNm VAZIt63u0JsrVjXrsk09XTVLpBpZikYZPmxyGI84AXGrlZuFZPRNzipdTdDk9lYvHRxvM37mg0I n0AqGlhWTFGxaX+u X-Received: by 2002:a17:90a:c883:b0:2ee:b2be:f390 with SMTP id 98e67ed59e1d1-2f782d37d55mr16960303a91.28.1737347952887; Sun, 19 Jan 2025 20:39:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IE/LLVNdSVYHUzksYfnrqOjOTr5MyK4oVWBqScHPNmHMLpefkCEwE5saBhiUFItI2773g4LFw== X-Received: by 2002:a17:90a:c883:b0:2ee:b2be:f390 with SMTP id 98e67ed59e1d1-2f782d37d55mr16960274a91.28.1737347952440; Sun, 19 Jan 2025 20:39:12 -0800 (PST) Received: from localhost.localdomain ([115.96.115.34]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-a9bcc3232a4sm5889604a12.30.2025.01.19.20.39.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jan 2025 20:39:11 -0800 (PST) From: Ani Sinha To: Fabiano Rosas , Laurent Vivier , Paolo Bonzini , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Ani Sinha Cc: qemu-devel@nongnu.org Subject: [PATCH v6 3/3] tests/qtest/vmcoreinfo: add a unit test to exercize basic vmcoreinfo function Date: Mon, 20 Jan 2025 10:08:34 +0530 Message-ID: <20250120043847.954881-4-anisinha@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250120043847.954881-1-anisinha@redhat.com> References: <20250120043847.954881-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: -60 X-Spam_score: -6.1 X-Spam_bar: ------ X-Spam_report: (-6.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996, 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_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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 verify 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 846b81e3ec..57167c3c73 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3016,6 +3016,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 @@ -3026,6 +3027,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 94b28e5a53..fc669336a6 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(); +}