From patchwork Fri Feb 3 17:06:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?S09OUkFEIEZyw6lkw6lyaWM=?= X-Patchwork-Id: 9554713 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5278760424 for ; Fri, 3 Feb 2017 17:12:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 54AAF28178 for ; Fri, 3 Feb 2017 17:12:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4617328427; Fri, 3 Feb 2017 17:12:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 858C228178 for ; Fri, 3 Feb 2017 17:12:29 +0000 (UTC) Received: from localhost ([::1]:35964 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZhPQ-0008WA-3X for patchwork-qemu-devel@patchwork.kernel.org; Fri, 03 Feb 2017 12:12:28 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60792) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZhKX-0004fA-9A for qemu-devel@nongnu.org; Fri, 03 Feb 2017 12:07:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZhKW-0002gh-7V for qemu-devel@nongnu.org; Fri, 03 Feb 2017 12:07:25 -0500 Received: from greensocs.com ([193.104.36.180]:39100) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZhKV-0002gW-TB for qemu-devel@nongnu.org; Fri, 03 Feb 2017 12:07:24 -0500 Received: from localhost (localhost [127.0.0.1]) by greensocs.com (Postfix) with ESMTP id 1D0F42E8DF7; Fri, 3 Feb 2017 18:07:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1486141643; bh=FdcjwPToYfxHdYzOk9jUuzLQxYMJ3/L7aasG6TU4keo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Ri6u0Z0F6CfTdsQsjvLnbfkPLrVoxG53kgoZoTgp2bBFQSn6xq/lvb0X9S7GUjPUz YZiQEIKSa3YQhgXLwDjw49A8AGm6Ls7Q4eD1OGQv4qxd5+CP1SY74GpwXD70CnukTe F5d2sU9sfExQaUyxv1PaLfb+fGIEhYlYdOebwKdA= X-Virus-Scanned: amavisd-new at greensocs.com Authentication-Results: gs-01.greensocs.com (amavisd-new); dkim=pass (1024-bit key) header.d=greensocs.com header.b=Eawz2gkL; dkim=pass (1024-bit key) header.d=greensocs.com header.b=Eawz2gkL Received: from greensocs.com ([127.0.0.1]) by localhost (gs-01.greensocs.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id EFRqM2MT5g8W; Fri, 3 Feb 2017 18:07:22 +0100 (CET) Received: by greensocs.com (Postfix, from userid 998) id CAB2D2E8DC5; Fri, 3 Feb 2017 18:07:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1486141641; bh=FdcjwPToYfxHdYzOk9jUuzLQxYMJ3/L7aasG6TU4keo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Eawz2gkLekWYzEmg7P9sCxnXn9g7sVpLCBmUBL7ungZMHuAUjZC+dpMxGEuXCncj0 YdmLjF1xzZPAcQSVjFB3zlt6Wc+VIjcWohP8gsy4en0hRlWy6O4xVILrAuwSNbwoBu /OQPm2dXDtkIlUnpoR2DlsM6MzvAko3rUlx+2MKg= Received: from corsair.home (bd231-7-88-127-3-24.fbx.proxad.net [88.127.3.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: fred.konrad@greensocs.com) by greensocs.com (Postfix) with ESMTPSA id DB4AD164DB9; Fri, 3 Feb 2017 18:07:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1486141641; bh=FdcjwPToYfxHdYzOk9jUuzLQxYMJ3/L7aasG6TU4keo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Eawz2gkLekWYzEmg7P9sCxnXn9g7sVpLCBmUBL7ungZMHuAUjZC+dpMxGEuXCncj0 YdmLjF1xzZPAcQSVjFB3zlt6Wc+VIjcWohP8gsy4en0hRlWy6O4xVILrAuwSNbwoBu /OQPm2dXDtkIlUnpoR2DlsM6MzvAko3rUlx+2MKg= From: fred.konrad@greensocs.com To: qemu-devel@nongnu.org Date: Fri, 3 Feb 2017 18:06:36 +0100 Message-Id: <1486141597-13941-5-git-send-email-fred.konrad@greensocs.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1486141597-13941-1-git-send-email-fred.konrad@greensocs.com> References: <1486141597-13941-1-git-send-email-fred.konrad@greensocs.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 193.104.36.180 Subject: [Qemu-devel] [RFC 4/5] exec: allow to get a pointer for some mmio memory region X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: edgar.iglesias@xilinx.com, peter.maydell@linaro.org, mark.burton@greensocs.com, alistair.francis@xilinx.com, clg@kaod.org, pbonzini@redhat.com, fred.konrad@greensocs.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: KONRAD Frederic This introduces a special callback which allows to run code from some MMIO devices. SysBusDevice with a MemoryRegion which implements the request_ptr callback will be notified when the guest try to execute code from their offset. Then it will be able to eg: pre-load some code from an SPI device or ask a pointer from an external simulator, etc.. When the pointer or the data in it are no longer valid the device has to invalidate it. Signed-off-by: KONRAD Frederic --- cputlb.c | 7 +++++++ include/exec/memory.h | 35 +++++++++++++++++++++++++++++++++++ memory.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/cputlb.c b/cputlb.c index 846341e..9077247 100644 --- a/cputlb.c +++ b/cputlb.c @@ -545,6 +545,13 @@ tb_page_addr_t get_page_addr_code(CPUArchState *env, target_ulong addr) if (memory_region_is_unassigned(mr)) { CPUClass *cc = CPU_GET_CLASS(cpu); + if (memory_region_request_mmio_ptr(mr, addr)) { + /* A MemoryRegion is potentially added so re-run the + * get_page_addr_code. + */ + return get_page_addr_code(env, addr); + } + if (cc->do_unassigned_access) { cc->do_unassigned_access(cpu, addr, false, true, 0, 4); } else { diff --git a/include/exec/memory.h b/include/exec/memory.h index 987f925..36b0eec 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -120,6 +120,15 @@ struct MemoryRegionOps { uint64_t data, unsigned size, MemTxAttrs attrs); + /* Instruction execution pre-callback: + * @addr is the address of the access relative to the @mr. + * @size is the size of the area returned by the callback. + * @offset is the location of the pointer inside @mr. + * + * Returns a pointer to a location which contains guest code. + */ + void *(*request_ptr)(void *opaque, hwaddr addr, unsigned *size, + unsigned *offset); enum device_endian endianness; /* Guest-visible constraints: */ @@ -1253,6 +1262,32 @@ void memory_global_dirty_log_stop(void); void mtree_info(fprintf_function mon_printf, void *f, bool flatview); /** + * memory_region_request_mmio_ptr: request a pointer to an mmio + * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer. + * When the device wants to invalidate the pointer it will call + * memory_region_invalidate_mmio_ptr. + * + * @mr: #MemoryRegion to check + * @addr: address within that region + * + * Returns true on success, false otherwise. + */ +bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr); + +/** + * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio + * previously requested. + * In the end that means that if something wants to execute from this area it + * will need to request the pointer again. + * + * @mr: #MemoryRegion associated to the pointer. + * @addr: address within that region + * @size: size of that area. + */ +void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset, + unsigned size); + +/** * memory_region_dispatch_read: perform a read directly to the specified * MemoryRegion. * diff --git a/memory.c b/memory.c index 6c58373..eb3e8ec 100644 --- a/memory.c +++ b/memory.c @@ -2375,6 +2375,51 @@ void memory_listener_unregister(MemoryListener *listener) QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as); } +bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr) +{ + void *host; + unsigned size = 0; + unsigned offset = 0; + MemoryRegion *sub; + + if (!mr || !mr->ops->request_ptr) { + return false; + } + + /* + * Avoid an update if the request_ptr call + * memory_region_invalidate_mmio_ptr which seems to be likely when we use + * a cache. + */ + memory_region_transaction_begin(); + + host = mr->ops->request_ptr(mr->opaque, addr - mr->addr, &size, &offset); + + if (!host || !size) { + memory_region_transaction_commit(); + return false; + } + + sub = g_new(MemoryRegion, 1); + memory_region_init_ram_ptr(sub, OBJECT(mr), "mmio-map", size, host); + memory_region_add_subregion(mr, offset, sub); + memory_region_transaction_commit(); + return true; +} + +void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset, + unsigned size) +{ + MemoryRegionSection section = memory_region_find(mr, offset, size); + + if (section.mr != mr) { + memory_region_del_subregion(mr, section.mr); + /* memory_region_find add a ref on section.mr */ + memory_region_unref(section.mr); + object_unparent(OBJECT(section.mr)); + } +} + void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) { memory_region_ref(root);