Skip to contentSkip to conversation

DMA understanding

DISCUSSION Abhinav Srivastava 3 participants 6 messages Jun 28, 2010 – Jul 2, 2010

lore
Share & tools
lore
#0 lore raw

Hi there,

I am trying to understand how an HVM guest domain performs its DMA operations, and how this DMA operations are intercepted by the Xen. I wanted to understand both the code path with and without Vt-d support (for intel processors). On looking inside the Xen code, I found that iommu code is inside the vmx/vtd/ directory only. By seeing the code, my understanding is that when Vt-d is enabled, iommu.c and dmar.c inside the vtd directory is the place to look for DMA operations. However, I do not understand which code path inside the hypervisor is getting used in case of Vt-d is disabled? How does Xen intercept guest DMA operations in this case? I am using Xen 3.3 version for my project (I admit that it is very old version).

Any help would be appreciated.

Thanks,
Abhinav

#1 lore raw
Quoted 4 lines · Abhinav Srivastava

Hi there,

I am trying to understand how an HVM guest domain performs its DMA operations, and how this DMA operations are intercepted by the Xen. I wanted to understand both the code path with and without Vt-d support (for intel processors). On looking inside the Xen code, I found that iommu code is inside the vmx/vtd/ directory only. By seeing the code, my understanding is that when Vt-d is enabled, iommu.c and dmar.c inside the vtd directory is the place to look for DMA operations. However, I do not understand which code path inside the hypervisor is getting used in case of Vt-d is disabled? How does Xen intercept guest DMA operations in this case? I am using Xen 3.3 version for my project (I admit that it is very old version).

Lets start without the Intel VT-d or AMD Vi in the picture.

When the QEMU boots up an HVM guest, it emulates everything the guest sees or does. Which means that when the guest decides to use the IDE controller to do DMA operations, QEMU decodes that operation (look in hw/ide.c, search for 'WIN_READDMA') and it follows it through by setting up a callback mechanism that ends up fetching the data from wherever the guest disk and then triggering an interrupt so that the guest noticies that the DMA finished.

So in essence the hypervisor does not deal with guest DMA at all.

When you insert an Intel VT-d or AMD Vi chipset you have the option of passing in a native PCI device to the guest. If you don't pass in a PCI device then you are still using the old mechanism.

#2 lore raw

Hi Konrad,

Thanks for your reply. You reply was very helpful in understanding the
DMA mechanism. The goal of my project is to intercept all DMA requests issued by guest HVM domains and check for the memory regions (guest physical address) mentioned in those requests. This will help detect malicious DMA writes specified by malicious drivers. I am trying to achieve this without VT-d support on intel processors.

I have some follow up questions:

1. When a guest HVM domain requests DMA operations, it specifies guest physical addresses. Who converts guest physical to host physical addresses? Does this conversion happen in Dom0 or hypervisor? Which code path should I be looking at?

2. I am looking at the place where I can hook into so that I could intercept all DMA requests issued by the HVM guest and verify the addresses? Is there any place where all DMA requests come and then routed to specific devices in QEMU emulation code? If I could hook at the common place, it would be easier to intercept rather putting the check in each device specific files.

I really appreciate for your time.

Thanks,
Abhinav

Quoted 57 lines · Konrad Rzeszutek Wilk

From: Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: [Xen-devel] DMA understanding
To: "Abhinav Srivastava" <[email protected]>
Cc: [email protected]
Date: Wednesday, 30 June, 2010, 9:32 PM

Quoted 4 lines · Abhinav Srivastava

Hi there,

I am trying to understand how an HVM guest domain

performs its DMA operations, and how this DMA operations are intercepted by the Xen. I wanted to understand both the code path with and without Vt-d support (for intel processors). On looking inside the Xen code, I found that iommu code is inside the vmx/vtd/ directory only. By seeing the code, my understanding is that when Vt-d is enabled, iommu.c and dmar.c inside the vtd directory is the place to look for DMA operations. However, I do not understand which code path inside the hypervisor is getting used in case of Vt-d is disabled?  How does Xen intercept guest DMA operations in this case? I am using Xen 3.3 version for my project (I admit that it is very old version).

Lets start without the Intel VT-d or AMD Vi in the picture.

