From patchwork Thu May 26 08:49:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 9136819 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 15A99607D5 for ; Thu, 26 May 2016 08:53:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0A705281D4 for ; Thu, 26 May 2016 08:53:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F37B2282BB; Thu, 26 May 2016 08:53:56 +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 379CF281D4 for ; Thu, 26 May 2016 08:53:56 +0000 (UTC) Received: from localhost ([::1]:37462 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5r3D-0000QF-B5 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 26 May 2016 04:53:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48891) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5qzM-0005m6-P5 for qemu-devel@nongnu.org; Thu, 26 May 2016 04:49:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5qzK-0007Nh-7Y for qemu-devel@nongnu.org; Thu, 26 May 2016 04:49:55 -0400 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:35077) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5qzJ-0007Ml-Sb for qemu-devel@nongnu.org; Thu, 26 May 2016 04:49:54 -0400 Received: by mail-wm0-x244.google.com with SMTP id e3so3186235wme.2 for ; Thu, 26 May 2016 01:49:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=80Kj7HpUtmtYUfTzhT71QmshFD7YxoY0YXaqK+Ti6qs=; b=DX8RR6HyA15Ddt6orq1OxvpaRrjH3/Dc8D/wL66IdLU8+DyRAggzF1TuV9XA6wsUGj /tNmGGUZzpPniZjMrmzAxAB/y4mZJKD/blsZWNUUb8i+3XJyerS0VipLqEsYD2ufMnTT 07I6n6LSwf9/IoYw+Si0gvgNh+JnnF8iANyNX//yt2eP/gTYpZ1dpx+G56gNzP5drCYd wyho8wcqcxxdMdjjClhuaUcfPjJiGbGOxDnFHW1s5T9OiKKr58psmz0NhTUyq0W5LAsb 2Zlk78fJpuPv0sBd+t8N7kF9+TY4X0sbPOZBAYEcxlBU15X7lKiccs00MoHRSIvXtdIm uUsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=80Kj7HpUtmtYUfTzhT71QmshFD7YxoY0YXaqK+Ti6qs=; b=ORHiwSwGdrwUinDSVGYamEvul64IingQRn+rRS5YjxFpnSyjh0sWMak3JxA0mn4uyG whPox8srnAwFbI4XsvPjSvcZZM59GnPihjmXtXc5iAAjXvaKhLvaoqa9o/67ZDg5iEuS 28E64mWEdSFLwBlnT7Rp0Ll280gfJp5oDCKso4h9OCwhXZYiH2x+uG61GC0odQw6WlDE Hn4cnpOXbAsl3wIUKlyjY0rAoryVKj8YQvS6W6vxcUuxxWUgqXXc9zt7yEspz+o6Ry9G aF7nFfdCtPNDsmh3l4thXYlRgr8P6I/j5BOdd9rcFtJQgLcFyX4/XlSjf8Zs+vNt1u5Z uuag== X-Gm-Message-State: ALyK8tLh7L1QdOM751Usp+mVxwXF+Jbp3+fer4L5eWOGkw2SbJmM768Isni78y3ZnYarIw== X-Received: by 10.194.133.194 with SMTP id pe2mr8172460wjb.5.1464252593088; Thu, 26 May 2016 01:49:53 -0700 (PDT) Received: from donizetti.lan (dynamic-adsl-78-12-252-58.clienti.tiscali.it. [78.12.252.58]) by smtp.gmail.com with ESMTPSA id c2sm2385115wme.4.2016.05.26.01.49.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 May 2016 01:49:52 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 26 May 2016 10:49:44 +0200 Message-Id: <1464252584-30832-5-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1464252584-30832-1-git-send-email-pbonzini@redhat.com> References: <1464252584-30832-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::244 Subject: [Qemu-devel] [PATCH 4/4] exec: hide mr->ram_addr from qemu_get_ram_ptr users 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: marcandre.lureau@redhat.com, arei.gonglei@huawei.com, famz@redhat.com, mst@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Let users of qemu_get_ram_ptr and qemu_ram_ptr_length pass in an address that is relative to the MemoryRegion. This basically means what address_space_translate returns. Because the semantics of the second parameter change, rename the function to qemu_map_ram_ptr. Signed-off-by: Paolo Bonzini --- exec.c | 57 +++++++++++++++++++------------------------- include/exec/memory.h | 5 ++-- memory.c | 4 ++-- scripts/dump-guest-memory.py | 19 +++------------ 4 files changed, 31 insertions(+), 54 deletions(-) diff --git a/exec.c b/exec.c index 7f62835..4488821 100644 --- a/exec.c +++ b/exec.c @@ -1822,12 +1822,13 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) * * Called within RCU critical section. */ -void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr) +void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr) { RAMBlock *block = ram_block; if (block == NULL) { block = qemu_get_ram_block(addr); + addr -= block->offset; } if (xen_enabled() && block->host == NULL) { @@ -1841,10 +1842,10 @@ void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr) block->host = xen_map_cache(block->offset, block->max_length, 1); } - return ramblock_ptr(block, addr - block->offset); + return ramblock_ptr(block, addr); } -/* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr +/* Return a host pointer to guest's ram. Similar to qemu_map_ram_ptr * but takes a size argument. * * Called within RCU critical section. @@ -1853,16 +1854,15 @@ static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr, hwaddr *size) { RAMBlock *block = ram_block; - ram_addr_t offset_inside_block; if (*size == 0) { return NULL; } if (block == NULL) { block = qemu_get_ram_block(addr); + addr -= block->offset; } - offset_inside_block = addr - block->offset; - *size = MIN(*size, block->max_length - offset_inside_block); + *size = MIN(*size, block->max_length - addr); if (xen_enabled() && block->host == NULL) { /* We need to check if the requested address is in the RAM @@ -1876,7 +1876,7 @@ static void *qemu_ram_ptr_length(RAMBlock *ram_block, ram_addr_t addr, block->host = xen_map_cache(block->offset, block->max_length, 1); } - return ramblock_ptr(block, offset_inside_block); + return ramblock_ptr(block, addr); } /* @@ -1986,13 +1986,13 @@ static void notdirty_mem_write(void *opaque, hwaddr ram_addr, } switch (size) { case 1: - stb_p(qemu_get_ram_ptr(NULL, ram_addr), val); + stb_p(qemu_map_ram_ptr(NULL, ram_addr), val); break; case 2: - stw_p(qemu_get_ram_ptr(NULL, ram_addr), val); + stw_p(qemu_map_ram_ptr(NULL, ram_addr), val); break; case 4: - stl_p(qemu_get_ram_ptr(NULL, ram_addr), val); + stl_p(qemu_map_ram_ptr(NULL, ram_addr), val); break; default: abort(); @@ -2454,6 +2454,8 @@ static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr, hwaddr length) { uint8_t dirty_log_mask = memory_region_get_dirty_log_mask(mr); + addr += memory_region_get_ram_addr(mr); + /* No early return if dirty_log_mask is or becomes 0, because * cpu_physical_memory_set_dirty_range will still call * xen_modified_memory. @@ -2566,9 +2568,8 @@ static MemTxResult address_space_write_continue(AddressSpace *as, hwaddr addr, abort(); } } else { - addr1 += memory_region_get_ram_addr(mr); /* RAM case */ - ptr = qemu_get_ram_ptr(mr->ram_block, addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); memcpy(ptr, buf, l); invalidate_and_set_dirty(mr, addr1, l); } @@ -2659,8 +2660,7 @@ MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr, } } else { /* RAM case */ - ptr = qemu_get_ram_ptr(mr->ram_block, - memory_region_get_ram_addr(mr) + addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); memcpy(buf, ptr, l); } @@ -2743,9 +2743,8 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as, memory_region_is_romd(mr))) { l = memory_access_size(mr, l, addr1); } else { - addr1 += memory_region_get_ram_addr(mr); /* ROM/RAM case */ - ptr = qemu_get_ram_ptr(mr->ram_block, addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); switch (type) { case WRITE_DATA: memcpy(ptr, buf, l); @@ -2903,7 +2902,6 @@ void *address_space_map(AddressSpace *as, hwaddr done = 0; hwaddr l, xlat, base; MemoryRegion *mr, *this_mr; - ram_addr_t raddr; void *ptr; if (len == 0) { @@ -2938,7 +2936,6 @@ void *address_space_map(AddressSpace *as, } base = xlat; - raddr = memory_region_get_ram_addr(mr); for (;;) { len -= l; @@ -2957,7 +2954,7 @@ void *address_space_map(AddressSpace *as, memory_region_ref(mr); *plen = done; - ptr = qemu_ram_ptr_length(mr->ram_block, raddr + base, plen); + ptr = qemu_ram_ptr_length(mr->ram_block, base, plen); rcu_read_unlock(); return ptr; @@ -2976,7 +2973,6 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, mr = memory_region_from_host(buffer, &addr1); assert(mr != NULL); - addr1 += memory_region_get_ram_addr(mr); if (is_write) { invalidate_and_set_dirty(mr, addr1, access_len); } @@ -3042,8 +3038,7 @@ static inline uint32_t address_space_ldl_internal(AddressSpace *as, hwaddr addr, #endif } else { /* RAM case */ - ptr = qemu_get_ram_ptr(mr->ram_block, - memory_region_get_ram_addr(mr) + addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); switch (endian) { case DEVICE_LITTLE_ENDIAN: val = ldl_le_p(ptr); @@ -3136,8 +3131,7 @@ static inline uint64_t address_space_ldq_internal(AddressSpace *as, hwaddr addr, #endif } else { /* RAM case */ - ptr = qemu_get_ram_ptr(mr->ram_block, - memory_region_get_ram_addr(mr) + addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); switch (endian) { case DEVICE_LITTLE_ENDIAN: val = ldq_le_p(ptr); @@ -3250,8 +3244,7 @@ static inline uint32_t address_space_lduw_internal(AddressSpace *as, #endif } else { /* RAM case */ - ptr = qemu_get_ram_ptr(mr->ram_block, - memory_region_get_ram_addr(mr) + addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); switch (endian) { case DEVICE_LITTLE_ENDIAN: val = lduw_le_p(ptr); @@ -3333,13 +3326,13 @@ void address_space_stl_notdirty(AddressSpace *as, hwaddr addr, uint32_t val, r = memory_region_dispatch_write(mr, addr1, val, 4, attrs); } else { - addr1 += memory_region_get_ram_addr(mr); - ptr = qemu_get_ram_ptr(mr->ram_block, addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); stl_p(ptr, val); dirty_log_mask = memory_region_get_dirty_log_mask(mr); dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE); - cpu_physical_memory_set_dirty_range(addr1, 4, dirty_log_mask); + cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr) + addr, + 4, dirty_log_mask); r = MEMTX_OK; } if (result) { @@ -3388,8 +3381,7 @@ static inline void address_space_stl_internal(AddressSpace *as, r = memory_region_dispatch_write(mr, addr1, val, 4, attrs); } else { /* RAM case */ - addr1 += memory_region_get_ram_addr(mr); - ptr = qemu_get_ram_ptr(mr->ram_block, addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); switch (endian) { case DEVICE_LITTLE_ENDIAN: stl_le_p(ptr, val); @@ -3498,8 +3490,7 @@ static inline void address_space_stw_internal(AddressSpace *as, r = memory_region_dispatch_write(mr, addr1, val, 2, attrs); } else { /* RAM case */ - addr1 += memory_region_get_ram_addr(mr); - ptr = qemu_get_ram_ptr(mr->ram_block, addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); switch (endian) { case DEVICE_LITTLE_ENDIAN: stw_le_p(ptr, val); diff --git a/include/exec/memory.h b/include/exec/memory.h index 71a27ab..4ab6800 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1393,7 +1393,7 @@ MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr, MemoryRegion *mr); MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, uint8_t *buf, int len); -void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr); +void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr); static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) { @@ -1431,8 +1431,7 @@ MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, l = len; mr = address_space_translate(as, addr, &addr1, &l, false); if (len == l && memory_access_is_direct(mr, false)) { - addr1 += memory_region_get_ram_addr(mr); - ptr = qemu_get_ram_ptr(mr->ram_block, addr1); + ptr = qemu_map_ram_ptr(mr->ram_block, addr1); memcpy(buf, ptr, len); } else { result = address_space_read_continue(as, addr, attrs, buf, len, diff --git a/memory.c b/memory.c index f8085ea..8ba496d 100644 --- a/memory.c +++ b/memory.c @@ -1657,10 +1657,10 @@ void *memory_region_get_ram_ptr(MemoryRegion *mr) mr = mr->alias; } assert(mr->ram_block); - ptr = qemu_get_ram_ptr(mr->ram_block, memory_region_get_ram_addr(mr)); + ptr = qemu_map_ram_ptr(mr->ram_block, offset); rcu_read_unlock(); - return ptr + offset; + return ptr; } MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset) diff --git a/scripts/dump-guest-memory.py b/scripts/dump-guest-memory.py index eb24f78..9956fc0 100644 --- a/scripts/dump-guest-memory.py +++ b/scripts/dump-guest-memory.py @@ -328,23 +328,10 @@ def qlist_foreach(head, field_str): yield var -def qemu_get_ram_block(ram_addr): - """Returns the RAMBlock struct to which the given address belongs.""" - - ram_blocks = gdb.parse_and_eval("ram_list.blocks") - - for block in qlist_foreach(ram_blocks, "next"): - if (ram_addr - block["offset"]) < block["used_length"]: - return block - - raise gdb.GdbError("Bad ram offset %x" % ram_addr) - - -def qemu_get_ram_ptr(ram_addr): +def qemu_map_ram_ptr(block, offset): """Returns qemu vaddr for given guest physical address.""" - block = qemu_get_ram_block(ram_addr) - return block["host"] + (ram_addr - block["offset"]) + return block["host"] + offset def memory_region_get_ram_ptr(memory_region): @@ -352,7 +339,7 @@ def memory_region_get_ram_ptr(memory_region): return (memory_region_get_ram_ptr(memory_region["alias"].dereference()) + memory_region["alias_offset"]) - return qemu_get_ram_ptr(memory_region["ram_block"]["offset"]) + return qemu_map_ram_ptr(memory_region["ram_block"], 0) def get_guest_phys_blocks():