Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions accel/tcg/cpu-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ static inline TranslationBlock *tb_lookup(CPUState *cpu, vaddr pc,

tb = qatomic_read(&jc->array[hash].tb);
if (likely(tb &&
jc->array[hash].gen == jc->gen &&
jc->array[hash].pc == pc &&
tb->cs_base == cs_base &&
tb->flags == flags &&
Expand All @@ -260,6 +261,7 @@ static inline TranslationBlock *tb_lookup(CPUState *cpu, vaddr pc,
}

jc->array[hash].pc = pc;
jc->array[hash].gen = jc->gen;
qatomic_set(&jc->array[hash].tb, tb);

hit:
Expand Down Expand Up @@ -995,6 +997,7 @@ cpu_exec_loop(CPUState *cpu, SyncClocks *sc)
h = tb_jmp_cache_hash_func(pc);
jc = cpu->tb_jmp_cache;
jc->array[h].pc = pc;
jc->array[h].gen = jc->gen;
qatomic_set(&jc->array[h].tb, tb);
}

Expand Down
26 changes: 25 additions & 1 deletion accel/tcg/cputlb.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,24 @@ void tlb_flush_page_by_mmuidx(CPUState *cpu, vaddr addr, uint16_t idxmap)
tlb_flush_page_by_mmuidx_async_0(cpu, addr, idxmap);
}

void tlb_flush_data_pages_by_mmuidx(CPUState *cpu, vaddr addr, vaddr npages,
uint16_t idxmap)
{
assert_cpu_is_self(cpu);
addr &= TARGET_PAGE_MASK;

qemu_spin_lock(&cpu->neg.tlb.c.lock);
for (vaddr i = 0; i < npages; i++) {
for (int mmu_idx = 0; mmu_idx < NB_MMU_MODES; mmu_idx++) {
if ((idxmap >> mmu_idx) & 1) {
tlb_flush_page_locked(cpu, mmu_idx,
addr + i * TARGET_PAGE_SIZE);
}
}
}
qemu_spin_unlock(&cpu->neg.tlb.c.lock);
}

void tlb_flush_page(CPUState *cpu, vaddr addr)
{
tlb_flush_page_by_mmuidx(cpu, addr, ALL_MMUIDX_BITS);
Expand Down Expand Up @@ -736,7 +754,13 @@ static void tlb_flush_range_by_mmuidx_async_0(CPUState *cpu,
* If the length is larger than the jump cache size, then it will take
* longer to clear each entry individually than it will to clear it all.
*/
if (d.len >= (TARGET_PAGE_SIZE * TB_JMP_CACHE_SIZE)) {
/*
* A page's jump cache bucket is its page number bits [0, P) xor bits
* [P, 2P), P = TB_JMP_PAGE_BITS. Any run of 2 << P pages holds an
* aligned run of 1 << P pages, which covers every bucket: clearing it
* page by page would empty the whole cache, only more slowly.
*/
if (d.len >= (TARGET_PAGE_SIZE * 2 * (TB_JMP_CACHE_SIZE / TB_JMP_PAGE_SIZE))) {
tcg_flush_jmp_cache(cpu);
return;
}
Expand Down
17 changes: 16 additions & 1 deletion accel/tcg/tb-jmp-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
#include "qemu/rcu.h"
#include "exec/cpu-common.h"

#define TB_JMP_CACHE_BITS 12
/*
* 4096 entries (the upstream default) thrash badly under a Mac OS X guest:
* a game's hot code covers 100k+ translation blocks, so most indirect
* branches -- and PowerPC returns through blr are indirect -- miss the
* cache and fall back to the (much slower) hash table. 64K entries cost
* 1 MB per vCPU and turn most of those misses into hits.
*/
#define TB_JMP_CACHE_BITS 16
#define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)

/*
Expand All @@ -24,9 +31,17 @@
*/
typedef struct CPUJumpCache {
struct rcu_head rcu;
/*
* Emptying the cache means bumping this counter: an entry counts only
* while its own gen matches. Clearing 64K entries outright on every
* TLB flush would cost far more than the cache saves, and a 32-bit
* hash MMU guest flushes constantly.
*/
uint32_t gen;
struct {
TranslationBlock *tb;
vaddr pc;
uint32_t gen;
} array[TB_JMP_CACHE_SIZE];
} CPUJumpCache;

Expand Down
9 changes: 9 additions & 0 deletions accel/tcg/tcg-accel-ops-mttcg.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/

#include "qemu/osdep.h"
#ifdef __APPLE__
#include <pthread/qos.h>
#endif
#include "system/tcg.h"
#include "system/replay.h"
#include "system/cpu-timers.h"
Expand Down Expand Up @@ -70,6 +73,12 @@ static void *mttcg_cpu_thread_fn(void *arg)
g_assert(!icount_enabled());

rcu_register_thread();
#ifdef __APPLE__
/* The vCPU thread is the emulator's critical path: keep it on the
* performance cores instead of letting macOS park it on efficiency
* cores whenever the host is busy or the window is in the background. */
pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);
#endif
force_rcu.notifier.notify = mttcg_force_rcu;
force_rcu.cpu = cpu;
rcu_add_force_rcu_notifier(&force_rcu.notifier);
Expand Down
9 changes: 9 additions & 0 deletions accel/tcg/tcg-accel-ops-rr.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/

#include "qemu/osdep.h"
#ifdef __APPLE__
#include <pthread/qos.h>
#endif
#include "qemu/lockable.h"
#include "system/tcg.h"
#include "system/replay.h"
Expand Down Expand Up @@ -184,6 +187,12 @@ static void *rr_cpu_thread_fn(void *arg)

assert(tcg_enabled());
rcu_register_thread();
#ifdef __APPLE__
/* The vCPU thread is the emulator's critical path: keep it on the
* performance cores instead of letting macOS park it on efficiency
* cores whenever the host is busy or the window is in the background. */
pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);
#endif
force_rcu.notify = rr_force_rcu;
rcu_add_force_rcu_notifier(&force_rcu);
tcg_register_thread();
Expand Down
13 changes: 11 additions & 2 deletions accel/tcg/translate-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,16 @@ void tcg_flush_jmp_cache(CPUState *cpu)
return;
}

for (int i = 0; i < TB_JMP_CACHE_SIZE; i++) {
qatomic_set(&jc->array[i].tb, NULL);
/*
* Retire every entry at once. Only on the (4-billionth) wrap does the
* cache have to be walked, so that generation 0 cannot match entries
* left behind by an earlier lap of the counter.
*/
if (++jc->gen == 0) {
for (int i = 0; i < TB_JMP_CACHE_SIZE; i++) {
qatomic_set(&jc->array[i].tb, NULL);
jc->array[i].gen = 0;
}
jc->gen = 1;
}
}
89 changes: 89 additions & 0 deletions accel/tcg/translator.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "internal-target.h"
#include "disas/disas.h"
#include "tb-internal.h"
#include "tb-hash.h"
#include "tb-jmp-cache.h"

static void set_can_do_io(DisasContextBase *db, bool val)
{
Expand Down Expand Up @@ -120,6 +122,93 @@ bool translator_use_goto_tb(DisasContextBase *db, vaddr dest)
return translator_is_same_page(db, dest);
}

/*
* helper_lookup_tb_ptr's jump cache probe, inline: hash @pc, and if the
* entry holds a live TB for it with this TB's cs_base, flags and cflags,
* jump straight to it; otherwise call the helper as before. Returns and
* other indirect branches take this path on every execution, and the
* helper call costs far more than the probe.
*
* Only for exits where cpu_get_tb_cpu_state() still returns this TB's
* cs_base and flags, and only for TBs with ordinary cflags, which is what
* curr_cflags() returns while such TBs run: anything unusual (icount,
* single-stepping, breakpoints on the page, plugins, exec logging) keeps
* the helper, which handles it.
*/
/* QEMU_TB_INLINE=0 always calls helper_lookup_tb_ptr (for A/B tests). */
static bool tb_inline_lookup_on(void)
{
static int on = -1;

if (on < 0) {
const char *e = getenv("QEMU_TB_INLINE");
on = !(e && e[0] == '0');
}
return on;
}

void translator_lookup_and_goto_ptr(DisasContextBase *db, TCGv_i64 pc)
{
TranslationBlock *tb = db->tb;
uint32_t cf = tb_cflags(tb);
const size_t esz = sizeof(((CPUJumpCache *)0)->array[0]);
TCGLabel *miss;
TCGv_i64 h, t;
TCGv_ptr jc, e, next;
TCGv_i32 a, b;

if ((cf & (CF_COUNT_MASK | CF_NO_GOTO_TB | CF_NO_GOTO_PTR | CF_SINGLE_STEP |
CF_MEMI_ONLY | CF_USE_ICOUNT | CF_NOIRQ | CF_PCREL |
CF_BP_PAGE)) ||
db->plugin_enabled || qemu_loglevel_mask(CPU_LOG_TB_CPU | CPU_LOG_EXEC) ||
!tb_inline_lookup_on()) {
tcg_gen_lookup_and_goto_ptr();
return;
}

miss = gen_new_label();
h = tcg_temp_new_i64();
t = tcg_temp_new_i64();
jc = tcg_temp_new_ptr();
e = tcg_temp_new_ptr();
next = tcg_temp_new_ptr();
a = tcg_temp_new_i32();
b = tcg_temp_new_i32();

/* tb_jmp_cache_hash_func(pc) * esz */
tcg_gen_shri_i64(t, pc, TARGET_PAGE_BITS - TB_JMP_PAGE_BITS);
tcg_gen_xor_i64(t, t, pc);
tcg_gen_shri_i64(h, t, TARGET_PAGE_BITS - TB_JMP_PAGE_BITS);
tcg_gen_andi_i64(h, h, TB_JMP_PAGE_MASK);
tcg_gen_andi_i64(t, t, TB_JMP_ADDR_MASK);
tcg_gen_or_i64(h, h, t);
tcg_gen_muli_i64(h, h, esz);

tcg_gen_ld_ptr(jc, tcg_env, offsetof(ArchCPU, parent_obj.tb_jmp_cache) -
offsetof(ArchCPU, env));
tcg_gen_trunc_i64_ptr(e, h);
tcg_gen_add_ptr(e, e, jc);

tcg_gen_ld_ptr(next, e, offsetof(CPUJumpCache, array[0].tb));
tcg_gen_brcondi_ptr(TCG_COND_EQ, next, 0, miss);
tcg_gen_ld_i64(t, e, offsetof(CPUJumpCache, array[0].pc));
tcg_gen_brcond_i64(TCG_COND_NE, t, pc, miss);
tcg_gen_ld_i32(a, e, offsetof(CPUJumpCache, array[0].gen));
tcg_gen_ld_i32(b, jc, offsetof(CPUJumpCache, gen));
tcg_gen_brcond_i32(TCG_COND_NE, a, b, miss);
tcg_gen_ld_i64(t, next, offsetof(TranslationBlock, cs_base));
tcg_gen_brcondi_i64(TCG_COND_NE, t, tb->cs_base, miss);
tcg_gen_ld_i32(a, next, offsetof(TranslationBlock, flags));
tcg_gen_brcondi_i32(TCG_COND_NE, a, tb->flags, miss);
tcg_gen_ld_i32(a, next, offsetof(TranslationBlock, cflags));
tcg_gen_brcondi_i32(TCG_COND_NE, a, cf, miss);
tcg_gen_ld_ptr(next, next, offsetof(TranslationBlock, tc.ptr));
tcg_gen_goto_ptr(next);

gen_set_label(miss);
tcg_gen_lookup_and_goto_ptr();
}

void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
vaddr pc, void *host_pc, const TranslatorOps *ops,
DisasContextBase *db)
Expand Down
18 changes: 18 additions & 0 deletions include/exec/cputlb.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ void tlb_flush_all_cpus_synced(CPUState *src_cpu);
void tlb_flush_page_by_mmuidx(CPUState *cpu, vaddr addr,
uint16_t idxmap);

