unicorn-engine / unicorn

Unicorn CPU emulator framework (ARM, AArch64, M68K, Mips, Sparc, PowerPC, RiscV, S390x, TriCore, X86)
http://www.unicorn-engine.org
GNU General Public License v2.0
7.65k stars 1.35k forks source link

Memory aliasing #1103

Closed mkettn closed 3 years ago

mkettn commented 5 years ago

On some microcontrollers memory is aliased. e.g. for arm cortex m address 0x0 is an alias to 0x08000000. is this possible to do in unicorn? AFAIK Qemu can alias memory regions.

gerph commented 5 years ago

I've never looked at doing this myself (although I may need to in the future), but I suspect that the easiest way to do this is by allocating the memory yourself from the same pointer.

That is, instead of registering the memory with uc_mem_map twice, you register it with mem_map_ptr.

That is, instead of doing this:

uc_mem_map(uc, 0x0, ram_size, permissions);
uc_mem_map(uc, 0x08000000, ram_size, permissions);

Which would give you two distinct areas with different mappings, you instead do something like this:

void *ptr = malloc(ram_size);
uc_mem_map_ptr(uc, 0x0, ram_size, permissions, ptr);
uc_mem_map_ptr(uc, 0x08000000, ram_size, permissions, ptr);

Which will give you two regions which map to the same memory area. How this is dealt with through the cache is left as an exercise for the reader.