14 #define KB_PIECE_BITS 16
18#ifndef KB_IMAGE_PIECE_BITS
19 #define KB_IMAGE_PIECE_BITS (KB_PIECE_BITS < 14 ? KB_PIECE_BITS : 14)
21#define KB_MIN_PIECE_BITS \
22 (KB_IMAGE_PIECE_BITS < KB_PIECE_BITS ? KB_IMAGE_PIECE_BITS : KB_PIECE_BITS)
36#define KB_FLAT_BITS 63
38#define KB_BUF_MAX (64u << 10)
53 #define KB_NOINLINE __attribute__((noinline))
67#define KB_A64_SIGTRAMP 0x7ffffff00000ull
184extern const uint16_t kb_class[];
253 uint64_t pc, next, target;
272 struct kb_block *to_target, *to_next;
273 uint32_t taken, fall;
281#if defined(KB_HOT) || defined(KB_COUNT)
294 uint64_t (*aot)(
struct kb_cpu* cpu, uint64_t pc);
302#define KB_IMAGE_SEGS 8
304 uint64_t va, off, len;
313 struct kb_seg seg[KB_IMAGE_SEGS];
340 uint64_t handler, flags, restorer, mask;
350 uint64_t r[KB_NREGS];
359 uint64_t lz_b, lz_c, lz_r;
368 struct kb_file files[KB_FILES];
370 uint64_t image_total,
375 uint64_t lo, end, tab;
386 uint32_t nmarks, capmarks;
388 uint64_t brk_start, brk_end, mmap_next;
391 const char* last_fault;
396 uint64_t plan_flags_run,
400 uint64_t plan_ops, plan_ops_removed;
403 uint64_t decoded, loaded;
406 uint64_t traces, trace_exits;
407 uint64_t plan_ops_run, plan_ops_ran;
427 int fault_sig, fault_code, sigreturned;
442uint8_t* kb_host(
struct kb_cpu* cpu, uint64_t va, uint64_t len);
443const uint8_t* kb_host_r(
struct kb_cpu* cpu, uint64_t va, uint64_t len);
444const uint8_t* kb_host_x(
struct kb_cpu* cpu, uint64_t va, uint64_t len);
445uint8_t* kb_span(
struct kb_cpu* cpu, uint64_t va, uint64_t* len);
446const uint8_t* kb_span_r(
struct kb_cpu* cpu, uint64_t va, uint64_t* len);
447const uint8_t* kb_span_x(
struct kb_cpu* cpu, uint64_t va, uint64_t* len);
448uint8_t* kb_buf(
struct kb_cpu* cpu, uint64_t va, uint64_t len);
449const uint8_t* kb_buf_r(
struct kb_cpu* cpu, uint64_t va, uint64_t len);
450void kb_flush(
struct kb_cpu* cpu);
451int kb_read(
struct kb_cpu* cpu, uint64_t va,
void* dst, uint64_t len);
452int kb_write(
struct kb_cpu* cpu, uint64_t va,
const void* src, uint64_t len);
456int kb_poke(
struct kb_cpu* cpu, uint64_t va,
const void* src, uint64_t len);
458int kb_can(
struct kb_cpu* cpu, uint64_t va, uint64_t len,
int acc);
462int kb_fault_code(
const struct kb_cpu* cpu, uint64_t va);
468 struct kb_cpu* cpu, uint64_t va, uint64_t len,
struct iovec* iov,
int max
471 struct kb_cpu* cpu, uint64_t va, uint64_t len,
struct iovec* iov,
int max
473uint64_t kb_pieces(uint64_t start, uint64_t end,
int bits);
474uint64_t kb_piece_va(
const struct kb_mapping* m, uint64_t k);
479const uint8_t* kb_piece_r(
488static inline int kb_shared(
const uint8_t* p) {
489 return (
int) ((uintptr_t) p & 1);
491int kb_bus(uint64_t va);
492uint8_t* kb_piece_blank(
493 struct kb_mapping* m, uint64_t va, uint64_t* lo, uint64_t* hi
498void kb_image_report(
const struct kb_cpu* cpu);
504uint64_t kb_prim_enabled(
void);
506void kb_sha256_blocks(uint32_t st[8],
const uint8_t* p,
size_t n);
507void kb_sha1_blocks(uint32_t st[5],
const uint8_t* p,
size_t n);
508void kb_md5_blocks(uint32_t st[4],
const uint8_t* p,
size_t n);
509void kb_sha512_blocks(uint64_t st[8],
const uint8_t* p,
size_t n);
510uint32_t kb_cksum_blocks(
511 uint32_t crc,
const uint8_t* tab,
const uint8_t* p,
size_t n
513int kb_cksum_canon(
const uint8_t* tab);
514uint32_t kb_cksum_fast(uint32_t crc,
const uint8_t* p,
size_t n);
515const char* kb_cksum_path(
void);
535int kb_digest_ready(
void);
536int kb_digest_at(
struct kb_cpu* cpu, uint64_t pc);
537int kb_digest_update_at(
struct kb_cpu* cpu, uint64_t pc);
539 struct kb_cpu* cpu,
int alg, uint64_t ctx, uint64_t buf, uint64_t len
542 struct kb_cpu* cpu,
int alg, uint64_t buf, uint64_t len, uint64_t ctx
544int kb_digest_finish(
struct kb_cpu* cpu,
int alg, uint64_t ctx, uint64_t res);
545void kb_blake2b_compress(
546 uint64_t h[8],
const uint8_t blk[128],
const uint64_t t[2],
552int kb_digest_save(uint8_t* out);
553void kb_digest_adopt(
const uint8_t* in,
int len);
557int kb_inflate_ready(
void);
558int kb_inflate_at(
struct kb_cpu* cpu, uint64_t pc);
559int kb_inflate_admit(
struct kb_cpu* cpu,
int id,
int mode);
560int kb_inflate(
struct kb_cpu* cpu, uint64_t* next);
561int kb_gzcrc(
struct kb_cpu* cpu);
581 KB_THUNK_ZPOISON = 0x7f
584extern const char*
const kb_zs_name[KB_ZS_N];
585int kb_thunk_at(
struct kb_cpu* cpu, uint64_t pc);
589int kb_thunk_zok(
struct kb_cpu* cpu, uint64_t caller,
int inflate);
594#define KB_ZV_RESET_CLEARS 4
595int kb_zs(
struct kb_cpu* cpu,
int e,
int ver, uint64_t* v);
596void kb_zs_report(FILE* f);
599 struct kb_cpu* cpu, uint64_t pc, uint64_t* lo, uint64_t* hi
632#define KB_ANY_ARCH (-1)
637 int (*identify)(
struct kb_cpu* cpu, uint64_t pc,
int id);
638 int (*admit)(
struct kb_cpu* cpu,
int id,
int mode);
639 int (*run)(
struct kb_cpu* cpu,
int id,
int mode, uint64_t* next);
640 int (*save)(uint8_t* out);
641 void (*adopt)(
const uint8_t* in,
int len);
644int kb_prim_rows(
void);
645const char* kb_prim_name(
int id);
647int kb_prim_thunk_id(
const char* name);
649#define KB_PRIM_STATE_MAX 4096
650int kb_prim_save(uint8_t* out);
651void kb_prim_adopt(
const uint8_t* in,
int len);
652int kb_prim_at(
struct kb_cpu* cpu, uint64_t pc);
655int kb_prim(
struct kb_cpu* cpu,
int id, uint64_t* next);
656void kb_prim_report(
void);
661#define KB_FNV0 0xcbf29ce484222325ull
662uint64_t kb_fnv(uint64_t h,
const uint8_t* p,
size_t n);
663int kb_fnv_at(
struct kb_cpu* cpu, uint64_t pc, uint32_t len, uint64_t* h);
664int kb_code_is(
struct kb_cpu* cpu, uint64_t pc, uint32_t len, uint64_t
hash);
665void kb_die(
const char* what);
666uint64_t kb_mark(
void);
667int kb_marked(uint64_t m);
668void kb_decode_report(
const struct kb_cpu* cpu);
674 struct kb_cpu* cpu,
struct kb_ic* ic, uint64_t va, uint64_t len
676uint8_t* kb_host_ic_at(
677 struct kb_cpu* cpu,
struct kb_ic* ic, uint64_t va, uint64_t len,
int acc
680 struct kb_cpu* cpu, uint64_t start, uint64_t len,
int prot
683 struct kb_cpu* cpu, uint64_t start, uint64_t len,
int prot,
int bits
691 struct kb_cpu* cpu, uint64_t hint,
int acc, uint64_t* lo, uint64_t* lim,
694int kb_unmap(
struct kb_cpu* cpu, uint64_t start, uint64_t len);
695int kb_protect(
struct kb_cpu* cpu, uint64_t start, uint64_t len,
int prot);
696uint64_t kb_free_at(
struct kb_cpu* cpu, uint64_t at, uint64_t len);
697uint64_t kb_load(
struct kb_cpu* cpu, uint64_t va,
int w);
698void kb_store(
struct kb_cpu* cpu, uint64_t va, uint64_t v,
int w);
699uint64_t kb_load_ic(
struct kb_cpu* cpu,
struct kb_ic* ic, uint64_t va,
int w);
701 struct kb_cpu* cpu,
struct kb_ic* ic, uint64_t va, uint64_t v,
int w
706extern const char kb_smc[];
708 struct kb_cpu* cpu,
struct kb_ic* ic, uint64_t va, uint64_t v,
int w
718int kb_wasm_load(
struct kb_cpu* cpu,
const char* path);
719int kb_wasm_main(
struct kb_cpu* cpu,
int argc,
char** argv);
720extern const char* kb_wasm_traps[];
724extern volatile uint32_t kb_epoch;
725#define KB_BUMP() ((void) __atomic_fetch_add(&kb_epoch, 1, __ATOMIC_RELAXED))
728int kb_cond(
struct kb_cpu* cpu,
int cond);
730void kb_flags_sync(
struct kb_cpu* cpu);
731#define KB_SYNC(cpu) \
733 if ((cpu)->lz) kb_flags_sync(cpu); \
740int kb_run(
struct kb_cpu* cpu);
749 #define KB_POLL_FUEL 64
752extern int kb_syscalled;
757void kb_hot_dump(
struct kb_cpu* cpu);
763 uint64_t rd, wr, refills, entries;
764 uint64_t lat_n, lat_sum,
773 uint64_t ck_gen, ck_range, ck_code, ck_poll, ck_thread, ck_fault, ck_link,
774 ck_exit, ck_epoch, deopt_map, deopt_code, polls;
777 uint64_t rd_fs, rd_cpuid, ijmp, exits;
778 uint64_t bump_map, bump_sig;
780extern struct kb_count kb_count;
781void kb_count_report(
struct kb_cpu* cpu);
782 #define KB_BUMP_AS(kind) (kb_count.bump_##kind++, KB_BUMP())
784 #define KB_BUMP_AS(kind) KB_BUMP()
790void kb_aot_detach(
struct kb_block* b);
791void kb_aot_stale(
struct kb_cpu* cpu);
793void kb_aot_string(
struct kb_cpu* cpu,
int stos,
int w,
int rep);
794extern volatile int* kb_pending_flag;
799 struct kb_cpu* cpu,
const char* path,
int argc,
char** argv,
char** envp
804extern const char* kb_self;
808void kb_syscall(
struct kb_cpu* cpu);
809int64_t kb_err(
int e);
810char* kb_str(
struct kb_cpu* cpu, uint64_t va);
819void kb_log_init(
int resumed);
820int kb_own_fd(
int fd);
821int kb_keep_fd(
int fd);
822FILE* kb_own_fopen(
const char* path,
const char* mode);
823int kb_past_limit(
int fd);
824int64_t kb_newfd(
int fd);
825int64_t kb_newfd2(
int* fds);
826void kb_nofile_get(uint64_t out[2]);
827void kb_nofile_set(
const uint64_t in[2]);
831int kb_fork_by_exec(
void);
832int64_t kb_fork(
struct kb_cpu* cpu);
833int kb_resume(
struct kb_cpu* cpu,
int fd);
837int64_t kb_net(
struct kb_cpu* cpu, int64_t nr,
const uint64_t* a);
849int64_t kb_sigprocmask(
struct kb_cpu* cpu,
int how, uint64_t set, uint64_t old);
850void kb_raise(
struct kb_cpu* cpu,
int s,
int code);
851void kb_sigreturn(
struct kb_cpu* cpu);
852int kb_restart(
struct kb_cpu* cpu,
int restartable);
853void kb_fxsave(
struct kb_cpu* cpu, uint64_t at);
854void kb_fxrstor(
struct kb_cpu* cpu, uint64_t at);
855void kb_signals(
struct kb_cpu* cpu);
856int kb_fault(
struct kb_cpu* cpu,
int s,
int code, uint64_t addr);
857int kb_host_sig(
int s);
858int64_t kb_sigsuspend(
struct kb_cpu* cpu, uint64_t mask);
859void kb_sig_reinstall(
struct kb_cpu* cpu);
860void kb_sig_inherit(
struct kb_cpu* cpu);
861int64_t kb_sigpending(
struct kb_cpu* cpu, uint64_t set);
862int64_t kb_timer(
struct kb_cpu* cpu, int64_t nr,
const uint64_t* a);
866extern int kb_nthreads;
867extern uint64_t kb_switches;
868extern volatile int* kb_pending_flag;
870#define KB_SWITCHED INT64_MIN
871#define KB_SLICE 16384
872int64_t kb_thread_clone(
struct kb_cpu* cpu,
const uint64_t* a);
873int kb_thread_exit(
struct kb_cpu* cpu);
874int64_t kb_gettid(
void);
875int64_t kb_futex(
struct kb_cpu* cpu,
const uint64_t* a);
876void kb_yield(
struct kb_cpu* cpu);
878 struct kb_cpu* cpu, int64_t nr,
const uint64_t* a, int64_t* v
880void kb_thread_forked(
void);
881void kb_thread_called(
void);
888void kb_put(
struct kb_emit* e,
int op,
int w,
int a,
int b,
int c, int64_t imm);
907#define KB_TRACE_AT 64
914 uint64_t lo[KB_SEGS], hi[KB_SEGS];
918 uint64_t end, uint64_t* at
924void kb_persist_load(
struct kb_cpu* cpu,
const char* elf);
926void kb_persist_save(
struct kb_cpu* cpu);
927uint8_t* kb_persist_pack(
928 struct kb_cpu* cpu,
size_t* n,
int (*want)(uint64_t pc)
930void kb_persist_adopt(
struct kb_cpu* cpu, uint8_t* blob,
size_t n);
931uint64_t kb_code_hash(
struct kb_cpu* cpu,
const struct kb_block* b,
int* ok);