From patchwork Wed Sep 14 11:52:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 605729 Delivered-To: patch@linaro.org Received: by 2002:ab3:1c02:0:b0:1e7:4ce8:c350 with SMTP id u2csp730694lth; Wed, 14 Sep 2022 06:12:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR4zu7fmi5ExUdvsxi9lrwk5pTU7D1zleFvd23B9v2KETaPZsVtVI/zIUaOiAkRPO2UXxbYI X-Received: by 2002:a05:620a:271b:b0:6cd:fd1f:7472 with SMTP id b27-20020a05620a271b00b006cdfd1f7472mr14426896qkp.142.1663161141953; Wed, 14 Sep 2022 06:12:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663161141; cv=none; d=google.com; s=arc-20160816; b=gnEPHzICmOvmnt+YGfMiJUx1bTdzH8p62MZf9nwu0n5SuK3GgBB6x1GMfxa2i/ey1q aP4+92v6TCH8ETXMd1EoulSgQV2z0P/My5IPXNOTmmhT/HHC4GlGfmwU6m7s/389RzR8 0jDQNERR4GYAc5KXcUQIgdWRQ19ocbLNwMqnCvoAlHV4cJakhO8sJzVDkt2J4Q+lQ8GF nf9rKIsbv0mrBr4BQInr4MBmhFE8zbdK+OHAakmLyGBelyNbbz/64a7DGWAbygfmR1iE HzX8hcbIsd2qbdEh4cndVs5jNFFQiInWdyE9uHPHKBKYw4AtMjZj972Dg21P4QC0lRjY OeGQ== 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:cc:to :from:dkim-signature; bh=cWtVxLGk2Gz3c0rqKUOonGO/yrqOCOPVcHSSu/486Rw=; b=RgxFZUmtkY/GInVXzAMxtUa6N3txUQAMCo8j/4Wb4wh0vPWSQZD/KZ7JM7cU6OMA8T +4vNQpHTmBtvXEqOMf6zHsD91fAcFvXFHHdjOu+7F0CA8v7o+6SqupgkyDu8QWILbPoQ aPYjsYsbZGuCFKsSulRTBAGjolqFhyFxzTwTnxuYJ2kaDfzcqBBsFPU57gMevOrMdaly aJoZ0qykAaZykdzjz8lvR9VHBOBjA/bl+XsdHeOfE01Tt8WiUBfSWj9fR3TT5w6ngq3S TOgHOqV/KVgZHuhQyggx2SDOww9nE/NKyVNhyMI8IKTojtL6vXUjA+X+qQyTCdnEAUB0 rhxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="ja/yRueS"; 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 w6-20020a05620a424600b006cbdc9d33d8si1405435qko.429.2022.09.14.06.12.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 14 Sep 2022 06:12:21 -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="ja/yRueS"; 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]:50016 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oYSBf-0000zz-VB for patch@linaro.org; Wed, 14 Sep 2022 09:12:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39750) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oYQwc-0000Xi-SV for qemu-devel@nongnu.org; Wed, 14 Sep 2022 07:52:42 -0400 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]:47018) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oYQwU-00006d-AP for qemu-devel@nongnu.org; Wed, 14 Sep 2022 07:52:42 -0400 Received: by mail-wm1-x32d.google.com with SMTP id n40-20020a05600c3ba800b003b49aefc35fso2092463wms.5 for ; Wed, 14 Sep 2022 04:52:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=cWtVxLGk2Gz3c0rqKUOonGO/yrqOCOPVcHSSu/486Rw=; b=ja/yRueSVKZ7ylhmIRANZaQgPgwiigr1YqdumzoV0IUKLOUD8vSrqjM21hwdwaP9Mt SpROa/N7SjCJQ9C9jI/exZtUv9/1c/rk979uZepwP0OkGGpdYeGbp1BEIA9R1gJZG8N2 cwRg6GFDQmUNXKTtmulr40XfMIZI4xxOa2W2AUiWbS8nVKuQD5LzphEnyRDMDzwiLVwd 0OHzLP6x8dxYyh1rRVTCHTNPwVd4rHa8sGLMFjF9MQ/LZhmV3624p3cNtT7NhHEotCCw Q2fRjaQlUGlclcZDzzGgzUSTfSXrttYgpZms2hoYbllKS7VxzWqYZSPvJWKAaLrwnHyx Pzgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=cWtVxLGk2Gz3c0rqKUOonGO/yrqOCOPVcHSSu/486Rw=; b=2rPm46R5uBjqN+B0Fq5b8XFWJkNBuFoJUG3U2Oun8s6dIt4QPLH0KlVfuRUAKxF+G9 MZtvj3M52rlmwmLS6gEB0tBvHWSu9GbP/zLf5SEUZGx6rxqLGmEjWNBglmg3Gjh24HxV r0UKIRnk8RWlRHvcQaoISFMojgQHxwygm5u4EhNtkw3iKjujJTljN1tejjxLs6WaVXyx 1S2q4f19QQhJLD+0sXBE9X6pGoRVY6AHhl2NTn3wbHUvKcCQl5QvzD9vS6Lq6MFdaG3d VnSJ6qpV0+1Och9u1AMnxE5aseiya10avC8aR5rSu3yl0oBlWGycKlkmwvHwa6oJ0bos JljQ== X-Gm-Message-State: ACgBeo0TuRURG2gC5UO9guMcbFvi4loQQy9r2M64d03uVfKCM0gcOEoQ KP/PXQ6bBKMVpqRZXaGGc4t7Cr5wwhoR5HSu X-Received: by 2002:a05:600c:414b:b0:3b4:a655:720f with SMTP id h11-20020a05600c414b00b003b4a655720fmr955500wmm.92.1663156352920; Wed, 14 Sep 2022 04:52:32 -0700 (PDT) Received: from stoup.. ([89.101.193.70]) by smtp.gmail.com with ESMTPSA id t9-20020a5d6909000000b0022a3a887ceasm13712970wru.49.2022.09.14.04.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Sep 2022 04:52:32 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Peter Maydell Subject: [PULL 14/20] target/arm: Detect overflow when calculating next PMU interrupt Date: Wed, 14 Sep 2022 12:52:11 +0100 Message-Id: <20220914115217.117532-16-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220914115217.117532-1-richard.henderson@linaro.org> References: <20220914115217.117532-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32d; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x32d.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, T_SCC_BODY_TEXT_LINE=-0.01 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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell In pmccntr_op_finish() and pmevcntr_op_finish() we calculate the next point at which we will get an overflow and need to fire the PMU interrupt or set the overflow flag. We do this by calculating the number of nanoseconds to the overflow event and then adding it to qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL). However, we don't check whether that signed addition overflows, which can happen if the next PMU interrupt would happen massively far in the future (250 years or more). Since QEMU assumes that "when the QEMU_CLOCK_VIRTUAL rolls over" is "never", the sensible behaviour in this situation is simply to not try to set the timer if it would be beyond that point. Detect the overflow, and skip setting the timer in that case. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-Id: <20220822132358.3524971-7-peter.maydell@linaro.org> Signed-off-by: Richard Henderson --- target/arm/helper.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/target/arm/helper.c b/target/arm/helper.c index b792694df0..11a7a57710 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -1227,10 +1227,13 @@ static void pmccntr_op_finish(CPUARMState *env) int64_t overflow_in = cycles_ns_per(remaining_cycles); if (overflow_in > 0) { - int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + - overflow_in; - ARMCPU *cpu = env_archcpu(env); - timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); + int64_t overflow_at; + + if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), + overflow_in, &overflow_at)) { + ARMCPU *cpu = env_archcpu(env); + timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); + } } #endif @@ -1275,10 +1278,13 @@ static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter) int64_t overflow_in = pm_events[event_idx].ns_per_count(delta); if (overflow_in > 0) { - int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + - overflow_in; - ARMCPU *cpu = env_archcpu(env); - timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); + int64_t overflow_at; + + if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), + overflow_in, &overflow_at)) { + ARMCPU *cpu = env_archcpu(env); + timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); + } } #endif