Skip to content

Commit 7633d5f

Browse files
committed
reorganize stuff
1 parent 5682a6d commit 7633d5f

21 files changed

+367
-262
lines changed

accel/tcg/cpu-exec.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
#include "internal-common.h"
4646
#include "internal-target.h"
4747

48+
//// --- Begin LibAFL code ---
49+
50+
#include "libafl/exit.h"
51+
#include "libafl/tcg.h"
52+
#include "libafl/hooks/tcg/edge.h"
53+
54+
//// --- End LibAFL code ---
55+
56+
4857
/* -icount align implementation. */
4958

5059
typedef struct SyncClocks {
@@ -702,12 +711,6 @@ static inline void cpu_handle_debug_exception(CPUState *cpu)
702711
}
703712
}
704713

705-
//// --- Begin LibAFL code ---
706-
707-
#include "libafl/exit.h"
708-
709-
//// --- End LibAFL code ---
710-
711714
static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
712715
{
713716
//// --- Begin LibAFL code ---
@@ -958,14 +961,6 @@ static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
958961
#endif
959962
}
960963

961-
//// --- Begin LibAFL code ---
962-
963-
TranslationBlock *libafl_gen_edge(CPUState *cpu, target_ulong src_block,
964-
target_ulong dst_block, int exit_n, target_ulong cs_base,
965-
uint32_t flags, int cflags);
966-
967-
//// --- End LibAFL code ---
968-
969964
/* main execution loop */
970965

971966
static int __attribute__((noinline))

accel/tcg/internal-common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void tb_reset_jump(TranslationBlock *tb, int n);
5454
TranslationBlock *tb_link_page(TranslationBlock *tb);
5555
void cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
5656
uintptr_t host_pc);
57+
int encode_search(TranslationBlock *tb, uint8_t *block);
5758

5859
/**
5960
* tlb_init - initialize a CPU's TLB

accel/tcg/tcg-runtime.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,6 @@
3131
#include "exec/helper-info.c.inc"
3232
#undef HELPER_H
3333

34-
//// --- Begin LibAFL code ---
35-
36-
#include "libafl/exit.h"
37-
38-
void HELPER(libafl_qemu_handle_breakpoint)(CPUArchState *env, uint64_t pc)
39-
{
40-
CPUState* cpu = env_cpu(env);
41-
libafl_exit_request_breakpoint(cpu, (target_ulong) pc);
42-
}
43-
44-
void HELPER(libafl_qemu_handle_custom_insn)(CPUArchState *env, uint64_t pc, uint32_t kind)
45-
{
46-
CPUState* cpu = env_cpu(env);
47-
libafl_exit_request_custom_insn(cpu, (target_ulong) pc, (enum libafl_custom_insn_kind) kind);
48-
}
49-
50-
//// --- End LibAFL code ---
51-
5234
/* 32-bit helpers */
5335

5436
int32_t HELPER(div_i32)(int32_t arg1, int32_t arg2)

accel/tcg/tcg-runtime.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,3 @@ DEF_HELPER_FLAGS_4(gvec_leus32, TCG_CALL_NO_RWG, void, ptr, ptr, i64, i32)
323323
DEF_HELPER_FLAGS_4(gvec_leus64, TCG_CALL_NO_RWG, void, ptr, ptr, i64, i32)
324324

