From patchwork Wed Jul 20 16:49:16 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Avi Kivity X-Patchwork-Id: 105871 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id EE7B7B6F80 for ; Thu, 21 Jul 2011 11:54:28 +1000 (EST) Received: from localhost ([::1]:38379 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QjdsW-0005CP-0H for incoming@patchwork.ozlabs.org; Wed, 20 Jul 2011 17:00:24 -0400 Received: from eggs.gnu.org ([140.186.70.92]:60748) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QjaAb-0008Ed-Lb for qemu-devel@nongnu.org; Wed, 20 Jul 2011 13:02:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QjZyv-0004w9-SQ for qemu-devel@nongnu.org; Wed, 20 Jul 2011 12:50:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42346) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QjZys-0004tk-W5 for qemu-devel@nongnu.org; Wed, 20 Jul 2011 12:50:43 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p6KGogWI022638 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 20 Jul 2011 12:50:42 -0400 Received: from cleopatra.tlv.redhat.com (cleopatra.tlv.redhat.com [10.35.255.11]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p6KGofYE006704; Wed, 20 Jul 2011 12:50:41 -0400 Received: from s01.tlv.redhat.com (s01.tlv.redhat.com [10.35.255.8]) by cleopatra.tlv.redhat.com (Postfix) with ESMTP id 04835250B38; Wed, 20 Jul 2011 19:50:39 +0300 (IDT) From: Avi Kivity To: qemu-devel@nongnu.org Date: Wed, 20 Jul 2011 19:49:16 +0300 Message-Id: <1311180636-17012-7-git-send-email-avi@redhat.com> In-Reply-To: <1311180636-17012-1-git-send-email-avi@redhat.com> References: <1311180636-17012-1-git-send-email-avi@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: kvm@vger.kernel.org Subject: [Qemu-devel] [RFC v5 06/86] memory: abstract address space operations X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Prepare for multiple address space support by abstracting away the details of registering a memory range with qemu's flat representation into an AddressSpace object. Note operations which are memory specific are not abstracted, since they will never be called on I/O address spaces anyway. Signed-off-by: Avi Kivity --- memory.c | 111 +++++++++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 81 insertions(+), 30 deletions(-) diff --git a/memory.c b/memory.c index 48470d2..7187223 100644 --- a/memory.c +++ b/memory.c @@ -82,12 +82,26 @@ struct FlatView { unsigned nr_allocated; }; +typedef struct AddressSpace AddressSpace; +typedef struct AddressSpaceOps AddressSpaceOps; + +/* A system address space - I/O, memory, etc. */ +struct AddressSpace { + const AddressSpaceOps *ops; + MemoryRegion *root; + FlatView current_map; +}; + +struct AddressSpaceOps { + void (*range_add)(AddressSpace *as, FlatRange *fr); + void (*range_del)(AddressSpace *as, FlatRange *fr); + void (*log_start)(AddressSpace *as, FlatRange *fr); + void (*log_stop)(AddressSpace *as, FlatRange *fr); +}; + #define FOR_EACH_FLAT_RANGE(var, view) \ for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var) -static FlatView current_memory_map; -static MemoryRegion *root_memory_region; - static bool flatrange_equal(FlatRange *a, FlatRange *b) { return a->mr == b->mr @@ -144,6 +158,54 @@ static void flatview_simplify(FlatView *view) } } +static void as_memory_range_add(AddressSpace *as, FlatRange *fr) +{ + ram_addr_t phys_offset, region_offset; + + phys_offset = fr->mr->ram_addr; + region_offset = fr->offset_in_region; + /* cpu_register_physical_memory_log() wants region_offset for + * mmio, but prefers offseting phys_offset for RAM. Humour it. + */ + if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { + phys_offset += region_offset; + region_offset = 0; + } + + cpu_register_physical_memory_log(fr->addr.start, + fr->addr.size, + phys_offset, + region_offset, + fr->dirty_log_mask); +} + +static void as_memory_range_del(AddressSpace *as, FlatRange *fr) +{ + cpu_register_physical_memory(fr->addr.start, fr->addr.size, + IO_MEM_UNASSIGNED); +} + +static void as_memory_log_start(AddressSpace *as, FlatRange *fr) +{ + cpu_physical_log_start(fr->addr.start, fr->addr.size); +} + +static void as_memory_log_stop(AddressSpace *as, FlatRange *fr) +{ + cpu_physical_log_stop(fr->addr.start, fr->addr.size); +} + +static const AddressSpaceOps address_space_ops_memory = { + .range_add = as_memory_range_add, + .range_del = as_memory_range_del, + .log_start = as_memory_log_start, + .log_stop = as_memory_log_stop, +}; + +static AddressSpace address_space_memory = { + .ops = &address_space_ops_memory, +}; + /* Render a memory region into the global view. Ranges in @view obscure * ranges in @mr. */ @@ -236,13 +298,12 @@ static FlatView generate_memory_topology(MemoryRegion *mr) return view; } -static void memory_region_update_topology(void) +static void address_space_update_topology(AddressSpace *as) { - FlatView old_view = current_memory_map; - FlatView new_view = generate_memory_topology(root_memory_region); + FlatView old_view = as->current_map; + FlatView new_view = generate_memory_topology(as->root); unsigned iold, inew; FlatRange *frold, *frnew; - ram_addr_t phys_offset, region_offset; /* Generate a symmetric difference of the old and new memory maps. * Kill ranges in the old map, and instantiate ranges in the new map. @@ -267,16 +328,15 @@ static void memory_region_update_topology(void) && !flatrange_equal(frold, frnew)))) { /* In old, but (not in new, or in new but attributes changed). */ - cpu_register_physical_memory(frold->addr.start, frold->addr.size, - IO_MEM_UNASSIGNED); + as->ops->range_del(as, frold); ++iold; } else if (frold && frnew && flatrange_equal(frold, frnew)) { /* In both (logging may have changed) */ if (frold->dirty_log_mask && !frnew->dirty_log_mask) { - cpu_physical_log_stop(frnew->addr.start, frnew->addr.size); + as->ops->log_stop(as, frnew); } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) { - cpu_physical_log_start(frnew->addr.start, frnew->addr.size); + as->ops->log_start(as, frnew); } ++iold; @@ -284,28 +344,19 @@ static void memory_region_update_topology(void) } else { /* In new */ - phys_offset = frnew->mr->ram_addr; - region_offset = frnew->offset_in_region; - /* cpu_register_physical_memory_log() wants region_offset for - * mmio, but prefers offseting phys_offset for RAM. Humour it. - */ - if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) { - phys_offset += region_offset; - region_offset = 0; - } - - cpu_register_physical_memory_log(frnew->addr.start, - frnew->addr.size, - phys_offset, - region_offset, - frnew->dirty_log_mask); + as->ops->range_add(as, frnew); ++inew; } } - current_memory_map = new_view; + as->current_map = new_view; flatview_destroy(&old_view); } +static void memory_region_update_topology(void) +{ + address_space_update_topology(&address_space_memory); +} + void memory_region_init(MemoryRegion *mr, const char *name, uint64_t size) @@ -551,7 +602,7 @@ void memory_region_sync_dirty_bitmap(MemoryRegion *mr) { FlatRange *fr; - FOR_EACH_FLAT_RANGE(fr, ¤t_memory_map) { + FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) { if (fr->mr == mr) { cpu_physical_sync_dirty_bitmap(fr->addr.start, fr->addr.start + fr->addr.size); @@ -592,7 +643,7 @@ static void memory_region_update_coalesced_range(MemoryRegion *mr) CoalescedMemoryRange *cmr; AddrRange tmp; - FOR_EACH_FLAT_RANGE(fr, ¤t_memory_map) { + FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) { if (fr->mr == mr) { qemu_unregister_coalesced_mmio(fr->addr.start, fr->addr.size); QTAILQ_FOREACH(cmr, &mr->coalesced, link) { @@ -702,6 +753,6 @@ void memory_region_del_subregion(MemoryRegion *mr, void set_system_memory_map(MemoryRegion *mr) { - root_memory_region = mr; + address_space_memory.root = mr; memory_region_update_topology(); }