[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] Re: [Qemu-devel] [PATCH RFC V3 10/12] xen: Initialize event channels and io rings


  • To: anthony.perard@xxxxxxxxxx
  • From: Blue Swirl <blauwirbel@xxxxxxxxx>
  • Date: Fri, 17 Sep 2010 19:27:08 +0000
  • Cc: xen-devel@xxxxxxxxxxxxxxxxxxx, qemu-devel@xxxxxxxxxx, Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>
  • Delivery-date: Fri, 17 Sep 2010 12:28:59 -0700
  • Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=F7Y2Cqrs3CS9nYD/41hq5Myqw3sT/2RcGgiCzEOcdUEcVKaEyIcWmD61eSaGDseyUd DnCfSrNEhrjT3UWqbIIZDo8O2n5TjiqvMmdBeoDAZDTBDNU6kHcwaIIe9GrJpAh/wpyK c+56vrhkRr/crlORNsqdFqvhPX3hmXjovOWJw=
  • List-id: Xen developer discussion <xen-devel.lists.xensource.com>

On Fri, Sep 17, 2010 at 11:15 AM,  <anthony.perard@xxxxxxxxxx> wrote:
> From: Anthony PERARD <anthony.perard@xxxxxxxxxx>
>
> Open and bind event channels; map ioreq and buffered ioreq rings.

In general, because of CPUState accesses and cpu_in/out use, this
looks like CPU code, specifically x86. Could this belong to
target-i386/xen.c instead, much like target-i386/kvm.c vs ./kvm-all.c?
Do other CPU types use this stuff?

