qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

util.c (2623B)


      1 /*
      2  * Copyright (C) 2021 Red Hat, Inc.
      3  *
      4  * This program is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU General Public License as
      6  * published by the Free Software Foundation; either version 2 or
      7  * (at your option) version 3 of the License.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #include "qemu/osdep.h"
     19 
     20 #include "hw/pci/pci.h"
     21 #include "hw/pci/pci_bus.h"
     22 #include "qapi/error.h"
     23 #include "ui/console.h"
     24 
     25 /*
     26  * Recursively (in reverse order) appends addresses of PCI devices as it moves
     27  * up in the PCI hierarchy.
     28  *
     29  * @returns true on success, false when the buffer wasn't large enough
     30  */
     31 static bool append_pci_address(char *buf, size_t buf_size, const PCIDevice *pci)
     32 {
     33     PCIBus *bus = pci_get_bus(pci);
     34     /*
     35      * equivalent to if (!pci_bus_is_root(bus)), but the function is not built
     36      * with PCI_CONFIG=n, avoid using an #ifdef by checking directly
     37      */
     38     if (bus->parent_dev != NULL) {
     39         append_pci_address(buf, buf_size, bus->parent_dev);
     40     }
     41 
     42     size_t len = strlen(buf);
     43     ssize_t written = snprintf(buf + len, buf_size - len, "/%02x.%x",
     44         PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
     45 
     46     return written > 0 && written < buf_size - len;
     47 }
     48 
     49 bool qemu_console_fill_device_address(QemuConsole *con,
     50                                       char *device_address,
     51                                       size_t size,
     52                                       Error **errp)
     53 {
     54     ERRP_GUARD();
     55     DeviceState *dev = DEVICE(object_property_get_link(OBJECT(con),
     56                                                        "device",
     57                                                        &error_abort));
     58     PCIDevice *pci = (PCIDevice *) object_dynamic_cast(OBJECT(dev),
     59                                                        TYPE_PCI_DEVICE);
     60 
     61     if (pci == NULL) {
     62         error_setg(errp, "Setting device address of a display device: "
     63                    "Not a PCI device.");
     64         return false;
     65     }
     66 
     67     strncpy(device_address, "pci/0000", size);
     68     if (!append_pci_address(device_address, size, pci)) {
     69         error_setg(errp, "Setting device address of a display device: "
     70                    "Too many PCI devices in the chain.");
     71         return false;
     72     }
     73 
     74     return true;
     75 }