Skip to content
Draft
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
458 changes: 458 additions & 0 deletions accel/tcg/cpu-exec.c

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions accel/tcg/translate-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@
#include "ts.h"
#include "latx-smc.h"
#endif
#if defined(CONFIG_LATX_KZT)
extern uintptr_t KztPltResolverBridge(void);
extern uintptr_t kzt_lazy_target_bridge_pc;

static uint64_t kzt_lazy_bridge_tb_gen_timing_now(void)
{
struct timespec value;

if (clock_gettime(CLOCK_MONOTONIC_RAW, &value) != 0) {
return 0;
}
return (uint64_t)value.tv_sec * 1000000000ULL +
(uint64_t)value.tv_nsec;
}
#endif
#ifdef CONFIG_LATX_FAST_JMPCACHE
#include "exec/fasttb.h"
#endif
Expand Down Expand Up @@ -2016,6 +2031,32 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
int64_t ti;
#endif
void *host_pc;
#if defined(CONFIG_LATX_KZT)
const char *kzt_lazy_bridge_role = NULL;
uint64_t kzt_lazy_bridge_tb_gen_start = 0;
int kzt_lazy_diagnostics_enabled =
unlikely(option_kzt_lazy_diagnostics);
uintptr_t kzt_lazy_target_snapshot = kzt_lazy_diagnostics_enabled
? __atomic_load_n(
&kzt_lazy_target_bridge_pc, __ATOMIC_ACQUIRE)
: 0;
uintptr_t kzt_plt_resolver_bridge = kzt_lazy_diagnostics_enabled
? KztPltResolverBridge()
: 0;

if (kzt_lazy_diagnostics_enabled && kzt_plt_resolver_bridge &&
pc == kzt_plt_resolver_bridge) {
kzt_lazy_bridge_role = "resolver";
} else if (kzt_lazy_diagnostics_enabled &&
kzt_lazy_target_snapshot &&
pc == kzt_lazy_target_snapshot) {
kzt_lazy_bridge_role = "target";
}
if (kzt_lazy_bridge_role) {
kzt_lazy_bridge_tb_gen_start =
kzt_lazy_bridge_tb_gen_timing_now();
}
#endif

assert_memory_lock();
qemu_thread_jit_write();
Expand Down Expand Up @@ -2441,6 +2482,21 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
tcg_tb_remove(tb);
return existing_tb;
}
#if defined(CONFIG_LATX_KZT)
if (kzt_lazy_bridge_role) {
uint64_t timing_done = kzt_lazy_bridge_tb_gen_timing_now();

fprintf(
stderr,
"kzt_lazy_bridge_tb_gen_timing schema=1 "
"role=%s pc=0x%lx total_ns=%lu\n",
kzt_lazy_bridge_role, (unsigned long)pc,
(unsigned long)(
timing_done >= kzt_lazy_bridge_tb_gen_start
? timing_done - kzt_lazy_bridge_tb_gen_start
: 0));
}
#endif
return tb;
}

Expand Down
25 changes: 25 additions & 0 deletions include/hw/core/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,20 @@ struct CPUState {

/* Accessed in parallel; all accesses must be atomic */
TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
#ifdef CONFIG_LATX_KZT
struct {
uintptr_t pc;
uint32_t flags;
uint32_t cflags;
unsigned flush_generation;
TranslationBlock *tb;
} kzt_pinned_bridge_cache[4];
unsigned kzt_pinned_bridge_flush_generation;
struct {
uintptr_t pc;
unsigned flush_generation;
} kzt_prebind_prepared_guest[8];
#endif

struct GDBRegisterState *gdb_regs;
int gdb_num_regs;
Expand Down Expand Up @@ -459,6 +473,17 @@ static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
qatomic_set(&cpu->tb_jmp_cache[i], NULL);
}
#ifdef CONFIG_LATX_KZT
++cpu->kzt_pinned_bridge_flush_generation;
for (i = 0; i < ARRAY_SIZE(cpu->kzt_pinned_bridge_cache); i++) {
qatomic_set(&cpu->kzt_pinned_bridge_cache[i].tb, NULL);
cpu->kzt_pinned_bridge_cache[i].pc = 0;
}
for (i = 0; i < ARRAY_SIZE(cpu->kzt_prebind_prepared_guest); i++) {
cpu->kzt_prebind_prepared_guest[i].pc = 0;
cpu->kzt_prebind_prepared_guest[i].flush_generation = 0;
}
#endif
}