When the QEMU boots up an HVM guest, it emulates everything the guest
sees or does. Which means that when the guest decides to use the
IDE controller to do DMA operations, QEMU decodes that operation
(look in hw/ide.c, search for 'WIN_READDMA') and it follows it
through by setting up a callback mechanism that ends up fetching
the data from wherever the guest disk and then triggering an interrupt
so that the guest noticies that the DMA finished.

So in essence the hypervisor does not deal with guest DMA at all.

When you insert an Intel VT-d or AMD Vi chipset you have the option
of passing in a native PCI device to the guest. If you don't pass
in a PCI device then you are still using the old mechanism.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw

Re: QEMU and hypervisor DMA understanding. Want to track DMA operations on QEMU devices.

Quoted 9 lines · Abhinav Srivastava

Hi Konrad,

Thanks for your reply. You reply was very helpful in understanding the
DMA mechanism. The goal of my project is to intercept all DMA requests issued by guest HVM domains and check for the memory regions (guest physical address) mentioned in those requests. This will help detect malicious DMA writes specified by malicious drivers. I am trying to achieve this without VT-d support on intel processors.

I have some follow up questions:

1. When a guest HVM domain requests DMA operations, it specifies guest physical addresses. Who converts guest physical to host physical addresses? Does this conversion happen in Dom0 or hypervisor? Which code path should I be looking at?

Hypervisor. Shadow page table. George might have a document tucked away explaining how the shadow page table works.

Quoted 3 lines

2. I am looking at the place where I can hook into so that I could intercept all DMA requests issued by the HVM guest and verify the addresses? Is there any place where all DMA requests come and then routed to specific devices in QEMU emulation code? If I could hook at the common place, it would be easier to intercept rather putting the check in each device specific files.

Ian might know this better..

Quoted 67 lines

I really appreciate for your time.

Thanks,
Abhinav

Quoted 57 lines · Konrad Rzeszutek Wilk

From: Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: [Xen-devel] DMA understanding
To: "Abhinav Srivastava" <[email protected]>
Cc: [email protected]
Date: Wednesday, 30 June, 2010, 9:32 PM

Quoted 4 lines · Abhinav Srivastava

Hi there,

I am trying to understand how an HVM guest domain

performs its DMA operations, and how this DMA operations are intercepted by the Xen. I wanted to understand both the code path with and without Vt-d support (for intel processors). On looking inside the Xen code, I found that iommu code is inside the vmx/vtd/ directory only. By seeing the code, my understanding is that when Vt-d is enabled, iommu.c and dmar.c inside the vtd directory is the place to look for DMA operations. However, I do not understand which code path inside the hypervisor is getting used in case of Vt-d is disabled?  How does Xen intercept guest DMA operations in this case? I am using Xen 3.3 version for my project (I admit that it is very old version).

Lets start without the Intel VT-d or AMD Vi in the picture.

When the QEMU boots up an HVM guest, it emulates everything the guest
sees or does. Which means that when the guest decides to use the
IDE controller to do DMA operations, QEMU decodes that operation
(look in hw/ide.c, search for 'WIN_READDMA') and it follows it
through by setting up a callback mechanism that ends up fetching
the data from wherever the guest disk and then triggering an interrupt
so that the guest noticies that the DMA finished.

So in essence the hypervisor does not deal with guest DMA at all.

When you insert an Intel VT-d or AMD Vi chipset you have the option
of passing in a native PCI device to the guest. If you don't pass
in a PCI device then you are still using the old mechanism.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw

Re: QEMU and hypervisor DMA understanding. Want to track DMA operations on QEMU devices.

Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 6 lines · Abhinav Srivastava

Hi Konrad,

Thanks for your reply. You reply was very helpful in understanding the
DMA mechanism. The goal of my project is to intercept all DMA requests issued by guest HVM domains and check for the memory regions (guest physical address) mentioned in those requests. This will help detect malicious DMA writes specified by malicious drivers. I am trying to achieve this without VT-d support on intel processors.

Quoted 6 lines
Quoted 3 lines

I have some follow up questions:

1. When a guest HVM domain requests DMA operations, it specifies guest physical addresses. Who converts guest physical to host physical addresses? Does this conversion happen in Dom0 or hypervisor? Which code path should I be looking at?

Hypervisor. Shadow page table. George might have a document tucked away explaining how the shadow page table works.

