Skip to content
Snippets Groups Projects
  1. Mar 15, 2018
  2. Feb 08, 2018
  3. Jan 16, 2018
  4. Dec 29, 2017
  5. Nov 03, 2017
  6. Oct 24, 2017
    • Richard Henderson's avatar
      tcg: Initialize cpu_env generically · 1c2adb95
      Richard Henderson authored
      
      This is identical for each target.  So, move the initialization to
      common code.  Move the variable itself out of tcg_ctx and name it
      cpu_env to minimize changes within targets.
      
      This also means we can remove tcg_global_reg_new_{ptr,i32,i64},
      since there are no longer global-register temps created by targets.
      
      Reviewed-by: default avatarEmilio G. Cota <cota@braap.org>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <f4bug@amsat.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      1c2adb95
    • Emilio G. Cota's avatar
      tcg: enable multiple TCG contexts in softmmu · 3468b59e
      Emilio G. Cota authored
      
      This enables parallel TCG code generation. However, we do not take
      advantage of it yet since tb_lock is still held during tb_gen_code.
      
      In user-mode we use a single TCG context; see the documentation
      added to tcg_region_init for the rationale.
      
      Note that targets do not need any conversion: targets initialize a
      TCGContext (e.g. defining TCG globals), and after this initialization
      has finished, the context is cloned by the vCPU threads, each of
      them keeping a separate copy.
      
      TCG threads claim one entry in tcg_ctxs[] by atomically increasing
      n_tcg_ctxs. Do not be too annoyed by the subsequent atomic_read's
      of that variable and tcg_ctxs; they are there just to play nice with
      analysis tools such as thread sanitizer.
      
      Note that we do not allocate an array of contexts (we allocate
      an array of pointers instead) because when tcg_context_init
      is called, we do not know yet how many contexts we'll use since
      the bool behind qemu_tcg_mttcg_enabled() isn't set yet.
      
      Previous patches folded some TCG globals into TCGContext. The non-const
      globals remaining are only set at init time, i.e. before the TCG
      threads are spawned. Here is a list of these set-at-init-time globals
      under tcg/:
      
      Only written by tcg_context_init:
      - indirect_reg_alloc_order
      - tcg_op_defs
      Only written by tcg_target_init (called from tcg_context_init):
      - tcg_target_available_regs
      - tcg_target_call_clobber_regs
      - arm: arm_arch, use_idiv_instructions
      - i386: have_cmov, have_bmi1, have_bmi2, have_lzcnt,
              have_movbe, have_popcnt
      - mips: use_movnz_instructions, use_mips32_instructions,
              use_mips32r2_instructions, got_sigill (tcg_target_detect_isa)
      - ppc: have_isa_2_06, have_isa_3_00, tb_ret_addr
      - s390: tb_ret_addr, s390_facilities
      - sparc: qemu_ld_trampoline, qemu_st_trampoline (build_trampolines),
               use_vis3_instructions
      
      Only written by tcg_prologue_init:
      - 'struct jit_code_entry one_entry'
      - aarch64: tb_ret_addr
      - arm: tb_ret_addr
      - i386: tb_ret_addr, guest_base_flags
      - ia64: tb_ret_addr
      - mips: tb_ret_addr, bswap32_addr, bswap32u_addr, bswap64_addr
      
      Reviewed-by: default avatarRichard Henderson <rth@twiddle.net>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      3468b59e
    • Emilio G. Cota's avatar
      tcg: introduce regions to split code_gen_buffer · e8feb96f
      Emilio G. Cota authored
      
      This is groundwork for supporting multiple TCG contexts.
      
      The naive solution here is to split code_gen_buffer statically
      among the TCG threads; this however results in poor utilization
      if translation needs are different across TCG threads.
      
      What we do here is to add an extra layer of indirection, assigning
      regions that act just like pages do in virtual memory allocation.
      (BTW if you are wondering about the chosen naming, I did not want
      to use blocks or pages because those are already heavily used in QEMU).
      
      We use a global lock to serialize allocations as well as statistics
      reporting (we now export the size of the used code_gen_buffer with
      tcg_code_size()). Note that for the allocator we could just use
      a counter and atomic_inc; however, that would complicate the gathering
      of tcg_code_size()-like stats. So given that the region operations are
      not a fast path, a lock seems the most reasonable choice.
      
      The effectiveness of this approach is clear after seeing some numbers.
      I used the bootup+shutdown of debian-arm with '-tb-size 80' as a benchmark.
      Note that I'm evaluating this after enabling per-thread TCG (which
      is done by a subsequent commit).
      
      * -smp 1, 1 region (entire buffer):
          qemu: flush code_size=83885014 nb_tbs=154739 avg_tb_size=357
          qemu: flush code_size=83884902 nb_tbs=153136 avg_tb_size=363
          qemu: flush code_size=83885014 nb_tbs=152777 avg_tb_size=364
          qemu: flush code_size=83884950 nb_tbs=150057 avg_tb_size=373
          qemu: flush code_size=83884998 nb_tbs=150234 avg_tb_size=373
          qemu: flush code_size=83885014 nb_tbs=154009 avg_tb_size=360
          qemu: flush code_size=83885014 nb_tbs=151007 avg_tb_size=370
          qemu: flush code_size=83885014 nb_tbs=151816 avg_tb_size=367
      
      That is, 8 flushes.
      
      * -smp 8, 32 regions (80/32 MB per region) [i.e. this patch]:
      
          qemu: flush code_size=76328008 nb_tbs=141040 avg_tb_size=356
          qemu: flush code_size=75366534 nb_tbs=138000 avg_tb_size=361
          qemu: flush code_size=76864546 nb_tbs=140653 avg_tb_size=361
          qemu: flush code_size=76309084 nb_tbs=135945 avg_tb_size=375
          qemu: flush code_size=74581856 nb_tbs=132909 avg_tb_size=375
          qemu: flush code_size=73927256 nb_tbs=135616 avg_tb_size=360
          qemu: flush code_size=78629426 nb_tbs=142896 avg_tb_size=365
          qemu: flush code_size=76667052 nb_tbs=138508 avg_tb_size=368
      
      Again, 8 flushes. Note how buffer utilization is not 100%, but it
      is close. Smaller region sizes would yield higher utilization,
      but we want region allocation to be rare (it acquires a lock), so
      we do not want to go too small.
      
      * -smp 8, static partitioning of 8 regions (10 MB per region):
          qemu: flush code_size=21936504 nb_tbs=40570 avg_tb_size=354
          qemu: flush code_size=11472174 nb_tbs=20633 avg_tb_size=370
          qemu: flush code_size=11603976 nb_tbs=21059 avg_tb_size=365
          qemu: flush code_size=23254872 nb_tbs=41243 avg_tb_size=377
          qemu: flush code_size=28289496 nb_tbs=52057 avg_tb_size=358
          qemu: flush code_size=43605160 nb_tbs=78896 avg_tb_size=367
          qemu: flush code_size=45166552 nb_tbs=82158 avg_tb_size=364
          qemu: flush code_size=63289640 nb_tbs=116494 avg_tb_size=358
          qemu: flush code_size=51389960 nb_tbs=93937 avg_tb_size=362
          qemu: flush code_size=59665928 nb_tbs=107063 avg_tb_size=372
          qemu: flush code_size=38380824 nb_tbs=68597 avg_tb_size=374
          qemu: flush code_size=44884568 nb_tbs=79901 avg_tb_size=376
          qemu: flush code_size=50782632 nb_tbs=90681 avg_tb_size=374
          qemu: flush code_size=39848888 nb_tbs=71433 avg_tb_size=372
          qemu: flush code_size=64708840 nb_tbs=119052 avg_tb_size=359
          qemu: flush code_size=49830008 nb_tbs=90992 avg_tb_size=362
          qemu: flush code_size=68372408 nb_tbs=123442 avg_tb_size=368
          qemu: flush code_size=33555560 nb_tbs=59514 avg_tb_size=378
          qemu: flush code_size=44748344 nb_tbs=80974 avg_tb_size=367
          qemu: flush code_size=37104248 nb_tbs=67609 avg_tb_size=364
      
      That is, 20 flushes. Note how a static partitioning approach uses
      the code buffer poorly, leading to many unnecessary flushes.
      
      Reviewed-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      e8feb96f
    • Emilio G. Cota's avatar
      tcg: allocate optimizer temps with tcg_malloc · 34184b07
      Emilio G. Cota authored
      
      Groundwork for supporting multiple TCG contexts.
      
      While at it, also allocate temps_used directly as a bitmap of the
      required size, instead of using a bitmap of TCG_MAX_TEMPS via
      TCGTempSet.
      
      Performance-wise we lose about 1.12% in a translation-heavy workload
      such as booting+shutting down debian-arm:
      
      Performance counter stats for 'taskset -c 0 arm-softmmu/qemu-system-arm \
      	-machine type=virt -nographic -smp 1 -m 4096 \
      	-netdev user,id=unet,hostfwd=tcp::2222-:22 \
      	-device virtio-net-device,netdev=unet \
      	-drive file=die-on-boot.qcow2,id=myblock,index=0,if=none \
      	-device virtio-blk-device,drive=myblock \
      	-kernel kernel.img -append console=ttyAMA0 root=/dev/vda1 \
      	-name arm,debug-threads=on -smp 1' (10 runs):
      
                   exec time (s)  Relative slowdown wrt original (%)
      ---------------------------------------------------------------
       original     20.213321616                                  0.
       tcg_malloc   20.441130078                           1.1270214
       TCGContext   20.477846517                           1.3086662
       g_malloc     20.780527895                           2.8061013
      
      The other two alternatives shown in the table are:
      - TCGContext: embed temps[TCG_MAX_TEMPS] and TCGTempSet used_temps
        in TCGContext. This is simple enough but it isn't faster than using
        tcg_malloc; moreover, it wastes memory.
      - g_malloc: allocate/deallocate both temps and used_temps every time
        tcg_optimize is executed.
      
      Suggested-by: default avatarRichard Henderson <rth@twiddle.net>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      34184b07
    • Emilio G. Cota's avatar
      tcg: distribute profiling counters across TCGContext's · c3fac113
      Emilio G. Cota authored
      
      This is groundwork for supporting multiple TCG contexts.
      
      To avoid scalability issues when profiling info is enabled, this patch
      makes the profiling info counters distributed via the following changes:
      
      1) Consolidate profile info into its own struct, TCGProfile, which
         TCGContext also includes. Note that tcg_table_op_count is brought
         into TCGProfile after dropping the tcg_ prefix.
      2) Iterate over the TCG contexts in the system to obtain the total counts.
      
      This change also requires updating the accessors to TCGProfile fields to
      use atomic_read/set whenever there may be conflicting accesses (as defined
      in C11) to them.
      
      Reviewed-by: default avatarRichard Henderson <rth@twiddle.net>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      c3fac113
    • Emilio G. Cota's avatar
      tcg: introduce **tcg_ctxs to keep track of all TCGContext's · df2cce29
      Emilio G. Cota authored
      
      Groundwork for supporting multiple TCG contexts.
      
      Note that having n_tcg_ctxs is unnecessary. However, it is
      convenient to have it, since it will simplify iterating over the
      array: we'll have just a for loop instead of having to iterate
      over a NULL-terminated array (which would require n+1 elems)
      or having to check with ifdef's for usermode/softmmu.
      
      Reviewed-by: default avatarRichard Henderson <rth@twiddle.net>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      df2cce29
    • Emilio G. Cota's avatar
      gen-icount: fold exitreq_label into TCGContext · 26689780
      Emilio G. Cota authored
      
      Groundwork for supporting multiple TCG contexts.
      
      Reviewed-by: default avatarRichard Henderson <rth@twiddle.net>
      Reviewed-by: default avatarAlex Bennée <alex.bennee@linaro.org>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      26689780
    • Emilio G. Cota's avatar
      tcg: define tcg_init_ctx and make tcg_ctx a pointer · b1311c4a
      Emilio G. Cota authored
      
      Groundwork for supporting multiple TCG contexts.
      
      The core of this patch is this change to tcg/tcg.h:
      
      > -extern TCGContext tcg_ctx;
      > +extern TCGContext tcg_init_ctx;
      > +extern TCGContext *tcg_ctx;
      
      Note that for now we set *tcg_ctx to whatever TCGContext is passed
      to tcg_context_init -- in this case &tcg_init_ctx.
      
      Reviewed-by: default avatarRichard Henderson <rth@twiddle.net>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      b1311c4a
    • Emilio G. Cota's avatar
      tcg: take tb_ctx out of TCGContext · 44ded3d0
      Emilio G. Cota authored
      
      Groundwork for supporting multiple TCG contexts.
      
      Reviewed-by: default avatarRichard Henderson <rth@twiddle.net>
      Reviewed-by: default avatarAlex Bennée <alex.bennee@linaro.org>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      44ded3d0
    • Emilio G. Cota's avatar
      tcg: check CF_PARALLEL instead of parallel_cpus · e82d5a24
      Emilio G. Cota authored
      
      Thereby decoupling the resulting translated code from the current state
      of the system.
      
      The tb->cflags field is not passed to tcg generation functions. So
      we add a field to TCGContext, storing there a copy of tb->cflags.
      
      Most architectures have <= 32 registers, which results in a 4-byte hole
      in TCGContext. Use this hole for the new field.
      
      Reviewed-by: default avatarRichard Henderson <rth@twiddle.net>
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      e82d5a24
    • Emilio G. Cota's avatar
      tcg: define CF_PARALLEL and use it for TB hashing along with CF_COUNT_MASK · 4e2ca83e
      Emilio G. Cota authored
      
      This will enable us to decouple code translation from the value
      of parallel_cpus at any given time. It will also help us minimize
      TB flushes when generating code via EXCP_ATOMIC.
      
      Note that the declaration of parallel_cpus is brought to exec-all.h
      to be able to define there the "curr_cflags" inline.
      
      Signed-off-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      4e2ca83e
    • Richard Henderson's avatar
      tcg: Use offsets not indices for TCGv_* · e89b28a6
      Richard Henderson authored
      
      Using the offset of a temporary, relative to TCGContext, rather than
      its index means that we don't use 0.  That leaves offset 0 free for
      a NULL representation without having to leave index 0 unused.
      
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <f4bug@amsat.org>
      Reviewed-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      e89b28a6
    • Richard Henderson's avatar
      tcg: Remove TCGV_EQUAL* · 11f4e8f8
      Richard Henderson authored
      
      When we used structures for TCGv_*, we needed a macro in order to
      perform a comparison.  Now that we use pointers, this is just clutter.
      
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <f4bug@amsat.org>
      Reviewed-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      11f4e8f8
    • Richard Henderson's avatar
      tcg: Remove GET_TCGV_* and MAKE_TCGV_* · dc41aa7d
      Richard Henderson authored
      
      The GET and MAKE functions weren't really specific enough.
      We now have a full complement of functions that convert exactly
      between temporaries, arguments, tcgv pointers, and indices.
      
      The target/sparc change is also a bug fix, which would have affected
      a host that defines TCG_TARGET_HAS_extr[lh]_i64_i32, i.e. MIPS64.
      
      Reviewed-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarRichard Henderson <richard.henderson@linaro.org>
      dc41aa7d
Loading