Patchwork [RFC,V3,04/12] xen: Add the Xen platform pci device

login
register
mail settings
Submitter Anthony PERARD
Date Sept. 17, 2010, 11:14 a.m.
Message ID <1284722107-28550-5-git-send-email-anthony.perard@citrix.com>
Download mbox | patch
Permalink /patch/65063/
State New
Headers show

Comments

Anthony PERARD - Sept. 17, 2010, 11:14 a.m.
From: Anthony PERARD <anthony.perard@citrix.com>

Introduce a new emulated PCI device, specific to fully virtualized Xen
guests.  The device is necessary for PV on HVM drivers to work.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
 Makefile.target     |    1 +
 hw/hw.h             |    3 +
 hw/pci_ids.h        |    2 +
 hw/xen_machine_fv.c |    3 +
 hw/xen_platform.c   |  455 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/xen_platform.h   |    8 +
 6 files changed, 472 insertions(+), 0 deletions(-)
 create mode 100644 hw/xen_platform.c
 create mode 100644 hw/xen_platform.h
Blue Swirl - Sept. 17, 2010, 6:06 p.m.
On Fri, Sep 17, 2010 at 11:14 AM,  <anthony.perard@citrix.com> wrote:
> From: Anthony PERARD <anthony.perard@citrix.com>
>
> Introduce a new emulated PCI device, specific to fully virtualized Xen
> guests.  The device is necessary for PV on HVM drivers to work.
>
> Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
> Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
> ---
>  Makefile.target     |    1 +
>  hw/hw.h             |    3 +
>  hw/pci_ids.h        |    2 +
>  hw/xen_machine_fv.c |    3 +
>  hw/xen_platform.c   |  455 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  hw/xen_platform.h   |    8 +
>  6 files changed, 472 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 1984f58..6b390e6 100644
> --- a/Makefile.target
> +++ b/Makefile.target
> @@ -188,6 +188,7 @@ obj-$(CONFIG_NO_XEN) += xen-stub.o
>
>  # xen full virtualized machine
>  obj-$(CONFIG_XEN) += xen_machine_fv.o
> +obj-$(CONFIG_XEN) += xen_platform.o
>
>  # USB layer
>  obj-$(CONFIG_USB_OHCI) += usb-ohci.o
> diff --git a/hw/hw.h b/hw/hw.h
> index 4405092..67f3369 100644
> --- a/hw/hw.h
> +++ b/hw/hw.h
> @@ -653,6 +653,9 @@ extern const VMStateDescription vmstate_i2c_slave;
>  #define VMSTATE_INT32_LE(_f, _s)                                   \
>     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
>
> +#define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
> +    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
> +
>  #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
>     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
>
> diff --git a/hw/pci_ids.h b/hw/pci_ids.h
> index 39e9f1d..1f2e0dd 100644
> --- a/hw/pci_ids.h
> +++ b/hw/pci_ids.h
> @@ -105,3 +105,5 @@
>  #define PCI_DEVICE_ID_INTEL_82371AB      0x7111
>  #define PCI_DEVICE_ID_INTEL_82371AB_2    0x7112
>  #define PCI_DEVICE_ID_INTEL_82371AB_3    0x7113
> +
> +#define PCI_VENDOR_ID_XENSOURCE          0x5853
> diff --git a/hw/xen_machine_fv.c b/hw/xen_machine_fv.c
> index 03683c7..65fd44a 100644
> --- a/hw/xen_machine_fv.c
> +++ b/hw/xen_machine_fv.c
> @@ -34,6 +34,7 @@
>  #include "blockdev.h"
>
>  #include "xen/hvm/hvm_info_table.h"
> +#include "xen_platform.h"
>
>  #define MAX_IDE_BUS 2
>
> @@ -87,6 +88,8 @@ static void xen_init_fv(ram_addr_t ram_size,
>
>     pc_vga_init(pci_bus);
>
> +    pci_xen_platform_init(pci_bus);
> +
>     /* 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..15b490a
> --- /dev/null
> +++ b/hw/xen_platform.c
> @@ -0,0 +1,455 @@
> +/*
> + * 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>
> +
> +//#define PLATFORM_DEBUG
> +
> +#ifdef PLATFORM_DEBUG
> +#define DPRINTF(fmt, ...) do { \
> +    fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
> +} while (0)
> +#else
> +#define DPRINTF(fmt, ...) do { } while (0)
> +#endif
> +
> +#define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
> +
> +typedef struct PCIXenPlatformState {
> +    PCIDevice  pci_dev;
> +    uint8_t flags; /* used only for version_id == 2 */
> +    int drivers_blacklisted;
> +    uint16_t driver_product_version;
> +
> +    /* Log from guest drivers */
> +    int throttling_disabled;
> +    char log_buffer[4096];
> +    int log_buffer_off;
> +} PCIXenPlatformState;
> +
> +#define XEN_PLATFORM_IOPORT 0x10
> +
> +/* 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(PCIXenPlatformState *s, unsigned count)
> +{
> +    static unsigned available;
> +    static struct timespec last_refil;

last_refill

> +    static int started;
> +    static int warned;
> +
> +    struct timespec waiting_for, now;
> +    double delay;
> +    struct timespec ts;
> +
> +    if (s->throttling_disabled)
> +        return;

Braces should be added here and other places.

> +
> +    if (!started) {
> +        clock_gettime(CLOCK_MONOTONIC, &last_refil);
> +        available = BUCKET_MAX_SIZE;
> +        started = 1;
> +    }
> +
> +    if (count > BUCKET_MAX_SIZE) {
> +        DPRINTF("tried to get %d tokens, but bucket size is %d\n",

count is unsigned, so %u.

> +                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) {
> +                DPRINTF("throttling guest access to syslog");
> +                warned = 1;
> +            }
> +            while (nanosleep(&ts, &ts) < 0 && errno == EINTR)
> +                ;

braces

> +        }
> +
> +        /* Refil */

