[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH v3 3/6] ioreq-server: create basic ioreq server abstraction.
Collect together data structures concerning device emulation together into a new struct hvm_ioreq_server. Code that deals with the shared and buffered ioreq pages is extracted from functions such as hvm_domain_initialise, hvm_vcpu_initialise and do_hvm_op and consolidated into a set of hvm_ioreq_server manipulation functions. This patch also adds some more missing emacs boilerplate in the places where I needed it. Signed-off-by: Paul Durrant <paul.durrant@xxxxxxxxxx> --- xen/arch/x86/hvm/hvm.c | 303 +++++++++++++++++++++++++------------- xen/include/asm-x86/hvm/domain.h | 17 ++- xen/include/asm-x86/hvm/vcpu.h | 12 +- 3 files changed, 226 insertions(+), 106 deletions(-) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 10b8e8c..bbf9577 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -345,39 +345,43 @@ void hvm_migrate_pirqs(struct vcpu *v) spin_unlock(&d->event_lock); } -static ioreq_t *get_ioreq(struct vcpu *v) +static ioreq_t *get_ioreq(struct hvm_ioreq_server *s, int id) { - struct domain *d = v->domain; - shared_iopage_t *p = d->arch.hvm_domain.ioreq.va; + shared_iopage_t *p = s->ioreq.va; - ASSERT((v == current) || spin_is_locked(&d->arch.hvm_domain.ioreq.lock)); + ASSERT(p != NULL); - return p ? &p->vcpu_ioreq[v->vcpu_id] : NULL; + return &p->vcpu_ioreq[id]; } bool_t hvm_io_pending(struct vcpu *v) { - ioreq_t *p = get_ioreq(v); + struct hvm_ioreq_server *s = v->domain->arch.hvm_domain.ioreq_server; + ioreq_t *p; - if ( !p ) - return 0; + if ( !s ) + return 0; + p = get_ioreq(s, v->vcpu_id); return ( p->state != STATE_IOREQ_NONE ); } void hvm_do_resume(struct vcpu *v) { - ioreq_t *p = get_ioreq(v); + struct domain *d = v->domain; + struct hvm_ioreq_server *s = d->arch.hvm_domain.ioreq_server; + ioreq_t *p; check_wakeup_from_wait(); if ( is_hvm_vcpu(v) ) pt_restore_timer(v); - /* NB. Optimised for common case (p->state == STATE_IOREQ_NONE). */ - if ( !p ) + if ( !s ) goto check_inject_trap; + /* NB. Optimised for common case (p->state == STATE_IOREQ_NONE). */ + p = get_ioreq(s, v->vcpu_id); while ( p->state != STATE_IOREQ_NONE ) { switch ( p->state ) @@ -387,7 +391,7 @@ void hvm_do_resume(struct vcpu *v) break; case STATE_IOREQ_READY: /* IOREQ_{READY,INPROCESS} -> IORESP_READY */ case STATE_IOREQ_INPROCESS: - wait_on_xen_event_channel(v->arch.hvm_vcpu.xen_port, + wait_on_xen_event_channel(p->vp_eport, (p->state != STATE_IOREQ_READY) && (p->state != STATE_IOREQ_INPROCESS)); break; @@ -410,7 +414,6 @@ void hvm_do_resume(struct vcpu *v) static void hvm_init_ioreq_page( struct domain *d, struct hvm_ioreq_page *iorp) { - memset(iorp, 0, sizeof(*iorp)); spin_lock_init(&iorp->lock); domain_pause(d); } @@ -553,6 +556,149 @@ static int handle_pvh_io( return X86EMUL_OKAY; } +static void hvm_update_ioreq_server_evtchn(struct hvm_ioreq_server *s) +{ + if ( s->ioreq.va != NULL ) + { + struct domain *d = s->domain; + shared_iopage_t *p = s->ioreq.va; + struct vcpu *v; + + for_each_vcpu ( d, v ) + p->vcpu_ioreq[v->vcpu_id].vp_eport = v->arch.hvm_vcpu.ioreq_evtchn; + } +} + +static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s, struct vcpu *v) +{ + int rc; + + rc = alloc_unbound_xen_event_channel(v, s->domid, NULL); + if ( rc < 0 ) + goto done; + + v->arch.hvm_vcpu.ioreq_evtchn = rc; + + if ( v->vcpu_id == 0 ) + { + struct domain *d = s->domain; + + rc = alloc_unbound_xen_event_channel(v, s->domid, NULL); + if ( rc < 0 ) + goto done; + + s->buf_ioreq_evtchn = rc; + d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN] = s->buf_ioreq_evtchn; + } + + hvm_update_ioreq_server_evtchn(s); + rc = 0; + + done: + return rc; +} + +static void hvm_ioreq_server_remove_vcpu(struct hvm_ioreq_server *s, struct vcpu *v) +{ + if ( v->vcpu_id == 0 ) + free_xen_event_channel(v, s->buf_ioreq_evtchn); + + free_xen_event_channel(v, v->arch.hvm_vcpu.ioreq_evtchn); +} + +static int hvm_create_ioreq_server(struct domain *d) +{ + struct hvm_ioreq_server *s; + + s = xzalloc(struct hvm_ioreq_server); + if ( !s ) + return -ENOMEM; + + s->domain = d; + + hvm_init_ioreq_page(d, &s->ioreq); + hvm_init_ioreq_page(d, &s->buf_ioreq); + + d->arch.hvm_domain.ioreq_server = s; + return 0; +} + +static void hvm_destroy_ioreq_server(struct domain *d) +{ + struct hvm_ioreq_server *s = d->arch.hvm_domain.ioreq_server; + + hvm_destroy_ioreq_page(d, &s->ioreq); + hvm_destroy_ioreq_page(d, &s->buf_ioreq); + + xfree(s); +} + +static int hvm_replace_event_channel(struct vcpu *v, domid_t remote_domid, + evtchn_port_t *p_port) +{ + evtchn_port_t old_port, new_port; + + new_port = alloc_unbound_xen_event_channel(v, remote_domid, NULL); + if ( new_port < 0 ) + return new_port; + + /* xchg() ensures that only we call free_xen_event_channel(). */ + old_port = xchg(p_port, new_port); + free_xen_event_channel(v, old_port); + return 0; +} + +static int hvm_set_ioreq_server_domid(struct hvm_ioreq_server *s, domid_t domid) +{ + struct domain *d = s->domain; + struct vcpu *v; + int rc; + + domain_pause(d); + + for_each_vcpu ( d, v ) + { + rc = hvm_replace_event_channel(v, domid, &v->arch.hvm_vcpu.ioreq_evtchn); + if ( rc ) + goto done; + + if ( v->vcpu_id == 0 ) { + rc = hvm_replace_event_channel(v, domid, &s->buf_ioreq_evtchn); + if ( rc ) + goto done; + + d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN] = s->buf_ioreq_evtchn; + } + } + + hvm_update_ioreq_server_evtchn(s); + + s->domid = domid; + rc = 0; + + done: + domain_unpause(d); + + return rc; +} + +static int hvm_set_ioreq_server_pfn(struct hvm_ioreq_server *s, unsigned long pfn) +{ + int rc; + + rc = hvm_set_ioreq_page(s->domain, &s->ioreq, pfn); + if ( rc ) + return rc; + + hvm_update_ioreq_server_evtchn(s); + return 0; +} + +static int hvm_set_ioreq_server_buf_pfn(struct hvm_ioreq_server *s, unsigned long pfn) +{ + return hvm_set_ioreq_page(s->domain, &s->buf_ioreq, pfn); +} + int hvm_domain_initialise(struct domain *d) { int rc; @@ -620,17 +766,20 @@ int hvm_domain_initialise(struct domain *d) rtc_init(d); - hvm_init_ioreq_page(d, &d->arch.hvm_domain.ioreq); - hvm_init_ioreq_page(d, &d->arch.hvm_domain.buf_ioreq); + rc = hvm_create_ioreq_server(d); + if ( rc != 0 ) + goto fail2; register_portio_handler(d, 0xe9, 1, hvm_print_line); rc = hvm_funcs.domain_initialise(d); if ( rc != 0 ) - goto fail2; + goto fail3; return 0; + fail3: + hvm_destroy_ioreq_server(d); fail2: rtc_deinit(d); stdvga_deinit(d); @@ -654,8 +803,7 @@ void hvm_domain_relinquish_resources(struct domain *d) if ( hvm_funcs.nhvm_domain_relinquish_resources ) hvm_funcs.nhvm_domain_relinquish_resources(d); - hvm_destroy_ioreq_page(d, &d->arch.hvm_domain.ioreq); - hvm_destroy_ioreq_page(d, &d->arch.hvm_domain.buf_ioreq); + hvm_destroy_ioreq_server(d); msixtbl_pt_cleanup(d); @@ -1287,7 +1435,7 @@ int hvm_vcpu_initialise(struct vcpu *v) { int rc; struct domain *d = v->domain; - domid_t dm_domid; + struct hvm_ioreq_server *s = d->arch.hvm_domain.ioreq_server; hvm_asid_flush_vcpu(v); @@ -1330,30 +1478,10 @@ int hvm_vcpu_initialise(struct vcpu *v) && (rc = nestedhvm_vcpu_initialise(v)) < 0 ) /* teardown: nestedhvm_vcpu_destroy */ goto fail5; - dm_domid = d->arch.hvm_domain.params[HVM_PARAM_DM_DOMAIN]; - - /* Create ioreq event channel. */ - rc = alloc_unbound_xen_event_channel(v, dm_domid, NULL); /* teardown: none */ - if ( rc < 0 ) + rc = hvm_ioreq_server_add_vcpu(s, v); + if ( rc != 0 ) goto fail6; - /* Register ioreq event channel. */ - v->arch.hvm_vcpu.xen_port = rc; - - if ( v->vcpu_id == 0 ) - { - /* Create bufioreq event channel. */ - rc = alloc_unbound_xen_event_channel(v, dm_domid, NULL); /* teardown: none */ - if ( rc < 0 ) - goto fail6; - d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN] = rc; - } - - spin_lock(&d->arch.hvm_domain.ioreq.lock); - if ( d->arch.hvm_domain.ioreq.va != NULL ) - get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port; - spin_unlock(&d->arch.hvm_domain.ioreq.lock); - if ( v->vcpu_id == 0 ) { /* NB. All these really belong in hvm_domain_initialise(). */ @@ -1387,6 +1515,11 @@ int hvm_vcpu_initialise(struct vcpu *v) void hvm_vcpu_destroy(struct vcpu *v) { + struct domain *d = v->domain; + struct hvm_ioreq_server *s = d->arch.hvm_domain.ioreq_server; + + hvm_ioreq_server_remove_vcpu(s, v); + nestedhvm_vcpu_destroy(v); free_compat_arg_xlat(v); @@ -1398,9 +1531,6 @@ void hvm_vcpu_destroy(struct vcpu *v) vlapic_destroy(v); hvm_funcs.vcpu_destroy(v); - - /* Event channel is already freed by evtchn_destroy(). */ - /*free_xen_event_channel(v, v->arch.hvm_vcpu.xen_port);*/ } void hvm_vcpu_down(struct vcpu *v) @@ -1429,8 +1559,9 @@ void hvm_vcpu_down(struct vcpu *v) int hvm_buffered_io_send(struct domain *d, const ioreq_t *p) { - struct hvm_ioreq_page *iorp = &d->arch.hvm_domain.buf_ioreq; - buffered_iopage_t *pg = iorp->va; + struct hvm_ioreq_server *s = d->arch.hvm_domain.ioreq_server; + struct hvm_ioreq_page *iorp; + buffered_iopage_t *pg; buf_ioreq_t bp; /* Timeoffset sends 64b data, but no address. Use two consecutive slots. */ int qw = 0; @@ -1438,6 +1569,12 @@ int hvm_buffered_io_send(struct domain *d, const ioreq_t *p) /* Ensure buffered_iopage fits in a page */ BUILD_BUG_ON(sizeof(buffered_iopage_t) > PAGE_SIZE); + if ( !s ) + return 0; + + iorp = &s->buf_ioreq; + pg = iorp->va; + /* * Return 0 for the cases we can't deal with: * - 'addr' is only a 20-bit field, so we cannot address beyond 1MB @@ -1496,7 +1633,7 @@ int hvm_buffered_io_send(struct domain *d, const ioreq_t *p) wmb(); pg->write_pointer += qw ? 2 : 1; - notify_via_xen_event_channel(d, d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]); + notify_via_xen_event_channel(d, s->buf_ioreq_evtchn); spin_unlock(&iorp->lock); return 1; @@ -1504,24 +1641,28 @@ int hvm_buffered_io_send(struct domain *d, const ioreq_t *p) bool_t hvm_has_dm(struct domain *d) { - return !!d->arch.hvm_domain.ioreq.va; + return !!d->arch.hvm_domain.ioreq_server; } bool_t hvm_send_assist_req(struct vcpu *v, const ioreq_t *proto_p) { + struct domain *d = v->domain; + struct hvm_ioreq_server *s = d->arch.hvm_domain.ioreq_server; ioreq_t *p; if ( unlikely(!vcpu_start_shutdown_deferral(v)) ) return 0; /* implicitly bins the i/o operation */ - if ( !(p = get_ioreq(v)) ) + if ( !s ) return 0; + p = get_ioreq(s, v->vcpu_id); + if ( unlikely(p->state != STATE_IOREQ_NONE) ) { /* This indicates a bug in the device model. Crash the domain. */ gdprintk(XENLOG_ERR, "Device model set bad IO state %d.\n", p->state); - domain_crash(v->domain); + domain_crash(d); return 0; } @@ -1534,14 +1675,14 @@ bool_t hvm_send_assist_req(struct vcpu *v, const ioreq_t *proto_p) p->df = proto_p->df; p->data = proto_p->data; - prepare_wait_on_xen_event_channel(v->arch.hvm_vcpu.xen_port); + prepare_wait_on_xen_event_channel(p->vp_eport); /* * Following happens /after/ blocking and setting up ioreq contents. * prepare_wait_on_xen_event_channel() is an implicit barrier. */ p->state = STATE_IOREQ_READY; - notify_via_xen_event_channel(v->domain, v->arch.hvm_vcpu.xen_port); + notify_via_xen_event_channel(d, p->vp_eport); return 1; } @@ -4134,21 +4275,6 @@ static int hvmop_flush_tlb_all(void) return 0; } -static int hvm_replace_event_channel(struct vcpu *v, domid_t remote_domid, - int *p_port) -{ - int old_port, new_port; - - new_port = alloc_unbound_xen_event_channel(v, remote_domid, NULL); - if ( new_port < 0 ) - return new_port; - - /* xchg() ensures that only we call free_xen_event_channel(). */ - old_port = xchg(p_port, new_port); - free_xen_event_channel(v, old_port); - return 0; -} - long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) { @@ -4161,7 +4287,6 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) case HVMOP_get_param: { struct xen_hvm_param a; - struct hvm_ioreq_page *iorp; struct domain *d; struct vcpu *v; @@ -4194,19 +4319,12 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) switch ( a.index ) { case HVM_PARAM_IOREQ_PFN: - iorp = &d->arch.hvm_domain.ioreq; - if ( (rc = hvm_set_ioreq_page(d, iorp, a.value)) != 0 ) - break; - spin_lock(&iorp->lock); - if ( iorp->va != NULL ) - /* Initialise evtchn port info if VCPUs already created. */ - for_each_vcpu ( d, v ) - get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port; - spin_unlock(&iorp->lock); + rc = hvm_set_ioreq_server_pfn(d->arch.hvm_domain.ioreq_server, + a.value); break; case HVM_PARAM_BUFIOREQ_PFN: - iorp = &d->arch.hvm_domain.buf_ioreq; - rc = hvm_set_ioreq_page(d, iorp, a.value); + rc = hvm_set_ioreq_server_buf_pfn(d->arch.hvm_domain.ioreq_server, + a.value); break; case HVM_PARAM_CALLBACK_IRQ: hvm_set_callback_via(d, a.value); @@ -4261,31 +4379,8 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) if ( a.value == DOMID_SELF ) a.value = curr_d->domain_id; - rc = 0; - domain_pause(d); /* safe to change per-vcpu xen_port */ - if ( d->vcpu[0] ) - rc = hvm_replace_event_channel(d->vcpu[0], a.value, - (int *)&d->vcpu[0]->domain->arch.hvm_domain.params - [HVM_PARAM_BUFIOREQ_EVTCHN]); - if ( rc ) - { - domain_unpause(d); - break; - } - iorp = &d->arch.hvm_domain.ioreq; - for_each_vcpu ( d, v ) - { - rc = hvm_replace_event_channel(v, a.value, - &v->arch.hvm_vcpu.xen_port); - if ( rc ) - break; - - spin_lock(&iorp->lock); - if ( iorp->va != NULL ) - get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port; - spin_unlock(&iorp->lock); - } - domain_unpause(d); + rc = hvm_set_ioreq_server_domid(d->arch.hvm_domain.ioreq_server, + a.value); break; case HVM_PARAM_ACPI_S_STATE: /* Not reflexive, as we must domain_pause(). */ diff --git a/xen/include/asm-x86/hvm/domain.h b/xen/include/asm-x86/hvm/domain.h index b1e3187..d8dbfab 100644 --- a/xen/include/asm-x86/hvm/domain.h +++ b/xen/include/asm-x86/hvm/domain.h @@ -41,10 +41,16 @@ struct hvm_ioreq_page { void *va; }; -struct hvm_domain { +struct hvm_ioreq_server { + struct domain *domain; + domid_t domid; /* domid of emulator */ struct hvm_ioreq_page ioreq; struct hvm_ioreq_page buf_ioreq; + evtchn_port_t buf_ioreq_evtchn; +}; +struct hvm_domain { + struct hvm_ioreq_server *ioreq_server; struct pl_time pl_time; struct hvm_io_handler *io_handler; @@ -100,3 +106,12 @@ struct hvm_domain { #endif /* __ASM_X86_HVM_DOMAIN_H__ */ +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/include/asm-x86/hvm/vcpu.h b/xen/include/asm-x86/hvm/vcpu.h index 122ab0d..b282bab 100644 --- a/xen/include/asm-x86/hvm/vcpu.h +++ b/xen/include/asm-x86/hvm/vcpu.h @@ -138,7 +138,7 @@ struct hvm_vcpu { spinlock_t tm_lock; struct list_head tm_list; - int xen_port; + evtchn_port_t ioreq_evtchn; bool_t flag_dr_dirty; bool_t debug_state_latch; @@ -186,3 +186,13 @@ struct hvm_vcpu { }; #endif /* __ASM_X86_HVM_VCPU_H__ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ -- 1.7.10.4 _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |