Skip to contentSkip to conversation

Reversing the DMA handle

DISCUSSION Pete Zaitcev 3 participants 6 messages Jan 10, 2007 – Jan 11, 2007 USB

lore
Share & tools
lore
#0 lore raw

Dear Xen developers:

I maintain a component in Linux kernel, called usbmon. It provides for snooping of USB traffic, analoguously to tcpdump. Due to pequliarities of USB API in Linux, usbmon has to take a DMA address (coming from dma_alloc_coherent() typically), find the corresponding PFN and call pkmap()/memcpy()/pkunmap().

My question is: how to find PFN under Xen if DMA address is known, on x86_64 and i386?

I see some functions which may be useful (e.g. mfn_to_pfn()), but what I need the most is some tribal knowledge about what is the proper way to do it. This includes things like working in both dom0 and domU, not faulting in machine_to_phys_mapping[] while in hot path, and so on.

Thank you,
-- Pete

#1 lore raw
Quoted 7 lines · Pete Zaitcev

Dear Xen developers:

I maintain a component in Linux kernel, called usbmon. It provides for snooping of USB traffic, analoguously to tcpdump. Due to pequliarities of USB API in Linux, usbmon has to take a DMA address (coming from dma_alloc_coherent() typically), find the corresponding PFN and call pkmap()/memcpy()/pkunmap().

Hi Pete,

Regardless of Xen, how is this supposed to work in the presence of IOMMUs?

Cheers,
Muli

#2 lore raw
Quoted 10 lines · Muli Ben-Yehuda
Quoted 5 lines · Pete Zaitcev

I maintain a component in Linux kernel, called usbmon. It provides for snooping of USB traffic, analoguously to tcpdump. Due to pequliarities of USB API in Linux, usbmon has to take a DMA address (coming from dma_alloc_coherent() typically), find the corresponding PFN and call pkmap()/memcpy()/pkunmap().

Regardless of Xen, how is this supposed to work in the presence of IOMMUs?

It cannot, generically speaking. So, no usbmon on Calgari. But for a specific IOMMU I can place necessary hooks. I am looking at SWIOTLB currently. And don't forget that it only works on cache-coherent architectures, so no SPARC support ever.

-- Pete

#3 lore raw
In reply to Pete Zaitcev
Quoted 8 lines · Pete Zaitcev

I maintain a component in Linux kernel, called usbmon. It provides for snooping of USB traffic, analoguously to tcpdump. Due to pequliarities of USB API in Linux, usbmon has to take a DMA address (coming from dma_alloc_coherent() typically), find the corresponding PFN and call pkmap()/memcpy()/pkunmap().

My question is: how to find PFN under Xen if DMA address is known, on x86_64 and i386?

Use mfn_to_local_pfn() which is safe to call from any context. This will return a !pfn_valid() value if the given MFN actually belongs to a foreign domain (e.g., we are issuing a DMA on behalf of another guest via netback or blkback) or if the MFN is not RAM. Otherwise it will return the local PFN. Of course you may then want to do further translation if that happens to be in the swiotlb aperture.

-- Keir

#4 lore raw
Quoted 8 lines · Keir Fraser
Quoted 2 lines · Pete Zaitcev

My question is: how to find PFN under Xen if DMA address is known, on x86_64 and i386?

Use mfn_to_local_pfn() which is safe to call from any context. [...] Of course you may then want to do further translation if that happens to be in the swiotlb aperture.

Keir, thanks a lot, that worked perfectly.

The SWIOTLB is something I can't understand still. The code sure looks like returning physical addresses to be used as DMA handles, just like any usual routines do:

void *
swiotlb_alloc_coherent(struct device *hwdev, size_t size,
dma_addr_t *dma_handle, gfp_t flags)
{
....

	ret = (void *)__get_free_pages(flags, order);
	dev_addr = virt_to_phys(ret);
	*dma_handle = dev_addr;

And the code in pci-dma.c does:

void *
dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,

		   gfp_t gfp)
	memory = dma_alloc_pages(dev, gfp, get_order(size));
			*dma_handle = virt_to_bus(memory);

The virt_to_bus and virt_to_phys are the same on x86_64.

So this is a kind of a mystery, but I'll think about it and maybe ask at linux-kernel.

Thanks,
-- Pete

drivers/usb/mon/mon_dma.c +8 -4
diff -urp -X dontdiff linux-2.6.17-1.2647.fc6/drivers/usb/mon/mon_dma.c linux-2.6.17-1.2647.fc6.z1/drivers/usb/mon/mon_dma.c
@@ -19,21 +19,25 @@
 #if defined(__i386__) || defined(__x86_64__) /* CONFIG_ARCH_I386 doesn't exit */
 #define MON_HAS_UNMAP 1
 
-#define phys_to_page(phys)	pfn_to_page((phys) >> PAGE_SHIFT)
-
 char mon_dmapeek(unsigned char *dst, dma_addr_t dma_addr, int len)
 {
 	struct page *pg;
 	unsigned long flags;
 	unsigned char *map;
 	unsigned char *ptr;
+	unsigned long mfn;
 
 	/*
 	 * On i386, a DMA handle is the "physical" address of a page.
 	 * In other words, the bus address is equal to physical address.
-	 * There is no IOMMU.
+	 * But sometimes IOMMUs are present (Calgary, AMD's x86_64, SWIOTLB).
+	 * Also, Xen introduces "machine" address, which is used for DMA.
+	 * We handle Xen, but not IOMMUs yet.
 	 */
-	pg = phys_to_page(dma_addr);
+	mfn = mfn_to_local_pfn(dma_addr >> PAGE_SHIFT);
+	if (!pfn_valid(mfn))
+		return 'X';
+	pg = pfn_to_page(mfn);
 
 	/*
 	 * We are called from hardware IRQs in case of callbacks.
#5 lore raw
Quoted 3 lines · Pete Zaitcev

The SWIOTLB is something I can't understand still. The code sure looks like returning physical addresses to be used as DMA handles, just like any usual routines do:

Yes, but suitably low physical addresses which are kept in sync with the original high addresses by occasional memcpy().

The question is: if you have a 'DMA address' that belongs to the swiotlb, do you mind if the MFN->PFN translation yields you an address inside the swiotlb aperture rather than the original driver address? Since both addresses are valid local memory, and both should contain a reasonably up-to-date copy of the data you are wanting to sniff, I guess maybe you don't actually care.

-- Keir