Refill

> +        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;

We have muldiv64(), perhaps it could be used here?

> +        if (available > BUCKET_MAX_SIZE)
> +            available = BUCKET_MAX_SIZE;
> +        last_refil = now;
> +    }
> +
> +    assert(available >= count);

Is it possible to trigger this from the guest?

> +
> +    available -= count;
> +}
> +
> +/* Xen Platform, Fixed IOPort */
> +
> +static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
> +{
> +    PCIXenPlatformState *s = opaque;
> +
> +    switch (addr - XEN_PLATFORM_IOPORT) {
> +    case 0:
> +        /* TODO: */
> +        /* 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:
> +            DPRINTF("Citrix Windows PV drivers loaded in guest\n");
> +            break;
> +        case 0:
> +            DPRINTF("Guest claimed to be running PV product 0?\n");
> +            break;
> +        default:
> +            DPRINTF("Unknown PV product %d loaded in guest\n", val);
> +            break;
> +        }
> +        s->driver_product_version = val;
> +        break;
> +    }
> +}
> +
> +static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
> +                                         uint32_t val)
> +{
> +    switch (addr - XEN_PLATFORM_IOPORT) {
> +    case 0:
> +        /* PV driver version */
> +        break;
> +    }
> +}
> +
> +static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
> +{
> +    PCIXenPlatformState *s = opaque;
> +
> +    switch (addr - XEN_PLATFORM_IOPORT) {
> +    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))
> +            DPRINTF("unable to change ro/rw state of ROM memory area!\n");

braces

