From patchwork Mon Aug 7 16:36:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711133 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2126998ltf; Mon, 7 Aug 2023 09:37:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGIwL7ZKqDZaPA68ugODLacKakHwOVKBaC2rxcCeuYAq1qXwltSEkgeps0xSAeXDsDbd7um X-Received: by 2002:a05:620a:290d:b0:767:2d95:fae4 with SMTP id m13-20020a05620a290d00b007672d95fae4mr12413326qkp.73.1691426236806; Mon, 07 Aug 2023 09:37:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426236; cv=none; d=google.com; s=arc-20160816; b=K0spGyl+6coxMqS57p1fN2uT2SlY3tD71dfDPYhkW01Sg38cjzyJ1jbddNZ6Te1mHp n6Gm/ZWNAwthv3Dm3qKowfJUIZBTWxzvucPaIdMBYdQZy+pD+wtC9b5PAvmx/8nQdWR3 xJ/IXAKjEdZwyB/uPROxB1e1GX3wpK2GTROxU5yCPRx96qhitQgR42kzrDA1rKtWszot nMagl8MMZc2Nb4pAIWhpPdgs/kuuMu6vdsssXgznkjaGKqAc0FlY4tt0RzyWLBTMQwHY ky7U51fgGHMTHulBLgfni5nEidbMmYgBKnjf3IwWU+gAyyTL2WGEZVy8xvP56fABZUom WjAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=m/S7kO9TVAY5h9AXp3Rhk6dYJIKvvZfYNmVBr4WCkpo=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=zs29ISt3KHB2ycT9FI+okxL4gORyQqkTNPJFrNVZ6fi/VSDQCndGTXsTgAqccsKfkT vAyWusvqlcvKAVTXYKovorZRWJhqApU8PKBcXWr7GtOBlN4pv0U4bB81HglMY7q3ZJnR WDx7ZjZyTnh4VagLoM59GBmXLvONeD2pfPCIXDEUH9uiNiqQ+J+OQn24/weUx9B1TNwv BbdLBZGhVnQ9vMROdUjTzbZkzNcYTmmlXsjfVgIX2gjyaqC1YlCoDqFUfxVXGCNfcqbS zXmn94Y9XtsmDm+nncrCxx5TAqjWXv36Fz774f28DrcdkJWJlc29B9TKSMojkqetALc8 rFRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aVq26DmA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id bm33-20020a05620a19a100b00767d0ec9a2bsi5282304qkb.303.2023.08.07.09.37.16 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:37:16 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aVq26DmA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EG-00072f-OX; Mon, 07 Aug 2023 12:37:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EF-000700-CR for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:11 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3ED-0002fp-2f for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:11 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1bc6bfc4b58so7164165ad.1 for ; Mon, 07 Aug 2023 09:37:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426227; x=1692031027; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=m/S7kO9TVAY5h9AXp3Rhk6dYJIKvvZfYNmVBr4WCkpo=; b=aVq26DmAbXKGwBYvPnyi0T7cZiAIjbQVjYeTTHXWnvAnmgh6sAF/FvsCWTagbt9+2+ D2zJbke/wnihNY9N5H9vKAwGzw9P9knNuPyH5MUIVhx1SFmAlGQxrPvrmLHmsgxUpyik bS+q0D5n15av9ZARyD9kZy/86LaPXI+iAgSVOXsOgfR5CTv93GBIh/BS/ZYNgiMhl5Lf FiFwo0dlpHQloOaUIWLAFZ6yfC0ZLIx+ZB14744Ickpt/S8ngBYVU4qImawTdACEzzYK yUBAUW/21m9dtaiVxpS/ohHJpY6C0fAkR9Iu0sPGndclg5DwSpdHLDflEDxTbwW4sahD qd3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426227; x=1692031027; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m/S7kO9TVAY5h9AXp3Rhk6dYJIKvvZfYNmVBr4WCkpo=; b=L1yMCHKEvyitoByv/6NdDZDnfvDPTOe9XW1qw8m/qqEECgciUxYw16OvMRrqsQm/gv vG2w01iGu9gsYpIqGvZNz8odwUkHMCwi0km/TZOsNSGU7TZQf4cStPkv1bl+agzHhiPS PtyKGiiijOvOGMA+BMLqEJr4lvphyDiXuQDQ23JKo3JvOkRiTY6g7lKvp+xZuH4HUo2c HFxsmysRC5+iajxfcc8MPBsCPQIX8OSeYKoUAUMuN+510nH9bUPAeGVqDBCU3V0W59ef SAxlqpBilQfxekm7ZfzeGqtWSz245l21WU2oFWwVpnN6kb/nrsSs50U5zbgGOtiX8iUZ 54dQ== X-Gm-Message-State: AOJu0YyWIjeFyHTwAm18RwjGZocWYvbL80kwvSJBapQpcLXuwo0vvCI9 DCd8XUCh9pmsjhdzV/F8gqsAYi8IEDxKP/0Fz28= X-Received: by 2002:a17:903:2351:b0:1bb:d280:5e0b with SMTP id c17-20020a170903235100b001bbd2805e0bmr9039209plh.18.1691426227604; Mon, 07 Aug 2023 09:37:07 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:07 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 01/14] linux-user: Adjust task_unmapped_base for reserved_va Date: Mon, 7 Aug 2023 09:36:52 -0700 Message-Id: <20230807163705.9848-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Ensure that the chosen values for mmap_next_start and task_unmapped_base are within the guest address space. Tested-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson --- linux-user/user-mmap.h | 18 +++++++++++++++++- linux-user/main.c | 28 ++++++++++++++++++++++++++++ linux-user/mmap.c | 18 +++--------------- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/linux-user/user-mmap.h b/linux-user/user-mmap.h index 7265c2c116..fd456e024e 100644 --- a/linux-user/user-mmap.h +++ b/linux-user/user-mmap.h @@ -18,6 +18,23 @@ #ifndef LINUX_USER_USER_MMAP_H #define LINUX_USER_USER_MMAP_H +#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 +#ifdef TARGET_AARCH64 +# define TASK_UNMAPPED_BASE 0x5500000000 +#else +# define TASK_UNMAPPED_BASE (1ul << 38) +#endif +#else +#ifdef TARGET_HPPA +# define TASK_UNMAPPED_BASE 0xfa000000 +#else +# define TASK_UNMAPPED_BASE 0x40000000 +#endif +#endif + +extern abi_ulong task_unmapped_base; +extern abi_ulong mmap_next_start; + int target_mprotect(abi_ulong start, abi_ulong len, int prot); abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, int flags, int fd, off_t offset); @@ -26,7 +43,6 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, abi_ulong new_size, unsigned long flags, abi_ulong new_addr); abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice); -extern abi_ulong mmap_next_start; abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong); void mmap_fork_start(void); void mmap_fork_end(int child); diff --git a/linux-user/main.c b/linux-user/main.c index 556956c363..be621dc792 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -821,6 +821,34 @@ int main(int argc, char **argv, char **envp) reserved_va = max_reserved_va; } + /* + * Temporarily disable + * "comparison is always false due to limited range of data type" + * due to comparison between (possible) uint64_t and uintptr_t. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wtype-limits" + + /* + * Select an initial value for task_unmapped_base that is in range. + */ + if (reserved_va) { + if (TASK_UNMAPPED_BASE < reserved_va) { + task_unmapped_base = TASK_UNMAPPED_BASE; + } else { + /* The most common default formula is TASK_SIZE / 3. */ + task_unmapped_base = TARGET_PAGE_ALIGN(reserved_va / 3); + } + } else if (TASK_UNMAPPED_BASE < UINTPTR_MAX) { + task_unmapped_base = TASK_UNMAPPED_BASE; + } else { + /* 32-bit host: pick something medium size. */ + task_unmapped_base = 0x10000000; + } + mmap_next_start = task_unmapped_base; + +#pragma GCC diagnostic pop + { Error *err = NULL; if (seed_optarg != NULL) { diff --git a/linux-user/mmap.c b/linux-user/mmap.c index eb04fab8ab..84436d45c8 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -299,20 +299,8 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, return true; } -#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 -#ifdef TARGET_AARCH64 -# define TASK_UNMAPPED_BASE 0x5500000000 -#else -# define TASK_UNMAPPED_BASE (1ul << 38) -#endif -#else -#ifdef TARGET_HPPA -# define TASK_UNMAPPED_BASE 0xfa000000 -#else -# define TASK_UNMAPPED_BASE 0x40000000 -#endif -#endif -abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; +abi_ulong task_unmapped_base; +abi_ulong mmap_next_start; /* * Subroutine of mmap_find_vma, used when we have pre-allocated @@ -391,7 +379,7 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) if ((addr & (align - 1)) == 0) { /* Success. */ - if (start == mmap_next_start && addr >= TASK_UNMAPPED_BASE) { + if (start == mmap_next_start && addr >= task_unmapped_base) { mmap_next_start = addr + size; } return addr; From patchwork Mon Aug 7 16:36:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711135 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127200ltf; Mon, 7 Aug 2023 09:37:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGnMQMxdVH/luLl6T4h4DnFXA+ICg7CSSqnxSyGbcMEsrYF/ogrzL/R9XhLQVFnOHd42zz2 X-Received: by 2002:a05:620a:4045:b0:76c:c10a:a1bd with SMTP id i5-20020a05620a404500b0076cc10aa1bdmr16065146qko.4.1691426264714; Mon, 07 Aug 2023 09:37:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426264; cv=none; d=google.com; s=arc-20160816; b=aXYA8yNI7SCPWDX7TVjUJamqAL/hPMDygFVH15mT9W7fVIUKOAMUaeDmnVDA7BbjnD ZnfwzvkrtpLfP3SOJaLsEmixhGmv6sPqZ9qBVoWHhqKuneA1pvmilOiPIWEPj1ISU83s CCtz8kKZxDF2PEPjl5Y+MwpmRzXJ11a42v+wZ+urz7Pub3uOZQaKGqa1bxo89KB/Q4iY 3xJDbzViToHryVylwvFWbu9WzoxGer+bdSa5A8lQfV29LP3w7VP/ancq1MVEepNdcYLh aFyu4wX7Wanrlz4Jj8C8MNS7XNKG0IWyqqxtEyiSbtBYgaMsLKIpVluH8QLAgzmQx5++ bgNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nsfMjTlh4URCmzpsUVnlBQkKHMyACEHtsPPiByh/vaY=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=VbTtlXII2cNcR8yrrn6eCaBOztoY2Oi5nCyeS/3IjmxgEvz42OU2vDweDz89t5hQ3G 8XSWjjkBecBdoIvshrjZCdQWUjwmMHut/zbGeblhCg19VRd9G49tTDhGTOFaIJC661c/ 72c76qpK/z/8WV/KJlNgsbbnA9sTXrEPAmiax7AruAtFrjG6BM82O3cJEAuMcyThBhhG /csFyqoD4IMFc3E+jdTk4iiY76T5wM1x3In3Qv+366X/XXfgLeHOig6Ht3tTksmjMVXm GJoHASHamVoTdemDzI8oLA6H5n7r3QDezGLXgYspZFJlQgckgbGH553ewXfj+W6+X+cu /Brg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=qgN7hEIC; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id y7-20020a05620a44c700b0076cca39fa8dsi5553271qkp.296.2023.08.07.09.37.44 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:37:44 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=qgN7hEIC; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3Ec-0007dS-90; Mon, 07 Aug 2023 12:37:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EH-00072u-DP for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:14 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EE-0002g6-4W for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:13 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1bb2468257fso28817105ad.0 for ; Mon, 07 Aug 2023 09:37:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426229; x=1692031029; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nsfMjTlh4URCmzpsUVnlBQkKHMyACEHtsPPiByh/vaY=; b=qgN7hEIC8lY0a/po+yzWfVddfX/vX6eiR0lTbDEstvM95aNqd/FISBI0m7mGBqviEz pb9DeJjJxaer+FCYezsL/o+9ncJzlWZvHj7ZP/f25SkXBAKu7HYGyGlfTMzJzErKBDma Eq5mthjvu2W74vZfQlqgFAu1UrdnLkUj7r8ibKXORdWiire7kiTxe8IhjGADG1eU5p6e cFAqfAzY9AiUoUdcWtL1DCqZjh0DYGbKlK1Me6yDJuRwnA9n0jYrVai8rtK3yJLjZS8W 4Dwc3hAIAbnjTiZCDGSpHLVH0FRv39zcIans7psn5+Hyn70omnBpIrljgDm39T5sv2Th VhEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426229; x=1692031029; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nsfMjTlh4URCmzpsUVnlBQkKHMyACEHtsPPiByh/vaY=; b=Q51+2hb7+yYjiO2MJdzNLvn0T7jt/vxeP0EInVlsh3K86VN+XvhhH9AHxPL2dLPYud aTn8kLTHMoOBsgA/DKOaf+yMIuvSn60HnxWJ86iew6so5pGg+ZgcgTvC2cUWWVXLEEen ZufRSoWlrxLWcX3oTKeyVaQFcrewxA+R+mQ0i55M1Y70Q4N37kaiai2r7kWCTCbBESOa 3jco3dYVCUtLDZDNkn4JlwfPVD8kKyOY9MBHaRWZ2o7CoNZPrNzoAMiT/dp5PRVQmA5H eLgsT7nwEeDOVnHZlbMTkT12+qVja573BqvYlOYsrrc2oco9U31MBmdjuPVF8mEc2jxA xT0g== X-Gm-Message-State: AOJu0YytdKLXKvWtFh9lwX4ojJM98xA1StKb6TuQuhYfNyyZ/bee+5qM F4LFd6c02QtZDPHmuaRkaw/OnxzNR/U4dmRMWAQ= X-Received: by 2002:a17:902:db05:b0:1bc:7833:f5 with SMTP id m5-20020a170902db0500b001bc783300f5mr1638724plx.62.1691426228725; Mon, 07 Aug 2023 09:37:08 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 02/14] linux-user: Define TASK_UNMAPPED_BASE in $guest/target_mman.h Date: Mon, 7 Aug 2023 09:36:53 -0700 Message-Id: <20230807163705.9848-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Provide default values that are as close as possible to the values used by the guest's kernel. Tested-by: Helge Deller Reviewed-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/aarch64/target_mman.h | 10 ++++++++++ linux-user/alpha/target_mman.h | 8 ++++++++ linux-user/arm/target_mman.h | 8 ++++++++ linux-user/cris/target_mman.h | 9 +++++++++ linux-user/hexagon/target_mman.h | 10 ++++++++++ linux-user/hppa/target_mman.h | 3 +++ linux-user/i386/target_mman.h | 13 +++++++++++++ linux-user/loongarch64/target_mman.h | 8 ++++++++ linux-user/m68k/target_mman.h | 3 +++ linux-user/microblaze/target_mman.h | 8 ++++++++ linux-user/mips/target_mman.h | 7 +++++++ linux-user/nios2/target_mman.h | 7 +++++++ linux-user/openrisc/target_mman.h | 7 +++++++ linux-user/ppc/target_mman.h | 13 +++++++++++++ linux-user/riscv/target_mman.h | 7 +++++++ linux-user/s390x/target_mman.h | 10 ++++++++++ linux-user/sh4/target_mman.h | 4 ++++ linux-user/sparc/target_mman.h | 14 ++++++++++++++ linux-user/user-mmap.h | 14 -------------- linux-user/x86_64/target_mman.h | 12 ++++++++++++ linux-user/xtensa/target_mman.h | 6 ++++++ 21 files changed, 167 insertions(+), 14 deletions(-) diff --git a/linux-user/aarch64/target_mman.h b/linux-user/aarch64/target_mman.h index f721295fe1..4d3eecfb26 100644 --- a/linux-user/aarch64/target_mman.h +++ b/linux-user/aarch64/target_mman.h @@ -4,6 +4,16 @@ #define TARGET_PROT_BTI 0x10 #define TARGET_PROT_MTE 0x20 +/* + * arch/arm64/include/asm/processor.h: + * + * TASK_UNMAPPED_BASE DEFAULT_MAP_WINDOW / 4 + * DEFAULT_MAP_WINDOW DEFAULT_MAP_WINDOW_64 + * DEFAULT_MAP_WINDOW_64 UL(1) << VA_BITS_MIN + * VA_BITS_MIN 48 (unless explicitly configured smaller) + */ +#define TASK_UNMAPPED_BASE (1ull << (48 - 2)) + #include "../generic/target_mman.h" #endif diff --git a/linux-user/alpha/target_mman.h b/linux-user/alpha/target_mman.h index 6bb03e7336..c90b493711 100644 --- a/linux-user/alpha/target_mman.h +++ b/linux-user/alpha/target_mman.h @@ -20,6 +20,14 @@ #define TARGET_MS_SYNC 2 #define TARGET_MS_INVALIDATE 4 +/* + * arch/alpha/include/asm/processor.h: + * + * TASK_UNMAPPED_BASE TASK_SIZE / 2 + * TASK_SIZE 0x40000000000UL + */ +#define TASK_UNMAPPED_BASE 0x20000000000ull + #include "../generic/target_mman.h" #endif diff --git a/linux-user/arm/target_mman.h b/linux-user/arm/target_mman.h index e7ba6070fe..76275b2c7e 100644 --- a/linux-user/arm/target_mman.h +++ b/linux-user/arm/target_mman.h @@ -1 +1,9 @@ +/* + * arch/arm/include/asm/memory.h + * TASK_UNMAPPED_BASE ALIGN(TASK_SIZE / 3, SZ_16M) + * TASK_SIZE CONFIG_PAGE_OFFSET + * CONFIG_PAGE_OFFSET 0xC0000000 (default in Kconfig) + */ +#define TASK_UNMAPPED_BASE 0x40000000 + #include "../generic/target_mman.h" diff --git a/linux-user/cris/target_mman.h b/linux-user/cris/target_mman.h index e7ba6070fe..9df7b1eda5 100644 --- a/linux-user/cris/target_mman.h +++ b/linux-user/cris/target_mman.h @@ -1 +1,10 @@ +/* + * arch/cris/include/asm/processor.h: + * TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3)) + * + * arch/cris/include/arch-v32/arch/processor.h + * TASK_SIZE 0xb0000000 + */ +#define TASK_UNMAPPED_BASE TARGET_PAGE_ALIGN(0xb0000000 / 3) + #include "../generic/target_mman.h" diff --git a/linux-user/hexagon/target_mman.h b/linux-user/hexagon/target_mman.h index e7ba6070fe..c5ae336e07 100644 --- a/linux-user/hexagon/target_mman.h +++ b/linux-user/hexagon/target_mman.h @@ -1 +1,11 @@ +/* + * arch/hexgon/include/asm/processor.h + * TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) + * + * arch/hexagon/include/asm/mem-layout.h + * TASK_SIZE PAGE_OFFSET + * PAGE_OFFSET 0xc0000000 + */ +#define TASK_UNMAPPED_BASE 0x40000000 + #include "../generic/target_mman.h" diff --git a/linux-user/hppa/target_mman.h b/linux-user/hppa/target_mman.h index 97f87d042a..6459e7dbdd 100644 --- a/linux-user/hppa/target_mman.h +++ b/linux-user/hppa/target_mman.h @@ -24,6 +24,9 @@ #define TARGET_MS_ASYNC 2 #define TARGET_MS_INVALIDATE 4 +/* arch/parisc/include/asm/processor.h: DEFAULT_MAP_BASE32 */ +#define TASK_UNMAPPED_BASE 0x40000000 + #include "../generic/target_mman.h" #endif diff --git a/linux-user/i386/target_mman.h b/linux-user/i386/target_mman.h index e7ba6070fe..cc3382007f 100644 --- a/linux-user/i386/target_mman.h +++ b/linux-user/i386/target_mman.h @@ -1 +1,14 @@ +/* + * arch/x86/include/asm/processor.h: + * TASK_UNMAPPED_BASE __TASK_UNMAPPED_BASE(TASK_SIZE_LOW) + * __TASK_UNMAPPED_BASE(S) PAGE_ALIGN(S / 3) + * + * arch/x86/include/asm/page_32_types.h: + * TASK_SIZE_LOW TASK_SIZE + * TASK_SIZE __PAGE_OFFSET + * __PAGE_OFFSET CONFIG_PAGE_OFFSET + * CONFIG_PAGE_OFFSET 0xc0000000 (default in Kconfig) + */ +#define TASK_UNMAPPED_BASE 0x40000000 + #include "../generic/target_mman.h" diff --git a/linux-user/loongarch64/target_mman.h b/linux-user/loongarch64/target_mman.h index e7ba6070fe..d70e44d44c 100644 --- a/linux-user/loongarch64/target_mman.h +++ b/linux-user/loongarch64/target_mman.h @@ -1 +1,9 @@ +/* + * arch/loongarch/include/asm/processor.h: + * TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) + * TASK_SIZE64 0x1UL << (... ? VA_BITS : ...) + */ +#define TASK_UNMAPPED_BASE \ + TARGET_PAGE_ALIGN((1ull << TARGET_VIRT_ADDR_SPACE_BITS) / 3) + #include "../generic/target_mman.h" diff --git a/linux-user/m68k/target_mman.h b/linux-user/m68k/target_mman.h index e7ba6070fe..d3eceb663b 100644 --- a/linux-user/m68k/target_mman.h +++ b/linux-user/m68k/target_mman.h @@ -1 +1,4 @@ +/* arch/m68k/include/asm/processor.h */ +#define TASK_UNMAPPED_BASE 0xC0000000 + #include "../generic/target_mman.h" diff --git a/linux-user/microblaze/target_mman.h b/linux-user/microblaze/target_mman.h index e7ba6070fe..ffee869db4 100644 --- a/linux-user/microblaze/target_mman.h +++ b/linux-user/microblaze/target_mman.h @@ -1 +1,9 @@ +/* + * arch/microblaze/include/asm/processor.h: + * TASK_UNMAPPED_BASE (TASK_SIZE / 8 * 3) + * TASK_SIZE CONFIG_KERNEL_START + * CONFIG_KERNEL_START 0xc0000000 (default in Kconfig) + */ +#define TASK_UNMAPPED_BASE 0x48000000 + #include "../generic/target_mman.h" diff --git a/linux-user/mips/target_mman.h b/linux-user/mips/target_mman.h index e97694aa4e..fe1eec2d0b 100644 --- a/linux-user/mips/target_mman.h +++ b/linux-user/mips/target_mman.h @@ -14,6 +14,13 @@ #define TARGET_MAP_STACK 0x40000 #define TARGET_MAP_HUGETLB 0x80000 +/* + * arch/mips/include/asm/processor.h: + * TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) + */ +#define TASK_UNMAPPED_BASE \ + TARGET_PAGE_ALIGN((1ull << TARGET_VIRT_ADDR_SPACE_BITS) / 3) + #include "../generic/target_mman.h" #endif diff --git a/linux-user/nios2/target_mman.h b/linux-user/nios2/target_mman.h index e7ba6070fe..ce18f4f871 100644 --- a/linux-user/nios2/target_mman.h +++ b/linux-user/nios2/target_mman.h @@ -1 +1,8 @@ +/* + * arch/nios2/include/asm/processor.h: + * TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) + * TASK_SIZE 0x7FFF0000UL + */ +#define TASK_UNMAPPED_BASE TARGET_PAGE_ALIGN(0x7FFF0000 / 3) + #include "../generic/target_mman.h" diff --git a/linux-user/openrisc/target_mman.h b/linux-user/openrisc/target_mman.h index e7ba6070fe..f1aaad809d 100644 --- a/linux-user/openrisc/target_mman.h +++ b/linux-user/openrisc/target_mman.h @@ -1 +1,8 @@ +/* + * arch/openrisc/include/asm/processor.h: + * TASK_UNMAPPED_BASE (TASK_SIZE / 8 * 3) + * TASK_SIZE (0x80000000UL) + */ +#define TASK_UNMAPPED_BASE 0x30000000 + #include "../generic/target_mman.h" diff --git a/linux-user/ppc/target_mman.h b/linux-user/ppc/target_mman.h index 67cc218f2e..04f99c6077 100644 --- a/linux-user/ppc/target_mman.h +++ b/linux-user/ppc/target_mman.h @@ -4,6 +4,19 @@ #define TARGET_MAP_NORESERVE 0x40 #define TARGET_MAP_LOCKED 0x80 +/* + * arch/powerpc/include/asm/task_size_64.h + * TASK_UNMAPPED_BASE_USER32 (PAGE_ALIGN(TASK_SIZE_USER32 / 4)) + * TASK_UNMAPPED_BASE_USER64 (PAGE_ALIGN(DEFAULT_MAP_WINDOW_USER64 / 4)) + * TASK_SIZE_USER32 (0x0000000100000000UL - (1 * PAGE_SIZE)) + * DEFAULT_MAP_WINDOW_USER64 TASK_SIZE_64TB (with 4k pages) + */ +#ifdef TARGET_PPC64 +#define TASK_UNMAPPED_BASE 0x0000100000000000ull +#else +#define TASK_UNMAPPED_BASE 0x40000000 +#endif + #include "../generic/target_mman.h" #endif diff --git a/linux-user/riscv/target_mman.h b/linux-user/riscv/target_mman.h index e7ba6070fe..0f06dadbd4 100644 --- a/linux-user/riscv/target_mman.h +++ b/linux-user/riscv/target_mman.h @@ -1 +1,8 @@ +/* + * arch/loongarch/include/asm/processor.h: + * TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) + */ +#define TASK_UNMAPPED_BASE \ + TARGET_PAGE_ALIGN((1ull << (TARGET_VIRT_ADDR_SPACE_BITS - 1)) / 3) + #include "../generic/target_mman.h" diff --git a/linux-user/s390x/target_mman.h b/linux-user/s390x/target_mman.h index e7ba6070fe..40d149b329 100644 --- a/linux-user/s390x/target_mman.h +++ b/linux-user/s390x/target_mman.h @@ -1 +1,11 @@ +/* + * arch/s390/include/asm/processor.h: + * TASK_UNMAPPED_BASE (... : (_REGION2_SIZE >> 1)) + * + * arch/s390/include/asm/pgtable.h: + * _REGION2_SIZE (1UL << _REGION2_SHIFT) + * _REGION2_SHIFT 42 + */ +#define TASK_UNMAPPED_BASE (1ull << 41) + #include "../generic/target_mman.h" diff --git a/linux-user/sh4/target_mman.h b/linux-user/sh4/target_mman.h index e7ba6070fe..bbbc223398 100644 --- a/linux-user/sh4/target_mman.h +++ b/linux-user/sh4/target_mman.h @@ -1 +1,5 @@ +/* arch/sh/include/asm/processor_32.h */ +#define TASK_UNMAPPED_BASE \ + TARGET_PAGE_ALIGN((1u << TARGET_VIRT_ADDR_SPACE_BITS) / 3) + #include "../generic/target_mman.h" diff --git a/linux-user/sparc/target_mman.h b/linux-user/sparc/target_mman.h index 9bad99c852..692ebf9dd7 100644 --- a/linux-user/sparc/target_mman.h +++ b/linux-user/sparc/target_mman.h @@ -5,6 +5,20 @@ #define TARGET_MAP_LOCKED 0x100 #define TARGET_MAP_GROWSDOWN 0x0200 +/* + * arch/sparc/include/asm/page_64.h: + * TASK_UNMAPPED_BASE (test_thread_flag(TIF_32BIT) ? \ + * _AC(0x0000000070000000,UL) : \ + * VA_EXCLUDE_END) + * But VA_EXCLUDE_END is > 0xffff800000000000UL which doesn't work + * in userland emulation. + */ +#ifdef TARGET_ABI32 +#define TASK_UNMAPPED_BASE 0x70000000 +#else +#define TASK_UNMAPPED_BASE (1ull << (TARGET_VIRT_ADDR_SPACE_BITS - 2)) +#endif + #include "../generic/target_mman.h" #endif diff --git a/linux-user/user-mmap.h b/linux-user/user-mmap.h index fd456e024e..bae49059e0 100644 --- a/linux-user/user-mmap.h +++ b/linux-user/user-mmap.h @@ -18,20 +18,6 @@ #ifndef LINUX_USER_USER_MMAP_H #define LINUX_USER_USER_MMAP_H -#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 -#ifdef TARGET_AARCH64 -# define TASK_UNMAPPED_BASE 0x5500000000 -#else -# define TASK_UNMAPPED_BASE (1ul << 38) -#endif -#else -#ifdef TARGET_HPPA -# define TASK_UNMAPPED_BASE 0xfa000000 -#else -# define TASK_UNMAPPED_BASE 0x40000000 -#endif -#endif - extern abi_ulong task_unmapped_base; extern abi_ulong mmap_next_start; diff --git a/linux-user/x86_64/target_mman.h b/linux-user/x86_64/target_mman.h index e7ba6070fe..f9ff652b37 100644 --- a/linux-user/x86_64/target_mman.h +++ b/linux-user/x86_64/target_mman.h @@ -1 +1,13 @@ +/* + * arch/x86/include/asm/processor.h: + * TASK_UNMAPPED_BASE __TASK_UNMAPPED_BASE(TASK_SIZE_LOW) + * __TASK_UNMAPPED_BASE(S) PAGE_ALIGN(S / 3) + * + * arch/x86/include/asm/page_64_types.h: + * TASK_SIZE_LOW DEFAULT_MAP_WINDOW + * DEFAULT_MAP_WINDOW ((1UL << 47) - PAGE_SIZE) + */ +#define TASK_UNMAPPED_BASE \ + TARGET_PAGE_ALIGN((1ull << TARGET_VIRT_ADDR_SPACE_BITS) / 3) + #include "../generic/target_mman.h" diff --git a/linux-user/xtensa/target_mman.h b/linux-user/xtensa/target_mman.h index 3933771b5b..c4f671adb7 100644 --- a/linux-user/xtensa/target_mman.h +++ b/linux-user/xtensa/target_mman.h @@ -14,6 +14,12 @@ #define TARGET_MAP_STACK 0x40000 #define TARGET_MAP_HUGETLB 0x80000 +/* + * arch/xtensa/include/asm/processor.h: + * TASK_UNMAPPED_BASE (TASK_SIZE / 2) + */ +#define TASK_UNMAPPED_BASE (1u << (TARGET_VIRT_ADDR_SPACE_BITS - 1)) + #include "../generic/target_mman.h" #endif From patchwork Mon Aug 7 16:36:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711141 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127661ltf; Mon, 7 Aug 2023 09:38:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFIX8BxhxqNQyuqrsJIhNZ31RpfqZm8nVcLa1IVaOvv2tcgsb/6UKw1G6GlkwOeX4ckDdpl X-Received: by 2002:a05:622a:148f:b0:40f:e4b2:4ba8 with SMTP id t15-20020a05622a148f00b0040fe4b24ba8mr14711148qtx.65.1691426315968; Mon, 07 Aug 2023 09:38:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426315; cv=none; d=google.com; s=arc-20160816; b=NC9eeuyHgUsSxeGnYFOvpvAtDRtC73Kshf7UTHkskWM3f5sYrQjHQ38pMS79r3T9jS OKF7g5/ZoDJRSU/5+L2g4UAniW0ddmoUxhnc3JSMDBZ3e8FhGRg7KFtITeB8r82twNN1 ZHfpcWsRmyKhy/Zwa0/20NB4MjP2+LSFgri0FK/xYs3lAXoDs7BGLV+oftbQewnPHy9f Ees76CnZ5nKCLiyjwg8ue4VDEQLmnL5IgCBsPUf/xosKJMvhb5qpNi40WRzSXGYXkGud /Qph8kvJuxbAwvXNoW6Dwi3Hrrthaz/kpqSTK2ntjx3tsvUc5NSzo2bp+ybLkj+jvoqW 49Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=YQiLPVCfyeMX/HaaqqWD824ZzDbo71gJMZBplamcMFg=; fh=nM8F09xKNAze8fdJGCDZoH2K0rjOXqNih6JoyRf4NS8=; b=ZRYp+EsfQnTMjjSpAzCefRe57xlvv1/fAcAYaJCh4JkrdXN2aSimxyuOX/Nml0mayB qNBiQ5le5SofX9K8VLCxNd7cZrHdf9hGri7nL9tkSUvmIhxVecs9P9tqmoeW8697KX4K FxlGqf1tm8Cma0PaiVfGNLgJRUnVHGO5twElmGY/xJXb+YDt6botn3wAr3v9iSVZEad6 UQGxoSdSZ3SZQGPUf0w7CAQite+iDpm3RFZKQLvmacJMoVNAatX94MDih3T+std6bP5F nxKxw0oUBqqTzv3SpPAXcD5xOo9rXmCL+arS0YfOKuDy50/gNyHkirgz7iFqfXQG7yHc KHgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JA26p7Yv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d13-20020a05622a100d00b0040eb18dbe18si5304142qte.410.2023.08.07.09.38.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:38:35 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JA26p7Yv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EW-0007MP-Oc; Mon, 07 Aug 2023 12:37:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EH-00072t-CB for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:14 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EF-0002gJ-4u for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:13 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1b8b4748fe4so30495325ad.1 for ; Mon, 07 Aug 2023 09:37:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426229; x=1692031029; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YQiLPVCfyeMX/HaaqqWD824ZzDbo71gJMZBplamcMFg=; b=JA26p7Yve1850Z5Nu90y1TEL4FQajHlGbJYBRs432wn8oJR4BG/gUn5TtAep0T0A2r r+JcVFvLKCOryKqccL4slnUr9pcMOc7zNGoP3U7LgoLPwvBrUA44osLiYFTsYkWuEpKV gKwNPua328QhF/g9HxNT3S4qgjY9dCN/kQ5mse06g2e6ygi00s2LTvoEVg6oCtGoII6+ GPEJXi9SIO3bs4r7XIcTlwWwrlcl0qrKtbj2d4i7QvioL49rj1rjeWh3bDF1bSuaWGqn mgV1aV+2Xi3qa68Ioi2IBXsBtFVepzfqPiG/FO2pOZkYvLKHr5cpvq1WCKkmq/lVv5Ik 5V8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426229; x=1692031029; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YQiLPVCfyeMX/HaaqqWD824ZzDbo71gJMZBplamcMFg=; b=UYScA77E7oXlQZJLRLb0A10sspRghTvo4A4xgaShgQE4DoEwmQ5Y2LA0YTXOqerUmP M8DeZ5wLlc+wKEIX2l78uFGTp/dqMKU6PYJZeGa6PtDYLAdhkgPlvcFBzJ1sia+INYQx 4xt/JDhfPdd31G9DHPnU3WA/6KjECMHAntC0z0nPmhg+ZPSvSPkSI0nbEOl2Sv4bDsrO ZTLOhqpCqfh6tVX4ITJEEwqzLZAw6RnoBcVz4f+1ZT893Bzes+51+v+OziBXMbYgKPPz NSkbygDq2gXftya9yLJJbofHrvSRmBYPH4UGX8B8qQxQzetsD9aLaq1xzzugknavlcbA RC/A== X-Gm-Message-State: AOJu0Yzd6/l/aMcG6MiTVzSNAGb+nU4MyTLf5y0tzdn02jHzFMzBRtK3 r1KTU+L4KbDOwI5gJosbdTcT5uh61dgADiHZZXY= X-Received: by 2002:a17:902:f691:b0:1b6:bced:1dc2 with SMTP id l17-20020a170902f69100b001b6bced1dc2mr9833518plg.0.1691426229581; Mon, 07 Aug 2023 09:37:09 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:09 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de Subject: [PATCH for-8.1 v10 03/14] linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h Date: Mon, 7 Aug 2023 09:36:54 -0700 Message-Id: <20230807163705.9848-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Copy each guest kernel's default value, then bound it against reserved_va or the host address space. Tested-by: Helge Deller Reviewed-by: Helge Deller Signed-off-by: Richard Henderson --- linux-user/aarch64/target_mman.h | 3 +++ linux-user/alpha/target_mman.h | 3 +++ linux-user/arm/target_mman.h | 3 +++ linux-user/cris/target_mman.h | 3 +++ linux-user/hexagon/target_mman.h | 3 +++ linux-user/hppa/target_mman.h | 3 +++ linux-user/i386/target_mman.h | 3 +++ linux-user/loongarch64/target_mman.h | 3 +++ linux-user/m68k/target_mman.h | 2 ++ linux-user/microblaze/target_mman.h | 3 +++ linux-user/mips/target_mman.h | 3 +++ linux-user/nios2/target_mman.h | 3 +++ linux-user/openrisc/target_mman.h | 3 +++ linux-user/ppc/target_mman.h | 7 +++++++ linux-user/riscv/target_mman.h | 3 +++ linux-user/s390x/target_mman.h | 10 ++++++++++ linux-user/sh4/target_mman.h | 3 +++ linux-user/sparc/target_mman.h | 11 +++++++++++ linux-user/user-mmap.h | 1 + linux-user/x86_64/target_mman.h | 3 +++ linux-user/xtensa/target_mman.h | 4 ++++ linux-user/main.c | 15 +++++++++++++++ linux-user/mmap.c | 1 + 23 files changed, 96 insertions(+) diff --git a/linux-user/aarch64/target_mman.h b/linux-user/aarch64/target_mman.h index 4d3eecfb26..69ec5d5739 100644 --- a/linux-user/aarch64/target_mman.h +++ b/linux-user/aarch64/target_mman.h @@ -14,6 +14,9 @@ */ #define TASK_UNMAPPED_BASE (1ull << (48 - 2)) +/* arch/arm64/include/asm/elf.h */ +#define ELF_ET_DYN_BASE TARGET_PAGE_ALIGN((1ull << 48) / 3 * 2) + #include "../generic/target_mman.h" #endif diff --git a/linux-user/alpha/target_mman.h b/linux-user/alpha/target_mman.h index c90b493711..8edfe2b88c 100644 --- a/linux-user/alpha/target_mman.h +++ b/linux-user/alpha/target_mman.h @@ -28,6 +28,9 @@ */ #define TASK_UNMAPPED_BASE 0x20000000000ull +/* arch/alpha/include/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000) + #include "../generic/target_mman.h" #endif diff --git a/linux-user/arm/target_mman.h b/linux-user/arm/target_mman.h index 76275b2c7e..51005da869 100644 --- a/linux-user/arm/target_mman.h +++ b/linux-user/arm/target_mman.h @@ -6,4 +6,7 @@ */ #define TASK_UNMAPPED_BASE 0x40000000 +/* arch/arm/include/asm/elf.h */ +#define ELF_ET_DYN_BASE 0x00400000 + #include "../generic/target_mman.h" diff --git a/linux-user/cris/target_mman.h b/linux-user/cris/target_mman.h index 9df7b1eda5..9ace8ac292 100644 --- a/linux-user/cris/target_mman.h +++ b/linux-user/cris/target_mman.h @@ -7,4 +7,7 @@ */ #define TASK_UNMAPPED_BASE TARGET_PAGE_ALIGN(0xb0000000 / 3) +/* arch/cris/include/uapi/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE * 2) + #include "../generic/target_mman.h" diff --git a/linux-user/hexagon/target_mman.h b/linux-user/hexagon/target_mman.h index c5ae336e07..e6b5e2ca36 100644 --- a/linux-user/hexagon/target_mman.h +++ b/linux-user/hexagon/target_mman.h @@ -8,4 +8,7 @@ */ #define TASK_UNMAPPED_BASE 0x40000000 +/* arch/hexagon/include/asm/elf.h */ +#define ELF_ET_DYN_BASE 0x08000000 + #include "../generic/target_mman.h" diff --git a/linux-user/hppa/target_mman.h b/linux-user/hppa/target_mman.h index 6459e7dbdd..ccda46e842 100644 --- a/linux-user/hppa/target_mman.h +++ b/linux-user/hppa/target_mman.h @@ -27,6 +27,9 @@ /* arch/parisc/include/asm/processor.h: DEFAULT_MAP_BASE32 */ #define TASK_UNMAPPED_BASE 0x40000000 +/* arch/parisc/include/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x01000000) + #include "../generic/target_mman.h" #endif diff --git a/linux-user/i386/target_mman.h b/linux-user/i386/target_mman.h index cc3382007f..e3b8e1eaa6 100644 --- a/linux-user/i386/target_mman.h +++ b/linux-user/i386/target_mman.h @@ -11,4 +11,7 @@ */ #define TASK_UNMAPPED_BASE 0x40000000 +/* arch/x86/include/asm/elf.h */ +#define ELF_ET_DYN_BASE 0x00400000 + #include "../generic/target_mman.h" diff --git a/linux-user/loongarch64/target_mman.h b/linux-user/loongarch64/target_mman.h index d70e44d44c..8c2a3d5596 100644 --- a/linux-user/loongarch64/target_mman.h +++ b/linux-user/loongarch64/target_mman.h @@ -6,4 +6,7 @@ #define TASK_UNMAPPED_BASE \ TARGET_PAGE_ALIGN((1ull << TARGET_VIRT_ADDR_SPACE_BITS) / 3) +/* arch/loongarch/include/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE * 2) + #include "../generic/target_mman.h" diff --git a/linux-user/m68k/target_mman.h b/linux-user/m68k/target_mman.h index d3eceb663b..20cfe750c5 100644 --- a/linux-user/m68k/target_mman.h +++ b/linux-user/m68k/target_mman.h @@ -1,4 +1,6 @@ /* arch/m68k/include/asm/processor.h */ #define TASK_UNMAPPED_BASE 0xC0000000 +/* arch/m68k/include/asm/elf.h */ +#define ELF_ET_DYN_BASE 0xD0000000 #include "../generic/target_mman.h" diff --git a/linux-user/microblaze/target_mman.h b/linux-user/microblaze/target_mman.h index ffee869db4..6b3dd54f89 100644 --- a/linux-user/microblaze/target_mman.h +++ b/linux-user/microblaze/target_mman.h @@ -6,4 +6,7 @@ */ #define TASK_UNMAPPED_BASE 0x48000000 +/* arch/microblaze/include/uapi/asm/elf.h */ +#define ELF_ET_DYN_BASE 0x08000000 + #include "../generic/target_mman.h" diff --git a/linux-user/mips/target_mman.h b/linux-user/mips/target_mman.h index fe1eec2d0b..b84fe1e8a8 100644 --- a/linux-user/mips/target_mman.h +++ b/linux-user/mips/target_mman.h @@ -21,6 +21,9 @@ #define TASK_UNMAPPED_BASE \ TARGET_PAGE_ALIGN((1ull << TARGET_VIRT_ADDR_SPACE_BITS) / 3) +/* arch/mips/include/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE * 2) + #include "../generic/target_mman.h" #endif diff --git a/linux-user/nios2/target_mman.h b/linux-user/nios2/target_mman.h index ce18f4f871..ab16ad4f03 100644 --- a/linux-user/nios2/target_mman.h +++ b/linux-user/nios2/target_mman.h @@ -5,4 +5,7 @@ */ #define TASK_UNMAPPED_BASE TARGET_PAGE_ALIGN(0x7FFF0000 / 3) +/* arch/nios2/include/asm/elf.h */ +#define ELF_ET_DYN_BASE 0xD0000000 + #include "../generic/target_mman.h" diff --git a/linux-user/openrisc/target_mman.h b/linux-user/openrisc/target_mman.h index f1aaad809d..243c1d5f26 100644 --- a/linux-user/openrisc/target_mman.h +++ b/linux-user/openrisc/target_mman.h @@ -5,4 +5,7 @@ */ #define TASK_UNMAPPED_BASE 0x30000000 +/* arch/openrisc/include/asm/elf.h */ +#define ELF_ET_DYN_BASE 0x08000000 + #include "../generic/target_mman.h" diff --git a/linux-user/ppc/target_mman.h b/linux-user/ppc/target_mman.h index 04f99c6077..646d1ccae7 100644 --- a/linux-user/ppc/target_mman.h +++ b/linux-user/ppc/target_mman.h @@ -17,6 +17,13 @@ #define TASK_UNMAPPED_BASE 0x40000000 #endif +/* arch/powerpc/include/asm/elf.h */ +#ifdef TARGET_PPC64 +#define ELF_ET_DYN_BASE 0x100000000ull +#else +#define ELF_ET_DYN_BASE 0x000400000 +#endif + #include "../generic/target_mman.h" #endif diff --git a/linux-user/riscv/target_mman.h b/linux-user/riscv/target_mman.h index 0f06dadbd4..3049bcc67d 100644 --- a/linux-user/riscv/target_mman.h +++ b/linux-user/riscv/target_mman.h @@ -5,4 +5,7 @@ #define TASK_UNMAPPED_BASE \ TARGET_PAGE_ALIGN((1ull << (TARGET_VIRT_ADDR_SPACE_BITS - 1)) / 3) +/* arch/riscv/include/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE * 2) + #include "../generic/target_mman.h" diff --git a/linux-user/s390x/target_mman.h b/linux-user/s390x/target_mman.h index 40d149b329..c82435e381 100644 --- a/linux-user/s390x/target_mman.h +++ b/linux-user/s390x/target_mman.h @@ -8,4 +8,14 @@ */ #define TASK_UNMAPPED_BASE (1ull << 41) +/* + * arch/s390/include/asm/elf.h: + * ELF_ET_DYN_BASE (STACK_TOP / 3 * 2) & ~((1UL << 32) - 1) + * + * arch/s390/include/asm/processor.h: + * STACK_TOP VDSO_LIMIT - VDSO_SIZE - PAGE_SIZE + * VDSO_LIMIT _REGION2_SIZE + */ +#define ELF_ET_DYN_BASE (((1ull << 42) / 3 * 2) & ~0xffffffffull) + #include "../generic/target_mman.h" diff --git a/linux-user/sh4/target_mman.h b/linux-user/sh4/target_mman.h index bbbc223398..dd9016081e 100644 --- a/linux-user/sh4/target_mman.h +++ b/linux-user/sh4/target_mman.h @@ -2,4 +2,7 @@ #define TASK_UNMAPPED_BASE \ TARGET_PAGE_ALIGN((1u << TARGET_VIRT_ADDR_SPACE_BITS) / 3) +/* arch/sh/include/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE * 2) + #include "../generic/target_mman.h" diff --git a/linux-user/sparc/target_mman.h b/linux-user/sparc/target_mman.h index 692ebf9dd7..696ca73fe4 100644 --- a/linux-user/sparc/target_mman.h +++ b/linux-user/sparc/target_mman.h @@ -19,6 +19,17 @@ #define TASK_UNMAPPED_BASE (1ull << (TARGET_VIRT_ADDR_SPACE_BITS - 2)) #endif +/* + * arch/sparc/include/asm/elf_64.h + * Except that COMPAT_ELF_ET_DYN_BASE exactly matches TASK_UNMAPPED_BASE, + * so move it up a bit. + */ +#ifdef TARGET_ABI32 +#define ELF_ET_DYN_BASE 0x78000000 +#else +#define ELF_ET_DYN_BASE 0x0000010000000000ull +#endif + #include "../generic/target_mman.h" #endif diff --git a/linux-user/user-mmap.h b/linux-user/user-mmap.h index bae49059e0..5dd48a458d 100644 --- a/linux-user/user-mmap.h +++ b/linux-user/user-mmap.h @@ -20,6 +20,7 @@ extern abi_ulong task_unmapped_base; extern abi_ulong mmap_next_start; +extern abi_ulong elf_et_dyn_base; int target_mprotect(abi_ulong start, abi_ulong len, int prot); abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, diff --git a/linux-user/x86_64/target_mman.h b/linux-user/x86_64/target_mman.h index f9ff652b37..48fbf20b42 100644 --- a/linux-user/x86_64/target_mman.h +++ b/linux-user/x86_64/target_mman.h @@ -10,4 +10,7 @@ #define TASK_UNMAPPED_BASE \ TARGET_PAGE_ALIGN((1ull << TARGET_VIRT_ADDR_SPACE_BITS) / 3) +/* arch/x86/include/asm/elf.h */ +#define ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE * 2) + #include "../generic/target_mman.h" diff --git a/linux-user/xtensa/target_mman.h b/linux-user/xtensa/target_mman.h index c4f671adb7..8fa6337a97 100644 --- a/linux-user/xtensa/target_mman.h +++ b/linux-user/xtensa/target_mman.h @@ -20,6 +20,10 @@ */ #define TASK_UNMAPPED_BASE (1u << (TARGET_VIRT_ADDR_SPACE_BITS - 1)) +/* arch/xtensa/include/asm/elf.h */ +#define ELF_ET_DYN_BASE \ + TARGET_PAGE_ALIGN((1u << TARGET_VIRT_ADDR_SPACE_BITS) / 3) + #include "../generic/target_mman.h" #endif diff --git a/linux-user/main.c b/linux-user/main.c index be621dc792..96be354897 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -847,6 +847,21 @@ int main(int argc, char **argv, char **envp) } mmap_next_start = task_unmapped_base; + /* Similarly for elf_et_dyn_base. */ + if (reserved_va) { + if (ELF_ET_DYN_BASE < reserved_va) { + elf_et_dyn_base = ELF_ET_DYN_BASE; + } else { + /* The most common default formula is TASK_SIZE / 3 * 2. */ + elf_et_dyn_base = TARGET_PAGE_ALIGN(reserved_va / 3) * 2; + } + } else if (ELF_ET_DYN_BASE < UINTPTR_MAX) { + elf_et_dyn_base = ELF_ET_DYN_BASE; + } else { + /* 32-bit host: pick something medium size. */ + elf_et_dyn_base = 0x18000000; + } + #pragma GCC diagnostic pop { diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 84436d45c8..949c4090f3 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -301,6 +301,7 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, abi_ulong task_unmapped_base; abi_ulong mmap_next_start; +abi_ulong elf_et_dyn_base; /* * Subroutine of mmap_find_vma, used when we have pre-allocated From patchwork Mon Aug 7 16:36:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711134 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127144ltf; Mon, 7 Aug 2023 09:37:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGPIhvPVt22P5+QtDj15P1w+lD98dXHlkdWc4xcruC0X0GOvYhkkqafW3mHKyoEnngmECys X-Received: by 2002:ac8:5d8c:0:b0:3e4:e2ce:526f with SMTP id d12-20020ac85d8c000000b003e4e2ce526fmr13143790qtx.39.1691426255518; Mon, 07 Aug 2023 09:37:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426255; cv=none; d=google.com; s=arc-20160816; b=S2A5yirwc5sJAhD+qOxy2I5q9S3EFpDIfQpKoRrwNTKYBcnSxq7r15y7sCtNY5sA9n RJxuVZuDLcnZdc7uBJOEYLhqk8tPXbnezp8h225DeWGmbR7fVsatWev79p0b/EaJfRDd yHQxsAzCsGwoFuVBdyIkuhL5M9w1OEwfBr4gEhWXI6BOu9HJpV3/XAWr+YOXs6NaJ9f6 /nGG0Sj79uKJuvt/MbHea9RRlRYWVcoFmWrKRb2UuZM8+aP5Hi/gTQebFN658RYDHgRh c+iEzkKqLE1f8iozCNGYnKaW1wS3piWu8KaZlolB9drPEJn4NBgNCDhLKxfXPFGDj6aP awAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SJUFfIS4Wr1y8fuwf4as7ebNw3oHaTbxYse/4v8c7ug=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=JmNEIR8Z2oSYp/30kBefqiX1XH4kkDgP5ECuSn2n+BBHRl9wiStX4101VCSmz57vRG xDRmHCr+Pr0GhLTeU9z0GpXSlepojdPNqE+DKdvJUpMjD1v+rFyswAQp3Ud2NYPpMH6i RtxSrA1xsJgGedAbFnKxiz27q9xuuNFfcsfSesFIw6zgui02LvOmjwQBX0uq0uGsBYuS WpkmTOdD+h1FOxXjxb9CKseipOTzSwsO1gepLvTQGIvrLTWUXKN7wgr9KPBjCZKkWyMk OaZA7YafXWlArn17EyQsG6j61Jq44QGQKurS0iM197hu7Kq/mATk/TlMUhLTFyyCu/Pd 3c5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="E/VEtqpp"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a5-20020ac85b85000000b00405d0bc11f0si5103433qta.563.2023.08.07.09.37.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:37:35 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="E/VEtqpp"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3ES-00079R-UW; Mon, 07 Aug 2023 12:37:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EH-00072s-92 for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:14 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EF-0002gU-Nk for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:13 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1bc73a2b0easo5470265ad.0 for ; Mon, 07 Aug 2023 09:37:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426230; x=1692031030; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SJUFfIS4Wr1y8fuwf4as7ebNw3oHaTbxYse/4v8c7ug=; b=E/VEtqppstgRkSvtLGxvUOl/Gdqzl9f/roRZtLAClsJKncpE1H3Id8ICCtNnqdc7fC 5JD8DE7aiPvhCmpnocjUtn6dTwtayqFkAVACOPxOcrGVgkGoTnwvap94sVCb1zO80Cfs ExLHIitUKXmpL+IfN6dG8Zsnx6T8aQNmu+e6rhcFLVGqHWb+eJqaqxipab9wWuvcF/PC lEPWtbF0MMJJS/N5uIMfP8JdWeVkUqPgQ2ffWF//3WPwgka48a3sR/aSfscUv6CQRmHN 6PjeIdHbq8Ltca/RfwnSY4ohB8ZrPsZGwWIXISvOBk3xgVtSkDZBQx5zaAbBbvZJwq0X RxLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426230; x=1692031030; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SJUFfIS4Wr1y8fuwf4as7ebNw3oHaTbxYse/4v8c7ug=; b=P5O6iaGuim8QADuPfC60+nqT2KomfkK8ojplzZz7GvJPOWe5lbXPvbeEb9TeEzkH8w vtDNlTSSQisSeLCV/W6i/XSGorqw9X/tgUTDp3oWAjSBss7RldQU7yQWj7fyEKy4s9r5 d925uENk4XZbvLCn7sKC1Xg2A/pAm1BHGhGneaRfDMZK81B3sQa9oSWRWkOL2uvUv91P 3En7NnP1v6NYlpfEQ6iLp+WnafzwiOhrO1xOcHkumj9ykO/UjDpqI9Bp/BpYd0NVDuZ0 g3EJJB5d0ORqlks9h1J3B3Ifa08DP/z3BBuZ4pP+oFHOuQ7XQFh1jSu0T5DQPcyLhoE0 dQvA== X-Gm-Message-State: AOJu0Ywtviy7GdIQTpzzxUII8VLkYH6hKlb6F8ikQIhyIV4MkJ8FIrg8 V2lelJZwfE9e5VYh6O7m+yTUq+GWg+VTFCRon8Y= X-Received: by 2002:a17:902:82c5:b0:1b8:6cae:4400 with SMTP id u5-20020a17090282c500b001b86cae4400mr7659675plz.37.1691426230493; Mon, 07 Aug 2023 09:37:10 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:10 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 04/14] linux-user: Use MAP_FIXED_NOREPLACE for initial image mmap Date: Mon, 7 Aug 2023 09:36:55 -0700 Message-Id: <20230807163705.9848-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Use this as extra protection for the guest mapping over any qemu host mappings. Tested-by: Helge Deller Reviewed-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson --- linux-user/elfload.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 36e4026f05..1b4bb2d5af 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3147,8 +3147,11 @@ static void load_elf_image(const char *image_name, int image_fd, /* * Reserve address space for all of this. * - * In the case of ET_EXEC, we supply MAP_FIXED so that we get - * exactly the address range that is required. + * In the case of ET_EXEC, we supply MAP_FIXED_NOREPLACE so that we get + * exactly the address range that is required. Without reserved_va, + * the guest address space is not isolated. We have attempted to avoid + * conflict with the host program itself via probe_guest_base, but using + * MAP_FIXED_NOREPLACE instead of MAP_FIXED provides an extra check. * * Otherwise this is ET_DYN, and we are searching for a location * that can hold the memory space required. If the image is @@ -3160,7 +3163,7 @@ static void load_elf_image(const char *image_name, int image_fd, */ load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | - (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0), + (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0), -1, 0); if (load_addr == -1) { goto exit_mmap; From patchwork Mon Aug 7 16:36:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711142 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127712ltf; Mon, 7 Aug 2023 09:38:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEb80hK/cgve6Avc3H7dj6/6SRYVOV7aMmp8FWDDoB5mAfvuUip0dtLVSIfI1siqZ1kl8qv X-Received: by 2002:ac8:580f:0:b0:403:b9ea:7b82 with SMTP id g15-20020ac8580f000000b00403b9ea7b82mr15573016qtg.29.1691426322253; Mon, 07 Aug 2023 09:38:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426322; cv=none; d=google.com; s=arc-20160816; b=eApyxNfsLGLy5C2lpxp/rVybkQi/4GxgCEpXPStP4AfOhjparpAyqNmlnwjSuSkqB7 fFeV8LT/oSBBdBPoksBD5yeLs9sbosoZliFpoKSBriEHCg2fgpOJXfRnoqAAtcsD6z95 I7n0WxbAQQIugPAoPZWqfw92M07PjqDJNYsVM1UGOoXyF8ypke72RXf3e7cY03hhl8XM sTeqmjw+ZRyHDIo8jjiDRy/DLdlqYZhuBGGeFa0tKcTIf7PLS1C0dNa2zaPsX68z878P /KYP9sxEYdCXaLp5+xuBW1epQFtKeIuvXBuvNuAswmaUln1jmNHuE3WkiIWKhKYKIIBg 2TIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gdGMnmtdSmtXaxIRFinY8s6zOHM1AhXo+8V5ig/K9Os=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=K6fsFL0kJyuapPU2Gso3cedgxNyBGUU9CN5cHk0OhcRif525AETwDr3mTrzq84uyoU h6WMXUTfJ+EsRqnQp3N/LzzrqYEauunG5R97RgYssXn3Z0G5NkVHPtXNjLd5aooB6PSW SGHEUqy+KJNP5H5ZVeNrm2UUhTW8SLWgcQE2/mcvSKDCsZ9NJ47opiWcNwEoQkb2jj/t N+byqDBhOB7W546lCjeJcf/XhS+d4Ky3hoMo9LGT4Jd6p5Bd9M/cymjXQegKRGysQy8f /G8P5REMhg/YCGtCq3clkY0ShOFkXsvu8hJmDHyJ8uNpetoQrAwfp+I09Dh4H5UcrDqt U4iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="UVG7G/Ca"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id x7-20020ac85f07000000b0040ee4ab47d4si5204210qta.594.2023.08.07.09.38.42 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:38:42 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="UVG7G/Ca"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EV-0007Ed-KD; Mon, 07 Aug 2023 12:37:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EI-00072v-AO for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:14 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EG-0002gn-LB for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:14 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1bbf8cb61aeso29993225ad.2 for ; Mon, 07 Aug 2023 09:37:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426231; x=1692031031; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gdGMnmtdSmtXaxIRFinY8s6zOHM1AhXo+8V5ig/K9Os=; b=UVG7G/CadsGT6SZkxRHBTw6gJdpmAysIbdAt3gNz/0BM7H7fy6AINFopvYVHOKHfvn gHI/0FEGAc8OFnfnhKvsPeF9384P9CO7hJwGD6BGgvI0HTn2TQbjQ+BFb8V/elY7SXCX Yz3bLiIG7WB2izulCWNKH0e6a66hJiu7vuBYMa5JRWt5Tea2AOMhI8srMylWsaSFIDFx tocacSySoD3IDZkHXnqkZp1xfZhmWZiUwsFB/m/OkFHCfhahJc7TgrWckMOFBuFDn8ia NzPpbz4XWY9TSeO1k3nkeR85UurTDqQE/RQe4HzQ5ot3DPjgW5htsoUZyXAKvvWuU50H hXNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426231; x=1692031031; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gdGMnmtdSmtXaxIRFinY8s6zOHM1AhXo+8V5ig/K9Os=; b=Nw4y3mWhxmYeFouhRUo/OdkZwkpUEzoPfXInq683Ng6TvXwETNtrKJMO2UC2r24hBN NAy7pBWgldB68ifu56VEL3wPwS5CgMmoE7keJJATppH4H9Y76RHWtpCCDUTT3pSGoEMz ulmAAnns1p1KTMJF3K+QDwTECsfL+iXU5VWnL5rXmUI40A03WPQhcV0OtKb7+OoWMUK4 BQUUaV4rLvlkv7KgtXlsOhdmKhE+5JAz+BdInQGoXgAGP/iRvznlzfWL34OCEGx1C49P VXTOt6T/Typ7dQpcIEcWVCkL6T3GUHyUo/QUGGa2G8Zs5hRsKou4lYYD3K1VhdmW5jw1 U4iQ== X-Gm-Message-State: AOJu0Yy8P9VOZJSFmmO6XRhiFeIJmIi1mBHCZJ5padhPC2elNlwuBgo5 Aw1s3k+b3drXJL94QD5a+kZdXMgoMvQ29fUxK8U= X-Received: by 2002:a17:902:ba95:b0:1b7:f546:44d7 with SMTP id k21-20020a170902ba9500b001b7f54644d7mr7401608pls.17.1691426231284; Mon, 07 Aug 2023 09:37:11 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:10 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 05/14] linux-user: Use elf_et_dyn_base for ET_DYN with interpreter Date: Mon, 7 Aug 2023 09:36:56 -0700 Message-Id: <20230807163705.9848-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x636.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Follow the lead of the linux kernel in fs/binfmt_elf.c, in which an ET_DYN executable which uses an interpreter (usually a PIE executable) is loaded away from where the interpreter itself will be loaded. Tested-by: Helge Deller Reviewed-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson --- linux-user/elfload.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 1b4bb2d5af..d1b278d799 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3107,6 +3107,8 @@ static void load_elf_image(const char *image_name, int image_fd, } } + load_addr = loaddr; + if (pinterp_name != NULL) { /* * This is the main executable. @@ -3136,11 +3138,32 @@ static void load_elf_image(const char *image_name, int image_fd, */ probe_guest_base(image_name, loaddr, hiaddr); } else { + abi_ulong align; + /* * The binary is dynamic, but we still need to * select guest_base. In this case we pass a size. */ probe_guest_base(image_name, 0, hiaddr - loaddr); + + /* + * Avoid collision with the loader by providing a different + * default load address. + */ + load_addr += elf_et_dyn_base; + + /* + * TODO: Better support for mmap alignment is desirable. + * Since we do not have complete control over the guest + * address space, we prefer the kernel to choose some address + * rather than force the use of LOAD_ADDR via MAP_FIXED. + * But without MAP_FIXED we cannot guarantee alignment, + * only suggest it. + */ + align = pow2ceil(info->alignment); + if (align) { + load_addr &= -align; + } } } @@ -3155,13 +3178,13 @@ static void load_elf_image(const char *image_name, int image_fd, * * Otherwise this is ET_DYN, and we are searching for a location * that can hold the memory space required. If the image is - * pre-linked, LOADDR will be non-zero, and the kernel should + * pre-linked, LOAD_ADDR will be non-zero, and the kernel should * honor that address if it happens to be free. * * In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, + load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0), -1, 0); From patchwork Mon Aug 7 16:36:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711139 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127555ltf; Mon, 7 Aug 2023 09:38:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH70V7saoByPv1KFsFAoa2cYuWjeH97TKCpgQOewAKkDDMWNc/XUd26d+0rRm9BXdoOXySL X-Received: by 2002:a0c:f094:0:b0:63c:ea63:4717 with SMTP id g20-20020a0cf094000000b0063cea634717mr10088405qvk.43.1691426305730; Mon, 07 Aug 2023 09:38:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426305; cv=none; d=google.com; s=arc-20160816; b=KWxKnR+d9LHwxoC9Pu5MKhkVrJQb64sDCAmdnyGmbkw9Xeci0i/PPaVAjt4eQlql1/ NONXCY9SKgoiYPIebO583K+kMZu3XsR3NaxyAakvkGu0FS31eFg6SoLOCQMo0147uH1b Z/TBusgyjCZf3ukkLIg0TgtkCgbs4xV/G58/Daehqjwi+bQ1KO3ofoxVRDBTIrs5pFYy 1ZhiUVcl1fv4lu7hva1cqezhmM3t7TTmqls7COVt5YUsDlsYPszYo8s4DdyzcEb5CohK vKdvsStxp6f+6KwMg7kadXLlPitMPscEsFpWJDw+zPBnczsfMW1nO4XXXOVciHLj+S4y p++g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AMertvkjKANJvxbefrHCxJAv2awwG2qfsSApx0ejDkI=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=KmFmCEuc3ScsaJk/32AoqtdANoNpMCDw35cC3LA3O+sKEQvoU1VbX088M5ZdnKXT0a JK5PwX1uNpg0W7GbV/Gi40qjDpNDDiJlBTpcesNF41TeEzj79DvrcOmsxKFun2olziQw K8Apvj1w1GcwuFinG4TGtGkdVqiq/ii76g47Yc4zEI/ojCnLWLhgPghv2u4DpTmIpDPz LdmpS4k0+4cxkyOOru7isvVYWNdVAyPYnH0j0gpwTNtKcNTI2+ym/Toz7BAJioyawLUS QvzboZlfSHG7Sr4fkQuz3mh9nUx8w5tyY0BFy8qrzYvBIAKK7lAt2N1VFpXb6N3FycDi +wXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SN6+KaU1; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a23-20020a0cb357000000b0063d0981bed0si5093631qvf.406.2023.08.07.09.38.25 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:38:25 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SN6+KaU1; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EW-0007NE-Tc; Mon, 07 Aug 2023 12:37:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EJ-00073r-EL for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:18 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EH-0002h0-M6 for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:15 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1b9c5e07c1bso42277055ad.2 for ; Mon, 07 Aug 2023 09:37:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426232; x=1692031032; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AMertvkjKANJvxbefrHCxJAv2awwG2qfsSApx0ejDkI=; b=SN6+KaU16CQ7BBO0ZWGlSIhpncMDd+HHOAFPUgHasS1BYRV8UZc1hZW0aLtX3pnNk3 Nk1Ceun6lCpgq+L0Fi4WCbC6lo5aiGteW351gE8Hz6BJAinLdXJ7r6e4kpEn9cui21hq f/k5jfFVXDtQ5g3NYzYf/blpixECY4TWfIi8xWkBc1FGuZ+Uo+52rnzWEaBxa0A3Moj3 MzfOYHG59/BKcREWwbYWqV+h2rdna/Aq9MVwvZr8ahGJkRLO8E9AkKf9q5ZWR2K60ylK GL+yjVsX/nEiGH+VUrLFbs1mBwITF/W9UhJglxAxH7DsAZ4mhxAmGJNTvalGJpTJJuii wrdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426232; x=1692031032; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AMertvkjKANJvxbefrHCxJAv2awwG2qfsSApx0ejDkI=; b=e8Jcyx/XzklzVo0Fg+QakO4yu16qDKqWVXR+vIWVYBebFbVqK8AfdSBbVAVmau8uO6 Cz9PUeeK6honWjkvRe/wofa4hQgE2W7ml9m2slGZNsG+jZK81BupC/tZ5pjmcOoEV5Zr 2c18aDBe/MreaUHLFoiWt2GbRQ1mR7NL5z2qYIQ+bbJpI9bkxS4II2kjSzybYmptmNXG gGaDYksQayAULHDMzDcMJWNS0q8AB101eeYl1OLNcYziWR2BqalITjJ/PZbBwtR/qM4a 6NRRJoFCnaEyTm5yhnluKeavRQVWVYNgO3FqEhzdc2ALIz/+iwZEyrPDq9pl8lMOMnnS 2xaw== X-Gm-Message-State: AOJu0YwJ8eWu02UjES9Gp+/abbxFbXOGrpODfKOZZ3FmAUcdvFm2kj0k 8isTkih2Aw0ycTSY0czv83E8e7aXNqwSURkH4mU= X-Received: by 2002:a17:902:f68c:b0:1b3:d4d5:beb2 with SMTP id l12-20020a170902f68c00b001b3d4d5beb2mr12616858plg.9.1691426232309; Mon, 07 Aug 2023 09:37:12 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:11 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 06/14] linux-user: Adjust initial brk when interpreter is close to executable Date: Mon, 7 Aug 2023 09:36:57 -0700 Message-Id: <20230807163705.9848-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::629; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x629.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Helge Deller While we attempt to load a ET_DYN executable far away from TASK_UNMAPPED_BASE, we are not completely in control of the address space layout. If the interpreter lands close to the executable, leaving insufficient heap space, move brk. Tested-by: Helge Deller Signed-off-by: Helge Deller [rth: Re-order after ELF_ET_DYN_BASE patch so that we do not "temporarily break" tsan, and also to minimize the changes required. Remove image_info.reserve_brk as unused.] Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/qemu.h | 1 - linux-user/elfload.c | 51 +++++++++++++------------------------------- 2 files changed, 15 insertions(+), 37 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 2046a23037..4f8b55e2fb 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -30,7 +30,6 @@ struct image_info { abi_ulong start_data; abi_ulong end_data; abi_ulong brk; - abi_ulong reserve_brk; abi_ulong start_mmap; abi_ulong start_stack; abi_ulong stack_limit; diff --git a/linux-user/elfload.c b/linux-user/elfload.c index d1b278d799..3553a3eaef 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3110,27 +3110,6 @@ static void load_elf_image(const char *image_name, int image_fd, load_addr = loaddr; if (pinterp_name != NULL) { - /* - * This is the main executable. - * - * Reserve extra space for brk. - * We hold on to this space while placing the interpreter - * and the stack, lest they be placed immediately after - * the data segment and block allocation from the brk. - * - * 16MB is chosen as "large enough" without being so large as - * to allow the result to not fit with a 32-bit guest on a - * 32-bit host. However some 64 bit guests (e.g. s390x) - * attempt to place their heap further ahead and currently - * nothing stops them smashing into QEMUs address space. - */ -#if TARGET_LONG_BITS == 64 - info->reserve_brk = 32 * MiB; -#else - info->reserve_brk = 16 * MiB; -#endif - hiaddr += info->reserve_brk; - if (ehdr->e_type == ET_EXEC) { /* * Make sure that the low address does not conflict with @@ -3221,7 +3200,8 @@ static void load_elf_image(const char *image_name, int image_fd, info->end_code = 0; info->start_data = -1; info->end_data = 0; - info->brk = 0; + /* Usual start for brk is after all sections of the main executable. */ + info->brk = TARGET_PAGE_ALIGN(hiaddr); info->elf_flags = ehdr->e_flags; prot_exec = PROT_EXEC; @@ -3315,9 +3295,6 @@ static void load_elf_image(const char *image_name, int image_fd, info->end_data = vaddr_ef; } } - if (vaddr_em > info->brk) { - info->brk = vaddr_em; - } #ifdef TARGET_MIPS } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { Mips_elf_abiflags_v0 abiflags; @@ -3646,6 +3623,19 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) if (elf_interpreter) { load_elf_interp(elf_interpreter, &interp_info, bprm->buf); + /* + * While unusual because of ELF_ET_DYN_BASE, if we are unlucky + * with the mappings the interpreter can be loaded above but + * near the main executable, which can leave very little room + * for the heap. + * If the current brk has less than 16MB, use the end of the + * interpreter. + */ + if (interp_info.brk > info->brk && + interp_info.load_bias - info->brk < 16 * MiB) { + info->brk = interp_info.brk; + } + /* If the program interpreter is one of these two, then assume an iBCS2 image. Otherwise assume a native linux image. */ @@ -3699,17 +3689,6 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) bprm->core_dump = &elf_core_dump; #endif - /* - * If we reserved extra space for brk, release it now. - * The implementation of do_brk in syscalls.c expects to be able - * to mmap pages in this space. - */ - if (info->reserve_brk) { - abi_ulong start_brk = TARGET_PAGE_ALIGN(info->brk); - abi_ulong end_brk = TARGET_PAGE_ALIGN(info->brk + info->reserve_brk); - target_munmap(start_brk, end_brk - start_brk); - } - return 0; } From patchwork Mon Aug 7 16:36:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711145 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2129288ltf; Mon, 7 Aug 2023 09:41:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcYJmSRrcfkySdWnW+eTfS+JGIe352x0NehkysC91ZH7w9HUReJvyHwakOXR+EzQqyKL09 X-Received: by 2002:a05:620a:2944:b0:767:81e5:566 with SMTP id n4-20020a05620a294400b0076781e50566mr14585487qkp.58.1691426513684; Mon, 07 Aug 2023 09:41:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426513; cv=none; d=google.com; s=arc-20160816; b=J/VoV5g1PZk0n1MQ7CU6sbocQAbfPAK/zg3JxhlZzd8QXbd+Eb96ZgeOVDPD/TGt5M TVKLfpnvfEeeqalP5lXKLuTUfskj2kt8F9JA1zhQPEgfjOSWlxH/LSyYbl2os/R2qniq htQhRJToAI7jy+ilU+3E7VtyXSlpBD5UBdUjIhJ6r95wz7emAhiO+LgJS12rK7ghykhr 6pz/dIi68VwN6zOUFVEhetVatu5/MtPDyY+KWVD4R1QPWS6QxydA2AxyW53ozwix1ECx USiVDLK+bd3Bb2qFXizDMg9sb39CK/DE8qlR39/2J/8XpGWyC6i7Y56d6KqgaJrEIDQD uahA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TbCJ8DmT72t6xcy/KS1lARRJzdw3e4AQqay5Lza2sjc=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=pfYae1lEtjylUuzv34mSe9c2F42VhVOS5/zi0/X8DTuKGABqU/cpsbYKP3xLXvka31 lYRBvebQ69zjuo+JwiZVwmJitQqpRnDUJ/KsSw6pjbJh3cjl7Iu34/pBpmfAtF6HXtZr 8a+EmIWn71y2lKt4SLa2rOdj6pvHwam2NFttUu3oefswPkyfr+rThLLZM7V7tEbuES8x XwZcpBmg0So1w9QwJYrRINFak5soESv90z1vina5+BMBi9MFBC+/sCA2fey/3qvH4YEM j1e4igXTYYYW+1c+R44CLbkBaba8GC8fk9Y7p6kZMIkUMgRiMQ+q27Mk0MwpMVuchPHn kzDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tca7KPmt; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id h4-20020a05620a284400b0075cb23ee1fesi4984924qkp.602.2023.08.07.09.41.53 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:41:53 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tca7KPmt; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EX-0007QH-JE; Mon, 07 Aug 2023 12:37:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EK-00074K-CN for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:18 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EI-0002hG-D1 for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:16 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1b8b4748fe4so30496535ad.1 for ; Mon, 07 Aug 2023 09:37:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426233; x=1692031033; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TbCJ8DmT72t6xcy/KS1lARRJzdw3e4AQqay5Lza2sjc=; b=tca7KPmtrOwNCoWQHPjzXFDIPcIDJNknAvtwAU2lIB3GfE1NbcvJsoSh3xriaKk/bq WkOPfaj+Btt6yVRQySwCYdFTpFNPnwqyXuHjJEgyja9Lh6ZrNZ8PUutHUojn/FS+1ua8 2JJbjHkvCmDeaWSIFNgUaOejCeI+VXCLuKQTfU+zyWtzKGaigXQWLLsh5OH7iGhXnlJX oH3erq0OvGlRiQYwJg6n/52Dc6j3p4eWR2G37YHIr20x3hmMo+RFc6M/cw8JzKEmipFF lZnG3TTm41NGPi3rJKU+gIPk2euHmooBl6FH/1q5fC89CSN4b/wsBiL1k8t7N1lEY9cM hyTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426233; x=1692031033; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TbCJ8DmT72t6xcy/KS1lARRJzdw3e4AQqay5Lza2sjc=; b=Aea611AQz03DzcRTYx+YNCQj7MSd9/myquuUGAkOMZgXEREaasZq1AODfUoF4xFndd VOSoKMDwqzBjbAXZ0jZoYJhLP+4Z27A2ORpF3htmvpoqwaeWYFTdP5Z2zreiyuus6V9K 3OU7J0HFoXhQoT33Va+koKTB2XCrhjEapfGe8M3znfy/KWODTcbqxfYOTE7YPE7D06tU KPoC5ZZYNPMMLQxxAC56AUSZ8QOpL+Ifbe09SiKlY4TA6g+bfJKOkkOf13JmQ7KPI7IE +dZb9G76A6OA7jRuloZ0qp5/ZoGkXvqjwv5+MFYM8eHV+ORPOfd/zWCw/lgFnSDwNpoE knHA== X-Gm-Message-State: AOJu0YwoQIkSjuRk7mhyHgP3xB96TgnMNI4dD59eoI49ZcZu+QTFz6Qk qcMU3YBh4nmGCUAjZiK+E5Z6bDRYlUXOrmWfQWs= X-Received: by 2002:a17:902:cec1:b0:1bc:6a89:86ba with SMTP id d1-20020a170902cec100b001bc6a8986bamr4338259plg.16.1691426233129; Mon, 07 Aug 2023 09:37:13 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:12 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 07/14] linux-user: Do not adjust image mapping for host page size Date: Mon, 7 Aug 2023 09:36:58 -0700 Message-Id: <20230807163705.9848-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x630.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Remove TARGET_ELF_EXEC_PAGESIZE, and 3 other TARGET_ELF_PAGE* macros based off of that. Rely on target_mmap to handle guest vs host page size mismatch. Tested-by: Helge Deller Reviewed-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 3553a3eaef..964b21f997 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1960,15 +1960,6 @@ struct exec #define ZMAGIC 0413 #define QMAGIC 0314 -/* Necessary parameters */ -#define TARGET_ELF_EXEC_PAGESIZE \ - (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \ - TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE)) -#define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE) -#define TARGET_ELF_PAGESTART(_v) ((_v) & \ - ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1)) -#define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1)) - #define DLINFO_ITEMS 16 static inline void memcpy_fromfs(void * to, const void * from, unsigned long n) @@ -3241,8 +3232,8 @@ static void load_elf_image(const char *image_name, int image_fd, } vaddr = load_bias + eppnt->p_vaddr; - vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr); - vaddr_ps = TARGET_ELF_PAGESTART(vaddr); + vaddr_po = vaddr & ~TARGET_PAGE_MASK; + vaddr_ps = vaddr & TARGET_PAGE_MASK; vaddr_ef = vaddr + eppnt->p_filesz; vaddr_em = vaddr + eppnt->p_memsz; @@ -3252,7 +3243,7 @@ static void load_elf_image(const char *image_name, int image_fd, * but no backing file segment. */ if (eppnt->p_filesz != 0) { - vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po); + vaddr_len = eppnt->p_filesz + vaddr_po; error = target_mmap(vaddr_ps, vaddr_len, elf_prot, MAP_PRIVATE | MAP_FIXED, image_fd, eppnt->p_offset - vaddr_po); @@ -3268,7 +3259,7 @@ static void load_elf_image(const char *image_name, int image_fd, zero_bss(vaddr_ef, vaddr_em, elf_prot); } } else if (eppnt->p_memsz != 0) { - vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_memsz + vaddr_po); + vaddr_len = eppnt->p_memsz + vaddr_po; error = target_mmap(vaddr_ps, vaddr_len, elf_prot, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0); From patchwork Mon Aug 7 16:36:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711147 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2130457ltf; Mon, 7 Aug 2023 09:44:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHgvnCrNoVewllPJz5m6GzdY/IEYkVSfJIFu0IxsOoxKRzLyxOn4qiiEnV4eiQeHlGQsfJ3 X-Received: by 2002:a05:620a:4407:b0:76c:5952:7317 with SMTP id v7-20020a05620a440700b0076c59527317mr11400615qkp.3.1691426512385; Mon, 07 Aug 2023 09:41:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426512; cv=none; d=google.com; s=arc-20160816; b=s8EpOF039ES9pCeWlWaOXaS4xV1QGs7Bf4WKueQxyIZM6Em83D85umKUrWGC7+3zZ8 9sM1JwWNprrAgR6QkfLxFCQ8Qqapue9NoNVxPKvJugf9OARSv9Ba8tdiaszNYyCA8ds8 4mUQIGHckini9upfcG33/z4oJY8fs5HJ2Q54G6352+NdRI4b2JdKEuDaebCuo2tVzzzd C4U3e/XxhVBrm5UXmyJE6mG6zYbCSsiCrZ7oIZ1dWW5Kx+F0dNffsHQGl2aOVncBvHKB 1uM/3osyaQ166rJ+DLTupfIBJGyaVc1JR8om4gv5KspM4mHQqfcl1eDJ50YXjgBdOMJ1 eJuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=H3Tim9UDK/YyEXiyy7lP2/6WWteP8ml5X65zqKKrj48=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=KCWwEtbPbcvxNbA2Q0/S3WdC7SxyKVEHEBa8bfko03mL7llrC3pTcHY/1c5u/B+VXP i/5L31AErMhGIZZU+K9uQwBjDXUYfNwGINMBeBghS6W/1iy4aR/04+jmWXxR12Y8qhqn lawpcQnoXr4D92eE4vPLOA3wjMEjiisO7KoV9jRD4v/UQTRylB09hFbVifB+an67oKDP Og2hHuwHljI/05fujRbFZntLiCoJfCEh/QtMBYhZKi2xO6LRtitkAVyVX3Mk+7EWcah/ GegPi7OMXj2vKzSUamvhuMT1mZe8kJLPf/t2+2MwSPngV2BsW+6+8tvA4vBfcO9/BjUU 9jDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KqZHxkLH; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id p11-20020a05622a048b00b00404228e80a0si5155208qtx.465.2023.08.07.09.41.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:41:52 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KqZHxkLH; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EW-0007MR-Nf; Mon, 07 Aug 2023 12:37:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EK-00075f-QV for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:18 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EJ-0002hT-5B for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:16 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1bc63ef9959so15798455ad.2 for ; Mon, 07 Aug 2023 09:37:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426234; x=1692031034; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=H3Tim9UDK/YyEXiyy7lP2/6WWteP8ml5X65zqKKrj48=; b=KqZHxkLHdIPHZ19qpKR9NfMlver6K4bSX9dOxh0jkZ+p+lZxHOZh21T/H0sQV2r/Ly zPBeydIGVId82Ie81xOjcpqM+xqajc2dsEFcJHNDGu4vOo2AD8wtSZ5wDeJ/19ZQaWHy 0Qtz+jHGiF8ARfBHMb06nz6JNKTuiFQDWv2CZny9RZBMavAFfJOQLHj3Ptm5N1TGQHBu lmAJEGgob4/Qg4Jk7isxAw2tYrS0nCAQFQA4JcAhulorzXR32SRmc2Ca5Lro30e+hyoz 7Y0CAHn2OfxZ0heKjQGMrPrUOXECdiJnHO+3qKTuQ91p8cgcq+qeFkasjfRPwsWZCHcS vCZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426234; x=1692031034; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H3Tim9UDK/YyEXiyy7lP2/6WWteP8ml5X65zqKKrj48=; b=brg7W2j8XVLEtb/kKxH3wi4cBquRpYSPUGXFb82C9yzB5CxmVbSOtub6JMfCV2J18e 7Mboa+AtIvRnrky55MQGbhwjHp7we5QEVUsFDRx8TaTVaAgg/MR9rl4LLnX5KAaOEwks vLjnPA3iYJTz57vdupCscpQ3ovsk/0gzIunnFczuMJxkN6OzwUiSymbElE7/OtEURr2C 1NcPlW5KEXc2NPBOVmSdsHA3mkEG9l/Uc8YxRaH34l/hk84j/YWExp5gHINq0mngSQOW WHGDhLwhxPWJ0z9dFk7lJ2kyh51fxPz0Ko49dh+Bu8WrTTljoRho4mhlPQJTrBuYwjCv VLKA== X-Gm-Message-State: AOJu0YxgbEbgMYg15R12eP5OOC+mm1HAUV71oidMJY0h1mXvli2VAsuJ mkl4T/L5ULvQb6fCwsdae5ox3fKTOzrf2frDZM4= X-Received: by 2002:a17:903:1ce:b0:1b8:88c5:2d2f with SMTP id e14-20020a17090301ce00b001b888c52d2fmr12125907plh.64.1691426233899; Mon, 07 Aug 2023 09:37:13 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:13 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 08/14] linux-user: Do not adjust zero_bss for host page size Date: Mon, 7 Aug 2023 09:36:59 -0700 Message-Id: <20230807163705.9848-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Rely on target_mmap to handle guest vs host page size mismatch. Tested-by: Helge Deller Reviewed-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson --- linux-user/elfload.c | 54 +++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 964b21f997..6c28cb70ef 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2213,44 +2213,36 @@ static abi_ulong setup_arg_pages(struct linux_binprm *bprm, /* Map and zero the bss. We need to explicitly zero any fractional pages after the data section (i.e. bss). */ -static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot) +static void zero_bss(abi_ulong start_bss, abi_ulong end_bss, int prot) { - uintptr_t host_start, host_map_start, host_end; + abi_ulong align_bss; - last_bss = TARGET_PAGE_ALIGN(last_bss); + align_bss = TARGET_PAGE_ALIGN(start_bss); + end_bss = TARGET_PAGE_ALIGN(end_bss); - /* ??? There is confusion between qemu_real_host_page_size and - qemu_host_page_size here and elsewhere in target_mmap, which - may lead to the end of the data section mapping from the file - not being mapped. At least there was an explicit test and - comment for that here, suggesting that "the file size must - be known". The comment probably pre-dates the introduction - of the fstat system call in target_mmap which does in fact - find out the size. What isn't clear is if the workaround - here is still actually needed. For now, continue with it, - but merge it with the "normal" mmap that would allocate the bss. */ + if (start_bss < align_bss) { + int flags = page_get_flags(start_bss); - host_start = (uintptr_t) g2h_untagged(elf_bss); - host_end = (uintptr_t) g2h_untagged(last_bss); - host_map_start = REAL_HOST_PAGE_ALIGN(host_start); - - if (host_map_start < host_end) { - void *p = mmap((void *)host_map_start, host_end - host_map_start, - prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (p == MAP_FAILED) { - perror("cannot mmap brk"); - exit(-1); + if (!(flags & PAGE_VALID)) { + /* Map the start of the bss. */ + align_bss -= TARGET_PAGE_SIZE; + } else if (flags & PAGE_WRITE) { + /* The page is already mapped writable. */ + memset(g2h_untagged(start_bss), 0, align_bss - start_bss); + } else { + /* Read-only zeros? */ + g_assert_not_reached(); } } - /* Ensure that the bss page(s) are valid */ - if ((page_get_flags(last_bss-1) & prot) != prot) { - page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss - 1, - prot | PAGE_VALID); - } - - if (host_start < host_map_start) { - memset((void *)host_start, 0, host_map_start - host_start); + if (align_bss < end_bss) { + abi_long err = target_mmap(align_bss, end_bss - align_bss, prot, + MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, + -1, 0); + if (err == -1) { + perror("cannot mmap brk"); + exit(-1); + } } } From patchwork Mon Aug 7 16:37:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711136 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127244ltf; Mon, 7 Aug 2023 09:37:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEYwjdgqZzRNSliJBGkLforMUr98rIySoWqJ8o3y/xgUHedqMAPqMbzhw0/4G+CY12nFtt0 X-Received: by 2002:a05:620a:3727:b0:76c:a961:5ea5 with SMTP id de39-20020a05620a372700b0076ca9615ea5mr12852313qkb.70.1691426268967; Mon, 07 Aug 2023 09:37:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426268; cv=none; d=google.com; s=arc-20160816; b=H3vwen6qAYqeFSq63yGFrMQNrkqn09rLTqWZDY1aXQ39oXfEwDygpubj7jgvu4yXpc Zd3gGlzCY+YHqP42vKLaU3OVZZDtVbM0PtMq65hK0CahX+1ZPfpbqPJOXML/0UrdeuWX qKjvhB+xdPX3r3T7xJk6tKRVKQVWDiP9/f45FWyHQ/M2kP3dvPrClFCl43DZHDaGLylw ABMY3Xz5DbkcIONPG3L2FNSLfBlH8bvlFC6hz9g0EiNkHF8LbrTM39C+YTkVorBoGUNP CrVd21EzWXyQXf80l9mYhQsvHxNCW6ugHQ9uwOhjpL5/8zsI91p8+mf2dqmsq9PU/6xY 6EzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Y+mZZ88pcdw893U+hesCVPS8DRB2GmOsmcmXPqFY2jE=; fh=eYV+9QhSqCUYQJvtNbbWH67WkoSCLPz+dOVCBJL5cVY=; b=mnbQAFXxBiHtYxZ+Ew6r1lixmlFWxUPoBmCHv05GYyNcwaOsybBf7m3Hdt3cNU2p+1 bAtQQ3eMoQfjVJ/t98azrJyP3HSJtT8PBTmOD9VQk5T6qiywd+zaUmIXvZLLRYezuzMm wXKfJwTKAmRp0toItmb3Q/ep7eV4UHcWXy5qvnuDNZ4IxdJgJRa93WJackxEUA2F9Fcu 3K2qXYw0neXgA9RShVYJLhnqO+aJsHiZhjTLD2inN7BNl/nNZ75HZWMKTAgnNpw0bKEt Tv2ZGJd5uYSVh+jFw/sSOX26QtUrdACYsI2D7AknI1NXhqOEa7rUCnaw8W1KPDGHZ/FU qx3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LAdWI4Bv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id f10-20020a05620a280a00b00767eff18f66si5186605qkp.553.2023.08.07.09.37.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:37:48 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LAdWI4Bv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EY-0007VZ-Th; Mon, 07 Aug 2023 12:37:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EL-00075k-DQ for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:19 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EJ-0002hc-To for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:17 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1bc7b25c699so2821165ad.1 for ; Mon, 07 Aug 2023 09:37:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426234; x=1692031034; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y+mZZ88pcdw893U+hesCVPS8DRB2GmOsmcmXPqFY2jE=; b=LAdWI4BvRcfFZcUbI/FkufRUEHGCXJICTh669vZNEvlcp8YaUs+QwEc0FtOHOT2p9i 8qqrGQNRxPMUatTKC4w4JlAH1sLZ1UT8OtyVNrTxud8LV46HnSpTLdeBMrwS7XQKMjsu XUJVakuTSQvXWP29UlV0y1d3fnIhUXDU6sCfclDj+csfjECF751ptunnSF1gfsXm3HOF 2huBl03YBef99FOLItStXg6ggqHuxJhFXuVWlIqP0uYNau6rT9wD6yQeEoc8OPBcq2XE b/K58hsmjfYMpLLU710lw9bA9C0lalcvJKy+h+x0jD1gTXBO/Gp3yljJ+kfMyxsnsWJ9 lHOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426234; x=1692031034; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y+mZZ88pcdw893U+hesCVPS8DRB2GmOsmcmXPqFY2jE=; b=epK4qxjItBk81ZkZ06ywzfA8tIPbyF34d6frawPL+5mk3mImApJOiHNjlKY0RVHeP4 pYCt0JfcAdFXWTwmQkvU5H7hLsmGTaTbD+xoBWfh4GKyc7RmhrIl5C60lgZUFZUafuwT 6i7cB/FdUypruYqJxatFo/dvoPIyeurOumSkf4ldKeE43vBS9nUFtBhiBsBBobYKgycG Gwp/oLMswUN4+qudy254LIGBpj6vvgRzCHHfosEZvCrgn8TvgQQUMLpyg4FNVsqQXYcL VN+mukBPm+X6CtnG87HmH3shnWoCVhvNcZ3IczQAXbE3weNyINWg4kEVtT8UkeiSXHP5 3NPA== X-Gm-Message-State: AOJu0YzOzRHsL0jegGwV3iYK0mcsQTEP7E3rKkAvZOJRR+BT4yO4Fcc4 407kkEdBgfHGuLB1YMVKCpvw3rr6xpaRXuJAUVw= X-Received: by 2002:a17:902:db0c:b0:1bc:7441:d81a with SMTP id m12-20020a170902db0c00b001bc7441d81amr1924036plx.6.1691426234670; Mon, 07 Aug 2023 09:37:14 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:14 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de, Akihiko Odaki Subject: [PATCH for-8.1 v10 09/14] linux-user: Use zero_bss for PT_LOAD with no file contents too Date: Mon, 7 Aug 2023 09:37:00 -0700 Message-Id: <20230807163705.9848-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org If p_filesz == 0, then vaddr_ef == vaddr. We can reuse the code in zero_bss rather than incompletely duplicating it in load_elf_image. Tested-by: Helge Deller Reviewed-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 6c28cb70ef..c9e176a9f6 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3210,7 +3210,7 @@ static void load_elf_image(const char *image_name, int image_fd, for (i = 0; i < ehdr->e_phnum; i++) { struct elf_phdr *eppnt = phdr + i; if (eppnt->p_type == PT_LOAD) { - abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len; + abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em; int elf_prot = 0; if (eppnt->p_flags & PF_R) { @@ -3235,30 +3235,17 @@ static void load_elf_image(const char *image_name, int image_fd, * but no backing file segment. */ if (eppnt->p_filesz != 0) { - vaddr_len = eppnt->p_filesz + vaddr_po; - error = target_mmap(vaddr_ps, vaddr_len, elf_prot, - MAP_PRIVATE | MAP_FIXED, + error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, + elf_prot, MAP_PRIVATE | MAP_FIXED, image_fd, eppnt->p_offset - vaddr_po); - if (error == -1) { goto exit_mmap; } + } - /* - * If the load segment requests extra zeros (e.g. bss), map it. - */ - if (eppnt->p_filesz < eppnt->p_memsz) { - zero_bss(vaddr_ef, vaddr_em, elf_prot); - } - } else if (eppnt->p_memsz != 0) { - vaddr_len = eppnt->p_memsz + vaddr_po; - error = target_mmap(vaddr_ps, vaddr_len, elf_prot, - MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, - -1, 0); - - if (error == -1) { - goto exit_mmap; - } + /* If the load segment requests extra zeros (e.g. bss), map it. */ + if (vaddr_ef < vaddr_em) { + zero_bss(vaddr_ef, vaddr_em, elf_prot); } /* Find the full program boundaries. */ From patchwork Mon Aug 7 16:37:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711137 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127491ltf; Mon, 7 Aug 2023 09:38:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFivvIhq4+c142gnC0bqd48qwwcQs1iYw7qW06OpA7w9dayZBQkTJs+iyVldWgijKIJD2Gw X-Received: by 2002:ac8:5f10:0:b0:40f:d69c:ff53 with SMTP id x16-20020ac85f10000000b0040fd69cff53mr11245271qta.37.1691426299546; Mon, 07 Aug 2023 09:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426299; cv=none; d=google.com; s=arc-20160816; b=wS1OFfX7BIvvwkeOHslN47jyVRsTkiT+NgsUaC9+tpsIJTfymcywPkTnKiPv9fBWPl cnj1bfW6GuTXBH0jnkCIC6Q6L9OZURnAjIT72n2XO8UMOgmYBCtVjzuHtv5NYIa/Q9ya zGAssER83gJlZnDDY0O8ZIaj4pKe5pw5E46uSzb8sLUrn0HjoEpTklezf8Mc7tCKFmpU xNQ0wbk0e9xeNVBtsKnbmsLC8KDih55cr1kQmB8n6yy0C9StARJhXRLN68niW+MGyYiq ofCJsa9huUXLED8FVpAMrVRasWtYTGMbbNIg6MkxSgOXaawrKeVrH/Jx1b9c+ZLTeLth ylrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WIhYglDsXCnrxRsjPebomm6uEmgGkuEHTNX79IYUpQ8=; fh=nM8F09xKNAze8fdJGCDZoH2K0rjOXqNih6JoyRf4NS8=; b=XJp1L1q81JRaeXNx8entj32lQ7l3OustLcrW6eF18j39CRdTO5EF7Fs82TaAQtVZFF SZ7PQ8dHlxrOA4YOqcSPLch6KcUUla9k4JRqoBwOtp798L6FZ6IrO/BLrpkD/lLTzvs1 n+Ijv+pXpa6ki0nDrJ/SlX6SvKoFtAKSIhEwscvp8llIfu1W6rGAjNbCWYaHmeyceTVH EWH7jmvkIaSLdeYP2JKIyPyNYwegM5CDTDg/Bjf/B8T0eyAJ+6tk1+FrBtgmp5OI0cnE nt1ZOxST8qmDSfiroJE9FE1OuFukfzC5rRTi76MX3tIPYPG2eIJPHGXF0ZX+xIXNvmov u3UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CFSdv7OT; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id g13-20020ac87f4d000000b0040fdcdaea19si5080074qtk.521.2023.08.07.09.38.19 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:38:19 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CFSdv7OT; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EV-0007GK-Jx; Mon, 07 Aug 2023 12:37:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EM-00075n-R0 for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:19 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EK-0002hn-Te for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:18 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1bc3d94d40fso41463835ad.3 for ; Mon, 07 Aug 2023 09:37:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426235; x=1692031035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WIhYglDsXCnrxRsjPebomm6uEmgGkuEHTNX79IYUpQ8=; b=CFSdv7OTnz6coMmJysSXHW+L9LcNUwG2eheyvoGpLwP075NWHsocWBP+TxlPNmRfND fH2OfoF8A69NuALMyoJpysW0ihECO1W9r4TEsqR4qjDDHEgdGvZNfJdqCImnoqmClzKU +agwVrCBcO8SkVp8f/QQUNDMUvB8ZAVoXoAyBcJRXHdIdk4ezLxDbsRWLjpOwZUefSTL E9xLD+x14EOI87/TQqqhCXQqxNsTjcKiNES2o0Y/BrWX56IcIS0QmAg8EM/HC2Bc+Luy Cz0jnerKquRW/7iGd8lXOTrjGNgr5GXEfzT4TfgMfcghR4rQHqUqJzB5ShCxWRFO319U 5wkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426235; x=1692031035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WIhYglDsXCnrxRsjPebomm6uEmgGkuEHTNX79IYUpQ8=; b=JM4h7FSONMYLbUhw4G91BHBgNrllEhFVj0LEz5meVYeb1kRK7yQpMBAKr1U6mgGMZP 4/NY70/RS5jJ9Pv/hH7G3p7MR8JJ2vmBh5i1QhTtPx9EWWOR+snEsOBHU+uLtRS/B4qB spX4ZrbV5nW6H5oD5bCNhurQDd8CqsaPHd+42lxo41gHo9ppt6EXmBMZiMhEZj54pAI8 ZddPbQYVEln2zEo9VyjLbWCYjWLsoktn6aBUrwfzXj67zbIRYGv5UR2naJCunSubj/H2 WuDarzhI+wchgN0tPwdv216E63QsdCTCRjucR9iteuDPWRH3cTDdDcDuHcTYzjPTfOLI uxrw== X-Gm-Message-State: AOJu0Yy0uQTuyFmK53YY8cr5KfZUGNhd3LQ5Z6frjnyqKndpkzM0bxnI YnqU6qytgcbnu772mi4pDfZCKmOHjKg15knqGWY= X-Received: by 2002:a17:902:f545:b0:1bc:c82:9368 with SMTP id h5-20020a170902f54500b001bc0c829368mr11947086plf.32.1691426235629; Mon, 07 Aug 2023 09:37:15 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:15 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de Subject: [PATCH for-8.1 v10 10/14] util/selfmap: Rewrite using qemu/interval-tree.h Date: Mon, 7 Aug 2023 09:37:01 -0700 Message-Id: <20230807163705.9848-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We will want to be able to search the set of mappings. For this patch, the two users iterate the tree in order. Signed-off-by: Richard Henderson Reviewed-by: Richard Henderson --- include/qemu/selfmap.h | 20 ++++---- linux-user/elfload.c | 14 +++-- linux-user/syscall.c | 15 +++--- util/selfmap.c | 114 +++++++++++++++++++++++++---------------- 4 files changed, 96 insertions(+), 67 deletions(-) diff --git a/include/qemu/selfmap.h b/include/qemu/selfmap.h index 3479a2a618..aacd6ae0a0 100644 --- a/include/qemu/selfmap.h +++ b/include/qemu/selfmap.h @@ -9,9 +9,10 @@ #ifndef SELFMAP_H #define SELFMAP_H +#include "qemu/interval-tree.h" + typedef struct { - unsigned long start; - unsigned long end; + IntervalTreeNode itree; /* flags */ bool is_read; @@ -19,26 +20,25 @@ typedef struct { bool is_exec; bool is_priv; - unsigned long offset; - gchar *dev; + uint64_t offset; uint64_t inode; - gchar *path; + const char *path; + char dev[]; } MapInfo; - /** * read_self_maps: * * Read /proc/self/maps and return a list of MapInfo structures. */ -GSList *read_self_maps(void); +IntervalTreeRoot *read_self_maps(void); /** * free_self_maps: - * @info: a GSlist + * @info: an interval tree * - * Free a list of MapInfo structures. + * Free a tree of MapInfo structures. */ -void free_self_maps(GSList *info); +void free_self_maps(IntervalTreeRoot *root); #endif /* SELFMAP_H */ diff --git a/linux-user/elfload.c b/linux-user/elfload.c index c9e176a9f6..f497286abe 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2622,7 +2622,8 @@ static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size, long align, uintptr_t offset) { - GSList *maps, *iter; + IntervalTreeRoot *maps; + IntervalTreeNode *iter; uintptr_t this_start, this_end, next_start, brk; intptr_t ret = -1; @@ -2640,12 +2641,15 @@ static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size, /* The first hole is before the first map entry. */ this_start = mmap_min_addr; - for (iter = maps; iter; - this_start = next_start, iter = g_slist_next(iter)) { + for (iter = interval_tree_iter_first(maps, 0, -1); + iter; + this_start = next_start, + iter = interval_tree_iter_next(iter, 0, -1)) { + MapInfo *info = container_of(iter, MapInfo, itree); uintptr_t align_start, hole_size; - this_end = ((MapInfo *)iter->data)->start; - next_start = ((MapInfo *)iter->data)->end; + this_end = info->itree.start; + next_start = info->itree.last + 1; align_start = ROUND_UP(this_start + offset, align); /* Skip holes that are too small. */ diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 7c2c2f6e2f..a15bce2be2 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8070,16 +8070,17 @@ static int open_self_maps_1(CPUArchState *cpu_env, int fd, bool smaps) { CPUState *cpu = env_cpu(cpu_env); TaskState *ts = cpu->opaque; - GSList *map_info = read_self_maps(); - GSList *s; + IntervalTreeRoot *map_info = read_self_maps(); + IntervalTreeNode *s; int count; - for (s = map_info; s; s = g_slist_next(s)) { - MapInfo *e = (MapInfo *) s->data; + for (s = interval_tree_iter_first(map_info, 0, -1); s; + s = interval_tree_iter_next(s, 0, -1)) { + MapInfo *e = container_of(s, MapInfo, itree); - if (h2g_valid(e->start)) { - unsigned long min = e->start; - unsigned long max = e->end; + if (h2g_valid(e->itree.start)) { + unsigned long min = e->itree.start; + unsigned long max = e->itree.last + 1; int flags = page_get_flags(h2g(min)); const char *path; diff --git a/util/selfmap.c b/util/selfmap.c index 2c14f019ce..4db5b42651 100644 --- a/util/selfmap.c +++ b/util/selfmap.c @@ -10,74 +10,98 @@ #include "qemu/cutils.h" #include "qemu/selfmap.h" -GSList *read_self_maps(void) +IntervalTreeRoot *read_self_maps(void) { - gchar *maps; - GSList *map_info = NULL; + IntervalTreeRoot *root; + gchar *maps, **lines; + guint i, nlines; - if (g_file_get_contents("/proc/self/maps", &maps, NULL, NULL)) { - gchar **lines = g_strsplit(maps, "\n", 0); - int i, entries = g_strv_length(lines); + if (!g_file_get_contents("/proc/self/maps", &maps, NULL, NULL)) { + return NULL; + } - for (i = 0; i < entries; i++) { - gchar **fields = g_strsplit(lines[i], " ", 6); - if (g_strv_length(fields) > 4) { - MapInfo *e = g_new0(MapInfo, 1); - int errors = 0; - const char *end; + root = g_new0(IntervalTreeRoot, 1); + lines = g_strsplit(maps, "\n", 0); + nlines = g_strv_length(lines); - errors |= qemu_strtoul(fields[0], &end, 16, &e->start); - errors |= qemu_strtoul(end + 1, NULL, 16, &e->end); + for (i = 0; i < nlines; i++) { + gchar **fields = g_strsplit(lines[i], " ", 6); + guint nfields = g_strv_length(fields); + + if (nfields > 4) { + uint64_t start, end, offset, inode; + int errors = 0; + const char *p; + + errors |= qemu_strtou64(fields[0], &p, 16, &start); + errors |= qemu_strtou64(p + 1, NULL, 16, &end); + errors |= qemu_strtou64(fields[2], NULL, 16, &offset); + errors |= qemu_strtou64(fields[4], NULL, 10, &inode); + + if (!errors) { + size_t dev_len, path_len; + MapInfo *e; + + dev_len = strlen(fields[3]) + 1; + if (nfields == 6) { + p = fields[5]; + p += strspn(p, " "); + path_len = strlen(p) + 1; + } else { + p = NULL; + path_len = 0; + } + + e = g_malloc0(sizeof(*e) + dev_len + path_len); + + e->itree.start = start; + e->itree.last = end - 1; + e->offset = offset; + e->inode = inode; e->is_read = fields[1][0] == 'r'; e->is_write = fields[1][1] == 'w'; e->is_exec = fields[1][2] == 'x'; e->is_priv = fields[1][3] == 'p'; - errors |= qemu_strtoul(fields[2], NULL, 16, &e->offset); - e->dev = g_strdup(fields[3]); - errors |= qemu_strtou64(fields[4], NULL, 10, &e->inode); - - if (!errors) { - /* - * The last field may have leading spaces which we - * need to strip. - */ - if (g_strv_length(fields) == 6) { - e->path = g_strdup(g_strchug(fields[5])); - } - map_info = g_slist_prepend(map_info, e); - } else { - g_free(e->dev); - g_free(e); + memcpy(e->dev, fields[3], dev_len); + if (path_len) { + e->path = memcpy(e->dev + dev_len, p, path_len); } + + interval_tree_insert(&e->itree, root); } - - g_strfreev(fields); } - g_strfreev(lines); - g_free(maps); + g_strfreev(fields); } + g_strfreev(lines); + g_free(maps); - /* ensure the map data is in the same order we collected it */ - return g_slist_reverse(map_info); + return root; } /** * free_self_maps: - * @info: a GSlist + * @root: an interval tree * - * Free a list of MapInfo structures. + * Free a tree of MapInfo structures. + * Since we allocated each MapInfo in one chunk, we need not consider the + * contents and can simply free each RBNode. */ -static void free_info(gpointer data) + +static void free_rbnode(RBNode *n) { - MapInfo *e = (MapInfo *) data; - g_free(e->dev); - g_free(e->path); - g_free(e); + if (n) { + free_rbnode(n->rb_left); + free_rbnode(n->rb_right); + g_free(n); + } } -void free_self_maps(GSList *info) +void free_self_maps(IntervalTreeRoot *root) { - g_slist_free_full(info, &free_info); + if (root) { + free_rbnode(root->rb_root.rb_node); + g_free(root); + } } From patchwork Mon Aug 7 16:37:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711140 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127645ltf; Mon, 7 Aug 2023 09:38:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IED0cfHRiYWeT0xnU0P3AisyFCF0Z0/W1V5AhK3eiNzgSGmuREkO0vG4iXLNBAbmf45kjkS X-Received: by 2002:ac8:5bc1:0:b0:403:97e7:e1a1 with SMTP id b1-20020ac85bc1000000b0040397e7e1a1mr13127481qtb.54.1691426315244; Mon, 07 Aug 2023 09:38:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426315; cv=none; d=google.com; s=arc-20160816; b=Ab8arFBWe09TMECwq0Db7obp5Nzlbd7up1y9yOuHnq/jKzPbVq5vH9TCPJrlaP6Ua8 Xw54JUupUlBPwNqZzjUuf95BWIsZLBcMoGOk1Wx7Vh/MIXNNLalyZLv8/EricVeeBpRk aqPFIBUDHpDcZz4CzWTTtNq0eYg6tezM5XYHJ9P/zudhXY+fQpjqquEq63RaIfdcJkG/ p9fCJchjgNiqAdgQCe6yzbMmEQ9GQDy5OiZkfyP7ExibIRhAZex5e1aGbBEijC0vdNG3 stOnMw29hty1FH3ofexiLMwXraua5AfvnioH0UC4nWKfJsEcVsSvyUYLNA1CIeUV4NrD 0TbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LDhuT++xsqj01ETEWaKWE9ZYg7Hhe6sUk2NPXqCd0Zs=; fh=nM8F09xKNAze8fdJGCDZoH2K0rjOXqNih6JoyRf4NS8=; b=JeNfxtpF1GMeI8NuXYqIrJjlv95Ao6q/AyFs/zLcJk3vY4gkOWEONGDUgTXqbfzDF8 etL55XXJjmihPySvRJWAorf7BNPk2FvTQH8w62TYnQxOJh75LxcaStnksSNjaZl0v3Io 5s+hOuFd8PSZXoCb5FALheZq9G70atzNZLAyyDhcvano4BiyGfMHcDil9iWUAZniZFeY BYInPjtx8WY3OdEO8au61ALKH+phgWh9yoqvsKMD4deo9hMUproz2YS7uGNm0C4VULEU o6yAoe0Xx+vkThtd9k3D8h1224AMsX57ELVasFe1lfL0uo9o3PFVnfLTQLn1y0Nl92/f +qbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="n5zy/Phr"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d19-20020a05622a15d300b00403fd799ca4si5037917qty.84.2023.08.07.09.38.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:38:35 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="n5zy/Phr"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EV-0007JI-SX; Mon, 07 Aug 2023 12:37:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EN-00076H-El for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:22 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EL-0002i8-Sj for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:19 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1b9c5e07c1bso42277755ad.2 for ; Mon, 07 Aug 2023 09:37:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426236; x=1692031036; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LDhuT++xsqj01ETEWaKWE9ZYg7Hhe6sUk2NPXqCd0Zs=; b=n5zy/PhrTWy23L1oOIUnWS3oJprsmY065QGpA6tsdpW52JiRhhFYVFMZjyjUGt3qYY dvT4rvHyGK4zf7n5CLupovHy4sXDiLhepcpla6/cG5NsClwbHT1bYgzQGjSK4gedw8a4 ueYvOG2XpIEZmehlu47t7pDkwiLeaJxLhGMnrY9fKN1C3K/UsDsBnWhVITz0oJeMZhrM uSX8RGODefpcG73TowuV83wnZ7t5y9qLriWuiPnR58Njg1VwJmTEfNbHN5cDMx1cP8OS mDY07idypRUIqK6ogu2OtRbhEq5H3Lu1L5PuvMO1nzf4Ls/PI0ixsbuIzCJVpA+oaOAI XHMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426236; x=1692031036; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LDhuT++xsqj01ETEWaKWE9ZYg7Hhe6sUk2NPXqCd0Zs=; b=bBFsjioUinWt5MUPQdaDX3tzBJd1xA+zzq7Nv7aCexPrC+P6L1tbmZ2faNLCHAVwXb Nr5tAauPTY7W+2oDtLqQs5t4Hv/TYCc45l8GEUNnDOJofI6ZZ+83CXhd/bvhkOvJEFLL rbFz9JqxkCBlKqlqSeu3vRTmp4Uwj6oc7wODHy1BteQ1fM1o3wzj6kGccTAetd4tmoiR oFwt4rqAehpEDr26k2uVBMlLxQLALoC21oGLVlu2aVBStE+XWJWx+b/tBeumCZ1M70+B oBbd7hjeKHAEZfC3/RmtnHzCBVj6ZJDG8QxEDdWVEov2GjIbyehO5CWM1vJyEtJSdhAI w30A== X-Gm-Message-State: AOJu0YyNiaEoeL5QC9ST2YpmaOO1OswXr2QB65p6O+2gQU7IBd6iaMak 0hq7OfCpUVuL3HWdk0ubWWWq/5pbFA8w6nE5XKU= X-Received: by 2002:a17:902:6bc9:b0:1bb:ed01:2d03 with SMTP id m9-20020a1709026bc900b001bbed012d03mr9511445plt.50.1691426236511; Mon, 07 Aug 2023 09:37:16 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:16 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de Subject: [PATCH for-8.1 v10 11/14] linux-user: Remove duplicate CPU_LOG_PAGE from probe_guest_base Date: Mon, 7 Aug 2023 09:37:02 -0700 Message-Id: <20230807163705.9848-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x630.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The proper logging for probe_guest_base is in the main function. There is no need to duplicate that in the subroutines. Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index f497286abe..400af4a4c0 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2562,9 +2562,6 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, if (test != addr) { pgb_fail_in_use(image_name); } - qemu_log_mask(CPU_LOG_PAGE, - "%s: base @ %p for %" PRIu64 " bytes\n", - __func__, addr, (uint64_t)guest_hiaddr - guest_loaddr + 1); } /** @@ -2607,9 +2604,6 @@ static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, if (mmap_start != MAP_FAILED) { munmap(mmap_start, guest_size); if (mmap_start == (void *) align_start) { - qemu_log_mask(CPU_LOG_PAGE, - "%s: base @ %p for %" PRIdPTR" bytes\n", - __func__, mmap_start + offset, guest_size); return (uintptr_t) mmap_start + offset; } } @@ -2691,13 +2685,6 @@ static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size, } } free_self_maps(maps); - - if (ret != -1) { - qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %" PRIxPTR - " for %" PRIuPTR " bytes\n", - __func__, ret, guest_size); - } - return ret; } @@ -2749,9 +2736,6 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, } guest_base = addr; - - qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %"PRIxPTR" for %" PRIuPTR" bytes\n", - __func__, addr, hiaddr - loaddr); } static void pgb_dynamic(const char *image_name, long align) @@ -2809,9 +2793,6 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr, reserved_va + 1, test, strerror(errno)); exit(EXIT_FAILURE); } - - qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %p for %lu bytes\n", - __func__, addr, reserved_va + 1); } void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, From patchwork Mon Aug 7 16:37:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711144 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2128182ltf; Mon, 7 Aug 2023 09:39:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLq02Wh3JRlTyiwVtnxDNcB5XBOTJ8CDq9zl7uGq7Q12IRCjW31Bdz11AWI2HlpSdhhtWO X-Received: by 2002:a0c:e291:0:b0:63f:8212:bfd9 with SMTP id r17-20020a0ce291000000b0063f8212bfd9mr3079689qvl.59.1691426383361; Mon, 07 Aug 2023 09:39:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426383; cv=none; d=google.com; s=arc-20160816; b=sx7kfZXvjEuFj2yDjUCvkzZfOrDdzYLBCedQOBAWlgdtA87Nds/VEHs86zd1o2ID3i ZMBEgGBp6uIG2ZO9FRfcTB3hRwBUgEvPNYkBJpedCxGjNVl+eK/W+L4GxhGmygAWgygD wr4505i8XnRjyo3EXy8Lrc2gyXDvy3pA4sfwmJv1u1a7zoq2zC2kLi5l1s+eaRq3iVx+ ojfA4DP7HpvpzhFYTXFvTQ/+Xwi3ViziA1G5SaG5+VgeKd9pu36p8JZi1lF4VWcuvxnO kEp3CNkwkgKQDNacoEtJVoCZKs/wevyw8iy3UMSwwyVuI+UKgHbsBu01ZSYtDehZaEoT MT8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=37Oa1Q6Q3bF0gL9Um9Xifn0V/D4ohTtQq5asniTUIIY=; fh=nM8F09xKNAze8fdJGCDZoH2K0rjOXqNih6JoyRf4NS8=; b=FqZSn+PD1ooA+8xGhQZg5uboxp3JozPxbnks/oNHx9F9/EKzITqo1+Jbl9BeZfwuui pOnSDtiV27DIv5jRoiDq8Wl6nrYjjWyO/rXacIO57o6cDNrJCSS8faxpkGO0sIiWqrvh N2pi/VmBGQXFHiuc04V9wRj6KF4z3EqVJd36NSNt4YHedFtcecp23EytOMYY2KKLW7D5 r3LX/NF1uXRvYxbH2zSN36Yiespv3olm8TgGc2FQzSj6rzTZF8oaoyAheFF+OK2nMJ7J WHl7Bi268DlkwaCUTVNPNvU4LME2RceSm4D1qhFs8f5IzKLtG3/PdW9a2r6ZTXIDFUty 9/7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GCKh8Cay; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id m4-20020a0cac44000000b0063d15cbadb0si5085030qvb.613.2023.08.07.09.39.43 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:39:43 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GCKh8Cay; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EZ-0007Yv-Mh; Mon, 07 Aug 2023 12:37:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EO-00076J-GD for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:22 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EM-0002iJ-NP for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:20 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1bbf8cb694aso40640755ad.3 for ; Mon, 07 Aug 2023 09:37:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426237; x=1692031037; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=37Oa1Q6Q3bF0gL9Um9Xifn0V/D4ohTtQq5asniTUIIY=; b=GCKh8Cay5DJtgXhZ5sxRPiapN3ly1+VFPoH4828ht5vYjyDxgaj7/svl6v958AtbtL xA4aHW7bQyqg6E76O6+n1d+qc9bYRZNcuplLnEFlxklqCpKYrMrbD49PjzvzCStSCU4f c7Tqpkphb3jvlKxUTveOxzpGw4LtAZ1bV/9Qa3bAA2sWAgrdNa71lgLpU/jfTHiESNMr +XqVUMbfcPKRqkDNCF3jkEyx9gTySEx91Xb5+jCpk+Gt42EqsnsWEJPos8dMX+OgDrve 1X3x0sMyqKpkYcOcwbCYJaX1wPl3k1RB2qsK9OJb9eJ+FbsZEK+8gA1fnEYKAgTC1Voa JwTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426237; x=1692031037; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=37Oa1Q6Q3bF0gL9Um9Xifn0V/D4ohTtQq5asniTUIIY=; b=DVq+3sHACpgPpfCs2Dj8pwTUenAvM6M/gKM/3fpHJnwuyF/a1bmQ9ECyp3q0LMhUVv YwsE5JtKKCRns2sjdt6syyBaGKbUtE1dzWdhN06n6EQDikS8S/P1r71Xm5STk91LMW1n CPEKFqF6P/GrpFkWwPY+pnYJL4EiVAdkt+HngIFGVh418doww9j2QUcJVEwHaBmo+5Ro zvsvo7F2Tz+G89Nen9v2IYTm+SAIfvHheohFvA+Xmxg9hdlm9y4Flfe4emnZv4zVWWcc Vj8J8YCtlz6gqNH3rF+JigR09mWW38E3a6rDlmCjApZko1icDNqSrtVsvWf3+E31/y/p 2jfQ== X-Gm-Message-State: AOJu0Yw0RU0gqNyHFvqGXy8uLzf3OA+jP9zCj22iyeNQbZeEN7SezLq8 nK7WEU2/KMRGWBRUxw3Q0/26HLL5J6zgGci2Lcc= X-Received: by 2002:a17:902:d3c5:b0:1aa:d971:4623 with SMTP id w5-20020a170902d3c500b001aad9714623mr9888148plb.38.1691426237352; Mon, 07 Aug 2023 09:37:17 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:16 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de Subject: [PATCH for-8.1 v10 12/14] linux-user: Consolidate guest bounds check in probe_guest_base Date: Mon, 7 Aug 2023 09:37:03 -0700 Message-Id: <20230807163705.9848-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x632.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org The three sets of checks are identical, logically. Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 50 +++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 400af4a4c0..484ab7131a 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2527,25 +2527,6 @@ static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, exit(EXIT_FAILURE); } - /* Sanity check the guest binary. */ - if (reserved_va) { - if (guest_hiaddr > reserved_va) { - error_report("%s: requires more than reserved virtual " - "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr, reserved_va); - exit(EXIT_FAILURE); - } - } else { -#if HOST_LONG_BITS < TARGET_ABI_BITS - if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) { - error_report("%s: requires more virtual address space " - "than the host can provide (0x%" PRIx64 ")", - image_name, (uint64_t)guest_hiaddr + 1 - guest_base); - exit(EXIT_FAILURE); - } -#endif - } - /* * Expand the allocation to the entire reserved_va. * Exclude the mmap_min_addr hole. @@ -2696,13 +2677,6 @@ static void pgb_static(const char *image_name, abi_ulong orig_loaddr, uintptr_t offset = 0; uintptr_t addr; - if (hiaddr != orig_hiaddr) { - error_report("%s: requires virtual address space that the " - "host cannot provide (0x%" PRIx64 ")", - image_name, (uint64_t)orig_hiaddr + 1); - exit(EXIT_FAILURE); - } - loaddr &= -align; if (HI_COMMPAGE) { /* @@ -2768,13 +2742,6 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr, int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE; void *addr, *test; - if (guest_hiaddr > reserved_va) { - error_report("%s: requires more than reserved virtual " - "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr, reserved_va); - exit(EXIT_FAILURE); - } - /* Widen the "image" to the entire reserved address space. */ pgb_static(image_name, 0, reserved_va, align); @@ -2801,6 +2768,23 @@ void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, /* In order to use host shmat, we must be able to honor SHMLBA. */ uintptr_t align = MAX(SHMLBA, qemu_host_page_size); + /* Sanity check the guest binary. */ + if (reserved_va) { + if (guest_hiaddr > reserved_va) { + error_report("%s: requires more than reserved virtual " + "address space (0x%" PRIx64 " > 0x%lx)", + image_name, (uint64_t)guest_hiaddr, reserved_va); + exit(EXIT_FAILURE); + } + } else { + if (guest_hiaddr != (uintptr_t)guest_hiaddr) { + error_report("%s: requires more virtual address space " + "than the host can provide (0x%" PRIx64 ")", + image_name, (uint64_t)guest_hiaddr + 1); + exit(EXIT_FAILURE); + } + } + if (have_guest_base) { pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align); } else if (reserved_va) { From patchwork Mon Aug 7 16:37:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711143 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2127820ltf; Mon, 7 Aug 2023 09:38:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+TckCCdkrwXJO7B7IjNkA5e2FuNA4O4+Q7xx0l8Vv0sYsH5vdMIYFqMj32ymEKPa2Dwz0 X-Received: by 2002:ac8:5c02:0:b0:40f:f17e:9adf with SMTP id i2-20020ac85c02000000b0040ff17e9adfmr13507337qti.47.1691426334162; Mon, 07 Aug 2023 09:38:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426334; cv=none; d=google.com; s=arc-20160816; b=upIEBeaHy6RMAx/+VvXCAJuHKKHN3EkHd6PQYEflo1GJFRfET4EdEaWQkQFqVcL0dt IfjKzY3o0y2bovtHto+IQTQGGZ9ECIH2JOib+DJPxAv2Jb8Bd6oRbnBzrdsNROiEXiTa /pkfwfWqjePQnA3sJjAT9xbdvSU84XGN91duZHJe9cZLVb/luIcu6H8+lzz1rUpOx5cX cmTbXLES4+jWFfAGw/vVfEn7is15y+qmoZ2QIXhmib7y6ZoUbaEpgwUfyl0u4oOvVsgE MLJc6e1X8PiP3s3qUkOWkDysdirLPI89N6BWDqY55LWrY3gIkwL5fbEjqCQoiMTMK7/q ig7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UWcQY33lfMI5vcNDVB1diRmeJ4ckk4q/UyqRf5tZgVY=; fh=nM8F09xKNAze8fdJGCDZoH2K0rjOXqNih6JoyRf4NS8=; b=mdDTq4c4apTqgF+uWnHkZMAOJaBBhYW83rMa0p7BlF/hnA4XK/RRe6ImPViYsWyHZ4 tor+KHiF9aQFMibGuKtivryp6e/M4WPnLGZJIw4oKE8gR46xFl18s9Jw9XzEszDeAISq muFR/53UmcLe4FquBW8GpZ9gK+4R/M3sBWE141H9sEVpovWVoXkOl8UYOsbiZa6iVHuv E3bQsPtTTeB2W/VnwpSZsubentfp3D6bV+EBrAi7eAqeCqKo5uppp8GHwvzPN4OC+iTm Pz6Ul70ToTdSlSSE5rUi6TcTpQQZ4frW75MPo+7pZXd3fw2tUPEh0NWwLICKK9JnsDFk SNBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LMyuvnoa; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id w17-20020ac87e91000000b0040fd6d8194bsi5077858qtj.231.2023.08.07.09.38.53 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:38:54 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LMyuvnoa; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EY-0007Sh-7H; Mon, 07 Aug 2023 12:37:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EP-00076Q-JA for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:22 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EN-0002iU-Jv for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:21 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6877eb31261so3388623b3a.1 for ; Mon, 07 Aug 2023 09:37:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426238; x=1692031038; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UWcQY33lfMI5vcNDVB1diRmeJ4ckk4q/UyqRf5tZgVY=; b=LMyuvnoaPk7Eh6a/D9KwaDC3vjgvh6QhJwhQedxikjJP/0QmMxtb6PKIOmhuoUdwDA e8NSLhRvjO1DkaiAy8PtaOFVv/Ft3Hqe06/yOaayeTM/Y3sxyPT59ZVIGa9orrz7d9wG jA6y/GZ7rhzvmDyXXWDGhGmrOdNOn/MfH+ZV6zIxSS39oH8vlGSI2Q8Gfc3RNZ0CXvol cHhTAS2WV4kyB4FtJCn/0luqtphtZRNIbLrJ2HfnQAPDuIJu3Obc4/iyohhnfEpegJpy 4if7EGLBwsh6jGV3V8rRBS6a2bDnbIOt+8Opl8bv8adBxlOPgDOxQgYJLMxAH2bAW6/+ 5SMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426238; x=1692031038; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UWcQY33lfMI5vcNDVB1diRmeJ4ckk4q/UyqRf5tZgVY=; b=Z75L19R6Uam45UE3cSeHlJ2eWCEqu6pVHXisktLbIf+1jw9FUQppE+6Pg5vOJ7NcVX r7+gksbTM/xZ/x7fIAOaj+LwIZ+QTqEFqNC0i6qQVN7BCCKZMBt9E3XGmjOwVo+7oB4H teUeGdF4DJwQJTaNlyUsrtBiHjazCOOUOhNudpQ8v1/QpJBpBuryhqi3Zzfiackx7cYO yppW7H3KPlg4/STrCrEFAzvx8ImmpQRS6bMWu63pvn4suMxenGTMUP73VpQ3UcgeQF1U TohuVdRM9fqZy1ViZ7pQ6GSM0BVYJWQyT15Y6RT9qOgyuzgaNgKk/Hj5FxoaUdSVt6GT ATsw== X-Gm-Message-State: AOJu0YyoaxwI2ExXJdLWzLCtS4E6xwLjJ2quObXUuWzO1V1MMdLJkdM5 j0pAT6uL00nKRTV/XfWwq217KmIApY21vC61Hzc= X-Received: by 2002:a17:902:f68c:b0:1b8:9044:b8ae with SMTP id l12-20020a170902f68c00b001b89044b8aemr11576832plg.11.1691426238101; Mon, 07 Aug 2023 09:37:18 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:17 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de Subject: [PATCH for-8.1 v10 13/14] linux-user: Rewrite fixed probe_guest_base Date: Mon, 7 Aug 2023 09:37:04 -0700 Message-Id: <20230807163705.9848-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::434; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x434.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Create a set of subroutines to collect a set of guest addresses, all of which must be mappable on the host. Use this within the renamed pgb_fixed subroutine to validate the user's choice of guest_base specified by the -B command-line option. Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 189 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 162 insertions(+), 27 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 484ab7131a..33c74be3af 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2506,6 +2506,158 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, #endif #endif +/** + * pgb_try_mmap: + * @addr: host start address + * @addr_last: host last address + * @keep: do not unmap the probe region + * + * Return 1 if [@addr, @addr_last] is not mapped in the host, + * return 0 if it is not available to map, and -1 on mmap error. + * If @keep, the region is left mapped on success, otherwise unmapped. + */ +static int pgb_try_mmap(uintptr_t addr, uintptr_t addr_last, bool keep) +{ + size_t size = addr_last - addr + 1; + void *p = mmap((void *)addr, size, PROT_NONE, + MAP_ANONYMOUS | MAP_PRIVATE | + MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0); + int ret; + + if (p == MAP_FAILED) { + return errno == EEXIST ? 0 : -1; + } + ret = p == (void *)addr; + if (!keep || !ret) { + munmap(p, size); + } + return ret; +} + +/** + * pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t size, uintptr_t brk) + * @addr: host address + * @size: size + * @brk: host brk + * + * Like pgb_try_mmap, but additionally reserve some memory following brk. + */ +static int pgb_try_mmap_skip_brk(uintptr_t addr, uintptr_t addr_last, + uintptr_t brk, bool keep) +{ + uintptr_t brk_last = brk + 16 * MiB - 1; + + /* Do not map anything close to the host brk. */ + if (addr <= brk_last && brk <= addr_last) { + return 0; + } + return pgb_try_mmap(addr, addr_last, keep); +} + +/** + * pgb_try_mmap_set: + * @ga: set of guest addrs + * @base: guest_base + * @brk: host brk + * + * Return true if all @ga can be mapped by the host at @base. + * On success, retain the mapping at index 0 for reserved_va. + */ + +typedef struct PGBAddrs { + uintptr_t bounds[3][2]; /* start/last pairs */ + int nbounds; + bool with_null_page; +} PGBAddrs; + +static bool pgb_try_mmap_set(const PGBAddrs *ga, uintptr_t base, uintptr_t brk) +{ + for (int i = ga->nbounds - 1; i >= 0; --i) { + if (pgb_try_mmap_skip_brk(ga->bounds[i][0] + base, + ga->bounds[i][1] + base, + brk, i == 0 && reserved_va) <= 0) { + return false; + } + } + return true; +} + +/** + * pgb_addr_set: + * @ga: output set of guest addrs + * @guest_loaddr: guest image low address + * @guest_loaddr: guest image high address + * @identity: create for identity mapping + * + * Fill in @ga with the image, COMMPAGE and NULL page. + */ +static bool pgb_addr_set(PGBAddrs *ga, abi_ulong guest_loaddr, + abi_ulong guest_hiaddr, bool try_identity) +{ + int n; + + /* + * With a low commpage, or a guest mapped very low, + * we may not be able to use the identity map. + */ + if (try_identity) { + if (LO_COMMPAGE != -1 && LO_COMMPAGE < mmap_min_addr) { + return false; + } + if (guest_loaddr != 0 && guest_loaddr < mmap_min_addr) { + return false; + } + } + + memset(ga, 0, sizeof(*ga)); + n = 0; + + if (reserved_va) { + ga->bounds[n][0] = try_identity ? mmap_min_addr : 0; + ga->bounds[n][1] = reserved_va; + n++; + /* LO_COMMPAGE and NULL handled by reserving from 0. */ + } else { + /* Add any LO_COMMPAGE or NULL page. */ + if (LO_COMMPAGE != -1) { + ga->bounds[n][0] = 0; + ga->bounds[n][1] = LO_COMMPAGE + TARGET_PAGE_SIZE - 1; + n++; + } else if (!try_identity) { + ga->bounds[n][0] = 0; + ga->bounds[n][1] = TARGET_PAGE_SIZE - 1; + n++; + } + + /* Add the guest image for ET_EXEC. */ + if (guest_loaddr) { + ga->bounds[n][0] = guest_loaddr; + ga->bounds[n][1] = guest_hiaddr; + n++; + } + } + + /* + * Temporarily disable + * "comparison is always false due to limited range of data type" + * due to comparison between unsigned and (possible) 0. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wtype-limits" + + /* Add any HI_COMMPAGE not covered by reserved_va. */ + if (reserved_va < HI_COMMPAGE) { + ga->bounds[n][0] = HI_COMMPAGE & qemu_host_page_mask; + ga->bounds[n][1] = HI_COMMPAGE + TARGET_PAGE_SIZE - 1; + n++; + } + +#pragma GCC diagnostic pop + + ga->nbounds = n; + return true; +} + static void pgb_fail_in_use(const char *image_name) { error_report("%s: requires virtual address space that is in use " @@ -2514,33 +2666,21 @@ static void pgb_fail_in_use(const char *image_name) exit(EXIT_FAILURE); } -static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr, - abi_ulong guest_hiaddr, long align) +static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr, + uintptr_t guest_hiaddr, uintptr_t align) { - const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE; - void *addr, *test; + PGBAddrs ga; + uintptr_t brk = (uintptr_t)sbrk(0); if (!QEMU_IS_ALIGNED(guest_base, align)) { fprintf(stderr, "Requested guest base %p does not satisfy " - "host minimum alignment (0x%lx)\n", + "host minimum alignment (0x%" PRIxPTR ")\n", (void *)guest_base, align); exit(EXIT_FAILURE); } - /* - * Expand the allocation to the entire reserved_va. - * Exclude the mmap_min_addr hole. - */ - if (reserved_va) { - guest_loaddr = (guest_base >= mmap_min_addr ? 0 - : mmap_min_addr - guest_base); - guest_hiaddr = reserved_va; - } - - /* Reserve the address space for the binary, or reserved_va. */ - test = g2h_untagged(guest_loaddr); - addr = mmap(test, guest_hiaddr - guest_loaddr + 1, PROT_NONE, flags, -1, 0); - if (test != addr) { + if (!pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, !guest_base) + || !pgb_try_mmap_set(&ga, guest_base, brk)) { pgb_fail_in_use(image_name); } } @@ -2786,7 +2926,7 @@ void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, } if (have_guest_base) { - pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align); + pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align); } else if (reserved_va) { pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align); } else if (guest_loaddr) { @@ -2797,13 +2937,8 @@ void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, /* Reserve and initialize the commpage. */ if (!init_guest_commpage()) { - /* - * With have_guest_base, the user has selected the address and - * we are trying to work with that. Otherwise, we have selected - * free space and init_guest_commpage must succeeded. - */ - assert(have_guest_base); - pgb_fail_in_use(image_name); + /* We have already probed for the commpage being free. */ + g_assert_not_reached(); } assert(QEMU_IS_ALIGNED(guest_base, align)); From patchwork Mon Aug 7 16:37:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711146 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2129299ltf; Mon, 7 Aug 2023 09:41:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqOkhnXVSgGZQgw/sX3VS6eItv2KlIciwPlPrXyfX1FQV2mZ4uKXkJCRyQ9opCUJRlc76+ X-Received: by 2002:ac8:5f82:0:b0:407:fd3b:c6f4 with SMTP id j2-20020ac85f82000000b00407fd3bc6f4mr11723204qta.2.1691426514525; Mon, 07 Aug 2023 09:41:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691426514; cv=none; d=google.com; s=arc-20160816; b=Ov6v0j+vvcm+rkclwzhPYsKBXfl0J8FBfDA7rXiFa00cRX5lnw5XaI145/SJMEUjVe E3ETi31tKsLkY1qAKV26xBt3P6ux1LfR+SyG4QJoD490nnYyH8we3+7XK9qvszXn6YKR HyzY/nSxwO0+opUGuxv0m86MjwH+gdReBO8YqxOUE4WJQapUruNjW7BV7SbFL+leM9Tp /lJ1YSvn4a5WWMTbn8yZp4Xdx5pV6LgARirRYuc+vW8iYE+8J4sQGkJExuIYzGOp5fuq agZqZdjHvbdsqtWVZnHneQEp3CxASrbF4b0nN1E4H79C1XsiOekN1eBBUCJDdw4xkJSH DXFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2KKkyXS5jRgs+egnpTlRzEnIYCcJirPHC6HFXR682UI=; fh=nM8F09xKNAze8fdJGCDZoH2K0rjOXqNih6JoyRf4NS8=; b=RRR+iYhbvgqAeAq5fRYDyvTwJzpVDTPsZsDrKJ5SCEis1Kbxmtw3SXn39pxzGYaNgM K9bo/Kk8r60YtqmjoO1f9Ig0/sAwyo6jvrV4/kA1f01kQbVc1yBouh004xAWIHKJYnVI l84NPxpwaySDqobX7eL/nhlD5sePvCApfIxGXaj0vpN+nSe3BAxSO2hTux519iLfFj1R ay1bKFmL0UoTOMj/fP6slR9PJkJMIMaqY3Ud916g5C7CtidQB6PAyC0S7Tg+qxYoPoEB 86YbG1c2oARdNOnLc7Xzi76U+3F4LsRHGnhETV9u3xv/Cnh7tECQOOC5vHFUc26ZdYCE xJCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dtwX2AuA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id u5-20020ac858c5000000b0040f3c2813e2si5162891qta.166.2023.08.07.09.41.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Aug 2023 09:41:54 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dtwX2AuA; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qT3EX-0007QR-JZ; Mon, 07 Aug 2023 12:37:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qT3EQ-00076o-Ke for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:22 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qT3EO-0002ig-Bg for qemu-devel@nongnu.org; Mon, 07 Aug 2023 12:37:22 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1b9c5e07c1bso42278575ad.2 for ; Mon, 07 Aug 2023 09:37:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691426239; x=1692031039; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2KKkyXS5jRgs+egnpTlRzEnIYCcJirPHC6HFXR682UI=; b=dtwX2AuAhgsFHuE8mkaF6mD16xLgxmQeqyAqMH/P6CGJ6DhMLPwVk0/nlj90OpvtYn 0krER0vFjks8+h4KKd0b9PE1X/Adtrhc8JTOhWFRTHIGdcq5TfL/No+XAlIczSbiREEO IRBxZvfvqFtiv71Sa+AhxLvN3eaBoukzw3f6hE5/w3Wc/12eyFz5rKeW4lLMb2gR040G UKe4HMAbAn6yIPFvEd+9NrpqWaSqlUZmSS4xsWGDXFElz+wfiNNbDx4QpqUd2U6vwEM7 rlbGODJEDP8qC1KltgDxBp/hD1cRX4u1yikbmVkoEvKgZdjDlRDsXcHFX+aI5CoCeJ7b rjLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691426239; x=1692031039; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2KKkyXS5jRgs+egnpTlRzEnIYCcJirPHC6HFXR682UI=; b=i42TLdoX7/n6h9BYgl1ZnjDhfcLvKwLec1CisyFKPOgVZseCVkzNMLLM+cfg3u86d3 s/I+pww4zq0izku/uCd7m94aDBAPKAoxRWWHq+ElqMMQGgKcX2UNehbsZapxHQ5WkHZ+ VAzAffbUoN6r/vuz4Z6DWwxQCQjWJLHvERhCO1LXM8BO9KKXBuKY096wBE2YRPd7n61U bblds2P0KIu8Aod8di8FIpRJb+ty8uHX69xmUOcjaYS3dxhxEpQpydKXAMm/taFRW/jo V3KFE1nanZF/RcLWQJTRA7vIIl82ozrQaUXWkcy5NlkYyFvkmz1aE7eyi2ICKg6AgdK2 JGCw== X-Gm-Message-State: AOJu0Yx336DluZ5ZIiqyGQkyGNtrp8/7qbwJ2PM3+m7MWUeej538Zit7 M/Aq7XvWrUedhfQ7frknKBEm4SzsrJ9azUzkwoQ= X-Received: by 2002:a17:902:7604:b0:1b9:cb8b:3bd3 with SMTP id k4-20020a170902760400b001b9cb8b3bd3mr10515345pll.31.1691426239003; Mon, 07 Aug 2023 09:37:19 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:e306:567a:e0a1:341]) by smtp.gmail.com with ESMTPSA id l6-20020a170902d34600b001b9df8f14d7sm7119837plk.267.2023.08.07.09.37.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 09:37:18 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, philmd@linaro.org, laurent@vivier.eu, deller@gmx.de Subject: [PATCH for-8.1 v10 14/14] linux-user: Rewrite non-fixed probe_guest_base Date: Mon, 7 Aug 2023 09:37:05 -0700 Message-Id: <20230807163705.9848-15-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230807163705.9848-1-richard.henderson@linaro.org> References: <20230807163705.9848-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Use pgb_addr_set to probe for all of the guest addresses, not just the main executable. Handle the identity map specially and separately from the search. If /proc/self/maps is available, utilize the full power of the interval tree search, rather than a linear search through the address list. If /proc/self/maps is not available, increase the skip between probes so that we do not probe every single page of the host address space. Choose 1 MiB for 32-bit hosts (max 4k probes) and 1 GiB for 64-bit hosts (possibly a large number of probes, but the large step makes it more likely to find empty space quicker). Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- linux-user/elfload.c | 311 ++++++++++++++++--------------------------- 1 file changed, 115 insertions(+), 196 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 33c74be3af..ffea900308 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2686,220 +2686,143 @@ static void pgb_fixed(const char *image_name, uintptr_t guest_loaddr, } /** - * pgd_find_hole_fallback: potential mmap address - * @guest_size: size of available space - * @brk: location of break - * @align: memory alignment + * pgb_find_fallback: * - * This is a fallback method for finding a hole in the host address - * space if we don't have the benefit of being able to access - * /proc/self/map. It can potentially take a very long time as we can - * only dumbly iterate up the host address space seeing if the - * allocation would work. + * This is a fallback method for finding holes in the host address space + * if we don't have the benefit of being able to access /proc/self/map. + * It can potentially take a very long time as we can only dumbly iterate + * up the host address space seeing if the allocation would work. */ -static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk, - long align, uintptr_t offset) +static uintptr_t pgb_find_fallback(const PGBAddrs *ga, uintptr_t align, + uintptr_t brk) { - uintptr_t base; + /* TODO: come up with a better estimate of how much to skip. */ + uintptr_t skip = sizeof(uintptr_t) == 4 ? MiB : GiB; - /* Start (aligned) at the bottom and work our way up */ - base = ROUND_UP(mmap_min_addr, align); - - while (true) { - uintptr_t align_start, end; - align_start = ROUND_UP(base, align); - end = align_start + guest_size + offset; - - /* if brk is anywhere in the range give ourselves some room to grow. */ - if (align_start <= brk && brk < end) { - base = brk + (16 * MiB); - continue; - } else if (align_start + guest_size < align_start) { - /* we have run out of space */ + for (uintptr_t base = skip; ; base += skip) { + base = ROUND_UP(base, align); + if (pgb_try_mmap_set(ga, base, brk)) { + return base; + } + if (base >= -skip) { return -1; - } else { - int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE | - MAP_FIXED_NOREPLACE; - void * mmap_start = mmap((void *) align_start, guest_size, - PROT_NONE, flags, -1, 0); - if (mmap_start != MAP_FAILED) { - munmap(mmap_start, guest_size); - if (mmap_start == (void *) align_start) { - return (uintptr_t) mmap_start + offset; - } - } - base += qemu_host_page_size; } } } -/* Return value for guest_base, or -1 if no hole found. */ -static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size, - long align, uintptr_t offset) +static uintptr_t pgb_try_itree(const PGBAddrs *ga, uintptr_t base, + IntervalTreeRoot *root) { - IntervalTreeRoot *maps; - IntervalTreeNode *iter; - uintptr_t this_start, this_end, next_start, brk; - intptr_t ret = -1; + for (int i = ga->nbounds - 1; i >= 0; --i) { + uintptr_t s = base + ga->bounds[i][0]; + uintptr_t l = base + ga->bounds[i][1]; + IntervalTreeNode *n; + + if (l < s) { + /* Wraparound. Skip to advance S to 0. */ + return -s; + } + + n = interval_tree_iter_first(root, s, l); + if (n != NULL) { + /* Conflict. Skip to advance S to LAST + 1. */ + return n->last - s + 1; + } + } + return 0; /* success */ +} + +static uintptr_t pgb_find_itree(const PGBAddrs *ga, IntervalTreeRoot *root, + uintptr_t align, uintptr_t brk) +{ + uintptr_t last = mmap_min_addr; + uintptr_t base, skip; + + while (true) { + base = ROUND_UP(last, align); + if (base < last) { + return -1; + } + + skip = pgb_try_itree(ga, base, root); + if (skip == 0) { + break; + } + + last = base + skip; + if (last < base) { + return -1; + } + } + + /* + * We've chosen 'base' based on holes in the interval tree, + * but we don't yet know if it is a valid host address. + * Because it is the first matching hole, if the host addresses + * are invalid we know there are no further matches. + */ + return pgb_try_mmap_set(ga, base, brk) ? base : -1; +} + +static void pgb_dynamic(const char *image_name, uintptr_t guest_loaddr, + uintptr_t guest_hiaddr, uintptr_t align) +{ + IntervalTreeRoot *root; + uintptr_t brk, ret; + PGBAddrs ga; assert(QEMU_IS_ALIGNED(guest_loaddr, align)); - maps = read_self_maps(); + /* Try the identity map first. */ + if (pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, true)) { + brk = (uintptr_t)sbrk(0); + if (pgb_try_mmap_set(&ga, 0, brk)) { + guest_base = 0; + return; + } + } + + /* + * Rebuild the address set for non-identity map. + * This differs in the mapping of the guest NULL page. + */ + pgb_addr_set(&ga, guest_loaddr, guest_hiaddr, false); + + root = read_self_maps(); /* Read brk after we've read the maps, which will malloc. */ brk = (uintptr_t)sbrk(0); - if (!maps) { - return pgd_find_hole_fallback(guest_size, brk, align, offset); - } - - /* The first hole is before the first map entry. */ - this_start = mmap_min_addr; - - for (iter = interval_tree_iter_first(maps, 0, -1); - iter; - this_start = next_start, - iter = interval_tree_iter_next(iter, 0, -1)) { - MapInfo *info = container_of(iter, MapInfo, itree); - uintptr_t align_start, hole_size; - - this_end = info->itree.start; - next_start = info->itree.last + 1; - align_start = ROUND_UP(this_start + offset, align); - - /* Skip holes that are too small. */ - if (align_start >= this_end) { - continue; - } - hole_size = this_end - align_start; - if (hole_size < guest_size) { - continue; - } - - /* If this hole contains brk, give ourselves some room to grow. */ - if (this_start <= brk && brk < this_end) { - hole_size -= guest_size; - if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) { - align_start += 1 * GiB; - } else if (hole_size >= 16 * MiB) { - align_start += 16 * MiB; - } else { - align_start = (this_end - guest_size) & -align; - if (align_start < this_start) { - continue; - } - } - } - - /* Record the lowest successful match. */ - if (ret < 0) { - ret = align_start; - } - /* If this hole contains the identity map, select it. */ - if (align_start <= guest_loaddr && - guest_loaddr + guest_size <= this_end) { - ret = 0; - } - /* If this hole ends above the identity map, stop looking. */ - if (this_end >= guest_loaddr) { - break; - } - } - free_self_maps(maps); - return ret; -} - -static void pgb_static(const char *image_name, abi_ulong orig_loaddr, - abi_ulong orig_hiaddr, long align) -{ - uintptr_t loaddr = orig_loaddr; - uintptr_t hiaddr = orig_hiaddr; - uintptr_t offset = 0; - uintptr_t addr; - - loaddr &= -align; - if (HI_COMMPAGE) { + if (!root) { + ret = pgb_find_fallback(&ga, align, brk); + } else { /* - * Extend the allocation to include the commpage. - * For a 64-bit host, this is just 4GiB; for a 32-bit host we - * need to ensure there is space bellow the guest_base so we - * can map the commpage in the place needed when the address - * arithmetic wraps around. + * Reserve the area close to the host brk. + * This will be freed with the rest of the tree. */ - if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) { - hiaddr = UINT32_MAX; - } else { - offset = -(HI_COMMPAGE & -align); - } - } else if (LO_COMMPAGE != -1) { - loaddr = MIN(loaddr, LO_COMMPAGE & -align); + IntervalTreeNode *b = g_new0(IntervalTreeNode, 1); + b->start = brk; + b->last = brk + 16 * MiB - 1; + interval_tree_insert(b, root); + + ret = pgb_find_itree(&ga, root, align, brk); + free_self_maps(root); } - addr = pgb_find_hole(loaddr, hiaddr - loaddr + 1, align, offset); - if (addr == -1) { - /* - * If HI_COMMPAGE, there *might* be a non-consecutive allocation - * that can satisfy both. But as the normal arm32 link base address - * is ~32k, and we extend down to include the commpage, making the - * overhead only ~96k, this is unlikely. - */ - error_report("%s: Unable to allocate %#zx bytes of " - "virtual address space", image_name, - (size_t)(hiaddr - loaddr)); - exit(EXIT_FAILURE); - } - - guest_base = addr; -} - -static void pgb_dynamic(const char *image_name, long align) -{ - /* - * The executable is dynamic and does not require a fixed address. - * All we need is a commpage that satisfies align. - * If we do not need a commpage, leave guest_base == 0. - */ - if (HI_COMMPAGE) { - uintptr_t addr, commpage; - - /* 64-bit hosts should have used reserved_va. */ - assert(sizeof(uintptr_t) == 4); - - /* - * By putting the commpage at the first hole, that puts guest_base - * just above that, and maximises the positive guest addresses. - */ - commpage = HI_COMMPAGE & -align; - addr = pgb_find_hole(commpage, -commpage, align, 0); - assert(addr != -1); - guest_base = addr; - } -} - -static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr, - abi_ulong guest_hiaddr, long align) -{ - int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE; - void *addr, *test; - - /* Widen the "image" to the entire reserved address space. */ - pgb_static(image_name, 0, reserved_va, align); - - /* osdep.h defines this as 0 if it's missing */ - flags |= MAP_FIXED_NOREPLACE; - - /* Reserve the memory on the host. */ - assert(guest_base != 0); - test = g2h_untagged(0); - addr = mmap(test, reserved_va + 1, PROT_NONE, flags, -1, 0); - if (addr == MAP_FAILED || addr != test) { - error_report("Unable to reserve 0x%lx bytes of virtual address " - "space at %p (%s) for use as guest address space (check your " - "virtual memory ulimit setting, mmap_min_addr or reserve less " - "using qemu-user's -R option)", - reserved_va + 1, test, strerror(errno)); + if (ret == -1) { + int w = TARGET_LONG_BITS / 4; + + error_report("%s: Unable to find a guest_base to satisfy all " + "guest address mapping requirements", image_name); + + for (int i = 0; i < ga.nbounds; ++i) { + error_printf(" %0*" PRIx64 "-%0*" PRIx64 "\n", + w, (uint64_t)ga.bounds[i][0], + w, (uint64_t)ga.bounds[i][1]); + } exit(EXIT_FAILURE); } + guest_base = ret; } void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, @@ -2927,12 +2850,8 @@ void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, if (have_guest_base) { pgb_fixed(image_name, guest_loaddr, guest_hiaddr, align); - } else if (reserved_va) { - pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align); - } else if (guest_loaddr) { - pgb_static(image_name, guest_loaddr, guest_hiaddr, align); } else { - pgb_dynamic(image_name, align); + pgb_dynamic(image_name, guest_loaddr, guest_hiaddr, align); } /* Reserve and initialize the commpage. */