fw_cfg: spec update, misc. cleanup, optimize read
v4 cover fw_cfg: spec update, misc. cleanup, optimize read
New since v3:
- Patches 1..3 unchanged
- Inserted new patch at position #4: avoid calculating an entry pointer for the current item when s->cur_entry is FW_CFG_INVALID
- Patch 5 (formerly #4) now has the generic read method correctly shift-left (adding 0 padding on the right) in the event that the current item payload is exhausted in the course of a multi-byte read operation (Laszlo, thanks again for catching that!)
- Patch 6 (formerly #5) is a rebased version of its former self, but with no actual significant modifications.
Thanks,
--Gabriel
Quoted 6 lines
New since v2:
- Patches 1-3: updated to address Laszlo's suggestions for better and more accurate change descriptions in commit logs, comments, etc.
Quoted 32 lines
- Patch 4: Per Laszlo's recommendation, this has been split into two separate components for improved legibility:
- Patch 4/5: Introduces the new generic read method, and replaces the existing MMIO logic
- Patch 5/5: Replaces the IOPort read logic, and cleans up the remaining unused bits of code.
Quoted 23 lines
This series' main purpose is to update (and simplify) the specified read callback behavior. An earlier standalone patch to move qemu function call API documentation into
fw_cfg.hshould logically be part of the series.Here's the summary of what each patch does:
- Patch 1/4 is an updated version of the standalone v1 patch I sent out earlier; it moves all the qemu-internal host-side function call api documentation out of
docs/specs/fw_cfg.txt, and into thefw_cfg.hheader file, next to the prototype of each documented api function.- Patch 2/4 modifies the specified behavior of read callbacks (from being invoked once per byte read, to being invoked once, before ANY data is read, specifically once each time an item is selected).
- Patch 3/4 additionally removes the now-redundant offset argument from the read callback prototype.
- Finally, 4/4 consolidates (non-DMA) reads, minimizing the number of times redundant sanity checks are performed, particularly for wide (> byte) sized reads.
Gabriel L. Somlo (6):
fw_cfg: move internal function call docs to header file
fw_cfg: amend callback behavior spec to once per select
fw_cfg: remove offset argument from callback prototype
fw_cfg: avoid calculating invalid current entry pointer
fw_cfg: add generic non-DMA read method
fw_cfg: replace ioport data read with generic method
docs/specs/fw_cfg.txt | 85 +----------------------------- hw/arm/virt-acpi-build.c | 2 +- hw/i386/acpi-build.c | 2 +- hw/nvram/fw_cfg.c | 74 +++++++++++++-------------- include/hw/nvram/fw_cfg.h | 128 +++++++++++++++++++++++++++++++++++++++++++++- trace-events | 2 +- 6 files changed, 165 insertions(+), 128 deletions(-)
Signature · 1 line
2.4.3
PATCH v4 1/6 fw_cfg: move internal function call docs to header file
Move documentation for fw_cfg functions internal to qemu from docs/specs/fw_cfg.txt to the fw_cfg.h header file, next to their prototype declarations, formatted as doc-comments.
NOTE: Documentation for fw_cfg_add_callback() is completely dropped by this patch, as that function has been eliminated by commit 023e3148.
--- docs/specs/fw_cfg.txt | 85 +----------------------------- include/hw/nvram/fw_cfg.h | 129 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 84 deletions(-)
docs/specs/fw_cfg.txt +1 -84
@@ -192,90 +192,7 @@ To check the result, read the "control" field: today due to implementation not being async, but may in the future). -= Host-side API = - -The following functions are available to the QEMU programmer for adding -data to a fw_cfg device during guest initialization (see fw_cfg.h for -each function's complete prototype): - -== fw_cfg_add_bytes() == - -Given a selector key value, starting pointer, and size, create an item -as a raw "blob" of the given size, available by selecting the given key. -The data referenced by the starting pointer is only linked, NOT copied, -into the data structure of the fw_cfg device. - -== fw_cfg_add_string() == - -Instead of a starting pointer and size, this function accepts a pointer -to a NUL-terminated ascii string, and inserts a newly allocated copy of -the string (including the NUL terminator) into the fw_cfg device data -structure. - -== fw_cfg_add_iXX() == - -Insert an XX-bit item, where XX may be 16, 32, or 64. These functions -will convert a 16-, 32-, or 64-bit integer to little-endian, then add -a dynamically allocated copy of the appropriately sized item to fw_cfg -under the given selector key value. - -== fw_cfg_modify_iXX() == - -Modify the value of an XX-bit item (where XX may be 16, 32, or 64). -Similarly to the corresponding fw_cfg_add_iXX() function set, convert -a 16-, 32-, or 64-bit integer to little endian, create a dynamically -allocated copy of the required size, and replace the existing item at -the given selector key value with the newly allocated one. The previous -item, assumed to have been allocated during an earlier call to -fw_cfg_add_iXX() or fw_cfg_modify_iXX() (of the same width XX), is freed -before the function returns. - -== fw_cfg_add_file() == - -Given a filename (i.e., fw_cfg item name), starting pointer, and size, -create an item as a raw "blob" of the given size. Unlike fw_cfg_add_bytes() -above, the next available selector key (above 0x0020, FW_CFG_FILE_FIRST) -will be used, and a new entry will be added to the file directory structure -(at key 0x0019), containing the item name, blob size, and automatically -assigned selector key value. The data referenced by the starting pointer -is only linked, NOT copied, into the fw_cfg data structure. - -== fw_cfg_add_file_callback() == - -Like fw_cfg_add_file(), but additionally sets pointers to a callback -function (and opaque argument), which will be executed host-side by -QEMU each time a byte is read by the guest from this particular item. - -NOTE: The callback function is given the opaque argument set by -fw_cfg_add_file_callback(), but also the current data offset, -allowing it the option of only acting upon specific offset values -(e.g., 0, before the first data byte of the selected item is -returned to the guest). - -== fw_cfg_modify_file() == - -Given a filename (i.e., fw_cfg item name), starting pointer, and size, -completely replace the configuration item referenced by the given item -name with the new given blob. If an existing blob is found, its -callback information is removed, and a pointer to the old data is -returned to allow the caller to free it, helping avoid memory leaks. -If a configuration item does not already exist under the given item -name, a new item will be created as with fw_cfg_add_file(), and NULL -is returned to the caller. In any case, the data referenced by the -starting pointer is only linked, NOT copied, into the fw_cfg data -structure. - -== fw_cfg_add_callback() == - -Like fw_cfg_add_bytes(), but additionally sets pointers to a callback -function (and opaque argument), which will be executed host-side by -QEMU each time a guest-side write operation to this particular item -completes fully overwriting the item's data. - -NOTE: This function is deprecated, and will be completely removed -starting with QEMU v2.4. - -== Externally Provided Items == += Externally Provided Items = As of v2.4, "file" fw_cfg items (i.e., items with selector keys above FW_CFG_FILE_FIRST, and with a corresponding entry in the fw_cfg file
include/hw/nvram/fw_cfg.h +129 -0
148 lines not on this page · raw message
Signature · 1 line
2.4.3
PATCH v4 2/6 fw_cfg: amend callback behavior spec to once per select
In reply to Gabriel L. SomloCurrently, the fw_cfg internal API specifies that if an item was set up with a read callback, the callback must be run each time a byte is read from the item. This behavior is both wasteful (most items do not have a read callback set), and impractical for bulk transfers (e.g., DMA read).
At the time of this writing, the only items configured with a callback are "/etc/table-loader", "/etc/acpi/tables", and "/etc/acpi/rsdp". They all share the same callback functions: virt_acpi_build_update() on ARM (in hw/arm/virt-acpi-build.c), and acpi_build_update() on i386 (in hw/i386/acpi.c). Both of these callbacks are one-shot (i.e. they return without doing anything at all after the first time they are invoked with a given build_state; since build_state is also shared across all three items mentioned above, the callback only ever runs *once*, the first time either of the listed items is read).
This patch amends the specification for fw_cfg_add_file_callback() to state that any available read callback will only be invoked once each time the item is selected. This change has no practical effect on the current behavior of QEMU, and it enables us to significantly optimize the behavior of fw_cfg reads during guest firmware setup, eliminating a large amount of redundant callback checks and invocations.
--- hw/nvram/fw_cfg.c | 19 ++++++++++--------- include/hw/nvram/fw_cfg.h | 10 +++------- 2 files changed, 13 insertions(+), 16 deletions(-)
hw/nvram/fw_cfg.c +10 -9
@@ -252,7 +252,8 @@ static void fw_cfg_write(FWCfgState *s, uint8_t value)static int fw_cfg_select (FWCfgState *s,uint16_t key) { -int ret; +int arch, ret; +FWCfgEntry *e; s->cur_offset =0 ;if ((key &FW_CFG_ENTRY_MASK ) >=FW_CFG_MAX_ENTRY ) {
@@ -261,6 +262,12 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key) }else { s->cur_entry = key; ret =1 ; +/* entry successfully selected, now run callback if present */ + arch = !!(key &FW_CFG_ARCH_LOCAL ); + e = &s->entries[arch][key &FW_CFG_ENTRY_MASK ]; +if (e->read_callback) { + e->read_callback (e->callback_opaque, s->cur_offset); + } }trace_fw_cfg_select (s, key, ret);
@@ -276,9 +283,6 @@ static uint8_t fw_cfg_read(FWCfgState *s)if (s->cur_entry ==FW_CFG_INVALID || !e->data || s->cur_offset >= e->len) ret =0 ;else { -if (e->read_callback) { - e->read_callback (e->callback_opaque, s->cur_offset); - } ret = e->data[s->cur_offset++]; }
@@ -371,10 +375,6 @@ static void fw_cfg_dma_transfer(FWCfgState *s) len = (e->len - s->cur_offset); } -if (e->read_callback) { - e->read_callback (e->callback_opaque, s->cur_offset); - } -/* If the access is not a read access, it will be a skip access, * tested before. */
@@ -513,7 +513,8 @@ static void fw_cfg_reset(DeviceState *d) {FWCfgState *s =FW_CFG (d); -fw_cfg_select (s,0 ); +/* we never register a read callback for FW_CFG_SIGNATURE */ +fw_cfg_select (s,FW_CFG_SIGNATURE ); }/* Save restore 32 bit int as uint16_t
include/hw/nvram/fw_cfg.h +3 -7
@@ -183,13 +183,9 @@ void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,* structure residing at key value FW_CFG_FILE_DIR, containing the item name, * data size, and assigned selector key value. * Additionally, set a callback function (and argument) to be called each -* time a byte is read by the guest from this particular item, or, in the -* case of DMA, each time a read or skip request overlaps with the valid -* offset range of the item. -* NOTE: In addition to the opaque argument set here, the callback function -* takes the current data offset as an additional argument, allowing it the -* option of only acting upon specific offset values (e.g., 0, before the -* first data byte of the selected item is returned to the guest). +* time this item is selected (by having its selector key either written to +* the fw_cfg control register, or passed to QEMU in FWCfgDmaAccess.control +* with FW_CFG_DMA_CTL_SELECT). */ void fw_cfg_add_file_callback(FWCfgState *s, const char *filename, FWCfgReadCallback callback, void *callback_opaque,
Signature · 1 line
2.4.3
PATCH v4 3/6 fw_cfg: remove offset argument from callback prototype
In reply to Gabriel L. SomloRead callbacks are now only invoked at item selection, before any data is read. As such, the value of the offset argument passed to the callback will always be 0. Also, the two callback instances currently in use both leave their offset argument unused.
This patch removes the offset argument from the fw_cfg read callback prototype, and from the currently available instances. The unused (write) callback prototype is also removed (write support was removed earlier, in commit 023e3148).
--- hw/arm/virt-acpi-build.c | 2 +- hw/i386/acpi-build.c | 2 +- hw/nvram/fw_cfg.c | 2 +- include/hw/nvram/fw_cfg.h | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-)
hw/arm/virt-acpi-build.c +1 -1
@@ -626,7 +626,7 @@ static void acpi_ram_update(MemoryRegion *mr, GArray *data)memory_region_set_dirty (mr,0 , size); } -static void virt_acpi_build_update (void *build_opaque,uint32_t offset) +static void virt_acpi_build_update (void *build_opaque) {AcpiBuildState *build_state = build_opaque;AcpiBuildTables tables;
hw/i386/acpi-build.c +1 -1
@@ -1818,7 +1818,7 @@ static void acpi_ram_update(MemoryRegion *mr, GArray *data)memory_region_set_dirty (mr,0 , size); } -static void acpi_build_update (void *build_opaque,uint32_t offset) +static void acpi_build_update (void *build_opaque) {AcpiBuildState *build_state = build_opaque;AcpiBuildTables tables;
hw/nvram/fw_cfg.c +1 -1
@@ -266,7 +266,7 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key) arch = !!(key &FW_CFG_ARCH_LOCAL ); e = &s->entries[arch][key &FW_CFG_ENTRY_MASK ];if (e->read_callback) { - e->read_callback (e->callback_opaque, s->cur_offset); + e->read_callback (e->callback_opaque); } }
include/hw/nvram/fw_cfg.h +1 -2
@@ -70,8 +70,7 @@ typedef struct FWCfgDmaAccess {uint64_t address; }QEMU_PACKED FWCfgDmaAccess; -typedef void (*FWCfgCallback )(void *opaque,uint8_t *data); -typedef void (*FWCfgReadCallback )(void *opaque,uint32_t offset); +typedef void (*FWCfgReadCallback )(void *opaque);/** * fw_cfg_add_bytes:
Signature · 1 line
2.4.3
PATCH v4 4/6 fw_cfg: avoid calculating invalid current entry pointer
In reply to Gabriel L. SomloWhen calculating a pointer to the currently selected fw_cfg item, the following is used:
FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
When s->cur_entry is FW_CFG_INVALID, we are calculating the address of a non-existent element in s->entries[arch][...], which is undefined.
This patch ensures the resulting entry pointer is se to NULL whenever s->cur_entry is FW_CFG_INVALID.
--- hw/nvram/fw_cfg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
hw/nvram/fw_cfg.c +4 -2
@@ -277,7 +277,8 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key)static uint8_t fw_cfg_read (FWCfgState *s) {int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); -FWCfgEntry *e = &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ];uint8_t ret;if (s->cur_entry ==FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
@@ -342,7 +343,8 @@ static void fw_cfg_dma_transfer(FWCfgState *s) } arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); - e = &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; + e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ];if (dma.control &FW_CFG_DMA_CTL_READ ) { read =1 ;
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v4 4/6] fw_cfg: avoid calculating invalid current entry pointer
Quoted 9 lines · Gabriel L. Somlo
When calculating a pointer to the currently selected
fw_cfgitem, the following is used:FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];When
s->cur_entryisFW_CFG_INVALID, we are calculating the address of a non-existent element in s->entries[arch][...], which is undefined.This patch ensures the resulting entry pointer is se to NULL whenever
se[t] to NULL
Quoted 25 lines
s->cur_entryisFW_CFG_INVALID.Reported-by Laszlo Ersek <[email protected]>Cc Marc Marí <[email protected]>Signed-off-by Gabriel Somlo <[email protected]>--- hw/nvram/fw_cfg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)hw/nvram/fw_cfg.c +2
-1@@ -277,7 +277,8 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key)static uint8_t fw_cfg_read (FWCfgState *s) {int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); -FWCfgEntry *e = &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ];uint8_t ret;if (s->cur_entry ==FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
hw/nvram/fw_cfg.c static void fw_cfg_dma_transfer(FWCfgState *s) · line 350
Quoted 9 lines
hw/nvram/fw_cfg.c +2
-1@@ -342,7 +343,8 @@ static void fw_cfg_dma_transfer(FWCfgState *s) } arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); - e = &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; + e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ];if (dma.control &FW_CFG_DMA_CTL_READ ) { read =1 ;
PATCH v4 5/6 fw_cfg: add generic non-DMA read method
In reply to Gabriel L. SomloIntroduce fw_cfg_data_read(), a generic read method which works on all access widths (1 through 8 bytes, inclusive), and can be used during both IOPort and MMIO read accesses.
To maintain legibility, only fw_cfg_data_mem_read() (the MMIO data read method) is replaced by this patch. The new method essentially unwinds the fw_cfg_data_mem_read() + fw_cfg_read() combo, but without unnecessarily repeating all the validity checks performed by the latter on each byte being read.
This patch also modifies the trace_fw_cfg_read prototype to accept a 64-bit value argument, allowing it to work properly with the new read method, but also remain backward compatible with existing call sites.
--- hw/nvram/fw_cfg.c | 44 ++++++++++++++++++++++++++++++-------------- trace-events | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-)
hw/nvram/fw_cfg.c +30 -14
@@ -274,6 +274,35 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key)return ret; } +static uint64_t fw_cfg_data_read (void *opaque,hwaddr addr,unsigned size) +{ +FWCfgState *s = opaque; +int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +uint64_t value =0 ; + +assert (size <=sizeof (value)); +if (s->cur_entry !=FW_CFG_INVALID && e->data) { +/* The least significant 'size' bytes of the return value are +* expected to contain a string preserving portion of the item +* data, padded with zeros to the right in case we run out early. +*/ +while (size && s->cur_offset < e->len) { + value = (value <<8 ) | e->data[s->cur_offset++]; + size--; + } +/* If size is still not zero, we *did* run out early, so continue +* left-shifting, to add the appropriate number of padding zeros +* on the right. +*/ + value <<=8 * size; + } + +trace_fw_cfg_read (s, value); +return value; +} +static uint8_t fw_cfg_read (FWCfgState *s) {int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL );
@@ -291,19 +320,6 @@ static uint8_t fw_cfg_read(FWCfgState *s)return ret; } -static uint64_t fw_cfg_data_mem_read (void *opaque,hwaddr addr, -unsigned size) -{ -FWCfgState *s = opaque; -uint64_t value =0 ; -unsigned i; - -for (i =0 ; i < size; ++i) { - value = (value <<8 ) |fw_cfg_read (s); - } -return value; -} -static void fw_cfg_data_mem_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {
@@ -485,7 +501,7 @@ static const MemoryRegionOps fw_cfg_ctl_mem_ops = { };static const MemoryRegionOps fw_cfg_data_mem_ops = { - .read = fw_cfg_data_mem_read, + .read = fw_cfg_data_read, .write = fw_cfg_data_mem_write, .endianness =DEVICE_BIG_ENDIAN , .valid = {
trace-events +1 -1
@@ -196,7 +196,7 @@ ecc_diag_mem_readb(uint64_t addr, uint32_t ret) "Read diagnostic %"PRId64"= %02x # hw/nvram/fw_cfg.c fw_cfg_select(void *s, uint16_t key, int ret) "%p key %d = %d" -fw_cfg_read(void *s, uint8_t ret) "%p = %d" +fw_cfg_read(void *s, uint64_t ret) "%p = %"PRIx64 fw_cfg_add_file(void *s, int index, char *name, size_t len) "%p #%d: %s (%zd bytes)" # hw/block/hd-geometry.c
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v4 5/6] fw_cfg: add generic non-DMA read method
Quoted 50 lines · Gabriel L. Somlo
Introduce
fw_cfg_data_read(), a generic read method which works on all access widths (1 through 8 bytes, inclusive), and can be used during both IOPort and MMIO read accesses.To maintain legibility, only
fw_cfg_data_mem_read()(the MMIO data read method) is replaced by this patch. The new method essentially unwinds thefw_cfg_data_mem_read()+fw_cfg_read()combo, but without unnecessarily repeating all the validity checks performed by the latter on each byte being read.This patch also modifies the
trace_fw_cfg_readprototype to accept a 64-bit value argument, allowing it to work properly with the new read method, but also remain backward compatible with existing call sites.Cc Laszlo Ersek <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Marc Marí <[email protected]>Signed-off-by Gabriel Somlo <[email protected]>--- hw/nvram/fw_cfg.c | 44 ++++++++++++++++++++++++++++++-------------- trace-events | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-)hw/nvram/fw_cfg.c +18
-0@@ -274,6 +274,35 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key)return ret; } +static uint64_t fw_cfg_data_read (void *opaque,hwaddr addr,unsigned size) +{ +FWCfgState *s = opaque; +int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +uint64_t value =0 ; + +assert (size <=sizeof (value)); +if (s->cur_entry !=FW_CFG_INVALID && e->data) { +/* The least significant 'size' bytes of the return value are +* expected to contain a string preserving portion of the item +* data, padded with zeros to the right in case we run out early. +*/ +while (size && s->cur_offset < e->len) { + value = (value <<8 ) | e->data[s->cur_offset++]; + size--; + }
hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key) · line 304
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+/* If size is still not zero, we *did* run out early, so continue +* left-shifting, to add the appropriate number of padding zeros +* on the right. +*/ + value <<=8 * size; + } + +trace_fw_cfg_read (s, value); +return value; +}
With the wording you proposed in
<http://thread.gmane.org/gmane.comp.emulators.qemu/373165/focus=373507>,
this looks okay.
... Except my (2a) proposal wasn't entirely correct, and now you get to fix it up for v5. :( Apologies. (It is a different experience when you see the code in full.)
Namely, consider the case when this code is entered with:
(size == 8 && s->cur_offset == e->len)
(Which is possible if the guest makes a qword read access just after reading the full blob.)
In this case, the loop won't be entered at all (which is okay), but then you'll have:
uint64_t << 64
which is undefined behavior. ("If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.")
So please protect the final shift with "if (size < 8)".
*Alternatively*, you could restrict the *outer* condition, i.e.,
s->cur_entry != FW_CFG_INVALID && e->data
with (s->cur_offset < e->len).
... And then you can even replace the "while" with a "do" loop. (Because both (size > 0) and (s->cur_offset < e->len) will be true if the loop is reached at all.)
Just the code, without comments:
assert(size <= sizeof(value));
assert(size > 0);
if (s->cur_entry != FW_CFG_INVALID && e->data &&
s->cur_offset < e->len) {
/* ... */
do {
value = (value << 8) | e->data[s->cur_offset++];
size--;
} while (size && s->cur_offset < e->len);
/* ... */
value <<= 8 * size;
}This makes it clear that "size" is strictly smaller than sizeof(value) when the shift is reached.
I'll let you choose between the two alternatives. :)
Thanks, and I'm sorry.
Laszlo
Quoted 47 lines
hw/nvram/fw_cfg.c +2
-14+static uint8_t fw_cfg_read (FWCfgState *s) {int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL );@@ -291,19 +320,6 @@ static uint8_t fw_cfg_read(FWCfgState *s)return ret; } -static uint64_t fw_cfg_data_mem_read (void *opaque,hwaddr addr, -unsigned size) -{ -FWCfgState *s = opaque; -uint64_t value =0 ; -unsigned i; - -for (i =0 ; i < size; ++i) { - value = (value <<8 ) |fw_cfg_read (s); - } -return value; -} -static void fw_cfg_data_mem_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {@@ -485,7 +501,7 @@ static const MemoryRegionOps fw_cfg_ctl_mem_ops = { };static const MemoryRegionOps fw_cfg_data_mem_ops = { - .read = fw_cfg_data_mem_read, + .read = fw_cfg_data_read, .write = fw_cfg_data_mem_write, .endianness =DEVICE_BIG_ENDIAN , .valid = {trace-events +1
-1@@ -196,7 +196,7 @@ ecc_diag_mem_readb(uint64_t addr, uint32_t ret) "Read diagnostic %"PRId64"= %02x # hw/nvram/fw_cfg.c fw_cfg_select(void *s, uint16_t key, int ret) "%p key %d = %d" -fw_cfg_read(void *s, uint8_t ret) "%p = %d" +fw_cfg_read(void *s, uint64_t ret) "%p = %"PRIx64 fw_cfg_add_file(void *s, int index, char *name, size_t len) "%p #%d: %s (%zd bytes)" # hw/block/hd-geometry.c
Re: [Qemu-devel] [PATCH v4 5/6] fw_cfg: add generic non-DMA read method
Quoted 77 lines · Laszlo Ersek
Quoted 50 lines · Gabriel L. Somlo
Introduce
fw_cfg_data_read(), a generic read method which works on all access widths (1 through 8 bytes, inclusive), and can be used during both IOPort and MMIO read accesses.To maintain legibility, only
fw_cfg_data_mem_read()(the MMIO data read method) is replaced by this patch. The new method essentially unwinds thefw_cfg_data_mem_read()+fw_cfg_read()combo, but without unnecessarily repeating all the validity checks performed by the latter on each byte being read.This patch also modifies the
trace_fw_cfg_readprototype to accept a 64-bit value argument, allowing it to work properly with the new read method, but also remain backward compatible with existing call sites.Cc Laszlo Ersek <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Marc Marí <[email protected]>Signed-off-by Gabriel Somlo <[email protected]>--- hw/nvram/fw_cfg.c | 44 ++++++++++++++++++++++++++++++-------------- trace-events | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-)hw/nvram/fw_cfg.c +18
-0@@ -274,6 +274,35 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key)return ret; } +static uint64_t fw_cfg_data_read (void *opaque,hwaddr addr,unsigned size) +{ +FWCfgState *s = opaque; +int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +uint64_t value =0 ; + +assert (size <=sizeof (value)); +if (s->cur_entry !=FW_CFG_INVALID && e->data) { +/* The least significant 'size' bytes of the return value are +* expected to contain a string preserving portion of the item +* data, padded with zeros to the right in case we run out early. +*/ +while (size && s->cur_offset < e->len) { + value = (value <<8 ) | e->data[s->cur_offset++]; + size--; + }hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key) · line 304
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+/* If size is still not zero, we *did* run out early, so continue +* left-shifting, to add the appropriate number of padding zeros +* on the right. +*/ + value <<=8 * size; + } + +trace_fw_cfg_read (s, value); +return value; +}With the wording you proposed in
<http://thread.gmane.org/gmane.comp.emulators.qemu/373165/focus=373507>,
this looks okay.... Except my (2a) proposal wasn't entirely correct, and now you get to fix it up for v5. :( Apologies. (It is a different experience when you see the code in full.)
Namely, consider the case when this code is entered with:
(size == 8 && s->cur_offset == e->len)(Which is possible if the guest makes a qword read access just after reading the full blob.)
Quoted 8 lines
In this case, the loop won't be entered at all (which is okay), but then you'll have:
uint64_t<< 64which is undefined behavior. ("If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.")
Yeah, we're hitting all the corner cases of the C standard, aren't we :)
Quoted 27 lines
So please protect the final shift with "if (size < 8)".
*Alternatively*, you could restrict the *outer* condition, i.e.,
s->cur_entry != FW_CFG_INVALID && e->datawith (
s->cur_offset<e->len).... And then you can even replace the "while" with a "do" loop. (Because both (size > 0) and (
s->cur_offset<e->len) will be true if the loop is reached at all.)Just the code, without comments:
assert(size <= sizeof(value)); assert(size > 0); if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) { /* ... */ do { value = (value << 8) | e->data[s->cur_offset++]; size--; } while (size && s->cur_offset < e->len); /* ... */ value <<= 8 * size; }
Quoted 4 lines
This makes it clear that "size" is strictly smaller than sizeof(value) when the shift is reached.
I'll let you choose between the two alternatives. :)
I like the do/while idea, so here's the new function:
+static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size) +{
+ FWCfgState *s = opaque; + int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); + FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
+ &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
+ uint64_t value = 0;
+
+ assert(size > 0 && size <= sizeof(value));
+ if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) {
+ /* The least significant 'size' bytes of the return value are
+ * expected to contain a string preserving portion of the item + * data, padded with zeros on the right in case we run out early. + * In technical terms, we're composing the host-endian representation + * of the big endian interpretation of the fw_cfg string. + */
+ do {
+ value = (value << 8) | e->data[s->cur_offset++];
+ } while (--size && s->cur_offset < e->len);
+ /* If size is still not zero, we *did* run out early, so continue
+ * left-shifting, to add the appropriate number of padding zeros + * on the right. + */
+ value <<= 8 * size;
+ }
+
+ trace_fw_cfg_read(s, value);
+ return value;
+}
Quoted 2 lines
Thanks, and I'm sorry.
Thank you, and no worries -- after all, what's *my* excuse for not catching it ? :)
Guess I'll put a low-pass filter on blasting out v5, given how this is a "target rich environment" for subtle C standard violations :)
Cheers,
--Gabriel
Quoted 49 lines
Quoted 48 lines
hw/nvram/fw_cfg.c +2
-14+static uint8_t fw_cfg_read (FWCfgState *s) {int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL );@@ -291,19 +320,6 @@ static uint8_t fw_cfg_read(FWCfgState *s)return ret; } -static uint64_t fw_cfg_data_mem_read (void *opaque,hwaddr addr, -unsigned size) -{ -FWCfgState *s = opaque; -uint64_t value =0 ; -unsigned i; - -for (i =0 ; i < size; ++i) { - value = (value <<8 ) |fw_cfg_read (s); - } -return value; -} -static void fw_cfg_data_mem_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {@@ -485,7 +501,7 @@ static const MemoryRegionOps fw_cfg_ctl_mem_ops = { };static const MemoryRegionOps fw_cfg_data_mem_ops = { - .read = fw_cfg_data_mem_read, + .read = fw_cfg_data_read, .write = fw_cfg_data_mem_write, .endianness =DEVICE_BIG_ENDIAN , .valid = {trace-events +1
-1@@ -196,7 +196,7 @@ ecc_diag_mem_readb(uint64_t addr, uint32_t ret) "Read diagnostic %"PRId64"= %02x # hw/nvram/fw_cfg.c fw_cfg_select(void *s, uint16_t key, int ret) "%p key %d = %d" -fw_cfg_read(void *s, uint8_t ret) "%p = %d" +fw_cfg_read(void *s, uint64_t ret) "%p = %"PRIx64 fw_cfg_add_file(void *s, int index, char *name, size_t len) "%p #%d: %s (%zd bytes)" # hw/block/hd-geometry.c
Re: [Qemu-devel] [PATCH v4 5/6] fw_cfg: add generic non-DMA read method
Quoted 123 lines · Gabriel L. Somlo
Quoted 77 lines · Laszlo Ersek
Quoted 50 lines · Gabriel L. Somlo
Introduce
fw_cfg_data_read(), a generic read method which works on all access widths (1 through 8 bytes, inclusive), and can be used during both IOPort and MMIO read accesses.To maintain legibility, only
fw_cfg_data_mem_read()(the MMIO data read method) is replaced by this patch. The new method essentially unwinds thefw_cfg_data_mem_read()+fw_cfg_read()combo, but without unnecessarily repeating all the validity checks performed by the latter on each byte being read.This patch also modifies the
trace_fw_cfg_readprototype to accept a 64-bit value argument, allowing it to work properly with the new read method, but also remain backward compatible with existing call sites.Cc Laszlo Ersek <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Marc Marí <[email protected]>Signed-off-by Gabriel Somlo <[email protected]>--- hw/nvram/fw_cfg.c | 44 ++++++++++++++++++++++++++++++-------------- trace-events | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-)hw/nvram/fw_cfg.c +18
-0@@ -274,6 +274,35 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key)return ret; } +static uint64_t fw_cfg_data_read (void *opaque,hwaddr addr,unsigned size) +{ +FWCfgState *s = opaque; +int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +uint64_t value =0 ; + +assert (size <=sizeof (value)); +if (s->cur_entry !=FW_CFG_INVALID && e->data) { +/* The least significant 'size' bytes of the return value are +* expected to contain a string preserving portion of the item +* data, padded with zeros to the right in case we run out early. +*/ +while (size && s->cur_offset < e->len) { + value = (value <<8 ) | e->data[s->cur_offset++]; + size--; + }hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key) · line 304
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+/* If size is still not zero, we *did* run out early, so continue +* left-shifting, to add the appropriate number of padding zeros +* on the right. +*/ + value <<=8 * size; + } + +trace_fw_cfg_read (s, value); +return value; +}With the wording you proposed in
<http://thread.gmane.org/gmane.comp.emulators.qemu/373165/focus=373507>,
this looks okay.... Except my (2a) proposal wasn't entirely correct, and now you get to fix it up for v5. :( Apologies. (It is a different experience when you see the code in full.)
Namely, consider the case when this code is entered with:
(size == 8 && s->cur_offset == e->len)(Which is possible if the guest makes a qword read access just after reading the full blob.)
Quoted 8 lines
In this case, the loop won't be entered at all (which is okay), but then you'll have:
uint64_t<< 64which is undefined behavior. ("If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.")
Yeah, we're hitting all the corner cases of the C standard, aren't we :)
Quoted 27 lines
So please protect the final shift with "if (size < 8)".
*Alternatively*, you could restrict the *outer* condition, i.e.,
s->cur_entry != FW_CFG_INVALID && e->datawith (
s->cur_offset<e->len).... And then you can even replace the "while" with a "do" loop. (Because both (size > 0) and (
s->cur_offset<e->len) will be true if the loop is reached at all.)Just the code, without comments:
assert(size <= sizeof(value)); assert(size > 0); if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) { /* ... */ do { value = (value << 8) | e->data[s->cur_offset++]; size--; } while (size && s->cur_offset < e->len); /* ... */ value <<= 8 * size; }Quoted 4 lines
This makes it clear that "size" is strictly smaller than sizeof(value) when the shift is reached.
I'll let you choose between the two alternatives. :)
I like the do/while idea, so here's the new function:
hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key)
Quoted 9 lines
hw/nvram/fw_cfg.c +9
-0+static uint64_t fw_cfg_data_read (void *opaque,hwaddr addr,unsigned size) +{ +FWCfgState *s = opaque; +int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +uint64_t value =0 ; + +assert (size >0 && size <=sizeof (value));
It's a matter of taste, and I won't insist at all, just mention that I didn't write those two assert()s as separate statements :)
Namely, with a conjunction (P1 && P2 && ... && Pn), you have the possibility to spell the assertion as:
assert(P1); assert(P2); ... assert(Pn);
And, if any one of those fails, you will know *which one*. Because the line number in the "assertion failed" message will tell you.
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+if (s->cur_entry !=FW_CFG_INVALID && e->data && s->cur_offset < e->len) { +/* The least significant 'size' bytes of the return value are +* expected to contain a string preserving portion of the item +* data, padded with zeros on the right in case we run out early. +* In technical terms, we're composing the host-endian representation +* of the big endian interpretation of the fw_cfg string. +*/ +do { + value = (value <<8 ) | e->data[s->cur_offset++]; + }while (--size && s->cur_offset < e->len);
hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key) · line 304
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+/* If size is still not zero, we *did* run out early, so continue +* left-shifting, to add the appropriate number of padding zeros +* on the right. +*/ + value <<=8 * size; + } + +trace_fw_cfg_read (s, value); +return value; +}
Looks good!
Quoted 6 lines
Quoted 3 lines
Thanks, and I'm sorry.
Thank you, and no worries -- after all, what's *my* excuse for not catching it ? :)
"No interest in language lawyering", perhaps? :)
Quoted 2 lines
Guess I'll put a low-pass filter on blasting out v5, given how this is a "target rich environment" for subtle C standard violations :)
I think I'm ready to give my R-b for the final missing piece. (Not sure if others would like to comment as well, on v4.) Your call :)
Cheers
Laszlo
Quoted 53 lines
Cheers,
--GabrielQuoted 49 lines
Quoted 48 lines
hw/nvram/fw_cfg.c +2
-14+static uint8_t fw_cfg_read (FWCfgState *s) {int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL );@@ -291,19 +320,6 @@ static uint8_t fw_cfg_read(FWCfgState *s)return ret; } -static uint64_t fw_cfg_data_mem_read (void *opaque,hwaddr addr, -unsigned size) -{ -FWCfgState *s = opaque; -uint64_t value =0 ; -unsigned i; - -for (i =0 ; i < size; ++i) { - value = (value <<8 ) |fw_cfg_read (s); - } -return value; -} -static void fw_cfg_data_mem_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {@@ -485,7 +501,7 @@ static const MemoryRegionOps fw_cfg_ctl_mem_ops = { };static const MemoryRegionOps fw_cfg_data_mem_ops = { - .read = fw_cfg_data_mem_read, + .read = fw_cfg_data_read, .write = fw_cfg_data_mem_write, .endianness =DEVICE_BIG_ENDIAN , .valid = {trace-events +1
-1@@ -196,7 +196,7 @@ ecc_diag_mem_readb(uint64_t addr, uint32_t ret) "Read diagnostic %"PRId64"= %02x # hw/nvram/fw_cfg.c fw_cfg_select(void *s, uint16_t key, int ret) "%p key %d = %d" -fw_cfg_read(void *s, uint8_t ret) "%p = %d" +fw_cfg_read(void *s, uint64_t ret) "%p = %"PRIx64 fw_cfg_add_file(void *s, int index, char *name, size_t len) "%p #%d: %s (%zd bytes)" # hw/block/hd-geometry.c
Re: [Qemu-devel] [PATCH v4 5/6] fw_cfg: add generic non-DMA read method
Quoted 1 line · Laszlo Ersek
On 11/04/15 17:35, Gabriel L. Somlo wrote:
[...]
Quoted 8 lines
Quoted 1 line
+
assert(size > 0 && size <= sizeof(value));It's a matter of taste, and I won't insist at all, just mention that I didn't write those two assert()s as separate statements :)
Namely, with a conjunction (P1 && P2 && ... && Pn), you have the possibility to spell the assertion as:
Quoted 7 lines
assert(P1); assert(P2); ... assert(Pn);And, if any one of those fails, you will know *which one*. Because the line number in the "assertion failed" message will tell you.
Yes, matter of taste, but that can't stop me having opinions on matters of taste ;)
You pay for the more detailed assertion failure reporting with extra source code clutter (as written, it's immediately obvious that it's a bounds check, less so if split), and extra object code when NDEBUG is off (which it should always be).
Personally, I'm content to fish details out of a core dump. YMMV.
[...]
Re: [Qemu-devel] [PATCH v4 5/6] fw_cfg: add generic non-DMA read method
Quoted 23 lines · Markus Armbruster
Quoted 1 line · Laszlo Ersek
On 11/04/15 17:35, Gabriel L. Somlo wrote:
[...]
Quoted 8 lines
Quoted 1 line
+
assert(size > 0 && size <= sizeof(value));It's a matter of taste, and I won't insist at all, just mention that I didn't write those two assert()s as separate statements :)
Namely, with a conjunction (P1 && P2 && ... && Pn), you have the possibility to spell the assertion as:
Quoted 7 lines
assert(P1); assert(P2); ... assert(Pn);And, if any one of those fails, you will know *which one*. Because the line number in the "assertion failed" message will tell you.
Yes, matter of taste, but that can't stop me having opinions on matters of taste ;)
Quoted 6 lines
You pay for the more detailed assertion failure reporting with extra source code clutter (as written, it's immediately obvious that it's a bounds check, less so if split), and extra object code when NDEBUG is off (which it should always be).
Personally, I'm content to fish details out of a core dump. YMMV.
The 12 GB core dump that the Launchpad user, reporting the bug from the other side of the Earth, failed to save? :)
But, I do concede your point. (Line numbers aren't a panacea either, in situations like the above.) Gabriel, please pick whichever format you like more.
Thanks
Laszlo
Re: [Qemu-devel] [PATCH v4 5/6] fw_cfg: add generic non-DMA read method
In reply to Laszlo ErsekQuoted 140 lines · Laszlo Ersek
Quoted 123 lines · Gabriel L. Somlo
Quoted 77 lines · Laszlo Ersek
Quoted 50 lines · Gabriel L. Somlo
Introduce
fw_cfg_data_read(), a generic read method which works on all access widths (1 through 8 bytes, inclusive), and can be used during both IOPort and MMIO read accesses.To maintain legibility, only
fw_cfg_data_mem_read()(the MMIO data read method) is replaced by this patch. The new method essentially unwinds thefw_cfg_data_mem_read()+fw_cfg_read()combo, but without unnecessarily repeating all the validity checks performed by the latter on each byte being read.This patch also modifies the
trace_fw_cfg_readprototype to accept a 64-bit value argument, allowing it to work properly with the new read method, but also remain backward compatible with existing call sites.Cc Laszlo Ersek <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Marc Marí <[email protected]>Signed-off-by Gabriel Somlo <[email protected]>--- hw/nvram/fw_cfg.c | 44 ++++++++++++++++++++++++++++++-------------- trace-events | 2 +- 2 files changed, 31 insertions(+), 15 deletions(-)hw/nvram/fw_cfg.c +18
-0@@ -274,6 +274,35 @@ static int fw_cfg_select(FWCfgState *s, uint16_t key)return ret; } +static uint64_t fw_cfg_data_read (void *opaque,hwaddr addr,unsigned size) +{ +FWCfgState *s = opaque; +int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +uint64_t value =0 ; + +assert (size <=sizeof (value)); +if (s->cur_entry !=FW_CFG_INVALID && e->data) { +/* The least significant 'size' bytes of the return value are +* expected to contain a string preserving portion of the item +* data, padded with zeros to the right in case we run out early. +*/ +while (size && s->cur_offset < e->len) { + value = (value <<8 ) | e->data[s->cur_offset++]; + size--; + }hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key) · line 304
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+/* If size is still not zero, we *did* run out early, so continue +* left-shifting, to add the appropriate number of padding zeros +* on the right. +*/ + value <<=8 * size; + } + +trace_fw_cfg_read (s, value); +return value; +}With the wording you proposed in
<http://thread.gmane.org/gmane.comp.emulators.qemu/373165/focus=373507>,
this looks okay.... Except my (2a) proposal wasn't entirely correct, and now you get to fix it up for v5. :( Apologies. (It is a different experience when you see the code in full.)
Namely, consider the case when this code is entered with:
(size == 8 && s->cur_offset == e->len)(Which is possible if the guest makes a qword read access just after reading the full blob.)
Quoted 8 lines
In this case, the loop won't be entered at all (which is okay), but then you'll have:
uint64_t<< 64which is undefined behavior. ("If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.")
Yeah, we're hitting all the corner cases of the C standard, aren't we :)
Quoted 27 lines
So please protect the final shift with "if (size < 8)".
*Alternatively*, you could restrict the *outer* condition, i.e.,
s->cur_entry != FW_CFG_INVALID && e->datawith (
s->cur_offset<e->len).... And then you can even replace the "while" with a "do" loop. (Because both (size > 0) and (
s->cur_offset<e->len) will be true if the loop is reached at all.)Just the code, without comments:
assert(size <= sizeof(value)); assert(size > 0); if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) { /* ... */ do { value = (value << 8) | e->data[s->cur_offset++]; size--; } while (size && s->cur_offset < e->len); /* ... */ value <<= 8 * size; }Quoted 4 lines
This makes it clear that "size" is strictly smaller than sizeof(value) when the shift is reached.
I'll let you choose between the two alternatives. :)
I like the do/while idea, so here's the new function:
hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key)
Quoted 9 lines
hw/nvram/fw_cfg.c +9
-0+static uint64_t fw_cfg_data_read (void *opaque,hwaddr addr,unsigned size) +{ +FWCfgState *s = opaque; +int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); +FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : + &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; +uint64_t value =0 ; + +assert (size >0 && size <=sizeof (value));It's a matter of taste, and I won't insist at all, just mention that I didn't write those two assert()s as separate statements :)
Namely, with a conjunction (P1 && P2 && ... && Pn), you have the possibility to spell the assertion as:
Quoted 7 lines
assert(P1); assert(P2); ... assert(Pn);And, if any one of those fails, you will know *which one*. Because the line number in the "assertion failed" message will tell you.
Ah, good point! My original instinct was to blend in with the existing style as much as possible (many other places where several &&s are packed into a single assert). But now that you spelled it out, it's a good case for NOT doing that, so I'll add this to my list of "substance over style" exceptions to watch out for :)
Quoted 25 lines
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+if (s->cur_entry !=FW_CFG_INVALID && e->data && s->cur_offset < e->len) { +/* The least significant 'size' bytes of the return value are +* expected to contain a string preserving portion of the item +* data, padded with zeros on the right in case we run out early. +* In technical terms, we're composing the host-endian representation +* of the big endian interpretation of the fw_cfg string. +*/ +do { + value = (value <<8 ) | e->data[s->cur_offset++]; + }while (--size && s->cur_offset < e->len);hw/nvram/fw_cfg.c static int fw_cfg_select(FWCfgState *s, uint16_t key) · line 304
Quoted 10 lines
hw/nvram/fw_cfg.c +10
-0+/* If size is still not zero, we *did* run out early, so continue +* left-shifting, to add the appropriate number of padding zeros +* on the right. +*/ + value <<=8 * size; + } + +trace_fw_cfg_read (s, value); +return value; +}Looks good!
Quoted 2 lines
Quoted 2 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Thanks, and I'm sorry.
Thank you, and no worries -- after all, what's *my* excuse for not catching it ? :)
"No interest in language lawyering", perhaps? :)
Oh, but it's the fine print where most interesting things always happen :)
Quoted 5 lines
Quoted 2 lines
Guess I'll put a low-pass filter on blasting out v5, given how this is a "target rich environment" for subtle C standard violations :)
I think I'm ready to give my R-b for the final missing piece. (Not sure if others would like to comment as well, on v4.) Your call :)
OK, coming right up!
Thanks again,
--Gabriel
Quoted 48 lines
Quoted 47 lines
Quoted 47 lines
Quoted 46 lines
hw/nvram/fw_cfg.c +1
-14static uint8_t fw_cfg_read (FWCfgState *s) {int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL );@@ -291,19 +320,6 @@ static uint8_t fw_cfg_read(FWCfgState *s)return ret; } -static uint64_t fw_cfg_data_mem_read (void *opaque,hwaddr addr, -unsigned size) -{ -FWCfgState *s = opaque; -uint64_t value =0 ; -unsigned i; - -for (i =0 ; i < size; ++i) { - value = (value <<8 ) |fw_cfg_read (s); - } -return value; -} -static void fw_cfg_data_mem_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {@@ -485,7 +501,7 @@ static const MemoryRegionOps fw_cfg_ctl_mem_ops = { };static const MemoryRegionOps fw_cfg_data_mem_ops = { - .read = fw_cfg_data_mem_read, + .read = fw_cfg_data_read, .write = fw_cfg_data_mem_write, .endianness =DEVICE_BIG_ENDIAN , .valid = {trace-events +1
-1@@ -196,7 +196,7 @@ ecc_diag_mem_readb(uint64_t addr, uint32_t ret) "Read diagnostic %"PRId64"= %02x # hw/nvram/fw_cfg.c fw_cfg_select(void *s, uint16_t key, int ret) "%p key %d = %d" -fw_cfg_read(void *s, uint8_t ret) "%p = %d" +fw_cfg_read(void *s, uint64_t ret) "%p = %"PRIx64 fw_cfg_add_file(void *s, int index, char *name, size_t len) "%p #%d: %s (%zd bytes)" # hw/block/hd-geometry.c
PATCH v4 6/6 fw_cfg: replace ioport data read with generic method
In reply to Gabriel L. SomloIOPort read access is limited to one byte at a time by fw_cfg_comb_valid(). As such, fw_cfg_comb_read() may safely ignore its size argument (which will always be 1), and simply call its fw_cfg_read() helper function once, returning 8 bits via the least significant byte of a 64-bit return value.
This patch replaces fw_cfg_comb_read() with the generic method fw_cfg_data_read(), and removes the unused fw_cfg_read() helper.
When called with size = 1, fw_cfg_data_read() acts exactly like fw_cfg_read(), performing the same set of sanity checks, and executing the while loop at most once (subject to the current read offset being within range).
--- hw/nvram/fw_cfg.c | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-)
hw/nvram/fw_cfg.c +1 -24
@@ -303,23 +303,6 @@ static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size)return value; } -static uint8_t fw_cfg_read (FWCfgState *s) -{ -int arch = !!(s->cur_entry &FW_CFG_ARCH_LOCAL ); -FWCfgEntry *e = (s->cur_entry ==FW_CFG_INVALID ) ?NULL : - &s->entries[arch][s->cur_entry &FW_CFG_ENTRY_MASK ]; -uint8_t ret; - -if (s->cur_entry ==FW_CFG_INVALID || !e->data || s->cur_offset >= e->len) - ret =0 ; -else { - ret = e->data[s->cur_offset++]; - } - -trace_fw_cfg_read (s, ret); -return ret; -} -static void fw_cfg_data_mem_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {
@@ -469,12 +452,6 @@ static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,return is_write && size ==2 ; } -static uint64_t fw_cfg_comb_read (void *opaque,hwaddr addr, -unsigned size) -{ -return fw_cfg_read (opaque); -} -static void fw_cfg_comb_write (void *opaque,hwaddr addr,uint64_t value,unsigned size) {
@@ -512,7 +489,7 @@ static const MemoryRegionOps fw_cfg_data_mem_ops = { };static const MemoryRegionOps fw_cfg_comb_mem_ops = { - .read = fw_cfg_comb_read, + .read = fw_cfg_data_read, .write = fw_cfg_comb_write, .endianness =DEVICE_LITTLE_ENDIAN , .valid.accepts = fw_cfg_comb_valid,
Signature · 1 line
2.4.3