> +        else {
> +            s->flags = val & PFFLAG_ROM_LOCK;
> +            DPRINTF("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' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
> +            /* Flush buffer */
> +            s->log_buffer[s->log_buffer_off] = 0;
> +            throttle(s, s->log_buffer_off);
> +            DPRINTF("%s\n", s->log_buffer);
> +            s->log_buffer_off = 0;
> +            break;
> +        }
> +        s->log_buffer[s->log_buffer_off++] = val;
> +        break;
> +    }
> +}
> +
> +static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
> +{
> +    PCIXenPlatformState *s = opaque;
> +
> +    switch (addr - XEN_PLATFORM_IOPORT) {
> +    case 0:
> +        if (s->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_readb(void *opaque, uint32_t addr)
> +{
> +    PCIXenPlatformState *s = opaque;
> +
> +    switch (addr - XEN_PLATFORM_IOPORT) {
> +    case 0:
> +        /* Platform flags */
> +        return s->flags;
> +    case 2:
> +        /* Version number */
> +        return 1;
> +    default:
> +        return 0xff;
> +    }
> +}
> +
> +static void platform_fixed_ioport_reset(void *opaque)
> +{
> +    PCIXenPlatformState *s = opaque;
> +
> +    platform_fixed_ioport_writeb(s, XEN_PLATFORM_IOPORT, 0);
> +}
> +
> +static void platform_fixed_ioport_init(PCIXenPlatformState* s)
> +{
> +    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 4, platform_fixed_ioport_writel, s);
> +    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 2, platform_fixed_ioport_writew, s);
> +    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 1, platform_fixed_ioport_writeb, s);
> +    register_ioport_read(XEN_PLATFORM_IOPORT, 16, 2, platform_fixed_ioport_readw, s);
> +    register_ioport_read(XEN_PLATFORM_IOPORT, 16, 1, platform_fixed_ioport_readb, s);
> +}
> +
> +/* Xen Platform PCI Device */
> +
> +static uint32_t xen_platform_ioport_readb(void *opaque, uint32_t addr)
> +{
> +    addr &= 0xff;
> +
> +    if (addr == 0)
> +        return platform_fixed_ioport_readb(opaque, XEN_PLATFORM_IOPORT);

braces

> +    else
> +        return ~0u;
> +}
> +
> +static void xen_platform_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
> +{
> +    PCIXenPlatformState *s = opaque;
> +
> +    addr &= 0xff;
> +    val  &= 0xff;
> +
> +    switch (addr) {
> +    case 0: /* Platform flags */
> +        platform_fixed_ioport_writeb(opaque, XEN_PLATFORM_IOPORT, val);
> +        break;
> +    case 8:
> +        {
> +            if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
> +                /* Flush buffer */
> +                s->log_buffer[s->log_buffer_off] = 0;
> +                throttle(s, s->log_buffer_off);
> +                DPRINTF("%s\n", s->log_buffer);
> +                s->log_buffer_off = 0;
> +                break;
> +            }
> +            s->log_buffer[s->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 = DO_UPCAST(PCIXenPlatformState, pci_dev, pci_dev);
> +
> +    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) {
> +        DPRINTF("Warning: attempted read from physical address "
> +                "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
> +        warnings++;

Since DPRINTF only works in a specially compiled version, I'd remove
these checks. There could also be additional debug flags besides
PLATFORM_DEBUG to enable these warnings if these are too noisy, like
DEBUG_MMIO. I'd rename PLATFORM_DEBUG to DEBUG_PLATFORM.
Isaku Yamahata - Sept. 24, 2010, 5:10 a.m.
On Fri, Sep 17, 2010 at 12:14:59PM +0100, anthony.perard@citrix.com wrote:
> +static int xen_platform_initfn(PCIDevice *dev)
> +{
> +    PCIXenPlatformState *d = DO_UPCAST(PCIXenPlatformState, pci_dev, dev);
> +    uint8_t *pci_conf;
> +
> +    pci_conf = d->pci_dev.config;
> +
> +    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_XENSOURCE);
> +    pci_config_set_device_id(pci_conf, 0x0001);
> +    pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
> +
> +    pci_config_set_revision(pci_conf, 1);
> +    pci_config_set_prog_interface(pci_conf, 0);
> +
> +    pci_config_set_class(pci_conf, PCI_CLASS_OTHERS << 8 | 0x80);
> +
> +    pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;

Eliminate this line. Don't overwrite multifunction bit.
Please refer to 
498238687fd3a2bf3efb32694732f88ceac72e99
6eab3de16d36c48a983366b09d0a0029a5260bc3



> +    pci_conf[PCI_INTERRUPT_PIN] = 1;
> +
> +    /* Microsoft WHQL requires non-zero subsystem IDs. */
> +    /* http://www.pcisig.com/reflector/msg02205.html.  */
> +    pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, pci_conf[PCI_VENDOR_ID]);
> +    pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0001);
> +
> +    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);
> +
> +    platform_fixed_ioport_init(d);
> +
> +    return 0;
> +}
Isaku Yamahata - Sept. 24, 2010, 5:52 a.m.
On Fri, Sep 17, 2010 at 12:14:59PM +0100, anthony.perard@citrix.com wrote:

