From patchwork Mon Aug 22 13:23:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 599109 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:4388:0:0:0:0 with SMTP id w8csp1655810mae; Mon, 22 Aug 2022 06:29:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR6eHr4ok9BwwlQdk+tIwisTobnq7iv7ZmZd4TLZocoQPjmXqILVG+HaLGzmGtHyxUDim0w8 X-Received: by 2002:a05:6214:2a81:b0:473:af82:9a95 with SMTP id jr1-20020a0562142a8100b00473af829a95mr15717516qvb.44.1661174970789; Mon, 22 Aug 2022 06:29:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661174970; cv=none; d=google.com; s=arc-20160816; b=W8BximQ1etricWWCHRdPe37ZjGmxB8iC+3lOJmAOV7M+aFRCAkKMWyXIC6EAYpbAxX /mn1pal2Tx5kQmBTKkw5t9hcJGl8xvpGtCEDZh+3PYa16PtuPuI2juoNTOuRGtfShJlf xB451FIAOYw5ETM6IanDUE1LiKVCBAHwMt7BxrDEwJN+YMKy7koAvKCt3Na09nyso9ac caiTwAYGAaWCpA9l9WEp3MxKsl39eopDlc808i5aLfMD+Ir1cx7hglZXOqeGb2F4dVhd bY4rekmUng0tvT4RhDzo+vGbaxZ1nOmqTyeDvJChZa7SN+Hmkv2ms+qd+sOFkC4IAwaK xwAg== 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=gAJToUGMZxgV0d4Q4X3oKY4UbjuKkCvUr3anhNbi7Gw=; b=Z43WR9iUevs0VcjmDtLS8MLT0bmEzc9JLvflKwghy7zYsEgBTGC4aTNIa00G8/+bSc O7+CDB4zksO2HFrqVsIIsKRnGmeWE7FEGj0oPj3sByRp/GFYiQq01dmbg8+k4o8EgG2j LIY6A7bFjaARGIK7OuhzP+HpEi+JytpYwNN+cm3R9aLowbBcSft/7niQQHvt/HbtEVRj 4fzFnzQoPvkYQsCBH0Pc3QHPndvhyy5aAfg1Uphp7f1sl4sRo9B3qSgrLVLKZQSr0yhb J1jmnr8jT661UJUwAX5/nDOC6V/QgzFpTUfedWtHJHzZq+Lofj61tnrZO/JKWJIdylJw Nt9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=mp+4Cvc2; 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 h23-20020ae9ec17000000b006bc04759a94si1712223qkg.411.2022.08.22.06.29.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 22 Aug 2022 06:29:30 -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=mp+4Cvc2; 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]:47580 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQ7Ug-0004vY-Dz for patch@linaro.org; Mon, 22 Aug 2022 09:29:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58710) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQ7PW-00065D-O1 for qemu-devel@nongnu.org; Mon, 22 Aug 2022 09:24:10 -0400 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]:38731) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oQ7PU-0001fL-Vi for qemu-devel@nongnu.org; Mon, 22 Aug 2022 09:24:10 -0400 Received: by mail-wr1-x429.google.com with SMTP id b5so8926148wrr.5 for ; Mon, 22 Aug 2022 06:24:07 -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; bh=gAJToUGMZxgV0d4Q4X3oKY4UbjuKkCvUr3anhNbi7Gw=; b=mp+4Cvc2B2WVUm2VMs2TIKnUWypi5GuYqJehgRpGcGyztzYwY2ExisP+c7HQZnloeL 9DXonw24UVUI/opfwc3UrMKXistkpbxPpA3p2mvDmJyVM/Tzrx5bt4WpD+GJCp5IPyRg 00h7GN7aDZfzJSPsSFxtANOEExTX2D5wRz2Jhmhlgh/ncIARqVQOaTt2g+7aJGx4qsei oRVW9Pwu6EqFZEot/omyUqfo4+y8GqAMIS8a397RMt/NxnogXWs7hBdzSUnw0wbJsGpb 8qTBr351CJU44LRsLYaanf+VcErpWsxaTEAmj/jS+cxFDN7QSX/6IrH9hYdsAAYH/Vl+ ERYw== 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; bh=gAJToUGMZxgV0d4Q4X3oKY4UbjuKkCvUr3anhNbi7Gw=; b=KnGx6FvWb142ofxNeByhcLXleniY/j2uKVKbf9spcdldGQzStdJSNxI5O1yFgXA8Tz 3qDrdlnqeX2FelNtV2tKOMN4C/IlZulJA/g5oAlOeeXL/SoHpwAk0mNt4o00htGYIsAo DVXq0QxRPvivQW2LDmIjrwYtqSYVPfmhJAO046Vox8MTkMSdkd+ox7jlJhRxNbD2QET6 5TeS4SgzM+vAo/KEE7qjaO4FkPaTbVDo5++VHB2dABayNv84tZ/BNwmW9LG37gXJ89Lb VjaPddjVlKHNcCRFe3DIWt8GRrPeO4goglyd/r0JA9iY8m3TodmgeXoHcfef58Pgc5Jn xbog== X-Gm-Message-State: ACgBeo3KZI8AwkfddhzfO7/wCGdHWYs/BJ5NS3p66t/8T28156GChBWA DH5k1bO4UouDmNNSUJyzbLgZj4SUmaskbA== X-Received: by 2002:adf:de08:0:b0:225:426d:480 with SMTP id b8-20020adfde08000000b00225426d0480mr6492941wrm.636.1661174646739; Mon, 22 Aug 2022 06:24:06 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id f12-20020adff98c000000b0021f13097d6csm11527946wrr.16.2022.08.22.06.24.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Aug 2022 06:24:06 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: Richard Henderson Subject: [PATCH v2 06/10] target/arm: Detect overflow when calculating next PMU interrupt Date: Mon, 22 Aug 2022 14:23:54 +0100 Message-Id: <20220822132358.3524971-7-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220822132358.3524971-1-peter.maydell@linaro.org> References: <20220822132358.3524971-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x429.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" 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 --- v1->v2: fixed bogus indentation --- 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 7d4127a1573..94307a6c417 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