From patchwork Fri Apr 7 15:42:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 97039 Delivered-To: patch@linaro.org Received: by 10.140.89.233 with SMTP id v96csp341906qgd; Fri, 7 Apr 2017 08:48:17 -0700 (PDT) X-Received: by 10.237.53.226 with SMTP id d31mr40657198qte.184.1491580097626; Fri, 07 Apr 2017 08:48:17 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id p18si5148330qtc.21.2017.04.07.08.48.17 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 07 Apr 2017 08:48:17 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:51515 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cwW7V-0006fm-5p for patch@linaro.org; Fri, 07 Apr 2017 11:48:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53430) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cwW1t-0002RX-2G for qemu-devel@nongnu.org; Fri, 07 Apr 2017 11:42:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cwW1s-0007Wi-4a for qemu-devel@nongnu.org; Fri, 07 Apr 2017 11:42:29 -0400 Received: from mail-wm0-x232.google.com ([2a00:1450:400c:c09::232]:35735) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cwW1r-0007WU-UC for qemu-devel@nongnu.org; Fri, 07 Apr 2017 11:42:28 -0400 Received: by mail-wm0-x232.google.com with SMTP id w64so1698364wma.0 for ; Fri, 07 Apr 2017 08:42:27 -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=FymwcrX/nal7C5r6MWL6iyizFy4DQcKc7z+EDAFp5Oo=; b=c+0ABzIeRjPWanWunWoCKaZg3YmHxCGuampRPIsMoYpUOfF+4yT+jaM2HMSS855rNV p7r5RSbEdtwRY4uQqzzNxBm9jXjPiZd47//cBN+Y1rKbW35KXWx+Wwz4gZ9Eht8hgADM yJ+sK7/h9yAkeSvIfFTLDziDcOsifAp8QheMU= 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=FymwcrX/nal7C5r6MWL6iyizFy4DQcKc7z+EDAFp5Oo=; b=a6ykgT0ICGmKkhFT3WY+8fhq4ZLCcxUqMml7S7BjYpZIXQAUSVAoFK0ES93YtT0+ok dJsUxSRLZQ9uVeMyKmYKJUxkFWndMp6oI3lldX1c0fa5GVYicGZ6xCLecTT0/LwTrtiM 7MqNssGguGrktOHrQUTr7VrOEOXQdBf4priTggGExO5udYL2S8VsBXigv1V8Cs3VurSo /DZhBMXb7Ao/SptW1ckbla3L1WynV0PBFht1P4GGHT+M1mZU4/V0VCiukDKOfCcidNjz JF2NA0VuR1+uCXB5nyvs2Dipk1Zm0p2JS68Dts1L5fXA14H9HK7Md7gxT+S5+uR86zF1 r3bg== X-Gm-Message-State: AN3rC/7nnx3FPKVALJQuv/Ptffy3drXHp9fe8HJOKatYbGqvq7XO0ZzecruhzYJPVKpNoUbB X-Received: by 10.28.63.71 with SMTP id m68mr34442wma.46.1491579746860; Fri, 07 Apr 2017 08:42:26 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id g23sm6733124wme.8.2017.04.07.08.42.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Apr 2017 08:42:24 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id AEFC73E121E; Fri, 7 Apr 2017 16:42:22 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: dovgaluk@ispras.ru, rth@twiddle.net, pbonzini@redhat.com Date: Fri, 7 Apr 2017 16:42:18 +0100 Message-Id: <20170407154221.26918-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170407154221.26918-1-alex.bennee@linaro.org> References: <20170407154221.26918-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::232 Subject: [Qemu-devel] [PATCH v3 09/12] cpus: introduce cpu_update_icount helper X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mttcg@listserver.greensocs.com, peter.maydell@linaro.org, nikunj@linux.vnet.ibm.com, Peter Crosthwaite , a.rigo@virtualopensystems.com, qemu-devel@nongnu.org, cota@braap.org, bobby.prani@gmail.com, =?utf-8?q?Alex_Benn=C3=A9e?= , fred.konrad@greensocs.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" By holding off updates to timer_state.qemu_icount we can run into trouble when the non-vCPU thread needs to know the time. This helper ensures we atomically update timers_state.qemu_icount based on what has been currently executed. Signed-off-by: Alex Bennée --- cpus.c | 23 +++++++++++++++++++++-- include/qemu/timer.h | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) -- 2.11.0 diff --git a/cpus.c b/cpus.c index 88eabdc19f..3854f17c35 100644 --- a/cpus.c +++ b/cpus.c @@ -232,12 +232,31 @@ static int64_t cpu_get_icount_executed(CPUState *cpu) return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra); } +/* + * Update the global shared timer_state.qemu_icount to take into + * account executed instructions. This is done by the TCG vCPU + * thread so the main-loop can see time has moved forward. + */ +void cpu_update_icount(CPUState *cpu) +{ + int64_t executed = cpu_get_icount_executed(cpu); + cpu->icount_budget -= executed; + +#ifdef CONFIG_ATOMIC64 + atomic_set__nocheck(&timers_state.qemu_icount, + atomic_read__nocheck(&timers_state.qemu_icount) + + executed); +#else /* FIXME: we need 64bit atomics to do this safely */ + timers_state.qemu_icount += executed; +#endif +} + int64_t cpu_get_icount_raw(void) { int64_t icount; CPUState *cpu = current_cpu; - icount = timers_state.qemu_icount; + icount = atomic_read(&timers_state.qemu_icount); if (cpu && cpu->running) { if (!cpu->can_do_io) { fprintf(stderr, "Bad icount read\n"); @@ -1220,7 +1239,7 @@ static void process_icount_data(CPUState *cpu) { if (use_icount) { /* Account for executed instructions */ - timers_state.qemu_icount += cpu_get_icount_executed(cpu); + cpu_update_icount(cpu); /* Reset the counters */ cpu->icount_decr.u16.low = 0; diff --git a/include/qemu/timer.h b/include/qemu/timer.h index e1742f2f3d..8a1eb74839 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -869,6 +869,7 @@ int64_t cpu_get_icount_raw(void); int64_t cpu_get_icount(void); int64_t cpu_get_clock(void); int64_t cpu_icount_to_ns(int64_t icount); +void cpu_update_icount(CPUState *cpu); /*******************************************/ /* host CPU ticks (if available) */