/**
* tlb_flush_data_pages_by_mmuidx:
* @cpu: CPU whose TLB should be flushed
* @addr: virtual address of the first page
* @npages: number of consecutive pages
* @idxmap: bitmap of MMU indexes to flush
*
* Like tlb_flush_page_by_mmuidx on each page, for a mapping change that
* cannot affect instruction fetch (a data-only translation): the TB jump
* cache is left alone, and the TLB lock is taken once.
*/
void tlb_flush_data_pages_by_mmuidx(CPUState *cpu, vaddr addr, vaddr npages,
uint16_t idxmap);

/**
* tlb_flush_page_by_mmuidx_all_cpus_synced:
* @cpu: Originating CPU of the flush
Expand Down Expand Up @@ -246,6 +260,10 @@ static inline void tlb_flush_page_by_mmuidx(CPUState *cpu,
vaddr addr, uint16_t idxmap)
{
}
static inline void tlb_flush_data_pages_by_mmuidx(CPUState *cpu, vaddr addr,
vaddr npages, uint16_t idxmap)
{
}

static inline void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)
{
Expand Down
12 changes: 12 additions & 0 deletions include/exec/translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
*/
bool translator_use_goto_tb(DisasContextBase *db, vaddr dest);

/**
* translator_lookup_and_goto_ptr
* @db: Disassembly context
* @pc: the next pc, as the CPU state already holds it
*
* Like tcg_gen_lookup_and_goto_ptr(), but probes the jump cache inline
* first. Only valid where the CPU's cs_base and flags are still this
* TB's (see the definition).
*/
struct TCGv_i64_d;
void translator_lookup_and_goto_ptr(DisasContextBase *db, struct TCGv_i64_d *pc);

