2015-08-21 10:04:50 +03:00
|
|
|
/* Unicorn Emulator Engine */
|
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2015 */
|
2021-10-03 17:14:44 +03:00
|
|
|
/* Modified for Unicorn Engine by Chen Huitao<chenhuitao@hfmrit.com>, 2020 */
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
#ifndef UC_PRIV_H
|
|
|
|
#define UC_PRIV_H
|
|
|
|
|
2017-01-20 16:13:21 +03:00
|
|
|
#include "unicorn/platform.h"
|
2015-08-21 10:04:50 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "qemu.h"
|
2022-06-02 15:46:02 +03:00
|
|
|
#include "qemu/xxhash.h"
|
2015-08-21 10:04:50 +03:00
|
|
|
#include "unicorn/unicorn.h"
|
2016-01-16 11:44:02 +03:00
|
|
|
#include "list.h"
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-11-16 23:07:03 +03:00
|
|
|
// The max recursive nested uc_emu_start levels
|
|
|
|
#define UC_MAX_NESTED_LEVEL (64)
|
|
|
|
|
2016-01-23 01:47:29 +03:00
|
|
|
// These are masks of supported modes for each cpu/arch.
|
|
|
|
// They should be updated when changes are made to the uc_mode enum typedef.
|
2021-10-29 13:44:49 +03:00
|
|
|
#define UC_MODE_ARM_MASK \
|
|
|
|
(UC_MODE_ARM | UC_MODE_THUMB | UC_MODE_LITTLE_ENDIAN | UC_MODE_MCLASS | \
|
2022-02-12 16:15:54 +03:00
|
|
|
UC_MODE_ARM926 | UC_MODE_ARM946 | UC_MODE_ARM1176 | UC_MODE_BIG_ENDIAN | \
|
|
|
|
UC_MODE_ARMBE8)
|
2021-10-29 13:44:49 +03:00
|
|
|
#define UC_MODE_MIPS_MASK \
|
|
|
|
(UC_MODE_MIPS32 | UC_MODE_MIPS64 | UC_MODE_LITTLE_ENDIAN | \
|
|
|
|
UC_MODE_BIG_ENDIAN)
|
|
|
|
#define UC_MODE_X86_MASK \
|
|
|
|
(UC_MODE_16 | UC_MODE_32 | UC_MODE_64 | UC_MODE_LITTLE_ENDIAN)
|
|
|
|
#define UC_MODE_PPC_MASK (UC_MODE_PPC32 | UC_MODE_PPC64 | UC_MODE_BIG_ENDIAN)
|
|
|
|
#define UC_MODE_SPARC_MASK \
|
|
|
|
(UC_MODE_SPARC32 | UC_MODE_SPARC64 | UC_MODE_BIG_ENDIAN)
|
|
|
|
#define UC_MODE_M68K_MASK (UC_MODE_BIG_ENDIAN)
|
|
|
|
#define UC_MODE_RISCV_MASK \
|
|
|
|
(UC_MODE_RISCV32 | UC_MODE_RISCV64 | UC_MODE_LITTLE_ENDIAN)
|
2021-12-27 00:58:32 +03:00
|
|
|
#define UC_MODE_S390X_MASK (UC_MODE_BIG_ENDIAN)
|
2022-04-30 00:11:34 +03:00
|
|
|
#define UC_MODE_TRICORE_MASK (UC_MODE_LITTLE_ENDIAN)
|
2021-10-29 13:44:49 +03:00
|
|
|
|
|
|
|
#define ARR_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
#define READ_QWORD(x) ((uint64_t)x)
|
2016-03-02 06:43:02 +03:00
|
|
|
#define READ_DWORD(x) (x & 0xffffffff)
|
|
|
|
#define READ_WORD(x) (x & 0xffff)
|
|
|
|
#define READ_BYTE_H(x) ((x & 0xffff) >> 8)
|
|
|
|
#define READ_BYTE_L(x) (x & 0xff)
|
2017-01-15 15:13:35 +03:00
|
|
|
#define WRITE_DWORD(x, w) (x = (x & ~0xffffffffLL) | (w & 0xffffffff))
|
2016-03-02 06:43:02 +03:00
|
|
|
#define WRITE_WORD(x, w) (x = (x & ~0xffff) | (w & 0xffff))
|
|
|
|
#define WRITE_BYTE_H(x, b) (x = (x & ~0xff00) | ((b & 0xff) << 8))
|
|
|
|
#define WRITE_BYTE_L(x, b) (x = (x & ~0xff) | (b & 0xff))
|
|
|
|
|
2021-11-01 02:39:36 +03:00
|
|
|
struct TranslationBlock;
|
|
|
|
|
2021-11-24 02:18:19 +03:00
|
|
|
// Place the struct here since we need it in uc.c
|
|
|
|
typedef struct _mmio_cbs {
|
|
|
|
uc_cb_mmio_read_t read;
|
|
|
|
void *user_data_read;
|
|
|
|
uc_cb_mmio_write_t write;
|
|
|
|
void *user_data_write;
|
|
|
|
MemoryRegionOps ops;
|
|
|
|
} mmio_cbs;
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef uc_err (*query_t)(struct uc_struct *uc, uc_query_type type,
|
|
|
|
size_t *result);
|
2016-01-23 12:14:44 +03:00
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
// return 0 on success, -1 on failure
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef int (*reg_read_t)(struct uc_struct *uc, unsigned int *regs, void **vals,
|
|
|
|
int count);
|
|
|
|
typedef int (*reg_write_t)(struct uc_struct *uc, unsigned int *regs,
|
|
|
|
void *const *vals, int count);
|
|
|
|
|
|
|
|
typedef int (*context_reg_read_t)(struct uc_context *ctx, unsigned int *regs,
|
|
|
|
void **vals, int count);
|
|
|
|
typedef int (*context_reg_write_t)(struct uc_context *ctx, unsigned int *regs,
|
|
|
|
void *const *vals, int count);
|
2021-10-03 17:14:44 +03:00
|
|
|
typedef struct {
|
|
|
|
context_reg_read_t context_reg_read;
|
|
|
|
context_reg_write_t context_reg_write;
|
|
|
|
} context_reg_rw_t;
|
|
|
|
|
2015-08-26 15:00:00 +03:00
|
|
|
typedef void (*reg_reset_t)(struct uc_struct *uc);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef bool (*uc_write_mem_t)(AddressSpace *as, hwaddr addr,
|
|
|
|
const uint8_t *buf, int len);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef bool (*uc_read_mem_t)(AddressSpace *as, hwaddr addr, uint8_t *buf,
|
|
|
|
int len);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef void (*uc_args_void_t)(void *);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef void (*uc_args_uc_t)(struct uc_struct *);
|
|
|
|
typedef void (*uc_args_int_uc_t)(struct uc_struct *);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef void (*uc_args_uc_long_t)(struct uc_struct *, unsigned long);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
typedef void (*uc_args_uc_u64_t)(struct uc_struct *, uint64_t addr);
|
|
|
|
|
2022-05-07 01:30:18 +03:00
|
|
|
typedef uint64_t (*uc_get_pc_t)(struct uc_struct *);
|
2022-05-07 01:16:31 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef MemoryRegion *(*uc_args_uc_ram_size_t)(struct uc_struct *, hwaddr begin,
|
|
|
|
size_t size, uint32_t perms);
|
2015-08-26 23:29:54 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef MemoryRegion *(*uc_args_uc_ram_size_ptr_t)(struct uc_struct *,
|
|
|
|
hwaddr begin, size_t size,
|
|
|
|
uint32_t perms, void *ptr);
|
2015-11-28 04:25:53 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef void (*uc_mem_unmap_t)(struct uc_struct *, MemoryRegion *mr);
|
2015-08-30 07:17:30 +03:00
|
|
|
|
2015-08-26 23:29:54 +03:00
|
|
|
typedef void (*uc_readonly_mem_t)(MemoryRegion *mr, bool readonly);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
typedef int (*uc_cpus_init)(struct uc_struct *, const char *);
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef MemoryRegion *(*uc_memory_map_io_t)(struct uc_struct *uc,
|
|
|
|
ram_addr_t begin, size_t size,
|
|
|
|
uc_cb_mmio_read_t read_cb,
|
|
|
|
uc_cb_mmio_write_t write_cb,
|
|
|
|
void *user_data_read,
|
|
|
|
void *user_data_write);
|
2021-10-03 17:14:44 +03:00
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
// which interrupt should make emulation stop?
|
2021-10-03 17:14:44 +03:00
|
|
|
typedef bool (*uc_args_int_t)(struct uc_struct *uc, int intno);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2015-10-27 09:37:03 +03:00
|
|
|
// some architecture redirect virtual memory to physical memory like Mips
|
|
|
|
typedef uint64_t (*uc_mem_redirect_t)(uint64_t address);
|
|
|
|
|
2017-05-13 20:16:17 +03:00
|
|
|
// validate if Unicorn supports hooking a given instruction
|
2021-10-29 13:44:49 +03:00
|
|
|
typedef bool (*uc_insn_hook_validate)(uint32_t insn_enum);
|
2017-05-13 20:16:17 +03:00
|
|
|
|
2021-11-03 03:46:24 +03:00
|
|
|
typedef bool (*uc_opcode_hook_validate_t)(uint32_t op, uint32_t flags);
|
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
// init target page
|
|
|
|
typedef void (*uc_target_page_init)(struct uc_struct *);
|
|
|
|
|
|
|
|
// soft float init
|
|
|
|
typedef void (*uc_softfloat_initialize)(void);
|
|
|
|
|
|
|
|
// tcg flush softmmu tlb
|
|
|
|
typedef void (*uc_tcg_flush_tlb)(struct uc_struct *uc);
|
|
|
|
|
2021-11-01 02:39:36 +03:00
|
|
|
// Invalidate the TB at given address
|
|
|
|
typedef void (*uc_invalidate_tb_t)(struct uc_struct *uc, uint64_t start,
|
|
|
|
size_t len);
|
|
|
|
|
|
|
|
// Request generating TB at given address
|
2021-11-02 00:11:43 +03:00
|
|
|
typedef uc_err (*uc_gen_tb_t)(struct uc_struct *uc, uint64_t pc, uc_tb *out_tb);
|
2021-11-01 02:39:36 +03:00
|
|
|
|
2022-04-26 01:45:49 +03:00
|
|
|
// tb flush
|
|
|
|
typedef uc_tcg_flush_tlb uc_tb_flush_t;
|
|
|
|
|
2016-01-16 11:44:02 +03:00
|
|
|
struct hook {
|
2021-10-29 13:44:49 +03:00
|
|
|
int type; // UC_HOOK_*
|
|
|
|
int insn; // instruction for HOOK_INSN
|
|
|
|
int refs; // reference count to free hook stored in multiple lists
|
2021-11-03 03:46:24 +03:00
|
|
|
int op; // opcode for HOOK_TCG_OPCODE
|
|
|
|
int op_flags; // opcode flags for HOOK_TCG_OPCODE
|
2021-10-29 13:44:49 +03:00
|
|
|
bool to_delete; // set to true when the hook is deleted by the user. The
|
|
|
|
// destruction of the hook is delayed.
|
|
|
|
uint64_t begin, end; // only trigger if PC or memory access is in this
|
|
|
|
// address (depends on hook type)
|
2016-01-16 11:44:02 +03:00
|
|
|
void *callback; // a uc_cb_* type
|
2015-08-21 10:04:50 +03:00
|
|
|
void *user_data;
|
2022-06-02 15:46:02 +03:00
|
|
|
GHashTable *hooked_regions; // The regions this hook instrumented on
|
2015-08-21 10:04:50 +03:00
|
|
|
};
|
|
|
|
|
2021-11-21 18:44:39 +03:00
|
|
|
// Add an inline hook to helper_table
|
|
|
|
typedef void (*uc_add_inline_hook_t)(struct uc_struct *uc, struct hook *hk,
|
|
|
|
void **args, int args_len);
|
|
|
|
|
|
|
|
// Delete a hook from helper_table
|
|
|
|
typedef void (*uc_del_inline_hook_t)(struct uc_struct *uc, struct hook *hk);
|
|
|
|
|
2016-01-16 11:44:02 +03:00
|
|
|
// hook list offsets
|
2021-10-03 17:14:44 +03:00
|
|
|
//
|
|
|
|
// The lowest 6 bits are used for hook type index while the others
|
|
|
|
// are used for hook flags.
|
|
|
|
//
|
2016-01-16 11:44:02 +03:00
|
|
|
// mirrors the order of uc_hook_type from include/unicorn/unicorn.h
|
2021-10-03 17:14:44 +03:00
|
|
|
typedef enum uc_hook_idx {
|
2016-01-16 11:44:02 +03:00
|
|
|
UC_HOOK_INTR_IDX,
|
|
|
|
UC_HOOK_INSN_IDX,
|
|
|
|
UC_HOOK_CODE_IDX,
|
|
|
|
UC_HOOK_BLOCK_IDX,
|
|
|
|
UC_HOOK_MEM_READ_UNMAPPED_IDX,
|
|
|
|
UC_HOOK_MEM_WRITE_UNMAPPED_IDX,
|
|
|
|
UC_HOOK_MEM_FETCH_UNMAPPED_IDX,
|
|
|
|
UC_HOOK_MEM_READ_PROT_IDX,
|
|
|
|
UC_HOOK_MEM_WRITE_PROT_IDX,
|
|
|
|
UC_HOOK_MEM_FETCH_PROT_IDX,
|
|
|
|
UC_HOOK_MEM_READ_IDX,
|
|
|
|
UC_HOOK_MEM_WRITE_IDX,
|
|
|
|
UC_HOOK_MEM_FETCH_IDX,
|
2016-10-22 06:19:55 +03:00
|
|
|
UC_HOOK_MEM_READ_AFTER_IDX,
|
2019-09-22 20:53:06 +03:00
|
|
|
UC_HOOK_INSN_INVALID_IDX,
|
2021-11-02 01:27:35 +03:00
|
|
|
UC_HOOK_EDGE_GENERATED_IDX,
|
2021-11-03 03:46:24 +03:00
|
|
|
UC_HOOK_TCG_OPCODE_IDX,
|
2016-01-16 11:44:02 +03:00
|
|
|
|
|
|
|
UC_HOOK_MAX,
|
2021-10-03 17:14:44 +03:00
|
|
|
} uc_hook_idx;
|
|
|
|
|
2021-11-02 01:27:35 +03:00
|
|
|
// Copy the essential information from TranslationBlock
|
|
|
|
#define UC_TB_COPY(uc_tb, tb) \
|
|
|
|
do { \
|
|
|
|
(uc_tb)->pc = tb->pc; \
|
|
|
|
(uc_tb)->icount = tb->icount; \
|
|
|
|
(uc_tb)->size = tb->size; \
|
|
|
|
} while (0)
|
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
// The lowest 6 bits are used for hook type index.
|
2021-10-29 13:44:49 +03:00
|
|
|
#define UC_HOOK_IDX_MASK ((1 << 6) - 1)
|
2021-10-03 17:14:44 +03:00
|
|
|
|
|
|
|
// hook flags
|
2021-10-29 13:44:49 +03:00
|
|
|
#define UC_HOOK_FLAG_NO_STOP \
|
|
|
|
(1 << 6) // Don't stop emulation in this uc_tracecode.
|
2021-10-03 17:14:44 +03:00
|
|
|
|
|
|
|
// The rest of bits are reserved for hook flags.
|
|
|
|
#define UC_HOOK_FLAG_MASK (~(UC_HOOK_IDX_MASK))
|
2016-01-16 11:44:02 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
#define HOOK_FOREACH_VAR_DECLARE struct list_item *cur
|
2017-01-19 14:50:28 +03:00
|
|
|
|
2016-01-16 11:44:02 +03:00
|
|
|
// for loop macro to loop over hook lists
|
2021-10-29 13:44:49 +03:00
|
|
|
#define HOOK_FOREACH(uc, hh, idx) \
|
|
|
|
for (cur = (uc)->hook[idx##_IDX].head; \
|
|
|
|
cur != NULL && ((hh) = (struct hook *)cur->data); cur = cur->next)
|
2016-01-16 11:44:02 +03:00
|
|
|
|
|
|
|
// if statement to check hook bounds
|
2021-10-29 13:44:49 +03:00
|
|
|
#define HOOK_BOUND_CHECK(hh, addr) \
|
|
|
|
((((addr) >= (hh)->begin && (addr) <= (hh)->end) || \
|
|
|
|
(hh)->begin > (hh)->end) && \
|
|
|
|
!((hh)->to_delete))
|
2016-01-16 11:44:02 +03:00
|
|
|
|
|
|
|
#define HOOK_EXISTS(uc, idx) ((uc)->hook[idx##_IDX].head != NULL)
|
2021-10-29 13:44:49 +03:00
|
|
|
#define HOOK_EXISTS_BOUNDED(uc, idx, addr) \
|
|
|
|
_hook_exists_bounded((uc)->hook[idx##_IDX].head, addr)
|
2016-01-16 11:44:02 +03:00
|
|
|
|
|
|
|
static inline bool _hook_exists_bounded(struct list_item *cur, uint64_t addr)
|
|
|
|
{
|
|
|
|
while (cur != NULL) {
|
|
|
|
if (HOOK_BOUND_CHECK((struct hook *)cur->data, addr))
|
|
|
|
return true;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
// relloc increment, KEEP THIS A POWER OF 2!
|
2015-08-26 09:08:18 +03:00
|
|
|
#define MEM_BLOCK_INCR 32
|
|
|
|
|
2021-10-03 17:14:44 +03:00
|
|
|
typedef struct TargetPageBits TargetPageBits;
|
|
|
|
typedef struct TCGContext TCGContext;
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
struct uc_struct {
|
|
|
|
uc_arch arch;
|
|
|
|
uc_mode mode;
|
2021-10-29 13:44:49 +03:00
|
|
|
uc_err errnum; // qemu/cpu-exec.c
|
2021-10-03 17:14:44 +03:00
|
|
|
AddressSpace address_space_memory;
|
|
|
|
AddressSpace address_space_io;
|
2016-01-23 12:14:44 +03:00
|
|
|
query_t query;
|
2015-08-24 19:42:50 +03:00
|
|
|
reg_read_t reg_read;
|
|
|
|
reg_write_t reg_write;
|
2015-08-21 10:04:50 +03:00
|
|
|
reg_reset_t reg_reset;
|
|
|
|
|
|
|
|
uc_write_mem_t write_mem;
|
|
|
|
uc_read_mem_t read_mem;
|
2021-10-29 13:44:49 +03:00
|
|
|
uc_args_void_t release; // release resource when uc_close()
|
|
|
|
uc_args_uc_u64_t set_pc; // set PC for tracecode
|
2022-05-07 01:16:31 +03:00
|
|
|
uc_get_pc_t get_pc;
|
2021-10-29 13:44:49 +03:00
|
|
|
uc_args_int_t
|
|
|
|
stop_interrupt; // check if the interrupt should stop emulation
|
2021-10-03 17:14:44 +03:00
|
|
|
uc_memory_map_io_t memory_map_io;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-03-26 03:24:28 +03:00
|
|
|
uc_args_uc_t init_arch, cpu_exec_init_all;
|
2015-11-11 20:43:41 +03:00
|
|
|
uc_args_int_uc_t vm_start;
|
2015-08-21 10:04:50 +03:00
|
|
|
uc_args_uc_long_t tcg_exec_init;
|
|
|
|
uc_args_uc_ram_size_t memory_map;
|
2015-11-28 04:25:53 +03:00
|
|
|
uc_args_uc_ram_size_ptr_t memory_map_ptr;
|
2015-08-30 07:17:30 +03:00
|
|
|
uc_mem_unmap_t memory_unmap;
|
2015-08-26 23:29:54 +03:00
|
|
|
uc_readonly_mem_t readonly_mem;
|
2015-10-27 09:37:03 +03:00
|
|
|
uc_mem_redirect_t mem_redirect;
|
2021-10-03 17:14:44 +03:00
|
|
|
uc_cpus_init cpus_init;
|
|
|
|
uc_target_page_init target_page;
|
|
|
|
uc_softfloat_initialize softfloat_initialize;
|
|
|
|
uc_tcg_flush_tlb tcg_flush_tlb;
|
2021-11-01 02:39:36 +03:00
|
|
|
uc_invalidate_tb_t uc_invalidate_tb;
|
|
|
|
uc_gen_tb_t uc_gen_tb;
|
2022-04-26 01:45:49 +03:00
|
|
|
uc_tb_flush_t tb_flush;
|
2021-11-21 18:44:39 +03:00
|
|
|
uc_add_inline_hook_t add_inline_hook;
|
|
|
|
uc_del_inline_hook_t del_inline_hook;
|
2021-10-03 17:14:44 +03:00
|
|
|
|
|
|
|
/* only 1 cpu in unicorn,
|
|
|
|
do not need current_cpu to handle current running cpu. */
|
|
|
|
CPUState *cpu;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2017-05-13 20:16:17 +03:00
|
|
|
uc_insn_hook_validate insn_hook_validate;
|
2021-11-03 03:46:24 +03:00
|
|
|
uc_opcode_hook_validate_t opcode_hook_invalidate;
|
2017-05-13 20:16:17 +03:00
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
MemoryRegion *system_memory; // qemu/exec.c
|
2021-10-29 13:44:49 +03:00
|
|
|
MemoryRegion *system_io; // qemu/exec.c
|
2015-08-21 10:04:50 +03:00
|
|
|
MemoryRegion io_mem_unassigned; // qemu/exec.c
|
2021-10-29 13:44:49 +03:00
|
|
|
RAMList ram_list; // qemu/exec.c
|
2021-10-03 17:14:44 +03:00
|
|
|
/* qemu/exec.c */
|
|
|
|
unsigned int alloc_hint;
|
|
|
|
/* qemu/exec-vary.c */
|
|
|
|
TargetPageBits *init_target_page;
|
2021-11-04 20:37:10 +03:00
|
|
|
int target_bits; // User defined page bits by uc_ctl
|
|
|
|
int cpu_model;
|
2021-10-29 13:44:49 +03:00
|
|
|
BounceBuffer bounce; // qemu/cpu-exec.c
|
2015-08-21 10:04:50 +03:00
|
|
|
volatile sig_atomic_t exit_request; // qemu/cpu-exec.c
|
2021-10-03 17:14:44 +03:00
|
|
|
/* qemu/accel/tcg/cpu-exec-common.c */
|
|
|
|
/* always be true after call tcg_exec_init(). */
|
|
|
|
bool tcg_allowed;
|
2015-08-21 10:04:50 +03:00
|
|
|
/* This is a multi-level map on the virtual address space.
|
|
|
|
The bottom level has pointers to PageDesc. */
|
2021-10-29 13:44:49 +03:00
|
|
|
void **l1_map; // qemu/accel/tcg/translate-all.c
|
2015-08-21 10:04:50 +03:00
|
|
|
size_t l1_map_size;
|
2021-10-03 17:14:44 +03:00
|
|
|
/* qemu/accel/tcg/translate-all.c */
|
|
|
|
int v_l1_size;
|
|
|
|
int v_l1_shift;
|
|
|
|
int v_l2_levels;
|
2015-08-21 10:04:50 +03:00
|
|
|
/* code generation context */
|
2021-10-03 17:14:44 +03:00
|
|
|
TCGContext *tcg_ctx;
|
2015-08-21 10:04:50 +03:00
|
|
|
/* memory.c */
|
|
|
|
QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners;
|
|
|
|
QTAILQ_HEAD(, AddressSpace) address_spaces;
|
2021-10-03 17:14:44 +03:00
|
|
|
GHashTable *flat_views;
|
|
|
|
bool memory_region_update_pending;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2016-01-16 11:44:02 +03:00
|
|
|
// linked lists containing hooks per type
|
|
|
|
struct list hook[UC_HOOK_MAX];
|
2020-05-07 09:24:48 +03:00
|
|
|
struct list hooks_to_del;
|
2021-11-21 18:44:39 +03:00
|
|
|
int hooks_count[UC_HOOK_MAX];
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
// hook to count number of instructions for uc_emu_start()
|
2016-01-16 11:44:02 +03:00
|
|
|
uc_hook count_hook;
|
2015-08-21 10:04:50 +03:00
|
|
|
|
|
|
|
size_t emu_counter; // current counter of uc_emu_start()
|
2021-10-29 13:44:49 +03:00
|
|
|
size_t emu_count; // save counter of uc_emu_start()
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2020-05-25 11:22:28 +03:00
|
|
|
int size_recur_mem; // size for mem access when in a recursive call
|
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
bool init_tcg; // already initialized local TCGv variables?
|
|
|
|
bool stop_request; // request to immediately stop emulation - for
|
|
|
|
// uc_emu_stop()
|
|
|
|
bool quit_request; // request to quit the current TB, but continue to
|
|
|
|
// emulate - for uc_mem_protect()
|
|
|
|
bool emulation_done; // emulation is done by uc_emu_start()
|
|
|
|
bool timed_out; // emulation timed out, that can retrieve via
|
|
|
|
// uc_query(UC_QUERY_TIMEOUT)
|
|
|
|
QemuThread timer; // timer for emulation timeout
|
|
|
|
uint64_t timeout; // timeout for uc_emu_start()
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
uint64_t invalid_addr; // invalid address to be accessed
|
|
|
|
int invalid_error; // invalid memory code: 1 = READ, 2 = WRITE, 3 = CODE
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-11-01 16:00:43 +03:00
|
|
|
int use_exits;
|
2022-03-07 01:40:34 +03:00
|
|
|
uint64_t exits[UC_MAX_NESTED_LEVEL]; // When multiple exits is not enabled.
|
|
|
|
GTree *ctl_exits; // addresses where emulation stops (@until param of
|
2022-03-07 01:40:50 +03:00
|
|
|
// uc_emu_start()) Also see UC_CTL_USE_EXITS for more
|
|
|
|
// details.
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-10-29 13:44:49 +03:00
|
|
|
int thumb; // thumb mode for ARM
|
2015-08-28 09:19:32 +03:00
|
|
|
MemoryRegion **mapped_blocks;
|
2015-08-26 07:52:18 +03:00
|
|
|
uint32_t mapped_block_count;
|
2015-09-04 10:40:47 +03:00
|
|
|
uint32_t mapped_block_cache_index;
|
2021-10-29 13:44:49 +03:00
|
|
|
void *qemu_thread_data; // to support cross compile to Windows
|
|
|
|
// (qemu-thread-win32.c)
|
2015-08-31 11:00:44 +03:00
|
|
|
uint32_t target_page_size;
|
|
|
|
uint32_t target_page_align;
|
2021-10-03 17:14:44 +03:00
|
|
|
uint64_t qemu_host_page_size;
|
|
|
|
uint64_t qemu_real_host_page_size;
|
|
|
|
int qemu_icache_linesize;
|
|
|
|
/* ARCH_REGS_STORAGE_SIZE */
|
|
|
|
int cpu_context_size;
|
2021-10-29 13:44:49 +03:00
|
|
|
uint64_t next_pc; // save next PC for some special cases
|
|
|
|
bool hook_insert; // insert new hook at begin of the hook list (append by
|
|
|
|
// default)
|
|
|
|
bool first_tb; // is this the first Translation-Block ever generated since
|
|
|
|
// uc_emu_start()?
|
2021-11-16 23:18:27 +03:00
|
|
|
bool no_exit_request; // Disable check_exit_request temporarily. A
|
2021-10-29 13:44:49 +03:00
|
|
|
// workaround to treat the IT block as a whole block.
|
2021-11-16 23:18:27 +03:00
|
|
|
bool init_done; // Whether the initialization is done.
|
2021-11-16 23:07:03 +03:00
|
|
|
|
|
|
|
sigjmp_buf jmp_bufs[UC_MAX_NESTED_LEVEL]; // To support nested uc_emu_start
|
|
|
|
int nested_level; // Current nested_level
|
2021-11-23 02:25:55 +03:00
|
|
|
|
2021-11-24 01:23:02 +03:00
|
|
|
struct TranslationBlock *last_tb; // The real last tb we executed.
|
2022-01-16 00:11:14 +03:00
|
|
|
|
|
|
|
FlatView *empty_view; // Static function variable moved from flatviews_init
|
2015-08-21 10:04:50 +03:00
|
|
|
};
|
|
|
|
|
2016-10-11 00:04:51 +03:00
|
|
|
// Metadata stub for the variable-size cpu context used with uc_context_*()
|
|
|
|
struct uc_context {
|
2021-10-29 13:44:49 +03:00
|
|
|
size_t context_size; // size of the real internal context structure
|
2021-11-16 23:18:27 +03:00
|
|
|
uc_mode mode; // the mode of this context
|
|
|
|
uc_arch arch; // the arch of this context
|
|
|
|
char data[0]; // context
|
2016-10-11 00:04:51 +03:00
|
|
|
};
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
// check if this address is mapped in (via uc_mem_map())
|
2021-10-29 13:44:49 +03:00
|
|
|
MemoryRegion *memory_mapping(struct uc_struct *uc, uint64_t address);
|
2015-08-21 10:04:50 +03:00
|
|
|
|
2021-11-01 02:39:36 +03:00
|
|
|
// We have to support 32bit system so we can't hold uint64_t on void*
|
|
|
|
static inline void uc_add_exit(uc_engine *uc, uint64_t addr)
|
|
|
|
{
|
|
|
|
uint64_t *new_exit = g_malloc(sizeof(uint64_t));
|
|
|
|
*new_exit = addr;
|
2022-03-07 01:40:34 +03:00
|
|
|
g_tree_insert(uc->ctl_exits, (gpointer)new_exit, (gpointer)1);
|
2021-11-01 02:39:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function has to exist since we would like to accept uint32_t or
|
|
|
|
// it's complex to achieve so.
|
|
|
|
static inline int uc_addr_is_exit(uc_engine *uc, uint64_t addr)
|
|
|
|
{
|
2022-03-07 01:40:34 +03:00
|
|
|
if (uc->use_exits) {
|
|
|
|
return g_tree_lookup(uc->ctl_exits, (gpointer)(&addr)) == (gpointer)1;
|
|
|
|
} else {
|
|
|
|
return uc->exits[uc->nested_level - 1] == addr;
|
|
|
|
}
|
2021-11-01 02:39:36 +03:00
|
|
|
}
|
|
|
|
|
2022-06-02 15:46:02 +03:00
|
|
|
typedef struct HookedRegion {
|
|
|
|
uint64_t start;
|
|
|
|
uint64_t length;
|
|
|
|
} HookedRegion;
|
|
|
|
|
|
|
|
// hooked_regions related functions
|
2022-06-02 15:46:26 +03:00
|
|
|
static inline guint hooked_regions_hash(const void *p)
|
|
|
|
{
|
|
|
|
HookedRegion *region = (HookedRegion *)p;
|
2022-06-02 15:46:02 +03:00
|
|
|
|
|
|
|
return qemu_xxhash4(region->start, region->length);
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:46:26 +03:00
|
|
|
static inline gboolean hooked_regions_equal(const void *lhs, const void *rhs)
|
|
|
|
{
|
|
|
|
HookedRegion *l = (HookedRegion *)lhs;
|
|
|
|
HookedRegion *r = (HookedRegion *)rhs;
|
2022-06-02 15:46:02 +03:00
|
|
|
|
|
|
|
return l->start == r->start && l->length == r->length;
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:46:26 +03:00
|
|
|
static inline void hooked_regions_add(struct hook *h, uint64_t start,
|
|
|
|
uint64_t length)
|
|
|
|
{
|
2022-06-02 15:46:02 +03:00
|
|
|
HookedRegion tmp;
|
|
|
|
tmp.start = start;
|
|
|
|
tmp.length = length;
|
|
|
|
|
2022-06-02 15:46:26 +03:00
|
|
|
if (!g_hash_table_lookup(h->hooked_regions, (void *)&tmp)) {
|
|
|
|
HookedRegion *r = malloc(sizeof(HookedRegion));
|
2022-06-02 15:46:02 +03:00
|
|
|
r->start = start;
|
|
|
|
r->length = length;
|
2022-06-02 15:46:26 +03:00
|
|
|
g_hash_table_insert(h->hooked_regions, (void *)r, (void *)1);
|
2022-06-02 15:46:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:46:26 +03:00
|
|
|
static inline void hooked_regions_check_single(struct list_item *cur,
|
|
|
|
uint64_t start, uint64_t length)
|
|
|
|
{
|
2022-06-02 15:46:02 +03:00
|
|
|
while (cur != NULL) {
|
|
|
|
if (HOOK_BOUND_CHECK((struct hook *)cur->data, start)) {
|
|
|
|
hooked_regions_add((struct hook *)cur->data, start, length);
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:46:26 +03:00
|
|
|
static inline void hooked_regions_check(uc_engine *uc, uint64_t start,
|
|
|
|
uint64_t length)
|
|
|
|
{
|
2022-06-02 15:46:02 +03:00
|
|
|
// Only UC_HOOK_BLOCK and UC_HOOK_CODE might be wrongle cached!
|
|
|
|
hooked_regions_check_single(uc->hook[UC_HOOK_CODE_IDX].head, start, length);
|
2022-06-02 15:46:26 +03:00
|
|
|
hooked_regions_check_single(uc->hook[UC_HOOK_BLOCK_IDX].head, start,
|
|
|
|
length);
|
2022-06-02 15:46:02 +03:00
|
|
|
}
|
|
|
|
|
2022-01-18 21:35:43 +03:00
|
|
|
#ifdef UNICORN_TRACER
|
|
|
|
#define UC_TRACE_START(loc) trace_start(get_tracer(), loc)
|
|
|
|
#define UC_TRACE_END(loc, fmt, ...) \
|
|
|
|
trace_end(get_tracer(), loc, fmt, __VA_ARGS__)
|
|
|
|
|
|
|
|
typedef enum trace_loc {
|
|
|
|
UC_TRACE_TB_EXEC = 0,
|
|
|
|
UC_TRACE_TB_TRANS,
|
|
|
|
UC_TRACER_MAX
|
|
|
|
} trace_loc;
|
|
|
|
|
|
|
|
typedef struct uc_tracer {
|
|
|
|
int64_t starts[UC_TRACER_MAX];
|
|
|
|
} uc_tracer;
|
|
|
|
|
|
|
|
uc_tracer *get_tracer();
|
|
|
|
|
|
|
|
void trace_start(uc_tracer *tracer, trace_loc loc);
|
|
|
|
|
|
|
|
void trace_end(uc_tracer *tracer, trace_loc loc, const char *fmt, ...);
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define UC_TRACE_START(loc)
|
|
|
|
#define UC_TRACE_END(loc, fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
2015-08-21 10:04:50 +03:00
|
|
|
#endif
|
2017-02-24 16:37:19 +03:00
|
|
|
/* vim: set ts=4 noet: */
|