From patchwork Wed Mar 10 23:57:04 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 47290 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8205EB7D0F for ; Thu, 11 Mar 2010 11:13:14 +1100 (EST) Received: from localhost ([127.0.0.1]:40309 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NpW1X-00035E-Es for incoming@patchwork.ozlabs.org; Wed, 10 Mar 2010 19:13:11 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NpVzF-0001RM-Pv for qemu-devel@nongnu.org; Wed, 10 Mar 2010 19:10:49 -0500 Received: from [199.232.76.173] (port=48166 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NpVzF-0001RE-GV for qemu-devel@nongnu.org; Wed, 10 Mar 2010 19:10:49 -0500 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1NpVzD-0001se-KV for qemu-devel@nongnu.org; Wed, 10 Mar 2010 19:10:49 -0500 Received: from are.twiddle.net ([75.149.56.221]:58431) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NpVzC-0001sK-Jf for qemu-devel@nongnu.org; Wed, 10 Mar 2010 19:10:47 -0500 Received: by are.twiddle.net (Postfix, from userid 5000) id 1E77A1020; Wed, 10 Mar 2010 16:10:40 -0800 (PST) Message-Id: <724d07612e819186ba1e8973df8af1b06c7b259f.1268265556.git.rth@twiddle.net> In-Reply-To: References: From: Richard Henderson Date: Wed, 10 Mar 2010 15:57:04 -0800 To: qemu-devel@nongnu.org X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: paul@codesourcery.com Subject: [Qemu-devel] [PATCH 6/6] Fix last page errors in page_check_range and page_set_flags. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The addr < end comparison prevents iterating over the last page in the guest address space; an iteration based on length avoids this problem. At the same time, assert that the given address is in the guest address space. Signed-off-by: Richard Henderson --- exec.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 36 insertions(+), 18 deletions(-) diff --git a/exec.c b/exec.c index 5455a74..245535a 100644 --- a/exec.c +++ b/exec.c @@ -2315,27 +2315,35 @@ int page_get_flags(target_ulong address) return p->flags; } -/* modify the flags of a page and invalidate the code if - necessary. The flag PAGE_WRITE_ORG is positioned automatically - depending on PAGE_WRITE */ +/* Modify the flags of a page and invalidate the code if necessary. + The flag PAGE_WRITE_ORG is positioned automatically depending + on PAGE_WRITE. The mmap_lock should already be held. */ void page_set_flags(target_ulong start, target_ulong end, int flags) { - PageDesc *p; - target_ulong addr; + target_ulong addr, len; + + /* This function should never be called with addresses outside the + guest address space. If this assert fires, it probably indicates + a missing call to h2g_valid. */ +#if HOST_LONG_BITS > L1_MAP_ADDR_SPACE_BITS + assert(end < (1ul << L1_MAP_ADDR_SPACE_BITS)); +#endif + assert(start < end); - /* mmap_lock should already be held. */ start = start & TARGET_PAGE_MASK; end = TARGET_PAGE_ALIGN(end); - if (flags & PAGE_WRITE) + + if (flags & PAGE_WRITE) { flags |= PAGE_WRITE_ORG; - for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { - p = page_find_alloc(addr >> TARGET_PAGE_BITS); - /* We may be called for host regions that are outside guest - address space. */ - if (!p) - return; - /* if the write protection is set, then we invalidate the code - inside */ + } + + for (addr = start, len = end - start; + len != 0; + len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { + PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1); + + /* If the write protection bit is set, then we invalidate + the code inside. */ if (!(p->flags & PAGE_WRITE) && (flags & PAGE_WRITE) && p->first_tb) { @@ -2351,14 +2359,24 @@ int page_check_range(target_ulong start, target_ulong len, int flags) target_ulong end; target_ulong addr; - if (start + len < start) - /* we've wrapped around */ + /* This function should never be called with addresses outside the + guest address space. If this assert fires, it probably indicates + a missing call to h2g_valid. */ +#if HOST_LONG_BITS > L1_MAP_ADDR_SPACE_BITS + assert(start < (1ul << L1_MAP_ADDR_SPACE_BITS)); +#endif + + if (start + len - 1 < start) { + /* We've wrapped around. */ return -1; + } end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */ start = start & TARGET_PAGE_MASK; - for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) { + for (addr = start, len = end - start; + len != 0; + len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { p = page_find(addr >> TARGET_PAGE_BITS); if( !p ) return -1;