|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Citrix Systems, Inc. |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library 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 GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + */ |
| 18 | + |
| 19 | +#include <event.h> |
| 20 | +#include <inttypes.h> |
| 21 | +#include <libpciemu.h> |
| 22 | +#include <signal.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <stdio.h> |
| 25 | +#include <string.h> |
| 26 | + |
| 27 | +#define PCI_VENDOR_ID_XEN 0x5853 |
| 28 | +#define PCI_DEVICE_ID_FAKEPCI 0xc148 |
| 29 | + |
| 30 | +static void pci_bar_update (uint8_t region, uint64_t addr, void *priv) |
| 31 | +{ |
| 32 | + (void) priv; |
| 33 | + |
| 34 | + if (addr == PCI_BAR_UNMAPPED) |
| 35 | + printf ("Bar %u unmapped\n", region); |
| 36 | + else |
| 37 | + printf ("Bar %u mapped to 0x%"PRIx64"\n", region, addr); |
| 38 | +} |
| 39 | + |
| 40 | +static uint64_t pci_mmio_read (uint64_t addr, uint32_t size, void *priv) |
| 41 | +{ |
| 42 | + (void) priv; |
| 43 | + |
| 44 | + fprintf (stderr, "pci_mmio_read addr=0x%"PRIx64" size=%u\n", addr, size); |
| 45 | + |
| 46 | + return 0x42; |
| 47 | +} |
| 48 | + |
| 49 | +static void pci_mmio_write (uint64_t addr, uint64_t data, uint32_t size, |
| 50 | + void *priv) |
| 51 | +{ |
| 52 | + (void) priv; |
| 53 | + |
| 54 | + fprintf (stderr, "pci_mmio_write addr=0x%"PRIx64" data=0x%"PRIx64" size=%u\n", |
| 55 | + addr, data, size); |
| 56 | +} |
| 57 | + |
| 58 | +static libpciemu_io_ops_t pci_mmio_ops = { |
| 59 | + .read = pci_mmio_read, |
| 60 | + .write = pci_mmio_write, |
| 61 | +}; |
| 62 | + |
| 63 | +static libpciemu_pci_t pci_init (int domid, libpciemu_handle_t iohandle) |
| 64 | +{ |
| 65 | + libpciemu_pci_info_t info; |
| 66 | + libpciemu_pci_t pci; |
| 67 | + |
| 68 | + memset (&info, 0, sizeof (info)); |
| 69 | + |
| 70 | + info.vendor_id = PCI_VENDOR_ID_XEN; |
| 71 | + info.device_id = PCI_DEVICE_ID_FAKEPCI; |
| 72 | + info.subvendor_id = PCI_VENDOR_ID_XEN; |
| 73 | + info.subdevice_id = PCI_DEVICE_ID_FAKEPCI; |
| 74 | + /* Use 0:08.0 */ |
| 75 | + info.bus = 0; |
| 76 | + info.device = 8; |
| 77 | + info.function = 0; |
| 78 | + info.bar_update = pci_bar_update; |
| 79 | + |
| 80 | + pci = libpciemu_pci_device_init (&info, NULL); |
| 81 | + |
| 82 | + if (!pci) |
| 83 | + return NULL; |
| 84 | + |
| 85 | + libpciemu_pci_register_bar (pci, 0, 1, PCI_BAR_TYPE_PREFETCH, 1024, |
| 86 | + &pci_mmio_ops); |
| 87 | + |
| 88 | + if (libpciemu_pci_device_register (iohandle, pci)) |
| 89 | + { |
| 90 | + fprintf (stderr, "Unable to register pci device\n"); |
| 91 | + libpciemu_pci_device_release (pci); |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + |
| 95 | + return pci; |
| 96 | +} |
| 97 | + |
| 98 | +static void shutdown (int signal) |
| 99 | +{ |
| 100 | + printf("Kill handlepci\n"); |
| 101 | + /* FIXME: Need some cleanup here */ |
| 102 | + exit(0); |
| 103 | +} |
| 104 | + |
| 105 | +static void usage (const char *prog) |
| 106 | +{ |
| 107 | + fprintf (stderr, "Usage: %s domid\n", prog); |
| 108 | +} |
| 109 | + |
| 110 | +static void pci_io (int fd, short event, void *opaque) |
| 111 | +{ |
| 112 | + libpciemu_handle_t iohandle = opaque; |
| 113 | + |
| 114 | + (void) fd; |
| 115 | + (void) event; |
| 116 | + |
| 117 | + libpciemu_handle (iohandle); |
| 118 | +} |
| 119 | + |
| 120 | +static void fake_pci_logging (libpciemu_loglvl lvl, const char *fmt, ...) |
| 121 | +{ |
| 122 | + va_list ap; |
| 123 | + |
| 124 | + (void) lvl; |
| 125 | + |
| 126 | + va_start(ap, fmt); |
| 127 | + vfprintf(stderr, fmt, ap); |
| 128 | + va_end(ap); |
| 129 | +} |
| 130 | + |
| 131 | +int main (int argc, char **argv) |
| 132 | +{ |
| 133 | + int domid; |
| 134 | + libpciemu_handle_t iohandle; |
| 135 | + int ret = 1; |
| 136 | + libpciemu_pci_t pci; |
| 137 | + struct event ioevent; |
| 138 | + |
| 139 | + if (argc != 2) |
| 140 | + { |
| 141 | + usage (argv[0]); |
| 142 | + return 1; |
| 143 | + } |
| 144 | + |
| 145 | + domid = atoi (argv[1]); |
| 146 | + if (domid <= 0) |
| 147 | + return 0; |
| 148 | + |
| 149 | + event_init (); |
| 150 | + signal (SIGKILL, shutdown); |
| 151 | + signal (SIGINT, shutdown); |
| 152 | + |
| 153 | + libpciemu_init (fake_pci_logging); |
| 154 | + |
| 155 | + if (!(iohandle = libpciemu_handle_create (domid))) |
| 156 | + { |
| 157 | + fprintf (stderr, "Unable to create iohandle\n"); |
| 158 | + goto pcicleanup; |
| 159 | + } |
| 160 | + |
| 161 | + event_set (&ioevent, libpciemu_handle_get_fd (iohandle), |
| 162 | + EV_READ | EV_PERSIST, pci_io, iohandle); |
| 163 | + event_add (&ioevent, NULL); |
| 164 | + |
| 165 | + if (!(pci = pci_init (domid, iohandle))) |
| 166 | + { |
| 167 | + fprintf (stderr, "Unable to create pci\n"); |
| 168 | + goto iohandlecleanup; |
| 169 | + } |
| 170 | + |
| 171 | + event_dispatch (); |
| 172 | + |
| 173 | +iohandlecleanup: |
| 174 | + libpciemu_handle_destroy (iohandle); |
| 175 | +pcicleanup: |
| 176 | + libpciemu_cleanup (); |
| 177 | + return ret; |
| 178 | +} |
0 commit comments