From patchwork Mon Apr 10 12:55:21 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: 97128 Delivered-To: patch@linaro.org Received: by 10.140.89.233 with SMTP id v96csp1350654qgd; Mon, 10 Apr 2017 06:01:51 -0700 (PDT) X-Received: by 10.31.166.80 with SMTP id p77mr1535337vke.123.1491829311882; Mon, 10 Apr 2017 06:01:51 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id 33si765855uaz.248.2017.04.10.06.01.51 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 10 Apr 2017 06:01:51 -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]:34360 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxYx3-0007ZU-Hs for patch@linaro.org; Mon, 10 Apr 2017 09:01:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53432) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cxYqs-0002UI-7X for qemu-devel@nongnu.org; Mon, 10 Apr 2017 08:55:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cxYqr-00038g-15 for qemu-devel@nongnu.org; Mon, 10 Apr 2017 08:55:26 -0400 Received: from mail-wm0-x231.google.com ([2a00:1450:400c:c09::231]:35923) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cxYqq-00038P-R8 for qemu-devel@nongnu.org; Mon, 10 Apr 2017 08:55:24 -0400 Received: by mail-wm0-x231.google.com with SMTP id o81so38078211wmb.1 for ; Mon, 10 Apr 2017 05:55:24 -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=9z7AxeVQ2/e8nqiU6HckJPKW2WhL9ciN/KoobzwdaVI=; b=Niz/OD9+uX63B8L2rMVNlsqhK5+BZsUXvmsQuzb7SL2R7phkOksyvVJuDxfVRkRydz DVTLKG4GfpWwbdlENIRZWjbg4WNgwWgVBjYkcT761AS2ziPWEjcOLHdVfqbq5R0Ld9bq TF83FQDTfh20pUv6CJKTUKw5AhTTMqouwY348= 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=9z7AxeVQ2/e8nqiU6HckJPKW2WhL9ciN/KoobzwdaVI=; b=WMiEKJMLwqf0SEmzQ46wiXAHk7MQsK7B8Co5u3bcfPgNPtgXsmgM6DB3s+g4aLS43p LECM8KwUqX1q9oUdHulNtyf9rmEwwkMSzQxdVHfyg2qYi3uWLtM4pRdQrtBwjumo8nXp HECsS7T6I0s4xEPSPEWRG252sqB0RdsYl8rqACMSJHzOJ0OjNm8bRK66DD4Oz1jPkr1U JwL7Dydzrv+B3Sn3vBVkYx86VVHHoSQ8Umc1UhA/9JX8Bjg8LBg2RqV38frLzPQqfAp4 74Zj/z6PwceQFsv5g5pDoZckq3wuh/4N8e+xgS5FrHOVwjCf+78gRPKbVkL637/2Mck9 3apQ== X-Gm-Message-State: AN3rC/46y6fVf9HI3faDjbp0MFi8KSNGOvQINbbqhxif52RTaBF2Ef1R qTcmf3rQLZfjCQpi X-Received: by 10.28.87.138 with SMTP id l132mr10290819wmb.95.1491828923597; Mon, 10 Apr 2017 05:55:23 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id m139sm10119057wmb.27.2017.04.10.05.55.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Apr 2017 05:55:20 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 727373E1027; Mon, 10 Apr 2017 13:55:25 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Date: Mon, 10 Apr 2017 13:55:21 +0100 Message-Id: <20170410125524.21008-9-alex.bennee@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170410125524.21008-1-alex.bennee@linaro.org> References: <20170410125524.21008-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::231 Subject: [Qemu-devel] [PULL 08/11] 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: Paolo Bonzini , Richard Henderson , =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org, Peter Crosthwaite 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 -- 2.11.0 diff --git a/cpus.c b/cpus.c index 0ecb0b87f0..a5125d7167 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) */