Skip to contentSkip to conversation

hw/passthrough: Prevent QEMU from mapping PCI option ROM at address 0

PATCH In review Malcolm Crossley 5 participants 12 messages May 12, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/passthrough: Prevent QEMU from mapping PCI option ROM at address 0

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pass-through.c +1 -1
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & PCI_BASE_ADDRESS_MEM_MASK) == 0))
         r_addr = -1;
 
     /* align resource size (memory type only) */
Signature · 1 line
1.7.1
#1 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 14:42, <[email protected]> wrote:

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pass-through.c +0 -0
index 304c438..7d6aefc 100644

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable, · line 2211

Quoted 5 lines
hw/pass-through.c +1 -1
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & PCI_BASE_ADDRESS_MEM_MASK) == 0))

You talk of the low bit, but mask off the low 4 - how does that fit together? Didn't you rather mean PCI_ROM_ADDRESS_MASK & ~PCI_ROM_ADDRESS_ENABLE in text and code?

Jan

#2 lore raw
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 14:42, <[email protected]> wrote:

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pass-through.c +0 -0
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
     }
 
Quoted 7 lines

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable, · line 2211

Quoted 3 lines
hw/pass-through.c +1 -1
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & PCI_BASE_ADDRESS_MEM_MASK) == 0))

You talk of the low bit, but mask off the low 4 - how does that fit together? Didn't you rather mean PCI_ROM_ADDRESS_MASK & ~PCI_ROM_ADDRESS_ENABLE in text and code?

The description provides an example of a driver setting the lower bits of the BAR.

The intent of the fix is to ensure no BAR is mapped address 0 which is achieved by ensuring only the address bits of the BAR are used for the comparison with 0.

Malcolm

Quoted 2 lines

Jan

#3 lore raw
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 15:26, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 14:42, <[email protected]> wrote:

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves

the

Quoted 9 lines
Quoted 9 lines

LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Quoted 5 lines

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev,

Quoted 5 lines
hw/pass-through.c +0 -0
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, 

int bar, int io_enable,

Quoted 5 lines

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,

Quoted 5 lines
hw/pass-through.c +1 -1
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & 

PCI_BASE_ADDRESS_MEM_MASK) == 0))

Quoted 4 lines

You talk of the low bit, but mask off the low 4 - how does that fit together? Didn't you rather mean PCI_ROM_ADDRESS_MASK & ~PCI_ROM_ADDRESS_ENABLE in text and code?

Quoted 6 lines

The description provides an example of a driver setting the lower bits of the BAR.

The intent of the fix is to ensure no BAR is mapped address 0 which is achieved by ensuring only the address bits of the BAR are used for the comparison with 0.

But the address bits here are bits 11-31, not 1-31 or 4-31.

Jan

#4 lore raw
Quoted 32 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 15:26, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 14:42, <[email protected]> wrote:

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves

the

Quoted 9 lines
Quoted 9 lines

LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Quoted 5 lines

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev,

Quoted 5 lines
hw/pass-through.c +0 -0
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, 

int bar, int io_enable,

Quoted 5 lines

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,

Quoted 5 lines
hw/pass-through.c +1 -1
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & 

PCI_BASE_ADDRESS_MEM_MASK) == 0))

Quoted 4 lines

You talk of the low bit, but mask off the low 4 - how does that fit together? Didn't you rather mean PCI_ROM_ADDRESS_MASK & ~PCI_ROM_ADDRESS_ENABLE in text and code?

Quoted 9 lines
Quoted 6 lines

The description provides an example of a driver setting the lower bits of the BAR.

The intent of the fix is to ensure no BAR is mapped address 0 which is achieved by ensuring only the address bits of the BAR are used for the comparison with 0.

But the address bits here are bits 11-31, not 1-31 or 4-31.

Ah, I understand you point now, sorry I looked at the wrong definition for PCI_ROM_ADDRESS_MASK before.

The original problem was that only the LSB was set and the driver was inferring that if the address (11-31) was 0 then the BAR would not be mapped over the 0 page.

This works for several reasons on bare metal:

1. hardware address decoders prefer the RAM ranges over the PCI ranges
2. the bridge window on the PCI range would not cover address 0

The problem we have is that QEMU is configuring a mapping based only on the BAR data information and so it mapping the option ROM on top of the 0 RAM page.

As this issue only affects qemu-trad, I think we should continue the previous behaviour and ensure no BAR can be mapped to the 0 page which as you correctly point out means increasing the mask to cover bits 0-10.

Do you agree? If so, I will submit a new patch.

Malcolm

Quoted 2 lines

Jan

#5 lore raw
Quoted 59 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 15:55, <[email protected]> wrote:
Quoted 32 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 15:26, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 12.05.14 at 14:42, <[email protected]> wrote:

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves

the

Quoted 9 lines
Quoted 9 lines

LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Quoted 5 lines

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev,

Quoted 5 lines
hw/pass-through.c +0 -0
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, 

int bar, int io_enable,

Quoted 5 lines

hw/pass-through.c static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,

Quoted 5 lines
hw/pass-through.c +1 -1
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & 

PCI_BASE_ADDRESS_MEM_MASK) == 0))

Quoted 4 lines

You talk of the low bit, but mask off the low 4 - how does that fit together? Didn't you rather mean PCI_ROM_ADDRESS_MASK & ~PCI_ROM_ADDRESS_ENABLE in text and code?

Quoted 9 lines
Quoted 6 lines

