Skip to contentSkip to conversation

ahci: map memory via device's address space instead of address_space_memory

PATCH Applied Le Tan 5 participants 15 messages Jul 3, 2014 – Jul 7, 2014

lore
Share & tools
lore

Le Tan

author
#0 lore raw

PATCH 1/1 ahci: map memory via device's address space instead of address_space_memory

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

---
 hw/ide/ahci.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/ide/ahci.c +11 -10
index 9bae22e..7bb0a03 100644
@@ -175,17 +175,18 @@ static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d,
     ahci_check_irq(s);
 }
 
-static void map_page(uint8_t **ptr, uint64_t addr, uint32_t wanted)
+static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr,
+                     uint32_t wanted)
 {
     hwaddr len = wanted;
 
     if (*ptr) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
     }
 
-    *ptr = cpu_physical_memory_map(addr, &len, 1);
+    *ptr = dma_memory_map(as, addr, &len, DMA_DIRECTION_FROM_DEVICE);
     if (len < wanted) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
         *ptr = NULL;
     }
 }
@@ -198,24 +199,24 @@ static void  ahci_port_write(AHCIState *s, int port, int offset, uint32_t val)
     switch (offset) {
         case PORT_LST_ADDR:
             pr->lst_addr = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_LST_ADDR_HI:
             pr->lst_addr_hi = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_FIS_ADDR:
             pr->fis_addr = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_FIS_ADDR_HI:
             pr->fis_addr_hi = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_IRQ_STAT:
@@ -1260,9 +1261,9 @@ static int ahci_state_post_load(void *opaque, int version_id)
         ad = &s->dev[i];
         AHCIPortRegs *pr = &ad->port_regs;
 
-        map_page(&ad->lst,
+        map_page(s->as, &ad->lst,
                  ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
-        map_page(&ad->res_fis,
+        map_page(s->as, &ad->res_fis,
                  ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
         /*
          * All pending i/o should be flushed out on a migrate. However,
Signature · 1 line
1.7.9.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · On Thu, Jul 03, 2014 at 04:26:27PM +0800, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

Makes sense

Quoted 74 lines
---
 hw/ide/ahci.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/ide/ahci.c +11 -10
index 9bae22e..7bb0a03 100644
@@ -175,17 +175,18 @@ static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d,
     ahci_check_irq(s);
 }
 
-static void map_page(uint8_t **ptr, uint64_t addr, uint32_t wanted)
+static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr,
+                     uint32_t wanted)
 {
     hwaddr len = wanted;
 
     if (*ptr) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
     }
 
-    *ptr = cpu_physical_memory_map(addr, &len, 1);
+    *ptr = dma_memory_map(as, addr, &len, DMA_DIRECTION_FROM_DEVICE);
     if (len < wanted) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
         *ptr = NULL;
     }
 }
@@ -198,24 +199,24 @@ static void  ahci_port_write(AHCIState *s, int port, int offset, uint32_t val)
     switch (offset) {
         case PORT_LST_ADDR:
             pr->lst_addr = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_LST_ADDR_HI:
             pr->lst_addr_hi = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_FIS_ADDR:
             pr->fis_addr = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_FIS_ADDR_HI:
             pr->fis_addr_hi = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_IRQ_STAT:
@@ -1260,9 +1261,9 @@ static int ahci_state_post_load(void *opaque, int version_id)
         ad = &s->dev[i];
         AHCIPortRegs *pr = &ad->port_regs;
 
-        map_page(&ad->lst,
+        map_page(s->as, &ad->lst,
                  ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
-        map_page(&ad->res_fis,
+        map_page(s->as, &ad->res_fis,
                  ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
         /*
          * All pending i/o should be flushed out on a migrate. However,
Signature · 1 line
1.7.9.5
#6 lore raw
In reply to Le Tan
Quoted 5 lines · On 2014-07-03 10:26, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU, but plain virtio should work, same for vmxnet.

Jan

Quoted 75 lines
---
 hw/ide/ahci.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/ide/ahci.c +11 -10
index 9bae22e..7bb0a03 100644
@@ -175,17 +175,18 @@ static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d,
     ahci_check_irq(s);
 }
 
-static void map_page(uint8_t **ptr, uint64_t addr, uint32_t wanted)
+static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr,
+                     uint32_t wanted)
 {
     hwaddr len = wanted;
 
     if (*ptr) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
     }
 
-    *ptr = cpu_physical_memory_map(addr, &len, 1);
+    *ptr = dma_memory_map(as, addr, &len, DMA_DIRECTION_FROM_DEVICE);
     if (len < wanted) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
         *ptr = NULL;
     }
 }
@@ -198,24 +199,24 @@ static void  ahci_port_write(AHCIState *s, int port, int offset, uint32_t val)
     switch (offset) {
         case PORT_LST_ADDR:
             pr->lst_addr = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_LST_ADDR_HI:
             pr->lst_addr_hi = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_FIS_ADDR:
             pr->fis_addr = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_FIS_ADDR_HI:
             pr->fis_addr_hi = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_IRQ_STAT:
@@ -1260,9 +1261,9 @@ static int ahci_state_post_load(void *opaque, int version_id)
         ad = &s->dev[i];
         AHCIPortRegs *pr = &ad->port_regs;
 
-        map_page(&ad->lst,
+        map_page(s->as, &ad->lst,
                  ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
-        map_page(&ad->res_fis,
+        map_page(s->as, &ad->res_fis,
                  ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
         /*
          * All pending i/o should be flushed out on a migrate. However,
#2 lore raw
In reply to Michael S. Tsirkin

Il 03/07/2014 10:28, Michael S. Tsirkin ha scritto:

Quoted 11 lines · On Thu, Jul 03, 2014 at 04:26:27PM +0800, Le Tan wrote:
Quoted 7 lines

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

Makes sense

I think this is ok for 2.1 as well.

Paolo

#7 lore raw
In reply to Jan Kiszka

Il 03/07/2014 10:43, Jan Kiszka ha scritto:

Quoted 10 lines · On 2014-07-03 10:26, Le Tan wrote:
Quoted 5 lines

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU, but plain virtio should work, same for vmxnet.

I think PPC folks explicitly wanted virtio to bypass the IOMMU, probably in order to get vhost running. It seems like a bad idea to me, but who am I...

Paolo

#9 lore raw
In reply to Jan Kiszka
Quoted 10 lines · Jan Kiszka
Quoted 5 lines · On 2014-07-03 10:26, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU,

vhost can be made to work: you just need to
update its memory tables as appropriate.
But see below

Quoted 1 line

but plain virtio should work,

It doesn't: all guests pass in physical addresses at the moment. We discussed requiring this for virtio 1.0, but in the end, most people thought that passing through virtio devices isn't worthwhile.
We can certainly add that as an option, with a feature bit.

If you feel otherwise, you can comment on the latest spec draft.

Quoted 81 lines

same for vmxnet.

Jan

Quoted 75 lines
---
 hw/ide/ahci.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/ide/ahci.c +11 -10
index 9bae22e..7bb0a03 100644
@@ -175,17 +175,18 @@ static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d,
     ahci_check_irq(s);
 }
 
-static void map_page(uint8_t **ptr, uint64_t addr, uint32_t wanted)
+static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr,
+                     uint32_t wanted)
 {
     hwaddr len = wanted;
 
     if (*ptr) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
     }
 
-    *ptr = cpu_physical_memory_map(addr, &len, 1);
+    *ptr = dma_memory_map(as, addr, &len, DMA_DIRECTION_FROM_DEVICE);
     if (len < wanted) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
         *ptr = NULL;
     }
 }
@@ -198,24 +199,24 @@ static void  ahci_port_write(AHCIState *s, int port, int offset, uint32_t val)
     switch (offset) {
         case PORT_LST_ADDR:
             pr->lst_addr = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_LST_ADDR_HI:
             pr->lst_addr_hi = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_FIS_ADDR:
             pr->fis_addr = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_FIS_ADDR_HI:
             pr->fis_addr_hi = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_IRQ_STAT:
@@ -1260,9 +1261,9 @@ static int ahci_state_post_load(void *opaque, int version_id)
         ad = &s->dev[i];
         AHCIPortRegs *pr = &ad->port_regs;
 
-        map_page(&ad->lst,
+        map_page(s->as, &ad->lst,
                  ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
-        map_page(&ad->res_fis,
+        map_page(s->as, &ad->res_fis,
                  ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
         /*
          * All pending i/o should be flushed out on a migrate. However,
#8 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Il 03/07/2014 10:43, Jan Kiszka ha scritto:

Quoted 7 lines · On 2014-07-03 10:26, Le Tan wrote:
Quoted 5 lines

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

Quoted 8 lines
Quoted 3 lines

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU, but plain virtio should work, same for vmxnet.

I think PPC folks explicitly wanted virtio to bypass the IOMMU, probably in order to get vhost running. It seems like a bad idea to me, but who am I...

Paolo

The argument went like this: IOMMU slows things down. People might want to run a VM where nested virt is *possible* so IOMMU has to be enabled but still get fast IO for the rest of the guest.

Signature · 1 line
MST
#10 lore raw
In reply to Michael S. Tsirkin
Quoted 12 lines · Michael S. Tsirkin
Quoted 10 lines · Jan Kiszka
Quoted 5 lines · On 2014-07-03 10:26, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU,

Quoted 7 lines

vhost can be made to work: you just need to
update its memory tables as appropriate.
But see below

Quoted 1 line

but plain virtio should work,

It doesn't: all guests pass in physical addresses at the moment.

You mean they do not put virtio devices into IOMMU domains, or they do put them but ignore any translation rules that are not 1:1?

Quoted 3 lines

We discussed requiring this for virtio 1.0, but in the end, most people thought that passing through virtio devices isn't worthwhile.

It should be consistent at least. If virtio is not translated, we have to exclude such devices via ACPI tables from the scope of our IOMMUs.

Quoted 3 lines

We can certainly add that as an option, with a feature bit.

If you feel otherwise, you can comment on the latest spec draft.

Does the spec at least state that "virtio devices are not subject to any guest configured IOMMU translation"? Is is this left undefined?

Jan

#11 lore raw
Quoted 17 lines · Jan Kiszka
Quoted 16 lines · Michael S. Tsirkin
Quoted 10 lines · Jan Kiszka
Quoted 5 lines · On 2014-07-03 10:26, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU,

vhost can be made to work: you just need to
update its memory tables as appropriate.
But see below

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

but plain virtio should work,

It doesn't: all guests pass in physical addresses at the moment.

You mean they do not put virtio devices into IOMMU domains, or they do put them but ignore any translation rules that are not 1:1?

Look at the code. We just pass in physical addresses ignoring which iommu domain device ended up with.

Quoted 6 lines
Quoted 3 lines

We discussed requiring this for virtio 1.0, but in the end, most people thought that passing through virtio devices isn't worthwhile.

It should be consistent at least. If virtio is not translated, we have to exclude such devices via ACPI tables from the scope of our IOMMUs.

I didn't know this is possible. How does one do this?

Quoted 10 lines
Quoted 3 lines

We can certainly add that as an option, with a feature bit.

If you feel otherwise, you can comment on the latest spec draft.

Does the spec at least state that "virtio devices are not subject to any guest configured IOMMU translation"? Is is this left undefined?

Jan

I don't think we have anything like this.

Signature · 1 line
MST
#12 lore raw
Quoted 20 lines · Michael S. Tsirkin
Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Michael S. Tsirkin
Quoted 10 lines · Jan Kiszka
Quoted 5 lines · On 2014-07-03 10:26, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU,

vhost can be made to work: you just need to
update its memory tables as appropriate.
But see below

Quoted 1 line

but plain virtio should work,

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

It doesn't: all guests pass in physical addresses at the moment.

You mean they do not put virtio devices into IOMMU domains, or they do put them but ignore any translation rules that are not 1:1?

Look at the code. We just pass in physical addresses ignoring which iommu domain device ended up with.

Should probably be fixed, at least in Linux. I suppose there is always a 1:1 domain where virtio devices can be put in so that they are not involved in any translation if this is not desired (PPC?).

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

We discussed requiring this for virtio 1.0, but in the end, most people thought that passing through virtio devices isn't worthwhile.

It should be consistent at least. If virtio is not translated, we have to exclude such devices via ACPI tables from the scope of our IOMMUs.

I didn't know this is possible. How does one do this?

Both the DMAR (Intel) and the IVRS (AMD) ACPI tables allow to report the scope of an IOMMU unit. There you list sets of devices on specific buses or a complete segment that the unit virtualizes. Le currently reports that there is a single DMAR unit for q35, and that one controls the complete segment 0. The structure could be adapted if a virtio device is present so that this device is not included. That should prevent that the guest tries to control virtio devices via IOMMUs or even assign them to some L2 guests.

We could then provide a machine property that controls virtualization of virtio devices, default off, but you can enable it when running properly adjusted guest drivers.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

We can certainly add that as an option, with a feature bit.

If you feel otherwise, you can comment on the latest spec draft.

Quoted 9 lines
Quoted 6 lines

Does the spec at least state that "virtio devices are not subject to any guest configured IOMMU translation"? Is is this left undefined?

Jan

I don't think we have anything like this.

And I'm not even sure it necessarily belongs to the spec. I think it's just a guest driver deficit to ignore IOMMU settings that the guest OS may apply.

Jan

#13 lore raw
Quoted 26 lines · Jan Kiszka
Quoted 25 lines · Michael S. Tsirkin
Quoted 23 lines · Jan Kiszka
Quoted 12 lines · Michael S. Tsirkin
Quoted 10 lines · Jan Kiszka
Quoted 5 lines · On 2014-07-03 10:26, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

BTW, when doing "git grep cpu_physical_memory_map hw", there are some more cases that should be checked (for x86). I suppose vhost is incompatible with an IOMMU,

Quoted 7 lines

vhost can be made to work: you just need to
update its memory tables as appropriate.
But see below

Quoted 1 line

but plain virtio should work,

It doesn't: all guests pass in physical addresses at the moment.

You mean they do not put virtio devices into IOMMU domains, or they do put them but ignore any translation rules that are not 1:1?

Quoted 6 lines
Quoted 2 lines

Look at the code. We just pass in physical addresses ignoring which iommu domain device ended up with.

Should probably be fixed, at least in Linux. I suppose there is always a 1:1 domain where virtio devices can be put in so that they are not involved in any translation if this is not desired (PPC?).

This will need some experiments.

Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

We discussed requiring this for virtio 1.0, but in the end, most people thought that passing through virtio devices isn't worthwhile.

It should be consistent at least. If virtio is not translated, we have to exclude such devices via ACPI tables from the scope of our IOMMUs.

I didn't know this is possible. How does one do this?

Quoted 8 lines

Both the DMAR (Intel) and the IVRS (AMD) ACPI tables allow to report the scope of an IOMMU unit. There you list sets of devices on specific buses or a complete segment that the unit virtualizes. Le currently reports that there is a single DMAR unit for q35, and that one controls the complete segment 0. The structure could be adapted if a virtio device is present so that this device is not included. That should prevent that the guest tries to control virtio devices via IOMMUs or even assign them to some L2 guests.

Might be tricky to do with hotplugged devices.

Quoted 16 lines

We could then provide a machine property that controls virtualization of virtio devices, default off, but you can enable it when running properly adjusted guest drivers.

Quoted 12 lines
Quoted 11 lines
Quoted 4 lines

We can certainly add that as an option, with a feature bit.

If you feel otherwise, you can comment on the latest spec draft.

Does the spec at least state that "virtio devices are not subject to any guest configured IOMMU translation"? Is is this left undefined?

Jan

Quoted 9 lines
Quoted 2 lines

I don't think we have anything like this.

And I'm not even sure it necessarily belongs to the spec. I think it's just a guest driver deficit to ignore IOMMU settings that the guest OS may apply.

Jan

I guess we might want to at least document the historical driver behaviour of skipping the IOMMU. But I think I agree this can wait:
we will have time to figure it out before we release QEMU 2.2.

Signature · 1 line
MST
#3 lore raw

[Qemu-devel] for-2.1 (was Re: [PATCH] ahci: map memory via device's address space instead of address_space_memory)

In reply to Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · On Thu, Jul 03, 2014 at 04:26:27PM +0800, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

Quoted 5 lines
Quoted 1 line

Makes sense

Stefan, Kevin, you are going to pick this one?

Quoted 74 lines
Quoted 74 lines
---
 hw/ide/ahci.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/ide/ahci.c +11 -10
index 9bae22e..7bb0a03 100644
@@ -175,17 +175,18 @@ static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d,
     ahci_check_irq(s);
 }
 
-static void map_page(uint8_t **ptr, uint64_t addr, uint32_t wanted)
+static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr,
+                     uint32_t wanted)
 {
     hwaddr len = wanted;
 
     if (*ptr) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
     }
 
-    *ptr = cpu_physical_memory_map(addr, &len, 1);
+    *ptr = dma_memory_map(as, addr, &len, DMA_DIRECTION_FROM_DEVICE);
     if (len < wanted) {
-        cpu_physical_memory_unmap(*ptr, len, 1, len);
+        dma_memory_unmap(as, *ptr, len, DMA_DIRECTION_FROM_DEVICE, len);
         *ptr = NULL;
     }
 }
@@ -198,24 +199,24 @@ static void  ahci_port_write(AHCIState *s, int port, int offset, uint32_t val)
     switch (offset) {
         case PORT_LST_ADDR:
             pr->lst_addr = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_LST_ADDR_HI:
             pr->lst_addr_hi = val;
-            map_page(&s->dev[port].lst,
+            map_page(s->as, &s->dev[port].lst,
                      ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
             s->dev[port].cur_cmd = NULL;
             break;
         case PORT_FIS_ADDR:
             pr->fis_addr = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_FIS_ADDR_HI:
             pr->fis_addr_hi = val;
-            map_page(&s->dev[port].res_fis,
+            map_page(s->as, &s->dev[port].res_fis,
                      ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
             break;
         case PORT_IRQ_STAT:
@@ -1260,9 +1261,9 @@ static int ahci_state_post_load(void *opaque, int version_id)
         ad = &s->dev[i];
         AHCIPortRegs *pr = &ad->port_regs;
 
-        map_page(&ad->lst,
+        map_page(s->as, &ad->lst,
                  ((uint64_t)pr->lst_addr_hi << 32) | pr->lst_addr, 1024);
-        map_page(&ad->res_fis,
+        map_page(s->as, &ad->res_fis,
                  ((uint64_t)pr->fis_addr_hi << 32) | pr->fis_addr, 256);
         /*
          * All pending i/o should be flushed out on a migrate. However,
Signature · 1 line
1.7.9.5
#4 lore raw

Re: [Qemu-devel] for-2.1 (was Re: [PATCH] ahci: map memory via device's address space instead of address_space_memory)

Quoted 2 lines

Stefan, Kevin, you are going to pick this one?

I think there's no hurry for 2.1.

Also...

Quoted 4 lines
Quoted 4 lines

hw/ide/ahci.c static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d, · line 179

Quoted 4 lines

---

hw/ide/ahci.c +2 -1
-static void map_page(uint8_t **ptr, uint64_t addr, uint32_t wanted)
+static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr,
+                     uint32_t wanted)

... it is a bit cleaner to pass the AHCIState here.

Paolo

#5 lore raw

Re: [Qemu-devel] for-2.1 (was Re: [PATCH] ahci: map memory via device's address space instead of address_space_memory)

In reply to Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · On Thu, Jul 03, 2014 at 04:26:27PM +0800, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Makes sense

Stefan, Kevin, you are going to pick this one?

Done.

Stefan

#14 lore raw
In reply to Le Tan
Quoted 10 lines · On Thu, Jul 03, 2014 at 04:26:27PM +0800, Le Tan wrote:

In map_page() in hw/ide/ahci.c, replace cpu_physical_memory_map() and cpu_physical_memory_unmap() with dma_memory_map() and dma_memory_unmap(), because ahci devices should not access memory directly but via their address space. Add an AddressSpace parameter to map_page(). In order to call map_page(), we should pass the AHCIState.as as the AddressSpace argument.

---
 hw/ide/ahci.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan