From patchwork Mon Mar 11 01:30:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 10846561 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 018666C2 for ; Mon, 11 Mar 2019 01:41:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E06E128EEA for ; Mon, 11 Mar 2019 01:41:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D300828EF9; Mon, 11 Mar 2019 01:41:42 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C602B28F1C for ; Mon, 11 Mar 2019 01:41:41 +0000 (UTC) Received: from localhost ([127.0.0.1]:53734 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h39wi-0005M0-Jm for patchwork-qemu-devel@patchwork.kernel.org; Sun, 10 Mar 2019 21:41:40 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58431) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h39vP-0004Qb-Rw for qemu-devel@nongnu.org; Sun, 10 Mar 2019 21:40:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h39mE-0008Ma-4Q for qemu-devel@nongnu.org; Sun, 10 Mar 2019 21:30:50 -0400 Received: from mga18.intel.com ([134.134.136.126]:20586) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h39mD-0008K0-RK for qemu-devel@nongnu.org; Sun, 10 Mar 2019 21:30:50 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Mar 2019 18:30:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,466,1544515200"; d="scan'208";a="132949912" Received: from richard.sh.intel.com (HELO localhost) ([10.239.159.54]) by orsmga003.jf.intel.com with ESMTP; 10 Mar 2019 18:30:42 -0700 From: Wei Yang To: qemu-devel@nongnu.org Date: Mon, 11 Mar 2019 09:30:16 +0800 Message-Id: <20190311013016.14411-1-richardw.yang@linux.intel.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.126 Subject: [Qemu-devel] [PATCH v2] exec.c: refactor function flatview_add_to_dispatch() 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: pbonzini@redhat.com, Wei Yang , rth@twiddle.net Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP flatview_add_to_dispatch() registers page based on the condition of *section*, which may looks like this: |s|PPPPPPP|s| where s stands for subpage and P for page. The procedure of this function could be described as: - register first subpage - register page - register last subpage This means the procedure could be simplified into these three steps instead of a loop iteration. This patch refactors the function into three corresponding steps and adds some comment to clarify it. Signed-off-by: Wei Yang --- v2: * removes the loop iteration as suggested by Paolo --- exec.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/exec.c b/exec.c index 0959b00c06..160b8587d4 100644 --- a/exec.c +++ b/exec.c @@ -1598,34 +1598,50 @@ static void register_multipage(FlatView *fv, phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index); } +/* + * The range in *section* may look like this: + * + * |s|PPPPPPP|s| + * + * where s stands for subpage and P for page. + * + * The procedure in following function could be described as: + * + * - register first subpage + * - register page + * - register last subpage + */ void flatview_add_to_dispatch(FlatView *fv, MemoryRegionSection *section) { - MemoryRegionSection now = *section, remain = *section; + MemoryRegionSection now, remain = *section; Int128 page_size = int128_make64(TARGET_PAGE_SIZE); - if (now.offset_within_address_space & ~TARGET_PAGE_MASK) { - uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space) - - now.offset_within_address_space; + /* register first subpage */ + if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) { + uint64_t left = TARGET_PAGE_ALIGN(remain.offset_within_address_space) + - remain.offset_within_address_space; + now = remain; now.size = int128_min(int128_make64(left), now.size); + remain.size = int128_sub(remain.size, now.size); + remain.offset_within_address_space += int128_get64(now.size); + remain.offset_within_region += int128_get64(now.size); register_subpage(fv, &now); - } else { - now.size = int128_zero(); } - while (int128_ne(remain.size, now.size)) { + + /* register page */ + if (int128_ge(remain.size, page_size)) { + now = remain; + now.size = int128_and(now.size, int128_neg(page_size)); remain.size = int128_sub(remain.size, now.size); remain.offset_within_address_space += int128_get64(now.size); remain.offset_within_region += int128_get64(now.size); - now = remain; - if (int128_lt(remain.size, page_size)) { - register_subpage(fv, &now); - } else if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) { - now.size = page_size; - register_subpage(fv, &now); - } else { - now.size = int128_and(now.size, int128_neg(page_size)); - register_multipage(fv, &now); - } + register_multipage(fv, &now); + } + + /* register last subpage */ + if (int128_gt(remain.size, 0)) { + register_subpage(fv, &remain); } }