Re: [Qemu-devel] How to access guest memory from qemu device internal

2014-11-23 Thread Kaiyuan

> -Origin email-
> From: Peter Maydell 
> Sent Time: Monday, November 24, 2014
> To: Kaiyuan 
> Cc: Greg Kurz , qemu-devel 
> Subject: Re: [Qemu-devel] How to access guest memory from qemu device internal
> 
> On 23 November 2014 at 13:18, Kaiyuan  wrote:
> > Thanks for your explanation about guest physical/virtual
> > address and host physical/virtual address, and I think I
> > asked a wrong question. Is there a function that translates
> > guest Physical address to host Virtual address so that I
> > can access guest space by a host pointer?
> 
> You can use cpu_physical_memory_map() and _unmap()
> for this kind of thing. (Make sure you unmap afterwards.)
> 
> -- PMM
Thank you very much for your help!Kaiyuan Liang




Re: [Qemu-devel] How to access guest memory from qemu device internal

2014-11-23 Thread Peter Maydell
On 23 November 2014 at 13:18, Kaiyuan  wrote:
> Thanks for your explanation about guest physical/virtual
> address and host physical/virtual address, and I think I
> asked a wrong question. Is there a function that translates
> guest Physical address to host Virtual address so that I
> can access guest space by a host pointer?

You can use cpu_physical_memory_map() and _unmap()
for this kind of thing. (Make sure you unmap afterwards.)

-- PMM



Re: [Qemu-devel] How to access guest memory from qemu device internal

2014-11-23 Thread Kaiyuan

> -Origin email-
> From: Peter Maydell 
> Sent Time: Friday, November 21, 2014
> To: Kaiyuan 
> Cc: Greg Kurz , qemu-devel 
> Subject: Re: [Qemu-devel] Fw:Re:Re: How to access guest memory from qemu 
> device internal
> 
> On 21 November 2014 09:17, Kaiyuan  wrote:
> > Thanks, Greg. It's useful to me. cpu_physical_memory_read()
> > looks like a wrapper of translating guest addr to host addr
> > Is there a function that translates guest address to host
> > physical address?
> 
> No, QEMU never deals with host physical addresses at all.
> In general there are three address types that might be
> in use:
>  * guest virtual address
>  * guest physical address
>  * host virtual address (only relevant if talking to RAM,
>  rather than an emulated device)
> 
> For devices, the guest within the VM should deal with
> converting a virtual address to a guest physical address,
> and should write guest physical addresses to the device.
> [If you think about real hardware, where the device is
> separate from the CPU, there's no way the device can know
> about guest virtual addresses, which are handled by the
> CPU's built in MMU.]
> 
> So the function Greg suggests is the one you want.
> 
> -- PMM
Thanks for your explanation about guest physical/virtual address and host 
physical/virtual address, and I think I asked a wrong question. Is there a 
function that translates guest Physical address to host Virtual address so that 
I can access guest space by a host pointer?




Re: [Qemu-devel] How to access guest memory from qemu device internal

2014-11-20 Thread Greg Kurz
On Thu, 20 Nov 2014 21:25:18 +0800 (GMT+08:00)
Kaiyuan  wrote:
> Hello, all
>  
> I added a custom device to qemu. This device is attached to sysbus by mmio 
> and has an address register in which device should access the guest memory 
> the register point to.
> I write a bare-metal program that pass an address like 0x1234ABCD to this 
> address register. Inside qemu device code I added, if device reads value from 
> register and directly accesses this value of 0x1234ABCD, it will access host 
> memory 0x1234ABCD rather than guest  memory 0x1234ABCD.
> Does qemu provide some functions that allow device to access guest memory 
> address?
> 

Have a look at *_phys* functions in the  QEMU header file 
(cpu_physical_memory_read() for
exemple).

> Thanks,
> Kaiyuan Liang
> 

--
G