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.33k stars 1.31k forks source link

Read/Write hook without memory allocation #1878

Closed iscle closed 10 months ago

iscle commented 10 months ago

Hi!

First of all, I'm not sure how QEMU handles this, but I'll explain my thoughts.

So, I'm using unicorn engine to implement an entire machine (iPod Classic, in this case). I've been developing the qemu port side-by-side with the unicorn port (because compiling QEMU for android is a pain in the ass).

In QEMU, when I want to add a peripheral that is memory-mapped I just make calls to memory_region_init_io and to sysbus_init_mmio, and then all accesses to those memory regions get redirected to the hook functions, where I can read and write my internal structure. When doing this, I don't think QEMU allocates a memory region for the peripheral size (for example 0x10000), but only calls the function when it needs the data and uses a single variable for it (this is all my thoughts, as I haven't been able to find something that contradicts this).

However, in unicorn it looks like you 100% need to allocate system memory for that peripheral, and then the callbacks get executed but the data is still read from the allocated system memory. Of course, in case of a read hook, i can use uc_mem_write to fix this issue.

In case QEMU does in fact allocate system memory, the issue can be closed :) In case it doesn't, can unicorn be modified so that we don't have to allocate huge amounts of useless memory for peripherals?

I hope I made it clear enough. Feel free to ask for any details!

Regards, Iscle

wtdcode commented 10 months ago

Yes you are correct and the thing you are looking for is uc_mmio_map

iscle commented 10 months ago

That was exactly it! Thanks for the reply :) I'm closing the issue.