[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] Re: [Qemu-devel] [PATCH 09/15] xen: Initialize event channels and io rings
On Thu, Aug 12, 2010 at 2:09 PM, <stefano.stabellini@xxxxxxxxxxxxx> wrote: > From: Anthony PERARD <anthony.perard@xxxxxxxxxx> > > Open and bind event channels; map ioreq and buffered ioreq rings. > > Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx> > Signed-off-by: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx> > --- > Âhw/xen_machine_fv.c  |  25 ++++ > Âtarget-xen/cpu.h   Â|  Â1 + > Âtarget-xen/helper.c  | Â362 > +++++++++++++++++++++++++++++++++++++++++++++++++ > Âtarget-xen/qemu-xen.h |  Â2 + > Â4 files changed, 390 insertions(+), 0 deletions(-) > > diff --git a/hw/xen_machine_fv.c b/hw/xen_machine_fv.c > index a6e778a..b1bc88d 100644 > --- a/hw/xen_machine_fv.c > +++ b/hw/xen_machine_fv.c > @@ -22,6 +22,9 @@ > Â* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN > Â* THE SOFTWARE. > Â*/ > +#include "config.h" > + > +#include <sys/mman.h> > > Â#include "hw.h" > Â#include "pc.h" > @@ -71,12 +74,34 @@ static void xen_init_fv(ram_addr_t ram_size, > >   CPUState *env; > > +  Âunsigned long ioreq_pfn; > +  Âextern void *shared_page; > +  Âextern void *buffered_io_page; These should be defined in a header file. > + >   /* Initialize backend core & drivers */ >   if (xen_dm_init() != 0) { >     fprintf(stderr, "%s: xen backend core setup failed\n", __FUNCTION__); >     exit(1); >   } > > +  Âxc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_IOREQ_PFN, &ioreq_pfn); > +  Âfprintf(stderr, "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) { > +    Âfprintf(stderr, "map shared IO page returned error %d handle=%p\n", > errno, xen_xc); hw_error()? > +    Âexit(-1); > +  Â} > + > +  Âxc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn); > +  Âfprintf(stderr, "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) { > +    Âfprintf(logfile, "map buffered IO page returned error %d\n", errno); > +    Âexit(-1); > +  Â} > + >   /* Initialize a dummy CPU */ >   if (cpu_model == NULL) { > Â#ifdef TARGET_X86_64 > diff --git a/target-xen/cpu.h b/target-xen/cpu.h > index 5a45d1c..573241f 100644 > --- a/target-xen/cpu.h > +++ b/target-xen/cpu.h > @@ -72,6 +72,7 @@ typedef struct CPUXenState { > > ÂCPUXenState *cpu_xen_init(const char *cpu_model); > Âint cpu_xen_exec(CPUXenState *s); > +void cpu_xen_close(CPUXenState *s); > > Âint cpu_get_pic_interrupt(CPUXenState *s); > Âvoid cpu_set_ferr(CPUX86State *s); > diff --git a/target-xen/helper.c b/target-xen/helper.c > index 8cb7771..4571ac0 100644 > --- a/target-xen/helper.c > +++ b/target-xen/helper.c > @@ -18,25 +18,77 @@ > Â* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA Â02111-1307 ÂUSA > Â*/ > > +#include "config.h" > + > +#include <inttypes.h> > + > +#include <xenctrl.h> > +#include <xen/hvm/ioreq.h> > + > Â#include "cpu.h" > Â#include "qemu-xen.h" > Â#include "xenstore.h" > +#include "hw/xen_backend.h" > + > +long time_offset = 0; > + > +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 fd for polling */ > +int xce_handle = -1; > + > +/* which vcpu we are serving */ > +int send_vcpu = 0; > + > +/* the evtchn port for polling the notification, */ > +evtchn_port_t *ioreq_local_port; > > ÂCPUXenState *cpu_xen_init(const char *cpu_model) > Â{ >   CPUXenState *env = NULL; >   static int inited; > +  Âint i, rc; > >   env = qemu_mallocz(sizeof(CPUXenState)); >   if (!env) >     return NULL; >   cpu_exec_init(env); > > +  Â/* There is no shared_page for PV, we're done now */ > +  Âif (shared_page == NULL) > +    Âreturn env; > + > +  Âioreq_local_port = > +    Â(evtchn_port_t *)qemu_mallocz(smp_cpus * sizeof(evtchn_port_t)); > +  Âif (!ioreq_local_port) > +    Âreturn NULL; > + >   /* init various static tables */ >   if (!inited) { >     inited = 1; > >     cpu_single_env = env; > + > +    Âxce_handle = xc_evtchn_open(); > +    Âif (xce_handle == -1) { > +      Âperror("open"); > +      Âreturn NULL; > +    Â} > + > +    Â/* 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 NULL; > +      Â} > +      Âioreq_local_port[i] = rc; > +    Â} >   } > >   return env; > @@ -70,7 +122,317 @@ target_phys_addr_t cpu_get_phys_page_debug(CPUState > *env, target_ulong addr) >   return addr; > Â} > > +// get the ioreq packets from share mem > +static ioreq_t *__cpu_get_ioreq(int vcpu) Don't use names with leading underscores. > +{ > +  Âioreq_t *req = &shared_page->vcpu_ioreq[vcpu]; > + > +  Âif (req->state != STATE_IOREQ_READY) { > +    Âfprintf(stderr, "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++ ) > +      Âif ( ioreq_local_port[i] == port ) > +        Âbreak; > + > +    Âif ( i == smp_cpus ) { > +      Âfprintf(stderr, "Fatal error while trying to get io event!\n"); > +      Âexit(1); > +    Â} > + > +    Â// 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(i); > +  Â} > + > +  Â// read error or read nothing > +  Âreturn NULL; > +} > + > +static unsigned long do_inp(CPUState *env, unsigned long 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: > +      Âfprintf(stderr, "inp: bad size: %lx %lx\n", addr, size); > +      Âexit(-1); > +  Â} > +} > + > +static void do_outp(CPUState *env, unsigned long addr, > +    Âunsigned long size, unsigned long 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: > +      Âfprintf(stderr, "outp: bad size: %lx %lx\n", addr, size); > +      Âexit(-1); > +  Â} > +} > + > +static inline void read_physical(uint64_t addr, unsigned long size, void > *val) > +{ > +  Âreturn cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 0); > +} > + > +static inline void write_physical(uint64_t addr, unsigned long size, void > *val) > +{ > +  Âreturn cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 1); > +} Useless redirection? > + > +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 { > +      Âunsigned long tmp; > + > +      Âfor (i = 0; i < req->count; i++) { > +        Âtmp = do_inp(env, req->addr, req->size); > +        Âwrite_physical((target_phys_addr_t) req->data > +            Â+ (sign * i * req->size), > +            Âreq->size, &tmp); > +      Â} > +    Â} > +  Â} 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++) { > +        Âunsigned long tmp = 0; > + > +        Âread_physical((target_phys_addr_t) req->data > +            Â+ (sign * i * req->size), > +            Âreq->size, &tmp); > +        Â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++) { > +        Âread_physical(req->addr > +            Â+ (sign * i * req->size), > +            Âreq->size, &req->data); > +      Â} > +    Â} else if (req->dir == IOREQ_WRITE) { > +      Âfor (i = 0; i < req->count; i++) { > +        Âwrite_physical(req->addr > +            Â+ (sign * i * req->size), > +            Âreq->size, &req->data); > +      Â} > +    Â} > +  Â} else { > +    Âtarget_ulong tmp; > + > +    Âif (req->dir == IOREQ_READ) { > +      Âfor (i = 0; i < req->count; i++) { > +        Âread_physical(req->addr > +            Â+ (sign * i * req->size), > +            Âreq->size, &tmp); > +        Âwrite_physical((target_phys_addr_t )req->data > +            Â+ (sign * i * req->size), > +            Âreq->size, &tmp); > +      Â} > +    Â} else if (req->dir == IOREQ_WRITE) { > +      Âfor (i = 0; i < req->count; i++) { > +        Âread_physical((target_phys_addr_t) req->data > +            Â+ (sign * i * req->size), > +            Âreq->size, &tmp); > +        Âwrite_physical(req->addr > +            Â+ (sign * i * req->size), > +            Âreq->size, &tmp); > +      Â} > +    Â} > +  Â} > +} > + > +static void cpu_ioreq_timeoffset(CPUState *env, ioreq_t *req) > +{ > +  Âchar b[64]; > + > +  Âtime_offset += (unsigned long)req->data; > + > +  Âfprintf(stderr, "Time offset set %ld, added offset %"PRId64"\n", > +      Âtime_offset, req->data); > +  Âsprintf(b, "%ld", time_offset); snprintf > +  Âxenstore_vm_write(xen_domid, "rtc/timeoffset", b); > +} > + > +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. */ > + > +    Âreq->state = STATE_IORESP_READY; > +    Âxc_evtchn_notify(xce_handle, ioreq_local_port[send_vcpu]); > +  Â} > +} > + > Â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); > + >   xenstore_record_dm_state("running"); > Â} > + > +void destroy_hvm_domain(void) > +{ > +  Âxc_interface *xcHandle; xc_handle or something. > +  Âint sts; > + > +  ÂxcHandle = xc_interface_open(NULL, NULL, 0); > +  Âif (xcHandle < 0) > +    Âfprintf(stderr, "Cannot acquire xenctrl handle\n"); > +  Âelse { > +    Âsts = xc_domain_shutdown(xcHandle, xen_domid, SHUTDOWN_poweroff); > +    Âif (sts != 0) > +      Âfprintf(stderr, "? xc_domain_shutdown failed to issue poweroff, " > +          Â"sts %d, errno %d\n", sts, errno); > +    Âelse > +      Âfprintf(stderr, "Issued domain %d poweroff\n", xen_domid); > +    Âxc_interface_close(xcHandle); > +  Â} > +} > diff --git a/target-xen/qemu-xen.h b/target-xen/qemu-xen.h > index 091ae07..79a4638 100644 > --- a/target-xen/qemu-xen.h > +++ b/target-xen/qemu-xen.h > @@ -22,12 +22,14 @@ void   qemu_invalidate_map_cache(void); > > Â/* target-xen/exec-dm.c */ > > +void destroy_hvm_domain(void); > Âint cpu_register_io_memory_fixed(int io_index, >              ÂCPUReadMemoryFunc * const *mem_read, >              ÂCPUWriteMemoryFunc * const *mem_write, >              Âvoid *opaque); > > Â/* target-xen/helper.c */ > +extern int xce_handle; > Âvoid xen_main_loop_prepare(void); > > Â#endif /*QEMU_XEN_H*/ > -- > 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 |