The description provides an example of a driver setting the lower bits of the BAR.

The intent of the fix is to ensure no BAR is mapped address 0 which is achieved by ensuring only the address bits of the BAR are used for the comparison with 0.

But the address bits here are bits 11-31, not 1-31 or 4-31.

Ah, I understand you point now, sorry I looked at the wrong definition for PCI_ROM_ADDRESS_MASK before.

The original problem was that only the LSB was set and the driver was inferring that if the address (11-31) was 0 then the BAR would not be mapped over the 0 page.

This works for several reasons on bare metal:

1. hardware address decoders prefer the RAM ranges over the PCI ranges
2. the bridge window on the PCI range would not cover address 0

The problem we have is that QEMU is configuring a mapping based only on the BAR data information and so it mapping the option ROM on top of the 0 RAM page.

Quoted 5 lines

As this issue only affects qemu-trad, I think we should continue the previous behaviour and ensure no BAR can be mapped to the 0 page which as you correctly point out means increasing the mask to cover bits 0-10.

Do you agree? If so, I will submit a new patch.

I agree with the conclusion; my knowledge of qemu is too limited to be able to say that I also agree with the difference analysis with real hardware.

Jan

#6 lore raw
In reply to Malcolm Crossley
Quoted 7 lines · Malcolm Crossley

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

Since you CC Ian J I assume this is against the qemu-xen-traditional tree. Is the same fix needed against upstream qemu?

Quoted 17 lines
---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pass-through.c +1 -1
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & PCI_BASE_ADDRESS_MEM_MASK) == 0))
         r_addr = -1;
 
     /* align resource size (memory type only) */
#7 lore raw
Quoted 6 lines · Ian Campbell
Quoted 5 lines · Malcolm Crossley

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

Quoted 6 lines
Quoted 2 lines

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

Since you CC Ian J I assume this is against the qemu-xen-traditional tree. Is the same fix needed against upstream qemu?

The fix is only required on qemu-xen-traditional tree.

I have not tested qemu-upstream but it looks fine from code inspection (line 436 in hw/xen/xen_pt.c has the correct masking on BAR address).

Quoted 19 lines
Quoted 17 lines
---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pass-through.c +1 -1
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev *ptdev, int bar, int io_enable,
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr & PCI_BASE_ADDRESS_MEM_MASK) == 0))
         r_addr = -1;
 
     /* align resource size (memory type only) */
#8 lore raw
Quoted 10 lines · Malcolm Crossley
Quoted 9 lines · Ian Campbell
Quoted 7 lines · Malcolm Crossley

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

Quoted 8 lines
Quoted 3 lines

Since you CC Ian J I assume this is against the qemu-xen-traditional tree. Is the same fix needed against upstream qemu?

The fix is only required on qemu-xen-traditional tree.

I have not tested qemu-upstream but it looks fine from code inspection (line 436 in hw/xen/xen_pt.c has the correct masking on BAR address).

Great, thanks for confirming.

Ian.

#9 lore raw
In reply to Malcolm Crossley
Quoted 9 lines

-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Malcolm Crossley
Sent: 12 May 2014 05:43
To: [email protected]; Ian Jackson
Cc: Malcolm Crossley
Subject: [Xen-devel] [PATCH] hw/passthrough: Prevent QEMU from mapping PCI option ROM at address 0

Quoted 8 lines

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing
the address to 0.

Is this for trad, upstream, or both?

You also need a s-o-b on your patch.

Paul

Quoted 27 lines
---
 hw/pass-through.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pass-through.c +1 -1
index 304c438..7d6aefc 100644
@@ -2208,7 +2208,7 @@ static void pt_bar_mapping_one(struct pt_dev
 *ptdev, int bar, int io_enable,
     }
 
     /* prevent guest software mapping memory resource to 00000000h */
-    if ((base->bar_flag == PT_BAR_FLAG_MEM) && (r_addr == 0))
+    if ((base->bar_flag == PT_BAR_FLAG_MEM) && ((r_addr &
 PCI_BASE_ADDRESS_MEM_MASK) == 0))
         r_addr = -1;
     /* align resource size (memory type only) */

--
1.7.1

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

#10 lore raw
In reply to Malcolm Crossley

Malcolm Crossley writes ("[PATCH] hw/passthrough: Prevent QEMU from mapping PCI option ROM at address 0"):

Quoted 7 lines

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

Is this just for qemu-xen-traditional ? Is there a corresponding patch to qemu-upstream ?

Ian.

#11 lore raw
Quoted 6 lines · Ian Jackson

Malcolm Crossley writes ("[PATCH] hw/passthrough: Prevent QEMU from mapping PCI option ROM at address 0"):

Quoted 5 lines

The PCI option ROM BAR uses the LSB to indicate if the BAR is enabled. The AMD graphics driver sets the address bit's of the BAR to 0 but leaves the LSB set to 1. Whilst this is not good practice, QEMU should be ignoring the non address parts of the BAR.

Quoted 6 lines
Quoted 2 lines

This patch adds masking of the non address parts of the BAR before comparing the address to 0.

Is this just for qemu-xen-traditional ? Is there a corresponding patch to qemu-upstream ?

It is just for qemu-xen-traditional, I've just submitted a v4 patch which expands the masking of the bottom bits up to 4k.

MMIO regions are handled differently in qemu-upstream and so a corresponding patch is not required.

Malcolm

Quoted 2 lines

Ian.