Basic Intel IOMMU DMAR emulation
PATCH 1/1 Basic Intel IOMMU DMAR emulation
This patch enables basic Intel IOMMU (VT-d) emulation for DMA remappings. Registers invalidation is supported, as well as partial queued invalidation. In addition the structure allows other IOMMU architectures to easily connect to the IOMMU indirection emulation.
In general the patch emulates on one hand the expected behavior of the registers, and performs the translation of DMA accesses according to the paging structures.
Currently only IDE device is supported (SCSI and E1000 should follow).
The emulation is done using Intel's proposed caching mode. The guest OS must use the caching mode in correct way. Unfortunately, this is not the current case with Linux. Patches were sent to Intel IOMMU owner in order to fix the pending issues.
The current patch has the following (additional) limitations:
1. DMAR tables must be loaded by command line ( -acpitable sig=DMAR,data=hw/DMAR.dat )
2. Queued invalidation does not support interrupts on wait descriptors
3. DMAR faults are not supported
4. No protection of read-only registers in the IOMMU.
For enabling registers based invalidation set in the command line:
-intel-iommu on -acpitable sig=DMAR,data=hw/DMAR.dat
For enabling queued invalidation set in the command line:
-intel-iommu queue -acpitable sig=DMAR,data=hw/DMAR.dat
--- Makefile.target | 1 + cpu-common.h | 22 ++ dma-helpers.c | 20 +- dma.h | 6 +- exec.c | 100 ++++++- hw/DMAR.dat | Bin 0 -> 28 bytes hw/ide/core.c | 31 ++- hw/intel_iommu.c | 888 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/intel_iommu.h | 18 ++ hw/pc.c | 2 + vl.c | 8 + 11 files changed, 1063 insertions(+), 33 deletions(-) create mode 100644 hw/DMAR.dat create mode 100644 hw/intel_iommu.c create mode 100644 hw/intel_iommu.h
Makefile.target +1 -0
@@ -233,6 +233,7 @@ obj-i386-y += testdev.o obj-i386-$(CONFIG_KVM_PIT) += i8254-kvm.o obj-i386-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += device-assignment.o +obj-i386-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += intel_iommu.o # Hardware support obj-ia64-y += ide.o pckbd.o vga.o $(SOUND_HW) dma.o $(AUDIODRV)
cpu-common.h +22 -0
@@ -47,6 +47,9 @@ void cpu_unregister_io_memory(int table_address);void cpu_physical_memory_rw (target_phys_addr_t addr,uint8_t *buf,int len,int is_write); +void cpu_physical_memory_rw_io (target_phys_addr_t addr,uint8_t *buf, +int len,int is_write,uint16_t devfn, +int * err);static inline void cpu_physical_memory_read (target_phys_addr_t addr,uint8_t *buf,int len) {
@@ -57,9 +60,28 @@ static inline void cpu_physical_memory_write(target_phys_addr_t addr, {cpu_physical_memory_rw (addr, (uint8_t *)buf, len,1 ); } +static inline void cpu_physical_memory_read_io (target_phys_addr_t addr, +uint8_t *buf,int len, +uint16_t devfn,int * err) +{ +cpu_physical_memory_rw_io (addr, buf, len,0 , devfn, err); +} +static inline void cpu_physical_memory_write_io (target_phys_addr_t addr, +uint8_t *buf,int len, +uint16_t devfn,int * err) +{ +cpu_physical_memory_rw_io (addr, buf, len,1 , devfn, err); +} + +void set_iommu_translation_fn (target_phys_addr_t (*fn)(target_phys_addr_t , +int ,int ,int *)); +void *cpu_physical_memory_map (target_phys_addr_t addr,target_phys_addr_t *plen,int is_write); +void *cpu_physical_memory_map_io (target_phys_addr_t addr, +target_phys_addr_t *plen, +int is_write,uint16_t devfn,int * err);void cpu_physical_memory_unmap (void *buffer,target_phys_addr_t len,int is_write,target_phys_addr_t access_len);void *cpu_register_map_client (void *opaque,void (*callback)(void *opaque));
dma-helpers.c +13 -7
@@ -47,6 +47,7 @@ typedef struct {target_phys_addr_t sg_cur_byte;QEMUIOVector iov;QEMUBH *bh; +uint16_t busdevfn; }DMAAIOCB ;static void dma_bdrv_cb (void *opaque,int ret);
@@ -84,6 +85,7 @@ static void dma_bdrv_cb(void *opaque, int ret)DMAAIOCB *dbs = (DMAAIOCB *)opaque;target_phys_addr_t cur_addr, cur_len;void *mem; +int err; dbs->acb =NULL ; dbs->sector_num += dbs->iov.size /512 ;
@@ -100,8 +102,9 @@ static void dma_bdrv_cb(void *opaque, int ret)while (dbs->sg_cur_index < dbs->sg->nsg) { cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte; cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte; - mem =cpu_physical_memory_map (cur_addr, &cur_len, !dbs->is_write); -if (!mem) + mem =cpu_physical_memory_map_io (cur_addr, &cur_len, !dbs->is_write, + dbs->busdevfn, &err); +if (!mem || err)break ;qemu_iovec_add (&dbs->iov, mem, cur_len); dbs->sg_cur_byte += cur_len;
@@ -147,7 +150,7 @@ static AIOPool dma_aio_pool = {static BlockDriverAIOCB *dma_bdrv_io (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector_num,BlockDriverCompletionFunc *cb,void *opaque, -int is_write) +int is_write,int busdevfn) {DMAAIOCB *dbs =qemu_aio_get (&dma_aio_pool, bs, cb, opaque);
@@ -159,6 +162,7 @@ static BlockDriverAIOCB *dma_bdrv_io( dbs->sg_cur_byte =0 ; dbs->is_write = is_write; dbs->bh =NULL ; + dbs->busdevfn = busdevfn;qemu_iovec_init (&dbs->iov, sg->nsg);/* * DMA flushing is handled in dma_bdrv_cb() calling dma_bdrv_unmap()
@@ -175,14 +179,16 @@ static BlockDriverAIOCB *dma_bdrv_io(BlockDriverAIOCB *dma_bdrv_read (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector, -void (*cb)(void *opaque,int ret),void *opaque) +void (*cb)(void *opaque,int ret),void *opaque, +uint16_t busdevfn) { -return dma_bdrv_io (bs, sg, sector, cb, opaque,0 ); +return dma_bdrv_io (bs, sg, sector, cb, opaque,0 , busdevfn); }BlockDriverAIOCB *dma_bdrv_write (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector, -void (*cb)(void *opaque,int ret),void *opaque) +void (*cb)(void *opaque,int ret),void *opaque, +uint16_t busdevfn) { -return dma_bdrv_io (bs, sg, sector, cb, opaque,1 ); +return dma_bdrv_io (bs, sg, sector, cb, opaque,1 , busdevfn); }
dma.h +4 -2
@@ -34,8 +34,10 @@ void qemu_sglist_destroy(QEMUSGList *qsg);BlockDriverAIOCB *dma_bdrv_read (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector, -BlockDriverCompletionFunc *cb,void *opaque); +BlockDriverCompletionFunc *cb,void *opaque, +uint16_t busdevfn);BlockDriverAIOCB *dma_bdrv_write (BlockDriverState *bs,QEMUSGList *sg,uint64_t sector, -BlockDriverCompletionFunc *cb,void *opaque); +BlockDriverCompletionFunc *cb,void *opaque, +uint16_t busdevfn);#endif
exec.c +86 -14
178 lines not on this page · raw message
hw/DMAR.dat new file binary +0 -0
hw/ide/core.c +21 -10
@@ -34,6 +34,7 @@#include "dma.h" #include <hw/ide/internal.h> +#include <hw/ide/pci.h> static int smart_attributes[][5 ] = {/* id, flags, val, wrst, thrsh */
@@ -432,7 +433,7 @@ static int dma_buf_prepare(BMDMAState *bm, int is_write)uint32_t addr;uint32_t size; } prd; -int l, len; +int l, len, err;qemu_sglist_init (&s->sg, s->nsector / (TARGET_PAGE_SIZE /512 ) +1 ); s->io_buffer_size =0 ;
@@ -442,7 +443,10 @@ static int dma_buf_prepare(BMDMAState *bm, int is_write)if (bm->cur_prd_last || (bm->cur_addr - bm->addr) >=4096 )return s->io_buffer_size !=0 ; -cpu_physical_memory_read (bm->cur_addr, (uint8_t *)&prd,8 ); +cpu_physical_memory_read_io (bm->cur_addr, (uint8_t *)&prd,8 , + bm->pci_dev->dev.devfn, &err); +if (err) +return 0 ; bm->cur_addr +=8 ; prd.addr =le32_to_cpu (prd.addr); prd.size =le32_to_cpu (prd.size);
@@ -514,7 +518,7 @@ static int dma_buf_rw(BMDMAState *bm, int is_write)uint32_t addr;uint32_t size; } prd; -int l, len; +int l, len, err;for (;;) { l = s->io_buffer_size - s->io_buffer_index;
@@ -525,7 +529,10 @@ static int dma_buf_rw(BMDMAState *bm, int is_write)if (bm->cur_prd_last || (bm->cur_addr - bm->addr) >=4096 )return 0 ; -cpu_physical_memory_read (bm->cur_addr, (uint8_t *)&prd,8 ); +cpu_physical_memory_read_io (bm->cur_addr, (uint8_t *)&prd,8 , + bm->pci_dev->dev.devfn, &err); +if (err) +return 0 ; bm->cur_addr +=8 ; prd.addr =le32_to_cpu (prd.addr); prd.size =le32_to_cpu (prd.size);
@@ -540,11 +547,13 @@ static int dma_buf_rw(BMDMAState *bm, int is_write) l = bm->cur_prd_len;if (l >0 ) {if (is_write) { -cpu_physical_memory_write (bm->cur_prd_addr, - s->io_buffer + s->io_buffer_index, l); +cpu_physical_memory_write_io (bm->cur_prd_addr, + s->io_buffer + s->io_buffer_index, l, + bm->pci_dev->dev.devfn, &err); }else { -cpu_physical_memory_read (bm->cur_prd_addr, - s->io_buffer + s->io_buffer_index, l); +cpu_physical_memory_read_io (bm->cur_prd_addr, + s->io_buffer + s->io_buffer_index, l, + bm->pci_dev->dev.devfn, &err); } bm->cur_prd_addr += l; bm->cur_prd_len -= l;
@@ -600,7 +609,8 @@ static void ide_read_dma_cb(void *opaque, int ret)#ifdef DEBUG_AIO printf ("aio_read: sector_num=%" PRId64" n=%d\n" , sector_num, n);#endif - bm->aiocb =dma_bdrv_read (s->bs, &s->sg, sector_num, ide_read_dma_cb, bm); + bm->aiocb =dma_bdrv_read (s->bs, &s->sg, sector_num, ide_read_dma_cb, bm, + bm->pci_dev->dev.devfn);ide_dma_submit_check (s, ide_read_dma_cb, bm); }
@@ -746,7 +756,8 @@ static void ide_write_dma_cb(void *opaque, int ret)#ifdef DEBUG_AIO printf ("aio_write: sector_num=%" PRId64" n=%d\n" , sector_num, n);#endif - bm->aiocb =dma_bdrv_write (s->bs, &s->sg, sector_num, ide_write_dma_cb, bm); + bm->aiocb =dma_bdrv_write (s->bs, &s->sg, sector_num, ide_write_dma_cb, bm, + bm->pci_dev->dev.devfn);ide_dma_submit_check (s, ide_write_dma_cb, bm); }
hw/intel_iommu.c new file +888 -0
888 lines not on this page · raw message
hw/intel_iommu.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef _INTEL_IOMMU_H_ +#define _INTEL_IOMMU_H_ + +#include "hw.h" + +struct QemuOpts ; + +/* Init the IOMMU emulation */ +void intel_iommu_init (void ); + +/* Configure the iommu */ +int intel_iommu_configure (const char * opt); + +/* Translate the physical address */ +/*uint64_t iommu_phy_addr_translate(target_phys_addr_t addr, +int is_write, int devfn, int* err); +*/ +#endif
hw/pc.c +2 -0
@@ -46,6 +46,7 @@#include "elf.h" #include "multiboot.h" #include "device-assignment.h" +#include "intel_iommu.h" #include "kvm.h"
@@ -1028,6 +1029,7 @@ static void pc_init1(ram_addr_t ram_size, isa_dev =isa_create_simple ("i8042" );DMA_init (0 ); +intel_iommu_init ();#ifdef HAS_AUDIO audio_init (pci_enabled ? pci_bus :NULL , isa_irq);#endif
vl.c +8 -0
@@ -136,6 +136,7 @@ int main(int argc, char **argv)#include "hw/xen.h" #include "hw/qdev.h" #include "hw/loader.h" +#include "hw/intel_iommu.h" #include "bt-host.h" #include "net.h" #include "net/slirp.h"
@@ -5750,6 +5751,13 @@ int main(int argc, char **argv, char **envp)fclose (fp);break ; } +case QEMU_OPTION_intel_iommu: + { +if (intel_iommu_configure (optarg) <0 ) { +fprintf (stderr,"Wrong intel_iommu configuration\n" ); +exit (1 ); + } + } } } }
Signature · 1 line
1.6.3.3
Quoted 2 lines
1. DMAR tables must be loaded by command line ( -acpitable sig=DMAR,data=hw/DMAR.dat )
Why?
cpu-common.h void cpu_unregister_io_memory(int table_address); · line 52
Quoted 3 lines
cpu-common.h +3
-0+void cpu_physical_memory_rw_io (target_phys_addr_t addr,uint8_t *buf, +int len,int is_write,uint16_t devfn, +int * err);
This appears to be PCI specific, which is wrong.
You should be using the qdev infrastructure.
hw/intel_iommu.c line 56
Quoted 7 lines
hw/intel_iommu.c new file +7
-0+/* Mapping related structures */ +struct IommuRootEntry { +uint32_t p :1 ; +uint32_t res1 :11 ; +uint64_t ctp :52 ; +uint64_t res2 :64 ; +};
This is almost certainly wrong on some hosts. You can not assume anything about bitfield layout. Do not use these if an exact binary representation is required.
Paul