From patchwork Wed Jul 31 21:24:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 1967321 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=rDvtgVCK; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZ4sL6g28z1yZv for ; Thu, 1 Aug 2024 07:26:14 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZGov-0007pq-Rh; Wed, 31 Jul 2024 17:25:17 -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 1sZGou-0007nN-7B for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:16 -0400 Received: from mail-lj1-x22e.google.com ([2a00:1450:4864:20::22e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZGor-0002AX-Od for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:15 -0400 Received: by mail-lj1-x22e.google.com with SMTP id 38308e7fff4ca-2f136e23229so46199901fa.1 for ; Wed, 31 Jul 2024 14:25:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722461111; x=1723065911; 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=lmDycacciVDN5eRotRRZuulYkMaRluWb6sjuNguDD+M=; b=rDvtgVCKgwsQGA478CUXIzu75jL8QSrmVMfqKRotzBbS/sQvsQdDdyE5Wt8o9voY4r Zb+yT3WKMhFhHnTxo+WJHdku3SPr/By8aArsfFR6FWwT3x5XlNkxC4s28ynW4+iLx8wC Q5BugH45hl/VCRhej8ssh3KtbPbtJGuqQyXseRjeYn+Ajsfti0R2w8Y/EHoRwj4MLSn3 qkNnS1COXWtEA8EPkoQo7UPop68Xh+IwjdtCWMcQp6Na+eMf/dnp3etSbxz/FlI7lEk9 YPXF84DxDLgNglAOaCtvTIfk0XcWxfjz+dJztMJdqArcz30LRazOwtrko9r1B4RdG6g4 x7Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722461111; x=1723065911; 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=lmDycacciVDN5eRotRRZuulYkMaRluWb6sjuNguDD+M=; b=bz6lD5TS9Q17+i14b3JhrLzIYN1t3bqC2COH4uPJGlM3kIAATHXSa6hmQENkgoMVTF GxkeCUyQp6gLDvZxFbxjBLJD7uJeQlHlm2KgoAic4NRnlYdnPi5ynd4ZRCRVD2Sok7vv ja91Hd2SAF5BgpxpfP98f3lbJlqqmooNJ6v/kGkjhQvvmcuTCuyp6leewtzM8qbOd7B6 FJpq7gb9faxzqzUhZpTLusdF2GtXfLtjRPjynV6ix+a2GS3u7xffV7lesch3tUkKDkib 470XaBHPDzKI9hRfShYvze9oJisnUhA+ir/91SXX5BO0jGlc3QxzX65LbkFMlgHQuk7J EL5w== X-Gm-Message-State: AOJu0Yy24eOwMfrbpUZV8vb1qZmluNFftW5XPQFLlyvZj94eua56jCPg rRzBohMbWl9bi4dVaCFOT77ZtLpkV+wlQnFjgL74ylGA7Q/YI/zRUJD0RbdzUGGVUguB1pgeCnx 6 X-Google-Smtp-Source: AGHT+IF21v7RXbC4zE2kKVBGAtlj6ogTZmPz2NG8LRoGfLj11Hq+90jT5dDgvIksgMVhMjCINs0qMA== X-Received: by 2002:a2e:3517:0:b0:2ef:2b44:9977 with SMTP id 38308e7fff4ca-2f1531027b6mr3324571fa.18.1722461111245; Wed, 31 Jul 2024 14:25:11 -0700 (PDT) Received: from m1x-phil.lan ([176.176.130.148]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36857d5asm17928671f8f.84.2024.07.31.14.25.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 31 Jul 2024 14:25:10 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Richard Henderson , qemu-block@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Bin Meng Subject: [PATCH-for-9.1? v2 1/4] hw/sd/sdhci: Reduce variables scope in sdhci_do_adma() Date: Wed, 31 Jul 2024 23:24:58 +0200 Message-ID: <20240731212501.44385-2-philmd@linaro.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240731212501.44385-1-philmd@linaro.org> References: <20240731212501.44385-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::22e; envelope-from=philmd@linaro.org; helo=mail-lj1-x22e.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org All variables are only used within the for loop. Declare them within it. In particular this resets 'dscr' on each iteration. Signed-off-by: Philippe Mathieu-Daudé --- hw/sd/sdhci.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/hw/sd/sdhci.c b/hw/sd/sdhci.c index 66b9364e9e..773f2b284b 100644 --- a/hw/sd/sdhci.c +++ b/hw/sd/sdhci.c @@ -751,20 +751,19 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr) static void sdhci_do_adma(SDHCIState *s) { - unsigned int begin, length; - const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK; - const MemTxAttrs attrs = { .memory = true }; - ADMADescr dscr = {}; - MemTxResult res; - int i; - if (s->trnmod & SDHC_TRNS_BLK_CNT_EN && !s->blkcnt) { /* Stop Multiple Transfer */ sdhci_end_transfer(s); return; } - for (i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) { + for (int i = 0; i < SDHC_ADMA_DESCS_PER_DELAY; ++i) { + unsigned int begin, length; + const uint16_t block_size = s->blksize & BLOCK_SIZE_MASK; + const MemTxAttrs attrs = { .memory = true }; + ADMADescr dscr = { }; + MemTxResult res; + s->admaerr &= ~SDHC_ADMAERR_LENGTH_MISMATCH; get_adma_description(s, &dscr); From patchwork Wed Jul 31 21:24:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 1967322 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=GgPNbAys; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZ4sN1G7jz1yZv for ; Thu, 1 Aug 2024 07:26:16 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZGp1-000815-Ej; Wed, 31 Jul 2024 17:25:23 -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 1sZGp0-0007z3-Ba for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:22 -0400 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZGox-0002Bf-Kh for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:22 -0400 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-4281faefea9so28942745e9.2 for ; Wed, 31 Jul 2024 14:25:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722461117; x=1723065917; 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=2dqJthe0fo5ExOTjiiBy7T5s1CTErpsVhn7olcnYzJ4=; b=GgPNbAysC4VXSAdVLlg3QIg5wtHE5SuAm0BBpMFIxFyPB8Nr+cN4/jBkvqT1NqgpGK 8EozHKhkoIRcYRm1Ghyh/5MXnXjTf5+yzdtu7QYEUgznhwi72WPYAIeZKv+ASIK2imqv kmuGPvlOsxq0442sLdDCNlSR1WcwkXMV48XBKnfk9JbNJaZaaH4ZyrAZZ+C0ARBTHoeh dIG4oMn7vYylgcz9kfDAl9E10b9hQAC0bo8YI07+2VNRFrwcgupAA/Bz3Oz3gY38ucDG dXpdmh7Z30yhzEfywRRxlA/ej38cwxroFfJTVCElvZV+v0r9zf2cKNy9vg/oLvQ9v7vO vJwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722461117; x=1723065917; 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=2dqJthe0fo5ExOTjiiBy7T5s1CTErpsVhn7olcnYzJ4=; b=rCb6HvPTeeCQMTHmCh6fc3AGoK6Pxy8INmFlbNO7QihaDI3ivN/bqupvGYA1eAAcJ/ WbG/Z1IHhBGDuO5lfKVAP48VQhexS5jXVuD001qHcvNvnaOqiUhpDInXCl4XnYzuiJIs BKM1k5h6sVB25MdoocoOIY/o7K4o7EWh+kFR1VUYTm4H8vc+seMGr2wjXR/HjOBzMdDm LBneFnydWWGsRzVPGipFxGwJyq2CQ1x7fKEe7hyejzsYrtNE8gvLNskwUpFrkV99wUtc HvzsW3l2OMux2QelLTfppz1viva+kww34ecUx8KSO/wcVNNWzsz4Vp3t23Rg9oMceN6c oGiQ== X-Gm-Message-State: AOJu0Yyl1q7P0v9qW2Qiqb3yxS/MqNpxYcnLxLSPYKpuW/p1yxIAIV8i yV3Qx2V5NcXltDe8vHq0obYMqErK1mL4y7rQkOCLHk6O37IPKvhAOjRo/yD3/jj4htTa42cAUIF g X-Google-Smtp-Source: AGHT+IGa5BxCQhH8ge/A5VN3Uj1JVF3dXZIkEC9f8w/HUC5vTYKEU9rawL4Q3K9Seim9a1oILo0PWA== X-Received: by 2002:a05:600c:4fd3:b0:426:5dd0:a1e4 with SMTP id 5b1f17b1804b1-428a99dfc67mr4484055e9.8.1722461117549; Wed, 31 Jul 2024 14:25:17 -0700 (PDT) Received: from m1x-phil.lan ([176.176.130.148]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367c0889sm17960188f8f.22.2024.07.31.14.25.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 31 Jul 2024 14:25:17 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Richard Henderson , qemu-block@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Bin Meng Subject: [PATCH-for-9.1? v2 2/4] hw/sd/sdhci: Reduce variables scope in get_adma_description() Date: Wed, 31 Jul 2024 23:24:59 +0200 Message-ID: <20240731212501.44385-3-philmd@linaro.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240731212501.44385-1-philmd@linaro.org> References: <20240731212501.44385-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32f; envelope-from=philmd@linaro.org; helo=mail-wm1-x32f.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The 'adma1' variable is only used in the SDHC_CTRL_ADMA1_32 case, and 'adma2' in SDHC_CTRL_ADMA2_32. Add braces in the switch case to use local declarations. Do the same in the SDHC_CTRL_ADMA2_64 case because we'll add a local variable there in the next commit. Replace 0xFFFFF000 -> ~0xfff to align with our codebase style. No functional change intended. Signed-off-by: Philippe Mathieu-Daudé --- hw/sd/sdhci.c | 87 ++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/hw/sd/sdhci.c b/hw/sd/sdhci.c index 773f2b284b..0a95f49b93 100644 --- a/hw/sd/sdhci.c +++ b/hw/sd/sdhci.c @@ -700,50 +700,59 @@ static void trace_adma_description(const char *type, const ADMADescr *dscr) static void get_adma_description(SDHCIState *s, ADMADescr *dscr) { - uint32_t adma1 = 0; - uint64_t adma2 = 0; hwaddr entry_addr = (hwaddr)s->admasysaddr; switch (SDHC_DMA_TYPE(s->hostctl1)) { case SDHC_CTRL_ADMA2_32: - dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), - MEMTXATTRS_UNSPECIFIED); - adma2 = le64_to_cpu(adma2); - /* The spec does not specify endianness of descriptor table. - * We currently assume that it is LE. - */ - dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull; - dscr->length = (uint16_t)extract64(adma2, 16, 16); - dscr->attr = (uint8_t)extract64(adma2, 0, 7); - dscr->incr = 8; - trace_adma_description("ADMA2_32", dscr); - break; - case SDHC_CTRL_ADMA1_32: - dma_memory_read(s->dma_as, entry_addr, &adma1, sizeof(adma1), - MEMTXATTRS_UNSPECIFIED); - adma1 = le32_to_cpu(adma1); - dscr->addr = (hwaddr)(adma1 & 0xFFFFF000); - dscr->attr = (uint8_t)extract32(adma1, 0, 7); - dscr->incr = 4; - if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) { - dscr->length = (uint16_t)extract32(adma1, 12, 16); - } else { - dscr->length = 4 * KiB; + { + uint64_t adma2 = 0; + + dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), + MEMTXATTRS_UNSPECIFIED); + adma2 = le64_to_cpu(adma2); + /* + * The spec does not specify endianness of descriptor table. + * We currently assume that it is LE. + */ + dscr->addr = (hwaddr)extract64(adma2, 32, 32) & ~0x3ull; + dscr->length = (uint16_t)extract64(adma2, 16, 16); + dscr->attr = (uint8_t)extract64(adma2, 0, 7); + dscr->incr = 8; + trace_adma_description("ADMA2_32", dscr); + break; + } + case SDHC_CTRL_ADMA1_32: + { + uint32_t adma1 = 0; + + dma_memory_read(s->dma_as, entry_addr, &adma1, sizeof(adma1), + MEMTXATTRS_UNSPECIFIED); + adma1 = le32_to_cpu(adma1); + dscr->addr = (hwaddr)(adma1 & ~0xfff); + dscr->attr = (uint8_t)extract32(adma1, 0, 7); + dscr->incr = 4; + if ((dscr->attr & SDHC_ADMA_ATTR_ACT_MASK) == SDHC_ADMA_ATTR_SET_LEN) { + dscr->length = (uint16_t)extract32(adma1, 12, 16); + } else { + dscr->length = 4 * KiB; + } + trace_adma_description("ADMA1_32", dscr); + break; } - trace_adma_description("ADMA1_32", dscr); - break; case SDHC_CTRL_ADMA2_64: - dma_memory_read(s->dma_as, entry_addr, &dscr->attr, 1, - MEMTXATTRS_UNSPECIFIED); - dma_memory_read(s->dma_as, entry_addr + 2, &dscr->length, 2, - MEMTXATTRS_UNSPECIFIED); - dscr->length = le16_to_cpu(dscr->length); - dma_memory_read(s->dma_as, entry_addr + 4, &dscr->addr, 8, - MEMTXATTRS_UNSPECIFIED); - dscr->addr = le64_to_cpu(dscr->addr); - dscr->attr &= (uint8_t) ~0xC0; - dscr->incr = 12; - trace_adma_description("ADMA2_64", dscr); - break; + { + dma_memory_read(s->dma_as, entry_addr, &dscr->attr, 1, + MEMTXATTRS_UNSPECIFIED); + dma_memory_read(s->dma_as, entry_addr + 2, &dscr->length, 2, + MEMTXATTRS_UNSPECIFIED); + dscr->length = le16_to_cpu(dscr->length); + dma_memory_read(s->dma_as, entry_addr + 4, &dscr->addr, 8, + MEMTXATTRS_UNSPECIFIED); + dscr->addr = le64_to_cpu(dscr->addr); + dscr->attr &= (uint8_t) ~0xC0; + dscr->incr = 12; + trace_adma_description("ADMA2_64", dscr); + break; + } } } From patchwork Wed Jul 31 21:25:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 1967323 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=Guf4mfy+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZ4sY60Lbz1yZv for ; Thu, 1 Aug 2024 07:26:25 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZGp7-0008M0-47; Wed, 31 Jul 2024 17:25:29 -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 1sZGp5-0008LI-ON for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:27 -0400 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZGp4-0002EX-8x for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:27 -0400 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-428163f7635so39240965e9.2 for ; Wed, 31 Jul 2024 14:25:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722461124; x=1723065924; 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=UExLWm9ddjnMRPI0UADCJZeULFWbdE7wtbj111e1TQ4=; b=Guf4mfy+Pa4b4IAfKEe/iv+6c3fe8KtDPXzmyIC1wLKBfri+6rfzM+wRIrUNFbnYU6 23S/wgVHBz/Pbs34WjFTTWaP6N0kAk7xk3zSEidzH+kMKlo8783MeGCq9XufBSs7E1Kc lg2qTtW9OF3ICCrACvYL09Rs3yJtRD5RXQWrPVl6LUElAyYF2qjF/fRVWkSRDvHOMJQj NljPjo7SqBLK2G6XQX7DCum6YcxXDEdyQohrIIIGuHEe2zam5pJtZXEPl8DEjZ+Y4Jh0 mVCy37VPErc5KuYLnL2OHYsugEiBqxqc9i0xIBv9d0wCuu+xLLcxD1Q/bgMxGhmM7ewH pziQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722461124; x=1723065924; 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=UExLWm9ddjnMRPI0UADCJZeULFWbdE7wtbj111e1TQ4=; b=rQ0KZii78WgQSmDU4zfVn+j+J9EgrguGYFFzMWeFGZ9DosjVKdExl2hR1tkZPRR3pk cUPYH1bylX/Vdje1Hh6/AZPPFA4DNcyYknbxVYoqWClv03A19vxLfp4fvb/6SB7Ofxb9 nr6g6HpOJQ+3cfu9coIA80/slZVjkGSMOO0AzzAS9Ub5HWViyZbUjx+AWwd5TVrWn02/ NRXLuW6GOy019dpa4CQUSV02Zlep5PGkqLjg4gSMfUnzTQBV0vCZ1QHG9Z4JwZAT/A80 3dGq2j3XFW4yiIrUMct1srwgvhdnLA+YFkfeIwvgq9ATzpFF+5ZmVAHfiSLVsbyDMivC jNtQ== X-Gm-Message-State: AOJu0YzIiRUOVeROncAcC/ITwbm+5tKkoP8uXuIZr7KjsN++b+ZHjGHp /Lvq37nhmNz80MgRlMfurXKNG8x/49sfwxVSOjfkWjN9CO+FxW/sQ3h+B0et2xYaR1FRbiwT0vd z X-Google-Smtp-Source: AGHT+IFBNMvEtEwBQ+Q5LVFvrrOzmRaVx39hGRDTtRbQh5GJXCpnQCMe5JrPciQ9Yghz9IfkI1HqAQ== X-Received: by 2002:a5d:6701:0:b0:362:c7b3:764c with SMTP id ffacd0b85a97d-36baaf762b8mr227649f8f.48.1722461124140; Wed, 31 Jul 2024 14:25:24 -0700 (PDT) Received: from m1x-phil.lan ([176.176.130.148]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4282baccae2sm34983355e9.30.2024.07.31.14.25.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 31 Jul 2024 14:25:23 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Richard Henderson , qemu-block@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Bin Meng Subject: [PATCH-for-9.1? v2 3/4] hw/sd/sdhci: Read ADMA2_64 descriptor with a single dma_memory_read() Date: Wed, 31 Jul 2024 23:25:00 +0200 Message-ID: <20240731212501.44385-4-philmd@linaro.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240731212501.44385-1-philmd@linaro.org> References: <20240731212501.44385-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::331; envelope-from=philmd@linaro.org; helo=mail-wm1-x331.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Instead of 3 consecutive dma_memory_read() calls, use a packed structure to read the descriptor in a single call. Signed-off-by: Philippe Mathieu-Daudé --- hw/sd/sdhci.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/hw/sd/sdhci.c b/hw/sd/sdhci.c index 0a95f49b93..2d8fa3151a 100644 --- a/hw/sd/sdhci.c +++ b/hw/sd/sdhci.c @@ -740,16 +740,20 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr) } case SDHC_CTRL_ADMA2_64: { - dma_memory_read(s->dma_as, entry_addr, &dscr->attr, 1, + struct { + uint8_t attr; + uint8_t pad; + uint16_t length; + uint64_t addr; + } QEMU_PACKED adma2; + QEMU_BUILD_BUG_ON(sizeof(adma2) != 12); + + dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), MEMTXATTRS_UNSPECIFIED); - dma_memory_read(s->dma_as, entry_addr + 2, &dscr->length, 2, - MEMTXATTRS_UNSPECIFIED); - dscr->length = le16_to_cpu(dscr->length); - dma_memory_read(s->dma_as, entry_addr + 4, &dscr->addr, 8, - MEMTXATTRS_UNSPECIFIED); - dscr->addr = le64_to_cpu(dscr->addr); - dscr->attr &= (uint8_t) ~0xC0; - dscr->incr = 12; + dscr->length = le16_to_cpu(adma2.length); + dscr->addr = le64_to_cpu(adma2.addr); + dscr->attr = adma2.attr & (uint8_t) ~0xc0; + dscr->incr = sizeof(adma2); trace_adma_description("ADMA2_64", dscr); break; } From patchwork Wed Jul 31 21:25:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 1967319 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=sz8jzUAL; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZ4s45GNtz1yZv for ; Thu, 1 Aug 2024 07:25:59 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZGpF-0000D8-PC; Wed, 31 Jul 2024 17:25:37 -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 1sZGpD-00008b-GP for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:35 -0400 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZGpB-0002Ha-3X for qemu-devel@nongnu.org; Wed, 31 Jul 2024 17:25:35 -0400 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3685a5e7d3cso3542580f8f.1 for ; Wed, 31 Jul 2024 14:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722461131; x=1723065931; 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=DSCKNCS385Z7tdy/qq25A1b3soTdqIksx3r0Ajnm5Pk=; b=sz8jzUALidOE1rs3PV9L2UlPNZKnPs8R3QqJU+M5g6um9MLDJUo7DYq9pbzd9ztBJY IGB1Q+neZOO6YDdDHN27cO77jnmpiB2xWlyyS9jkRD+MrPP1OuOVyk28elTkIcvqIop6 7W/VDtryzqxM3Bmber7JlqpNmaO7E+ajRKWwdT8T9B6F5uImeJN918/a3Fea/ZyOlE1M oESrz9NMgNHENh9pGh/PFx0RHCRVHGYUFtgcFYqixl339+5bmKY97J01k7iC00vHhfJW 6ETNWtym/3KHlYBxu9vXK7EBI8yVUPYubNiu+IABCQ7iKtTVnvSYxPA3retzqA6kLAkX vylA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722461131; x=1723065931; 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=DSCKNCS385Z7tdy/qq25A1b3soTdqIksx3r0Ajnm5Pk=; b=SdSYwlfVyeEh8dIOMRkYLDVApvcJqeytz58Qj5eAWKwvcY1v3A60MT0TlMqvwvtop/ E6EJ5lu1HTFwJ2d7uiOh2jgqCHJz78NNZ2KPUs4LCA+EuuM1hELm+HVYSir56tdpTXaM L6cCGaeTAbVBKbd6bWSKV+LVU+7l4c08GYNmx4F/mJbsDQ3TxXjTH4zmlHYRq0bwsw2K 6+qoFSwkRWNl2VLhb8XZ2RCIiVTxHkIwGcbAUnyk3ucSmdFZsQ0ftiOFydlzFUfFgEZ5 /bxmIq7j8lUy87hADbJdAULXCDba8OMgBil2taWd2vDy5xmeVyVraR2iP3fKTU7BDrrL iF1g== X-Gm-Message-State: AOJu0Yw4vd0Acmf0kJkwfEdD7iGun3lrQsWXla4OwfACDQfbnooMDFnM mKNbV33lx0Ps8UaXByNmpOILUnZnPb+TfsgtAdzKPVaZpN48lSPcC4KoELo49LTRE13bougT0mM d X-Google-Smtp-Source: AGHT+IEeu+k9huFO1cfrt9EXZTMiEMNZH103Pt7NmOXYT4BOnV71kbFvIg1M0+7qYRrFDhRHahCntg== X-Received: by 2002:adf:f6c4:0:b0:367:8e53:7fd7 with SMTP id ffacd0b85a97d-36baace2a4cmr402984f8f.28.1722461130582; Wed, 31 Jul 2024 14:25:30 -0700 (PDT) Received: from m1x-phil.lan ([176.176.130.148]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367d95c7sm17825028f8f.37.2024.07.31.14.25.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 31 Jul 2024 14:25:30 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Richard Henderson , qemu-block@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Bin Meng , qemu-stable@nongnu.org Subject: [PATCH-for-9.1? v2 4/4] hw/sd/sdhci: Check ADMA descriptors can be accessed Date: Wed, 31 Jul 2024 23:25:01 +0200 Message-ID: <20240731212501.44385-5-philmd@linaro.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240731212501.44385-1-philmd@linaro.org> References: <20240731212501.44385-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42d; envelope-from=philmd@linaro.org; helo=mail-wr1-x42d.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Since malicious guest can write invalid addresses to the ADMASYSADDR register, we need to check whether the descriptor could be correctly filled or not. Cc: qemu-stable@nongnu.org Fixes: d7dfca0807 ("hw/sdhci: introduce standard SD host controller") Signed-off-by: Philippe Mathieu-Daudé --- hw/sd/sdhci.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/hw/sd/sdhci.c b/hw/sd/sdhci.c index 2d8fa3151a..6794ee2267 100644 --- a/hw/sd/sdhci.c +++ b/hw/sd/sdhci.c @@ -701,13 +701,18 @@ static void trace_adma_description(const char *type, const ADMADescr *dscr) static void get_adma_description(SDHCIState *s, ADMADescr *dscr) { hwaddr entry_addr = (hwaddr)s->admasysaddr; + MemTxResult res; + switch (SDHC_DMA_TYPE(s->hostctl1)) { case SDHC_CTRL_ADMA2_32: { uint64_t adma2 = 0; - dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), - MEMTXATTRS_UNSPECIFIED); + res = dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), + MEMTXATTRS_UNSPECIFIED); + if (res != MEMTX_OK) { + break; + } adma2 = le64_to_cpu(adma2); /* * The spec does not specify endianness of descriptor table. @@ -724,8 +729,11 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr) { uint32_t adma1 = 0; - dma_memory_read(s->dma_as, entry_addr, &adma1, sizeof(adma1), - MEMTXATTRS_UNSPECIFIED); + res = dma_memory_read(s->dma_as, entry_addr, &adma1, sizeof(adma1), + MEMTXATTRS_UNSPECIFIED); + if (res != MEMTX_OK) { + break; + } adma1 = le32_to_cpu(adma1); dscr->addr = (hwaddr)(adma1 & ~0xfff); dscr->attr = (uint8_t)extract32(adma1, 0, 7); @@ -748,8 +756,11 @@ static void get_adma_description(SDHCIState *s, ADMADescr *dscr) } QEMU_PACKED adma2; QEMU_BUILD_BUG_ON(sizeof(adma2) != 12); - dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), - MEMTXATTRS_UNSPECIFIED); + res = dma_memory_read(s->dma_as, entry_addr, &adma2, sizeof(adma2), + MEMTXATTRS_UNSPECIFIED); + if (res != MEMTX_OK) { + break; + } dscr->length = le16_to_cpu(adma2.length); dscr->addr = le64_to_cpu(adma2.addr); dscr->attr = adma2.attr & (uint8_t) ~0xc0;