/**
* translator_io_start
* @db: Disassembly context
Expand Down
18 changes: 16 additions & 2 deletions include/tcg/tcg-apple-jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,31 @@ static __attribute__((__always_inline__)) void jit_write_protect(int enabled)

#endif

/*
* Switching the JIT mapping between writable and executable costs an
* "isb sy" -- a full pipeline resynchronisation. qemu_thread_jit_execute()
* runs before every translated block is entered, while writes happen only
* when code is generated or patched, so remember which state this thread is
* already in and skip the switch (and the barrier) when nothing changes.
* The APRR state is per-thread, so the cache is too -- and it must be a
* single variable shared by every translation unit, or one file's idea of
* the state would let another skip a switch it actually needed.
*/
extern __thread int qemu_jit_wx_state;

static inline void qemu_thread_jit_execute(void)
{
if (jit_write_protect_supported()) {
if (qemu_jit_wx_state != 1 && jit_write_protect_supported()) {
jit_write_protect(true);
qemu_jit_wx_state = 1;
}
}

static inline void qemu_thread_jit_write(void)
{
if (jit_write_protect_supported()) {
if (qemu_jit_wx_state != 0 && jit_write_protect_supported()) {
jit_write_protect(false);
qemu_jit_wx_state = 0;
}
}

Expand Down
8 changes: 8 additions & 0 deletions include/tcg/tcg-op-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void tcg_gen_goto_tb(unsigned idx);
* this op is equivalent to calling tcg_gen_exit_tb() with 0 as the argument.
*/
void tcg_gen_lookup_and_goto_ptr(void);
/* Jump to TB code at ptr, found by the caller; the TB must allow goto_ptr. */
void tcg_gen_goto_ptr(TCGv_ptr ptr);

void tcg_gen_plugin_cb(unsigned from);
void tcg_gen_plugin_mem_cb(TCGv_i64 addr, unsigned meminfo);
Expand Down Expand Up @@ -458,6 +460,12 @@ void tcg_gen_cmp_vec(TCGCond cond, unsigned vece, TCGv_vec r,

void tcg_gen_bitsel_vec(unsigned vece, TCGv_vec r, TCGv_vec a,
TCGv_vec b, TCGv_vec c);
void tcg_gen_tbl_vec(TCGv_vec r, TCGv_vec a, TCGv_vec b, TCGv_vec idx);
void tcg_gen_fop_vec(unsigned vece, unsigned op, TCGv_vec r,
TCGv_vec a, TCGv_vec b);
void tcg_gen_ffma_vec(unsigned vece, unsigned kind, TCGv_vec r,
TCGv_vec a, TCGv_vec c, TCGv_vec b);
void tcg_gen_fcvt_vec(bool to_double, TCGv_vec r, TCGv_vec a);
void tcg_gen_cmpsel_vec(TCGCond cond, unsigned vece, TCGv_vec r,
TCGv_vec a, TCGv_vec b, TCGv_vec c, TCGv_vec d);

Expand Down
14 changes: 14 additions & 0 deletions include/tcg/tcg-opc.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ DEF(cmp_vec, 1, 2, 1, TCG_OPF_VECTOR)

DEF(bitsel_vec, 1, 3, 0, TCG_OPF_VECTOR)
DEF(cmpsel_vec, 1, 4, 1, TCG_OPF_VECTOR)
/* r[i] = idx[i] < 16 ? a[idx[i]] : idx[i] < 32 ? b[idx[i] - 16] : 0 */
DEF(tbl_vec, 1, 3, 0, TCG_OPF_VECTOR)
/*
* Scalar IEEE binary floating point in the low element of a V64: a double
* for vece MO_64, a single for MO_32. Round to nearest even, no
* exception flags; results for NaN operands are the host's.
* fop_vec r = a op b, constant op: 0 add, 1 sub, 2 mul, 3 div.
* ffma_vec r = a*c + b with one rounding, constant: 0 a*c+b, 1 a*c-b,
* 2 -(a*c+b), 3 -(a*c-b) (the negated forms negate the rounded result).
* fcvt_vec: constant 0 double -> single (rounding), 1 single -> double.
*/
DEF(fop_vec, 1, 2, 1, TCG_OPF_VECTOR)
DEF(ffma_vec, 1, 3, 1, TCG_OPF_VECTOR)
DEF(fcvt_vec, 1, 1, 1, TCG_OPF_VECTOR)

DEF(last_generic, 0, 0, 0, TCG_OPF_NOT_PRESENT)

Expand Down
Loading
Loading