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
43 changes: 43 additions & 0 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 Down Expand Up @@ -635,6 +636,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 +846,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
9 changes: 9 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_lazy_binding.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,11 @@ typedef struct HVFX86LazyFlags {

typedef struct CPUX86State {
#ifdef CONFIG_LATX
#ifdef CONFIG_LATX_KZT
kzt_lazy_binding_pending_t kzt_lazy_binding_pending;
uintptr_t kzt_lazy_original_return;
kzt_guest_library_loader_scope_t kzt_guest_library_loader_scope;
#endif
void* checksum_fail_tb;
void *ibtc_table_p;
uint64_t vregs[5];
Expand Down
69 changes: 69 additions & 0 deletions target/i386/latx/context/box64context.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@
#include "librarian.h"
#include "library.h"
#include "wrapper.h"
#ifdef CONFIG_LATX_KZT
#include "kzt_guest_registry.h"
#include "kzt_guest_registry_context.h"
#include "kzt_guest_library_binding.h"
#endif
#include <pthread.h>

box64context_t *NewBox64Context(int argc)
{
#ifdef CONFIG_LATX_KZT
kzt_patch_spike_config_t patch_spike_config;
#endif
// init and put default values
box64context_t *context = (box64context_t*)box_calloc(1, sizeof(box64context_t));

Expand All @@ -38,10 +46,56 @@ box64context_t *NewBox64Context(int argc)
context->argc = argc;
context->argv = (char**)box_calloc(context->argc+1, sizeof(char*));
pthread_mutex_init(&context->mutex_lock, NULL);
#ifdef CONFIG_LATX_KZT
/* Optional acceleration metadata is created on first KZT use so a
* context that never observes a guest link-map keeps the old footprint. */
pthread_mutex_init(&context->kzt_bridge_mutex, NULL);
(void)kzt_guest_library_access_init(&context->kzt_guest_library_access);
kzt_patch_spike_config_from_options(&patch_spike_config);
kzt_patch_spike_guard_init(&context->kzt_patch_spike_guard,
&patch_spike_config);
#endif

return context;
}

#ifdef CONFIG_LATX_KZT
kzt_guest_registry_t *KztGuestRegistryForContext(box64context_t *context)
{
if (!context) {
return NULL;
}
return kzt_guest_registry_context_get(
&context->kzt_guest_registry_context, &context->mutex_lock);
}

kzt_guest_library_bindings_t *KztGuestLibraryBindingsForContext(box64context_t *context)
{
return context ? context->kzt_guest_library_access.bindings : NULL;
}

int KztGuestLibraryLookupForContext(
box64context_t *context,
const kzt_guest_library_binding_key_t *key,
kzt_guest_library_handle_t *handle)
{
return context
? kzt_guest_library_access_lookup(
&context->kzt_guest_library_access, key, handle)
: -1;
}
#endif

kzt_patch_spike_guard_t *KztPatchSpikeGuardForContext(box64context_t *context)
{
#ifdef CONFIG_LATX_KZT
return context ? &context->kzt_patch_spike_guard : NULL;
#else
(void)context;
return NULL;
#endif
}

EXPORTDYN
void FreeBox64Context(box64context_t** context)
{
Expand All @@ -53,10 +107,25 @@ void FreeBox64Context(box64context_t** context)

box64context_t* ctx = *context; // local copy to do the cleanning

#ifdef CONFIG_LATX_KZT
/* FreeBox64Context is entered only after guest execution and loader
* callbacks have stopped. Close the context-owned lookup gate first and
* drain acquired handles, while keeping registry/binding storage alive
* for the librarian destruction pass below. */
kzt_guest_library_access_begin_teardown(
&ctx->kzt_guest_library_access);
#endif

if(ctx->local_maplib)
FreeLibrarian(&ctx->local_maplib);
if(ctx->maplib)
FreeLibrarian(&ctx->maplib);
#ifdef CONFIG_LATX_KZT
kzt_guest_library_access_destroy(&ctx->kzt_guest_library_access);
kzt_guest_registry_context_destroy(&ctx->kzt_guest_registry_context,
&ctx->mutex_lock);
pthread_mutex_destroy(&ctx->kzt_bridge_mutex);
#endif
FreeDictionnary(&ctx->versym);

for(int i=0; i<ctx->elfsize; ++i) {
Expand Down
27 changes: 27 additions & 0 deletions target/i386/latx/context/dlopen_recycle_transaction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "dlopen_recycle_transaction.h"

void *dlopen_recycle_transaction(
void *success_handle, size_t *count, int increment_count, void *opaque,
dlopen_recycle_prepare_fn prepare, dlopen_recycle_step_fn guest_open,
dlopen_recycle_step_fn reload,
dlopen_recycle_rollback_fn rollback)
{
int guest_opened = 0;

if (!success_handle || !count || !prepare || !guest_open || !reload ||
!rollback)
return NULL;
prepare(opaque);
if (guest_open(opaque) != 0) {
rollback(opaque, 0);
return NULL;
}
guest_opened = 1;
if (reload(opaque) != 0) {
rollback(opaque, guest_opened);
return NULL;
}
if (increment_count)
++*count;
return success_handle;
}
9 changes: 9 additions & 0 deletions target/i386/latx/context/elf_plt_relocation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "elf_plt_relocation.h"

int elf_plt_relocation_apply(elf_plt_relocation_apply_fn apply, void *opaque,
int *need_resolver)
{
if (!apply)
return -1;
return apply(opaque, need_resolver) ? -1 : 0;
}
Loading
Loading