From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514484777361223.7932604399209; Thu, 28 Dec 2017 10:12:57 -0800 (PST) Received: from localhost ([::1]:60648 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcfo-00039u-9D for importer@patchew.org; Thu, 28 Dec 2017 13:12:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54599) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbR-000842-03 for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbP-00012t-My for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41874) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbP-0000xm-8F for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:23 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 6041D80503; Thu, 28 Dec 2017 13:08:15 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:04 -0500 Message-Id: <20171228180814.9749-2-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 01/10] linux-user: Use #if to only call validate_guest_space for 32-bit ARM target 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker Instead of defining a bogus validate_guest_space that always returns 1 on targets other than 32-bit ARM, use #if blocks to only call it on 32-bit ARM targets. This makes the "normal" flow control clearer. Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 20f3d8c2c3..cac991159c 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -354,7 +354,6 @@ enum { =20 /* The commpage only exists for 32 bit kernels */ =20 -#define TARGET_HAS_VALIDATE_GUEST_SPACE /* Return 1 if the proposed guest space is suitable for the guest. * Return 0 if the proposed guest space isn't suitable, but another * address space should be tried. @@ -1823,15 +1822,6 @@ static abi_ulong create_elf_tables(abi_ulong p, int = argc, int envc, return sp; } =20 -#ifndef TARGET_HAS_VALIDATE_GUEST_SPACE -/* If the guest doesn't have a validation function just agree */ -static int validate_guest_space(unsigned long guest_base, - unsigned long guest_size) -{ - return 1; -} -#endif - unsigned long init_guest_space(unsigned long host_start, unsigned long host_size, unsigned long guest_start, @@ -1845,11 +1835,12 @@ unsigned long init_guest_space(unsigned long host_s= tart, /* If just a starting address is given, then just verify that * address. */ if (host_start && !host_size) { +#if defined(TARGET_ARM) && !defined(TARGET_AARCH64) if (validate_guest_space(host_start, host_size) =3D=3D 1) { - return host_start; - } else { return (unsigned long)-1; } +#endif + return host_start; } =20 /* Setup the initial flags and start address. */ @@ -1888,6 +1879,8 @@ unsigned long init_guest_space(unsigned long host_sta= rt, =20 /* Check to see if the address is valid. */ if (!host_start || real_start =3D=3D current_start) { +#if defined(TARGET_ARM) && !defined(TARGET_AARCH64) + /* On 32-bit ARM, we need to also be able to map the commpage.= */ int valid =3D validate_guest_space(real_start - guest_start, real_size); if (valid =3D=3D 1) { @@ -1896,6 +1889,10 @@ unsigned long init_guest_space(unsigned long host_st= art, return (unsigned long)-1; } /* valid =3D=3D 0, so try again. */ +#else + /* On other architectures, whatever we have here is fine. */ + break; +#endif } =20 /* That address didn't work. Unmap and try a different one. --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 15144846383881005.7658277193942; Thu, 28 Dec 2017 10:10:38 -0800 (PST) Received: from localhost ([::1]:60588 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcdJ-0000qA-In for importer@patchew.org; Thu, 28 Dec 2017 13:10:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54594) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbQ-00083x-TP for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbP-000131-Nn for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41884) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbP-0000yG-IK for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:23 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 5CE3C80504; Thu, 28 Dec 2017 13:08:16 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:05 -0500 Message-Id: <20171228180814.9749-3-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 02/10] linux-user: Rename validate_guest_space => init_guest_commpage 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker init_guest_commpage is a much more honest description of what the function does. validate_guest_space not only suggests that the function has no side-effects, but also introduces confusion as to why it is only needed on 32-bit ARM targets. Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index cac991159c..453394239c 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -362,8 +362,8 @@ enum { * The guest code may leave a page mapped and populate it if the * address is suitable. */ -static int validate_guest_space(unsigned long guest_base, - unsigned long guest_size) +static int init_guest_commpage(unsigned long guest_base, + unsigned long guest_size) { unsigned long real_start, test_page_addr; =20 @@ -1836,7 +1836,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, * address. */ if (host_start && !host_size) { #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) - if (validate_guest_space(host_start, host_size) =3D=3D 1) { + if (init_guest_commpage(host_start, host_size) !=3D 1) { return (unsigned long)-1; } #endif @@ -1881,8 +1881,8 @@ unsigned long init_guest_space(unsigned long host_sta= rt, if (!host_start || real_start =3D=3D current_start) { #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) /* On 32-bit ARM, we need to also be able to map the commpage.= */ - int valid =3D validate_guest_space(real_start - guest_start, - real_size); + int valid =3D init_guest_commpage(real_start - guest_start, + real_size); if (valid =3D=3D 1) { break; } else if (valid =3D=3D -1) { --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514484643637486.5105040873261; Thu, 28 Dec 2017 10:10:43 -0800 (PST) Received: from localhost ([::1]:60593 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcde-00018a-I4 for importer@patchew.org; Thu, 28 Dec 2017 13:10:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54596) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbQ-00083z-VG for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbP-00013H-Ol for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41894) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbP-0000zS-KC for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:23 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 17DC880505; Thu, 28 Dec 2017 13:08:17 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:06 -0500 Message-Id: <20171228180814.9749-4-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 03/10] linux-user: init_guest_space: Clean up if we can't initialize the commpage 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker We'll just exit with an error anyway, so it doesn't really matter, but it is cleaned up in all of the other places were we error out. Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 1 + 1 file changed, 1 insertion(+) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 453394239c..1a6b660b25 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1886,6 +1886,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, if (valid =3D=3D 1) { break; } else if (valid =3D=3D -1) { + munmap((void *)real_start, host_size); return (unsigned long)-1; } /* valid =3D=3D 0, so try again. */ --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514484896579167.72580184786398; Thu, 28 Dec 2017 10:14:56 -0800 (PST) Received: from localhost ([::1]:60730 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUchj-0004ux-IN for importer@patchew.org; Thu, 28 Dec 2017 13:14:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54711) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbT-00086I-LP for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00014U-WC for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:27 -0500 Received: from mav.lukeshu.com ([104.207.138.63]:58354) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbQ-000105-Lq for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id BFD0280506; Thu, 28 Dec 2017 13:08:17 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:07 -0500 Message-Id: <20171228180814.9749-5-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 104.207.138.63 Subject: [Qemu-devel] [PATCH 04/10] linux-user: init_guest_space: Correctly handle guest_start in commpage initialization 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker init_guest_commpage needs to check if the mapped space, which ends at real_start+real_size overlaps with where it needs to put the commpage, which is (assuming sane qemu_host_page_size) guest_base + 0xffff000, where guest_base is real_start - guest_start. [guest_base][ 0xffff0000 ][commpage] [guest_base][guest_start][real_size] [commpage] [ real_start ][real_size] [commpage] ^ fail if this gap < 0 Since init_guest_commpage wants to do everything relative to guest_base (rather than real_start), it obviously needs to be comparing 0xffff0000 against guest_start+real_size, not just real_size. This bug has been present since 806d102141b99d4f1e55a97d68b7ea8c8ba3129f in 2012, but guest_start is usually 0, and prior to v2.11 real_size was usually much smaller than 0xffff0000, so it was uncommon for it to have made a difference. Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 1a6b660b25..f41cecc3cb 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1882,7 +1882,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) /* On 32-bit ARM, we need to also be able to map the commpage.= */ int valid =3D init_guest_commpage(real_start - guest_start, - real_size); + real_size + guest_start); if (valid =3D=3D 1) { break; } else if (valid =3D=3D -1) { --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514484896958745.3110561624483; Thu, 28 Dec 2017 10:14:56 -0800 (PST) Received: from localhost ([::1]:60731 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUchj-0004vc-Qc for importer@patchew.org; Thu, 28 Dec 2017 13:14:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54657) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbR-00084e-Vw for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00013W-2z for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:25 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41954) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbP-00012o-UW for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 7BB7D80507; Thu, 28 Dec 2017 13:08:18 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:08 -0500 Message-Id: <20171228180814.9749-6-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 05/10] linux-user: init_guest_space: Clarify page alignment logic 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker There are 3 parts to this change: - Add a comment showing the relative sizes and positions of the blocks of memory - introduce and use new aligned_{start,size} instead of adjusting real_{start_size} - When we clean up (on failure), munmap(real_start, real_size) instead of munmap(aligned_start, aligned_size). It *shouldn't* make any difference, but I will admit that this does mean we are making the syscall with different values, so this isn't quite a no-op patch. Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index f41cecc3cb..22f2632dfa 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1827,7 +1827,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, unsigned long guest_start, bool fixed) { - unsigned long current_start, real_start; + unsigned long current_start, aligned_start; int flags; =20 assert(host_start || host_size); @@ -1853,7 +1853,8 @@ unsigned long init_guest_space(unsigned long host_sta= rt, /* Otherwise, a non-zero size region of memory needs to be mapped * and validated. */ while (1) { - unsigned long real_size =3D host_size; + unsigned long real_start, real_size, aligned_size; + aligned_size =3D real_size =3D host_size; =20 /* Do not use mmap_find_vma here because that is limited to the * guest address space. We are going to make the @@ -1867,26 +1868,48 @@ unsigned long init_guest_space(unsigned long host_s= tart, =20 /* Ensure the address is properly aligned. */ if (real_start & ~qemu_host_page_mask) { + /* Ideally, we adjust like + * + * pages: [ ][ ][ ][ ][ ] + * old: [ real ] + * [ aligned ] + * new: [ real ] + * [ aligned ] + * + * But if there is something else mapped right after it, + * then obviously it won't have room to grow, and the + * kernel will put the new larger real someplace else with + * unknown alignment (if we made it to here, then + * fixed=3Dfalse). Which is why we grow real by a full page + * size, instead of by part of one; so that even if we get + * moved, we can still guarantee alignment. But this does + * mean that there is a padding of < 1 page both before + * and after the aligned range; the "after" could could + * cause problems for ARM emulation where it could butt in + * to where we need to put the commpage. + */ munmap((void *)real_start, host_size); - real_size =3D host_size + qemu_host_page_size; + real_size =3D aligned_size + qemu_host_page_size; real_start =3D (unsigned long) mmap((void *)real_start, real_size, PROT_NONE, flags, -1, = 0); if (real_start =3D=3D (unsigned long)-1) { return (unsigned long)-1; } - real_start =3D HOST_PAGE_ALIGN(real_start); + aligned_start =3D HOST_PAGE_ALIGN(real_start); + } else { + aligned_start =3D real_start; } =20 /* Check to see if the address is valid. */ - if (!host_start || real_start =3D=3D current_start) { + if (!host_start || aligned_start =3D=3D current_start) { #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) /* On 32-bit ARM, we need to also be able to map the commpage.= */ - int valid =3D init_guest_commpage(real_start - guest_start, - real_size + guest_start); + int valid =3D init_guest_commpage(aligned_start - guest_start, + aligned_size + guest_start); if (valid =3D=3D 1) { break; } else if (valid =3D=3D -1) { - munmap((void *)real_start, host_size); + munmap((void *)real_start, real_size); return (unsigned long)-1; } /* valid =3D=3D 0, so try again. */ @@ -1905,7 +1928,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, * address space randomization put a shared library somewhere * inconvenient. */ - munmap((void *)real_start, host_size); + munmap((void *)real_start, real_size); current_start +=3D qemu_host_page_size; if (host_start =3D=3D current_start) { /* Theoretically possible if host doesn't have any suitably @@ -1917,7 +1940,7 @@ unsigned long init_guest_space(unsigned long host_sta= rt, =20 qemu_log_mask(CPU_LOG_PAGE, "Reserved 0x%lx bytes of guest address spa= ce\n", host_size); =20 - return real_start; + return aligned_start; } =20 static void probe_guest_base(const char *image_name, --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514484634062312.84703141005957; Thu, 28 Dec 2017 10:10:34 -0800 (PST) Received: from localhost ([::1]:60589 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcdK-0000qg-KU for importer@patchew.org; Thu, 28 Dec 2017 13:10:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54601) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbR-000843-0i for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00013U-2m for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41956) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbP-00012p-Vb for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 350F480508; Thu, 28 Dec 2017 13:08:19 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:09 -0500 Message-Id: <20171228180814.9749-7-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 06/10] linux-user: init_guest_commpage: Add a comment about size check 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 22f2632dfa..b560f5d6fe 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -374,6 +374,11 @@ static int init_guest_commpage(unsigned long guest_bas= e, =20 /* If the commpage lies within the already allocated guest space, * then there is no way we can allocate it. + * + * You may be thinking that that this check is redundant because + * we already validated the guest size against MAX_RESERVED_VA; + * but if qemu_host_page_mask is unusually large, then + * test_page_addr may be lower. */ if (test_page_addr >=3D guest_base && test_page_addr < (guest_base + guest_size)) { --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514484791054488.7198206282118; Thu, 28 Dec 2017 10:13:11 -0800 (PST) Received: from localhost ([::1]:60652 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcg2-0003MK-28 for importer@patchew.org; Thu, 28 Dec 2017 13:13:10 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54651) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbR-00084b-UX for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00013k-4B for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:25 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41960) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbQ-000137-0J for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id E366580509; Thu, 28 Dec 2017 13:08:19 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:10 -0500 Message-Id: <20171228180814.9749-8-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 07/10] linux-user: init_guest_space: Clean up control flow a bit 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker Instead of doing if (check1) { if (check2) { success; } } retry; Do a clearer if (!check1) { goto try_again; } if (!check2) { goto try_again; } success; try_again: retry; Besides being clearer, this makes it easier to insert more checks that need to trigger a retry on check failure, or rearrange them, or anything like that. Because some indentation is changing, "ignore space change" may be useful for viewing this patch. Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index b560f5d6fe..5c0ad65611 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1906,24 +1906,28 @@ unsigned long init_guest_space(unsigned long host_s= tart, } =20 /* Check to see if the address is valid. */ - if (!host_start || aligned_start =3D=3D current_start) { + if (host_start && aligned_start !=3D current_start) { + goto try_again; + } + #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) - /* On 32-bit ARM, we need to also be able to map the commpage.= */ - int valid =3D init_guest_commpage(aligned_start - guest_start, - aligned_size + guest_start); - if (valid =3D=3D 1) { - break; - } else if (valid =3D=3D -1) { - munmap((void *)real_start, real_size); - return (unsigned long)-1; - } - /* valid =3D=3D 0, so try again. */ -#else - /* On other architectures, whatever we have here is fine. */ - break; -#endif + /* On 32-bit ARM, we need to also be able to map the commpage. */ + int valid =3D init_guest_commpage(aligned_start - guest_start, + aligned_size + guest_start); + if (valid =3D=3D -1) { + munmap((void *)real_start, real_size); + return (unsigned long)-1; + } else if (valid =3D=3D -1) { + goto try_again; } +#endif + + /* If nothing has said `return -1` or `goto try_again` yet, + * then the address we have is good. + */ + break; =20 + try_again: /* That address didn't work. Unmap and try a different one. * The address the host picked because is typically right at * the top of the host address space and leaves the guest with --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 151448478226390.95378804038523; Thu, 28 Dec 2017 10:13:02 -0800 (PST) Received: from localhost ([::1]:60649 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcfo-00039y-3Q for importer@patchew.org; Thu, 28 Dec 2017 13:12:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54632) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbR-00084S-N7 for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00013f-3w for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:25 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41958) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbQ-000134-0c for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 9DF198050A; Thu, 28 Dec 2017 13:08:20 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:11 -0500 Message-Id: <20171228180814.9749-9-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 08/10] linux-user: init_guest_space: Don't try to align if we'll reject it 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker If the ensure-alignment code gets triggered, then the "if (host_start && real_start !=3D current_start)" check will always trigge= r, so save 2 syscalls and put that check first. Note that we can't just switch to using MAP_FIXED for that check, because then we couldn't differentiate between a failure because "there isn't enough space" and "there isn't enough space *here*". Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 5c0ad65611..1b7583d659 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1871,6 +1871,11 @@ unsigned long init_guest_space(unsigned long host_st= art, return (unsigned long)-1; } =20 + /* Check to see if the address is valid. */ + if (host_start && real_start !=3D current_start) { + goto try_again; + } + /* Ensure the address is properly aligned. */ if (real_start & ~qemu_host_page_mask) { /* Ideally, we adjust like @@ -1905,11 +1910,6 @@ unsigned long init_guest_space(unsigned long host_st= art, aligned_start =3D real_start; } =20 - /* Check to see if the address is valid. */ - if (host_start && aligned_start !=3D current_start) { - goto try_again; - } - #if defined(TARGET_ARM) && !defined(TARGET_AARCH64) /* On 32-bit ARM, we need to also be able to map the commpage. */ int valid =3D init_guest_commpage(aligned_start - guest_start, --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514485079199559.5810267472559; Thu, 28 Dec 2017 10:17:59 -0800 (PST) Received: from localhost ([::1]:60893 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUckf-0007AA-8j for importer@patchew.org; Thu, 28 Dec 2017 13:17:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54709) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbT-00086C-Ka for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00013r-55 for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:27 -0500 Received: from mav.lukeshu.com ([104.207.138.63]:58414) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbQ-00013E-1l for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 59E098050B; Thu, 28 Dec 2017 13:08:21 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:12 -0500 Message-Id: <20171228180814.9749-10-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 104.207.138.63 Subject: [Qemu-devel] [PATCH 09/10] linux-user: init_guest_space: Add a comment about search strategy 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: Luke Shumaker , Riku Voipio , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker Signed-off-by: Luke Shumaker Reviewed-by: Peter Maydell --- linux-user/elfload.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 1b7583d659..7736ea2c3a 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1936,6 +1936,10 @@ unsigned long init_guest_space(unsigned long host_st= art, * happen often. Probably means we got unlucky and host * address space randomization put a shared library somewhere * inconvenient. + * + * This is probably a good strategy if host_start, but is + * probably a bad strategy if not, which means we got here + * because of trouble with ARM commpage setup. */ munmap((void *)real_start, real_size); current_start +=3D qemu_host_page_size; --=20 2.15.1 From nobody Tue Oct 28 01:53:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1514485010193842.9749479223792; Thu, 28 Dec 2017 10:16:50 -0800 (PST) Received: from localhost ([::1]:60847 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcjM-0006Mo-60 for importer@patchew.org; Thu, 28 Dec 2017 13:16:36 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54678) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eUcbS-000857-ET for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eUcbQ-00013w-5X for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:26 -0500 Received: from mav.lukeshu.com ([2001:19f0:5c00:8069:5400:ff:fe26:6a86]:41964) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eUcbQ-00013G-1N for qemu-devel@nongnu.org; Thu, 28 Dec 2017 13:08:24 -0500 Received: from build64-par (unknown [IPv6:2601:803:202:9275:da50:e6ff:fe00:4a5b]) by mav.lukeshu.com (Postfix) with ESMTPSA id 1071E8050C; Thu, 28 Dec 2017 13:08:22 -0500 (EST) From: Luke Shumaker To: qemu-devel@nongnu.org Date: Thu, 28 Dec 2017 13:08:13 -0500 Message-Id: <20171228180814.9749-11-lukeshu@lukeshu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171228180814.9749-1-lukeshu@lukeshu.com> References: <20171228180814.9749-1-lukeshu@lukeshu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:19f0:5c00:8069:5400:ff:fe26:6a86 Subject: [Qemu-devel] [PATCH 10/10] linux-user: init_guest_space: Try to make ARM space+commpage continuous 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: Luke Shumaker , Riku Voipio , paul@codesourcery.com, Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Luke Shumaker At a fixed distance after the usable memory that init_guest_space maps, for 32-bit ARM targets we also need to map a commpage. The normal init_guest_space logic doesn't keep this in mind when searching for an address range. If !host_start, then try to find a big continuous segment where we can put both the usable memory and the commpage; we then munmap that segment and set current_start to that address; and let the normal code mmap the usable memory and the commpage separately. That is: if we don't have hint of where to start looking for memory, come up with one that is better than NULL. Depending on host_size and guest_start, there may or may not be a gap between the usable memory and the commpage, so this is slightly more restrictive than it needs to be; but it's only a hint, so that's OK. We only do that for !host start, because if host_start, then either: - we got an address passed in with -B, in which case we don't want to interfere with what the user said; - or host_start is based off of the ELF image's loaddr. The check "if (host_start && real_start !=3D current_start)" suggests that we really want lowest available address that is >=3D loaddr. I don't know why that is, but I'm trusting that Paul Brook knew what he was doing when he wrote the original version of that check in c581deda322080e8beb88b2e468d4af54454e4b3 way back in 2010. Signed-off-by: Luke Shumaker --- linux-user/elfload.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 7736ea2c3a..cd3a7d877d 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1857,6 +1857,55 @@ unsigned long init_guest_space(unsigned long host_st= art, =20 /* Otherwise, a non-zero size region of memory needs to be mapped * and validated. */ + +#if defined(TARGET_ARM) && !defined(TARGET_AARCH64) + /* On 32-bit ARM, we need to map not just the usable memory, but + * also the commpage. Try to find a suitable place by allocating + * a big chunk for all of it. If host_start, then the naive + * strategy probably does good enough. + */ + if (!host_start) { + unsigned long guest_full_size, host_full_size, real_start; + + guest_full_size =3D + (0xffff0f00 & qemu_host_page_mask) + qemu_host_page_size; + host_full_size =3D guest_full_size - guest_start; + real_start =3D (unsigned long) + mmap(NULL, host_full_size, PROT_NONE, flags, -1, 0); + if (real_start =3D=3D (unsigned long)-1) { + if (host_size < host_full_size - qemu_host_page_size) { + /* We failed to map a continous segment, but we're + * allowed to have a gap between the usable memory and + * the commpage where other things can be mapped. + * This sparseness gives us more flexibility to find + * an address range. + */ + goto naive; + } + return (unsigned long)-1; + } + munmap((void *)real_start, host_full_size); + if (real_start & ~qemu_host_page_mask) { + /* The same thing again, but with an extra qemu_host_page_size + * so that we can shift around alignment. + */ + unsigned long real_size =3D host_full_size + qemu_host_page_si= ze; + real_start =3D (unsigned long) + mmap(NULL, real_size, PROT_NONE, flags, -1, 0); + if (real_start =3D=3D (unsigned long)-1) { + if (host_size < host_full_size - qemu_host_page_size) { + goto naive; + } + return (unsigned long)-1; + } + munmap((void *)real_start, real_size); + real_start =3D HOST_PAGE_ALIGN(real_start); + } + current_start =3D real_start; + } + naive: +#endif + while (1) { unsigned long real_start, real_size, aligned_size; aligned_size =3D real_size =3D host_size; --=20 2.15.1