Initialize data memory in user space emulation
Hi all,
I need a way to manipulate data saved in a reserved memory region in linux user space emulation. I found the -B option very promising. Documentation says it is useful when the address region required by guest applications is reserved on the host.
But how can I initialize that memory with data?
Thanks,
Libo Zhou
Quoted 6 lines · Libo Zhou
Hi all,
I need a way to manipulate data saved in a reserved memory region in linux user space emulation. I found the -B option very promising. Documentation says it is useful when the address region required by guest applications is reserved on the host.
But how can I initialize that memory with data?
Hi -- this isn't really what the -B option does. What -B does is set the 'guest base', which is the offset between what the guest sees as its address 0 and what the host's address is. So if you use -B to set the guest_base to 0x10000000, then when the guest tries to mmap() a file to address 0x20000000 then we will put the file at host address 0x30000000 (the address the guest asks for plus the guest_base).
There is no way at all for the guest to access any data below the guest_base address. The reason this option exists is in case the default value QEMU chooses for guest_base would result in the guest trying to mmap files in places that are already used by the host QEMU binary or libraries. It's almost never an option users need to set.
I'm not really sure what you mean by "a reserved memory region", so it's a bit hard to say what might be the best way to do what you want to do.
thanks
-- PMM
Quoted 1 line · Libo Zhou
I need to emulate a Digital Signal Processor. The DSP essentially has only basic MIPS ISA, and it manipulates the data stored in a "data memory". I can run an ELF with user space emulation, what I need to additionally do is to initialize the "data memory" first, then run an ELF that manipulates the data in the "data memory", and finally see if the resulting data are correct or not.
QEMU doesn't really support doing that kind of thing, because actual Linux binaries don't execute in an environment like that. You could probably hack QEMU to mmap a file into the guest's memory before we start to run the userspace process, but there's nothing that will do what you want out of the box.
thanks
-- PMM
Hi Peter,
Thanks for your explanation. Please let me add more details about what I need to do.
I need to emulate a Digital Signal Processor. The DSP essentially has only basic MIPS ISA, and it manipulates the data stored in a "data memory". I can run an ELF with user space emulation, what I need to additionally do is to initialize the "data memory" first, then run an ELF that manipulates the data in the "data memory", and finally see if the resulting data are correct or not.
Do you have any suggestions?
Cheers,
Libo Zhou
------------------ Original ------------------
From: "Peter Maydell";<[email protected]>; Send time: Friday, Sep 20, 2019 6:36 PM
To: "Libo Zhou"<[email protected]>;
Cc: "qemu-devel"<[email protected]>;
Subject: Re: Initialize data memory in user space emulation
Quoted 6 lines · Libo Zhou
Hi all,
I need a way to manipulate data saved in a reserved memory region in linux user space emulation. I found the -B option very promising. Documentation says it is useful when the address region required by guest applications is reserved on the host.
But how can I initialize that memory with data?
Hi -- this isn't really what the -B option does. What -B does is set the 'guest base', which is the offset between what the guest sees as its address 0 and what the host's address is. So if you use -B to set the guest_base to 0x10000000, then when the guest tries to mmap() a file to address 0x20000000 then we will put the file at host address 0x30000000 (the address the guest asks for plus the guest_base).
There is no way at all for the guest to access any data below the guest_base address. The reason this option exists is in case the default value QEMU chooses for guest_base would result in the guest trying to mmap files in places that are already used by the host QEMU binary or libraries. It's almost never an option users need to set.
I'm not really sure what you mean by "a reserved memory region", so it's a bit hard to say what might be the best way to do what you want to do.
thanks
-- PMM
I actually thought about hacking it to mmap from file into guest memory before running ELF. In include/exec/memory.h, the function memory_region_init_ram_from_file sounds like what I need. But the entire memory.h source file is wrapped by #ifndef CONFIG_USER_ONLY, I cannot use that API in user space emulation, right?
------------------ Original ------------------
From: "Peter Maydell";<[email protected]>; Send time: Friday, Sep 20, 2019 10:55 PM
To: "Libo Zhou"<[email protected]>;
Cc: "qemu-devel"<[email protected]>;
Subject: Re: Initialize data memory in user space emulation
Quoted 1 line · Libo Zhou
I need to emulate a Digital Signal Processor. The DSP essentially has only basic MIPS ISA, and it manipulates the data stored in a "data memory". I can run an ELF with user space emulation, what I need to additionally do is to initialize the "data memory" first, then run an ELF that manipulates the data in the "data memory", and finally see if the resulting data are correct or not.
QEMU doesn't really support doing that kind of thing, because actual Linux binaries don't execute in an environment like that. You could probably hack QEMU to mmap a file into the guest's memory before we start to run the userspace process, but there's nothing that will do what you want out of the box.
thanks
-- PMM
Quoted 2 lines · Libo Zhou
I actually thought about hacking it to mmap from file into guest memory before running ELF. In
include/exec/memory.h, the functionmemory_region_init_ram_from_filesounds like what I need. But the entirememory.hsource file is wrapped by #ifndefCONFIG_USER_ONLY, I cannot use that API in user space emulation, right?
Correct, you don't want to use that. You want to put some code in linux-user/main.c, probably just after the call to signal_init(), which opens the host file and then calls target_mmap() to mmap it into the host.
If you happen to have the source for the guest program, you could also just make it do the open and mmap() itself as the first thing it does in main() -- this is basically equivalent. Or if the guest program is dynamically linked then you can use QEMU's -E option to set the LD_PRELOAD environment variable for the guest to make it load a guest shared library that has a constructor function that does the open/mmap before the guest main() gets control.
thanks
-- PMM
Thanks for your insightful input. I will come back to this thread after I tried all the options.
Cheers,
Libo Zhou
------------------ Original ------------------
From: "Peter Maydell";<[email protected]>; Send time: Saturday, Sep 21, 2019 0:08 AM
To: "Libo Zhou"<[email protected]>;
Cc: "qemu-devel"<[email protected]>;
Subject: Re: Initialize data memory in user space emulation
Quoted 2 lines · Libo Zhou
I actually thought about hacking it to mmap from file into guest memory before running ELF. In
include/exec/memory.h, the functionmemory_region_init_ram_from_filesounds like what I need. But the entirememory.hsource file is wrapped by #ifndefCONFIG_USER_ONLY, I cannot use that API in user space emulation, right?
Correct, you don't want to use that. You want to put some code in linux-user/main.c, probably just after the call to signal_init(), which opens the host file and then calls target_mmap() to mmap it into the host.
If you happen to have the source for the guest program, you could also just make it do the open and mmap() itself as the first thing it does in main() -- this is basically equivalent. Or if the guest program is dynamically linked then you can use QEMU's -E option to set the LD_PRELOAD environment variable for the guest to make it load a guest shared library that has a constructor function that does the open/mmap before the guest main() gets control.
thanks
-- PMM