Bugfixes and pseries enhancements
cover Bugfixes and pseries enhancements
This series has two parts. THe first has a few bugfixes for generic qemu code. The second has a number of pseries machine specific enhancements and bugfixes. They are included in the one series because some of the pseries specific patches rely on the generic bugfixes (in the sense that they expose the bugs more badly than they were previously).
I've posted some of the generic fixes before, this series respins the first one (load_image_targphys()) with a minor tweak, the rest are unchanged.
PATCH 1/9 load_image_targphys() should enforce the max size
From: Benjamin Herrenschmidt <[email protected]>
load_image_targphys() gets passed a max size for the file, but doesn't enforce it at all. Add a check and return -1 (error) if the file is too big, without loading it. Fix the bracing style in the function while we're at it.
--- hw/loader.c | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)
hw/loader.c +5 -1
@@ -108,8 +108,12 @@ int load_image_targphys(const char *filename,int size; size =get_image_size (filename); -if (size >0 ) +if (size > max_sz) { +return -1 ; + } +if (size >0 ) {rom_add_file_fixed (filename, addr,-1 ); + }return size; }
Signature · 1 line
1.7.7.3
PATCH 2/9 Fix dirty logging with 32-bit qemu & 64-bit guests
In reply to David GibsonFrom: Benjamin Herrenschmidt <[email protected]>
The kvm_get_dirty_pages_log_range() function uses two address variables to step through the monitored memory region to update the dirty log. However, these variables have type unsigned long, which can overflow if running a 64-bit guest with a 32-bit qemu binary.
This patch changes these to target_phys_addr_t which will have the correct size.
--- kvm-all.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
kvm-all.c +2 -1
@@ -344,7 +344,8 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section, unsigned long *bitmap) {unsigned int i, j; -unsigned long page_number, addr, addr1, c; +unsigned long page_number, c; +target_phys_addr_t addr, addr1;unsigned int len = ((section->size /TARGET_PAGE_SIZE ) +HOST_LONG_BITS -1 ) /HOST_LONG_BITS ;/*
Signature · 1 line
1.7.7.3
PATCH 3/9 pci: Make bounds checks on config space accesses actually work
In reply to David GibsonThe pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense. Accesses which entirely overrun the region will *not* be blocked (an exploitable bug), because in that case (limit - addr) will be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.
This patch handles things more sanely by simply ignoring writes which overrun, and returning -1 for reads, which is the usual hardware convention for reads to unpopulated IO regions.
--- hw/pci_host.c | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)
hw/pci_host.c +8 -2
@@ -51,14 +51,20 @@ void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr, uint32_t limit, uint32_t val, uint32_t len) { assert(len <= 4); - pci_dev->config_write (pci_dev, addr, val,MIN (len, limit - addr)); +if ((addr + len) <= limit) { + pci_dev->config_write (pci_dev, addr, val, len); + } }uint32_t pci_host_config_read_common (PCIDevice *pci_dev,uint32_t addr,uint32_t limit,uint32_t len) {assert (len <=4 ); -return pci_dev->config_read (pci_dev, addr,MIN (len, limit - addr)); +if ((addr + len) <= limit) { +return pci_dev->config_read (pci_dev, addr, len); + }else { +return ~0x0 ; + } }void pci_data_write (PCIBus *s,uint32_t addr,uint32_t val,int len)
Signature · 1 line
1.7.7.3
Re: [Qemu-devel] [PATCH 3/9] pci: Make bounds checks on config space accesses actually work
Quoted 15 lines · David Gibson
The pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense. Accesses which entirely overrun the region will *not* be blocked (an exploitable bug), because in that case (limit - addr) will be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.
Quoted 5 lines
This patch handles things more sanely by simply ignoring writes which overrun, and returning -1 for reads, which is the usual hardware convention for reads to unpopulated IO regions.
Signed-off-by David Gibson<[email protected]>
Michael, please ack or apply yourself. I'll cache this in my tree regardless so it doesn't get lost.
Alex
Quoted 31 lines
--- hw/pci_host.c | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)hw/pci_host.c +8
-2@@ -51,14 +51,20 @@ void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr, uint32_t limit, uint32_t val, uint32_t len) { assert(len<= 4); - pci_dev->config_write (pci_dev, addr, val,MIN (len, limit - addr)); +if ((addr + len)<= limit) { + pci_dev->config_write (pci_dev, addr, val, len); + } }uint32_t pci_host_config_read_common (PCIDevice *pci_dev,uint32_t addr,uint32_t limit,uint32_t len) {assert (len<=4 ); -return pci_dev->config_read (pci_dev, addr,MIN (len, limit - addr)); +if ((addr + len)<= limit) { +return pci_dev->config_read (pci_dev, addr, len); + }else { +return ~0x0 ; + } }void pci_data_write (PCIBus *s,uint32_t addr,uint32_t val,int len)
Re: [Qemu-devel] [PATCH 3/9] pci: Make bounds checks on config space accesses actually work
Quoted 20 lines · Alexander Graf
Quoted 19 lines · David Gibson
The pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense. Accesses which entirely overrun the region will *not* be blocked (an exploitable bug), because in that case (limit - addr) will be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.
This patch handles things more sanely by simply ignoring writes which overrun, and returning -1 for reads, which is the usual hardware convention for reads to unpopulated IO regions.
Quoted 7 lines
Quoted 1 line
Signed-off-by David Gibson<[email protected]>Michael, please ack or apply yourself. I'll cache this in my tree regardless so it doesn't get lost.
Alex
I'd like to understand the bug a bit.
Quoted 32 lines
Quoted 31 lines
--- hw/pci_host.c | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)hw/pci_host.c +8
-2@@ -51,14 +51,20 @@ void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr, uint32_t limit, uint32_t val, uint32_t len) { assert(len<= 4); - pci_dev->config_write (pci_dev, addr, val,MIN (len, limit - addr)); +if ((addr + len)<= limit) { + pci_dev->config_write (pci_dev, addr, val, len); + } }uint32_t pci_host_config_read_common (PCIDevice *pci_dev,uint32_t addr,uint32_t limit,uint32_t len) {assert (len<=4 ); -return pci_dev->config_read (pci_dev, addr,MIN (len, limit - addr)); +if ((addr + len)<= limit) { +return pci_dev->config_read (pci_dev, addr, len); + }else { +return ~0x0 ; + } }void pci_data_write (PCIBus *s,uint32_t addr,uint32_t val,int len)
Re: [Qemu-devel] [PATCH 3/9] pci: Make bounds checks on config space accesses actually work
In reply to David GibsonQuoted 10 lines · David Gibson
The pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense.
Why does it make little sense? Makes sense to me.
Quoted 6 lines
Accesses which entirely overrun the region will *not*
be blocked (an exploitable bug)
, because in that case (limit - addr) will
be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.
The assumption was callers never pass in such values. Could you please give an example how this exploitable bug can get triggered?
Quoted 40 lines
This patch handles things more sanely by simply ignoring writes which overrun, and returning -1 for reads, which is the usual hardware convention for reads to unpopulated IO regions.
Signed-off-by David Gibson <[email protected]>--- hw/pci_host.c | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)hw/pci_host.c +8
-2@@ -51,14 +51,20 @@ void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr, uint32_t limit, uint32_t val, uint32_t len) { assert(len <= 4); - pci_dev->config_write (pci_dev, addr, val,MIN (len, limit - addr)); +if ((addr + len) <= limit) { + pci_dev->config_write (pci_dev, addr, val, len); + } }uint32_t pci_host_config_read_common (PCIDevice *pci_dev,uint32_t addr,uint32_t limit,uint32_t len) {assert (len <=4 ); -return pci_dev->config_read (pci_dev, addr,MIN (len, limit - addr)); +if ((addr + len) <= limit) { +return pci_dev->config_read (pci_dev, addr, len); + }else { +return ~0x0 ; + } }void pci_data_write (PCIBus *s,uint32_t addr,uint32_t val,int len)Signature · 1 line
1.7.7.3
Re: [Qemu-devel] [Qemu-ppc] [PATCH 3/9] pci: Make bounds checks on config space accesses actually work
Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · David Gibson
The pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Quoted 7 lines
Quoted 5 lines
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense.
Why does it make little sense? Makes sense to me.
Well, for starters a partial overrun would have to be an unaligned config space access, which is not supported by PCI. The behaviour if you try is undefined on most bridges and unlikely to be a partial read/write (ignoring the low addr bits would be more likely).
Even if a bridge somehow did a partial access, it's still wrong for reads, since the overrunning bits would typically return 0xff not 0x00, and so you'd need to 0xff pad the result.
There's just no point doing anything other than simply failing partial overruns.
Quoted 8 lines
Quoted 6 lines
Accesses which entirely overrun the region will *not*
be blocked (an exploitable bug)
, because in that case (limit - addr) will
be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.The assumption was callers never pass in such values.
So, callers are to to treat this function, taking a limit parameter as having semantics of "checks a pointless edge case, but not the obvious type of overrun". You think that's a sensible semantic for a general helper function? Seriously?
Quoted 2 lines
Could you please give an example how this exploitable bug can get triggered?
Ah, yes, it's not actually exploitable in the current code, since the only callers mask the address down. It becomes exploitable if someone writes a new bridge which doesn't go via the existing accessors and assumes that the function which looks like it bounds checks actually bounds checks (which I'm about to do for the pseries PCI code.
Factoring the bounds checking into pci_host_config_{read,write} just makes more sense.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [Qemu-devel] [Qemu-ppc] [PATCH 3/9] pci: Make bounds checks on config space accesses actually work
Quoted 13 lines · David Gibson
Quoted 12 lines · Michael S. Tsirkin
Quoted 10 lines · David Gibson
The pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense.
Quoted 6 lines
Quoted 1 line
Why does it make little sense? Makes sense to me.
Well, for starters a partial overrun would have to be an unaligned config space access, which is not supported by PCI. The behaviour if you try is undefined on most bridges and unlikely to be a partial read/write (ignoring the low addr bits would be more likely).
Yes, bus level cycles have dword granularity in PCI.
But look e.g. at our express implementation.
Config is memory mapped, so we simply map this as memory into guest. If you do a large read, what happens on real hardware? I'm not sure, but it looks possible that it will get split and multiple dword transactions generated on the bus. OTOH our implementation passes the read on as is, so
it can get a multi-dword.
I'll try to play with some real systems to see what they do.
Quoted 3 lines
Even if a bridge somehow did a partial access, it's still wrong for reads, since the overrunning bits would typically return
0xffnot0x00, and so you'd need to0xffpad the result.
Or maybe an error needs to be generated. We really don't know, it's up to the caller.
Quoted 2 lines
There's just no point doing anything other than simply failing partial overruns.
There's no point in random code churn either.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Accesses which entirely overrun the region will *not*
be blocked (an exploitable bug)
, because in that case (limit - addr) will
be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.
Quoted 6 lines
Quoted 1 line
The assumption was callers never pass in such values.
So, callers are to to treat this function, taking a limit parameter as having semantics of "checks a pointless edge case, but not the obvious type of overrun". You think that's a sensible semantic for a general helper function? Seriously?
I don't mind adding an extra check at this level. But the comment would need to be reworded from 'fix a bug' to 'pseries wants to pass out of range values so let's check this'.
Quoted 17 lines
Quoted 2 lines
Could you please give an example how this exploitable bug can get triggered?
Ah, yes, it's not actually exploitable in the current code, since the only callers mask the address down. It becomes exploitable if someone writes a new bridge which doesn't go via the existing accessors and assumes that the function which looks like it bounds checks actually bounds checks (which I'm about to do for the pseries PCI code.
Factoring the bounds checking into pci_host_config_{read,write} just makes more sense.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [Qemu-devel] [Qemu-ppc] [PATCH 3/9] pci: Make bounds checks on config space accesses actually work
Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · David Gibson
Quoted 13 lines · Michael S. Tsirkin
Quoted 10 lines · David Gibson
The pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense.
Why does it make little sense? Makes sense to me.
Quoted 8 lines
Quoted 4 lines
Well, for starters a partial overrun would have to be an unaligned config space access, which is not supported by PCI. The behaviour if you try is undefined on most bridges and unlikely to be a partial read/write (ignoring the low addr bits would be more likely).
Yes, bus level cycles have dword granularity in PCI.
But look e.g. at our express implementation.
Config is memory mapped, so we simply map this as memory into guest.
So?
Quoted 3 lines
If you do a large read, what happens on real hardware? I'm not sure, but it looks possible that it will get split and multiple dword transactions generated on the bus.
Totally irrelevant in this context. The function definition only allows a maximum of 4 byte transfers (because of the uint32_ts), so the bus emulation logic would already have to split a long read into multiple calls to the function.
Quoted 8 lines
OTOH our implementation passes the read on as is, so it can get a multi-dword.
I'll try to play with some real systems to see what they do.Quoted 3 lines
Even if a bridge somehow did a partial access, it's still wrong for reads, since the overrunning bits would typically return
0xffnot0x00, and so you'd need to0xffpad the result.
Quoted 7 lines
Or maybe an error needs to be generated. We really don't know, it's up to the caller.
Quoted 2 lines
There's just no point doing anything other than simply failing partial overruns.
There's no point in random code churn either.
Removing an insane semantic os not random code churn.
Quoted 14 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Accesses which entirely overrun the region will *not*
be blocked (an exploitable bug)
, because in that case (limit - addr) will
be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.Quoted 6 lines
Quoted 1 line
The assumption was callers never pass in such values.
So, callers are to to treat this function, taking a limit parameter as having semantics of "checks a pointless edge case, but not the obvious type of overrun". You think that's a sensible semantic for a general helper function? Seriously?
Quoted 4 lines
I don't mind adding an extra check at this level. But the comment would need to be reworded from 'fix a bug' to 'pseries wants to pass out of range values so let's check this'.
Oh, FFS, you're just making excuses for not making a simple fix to stupid code.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [Qemu-devel] [Qemu-ppc] [PATCH 3/9] pci: Make bounds checks on config space accesses actually work
Quoted 69 lines · David Gibson
Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · David Gibson
Quoted 13 lines · Michael S. Tsirkin
Quoted 10 lines · David Gibson
The pci_host_config_{read,write}_common() functions perform PCI config accesses. They take a limit parameter which they appear to be supposed to bounds check against, however the bounds checking logic, such as it is, is completely broken.
Currently, it takes the minimum of the supplied length and the remaining space in the region and passes that as the length to the underlying config_{read,write} function pointer. This means that accesses which partially overrun the region will be silently truncated - which makes little sense.
Why does it make little sense? Makes sense to me.
Quoted 8 lines
Quoted 4 lines
Well, for starters a partial overrun would have to be an unaligned config space access, which is not supported by PCI. The behaviour if you try is undefined on most bridges and unlikely to be a partial read/write (ignoring the low addr bits would be more likely).
Yes, bus level cycles have dword granularity in PCI.
But look e.g. at our express implementation.
Config is memory mapped, so we simply map this as memory into guest.So?
Quoted 3 lines
If you do a large read, what happens on real hardware? I'm not sure, but it looks possible that it will get split and multiple dword transactions generated on the bus.
Totally irrelevant in this context. The function definition only allows a maximum of 4 byte transfers (because of the
uint32_ts), so the bus emulation logic would already have to split a long read into multiple calls to the function.Quoted 8 lines
OTOH our implementation passes the read on as is, so it can get a multi-dword.
I'll try to play with some real systems to see what they do.Quoted 3 lines
Even if a bridge somehow did a partial access, it's still wrong for reads, since the overrunning bits would typically return
0xffnot0x00, and so you'd need to0xffpad the result.Quoted 7 lines
Or maybe an error needs to be generated. We really don't know, it's up to the caller.
Quoted 2 lines
There's just no point doing anything other than simply failing partial overruns.
There's no point in random code churn either.
Removing an insane semantic os not random code churn.
Quoted 14 lines
Quoted 13 lines
Quoted 8 lines
Quoted 6 lines
Accesses which entirely overrun the region will *not*
be blocked (an exploitable bug)
, because in that case (limit - addr) will
be negative and so the unsigned MIN will always return len instead. Even if signed arithmetic was used, the config_{read,write} callback wouldn't know what to do with a negative len parameter.The assumption was callers never pass in such values.
So, callers are to to treat this function, taking a limit parameter as having semantics of "checks a pointless edge case, but not the obvious type of overrun". You think that's a sensible semantic for a general helper function? Seriously?
Quoted 7 lines
Quoted 4 lines
I don't mind adding an extra check at this level. But the comment would need to be reworded from 'fix a bug' to 'pseries wants to pass out of range values so let's check this'.
Oh, FFS, you're just making excuses for not making a simple fix to stupid code.
ping?
Alex
PATCH 4/9 Update gitignore file
In reply to David GibsonThis patch adds several auto-generated files to .gitignore which were previously missing.
--- .gitignore | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
.gitignore +4 -0
@@ -42,6 +42,10 @@ qemu-ga qemu-monitor.texi QMP/qmp-commands.txt test-coroutine +test-qmp-input-visitor +test-qmp-output-visitor +fsdev/virtfs-proxy-helper.1 +fsdev/virtfs-proxy-helper.pod .gdbinit *.a *.aux
Signature · 1 line
1.7.7.3
PATCH 5/9 Correct types in bmdma_addr_{read,write}
In reply to David GibsonBack when I made patches introducing dma_addr_t and various PCI DMA wrapper functions, I made a mistake. The bmdma_addr_{read,write} functions need to take target_phys_addr_t not dma_addr_t, since they are assigned to MemoryRegionOps callbacks.
This patch corrects my error.
--- hw/ide/pci.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
hw/ide/pci.c +2 -2
@@ -327,7 +327,7 @@ void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val) bm->cmd = val &0x09 ; } -static uint64_t bmdma_addr_read (void *opaque,dma_addr_t addr, +static uint64_t bmdma_addr_read (void *opaque,target_phys_addr_t addr,unsigned width) {BMDMAState *bm = opaque;
@@ -341,7 +341,7 @@ static uint64_t bmdma_addr_read(void *opaque, dma_addr_t addr,return data; } -static void bmdma_addr_write (void *opaque,dma_addr_t addr, +static void bmdma_addr_write (void *opaque,target_phys_addr_t addr,uint64_t data,unsigned width) {BMDMAState *bm = opaque;
Signature · 1 line
1.7.7.3
PATCH 6/9 pseries: Support PCI extended config space in RTAS calls
In reply to David GibsonFrom: Benjamin Herrenschmidt <[email protected]>
On the pseries machine (which expexts a paravirtualized guest), guest access to PCI config space is via host-provided RTAS functions. This patch extends these RTAS functions to permit access to PCI extended config space, as specified in PAPR.
--- hw/spapr_pci.c | 14 ++++++++++---- 1 files changed, 10 insertions(+), 4 deletions(-)
hw/spapr_pci.c +10 -4
@@ -62,6 +62,11 @@ static PCIDevice *find_dev(sPAPREnvironment *spapr,return NULL ; } +static uint32_t rtas_pci_cfgaddr (uint32_t arg) +{ +return ((arg >>20 ) &0xf00 ) | (arg &0xff ); +} + static void rtas_ibm_read_pci_config(sPAPREnvironment *spapr, uint32_t token, uint32_t nargs, target_ulong args,
@@ -76,7 +81,7 @@ static void rtas_ibm_read_pci_config(sPAPREnvironment *spapr,return ; } size =rtas_ld (args,3 ); - addr =rtas_ld (args,0 ) &0xFF ; + addr =rtas_pci_cfgaddr (rtas_ld (args,0 )); val =pci_default_read_config (dev, addr, size);rtas_st (rets,0 ,0 );rtas_st (rets,1 , val);
@@ -95,7 +100,7 @@ static void rtas_read_pci_config(sPAPREnvironment *spapr,return ; } size =rtas_ld (args,1 ); - addr =rtas_ld (args,0 ) &0xFF ; + addr =rtas_pci_cfgaddr (rtas_ld (args,0 )); val =pci_default_read_config (dev, addr, size);rtas_st (rets,0 ,0 );rtas_st (rets,1 , val);
@@ -116,7 +121,7 @@ static void rtas_ibm_write_pci_config(sPAPREnvironment *spapr, } val =rtas_ld (args,4 ); size =rtas_ld (args,3 ); - addr =rtas_ld (args,0 ) &0xFF ; + addr =rtas_pci_cfgaddr (rtas_ld (args,0 ));pci_default_write_config (dev, addr, val, size);rtas_st (rets,0 ,0 ); }
@@ -135,7 +140,7 @@ static void rtas_write_pci_config(sPAPREnvironment *spapr, } val =rtas_ld (args,2 ); size =rtas_ld (args,1 ); - addr =rtas_ld (args,0 ) &0xFF ; + addr =rtas_pci_cfgaddr (rtas_ld (args,0 ));pci_default_write_config (dev, addr, val, size);rtas_st (rets,0 ,0 ); }
@@ -394,6 +399,7 @@ int spapr_populate_pci_devices(sPAPRPHBState *phb,_FDT (fdt_setprop (fdt, bus_off,"reg" , &bus_reg,sizeof (bus_reg)));_FDT (fdt_setprop (fdt, bus_off,"interrupt-map-mask" , &interrupt_map_mask,sizeof (interrupt_map_mask))); +_FDT (fdt_setprop_cell (fdt, bus_off,"ibm,pci-config-space-type" ,0x1 ));/* Populate PCI devices and allocate IRQs */ devices =0 ;
Signature · 1 line
1.7.7.3
PATCH 7/9 pseries: Use correct dispatcher for PCI config space accesses
In reply to David GibsonThe pseries machine expects a para-virtualized guest and so supplies RTAS functions (via a hypercall) for performing PCI config space access. Currently the implementation of these calls into pci_default_{read,write}_config(). However this would be incorrect for any PCI device which overrides the default config read/write functions. AFAICT there's only one such device today, but we should still get it right. In addition the pci_host_config_{read,write}_common() functions which do correctly do this dispatch, perform bounds checking on the config space address, lack of which currently leads to an exploitable bug.
This patch corrects the problem.
--- hw/spapr_pci.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
hw/spapr_pci.c +4 -4
@@ -82,7 +82,7 @@ static void rtas_ibm_read_pci_config(sPAPREnvironment *spapr, } size =rtas_ld (args,3 ); addr =rtas_pci_cfgaddr (rtas_ld (args,0 )); - val =pci_default_read_config (dev, addr, size); + val =pci_host_config_read_common (dev, addr,pci_config_size (dev), size);rtas_st (rets,0 ,0 );rtas_st (rets,1 , val); }
@@ -101,7 +101,7 @@ static void rtas_read_pci_config(sPAPREnvironment *spapr, } size =rtas_ld (args,1 ); addr =rtas_pci_cfgaddr (rtas_ld (args,0 )); - val =pci_default_read_config (dev, addr, size); + val =pci_host_config_read_common (dev, addr,pci_config_size (dev), size);rtas_st (rets,0 ,0 );rtas_st (rets,1 , val); }
@@ -122,7 +122,7 @@ static void rtas_ibm_write_pci_config(sPAPREnvironment *spapr, val =rtas_ld (args,4 ); size =rtas_ld (args,3 ); addr =rtas_pci_cfgaddr (rtas_ld (args,0 )); -pci_default_write_config (dev, addr, val, size); +pci_host_config_write_common (dev, addr,pci_config_size (dev), val, size);rtas_st (rets,0 ,0 ); }
@@ -141,7 +141,7 @@ static void rtas_write_pci_config(sPAPREnvironment *spapr, val =rtas_ld (args,2 ); size =rtas_ld (args,1 ); addr =rtas_pci_cfgaddr (rtas_ld (args,0 )); -pci_default_write_config (dev, addr, val, size); +pci_host_config_write_common (dev, addr,pci_config_size (dev), val, size);rtas_st (rets,0 ,0 ); }
Signature · 1 line
1.7.7.3
PATCH 8/9 pseries: Don't try to munmap() a malloc()ed TCE table
In reply to David GibsonFor the pseries machine, TCE (IOMMU) tables can either be directly malloc()ed in qemu or, when running on a KVM which supports it, mmap()ed from a KVM ioctl. The latter option is used when available, because it allows the (frequent bottlenext) H_PUT_TCE hypercall to be KVM accelerated. However, even when KVM is persent, TCE acceleration is not always possible. Only KVM HV supports this ioctl(), not KVM PR, or the kernel could run out of contiguous memory to allocate the new table. In this case we need to fall back on the malloc()ed table.
When a device is removed, and we need to remove the TCE table, we need to either munmap() or free() the table as appropriate for how it was allocated. The code is supposed to do that, but we buggily fail to initialize the tcet->fd variable in the malloc() case, which is used as a flag to determine which is the right choice.
This patch fixes the bug, and cleans up error messages relating to this path while we're at it.
--- target-ppc/kvm.c | 12 ++++++++++-- 1 files changed, 10 insertions(+), 2 deletions(-)
target-ppc/kvm.c +10 -2
@@ -839,12 +839,18 @@ void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd)int fd;void *table; +/* Must set fd to -1 so we don't try to munmap when called for +* destroying the table, which the upper layers -will- do +*/ + *pfd =-1 ;if (!cap_spapr_tce) {return NULL ; } fd =kvm_vm_ioctl (kvm_state,KVM_CREATE_SPAPR_TCE , &args);if (fd <0 ) { +fprintf (stderr,"KVM: Failed to create TCE table for liobn 0x%x\n" , + liobn);return NULL ; }
@@ -853,6 +859,8 @@ void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd) table =mmap (NULL , len,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 );if (table ==MAP_FAILED ) { +fprintf (stderr,"KVM: Failed to map TCE table for liobn 0x%x\n" , + liobn);close (fd);return NULL ; }
@@ -872,8 +880,8 @@ int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t window_size) len = (window_size /SPAPR_VIO_TCE_PAGE_SIZE )*sizeof (VIOsPAPR_RTCE);if ((munmap (table, len) <0 ) || (close (fd) <0 )) { -fprintf (stderr,"KVM: Unexpected error removing KVM SPAPR TCE " -"table: %s" ,strerror (errno)); +fprintf (stderr,"KVM: Unexpected error removing TCE table: %s" , +strerror (errno));/* Leak the table */ }
Signature · 1 line
1.7.7.3
PATCH 9/9 pseries: SLOF PCI flag day
In reply to David GibsonFrom: Benjamin Herrenschmidt <[email protected]>
Currently on the pseries machine the SLOF firmware is used normally, but we bypass it when -kernel is specified. Having these two different boot paths can cause some confusion.
In particular at present we need to "probe" the (emulated) PCI bus and produce device tree nodes for the PCI devices in qemu, for the -kernel case. In the SLOF case, it takes the device tree from qemu adds some stuff to it then passes it on to the kernel.
It's been decided that a better approach is to always boot through SLOF, even when using -kernel. WIth this approach we can leave PCI probing and device node creation to SLOF in all cases which removes a bunch of code in qemu, and avoids iterating the PCI devices from the machine specific init code which we're not supposed to do.
This patch changes qemu to always boot through SLOF, and not to create PCI nodes. Simultaneously it updates the included version of SLOF (submodule and binary image) to one which supports (and requires) the new approach.
The new SLOF version also includes a number of unrelated enhancements:
support for booting from virtio-pci devices and e1000, greatly improved FCode support and many bugfixes. It also makes SLOF ready to be used even when specifying a kernel on the qemu command line.
--- hw/spapr.c | 135 ++++++++++++++++++++++++++++++++++++------------------ hw/spapr_pci.c | 134 +++++------------------------------------------------ pc-bios/README | 2 +- pc-bios/slof.bin | Bin 738744 -> 869584 bytes roms/SLOF | 2 +- 5 files changed, 105 insertions(+), 168 deletions(-)
hw/spapr.c +91 -44
@@ -50,19 +50,29 @@#include <libfdt.h> -#define KERNEL_LOAD_ADDR 0x00000000 -#define INITRD_LOAD_ADDR 0x02800000 +/* SLOF memory layout: +* +* SLOF raw image loaded at 0, copies its romfs right below the flat +* device-tree, then position SLOF itself 31M below that +* +* So we set FW_OVERHEAD to 40MB which should account for all of that +* and more +* +* We load our kernel at 4M, leaving space for SLOF initial image +*/ #define FDT_MAX_SIZE 0x10000#define RTAS_MAX_SIZE 0x10000#define FW_MAX_SIZE 0x400000#define FW_FILE_NAME "slof.bin" +#define FW_OVERHEAD 0x2800000 +#define KERNEL_LOAD_ADDR FW_MAX_SIZE -#define MIN_RMA_SLOF 128UL +#define MIN_RMA_SLOF 128UL#define TIMEBASE_FREQ 512000000ULL#define MAX_CPUS 256 -#define XICS_IRQS 1024 +#define XICS_IRQS 1024#define SPAPR_PCI_BUID 0x800000020000001ULL#define SPAPR_PCI_MEM_WIN_ADDR (0x10000000000ULL + 0xA0000000)
@@ -139,6 +149,7 @@ static void *spapr_create_fdt_skel(const char *cpu_model,target_phys_addr_t rma_size, target_phys_addr_t initrd_base, target_phys_addr_t initrd_size, + target_phys_addr_t kernel_size, const char *boot_device, const char *kernel_cmdline, long hash_shift)
@@ -176,6 +187,12 @@ static void *spapr_create_fdt_skel(const char *cpu_model, fdt =g_malloc0 (FDT_MAX_SIZE );_FDT ((fdt_create (fdt,FDT_MAX_SIZE ))); +if (kernel_size) { +_FDT ((fdt_add_reservemap_entry (fdt,KERNEL_LOAD_ADDR , kernel_size))); + } +if (initrd_size) { +_FDT ((fdt_add_reservemap_entry (fdt, initrd_base, initrd_size))); + }_FDT ((fdt_finish_reservemap (fdt)));/* Root node */
@@ -197,15 +214,13 @@ static void *spapr_create_fdt_skel(const char *cpu_model, &start_prop, sizeof(start_prop))));_FDT ((fdt_property (fdt,"linux,initrd-end" , &end_prop,sizeof (end_prop)))); -_FDT ((fdt_property_string (fdt,"qemu,boot-device" , boot_device))); +if (kernel_size) { +uint64_t kprop[2 ] = {cpu_to_be64 (KERNEL_LOAD_ADDR ), +cpu_to_be64 (kernel_size) }; -/* -* Because we don't always invoke any firmware, we can't rely on -* that to do BAR allocation. Long term, we should probably do -* that ourselves, but for now, this setting (plus advertising the -* current BARs as 0) causes sufficiently recent kernels to to the -* BAR assignment themselves */ -_FDT ((fdt_property_cell (fdt,"linux,pci-probe-only" ,0 ))); +_FDT ((fdt_property (fdt,"qemu,boot-kernel" , &kprop,sizeof (kprop)))); + } +_FDT ((fdt_property_string (fdt,"qemu,boot-device" , boot_device)));_FDT ((fdt_end_node (fdt)));
@@ -445,6 +460,12 @@ static void spapr_finalize_fdt(sPAPREnvironment *spapr,_FDT ((fdt_pack (fdt))); +if (fdt_totalsize (fdt) >FDT_MAX_SIZE ) { +hw_error ("FDT too big ! 0x%x bytes (max is 0x%x)\n" , +fdt_totalsize (fdt),FDT_MAX_SIZE ); +exit (1 ); + } +cpu_physical_memory_write (fdt_addr, fdt,fdt_totalsize (fdt));g_free (fdt);
@@ -494,8 +515,9 @@ static void ppc_spapr_init(ram_addr_t ram_size,MemoryRegion *sysmem =get_system_memory ();MemoryRegion *ram =g_new (MemoryRegion,1 );target_phys_addr_t rma_alloc_size, rma_size; -uint32_t initrd_base; -long kernel_size, initrd_size, fw_size; +uint32_t initrd_base =0 ; +long kernel_size =0 , initrd_size =0 ; +long load_limit, rtas_limit, fw_size;long pteg_shift =17 ;char *filename;
@@ -517,11 +539,13 @@ static void ppc_spapr_init(ram_addr_t ram_size, rma_size = ram_size; } -/* We place the device tree just below either the top of the RMA, +/* We place the device tree and RTAS just below either the top of the RMA, * or just below 2GB, whichever is lowere, so that it can be * processed with 32-bit real mode code if necessary */ - spapr->fdt_addr =MIN (rma_size,0x80000000 ) -FDT_MAX_SIZE ; - spapr->rtas_addr = spapr->fdt_addr -RTAS_MAX_SIZE ; + rtas_limit =MIN (rma_size,0x80000000 ); + spapr->rtas_addr = rtas_limit -RTAS_MAX_SIZE ; + spapr->fdt_addr = spapr->rtas_addr -FDT_MAX_SIZE ; + load_limit = spapr->fdt_addr -FW_OVERHEAD ;/* init CPUs */ if (cpu_model ==NULL ) {
@@ -577,13 +601,19 @@ static void ppc_spapr_init(ram_addr_t ram_size, filename =qemu_find_file (QEMU_FILE_TYPE_BIOS ,"spapr-rtas.bin" ); spapr->rtas_size =load_image_targphys (filename, spapr->rtas_addr, - ram_size - spapr->rtas_addr); + rtas_limit - spapr->rtas_addr);if (spapr->rtas_size <0 ) {hw_error ("qemu: could not load LPAR rtas '%s'\n" , filename);exit (1 ); } +if (spapr->rtas_size >RTAS_MAX_SIZE ) { +hw_error ("RTAS too big ! 0x%lx bytes (max is 0x%x)\n" , + spapr->rtas_size,RTAS_MAX_SIZE ); +exit (1 ); + }g_free (filename); +/* Set up Interrupt Controller */ spapr->icp =xics_system_init (XICS_IRQS ); spapr->next_irq =16 ;
@@ -622,6 +652,20 @@ static void ppc_spapr_init(ram_addr_t ram_size,spapr_vscsi_create (spapr->vio_bus,0x2000 + i); } +if (rma_size < (MIN_RMA_SLOF <<20 )) { +fprintf (stderr,"qemu: pSeries SLOF firmware requires >= " +"%ldM guest RMA (Real Mode Area memory)\n" ,MIN_RMA_SLOF ); +exit (1 ); + } + +fprintf (stderr,"sPAPR memory map:\n" ); +fprintf (stderr,"RTAS : 0x%08lx..%08lx\n" , + (unsigned long )spapr->rtas_addr, + (unsigned long )(spapr->rtas_addr + spapr->rtas_size -1 )); +fprintf (stderr,"FDT : 0x%08lx..%08lx\n" , + (unsigned long )spapr->fdt_addr, + (unsigned long )(spapr->fdt_addr +FDT_MAX_SIZE -1 )); +if (kernel_filename) {uint64_t lowaddr =0 ;
@@ -630,57 +674,60 @@ static void ppc_spapr_init(ram_addr_t ram_size,if (kernel_size <0 ) { kernel_size =load_image_targphys (kernel_filename,KERNEL_LOAD_ADDR , - ram_size -KERNEL_LOAD_ADDR ); + load_limit -KERNEL_LOAD_ADDR ); }if (kernel_size <0 ) {fprintf (stderr,"qemu: could not load kernel '%s'\n" , kernel_filename);exit (1 ); } +fprintf (stderr,"Kernel : 0x%08x..%08lx\n" , +KERNEL_LOAD_ADDR ,KERNEL_LOAD_ADDR + kernel_size -1 );/* load initrd */ if (initrd_filename) { - initrd_base =INITRD_LOAD_ADDR ; +/* Try to locate the initrd in the gap between the kernel +* and the firmware. Add a bit of space just in case +*/ + initrd_base = (KERNEL_LOAD_ADDR + kernel_size +0x1ffff ) & ~0xffff ; initrd_size =load_image_targphys (initrd_filename, initrd_base, - ram_size - initrd_base); + load_limit - initrd_base);if (initrd_size <0 ) {fprintf (stderr,"qemu: could not load initial ram disk '%s'\n" , initrd_filename);exit (1 ); } +fprintf (stderr,"Ramdisk : 0x%08lx..%08lx\n" , + (long )initrd_base, (long )(initrd_base + initrd_size -1 )); }else { initrd_base =0 ; initrd_size =0 ; } + } - spapr->entry_point =KERNEL_LOAD_ADDR ; - }else { -if (rma_size < (MIN_RMA_SLOF <<20 )) { -fprintf (stderr,"qemu: pSeries SLOF firmware requires >= " -"%ldM guest RMA (Real Mode Area memory)\n" ,MIN_RMA_SLOF ); -exit (1 ); - } - filename =qemu_find_file (QEMU_FILE_TYPE_BIOS ,FW_FILE_NAME ); - fw_size =load_image_targphys (filename,0 ,FW_MAX_SIZE ); -if (fw_size <0 ) { -hw_error ("qemu: could not load LPAR rtas '%s'\n" , filename); -exit (1 ); - } -g_free (filename); - spapr->entry_point =0x100 ; - initrd_base =0 ; - initrd_size =0 ; - -/* SLOF will startup the secondary CPUs using RTAS, -rather than expecting a kexec() style entry */ -for (env = first_cpu; env !=NULL ; env = env->next_cpu) { - env->halted =1 ; - } + filename =qemu_find_file (QEMU_FILE_TYPE_BIOS ,FW_FILE_NAME ); + fw_size =load_image_targphys (filename,0 ,FW_MAX_SIZE ); +if (fw_size <0 ) { +hw_error ("qemu: could not load LPAR rtas '%s'\n" , filename); +exit (1 ); + } +g_free (filename); +fprintf (stderr,"Firmware load : 0x%08x..%08lx\n" , +0 , fw_size); +fprintf (stderr,"Firmware runtime : 0x%08lx..%08lx\n" , + load_limit, (unsigned long )spapr->fdt_addr); + + spapr->entry_point =0x100 ; + +/* SLOF will startup the secondary CPUs using RTAS */ +for (env = first_cpu; env !=NULL ; env = env->next_cpu) { + env->halted =1 ; }/* Prepare the device tree */ spapr->fdt_skel =spapr_create_fdt_skel (cpu_model, rma_size, initrd_base, initrd_size, + kernel_size, boot_device, kernel_cmdline, pteg_shift +7 );assert (spapr->fdt_skel !=NULL );
hw/spapr_pci.c +12 -122
168 lines not on this page · raw message
pc-bios/README +1 -1
@@ -17,7 +17,7 @@ - SLOF (Slimline Open Firmware) is a free IEEE 1275 Open Firmware implementation for certain IBM POWER hardware. The sources are at https://github.com/dgibson/SLOF, and the image currently in qemu is - built from git tag qemu-slof-20111013. + built from git tag qemu-slof-20120111.1. - sgabios (the Serial Graphics Adapter option ROM) provides a means for legacy x86 software to communicate with an attached serial console as
pc-bios/slof.bin binary +0 -0
roms/SLOF +1 -1
@@ -1 +1 @@ -Subproject commit 32e3430c018ceb8413cb808477449d1968c42497 +Subproject commit ab062ff3b37c39649f2b0d94ed607adc6f6b3c7d
Signature · 1 line
1.7.7.3
Quoted 11 lines · David Gibson
This series has two parts. THe first has a few bugfixes for generic qemu code. The second has a number of pseries machine specific enhancements and bugfixes. They are included in the one series because some of the pseries specific patches rely on the generic bugfixes (in the sense that they expose the bugs more badly than they were previously).
I've posted some of the generic fixes before, this series respins the first one (
load_image_targphys()) with a minor tweak, the rest are unchanged.
I put all of them in my tree for now, waiting for you to figure the PCI host limit piece out with Michael.
Alex