325325
DEF_HELPER_FLAGS_5(gvec_bitsel, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
326-
327-
//// --- Begin LibAFL code ---
328-
329-
DEF_HELPER_FLAGS_2(libafl_qemu_handle_breakpoint, TCG_CALL_NO_RWG,
330-
void, env, i64)
331-
332-
DEF_HELPER_FLAGS_3(libafl_qemu_handle_custom_insn, TCG_CALL_NO_RWG,
333-
void, env, i64, i32)
334-
335-
//// --- End LibAFL code ---

accel/tcg/translate-all.c

Lines changed: 4 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ static int64_t decode_sleb128(const uint8_t **pp)
132132
line. The seed for the first line is { tb->pc, 0..., tb->tc.ptr }.
133133
That is, the first column is seeded with the guest pc, the last column
134134
with the host pc, and the middle columns with zeros. */
135-
136-
static int encode_search(TranslationBlock *tb, uint8_t *block)
135+
/*
136+
static
137+
*/
138+
int encode_search(TranslationBlock *tb, uint8_t *block)
137139
{
138140
uint8_t *highwater = tcg_ctx->code_gen_highwater;
139141
uint64_t *insn_data = tcg_ctx->gen_insn_data;
@@ -299,208 +301,6 @@ static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
299301

300302
return tcg_gen_code(tcg_ctx, tb, pc);
301303
}
302-
303-
/* Called with mmap_lock held for user mode emulation. */
304-
TranslationBlock *libafl_gen_edge(CPUState *cpu, target_ulong src_block,
305-
target_ulong dst_block, int exit_n,
306-
target_ulong cs_base, uint32_t flags,
307-
int cflags)
308-
{
309-
CPUArchState *env = cpu_env(cpu);
310-
TranslationBlock *tb;
311-
tb_page_addr_t phys_pc;
312-
tcg_insn_unit *gen_code_buf;
313-
int gen_code_size, search_size, max_insns;
314-
int64_t ti;
315-
void *host_pc;
316-
317-
// edge hooks generation callbacks
318-
// early check if it should be skipped or not
319-
bool no_exec_hook = libafl_qemu_hook_edge_gen(src_block, dst_block);
320-
if (no_exec_hook) {
321-
// no exec hooks to run for edges, not point in generating a TB
322-
return NULL;
323-
}
324-
325-
target_ulong pc = src_block ^ reverse_bits((target_ulong)exit_n);
326-
327-
assert_memory_lock();
328-
qemu_thread_jit_write();
329-
330-
// TODO: this (get_page_addr_code_hostp) is a bottleneck in systemmode, investigate why
331-
phys_pc = get_page_addr_code_hostp(env, src_block, &host_pc);
332-
phys_pc ^= reverse_bits((tb_page_addr_t)exit_n);
333-
334-
// if (phys_pc == -1) {
335-
// /* Generate a one-shot TB with 1 insn in it */
336-
// cflags = (cflags & ~CF_COUNT_MASK) | 1;
337-
// }
338-
339-
/* Generate a one-shot TB with max 16 insn in it */
340-
cflags = (cflags & ~CF_COUNT_MASK) | LIBAFL_MAX_INSNS;
341-
QEMU_BUILD_BUG_ON(LIBAFL_MAX_INSNS > TCG_MAX_INSNS);
342-
343-
max_insns = cflags & CF_COUNT_MASK;
344-
if (max_insns == 0) {
345-
max_insns = TCG_MAX_INSNS;
346-
}
347-
QEMU_BUILD_BUG_ON(CF_COUNT_MASK + 1 != TCG_MAX_INSNS);
348-
349-
buffer_overflow:
350-
assert_no_pages_locked();
351-
tb = tcg_tb_alloc(tcg_ctx);
352-
if (unlikely(!tb)) {
353-
/* flush must be done */
354-
tb_flush(cpu);
355-
mmap_unlock();
356-
/* Make the execution loop process the flush as soon as possible. */
357-
cpu->exception_index = EXCP_INTERRUPT;
358-
cpu_loop_exit(cpu);
359-
}
360-
361-
gen_code_buf = tcg_ctx->code_gen_ptr;
362-
tb->tc.ptr = tcg_splitwx_to_rx(gen_code_buf);
363-
364-
if (!(cflags & CF_PCREL)) {
365-
tb->pc = pc;
366-
}
367-
368-
tb->cs_base = cs_base;
369-
tb->flags = flags;
370-
tb->cflags = cflags | CF_IS_EDGE;
371-
tb_set_page_addr0(tb, phys_pc);
372-
tb_set_page_addr1(tb, -1);
373-
// if (phys_pc != -1) {
374-
// tb_lock_page0(phys_pc);
375-
// }
376-
377-
tcg_ctx->gen_tb = tb;
378-
tcg_ctx->addr_type = TARGET_LONG_BITS == 32 ? TCG_TYPE_I32 : TCG_TYPE_I64;
379-
#ifdef CONFIG_SOFTMMU
380-
tcg_ctx->page_bits = TARGET_PAGE_BITS;
381-
tcg_ctx->page_mask = TARGET_PAGE_MASK;
382-
tcg_ctx->tlb_dyn_max_bits = CPU_TLB_DYN_MAX_BITS;
383-
#endif
384-
tcg_ctx->insn_start_words = TARGET_INSN_START_WORDS;
385-
#ifdef TCG_GUEST_DEFAULT_MO
386-
tcg_ctx->guest_mo = TCG_GUEST_DEFAULT_MO;
387-
#else
388-
tcg_ctx->guest_mo = TCG_MO_ALL;
389-
#endif
390-
391-
restart_translate:
392-
trace_translate_block(tb, pc, tb->tc.ptr);
393-
394-
gen_code_size = libafl_setjmp_gen_code(env, tb, pc, host_pc, &max_insns, &ti);
395-
if (unlikely(gen_code_size < 0)) {
396-
switch (gen_code_size) {
397-
case -1:
398-
/*
399-
* Overflow of code_gen_buffer, or the current slice of it.
400-
*
401-
* TODO: We don't need to re-do gen_intermediate_code, nor
402-
* should we re-do the tcg optimization currently hidden
403-
* inside tcg_gen_code. All that should be required is to
404-
* flush the TBs, allocate a new TB, re-initialize it per
405-
* above, and re-do the actual code generation.
406-
*/
407-
qemu_log_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT,
408-
"Restarting code generation for "
409-
"code_gen_buffer overflow\n");
410-
tb_unlock_pages(tb);
411-
tcg_ctx->gen_tb = NULL;
412-
goto buffer_overflow;
413-
414-
case -2:
415-
assert(false && "This should never happen for edge code. There must be a bug.");
416-
/*
417-
* The code generated for the TranslationBlock is too large.
418-
* The maximum size allowed by the unwind info is 64k.
419-
* There may be stricter constraints from relocations
420-
* in the tcg backend.
421-
*
422-
* Try again with half as many insns as we attempted this time.
423-
* If a single insn overflows, there's a bug somewhere...
424-
*/
425-
assert(max_insns > 1);
426-
max_insns /= 2;
427-
qemu_log_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT,
428-
"Restarting code generation with "
429-
"smaller translation block (max %d insns)\n",
430-
max_insns);
431-
432-
/*
433-
* The half-sized TB may not cross pages.
434-
* TODO: Fix all targets that cross pages except with
435-
* the first insn, at which point this can't be reached.
436-
*/
437-
// phys_p2 = tb_page_addr1(tb);
438-
// if (unlikely(phys_p2 != -1)) {
439-
// tb_unlock_page1(phys_pc, phys_p2);
440-
// tb_set_page_addr1(tb, -1);
441-
// }
442-
goto restart_translate;
443-
444-
case -3:
445-
/*
446-
* We had a page lock ordering problem. In order to avoid
447-
* deadlock we had to drop the lock on page0, which means
448-
* that everything we translated so far is compromised.
449-
* Restart with locks held on both pages.
450-
*/
451-
qemu_log_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT,
452-
"Restarting code generation with re-locked pages");
453-
goto restart_translate;
454-
455-
default:
456-
g_assert_not_reached();
457-
}
458-
}
459-
tcg_ctx->gen_tb = NULL;
460-
461-
search_size = encode_search(tb, (void *)gen_code_buf + gen_code_size);
462-
if (unlikely(search_size < 0)) {
463-
tb_unlock_pages(tb);
464-
goto buffer_overflow;
465-
}
466-
tb->tc.size = gen_code_size;
467-
468-
/*
469-
* For CF_PCREL, attribute all executions of the generated code
470-
* to its first mapping.
471-
*/
472-
perf_report_code(pc, tb, tcg_splitwx_to_rx(gen_code_buf));
473-
474-
qatomic_set(&tcg_ctx->code_gen_ptr, (void *)
475-
ROUND_UP((uintptr_t)gen_code_buf + gen_code_size + search_size,
476-
CODE_GEN_ALIGN));
477-
478-
/* init jump list */
479-
qemu_spin_init(&tb->jmp_lock);
480-
tb->jmp_list_head = (uintptr_t)NULL;
481-
tb->jmp_list_next[0] = (uintptr_t)NULL;
482-
tb->jmp_list_next[1] = (uintptr_t)NULL;
483-
tb->jmp_dest[0] = (uintptr_t)NULL;
484-
tb->jmp_dest[1] = (uintptr_t)NULL;
485-
486-
/* init original jump addresses which have been set during tcg_gen_code() */
487-
if (tb->jmp_reset_offset[0] != TB_JMP_OFFSET_INVALID) {
488-
tb_reset_jump(tb, 0);
489-
}
490-
if (tb->jmp_reset_offset[1] != TB_JMP_OFFSET_INVALID) {
491-
tb_reset_jump(tb, 1);
492-
}
493-
494-
assert_no_pages_locked();
495-
496-
#ifndef CONFIG_USER_ONLY
497-
tb->page_addr[0] = tb->page_addr[1] = -1;
498-
#endif
499-
return tb;
500-
}
501-
502-
//// --- End LibAFL code ---
503-
504304
/* Called with mmap_lock held for user mode emulation. */
505305
TranslationBlock *tb_gen_code(CPUState *cpu,
506306
vaddr pc, uint64_t cs_base,

hw/core/cpu-system.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
#include "migration/vmstate.h"
3232
#include "system/tcg.h"
3333

34+
//// --- Begin LibAFL code ---
35+
36+
#ifndef CONFIG_USER_ONLY
37+
#include "libafl/syx-snapshot/device-save.h"
38+
#endif
39+
40+
//// --- End LibAFL code ---
41+
3442
bool cpu_has_work(CPUState *cpu)
3543
{
3644
return cpu->cc->sysemu_ops->has_work(cpu);
@@ -214,7 +222,16 @@ static int cpu_common_post_load(void *opaque, int version_id)
214222
* memory we've translated code from. So we must flush all TBs,
215223
* which will now be stale.
216224
*/
217-
tb_flush(cpu);
225+
//tb_flush(cpu);
226+
//// --- Begin LibAFL code ---
227+
228+
// flushing the TBs every restore makes it really slow
229+
// TODO handle writes to X code with specific calls to tb_invalidate_phys_addr
230+
if (!libafl_devices_is_restoring()) {
231+
tb_flush(cpu);
232+
}
233+
234+
//// --- End LibAFL code ---
218235
}
219236

220237
return 0;

include/exec/cpu-all.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ static inline void stl_phys_notdirty(AddressSpace *as, hwaddr addr, uint32_t val
100100
#include "exec/cpu-defs.h"
101101
#include "exec/target_page.h"
102102

103-
//// --- Begin LibAFL code ---
104-
IntervalTreeRoot* pageflags_get_root(void);
105-
//// --- End LibAFL code ---
106-
107103
CPUArchState *cpu_copy(CPUArchState *env);
108104

109105
#include "cpu.h"

include/exec/helper-gen-common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@
1111
#include "exec/helper-gen.h.inc"
1212
#undef HELPER_H
1313

14+
//// --- Begin LibAFL code ---
15+
#define HELPER_H "libafl/tcg-helper.h"
16+
#include "exec/helper-gen.h.inc"
17+
#undef HELPER_H
18+
//// --- End LibAFL code ---
19+
1420
#endif /* HELPER_GEN_COMMON_H */

include/exec/helper-proto-common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
#include "exec/helper-proto.h.inc"
1414
#undef HELPER_H
1515

16+
//// --- Begin LibAFL code ---
17+
#define HELPER_H "libafl/tcg-helper.h"
18+
#include "exec/helper-proto.h.inc"
19+
#undef HELPER_H
20+
//// --- End LibAFL code ---
21+
1622
#endif /* HELPER_PROTO_COMMON_H */

include/libafl/exit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

33
#include "qemu/osdep.h"
4-
#include "exec/cpu-defs.h"
4+
#include "exec/cpu_ldst.h"
5+
#include "hw/core/cpu.h"
56

67
#define EXCP_LIBAFL_EXIT 0xf4775747
78

0 commit comments

Comments
 (0)