No need to go into shadow pagetables. Xen sets up and maintains the guest-physical-to-host-physical mapping, called the p2m table. DMA is done by qemu mapping the guest's memory and writing to it; qemu makes the mapping hypercall with a pfn and the hypervitor translates to an mfn for it.

Quoted 4 lines
Quoted 3 lines

2. I am looking at the place where I can hook into so that I could intercept all DMA requests issued by the HVM guest and verify the addresses? Is there any place where all DMA requests come and then routed to specific devices in QEMU emulation code? If I could hook at the common place, it would be easier to intercept rather putting the check in each device specific files.

You definitely want to do this in qemu rather than Xen. Wemu has central accessors for guest memory that you can hook. It used to be called cpu_physical_memory_rw(), but I haven't looked at it in a while.

Tim.

Quoted 73 lines

Ian might know this better..

Quoted 67 lines

I really appreciate for your time.

Thanks,
Abhinav

Quoted 57 lines · Konrad Rzeszutek Wilk

From: Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: [Xen-devel] DMA understanding
To: "Abhinav Srivastava" <[email protected]>
Cc: [email protected]
Date: Wednesday, 30 June, 2010, 9:32 PM

Quoted 4 lines · Abhinav Srivastava

Hi there,

I am trying to understand how an HVM guest domain

performs its DMA operations, and how this DMA operations are intercepted by the Xen. I wanted to understand both the code path with and without Vt-d support (for intel processors). On looking inside the Xen code, I found that iommu code is inside the vmx/vtd/ directory only. By seeing the code, my understanding is that when Vt-d is enabled, iommu.c and dmar.c inside the vtd directory is the place to look for DMA operations. However, I do not understand which code path inside the hypervisor is getting used in case of Vt-d is disabled?  How does Xen intercept guest DMA operations in this case? I am using Xen 3.3 version for my project (I admit that it is very old version).

Lets start without the Intel VT-d or AMD Vi in the picture.

When the QEMU boots up an HVM guest, it emulates everything the guest
sees or does. Which means that when the guest decides to use the
IDE controller to do DMA operations, QEMU decodes that operation
(look in hw/ide.c, search for 'WIN_READDMA') and it follows it
through by setting up a callback mechanism that ends up fetching
the data from wherever the guest disk and then triggering an interrupt
so that the guest noticies that the DMA finished.

So in essence the hypervisor does not deal with guest DMA at all.

When you insert an Intel VT-d or AMD Vi chipset you have the option
of passing in a native PCI device to the guest. If you don't pass
in a PCI device then you are still using the old mechanism.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#5 lore raw

Re: QEMU and hypervisor DMA understanding. Want to track DMA operations on QEMU devices.

Hi Tim and Konrad,

Thanks for your reply and pointers. As per Tim's reply, I will look into the Qemu code and try to hook where the translation happens. BTW, what is the entry point into the qemu code on the DMA code path? Is it cpu_physical_memory_rw() or some other function? And, when the term "physical memory" is used in the qemu code, it is the guest physical memory or the host physical memory?

Thanks & Regards,
Abhinav

Quoted 173 lines · Tim Deegan

From: Tim Deegan <[email protected]>
Subject: Re: [Xen-devel] QEMU and hypervisor DMA understanding. Want to track DMA operations on QEMU devices.
To: "Konrad Rzeszutek Wilk" <[email protected]>
Cc: "George Dunlap" <[email protected]>, "Ian Campbell" <[email protected]>, "[email protected]" <[email protected]>, "Abhinav Srivastava" <[email protected]>
Date: Thursday, 1 July, 2010, 8:47 PM
At 15:51 +0100 on 01 Jul

Quoted 1 line · Konrad Rzeszutek Wilk

On Thu, Jul 01, 2010 at 11:38:31AM +0530, Abhinav

Quoted 3 lines · Srivastava
Quoted 3 lines

Hi Konrad,

Thanks for your reply. You reply was very helpful

in understanding the

Quoted 1 line
Quoted 1 line

DMA mechanism. The goal of my project is to

intercept all DMA requests issued by guest HVM domains and check for the memory regions (guest physical address) mentioned in those requests. This will help detect malicious DMA writes specified by malicious drivers. I am trying

Quoted 1 line
Quoted 1 line