/**
Expand Down
9 changes: 9 additions & 0 deletions linux-user/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ extern void __gcov_dump(void);
#include "latx-perf.h"
#endif

#ifdef CONFIG_LATX_KZT
void kzt_tb_steady_diagnostics_report(void);
void kzt_lifecycle_diagnostics_report(void);
#endif

void preexit_cleanup(CPUArchState *env, int code)
{
#ifdef CONFIG_LATX_KZT
kzt_lifecycle_diagnostics_report();
kzt_tb_steady_diagnostics_report();
#endif
#ifdef CONFIG_LATX_PERF
latx_timer_stop(TIMER_PROCESS);
#endif
Expand Down
54 changes: 51 additions & 3 deletions linux-user/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ int box64_pagesize;
uintptr_t box64_load_addr = 0;
int dlsym_error = 0;
int kzt_call_log = 0;
int kzt_registry_diagnostics = 0;
int cycle_log = 0;
int allow_missing_libs = 1;
int box64_nogtk = 0;
Expand All @@ -89,8 +90,8 @@ int fix_64bit_inodes = 0;
int box64_nopulse = 0;
int box64_novulkan = 0;
char* libGL = NULL;
int kzt_init(char** argv, int argc,char** target_argv, int target_argc,
struct linux_binprm* bprm);
int kzt_init(CPUX86State *env, char** argv, int argc, char** target_argv,
int target_argc, struct linux_binprm* bprm);
void kzt_bridge_init(void);
int is_user_map = 0;
#endif
Expand Down Expand Up @@ -293,6 +294,11 @@ CPUArchState *cpu_copy(CPUArchState *env)

new_cpu->tcg_cflags = cpu->tcg_cflags;
memcpy(new_env, env, sizeof(CPUArchState));
#if defined(CONFIG_LATX_KZT) && defined(TARGET_X86_64)
memset(&new_env->kzt_guest_dlerror_state, 0,
sizeof(new_env->kzt_guest_dlerror_state));
new_env->kzt_guest_dlerror_state.dlerror_slow_required = 0;
#endif

/*
* NOTE: Current QEMU only has one and only one gdt_table ptr.
Expand Down Expand Up @@ -635,6 +641,33 @@ static void handle_arg_latx_kzt(const char *arg)
{
option_kzt = strtol(arg, NULL, 0);
}

static void handle_arg_latx_kzt_lazy_diagnostics(const char *arg)
{
option_kzt_lazy_diagnostics = strtol(arg, NULL, 0) > 0;
}

static void handle_arg_latx_kzt_registry_diagnostics(const char *arg)
{
kzt_registry_diagnostics = strtol(arg, NULL, 0);
}

static void handle_arg_latx_kzt_patch_spike(const char *arg)
{
option_kzt_patch_spike = strtol(arg, NULL, 0) > 0;
}

static void handle_arg_latx_kzt_patch_spike_write(const char *arg)
{
option_kzt_patch_spike_write = strtol(arg, NULL, 0) > 0;
}

static void handle_arg_latx_kzt_patch_spike_budget(const char *arg)
{
long value = strtol(arg, NULL, 0);

option_kzt_patch_spike_budget = value > 0 ? (unsigned long)value : 0;
}
#endif

static void handle_arg_latx_fputag(const char *arg)
Expand Down Expand Up @@ -818,6 +851,21 @@ static const struct qemu_argument arg_table[] = {
#if defined(CONFIG_LATX_KZT)
{"latx-kzt", "LATX_KZT", true, handle_arg_latx_kzt,
"", "enable kuzhitong"},
{"latx-kzt-lazy-diagnostics", "LATX_KZT_LAZY_DIAGNOSTICS",
true, handle_arg_latx_kzt_lazy_diagnostics,
"", "enable KZT lazy completion diagnostics"},
{"latx-kzt-registry-diagnostics", "LATX_KZT_REGISTRY_DIAGNOSTICS",
true, handle_arg_latx_kzt_registry_diagnostics,
"", "enable KZT guest registry diagnostics"},
{"latx-kzt-patch-spike", "LATX_KZT_PATCH_SPIKE",
true, handle_arg_latx_kzt_patch_spike,
"", "enable KZT controlled patch spike planning"},
{"latx-kzt-patch-spike-write", "LATX_KZT_PATCH_SPIKE_WRITE",
true, handle_arg_latx_kzt_patch_spike_write,
"", "enable KZT controlled patch spike writes"},
{"latx-kzt-patch-spike-budget", "LATX_KZT_PATCH_SPIKE_BUDGET",
true, handle_arg_latx_kzt_patch_spike_budget,
"", "set KZT controlled patch spike write budget"},
#endif
{"latx-fputag", "LATX_FPUTAG", true, handle_arg_latx_fputag,
"", "enable fputag"},
Expand Down Expand Up @@ -1514,7 +1562,7 @@ int main(int argc, char **argv, char **envp)
}
#endif
#if defined(CONFIG_LATX_KZT) && defined(TARGET_X86_64)
kzt_init(argv, argc, target_argv, target_argc, &bprm);
kzt_init(env, argv, argc, target_argv, target_argc, &bprm);
#endif
for (wrk = target_environ; *wrk; wrk++) {
g_free(*wrk);
Expand Down
11 changes: 11 additions & 0 deletions linux-user/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@
#include "ioctl/mpt3sas_ctl.h"

#include "qemu.h"
#if defined(CONFIG_LATX_FAST_JMPCACHE) && defined(CONFIG_LATX_KZT)
void kzt_tb_steady_diagnostics_snapshot_fast_cache(CPUState *cpu);
#endif
#include "signal-common.h"
#include "qemu/guest-random.h"
#include "qemu/selfmap.h"
Expand Down Expand Up @@ -11528,6 +11531,9 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
#ifdef CONFIG_LATX_FAST_JMPCACHE
{
CPUX86State *x86env = env;
#ifdef CONFIG_LATX_KZT
kzt_tb_steady_diagnostics_snapshot_fast_cache(cpu);
#endif
if (x86env->tb_jmp_cache_ptr) {
free(x86env->tb_jmp_cache_ptr);
}
Expand Down Expand Up @@ -13976,6 +13982,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
#ifdef __NR_exit_group
/* new thread calls */
case TARGET_NR_exit_group:
#ifdef CONFIG_LATX_FAST_JMPCACHE
#ifdef CONFIG_LATX_KZT
kzt_tb_steady_diagnostics_snapshot_fast_cache(cpu);
#endif
#endif
preexit_cleanup(cpu_env, arg1);
/* dump basic block here. TODO */
#ifdef CONFIG_LATX_AOT
Expand Down
9 changes: 9 additions & 0 deletions target/i386/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#include "sysemu/xen.h"
#include "sysemu/whpx.h"
#include "sev_i386.h"
#if defined(CONFIG_LATX_KZT) && defined(TARGET_X86_64)
#include "kzt_guest_dl_api.h"
#endif

