From patchwork Thu Sep 7 16:03:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 720625 Delivered-To: patch@linaro.org Received: by 2002:adf:eec5:0:b0:31d:da82:a3b4 with SMTP id a5csp317678wrp; Thu, 7 Sep 2023 09:05:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGIgfXZrD5RvT6ELmL1rTw6G14xrfX9ImUfxWINaJUqFrRc6DvyzUoc6nmWFT/Ii/Jt5MzT X-Received: by 2002:ac8:5bc5:0:b0:410:8e9f:f082 with SMTP id b5-20020ac85bc5000000b004108e9ff082mr26092191qtb.14.1694102752305; Thu, 07 Sep 2023 09:05:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694102752; cv=none; d=google.com; s=arc-20160816; b=AfZWrRsTCC1bl8NazqAlZDhKqHcoGn0Weop8Np5WZi/G/eHBQcobciaLhP9Oqvvt94 iMLWQFd7N0RkD5Mgt89SbXlOlKp4mvEcFY3EMv1ToLyQ3b+W36zDxV3kQ20oGXVwYFLM mFDhgN7mQXYrl7jY07L13LtysJItt3i2vCfknMVr19K2aN/NUy8YLrHMyBz41p1PCZah q3OnmWcqi52R2Ws49cOrmp7mzGtWbdf7v7XyrO6+EmpSKr3DUTEZnJMk/VKPEjkKB1vo 2G4oJs+l0CUdlrX1KSaIVGaY8zacxbpnwmE8l6cK6UbZHGupMUkoVLwXW0GvMF6dLeCo uijQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=MlrOF6wYS3oKU3yUenQo/IwZLeVaHMbPfXY1DgfuGLg=; fh=H2AmuqulvQE+T5zu97MCEUC3z9wF9NssS7895NhR/+c=; b=sRzaBJx7JBA1aAGVm/UgIvMMQUaG+8WYqYzUc2aWoLeol8ewJu1psK0VWnubeKXbpA 37n+EN/MIl3fPAXa4IlfLJhcJg2BjFrtmlWMrJiOcKySw3+8drU3Z4i7Pq6K3AtZSe9E NNBvAetE3zT09VtGuFPWFG87zQc3XbcAs+oc8cwGBd2AtnOBJonS8yuSRbnXxGWQnKx/ sPRCvGbAbS4txsznqhbcjHgd9UUZQ0O4Wfsk+xhU1bO+T5wNnjrvDwUhPOn+8i7IoYrJ eNOMqadKiLLg+Ki9GdgB5Y0V8Oov5qFjtdBnO/h7mHIdEggrM41Oqk5qe2dpC2Mr5Mx7 6tBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mqOEgaii; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id z3-20020a05622a028300b0040fe13367c8si11631849qtw.243.2023.09.07.09.05.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 07 Sep 2023 09:05:52 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mqOEgaii; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qeHUC-00011h-BK; Thu, 07 Sep 2023 12:04:04 -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 1qeHU6-0000wN-OY for qemu-devel@nongnu.org; Thu, 07 Sep 2023 12:03:58 -0400 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qeHTy-00021d-H8 for qemu-devel@nongnu.org; Thu, 07 Sep 2023 12:03:58 -0400 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-31c63cd4ec2so1093176f8f.0 for ; Thu, 07 Sep 2023 09:03:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694102629; x=1694707429; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=MlrOF6wYS3oKU3yUenQo/IwZLeVaHMbPfXY1DgfuGLg=; b=mqOEgaiiwDWLoBVs1a9/CwngR6XTPzcOzsplLhE0SjNJCXyIjkdNB3TlKu7hEU91bF 2OfWIVo+bpX4cHsMhuZ48B04JAYawLCdGpub5GqAInXy9UpQJ553PaMu9GyQoZnb8UwZ fCVLsuA4wTxLWgj6+GZo++qTd4NvFgKXBkcKko5TG/tLuRmy+eSvi3WSa8e5pO9FIxdG BbeN0y/na0UQ1Gq9tZP2ThrLo160mzq6l3Lnqu6Z9up8WF+LsMeUwzenlslJ7D19KRms s/dc3qELGNRch9WrzsxHkJx3oWc/0SqSYIR/+N5z4YChNCuRRc1oVZt9pcyorTEir84k ws4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1694102629; x=1694707429; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MlrOF6wYS3oKU3yUenQo/IwZLeVaHMbPfXY1DgfuGLg=; b=agwVLB9NPn5MoSd1CwiSNSDCpNOfQkDi5TXv58pjIg8FIsEpMdFz29oGGDgvuaoLUZ l0yHDyuEEDcAmia7LFDVZYpGe/nZzR8m6FGEJVjYWqNwOXBEUCHEmEY2pKuCMq7ZhnzY DgHsp60OHfboHUvHBUfJ2S8euGS+vZ1sH3Wy1pQVLRZE95+KLJep5AT7b+PnYe8nD39A chQTA2cRfA4oyB+t1doPwM8OF28yjobiG3QrxAqZaWd0awhrAtM+3VKq2lVZLcj1cubP HGapyoaOn974DtFDgZoqJC3ceNg0mMSVyxmIJJKITou3MVxCIM5NlSTrw+W3mqEfVYyX /6BA== X-Gm-Message-State: AOJu0YxhIOrCGg16dBg2f/YQyTgdDwFHWqYQYkf8lArnVdiBzD290Xv5 BiiXdmvwa8oPNYxUmEvNsxUN0A== X-Received: by 2002:a5d:534c:0:b0:319:71be:9241 with SMTP id t12-20020a5d534c000000b0031971be9241mr5422160wrv.25.1694102629125; Thu, 07 Sep 2023 09:03:49 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id d1-20020adfa401000000b0031980294e9fsm20256241wra.116.2023.09.07.09.03.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Sep 2023 09:03:48 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH 12/14] target/arm: Implement MTE tag-checking functions for FEAT_MOPS copies Date: Thu, 7 Sep 2023 17:03:38 +0100 Message-Id: <20230907160340.260094-13-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230907160340.260094-1-peter.maydell@linaro.org> References: <20230907160340.260094-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::436; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x436.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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The FEAT_MOPS memory copy operations need an extra helper routine for checking for MTE tag checking failures beyond the ones we already added for memory set operations: * mte_mops_probe_rev() does the same job as mte_mops_probe(), but it checks tags starting at the provided address and working backwards, rather than forwards Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson --- target/arm/internals.h | 17 +++++++ target/arm/tcg/mte_helper.c | 99 +++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/target/arm/internals.h b/target/arm/internals.h index 642f77df29b..1dd9182a54a 100644 --- a/target/arm/internals.h +++ b/target/arm/internals.h @@ -1288,6 +1288,23 @@ uint64_t mte_check(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t ra); uint64_t mte_mops_probe(CPUARMState *env, uint64_t ptr, uint64_t size, uint32_t desc); +/** + * mte_mops_probe_rev: Check where the next MTE failure is for a FEAT_MOPS + * operation going in the reverse direction + * @env: CPU env + * @ptr: *end* address of memory region (dirty pointer) + * @size: length of region (guaranteed not to cross a page boundary) + * @desc: MTEDESC descriptor word (0 means no MTE checks) + * Returns: the size of the region that can be copied without hitting + * an MTE tag failure + * + * Note that we assume that the caller has already checked the TBI + * and TCMA bits with mte_checks_needed() and an MTE check is definitely + * required. + */ +uint64_t mte_mops_probe_rev(CPUARMState *env, uint64_t ptr, uint64_t size, + uint32_t desc); + /** * mte_check_fail: Record an MTE tag check failure * @env: CPU env diff --git a/target/arm/tcg/mte_helper.c b/target/arm/tcg/mte_helper.c index d934f89bebb..ad53027044e 100644 --- a/target/arm/tcg/mte_helper.c +++ b/target/arm/tcg/mte_helper.c @@ -733,6 +733,55 @@ static int checkN(uint8_t *mem, int odd, int cmp, int count) return n; } +/** + * checkNrev: + * @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. + * + * This is like checkN, but it runs backwards, checking the + * tags starting with @tag and then the tags preceding it. + * This is needed by the backwards-memory-copying operations. + */ +static int checkNrev(uint8_t *mem, int odd, int cmp, int count) +{ + int n = 0, diff; + + /* Replicate the test tag and compare. */ + cmp *= 0x11; + diff = *mem-- ^ cmp; + + if (!odd) { + goto start_even; + } + + while (1) { + /* Test odd tag. */ + if (unlikely((diff) & 0xf0)) { + break; + } + if (++n == count) { + break; + } + + start_even: + /* Test even tag. */ + if (unlikely((diff) & 0x0f)) { + break; + } + if (++n == count) { + break; + } + + diff = *mem-- ^ cmp; + } + return n; +} + /** * mte_probe_int() - helper for mte_probe and mte_check * @env: CPU environment @@ -1041,6 +1090,56 @@ uint64_t mte_mops_probe(CPUARMState *env, uint64_t ptr, uint64_t size, } } +uint64_t mte_mops_probe_rev(CPUARMState *env, uint64_t ptr, uint64_t size, + uint32_t desc) +{ + int mmu_idx, tag_count; + uint64_t ptr_tag, tag_first, tag_last; + void *mem; + bool w = FIELD_EX32(desc, MTEDESC, WRITE); + uint32_t n; + + mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX); + /* True probe; this will never fault */ + mem = allocation_tag_mem_probe(env, mmu_idx, ptr, + w ? MMU_DATA_STORE : MMU_DATA_LOAD, + size, MMU_DATA_LOAD, true, 0); + if (!mem) { + return size; + } + + /* + * TODO: checkNrev() is not designed for checks of the size we expect + * for FEAT_MOPS operations, so we should implement this differently. + * Maybe we should do something like + * if (region start and size are aligned nicely) { + * do direct loads of 64 tag bits at a time; + * } else { + * call checkN() + * } + */ + /* Round the bounds to the tag granule, and compute the number of tags. */ + ptr_tag = allocation_tag_from_addr(ptr); + tag_first = QEMU_ALIGN_DOWN(ptr - (size - 1), TAG_GRANULE); + tag_last = QEMU_ALIGN_DOWN(ptr, TAG_GRANULE); + tag_count = ((tag_last - tag_first) / TAG_GRANULE) + 1; + n = checkNrev(mem, ptr & TAG_GRANULE, ptr_tag, tag_count); + if (likely(n == tag_count)) { + return size; + } + + /* + * Failure; for the first granule, it's at @ptr. Otherwise + * it's at the last byte of the nth granule. Calculate how + * many bytes we can access without hitting that failure. + */ + if (n == 0) { + return 0; + } else { + return (n - 1) * TAG_GRANULE + ((ptr + 1) - tag_last); + } +} + void mte_mops_set_tags(CPUARMState *env, uint64_t ptr, uint64_t size, uint32_t desc) {