From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, mst@redhat.com
Subject: Re: [PATCH] pci: add romsize property
Date: Fri, 18 Dec 2020 18:54:57 +0000 [thread overview]
Message-ID: <20201218185457.GH2956@work-vm> (raw)
In-Reply-To: <20201218182736.1634344-1-pbonzini@redhat.com>
* Paolo Bonzini (pbonzini@redhat.com) wrote:
> This property can be useful for distros to set up known-good ROM sizes for
> migration purposes. The VM will fail to start if the ROM is too large,
> and migration compatibility will not be broken if the ROM is too small.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
I suspect something breaks horribly if you set this to the 4GB that the
UINT32 would allow.
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> ---
> hw/pci/pci.c | 19 +++++++++++++++++--
> hw/xen/xen_pt_load_rom.c | 14 ++++++++++++--
> include/hw/pci/pci.h | 1 +
> 3 files changed, 30 insertions(+), 4 deletions(-)
>
> diff --git a/hw/pci/pci.c b/hw/pci/pci.c
> index d4349ea577..fd25253c2a 100644
> --- a/hw/pci/pci.c
> +++ b/hw/pci/pci.c
> @@ -67,6 +67,7 @@ static void pcibus_reset(BusState *qbus);
> static Property pci_props[] = {
> DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
> DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
> + DEFINE_PROP_UINT32("romsize", PCIDevice, romsize, -1),
> DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1),
> DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
> QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
> @@ -2106,6 +2107,11 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
> bool is_default_rom;
> uint16_t class_id;
>
> + if (pci_dev->romsize != -1 && !is_power_of_2(pci_dev->romsize)) {
> + error_setg(errp, "ROM size %d is not a power of two", pci_dev->romsize);
> + return;
> + }
> +
> /* initialize cap_present for pci_is_express() and pci_config_size(),
> * Note that hybrid PCIs are not set automatically and need to manage
> * QEMU_PCI_CAP_EXPRESS manually */
> @@ -2366,7 +2372,16 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
> g_free(path);
> return;
> }
> - size = pow2ceil(size);
> + if (pdev->romsize != -1) {
> + if (size > pdev->romsize) {
> + error_setg(errp, "romfile \"%s\" (%d bytes) is too large for ROM size %d",
> + pdev->romfile, size, pdev->romsize);
> + g_free(path);
> + return;
> + }
> + } else {
> + pdev->romsize = pow2ceil(size);
> + }
>
> vmsd = qdev_get_vmsd(DEVICE(pdev));
>
> @@ -2376,7 +2391,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
> snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
> }
> pdev->has_rom = true;
> - memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, size, &error_fatal);
> + memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, pdev->romsize, &error_fatal);
> ptr = memory_region_get_ram_ptr(&pdev->rom);
> if (load_image_size(path, ptr, size) < 0) {
> error_setg(errp, "failed to load romfile \"%s\"", pdev->romfile);
> diff --git a/hw/xen/xen_pt_load_rom.c b/hw/xen/xen_pt_load_rom.c
> index a50a80837e..153812f8cd 100644
> --- a/hw/xen/xen_pt_load_rom.c
> +++ b/hw/xen/xen_pt_load_rom.c
> @@ -53,10 +53,20 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev,
> }
> fseek(fp, 0, SEEK_SET);
>
> + if (dev->romsize != -1) {
> + if (st.st_size > dev->romsize) {
> + error_report("ROM BAR \"%s\" (%ld bytes) is too large for ROM size %d",
> + rom_file, (long) st.st_size, dev->romsize);
> + goto close_rom;
> + }
> + } else {
> + dev->romsize = st.st_size;
> + }
> +
> snprintf(name, sizeof(name), "%s.rom", object_get_typename(owner));
> - memory_region_init_ram(&dev->rom, owner, name, st.st_size, &error_abort);
> + memory_region_init_ram(&dev->rom, owner, name, dev->romsize, &error_abort);
> ptr = memory_region_get_ram_ptr(&dev->rom);
> - memset(ptr, 0xff, st.st_size);
> + memset(ptr, 0xff, dev->romsize);
>
> if (!fread(ptr, 1, st.st_size, fp)) {
> error_report("pci-assign: Cannot read from host %s", rom_file);
> diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
> index 259f9c992d..b028245b62 100644
> --- a/include/hw/pci/pci.h
> +++ b/include/hw/pci/pci.h
> @@ -343,6 +343,7 @@ struct PCIDevice {
>
> /* Location of option rom */
> char *romfile;
> + uint32_t romsize;
> bool has_rom;
> MemoryRegion rom;
> uint32_t rom_bar;
> --
> 2.26.2
>
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
next prev parent reply other threads:[~2020-12-18 18:57 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-18 18:27 [PATCH] pci: add romsize property Paolo Bonzini
2020-12-18 18:54 ` Dr. David Alan Gilbert [this message]
2021-01-19 16:31 ` Peter Xu
2021-01-19 16:51 ` Philippe Mathieu-Daudé
2021-01-19 17:10 ` Paolo Bonzini
2021-01-19 17:19 ` Philippe Mathieu-Daudé
2021-01-19 17:20 ` Laszlo Ersek
2021-01-22 14:54 ` Paolo Bonzini
2021-01-20 10:14 ` David Edmondson
2021-01-22 14:30 ` Michael S. Tsirkin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201218185457.GH2956@work-vm \
--to=dgilbert@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).