Received: from eggs.gnu.org ([2001:4830:134:3::10]:44841) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zu11q-00073O-VB for qemu-devel@nongnu.org; Wed, 04 Nov 2015 11:35:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zu11n-0007J2-LY for qemu-devel@nongnu.org; Wed, 04 Nov 2015 11:35:18 -0500 Received: from smtp.andrew.cmu.edu ([128.2.157.38]:38918) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zu11n-0007In-HT for qemu-devel@nongnu.org; Wed, 04 Nov 2015 11:35:15 -0500 Date: Wed, 4 Nov 2015 11:35:05 -0500 From: "Gabriel L. Somlo" Message-ID: <20151104163505.GA4505@HEDWIG.INI.CMU.EDU> References: <1446586842-21793-1-git-send-email-somlo@cmu.edu> <1446586842-21793-6-git-send-email-somlo@cmu.edu> <563A1E69.40602@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <563A1E69.40602@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v4 5/6] fw_cfg: add generic non-DMA read method List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Laszlo Ersek Cc: peter.maydell@linaro.org, jordan.l.justen@intel.com, qemu-devel@nongnu.org, kraxel@redhat.com, pbonzini@redhat.com, markmb@redhat.com On Wed, Nov 04, 2015 at 04:04:09PM +0100, Laszlo Ersek wrote: > On 11/03/15 22:40, Gabriel L. Somlo wrote: > > 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. > >=20 > > 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. > >=20 > > 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. > >=20 > > Cc: Laszlo Ersek > > Cc: Gerd Hoffmann > > Cc: Marc Mar=ED > > Signed-off-by: Gabriel Somlo > > --- > > hw/nvram/fw_cfg.c | 44 ++++++++++++++++++++++++++++++-------------- > > trace-events | 2 +- > > 2 files changed, 31 insertions(+), 15 deletions(-) > >=20 > > diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c > > index 046fa74..9e01b46 100644 > > --- a/hw/nvram/fw_cfg.c > > +++ b/hw/nvram/fw_cfg.c > > @@ -274,6 +274,35 @@ static int fw_cfg_select(FWCfgState *s, uint16_t= key) > > return ret; > > } > > =20 > > +static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned= size) > > +{ > > + FWCfgState *s =3D opaque; > > + int arch =3D !!(s->cur_entry & FW_CFG_ARCH_LOCAL); > > + FWCfgEntry *e =3D (s->cur_entry =3D=3D FW_CFG_INVALID) ? NULL : > > + &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MA= SK]; > > + uint64_t value =3D 0; > > + > > + assert(size <=3D sizeof(value)); > > + if (s->cur_entry !=3D FW_CFG_INVALID && e->data) { > > + /* The least significant 'size' bytes of the return value ar= e > > + * expected to contain a string preserving portion of the it= em > > + * data, padded with zeros to the right in case we run out e= arly. > > + */ > > + while (size && s->cur_offset < e->len) { > > + value =3D (value << 8) | e->data[s->cur_offset++]; > > + size--; > > + } > > + /* If size is still not zero, we *did* run out early, so con= tinue > > + * left-shifting, to add the appropriate number of padding z= eros > > + * on the right. > > + */ > > + value <<=3D 8 * size; > > + } > > + > > + trace_fw_cfg_read(s, value); > > + return value; > > +} >=20 > With the wording you proposed in > , > this looks okay. >=20 > ... 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.) >=20 > Namely, consider the case when this code is entered with: >=20 > (size =3D=3D 8 && s->cur_offset =3D=3D e->len) >=20 > (Which is possible if the guest makes a qword read access just after > reading the full blob.) >=20 > In this case, the loop won't be entered at all (which is okay), but the= n > you'll have: >=20 > uint64_t << 64 >=20 > 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.") Yeah, we're hitting all the corner cases of the C standard, aren't we :) > So please protect the final shift with "if (size < 8)". >=20 > *Alternatively*, you could restrict the *outer* condition, i.e., >=20 > s->cur_entry !=3D FW_CFG_INVALID && e->data >=20 > with (s->cur_offset < e->len). >=20 > ... And then you can even replace the "while" with a "do" loop. (Becaus= e > both (size > 0) and (s->cur_offset < e->len) will be true if the loop i= s > reached at all.) >=20 > Just the code, without comments: >=20 > assert(size <=3D sizeof(value)); > assert(size > 0); > if (s->cur_entry !=3D FW_CFG_INVALID && e->data && > s->cur_offset < e->len) { > /* ... */ > do { > value =3D (value << 8) | e->data[s->cur_offset++]; > size--; > } while (size && s->cur_offset < e->len); > /* ... */ > value <<=3D 8 * size; > } >=20 > This makes it clear that "size" is strictly smaller than sizeof(value) > when the shift is reached. >=20 > 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 siz= e) +{ + FWCfgState *s =3D opaque; + int arch =3D !!(s->cur_entry & FW_CFG_ARCH_LOCAL); + FWCfgEntry *e =3D (s->cur_entry =3D=3D FW_CFG_INVALID) ? NULL : + &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; + uint64_t value =3D 0; + + assert(size > 0 && size <=3D sizeof(value)); + if (s->cur_entry !=3D 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 represent= ation + * of the big endian interpretation of the fw_cfg string. + */ + do { + value =3D (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 continu= e + * left-shifting, to add the appropriate number of padding zeros + * on the right. + */ + value <<=3D 8 * size; + } + + trace_fw_cfg_read(s, value); + return value; +} >=20 > Thanks, and I'm sorry. Thank you, and no worries -- after all, what's *my* excuse for not catching it ? :)=20 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 >=20 > > + > > static uint8_t fw_cfg_read(FWCfgState *s) > > { > > int arch =3D !!(s->cur_entry & FW_CFG_ARCH_LOCAL); > > @@ -291,19 +320,6 @@ static uint8_t fw_cfg_read(FWCfgState *s) > > return ret; > > } > > =20 > > -static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr, > > - unsigned size) > > -{ > > - FWCfgState *s =3D opaque; > > - uint64_t value =3D 0; > > - unsigned i; > > - > > - for (i =3D 0; i < size; ++i) { > > - value =3D (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 =3D= { > > }; > > =20 > > static const MemoryRegionOps fw_cfg_data_mem_ops =3D { > > - .read =3D fw_cfg_data_mem_read, > > + .read =3D fw_cfg_data_read, > > .write =3D fw_cfg_data_mem_write, > > .endianness =3D DEVICE_BIG_ENDIAN, > > .valid =3D { > > diff --git a/trace-events b/trace-events > > index 72136b9..5073040 100644 > > --- a/trace-events > > +++ b/trace-events > > @@ -196,7 +196,7 @@ ecc_diag_mem_readb(uint64_t addr, uint32_t ret) "= Read diagnostic %"PRId64"=3D %02x > > =20 > > # hw/nvram/fw_cfg.c > > fw_cfg_select(void *s, uint16_t key, int ret) "%p key %d =3D %d" > > -fw_cfg_read(void *s, uint8_t ret) "%p =3D %d" > > +fw_cfg_read(void *s, uint64_t ret) "%p =3D %"PRIx64 > > fw_cfg_add_file(void *s, int index, char *name, size_t len) "%p #%d:= %s (%zd bytes)" > > =20 > > # hw/block/hd-geometry.c > >=20 >=20