#include "qemu/error-report.h"
#include "qemu/module.h"
Expand Down Expand Up @@ -6308,6 +6311,9 @@ static void x86_cpu_reset(DeviceState *dev)
#if defined(CONFIG_LATX) && !defined(CONFIG_LATX_FAST_JMPCACHE)
env->tb_jmp_cache_ptr = s->tb_jmp_cache;
#endif
#if defined(CONFIG_LATX_KZT) && defined(TARGET_X86_64)
env->kzt_guest_dlerror_state.dlerror_slow_required = 0;
#endif
}

#ifndef CONFIG_USER_ONLY
Expand Down Expand Up @@ -7011,6 +7017,9 @@ static void x86_cpu_unrealizefn(DeviceState *dev)
cpu->apic_state = NULL;
}

#if defined(CONFIG_LATX_KZT) && defined(TARGET_X86_64)
kzt_guest_dl_api_free_errors(&cpu->env.kzt_guest_dlerror_state);
#endif
xcc->parent_unrealize(dev);
}

Expand Down
11 changes: 11 additions & 0 deletions target/i386/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include "cpu-qom.h"
#include "exec/cpu-defs.h"
#include "qapi/qapi-types-common.h"
#ifdef CONFIG_LATX_KZT
#include "kzt_guest_dl_state.h"
#include "kzt_loader_callback_scope.h"
#endif

/* The x86 has a strong memory model with some store-after-load re-ordering */
#define TCG_GUEST_DEFAULT_MO (TCG_MO_ALL & ~TCG_MO_ST_LD)
Expand Down Expand Up @@ -1404,6 +1408,13 @@ typedef struct HVFX86LazyFlags {

typedef struct CPUX86State {
#ifdef CONFIG_LATX
#ifdef CONFIG_LATX_KZT
struct box64context_s *kzt_runtime_context;
kzt_guest_library_loader_scope_t kzt_guest_library_loader_scope;
#ifdef TARGET_X86_64
kzt_guest_dlerror_state_t kzt_guest_dlerror_state;
#endif
#endif
void* checksum_fail_tb;
void *ibtc_table_p;
uint64_t vregs[5];
Expand Down
Loading
Loading