[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
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
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |