All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: fam@euphon.net, "Peter Maydell" <peter.maydell@linaro.org>,
	berrange@redhat.com, robert.foley@linaro.org,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	richard.henderson@linaro.org, f4bug@amsat.org,
	robhenry@microsoft.com, aaron@os.amperecomputing.com,
	cota@braap.org, "Pavel Dovgalyuk" <dovgaluk@ispras.ru>,
	kuhn.chenqun@huawei.com, peter.puhov@linaro.org,
	aurelien@aurel32.net
Subject: [PATCH v1 02/13] docs/devel: add some notes on tcg-icount for developers
Date: Thu,  9 Jul 2020 15:13:16 +0100	[thread overview]
Message-ID: <20200709141327.14631-3-alex.bennee@linaro.org> (raw)
In-Reply-To: <20200709141327.14631-1-alex.bennee@linaro.org>

This attempts to bring together my understanding of the requirements
for icount behaviour into one reference document for our developer
notes.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Pavel Dovgalyuk <dovgaluk@ispras.ru>
Cc: Peter Maydell <peter.maydell@linaro.org>
Message-Id: <20200619135844.23307-1-alex.bennee@linaro.org>

---
v2
  - fix copyright date
  - it's -> its
  - drop mentioned of gen_io_end()
  - remove and correct original conjecture
v3
  - include link in index
v4
  - Grammar fixes from Peter
  - re-worded final section
---
 docs/devel/index.rst      |  1 +
 docs/devel/tcg-icount.rst | 97 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+)
 create mode 100644 docs/devel/tcg-icount.rst

diff --git a/docs/devel/index.rst b/docs/devel/index.rst
index 4ecaea3643fb..ae6eac7c9c66 100644
--- a/docs/devel/index.rst
+++ b/docs/devel/index.rst
@@ -23,6 +23,7 @@ Contents:
    decodetree
    secure-coding-practices
    tcg
+   tcg-icount
    multi-thread-tcg
    tcg-plugins
    bitops