>
> Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
> Signed-off-by: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>
> ---
> Âhw/xen_common.h | Â Â1 +
> Âxen-all.c    | Â381 
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++
> Â2 files changed, 382 insertions(+), 0 deletions(-)
>
> diff --git a/hw/xen_common.h b/hw/xen_common.h
> index dd54063..96cfad7 100644
> --- a/hw/xen_common.h
> +++ b/hw/xen_common.h
> @@ -53,5 +53,6 @@ typedef xc_interface *qemu_xc_interface;
> Â#endif
>
> Âqemu_irq *i8259_xen_init(void);
> +void destroy_hvm_domain(void);
>
> Â#endif /* QEMU_HW_XEN_COMMON_H */
> diff --git a/xen-all.c b/xen-all.c
> index 4e0b061..13672f0 100644
> --- a/xen-all.c
> +++ b/xen-all.c
> @@ -8,12 +8,38 @@
>
> Â#include "config.h"
>
> +#include <sys/mman.h>
> +
> Â#include "hw/pci.h"
> Â#include "hw/xen_common.h"
> Â#include "hw/xen_backend.h"
>
> Â#include "xen_mapcache.h"
>
> +#include <xen/hvm/ioreq.h>
> +
> +//#define DEBUG_XEN
> +
> +#ifdef DEBUG_XEN
> +#define DPRINTF(fmt, ...) \
> + Â Âdo { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
> +#else
> +#define DPRINTF(fmt, ...) \
> + Â Âdo { } while (0)
> +#endif
> +
> +shared_iopage_t *shared_page = NULL;
> +#define BUFFER_IO_MAX_DELAY Â100
> +buffered_iopage_t *buffered_io_page = NULL;
> +QEMUTimer *buffered_io_timer;
> +/* the evtchn port for polling the notification, */
> +evtchn_port_t *ioreq_local_port;
> +/* the evtchn fd for polling */
> +int xce_handle = -1;
> +/* which vcpu we are serving */
> +int send_vcpu = 0;
> +long time_offset = 0;

Are all these global needed? Can some of them actually be 'static'?
Could you wrap these into a struct and pass that around?

> +
> Â/* Xen specific function for piix pci */
>
> Âint xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
> @@ -111,19 +137,374 @@ void xen_ram_alloc(ram_addr_t ram_addr, ram_addr_t 
> size)
> Â}
>
>
> +/* VCPU Operations, MMIO, IO ring ... */
> +
> +/* get the ioreq packets from share mem */
> +static ioreq_t *cpu_get_ioreq_from_shared_memory(int vcpu)
> +{
> + Â Âioreq_t *req = &shared_page->vcpu_ioreq[vcpu];
> +
> + Â Âif (req->state != STATE_IOREQ_READY) {
> + Â Â Â ÂDPRINTF("I/O request not ready: "
> + Â Â Â Â Â Â Â Â"%x, ptr: %x, port: %"PRIx64", "
> + Â Â Â Â Â Â Â Â"data: %"PRIx64", count: %u, size: %u\n",
> + Â Â Â Â Â Â Â Âreq->state, req->data_is_ptr, req->addr,
> + Â Â Â Â Â Â Â Âreq->data, req->count, req->size);
> + Â Â Â Âreturn NULL;
> + Â Â}
> +
> + Â Âxen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
> +
> + Â Âreq->state = STATE_IOREQ_INPROCESS;
> + Â Âreturn req;
> +}
> +
> +/* use poll to get the port notification */
> +/* ioreq_vec--out,the */
> +/* retval--the number of ioreq packet */
> +static ioreq_t *cpu_get_ioreq(void)
> +{
> + Â Âint i;
> + Â Âevtchn_port_t port;
> +
> + Â Âport = xc_evtchn_pending(xce_handle);
> + Â Âif (port != -1) {
> + Â Â Â Âfor ( i = 0; i < smp_cpus; i++ )

Please add braces and remove extra spaces after '(' and before ')',
also in other places.

> + Â Â Â Â Â Âif ( ioreq_local_port[i] == port )
> + Â Â Â Â Â Â Â Âbreak;
> +
> + Â Â Â Âif ( i == smp_cpus ) {
> + Â Â Â Â Â Âhw_error("Fatal error while trying to get io event!\n");
> + Â Â Â Â}
> +
> + Â Â Â Â/* unmask the wanted port again */
> + Â Â Â Âxc_evtchn_unmask(xce_handle, port);
> +
> + Â Â Â Â/* get the io packet from shared memory */
> + Â Â Â Âsend_vcpu = i;
> + Â Â Â Âreturn cpu_get_ioreq_from_shared_memory(i);
> + Â Â}
> +
> + Â Â/* read error or read nothing */
> + Â Âreturn NULL;
> +}
> +
> +static uint32_t do_inp(CPUState *env, pio_addr_t addr, unsigned long size)
> +{
> + Â Âswitch(size) {
> + Â Â Â Âcase 1:
> + Â Â Â Â Â Âreturn cpu_inb(addr);
> + Â Â Â Âcase 2:
> + Â Â Â Â Â Âreturn cpu_inw(addr);
> + Â Â Â Âcase 4:
> + Â Â Â Â Â Âreturn cpu_inl(addr);
> + Â Â Â Âdefault:
> + Â Â Â Â Â Âhw_error("inp: bad size: %04"FMT_pioaddr" %lx", addr, size);
> + Â Â}
> +}
> +
> +static void do_outp(CPUState *env, pio_addr_t addr,
> + Â Â Â Âunsigned long size, uint32_t val)
> +{
> + Â Âswitch(size) {
> + Â Â Â Âcase 1:
> + Â Â Â Â Â Âreturn cpu_outb(addr, val);
> + Â Â Â Âcase 2:
> + Â Â Â Â Â Âreturn cpu_outw(addr, val);
> + Â Â Â Âcase 4:
> + Â Â Â Â Â Âreturn cpu_outl(addr, val);
> + Â Â Â Âdefault:
> + Â Â Â Â Â Âhw_error("outp: bad size: %04"FMT_pioaddr" %lx", addr, size);
> + Â Â}
> +}
> +
> +static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
> +{
> + Â Âint i, sign;
> +
> + Â Âsign = req->df ? -1 : 1;
> +
> + Â Âif (req->dir == IOREQ_READ) {
> + Â Â Â Âif (!req->data_is_ptr) {
> + Â Â Â Â Â Âreq->data = do_inp(env, req->addr, req->size);
> + Â Â Â Â} else {
> + Â Â Â Â Â Âuint32_t tmp;
> +
> + Â Â Â Â Â Âfor (i = 0; i < req->count; i++) {
> + Â Â Â Â Â Â Â Âtmp = do_inp(env, req->addr, req->size);
> + Â Â Â Â Â Â Â Âcpu_physical_memory_write(req->data + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &tmp, req->size);
> + Â Â Â Â Â Â}
> + Â Â Â Â}
> + Â Â} else if (req->dir == IOREQ_WRITE) {
> + Â Â Â Âif (!req->data_is_ptr) {
> + Â Â Â Â Â Âdo_outp(env, req->addr, req->size, req->data);
> + Â Â Â Â} else {
> + Â Â Â Â Â Âfor (i = 0; i < req->count; i++) {
> + Â Â Â Â Â Â Â Âuint32_t tmp = 0;
> +
> + Â Â Â Â Â Â Â Âcpu_physical_memory_read(req->data + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &tmp, req->size);
> + Â Â Â Â Â Â Â Âdo_outp(env, req->addr, req->size, tmp);
> + Â Â Â Â Â Â}
> + Â Â Â Â}
> + Â Â}
> +}
> +
> +static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
> +{
> + Â Âint i, sign;
> +
> + Â Âsign = req->df ? -1 : 1;
> +
> + Â Âif (!req->data_is_ptr) {
> + Â Â Â Âif (req->dir == IOREQ_READ) {
> + Â Â Â Â Â Âfor (i = 0; i < req->count; i++) {
> + Â Â Â Â Â Â Â Âcpu_physical_memory_read(req->addr + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &req->data, req->size);
> + Â Â Â Â Â Â}
> + Â Â Â Â} else if (req->dir == IOREQ_WRITE) {
> + Â Â Â Â Â Âfor (i = 0; i < req->count; i++) {
> + Â Â Â Â Â Â Â Âcpu_physical_memory_write(req->addr + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &req->data, req->size);
> + Â Â Â Â Â Â}
> + Â Â Â Â}
> + Â Â} else {
> + Â Â Â Âtarget_ulong tmp;
> +
> + Â Â Â Âif (req->dir == IOREQ_READ) {
> + Â Â Â Â Â Âfor (i = 0; i < req->count; i++) {
> + Â Â Â Â Â Â Â Âcpu_physical_memory_read(req->addr + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &tmp, req->size);
> + Â Â Â Â Â Â Â Âcpu_physical_memory_write(req->data + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &tmp, req->size);
> + Â Â Â Â Â Â}
> + Â Â Â Â} else if (req->dir == IOREQ_WRITE) {
> + Â Â Â Â Â Âfor (i = 0; i < req->count; i++) {
> + Â Â Â Â Â Â Â Âcpu_physical_memory_read(req->data + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &tmp, req->size);
> + Â Â Â Â Â Â Â Âcpu_physical_memory_write(req->addr + (sign * i * req->size),
> + Â Â Â Â Â Â Â Â Â Â Â Â(uint8_t*) &tmp, req->size);
> + Â Â Â Â Â Â}
> + Â Â Â Â}
> + Â Â}
> +}
> +
> +static void cpu_ioreq_timeoffset(CPUState *env, ioreq_t *req)
> +{
> + Â Â/* char b[64]; */
> +
> + Â Âtime_offset += (unsigned long)req->data;
> +
> + Â Â//DPRINTF("Time offset set %ld, added offset %"PRId64"\n",
> + Â Â Â Â Â Â//time_offset, req->data);
> + Â Â/* snprintf(b, 64, "%ld", time_offset); */
> + Â Â/* xenstore_vm_write(xen_domid, "rtc/timeoffset", b); */

The commented out stuff should probably go.

> +}
> +
> +static void handle_ioreq(CPUState *env, ioreq_t *req)
> +{
> + Â Âif (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
> + Â Â Â Â Â Â(req->size < sizeof(target_ulong)))
> + Â Â Â Âreq->data &= ((target_ulong)1 << (8 * req->size)) - 1;
> +
> + Â Âswitch (req->type) {
> + Â Â Â Âcase IOREQ_TYPE_PIO:
> + Â Â Â Â Â Âcpu_ioreq_pio(env, req);
> + Â Â Â Â Â Âbreak;
> + Â Â Â Âcase IOREQ_TYPE_COPY:
> + Â Â Â Â Â Âcpu_ioreq_move(env, req);
> + Â Â Â Â Â Âbreak;
> + Â Â Â Âcase IOREQ_TYPE_TIMEOFFSET:
> + Â Â Â Â Â Âcpu_ioreq_timeoffset(env, req);
> + Â Â Â Â Â Âbreak;
> + Â Â Â Âcase IOREQ_TYPE_INVALIDATE:
> + Â Â Â Â Â Âqemu_invalidate_map_cache();
> + Â Â Â Â Â Âbreak;
> + Â Â Â Âdefault:
> + Â Â Â Â Â Âhw_error("Invalid ioreq type 0x%x\n", req->type);
> + Â Â}
> +}
> +
> +static void handle_buffered_iopage(CPUState *env)
> +{
> + Â Âbuf_ioreq_t *buf_req = NULL;
> + Â Âioreq_t req;
> + Â Âint qw;
> +
> + Â Âif (!buffered_io_page)
> + Â Â Â Âreturn;
> +
> + Â Âwhile (buffered_io_page->read_pointer !=
> + Â Â Â Â Â Âbuffered_io_page->write_pointer) {
> + Â Â Â Âbuf_req = &buffered_io_page->buf_ioreq[
> + Â Â Â Â Â Âbuffered_io_page->read_pointer % IOREQ_BUFFER_SLOT_NUM];
> + Â Â Â Âreq.size = 1UL << buf_req->size;
> + Â Â Â Âreq.count = 1;
> + Â Â Â Âreq.addr = buf_req->addr;
> + Â Â Â Âreq.data = buf_req->data;
> + Â Â Â Âreq.state = STATE_IOREQ_READY;
> + Â Â Â Âreq.dir = buf_req->dir;
> + Â Â Â Âreq.df = 1;
> + Â Â Â Âreq.type = buf_req->type;
> + Â Â Â Âreq.data_is_ptr = 0;
> + Â Â Â Âqw = (req.size == 8);
> + Â Â Â Âif (qw) {
> + Â Â Â Â Â Âbuf_req = &buffered_io_page->buf_ioreq[
> + Â Â Â Â Â Â Â Â(buffered_io_page->read_pointer+1) % IOREQ_BUFFER_SLOT_NUM];
> + Â Â Â Â Â Âreq.data |= ((uint64_t)buf_req->data) << 32;
> + Â Â Â Â}
> +
> + Â Â Â Âhandle_ioreq(env, &req);
> +
> + Â Â Â Âxen_mb();
> + Â Â Â Âbuffered_io_page->read_pointer += qw ? 2 : 1;
> + Â Â}
> +}
> +
> +static void handle_buffered_io(void *opaque)
> +{
> + Â ÂCPUState *env = opaque;
> +
> + Â Âhandle_buffered_iopage(env);
> + Â Âqemu_mod_timer(buffered_io_timer, BUFFER_IO_MAX_DELAY +
> + Â Â Â Â Â Â Â Â Â qemu_get_clock(rt_clock));
> +}
> +
> +static void cpu_handle_ioreq(void *opaque)
> +{
> + Â ÂCPUState *env = opaque;
> + Â Âioreq_t *req = cpu_get_ioreq();
> +
> + Â Âhandle_buffered_iopage(env);
> + Â Âif (req) {
> + Â Â Â Âhandle_ioreq(env, req);
> +
> + Â Â Â Âif (req->state != STATE_IOREQ_INPROCESS) {
> + Â Â Â Â Â Âfprintf(stderr, "Badness in I/O request ... not in service?!: "
> + Â Â Â Â Â Â Â Â Â Â"%x, ptr: %x, port: %"PRIx64", "
> + Â Â Â Â Â Â Â Â Â Â"data: %"PRIx64", count: %u, size: %u\n",
> + Â Â Â Â Â Â Â Â Â Âreq->state, req->data_is_ptr, req->addr,
> + Â Â Â Â Â Â Â Â Â Âreq->data, req->count, req->size);
> + Â Â Â Â Â Âdestroy_hvm_domain();
> + Â Â Â Â Â Âreturn;
> + Â Â Â Â}
> +
> + Â Â Â Âxen_wmb(); /* Update ioreq contents /then/ update state. */
> +
> + Â Â Â Â/*
> + Â Â Â Â * We do this before we send the response so that the tools
> + Â Â Â Â * have the opportunity to pick up on the reset before the
> + Â Â Â Â * guest resumes and does a hlt with interrupts disabled which
> + Â Â Â Â * causes Xen to powerdown the domain.
> + Â Â Â Â */
> + Â Â Â Âif (vm_running) {
> + Â Â Â Â Â Âif (qemu_shutdown_requested_get()) {
> + Â Â Â Â Â Â Â Âdestroy_hvm_domain();
> + Â Â Â Â Â Â}
> + Â Â Â Â Â Âif (qemu_reset_requested_get()) {
> + Â Â Â Â Â Â Â Âqemu_system_reset();
> + Â Â Â Â Â Â}
> + Â Â Â Â}
> +
> + Â Â Â Âreq->state = STATE_IORESP_READY;
> + Â Â Â Âxc_evtchn_notify(xce_handle, ioreq_local_port[send_vcpu]);
> + Â Â}
> +}
> +
> +static void xen_main_loop_prepare(void)
> +{
> + Â ÂCPUState *env = cpu_single_env;
> +
> + Â Âint evtchn_fd = xce_handle == -1 ? -1 : xc_evtchn_fd(xce_handle);
> +
> + Â Âbuffered_io_timer = qemu_new_timer(rt_clock, handle_buffered_io,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â cpu_single_env);
> + Â Âqemu_mod_timer(buffered_io_timer, qemu_get_clock(rt_clock));
> +
> + Â Âif (evtchn_fd != -1)
> + Â Â Â Âqemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, env);

