One Hat Cyber Team
Your IP :
216.73.216.115
Server IP :
194.44.31.54
Server :
Linux zen.imath.kiev.ua 4.18.0-553.77.1.el8_10.x86_64 #1 SMP Fri Oct 3 14:30:23 UTC 2025 x86_64
Server Software :
Apache/2.4.37 (Rocky Linux) OpenSSL/1.1.1k
PHP Version :
5.6.40
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
usr
/
share
/
doc
/
qemu-kvm
/
devel
/
Edit File:
tcg.html
<!DOCTYPE html> <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Translator Internals — QEMU qemu-kvm-6.2.0-53.module+el8.10.0+2055+8eb7870b.4 documentation</title> <link rel="shortcut icon" href="../_static/qemu_32x32.png"/> <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="TCG Instruction Counting" href="tcg-icount.html" /> <link rel="prev" title="Secure Coding Practices" href="secure-coding-practices.html" /> <script src="../_static/js/modernizr.min.js"></script> </head> <body class="wy-body-for-nav"> <div class="wy-grid-for-nav"> <nav data-toggle="wy-nav-shift" class="wy-nav-side"> <div class="wy-side-scroll"> <div class="wy-side-nav-search"> <a href="../index.html" class="icon icon-home"> QEMU <img src="../_static/qemu_128x128.png" class="logo" alt="Logo"/> </a> <div class="version"> 6.2.0 </div> <div role="search"> <form id="rtd-search-form" class="wy-form" action="../search.html" method="get"> <input type="text" name="q" placeholder="Search docs" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> <p class="caption"><span class="caption-text">Contents:</span></p> <ul class="current"> <li class="toctree-l1"><a class="reference internal" href="../about/index.html">About QEMU</a></li> <li class="toctree-l1"><a class="reference internal" href="../system/index.html">System Emulation</a></li> <li class="toctree-l1"><a class="reference internal" href="../user/index.html">User Mode Emulation</a></li> <li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Tools</a></li> <li class="toctree-l1"><a class="reference internal" href="../interop/index.html">System Emulation Management and Interoperability</a></li> <li class="toctree-l1"><a class="reference internal" href="../specs/index.html">System Emulation Guest Hardware Specifications</a></li> <li class="toctree-l1 current"><a class="reference internal" href="index.html">Developer Information</a><ul class="current"> <li class="toctree-l2"><a class="reference internal" href="code-of-conduct.html">Code of Conduct</a></li> <li class="toctree-l2"><a class="reference internal" href="conflict-resolution.html">Conflict Resolution Policy</a></li> <li class="toctree-l2"><a class="reference internal" href="build-system.html">The QEMU build system architecture</a></li> <li class="toctree-l2"><a class="reference internal" href="style.html">QEMU Coding Style</a></li> <li class="toctree-l2"><a class="reference internal" href="kconfig.html">QEMU and Kconfig</a></li> <li class="toctree-l2"><a class="reference internal" href="testing.html">Testing in QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="fuzzing.html">Fuzzing</a></li> <li class="toctree-l2"><a class="reference internal" href="control-flow-integrity.html">Control-Flow Integrity (CFI)</a></li> <li class="toctree-l2"><a class="reference internal" href="loads-stores.html">Load and Store APIs</a></li> <li class="toctree-l2"><a class="reference internal" href="memory.html">The memory API</a></li> <li class="toctree-l2"><a class="reference internal" href="migration.html">Migration</a></li> <li class="toctree-l2"><a class="reference internal" href="atomics.html">Atomic operations in QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="stable-process.html">QEMU and the stable process</a></li> <li class="toctree-l2"><a class="reference internal" href="ci.html">CI</a></li> <li class="toctree-l2"><a class="reference internal" href="qtest.html">QTest Device Emulation Testing Framework</a></li> <li class="toctree-l2"><a class="reference internal" href="decodetree.html">Decodetree Specification</a></li> <li class="toctree-l2"><a class="reference internal" href="secure-coding-practices.html">Secure Coding Practices</a></li> <li class="toctree-l2 current"><a class="current reference internal" href="#">Translator Internals</a><ul> <li class="toctree-l3"><a class="reference internal" href="#cpu-state-optimisations">CPU state optimisations</a></li> <li class="toctree-l3"><a class="reference internal" href="#direct-block-chaining">Direct block chaining</a><ul> <li class="toctree-l4"><a class="reference internal" href="#lookup-and-goto-ptr"><code class="docutils literal notranslate"><span class="pre">lookup_and_goto_ptr</span></code></a></li> <li class="toctree-l4"><a class="reference internal" href="#goto-tb-exit-tb"><code class="docutils literal notranslate"><span class="pre">goto_tb</span> <span class="pre">+</span> <span class="pre">exit_tb</span></code></a></li> </ul> </li> <li class="toctree-l3"><a class="reference internal" href="#self-modifying-code-and-translated-code-invalidation">Self-modifying code and translated code invalidation</a></li> <li class="toctree-l3"><a class="reference internal" href="#exception-support">Exception support</a></li> <li class="toctree-l3"><a class="reference internal" href="#mmu-emulation">MMU emulation</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="tcg-icount.html">TCG Instruction Counting</a></li> <li class="toctree-l2"><a class="reference internal" href="tracing.html">Tracing</a></li> <li class="toctree-l2"><a class="reference internal" href="multi-thread-tcg.html">Multi-threaded TCG</a></li> <li class="toctree-l2"><a class="reference internal" href="tcg-plugins.html">QEMU TCG Plugins</a></li> <li class="toctree-l2"><a class="reference internal" href="bitops.html">Bitwise operations</a></li> <li class="toctree-l2"><a class="reference internal" href="ui.html">QEMU UI subsystem</a></li> <li class="toctree-l2"><a class="reference internal" href="reset.html">Reset in QEMU: the Resettable interface</a></li> <li class="toctree-l2"><a class="reference internal" href="s390-dasd-ipl.html">Booting from real channel-attached devices on s390x</a></li> <li class="toctree-l2"><a class="reference internal" href="clocks.html">Modelling a clock tree in QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="qom.html">The QEMU Object Model (QOM)</a></li> <li class="toctree-l2"><a class="reference internal" href="modules.html">QEMU modules</a></li> <li class="toctree-l2"><a class="reference internal" href="block-coroutine-wrapper.html">block-coroutine-wrapper</a></li> <li class="toctree-l2"><a class="reference internal" href="multi-process.html">Multi-process QEMU</a></li> <li class="toctree-l2"><a class="reference internal" href="ebpf_rss.html">eBPF RSS virtio-net support</a></li> <li class="toctree-l2"><a class="reference internal" href="vfio-migration.html">VFIO device Migration</a></li> <li class="toctree-l2"><a class="reference internal" href="qapi-code-gen.html">How to use the QAPI code generator</a></li> <li class="toctree-l2"><a class="reference internal" href="writing-monitor-commands.html">How to write monitor commands</a></li> <li class="toctree-l2"><a class="reference internal" href="trivial-patches.html">Trivial Patches</a></li> <li class="toctree-l2"><a class="reference internal" href="submitting-a-patch.html">Submitting a Patch</a></li> <li class="toctree-l2"><a class="reference internal" href="submitting-a-pull-request.html">Submitting a Pull Request</a></li> </ul> </li> </ul> </div> </div> </nav> <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> <nav class="wy-nav-top" aria-label="top navigation"> <i data-toggle="wy-nav-top" class="fa fa-bars"></i> <a href="../index.html">QEMU</a> </nav> <div class="wy-nav-content"> <div class="rst-content"> <div role="navigation" aria-label="breadcrumbs navigation"> <ul class="wy-breadcrumbs"> <li><a href="../index.html">Docs</a> »</li> <li><a href="index.html">Developer Information</a> »</li> <li>Translator Internals</li> <li class="wy-breadcrumbs-aside"> <a href="https://gitlab.com/qemu-project/qemu/blob/master/docs/devel/tcg.rst" class="fa fa-gitlab"> Edit on GitLab</a> </li> </ul> <hr/> </div> <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> <div itemprop="articleBody"> <div class="section" id="translator-internals"> <h1>Translator Internals<a class="headerlink" href="#translator-internals" title="Permalink to this headline">¶</a></h1> <p>QEMU is a dynamic translator. When it first encounters a piece of code, it converts it to the host instruction set. Usually dynamic translators are very complicated and highly CPU dependent. QEMU uses some tricks which make it relatively easily portable and simple while achieving good performances.</p> <p>QEMU’s dynamic translation backend is called TCG, for “Tiny Code Generator”. For more information, please take a look at <code class="docutils literal notranslate"><span class="pre">tcg/README</span></code>.</p> <p>The following sections outline some notable features and implementation details of QEMU’s dynamic translator.</p> <div class="section" id="cpu-state-optimisations"> <h2>CPU state optimisations<a class="headerlink" href="#cpu-state-optimisations" title="Permalink to this headline">¶</a></h2> <p>The target CPUs have many internal states which change the way they evaluate instructions. In order to achieve a good speed, the translation phase considers that some state information of the virtual CPU cannot change in it. The state is recorded in the Translation Block (TB). If the state changes (e.g. privilege level), a new TB will be generated and the previous TB won’t be used anymore until the state matches the state recorded in the previous TB. The same idea can be applied to other aspects of the CPU state. For example, on x86, if the SS, DS and ES segments have a zero base, then the translator does not even generate an addition for the segment base.</p> </div> <div class="section" id="direct-block-chaining"> <h2>Direct block chaining<a class="headerlink" href="#direct-block-chaining" title="Permalink to this headline">¶</a></h2> <p>After each translated basic block is executed, QEMU uses the simulated Program Counter (PC) and other CPU state information (such as the CS segment base value) to find the next basic block.</p> <p>In its simplest, less optimized form, this is done by exiting from the current TB, going through the TB epilogue, and then back to the main loop. That’s where QEMU looks for the next TB to execute, translating it from the guest architecture if it isn’t already available in memory. Then QEMU proceeds to execute this next TB, starting at the prologue and then moving on to the translated instructions.</p> <p>Exiting from the TB this way will cause the <code class="docutils literal notranslate"><span class="pre">cpu_exec_interrupt()</span></code> callback to be re-evaluated before executing additional instructions. It is mandatory to exit this way after any CPU state changes that may unmask interrupts.</p> <p>In order to accelerate the cases where the TB for the new simulated PC is already available, QEMU has mechanisms that allow multiple TBs to be chained directly, without having to go back to the main loop as described above. These mechanisms are:</p> <div class="section" id="lookup-and-goto-ptr"> <h3><code class="docutils literal notranslate"><span class="pre">lookup_and_goto_ptr</span></code><a class="headerlink" href="#lookup-and-goto-ptr" title="Permalink to this headline">¶</a></h3> <p>Calling <code class="docutils literal notranslate"><span class="pre">tcg_gen_lookup_and_goto_ptr()</span></code> will emit a call to <code class="docutils literal notranslate"><span class="pre">helper_lookup_tb_ptr</span></code>. This helper will look for an existing TB that matches the current CPU state. If the destination TB is available its code address is returned, otherwise the address of the JIT epilogue is returned. The call to the helper is always followed by the tcg <code class="docutils literal notranslate"><span class="pre">goto_ptr</span></code> opcode, which branches to the returned address. In this way, we either branch to the next TB or return to the main loop.</p> </div> <div class="section" id="goto-tb-exit-tb"> <h3><code class="docutils literal notranslate"><span class="pre">goto_tb</span> <span class="pre">+</span> <span class="pre">exit_tb</span></code><a class="headerlink" href="#goto-tb-exit-tb" title="Permalink to this headline">¶</a></h3> <p>The translation code usually implements branching by performing the following steps:</p> <ol class="arabic simple"> <li>Call <code class="docutils literal notranslate"><span class="pre">tcg_gen_goto_tb()</span></code> passing a jump slot index (either 0 or 1) as a parameter.</li> <li>Emit TCG instructions to update the CPU state with any information that has been assumed constant and is required by the main loop to correctly locate and execute the next TB. For most guests, this is just the PC of the branch destination, but others may store additional data. The information updated in this step must be inferable from both <code class="docutils literal notranslate"><span class="pre">cpu_get_tb_cpu_state()</span></code> and <code class="docutils literal notranslate"><span class="pre">cpu_restore_state()</span></code>.</li> <li>Call <code class="docutils literal notranslate"><span class="pre">tcg_gen_exit_tb()</span></code> passing the address of the current TB and the jump slot index again.</li> </ol> <p>Step 1, <code class="docutils literal notranslate"><span class="pre">tcg_gen_goto_tb()</span></code>, will emit a <code class="docutils literal notranslate"><span class="pre">goto_tb</span></code> TCG instruction that later on gets translated to a jump to an address associated with the specified jump slot. Initially, this is the address of step 2’s instructions, which update the CPU state information. Step 3, <code class="docutils literal notranslate"><span class="pre">tcg_gen_exit_tb()</span></code>, exits from the current TB returning a tagged pointer composed of the last executed TB’s address and the jump slot index.</p> <p>The first time this whole sequence is executed, step 1 simply jumps to step 2. Then the CPU state information gets updated and we exit from the current TB. As a result, the behavior is very similar to the less optimized form described earlier in this section.</p> <p>Next, the main loop looks for the next TB to execute using the current CPU state information (creating the TB if it wasn’t already available) and, before starting to execute the new TB’s instructions, patches the previously executed TB by associating one of its jump slots (the one specified in the call to <code class="docutils literal notranslate"><span class="pre">tcg_gen_exit_tb()</span></code>) with the address of the new TB.</p> <p>The next time this previous TB is executed and we get to that same <code class="docutils literal notranslate"><span class="pre">goto_tb</span></code> step, it will already be patched (assuming the destination TB is still in memory) and will jump directly to the first instruction of the destination TB, without going back to the main loop.</p> <p>For the <code class="docutils literal notranslate"><span class="pre">goto_tb</span> <span class="pre">+</span> <span class="pre">exit_tb</span></code> mechanism to be used, the following conditions need to be satisfied:</p> <ul class="simple"> <li>The change in CPU state must be constant, e.g., a direct branch and not an indirect branch.</li> <li>The direct branch cannot cross a page boundary. Memory mappings may change, causing the code at the destination address to change.</li> </ul> <p>Note that, on step 3 (<code class="docutils literal notranslate"><span class="pre">tcg_gen_exit_tb()</span></code>), in addition to the jump slot index, the address of the TB just executed is also returned. This address corresponds to the TB that will be patched; it may be different than the one that was directly executed from the main loop if the latter had already been chained to other TBs.</p> </div> </div> <div class="section" id="self-modifying-code-and-translated-code-invalidation"> <h2>Self-modifying code and translated code invalidation<a class="headerlink" href="#self-modifying-code-and-translated-code-invalidation" title="Permalink to this headline">¶</a></h2> <p>Self-modifying code is a special challenge in x86 emulation because no instruction cache invalidation is signaled by the application when code is modified.</p> <p>User-mode emulation marks a host page as write-protected (if it is not already read-only) every time translated code is generated for a basic block. Then, if a write access is done to the page, Linux raises a SEGV signal. QEMU then invalidates all the translated code in the page and enables write accesses to the page. For system emulation, write protection is achieved through the software MMU.</p> <p>Correct translated code invalidation is done efficiently by maintaining a linked list of every translated block contained in a given page. Other linked lists are also maintained to undo direct block chaining.</p> <p>On RISC targets, correctly written software uses memory barriers and cache flushes, so some of the protection above would not be necessary. However, QEMU still requires that the generated code always matches the target instructions in memory in order to handle exceptions correctly.</p> </div> <div class="section" id="exception-support"> <h2>Exception support<a class="headerlink" href="#exception-support" title="Permalink to this headline">¶</a></h2> <p>longjmp() is used when an exception such as division by zero is encountered.</p> <p>The host SIGSEGV and SIGBUS signal handlers are used to get invalid memory accesses. QEMU keeps a map from host program counter to target program counter, and looks up where the exception happened based on the host program counter at the exception point.</p> <p>On some targets, some bits of the virtual CPU’s state are not flushed to the memory until the end of the translation block. This is done for internal emulation state that is rarely accessed directly by the program and/or changes very often throughout the execution of a translation block—this includes condition codes on x86, delay slots on SPARC, conditional execution on Arm, and so on. This state is stored for each target instruction, and looked up on exceptions.</p> </div> <div class="section" id="mmu-emulation"> <h2>MMU emulation<a class="headerlink" href="#mmu-emulation" title="Permalink to this headline">¶</a></h2> <p>For system emulation QEMU uses a software MMU. In that mode, the MMU virtual to physical address translation is done at every memory access.</p> <p>QEMU uses an address translation cache (TLB) to speed up the translation. In order to avoid flushing the translated code each time the MMU mappings change, all caches in QEMU are physically indexed. This means that each basic block is indexed with its physical address.</p> <p>In order to avoid invalidating the basic block chain when MMU mappings change, chaining is only performed when the destination of the jump shares a page with the basic block that is performing the jump.</p> <p>The MMU can also distinguish RAM and ROM memory areas from MMIO memory areas. Access is faster for RAM and ROM because the translation cache also hosts the offset between guest address and host memory. Accessing MMIO memory areas instead calls out to C code for device emulation. Finally, the MMU helps tracking dirty pages and pages pointed to by translation blocks.</p> </div> </div> </div> </div> <footer> <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> <a href="tcg-icount.html" class="btn btn-neutral float-right" title="TCG Instruction Counting" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> <a href="secure-coding-practices.html" class="btn btn-neutral" title="Secure Coding Practices" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a> </div> <hr/> <div role="contentinfo"> <p> © Copyright 2021, The QEMU Project Developers. </p> </div> Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. <!-- Empty para to force a blank line after "Built with Sphinx ..." --> <p></p> <p>This documentation is for QEMU version 6.2.0.</p> <p><a href="../about/license.html">QEMU and this manual are released under the GNU General Public License, version 2.</a></p> </footer> </div> </div> </section> </div> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT:'../', VERSION:'qemu-kvm-6.2.0-53.module+el8.10.0+2055+8eb7870b.4', LANGUAGE:'None', COLLAPSE_INDEX:false, FILE_SUFFIX:'.html', HAS_SOURCE: false, SOURCELINK_SUFFIX: '.txt' }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/js/theme.js"></script> <script type="text/javascript"> jQuery(function () { SphinxRtdTheme.Navigation.enable(true); }); </script> </body> </html>
Simpan