> +static uint32_t platform_mmio_read(void *opaque, target_phys_addr_t addr)
> +{
> +    static int warnings = 0;
> +
> +    if (warnings < 5) {
> +        DPRINTF("Warning: attempted read from physical address "
> +                "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
> +        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) {
> +        DPRINTF("Warning: attempted write of 0x%x to physical "
> +                "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
> +                val, addr);
> +        warnings++;
> +    }
> +}
> +
> +static CPUReadMemoryFunc * const platform_mmio_read_funcs[3] = {
> +    platform_mmio_read,
> +    platform_mmio_read,
> +    platform_mmio_read,
> +};
> +
> +static CPUWriteMemoryFunc * const 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, size, mmio_io_addr);
> +}

Please use cpu_register_io_memory_simple().

Patch

diff --git a/Makefile.target b/Makefile.target
index 1984f58..6b390e6 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -188,6 +188,7 @@  obj-$(CONFIG_NO_XEN) += xen-stub.o
 
 # xen full virtualized machine
 obj-$(CONFIG_XEN) += xen_machine_fv.o
+obj-$(CONFIG_XEN) += xen_platform.o
 
 # USB layer
 obj-$(CONFIG_USB_OHCI) += usb-ohci.o
diff --git a/hw/hw.h b/hw/hw.h
index 4405092..67f3369 100644
--- a/hw/hw.h
+++ b/hw/hw.h
@@ -653,6 +653,9 @@  extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_INT32_LE(_f, _s)                                   \
     VMSTATE_SINGLE(_f, _s, 0, vmstate_info_int32_le, int32_t)
 
+#define VMSTATE_UINT8_TEST(_f, _s, _t)                               \
+    VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint8, uint8_t)
+
 #define VMSTATE_UINT16_TEST(_f, _s, _t)                               \
     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_info_uint16, uint16_t)
 
diff --git a/hw/pci_ids.h b/hw/pci_ids.h
index 39e9f1d..1f2e0dd 100644
--- a/hw/pci_ids.h
+++ b/hw/pci_ids.h
@@ -105,3 +105,5 @@ 
 #define PCI_DEVICE_ID_INTEL_82371AB      0x7111
 #define PCI_DEVICE_ID_INTEL_82371AB_2    0x7112
 #define PCI_DEVICE_ID_INTEL_82371AB_3    0x7113
+
+#define PCI_VENDOR_ID_XENSOURCE          0x5853
diff --git a/hw/xen_machine_fv.c b/hw/xen_machine_fv.c
index 03683c7..65fd44a 100644
--- a/hw/xen_machine_fv.c
+++ b/hw/xen_machine_fv.c
@@ -34,6 +34,7 @@ 
 #include "blockdev.h"
 
 #include "xen/hvm/hvm_info_table.h"
+#include "xen_platform.h"
 
 #define MAX_IDE_BUS 2
 