braces

> +}
> +
> +
> Â/* Initialise Xen */
>
> +static void xen_vm_change_state_handler(void *opaque, int running, int 
> reason)
> +{
> + Â Âif (running)

braces

> + Â Â Â Âxen_main_loop_prepare();
> +}
> +
> Âint xen_init(int smp_cpus)
> Â{
> + Â Âint i, rc;
> + Â Âunsigned long ioreq_pfn;
> +
> Â Â xen_xc = xc_interface_open(NULL, NULL, 0);
> Â Â if (xen_xc == NULL) {
> Â Â Â Â xen_be_printf(NULL, 0, "can't open xen interface\n");
> Â Â Â Â return -1;
> Â Â }
>
> + Â Âxce_handle = xc_evtchn_open();
> + Â Âif (xce_handle == -1) {
> + Â Â Â Âperror("open");
> + Â Â Â Âreturn -errno;
> + Â Â}
> +
> + Â Âxc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn);
> + Â ÂDPRINTF("shared page at pfn %lx\n", ioreq_pfn);
> + Â Âshared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â PROT_READ|PROT_WRITE, ioreq_pfn);
> + Â Âif (shared_page == NULL) {
> + Â Â Â Âhw_error("map shared IO page returned error %d handle=%p", errno, 
> xen_xc);
> + Â Â}
> +
> + Â Âxc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
> + Â ÂDPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
> + Â Âbuffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂPROT_READ|PROT_WRITE, ioreq_pfn);
> + Â Âif (buffered_io_page == NULL) {
> + Â Â Â Âhw_error("map buffered IO page returned error %d", errno);
> + Â Â}
> +
> + Â Âioreq_local_port = qemu_mallocz(smp_cpus * sizeof(evtchn_port_t));
> +
> + Â Â/* FIXME: how about if we overflow the page here? */
> + Â Âfor (i = 0; i < smp_cpus; i++) {
> + Â Â Â Ârc = xc_evtchn_bind_interdomain(xce_handle, xen_domid,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âshared_page->vcpu_ioreq[i].vp_eport);
> + Â Â Â Âif (rc == -1) {
> + Â Â Â Â Â Âfprintf(stderr, "bind interdomain ioctl error %d\n", errno);
> + Â Â Â Â Â Âreturn -1;
> + Â Â Â Â}
> + Â Â Â Âioreq_local_port[i] = rc;
> + Â Â}
> +
> Â Â /* Init RAM management */
> Â Â qemu_map_cache_init();
> Â Â xen_ram_init(ram_size);
>
> + Â Âqemu_add_vm_change_state_handler(xen_vm_change_state_handler, NULL);
> +
> Â Â return 0;
> Â}
> +
> +void destroy_hvm_domain(void)
> +{
> + Â Âxc_interface *xc_handle;
> + Â Âint sts;
> +
> + Â Âxc_handle = xc_interface_open(NULL, NULL, 0);
> + Â Âif (!xc_handle)
> + Â Â Â Âfprintf(stderr, "Cannot acquire xenctrl handle\n");
> + Â Âelse {
> + Â Â Â Âsts = xc_domain_shutdown(xc_handle, xen_domid, SHUTDOWN_poweroff);
> + Â Â Â Âif (sts != 0)
> + Â Â Â Â Â Âfprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, "
> + Â Â Â Â Â Â Â Â Â Â"sts %d, errno %d\n", sts, errno);

braces, perror()

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.