to achieve this without VT-d support on intel

processors.

Quoted 4 lines
Quoted 4 lines

I have some follow up questions:

1. When a guest HVM domain requests DMA

operations, it specifies guest physical addresses. Who converts guest physical to host physical addresses? Does this conversion happen in Dom0 or hypervisor? Which code path should I be looking at?

Quoted 2 lines

Hypervisor. Shadow page table. George might have a

document tucked away

Quoted 1 line

explaining how the shadow page table works.

No need to go into shadow pagetables.  Xen sets up and maintains the
guest-physical-to-host-physical mapping, called the p2m table.  DMA is
done by qemu mapping the guest's memory and writing to it; qemu makes
the mapping hypercall with a pfn and the hypervitor translates to an mfn
for it.

Quoted 2 lines
Quoted 2 lines

2. I am looking at the place where I can hook

into so that I could intercept all DMA requests issued by the HVM guest and verify the addresses? Is there any place where all DMA requests come and then routed to specific devices in QEMU emulation code? If I could hook at the common place, it would be easier to intercept rather putting the check

Quoted 2 lines
Quoted 1 line

in each device specific files.

You definitely want to do this in qemu rather than Xen.  Wemu has
central accessors for guest memory that you can hook. It used to be
called cpu_physical_memory_rw(), but I haven't looked at it in a while.

Tim.

Quoted 8 lines

Ian might know this better..

Quoted 7 lines

I really appreciate for your time.

Thanks,
Abhinav

--- On Wed, 30/6/10, Konrad Rzeszutek Wilk <[email protected]>

Quoted 9 lines · wrote:
Quoted 9 lines
Quoted 9 lines

From: Konrad Rzeszutek Wilk <[email protected]>
Subject: Re: [Xen-devel] DMA understanding
To: "Abhinav Srivastava" <[email protected]>
Cc: [email protected]
Date: Wednesday, 30 June, 2010, 9:32 PM

Quoted 1 line · Abhinav Srivastava
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Hi there,

I am trying to understand how an HVM

guest domain

Quoted 1 line
Quoted 1 line
Quoted 1 line

performs its DMA operations, and how this

DMA operations are

Quoted 1 line
Quoted 1 line
Quoted 1 line

intercepted by the Xen. I wanted to

understand both the code

Quoted 1 line
Quoted 1 line
Quoted 1 line

path with and without Vt-d support (for

intel processors).

Quoted 1 line
Quoted 1 line
Quoted 1 line

On looking inside the Xen code, I found that

iommu code is

Quoted 1 line
Quoted 1 line
Quoted 1 line

inside the vmx/vtd/ directory only. By

seeing the code, my

Quoted 1 line
Quoted 1 line
Quoted 1 line

understanding is that when Vt-d is enabled,

iommu.c and

Quoted 1 line
Quoted 1 line
Quoted 1 line

dmar.c inside the vtd directory is the place

to look for DMA

Quoted 1 line
Quoted 1 line
Quoted 1 line

operations. However, I do not understand

which code path

Quoted 1 line
Quoted 1 line
Quoted 1 line

inside the hypervisor is getting used in

case of Vt-d is

Quoted 1 line
Quoted 1 line
Quoted 1 line

disabled?  How does Xen intercept guest DMA

operations

Quoted 1 line
Quoted 1 line
Quoted 1 line

in this case? I am using Xen 3.3 version for

my project (I

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

admit that it is very old version).

Lets start without the Intel VT-d or AMD Vi

in the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

picture.

When the QEMU boots up an HVM guest, it

emulates everything

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the guest
sees or does. Which means that when the

guest decides to

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

use the
IDE controller to do DMA operations, QEMU

decodes that

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

operation
(look in hw/ide.c, search for 'WIN_READDMA')

and it follows

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

it
through by setting up a callback mechanism

that ends up

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

fetching
the data from wherever the guest disk and

then triggering

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

an interrupt
so that the guest noticies that the DMA

finished.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So in essence the hypervisor does not deal

with guest DMA

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

at all.

When you insert an Intel VT-d or AMD Vi

chipset you have

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the option
of passing in a native PCI device to the

guest. If you

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

don't pass
in a PCI device then you are still using the

old

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

mechanism.

_______________________________________________

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Signature · 8 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel