From nobody Tue Sep 23 21:43:55 2025 Delivered-To: importer@patchew.org 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; 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=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1584042836; cv=none; d=zohomail.com; s=zohoarc; b=g1/kYpErlJUc6s5by+iZIjDvpXjqud2fgS3vepf3eEa5mNfhOeDNlRblpD/9RdIhyrpcFNOYYXwRb+V4/Aexc32KmPToNzNp9tds/Na2bRpQzDFczLfRzWlUuyIh2qnqf660a9/P0L7SBEEFMvvDfqfzWqGfQSqiSKeKzcZ+AFM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584042836; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mxH1z7hu6JWyA2yDv2VBwZVGJWKtIh1G7GDAiImS5lg=; b=ExPFy27k5EcY1gwwEsYEmRNVjzhOHd0GGFOcCKkF3jxM1j6TtdU0ic9uci3U06lYpiqBs4NPd/aA7/qohI7LLYi/FAuz61r2QP0wWiwZdvXUp/VlzR9Lu7GJ4vAPz4Ktx9lgXfGxDcjRt1I+GuYK3IGyVi8/PcmoAJxVdOk3nT4= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1584042836900575.3035502459237; Thu, 12 Mar 2020 12:53:56 -0700 (PDT) Received: from localhost ([::1]:49532 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCTty-0006AB-Pg for importer@patchew.org; Thu, 12 Mar 2020 15:53:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45699) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCTjO-000604-U1 for qemu-devel@nongnu.org; Thu, 12 Mar 2020 15:43:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCTjN-0002FF-FZ for qemu-devel@nongnu.org; Thu, 12 Mar 2020 15:42:58 -0400 Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]:37868) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jCTjN-0002El-9i for qemu-devel@nongnu.org; Thu, 12 Mar 2020 15:42:57 -0400 Received: by mail-pj1-x1044.google.com with SMTP id ca13so3080435pjb.2 for ; Thu, 12 Mar 2020 12:42:57 -0700 (PDT) Received: from localhost.localdomain (97-126-123-70.tukw.qwest.net. [97.126.123.70]) by smtp.gmail.com with ESMTPSA id g69sm6824842pje.34.2020.03.12.12.42.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Mar 2020 12:42:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mxH1z7hu6JWyA2yDv2VBwZVGJWKtIh1G7GDAiImS5lg=; b=Zvsz+a5+BrOwCde1qmvRbrjxWo/P4LV4e8xWh5Y68M7xSG4QN0XSKO5FxPj/dhIzr2 poXLWwfehj6gJl8TuTfIAt4oZSbHDf4cROm2X8Pfk1iq9s6AER771ob2R4wsWb4lfPCk 6Ym7xRbAGyxRro0qZEEQdOsZDoT1nHDjVgm/OngpzE+/TCnFbt4yAYCXPgeI2EWHoddp gtBOPs6kiFmcBvY/RRdA4FMvklE7Ie/VFNZxWWs0zeynNbFAs25JwvUaswV8K6p9XS1c YJ2ES/fyoC3KZfVbnynHmXxmhT8y1AFQQM/9GMKZ84WVgbYHeFgAVqsPT7zgjYekOaUl apnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mxH1z7hu6JWyA2yDv2VBwZVGJWKtIh1G7GDAiImS5lg=; b=k5qy0DcaSVg6wL3CUGXwIqbPH9OQkkSZ57UN1w2ohTWPES7IZvaT489OT3cHzm+feh QyX6MwOwxsfVO7cVyTAmrypOgz3GFkiHL9C1DIGQDWfY8ybUR6jqFNVHXK/qXDcxvt5q 2SW6UIg/irYTmBsrL7WTEVpd+sfCsnF6akmfdEuAbUMux6NS/QNeO+8gq1BsIez66d1V yv2zzYNHzGF9p+HBwblgrlDPrcppZPJ8qr/hGYc9LpHp1NsdJH6Qxyq59gob4Byfczwh 9LcNx1caJSWGmQfY/RO/kLmyawntTQP6RkgAt+PaWp516kjgAYaYBldmBRXvMWy04ntq VnCw== X-Gm-Message-State: ANhLgQ0Otgenv2k7HmJHKbxQx5Ur57NYYfp8BKZdUklZEoFPMIel18+W jP9jOXS31sEPbx4iAERpw4/W2RDlYzs= X-Google-Smtp-Source: ADFU+vtiuzqDLWcjp34ykO0VnMAaoR+cJwdzI5bjV6mHKDKtw8+DgXt7HPwWzobZ9oOYlScMV6TjLQ== X-Received: by 2002:a17:902:343:: with SMTP id 61mr9680731pld.332.1584042175877; Thu, 12 Mar 2020 12:42:55 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v6 26/42] target/arm: Implement helper_mte_checkN Date: Thu, 12 Mar 2020 12:42:03 -0700 Message-Id: <20200312194219.24406-27-richard.henderson@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200312194219.24406-1-richard.henderson@linaro.org> References: <20200312194219.24406-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::1044 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org, alex.bennee@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @linaro.org) Content-Type: text/plain; charset="utf-8" Fill out the stub that was added earlier. Signed-off-by: Richard Henderson --- target/arm/internals.h | 1 + target/arm/mte_helper.c | 162 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 162 insertions(+), 1 deletion(-) diff --git a/target/arm/internals.h b/target/arm/internals.h index 8bbaf9b453..04f0b619b7 100644 --- a/target/arm/internals.h +++ b/target/arm/internals.h @@ -1316,6 +1316,7 @@ FIELD(MTEDESC, TSIZE, 14, 10) /* mte_checkN only */ =20 bool mte_probe1(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t r= a); uint64_t mte_check1(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr= _t ra); +uint64_t mte_checkN(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr= _t ra); =20 static inline int allocation_tag_from_addr(uint64_t ptr) { diff --git a/target/arm/mte_helper.c b/target/arm/mte_helper.c index 7a87574b35..94f67b33d1 100644 --- a/target/arm/mte_helper.c +++ b/target/arm/mte_helper.c @@ -494,7 +494,167 @@ uint64_t HELPER(mte_check1)(CPUARMState *env, uint32_= t desc, uint64_t ptr) /* * Perform an MTE checked access for multiple logical accesses. */ + +/** + * checkN: + * @tag: tag memory to test + * @odd: true to begin testing at tags at odd nibble + * @cmp: the tag to compare against + * @count: number of tags to test + * + * Return the number of successful tests. + * Thus a return value < @count indicates a failure. + * + * A note about sizes: count is expected to be small. + * + * The most common use will be LDP/STP of two integer registers, + * which means 16 bytes of memory touching at most 2 tags, but + * often the access is aligned and thus just 1 tag. + * + * Using AdvSIMD LD/ST (multiple), one can access 64 bytes of memory, + * touching at most 5 tags. SVE LDR/STR (vector) with the default + * vector length is also 64 bytes; the maximum architectural length + * is 256 bytes touching at most 9 tags. + * + * The loop below uses 7 logical operations and 1 memory operation + * per tag pair. An implementation that loads an aligned word and + * uses masking to ignore adjacent tags requires 18 logical operations + * and thus does not begin to pay off until 6 tags. + * Which, according to the survey above, is unlikely to be common. + */ + +static int checkN(uint8_t *mem, int odd, int cmp, int count) +{ + int n =3D 0, diff; + + /* Replicate the test tag and compare. */ + cmp *=3D 0x11; + diff =3D *mem++ ^ cmp; + + if (odd) { + goto start_odd; + } + + while (1) { + /* Test even tag. */ + if (unlikely((diff) & 0x0f)) { + break; + } + if (++n =3D=3D count) { + break; + } + + start_odd: + /* Test odd tag. */ + if (unlikely((diff) & 0xf0)) { + break; + } + if (++n =3D=3D count) { + break; + } + + diff =3D *mem++ ^ cmp; + } + return n; +} + +uint64_t mte_checkN(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr= _t ra) +{ + int mmu_idx, ptr_tag, bit55; + uint64_t ptr_last, ptr_end, next_page; + uint64_t tag_first, tag_end; + uint64_t tag_byte_first, tag_byte_end; + uint32_t esize, total, tag_count, tag_size, n, c; + uint8_t *mem1, *mem2; + MMUAccessType type; + + bit55 =3D extract64(ptr, 55, 1); + + /* If TBI is disabled, the access is unchecked, and ptr is not dirty. = */ + if (unlikely(!tbi_check(desc, bit55))) { + return ptr; + } + + ptr_tag =3D allocation_tag_from_addr(ptr); + + if (tcma_check(desc, bit55, ptr_tag)) { + goto done; + } + + mmu_idx =3D FIELD_EX32(desc, MTEDESC, MIDX); + type =3D FIELD_EX32(desc, MTEDESC, WRITE) ? MMU_DATA_STORE : MMU_DATA_= LOAD; + esize =3D FIELD_EX32(desc, MTEDESC, ESIZE); + total =3D FIELD_EX32(desc, MTEDESC, TSIZE); + + /* Find the addr of the end of the access, and of the last element. */ + ptr_end =3D ptr + total; + ptr_last =3D ptr_end - esize; + + /* Round the bounds to the tag granule, and compute the number of tags= . */ + tag_first =3D QEMU_ALIGN_DOWN(ptr, TAG_GRANULE); + tag_end =3D QEMU_ALIGN_UP(ptr_last, TAG_GRANULE); + tag_count =3D (tag_end - tag_first) / TAG_GRANULE; + + /* Round the bounds to twice the tag granule, and compute the bytes. */ + tag_byte_first =3D QEMU_ALIGN_DOWN(ptr, 2 * TAG_GRANULE); + tag_byte_end =3D QEMU_ALIGN_UP(ptr_last, 2 * TAG_GRANULE); + + next_page =3D TARGET_PAGE_ALIGN(ptr); + if (likely(tag_end <=3D next_page)) { + /* Memory access stays on one page. */ + tag_size =3D (tag_byte_end - tag_byte_first) / (2 * TAG_GRANULE); + mem1 =3D allocation_tag_mem(env, mmu_idx, ptr, type, total, + MMU_DATA_LOAD, tag_size, ra); + if (!mem1) { + goto done; + } + /* Perform all of the comparisons. */ + n =3D checkN(mem1, ptr & TAG_GRANULE, ptr_tag, tag_count); + } else { + /* Memory access crosses to next page. */ + tag_size =3D (next_page - tag_byte_first) / (2 * TAG_GRANULE); + mem1 =3D allocation_tag_mem(env, mmu_idx, ptr, type, next_page - p= tr, + MMU_DATA_LOAD, tag_size, ra); + + tag_size =3D (tag_byte_end - next_page) / (2 * TAG_GRANULE); + mem2 =3D allocation_tag_mem(env, mmu_idx, next_page, type, + ptr_end - next_page, + MMU_DATA_LOAD, tag_size, ra); + + /* + * Perform all of the comparisons. + * Note the possible but unlikely case of the operation spanning + * two pages that do not both have tagging enabled. + */ + n =3D c =3D (next_page - tag_first) / TAG_GRANULE; + if (mem1) { + n =3D checkN(mem1, ptr & TAG_GRANULE, ptr_tag, c); + } + if (n =3D=3D c) { + if (!mem2) { + goto done; + } + n +=3D checkN(mem2, 0, ptr_tag, tag_count - c); + } + } + + /* + * If we failed, we know which granule. Compute the element that + * is first in that granule, and signal failure on that element. + */ + if (unlikely(n < tag_count)) { + uint64_t fail_ofs; + + fail_ofs =3D tag_first + n * TAG_GRANULE - ptr; + fail_ofs =3D ROUND_UP(fail_ofs, esize); + mte_check_fail(env, mmu_idx, ptr + fail_ofs, ra); + } + + done: + return useronly_clean_ptr(ptr); +} + uint64_t HELPER(mte_checkN)(CPUARMState *env, uint32_t desc, uint64_t ptr) { - return ptr; + return mte_checkN(env, desc, ptr, GETPC()); } --=20 2.20.1