[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] Re: [Qemu-devel] [PATCH 06/15] xen: Add the Xen platform pci device
On Thu, Aug 12, 2010 at 2:09 PM, <stefano.stabellini@xxxxxxxxxxxxx> wrote: > From: Anthony PERARD <anthony.perard@xxxxxxxxxx> > > Introduce a new emulated PCI device, specific to fully virtualized Xen > guests. ÂThe device is necessary for PV on HVM drivers to work. The code should be converted to qdev and VMState. > > Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx> > Signed-off-by: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx> > --- > ÂMakefile.target   |  Â1 + > Âhw/xen_machine_fv.c |  Â4 + > Âhw/xen_platform.c  | Â452 > +++++++++++++++++++++++++++++++++++++++++++++++++++ > Âhw/xen_platform.h  |  Â9 + > Â4 files changed, 466 insertions(+), 0 deletions(-) > Âcreate mode 100644 hw/xen_platform.c > Âcreate mode 100644 hw/xen_platform.h > > diff --git a/Makefile.target b/Makefile.target > index d1b63f2..1984cdd 100644 > --- a/Makefile.target > +++ b/Makefile.target > @@ -324,6 +324,7 @@ obj-xen-y += pc.o > Âobj-xen-y += piix_pci.o > Âobj-xen-y += mc146818rtc.o > Âobj-xen-y += xenstore.o > +obj-xen-y += xen_platform.o > > Âobj-xen-y += xen_mapcache.o > Âobj-xen-y += stub-functions.o > diff --git a/hw/xen_machine_fv.c b/hw/xen_machine_fv.c > index 114addf..ec826e7 100644 > --- a/hw/xen_machine_fv.c > +++ b/hw/xen_machine_fv.c > @@ -35,6 +35,7 @@ > Â#include "xen_common.h" > Â#include "xen_backend.h" > Â#include "xenstore.h" > +#include "xen_platform.h" > Â#include "xen/hvm/hvm_info_table.h" > > Â#define MAX_IDE_BUS 2 > @@ -93,6 +94,9 @@ static void xen_init_fv(ram_addr_t ram_size, > >   pc_vga_init(pci_bus); > > +  Âpci_xen_platform_init(pci_bus); > +  Âplatform_fixed_ioport_init(); > + >   /* init basic PC hardware */ >   pc_basic_device_init(isa_irq, &floppy_controller, &rtc_state); > > diff --git a/hw/xen_platform.c b/hw/xen_platform.c > new file mode 100644 > index 0000000..85d3f8b > --- /dev/null > +++ b/hw/xen_platform.c > @@ -0,0 +1,452 @@ > +/* > + * XEN platform pci device, formerly known as the event channel device > + * > + * Copyright (c) 2003-2004 Intel Corp. > + * Copyright (c) 2006 XenSource > + * > + * Permission is hereby granted, free of charge, to any person obtaining a > copy > + * of this software and associated documentation files (the "Software"), to > deal > + * in the Software without restriction, including without limitation the > rights > + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell > + * copies of the Software, and to permit persons to whom the Software is > + * furnished to do so, subject to the following conditions: > + * > + * The above copyright notice and this permission notice shall be included in > + * all copies or substantial portions of the Software. > + * > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL > + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER > + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING > FROM, > + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN > + * THE SOFTWARE. > + */ > + > +#include "hw.h" > +#include "pc.h" > +#include "pci.h" > +#include "irq.h" > +#include "xen_common.h" > +#include "net.h" > +#include "xen_platform.h" > +#include "xen_backend.h" > +#include "qemu-log.h" > + > +#include <assert.h> > +#include <xenguest.h> > + > +static int drivers_blacklisted; > +static uint16_t driver_product_version; > +static int throttling_disabled; > +static char log_buffer[4096]; > +static int log_buffer_off; > + > +static uint8_t platform_flags; A lot of static variables. Could you put these to PCIXenPlatformState? > + > +#define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */ > + > +typedef struct PCIXenPlatformState > +{ > +  ÂPCIDevice Âpci_dev; > +} PCIXenPlatformState; > + > + > +/* We throttle access to dom0 syslog, to avoid DOS attacks. ÂThis is > +  modelled as a token bucket, with one token for every byte of log. > +  The bucket size is 128KB (->1024 lines of 128 bytes each) and > +  refills at 256B/s. ÂIt starts full. ÂThe guest is blocked if no > +  tokens are available when it tries to generate a log message. */ > +#define BUCKET_MAX_SIZE (128*1024) > +#define BUCKET_FILL_RATE 256 > + > +static void throttle(unsigned count) > +{ > +  Âstatic unsigned available; > +  Âstatic struct timespec last_refil; > +  Âstatic int started; > +  Âstatic int warned; > + > +  Âstruct timespec waiting_for, now; > +  Âdouble delay; > +  Âstruct timespec ts; > + > +  Âif (throttling_disabled) > +    Âreturn; > + > +  Âif (!started) { > +    Âclock_gettime(CLOCK_MONOTONIC, &last_refil); > +    Âavailable = BUCKET_MAX_SIZE; > +    Âstarted = 1; > +  Â} > + > +  Âif (count > BUCKET_MAX_SIZE) { > +    Âfprintf(stderr, "tried to get %d tokens, but bucket size is %d\n", > +        ÂBUCKET_MAX_SIZE, count); > +    Âexit(1); > +  Â} > + > +  Âif (available < count) { > +    Â/* The bucket is empty. ÂRefil it */ > + > +    Â/* When will it be full enough to handle this request? */ > +    Âdelay = (double)(count - available) / BUCKET_FILL_RATE; > +    Âwaiting_for = last_refil; > +    Âwaiting_for.tv_sec += delay; > +    Âwaiting_for.tv_nsec += (delay - (int)delay) * 1e9; > +    Âif (waiting_for.tv_nsec >= 1000000000) { > +      Âwaiting_for.tv_nsec -= 1000000000; > +      Âwaiting_for.tv_sec++; > +    Â} > + > +    Â/* How long do we have to wait? (might be negative) */ > +    Âclock_gettime(CLOCK_MONOTONIC, &now); > +    Âts.tv_sec = waiting_for.tv_sec - now.tv_sec; > +    Âts.tv_nsec = waiting_for.tv_nsec - now.tv_nsec; > +    Âif (ts.tv_nsec < 0) { > +      Âts.tv_sec--; > +      Âts.tv_nsec += 1000000000; > +    Â} > + > +    Â/* Wait for it. */ > +    Âif (ts.tv_sec > 0 || > +      Â(ts.tv_sec == 0 && ts.tv_nsec > 0)) { > +      Âif (!warned) { > +        Âfprintf(stderr, "throttling guest access to syslog"); > +        Âwarned = 1; > +      Â} > +      Âwhile (nanosleep(&ts, &ts) < 0 && errno == EINTR) > +        Â; > +    Â} > + > +    Â/* Refil */ > +    Âclock_gettime(CLOCK_MONOTONIC, &now); > +    Âdelay = (now.tv_sec - last_refil.tv_sec) + > +      Â(now.tv_nsec - last_refil.tv_nsec) * 1.0e-9; > +    Âavailable += BUCKET_FILL_RATE * delay; > +    Âif (available > BUCKET_MAX_SIZE) > +      Âavailable = BUCKET_MAX_SIZE; > +    Âlast_refil = now; > +  Â} > + > +  Âassert(available >= count); > + > +  Âavailable -= count; > +} > + > +#define UNPLUG_ALL_IDE_DISKS 1 > +#define UNPLUG_ALL_NICS 2 > +#define UNPLUG_AUX_IDE_DISKS 4 These should go to the top of the file. Are they even used, the function below doesn't? > + > +static void platform_fixed_ioport_write2(void *opaque, uint32_t addr, > uint32_t val) > +{ > +  Âswitch (addr - 0x10) { 0x10 should be a #define, which should be used... > +  Âcase 0: > +    Â/* Unplug devices. ÂValue is a bitmask of which devices to > +      unplug, with bit 0 the IDE devices, bit 1 the network > +      devices, and bit 2 the non-primary-master IDE devices. */ > +    Âbreak; > +  Âcase 2: > +    Âswitch (val) { > +    Âcase 1: > +      Âfprintf(stderr, "Citrix Windows PV drivers loaded in guest\n"); > +      Âbreak; > +    Âcase 0: > +      Âfprintf(stderr, "Guest claimed to be running PV product 0?\n"); > +      Âbreak; > +    Âdefault: > +      Âfprintf(stderr, "Unknown PV product %d loaded in guest\n", val); > +      Âbreak; > +    Â} > +    Âdriver_product_version = val; > +    Âbreak; > +  Â} > +} > + > +static void platform_fixed_ioport_write4(void *opaque, uint32_t addr, > +                     uint32_t val) > +{ > +  Âswitch (addr - 0x10) { ... here ... > +  Âcase 0: > +    Â/* PV driver version */ > +    Âbreak; > +  Â} > +} > + > +static void platform_fixed_ioport_write1(void *opaque, uint32_t addr, > uint32_t val) > +{ > +  Âswitch (addr - 0x10) { ... here ... > +  Âcase 0: /* Platform flags */ { > +    Âhvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ? > +      ÂHVMMEM_ram_ro : HVMMEM_ram_rw; > +    Âif (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type, 0xc0, 0x40)) > +      Âfprintf(stderr,"platform_fixed_ioport: unable to change ro/rw " > +          Â"state of ROM memory area!\n"); Please introduce a macro (DPRINTF) and use that. > +    Âelse { > +      Âplatform_flags = val & PFFLAG_ROM_LOCK; > +      Âfprintf(stderr,"platform_fixed_ioport: changed ro/rw " > +          Â"state of ROM memory area. now is %s state.\n", > +          Â(mem_type == HVMMEM_ram_ro ? "ro":"rw")); > +    Â} > +    Âbreak; > +  Â} > +  Âcase 2: > +    Â/* Send bytes to syslog */ > +    Âif (val == '\n' || log_buffer_off == sizeof(log_buffer) - 1) { > +      Â/* Flush buffer */ > +      Âlog_buffer[log_buffer_off] = 0; > +      Âthrottle(log_buffer_off); > +      Âfprintf(stderr, "%s\n", log_buffer); > +      Âlog_buffer_off = 0; > +      Âbreak; > +    Â} > +    Âlog_buffer[log_buffer_off++] = val; > +    Âbreak; > +  Â} > +} > + > +static uint32_t platform_fixed_ioport_read2(void *opaque, uint32_t addr) > +{ > +  Âswitch (addr - 0x10) { ... here ... > +  Âcase 0: > +    Âif (drivers_blacklisted) { > +      Â/* The drivers will recognise this magic number and refuse > +       * to do anything. */ > +      Âreturn 0xd249; > +    Â} else { > +      Â/* Magic value so that you can identify the interface. */ > +      Âreturn 0x49d2; > +    Â} > +  Âdefault: > +    Âreturn 0xffff; > +  Â} > +} > + > +static uint32_t platform_fixed_ioport_read1(void *opaque, uint32_t addr) > +{ > +  Âswitch (addr - 0x10) { ... here ... > +  Âcase 0: > +    Â/* Platform flags */ > +    Âreturn platform_flags; > +  Âcase 2: > +    Â/* Version number */ > +    Âreturn 1; > +  Âdefault: > +    Âreturn 0xff; > +  Â} > +} > + > +static void platform_fixed_ioport_save(QEMUFile *f, void *opaque) > +{ > +  Âqemu_put_8s(f, &platform_flags); > +} > + > +static int platform_fixed_ioport_load(QEMUFile *f, void *opaque, int > version_id) > +{ > +  Âuint8_t flags; > + > +  Âif (version_id > 1) > +    Âreturn -EINVAL; > + > +  Âqemu_get_8s(f, &flags); > +  Âplatform_fixed_ioport_write1(NULL, 0x10, flags); > + > +  Âreturn 0; > +} > + > +void platform_fixed_ioport_init(void) > +{ > +  Âregister_savevm(NULL, "platform_fixed_ioport", 0, 1, > platform_fixed_ioport_save, > +          Âplatform_fixed_ioport_load, NULL); Please use VMState instead. > + > +  Âregister_ioport_write(0x10, 16, 4, platform_fixed_ioport_write4, NULL); and here and below. In fact, just s/0x10/XEN_PLATFORM_IOPORT/g. > +  Âregister_ioport_write(0x10, 16, 2, platform_fixed_ioport_write2, NULL); > +  Âregister_ioport_write(0x10, 16, 1, platform_fixed_ioport_write1, NULL); > +  Âregister_ioport_read(0x10, 16, 2, platform_fixed_ioport_read2, NULL); > +  Âregister_ioport_read(0x10, 16, 1, platform_fixed_ioport_read1, NULL); > + > +  Âplatform_fixed_ioport_write1(NULL, 0x10, 0); Introduce a reset function which performs something similar. > +} > + > +static uint32_t xen_platform_ioport_readb(void *opaque, uint32_t addr) > +{ > +  Âaddr &= 0xff; > + > +  Âreturn (addr == 0) ? platform_fixed_ioport_read1(NULL, 0x10) : ~0u; Just use if. > +} > + > +static void xen_platform_ioport_writeb(void *opaque, uint32_t addr, uint32_t > val) > +{ > +  Âaddr &= 0xff; > +  Âval Â&= 0xff; > + > +  Âswitch (addr) { > +  Âcase 0: /* Platform flags */ > +    Âplatform_fixed_ioport_write1(NULL, 0x10, val); > +    Âbreak; > +  Âcase 8: > +    Â{ > +      Âif (val == '\n' || log_buffer_off == sizeof(log_buffer) - 1) { > +        Â/* Flush buffer */ > +        Âlog_buffer[log_buffer_off] = 0; > +        Âthrottle(log_buffer_off); > +        Âfprintf(stderr, "%s\n", log_buffer); > +        Âlog_buffer_off = 0; > +        Âbreak; > +      Â} > +      Âlog_buffer[log_buffer_off++] = val; > +    Â} > +    Âbreak; > +  Âdefault: > +    Âbreak; > +  Â} > +} > + > +static void platform_ioport_map(PCIDevice *pci_dev, int region_num, pcibus_t > addr, pcibus_t size, int type) > +{ > +  ÂPCIXenPlatformState *d = (PCIXenPlatformState *)pci_dev; Useless cast in C. Moreover, you should use DO_UPCAST or container_of. > +  Âregister_ioport_write(addr, size, 1, xen_platform_ioport_writeb, d); > +  Âregister_ioport_read(addr, size, 1, xen_platform_ioport_readb, d); > +} > + > +static uint32_t platform_mmio_read(void *opaque, target_phys_addr_t addr) > +{ > +  Âstatic int warnings = 0; > +  Âif (warnings < 5) { > +    Âfprintf(stderr, "Warning: attempted read from physical address " > +        Â"0x%"PRIx64" in xen platform mmio space\n", (uint64_t)addr); Instead of the cast, you should use TARGET_FMT_plx. > +    Âwarnings++; > +  Â} > +  Âreturn 0; > +} > + > +static void platform_mmio_write(void *opaque, target_phys_addr_t addr, > +                Âuint32_t val) > +{ > +  Âstatic int warnings = 0; > +  Âif (warnings < 5) { > +    Âfprintf(stderr, "Warning: attempted write of 0x%x to physical " > +        Â"address 0x%"PRIx64" in xen platform mmio space\n", > +        Âval, (uint64_t)addr); > +    Âwarnings++; > +  Â} > +  Âreturn; > +} > + > +static CPUReadMemoryFunc *platform_mmio_read_funcs[3] = { These should be 'const'. > +  Âplatform_mmio_read, > +  Âplatform_mmio_read, > +  Âplatform_mmio_read, > +}; > + > +static CPUWriteMemoryFunc *platform_mmio_write_funcs[3] = { > +  Âplatform_mmio_write, > +  Âplatform_mmio_write, > +  Âplatform_mmio_write, > +}; > + > +static void platform_mmio_map(PCIDevice *d, int region_num, > +               Âpcibus_t addr, pcibus_t size, int type) > +{ > +  Âint mmio_io_addr; > + > +  Âmmio_io_addr = cpu_register_io_memory(platform_mmio_read_funcs, > +                     Âplatform_mmio_write_funcs, NULL); > + > +  Âcpu_register_physical_memory(addr, 0x1000000, mmio_io_addr); > +} > + > +struct pci_config_header { > +  Âuint16_t vendor_id; > +  Âuint16_t device_id; > +  Âuint16_t command; > +  Âuint16_t status; > +  Âuint8_t Ârevision; > +  Âuint8_t Âapi; > +  Âuint8_t Âsubclass; > +  Âuint8_t Âclass; > +  Âuint8_t Âcache_line_size; /* Units of 32 bit words */ > +  Âuint8_t Âlatency_timer; /* In units of bus cycles */ > +  Âuint8_t Âheader_type; /* Should be 0 */ > +  Âuint8_t Âbist; /* Built in self test */ > +  Âuint32_t base_address_regs[6]; > +  Âuint32_t reserved1; > +  Âuint16_t subsystem_vendor_id; > +  Âuint16_t subsystem_id; > +  Âuint32_t rom_addr; > +  Âuint32_t reserved3; > +  Âuint32_t reserved4; > +  Âuint8_t Âinterrupt_line; > +  Âuint8_t Âinterrupt_pin; > +  Âuint8_t Âmin_gnt; > +  Âuint8_t Âmax_lat; > +}; Why can't you use the facilities from pci.h? > + > +static void xen_pci_save(QEMUFile *f, void *opaque) > +{ > +  ÂPCIXenPlatformState *d = opaque; > +  Âuint64_t t = 0; > + > +  Âpci_device_save(&d->pci_dev, f); > +  Âqemu_put_be64s(f, &t); > +} > + > +static int xen_pci_load(QEMUFile *f, void *opaque, int version_id) > +{ > +  ÂPCIXenPlatformState *d = opaque; > +  Âint ret; > + > +  Âif (version_id > 3) > +    Âreturn -EINVAL; > + > +  Âret = pci_device_load(&d->pci_dev, f); > +  Âif (ret < 0) > +    Âreturn ret; > + > +  Âif (version_id >= 2) { > +    Âif (version_id == 2) { > +      Âuint8_t flags; > +      Âqemu_get_8s(f, &flags); > +      Âxen_platform_ioport_writeb(d, 0, flags); > +    Â} > +    Âqemu_get_be64(f); > +  Â} > + > +  Âreturn 0; > +} > + > +void pci_xen_platform_init(PCIBus *bus) > +{ > +  ÂPCIXenPlatformState *d; > +  Âstruct pci_config_header *pch; > + > +  Âprintf("Register xen platform.\n"); > +  Âd = (PCIXenPlatformState *)pci_register_device( > +    Âbus, "xen-platform", sizeof(PCIXenPlatformState), -1, NULL, NULL); > +  Âpch = (struct pci_config_header *)d->pci_dev.config; > +  Âpch->vendor_id = 0x5853; You should use pci_set_word etc. Please add 0x5853 to pci_ids.h. > +  Âpch->device_id = 0x0001; > +  Âpch->command = 3; /* IO and memory access */ > +  Âpch->revision = 1; > +  Âpch->api = 0; > +  Âpch->subclass = 0x80; /* Other */ > +  Âpch->class = 0xff; /* Unclassified device class */ > +  Âpch->header_type = 0; > +  Âpch->interrupt_pin = 1; > + > +  Â/* Microsoft WHQL requires non-zero subsystem IDs. */ > +  Â/* http://www.pcisig.com/reflector/msg02205.html. Â*/ > +  Âpch->subsystem_vendor_id = pch->vendor_id; /* Duplicate vendor id. Â*/ > +  Âpch->subsystem_id    Â= 0x0001;     /* Hardcode sub-id as 1. */ > + > +  Âpci_register_bar(&d->pci_dev, 0, 0x100, > +              PCI_BASE_ADDRESS_SPACE_IO, platform_ioport_map); > + > +  Â/* reserve 16MB mmio address for share memory*/ > +  Âpci_register_bar(&d->pci_dev, 1, 0x1000000, > +              PCI_BASE_ADDRESS_MEM_PREFETCH, platform_mmio_map); > + > +  Âregister_savevm(NULL, "platform", 0, 3, xen_pci_save, xen_pci_load, d); > +  Âprintf("Done register platform.\n"); > +} > + > diff --git a/hw/xen_platform.h b/hw/xen_platform.h > new file mode 100644 > index 0000000..6eeff22 > --- /dev/null > +++ b/hw/xen_platform.h > @@ -0,0 +1,9 @@ > +#ifndef XEN_PLATFORM_H > +#define XEN_PLATFORM_H > + > +#include "hw/pci.h" > + > +void pci_xen_platform_init(PCIBus *bus); > +void platform_fixed_ioport_init(void); > + > +#endif > -- > 1.7.0.4 > > > _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |