From patchwork Thu Oct 5 14:36:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 114943 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp664060qgn; Thu, 5 Oct 2017 07:36:37 -0700 (PDT) X-Received: by 10.237.57.135 with SMTP id m7mr23756175qte.4.1507214197475; Thu, 05 Oct 2017 07:36:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1507214197; cv=none; d=google.com; s=arc-20160816; b=BfKohOBElthgY1WbmI9Ma7FM0ZDMRRUDFEHxngt6zO+9PCyzFBrsLOqTziISyixEPU +VwE9eTfqsK6CTsVSiVn9j5n+aWbDFNqLzGCPKgFH+2b51X+sPj0LTx4hjw0qZIkTk6T PW3DIGMwKDWNhkToThm4vmY/ku5si58Uwx4hmbbacRVcOmoMaNpoLdoPzZA6st6tr79k 3xJzJ5wd92IkTZ30AmGiti+N2XB7VX6bv6PXpx3/ulFowUziGcYe5bKldulRCkO2IFrC ZJPA3B+o1T3kbAn2WTpYyYiWnwVA2qbUlwjvXFeFz803Aea5iD9RHA3JPoJm51PyUHlJ bH+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=8MPya8y2mIfaj5u/2uwzfaPNBjR97VQyGpq6Qna7SJw=; b=WFUr/tIMzzCts4+JdXH0cf2HN/MN8v4JEE/0vITvA/6T7I8S+O0s/mHoUIwD1EBQ0N q83tfSlTNgpbpEdBOpIfX6WYzu1j3nutwq38wRPrqI/KM075PCn7YhjXy4wpPJZ0CAPS mIl67fpTF1CgTGTvSLxDWpf9+Me6xRWSZlexxgIkoSO0+ORK9i8nmaAE21HU+EIIDDbZ Y+Nh7pgQs75a6gA2TT2RVqopdz5c9O/Wu3XN8oU1Tz22AtXCbxG9HSOmaARNbPTovM5c K3OGzLJmODXsVh8p3cpVz9fgj5HQ18LtfUtll8lHkZxxNjXnIO1xOqV9yY0kSqszZS7Z 4T7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=FyFr1A/5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id t5si1833371qkd.182.2017.10.05.07.36.37 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 05 Oct 2017 07:36:37 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=FyFr1A/5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:40181 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e07GP-0002Ih-17 for patch@linaro.org; Thu, 05 Oct 2017 10:36:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40734) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e07G1-0002HT-0U for qemu-devel@nongnu.org; Thu, 05 Oct 2017 10:36:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e07Fz-0001x9-9j for qemu-devel@nongnu.org; Thu, 05 Oct 2017 10:36:12 -0400 Received: from mail-qt0-x236.google.com ([2607:f8b0:400d:c0d::236]:51190) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e07Fz-0001wj-4X for qemu-devel@nongnu.org; Thu, 05 Oct 2017 10:36:11 -0400 Received: by mail-qt0-x236.google.com with SMTP id f15so25743650qtf.7 for ; Thu, 05 Oct 2017 07:36:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8MPya8y2mIfaj5u/2uwzfaPNBjR97VQyGpq6Qna7SJw=; b=FyFr1A/5DZEUtDX+ZlKkgtBVE0vzZcK5S1FLCeJA/MFHRSknHUtq3U868zbHoOCmXU 38fp+KE0dOO4sFdWdN6IzLgGTHAdz8tA7rqzKA06+BfQlESWQtuEYLuyH+MP0OCHWwlO ww8ygwrcp/feYw7lhB5/me9rtu+Kdnbj9OGyg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8MPya8y2mIfaj5u/2uwzfaPNBjR97VQyGpq6Qna7SJw=; b=L82ygOaSALKhv05rW7NzQjkAi1zV+Wo35aN18r8ehgP5HehmiUKrqJ4f8euuZC5oOH RzyAV8+CNTP190v2PyKuXye/UaO2vUvX+frTwO7PqwL2140sXVJ8Q8rPDvN6SU/GsrmL 1GASeagbkM8+U4/aRhH1KTT0HoaDpSMQWeit/qptKLHCKsd36uwUFutyB65vWTYkT3Yc 9dRgSuSa2WhVbdO8EDi0x7t5dJbQdt600nUYKHjZ+YVON1X0sBJWUSEOqbZpbTxb+7aE vbPjydMMA2l7AXdAPKoTIoyqtkleh409UlED5Ng0PG8DrQhjeds5J7R0oGRHr1O9e0h9 fY1w== X-Gm-Message-State: AMCzsaUQ76A/FE4JHN8Q4/TUuc3RulqDD2hO1YqqMgJMvn2t4+e0GFuu wxWcv1aU+W3jXRr/WFy6CZqyz2DK98c= X-Google-Smtp-Source: AOwi7QB0Epdy9RJGyJsrVzOQvvpy7goxOCRX09WvRB9HusaGkIi6Oz581fo8UXJuTFFmnwNrrTRIAQ== X-Received: by 10.129.172.22 with SMTP id k22mr5356324ywh.52.1507214170224; Thu, 05 Oct 2017 07:36:10 -0700 (PDT) Received: from bigtime.twiddle.net.com ([2606:a000:7a4a:b100::1b]) by smtp.gmail.com with ESMTPSA id c13sm8277148ywm.101.2017.10.05.07.36.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 05 Oct 2017 07:36:09 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 5 Oct 2017 10:36:00 -0400 Message-Id: <20171005143601.21584-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171005143601.21584-1-richard.henderson@linaro.org> References: <20171005143601.21584-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::236 Subject: [Qemu-devel] [PATCH v2 3/4] 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: peter.maydell@linaro.org, riku.voipio@iki.fi, Richard Henderson Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Richard Henderson 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 Reviewed-by: Peter Maydell Message-Id: <20170708025030.15845-3-rth@twiddle.net> --- 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(-) -- 2.13.6 diff --git a/linux-user/arm/target_cpu.h b/linux-user/arm/target_cpu.h index d888219150..c3eb4b243d 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 +/* We need to be able to map the commpage. + See validate_guest_space in linux-user/elfload.c. */ +#define MAX_RESERVED_VA 0xffff0000ul + 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 047554ee45..d239069975 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 /* Masks used to mark instructions to indicate which ISA level they diff --git a/target/nios2/cpu.h b/target/nios2/cpu.h index 50d803a217..9119eee587 100644 --- a/target/nios2/cpu.h +++ b/target/nios2/cpu.h @@ -226,7 +226,11 @@ qemu_irq *nios2_cpu_pic_init(Nios2CPU *cpu); void nios2_check_interrupts(CPUNios2State *env); #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 #define cpu_init(cpu_model) cpu_generic_init(TYPE_NIOS2_CPU, cpu_model) diff --git a/linux-user/main.c b/linux-user/main.c index 829f974662..fd54d344bb 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -60,23 +60,38 @@ do { \ } \ } while (0) -#if (TARGET_LONG_BITS == 32) && (HOST_LONG_BITS == 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 stack * 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 = 0x77000000; +#ifndef MAX_RESERVED_VA +# if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS +# if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \ + (TARGET_LONG_BITS == 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 = 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 it, + if directed by the command-line option, but not by default. */ +#if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32 +unsigned long reserved_va = MAX_RESERVED_VA; #else unsigned long reserved_va; #endif @@ -3978,11 +3993,8 @@ static void handle_arg_reserved_va(const char *arg) unsigned long unshifted = reserved_va; p++; reserved_va <<= shift; - if (((reserved_va >> shift) != unshifted) -#if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS - || (reserved_va > (1ul << TARGET_VIRT_ADDR_SPACE_BITS)) -#endif - ) { + if (reserved_va >> shift != unshifted + || (MAX_RESERVED_VA && reserved_va > MAX_RESERVED_VA)) { fprintf(stderr, "Reserved virtual address too big\n"); exit(EXIT_FAILURE); }