@@ -87,6 +88,8 @@  static void xen_init_fv(ram_addr_t ram_size,
 
     pc_vga_init(pci_bus);
 
+    pci_xen_platform_init(pci_bus);
+
     /* 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..15b490a
--- /dev/null
+++ b/hw/xen_platform.c
@@ -0,0 +1,455 @@ 
+/*
+ * 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>
+
+//#define PLATFORM_DEBUG
+
+#ifdef PLATFORM_DEBUG
+#define DPRINTF(fmt, ...) do { \
+    fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
+} while (0)
+#else
+#define DPRINTF(fmt, ...) do { } while (0)
+#endif
+
+#define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
+
+typedef struct PCIXenPlatformState {
+    PCIDevice  pci_dev;
+    uint8_t flags; /* used only for version_id == 2 */
+    int drivers_blacklisted;
+    uint16_t driver_product_version;
+
+    /* Log from guest drivers */
+    int throttling_disabled;
+    char log_buffer[4096];
+    int log_buffer_off;
+} PCIXenPlatformState;
+
+#define XEN_PLATFORM_IOPORT 0x10
+
+/* 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(PCIXenPlatformState *s, 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 (s->throttling_disabled)
+        return;
+
+    if (!started) {
+        clock_gettime(CLOCK_MONOTONIC, &last_refil);
+        available = BUCKET_MAX_SIZE;
+        started = 1;
+    }
+
+    if (count > BUCKET_MAX_SIZE) {
+        DPRINTF("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) {
+                DPRINTF("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;
+}
+
+/* Xen Platform, Fixed IOPort */
+
+static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
+{
+    PCIXenPlatformState *s = opaque;
+
+    switch (addr - XEN_PLATFORM_IOPORT) {
+    case 0:
+        /* TODO: */
+        /* 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:
+            DPRINTF("Citrix Windows PV drivers loaded in guest\n");
+            break;
+        case 0:
+            DPRINTF("Guest claimed to be running PV product 0?\n");
+            break;
+        default:
+            DPRINTF("Unknown PV product %d loaded in guest\n", val);
+            break;
+        }
+        s->driver_product_version = val;
+        break;
+    }
+}
+
+static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
+                                         uint32_t val)
+{
+    switch (addr - XEN_PLATFORM_IOPORT) {
+    case 0:
+        /* PV driver version */
+        break;
+    }
+}
+
+static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
+{
+    PCIXenPlatformState *s = opaque;
+
+    switch (addr - XEN_PLATFORM_IOPORT) {
+    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))
+            DPRINTF("unable to change ro/rw state of ROM memory area!\n");
+        else {
+            s->flags = val & PFFLAG_ROM_LOCK;
+            DPRINTF("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' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
+            /* Flush buffer */
+            s->log_buffer[s->log_buffer_off] = 0;
+            throttle(s, s->log_buffer_off);
+            DPRINTF("%s\n", s->log_buffer);
+            s->log_buffer_off = 0;
+            break;
+        }
+        s->log_buffer[s->log_buffer_off++] = val;
+        break;
+    }
+}
+
+static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
+{
+    PCIXenPlatformState *s = opaque;
+
+    switch (addr - XEN_PLATFORM_IOPORT) {
+    case 0:
+        if (s->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_readb(void *opaque, uint32_t addr)
+{
+    PCIXenPlatformState *s = opaque;
+
+    switch (addr - XEN_PLATFORM_IOPORT) {
+    case 0:
+        /* Platform flags */
+        return s->flags;
+    case 2:
+        /* Version number */
+        return 1;
+    default:
+        return 0xff;
+    }
+}
+
+static void platform_fixed_ioport_reset(void *opaque)
+{
+    PCIXenPlatformState *s = opaque;
+
+    platform_fixed_ioport_writeb(s, XEN_PLATFORM_IOPORT, 0);
+}
+
+static void platform_fixed_ioport_init(PCIXenPlatformState* s)
+{
+    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 4, platform_fixed_ioport_writel, s);
+    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 2, platform_fixed_ioport_writew, s);
+    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 1, platform_fixed_ioport_writeb, s);
+    register_ioport_read(XEN_PLATFORM_IOPORT, 16, 2, platform_fixed_ioport_readw, s);
+    register_ioport_read(XEN_PLATFORM_IOPORT, 16, 1, platform_fixed_ioport_readb, s);
+}
+
+/* Xen Platform PCI Device */
+
+static uint32_t xen_platform_ioport_readb(void *opaque, uint32_t addr)
+{
+    addr &= 0xff;
+
+    if (addr == 0)
+        return platform_fixed_ioport_readb(opaque, XEN_PLATFORM_IOPORT);
+    else
+        return ~0u;
+}
+
+static void xen_platform_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
+{
+    PCIXenPlatformState *s = opaque;
+
+    addr &= 0xff;
+    val  &= 0xff;
+
+    switch (addr) {
+    case 0: /* Platform flags */
+        platform_fixed_ioport_writeb(opaque, XEN_PLATFORM_IOPORT, val);
+        break;
+    case 8:
+        {
+            if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
+                /* Flush buffer */
+                s->log_buffer[s->log_buffer_off] = 0;
+                throttle(s, s->log_buffer_off);
+                DPRINTF("%s\n", s->log_buffer);
+                s->log_buffer_off = 0;
+                break;
+            }
+            s->log_buffer[s->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 = DO_UPCAST(PCIXenPlatformState, pci_dev, pci_dev);
+
+    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) {
+        DPRINTF("Warning: attempted read from physical address "
+                "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
+        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) {
+        DPRINTF("Warning: attempted write of 0x%x to physical "
+                "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
+                val, addr);
+        warnings++;
+    }
+}
+
+static CPUReadMemoryFunc * const platform_mmio_read_funcs[3] = {
+    platform_mmio_read,
+    platform_mmio_read,
+    platform_mmio_read,
+};
+
+static CPUWriteMemoryFunc * const 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, size, mmio_io_addr);
+}
+
+static int xen_platform_post_load(void *opaque, int version_id)
+{
+    PCIXenPlatformState *s = opaque;
+
+    platform_fixed_ioport_writeb(s, XEN_PLATFORM_IOPORT, s->flags);
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_xen_platform = {
+    .name = "platform",
+    .version_id = 4,
+    .minimum_version_id = 4,
+    .minimum_version_id_old = 4,
+    .post_load = xen_platform_post_load,
+    .fields = (VMStateField []) {
+        VMSTATE_PCI_DEVICE(pci_dev, PCIXenPlatformState),
+        VMSTATE_UINT8(flags, PCIXenPlatformState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static int xen_platform_initfn(PCIDevice *dev)
+{
+    PCIXenPlatformState *d = DO_UPCAST(PCIXenPlatformState, pci_dev, dev);
+    uint8_t *pci_conf;
+
+    pci_conf = d->pci_dev.config;
+
+    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_XENSOURCE);
+    pci_config_set_device_id(pci_conf, 0x0001);
+    pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
+
+    pci_config_set_revision(pci_conf, 1);
+    pci_config_set_prog_interface(pci_conf, 0);
+
+    pci_config_set_class(pci_conf, PCI_CLASS_OTHERS << 8 | 0x80);
+
+    pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
+    pci_conf[PCI_INTERRUPT_PIN] = 1;
+
+    /* Microsoft WHQL requires non-zero subsystem IDs. */
+    /* http://www.pcisig.com/reflector/msg02205.html.  */
+    pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, pci_conf[PCI_VENDOR_ID]);
+    pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0001);
+
+    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);
+
+    platform_fixed_ioport_init(d);
+
+    return 0;
+}
+
+static void platform_reset(DeviceState *dev)
+{
+    PCIXenPlatformState *s = DO_UPCAST(PCIXenPlatformState, pci_dev.qdev, dev);
+
+    platform_fixed_ioport_reset(s);
+}
+
+void pci_xen_platform_init(PCIBus *bus)
+{
+    PCIDevice *dev;
+
+    dev = pci_create(bus, -1, "xen-platform");
+
+    qdev_init_nofail(&dev->qdev);
+}
+
+static PCIDeviceInfo xen_platform_info = {
+    .init = xen_platform_initfn,
+    .qdev.name = "xen-platform",
+    .qdev.desc = "XEN platform pci device",
+    .qdev.size = sizeof(PCIXenPlatformState),
+    .qdev.vmsd = &vmstate_xen_platform,
+    .qdev.reset = platform_reset,
+};
+
+static void xen_platform_register(void)
+{
+    pci_qdev_register(&xen_platform_info);
+}
+
+device_init(xen_platform_register);
diff --git a/hw/xen_platform.h b/hw/xen_platform.h
new file mode 100644
index 0000000..574eecd
--- /dev/null
+++ b/hw/xen_platform.h
@@ -0,0 +1,8 @@ 
+#ifndef XEN_PLATFORM_H
+#define XEN_PLATFORM_H
+
+#include "hw/pci.h"
+
+void pci_xen_platform_init(PCIBus *bus);
+
+#endif