From nobody Mon Feb 9 23:39:08 2026 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; dkim=fail; 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 1500409234258841.4198472467522; Tue, 18 Jul 2017 13:20:34 -0700 (PDT) Received: from localhost ([::1]:58422 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXYyt-0005mP-W2 for importer@patchew.org; Tue, 18 Jul 2017 16:20:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50174) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXYjh-00078g-St for qemu-devel@nongnu.org; Tue, 18 Jul 2017 16:04:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dXYjc-0005Fz-Tu for qemu-devel@nongnu.org; Tue, 18 Jul 2017 16:04:49 -0400 Received: from mail-qt0-x241.google.com ([2607:f8b0:400d:c0d::241]:34369) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dXYjc-0005Ft-Ow for qemu-devel@nongnu.org; Tue, 18 Jul 2017 16:04:44 -0400 Received: by mail-qt0-x241.google.com with SMTP id 19so3683564qty.1 for ; Tue, 18 Jul 2017 13:04:44 -0700 (PDT) Received: from bigtime.com ([101.165.234.197]) by smtp.gmail.com with ESMTPSA id b201sm2339817qkg.5.2017.07.18.13.04.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Jul 2017 13:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=CBuRBLzxmFghtwkfEy0sH4hs6YokjkfNbTk+F9EWKO4=; b=ZeIWxOWy9xjf0a9YhVcYIwxWZbCbkwqAQegeonFSuAiXXjh1brfWI/TOL6QDYls+4L hZgMXxJWj+UCC4FbszUMlnCXigru8zj6fWlZ15cQqYcVDS0IcYmKohK/UMeB/+mmCllo WpvMMVe73L1zC7gaPsJK0EQDsXToN2mS96mv/mGP2T+P/RkImIcYpDWoeAXUYJdhryxy 02hv1Us3Zwrrff2TLXzd8JbR4+dEEKZx8sBfY2Uzi8i4YZAzPFD2P9zt18+z5QrCXmQd FDdOgPmRrJ+tioSYoDaApVCOrCPx4CPBT2Q3JLjy9ErmuiO1SYIoKTP56OqCeVod8qu/ xctA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=CBuRBLzxmFghtwkfEy0sH4hs6YokjkfNbTk+F9EWKO4=; b=loK8KR6+1kYwJIUvgyPePsJRj7IchNitk5uqacb/YwDGrvpVaDUIPm761Hy6MRWnFd h9zr/kuaL9Ma2poNyr6QULGSmDfDVkcvchTGf0P2cEKBjVk61GKo0+2Cze7M6xn/kWtQ RkIz+1JQdH3S17IxhDfxrho0szwUhLkNEHYWcj01XTXdXKAvqjGutEhiV3BZIi9YRpzZ uV8UcCq6Ip9pkboDCT4X/25q2KXgLBbYmDdbNqDsec4naMRjGzbNbDAQajxaX+nfXDXn mefWUotae+UIzHI46h39PQlBhE2ZpFRd24wfo267PgXeKWH0XVeHcmOnRA1iZU/qi9vA PohA== X-Gm-Message-State: AIVw1135HDXIPjIQwThlQoH+4861VPQzNTCgBlrDqPSJs0aosU7MI/vX x4NLKBU2MTop7Sx2Epc= X-Received: by 10.237.62.157 with SMTP id n29mr4283527qtf.0.1500408283866; Tue, 18 Jul 2017 13:04:43 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 18 Jul 2017 10:02:54 -1000 Message-Id: <20170718200255.31647-30-rth@twiddle.net> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170718200255.31647-1-rth@twiddle.net> References: <20170718200255.31647-1-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::241 Subject: [Qemu-devel] [PATCH v3 29/30] linux-user: Tidy and enforce reserved_va 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: aurelien@aurel32.net Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We had a check using TARGET_VIRT_ADDR_SPACE_BITS to make sure that the allocation coming in from the command-line option was not too large, but that didn't include target-specific knowledge about other restrictions on user-space. Remove several target-specific hacks in linux-user/main.c. For MIPS and Nios, we can replace them with proper adjustments to the respective target's TARGET_VIRT_ADDR_SPACE_BITS definition. For ARM, we had no existing ifdef but I suspect that the current default value of 0xf7000000 was chosen with this in mind. Define a workable value in linux-user/arm/, and also document why the special case is required. Signed-off-by: Richard Henderson --- linux-user/arm/target_cpu.h | 4 ++++ target/mips/mips-defs.h | 6 +++++- target/nios2/cpu.h | 6 +++++- linux-user/main.c | 38 +++++++++++++++++++++++++------------- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/linux-user/arm/target_cpu.h b/linux-user/arm/target_cpu.h index d888219..c4f79eb 100644 --- a/linux-user/arm/target_cpu.h +++ b/linux-user/arm/target_cpu.h @@ -19,6 +19,10 @@ #ifndef ARM_TARGET_CPU_H #define ARM_TARGET_CPU_H =20 +/* We need to be able to map the commpage. + See validate_guest_space in linux-user/elfload.c. */ +#define MAX_RESERVED_VA 0xfff00000ul + static inline void cpu_clone_regs(CPUARMState *env, target_ulong newsp) { if (newsp) { diff --git a/target/mips/mips-defs.h b/target/mips/mips-defs.h index 047554e..d239069 100644 --- a/target/mips/mips-defs.h +++ b/target/mips/mips-defs.h @@ -15,7 +15,11 @@ #else #define TARGET_LONG_BITS 32 #define TARGET_PHYS_ADDR_SPACE_BITS 40 -#define TARGET_VIRT_ADDR_SPACE_BITS 32 +# ifdef CONFIG_USER_ONLY +# define TARGET_VIRT_ADDR_SPACE_BITS 31 +# else +# define TARGET_VIRT_ADDR_SPACE_BITS 32 +#endif #endif =20 /* Masks used to mark instructions to indicate which ISA level they diff --git a/target/nios2/cpu.h b/target/nios2/cpu.h index 13931f3..da3f637 100644 --- a/target/nios2/cpu.h +++ b/target/nios2/cpu.h @@ -227,7 +227,11 @@ qemu_irq *nios2_cpu_pic_init(Nios2CPU *cpu); void nios2_check_interrupts(CPUNios2State *env); =20 #define TARGET_PHYS_ADDR_SPACE_BITS 32 -#define TARGET_VIRT_ADDR_SPACE_BITS 32 +#ifdef CONFIG_USER_ONLY +# define TARGET_VIRT_ADDR_SPACE_BITS 31 +#else +# define TARGET_VIRT_ADDR_SPACE_BITS 32 +#endif =20 #define cpu_init(cpu_model) CPU(cpu_nios2_init(cpu_model)) =20 diff --git a/linux-user/main.c b/linux-user/main.c index 30f0ae1..7693a62 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -60,23 +60,38 @@ do { = \ } \ } while (0) =20 -#if (TARGET_LONG_BITS =3D=3D 32) && (HOST_LONG_BITS =3D=3D 64) /* * When running 32-on-64 we should make sure we can fit all of the possible * guest address space into a contiguous chunk of virtual host memory. * * This way we will never overlap with our own libraries or binaries or st= ack * or anything else that QEMU maps. + * + * Many cpus reserve the high bit (or more than one for some 64-bit cpus) + * of the address for the kernel. Some cpus rely on this and user space + * uses the high bit(s) for pointer tagging and the like. For them, we + * must preserve the expected address space. */ -# if defined(TARGET_MIPS) || defined(TARGET_NIOS2) -/* - * MIPS only supports 31 bits of virtual address space for user space. - * Nios2 also only supports 31 bits. - */ -unsigned long reserved_va =3D 0x77000000; +#ifndef MAX_RESERVED_VA +# if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS +# if TARGET_VIRT_ADDR_SPACE_BITS =3D=3D 32 && \ + (TARGET_LONG_BITS =3D=3D 32 || defined(TARGET_ABI32)) +/* There are a number of places where we assign reserved_va to a variable + of type abi_ulong and expect it to fit. Avoid the last page. */ +# define MAX_RESERVED_VA (0xfffffffful & TARGET_PAGE_MASK) +# else +# define MAX_RESERVED_VA (1ul << TARGET_VIRT_ADDR_SPACE_BITS) +# endif # else -unsigned long reserved_va =3D 0xf7000000; +# define MAX_RESERVED_VA 0 # endif +#endif + +/* That said, reserving *too* much vm space via mmap can run into problems + with rlimits, oom due to page table creation, etc. We will still try i= t, + if directed by the command-line option, but not by default. */ +#if HOST_LONG_BITS =3D=3D 64 && TARGET_VIRT_ADDR_SPACE_BITS <=3D 32 +unsigned long reserved_va =3D MAX_RESERVED_VA; #else unsigned long reserved_va; #endif @@ -3976,11 +3991,8 @@ static void handle_arg_reserved_va(const char *arg) unsigned long unshifted =3D reserved_va; p++; reserved_va <<=3D shift; - if (((reserved_va >> shift) !=3D unshifted) -#if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS - || (reserved_va > (1ul << TARGET_VIRT_ADDR_SPACE_BITS)) -#endif - ) { + if (reserved_va >> shift !=3D unshifted + || (MAX_RESERVED_VA && reserved_va > MAX_RESERVED_VA)) { fprintf(stderr, "Reserved virtual address too big\n"); exit(EXIT_FAILURE); } --=20 2.9.4