diff --git a/docs/devel/tcg-icount.rst b/docs/devel/tcg-icount.rst
new file mode 100644
index 000000000000..8d67b6c076a7
--- /dev/null
+++ b/docs/devel/tcg-icount.rst
@@ -0,0 +1,97 @@
+..
+   Copyright (c) 2020, Linaro Limited
+   Written by Alex Bennée
+
+
+========================
+TCG Instruction Counting
+========================
+
+TCG has long supported a feature known as icount which allows for
+instruction counting during execution. This should not be confused
+with cycle accurate emulation - QEMU does not attempt to emulate how
+long an instruction would take on real hardware. That is a job for
+other more detailed (and slower) tools that simulate the rest of a
+micro-architecture.
+
+This feature is only available for system emulation and is
+incompatible with multi-threaded TCG. It can be used to better align
+execution time with wall-clock time so a "slow" device doesn't run too
+fast on modern hardware. It can also provides for a degree of
+deterministic execution and is an essential part of the record/replay
+support in QEMU.
+
+Core Concepts
+=============
+
+At its heart icount is simply a count of executed instructions which
+is stored in the TimersState of QEMU's timer sub-system. The number of
+executed instructions can then be used to calculate QEMU_CLOCK_VIRTUAL
+which represents the amount of elapsed time in the system since
+execution started. Depending on the icount mode this may either be a
+fixed number of ns per instruction or adjusted as execution continues
+to keep wall clock time and virtual time in sync.
+
+To be able to calculate the number of executed instructions the
+translator starts by allocating a budget of instructions to be
+executed. The budget of instructions is limited by how long it will be
+until the next timer will expire. We store this budget as part of a
+vCPU icount_decr field which shared with the machinery for handling
+cpu_exit(). The whole field is checked at the start of every
+translated block and will cause a return to the outer loop to deal
+with whatever caused the exit.
+
+In the case of icount, before the flag is checked we subtract the
+number of instructions the translation block would execute. If this
+would cause the instruction budget to go negative we exit the main
+loop and regenerate a new translation block with exactly the right
+number of instructions to take the budget to 0 meaning whatever timer
+was due to expire will expire exactly when we exit the main run loop.
+
+Dealing with MMIO
+-----------------
+
+While we can adjust the instruction budget for known events like timer
+expiry we cannot do the same for MMIO. Every load/store we execute
+might potentially trigger an I/O event, at which point we will need an
+up to date and accurate reading of the icount number.
+
+To deal with this case, when an I/O access is made we:
+
+  - restore un-executed instructions to the icount budget
+  - re-compile a single [1]_ instruction block for the current PC
+  - exit the cpu loop and execute the re-compiled block
+
+The new block is created with the CF_LAST_IO compile flag which
+ensures the final instruction translation starts with a call to
+gen_io_start() so we don't enter a perpetual loop constantly
+recompiling a single instruction block. For translators using the
+common translator_loop this is done automatically.
+  
+.. [1] sometimes two instructions if dealing with delay slots  
+
+Other I/O operations
+--------------------
+
+MMIO isn't the only type of operation for which we might need a
+correct and accurate clock. IO port instructions and accesses to
+system registers are the common examples here. These instructions have
+to be handled by the individual translators which have the knowledge
+of which operations are I/O operations.
+
+When the translator is handling an instruction of this kind:
+
+* it must call gen_io_start() if icount is enabled, at some
+   point before the generation of the code which actually does
+   the I/O, using a code fragment similar to:
+
+.. code:: c
+
+    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
+        gen_io_start();
+    }
+
+* it must end the TB immediately after this instruction
+
+Note that some older front-ends call a "gen_io_end()" function:
+this is obsolete and should not be used.
-- 
2.20.1



  parent reply	other threads:[~2020-07-09 14:14 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-09 14:13 [PATCH v1 00/13] misc rc0 fixes (docs, plugins, docker) Alex Bennée
2020-07-09 14:13 ` [PATCH v1 01/13] docs/devel: convert and update MTTCG design document Alex Bennée
2020-07-11 20:38   ` Emilio G. Cota
2020-07-09 14:13 ` Alex Bennée [this message]
2020-07-11 20:41   ` [PATCH v1 02/13] docs/devel: add some notes on tcg-icount for developers Emilio G. Cota
2020-07-09 14:13 ` [PATCH v1 03/13] docs: Add to gdbstub documentation the PhyMemMode Alex Bennée
2020-07-09 14:40   ` Philippe Mathieu-Daudé
2020-07-09 14:13 ` [PATCH v1 04/13] cputlb: ensure we save the IOTLB data in case of reset Alex Bennée
2020-07-10 21:03   ` Richard Henderson
2020-07-11 21:30     ` Emilio G. Cota
2020-07-12  9:58       ` Alex Bennée
2020-07-11 21:10   ` Emilio G. Cota
2020-07-09 14:13 ` [PATCH v1 05/13] hw/virtio/pci: include vdev name in registered PCI sections Alex Bennée
2020-07-27 13:32   ` Michael S. Tsirkin
2020-07-27 16:22     ` Alex Bennée
2020-07-09 14:13 ` [PATCH v1 06/13] plugins: add API to return a name for a IO device Alex Bennée
2020-07-09 15:03   ` Philippe Mathieu-Daudé
2020-07-09 16:30     ` Alex Bennée
2020-07-09 14:13 ` [PATCH v1 07/13] plugins: new hwprofile plugin Alex Bennée
2020-07-09 14:13 ` [PATCH v1 08/13] plugins: expand the bb plugin to be thread safe and track per-cpu Alex Bennée
2020-07-09 17:12   ` Robert Foley
2020-07-11 21:56   ` Emilio G. Cota
2020-07-12  9:48     ` Alex Bennée
2020-07-09 14:13 ` [PATCH v1 09/13] target/sh4: revert to using cpu_lduw_code to decode gusa Alex Bennée
2020-07-09 14:42   ` Philippe Mathieu-Daudé
2020-07-10 21:26   ` Richard Henderson
2020-07-09 14:13 ` [PATCH v1 10/13] tests/plugins: add -Wno-unknown-warning-option to handle -Wpsabi Alex Bennée
2020-07-09 17:27   ` Robert Foley
2020-07-10 21:29   ` Richard Henderson
2020-07-13 16:39     ` Alex Bennée
2020-07-13 18:27       ` Thomas Huth
2020-07-13 19:34         ` Alex Bennée
2020-07-09 14:13 ` [PATCH v1 11/13] tests/docker: fall back more gracefully when pull fails Alex Bennée
2020-07-09 14:46   ` Philippe Mathieu-Daudé
2020-07-09 14:13 ` [PATCH v1 12/13] tests/docker: update toolchain set in debian-xtensa-cross Alex Bennée
2020-07-09 14:54   ` Philippe Mathieu-Daudé
2020-07-09 14:13 ` [PATCH v1 13/13] configure: remove all dependencies on a (re)configure Alex Bennée

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200709141327.14631-3-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=aaron@os.amperecomputing.com \
    --cc=aurelien@aurel32.net \
    --cc=berrange@redhat.com \
    --cc=cota@braap.org \
    --cc=dovgaluk@ispras.ru \
    --cc=f4bug@amsat.org \
    --cc=fam@euphon.net \
    --cc=kuhn.chenqun@huawei.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peter.puhov@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=robert.foley@linaro.org \
    --cc=robhenry@microsoft.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.