From b27e9879321606997851b86890e2c6b30a909da8 Mon Sep 17 00:00:00 2001 From: Chris Eagle Date: Mon, 31 Aug 2015 01:00:44 -0700 Subject: [PATCH] Add target_page_size member to uc_struct to track TARGET_PAGE_SIZE --- include/uc_priv.h | 3 +++ qemu/memory.c | 2 +- qemu/unicorn_common.h | 3 +++ uc.c | 28 ++++++++++++---------------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/uc_priv.h b/include/uc_priv.h index 695b5fc6..aff7a7d7 100644 --- a/include/uc_priv.h +++ b/include/uc_priv.h @@ -175,6 +175,9 @@ struct uc_struct { bool block_full; MemoryRegion **mapped_blocks; uint32_t mapped_block_count; + + uint32_t target_page_size; + uint32_t target_page_align; }; #include "qemu_macro.h" diff --git a/qemu/memory.c b/qemu/memory.c index f5b70f35..e04d59b7 100644 --- a/qemu/memory.c +++ b/qemu/memory.c @@ -49,7 +49,7 @@ void memory_unmap(struct uc_struct *uc, MemoryRegion *mr) { target_ulong addr; //make sure all pages associated with the MemoryRegion are flushed - for (addr = mr->addr; addr < mr->end; addr += TARGET_PAGE_SIZE) { + for (addr = mr->addr; addr < mr->end; addr += uc->target_page_size) { tlb_flush_page(uc->current_cpu, addr); } mr->enabled = false; diff --git a/qemu/unicorn_common.h b/qemu/unicorn_common.h index 23ef0acb..5ba74fac 100644 --- a/qemu/unicorn_common.h +++ b/qemu/unicorn_common.h @@ -76,6 +76,9 @@ static inline void uc_common_init(struct uc_struct* uc) uc->memory_unmap = memory_unmap; uc->readonly_mem = memory_region_set_readonly; + uc->target_page_size = TARGET_PAGE_SIZE; + uc->target_page_align = TARGET_PAGE_SIZE - 1; + if (!uc->release) uc->release = release_common; } diff --git a/uc.c b/uc.c index ad2a7325..2dddb39c 100644 --- a/uc.c +++ b/uc.c @@ -31,10 +31,6 @@ #include "qemu/include/hw/boards.h" -//keep this a power of two! -#define UC_PAGE_SIZE 0x1000 -#define UC_ALIGN_MASK (UC_PAGE_SIZE - 1) - static uint8_t *copy_region(uch uc, MemoryRegion *mr); static bool split_region(uch handle, MemoryRegion *mr, uint64_t address, size_t size, bool do_delete); @@ -629,12 +625,12 @@ uc_err uc_mem_map(uch handle, uint64_t address, size_t size, uint32_t perms) // invalid memory mapping return UC_ERR_MAP; - // address must be aligned to UC_PAGE_SIZE - if ((address & UC_ALIGN_MASK) != 0) + // address must be aligned to uc->target_page_size + if ((address & uc->target_page_align) != 0) return UC_ERR_MAP; - // size must be multiple of UC_PAGE_SIZE - if ((size & UC_ALIGN_MASK) != 0) + // size must be multiple of uc->target_page_size + if ((size & uc->target_page_align) != 0) return UC_ERR_MAP; // check for only valid permissions @@ -773,12 +769,12 @@ uc_err uc_mem_protect(uch handle, uint64_t address, size_t size, uint32_t perms) // invalid memory mapping return UC_ERR_MAP; - // address must be aligned to UC_PAGE_SIZE - if ((address & UC_ALIGN_MASK) != 0) + // address must be aligned to uc->target_page_size + if ((address & uc->target_page_align) != 0) return UC_ERR_MAP; - // size must be multiple of UC_PAGE_SIZE - if ((size & UC_ALIGN_MASK) != 0) + // size must be multiple of uc->target_page_size + if ((size & uc->target_page_align) != 0) return UC_ERR_MAP; // check for only valid permissions @@ -833,12 +829,12 @@ uc_err uc_mem_unmap(uch handle, uint64_t address, size_t size) // nothing to unmap return UC_ERR_OK; - // address must be aligned to UC_PAGE_SIZE - if ((address & UC_ALIGN_MASK) != 0) + // address must be aligned to uc->target_page_size + if ((address & uc->target_page_align) != 0) return UC_ERR_MAP; - // size must be multiple of UC_PAGE_SIZE - if ((size & UC_ALIGN_MASK) != 0) + // size must be multiple of uc->target_page_size + if ((size & uc->target_page_align) != 0) return UC_ERR_MAP; //check that user's entire requested block is mapped