From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478955; cv=none; d=zohomail.com; s=zohoarc; b=dtizCQWbjK9kVMlvnTaDaicYeMTamuY3cN6C/qe5C9Uzi7WPSOiHPZUJ9ZV9xz96cZ7yB7WW0poBEr/aqw+7Iz079R5WppkGHgKBGUCSgM6YcMAA4bua0tKQCgTsPQ/GYSvoSlbpOSnKU+++qCVRLEvnleU7HBeSqtxaLIHj8HQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478955; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=SgPounakxdj3HtZ9qbiGo1zoQqRXX+aIAAD6NC3cqAA=; b=YckgkTLrSjC+ohQxMd4lD9WKh+CE3lFCRbOKbfXhzoUSeP3+JC1IdnQqi97Evhl0eFuZTq5gX1RHBc66+91QMQo9SylyI1SNFGVHOWFoA7UgaPSH+BueLyyj+nOJewzjOJf69L0pxnWgd+vvCqRFfya94xvRWi8q46QXzzV6MQg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596478955301139.37026527867567; Mon, 3 Aug 2020 11:22:35 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f62-0008Gn-DK; Mon, 03 Aug 2020 18:22:02 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f61-0008F4-6d for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:01 +0000 Received: from mail-lf1-x143.google.com (unknown [2a00:1450:4864:20::143]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 306e3785-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:47 +0000 (UTC) Received: by mail-lf1-x143.google.com with SMTP id m15so20303250lfp.7 for ; Mon, 03 Aug 2020 11:21:47 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:44 -0700 (PDT) X-Inumbo-ID: 306e3785-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SgPounakxdj3HtZ9qbiGo1zoQqRXX+aIAAD6NC3cqAA=; b=HhMbLvg3fg4aCR6fsnd1FBCT062tQqJzZiC+5xqsuGA7jnj88Qjy6AEWBsj8P5Vtsx MdHido/oWgwF5dz8pNUChSc6hej7b1B/MRt7BucrkrCekVN7WEzlOLvjTqFGrZjmLoZV Kx6q0/Vpmr34WFdmfcF9wvo4Oxx8e30ZHS3sGByk+NebGL+G0wTfz8SOfPTe8dHOqdW9 XnB5kGkgPS2UZBICfVKKUAu+51Al3JiJKxmwVHtzv27uDb1n5vI5qBRDx8R1+dZG6fkg cF+Q1nrC+OSh/9TugQZ/fTuZ6Fp+CwU7oyQRQ4SYFbw42NCBODf5BXMGkQV+EFnM3cml /qAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SgPounakxdj3HtZ9qbiGo1zoQqRXX+aIAAD6NC3cqAA=; b=PvW4DI9uag7C1QtmiYNhOMui5ywVEOFI8JgMdvAaEViWPObK3EsPXSY7ukeUQ979Ae fxkV2/H8Jn+OF52kwq9dNjUEfwtKNeE2xK0jwNTUrBvrJZ3RcxRwC0OzMOm5ejMcIrEf ErVZOIUi2DuSl6yJogLN33em7wo6W91E74exs2WQ9IP6OhHR+b4t9tbMQaFb5fZD90Fu bk/4zQLwT9vMHBCl/m8u4eNkh5poSQ+YjOi3uRMoKDJkhpAQmVx3NtSSg6MORJhH3K5F Ks6liIfH0AJCbnsqrKkig1OW1BVxu1V+4LJLxN3bTVPXTSdu7gsc0hrFgXuUqg2XdDle +2sg== X-Gm-Message-State: AOAM532/Nh1cAyJxHsR7DYf2JqYrdwU8s71Hk41U6/c45cE/5lwboPxk A9HtNDkAJIYaj/kajShxHEvoCr5RwDE= X-Google-Smtp-Source: ABdhPJwgh7P6XQin4fOMZhcfe6ygtRGL+QwSG0ONkKOqBa4xbUnNX1BC3Ecw0HofIklym10jONBc6A== X-Received: by 2002:a05:6512:523:: with SMTP id o3mr9023231lfc.212.1596478904919; Mon, 03 Aug 2020 11:21:44 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 01/12] hvm/ioreq: Make x86's IOREQ feature common Date: Mon, 3 Aug 2020 21:21:17 +0300 Message-Id: <1596478888-23030-2-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Kevin Tian , Stefano Stabellini , Julien Grall , Jun Nakajima , Wei Liu , Paul Durrant , Andrew Cooper , Ian Jackson , George Dunlap , Tim Deegan , Oleksandr Tyshchenko , Julien Grall , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko As a lot of x86 code can be re-used on Arm later on, this patch splits IOREQ support into common and arch specific parts. This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor. Please note, this is a split/cleanup of Julien's PoC: "Add support for Guest IO forwarding to a device emulator" Signed-off-by: Julien Grall Signed-off-by: Oleksandr Tyshchenko --- xen/arch/x86/Kconfig | 1 + xen/arch/x86/hvm/dm.c | 2 +- xen/arch/x86/hvm/emulate.c | 2 +- xen/arch/x86/hvm/hvm.c | 2 +- xen/arch/x86/hvm/io.c | 2 +- xen/arch/x86/hvm/ioreq.c | 1431 +----------------------------------= ---- xen/arch/x86/hvm/stdvga.c | 2 +- xen/arch/x86/hvm/vmx/realmode.c | 1 + xen/arch/x86/hvm/vmx/vvmx.c | 2 +- xen/arch/x86/mm.c | 2 +- xen/arch/x86/mm/shadow/common.c | 2 +- xen/common/Kconfig | 3 + xen/common/Makefile | 1 + xen/common/hvm/Makefile | 1 + xen/common/hvm/ioreq.c | 1430 +++++++++++++++++++++++++++++++++++= +++ xen/include/asm-x86/hvm/ioreq.h | 45 +- xen/include/asm-x86/hvm/vcpu.h | 7 - xen/include/xen/hvm/ioreq.h | 89 +++ 18 files changed, 1575 insertions(+), 1450 deletions(-) create mode 100644 xen/common/hvm/Makefile create mode 100644 xen/common/hvm/ioreq.c create mode 100644 xen/include/xen/hvm/ioreq.h diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index a636a4b..f5a9f87 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -91,6 +91,7 @@ config PV_LINEAR_PT =20 config HVM def_bool !PV_SHIM_EXCLUSIVE + select IOREQ_SERVER prompt "HVM support" ---help--- Interfaces to support HVM domains. HVM domains require hardware diff --git a/xen/arch/x86/hvm/dm.c b/xen/arch/x86/hvm/dm.c index e3f8451..70adb27 100644 --- a/xen/arch/x86/hvm/dm.c +++ b/xen/arch/x86/hvm/dm.c @@ -16,13 +16,13 @@ =20 #include #include +#include #include #include #include =20 #include #include -#include #include =20 #include diff --git a/xen/arch/x86/hvm/emulate.c b/xen/arch/x86/hvm/emulate.c index 8b4e73a..78993b3 100644 --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -9,6 +9,7 @@ * Keir Fraser */ =20 +#include #include #include #include @@ -20,7 +21,6 @@ #include #include #include -#include #include #include #include diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 5bb4758..c05025d 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -19,6 +19,7 @@ */ =20 #include +#include #include #include #include @@ -64,7 +65,6 @@ #include #include #include -#include #include #include #include diff --git a/xen/arch/x86/hvm/io.c b/xen/arch/x86/hvm/io.c index 724ab44..5d501d1 100644 --- a/xen/arch/x86/hvm/io.c +++ b/xen/arch/x86/hvm/io.c @@ -18,6 +18,7 @@ * this program; If not, see . */ =20 +#include #include #include #include @@ -35,7 +36,6 @@ #include #include #include -#include #include #include #include diff --git a/xen/arch/x86/hvm/ioreq.c b/xen/arch/x86/hvm/ioreq.c index 7240070..dd21e85 100644 --- a/xen/arch/x86/hvm/ioreq.c +++ b/xen/arch/x86/hvm/ioreq.c @@ -17,6 +17,7 @@ */ =20 #include +#include #include #include #include @@ -28,1069 +29,16 @@ #include #include =20 -#include -#include -#include -#include - -#include -#include - -static void set_ioreq_server(struct domain *d, unsigned int id, - struct hvm_ioreq_server *s) -{ - ASSERT(id < MAX_NR_IOREQ_SERVERS); - ASSERT(!s || !d->arch.hvm.ioreq_server.server[id]); - - d->arch.hvm.ioreq_server.server[id] =3D s; -} - -#define GET_IOREQ_SERVER(d, id) \ - (d)->arch.hvm.ioreq_server.server[id] - -static struct hvm_ioreq_server *get_ioreq_server(const struct domain *d, - unsigned int id) -{ - if ( id >=3D MAX_NR_IOREQ_SERVERS ) - return NULL; - - return GET_IOREQ_SERVER(d, id); -} - -/* - * Iterate over all possible ioreq servers. - * - * NOTE: The iteration is backwards such that more recently created - * ioreq servers are favoured in hvm_select_ioreq_server(). - * This is a semantic that previously existed when ioreq servers - * were held in a linked list. - */ -#define FOR_EACH_IOREQ_SERVER(d, id, s) \ - for ( (id) =3D MAX_NR_IOREQ_SERVERS; (id) !=3D 0; ) \ - if ( !(s =3D GET_IOREQ_SERVER(d, --(id))) ) \ - continue; \ - else - -static ioreq_t *get_ioreq(struct hvm_ioreq_server *s, struct vcpu *v) -{ - shared_iopage_t *p =3D s->ioreq.va; - - ASSERT((v =3D=3D current) || !vcpu_runnable(v)); - ASSERT(p !=3D NULL); - - return &p->vcpu_ioreq[v->vcpu_id]; -} - -bool hvm_io_pending(struct vcpu *v) -{ - struct domain *d =3D v->domain; - struct hvm_ioreq_server *s; - unsigned int id; - - FOR_EACH_IOREQ_SERVER(d, id, s) - { - struct hvm_ioreq_vcpu *sv; - - list_for_each_entry ( sv, - &s->ioreq_vcpu_list, - list_entry ) - { - if ( sv->vcpu =3D=3D v && sv->pending ) - return true; - } - } - - return false; -} - -static void hvm_io_assist(struct hvm_ioreq_vcpu *sv, uint64_t data) -{ - struct vcpu *v =3D sv->vcpu; - ioreq_t *ioreq =3D &v->arch.hvm.hvm_io.io_req; - - if ( hvm_ioreq_needs_completion(ioreq) ) - ioreq->data =3D data; - - sv->pending =3D false; -} - -static bool hvm_wait_for_io(struct hvm_ioreq_vcpu *sv, ioreq_t *p) -{ - unsigned int prev_state =3D STATE_IOREQ_NONE; - - while ( sv->pending ) - { - unsigned int state =3D p->state; - - smp_rmb(); - - recheck: - if ( unlikely(state =3D=3D STATE_IOREQ_NONE) ) - { - /* - * The only reason we should see this case is when an - * emulator is dying and it races with an I/O being - * requested. - */ - hvm_io_assist(sv, ~0ul); - break; - } - - if ( unlikely(state < prev_state) ) - { - gdprintk(XENLOG_ERR, "Weird HVM ioreq state transition %u -> %= u\n", - prev_state, state); - sv->pending =3D false; - domain_crash(sv->vcpu->domain); - return false; /* bail */ - } - - switch ( prev_state =3D state ) - { - case STATE_IORESP_READY: /* IORESP_READY -> NONE */ - p->state =3D STATE_IOREQ_NONE; - hvm_io_assist(sv, p->data); - break; - case STATE_IOREQ_READY: /* IOREQ_{READY,INPROCESS} -> IORESP_READ= Y */ - case STATE_IOREQ_INPROCESS: - wait_on_xen_event_channel(sv->ioreq_evtchn, - ({ state =3D p->state; - smp_rmb(); - state !=3D prev_state; })); - goto recheck; - default: - gdprintk(XENLOG_ERR, "Weird HVM iorequest state %u\n", state); - sv->pending =3D false; - domain_crash(sv->vcpu->domain); - return false; /* bail */ - } - } - - return true; -} - -bool handle_hvm_io_completion(struct vcpu *v) -{ - struct domain *d =3D v->domain; - struct hvm_vcpu_io *vio =3D &v->arch.hvm.hvm_io; - struct hvm_ioreq_server *s; - enum hvm_io_completion io_completion; - unsigned int id; - - if ( has_vpci(d) && vpci_process_pending(v) ) - { - raise_softirq(SCHEDULE_SOFTIRQ); - return false; - } - - FOR_EACH_IOREQ_SERVER(d, id, s) - { - struct hvm_ioreq_vcpu *sv; - - list_for_each_entry ( sv, - &s->ioreq_vcpu_list, - list_entry ) - { - if ( sv->vcpu =3D=3D v && sv->pending ) - { - if ( !hvm_wait_for_io(sv, get_ioreq(s, v)) ) - return false; - - break; - } - } - } - - vio->io_req.state =3D hvm_ioreq_needs_completion(&vio->io_req) ? - STATE_IORESP_READY : STATE_IOREQ_NONE; - - msix_write_completion(v); - vcpu_end_shutdown_deferral(v); - - io_completion =3D vio->io_completion; - vio->io_completion =3D HVMIO_no_completion; - - switch ( io_completion ) - { - case HVMIO_no_completion: - break; - - case HVMIO_mmio_completion: - return handle_mmio(); - - case HVMIO_pio_completion: - return handle_pio(vio->io_req.addr, vio->io_req.size, - vio->io_req.dir); - - case HVMIO_realmode_completion: - { - struct hvm_emulate_ctxt ctxt; - - hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs()); - vmx_realmode_emulate_one(&ctxt); - hvm_emulate_writeback(&ctxt); - - break; - } - default: - ASSERT_UNREACHABLE(); - break; - } - - return true; -} - -static gfn_t hvm_alloc_legacy_ioreq_gfn(struct hvm_ioreq_server *s) -{ - struct domain *d =3D s->target; - unsigned int i; - - BUILD_BUG_ON(HVM_PARAM_BUFIOREQ_PFN !=3D HVM_PARAM_IOREQ_PFN + 1); - - for ( i =3D HVM_PARAM_IOREQ_PFN; i <=3D HVM_PARAM_BUFIOREQ_PFN; i++ ) - { - if ( !test_and_clear_bit(i, &d->arch.hvm.ioreq_gfn.legacy_mask) ) - return _gfn(d->arch.hvm.params[i]); - } - - return INVALID_GFN; -} - -static gfn_t hvm_alloc_ioreq_gfn(struct hvm_ioreq_server *s) -{ - struct domain *d =3D s->target; - unsigned int i; - - for ( i =3D 0; i < sizeof(d->arch.hvm.ioreq_gfn.mask) * 8; i++ ) - { - if ( test_and_clear_bit(i, &d->arch.hvm.ioreq_gfn.mask) ) - return _gfn(d->arch.hvm.ioreq_gfn.base + i); - } - - /* - * If we are out of 'normal' GFNs then we may still have a 'legacy' - * GFN available. - */ - return hvm_alloc_legacy_ioreq_gfn(s); -} - -static bool hvm_free_legacy_ioreq_gfn(struct hvm_ioreq_server *s, - gfn_t gfn) -{ - struct domain *d =3D s->target; - unsigned int i; - - for ( i =3D HVM_PARAM_IOREQ_PFN; i <=3D HVM_PARAM_BUFIOREQ_PFN; i++ ) - { - if ( gfn_eq(gfn, _gfn(d->arch.hvm.params[i])) ) - break; - } - if ( i > HVM_PARAM_BUFIOREQ_PFN ) - return false; - - set_bit(i, &d->arch.hvm.ioreq_gfn.legacy_mask); - return true; -} - -static void hvm_free_ioreq_gfn(struct hvm_ioreq_server *s, gfn_t gfn) -{ - struct domain *d =3D s->target; - unsigned int i =3D gfn_x(gfn) - d->arch.hvm.ioreq_gfn.base; - - ASSERT(!gfn_eq(gfn, INVALID_GFN)); - - if ( !hvm_free_legacy_ioreq_gfn(s, gfn) ) - { - ASSERT(i < sizeof(d->arch.hvm.ioreq_gfn.mask) * 8); - set_bit(i, &d->arch.hvm.ioreq_gfn.mask); - } -} - -static void hvm_unmap_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) -{ - struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; - - if ( gfn_eq(iorp->gfn, INVALID_GFN) ) - return; - - destroy_ring_for_helper(&iorp->va, iorp->page); - iorp->page =3D NULL; - - hvm_free_ioreq_gfn(s, iorp->gfn); - iorp->gfn =3D INVALID_GFN; -} - -static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) -{ - struct domain *d =3D s->target; - struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; - int rc; - - if ( iorp->page ) - { - /* - * If a page has already been allocated (which will happen on - * demand if hvm_get_ioreq_server_frame() is called), then - * mapping a guest frame is not permitted. - */ - if ( gfn_eq(iorp->gfn, INVALID_GFN) ) - return -EPERM; - - return 0; - } - - if ( d->is_dying ) - return -EINVAL; - - iorp->gfn =3D hvm_alloc_ioreq_gfn(s); - - if ( gfn_eq(iorp->gfn, INVALID_GFN) ) - return -ENOMEM; - - rc =3D prepare_ring_for_helper(d, gfn_x(iorp->gfn), &iorp->page, - &iorp->va); - - if ( rc ) - hvm_unmap_ioreq_gfn(s, buf); - - return rc; -} - -static int hvm_alloc_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) -{ - struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; - struct page_info *page; - - if ( iorp->page ) - { - /* - * If a guest frame has already been mapped (which may happen - * on demand if hvm_get_ioreq_server_info() is called), then - * allocating a page is not permitted. - */ - if ( !gfn_eq(iorp->gfn, INVALID_GFN) ) - return -EPERM; - - return 0; - } - - page =3D alloc_domheap_page(s->target, MEMF_no_refcount); - - if ( !page ) - return -ENOMEM; - - if ( !get_page_and_type(page, s->target, PGT_writable_page) ) - { - /* - * The domain can't possibly know about this page yet, so failure - * here is a clear indication of something fishy going on. - */ - domain_crash(s->emulator); - return -ENODATA; - } - - iorp->va =3D __map_domain_page_global(page); - if ( !iorp->va ) - goto fail; - - iorp->page =3D page; - clear_page(iorp->va); - return 0; - - fail: - put_page_alloc_ref(page); - put_page_and_type(page); - - return -ENOMEM; -} - -static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) -{ - struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; - struct page_info *page =3D iorp->page; - - if ( !page ) - return; - - iorp->page =3D NULL; - - unmap_domain_page_global(iorp->va); - iorp->va =3D NULL; - - put_page_alloc_ref(page); - put_page_and_type(page); -} - -bool is_ioreq_server_page(struct domain *d, const struct page_info *page) -{ - const struct hvm_ioreq_server *s; - unsigned int id; - bool found =3D false; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - FOR_EACH_IOREQ_SERVER(d, id, s) - { - if ( (s->ioreq.page =3D=3D page) || (s->bufioreq.page =3D=3D page)= ) - { - found =3D true; - break; - } - } - - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - - return found; -} - -static void hvm_remove_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) - -{ - struct domain *d =3D s->target; - struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; - - if ( gfn_eq(iorp->gfn, INVALID_GFN) ) - return; - - if ( guest_physmap_remove_page(d, iorp->gfn, - page_to_mfn(iorp->page), 0) ) - domain_crash(d); - clear_page(iorp->va); -} - -static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) -{ - struct domain *d =3D s->target; - struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; - int rc; - - if ( gfn_eq(iorp->gfn, INVALID_GFN) ) - return 0; - - clear_page(iorp->va); - - rc =3D guest_physmap_add_page(d, iorp->gfn, - page_to_mfn(iorp->page), 0); - if ( rc =3D=3D 0 ) - paging_mark_pfn_dirty(d, _pfn(gfn_x(iorp->gfn))); - - return rc; -} - -static void hvm_update_ioreq_evtchn(struct hvm_ioreq_server *s, - struct hvm_ioreq_vcpu *sv) -{ - ASSERT(spin_is_locked(&s->lock)); - - if ( s->ioreq.va !=3D NULL ) - { - ioreq_t *p =3D get_ioreq(s, sv->vcpu); - - p->vp_eport =3D sv->ioreq_evtchn; - } -} - -#define HANDLE_BUFIOREQ(s) \ - ((s)->bufioreq_handling !=3D HVM_IOREQSRV_BUFIOREQ_OFF) - -static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s, - struct vcpu *v) -{ - struct hvm_ioreq_vcpu *sv; - int rc; - - sv =3D xzalloc(struct hvm_ioreq_vcpu); - - rc =3D -ENOMEM; - if ( !sv ) - goto fail1; - - spin_lock(&s->lock); - - rc =3D alloc_unbound_xen_event_channel(v->domain, v->vcpu_id, - s->emulator->domain_id, NULL); - if ( rc < 0 ) - goto fail2; - - sv->ioreq_evtchn =3D rc; - - if ( v->vcpu_id =3D=3D 0 && HANDLE_BUFIOREQ(s) ) - { - rc =3D alloc_unbound_xen_event_channel(v->domain, 0, - s->emulator->domain_id, NULL); - if ( rc < 0 ) - goto fail3; - - s->bufioreq_evtchn =3D rc; - } - - sv->vcpu =3D v; - - list_add(&sv->list_entry, &s->ioreq_vcpu_list); - - if ( s->enabled ) - hvm_update_ioreq_evtchn(s, sv); - - spin_unlock(&s->lock); - return 0; - - fail3: - free_xen_event_channel(v->domain, sv->ioreq_evtchn); - - fail2: - spin_unlock(&s->lock); - xfree(sv); - - fail1: - return rc; -} - -static void hvm_ioreq_server_remove_vcpu(struct hvm_ioreq_server *s, - struct vcpu *v) -{ - struct hvm_ioreq_vcpu *sv; - - spin_lock(&s->lock); - - list_for_each_entry ( sv, - &s->ioreq_vcpu_list, - list_entry ) - { - if ( sv->vcpu !=3D v ) - continue; - - list_del(&sv->list_entry); - - if ( v->vcpu_id =3D=3D 0 && HANDLE_BUFIOREQ(s) ) - free_xen_event_channel(v->domain, s->bufioreq_evtchn); - - free_xen_event_channel(v->domain, sv->ioreq_evtchn); - - xfree(sv); - break; - } - - spin_unlock(&s->lock); -} - -static void hvm_ioreq_server_remove_all_vcpus(struct hvm_ioreq_server *s) -{ - struct hvm_ioreq_vcpu *sv, *next; - - spin_lock(&s->lock); - - list_for_each_entry_safe ( sv, - next, - &s->ioreq_vcpu_list, - list_entry ) - { - struct vcpu *v =3D sv->vcpu; - - list_del(&sv->list_entry); - - if ( v->vcpu_id =3D=3D 0 && HANDLE_BUFIOREQ(s) ) - free_xen_event_channel(v->domain, s->bufioreq_evtchn); - - free_xen_event_channel(v->domain, sv->ioreq_evtchn); - - xfree(sv); - } - - spin_unlock(&s->lock); -} - -static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s) -{ - int rc; - - rc =3D hvm_map_ioreq_gfn(s, false); - - if ( !rc && HANDLE_BUFIOREQ(s) ) - rc =3D hvm_map_ioreq_gfn(s, true); - - if ( rc ) - hvm_unmap_ioreq_gfn(s, false); - - return rc; -} - -static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s) -{ - hvm_unmap_ioreq_gfn(s, true); - hvm_unmap_ioreq_gfn(s, false); -} - -static int hvm_ioreq_server_alloc_pages(struct hvm_ioreq_server *s) -{ - int rc; - - rc =3D hvm_alloc_ioreq_mfn(s, false); - - if ( !rc && (s->bufioreq_handling !=3D HVM_IOREQSRV_BUFIOREQ_OFF) ) - rc =3D hvm_alloc_ioreq_mfn(s, true); - - if ( rc ) - hvm_free_ioreq_mfn(s, false); - - return rc; -} - -static void hvm_ioreq_server_free_pages(struct hvm_ioreq_server *s) -{ - hvm_free_ioreq_mfn(s, true); - hvm_free_ioreq_mfn(s, false); -} - -static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s) -{ - unsigned int i; - - for ( i =3D 0; i < NR_IO_RANGE_TYPES; i++ ) - rangeset_destroy(s->range[i]); -} - -static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, - ioservid_t id) -{ - unsigned int i; - int rc; - - for ( i =3D 0; i < NR_IO_RANGE_TYPES; i++ ) - { - char *name; - - rc =3D asprintf(&name, "ioreq_server %d %s", id, - (i =3D=3D XEN_DMOP_IO_RANGE_PORT) ? "port" : - (i =3D=3D XEN_DMOP_IO_RANGE_MEMORY) ? "memory" : - (i =3D=3D XEN_DMOP_IO_RANGE_PCI) ? "pci" : - ""); - if ( rc ) - goto fail; - - s->range[i] =3D rangeset_new(s->target, name, - RANGESETF_prettyprint_hex); - - xfree(name); - - rc =3D -ENOMEM; - if ( !s->range[i] ) - goto fail; - - rangeset_limit(s->range[i], MAX_NR_IO_RANGES); - } - - return 0; - - fail: - hvm_ioreq_server_free_rangesets(s); - - return rc; -} - -static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s) -{ - struct hvm_ioreq_vcpu *sv; - - spin_lock(&s->lock); - - if ( s->enabled ) - goto done; - - hvm_remove_ioreq_gfn(s, false); - hvm_remove_ioreq_gfn(s, true); - - s->enabled =3D true; - - list_for_each_entry ( sv, - &s->ioreq_vcpu_list, - list_entry ) - hvm_update_ioreq_evtchn(s, sv); - - done: - spin_unlock(&s->lock); -} - -static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s) -{ - spin_lock(&s->lock); - - if ( !s->enabled ) - goto done; - - hvm_add_ioreq_gfn(s, true); - hvm_add_ioreq_gfn(s, false); - - s->enabled =3D false; - - done: - spin_unlock(&s->lock); -} - -static int hvm_ioreq_server_init(struct hvm_ioreq_server *s, - struct domain *d, int bufioreq_handling, - ioservid_t id) -{ - struct domain *currd =3D current->domain; - struct vcpu *v; - int rc; - - s->target =3D d; - - get_knownalive_domain(currd); - s->emulator =3D currd; - - spin_lock_init(&s->lock); - INIT_LIST_HEAD(&s->ioreq_vcpu_list); - spin_lock_init(&s->bufioreq_lock); - - s->ioreq.gfn =3D INVALID_GFN; - s->bufioreq.gfn =3D INVALID_GFN; - - rc =3D hvm_ioreq_server_alloc_rangesets(s, id); - if ( rc ) - return rc; - - s->bufioreq_handling =3D bufioreq_handling; - - for_each_vcpu ( d, v ) - { - rc =3D hvm_ioreq_server_add_vcpu(s, v); - if ( rc ) - goto fail_add; - } - - return 0; - - fail_add: - hvm_ioreq_server_remove_all_vcpus(s); - hvm_ioreq_server_unmap_pages(s); - - hvm_ioreq_server_free_rangesets(s); - - put_domain(s->emulator); - return rc; -} - -static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s) -{ - ASSERT(!s->enabled); - hvm_ioreq_server_remove_all_vcpus(s); - - /* - * NOTE: It is safe to call both hvm_ioreq_server_unmap_pages() and - * hvm_ioreq_server_free_pages() in that order. - * This is because the former will do nothing if the pages - * are not mapped, leaving the page to be freed by the latter. - * However if the pages are mapped then the former will set - * the page_info pointer to NULL, meaning the latter will do - * nothing. - */ - hvm_ioreq_server_unmap_pages(s); - hvm_ioreq_server_free_pages(s); - - hvm_ioreq_server_free_rangesets(s); - - put_domain(s->emulator); -} - -int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling, - ioservid_t *id) -{ - struct hvm_ioreq_server *s; - unsigned int i; - int rc; - - if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC ) - return -EINVAL; - - s =3D xzalloc(struct hvm_ioreq_server); - if ( !s ) - return -ENOMEM; - - domain_pause(d); - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - for ( i =3D 0; i < MAX_NR_IOREQ_SERVERS; i++ ) - { - if ( !GET_IOREQ_SERVER(d, i) ) - break; - } - - rc =3D -ENOSPC; - if ( i >=3D MAX_NR_IOREQ_SERVERS ) - goto fail; - - /* - * It is safe to call set_ioreq_server() prior to - * hvm_ioreq_server_init() since the target domain is paused. - */ - set_ioreq_server(d, i, s); - - rc =3D hvm_ioreq_server_init(s, d, bufioreq_handling, i); - if ( rc ) - { - set_ioreq_server(d, i, NULL); - goto fail; - } - - if ( id ) - *id =3D i; - - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - domain_unpause(d); - - return 0; - - fail: - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - domain_unpause(d); - - xfree(s); - return rc; -} - -int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) -{ - struct hvm_ioreq_server *s; - int rc; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - s =3D get_ioreq_server(d, id); - - rc =3D -ENOENT; - if ( !s ) - goto out; - - rc =3D -EPERM; - if ( s->emulator !=3D current->domain ) - goto out; - - domain_pause(d); - - p2m_set_ioreq_server(d, 0, s); - - hvm_ioreq_server_disable(s); - - /* - * It is safe to call hvm_ioreq_server_deinit() prior to - * set_ioreq_server() since the target domain is paused. - */ - hvm_ioreq_server_deinit(s); - set_ioreq_server(d, id, NULL); - - domain_unpause(d); - - xfree(s); - - rc =3D 0; - - out: - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - - return rc; -} - -int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, - unsigned long *ioreq_gfn, - unsigned long *bufioreq_gfn, - evtchn_port_t *bufioreq_port) -{ - struct hvm_ioreq_server *s; - int rc; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - s =3D get_ioreq_server(d, id); - - rc =3D -ENOENT; - if ( !s ) - goto out; - - rc =3D -EPERM; - if ( s->emulator !=3D current->domain ) - goto out; - - if ( ioreq_gfn || bufioreq_gfn ) - { - rc =3D hvm_ioreq_server_map_pages(s); - if ( rc ) - goto out; - } - - if ( ioreq_gfn ) - *ioreq_gfn =3D gfn_x(s->ioreq.gfn); - - if ( HANDLE_BUFIOREQ(s) ) - { - if ( bufioreq_gfn ) - *bufioreq_gfn =3D gfn_x(s->bufioreq.gfn); - - if ( bufioreq_port ) - *bufioreq_port =3D s->bufioreq_evtchn; - } - - rc =3D 0; - - out: - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - - return rc; -} - -int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, - unsigned long idx, mfn_t *mfn) -{ - struct hvm_ioreq_server *s; - int rc; - - ASSERT(is_hvm_domain(d)); - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - s =3D get_ioreq_server(d, id); - - rc =3D -ENOENT; - if ( !s ) - goto out; - - rc =3D -EPERM; - if ( s->emulator !=3D current->domain ) - goto out; - - rc =3D hvm_ioreq_server_alloc_pages(s); - if ( rc ) - goto out; - - switch ( idx ) - { - case XENMEM_resource_ioreq_server_frame_bufioreq: - rc =3D -ENOENT; - if ( !HANDLE_BUFIOREQ(s) ) - goto out; - - *mfn =3D page_to_mfn(s->bufioreq.page); - rc =3D 0; - break; - - case XENMEM_resource_ioreq_server_frame_ioreq(0): - *mfn =3D page_to_mfn(s->ioreq.page); - rc =3D 0; - break; - - default: - rc =3D -EINVAL; - break; - } - - out: - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - - return rc; -} - -int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, - uint32_t type, uint64_t start, - uint64_t end) -{ - struct hvm_ioreq_server *s; - struct rangeset *r; - int rc; - - if ( start > end ) - return -EINVAL; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - s =3D get_ioreq_server(d, id); - - rc =3D -ENOENT; - if ( !s ) - goto out; - - rc =3D -EPERM; - if ( s->emulator !=3D current->domain ) - goto out; - - switch ( type ) - { - case XEN_DMOP_IO_RANGE_PORT: - case XEN_DMOP_IO_RANGE_MEMORY: - case XEN_DMOP_IO_RANGE_PCI: - r =3D s->range[type]; - break; - - default: - r =3D NULL; - break; - } - - rc =3D -EINVAL; - if ( !r ) - goto out; - - rc =3D -EEXIST; - if ( rangeset_overlaps_range(r, start, end) ) - goto out; - - rc =3D rangeset_add_range(r, start, end); - - out: - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - - return rc; -} +#include +#include =20 -int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, - uint32_t type, uint64_t start, - uint64_t end) +void handle_realmode_completion(void) { - struct hvm_ioreq_server *s; - struct rangeset *r; - int rc; - - if ( start > end ) - return -EINVAL; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - s =3D get_ioreq_server(d, id); - - rc =3D -ENOENT; - if ( !s ) - goto out; - - rc =3D -EPERM; - if ( s->emulator !=3D current->domain ) - goto out; - - switch ( type ) - { - case XEN_DMOP_IO_RANGE_PORT: - case XEN_DMOP_IO_RANGE_MEMORY: - case XEN_DMOP_IO_RANGE_PCI: - r =3D s->range[type]; - break; - - default: - r =3D NULL; - break; - } - - rc =3D -EINVAL; - if ( !r ) - goto out; - - rc =3D -ENOENT; - if ( !rangeset_contains_range(r, start, end) ) - goto out; - - rc =3D rangeset_remove_range(r, start, end); - - out: - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + struct hvm_emulate_ctxt ctxt; =20 - return rc; + hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs()); + vmx_realmode_emulate_one(&ctxt); + hvm_emulate_writeback(&ctxt); } =20 /* @@ -1141,130 +89,12 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *= d, ioservid_t id, return rc; } =20 -int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, - bool enabled) -{ - struct hvm_ioreq_server *s; - int rc; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - s =3D get_ioreq_server(d, id); - - rc =3D -ENOENT; - if ( !s ) - goto out; - - rc =3D -EPERM; - if ( s->emulator !=3D current->domain ) - goto out; - - domain_pause(d); - - if ( enabled ) - hvm_ioreq_server_enable(s); - else - hvm_ioreq_server_disable(s); - - domain_unpause(d); - - rc =3D 0; - - out: - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - return rc; -} - -int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v) -{ - struct hvm_ioreq_server *s; - unsigned int id; - int rc; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - FOR_EACH_IOREQ_SERVER(d, id, s) - { - rc =3D hvm_ioreq_server_add_vcpu(s, v); - if ( rc ) - goto fail; - } - - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - - return 0; - - fail: - while ( ++id !=3D MAX_NR_IOREQ_SERVERS ) - { - s =3D GET_IOREQ_SERVER(d, id); - - if ( !s ) - continue; - - hvm_ioreq_server_remove_vcpu(s, v); - } - - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); - - return rc; -} - -void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v) -{ - struct hvm_ioreq_server *s; - unsigned int id; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - FOR_EACH_IOREQ_SERVER(d, id, s) - hvm_ioreq_server_remove_vcpu(s, v); - - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); -} - -void hvm_destroy_all_ioreq_servers(struct domain *d) +void hvm_get_ioreq_server_range_type(struct domain *d, + ioreq_t *p, + uint8_t *type, + uint64_t *addr) { - struct hvm_ioreq_server *s; - unsigned int id; - - if ( !relocate_portio_handler(d, 0xcf8, 0xcf8, 4) ) - return; - - spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); - - /* No need to domain_pause() as the domain is being torn down */ - - FOR_EACH_IOREQ_SERVER(d, id, s) - { - hvm_ioreq_server_disable(s); - - /* - * It is safe to call hvm_ioreq_server_deinit() prior to - * set_ioreq_server() since the target domain is being destroyed. - */ - hvm_ioreq_server_deinit(s); - set_ioreq_server(d, id, NULL); - - xfree(s); - } - - spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); -} - -struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, - ioreq_t *p) -{ - struct hvm_ioreq_server *s; - uint32_t cf8; - uint8_t type; - uint64_t addr; - unsigned int id; - - if ( p->type !=3D IOREQ_TYPE_COPY && p->type !=3D IOREQ_TYPE_PIO ) - return NULL; - - cf8 =3D d->arch.hvm.pci_cf8; + uint32_t cf8 =3D d->arch.hvm.pci_cf8; =20 if ( p->type =3D=3D IOREQ_TYPE_PIO && (p->addr & ~3) =3D=3D 0xcfc && @@ -1277,8 +107,8 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struc= t domain *d, reg =3D hvm_pci_decode_addr(cf8, p->addr, &sbdf); =20 /* PCI config data cycle */ - type =3D XEN_DMOP_IO_RANGE_PCI; - addr =3D ((uint64_t)sbdf.sbdf << 32) | reg; + *type =3D XEN_DMOP_IO_RANGE_PCI; + *addr =3D ((uint64_t)sbdf.sbdf << 32) | reg; /* AMD extended configuration space access? */ if ( CF8_ADDR_HI(cf8) && d->arch.cpuid->x86_vendor =3D=3D X86_VENDOR_AMD && @@ -1290,230 +120,15 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(st= ruct domain *d, =20 if ( !rdmsr_safe(MSR_AMD64_NB_CFG, msr_val) && (msr_val & (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT)) ) - addr |=3D CF8_ADDR_HI(cf8); + *addr |=3D CF8_ADDR_HI(cf8); } } else { - type =3D (p->type =3D=3D IOREQ_TYPE_PIO) ? - XEN_DMOP_IO_RANGE_PORT : XEN_DMOP_IO_RANGE_MEMORY; - addr =3D p->addr; - } - - FOR_EACH_IOREQ_SERVER(d, id, s) - { - struct rangeset *r; - - if ( !s->enabled ) - continue; - - r =3D s->range[type]; - - switch ( type ) - { - unsigned long start, end; - - case XEN_DMOP_IO_RANGE_PORT: - start =3D addr; - end =3D start + p->size - 1; - if ( rangeset_contains_range(r, start, end) ) - return s; - - break; - - case XEN_DMOP_IO_RANGE_MEMORY: - start =3D hvm_mmio_first_byte(p); - end =3D hvm_mmio_last_byte(p); - - if ( rangeset_contains_range(r, start, end) ) - return s; - - break; - - case XEN_DMOP_IO_RANGE_PCI: - if ( rangeset_contains_singleton(r, addr >> 32) ) - { - p->type =3D IOREQ_TYPE_PCI_CONFIG; - p->addr =3D addr; - return s; - } - - break; - } - } - - return NULL; -} - -static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p) -{ - struct domain *d =3D current->domain; - struct hvm_ioreq_page *iorp; - buffered_iopage_t *pg; - buf_ioreq_t bp =3D { .data =3D p->data, - .addr =3D p->addr, - .type =3D p->type, - .dir =3D p->dir }; - /* Timeoffset sends 64b data, but no address. Use two consecutive slot= s. */ - int qw =3D 0; - - /* Ensure buffered_iopage fits in a page */ - BUILD_BUG_ON(sizeof(buffered_iopage_t) > PAGE_SIZE); - - iorp =3D &s->bufioreq; - pg =3D iorp->va; - - if ( !pg ) - return X86EMUL_UNHANDLEABLE; - - /* - * Return 0 for the cases we can't deal with: - * - 'addr' is only a 20-bit field, so we cannot address beyond 1MB - * - we cannot buffer accesses to guest memory buffers, as the guest - * may expect the memory buffer to be synchronously accessed - * - the count field is usually used with data_is_ptr and since we do= n't - * support data_is_ptr we do not waste space for the count field ei= ther - */ - if ( (p->addr > 0xffffful) || p->data_is_ptr || (p->count !=3D 1) ) - return 0; - - switch ( p->size ) - { - case 1: - bp.size =3D 0; - break; - case 2: - bp.size =3D 1; - break; - case 4: - bp.size =3D 2; - break; - case 8: - bp.size =3D 3; - qw =3D 1; - break; - default: - gdprintk(XENLOG_WARNING, "unexpected ioreq size: %u\n", p->size); - return X86EMUL_UNHANDLEABLE; - } - - spin_lock(&s->bufioreq_lock); - - if ( (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=3D - (IOREQ_BUFFER_SLOT_NUM - qw) ) - { - /* The queue is full: send the iopacket through the normal path. */ - spin_unlock(&s->bufioreq_lock); - return X86EMUL_UNHANDLEABLE; - } - - pg->buf_ioreq[pg->ptrs.write_pointer % IOREQ_BUFFER_SLOT_NUM] =3D bp; - - if ( qw ) - { - bp.data =3D p->data >> 32; - pg->buf_ioreq[(pg->ptrs.write_pointer+1) % IOREQ_BUFFER_SLOT_NUM] = =3D bp; - } - - /* Make the ioreq_t visible /before/ write_pointer. */ - smp_wmb(); - pg->ptrs.write_pointer +=3D qw ? 2 : 1; - - /* Canonicalize read/write pointers to prevent their overflow. */ - while ( (s->bufioreq_handling =3D=3D HVM_IOREQSRV_BUFIOREQ_ATOMIC) && - qw++ < IOREQ_BUFFER_SLOT_NUM && - pg->ptrs.read_pointer >=3D IOREQ_BUFFER_SLOT_NUM ) - { - union bufioreq_pointers old =3D pg->ptrs, new; - unsigned int n =3D old.read_pointer / IOREQ_BUFFER_SLOT_NUM; - - new.read_pointer =3D old.read_pointer - n * IOREQ_BUFFER_SLOT_NUM; - new.write_pointer =3D old.write_pointer - n * IOREQ_BUFFER_SLOT_NU= M; - cmpxchg(&pg->ptrs.full, old.full, new.full); - } - - notify_via_xen_event_channel(d, s->bufioreq_evtchn); - spin_unlock(&s->bufioreq_lock); - - return X86EMUL_OKAY; -} - -int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p, - bool buffered) -{ - struct vcpu *curr =3D current; - struct domain *d =3D curr->domain; - struct hvm_ioreq_vcpu *sv; - - ASSERT(s); - - if ( buffered ) - return hvm_send_buffered_ioreq(s, proto_p); - - if ( unlikely(!vcpu_start_shutdown_deferral(curr)) ) - return X86EMUL_RETRY; - - list_for_each_entry ( sv, - &s->ioreq_vcpu_list, - list_entry ) - { - if ( sv->vcpu =3D=3D curr ) - { - evtchn_port_t port =3D sv->ioreq_evtchn; - ioreq_t *p =3D get_ioreq(s, curr); - - if ( unlikely(p->state !=3D STATE_IOREQ_NONE) ) - { - gprintk(XENLOG_ERR, "device model set bad IO state %d\n", - p->state); - break; - } - - if ( unlikely(p->vp_eport !=3D port) ) - { - gprintk(XENLOG_ERR, "device model set bad event channel %d= \n", - p->vp_eport); - break; - } - - proto_p->state =3D STATE_IOREQ_NONE; - proto_p->vp_eport =3D port; - *p =3D *proto_p; - - prepare_wait_on_xen_event_channel(port); - - /* - * Following happens /after/ blocking and setting up ioreq - * contents. prepare_wait_on_xen_event_channel() is an implicit - * barrier. - */ - p->state =3D STATE_IOREQ_READY; - notify_via_xen_event_channel(d, port); - - sv->pending =3D true; - return X86EMUL_RETRY; - } - } - - return X86EMUL_UNHANDLEABLE; -} - -unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered) -{ - struct domain *d =3D current->domain; - struct hvm_ioreq_server *s; - unsigned int id, failed =3D 0; - - FOR_EACH_IOREQ_SERVER(d, id, s) - { - if ( !s->enabled ) - continue; - - if ( hvm_send_ioreq(s, p, buffered) =3D=3D X86EMUL_UNHANDLEABLE ) - failed++; + *type =3D (p->type =3D=3D IOREQ_TYPE_PIO) ? + XEN_DMOP_IO_RANGE_PORT : XEN_DMOP_IO_RANGE_MEMORY; + *addr =3D p->addr; } - - return failed; } =20 static int hvm_access_cf8( @@ -1528,13 +143,19 @@ static int hvm_access_cf8( return X86EMUL_UNHANDLEABLE; } =20 -void hvm_ioreq_init(struct domain *d) +void arch_hvm_ioreq_init(struct domain *d) { spin_lock_init(&d->arch.hvm.ioreq_server.lock); =20 register_portio_handler(d, 0xcf8, 4, hvm_access_cf8); } =20 +void arch_hvm_ioreq_destroy(struct domain *d) +{ + if ( !relocate_portio_handler(d, 0xcf8, 0xcf8, 4) ) + return; +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/hvm/stdvga.c b/xen/arch/x86/hvm/stdvga.c index e267513..ab6d315 100644 --- a/xen/arch/x86/hvm/stdvga.c +++ b/xen/arch/x86/hvm/stdvga.c @@ -27,10 +27,10 @@ * can have side effects. */ =20 +#include #include #include #include -#include #include #include #include diff --git a/xen/arch/x86/hvm/vmx/realmode.c b/xen/arch/x86/hvm/vmx/realmod= e.c index bdbd9cb..b804262 100644 --- a/xen/arch/x86/hvm/vmx/realmode.c +++ b/xen/arch/x86/hvm/vmx/realmode.c @@ -9,6 +9,7 @@ * Keir Fraser */ =20 +#include #include #include #include diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index 7dfff6c..acfeb1c 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -18,11 +18,11 @@ * this program; If not, see . * */ +#include =20 #include #include #include -#include #include #include #include diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 82bc676..2b06e15 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -99,6 +99,7 @@ * doing the final put_page(), and remove it from the iommu if so. */ =20 +#include #include #include #include @@ -141,7 +142,6 @@ #include #include #include -#include =20 #include #include diff --git a/xen/arch/x86/mm/shadow/common.c b/xen/arch/x86/mm/shadow/commo= n.c index 7737773..c84cbb2 100644 --- a/xen/arch/x86/mm/shadow/common.c +++ b/xen/arch/x86/mm/shadow/common.c @@ -20,6 +20,7 @@ * along with this program; If not, see . */ =20 +#include #include #include #include @@ -34,7 +35,6 @@ #include #include #include -#include #include #include "private.h" =20 diff --git a/xen/common/Kconfig b/xen/common/Kconfig index 15e3b79..fb6fb51 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -139,6 +139,9 @@ config HYPFS_CONFIG Disable this option in case you want to spare some memory or you want to hide the .config contents from dom0. =20 +config IOREQ_SERVER + bool + config KEXEC bool "kexec support" default y diff --git a/xen/common/Makefile b/xen/common/Makefile index 06881d0..f6fc3f8 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -70,6 +70,7 @@ extra-y :=3D symbols-dummy.o =20 obj-$(CONFIG_COVERAGE) +=3D coverage/ obj-y +=3D sched/ +obj-$(CONFIG_IOREQ_SERVER) +=3D hvm/ obj-$(CONFIG_UBSAN) +=3D ubsan/ =20 obj-$(CONFIG_NEEDS_LIBELF) +=3D libelf/ diff --git a/xen/common/hvm/Makefile b/xen/common/hvm/Makefile new file mode 100644 index 0000000..326215d --- /dev/null +++ b/xen/common/hvm/Makefile @@ -0,0 +1 @@ +obj-y +=3D ioreq.o diff --git a/xen/common/hvm/ioreq.c b/xen/common/hvm/ioreq.c new file mode 100644 index 0000000..7e1fa23 --- /dev/null +++ b/xen/common/hvm/ioreq.c @@ -0,0 +1,1430 @@ +/* + * hvm/ioreq.c: hardware virtual machine I/O emulation + * + * Copyright (c) 2016 Citrix Systems Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +static void set_ioreq_server(struct domain *d, unsigned int id, + struct hvm_ioreq_server *s) +{ + ASSERT(id < MAX_NR_IOREQ_SERVERS); + ASSERT(!s || !d->arch.hvm.ioreq_server.server[id]); + + d->arch.hvm.ioreq_server.server[id] =3D s; +} + +/* + * Iterate over all possible ioreq servers. + * + * NOTE: The iteration is backwards such that more recently created + * ioreq servers are favoured in hvm_select_ioreq_server(). + * This is a semantic that previously existed when ioreq servers + * were held in a linked list. + */ +#define FOR_EACH_IOREQ_SERVER(d, id, s) \ + for ( (id) =3D MAX_NR_IOREQ_SERVERS; (id) !=3D 0; ) \ + if ( !(s =3D GET_IOREQ_SERVER(d, --(id))) ) \ + continue; \ + else + +static ioreq_t *get_ioreq(struct hvm_ioreq_server *s, struct vcpu *v) +{ + shared_iopage_t *p =3D s->ioreq.va; + + ASSERT((v =3D=3D current) || !vcpu_runnable(v)); + ASSERT(p !=3D NULL); + + return &p->vcpu_ioreq[v->vcpu_id]; +} + +bool hvm_io_pending(struct vcpu *v) +{ + struct domain *d =3D v->domain; + struct hvm_ioreq_server *s; + unsigned int id; + + FOR_EACH_IOREQ_SERVER(d, id, s) + { + struct hvm_ioreq_vcpu *sv; + + list_for_each_entry ( sv, + &s->ioreq_vcpu_list, + list_entry ) + { + if ( sv->vcpu =3D=3D v && sv->pending ) + return true; + } + } + + return false; +} + +static void hvm_io_assist(struct hvm_ioreq_vcpu *sv, uint64_t data) +{ + struct vcpu *v =3D sv->vcpu; + ioreq_t *ioreq =3D &v->arch.hvm.hvm_io.io_req; + + if ( hvm_ioreq_needs_completion(ioreq) ) + ioreq->data =3D data; + + sv->pending =3D false; +} + +static bool hvm_wait_for_io(struct hvm_ioreq_vcpu *sv, ioreq_t *p) +{ + unsigned int prev_state =3D STATE_IOREQ_NONE; + + while ( sv->pending ) + { + unsigned int state =3D p->state; + + smp_rmb(); + + recheck: + if ( unlikely(state =3D=3D STATE_IOREQ_NONE) ) + { + /* + * The only reason we should see this case is when an + * emulator is dying and it races with an I/O being + * requested. + */ + hvm_io_assist(sv, ~0ul); + break; + } + + if ( unlikely(state < prev_state) ) + { + gdprintk(XENLOG_ERR, "Weird HVM ioreq state transition %u -> %= u\n", + prev_state, state); + sv->pending =3D false; + domain_crash(sv->vcpu->domain); + return false; /* bail */ + } + + switch ( prev_state =3D state ) + { + case STATE_IORESP_READY: /* IORESP_READY -> NONE */ + p->state =3D STATE_IOREQ_NONE; + hvm_io_assist(sv, p->data); + break; + case STATE_IOREQ_READY: /* IOREQ_{READY,INPROCESS} -> IORESP_READ= Y */ + case STATE_IOREQ_INPROCESS: + wait_on_xen_event_channel(sv->ioreq_evtchn, + ({ state =3D p->state; + smp_rmb(); + state !=3D prev_state; })); + goto recheck; + default: + gdprintk(XENLOG_ERR, "Weird HVM iorequest state %u\n", state); + sv->pending =3D false; + domain_crash(sv->vcpu->domain); + return false; /* bail */ + } + } + + return true; +} + +bool handle_hvm_io_completion(struct vcpu *v) +{ + struct domain *d =3D v->domain; + struct hvm_vcpu_io *vio =3D &v->arch.hvm.hvm_io; + struct hvm_ioreq_server *s; + enum hvm_io_completion io_completion; + unsigned int id; + + if ( has_vpci(d) && vpci_process_pending(v) ) + { + raise_softirq(SCHEDULE_SOFTIRQ); + return false; + } + + FOR_EACH_IOREQ_SERVER(d, id, s) + { + struct hvm_ioreq_vcpu *sv; + + list_for_each_entry ( sv, + &s->ioreq_vcpu_list, + list_entry ) + { + if ( sv->vcpu =3D=3D v && sv->pending ) + { + if ( !hvm_wait_for_io(sv, get_ioreq(s, v)) ) + return false; + + break; + } + } + } + + vio->io_req.state =3D hvm_ioreq_needs_completion(&vio->io_req) ? + STATE_IORESP_READY : STATE_IOREQ_NONE; + + msix_write_completion(v); + vcpu_end_shutdown_deferral(v); + + io_completion =3D vio->io_completion; + vio->io_completion =3D HVMIO_no_completion; + + switch ( io_completion ) + { + case HVMIO_no_completion: + break; + + case HVMIO_mmio_completion: + return handle_mmio(); + + case HVMIO_pio_completion: + return handle_pio(vio->io_req.addr, vio->io_req.size, + vio->io_req.dir); + + case HVMIO_realmode_completion: + handle_realmode_completion(); + break; + + default: + ASSERT_UNREACHABLE(); + break; + } + + return true; +} + +static gfn_t hvm_alloc_legacy_ioreq_gfn(struct hvm_ioreq_server *s) +{ + struct domain *d =3D s->target; + unsigned int i; + + BUILD_BUG_ON(HVM_PARAM_BUFIOREQ_PFN !=3D HVM_PARAM_IOREQ_PFN + 1); + + for ( i =3D HVM_PARAM_IOREQ_PFN; i <=3D HVM_PARAM_BUFIOREQ_PFN; i++ ) + { + if ( !test_and_clear_bit(i, &d->arch.hvm.ioreq_gfn.legacy_mask) ) + return _gfn(d->arch.hvm.params[i]); + } + + return INVALID_GFN; +} + +static gfn_t hvm_alloc_ioreq_gfn(struct hvm_ioreq_server *s) +{ + struct domain *d =3D s->target; + unsigned int i; + + for ( i =3D 0; i < sizeof(d->arch.hvm.ioreq_gfn.mask) * 8; i++ ) + { + if ( test_and_clear_bit(i, &d->arch.hvm.ioreq_gfn.mask) ) + return _gfn(d->arch.hvm.ioreq_gfn.base + i); + } + + /* + * If we are out of 'normal' GFNs then we may still have a 'legacy' + * GFN available. + */ + return hvm_alloc_legacy_ioreq_gfn(s); +} + +static bool hvm_free_legacy_ioreq_gfn(struct hvm_ioreq_server *s, + gfn_t gfn) +{ + struct domain *d =3D s->target; + unsigned int i; + + for ( i =3D HVM_PARAM_IOREQ_PFN; i <=3D HVM_PARAM_BUFIOREQ_PFN; i++ ) + { + if ( gfn_eq(gfn, _gfn(d->arch.hvm.params[i])) ) + break; + } + if ( i > HVM_PARAM_BUFIOREQ_PFN ) + return false; + + set_bit(i, &d->arch.hvm.ioreq_gfn.legacy_mask); + return true; +} + +static void hvm_free_ioreq_gfn(struct hvm_ioreq_server *s, gfn_t gfn) +{ + struct domain *d =3D s->target; + unsigned int i =3D gfn_x(gfn) - d->arch.hvm.ioreq_gfn.base; + + ASSERT(!gfn_eq(gfn, INVALID_GFN)); + + if ( !hvm_free_legacy_ioreq_gfn(s, gfn) ) + { + ASSERT(i < sizeof(d->arch.hvm.ioreq_gfn.mask) * 8); + set_bit(i, &d->arch.hvm.ioreq_gfn.mask); + } +} + +static void hvm_unmap_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) +{ + struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; + + if ( gfn_eq(iorp->gfn, INVALID_GFN) ) + return; + + destroy_ring_for_helper(&iorp->va, iorp->page); + iorp->page =3D NULL; + + hvm_free_ioreq_gfn(s, iorp->gfn); + iorp->gfn =3D INVALID_GFN; +} + +static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) +{ + struct domain *d =3D s->target; + struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; + int rc; + + if ( iorp->page ) + { + /* + * If a page has already been allocated (which will happen on + * demand if hvm_get_ioreq_server_frame() is called), then + * mapping a guest frame is not permitted. + */ + if ( gfn_eq(iorp->gfn, INVALID_GFN) ) + return -EPERM; + + return 0; + } + + if ( d->is_dying ) + return -EINVAL; + + iorp->gfn =3D hvm_alloc_ioreq_gfn(s); + + if ( gfn_eq(iorp->gfn, INVALID_GFN) ) + return -ENOMEM; + + rc =3D prepare_ring_for_helper(d, gfn_x(iorp->gfn), &iorp->page, + &iorp->va); + + if ( rc ) + hvm_unmap_ioreq_gfn(s, buf); + + return rc; +} + +static int hvm_alloc_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) +{ + struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; + struct page_info *page; + + if ( iorp->page ) + { + /* + * If a guest frame has already been mapped (which may happen + * on demand if hvm_get_ioreq_server_info() is called), then + * allocating a page is not permitted. + */ + if ( !gfn_eq(iorp->gfn, INVALID_GFN) ) + return -EPERM; + + return 0; + } + + page =3D alloc_domheap_page(s->target, MEMF_no_refcount); + + if ( !page ) + return -ENOMEM; + + if ( !get_page_and_type(page, s->target, PGT_writable_page) ) + { + /* + * The domain can't possibly know about this page yet, so failure + * here is a clear indication of something fishy going on. + */ + domain_crash(s->emulator); + return -ENODATA; + } + + iorp->va =3D __map_domain_page_global(page); + if ( !iorp->va ) + goto fail; + + iorp->page =3D page; + clear_page(iorp->va); + return 0; + + fail: + put_page_alloc_ref(page); + put_page_and_type(page); + + return -ENOMEM; +} + +static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf) +{ + struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; + struct page_info *page =3D iorp->page; + + if ( !page ) + return; + + iorp->page =3D NULL; + + unmap_domain_page_global(iorp->va); + iorp->va =3D NULL; + + put_page_alloc_ref(page); + put_page_and_type(page); +} + +bool is_ioreq_server_page(struct domain *d, const struct page_info *page) +{ + const struct hvm_ioreq_server *s; + unsigned int id; + bool found =3D false; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + FOR_EACH_IOREQ_SERVER(d, id, s) + { + if ( (s->ioreq.page =3D=3D page) || (s->bufioreq.page =3D=3D page)= ) + { + found =3D true; + break; + } + } + + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return found; +} + +static void hvm_remove_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) + +{ + struct domain *d =3D s->target; + struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; + + if ( gfn_eq(iorp->gfn, INVALID_GFN) ) + return; + + if ( guest_physmap_remove_page(d, iorp->gfn, + page_to_mfn(iorp->page), 0) ) + domain_crash(d); + clear_page(iorp->va); +} + +static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) +{ + struct domain *d =3D s->target; + struct hvm_ioreq_page *iorp =3D buf ? &s->bufioreq : &s->ioreq; + int rc; + + if ( gfn_eq(iorp->gfn, INVALID_GFN) ) + return 0; + + clear_page(iorp->va); + + rc =3D guest_physmap_add_page(d, iorp->gfn, + page_to_mfn(iorp->page), 0); + if ( rc =3D=3D 0 ) + paging_mark_pfn_dirty(d, _pfn(gfn_x(iorp->gfn))); + + return rc; +} + +static void hvm_update_ioreq_evtchn(struct hvm_ioreq_server *s, + struct hvm_ioreq_vcpu *sv) +{ + ASSERT(spin_is_locked(&s->lock)); + + if ( s->ioreq.va !=3D NULL ) + { + ioreq_t *p =3D get_ioreq(s, sv->vcpu); + + p->vp_eport =3D sv->ioreq_evtchn; + } +} + +#define HANDLE_BUFIOREQ(s) \ + ((s)->bufioreq_handling !=3D HVM_IOREQSRV_BUFIOREQ_OFF) + +static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s, + struct vcpu *v) +{ + struct hvm_ioreq_vcpu *sv; + int rc; + + sv =3D xzalloc(struct hvm_ioreq_vcpu); + + rc =3D -ENOMEM; + if ( !sv ) + goto fail1; + + spin_lock(&s->lock); + + rc =3D alloc_unbound_xen_event_channel(v->domain, v->vcpu_id, + s->emulator->domain_id, NULL); + if ( rc < 0 ) + goto fail2; + + sv->ioreq_evtchn =3D rc; + + if ( v->vcpu_id =3D=3D 0 && HANDLE_BUFIOREQ(s) ) + { + rc =3D alloc_unbound_xen_event_channel(v->domain, 0, + s->emulator->domain_id, NULL); + if ( rc < 0 ) + goto fail3; + + s->bufioreq_evtchn =3D rc; + } + + sv->vcpu =3D v; + + list_add(&sv->list_entry, &s->ioreq_vcpu_list); + + if ( s->enabled ) + hvm_update_ioreq_evtchn(s, sv); + + spin_unlock(&s->lock); + return 0; + + fail3: + free_xen_event_channel(v->domain, sv->ioreq_evtchn); + + fail2: + spin_unlock(&s->lock); + xfree(sv); + + fail1: + return rc; +} + +static void hvm_ioreq_server_remove_vcpu(struct hvm_ioreq_server *s, + struct vcpu *v) +{ + struct hvm_ioreq_vcpu *sv; + + spin_lock(&s->lock); + + list_for_each_entry ( sv, + &s->ioreq_vcpu_list, + list_entry ) + { + if ( sv->vcpu !=3D v ) + continue; + + list_del(&sv->list_entry); + + if ( v->vcpu_id =3D=3D 0 && HANDLE_BUFIOREQ(s) ) + free_xen_event_channel(v->domain, s->bufioreq_evtchn); + + free_xen_event_channel(v->domain, sv->ioreq_evtchn); + + xfree(sv); + break; + } + + spin_unlock(&s->lock); +} + +static void hvm_ioreq_server_remove_all_vcpus(struct hvm_ioreq_server *s) +{ + struct hvm_ioreq_vcpu *sv, *next; + + spin_lock(&s->lock); + + list_for_each_entry_safe ( sv, + next, + &s->ioreq_vcpu_list, + list_entry ) + { + struct vcpu *v =3D sv->vcpu; + + list_del(&sv->list_entry); + + if ( v->vcpu_id =3D=3D 0 && HANDLE_BUFIOREQ(s) ) + free_xen_event_channel(v->domain, s->bufioreq_evtchn); + + free_xen_event_channel(v->domain, sv->ioreq_evtchn); + + xfree(sv); + } + + spin_unlock(&s->lock); +} + +static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s) +{ + int rc; + + rc =3D hvm_map_ioreq_gfn(s, false); + + if ( !rc && HANDLE_BUFIOREQ(s) ) + rc =3D hvm_map_ioreq_gfn(s, true); + + if ( rc ) + hvm_unmap_ioreq_gfn(s, false); + + return rc; +} + +static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s) +{ + hvm_unmap_ioreq_gfn(s, true); + hvm_unmap_ioreq_gfn(s, false); +} + +static int hvm_ioreq_server_alloc_pages(struct hvm_ioreq_server *s) +{ + int rc; + + rc =3D hvm_alloc_ioreq_mfn(s, false); + + if ( !rc && (s->bufioreq_handling !=3D HVM_IOREQSRV_BUFIOREQ_OFF) ) + rc =3D hvm_alloc_ioreq_mfn(s, true); + + if ( rc ) + hvm_free_ioreq_mfn(s, false); + + return rc; +} + +static void hvm_ioreq_server_free_pages(struct hvm_ioreq_server *s) +{ + hvm_free_ioreq_mfn(s, true); + hvm_free_ioreq_mfn(s, false); +} + +static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s) +{ + unsigned int i; + + for ( i =3D 0; i < NR_IO_RANGE_TYPES; i++ ) + rangeset_destroy(s->range[i]); +} + +static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s, + ioservid_t id) +{ + unsigned int i; + int rc; + + for ( i =3D 0; i < NR_IO_RANGE_TYPES; i++ ) + { + char *name; + + rc =3D asprintf(&name, "ioreq_server %d %s", id, + (i =3D=3D XEN_DMOP_IO_RANGE_PORT) ? "port" : + (i =3D=3D XEN_DMOP_IO_RANGE_MEMORY) ? "memory" : + (i =3D=3D XEN_DMOP_IO_RANGE_PCI) ? "pci" : + ""); + if ( rc ) + goto fail; + + s->range[i] =3D rangeset_new(s->target, name, + RANGESETF_prettyprint_hex); + + xfree(name); + + rc =3D -ENOMEM; + if ( !s->range[i] ) + goto fail; + + rangeset_limit(s->range[i], MAX_NR_IO_RANGES); + } + + return 0; + + fail: + hvm_ioreq_server_free_rangesets(s); + + return rc; +} + +static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s) +{ + struct hvm_ioreq_vcpu *sv; + + spin_lock(&s->lock); + + if ( s->enabled ) + goto done; + + hvm_remove_ioreq_gfn(s, false); + hvm_remove_ioreq_gfn(s, true); + + s->enabled =3D true; + + list_for_each_entry ( sv, + &s->ioreq_vcpu_list, + list_entry ) + hvm_update_ioreq_evtchn(s, sv); + + done: + spin_unlock(&s->lock); +} + +static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s) +{ + spin_lock(&s->lock); + + if ( !s->enabled ) + goto done; + + hvm_add_ioreq_gfn(s, true); + hvm_add_ioreq_gfn(s, false); + + s->enabled =3D false; + + done: + spin_unlock(&s->lock); +} + +static int hvm_ioreq_server_init(struct hvm_ioreq_server *s, + struct domain *d, int bufioreq_handling, + ioservid_t id) +{ + struct domain *currd =3D current->domain; + struct vcpu *v; + int rc; + + s->target =3D d; + + get_knownalive_domain(currd); + s->emulator =3D currd; + + spin_lock_init(&s->lock); + INIT_LIST_HEAD(&s->ioreq_vcpu_list); + spin_lock_init(&s->bufioreq_lock); + + s->ioreq.gfn =3D INVALID_GFN; + s->bufioreq.gfn =3D INVALID_GFN; + + rc =3D hvm_ioreq_server_alloc_rangesets(s, id); + if ( rc ) + return rc; + + s->bufioreq_handling =3D bufioreq_handling; + + for_each_vcpu ( d, v ) + { + rc =3D hvm_ioreq_server_add_vcpu(s, v); + if ( rc ) + goto fail_add; + } + + return 0; + + fail_add: + hvm_ioreq_server_remove_all_vcpus(s); + hvm_ioreq_server_unmap_pages(s); + + hvm_ioreq_server_free_rangesets(s); + + put_domain(s->emulator); + return rc; +} + +static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s) +{ + ASSERT(!s->enabled); + hvm_ioreq_server_remove_all_vcpus(s); + + /* + * NOTE: It is safe to call both hvm_ioreq_server_unmap_pages() and + * hvm_ioreq_server_free_pages() in that order. + * This is because the former will do nothing if the pages + * are not mapped, leaving the page to be freed by the latter. + * However if the pages are mapped then the former will set + * the page_info pointer to NULL, meaning the latter will do + * nothing. + */ + hvm_ioreq_server_unmap_pages(s); + hvm_ioreq_server_free_pages(s); + + hvm_ioreq_server_free_rangesets(s); + + put_domain(s->emulator); +} + +int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling, + ioservid_t *id) +{ + struct hvm_ioreq_server *s; + unsigned int i; + int rc; + + if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC ) + return -EINVAL; + + s =3D xzalloc(struct hvm_ioreq_server); + if ( !s ) + return -ENOMEM; + + domain_pause(d); + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + for ( i =3D 0; i < MAX_NR_IOREQ_SERVERS; i++ ) + { + if ( !GET_IOREQ_SERVER(d, i) ) + break; + } + + rc =3D -ENOSPC; + if ( i >=3D MAX_NR_IOREQ_SERVERS ) + goto fail; + + /* + * It is safe to call set_ioreq_server() prior to + * hvm_ioreq_server_init() since the target domain is paused. + */ + set_ioreq_server(d, i, s); + + rc =3D hvm_ioreq_server_init(s, d, bufioreq_handling, i); + if ( rc ) + { + set_ioreq_server(d, i, NULL); + goto fail; + } + + if ( id ) + *id =3D i; + + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + domain_unpause(d); + + return 0; + + fail: + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + domain_unpause(d); + + xfree(s); + return rc; +} + +int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) +{ + struct hvm_ioreq_server *s; + int rc; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + s =3D get_ioreq_server(d, id); + + rc =3D -ENOENT; + if ( !s ) + goto out; + + rc =3D -EPERM; + if ( s->emulator !=3D current->domain ) + goto out; + + domain_pause(d); + + p2m_set_ioreq_server(d, 0, s); + + hvm_ioreq_server_disable(s); + + /* + * It is safe to call hvm_ioreq_server_deinit() prior to + * set_ioreq_server() since the target domain is paused. + */ + hvm_ioreq_server_deinit(s); + set_ioreq_server(d, id, NULL); + + domain_unpause(d); + + xfree(s); + + rc =3D 0; + + out: + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return rc; +} + +int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, + unsigned long *ioreq_gfn, + unsigned long *bufioreq_gfn, + evtchn_port_t *bufioreq_port) +{ + struct hvm_ioreq_server *s; + int rc; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + s =3D get_ioreq_server(d, id); + + rc =3D -ENOENT; + if ( !s ) + goto out; + + rc =3D -EPERM; + if ( s->emulator !=3D current->domain ) + goto out; + + if ( ioreq_gfn || bufioreq_gfn ) + { + rc =3D hvm_ioreq_server_map_pages(s); + if ( rc ) + goto out; + } + + if ( ioreq_gfn ) + *ioreq_gfn =3D gfn_x(s->ioreq.gfn); + + if ( HANDLE_BUFIOREQ(s) ) + { + if ( bufioreq_gfn ) + *bufioreq_gfn =3D gfn_x(s->bufioreq.gfn); + + if ( bufioreq_port ) + *bufioreq_port =3D s->bufioreq_evtchn; + } + + rc =3D 0; + + out: + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return rc; +} + +int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, + unsigned long idx, mfn_t *mfn) +{ + struct hvm_ioreq_server *s; + int rc; + + ASSERT(is_hvm_domain(d)); + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + s =3D get_ioreq_server(d, id); + + rc =3D -ENOENT; + if ( !s ) + goto out; + + rc =3D -EPERM; + if ( s->emulator !=3D current->domain ) + goto out; + + rc =3D hvm_ioreq_server_alloc_pages(s); + if ( rc ) + goto out; + + switch ( idx ) + { + case XENMEM_resource_ioreq_server_frame_bufioreq: + rc =3D -ENOENT; + if ( !HANDLE_BUFIOREQ(s) ) + goto out; + + *mfn =3D page_to_mfn(s->bufioreq.page); + rc =3D 0; + break; + + case XENMEM_resource_ioreq_server_frame_ioreq(0): + *mfn =3D page_to_mfn(s->ioreq.page); + rc =3D 0; + break; + + default: + rc =3D -EINVAL; + break; + } + + out: + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return rc; +} + +int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, + uint32_t type, uint64_t start, + uint64_t end) +{ + struct hvm_ioreq_server *s; + struct rangeset *r; + int rc; + + if ( start > end ) + return -EINVAL; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + s =3D get_ioreq_server(d, id); + + rc =3D -ENOENT; + if ( !s ) + goto out; + + rc =3D -EPERM; + if ( s->emulator !=3D current->domain ) + goto out; + + switch ( type ) + { + case XEN_DMOP_IO_RANGE_PORT: + case XEN_DMOP_IO_RANGE_MEMORY: + case XEN_DMOP_IO_RANGE_PCI: + r =3D s->range[type]; + break; + + default: + r =3D NULL; + break; + } + + rc =3D -EINVAL; + if ( !r ) + goto out; + + rc =3D -EEXIST; + if ( rangeset_overlaps_range(r, start, end) ) + goto out; + + rc =3D rangeset_add_range(r, start, end); + + out: + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return rc; +} + +int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, + uint32_t type, uint64_t start, + uint64_t end) +{ + struct hvm_ioreq_server *s; + struct rangeset *r; + int rc; + + if ( start > end ) + return -EINVAL; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + s =3D get_ioreq_server(d, id); + + rc =3D -ENOENT; + if ( !s ) + goto out; + + rc =3D -EPERM; + if ( s->emulator !=3D current->domain ) + goto out; + + switch ( type ) + { + case XEN_DMOP_IO_RANGE_PORT: + case XEN_DMOP_IO_RANGE_MEMORY: + case XEN_DMOP_IO_RANGE_PCI: + r =3D s->range[type]; + break; + + default: + r =3D NULL; + break; + } + + rc =3D -EINVAL; + if ( !r ) + goto out; + + rc =3D -ENOENT; + if ( !rangeset_contains_range(r, start, end) ) + goto out; + + rc =3D rangeset_remove_range(r, start, end); + + out: + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return rc; +} + +int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, + bool enabled) +{ + struct hvm_ioreq_server *s; + int rc; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + s =3D get_ioreq_server(d, id); + + rc =3D -ENOENT; + if ( !s ) + goto out; + + rc =3D -EPERM; + if ( s->emulator !=3D current->domain ) + goto out; + + domain_pause(d); + + if ( enabled ) + hvm_ioreq_server_enable(s); + else + hvm_ioreq_server_disable(s); + + domain_unpause(d); + + rc =3D 0; + + out: + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + return rc; +} + +int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v) +{ + struct hvm_ioreq_server *s; + unsigned int id; + int rc; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + FOR_EACH_IOREQ_SERVER(d, id, s) + { + rc =3D hvm_ioreq_server_add_vcpu(s, v); + if ( rc ) + goto fail; + } + + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return 0; + + fail: + while ( ++id !=3D MAX_NR_IOREQ_SERVERS ) + { + s =3D GET_IOREQ_SERVER(d, id); + + if ( !s ) + continue; + + hvm_ioreq_server_remove_vcpu(s, v); + } + + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); + + return rc; +} + +void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v) +{ + struct hvm_ioreq_server *s; + unsigned int id; + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + FOR_EACH_IOREQ_SERVER(d, id, s) + hvm_ioreq_server_remove_vcpu(s, v); + + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); +} + +void hvm_destroy_all_ioreq_servers(struct domain *d) +{ + struct hvm_ioreq_server *s; + unsigned int id; + + arch_hvm_ioreq_destroy(d); + + spin_lock_recursive(&d->arch.hvm.ioreq_server.lock); + + /* No need to domain_pause() as the domain is being torn down */ + + FOR_EACH_IOREQ_SERVER(d, id, s) + { + hvm_ioreq_server_disable(s); + + /* + * It is safe to call hvm_ioreq_server_deinit() prior to + * set_ioreq_server() since the target domain is being destroyed. + */ + hvm_ioreq_server_deinit(s); + set_ioreq_server(d, id, NULL); + + xfree(s); + } + + spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock); +} + +struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, + ioreq_t *p) +{ + struct hvm_ioreq_server *s; + uint8_t type; + uint64_t addr; + unsigned int id; + + if ( p->type !=3D IOREQ_TYPE_COPY && p->type !=3D IOREQ_TYPE_PIO ) + return NULL; + + hvm_get_ioreq_server_range_type(d, p, &type, &addr); + + FOR_EACH_IOREQ_SERVER(d, id, s) + { + struct rangeset *r; + + if ( !s->enabled ) + continue; + + r =3D s->range[type]; + + switch ( type ) + { + unsigned long start, end; + + case XEN_DMOP_IO_RANGE_PORT: + start =3D addr; + end =3D start + p->size - 1; + if ( rangeset_contains_range(r, start, end) ) + return s; + + break; + + case XEN_DMOP_IO_RANGE_MEMORY: + start =3D hvm_mmio_first_byte(p); + end =3D hvm_mmio_last_byte(p); + + if ( rangeset_contains_range(r, start, end) ) + return s; + + break; + + case XEN_DMOP_IO_RANGE_PCI: + if ( rangeset_contains_singleton(r, addr >> 32) ) + { + p->type =3D IOREQ_TYPE_PCI_CONFIG; + p->addr =3D addr; + return s; + } + + break; + } + } + + return NULL; +} + +static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p) +{ + struct domain *d =3D current->domain; + struct hvm_ioreq_page *iorp; + buffered_iopage_t *pg; + buf_ioreq_t bp =3D { .data =3D p->data, + .addr =3D p->addr, + .type =3D p->type, + .dir =3D p->dir }; + /* Timeoffset sends 64b data, but no address. Use two consecutive slot= s. */ + int qw =3D 0; + + /* Ensure buffered_iopage fits in a page */ + BUILD_BUG_ON(sizeof(buffered_iopage_t) > PAGE_SIZE); + + iorp =3D &s->bufioreq; + pg =3D iorp->va; + + if ( !pg ) + return IOREQ_IO_UNHANDLED; + + /* + * Return 0 for the cases we can't deal with: + * - 'addr' is only a 20-bit field, so we cannot address beyond 1MB + * - we cannot buffer accesses to guest memory buffers, as the guest + * may expect the memory buffer to be synchronously accessed + * - the count field is usually used with data_is_ptr and since we do= n't + * support data_is_ptr we do not waste space for the count field ei= ther + */ + if ( (p->addr > 0xffffful) || p->data_is_ptr || (p->count !=3D 1) ) + return 0; + + switch ( p->size ) + { + case 1: + bp.size =3D 0; + break; + case 2: + bp.size =3D 1; + break; + case 4: + bp.size =3D 2; + break; + case 8: + bp.size =3D 3; + qw =3D 1; + break; + default: + gdprintk(XENLOG_WARNING, "unexpected ioreq size: %u\n", p->size); + return IOREQ_IO_UNHANDLED; + } + + spin_lock(&s->bufioreq_lock); + + if ( (pg->ptrs.write_pointer - pg->ptrs.read_pointer) >=3D + (IOREQ_BUFFER_SLOT_NUM - qw) ) + { + /* The queue is full: send the iopacket through the normal path. */ + spin_unlock(&s->bufioreq_lock); + return IOREQ_IO_UNHANDLED; + } + + pg->buf_ioreq[pg->ptrs.write_pointer % IOREQ_BUFFER_SLOT_NUM] =3D bp; + + if ( qw ) + { + bp.data =3D p->data >> 32; + pg->buf_ioreq[(pg->ptrs.write_pointer+1) % IOREQ_BUFFER_SLOT_NUM] = =3D bp; + } + + /* Make the ioreq_t visible /before/ write_pointer. */ + smp_wmb(); + pg->ptrs.write_pointer +=3D qw ? 2 : 1; + + /* Canonicalize read/write pointers to prevent their overflow. */ + while ( (s->bufioreq_handling =3D=3D HVM_IOREQSRV_BUFIOREQ_ATOMIC) && + qw++ < IOREQ_BUFFER_SLOT_NUM && + pg->ptrs.read_pointer >=3D IOREQ_BUFFER_SLOT_NUM ) + { + union bufioreq_pointers old =3D pg->ptrs, new; + unsigned int n =3D old.read_pointer / IOREQ_BUFFER_SLOT_NUM; + + new.read_pointer =3D old.read_pointer - n * IOREQ_BUFFER_SLOT_NUM; + new.write_pointer =3D old.write_pointer - n * IOREQ_BUFFER_SLOT_NU= M; + cmpxchg(&pg->ptrs.full, old.full, new.full); + } + + notify_via_xen_event_channel(d, s->bufioreq_evtchn); + spin_unlock(&s->bufioreq_lock); + + return IOREQ_IO_HANDLED; +} + +int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p, + bool buffered) +{ + struct vcpu *curr =3D current; + struct domain *d =3D curr->domain; + struct hvm_ioreq_vcpu *sv; + + ASSERT(s); + + if ( buffered ) + return hvm_send_buffered_ioreq(s, proto_p); + + if ( unlikely(!vcpu_start_shutdown_deferral(curr)) ) + return IOREQ_IO_RETRY; + + list_for_each_entry ( sv, + &s->ioreq_vcpu_list, + list_entry ) + { + if ( sv->vcpu =3D=3D curr ) + { + evtchn_port_t port =3D sv->ioreq_evtchn; + ioreq_t *p =3D get_ioreq(s, curr); + + if ( unlikely(p->state !=3D STATE_IOREQ_NONE) ) + { + gprintk(XENLOG_ERR, "device model set bad IO state %d\n", + p->state); + break; + } + + if ( unlikely(p->vp_eport !=3D port) ) + { + gprintk(XENLOG_ERR, "device model set bad event channel %d= \n", + p->vp_eport); + break; + } + + proto_p->state =3D STATE_IOREQ_NONE; + proto_p->vp_eport =3D port; + *p =3D *proto_p; + + prepare_wait_on_xen_event_channel(port); + + /* + * Following happens /after/ blocking and setting up ioreq + * contents. prepare_wait_on_xen_event_channel() is an implicit + * barrier. + */ + p->state =3D STATE_IOREQ_READY; + notify_via_xen_event_channel(d, port); + + sv->pending =3D true; + return IOREQ_IO_RETRY; + } + } + + return IOREQ_IO_UNHANDLED; +} + +unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered) +{ + struct domain *d =3D current->domain; + struct hvm_ioreq_server *s; + unsigned int id, failed =3D 0; + + FOR_EACH_IOREQ_SERVER(d, id, s) + { + if ( !s->enabled ) + continue; + + if ( hvm_send_ioreq(s, p, buffered) =3D=3D IOREQ_IO_UNHANDLED ) + failed++; + } + + return failed; +} + +void hvm_ioreq_init(struct domain *d) +{ + spin_lock_init(&d->arch.hvm.ioreq_server.lock); + + arch_hvm_ioreq_init(d); +} + +/* + * 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/ioreq.h b/xen/include/asm-x86/hvm/iore= q.h index e2588e9..0e871e0 100644 --- a/xen/include/asm-x86/hvm/ioreq.h +++ b/xen/include/asm-x86/hvm/ioreq.h @@ -19,41 +19,26 @@ #ifndef __ASM_X86_HVM_IOREQ_H__ #define __ASM_X86_HVM_IOREQ_H__ =20 -bool hvm_io_pending(struct vcpu *v); -bool handle_hvm_io_completion(struct vcpu *v); -bool is_ioreq_server_page(struct domain *d, const struct page_info *page); +#include +#include +#include + +void handle_realmode_completion(void); + +void hvm_get_ioreq_server_range_type(struct domain *d, + ioreq_t *p, + uint8_t *type, + uint64_t *addr); =20 -int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling, - ioservid_t *id); -int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id); -int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, - unsigned long *ioreq_gfn, - unsigned long *bufioreq_gfn, - evtchn_port_t *bufioreq_port); -int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, - unsigned long idx, mfn_t *mfn); -int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, - uint32_t type, uint64_t start, - uint64_t end); -int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, - uint32_t type, uint64_t start, - uint64_t end); int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, uint32_t type, uint32_t flags); -int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, - bool enabled); - -int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v); -void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v); -void hvm_destroy_all_ioreq_servers(struct domain *d); =20 -struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, - ioreq_t *p); -int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p, - bool buffered); -unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); +void arch_hvm_ioreq_init(struct domain *d); +void arch_hvm_ioreq_destroy(struct domain *d); =20 -void hvm_ioreq_init(struct domain *d); +#define IOREQ_IO_HANDLED X86EMUL_OKAY +#define IOREQ_IO_UNHANDLED X86EMUL_UNHANDLEABLE +#define IOREQ_IO_RETRY X86EMUL_RETRY =20 #endif /* __ASM_X86_HVM_IOREQ_H__ */ =20 diff --git a/xen/include/asm-x86/hvm/vcpu.h b/xen/include/asm-x86/hvm/vcpu.h index 5ccd075..6c1feda 100644 --- a/xen/include/asm-x86/hvm/vcpu.h +++ b/xen/include/asm-x86/hvm/vcpu.h @@ -91,13 +91,6 @@ struct hvm_vcpu_io { const struct g2m_ioport *g2m_ioport; }; =20 -static inline bool hvm_ioreq_needs_completion(const ioreq_t *ioreq) -{ - return ioreq->state =3D=3D STATE_IOREQ_READY && - !ioreq->data_is_ptr && - (ioreq->type !=3D IOREQ_TYPE_PIO || ioreq->dir !=3D IOREQ_WRITE= ); -} - struct nestedvcpu { bool_t nv_guestmode; /* vcpu in guestmode? */ void *nv_vvmcx; /* l1 guest virtual VMCB/VMCS */ diff --git a/xen/include/xen/hvm/ioreq.h b/xen/include/xen/hvm/ioreq.h new file mode 100644 index 0000000..40b7b5e --- /dev/null +++ b/xen/include/xen/hvm/ioreq.h @@ -0,0 +1,89 @@ +/* + * hvm.h: Hardware virtual machine assist interface definitions. + * + * Copyright (c) 2016 Citrix Systems Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#ifndef __HVM_IOREQ_H__ +#define __HVM_IOREQ_H__ + +#include + +#include + +#define GET_IOREQ_SERVER(d, id) \ + (d)->arch.hvm.ioreq_server.server[id] + +static inline struct hvm_ioreq_server *get_ioreq_server(const struct domai= n *d, + unsigned int id) +{ + if ( id >=3D MAX_NR_IOREQ_SERVERS ) + return NULL; + + return GET_IOREQ_SERVER(d, id); +} + +static inline bool hvm_ioreq_needs_completion(const ioreq_t *ioreq) +{ + return ioreq->state =3D=3D STATE_IOREQ_READY && + !ioreq->data_is_ptr && + (ioreq->type !=3D IOREQ_TYPE_PIO || ioreq->dir !=3D IOREQ_WRITE= ); +} + +bool hvm_io_pending(struct vcpu *v); +bool handle_hvm_io_completion(struct vcpu *v); +bool is_ioreq_server_page(struct domain *d, const struct page_info *page); + +int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling, + ioservid_t *id); +int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id); +int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, + unsigned long *ioreq_gfn, + unsigned long *bufioreq_gfn, + evtchn_port_t *bufioreq_port); +int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id, + unsigned long idx, mfn_t *mfn); +int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id, + uint32_t type, uint64_t start, + uint64_t end); +int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id, + uint32_t type, uint64_t start, + uint64_t end); +int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id, + bool enabled); + +int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v); +void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v); +void hvm_destroy_all_ioreq_servers(struct domain *d); + +struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, + ioreq_t *p); +int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p, + bool buffered); +unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); + +void hvm_ioreq_init(struct domain *d); + +#endif /* __HVM_IOREQ_H__ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478946; cv=none; d=zohomail.com; s=zohoarc; b=M9kj4h8xv5kYk8Hcpd2MmtjctSNpiw3VNg1wwxEMAATu2Ez2+o7bo+F3agJdd9x55Y1PqmNcZA35tuP7/QUlVoqNlwDNjzthzbE45vCoRd2q1vkkvAcHHrOu60YTQdEX0iLBzLHpqL9n5cwCKMZ6aXVFe8fh6aiCrh7BTLlHuy0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478946; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=0pU51lNIwHO5FoiBaR+EgnZXTe7Q2y5dW6t5E4s4GE0=; b=Y/XqYobZsekoucWbOam31D7H9pOGhkB0LaGFmydG//ZtFn804yf30rkwKjlsFkNI1OXwayYLOsEFkpo02e2G2JLiyxXclW/mv/gbc55A/uA0vQDaMrbnicCSpXH5+WFzKMQs/844SWSF8OnoRHO0RAizhpO4GQs/A0NPJ9TAgvY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596478946637387.77741585468675; Mon, 3 Aug 2020 11:22:26 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f5r-0008FL-Oq; Mon, 03 Aug 2020 18:21:51 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f5r-0008F4-6E for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:21:51 +0000 Received: from mail-lf1-x142.google.com (unknown [2a00:1450:4864:20::142]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 30e6dd89-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:48 +0000 (UTC) Received: by mail-lf1-x142.google.com with SMTP id v15so16439298lfg.6 for ; Mon, 03 Aug 2020 11:21:47 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:45 -0700 (PDT) X-Inumbo-ID: 30e6dd89-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0pU51lNIwHO5FoiBaR+EgnZXTe7Q2y5dW6t5E4s4GE0=; b=L5rF3KgsFvlVDo98woCwm4BaIz0k1rw0Xb4mS4wjytQGLduPEYVmvXPlKnzl5eD2Dt 69gVtlQ8s8gZWWofa3fLHq4aL5utJswNMUyS2ioZ1zlUww0TmLKNjvdEdtjxQsu+W1ql MPHYGq4ODov5OkpZidV2htaLqN4fd9SEFRtLZg+dzR9wg9th5Byi581AqwmrGxa2AZwt u7AmKOlKST+qcTugoOtBDlf64kf5jTnwlKdXeN4BloHGgsYj4+Nak6/TUiF17YQRuCh/ cRllifyG/soLz5YAOIqLQIjNE2g7N8APkwQKZ83PhuLUnG6cR6eEiIztphqniRDOcrMV J5Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0pU51lNIwHO5FoiBaR+EgnZXTe7Q2y5dW6t5E4s4GE0=; b=MaoLHE2n+wYmR8bbgaohtGEKt4bjeE3iDE50wbSO2kRQqbXxGkmAoJJ91gJo4GSLQZ yLwZAkVDwlJFyIKZnMo4J6XKh+EYIzK7sXlC+8dMd2KywPv+MrE6FtbRakgvzirvvhJd mvn6vw1uecXxO7sD87ybTqnoTHm+F/1ejBaovtg1oY89gFRDUlDHmItq7yuooxiLzf/L ZIhInek8Iwcy9BQBEvdTrJ+naOceYRpzkx6Bf8K9MkXp65HkCwyPt7a//HMd8jWQAWum sGXmwQybZiQH79J4kqVDkfj9RbHX+sQS6XnerDx2Yx6lp32GkLgANARiYFVRU3wV8+5w GoHg== X-Gm-Message-State: AOAM530KSeaJoAKS/KkfSim2faUjENTxG+Ho7bv7yyrbn5d015glBE5I tBxw1yh+ITVL2FFIOvYS9jrMZXA0Gkg= X-Google-Smtp-Source: ABdhPJxAh1cGoaamlcK3AxiJn33g5R3UYun+leBRiIIdErmvieI0n2cMaaIqQ9LXWTrYBOFj3QtFNg== X-Received: by 2002:a05:6512:5c7:: with SMTP id o7mr9496108lfo.124.1596478906122; Mon, 03 Aug 2020 11:21:46 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 02/12] hvm/dm: Make x86's DM feature common Date: Mon, 3 Aug 2020 21:21:18 +0300 Message-Id: <1596478888-23030-3-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Andrew Cooper , Ian Jackson , George Dunlap , Oleksandr Tyshchenko , Julien Grall , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko As a lot of x86 code can be re-used on Arm later on, this patch splits devicemodel support into common and arch specific parts. This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor. Please note, this is a split/cleanup of Julien's PoC: "Add support for Guest IO forwarding to a device emulator" Signed-off-by: Julien Grall Signed-off-by: Oleksandr Tyshchenko --- xen/arch/x86/hvm/dm.c | 287 +++-------------------------------------= ---- xen/common/hvm/Makefile | 1 + xen/common/hvm/dm.c | 287 ++++++++++++++++++++++++++++++++++++++++= ++++ xen/include/xen/hypercall.h | 12 ++ 4 files changed, 319 insertions(+), 268 deletions(-) create mode 100644 xen/common/hvm/dm.c diff --git a/xen/arch/x86/hvm/dm.c b/xen/arch/x86/hvm/dm.c index 70adb27..fb1ff09 100644 --- a/xen/arch/x86/hvm/dm.c +++ b/xen/arch/x86/hvm/dm.c @@ -29,13 +29,6 @@ =20 #include =20 -struct dmop_args { - domid_t domid; - unsigned int nr_bufs; - /* Reserve enough buf elements for all current hypercalls. */ - struct xen_dm_op_buf buf[2]; -}; - static bool _raw_copy_from_guest_buf_offset(void *dst, const struct dmop_args *args, unsigned int buf_idx, @@ -337,148 +330,20 @@ static int inject_event(struct domain *d, return 0; } =20 -static int dm_op(const struct dmop_args *op_args) +int arch_dm_op(struct xen_dm_op *op, struct domain *d, + const struct dmop_args *op_args, bool *const_op) { - struct domain *d; - struct xen_dm_op op; - bool const_op =3D true; long rc; - size_t offset; - - static const uint8_t op_size[] =3D { - [XEN_DMOP_create_ioreq_server] =3D sizeof(struct xen_= dm_op_create_ioreq_server), - [XEN_DMOP_get_ioreq_server_info] =3D sizeof(struct xen_= dm_op_get_ioreq_server_info), - [XEN_DMOP_map_io_range_to_ioreq_server] =3D sizeof(struct xen_= dm_op_ioreq_server_range), - [XEN_DMOP_unmap_io_range_from_ioreq_server] =3D sizeof(struct xen_= dm_op_ioreq_server_range), - [XEN_DMOP_set_ioreq_server_state] =3D sizeof(struct xen_= dm_op_set_ioreq_server_state), - [XEN_DMOP_destroy_ioreq_server] =3D sizeof(struct xen_= dm_op_destroy_ioreq_server), - [XEN_DMOP_track_dirty_vram] =3D sizeof(struct xen_= dm_op_track_dirty_vram), - [XEN_DMOP_set_pci_intx_level] =3D sizeof(struct xen_= dm_op_set_pci_intx_level), - [XEN_DMOP_set_isa_irq_level] =3D sizeof(struct xen_= dm_op_set_isa_irq_level), - [XEN_DMOP_set_pci_link_route] =3D sizeof(struct xen_= dm_op_set_pci_link_route), - [XEN_DMOP_modified_memory] =3D sizeof(struct xen_= dm_op_modified_memory), - [XEN_DMOP_set_mem_type] =3D sizeof(struct xen_= dm_op_set_mem_type), - [XEN_DMOP_inject_event] =3D sizeof(struct xen_= dm_op_inject_event), - [XEN_DMOP_inject_msi] =3D sizeof(struct xen_= dm_op_inject_msi), - [XEN_DMOP_map_mem_type_to_ioreq_server] =3D sizeof(struct xen_= dm_op_map_mem_type_to_ioreq_server), - [XEN_DMOP_remote_shutdown] =3D sizeof(struct xen_= dm_op_remote_shutdown), - [XEN_DMOP_relocate_memory] =3D sizeof(struct xen_= dm_op_relocate_memory), - [XEN_DMOP_pin_memory_cacheattr] =3D sizeof(struct xen_= dm_op_pin_memory_cacheattr), - }; - - rc =3D rcu_lock_remote_domain_by_id(op_args->domid, &d); - if ( rc ) - return rc; - - if ( !is_hvm_domain(d) ) - goto out; - - rc =3D xsm_dm_op(XSM_DM_PRIV, d); - if ( rc ) - goto out; - - offset =3D offsetof(struct xen_dm_op, u); - - rc =3D -EFAULT; - if ( op_args->buf[0].size < offset ) - goto out; - - if ( copy_from_guest_offset((void *)&op, op_args->buf[0].h, 0, offset)= ) - goto out; - - if ( op.op >=3D ARRAY_SIZE(op_size) ) - { - rc =3D -EOPNOTSUPP; - goto out; - } - - op.op =3D array_index_nospec(op.op, ARRAY_SIZE(op_size)); - - if ( op_args->buf[0].size < offset + op_size[op.op] ) - goto out; - - if ( copy_from_guest_offset((void *)&op.u, op_args->buf[0].h, offset, - op_size[op.op]) ) - goto out; - - rc =3D -EINVAL; - if ( op.pad ) - goto out; - - switch ( op.op ) - { - case XEN_DMOP_create_ioreq_server: - { - struct xen_dm_op_create_ioreq_server *data =3D - &op.u.create_ioreq_server; - - const_op =3D false; - - rc =3D -EINVAL; - if ( data->pad[0] || data->pad[1] || data->pad[2] ) - break; - - rc =3D hvm_create_ioreq_server(d, data->handle_bufioreq, - &data->id); - break; - } =20 - case XEN_DMOP_get_ioreq_server_info: + switch ( op->op ) { - struct xen_dm_op_get_ioreq_server_info *data =3D - &op.u.get_ioreq_server_info; - const uint16_t valid_flags =3D XEN_DMOP_no_gfns; - - const_op =3D false; - - rc =3D -EINVAL; - if ( data->flags & ~valid_flags ) - break; - - rc =3D hvm_get_ioreq_server_info(d, data->id, - (data->flags & XEN_DMOP_no_gfns) ? - NULL : &data->ioreq_gfn, - (data->flags & XEN_DMOP_no_gfns) ? - NULL : &data->bufioreq_gfn, - &data->bufioreq_port); - break; - } - - case XEN_DMOP_map_io_range_to_ioreq_server: - { - const struct xen_dm_op_ioreq_server_range *data =3D - &op.u.map_io_range_to_ioreq_server; - - rc =3D -EINVAL; - if ( data->pad ) - break; - - rc =3D hvm_map_io_range_to_ioreq_server(d, data->id, data->type, - data->start, data->end); - break; - } - - case XEN_DMOP_unmap_io_range_from_ioreq_server: - { - const struct xen_dm_op_ioreq_server_range *data =3D - &op.u.unmap_io_range_from_ioreq_server; - - rc =3D -EINVAL; - if ( data->pad ) - break; - - rc =3D hvm_unmap_io_range_from_ioreq_server(d, data->id, data->typ= e, - data->start, data->end); - break; - } - case XEN_DMOP_map_mem_type_to_ioreq_server: { struct xen_dm_op_map_mem_type_to_ioreq_server *data =3D - &op.u.map_mem_type_to_ioreq_server; + &op->u.map_mem_type_to_ioreq_server; unsigned long first_gfn =3D data->opaque; =20 - const_op =3D false; + *const_op =3D false; =20 rc =3D -EOPNOTSUPP; if ( !hap_enabled(d) ) @@ -522,36 +387,10 @@ static int dm_op(const struct dmop_args *op_args) break; } =20 - case XEN_DMOP_set_ioreq_server_state: - { - const struct xen_dm_op_set_ioreq_server_state *data =3D - &op.u.set_ioreq_server_state; - - rc =3D -EINVAL; - if ( data->pad ) - break; - - rc =3D hvm_set_ioreq_server_state(d, data->id, !!data->enabled); - break; - } - - case XEN_DMOP_destroy_ioreq_server: - { - const struct xen_dm_op_destroy_ioreq_server *data =3D - &op.u.destroy_ioreq_server; - - rc =3D -EINVAL; - if ( data->pad ) - break; - - rc =3D hvm_destroy_ioreq_server(d, data->id); - break; - } - case XEN_DMOP_track_dirty_vram: { const struct xen_dm_op_track_dirty_vram *data =3D - &op.u.track_dirty_vram; + &op->u.track_dirty_vram; =20 rc =3D -EINVAL; if ( data->pad ) @@ -567,7 +406,7 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_set_pci_intx_level: { const struct xen_dm_op_set_pci_intx_level *data =3D - &op.u.set_pci_intx_level; + &op->u.set_pci_intx_level; =20 rc =3D set_pci_intx_level(d, data->domain, data->bus, data->device, data->intx, @@ -578,7 +417,7 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_set_isa_irq_level: { const struct xen_dm_op_set_isa_irq_level *data =3D - &op.u.set_isa_irq_level; + &op->u.set_isa_irq_level; =20 rc =3D set_isa_irq_level(d, data->isa_irq, data->level); break; @@ -587,7 +426,7 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_set_pci_link_route: { const struct xen_dm_op_set_pci_link_route *data =3D - &op.u.set_pci_link_route; + &op->u.set_pci_link_route; =20 rc =3D hvm_set_pci_link_route(d, data->link, data->isa_irq); break; @@ -596,19 +435,19 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_modified_memory: { struct xen_dm_op_modified_memory *data =3D - &op.u.modified_memory; + &op->u.modified_memory; =20 rc =3D modified_memory(d, op_args, data); - const_op =3D !rc; + *const_op =3D !rc; break; } =20 case XEN_DMOP_set_mem_type: { struct xen_dm_op_set_mem_type *data =3D - &op.u.set_mem_type; + &op->u.set_mem_type; =20 - const_op =3D false; + *const_op =3D false; =20 rc =3D -EINVAL; if ( data->pad ) @@ -621,7 +460,7 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_inject_event: { const struct xen_dm_op_inject_event *data =3D - &op.u.inject_event; + &op->u.inject_event; =20 rc =3D -EINVAL; if ( data->pad0 || data->pad1 ) @@ -634,7 +473,7 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_inject_msi: { const struct xen_dm_op_inject_msi *data =3D - &op.u.inject_msi; + &op->u.inject_msi; =20 rc =3D -EINVAL; if ( data->pad ) @@ -647,7 +486,7 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_remote_shutdown: { const struct xen_dm_op_remote_shutdown *data =3D - &op.u.remote_shutdown; + &op->u.remote_shutdown; =20 domain_shutdown(d, data->reason); rc =3D 0; @@ -656,7 +495,7 @@ static int dm_op(const struct dmop_args *op_args) =20 case XEN_DMOP_relocate_memory: { - struct xen_dm_op_relocate_memory *data =3D &op.u.relocate_memory; + struct xen_dm_op_relocate_memory *data =3D &op->u.relocate_memory; struct xen_add_to_physmap xatp =3D { .domid =3D op_args->domid, .size =3D data->size, @@ -679,7 +518,7 @@ static int dm_op(const struct dmop_args *op_args) data->size -=3D rc; data->src_gfn +=3D rc; data->dst_gfn +=3D rc; - const_op =3D false; + *const_op =3D false; rc =3D -ERESTART; } break; @@ -688,7 +527,7 @@ static int dm_op(const struct dmop_args *op_args) case XEN_DMOP_pin_memory_cacheattr: { const struct xen_dm_op_pin_memory_cacheattr *data =3D - &op.u.pin_memory_cacheattr; + &op->u.pin_memory_cacheattr; =20 if ( data->pad ) { @@ -706,94 +545,6 @@ static int dm_op(const struct dmop_args *op_args) break; } =20 - if ( (!rc || rc =3D=3D -ERESTART) && - !const_op && copy_to_guest_offset(op_args->buf[0].h, offset, - (void *)&op.u, op_size[op.op]) ) - rc =3D -EFAULT; - - out: - rcu_unlock_domain(d); - - return rc; -} - -CHECK_dm_op_create_ioreq_server; -CHECK_dm_op_get_ioreq_server_info; -CHECK_dm_op_ioreq_server_range; -CHECK_dm_op_set_ioreq_server_state; -CHECK_dm_op_destroy_ioreq_server; -CHECK_dm_op_track_dirty_vram; -CHECK_dm_op_set_pci_intx_level; -CHECK_dm_op_set_isa_irq_level; -CHECK_dm_op_set_pci_link_route; -CHECK_dm_op_modified_memory; -CHECK_dm_op_set_mem_type; -CHECK_dm_op_inject_event; -CHECK_dm_op_inject_msi; -CHECK_dm_op_remote_shutdown; -CHECK_dm_op_relocate_memory; -CHECK_dm_op_pin_memory_cacheattr; - -int compat_dm_op(domid_t domid, - unsigned int nr_bufs, - XEN_GUEST_HANDLE_PARAM(void) bufs) -{ - struct dmop_args args; - unsigned int i; - int rc; - - if ( nr_bufs > ARRAY_SIZE(args.buf) ) - return -E2BIG; - - args.domid =3D domid; - args.nr_bufs =3D array_index_nospec(nr_bufs, ARRAY_SIZE(args.buf) + 1); - - for ( i =3D 0; i < args.nr_bufs; i++ ) - { - struct compat_dm_op_buf cmp; - - if ( copy_from_guest_offset(&cmp, bufs, i, 1) ) - return -EFAULT; - -#define XLAT_dm_op_buf_HNDL_h(_d_, _s_) \ - guest_from_compat_handle((_d_)->h, (_s_)->h) - - XLAT_dm_op_buf(&args.buf[i], &cmp); - -#undef XLAT_dm_op_buf_HNDL_h - } - - rc =3D dm_op(&args); - - if ( rc =3D=3D -ERESTART ) - rc =3D hypercall_create_continuation(__HYPERVISOR_dm_op, "iih", - domid, nr_bufs, bufs); - - return rc; -} - -long do_dm_op(domid_t domid, - unsigned int nr_bufs, - XEN_GUEST_HANDLE_PARAM(xen_dm_op_buf_t) bufs) -{ - struct dmop_args args; - int rc; - - if ( nr_bufs > ARRAY_SIZE(args.buf) ) - return -E2BIG; - - args.domid =3D domid; - args.nr_bufs =3D array_index_nospec(nr_bufs, ARRAY_SIZE(args.buf) + 1); - - if ( copy_from_guest_offset(&args.buf[0], bufs, 0, args.nr_bufs) ) - return -EFAULT; - - rc =3D dm_op(&args); - - if ( rc =3D=3D -ERESTART ) - rc =3D hypercall_create_continuation(__HYPERVISOR_dm_op, "iih", - domid, nr_bufs, bufs); - return rc; } =20 diff --git a/xen/common/hvm/Makefile b/xen/common/hvm/Makefile index 326215d..335fcc9 100644 --- a/xen/common/hvm/Makefile +++ b/xen/common/hvm/Makefile @@ -1 +1,2 @@ +obj-y +=3D dm.o obj-y +=3D ioreq.o diff --git a/xen/common/hvm/dm.c b/xen/common/hvm/dm.c new file mode 100644 index 0000000..09e9542 --- /dev/null +++ b/xen/common/hvm/dm.c @@ -0,0 +1,287 @@ +/* + * Copyright (c) 2016 Citrix Systems Inc. + * Copyright (c) 2019 Arm ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#include +#include +#include +#include + +static int dm_op(const struct dmop_args *op_args) +{ + struct domain *d; + struct xen_dm_op op; + long rc; + bool const_op =3D true; + const size_t offset =3D offsetof(struct xen_dm_op, u); + + static const uint8_t op_size[] =3D { + [XEN_DMOP_create_ioreq_server] =3D sizeof(struct xen_= dm_op_create_ioreq_server), + [XEN_DMOP_get_ioreq_server_info] =3D sizeof(struct xen_= dm_op_get_ioreq_server_info), + [XEN_DMOP_map_io_range_to_ioreq_server] =3D sizeof(struct xen_= dm_op_ioreq_server_range), + [XEN_DMOP_unmap_io_range_from_ioreq_server] =3D sizeof(struct xen_= dm_op_ioreq_server_range), + [XEN_DMOP_set_ioreq_server_state] =3D sizeof(struct xen_= dm_op_set_ioreq_server_state), + [XEN_DMOP_destroy_ioreq_server] =3D sizeof(struct xen_= dm_op_destroy_ioreq_server), + [XEN_DMOP_track_dirty_vram] =3D sizeof(struct xen_= dm_op_track_dirty_vram), + [XEN_DMOP_set_pci_intx_level] =3D sizeof(struct xen_= dm_op_set_pci_intx_level), + [XEN_DMOP_set_isa_irq_level] =3D sizeof(struct xen_= dm_op_set_isa_irq_level), + [XEN_DMOP_set_pci_link_route] =3D sizeof(struct xen_= dm_op_set_pci_link_route), + [XEN_DMOP_modified_memory] =3D sizeof(struct xen_= dm_op_modified_memory), + [XEN_DMOP_set_mem_type] =3D sizeof(struct xen_= dm_op_set_mem_type), + [XEN_DMOP_inject_event] =3D sizeof(struct xen_= dm_op_inject_event), + [XEN_DMOP_inject_msi] =3D sizeof(struct xen_= dm_op_inject_msi), + [XEN_DMOP_map_mem_type_to_ioreq_server] =3D sizeof(struct xen_= dm_op_map_mem_type_to_ioreq_server), + [XEN_DMOP_remote_shutdown] =3D sizeof(struct xen_= dm_op_remote_shutdown), + [XEN_DMOP_relocate_memory] =3D sizeof(struct xen_= dm_op_relocate_memory), + [XEN_DMOP_pin_memory_cacheattr] =3D sizeof(struct xen_= dm_op_pin_memory_cacheattr), + }; + + rc =3D rcu_lock_remote_domain_by_id(op_args->domid, &d); + if ( rc ) + return rc; + + if ( !is_hvm_domain(d) ) + goto out; + + rc =3D xsm_dm_op(XSM_DM_PRIV, d); + if ( rc ) + goto out; + + rc =3D -EFAULT; + if ( op_args->buf[0].size < offset ) + goto out; + + if ( copy_from_guest_offset((void *)&op, op_args->buf[0].h, 0, offset)= ) + goto out; + + if ( op.op >=3D ARRAY_SIZE(op_size) ) + { + rc =3D -EOPNOTSUPP; + goto out; + } + + op.op =3D array_index_nospec(op.op, ARRAY_SIZE(op_size)); + + if ( op_args->buf[0].size < offset + op_size[op.op] ) + goto out; + + if ( copy_from_guest_offset((void *)&op.u, op_args->buf[0].h, offset, + op_size[op.op]) ) + goto out; + + rc =3D -EINVAL; + if ( op.pad ) + goto out; + + switch ( op.op ) + { + case XEN_DMOP_create_ioreq_server: + { + struct xen_dm_op_create_ioreq_server *data =3D + &op.u.create_ioreq_server; + + const_op =3D false; + + rc =3D -EINVAL; + if ( data->pad[0] || data->pad[1] || data->pad[2] ) + break; + + rc =3D hvm_create_ioreq_server(d, data->handle_bufioreq, + &data->id); + break; + } + + case XEN_DMOP_get_ioreq_server_info: + { + struct xen_dm_op_get_ioreq_server_info *data =3D + &op.u.get_ioreq_server_info; + const uint16_t valid_flags =3D XEN_DMOP_no_gfns; + + const_op =3D false; + + rc =3D -EINVAL; + if ( data->flags & ~valid_flags ) + break; + + rc =3D hvm_get_ioreq_server_info(d, data->id, + (data->flags & XEN_DMOP_no_gfns) ? + NULL : (unsigned long *)&data->iore= q_gfn, + (data->flags & XEN_DMOP_no_gfns) ? + NULL : (unsigned long *)&data->bufi= oreq_gfn, + &data->bufioreq_port); + break; + } + + case XEN_DMOP_map_io_range_to_ioreq_server: + { + const struct xen_dm_op_ioreq_server_range *data =3D + &op.u.map_io_range_to_ioreq_server; + + rc =3D -EINVAL; + if ( data->pad ) + break; + + rc =3D hvm_map_io_range_to_ioreq_server(d, data->id, data->type, + data->start, data->end); + break; + } + + case XEN_DMOP_unmap_io_range_from_ioreq_server: + { + const struct xen_dm_op_ioreq_server_range *data =3D + &op.u.unmap_io_range_from_ioreq_server; + + rc =3D -EINVAL; + if ( data->pad ) + break; + + rc =3D hvm_unmap_io_range_from_ioreq_server(d, data->id, data->typ= e, + data->start, data->end); + break; + } + + case XEN_DMOP_set_ioreq_server_state: + { + const struct xen_dm_op_set_ioreq_server_state *data =3D + &op.u.set_ioreq_server_state; + + rc =3D -EINVAL; + if ( data->pad ) + break; + + rc =3D hvm_set_ioreq_server_state(d, data->id, !!data->enabled); + break; + } + + case XEN_DMOP_destroy_ioreq_server: + { + const struct xen_dm_op_destroy_ioreq_server *data =3D + &op.u.destroy_ioreq_server; + + rc =3D -EINVAL; + if ( data->pad ) + break; + + rc =3D hvm_destroy_ioreq_server(d, data->id); + break; + } + + default: + rc =3D arch_dm_op(&op, d, op_args, &const_op); + } + + if ( (!rc || rc =3D=3D -ERESTART) && + !const_op && copy_to_guest_offset(op_args->buf[0].h, offset, + (void *)&op.u, op_size[op.op]) ) + rc =3D -EFAULT; + + out: + rcu_unlock_domain(d); + + return rc; +} + +#ifdef CONFIG_COMPAT +CHECK_dm_op_create_ioreq_server; +CHECK_dm_op_get_ioreq_server_info; +CHECK_dm_op_ioreq_server_range; +CHECK_dm_op_set_ioreq_server_state; +CHECK_dm_op_destroy_ioreq_server; +CHECK_dm_op_track_dirty_vram; +CHECK_dm_op_set_pci_intx_level; +CHECK_dm_op_set_isa_irq_level; +CHECK_dm_op_set_pci_link_route; +CHECK_dm_op_modified_memory; +CHECK_dm_op_set_mem_type; +CHECK_dm_op_inject_event; +CHECK_dm_op_inject_msi; +CHECK_dm_op_remote_shutdown; +CHECK_dm_op_relocate_memory; +CHECK_dm_op_pin_memory_cacheattr; + +int compat_dm_op(domid_t domid, + unsigned int nr_bufs, + XEN_GUEST_HANDLE_PARAM(void) bufs) +{ + struct dmop_args args; + unsigned int i; + int rc; + + if ( nr_bufs > ARRAY_SIZE(args.buf) ) + return -E2BIG; + + args.domid =3D domid; + args.nr_bufs =3D array_index_nospec(nr_bufs, ARRAY_SIZE(args.buf) + 1); + + for ( i =3D 0; i < args.nr_bufs; i++ ) + { + struct compat_dm_op_buf cmp; + + if ( copy_from_guest_offset(&cmp, bufs, i, 1) ) + return -EFAULT; + +#define XLAT_dm_op_buf_HNDL_h(_d_, _s_) \ + guest_from_compat_handle((_d_)->h, (_s_)->h) + + XLAT_dm_op_buf(&args.buf[i], &cmp); + +#undef XLAT_dm_op_buf_HNDL_h + } + + rc =3D dm_op(&args); + + if ( rc =3D=3D -ERESTART ) + rc =3D hypercall_create_continuation(__HYPERVISOR_dm_op, "iih", + domid, nr_bufs, bufs); + + return rc; +} +#endif + +long do_dm_op(domid_t domid, + unsigned int nr_bufs, + XEN_GUEST_HANDLE_PARAM(xen_dm_op_buf_t) bufs) +{ + struct dmop_args args; + int rc; + + if ( nr_bufs > ARRAY_SIZE(args.buf) ) + return -E2BIG; + + args.domid =3D domid; + args.nr_bufs =3D array_index_nospec(nr_bufs, ARRAY_SIZE(args.buf) + 1); + + if ( copy_from_guest_offset(&args.buf[0], bufs, 0, args.nr_bufs) ) + return -EFAULT; + + rc =3D dm_op(&args); + + if ( rc =3D=3D -ERESTART ) + rc =3D hypercall_create_continuation(__HYPERVISOR_dm_op, "iih", + domid, nr_bufs, bufs); + + return rc; +} + +/* + * 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/xen/hypercall.h b/xen/include/xen/hypercall.h index 655acc7..19f509f 100644 --- a/xen/include/xen/hypercall.h +++ b/xen/include/xen/hypercall.h @@ -150,6 +150,18 @@ do_dm_op( unsigned int nr_bufs, XEN_GUEST_HANDLE_PARAM(xen_dm_op_buf_t) bufs); =20 +struct dmop_args { + domid_t domid; + unsigned int nr_bufs; + /* Reserve enough buf elements for all current hypercalls. */ + struct xen_dm_op_buf buf[2]; +}; + +int arch_dm_op(struct xen_dm_op *op, + struct domain *d, + const struct dmop_args *op_args, + bool *const_op); + #ifdef CONFIG_HYPFS extern long do_hypfs_op( --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478943; cv=none; d=zohomail.com; s=zohoarc; b=kzzR4XmMkbujXSaVO6nOhLP5g6ykf6Y7KMaLz+EiPLVkiGQHX7jl4YYIAoQezxUpDaRrdxP7EFif9U8Z/a6wM3b2FiLnHVYhixy16NQ48emWUKCzJ2KB0i/FUR15Iu+j77+wQULEDjlWIumOZoTm+qSP39yflWB/bw8ogNZsht4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478943; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=oslIVtwtHBl5F3ZhQAPxrDGKrXRg4UKsfSDdvc5ciUQ=; b=XtcdEPwmBWz3fEdw17orz/vCiiHzbnGlNXCeyYtNa7z1GTVfvVBRBnYhF8fnyFzcNrU+3CWfGtW4jysmnoXPhopFtz5OFitZgoRdOaJvjqtLNkmScjjxxmO2KJhMTS/7RVJFHAv8NmcG77Jb7sZFJUZF/s6QO/h7kqBI2KXXy1Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 159647894353097.76218675195344; Mon, 3 Aug 2020 11:22:23 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f5x-0008Fz-4r; Mon, 03 Aug 2020 18:21:57 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f5w-0008F4-6W for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:21:56 +0000 Received: from mail-lf1-x141.google.com (unknown [2a00:1450:4864:20::141]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3188cb0d-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:48 +0000 (UTC) Received: by mail-lf1-x141.google.com with SMTP id b11so20987266lfe.10 for ; Mon, 03 Aug 2020 11:21:48 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:46 -0700 (PDT) X-Inumbo-ID: 3188cb0d-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=oslIVtwtHBl5F3ZhQAPxrDGKrXRg4UKsfSDdvc5ciUQ=; b=XjS6U4v4PSxa1s3IfmNtC3cWuXvF7nIUasRVK2guc6ntXJJAHaL0mrU8joAIIvYweP 5THry527m+O7psZ3j7XNum5igWNb7Bik4NYBcmwLcCeHzrbOA948vb2hLndQ+QRQ5M/9 reN+IcpKVZaQlgxIXKL4XPnXEYNM+Rbz7xguH3dWq3++rLoQ5qsulqsDGbpU/WClvzrw v5Bvw3RV0xj+qh+5ooSIpQVZsUDGuVmG/zZdlWdTWTzTHVFKhnvKmDlnanVA+MHQkcyA nuZlQ+Nsx9tUO9qy0gGabRQZScyHIKApfOj8UHxkBtyK+C4jgQXQfEOdQp/61KXD20Zh hD9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=oslIVtwtHBl5F3ZhQAPxrDGKrXRg4UKsfSDdvc5ciUQ=; b=dH5TERHIS5J4t7mAvwYWbeMaKLtdUn13HGsitaVP1A5Ke9XnZEf83j2cbJVikD/W9E Ye8is4a3eZS4rzv6y7nNb1ubrtYmj+5hN3lL34gD6f/FPMAAhQ+1Cjdu2VPtxpSBo065 Kz2AQhXJ1CnG9/ZRmTYBXv0cnQ+o6GhudJ2pzmrKa2BVXsn8vFFCZ9kUxGNQ4pq0PK72 PYq1wpd4fvUtS18I3QKTJ+2JvrTAfto7eQ/+36dO5qjZPxqimUsGCP0+3f/4xEUxU5C5 Gc8NpRaKU2yPq6Y3ScqPB4E/mnybEsZftdBvJit2cPZMkhqy7nL8U4iuU04TsLLP/QCC C14w== X-Gm-Message-State: AOAM533KYj4iQv7XB4900AyXFVtFRt4HY/FAjcuOtmL7Ch7G//QaT1+P epEIiIArnEvKoIVJBefJzIo3zS5zm20= X-Google-Smtp-Source: ABdhPJxOLAttpeuy9/RgN7qjnqDLVPrgno1Gy+KRXHv9MzMDEQy+mv7bT2weMP+GR17sWoTcH/sXyg== X-Received: by 2002:ac2:46d0:: with SMTP id p16mr9369899lfo.142.1596478907282; Mon, 03 Aug 2020 11:21:47 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 03/12] xen/mm: Make x86's XENMEM_resource_ioreq_server handling common Date: Mon, 3 Aug 2020 21:21:19 +0300 Message-Id: <1596478888-23030-4-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Andrew Cooper , Ian Jackson , George Dunlap , Oleksandr Tyshchenko , Julien Grall , Jan Beulich , Volodymyr Babchuk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko As x86 implementation of XENMEM_resource_ioreq_server can be re-used on Arm later on, this patch makes it common and removes arch_acquire_resource as unneeded. This support is going to be used on Arm to be able run device emulator outside of Xen hypervisor. Please note, this is a split/cleanup of Julien's PoC: "Add support for Guest IO forwarding to a device emulator" Signed-off-by: Julien Grall Signed-off-by: Oleksandr Tyshchenko --- xen/arch/x86/mm.c | 45 --------------------------------------------- xen/common/memory.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- xen/include/asm-arm/mm.h | 8 -------- xen/include/asm-x86/mm.h | 4 ---- 4 files changed, 43 insertions(+), 59 deletions(-) diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index 2b06e15..33238d0 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -99,7 +99,6 @@ * doing the final put_page(), and remove it from the iommu if so. */ =20 -#include #include #include #include @@ -4600,50 +4599,6 @@ int xenmem_add_to_physmap_one( return rc; } =20 -int arch_acquire_resource(struct domain *d, unsigned int type, - unsigned int id, unsigned long frame, - unsigned int nr_frames, xen_pfn_t mfn_list[]) -{ - int rc; - - switch ( type ) - { -#ifdef CONFIG_HVM - case XENMEM_resource_ioreq_server: - { - ioservid_t ioservid =3D id; - unsigned int i; - - rc =3D -EINVAL; - if ( !is_hvm_domain(d) ) - break; - - if ( id !=3D (unsigned int)ioservid ) - break; - - rc =3D 0; - for ( i =3D 0; i < nr_frames; i++ ) - { - mfn_t mfn; - - rc =3D hvm_get_ioreq_server_frame(d, id, frame + i, &mfn); - if ( rc ) - break; - - mfn_list[i] =3D mfn_x(mfn); - } - break; - } -#endif - - default: - rc =3D -EOPNOTSUPP; - break; - } - - return rc; -} - long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) { int rc; diff --git a/xen/common/memory.c b/xen/common/memory.c index 714077c..9283e5e 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -30,6 +30,10 @@ #include #include =20 +#ifdef CONFIG_IOREQ_SERVER +#include +#endif + #ifdef CONFIG_X86 #include #endif @@ -1045,6 +1049,38 @@ static int acquire_grant_table(struct domain *d, uns= igned int id, return 0; } =20 +#ifdef CONFIG_IOREQ_SERVER +static int acquire_ioreq_server(struct domain *d, + unsigned int id, + unsigned long frame, + unsigned int nr_frames, + xen_pfn_t mfn_list[]) +{ + ioservid_t ioservid =3D id; + unsigned int i; + int rc; + + if ( !is_hvm_domain(d) ) + return -EINVAL; + + if ( id !=3D (unsigned int)ioservid ) + return -EINVAL; + + for ( i =3D 0; i < nr_frames; i++ ) + { + mfn_t mfn; + + rc =3D hvm_get_ioreq_server_frame(d, id, frame + i, &mfn); + if ( rc ) + return rc; + + mfn_list[i] =3D mfn_x(mfn); + } + + return 0; +} +#endif + static int acquire_resource( XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg) { @@ -1095,9 +1131,14 @@ static int acquire_resource( mfn_list); break; =20 +#ifdef CONFIG_IOREQ_SERVER + case XENMEM_resource_ioreq_server: + rc =3D acquire_ioreq_server(d, xmar.id, xmar.frame, xmar.nr_frames, + mfn_list); + break; +#endif default: - rc =3D arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame, - xmar.nr_frames, mfn_list); + rc =3D -EOPNOTSUPP; break; } =20 diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h index f8ba49b..0b7de31 100644 --- a/xen/include/asm-arm/mm.h +++ b/xen/include/asm-arm/mm.h @@ -358,14 +358,6 @@ static inline void put_page_and_type(struct page_info = *page) =20 void clear_and_clean_page(struct page_info *page); =20 -static inline -int arch_acquire_resource(struct domain *d, unsigned int type, unsigned in= t id, - unsigned long frame, unsigned int nr_frames, - xen_pfn_t mfn_list[]) -{ - return -EOPNOTSUPP; -} - unsigned int arch_get_dma_bitsize(void); =20 #endif /* __ARCH_ARM_MM__ */ diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h index 7e74996..2e111ad 100644 --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -649,8 +649,4 @@ static inline bool arch_mfn_in_directmap(unsigned long = mfn) return mfn <=3D (virt_to_mfn(eva - 1) + 1); } =20 -int arch_acquire_resource(struct domain *d, unsigned int type, - unsigned int id, unsigned long frame, - unsigned int nr_frames, xen_pfn_t mfn_list[]); - #endif /* __ASM_X86_MM_H__ */ --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478950; cv=none; d=zohomail.com; s=zohoarc; b=JA41UdCP61xuc5Ik1p23LOWcxGWVOpoS5RZwzuNjS0r/vfJgdODJVdqTeN+ZIi5g4Gp1+X2XByulZBtC9vxuK5q528HZOgjeYg+1w+zhpnGrvvpH2D6gcDLE5+Rt96E1ta3cxPZDsfWQu8gnm5dCIQx+ILl83yWAA367PMSrPJc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478950; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=VIKpZkxDN7eQd+zJAH4AgFtCzYqVcctBBddhzvbPXqo=; b=IBEJx7neRgEvI3cg0uWciQinxrwFZoNTU7Qldbk9myNRkcYCDtmBv7Lx+UJqUxSqMO4rojYqB9mqBwQ73B/fanGFBClgeISbF3u+T9YXru0xitojtAWYzOcXSP5OYeaV29unN8bwHtw9g5fnBV5eN791RUoicMNhWdcr+hrwlvQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596478950453596.2970110025653; Mon, 3 Aug 2020 11:22:30 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f66-0008Ii-Ty; Mon, 03 Aug 2020 18:22:06 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f66-0008F4-6y for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:06 +0000 Received: from mail-lj1-x242.google.com (unknown [2a00:1450:4864:20::242]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 32a454ac-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:50 +0000 (UTC) Received: by mail-lj1-x242.google.com with SMTP id x9so40753131ljc.5 for ; Mon, 03 Aug 2020 11:21:50 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:48 -0700 (PDT) X-Inumbo-ID: 32a454ac-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=VIKpZkxDN7eQd+zJAH4AgFtCzYqVcctBBddhzvbPXqo=; b=e3j1/HkO5R1dXvlgaiWw5rxSTHJNvfO+l/S5WMMyuCpZT87m6Yf1MuLXhh7PY9EnyD 1MGTQ0pNfMRuS/r5cVGQ1Zvx0z0V1D2I3+F+8d8JXrnXvQUxzmVphWWnJ/Tg3gbnRWbf cmGIx8NhkU0LyA3pVWBrW7C3WhUcWjxMGmzu9+L+SpY9rmHoAbGVqBk4AK1ElrudJRxT m5Z8SetlPA0SefvIYsKN0lmNhoLc/DZJP5fMdsxY+JMflv2Q19W8opgMzLdIyXIdBz03 05qzhXPYECXlvfzlqXWSyKC9CQxiB4GE/PIIovF+KUx4+JJfziMEcQnE2zMP7UAChF+2 D0dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=VIKpZkxDN7eQd+zJAH4AgFtCzYqVcctBBddhzvbPXqo=; b=IO6HgMFg8Ul7SRYbre/ySRS8mqEKQZ1EXdrofh/IdWJ+8PsHXXzUXnhNgYsSG98iHJ Th/0+XeiG4s5XfPZmNy+5MIZBr42XZVQRaFZ3GoZ2YqIxp4P7as7mw9Bn5S2eJZtsUXR hdTKhHXOJ97euwtycajj82jbKppwarYv5dCKotl9dSL1mo9Xg2BDjMhnXQ5ccpwHge9V H5UyHOr1WcsxjQ8okPO1Biv8psR+SC49VQDC7SkIOSRJJqPgriJ845qIbX0LNHp/BdKS qPxBmElkPXWDl+sOsMFV1dNE5dxaKIVfKSa7akPE6QPA5Fn59GM+m0eSk0vQR+yfq76e XkgQ== X-Gm-Message-State: AOAM531bq8jzLcyTl9XstEyFlGNyxL9MFFIIz0g3oaMHyHajxHDe78/t WKImdz+QZCOIlnfsYWZrdOeWy/cZCFE= X-Google-Smtp-Source: ABdhPJyPm5Z4kwcrUWIHRyaRXM9q/twQEsQ+GzpFpbwRFecJWQKGo/TF3Ea+USDqHv7zgWezKEs7Uw== X-Received: by 2002:a2e:8346:: with SMTP id l6mr8686276ljh.0.1596478908667; Mon, 03 Aug 2020 11:21:48 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 04/12] xen/arm: Introduce arch specific bits for IOREQ/DM features Date: Mon, 3 Aug 2020 21:21:20 +0300 Message-Id: <1596478888-23030-5-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Andrew Cooper , Ian Jackson , George Dunlap , Oleksandr Tyshchenko , Julien Grall , Jan Beulich , Daniel De Graaf , Volodymyr Babchuk Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko This patch makes possible to forward Guest MMIO accesses to a device emulator on Arm and enables that support for Arm64. Also update XSM code a bit to let DM op be used on Arm. New arch DM op will be introduced in the follow-up patch. Please note, at the moment build on Arm32 is broken (see cmpxchg usage in hvm_send_buffered_ioreq()) if someone wants to enable CONFIG_IOREQ_SERVER due to the lack of cmpxchg_64 support on Arm32. Please note, this is a split/cleanup of Julien's PoC: "Add support for Guest IO forwarding to a device emulator" Signed-off-by: Julien Grall Signed-off-by: Oleksandr Tyshchenko --- tools/libxc/xc_dom_arm.c | 25 +++++++--- xen/arch/arm/Kconfig | 1 + xen/arch/arm/Makefile | 2 + xen/arch/arm/dm.c | 34 +++++++++++++ xen/arch/arm/domain.c | 9 ++++ xen/arch/arm/hvm.c | 46 +++++++++++++++++- xen/arch/arm/io.c | 67 +++++++++++++++++++++++++- xen/arch/arm/ioreq.c | 86 +++++++++++++++++++++++++++++++++ xen/arch/arm/traps.c | 17 +++++++ xen/common/memory.c | 5 +- xen/include/asm-arm/domain.h | 80 +++++++++++++++++++++++++++++++ xen/include/asm-arm/hvm/ioreq.h | 103 ++++++++++++++++++++++++++++++++++++= ++++ xen/include/asm-arm/mmio.h | 1 + xen/include/asm-arm/p2m.h | 7 +-- xen/include/xsm/dummy.h | 4 +- xen/include/xsm/xsm.h | 6 +-- xen/xsm/dummy.c | 2 +- xen/xsm/flask/hooks.c | 5 +- 18 files changed, 476 insertions(+), 24 deletions(-) create mode 100644 xen/arch/arm/dm.c create mode 100644 xen/arch/arm/ioreq.c create mode 100644 xen/include/asm-arm/hvm/ioreq.h diff --git a/tools/libxc/xc_dom_arm.c b/tools/libxc/xc_dom_arm.c index 931404c..b5fc066 100644 --- a/tools/libxc/xc_dom_arm.c +++ b/tools/libxc/xc_dom_arm.c @@ -26,11 +26,19 @@ #include "xg_private.h" #include "xc_dom.h" =20 -#define NR_MAGIC_PAGES 4 + #define CONSOLE_PFN_OFFSET 0 #define XENSTORE_PFN_OFFSET 1 #define MEMACCESS_PFN_OFFSET 2 #define VUART_PFN_OFFSET 3 +#define IOREQ_SERVER_PFN_OFFSET 4 + +#define NR_IOREQ_SERVER_PAGES 8 +#define NR_MAGIC_PAGES (4 + NR_IOREQ_SERVER_PAGES) + +#define GUEST_MAGIC_BASE_PFN (GUEST_MAGIC_BASE >> XC_PAGE_SHIFT) + +#define special_pfn(x) (GUEST_MAGIC_BASE_PFN + (x)) =20 #define LPAE_SHIFT 9 =20 @@ -51,7 +59,7 @@ const char *xc_domain_get_native_protocol(xc_interface *x= ch, static int alloc_magic_pages(struct xc_dom_image *dom) { int rc, i; - const xen_pfn_t base =3D GUEST_MAGIC_BASE >> XC_PAGE_SHIFT; + const xen_pfn_t base =3D special_pfn(0); xen_pfn_t p2m[NR_MAGIC_PAGES]; =20 BUILD_BUG_ON(NR_MAGIC_PAGES > GUEST_MAGIC_SIZE >> XC_PAGE_SHIFT); @@ -71,10 +79,9 @@ static int alloc_magic_pages(struct xc_dom_image *dom) dom->xenstore_pfn =3D base + XENSTORE_PFN_OFFSET; dom->vuart_gfn =3D base + VUART_PFN_OFFSET; =20 - xc_clear_domain_page(dom->xch, dom->guest_domid, dom->console_pfn); - xc_clear_domain_page(dom->xch, dom->guest_domid, dom->xenstore_pfn); - xc_clear_domain_page(dom->xch, dom->guest_domid, base + MEMACCESS_PFN_= OFFSET); - xc_clear_domain_page(dom->xch, dom->guest_domid, dom->vuart_gfn); + /* XXX: Check return */ + xc_clear_domain_pages(dom->xch, dom->guest_domid, special_pfn(0), + NR_MAGIC_PAGES); =20 xc_hvm_param_set(dom->xch, dom->guest_domid, HVM_PARAM_CONSOLE_PFN, dom->console_pfn); @@ -88,6 +95,12 @@ static int alloc_magic_pages(struct xc_dom_image *dom) xc_hvm_param_set(dom->xch, dom->guest_domid, HVM_PARAM_STORE_EVTCHN, dom->xenstore_evtchn); =20 + /* Tell the domain where the pages are and how many there are. */ + xc_hvm_param_set(dom->xch, dom->guest_domid, HVM_PARAM_IOREQ_SERVER_PF= N, + special_pfn(IOREQ_SERVER_PFN_OFFSET)); + xc_hvm_param_set(dom->xch, dom->guest_domid, HVM_PARAM_NR_IOREQ_SERVER= _PAGES, + NR_IOREQ_SERVER_PAGES); + return 0; } =20 diff --git a/xen/arch/arm/Kconfig b/xen/arch/arm/Kconfig index 2777388..6b8a969 100644 --- a/xen/arch/arm/Kconfig +++ b/xen/arch/arm/Kconfig @@ -13,6 +13,7 @@ config ARM_64 def_bool y depends on 64BIT select HAS_FAST_MULTIPLY + select IOREQ_SERVER =20 config ARM def_bool y diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile index 7e82b21..617fa3e 100644 --- a/xen/arch/arm/Makefile +++ b/xen/arch/arm/Makefile @@ -13,6 +13,7 @@ obj-y +=3D cpuerrata.o obj-y +=3D cpufeature.o obj-y +=3D decode.o obj-y +=3D device.o +obj-$(CONFIG_IOREQ_SERVER) +=3D dm.o obj-y +=3D domain.o obj-y +=3D domain_build.init.o obj-y +=3D domctl.o @@ -27,6 +28,7 @@ obj-y +=3D guest_atomics.o obj-y +=3D guest_walk.o obj-y +=3D hvm.o obj-y +=3D io.o +obj-$(CONFIG_IOREQ_SERVER) +=3D ioreq.o obj-y +=3D irq.o obj-y +=3D kernel.init.o obj-$(CONFIG_LIVEPATCH) +=3D livepatch.o diff --git a/xen/arch/arm/dm.c b/xen/arch/arm/dm.c new file mode 100644 index 0000000..2437099 --- /dev/null +++ b/xen/arch/arm/dm.c @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 Arm ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#include +#include + +int arch_dm_op(struct xen_dm_op *op, struct domain *d, + const struct dmop_args *op_args, bool *const_op) +{ + return -EOPNOTSUPP; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/arm/domain.c b/xen/arch/arm/domain.c index 3116932..658eec0 100644 --- a/xen/arch/arm/domain.c +++ b/xen/arch/arm/domain.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -681,6 +682,10 @@ int arch_domain_create(struct domain *d, =20 ASSERT(config !=3D NULL); =20 +#ifdef CONFIG_IOREQ_SERVER + hvm_ioreq_init(d); +#endif + /* p2m_init relies on some value initialized by the IOMMU subsystem */ if ( (rc =3D iommu_domain_init(d, config->iommu_opts)) !=3D 0 ) goto fail; @@ -999,6 +1004,10 @@ int domain_relinquish_resources(struct domain *d) if (ret ) return ret; =20 +#ifdef CONFIG_IOREQ_SERVER + hvm_destroy_all_ioreq_servers(d); +#endif + PROGRESS(xen): ret =3D relinquish_memory(d, &d->xenpage_list); if ( ret ) diff --git a/xen/arch/arm/hvm.c b/xen/arch/arm/hvm.c index 8951b34..0379493 100644 --- a/xen/arch/arm/hvm.c +++ b/xen/arch/arm/hvm.c @@ -51,6 +51,14 @@ static int hvm_allow_set_param(const struct domain *d, u= nsigned int param) case HVM_PARAM_MONITOR_RING_PFN: return d =3D=3D current->domain ? -EPERM : 0; =20 + /* + * XXX: Do we need to follow x86's logic here: + * "The following parameters should only be changed once"? + */ + case HVM_PARAM_IOREQ_SERVER_PFN: + case HVM_PARAM_NR_IOREQ_SERVER_PAGES: + return 0; + /* Writeable only by Xen, hole, deprecated, or out-of-range. */ default: return -EINVAL; @@ -69,6 +77,11 @@ static int hvm_allow_get_param(const struct domain *d, u= nsigned int param) case HVM_PARAM_CONSOLE_EVTCHN: return 0; =20 + /* XXX: Could these be read by someone? What policy to apply? */ + case HVM_PARAM_IOREQ_SERVER_PFN: + case HVM_PARAM_NR_IOREQ_SERVER_PAGES: + return 0; + /* * The following parameters are intended for toolstack usage only. * They may not be read by the domain. @@ -82,6 +95,37 @@ static int hvm_allow_get_param(const struct domain *d, u= nsigned int param) } } =20 +static int hvmop_set_param(struct domain *d, const struct xen_hvm_param *a) +{ + int rc =3D 0; + + switch ( a->index ) + { + case HVM_PARAM_IOREQ_SERVER_PFN: + d->arch.hvm.ioreq_gfn.base =3D a->value; + break; + case HVM_PARAM_NR_IOREQ_SERVER_PAGES: + { + unsigned int i; + + if ( a->value =3D=3D 0 || + a->value > sizeof(d->arch.hvm.ioreq_gfn.mask) * 8 ) + { + rc =3D -EINVAL; + break; + } + for ( i =3D 0; i < a->value; i++ ) + set_bit(i, &d->arch.hvm.ioreq_gfn.mask); + + break; + } + } + + d->arch.hvm.params[a->index] =3D a->value; + + return rc; +} + long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM(void) arg) { long rc =3D 0; @@ -111,7 +155,7 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE_PARAM= (void) arg) if ( rc ) goto param_fail; =20 - d->arch.hvm.params[a.index] =3D a.value; + rc =3D hvmop_set_param(d, &a); } else { diff --git a/xen/arch/arm/io.c b/xen/arch/arm/io.c index ae7ef96..436f669 100644 --- a/xen/arch/arm/io.c +++ b/xen/arch/arm/io.c @@ -16,6 +16,7 @@ * GNU General Public License for more details. */ =20 +#include #include #include #include @@ -107,6 +108,62 @@ static const struct mmio_handler *find_mmio_handler(st= ruct domain *d, return handler; } =20 +#ifdef CONFIG_IOREQ_SERVER +static enum io_state try_fwd_ioserv(struct cpu_user_regs *regs, + struct vcpu *v, mmio_info_t *info) +{ + struct hvm_vcpu_io *vio =3D &v->arch.hvm.hvm_io; + ioreq_t p =3D { + .type =3D IOREQ_TYPE_COPY, + .addr =3D info->gpa, + .size =3D 1 << info->dabt.size, + .count =3D 0, + .dir =3D !info->dabt.write, + .df =3D 0, /* XXX: What's for? */ + .data =3D get_user_reg(regs, info->dabt.reg), + .state =3D STATE_IOREQ_READY, + }; + struct hvm_ioreq_server *s =3D NULL; + enum io_state rc; + + switch ( vio->io_req.state ) + { + case STATE_IOREQ_NONE: + break; + default: + printk("d%u wrong state %u\n", v->domain->domain_id, + vio->io_req.state); + return IO_ABORT; + } + + s =3D hvm_select_ioreq_server(v->domain, &p); + if ( !s ) + return IO_UNHANDLED; + + if ( !info->dabt.valid ) + { + printk("Valid bit not set\n"); + return IO_ABORT; + } + + vio->io_req =3D p; + + rc =3D hvm_send_ioreq(s, &p, 0); + if ( rc !=3D IO_RETRY || v->domain->is_shutting_down ) + vio->io_req.state =3D STATE_IOREQ_NONE; + else if ( !hvm_ioreq_needs_completion(&vio->io_req) ) + rc =3D IO_HANDLED; + else + vio->io_completion =3D HVMIO_mmio_completion; + + /* XXX: Decide what to do */ + if ( rc =3D=3D IO_RETRY ) + rc =3D IO_HANDLED; + + return rc; +} +#endif + enum io_state try_handle_mmio(struct cpu_user_regs *regs, const union hsr hsr, paddr_t gpa) @@ -123,7 +180,15 @@ enum io_state try_handle_mmio(struct cpu_user_regs *re= gs, =20 handler =3D find_mmio_handler(v->domain, info.gpa); if ( !handler ) - return IO_UNHANDLED; + { + int rc =3D IO_UNHANDLED; + +#ifdef CONFIG_IOREQ_SERVER + rc =3D try_fwd_ioserv(regs, v, &info); +#endif + + return rc; + } =20 /* All the instructions used on emulated MMIO region should be valid */ if ( !dabt.valid ) diff --git a/xen/arch/arm/ioreq.c b/xen/arch/arm/ioreq.c new file mode 100644 index 0000000..a9cc839 --- /dev/null +++ b/xen/arch/arm/ioreq.c @@ -0,0 +1,86 @@ +/* + * arm/ioreq.c: hardware virtual machine I/O emulation + * + * Copyright (c) 2019 Arm ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +bool handle_mmio(void) +{ + struct vcpu *v =3D current; + struct cpu_user_regs *regs =3D guest_cpu_user_regs(); + const union hsr hsr =3D { .bits =3D regs->hsr }; + const struct hsr_dabt dabt =3D hsr.dabt; + /* Code is similar to handle_read */ + uint8_t size =3D (1 << dabt.size) * 8; + register_t r =3D v->arch.hvm.hvm_io.io_req.data; + + /* We should only be here on Guest Data Abort */ + ASSERT(dabt.ec =3D=3D HSR_EC_DATA_ABORT_LOWER_EL); + + /* We are done with the IO */ + /* XXX: Is it the right place? */ + v->arch.hvm.hvm_io.io_req.state =3D STATE_IOREQ_NONE; + + /* XXX: Do we need to take care of write here ? */ + if ( dabt.write ) + return true; + + /* + * Sign extend if required. + * Note that we expect the read handler to have zeroed the bits + * outside the requested access size. + */ + if ( dabt.sign && (r & (1UL << (size - 1))) ) + { + /* + * We are relying on register_t using the same as + * an unsigned long in order to keep the 32-bit assembly + * code smaller. + */ + BUILD_BUG_ON(sizeof(register_t) !=3D sizeof(unsigned long)); + r |=3D (~0UL) << size; + } + + set_user_reg(regs, dabt.reg, r); + + return true; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/arm/traps.c b/xen/arch/arm/traps.c index 8f40d0e..4cdf098 100644 --- a/xen/arch/arm/traps.c +++ b/xen/arch/arm/traps.c @@ -18,6 +18,7 @@ =20 #include #include +#include #include #include #include @@ -1384,6 +1385,9 @@ static arm_hypercall_t arm_hypercall_table[] =3D { #ifdef CONFIG_HYPFS HYPERCALL(hypfs_op, 5), #endif +#ifdef CONFIG_IOREQ_SERVER + HYPERCALL(dm_op, 3), +#endif }; =20 #ifndef NDEBUG @@ -1958,6 +1962,9 @@ static void do_trap_stage2_abort_guest(struct cpu_use= r_regs *regs, case IO_UNHANDLED: /* IO unhandled, try another way to handle it. */ break; + default: + /* XXX: Handle IO_RETRY */ + ASSERT_UNREACHABLE(); } } =20 @@ -2275,6 +2282,16 @@ static void check_for_vcpu_work(void) */ void leave_hypervisor_to_guest(void) { +#ifdef CONFIG_IOREQ_SERVER + /* + * XXX: Check the return. Shall we call that in + * continue_running and context_switch instead? + * The benefits would be to avoid calling + * handle_hvm_io_completion on every return. + */ + local_irq_enable(); + handle_hvm_io_completion(current); +#endif local_irq_disable(); =20 check_for_vcpu_work(); diff --git a/xen/common/memory.c b/xen/common/memory.c index 9283e5e..0000477 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -8,6 +8,7 @@ */ =20 #include +#include #include #include #include @@ -30,10 +31,6 @@ #include #include =20 -#ifdef CONFIG_IOREQ_SERVER -#include -#endif - #ifdef CONFIG_X86 #include #endif diff --git a/xen/include/asm-arm/domain.h b/xen/include/asm-arm/domain.h index 4e2f582..e060b0a 100644 --- a/xen/include/asm-arm/domain.h +++ b/xen/include/asm-arm/domain.h @@ -11,12 +11,64 @@ #include #include #include +#include +#include #include #include =20 +struct hvm_ioreq_page { + gfn_t gfn; + struct page_info *page; + void *va; +}; + +struct hvm_ioreq_vcpu { + struct list_head list_entry; + struct vcpu *vcpu; + evtchn_port_t ioreq_evtchn; + bool pending; +}; + +#define NR_IO_RANGE_TYPES (XEN_DMOP_IO_RANGE_PCI + 1) +#define MAX_NR_IO_RANGES 256 + +#define MAX_NR_IOREQ_SERVERS 8 +#define DEFAULT_IOSERVID 0 + +struct hvm_ioreq_server { + struct domain *target, *emulator; + + /* Lock to serialize toolstack modifications */ + spinlock_t lock; + + struct hvm_ioreq_page ioreq; + struct list_head ioreq_vcpu_list; + struct hvm_ioreq_page bufioreq; + + /* Lock to serialize access to buffered ioreq ring */ + spinlock_t bufioreq_lock; + evtchn_port_t bufioreq_evtchn; + struct rangeset *range[NR_IO_RANGE_TYPES]; + bool enabled; + uint8_t bufioreq_handling; +}; + struct hvm_domain { uint64_t params[HVM_NR_PARAMS]; + + /* Guest page range used for non-default ioreq servers */ + struct { + unsigned long base; + unsigned long mask; + unsigned long legacy_mask; /* indexed by HVM param number */ + } ioreq_gfn; + + /* Lock protects all other values in the sub-struct and the default */ + struct { + spinlock_t lock; + struct hvm_ioreq_server *server[MAX_NR_IOREQ_SERVERS]; + } ioreq_server; }; =20 #ifdef CONFIG_ARM_64 @@ -93,6 +145,29 @@ struct arch_domain #endif } __cacheline_aligned; =20 +enum hvm_io_completion { + HVMIO_no_completion, + HVMIO_mmio_completion, + HVMIO_pio_completion, + HVMIO_realmode_completion +}; + +struct hvm_vcpu_io { + /* I/O request in flight to device model. */ + enum hvm_io_completion io_completion; + ioreq_t io_req; + + /* + * HVM emulation: + * Linear address @mmio_gla maps to MMIO physical frame @mmio_gpfn. + * The latter is known to be an MMIO frame (not RAM). + * This translation is only valid for accesses as per @mmio_access. + */ + struct npfec mmio_access; + unsigned long mmio_gla; + unsigned long mmio_gpfn; +}; + struct arch_vcpu { struct { @@ -206,6 +281,11 @@ struct arch_vcpu */ bool need_flush_to_ram; =20 + struct hvm_vcpu + { + struct hvm_vcpu_io hvm_io; + } hvm; + } __cacheline_aligned; =20 void vcpu_show_execution_state(struct vcpu *); diff --git a/xen/include/asm-arm/hvm/ioreq.h b/xen/include/asm-arm/hvm/iore= q.h new file mode 100644 index 0000000..83a560c --- /dev/null +++ b/xen/include/asm-arm/hvm/ioreq.h @@ -0,0 +1,103 @@ +/* + * hvm.h: Hardware virtual machine assist interface definitions. + * + * Copyright (c) 2016 Citrix Systems Inc. + * Copyright (c) 2019 Arm ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program; If not, see . + */ + +#ifndef __ASM_ARM_HVM_IOREQ_H__ +#define __ASM_ARM_HVM_IOREQ_H__ + +#include +#include + +#define has_vpci(d) (false) + +bool handle_mmio(void); + +static inline bool handle_pio(uint16_t port, unsigned int size, int dir) +{ + /* XXX */ + BUG(); + return true; +} + +static inline paddr_t hvm_mmio_first_byte(const ioreq_t *p) +{ + return p->addr; +} + +static inline paddr_t hvm_mmio_last_byte(const ioreq_t *p) +{ + unsigned long size =3D p->size; + + return p->addr + size - 1; +} + +struct hvm_ioreq_server; + +static inline int p2m_set_ioreq_server(struct domain *d, + unsigned int flags, + struct hvm_ioreq_server *s) +{ + return -EOPNOTSUPP; +} + +static inline void msix_write_completion(struct vcpu *v) +{ +} + +static inline void handle_realmode_completion(void) +{ + ASSERT_UNREACHABLE(); +} + +static inline void paging_mark_pfn_dirty(struct domain *d, pfn_t pfn) +{ +} + +static inline void hvm_get_ioreq_server_range_type(struct domain *d, + ioreq_t *p, + uint8_t *type, + uint64_t *addr) +{ + *type =3D (p->type =3D=3D IOREQ_TYPE_PIO) ? + XEN_DMOP_IO_RANGE_PORT : XEN_DMOP_IO_RANGE_MEMORY; + *addr =3D p->addr; +} + +static inline void arch_hvm_ioreq_init(struct domain *d) +{ +} + +static inline void arch_hvm_ioreq_destroy(struct domain *d) +{ +} + +#define IOREQ_IO_HANDLED IO_HANDLED +#define IOREQ_IO_UNHANDLED IO_UNHANDLED +#define IOREQ_IO_RETRY IO_RETRY + +#endif /* __ASM_X86_HVM_IOREQ_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-arm/mmio.h b/xen/include/asm-arm/mmio.h index 8dbfb27..7ab873c 100644 --- a/xen/include/asm-arm/mmio.h +++ b/xen/include/asm-arm/mmio.h @@ -37,6 +37,7 @@ enum io_state IO_ABORT, /* The IO was handled by the helper and led to an abor= t. */ IO_HANDLED, /* The IO was successfully handled by the helper. */ IO_UNHANDLED, /* The IO was not handled by the helper. */ + IO_RETRY, /* Retry the emulation for some reason */ }; =20 typedef int (*mmio_read_t)(struct vcpu *v, mmio_info_t *info, diff --git a/xen/include/asm-arm/p2m.h b/xen/include/asm-arm/p2m.h index 5fdb6e8..5823f11 100644 --- a/xen/include/asm-arm/p2m.h +++ b/xen/include/asm-arm/p2m.h @@ -385,10 +385,11 @@ static inline int set_foreign_p2m_entry(struct domain= *d, unsigned long gfn, mfn_t mfn) { /* - * NOTE: If this is implemented then proper reference counting of - * foreign entries will need to be implemented. + * XXX: handle properly reference. It looks like the page may not alwa= ys + * belong to d. */ - return -EOPNOTSUPP; + + return guest_physmap_add_entry(d, _gfn(gfn), mfn, 0, p2m_ram_rw); } =20 /* diff --git a/xen/include/xsm/dummy.h b/xen/include/xsm/dummy.h index 2368ace..317455a 100644 --- a/xen/include/xsm/dummy.h +++ b/xen/include/xsm/dummy.h @@ -713,14 +713,14 @@ static XSM_INLINE int xsm_pmu_op (XSM_DEFAULT_ARG str= uct domain *d, unsigned int } } =20 +#endif /* CONFIG_X86 */ + static XSM_INLINE int xsm_dm_op(XSM_DEFAULT_ARG struct domain *d) { XSM_ASSERT_ACTION(XSM_DM_PRIV); return xsm_default_action(action, current->domain, d); } =20 -#endif /* CONFIG_X86 */ - #ifdef CONFIG_ARGO static XSM_INLINE int xsm_argo_enable(const struct domain *d) { diff --git a/xen/include/xsm/xsm.h b/xen/include/xsm/xsm.h index a80bcf3..2a9b39d 100644 --- a/xen/include/xsm/xsm.h +++ b/xen/include/xsm/xsm.h @@ -177,8 +177,8 @@ struct xsm_operations { int (*ioport_permission) (struct domain *d, uint32_t s, uint32_t e, ui= nt8_t allow); int (*ioport_mapping) (struct domain *d, uint32_t s, uint32_t e, uint8= _t allow); int (*pmu_op) (struct domain *d, unsigned int op); - int (*dm_op) (struct domain *d); #endif + int (*dm_op) (struct domain *d); int (*xen_version) (uint32_t cmd); int (*domain_resource_map) (struct domain *d); #ifdef CONFIG_ARGO @@ -688,13 +688,13 @@ static inline int xsm_pmu_op (xsm_default_t def, stru= ct domain *d, unsigned int return xsm_ops->pmu_op(d, op); } =20 +#endif /* CONFIG_X86 */ + static inline int xsm_dm_op(xsm_default_t def, struct domain *d) { return xsm_ops->dm_op(d); } =20 -#endif /* CONFIG_X86 */ - static inline int xsm_xen_version (xsm_default_t def, uint32_t op) { return xsm_ops->xen_version(op); diff --git a/xen/xsm/dummy.c b/xen/xsm/dummy.c index d4cce68..e3afd06 100644 --- a/xen/xsm/dummy.c +++ b/xen/xsm/dummy.c @@ -148,8 +148,8 @@ void __init xsm_fixup_ops (struct xsm_operations *ops) set_to_dummy_if_null(ops, ioport_permission); set_to_dummy_if_null(ops, ioport_mapping); set_to_dummy_if_null(ops, pmu_op); - set_to_dummy_if_null(ops, dm_op); #endif + set_to_dummy_if_null(ops, dm_op); set_to_dummy_if_null(ops, xen_version); set_to_dummy_if_null(ops, domain_resource_map); #ifdef CONFIG_ARGO diff --git a/xen/xsm/flask/hooks.c b/xen/xsm/flask/hooks.c index a314bf8..645192a 100644 --- a/xen/xsm/flask/hooks.c +++ b/xen/xsm/flask/hooks.c @@ -1662,14 +1662,13 @@ static int flask_pmu_op (struct domain *d, unsigned= int op) return -EPERM; } } +#endif /* CONFIG_X86 */ =20 static int flask_dm_op(struct domain *d) { return current_has_perm(d, SECCLASS_HVM, HVM__DM); } =20 -#endif /* CONFIG_X86 */ - static int flask_xen_version (uint32_t op) { u32 dsid =3D domain_sid(current->domain); @@ -1872,8 +1871,8 @@ static struct xsm_operations flask_ops =3D { .ioport_permission =3D flask_ioport_permission, .ioport_mapping =3D flask_ioport_mapping, .pmu_op =3D flask_pmu_op, - .dm_op =3D flask_dm_op, #endif + .dm_op =3D flask_dm_op, .xen_version =3D flask_xen_version, .domain_resource_map =3D flask_domain_resource_map, #ifdef CONFIG_ARGO --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478949; cv=none; d=zohomail.com; s=zohoarc; b=N0RLHdLymu6qOCeC7s9ydTb18nKSfAMsc1eW2qxE5MgoMGVVdY5MdMO1F0NVCCc3VlXESzc1j8LNrPwvmwpGyJ7p4M2GJPnlwSMBrYAYjxWLTkMygRvKVs8nmgLllRXK4Ui2Wv/xdT4s6nmEPXafltfGyobjmikRi+psvTMb2nA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478949; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=Mv84D4r2aumLFDv8lYRdBY/1g2sPieE7HeRa3w7RXVY=; b=UnQA9BKBjU4om7liBTx7QWTMGr2Y5Ns+x1bHpoiWAPLhrbCTREuN06SNPRbBxTxKO9hoIHVku9Fs6533qgX9Ejxzryge1U88ZXf9vFeDNE8w4VRuji/2y2fK4fX/s2ZKeZXTDECPUyAx2NriI8j2MbR9vK3DlyKJaCiF1O0nUDA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596478949342313.5243105828622; Mon, 3 Aug 2020 11:22:29 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6C-0008KM-7N; Mon, 03 Aug 2020 18:22:12 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6B-0008F4-6y for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:11 +0000 Received: from mail-lf1-x142.google.com (unknown [2a00:1450:4864:20::142]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 32a454ae-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:51 +0000 (UTC) Received: by mail-lf1-x142.google.com with SMTP id x24so3651762lfe.11 for ; Mon, 03 Aug 2020 11:21:51 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:49 -0700 (PDT) X-Inumbo-ID: 32a454ae-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Mv84D4r2aumLFDv8lYRdBY/1g2sPieE7HeRa3w7RXVY=; b=PwJL8Ike+kIqVKmPv4Lw2iCjl0WHNL82j9B4uw9RwQQEO5EPmdiNiZqnuVtuBwWlEU Uosmt7kiLnfykZF/8NIzgiZBAu7LpgIadpcmhPbJg0Gda7UiU3wrkRWMUiPhuigx2vjs fgx7t8JQD1tYHx8TrRIqw54NcswwDk3NZH2aaOIElQVwKq8Sp1z39JU7Qzq1lGjaycPQ 3JPYcILbPRH9HhLPT1g3c5iDQH0ALiaBrmpQjlIu9Wx4LIco6LrI8KyePWraS4d8iXFo xKXCUm2X4VOAG7sjndU9Ltob7TYxUELO/oTbvE8jO4tQKeaiRJV1i2S7uM3hadfJ41OD /Adg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Mv84D4r2aumLFDv8lYRdBY/1g2sPieE7HeRa3w7RXVY=; b=bRtwMgOxh4WG0Och3XYnuo9a8HbECJPjODlL4vgeiA8OPvjGxQRlxnY0dyNlY76Lnu Ri3Fcxzi9NRK+aBhcZP7XUJneuwfTVCOfevr14Z0kyXyp2gCz5hHIG+DanW5gKGKM3jg D5JybWIirvyFOSbAiUdvLdeZLAp4vxWmwDwqBUx8SpbtOjD7dybrLqLFM4C1MFuFFYtz zx2hUKko1l8qMV2WK/+14//YVZc6lgj7Kt3Pybb+ImyYSJxoWoQp4EHSNuvd2C87sd/f q/ZWICF1+IQfVKHBoxL3arpX8scBzDLS5A2w9932rPsdnexeeYOeApJE0FghDWFh/3FH WQbA== X-Gm-Message-State: AOAM5339k+3/wRdXQJnSSEFVEK703B1xdjS436qxBPWB70sIVA2Mx2rv ldDaE+AdVs/EN/YWKfx+0xvkDiWQvCI= X-Google-Smtp-Source: ABdhPJxXWm0JBPyiKEZvceaAoqdJzrLR6fTtwnaGCqNa5E12FxcPBl8d3bee+TF2QDKbf03LyNkhCA== X-Received: by 2002:a19:c806:: with SMTP id y6mr2705506lff.156.1596478909751; Mon, 03 Aug 2020 11:21:49 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 05/12] hvm/dm: Introduce xendevicemodel_set_irq_level DM op Date: Mon, 3 Aug 2020 21:21:21 +0300 Message-Id: <1596478888-23030-6-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Andrew Cooper , Ian Jackson , George Dunlap , Oleksandr Tyshchenko , Julien Grall , Jan Beulich , Volodymyr Babchuk Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko This patch adds ability to the device emulator to notify otherend (some entity running in the guest) using a SPI and implements Arm specific bits for it. Proposed interface allows emulator to set the logical level of a one of a domain's IRQ lines. Please note, this is a split/cleanup of Julien's PoC: "Add support for Guest IO forwarding to a device emulator" Signed-off-by: Julien Grall Signed-off-by: Oleksandr Tyshchenko --- tools/libs/devicemodel/core.c | 18 ++++++++++++++++++ tools/libs/devicemodel/include/xendevicemodel.h | 4 ++++ tools/libs/devicemodel/libxendevicemodel.map | 1 + xen/arch/arm/dm.c | 22 +++++++++++++++++++++- xen/common/hvm/dm.c | 1 + xen/include/public/hvm/dm_op.h | 15 +++++++++++++++ 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/tools/libs/devicemodel/core.c b/tools/libs/devicemodel/core.c index 4d40639..30bd79f 100644 --- a/tools/libs/devicemodel/core.c +++ b/tools/libs/devicemodel/core.c @@ -430,6 +430,24 @@ int xendevicemodel_set_isa_irq_level( return xendevicemodel_op(dmod, domid, 1, &op, sizeof(op)); } =20 +int xendevicemodel_set_irq_level( + xendevicemodel_handle *dmod, domid_t domid, uint32_t irq, + unsigned int level) +{ + struct xen_dm_op op; + struct xen_dm_op_set_irq_level *data; + + memset(&op, 0, sizeof(op)); + + op.op =3D XEN_DMOP_set_irq_level; + data =3D &op.u.set_irq_level; + + data->irq =3D irq; + data->level =3D level; + + return xendevicemodel_op(dmod, domid, 1, &op, sizeof(op)); +} + int xendevicemodel_set_pci_link_route( xendevicemodel_handle *dmod, domid_t domid, uint8_t link, uint8_t irq) { diff --git a/tools/libs/devicemodel/include/xendevicemodel.h b/tools/libs/d= evicemodel/include/xendevicemodel.h index e877f5c..c06b3c8 100644 --- a/tools/libs/devicemodel/include/xendevicemodel.h +++ b/tools/libs/devicemodel/include/xendevicemodel.h @@ -209,6 +209,10 @@ int xendevicemodel_set_isa_irq_level( xendevicemodel_handle *dmod, domid_t domid, uint8_t irq, unsigned int level); =20 +int xendevicemodel_set_irq_level( + xendevicemodel_handle *dmod, domid_t domid, unsigned int irq, + unsigned int level); + /** * This function maps a PCI INTx line to a an IRQ line. * diff --git a/tools/libs/devicemodel/libxendevicemodel.map b/tools/libs/devi= cemodel/libxendevicemodel.map index 561c62d..a0c3012 100644 --- a/tools/libs/devicemodel/libxendevicemodel.map +++ b/tools/libs/devicemodel/libxendevicemodel.map @@ -32,6 +32,7 @@ VERS_1.2 { global: xendevicemodel_relocate_memory; xendevicemodel_pin_memory_cacheattr; + xendevicemodel_set_irq_level; } VERS_1.1; =20 VERS_1.3 { diff --git a/xen/arch/arm/dm.c b/xen/arch/arm/dm.c index 2437099..8431805 100644 --- a/xen/arch/arm/dm.c +++ b/xen/arch/arm/dm.c @@ -20,7 +20,27 @@ int arch_dm_op(struct xen_dm_op *op, struct domain *d, const struct dmop_args *op_args, bool *const_op) { - return -EOPNOTSUPP; + int rc; + + switch ( op->op ) + { + case XEN_DMOP_set_irq_level: + { + const struct xen_dm_op_set_irq_level *data =3D + &op->u.set_irq_level; + + /* XXX: Handle check */ + vgic_inject_irq(d, NULL, data->irq, data->level); + rc =3D 0; + break; + } + + default: + rc =3D -EOPNOTSUPP; + break; + } + + return rc; } =20 /* diff --git a/xen/common/hvm/dm.c b/xen/common/hvm/dm.c index 09e9542..e2e1250 100644 --- a/xen/common/hvm/dm.c +++ b/xen/common/hvm/dm.c @@ -47,6 +47,7 @@ static int dm_op(const struct dmop_args *op_args) [XEN_DMOP_remote_shutdown] =3D sizeof(struct xen_= dm_op_remote_shutdown), [XEN_DMOP_relocate_memory] =3D sizeof(struct xen_= dm_op_relocate_memory), [XEN_DMOP_pin_memory_cacheattr] =3D sizeof(struct xen_= dm_op_pin_memory_cacheattr), + [XEN_DMOP_set_irq_level] =3D sizeof(struct xen_= dm_op_set_irq_level), }; =20 rc =3D rcu_lock_remote_domain_by_id(op_args->domid, &d); diff --git a/xen/include/public/hvm/dm_op.h b/xen/include/public/hvm/dm_op.h index fd00e9d..c45d29e 100644 --- a/xen/include/public/hvm/dm_op.h +++ b/xen/include/public/hvm/dm_op.h @@ -417,6 +417,20 @@ struct xen_dm_op_pin_memory_cacheattr { uint32_t pad; }; =20 +/* + * XEN_DMOP_set_irq_level: Set the logical level of a one of a domain's + * IRQ lines. + * XXX Handle PPIs. + */ +#define XEN_DMOP_set_irq_level 19 + +struct xen_dm_op_set_irq_level { + uint32_t irq; + /* IN - Level: 0 -> deasserted, 1 -> asserted */ + uint8_t level; +}; + + struct xen_dm_op { uint32_t op; uint32_t pad; @@ -430,6 +444,7 @@ struct xen_dm_op { struct xen_dm_op_track_dirty_vram track_dirty_vram; struct xen_dm_op_set_pci_intx_level set_pci_intx_level; struct xen_dm_op_set_isa_irq_level set_isa_irq_level; + struct xen_dm_op_set_irq_level set_irq_level; struct xen_dm_op_set_pci_link_route set_pci_link_route; struct xen_dm_op_modified_memory modified_memory; struct xen_dm_op_set_mem_type set_mem_type; --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478955; cv=none; d=zohomail.com; s=zohoarc; b=aL/Hnk7LIQxNzyDZE9LwZqg1Mr0fCiJFAbWy0pLLLPcv42fWH2+uunT8J1eH0fKodFpdwkZIj2Bs95FHOBDdaR4zob6L0ur4Cbs862vasTOabwY7mE7QlpfzzFWjgiqhbsYGzehbgnlTQ2iKq/40Vq+zBewaFGUGlXq25qJUJ4c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478955; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=KJwl2Gf8Kc5S6/JKnP2lqwji4gRYsqHMXGwGJUsp3PA=; b=nWqCtohlMRCz7F/aBveTjBcBM1JsOwee4x36xe/fBYBDkb8QnBatY/yBcJad3StKjkxzcKMy5grZ1t37T1rJVS/3gePMaYYwbMCXyG4nlzweAEEzykRGy7qY7JdihfktPOxkRHJzzwM/DjPjjvXogX+fKexi2/LgYuo0Dc7htLE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596478955515668.6356498741479; Mon, 3 Aug 2020 11:22:35 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6H-0008Mg-Lg; Mon, 03 Aug 2020 18:22:17 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6G-0008F4-7C for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:16 +0000 Received: from mail-lf1-x143.google.com (unknown [2a00:1450:4864:20::143]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 33932a00-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:52 +0000 (UTC) Received: by mail-lf1-x143.google.com with SMTP id x24so3651785lfe.11 for ; Mon, 03 Aug 2020 11:21:52 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:50 -0700 (PDT) X-Inumbo-ID: 33932a00-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KJwl2Gf8Kc5S6/JKnP2lqwji4gRYsqHMXGwGJUsp3PA=; b=HYJEXbGiDpX09e/7tZjQdogZIcJH4gMNC09XuyfFLcrQThKzJ7c6ZFJCl2riNdhQHn N0zLR59eCnQ48byQmeEticc8+PAD0yhFJJGO6IXTqOkC/kFxBGf8zuKAGiT9BDnofh+L Ze/ZNeonVO5rP7j6/vDr6T5/wJd35ZREnwHf6AWZD8kDX9FS3rZoOyFbtM+3BM/4SBxi 5lottYR/7hNls0wK9wO/RtKoCc5JrHbiULIfTo72r1rS3n+lXW+BsCKRtGINM3emRq1K +nJ7h1sWOFTi+IBjAeZoQ4sh+1LGtWIN6fXQgH1k9sIoCzqBO0FPRMY9X4ll1/RNWz82 nKxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KJwl2Gf8Kc5S6/JKnP2lqwji4gRYsqHMXGwGJUsp3PA=; b=IUFu/dTEY5LkOYxAQx0EFwdVtskyv2cSXpFwqizsU1zL5E6P78BAbuJQbsgfTUSRFr gOjsGw+pHfx7d2WuogdYV2TDW9LmJaNGSUKUtDrkurV7Ft03H0qMbwk5iNT6iDthQui1 pVkadhh1YsDIks1bi+f1F/N/k5gf8RT8uNMSx4UjfX+D/TgiMrRsk38mLNAMoc3G2tJZ W+vSW6zWKa+a8Y2YX4/nRne7jOfDKW+hLEP96kD0tZyskatcU+pOpm/SJ5he77Xhg6Bj PfDpJQGQnCYpMUAzgmVZwon1rdu371NZx1eF5smC7571LhtXRJ3bNtquxGV8XdzwYVKz NvZw== X-Gm-Message-State: AOAM531mY97voCksnP1D1/ZhtYb/+6L2vGh28gX24uUG0Ek/YH1a96Ar fF6/7/ReBJ7I8ANEcTcsyJRVU3KDkTg= X-Google-Smtp-Source: ABdhPJz/K3XOJPdOW03K8F5ra2FDgmd10m+rY5j09JeC8qbFsgW0FOwPYAJfoEjxaSi+C7e6N/cx0w== X-Received: by 2002:ac2:4c33:: with SMTP id u19mr9422767lfq.154.1596478910795; Mon, 03 Aug 2020 11:21:50 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 06/12] libxl: Introduce basic virtio-mmio support on Arm Date: Mon, 3 Aug 2020 21:21:22 +0300 Message-Id: <1596478888-23030-7-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Ian Jackson , Oleksandr Tyshchenko , Julien Grall , Anthony PERARD Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko This patch creates specific device node in the Guest device-tree with allocated MMIO range and SPI interrupt if specific 'virtio' property is present in domain config. Please note, this patch breaks device passthrough use-case which will be fixed in one of the follow-up patches. Please note, this is a split/cleanup of Julien's PoC: "Add support for Guest IO forwarding to a device emulator" Signed-off-by: Julien Grall Signed-off-by: Oleksandr Tyshchenko --- tools/libxl/libxl_arm.c | 39 +++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 1 + tools/xl/xl_parse.c | 1 + 3 files changed, 41 insertions(+) diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c index 34f8a29..620b499 100644 --- a/tools/libxl/libxl_arm.c +++ b/tools/libxl/libxl_arm.c @@ -68,6 +68,10 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc, nr_spis =3D spi + 1; } =20 + + /* XXX: Handle properly virtio */ + nr_spis =3D 1; + LOG(DEBUG, "Configure the domain"); =20 config->arch.nr_spis =3D nr_spis; @@ -659,6 +663,37 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *= fdt, return 0; } =20 +#define GUEST_VIRTIO_MMIO_BASE xen_mk_ullong(0x02000000) +#define GUEST_VIRTIO_MMIO_SIZE xen_mk_ullong(0x200) +#define GUEST_VIRTIO_MMIO_SPI 33 + +static int make_virtio_mmio_node(libxl__gc *gc, void *fdt) +{ + int res; + gic_interrupt intr; + + /* XXX: Add address in the node name */ + res =3D fdt_begin_node(fdt, "virtio"); + if (res) return res; + + res =3D fdt_property_compat(gc, fdt, 1, "virtio,mmio"); + if (res) return res; + + res =3D fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS, GUEST_ROO= T_SIZE_CELLS, + 1, GUEST_VIRTIO_MMIO_BASE, GUEST_VIRTIO_MMIO_S= IZE); + if (res) return res; + + set_interrupt(intr, GUEST_VIRTIO_MMIO_SPI, 0xf, DT_IRQ_TYPE_EDGE_RISIN= G); + res =3D fdt_property_interrupts(gc, fdt, &intr, 1); + if (res) return res; + + res =3D fdt_end_node(fdt); + if (res) return res; + + return 0; + +} + static const struct arch_info *get_arch_info(libxl__gc *gc, const struct xc_dom_image *do= m) { @@ -962,6 +997,9 @@ next_resize: if (info->tee =3D=3D LIBXL_TEE_TYPE_OPTEE) FDT( make_optee_node(gc, fdt) ); =20 + if (libxl_defbool_val(info->arch_arm.virtio)) + FDT( make_virtio_mmio_node(gc, fdt) ); + if (pfdt) FDT( copy_partial_fdt(gc, fdt, pfdt) ); =20 @@ -1179,6 +1217,7 @@ void libxl__arch_domain_build_info_setdefault(libxl__= gc *gc, { /* ACPI is disabled by default */ libxl_defbool_setdefault(&b_info->acpi, false); + libxl_defbool_setdefault(&b_info->arch_arm.virtio, false); =20 if (b_info->type !=3D LIBXL_DOMAIN_TYPE_PV) return; diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl index 9d3f05f..b054bf9 100644 --- a/tools/libxl/libxl_types.idl +++ b/tools/libxl/libxl_types.idl @@ -639,6 +639,7 @@ libxl_domain_build_info =3D Struct("domain_build_info",[ =20 =20 ("arch_arm", Struct(None, [("gic_version", libxl_gic_version), + ("virtio", libxl_defbool), ("vuart", libxl_vuart_type), ])), # Alternate p2m is not bound to any architecture or guest type, as it = is diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index 61b4ef7..b8306aa 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -2579,6 +2579,7 @@ skip_usbdev: } =20 xlu_cfg_get_defbool(config, "dm_restrict", &b_info->dm_restrict, 0); + xlu_cfg_get_defbool(config, "virtio", &b_info->arch_arm.virtio, 0); =20 if (c_info->type =3D=3D LIBXL_DOMAIN_TYPE_HVM) { if (!xlu_cfg_get_string (config, "vga", &buf, 0)) { --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478957; cv=none; d=zohomail.com; s=zohoarc; b=fQaDpfNy4jsTkQzPd41BALUa+j97VPf7O9JrXJ3YJby8A/fBS1Zb+K+du4DOpdAN3V7GDgv39S6O+CG/SS6fyeFGNHn1iFHnh9TnwhMWVhMtkJOabm50Qxio2BnGIwhXUpP8TlBNfrUy/S9Gv9VjW7XdE0Jh2Tgtrx3aKMnWFvs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478957; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=81P0Yc4v6ZyioiYpKUcR0ZeC9nLn0E/s9OZDx4+Xc7c=; b=V4woFvRo3lvV0eAPjzqXS6bQi3Wm9xqC2to44KuFLTAPxTlwAchgAtxvurJ8rapy0BnhKsYUnWgp36I9Cps5DgbMKc5tpoqb2ZR8wBYEO9IJT8LRHIXkO3rDDe/WZs0W4JgC1+eZQzLaIDcySCTrB5a0cJ876fLEhpVBmWq/WFU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596478957506432.2218249427492; Mon, 3 Aug 2020 11:22:37 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6M-0008P2-UY; Mon, 03 Aug 2020 18:22:22 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6L-0008F4-7V for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:21 +0000 Received: from mail-lf1-x143.google.com (unknown [2a00:1450:4864:20::143]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 34593e8e-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:53 +0000 (UTC) Received: by mail-lf1-x143.google.com with SMTP id d2so15343777lfj.1 for ; Mon, 03 Aug 2020 11:21:53 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:51 -0700 (PDT) X-Inumbo-ID: 34593e8e-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=81P0Yc4v6ZyioiYpKUcR0ZeC9nLn0E/s9OZDx4+Xc7c=; b=NRXK/Eep0Wp1l3ih8IjXSdhoX84+x4SxXAZnDjEc2HgGLPVFXjpD3VHiS9QGzhpqQN afIsSh1llgIKPdo5p+Pl9UD9MwJAbPYOShphK491g+ofAXERik6ynfXHeDX940X2Ny/e dSd3Sw76daOwNXKGFkaCVTn27xZT6NKXWXhPqq6/THS0Lv/p5wkivy+O3tmFeCP3GUxq tq86cBHGOSCI1OXhKfJznyGzsnLQHturZB72B9x0if6ZOVSx5D86Kf9MQNxJ9rWjgTRQ /JG3k1b//DzA9iUXyoBIypIwUAvapNyPnxvhfpkOpPCb6fjALE1BFmDO8x6u5GMC/yk4 bU2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=81P0Yc4v6ZyioiYpKUcR0ZeC9nLn0E/s9OZDx4+Xc7c=; b=jKauXWAfXoSiVuNKyBIiGIghogNuFhTyzaRHA6s47Xelh+ZsSC8mbCdsUv1YSo8Ub8 IE300Njd3zLvBRmYfZRNX0x/JE5A1nNmwpYF+Ej1BrodS/ektpsPzHhAO/yOkEkI6p2R x5K6g7k/JYQ0DaGtRHMBWekMLjbMG+O99XQDzHapxRN1TnQ5JGjwjC0eVP09MJkZwn+O qOAtzSu2NUQgePQMGSM0clhkMqRn1JleKxe2D30ExkvOt4fBY9NQu+rzYdNhrjVZ1q3X g6AjtaEDJPKiXSklHS9UQzxSCyHO238rhH0McmIft3kpERd5puOH4nq22G9SoXOemY18 iAMQ== X-Gm-Message-State: AOAM530MuXpEb4/v1l6W3wBPBVMVrOss9sQO6lxA5B4z2zyFGJEwa8PY 2FVglOusfN8KE/R+031GTR0CkLTwDs8= X-Google-Smtp-Source: ABdhPJwB9MeBGy/n2L9LbiCW8INF2cJ2XhF0qsz3n8TE1nbBrC5AGBbU+X5NCnGLXt/qAJfCnWS8oQ== X-Received: by 2002:a19:c653:: with SMTP id w80mr9132168lff.167.1596478912037; Mon, 03 Aug 2020 11:21:52 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain Date: Mon, 3 Aug 2020 21:21:23 +0300 Message-Id: <1596478888-23030-8-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Andrew Cooper , Ian Jackson , George Dunlap , Oleksandr Tyshchenko , Jan Beulich , Daniel De Graaf Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case. Signed-off-by: Oleksandr Tyshchenko --- xen/common/domain.c | 15 +++++++++++++++ xen/common/domctl.c | 8 +++++++- xen/common/event_channel.c | 14 ++++++++++++-- xen/common/memory.c | 6 ++++++ xen/include/xsm/dummy.h | 16 +++++++++++++--- 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index e9be05f..5c9fef2 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -695,6 +695,7 @@ int domain_kill(struct domain *d) { int rc =3D 0; struct vcpu *v; + struct domain *td; =20 if ( d =3D=3D current->domain ) return -EINVAL; @@ -733,6 +734,20 @@ int domain_kill(struct domain *d) * have to be put before we call put_domain. */ vm_event_cleanup(d); put_domain(d); + /* + * XEN_DOMCTL_set_target implementation holds reference on + * target domain which doesn't allow to completely destroy it. + * Check if the reference are hold by someone and drop it + * when destroying target domain. + */ + for_each_domain ( td ) { + if ( td->target =3D=3D d ) { + td->target =3D NULL; + put_domain(d); + break; + } + } + send_global_virq(VIRQ_DOM_EXC); /* fallthrough */ case DOMDYING_dead: diff --git a/xen/common/domctl.c b/xen/common/domctl.c index a69b3b5..079c7b0 100644 --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -871,6 +871,12 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_= domctl) if ( (d =3D=3D e) || (d->target !=3D NULL) ) { put_domain(e); + /* + * Be a little bit more polite here, looks like the emulator + * has just been restarted. + */ + if ( d->target =3D=3D e ) + ret =3D 0; break; } =20 @@ -883,7 +889,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_d= omctl) break; } =20 - /* Hold reference on @e until we destroy @d. */ + /* Hold reference on @e until we destroy either @d or @e */ d->target =3D e; break; } diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c index a8d182b5..2aa497a 100644 --- a/xen/common/event_channel.c +++ b/xen/common/event_channel.c @@ -235,7 +235,12 @@ static long evtchn_alloc_unbound(evtchn_alloc_unbound_= t *alloc) ERROR_EXIT_DOM(port, d); chn =3D evtchn_from_port(d, port); =20 - rc =3D xsm_evtchn_unbound(XSM_TARGET, d, chn, alloc->remote_dom); + /* + * XXX: XSM_TARGET is not functional for emulator running in driver do= main. + * See xsm_default_action for details. Probably XSM_DM_PRIV could work, + * but there is a risk to break other users. + */ + rc =3D xsm_evtchn_unbound(XSM_HOOK, d, chn, alloc->remote_dom); if ( rc ) goto out; =20 @@ -1218,7 +1223,12 @@ int alloc_unbound_xen_event_channel( port =3D rc; chn =3D evtchn_from_port(ld, port); =20 - rc =3D xsm_evtchn_unbound(XSM_TARGET, ld, chn, remote_domid); + /* + * XXX: XSM_TARGET is not functional for emulator running in driver do= main. + * See xsm_default_action for details. Probably XSM_DM_PRIV could work, + * but there is a risk to break other users. + */ + rc =3D xsm_evtchn_unbound(XSM_HOOK, ld, chn, remote_domid); if ( rc ) goto out; =20 diff --git a/xen/common/memory.c b/xen/common/memory.c index 0000477..8b306f6 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1153,12 +1153,18 @@ static int acquire_resource( unsigned int i; =20 /* + * XXX: Ugly hack for now to let emulator running in driver domain + * to succeeded in acquiring resource. + */ +#if 0 + /* * FIXME: Until foreign pages inserted into the P2M are properly * reference counted, it is unsafe to allow mapping of * resource pages unless the caller is the hardware domain. */ if ( !is_hardware_domain(currd) ) return -EACCES; +#endif =20 if ( copy_from_guest(gfn_list, xmar.frame_list, xmar.nr_frames) ) rc =3D -EFAULT; diff --git a/xen/include/xsm/dummy.h b/xen/include/xsm/dummy.h index 317455a..c0813c0 100644 --- a/xen/include/xsm/dummy.h +++ b/xen/include/xsm/dummy.h @@ -139,13 +139,23 @@ static XSM_INLINE int xsm_domctl(XSM_DEFAULT_ARG stru= ct domain *d, int cmd) XSM_ASSERT_ACTION(XSM_OTHER); switch ( cmd ) { + /* + * XXX: Emulator running in driver domain tries to get vcpus num. + * Probably we could avoid that change by modifying emulator to not use + * domctl for getting vcpus num. + */ + case XEN_DOMCTL_getdomaininfo: + /* + * XXX: XSM_DM_PRIV is not functional for emulator running in driver d= omain + * without setting a target in advance. See xsm_default_action for det= ails. + */ + case XEN_DOMCTL_set_target: + return xsm_default_action(XSM_HOOK, current->domain, d); case XEN_DOMCTL_ioport_mapping: case XEN_DOMCTL_memory_mapping: case XEN_DOMCTL_bind_pt_irq: case XEN_DOMCTL_unbind_pt_irq: return xsm_default_action(XSM_DM_PRIV, current->domain, d); - case XEN_DOMCTL_getdomaininfo: - return xsm_default_action(XSM_XS_PRIV, current->domain, d); default: return xsm_default_action(XSM_PRIV, current->domain, d); } @@ -275,7 +285,7 @@ static XSM_INLINE int xsm_claim_pages(XSM_DEFAULT_ARG s= truct domain *d) static XSM_INLINE int xsm_evtchn_unbound(XSM_DEFAULT_ARG struct domain *d,= struct evtchn *chn, domid_t id2) { - XSM_ASSERT_ACTION(XSM_TARGET); + XSM_ASSERT_ACTION(XSM_HOOK); return xsm_default_action(action, current->domain, d); } =20 --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478961; cv=none; d=zohomail.com; s=zohoarc; b=XN8TOGfOCVPCZtsQCEbScGw6pOTN9VzGgoiKs5fRd/rBwx6hkdw9moP3bMFkz46b8Df02SwCtXbEeK5x/LW2DJcBY+/XxY4G4cWUwIHfww4Kj34NRV2I0FGOcZf0AjO23xyOm42WTlU31tKOXR19cjomB14xzcDP2aonEsZwIeY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478961; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=1oSrmefLumOTqH6DfN/w0NjDRHD49Q9D8VghKe2JYu8=; b=YajzwyPukLmj9p5ZQm15wHFb8f4T/Gsrbu8cUv4Uq7b+Qz4MFrLvJGpkbXUM/PHwq9wRFts9iD+JAtDSc6stklTh5dVkeHr1qYs9EO0E4b9mIAe13xySfUIlGkHE7IOOOGrcG4kc7KnkvIQf5RRSDbzyHGsrVF7JNr3rfCC2QHY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596478961407550.2612619393415; Mon, 3 Aug 2020 11:22:41 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6R-0008Rt-7Q; Mon, 03 Aug 2020 18:22:27 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6Q-0008F4-7l for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:26 +0000 Received: from mail-lf1-x141.google.com (unknown [2a00:1450:4864:20::141]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 34f13356-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:54 +0000 (UTC) Received: by mail-lf1-x141.google.com with SMTP id x24so3651859lfe.11 for ; Mon, 03 Aug 2020 11:21:54 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:52 -0700 (PDT) X-Inumbo-ID: 34f13356-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1oSrmefLumOTqH6DfN/w0NjDRHD49Q9D8VghKe2JYu8=; b=V//h+OxqeiTG5OZxMtbFbmVHdAJskjcYsEd2xBVHcfS3K60z84ELVyvw9UsE2OTKoV UGXy+Lkr2/oNp+UXmgZTECJ9QucYCQc7vn0fYfk8wB8WrfvyFeeM0qCFPuLvyonPNYbM 2aR6pdbUjRTh9dJ3By/wPCTtqD49tMntucBW3z8MfI33eAhL36GKjOw+NAyBmmQr/4Tn 0LfDyEag75/lUkYqunp4lJlPfXoQtAyOBzyL/qIBtzj/OggwyILcXnTdnHy2sg7THa+o sIfQcR+jow8oa6VJg//vmD5oY+75hRDzyu7zKKxmXPzNk6iVuVQGY6lAS3bILycYEDoP 5vEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1oSrmefLumOTqH6DfN/w0NjDRHD49Q9D8VghKe2JYu8=; b=Ap8qk2zJAKDGYOsOA33qUDPF+y3qEJBjMll6fcRqcB2nXoOp/Sy15w7Vfo9mN2yiy9 FB1raKhXu5UZoZkFs2qqQQxj+1QM1rP5AYKySnYzyzKneX5yaQaYueHvbhS3Kot2sGZA mA3+WyyDULzK+ERK5JP6QXeZI7x0wQsQEtmrK4aj8W1pfqqM6qQCouYUHrqT2b8VeTgI 19VFlv89yzu0Ws/sFn+wYoXWyJ/iAdMMCeXzi/mIRb2fR6NqffSXLnfZ7pLpv3skb9Kq ifsqF+udmHgmxzcyTpa1TOdUH1/MAzcueJOmdbslJyA0vDpTv51j+W7gzhIqv/thSRsk GV6g== X-Gm-Message-State: AOAM531hvV8mNb3fZaoSUYDQR321Fse+2d5mjpc4DYHEPn0Gmt+LRuPI THVS6SDnD0LwpPVPXnaMbp1El/imALs= X-Google-Smtp-Source: ABdhPJxm4yfX8MSfwMtCdpABV1bvZqTlKWi27x2GrGHTMWtxZu4TK/T5zajR7U42CXNf3f6RN1w07Q== X-Received: by 2002:a19:418a:: with SMTP id o132mr9319096lfa.63.1596478913112; Mon, 03 Aug 2020 11:21:53 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 08/12] xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation Date: Mon, 3 Aug 2020 21:21:24 +0300 Message-Id: <1596478888-23030-9-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Andrew Cooper , Ian Jackson , George Dunlap , Oleksandr Tyshchenko , Jan Beulich , Volodymyr Babchuk Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Borrow x86's logic to invalidate qemu mapcache. TODO: Move send_invalidate_req() to common code (ioreq.c?). Signed-off-by: Oleksandr Tyshchenko --- xen/arch/arm/ioreq.c | 14 ++++++++++++++ xen/arch/arm/traps.c | 6 ++++++ xen/common/memory.c | 6 ++++++ xen/include/asm-arm/domain.h | 2 ++ xen/include/asm-arm/hvm/ioreq.h | 2 ++ 5 files changed, 30 insertions(+) diff --git a/xen/arch/arm/ioreq.c b/xen/arch/arm/ioreq.c index a9cc839..8f60c41 100644 --- a/xen/arch/arm/ioreq.c +++ b/xen/arch/arm/ioreq.c @@ -75,6 +75,20 @@ bool handle_mmio(void) return true; } =20 +/* Ask ioemu mapcache to invalidate mappings. */ +void send_invalidate_req(void) +{ + ioreq_t p =3D { + .type =3D IOREQ_TYPE_INVALIDATE, + .size =3D 4, + .dir =3D IOREQ_WRITE, + .data =3D ~0UL, /* flush all */ + }; + + if ( hvm_broadcast_ioreq(&p, false) !=3D 0 ) + gprintk(XENLOG_ERR, "Unsuccessful map-cache invalidate\n"); +} + /* * Local variables: * mode: C diff --git a/xen/arch/arm/traps.c b/xen/arch/arm/traps.c index 4cdf098..ea472d1 100644 --- a/xen/arch/arm/traps.c +++ b/xen/arch/arm/traps.c @@ -1490,6 +1490,12 @@ static void do_trap_hypercall(struct cpu_user_regs *= regs, register_t *nr, /* Ensure the hypercall trap instruction is re-executed. */ if ( current->hcall_preempted ) regs->pc -=3D 4; /* re-execute 'hvc #XEN_HYPERCALL_TAG' */ + +#ifdef CONFIG_IOREQ_SERVER + if ( unlikely(current->domain->arch.hvm.qemu_mapcache_invalidate) && + test_and_clear_bool(current->domain->arch.hvm.qemu_mapcache_inval= idate) ) + send_invalidate_req(); +#endif } =20 void arch_hypercall_tasklet_result(struct vcpu *v, long res) diff --git a/xen/common/memory.c b/xen/common/memory.c index 8b306f6..8d9f0a8 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDL= E_PARAM(void) arg) break; } =20 + /* x86 already sets the flag in hvm_memory_op() */ +#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER) + if ( op =3D=3D XENMEM_decrease_reservation ) + curr_d->arch.hvm.qemu_mapcache_invalidate =3D true; +#endif + return rc; } =20 diff --git a/xen/include/asm-arm/domain.h b/xen/include/asm-arm/domain.h index e060b0a..0db8bb4 100644 --- a/xen/include/asm-arm/domain.h +++ b/xen/include/asm-arm/domain.h @@ -69,6 +69,8 @@ struct hvm_domain spinlock_t lock; struct hvm_ioreq_server *server[MAX_NR_IOREQ_SERVERS]; } ioreq_server; + + bool_t qemu_mapcache_invalidate; }; =20 #ifdef CONFIG_ARM_64 diff --git a/xen/include/asm-arm/hvm/ioreq.h b/xen/include/asm-arm/hvm/iore= q.h index 83a560c..392ce64 100644 --- a/xen/include/asm-arm/hvm/ioreq.h +++ b/xen/include/asm-arm/hvm/ioreq.h @@ -90,6 +90,8 @@ static inline void arch_hvm_ioreq_destroy(struct domain *= d) #define IOREQ_IO_UNHANDLED IO_UNHANDLED #define IOREQ_IO_RETRY IO_RETRY =20 +void send_invalidate_req(void); + #endif /* __ASM_X86_HVM_IOREQ_H__ */ =20 /* --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596478971; cv=none; d=zohomail.com; s=zohoarc; b=GS6kdw1DVikbCB2efVLWTDj8m5Y44qssSk1Ly+BbKvrPA3nogSaq30RWb+6TEXA9I6jTV1Rz5A3wYgd2I3WjqulgIRoz5Ptj3mY5RjJSO11I9PKVFbbz2Ge7ACqyVY9l2G9XN4i4tWucDUB/MU+ltfZgIcaY4HZjzs5CWBuZMTI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596478971; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=FmQeBTMaiyQz/Hd6vgyjkGjraUz7GZ+o5Z5LqiJNJDo=; b=ediCnthB9nB2higGDV2vK1W0fSGq7khrtICtxI48/NU2Ev097cuhJjn69UCbev8MlyyTo7285nacRtdRASPgM68WBDXq5q3KlUOfT54IwFNRg+TD9DH6TpedS7DBL6h+rDlpWoEKURmqqQB3o9FfQVeUqHh0SLR6i+4Mm5X7Xe4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 159647897101635.67709270359853; Mon, 3 Aug 2020 11:22:51 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6W-0008W8-Hk; Mon, 03 Aug 2020 18:22:32 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6V-0008F4-7r for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:31 +0000 Received: from mail-lj1-x241.google.com (unknown [2a00:1450:4864:20::241]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 359da712-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:55 +0000 (UTC) Received: by mail-lj1-x241.google.com with SMTP id w14so10874971ljj.4 for ; Mon, 03 Aug 2020 11:21:55 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:53 -0700 (PDT) X-Inumbo-ID: 359da712-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FmQeBTMaiyQz/Hd6vgyjkGjraUz7GZ+o5Z5LqiJNJDo=; b=gAWzKt2Cbi1NKv4UBav+vbo8LfDixp+Eap1dTPtwi2BMWPaf2jU4Rjoydyz7agqrr9 +X6kk1zWdRLI6Wwn1U8IRrX2XwWY4awi9IqLlCRZPsI8lcKJqeHb9zfPJq5eOZtney6D 17hNLeYak2BbiiZvuAwy6/k+DRZ50CV7KLH3G6tnwc498srQz0pF6HbbE50eT1K7CVS9 jtituXwf2GEWFHkta829sxlrHqcIwj5DY2U3IF9qf8QF3ybtuvAWKIBa3pId7Xpem9jO Me2bjrfWwt1B2qAbtkA2Gzfs31yZXRhANkD3FnWfpiYpjWdPGJXs5XS9Jt5K5t1rg/QC TqrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FmQeBTMaiyQz/Hd6vgyjkGjraUz7GZ+o5Z5LqiJNJDo=; b=YIIb4sSDldRLYrn3UP3EkqrHmXfCR1IcBlVTkY1j1Yj1sXT/Fp7Bdx+OB2mkiBadAA NUQl3r0t5wD8+2AdSbVbizy7DDExVWCVKUepPLC6yjCxhvq8ZzeTj53aPrYPXhXDLs2R 41N0P4RWSujN3xTqVE1xBOxfn0y8VIb2CRBEE6DAJ6JaoImv7Sy4yQqXzvg8H/sA91oL 1s3cqgOaIVlSmpDzeYxLvuOPeUGZgbJ8IY1LqJMnS5d2TeRzDFCxmmOh4UJnM1aUHXfa SJR/NiMgkpHsjhiOZAyl8XyDYD0xZyo936vmlo8gspw/bvxfQ/mBX29JiRdZWk+eX874 eVlw== X-Gm-Message-State: AOAM531KCGRPzOsvSMFSWVI4zg6Hq2PiK1DJKfZCilIeqeimDTzBMSJ4 KVtxZ9qkPZ/Cl15xqp4LFNZCHfFVaTs= X-Google-Smtp-Source: ABdhPJyPqjNSkPtYdYQNj5MmLgmHtV9oiYCjfWDQdqSaYeTCaiAqb1M+7uHa6BFqYFLlZF2nCrz+Vg== X-Received: by 2002:a2e:3c16:: with SMTP id j22mr8489386lja.92.1596478914266; Mon, 03 Aug 2020 11:21:54 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 09/12] libxl: Handle virtio-mmio irq in more correct way Date: Mon, 3 Aug 2020 21:21:25 +0300 Message-Id: <1596478888-23030-10-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Ian Jackson , Oleksandr Tyshchenko , Anthony PERARD Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko This patch makes possible to use device passthrough again. Signed-off-by: Oleksandr Tyshchenko --- tools/libxl/libxl_arm.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c index 620b499..4f748e3 100644 --- a/tools/libxl/libxl_arm.c +++ b/tools/libxl/libxl_arm.c @@ -9,6 +9,10 @@ #include #include =20 +#define GUEST_VIRTIO_MMIO_BASE xen_mk_ullong(0x02000000) +#define GUEST_VIRTIO_MMIO_SIZE xen_mk_ullong(0x200) +#define GUEST_VIRTIO_MMIO_SPI 33 + static const char *gicv_to_string(libxl_gic_version gic_version) { switch (gic_version) { @@ -27,8 +31,8 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc, { uint32_t nr_spis =3D 0; unsigned int i; - uint32_t vuart_irq; - bool vuart_enabled =3D false; + uint32_t vuart_irq, virtio_irq; + bool vuart_enabled =3D false, virtio_enabled =3D false; =20 /* * If pl011 vuart is enabled then increment the nr_spis to allow alloc= ation @@ -40,6 +44,17 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc, vuart_enabled =3D true; } =20 + /* + * XXX: Handle properly virtio + * A proper solution would be the toolstack to allocate the interrupts + * used by each virtio backend and let the backend now which one is us= ed + */ + if (libxl_defbool_val(d_config->b_info.arch_arm.virtio)) { + nr_spis +=3D (GUEST_VIRTIO_MMIO_SPI - 32) + 1; + virtio_irq =3D GUEST_VIRTIO_MMIO_SPI; + virtio_enabled =3D true; + } + for (i =3D 0; i < d_config->b_info.num_irqs; i++) { uint32_t irq =3D d_config->b_info.irqs[i]; uint32_t spi; @@ -59,6 +74,12 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc, return ERROR_FAIL; } =20 + /* The same check as for vpl011 */ + if (virtio_enabled && irq =3D=3D virtio_irq) { + LOG(ERROR, "Physical IRQ %u conflicting with virtio SPI\n", ir= q); + return ERROR_FAIL; + } + if (irq < 32) continue; =20 @@ -68,10 +89,6 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc, nr_spis =3D spi + 1; } =20 - - /* XXX: Handle properly virtio */ - nr_spis =3D 1; - LOG(DEBUG, "Configure the domain"); =20 config->arch.nr_spis =3D nr_spis; @@ -663,10 +680,6 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *= fdt, return 0; } =20 -#define GUEST_VIRTIO_MMIO_BASE xen_mk_ullong(0x02000000) -#define GUEST_VIRTIO_MMIO_SIZE xen_mk_ullong(0x200) -#define GUEST_VIRTIO_MMIO_SPI 33 - static int make_virtio_mmio_node(libxl__gc *gc, void *fdt) { int res; --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596479486; cv=none; d=zohomail.com; s=zohoarc; b=FNVa9C8K0qiTpx3GI1W7PbXtbS3KMERfUmbjh8KjdGon83eRyyCNevuWdHytbi8BZ++2gQyRzyszvZIVvShP8iiAc04V5RwG/QuoevlAlCO1tiA5D9a0VIzhq5Q0oeCPWykio3Kdb6l2QlcRwabYbYUai0rRqsz7uQGh+jyXSOc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596479486; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=NkBcm5wYA9ezMLwUACSNL6Lbpvri20JEcy5Q9KlbYZE=; b=i58YquULk1uPy1h5T0bzdorP+BUl+NZeooX4DeHUnkB9iE4F85S5sz4tqtWY1lqpvc8A4gS3xLPbu7VLkWG6D6Rlu9usi24rOqUZ4lOTWLTTvON+7IsKC9e0Ii7HKbcH7z3KkKsYGdMW5inVNU3r54WcvPUzqkL92gVoIbpwW0Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 159647948661451.84161588069401; Mon, 3 Aug 2020 11:31:26 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2fEo-0001ZH-3F; Mon, 03 Aug 2020 18:31:06 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6f-0008F4-7x for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:41 +0000 Received: from mail-lf1-x141.google.com (unknown [2a00:1450:4864:20::141]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 3639dd59-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:57 +0000 (UTC) Received: by mail-lf1-x141.google.com with SMTP id i19so20962415lfj.8 for ; Mon, 03 Aug 2020 11:21:57 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:54 -0700 (PDT) X-Inumbo-ID: 3639dd59-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NkBcm5wYA9ezMLwUACSNL6Lbpvri20JEcy5Q9KlbYZE=; b=KyMrgyuB0vbQO26N7CcvnLy46KtME50o0ONY/HeRJ7LbyZLMTbsYQMlSVGT3sHIEQe KVZmKTqQHeP5sd58b6wMc3S85WiffZs68k2n1ClhqJkWRHYoI/5XVaCW1Teh5gyvS6rn ru9rQpbPutAD/UIRWGEqgrWa1rPu8t80Y2PpI8evtVjZEDHcQDmQJ2xmLLEQ9gDt7lbc /LN6vQlv0M5/rO5qjt6FcwrG8eRelzvUxw4dDNy0Fw8vWmKZ3oTmANwQ6Ht91xB6Qv82 lR6qMTrXMSUXd3OFpwsyNwSkC4ttVDL8MNX1UOpQ3S7CrwIP2dis2wglngAqGdNSRyTH I2dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NkBcm5wYA9ezMLwUACSNL6Lbpvri20JEcy5Q9KlbYZE=; b=Ot6RZaSxzDXLGcbmgu02DW1UkFHVw+samRlv8dcIPwY6kMdsuF5zn3bHuOt+ApOqnW VtLgCzPxKoR1lN/SfrUn6gMgSka+VusS3UDsC8k2xnmGhWq5bSj9K2mf1qhn8MGAzm6y UMEhL1HNShUfEh+kPj8a4Fkz1yAPp8sedE9HYIL86u1wYcBFLl5CgbJY+hHmG4Y3/LFG 7LIJ7dP5C4C+ikSMyvlkRSPP1RDNsP0yMJ85Rs/Ro1qy+TcVZE3FhcovORz1Mo9NPigf tXAz7bVGb0OY9PbCBLibhEUW+E8egFwpsjPSKhyUabeSAOVdW0HqnZplYyC/PlB7CPt5 KqDQ== X-Gm-Message-State: AOAM532iiTW22sCNx+jZ6zKI1dztZgmo95t7+tjFEwfBbAuGg2TSl+lK uRTBRDZLoU8udsq4QIPh/sMJjD7T6bk= X-Google-Smtp-Source: ABdhPJyp9uHQ58t0oc/zp8wFTCh7uSzb9x/yAKr05xdSMw0lknBbQ514w1T9sEJrygK7Hqn2hzLbSQ== X-Received: by 2002:a19:4f11:: with SMTP id d17mr9081461lfb.48.1596478915315; Mon, 03 Aug 2020 11:21:55 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 10/12] libxl: Add support for virtio-disk configuration Date: Mon, 3 Aug 2020 21:21:26 +0300 Message-Id: <1596478888-23030-11-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Ian Jackson , Oleksandr Tyshchenko , Anthony PERARD Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko This patch adds basic support for configuring and assisting virtio-disk backend (emualator) which is intended to run out of Qemu and could be run in any domain. Xenstore was chosen as a communication interface for the emulator running in non-toolstack domain to be able to get configuration either by reading Xenstore directly or by receiving command line parameters (an updated 'xl d= evd' running in the same domain would read Xenstore beforehand and call backend executable with the required arguments). An example of domain configuration (two disks are assigned to the guest, the latter is in readonly mode): vdisk =3D [ 'backend=3DDomD, disks=3Drw:/dev/mmcblk0p3;ro:/dev/mmcblk1p3' ] Where per-disk Xenstore entries are: - filename and readonly flag (configured via "vdisk" property) - base and irq (allocated dynamically) Besides handling 'visible' params described in configuration file, patch also allocates virtio-mmio specific ones for each device and writes them into Xenstore. virtio-mmio params (irq and base) are unique per guest domain, they allocated at the domain creation time and passed through to the emulator. Each VirtIO device has at least one pair of these params. TODO: 1. An extra "virtio" property could be removed. 2. Update documentation. Signed-off-by: Oleksandr Tyshchenko --- tools/libxl/Makefile | 4 +- tools/libxl/libxl_arm.c | 63 +++++++++++++++---- tools/libxl/libxl_create.c | 1 + tools/libxl/libxl_internal.h | 1 + tools/libxl/libxl_types.idl | 15 +++++ tools/libxl/libxl_types_internal.idl | 1 + tools/libxl/libxl_virtio_disk.c | 109 +++++++++++++++++++++++++++++++= ++ tools/xl/Makefile | 2 +- tools/xl/xl.h | 3 + tools/xl/xl_cmdtable.c | 15 +++++ tools/xl/xl_parse.c | 115 +++++++++++++++++++++++++++++++= ++++ tools/xl/xl_virtio_disk.c | 46 ++++++++++++++ 12 files changed, 360 insertions(+), 15 deletions(-) create mode 100644 tools/libxl/libxl_virtio_disk.c create mode 100644 tools/xl/xl_virtio_disk.c diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile index 38cd43a..df94b13 100644 --- a/tools/libxl/Makefile +++ b/tools/libxl/Makefile @@ -141,7 +141,9 @@ LIBXL_OBJS =3D flexarray.o libxl.o libxl_create.o libxl= _dm.o libxl_pci.o \ libxl_vtpm.o libxl_nic.o libxl_disk.o libxl_console.o \ libxl_cpupool.o libxl_mem.o libxl_sched.o libxl_tmem.o \ libxl_9pfs.o libxl_domain.o libxl_vdispl.o \ - libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o $(LIBXL_OBJS-y) + libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o \ + libxl_virtio_disk.o $(LIBXL_OBJS-y) + LIBXL_OBJS +=3D libxl_genid.o LIBXL_OBJS +=3D _libxl_types.o libxl_flask.o _libxl_types_internal.o =20 diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c index 4f748e3..469a8b0 100644 --- a/tools/libxl/libxl_arm.c +++ b/tools/libxl/libxl_arm.c @@ -13,6 +13,12 @@ #define GUEST_VIRTIO_MMIO_SIZE xen_mk_ullong(0x200) #define GUEST_VIRTIO_MMIO_SPI 33 =20 +#ifndef container_of +#define container_of(ptr, type, member) ({ \ + typeof( ((type *)0)->member ) *__mptr =3D (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) +#endif + static const char *gicv_to_string(libxl_gic_version gic_version) { switch (gic_version) { @@ -44,14 +50,32 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc, vuart_enabled =3D true; } =20 - /* - * XXX: Handle properly virtio - * A proper solution would be the toolstack to allocate the interrupts - * used by each virtio backend and let the backend now which one is us= ed - */ if (libxl_defbool_val(d_config->b_info.arch_arm.virtio)) { - nr_spis +=3D (GUEST_VIRTIO_MMIO_SPI - 32) + 1; + uint64_t virtio_base; + libxl_device_virtio_disk *virtio_disk; + + virtio_base =3D GUEST_VIRTIO_MMIO_BASE; virtio_irq =3D GUEST_VIRTIO_MMIO_SPI; + + if (!d_config->num_virtio_disks) { + LOG(ERROR, "Virtio is enabled, but no Virtio devices present\n= "); + return ERROR_FAIL; + } + virtio_disk =3D &d_config->virtio_disks[0]; + + for (i =3D 0; i < virtio_disk->num_disks; i++) { + virtio_disk->disks[i].base =3D virtio_base; + virtio_disk->disks[i].irq =3D virtio_irq; + + LOG(DEBUG, "Allocate Virtio MMIO params: IRQ %u BASE 0x%"PRIx6= 4, + virtio_irq, virtio_base); + + virtio_irq ++; + virtio_base +=3D GUEST_VIRTIO_MMIO_SIZE; + } + virtio_irq --; + + nr_spis +=3D (virtio_irq - 32) + 1; virtio_enabled =3D true; } =20 @@ -75,8 +99,9 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc, } =20 /* The same check as for vpl011 */ - if (virtio_enabled && irq =3D=3D virtio_irq) { - LOG(ERROR, "Physical IRQ %u conflicting with virtio SPI\n", ir= q); + if (virtio_enabled && + (irq >=3D GUEST_VIRTIO_MMIO_SPI && irq <=3D virtio_irq)) { + LOG(ERROR, "Physical IRQ %u conflicting with Virtio IRQ range\= n", irq); return ERROR_FAIL; } =20 @@ -680,7 +705,8 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *f= dt, return 0; } =20 -static int make_virtio_mmio_node(libxl__gc *gc, void *fdt) +static int make_virtio_mmio_node(libxl__gc *gc, void *fdt, + uint64_t base, uint32_t irq) { int res; gic_interrupt intr; @@ -693,10 +719,10 @@ static int make_virtio_mmio_node(libxl__gc *gc, void = *fdt) if (res) return res; =20 res =3D fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS, GUEST_ROO= T_SIZE_CELLS, - 1, GUEST_VIRTIO_MMIO_BASE, GUEST_VIRTIO_MMIO_S= IZE); + 1, base, GUEST_VIRTIO_MMIO_SIZE); if (res) return res; =20 - set_interrupt(intr, GUEST_VIRTIO_MMIO_SPI, 0xf, DT_IRQ_TYPE_EDGE_RISIN= G); + set_interrupt(intr, irq, 0xf, DT_IRQ_TYPE_EDGE_RISING); res =3D fdt_property_interrupts(gc, fdt, &intr, 1); if (res) return res; =20 @@ -1010,8 +1036,19 @@ next_resize: if (info->tee =3D=3D LIBXL_TEE_TYPE_OPTEE) FDT( make_optee_node(gc, fdt) ); =20 - if (libxl_defbool_val(info->arch_arm.virtio)) - FDT( make_virtio_mmio_node(gc, fdt) ); + if (libxl_defbool_val(info->arch_arm.virtio)) { + libxl_domain_config *d_config =3D + container_of(info, libxl_domain_config, b_info); + libxl_device_virtio_disk *virtio_disk =3D &d_config->virtio_di= sks[0]; + unsigned int i; + + for (i =3D 0; i < virtio_disk->num_disks; i++) { + uint64_t base =3D virtio_disk->disks[i].base; + uint32_t irq =3D virtio_disk->disks[i].irq; + + FDT( make_virtio_mmio_node(gc, fdt, base, irq) ); + } + } =20 if (pfdt) FDT( copy_partial_fdt(gc, fdt, pfdt) ); diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index 2814818..8a0651e 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -1817,6 +1817,7 @@ const libxl__device_type *device_type_tbl[] =3D { &libxl__dtdev_devtype, &libxl__vdispl_devtype, &libxl__vsnd_devtype, + &libxl__virtio_disk_devtype, NULL }; =20 diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 94a2317..4e2024d 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -3988,6 +3988,7 @@ extern const libxl__device_type libxl__vdispl_devtype; extern const libxl__device_type libxl__p9_devtype; extern const libxl__device_type libxl__pvcallsif_devtype; extern const libxl__device_type libxl__vsnd_devtype; +extern const libxl__device_type libxl__virtio_disk_devtype; =20 extern const libxl__device_type *device_type_tbl[]; =20 diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl index b054bf9..5f8a3ff 100644 --- a/tools/libxl/libxl_types.idl +++ b/tools/libxl/libxl_types.idl @@ -935,6 +935,20 @@ libxl_device_vsnd =3D Struct("device_vsnd", [ ("pcms", Array(libxl_vsnd_pcm, "num_vsnd_pcms")) ]) =20 +libxl_virtio_disk_param =3D Struct("virtio_disk_param", [ + ("filename", string), + ("readonly", bool), + ("irq", uint32), + ("base", uint64), + ]) + +libxl_device_virtio_disk =3D Struct("device_virtio_disk", [ + ("backend_domid", libxl_domid), + ("backend_domname", string), + ("devid", libxl_devid), + ("disks", Array(libxl_virtio_disk_param, "num_disks")), + ]) + libxl_domain_config =3D Struct("domain_config", [ ("c_info", libxl_domain_create_info), ("b_info", libxl_domain_build_info), @@ -951,6 +965,7 @@ libxl_domain_config =3D Struct("domain_config", [ ("pvcallsifs", Array(libxl_device_pvcallsif, "num_pvcallsifs")), ("vdispls", Array(libxl_device_vdispl, "num_vdispls")), ("vsnds", Array(libxl_device_vsnd, "num_vsnds")), + ("virtio_disks", Array(libxl_device_virtio_disk, "num_virtio_disks")), # a channel manifests as a console with a name, # see docs/misc/channels.txt ("channels", Array(libxl_device_channel, "num_channels")), diff --git a/tools/libxl/libxl_types_internal.idl b/tools/libxl/libxl_types= _internal.idl index 3593e21..8f71980 100644 --- a/tools/libxl/libxl_types_internal.idl +++ b/tools/libxl/libxl_types_internal.idl @@ -32,6 +32,7 @@ libxl__device_kind =3D Enumeration("device_kind", [ (14, "PVCALLS"), (15, "VSND"), (16, "VINPUT"), + (17, "VIRTIO_DISK"), ]) =20 libxl__console_backend =3D Enumeration("console_backend", [ diff --git a/tools/libxl/libxl_virtio_disk.c b/tools/libxl/libxl_virtio_dis= k.c new file mode 100644 index 0000000..25e7f1a --- /dev/null +++ b/tools/libxl/libxl_virtio_disk.c @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2020 EPAM Systems Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +#include "libxl_internal.h" + +static int libxl__device_virtio_disk_setdefault(libxl__gc *gc, uint32_t do= mid, + libxl_device_virtio_disk *= virtio_disk, + bool hotplug) +{ + return libxl__resolve_domid(gc, virtio_disk->backend_domname, + &virtio_disk->backend_domid); +} + +static int libxl__virtio_disk_from_xenstore(libxl__gc *gc, const char *lib= xl_path, + libxl_devid devid, + libxl_device_virtio_disk *virt= io_disk) +{ + const char *be_path; + int rc; + + virtio_disk->devid =3D devid; + rc =3D libxl__xs_read_mandatory(gc, XBT_NULL, + GCSPRINTF("%s/backend", libxl_path), + &be_path); + if (rc) return rc; + + rc =3D libxl__backendpath_parse_domid(gc, be_path, &virtio_disk->backe= nd_domid); + if (rc) return rc; + + return 0; +} + +static void libxl__update_config_virtio_disk(libxl__gc *gc, + libxl_device_virtio_disk *dst, + libxl_device_virtio_disk *src) +{ + dst->devid =3D src->devid; +} + +static int libxl_device_virtio_disk_compare(libxl_device_virtio_disk *d1, + libxl_device_virtio_disk *d2) +{ + return COMPARE_DEVID(d1, d2); +} + +static void libxl__device_virtio_disk_add(libxl__egc *egc, uint32_t domid, + libxl_device_virtio_disk *virtio= _disk, + libxl__ao_device *aodev) +{ + libxl__device_add_async(egc, domid, &libxl__virtio_disk_devtype, virti= o_disk, aodev); +} + +static int libxl__set_xenstore_virtio_disk(libxl__gc *gc, uint32_t domid, + libxl_device_virtio_disk *virti= o_disk, + flexarray_t *back, flexarray_t = *front, + flexarray_t *ro_front) +{ + int rc; + unsigned int i; + + for (i =3D 0; i < virtio_disk->num_disks; i++) { + rc =3D flexarray_append_pair(ro_front, GCSPRINTF("%d/filename", i), + GCSPRINTF("%s", virtio_disk->disks[i].f= ilename)); + if (rc) return rc; + + rc =3D flexarray_append_pair(ro_front, GCSPRINTF("%d/readonly", i), + GCSPRINTF("%d", virtio_disk->disks[i].r= eadonly)); + if (rc) return rc; + + rc =3D flexarray_append_pair(ro_front, GCSPRINTF("%d/base", i), + GCSPRINTF("%lu", virtio_disk->disks[i].= base)); + if (rc) return rc; + + rc =3D flexarray_append_pair(ro_front, GCSPRINTF("%d/irq", i), + GCSPRINTF("%u", virtio_disk->disks[i].i= rq)); + if (rc) return rc; + } + + return 0; +} + +static LIBXL_DEFINE_UPDATE_DEVID(virtio_disk) +static LIBXL_DEFINE_DEVICE_FROM_TYPE(virtio_disk) +static LIBXL_DEFINE_DEVICES_ADD(virtio_disk) + +DEFINE_DEVICE_TYPE_STRUCT(virtio_disk, VIRTIO_DISK, + .update_config =3D (device_update_config_fn_t) libxl__update_config_vi= rtio_disk, + .from_xenstore =3D (device_from_xenstore_fn_t) libxl__virtio_disk_from= _xenstore, + .set_xenstore_config =3D (device_set_xenstore_config_fn_t) libxl__set_= xenstore_virtio_disk +); + +/* + * Local variables: + * mode: C + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/tools/xl/Makefile b/tools/xl/Makefile index af4912e..38e4701 100644 --- a/tools/xl/Makefile +++ b/tools/xl/Makefile @@ -22,7 +22,7 @@ XL_OBJS +=3D xl_vtpm.o xl_block.o xl_nic.o xl_usb.o XL_OBJS +=3D xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o XL_OBJS +=3D xl_info.o xl_console.o xl_misc.o XL_OBJS +=3D xl_vmcontrol.o xl_saverestore.o xl_migrate.o -XL_OBJS +=3D xl_vdispl.o xl_vsnd.o xl_vkb.o +XL_OBJS +=3D xl_vdispl.o xl_vsnd.o xl_vkb.o xl_virtio_disk.o =20 $(XL_OBJS): CFLAGS +=3D $(CFLAGS_libxentoollog) $(XL_OBJS): CFLAGS +=3D $(CFLAGS_XL) diff --git a/tools/xl/xl.h b/tools/xl/xl.h index 06569c6..3d26f19 100644 --- a/tools/xl/xl.h +++ b/tools/xl/xl.h @@ -178,6 +178,9 @@ int main_vsnddetach(int argc, char **argv); int main_vkbattach(int argc, char **argv); int main_vkblist(int argc, char **argv); int main_vkbdetach(int argc, char **argv); +int main_virtio_diskattach(int argc, char **argv); +int main_virtio_disklist(int argc, char **argv); +int main_virtio_diskdetach(int argc, char **argv); int main_usbctrl_attach(int argc, char **argv); int main_usbctrl_detach(int argc, char **argv); int main_usbdev_attach(int argc, char **argv); diff --git a/tools/xl/xl_cmdtable.c b/tools/xl/xl_cmdtable.c index 0833539..2bdf0b7 100644 --- a/tools/xl/xl_cmdtable.c +++ b/tools/xl/xl_cmdtable.c @@ -434,6 +434,21 @@ struct cmd_spec cmd_table[] =3D { "Destroy a domain's virtual sound device", " ", }, + { "virtio-disk-attach", + &main_virtio_diskattach, 1, 1, + "Create a new virtio block device", + " TBD\n" + }, + { "virtio-disk-list", + &main_virtio_disklist, 0, 0, + "List virtio block devices for a domain", + "", + }, + { "virtio-disk-detach", + &main_virtio_diskdetach, 0, 1, + "Destroy a domain's virtio block device", + " ", + }, { "uptime", &main_uptime, 0, 0, "Print uptime for all/some domains", diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index b8306aa..fd72109 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -1202,6 +1202,120 @@ out: if (rc) exit(EXIT_FAILURE); } =20 +#define MAX_VIRTIO_DISKS 4 + +static int parse_virtio_disk_config(libxl_device_virtio_disk *virtio_disk,= char *token) +{ + char *oparg; + libxl_string_list disks =3D NULL; + int i, rc; + + if (MATCH_OPTION("backend", token, oparg)) { + virtio_disk->backend_domname =3D strdup(oparg); + } else if (MATCH_OPTION("disks", token, oparg)) { + split_string_into_string_list(oparg, ";", &disks); + + virtio_disk->num_disks =3D libxl_string_list_length(&disks); + if (virtio_disk->num_disks > MAX_VIRTIO_DISKS) { + fprintf(stderr, "vdisk: currently only %d disks are supported", + MAX_VIRTIO_DISKS); + return 1; + } + virtio_disk->disks =3D xcalloc(virtio_disk->num_disks, + sizeof(*virtio_disk->disks)); + + for(i =3D 0; i < virtio_disk->num_disks; i++) { + char *disk_opt; + + rc =3D split_string_into_pair(disks[i], ":", &disk_opt, + &virtio_disk->disks[i].filename); + if (rc) { + fprintf(stderr, "vdisk: failed to split \"%s\" into pair\n= ", + disks[i]); + goto out; + } + + if (!strcmp(disk_opt, "ro")) + virtio_disk->disks[i].readonly =3D 1; + else if (!strcmp(disk_opt, "rw")) + virtio_disk->disks[i].readonly =3D 0; + else { + fprintf(stderr, "vdisk: failed to parse \"%s\" disk option= \n", + disk_opt); + rc =3D 1; + } + free(disk_opt); + + if (rc) goto out; + } + } else { + fprintf(stderr, "Unknown string \"%s\" in vdisk spec\n", token); + rc =3D 1; goto out; + } + + rc =3D 0; + +out: + libxl_string_list_dispose(&disks); + return rc; +} + +static void parse_virtio_disk_list(const XLU_Config *config, + libxl_domain_config *d_config) +{ + XLU_ConfigList *virtio_disks; + const char *item; + char *buf =3D NULL; + int rc; + + if (!xlu_cfg_get_list (config, "vdisk", &virtio_disks, 0, 0)) { + libxl_domain_build_info *b_info =3D &d_config->b_info; + int entry =3D 0; + + /* XXX Remove an extra property */ + libxl_defbool_setdefault(&b_info->arch_arm.virtio, false); + if (!libxl_defbool_val(b_info->arch_arm.virtio)) { + fprintf(stderr, "Virtio device requires Virtio property to be = set\n"); + exit(EXIT_FAILURE); + } + + while ((item =3D xlu_cfg_get_listitem(virtio_disks, entry)) !=3D N= ULL) { + libxl_device_virtio_disk *virtio_disk; + char *p; + + virtio_disk =3D ARRAY_EXTEND_INIT(d_config->virtio_disks, + d_config->num_virtio_disks, + libxl_device_virtio_disk_init); + + buf =3D strdup(item); + + p =3D strtok (buf, ","); + while (p !=3D NULL) + { + while (*p =3D=3D ' ') p++; + + rc =3D parse_virtio_disk_config(virtio_disk, p); + if (rc) goto out; + + p =3D strtok (NULL, ","); + } + + entry++; + + if (virtio_disk->num_disks =3D=3D 0) { + fprintf(stderr, "At least one virtio disk should be specif= ied\n"); + rc =3D 1; goto out; + } + } + } + + rc =3D 0; + +out: + free(buf); + if (rc) exit(EXIT_FAILURE); +} + void parse_config_data(const char *config_source, const char *config_data, int config_len, @@ -2732,6 +2846,7 @@ skip_usbdev: } =20 parse_vkb_list(config, d_config); + parse_virtio_disk_list(config, d_config); =20 xlu_cfg_get_defbool(config, "xend_suspend_evtchn_compat", &c_info->xend_suspend_evtchn_compat, 0); diff --git a/tools/xl/xl_virtio_disk.c b/tools/xl/xl_virtio_disk.c new file mode 100644 index 0000000..808a7da --- /dev/null +++ b/tools/xl/xl_virtio_disk.c @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2020 EPAM Systems Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +#include + +#include +#include +#include + +#include "xl.h" +#include "xl_utils.h" +#include "xl_parse.h" + +int main_virtio_diskattach(int argc, char **argv) +{ + return 0; +} + +int main_virtio_disklist(int argc, char **argv) +{ + return 0; +} + +int main_virtio_diskdetach(int argc, char **argv) +{ + return 0; +} + +/* + * Local variables: + * mode: C + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596479482; cv=none; d=zohomail.com; s=zohoarc; b=JWnjLKwXghz0hFBKQTnBIJOPEHCKKJ93Wd+cGY21aoNtPdiGeM3w13DugkXSXlvWDGtGripy2oNxIyl5cWdf4pdDJb4pc2MJSppj9o4l1JYnZnHVyed8x/GP1gwuiA2oUMBlgxxlmQeG2rsmEJSXNEMEtSp+aQ8muyiXm/0oKmY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596479482; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=Dr2Tb8xNL8Zd44BZpRkZWI6gE4U1QZUhIvG9zNQ9PsI=; b=Ttrgk6fJPT4T39CsbDs8GtxFzgJamRv5rmYw73QQXyCBqLP8EkbpMFDwtXpll0xKFzlPtfVlKoHAHuNbkwVfjuKvfwYKB209k3zIfQlxmB9AWJLE0jFdZezIWXoFDoOC7UiwS4+ts+ssP+8/0/BhdyiWgc1vZcg85asD44iRqFU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596479482586241.30879957109494; Mon, 3 Aug 2020 11:31:22 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2fEn-0001ZB-Rj; Mon, 03 Aug 2020 18:31:05 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6a-0008F4-7x for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:36 +0000 Received: from mail-lj1-x241.google.com (unknown [2a00:1450:4864:20::241]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 36cf2a99-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:57 +0000 (UTC) Received: by mail-lj1-x241.google.com with SMTP id z14so3126925ljm.1 for ; Mon, 03 Aug 2020 11:21:57 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:55 -0700 (PDT) X-Inumbo-ID: 36cf2a99-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Dr2Tb8xNL8Zd44BZpRkZWI6gE4U1QZUhIvG9zNQ9PsI=; b=S4fDFMYUo5BvRyBoYeUyXtrUL2wsl88uUzfLHup9REJqHQU6+5cxkMSy8GXKdCon0i L80wX4sKQDnu4EmrpVeh0MsK2uTWek4a7p4nU35hXmuZlH/WCSx/YXxzHrV9q/4nvqgj fIVLNzLCM4+8QqBoqUL3/z642rYSqAvQYcytvE7A/Q3JHR6UXO8b2vU+M6r+mGXatGUz /R0KKRA0+n4kSD8aXKKDDI01t+9iVRLWS0822Z3GfxMhtJx35KgVhiib7Xgwz5Vb08jN GyTGLWdC3/dRurzDFE0ONX/nvLoD3qxP/idC6x8jsZSAgFQfWeNDhoP6sTOUvrWKpZS+ EHHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Dr2Tb8xNL8Zd44BZpRkZWI6gE4U1QZUhIvG9zNQ9PsI=; b=k4HesusM6yhlO1QLpNYMu3g58ZiRk3uqkxGTzyNFKaZfPQVlavWAoJbD2RwM+TAZqz 8oOltKzCDvGvaMBx/jQ3meijXDjlrvv0arlS0N0jj3kevRF8c6xafvlq3jp/UTE+5+az Za7MTw+Lv4qacvOCG8mTnHaFhkx70zr1XCU3q020PT/5W1cbDDyAlq/KrwDNOdjrWJr8 PLFkfbRLkr75MyOvmB44MDb1ibLcki2J5/Pu1QJEU99gJTE9YuMVoizUvuYXqyzBYLFy iaSEL0R9sr3byjr7SSPO4OrIfUUZsMrlDngnfsuRxLCgxFUbnvCaWHiujcqoy2MU60QT 5j6Q== X-Gm-Message-State: AOAM531LHxm7nANe41MOhaYm7HQAtGKMMQzAsKG84rF+Q7Wq3ToznXb9 8FCZG8DK4ZFNwtrSKCB5XdEuF+Qh2kc= X-Google-Smtp-Source: ABdhPJx0T4FW5vY5u1CcVrgLb849G9GBkehpIIWB2wGYtYkUD+nzSAKb+0ckDVMOZMkooigwBd884Q== X-Received: by 2002:a05:651c:201b:: with SMTP id s27mr8503674ljo.468.1596478916403; Mon, 03 Aug 2020 11:21:56 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 11/12] libxl: Insert "dma-coherent" property into virtio-mmio device node Date: Mon, 3 Aug 2020 21:21:27 +0300 Message-Id: <1596478888-23030-12-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Ian Jackson , Oleksandr Tyshchenko , Anthony PERARD Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Without "dma-coherent" property present in virtio-mmio device node, guest assumes it is non-coherent and making non-cacheable accesses to the vring when the DMA API is used for vring operations. But virtio-mmio device which runs at the host size is making cacheable accesses to vring. This all may result in a loss of coherency between the guest and host. With this patch we can avoid modifying guest at all, otherwise we need to force VirtIO framework to not use DMA API for vring operations. Signed-off-by: Oleksandr Tyshchenko --- tools/libxl/libxl_arm.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c index 469a8b0..a68fb14 100644 --- a/tools/libxl/libxl_arm.c +++ b/tools/libxl/libxl_arm.c @@ -726,6 +726,9 @@ static int make_virtio_mmio_node(libxl__gc *gc, void *f= dt, res =3D fdt_property_interrupts(gc, fdt, &intr, 1); if (res) return res; =20 + res =3D fdt_property(fdt, "dma-coherent", NULL, 0); + if (res) return res; + res =3D fdt_end_node(fdt); if (res) return res; =20 --=20 2.7.4 From nobody Sun May 5 15:57:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1596479483; cv=none; d=zohomail.com; s=zohoarc; b=fdd1atZVtGMTMRh82IopFaleKyM9aLYiP+5jop201xtywrDTy8kK67DMo4mmZaBdExGxp4xXhTWItdfY5QJ98nC3z/IcT1LrSlSzDHFsHZ/IEsYAM3vS17RDzM9ZkP1wjMF6C3spKxhKdzYXHX671aHMd/Mq8kNkEaKcUD/zNTk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596479483; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=1DZeLDeQLAW3wDtg4nsTIUzMiSJtyCyrnnqQeeQIfZc=; b=Sk58N8bS3EN3yhXid5Bc2FDHutjBFRv8aGOvMFK/uxRZKES/NY0gNDvCrMy9bzz329kClGeBkYotttPPS+s2Y9F+Qh6bpI/SZwzR9VjrAlCBUV8mW1WykiRkvhsA8KeUwhL7P5B/9TkLMx4kwcHP4S/g3NqYm5TV0BEcU5FIGBc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1596479483255639.0932475173388; Mon, 3 Aug 2020 11:31:23 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2fEn-0001Z5-Js; Mon, 03 Aug 2020 18:31:05 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k2f6k-0008F4-8O for xen-devel@lists.xenproject.org; Mon, 03 Aug 2020 18:22:46 +0000 Received: from mail-lf1-x144.google.com (unknown [2a00:1450:4864:20::144]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 377cf859-d5b6-11ea-90c4-bc764e2007e4; Mon, 03 Aug 2020 18:21:58 +0000 (UTC) Received: by mail-lf1-x144.google.com with SMTP id m15so20303516lfp.7 for ; Mon, 03 Aug 2020 11:21:58 -0700 (PDT) Received: from otyshchenko.www.tendawifi.com ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id h18sm4486730ljk.7.2020.08.03.11.21.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2020 11:21:56 -0700 (PDT) X-Inumbo-ID: 377cf859-d5b6-11ea-90c4-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1DZeLDeQLAW3wDtg4nsTIUzMiSJtyCyrnnqQeeQIfZc=; b=l5r/G7XizMtl4Jo6THLgp7+OQ05dU8uylAR5kvt92eMz9hNK9Wx9iJfWx1sOkeXew/ dDdjL8kxdVKO/5J/ePv2UXhqUvgsMh92fR2c8HAexuAQ7HwpD5cM3l1R+bG+WidbLDQF a+2b7FLpvBsZl4fYu4Pzb41NLeN2NrwTjfDvaWlZ8vaBkSthTZNZodBHuO/vxcGDIRaP 9ZwEtQYArHZvgwpAkrLFip40/rvpfJvmuceHnh4u/ceo0ASjfq/4jQh0AvZis+PktZt1 3i5tnB0Y89SS1+fRHFdv8sw30HZoBCEh5KnE9XCYEMMBdwfkJOyy6wF+tY/bMqnoy9L6 1b9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1DZeLDeQLAW3wDtg4nsTIUzMiSJtyCyrnnqQeeQIfZc=; b=cLLPGX9hryIFtHJyeyAvpiVU2hCuNQZPNeEFbI4K0Ydo7SjKSghdLa+n6VaQqFTxYt 4cJotnvCiQNprB9bI1R/osUVRXc/8SnbLWALq4jIM4y5FbSakJjJRCyDlrzNWtNwn6bY OeCzW+NDmWbN0uFOT08Y9XDS3HIfiRlqIsppkC0bW1h/qnOt2LzUi231WaUFHvoVD4hB wQpP6xLyCvOHaUWVFPqRNAiuao1hGDABLNVtGrWeipwWlhm55xgcTodwiTcFS/4pvKGH ePthIijfwYcaBlIj9crynQJojQB2vqpDITtN8yAd0eSWQjiQf1/mW+y7nnNylAAK/SeZ cd4w== X-Gm-Message-State: AOAM531JOnCLJOL448rqK6w2cn+k/A1XvKTNMFGHLGqXh0bnvAXQG1vn vVC5Y8ZLdmbP+pQdfcEyEOTk5hSzR4g= X-Google-Smtp-Source: ABdhPJytiiD7l56b7Vm/NaSQXoaXOLDxnIvoSE8ZvLGZHZEUatUZoCFu/Kmqn5PTbDoeaT2KzUFD3A== X-Received: by 2002:ac2:598f:: with SMTP id w15mr9054121lfn.216.1596478917357; Mon, 03 Aug 2020 11:21:57 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Subject: [RFC PATCH V1 12/12] libxl: Fix duplicate memory node in DT Date: Mon, 3 Aug 2020 21:21:28 +0300 Message-Id: <1596478888-23030-13-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Ian Jackson , Oleksandr Tyshchenko , Anthony PERARD Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko When two or more VirtIO devices are passed to DomU the following message is observed: OF: Duplicate name in base, renamed to "virtio#1" Signed-off-by: Oleksandr Tyshchenko --- tools/libxl/libxl_arm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c index a68fb14..9671a44 100644 --- a/tools/libxl/libxl_arm.c +++ b/tools/libxl/libxl_arm.c @@ -710,9 +710,11 @@ static int make_virtio_mmio_node(libxl__gc *gc, void *= fdt, { int res; gic_interrupt intr; + /* Placeholder for virtio@ + a 64-bit number + \0 */ + char buf[24]; =20 - /* XXX: Add address in the node name */ - res =3D fdt_begin_node(fdt, "virtio"); + snprintf(buf, sizeof(buf), "virtio@%"PRIx64, base); + res =3D fdt_begin_node(fdt, buf); if (res) return res; =20 res =3D fdt_property_compat(gc, fdt, 1, "virtio,mmio"); --=20 2.7.4