From patchwork Tue Aug 8 21:08:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711576 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2881820ltf; Tue, 8 Aug 2023 14:10:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF6ljFf02vCVnRUSwFgfmY7wkBRwsSHt7tVPv/HT3T8JaVaor2sQttV4r34UT1/6XtvzzTa X-Received: by 2002:ac8:5bcf:0:b0:40a:fc6a:e86e with SMTP id b15-20020ac85bcf000000b0040afc6ae86emr1442298qtb.62.1691529027118; Tue, 08 Aug 2023 14:10:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529027; cv=none; d=google.com; s=arc-20160816; b=ArU9pibSRTzeo6njzquwS964jiSi3yls0CDWlXpzaun8a5dAd6U7ggnawzUNYUIYoU BO4Mb2a5LIy0KKioiGlR6x1nUEE1zlEmpNbpVBqe34Gkrls/vfSUTCr5cKjHHtjFSrnM AFaGPP42/eB++QiEe1elp/jU+9bFHHfKpdzuljmWZRUQsS2g5WMp+MFDEqgb/Yd0m2mx wEYFhcaqE2JE04B05b+panIQiuYZkyKWzs35kZqNqnKLA0ysbIzbadoW17lMF2Bq+2yc +mKRxs/+KGXzmwbOlKq1MSTkzEVtiHFCQZHpGCmLKHhLiGZDIxVbaKExlKJC+hytAqDw IvmA== 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=p1NLSIvorkQrsaaDKLyaBigg9Lde4sad1iqhAbZMlyw=; fh=e1NRMjxVMtV0F5wYJj2s+gr60lsrqkDmE/ulJ4pjdtQ=; b=0pc0P1bZxgAJWqVr0HH4N8ccHNQif+qyeeNFAOL5yGEFH5HIvV+WrQq/GlbTckpX92 anl20+HilLQnyIx59vIB6BgMuzt+NA3PKcsZBXIGydr97g0Gl4TCNvbo7EFhHW6udMaj yELuPsIfSdnvTdGeh0++FFGbI0uygTEeH6AGRabYcuHDfGeyXIbbMqaGF1KNl+eXwnZy qCTBmv0b5bO3qW79Q/Q66VfIcYoFyO3YGaWgel5WfWAupG4kBSAtKGZfJ2hoqs8PHoIn HjFiQL6thmXAWCixfR8c/XXg4b2ktbpCzQnB7xh575PzNK936Vu2mQvuc7Dq9whDP8Uz LYtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MDpP9kd7; 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-20020a05622a048b00b00403489b9d8fsi6982709qtx.472.2023.08.08.14.10.26 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:10:27 -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=MDpP9kd7; 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 1qTTwt-0007ez-TR; Tue, 08 Aug 2023 17:09:03 -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 1qTTws-0007eP-44 for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:02 -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 1qTTwq-0005vF-BH for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:01 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1bc3d94d40fso53342925ad.3 for ; Tue, 08 Aug 2023 14:08:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528939; x=1692133739; 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=p1NLSIvorkQrsaaDKLyaBigg9Lde4sad1iqhAbZMlyw=; b=MDpP9kd7pBJnh2iFK0ctNMk5dSBef9/29x7WlX4pZnMuIP7GlBFsLMgqrvTrXk/koM JZ8k94EdoUwS+lkhgVwEZq5tYlbz8g03IhQcsiWa3BLywJAiU4TIDSO++1jUPnxMaqMi nHKv/1pLs3yyoaMxJw7Lgj1JATSHmKEYeubktBkZTru1i8v05dDiUANLbVyh4sU9PaJp whR5ud+QMwXONiItj3XyvnK0Vxi8Nye3WeWapifVDGuvOJsXBPnuDwk1JccqKap/W3QI IdhrbOPpcEF2cB5NSKdI1FwdUkwHuVHH6TSInc3VvzEiwEdUfIDHFai2TnWvMJ0gASRV TedQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528939; x=1692133739; 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=p1NLSIvorkQrsaaDKLyaBigg9Lde4sad1iqhAbZMlyw=; b=MgymohYg/HfyyweHXShfUhkM7Zj5SOtnrwucTjzusr9QMNcQAPM8nsce3Lzbcge7sE zztObyRR+eETT4slrlZ/88Vv5cXTnOfLNRZVKnE+HY0fosDTgr3h4TQnM5I63zDcSKuX 4Z7V2IcQOstHEad22E6gu/iPeNQPQjvb/OH1jAw2o5Ewbn4zp2r9yN1fOdE3aRngS1Wc 2JolgOVO8LBwgwAomCRG9psIZU1SNGWfwA0NO5m9mxQojpA4hy8U0xDwwT+hGfny8vL+ mwsbtCX178rJnUG004GXb0Xpmd41tJ/rZ6Wzykxek9VTQvgfx972D9MOITsJX3P5Yg3D +h2g== X-Gm-Message-State: AOJu0Yx7WcInMwNxA+KnqDij5py8s+yHyIzPCHsXWiRvE+ZA3cf1/BXT b0iBrnqErPyxpqnaa0MaQUiIFYhhu6kEgPyPb4U= X-Received: by 2002:a17:902:dacf:b0:1bb:97d0:c628 with SMTP id q15-20020a170902dacf00b001bb97d0c628mr1038330plx.31.1691528938986; Tue, 08 Aug 2023 14:08:58 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.08.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:08:58 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki Subject: [PULL 01/14] linux-user: Adjust task_unmapped_base for reserved_va Date: Tue, 8 Aug 2023 14:08:43 -0700 Message-Id: <20230808210856.95568-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-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 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 | 34 +++++++++++++++++++++++++++++++++- linux-user/main.c | 28 ++++++++++++++++++++++++++++ linux-user/mmap.c | 18 +++--------------- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/linux-user/user-mmap.h b/linux-user/user-mmap.h index 7265c2c116..2c9d99ed6c 100644 --- a/linux-user/user-mmap.h +++ b/linux-user/user-mmap.h @@ -18,6 +18,39 @@ #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 + +/* + * Guest parameters for the ADDR_COMPAT_LAYOUT personality + * (at present this is the only layout supported by QEMU). + * + * TASK_UNMAPPED_BASE: For mmap without hint (addr != 0), the search + * for unused virtual memory begins at TASK_UNMAPPED_BASE. + * + * task_unmapped_base: When the guest address space is limited via -R, + * the value of TASK_UNMAPPED_BASE is adjusted to fit. + */ +extern abi_ulong task_unmapped_base; + +/* + * mmap_next_start: The base address for the next mmap without hint, + * increased after each successful map, starting at task_unmapped_base. + * This is an optimization within QEMU and not part of ADDR_COMPAT_LAYOUT. + */ +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 +59,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 Tue Aug 8 21:08:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711575 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2881619ltf; Tue, 8 Aug 2023 14:10:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGytanzkxxLjFIWxPQ6LvBFFDPTN/JAa9N5So4ihDBKXHU5tnvzTNCBItrAl7qXo5+TdKy7 X-Received: by 2002:ac8:7dc8:0:b0:403:ecfe:2a66 with SMTP id c8-20020ac87dc8000000b00403ecfe2a66mr1049162qte.57.1691529002352; Tue, 08 Aug 2023 14:10:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529002; cv=none; d=google.com; s=arc-20160816; b=VhySB6AyEc4bM5xw8j2t4+VF4qS4+Agbu/gNxEGEo5YJTzwh23L28vyagUFoEbNob9 FgT6Rlu249eVonwsj/DxEYG6yhU2e72hkRvL6fWdapqVvfXy8AOs7DuMlHFQojnClGLB 2gIjUGGVNPtCAr5ZFL+VEAAuaOqTn/5xLmvrNscNFH9H9t+CnDKZ5+e/32cS35TN58Op I80S7Pg3oJNT+zFsYNKZTCVC9NUfLVfDQyEcZlU2gU6szYwZuoYlWMproV+M7/ZIu523 thSegOlEbLGElcJ18LHjiMUTL+vfOL7v2AkDe+qfFGR6XEK//k40R1jO/k9NxcpK+lhE 3c/A== 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=A9/ZyN+A4dvu4eBsZbJl245Yx/P0Ka3dfVWhmlJt8gU=; fh=9PWVySHAdMbaJwSH2JIu1yvgc2H4HTtBBPJgaJCWFJQ=; b=d9g2g8TWDCCh1pW2+VRZAR/9YVrSStrJDB/DJQcTS/34og0yUECE2DEjpAejnNRRyW B4EFP7miaoOOFgvRZv/wXGyL6Sfd1zfYRijsNTLpSZvy018YWB+nMM2I+I33uCFWBqwr vWfLzRDjUs7LOUx8oazQnOEWyagZIyhiIpKMFwcxPHMk8OrVlKqCokFP5Z/StrNWB8MA uPDkRgjODccNovQSZsbVx65nGjb0qETYeFRTll3sb5xxHyh1fOeiOyRTOIXXDZ+ANYkj mDaRc/gY9tiS8r5hsMg5ajz9hMaW0F/zL5VJOzeGxstaCyuRD08M3/Jc105OJJHOWT3y 2i7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ad8e9m+3; 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 w13-20020ac87e8d000000b0040e1bc9f1e7si6629483qtj.249.2023.08.08.14.10.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:10:02 -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=ad8e9m+3; 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 1qTTwv-0007fp-FZ; Tue, 08 Aug 2023 17:09:05 -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 1qTTwt-0007ex-QE for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:03 -0400 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwr-0005vU-BY for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:03 -0400 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-565334377d0so9890a12.2 for ; Tue, 08 Aug 2023 14:09:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528940; x=1692133740; 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=A9/ZyN+A4dvu4eBsZbJl245Yx/P0Ka3dfVWhmlJt8gU=; b=ad8e9m+3TPqbUQMZ9DaCKu5cPxYMMMfIn51ZhywyAJKhMopHZnrUpCiRFqAWAb9Ap1 LkdpINZ4bdpr/izsojVveu9N9CLp7IF6Rce5uMF5WGpNu8XloAqMpTFzCuP3PJlw9OyL wJIohIJAHtJQ8nI8Esp3nxGfpCxXYq0ZG3uDcjKchJxfV9wiyxKbZu01oWosdz7TrMtC UQ2gqSrQyM8CAdBDFmEYWNRJcM8L3UNNhejkqZRzrmk5fmWh/NtstlFwBZYVcHFxEFpO hLbAzDNqQFdE/xsrHFhfD/0XV7SNggMDQojcIOuQq8vhupeRo2mHHqV6Uz3SDggM8FgB kGBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528940; x=1692133740; 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=A9/ZyN+A4dvu4eBsZbJl245Yx/P0Ka3dfVWhmlJt8gU=; b=TP8Lj4wExT2dQjrH+1nkc331kRfhzWO2BqAUmVjVz7dbQRPMv1OVg4StjrRxN3H7tG pMir8SF7D8PqI5U62NBSjnhFuI6LDBMXSLZCE4zUuUaaF8oOMqxkaZxOX/dv1sTyiRLe 95iKlqJhjxB3dAs0OL4f3Ywhe//0o89sTx+avtKbTAmk+lUsuR7Rchxw2sr0eZc1gVuu kxSXehbUpUiX9N3hAr4yX+OP5R3ypQg14XrTUR9cmGbFwJpTL2bcJP0i76JY4ZncMOmc eu9/qGUspkaPSZIqnS730bBd6adHyXIciDtsbybYWCzfBx+StLMTAhyv9QOGMlms+I4J iPAQ== X-Gm-Message-State: AOJu0YyvmRRJRQWj8mPuo+ojuP3BR0d36J8jEKVW8NYGtAIrjUzXoqnH f1K8346v+e+hlpV+p76NdkQ8PCH7fEIjIq8LVHM= X-Received: by 2002:a17:90a:1608:b0:268:13c4:b800 with SMTP id n8-20020a17090a160800b0026813c4b800mr674710pja.21.1691528939821; Tue, 08 Aug 2023 14:08:59 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.08.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:08:59 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 02/14] linux-user: Define TASK_UNMAPPED_BASE in $guest/target_mman.h Date: Tue, 8 Aug 2023 14:08:44 -0700 Message-Id: <20230808210856.95568-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52a; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52a.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 Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- 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 2c9d99ed6c..f844ba4e61 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 - /* * Guest parameters for the ADDR_COMPAT_LAYOUT personality * (at present this is the only layout supported by QEMU). 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 Tue Aug 8 21:08:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711579 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882065ltf; Tue, 8 Aug 2023 14:10:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG4Ah1ANr3Dlo5vAsRcdu3RSetucs+ND65va5L7HfWmrVYKDvtAcXK73iiKEKBHkOCxruxX X-Received: by 2002:a05:6358:7606:b0:135:4003:784a with SMTP id r6-20020a056358760600b001354003784amr557241rwg.19.1691529052500; Tue, 08 Aug 2023 14:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529052; cv=none; d=google.com; s=arc-20160816; b=APBEabNFafdVgEYYWFMwzm71XEio6YczmM/LwV/p4IjlIPkW3FsUTb6lKyz9FHdqbZ hHRb1oWz+glLWtMugZ40Km0GLZlVqv4MHde8F65ffz5k2d1CVkzMG56WqTDHxF/DBHZO hTzG6VEjjnptNKDXSb+sCnHZvhi/x2qBS4SXLb6CfGRUgLq0iL+VteCNDabALYXFw/GV YI6ovCy8wrpPLIUM+FzltddCxH8iO5e9iPbhsuVIt6+hARgq+FugIbIXvBXLOu1Z7squ EvQlJHlawngYvGo+VmVqFMyKjNWmC3bNxT+HswYBG0nCD00N0klNu0x7isDSv1xvUomQ CpSA== 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=wivyov7AEZeRw2PcNpFecn0oeBHeQvthqNe4bGaMto4=; fh=nKwyeXx8rt8hF214OSFP+HjBpDto8utNxLoZSNqK/Bc=; b=0ukA5eh92z8q+NKwYizJtkdYyt3/0nTpK6nitUNdcpf8kkMlxymhqEA7bAykjZRu8M aRhEU1Q9ZUWKc5SeBUhh6+zHeWcvIY+VJ48EiNbWpOI6+yBqqSN5lFn6W7JscC0FNN8m RusRgPh3VC4BvnxwW0PsIFnnwSV3CqTfcQNSJ1f8EbVms4Vypj1HJd8/Kxb9zeoiJGnt hijb6FoR7+wsiefwVqh540xB8DRAlcP6T8tXF8GSNARnlxT13YZMzB7vg5MRFpz6g/RC SvCIiDo2JiVdjaJgzq+M2X4K1e8PyYW03qw4xPDPO+uoaNZojvuNKg4bE69m0NuuUIHF vzIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=uE7Sqft1; 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 b3-20020a0ccd03000000b0063cb0c44abcsi6774130qvm.373.2023.08.08.14.10.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:10: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=uE7Sqft1; 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 1qTTwv-0007fx-U5; Tue, 08 Aug 2023 17:09:05 -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 1qTTwu-0007fK-Lw for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:04 -0400 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTws-0005vd-DK for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:04 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1bc411e9d17so38432605ad.0 for ; Tue, 08 Aug 2023 14:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528941; x=1692133741; 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=wivyov7AEZeRw2PcNpFecn0oeBHeQvthqNe4bGaMto4=; b=uE7Sqft1xQe+xPNM1hSHiPtrKw4awhEjr/A8NR6QvVvjOexfB3X0danKSF9spowljC Tahiinct+/y52FPer034/c5bcMgT4ZKzA7jTR7/DB4i9t4PSIwKd0XUFNfiqtZ/cxJMk uMXAtV+5xZuApyAZGUgwrnFbQSOT5NyWO/qh7Hz3lYwFBtZJWit2NDfQ4Feq3qsfpixZ RJKl/96khhNvqQxrwOU62EqgWC4nAdOJW+frH55LusXcXfhvxE4r/HqzXD3gbkplAqTx QxyBzfobczAvTsN9YCR9e1ztwXLYRT3X+M92XpefVf0Rb9ShRzQVfOuK9jsSY81+w9yS 9btQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528941; x=1692133741; 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=wivyov7AEZeRw2PcNpFecn0oeBHeQvthqNe4bGaMto4=; b=BHF0IphDtAetma04OPVz3T6ECS2Zn1Sbr1Bd93SvGkIEKnvkksWKtmoxIXnan45FN5 BpWhR+pqFfi3jDgE8SIl0VBP5BvPWRFRQdrGh7guUO0fvWzqgf9eh9smQAQDbkaDGrIT WmfwyFKSmIJ2w+rrY24KcmOC6pi/eIR+8dMYS84y9bbMLyhqgqh1oPMIOnKlsioFs3TG g+SwHpycxTRx72ZjTMgk8Aq4QsNu0NvMVf33yOzI8hQil+d8PwPpAqC6bgl/CLag0Wte IIWs6oIGrYslbc27fLLoy/tWNP5+CS1/9rkrGnVhbrnJqqzloLYSPcnVbYK9+uPkdUJ1 GHkA== X-Gm-Message-State: AOJu0YydJee7qAueTkKl7ENxNvURMqaO0FlLD9mg4JYvjKAPvQUH39SY SahhlpNVI47y3Hcxji7gAjZl7qAb//ATxXv3uyM= X-Received: by 2002:a17:903:120c:b0:1b8:b3f0:3d57 with SMTP id l12-20020a170903120c00b001b8b3f03d57mr983853plh.31.1691528940854; Tue, 08 Aug 2023 14:09:00 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.08.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:00 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller Subject: [PULL 03/14] linux-user: Define ELF_ET_DYN_BASE in $guest/target_mman.h Date: Tue, 8 Aug 2023 14:08:45 -0700 Message-Id: <20230808210856.95568-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x635.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 | 13 +++++++++++-- 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, 106 insertions(+), 2 deletions(-) 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 f844ba4e61..0f4883eb57 100644 --- a/linux-user/user-mmap.h +++ b/linux-user/user-mmap.h @@ -25,10 +25,19 @@ * TASK_UNMAPPED_BASE: For mmap without hint (addr != 0), the search * for unused virtual memory begins at TASK_UNMAPPED_BASE. * - * task_unmapped_base: When the guest address space is limited via -R, - * the value of TASK_UNMAPPED_BASE is adjusted to fit. + * ELF_ET_DYN_BASE: When the executable is ET_DYN (i.e. PIE), and requires + * an interpreter (i.e. not -static-pie), use ELF_ET_DYN_BASE instead of + * TASK_UNMAPPED_BASE for selecting the address of the executable. + * This provides some distance between the executable and the interpreter, + * which allows the initial brk to be placed immediately after the + * executable and also have room to grow. + * + * task_unmapped_base, elf_et_dyn_base: When the guest address space is + * limited via -R, the values of TASK_UNMAPPED_BASE and ELF_ET_DYN_BASE + * must be adjusted to fit. */ extern abi_ulong task_unmapped_base; +extern abi_ulong elf_et_dyn_base; /* * mmap_next_start: The base address for the next mmap without hint, 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..9aab48d4a3 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -300,6 +300,7 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, } abi_ulong task_unmapped_base; +abi_ulong elf_et_dyn_base; abi_ulong mmap_next_start; /* From patchwork Tue Aug 8 21:08:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711572 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2881466ltf; Tue, 8 Aug 2023 14:09:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHcbGMegOUZHNXV/p2Zxc3BcyrIx2C8mUdDoncjgQDnZMjDFTy8wak7iFrAtFJigNC5NUNj X-Received: by 2002:a05:620a:a83:b0:767:154b:2f91 with SMTP id v3-20020a05620a0a8300b00767154b2f91mr893820qkg.24.1691528985008; Tue, 08 Aug 2023 14:09:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691528984; cv=none; d=google.com; s=arc-20160816; b=sqGNOS/n/W6WgRQbY6v1tX2hdL2IPwDbmCKv+3yDdqFdYQtG89cukQpXyp0DBZc1+j E9mH7aVxnrDk+Ro29oXh3+gNbki+NcoHfTpQvoVIvxznBbM5eVMdxaYXdIyUo2UpgqAe hCLSPooTWDBogPGw6HMTya7PB/uS3h69k4+pOJf6PxDUKHt1umEEEgiVlXne7C8s58Fm 5G43GbNoWsoN7cUFq2VfvJhp4+yy1DtKUZWf6DfxVkIqqzSnGrUirZCbcre1fmkrMJ5C 30ZSFyiI5uQeMhKKavActH0KF8ql6nyXVUPJSuDyXtQUxgNVzQ7Z3n9E4DsEiG+nY/QU wx/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=SJUFfIS4Wr1y8fuwf4as7ebNw3oHaTbxYse/4v8c7ug=; fh=e1NRMjxVMtV0F5wYJj2s+gr60lsrqkDmE/ulJ4pjdtQ=; b=nluAY4/z+W6BJZeNE4eKBIHvJvu97+3s8mMI/K25RHP+RTQDy4nHBrRnbrv4K2DlDF yKpsRgjqEAZNPV6gvU+U8U0GjziCqZlze2AAilMPV+941yucV/i11jWu4gHZh8w8BnEh VjS3wshPjRYMv/DDvxnPaATdJey9GSnmk7JhOcNoZUFHkiV+CMARdZM0w/IFjq7aRMgq EPzr6vZm4uk1lHkSM5gVX/GUpF+ZrUuRtpFbbTEgG5M+ix0ycwWB7NHFzQWfVEZSjsEx FGswYV5bFFiT1qh6GHQhWlC/JvDJkLV/0m6IGX0629vfKnVv0uVlZ+BBYkMKCzQzKUMo MWKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c3odcsCm; 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 x18-20020a05620a259200b00767b7c58ac4si6850560qko.605.2023.08.08.14.09.44 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:09: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=c3odcsCm; 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 1qTTww-0007gV-Mw; Tue, 08 Aug 2023 17:09:06 -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 1qTTwu-0007fM-Q3 for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:04 -0400 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwt-0005vx-3Q for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:04 -0400 Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-56433b1b12dso3652336a12.1 for ; Tue, 08 Aug 2023 14:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528942; x=1692133742; 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=c3odcsCmTbi6SjqtVD0sLtX7A5t+73vs2CspyQ4GUvPyU5DCV6Ipb7uTYNQH0jzXhN Q5WBls7nlioIAEk1aVJ85Y3jLYOLdcBJRomyjaU4QbQyHYCulJ34NvJfOhoEDCWLfGEv eS9zrkpUpCGi+SMJ2TJL5MMVbkzkcwuu22P9nvYmn86ZxaaNn9nrBoudSNmnVdax8V0K v95GinP1JDJQQ7ZWQFwnrK1eJXI0ulirLLjHKTijBv7aS+A9jRm3k/tu1UP7IyrGYFe4 ixPQCI1068f5NOknzSKbTnnYzPP8wuxiY6jugSH1A+3eML4oeZBc06If8Irgu5VsVZsi t46g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528942; x=1692133742; 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=DE89nli0ZFT4FTVhRGQhTkQYk09/R1Dcg86nKlzGOYjhrE4yewUUkv+4LGX4P1T40e FOCLi36oOD3B93ckstizDfQ14iQVwUfRIDLpp6krUCT716qQ1k41CxY+EumwcpsyNkcj 2NnFBmYu/oqr5uoyQg5JkEG3bG3O2ynnUhQEfvG4tgt0g/+b3te9sQXhjLXswNIdzQqH +CZ7hI1ZWcqlXYfNDSHIBGiExe5cA1YJqdzO8njgrGtkA1euv/noW+MGgpwIw6es4NUu HfPp1n9demPkz/s89voDPmOPh+8G+h3a9AZl97YjlHJHvMEmlKAJV5nmU3teMf0kJpP9 ofsQ== X-Gm-Message-State: AOJu0YwR5372LLch/ATRw4Zxey2d4fF9u66Ul22Kc2onLQXNVjOzZLyw QsjYEP7eukAk9biSCaGtC8DpUverVhE1849hTzY= X-Received: by 2002:a17:90a:6c05:b0:268:c5c7:f7ed with SMTP id x5-20020a17090a6c0500b00268c5c7f7edmr642605pjj.30.1691528941789; Tue, 08 Aug 2023 14:09:01 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:01 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki Subject: [PULL 04/14] linux-user: Use MAP_FIXED_NOREPLACE for initial image mmap Date: Tue, 8 Aug 2023 14:08:46 -0700 Message-Id: <20230808210856.95568-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::536; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x536.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 Tue Aug 8 21:08:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711584 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882303ltf; Tue, 8 Aug 2023 14:11:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGYDwjpsMSTBOLScbfqOjJtVuDY/FIKwXsoUK5NKCnHqEapkDmwsnuX+RiX546zgG1XuD8j X-Received: by 2002:a05:620a:4103:b0:767:22eb:6ed7 with SMTP id j3-20020a05620a410300b0076722eb6ed7mr1105412qko.4.1691529079210; Tue, 08 Aug 2023 14:11:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529079; cv=none; d=google.com; s=arc-20160816; b=WeN38/o27Fl2k1JMDgdRJJXrEbg5oqC1BIKPz7yyQQUn92+2K1p+NMAETpNFAOSqod osNbfsOYpRaeL+SonwC6idVoUw+9Y6y42/eFbh6DS5NcT7Y4R6vw+PL6QWBN/xf6c6/e v/9n6EuL0ji3qjrxhbwqfEI35BgHOZE1kPF0KsoAWhy86AE65fJqTYXJZi7AMdFxPLk4 6NONJnkV6+GMf8FPr6/S9gdfOw0bRAkXk8cUFgJMlCk+RGV31yz+XiyOmhuIzyxRwTm0 DRN2E9I6wDyEJqHZMtF33GzB7mDfU3/v+P1K+s4SZpj28qmFCiskoxYYcF26UA/87MX3 /URw== 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=e1NRMjxVMtV0F5wYJj2s+gr60lsrqkDmE/ulJ4pjdtQ=; b=TsSExPTU+HzHli3LKxkpUj2czJ+gQhJYes5+4R7DQ25YawMNiInFrj/bTB9cvqddRg RbRJWBDkZOga57uc7HbcO8BCdcHyNaa84vJqSdC0kiYlPPq4Ff8X02F9CAOq/caYxvGA vN+xQ/W3Gcy3NYKjz9BetUIXoBgKhWOEibmKnJQntqb3E5oVPOgirl6rIOuqU4gbouTY /fXxHm6PsC4o6VEJIghiUz0mOCCC4vRcNPM+geJvbwMBkqr08AVk4MiwJGhC6659GXIe ZHpDYP+ZpbZJZCBmUl+wmeFHpnc0XmHGOG3NlHA9Tqu13fxMwQLmWxfQnsBTDINZPgU3 BxNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=UjDpVlIi; 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 s8-20020a05620a29c800b0076d042d4d09si3760536qkp.751.2023.08.08.14.11.18 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:11: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=UjDpVlIi; 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 1qTTwx-0007gw-I1; Tue, 08 Aug 2023 17:09:07 -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 1qTTwv-0007fq-KA for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:05 -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 1qTTwt-0005w6-Tj for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:05 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1bb7b8390e8so39599115ad.2 for ; Tue, 08 Aug 2023 14:09:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528942; x=1692133742; 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=UjDpVlIiYsF6ilISjjrc5o9WMy5YabFGkw6/jtCyrTgMj+Y8zgEmJQRo9IhYNigZjo cvPypdt49XcwHtrW47BfZm3DQpPpP+MCvdJL1bJSPXmx5mtHI3BVXgL0HMOx4dackIKR mjSuPV8F2vE8986LG5SdTuIWehi82/cIu8k0qRp8wDqaDj6sQWBlrMTK8oVhma9+5KOL 974UXP/culj2J2EXY50eFV1ck6yMfzzwIKlBiJUDxgeYfULWVtxzu0XSMkIc0l0H64T/ 20532FQ919OK7RU/NdJCJiUJsnx5C7HA6Fylbvnanj6rMVqlOJpbHNK/H8hE9m7Kn8pk e5Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528942; x=1692133742; 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=fET6X+09IOsxZl6YstMWGD5euhjXq6SlSwdrjhefSeOHcmjCdjsZ2ZnL5qayhbffSn T3Slzef1i1YvI5SON47DHSqFZo3mZYu2P7nFIPRAqnGyPfR4pFE0O2NV4DJXFY1+RtWU UVakTZx9SdXOGF1vPrvr6Q8qSEoglIZO+D7ctXJXMjrkCPTOCRcOlnNoDz2wLjGRT+UK 5an1EFbrtuDGZveX0pQRUMcf4WcFAQbrks/+X0jHvUyWudaANLidShpHMB+Xq9zgqSN3 hLIzeG83Gfa3qRSNGTlUMrfwOKrd2zajikEPeiSmNjM2ijXkRU4oNJo4YVsCoLtfz51T H84g== X-Gm-Message-State: AOJu0YywWnAesmmxLnE9J0jwxSeP1tApjyRGTc6m5h83/PnGb9eyXqjj asJrQ+zUwK1X5Ge7CHy/E2HTC2k1wNJQlBchgV8= X-Received: by 2002:a17:902:e885:b0:1b8:72b2:fd3b with SMTP id w5-20020a170902e88500b001b872b2fd3bmr888619plg.54.1691528942605; Tue, 08 Aug 2023 14:09:02 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:02 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki Subject: [PULL 05/14] linux-user: Use elf_et_dyn_base for ET_DYN with interpreter Date: Tue, 8 Aug 2023 14:08:47 -0700 Message-Id: <20230808210856.95568-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-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 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 Tue Aug 8 21:08:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711581 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882178ltf; Tue, 8 Aug 2023 14:11:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHhUNc1upY4XMNK3xHn+BjmZ/BiRUXdzphmspWg7Mu13azwbXU5aguQ2siQ1X4vffq2N9ii X-Received: by 2002:a05:6214:4402:b0:635:3892:a2b0 with SMTP id oj2-20020a056214440200b006353892a2b0mr591246qvb.15.1691529067852; Tue, 08 Aug 2023 14:11:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529067; cv=none; d=google.com; s=arc-20160816; b=qMMPQ+FavHSF37hNmoBTFEk9P/auoeiU2jOolgD7c2grQfcspeQHoz5kMypgjhnaeT xM4coeRKhdG7+V7ZaAskatqN80xKPM+bCS+cqou2YeIpC1TAOrIeJ2u7OdxdmZYDxaYy 7W2LKakkGUMff01lpq9YkZ3GJTXGc6AxSxskf9k6cDGBUSaCMZ0PUpsumn2s52ViULGX ozBkeKCfjYx1HTYl7Q/+h606Xu58iBuTUS/yg9yt1NJC8wDUxZV38wa7tF5GjrXziDO2 A6IU84MxKuadACD0dLMfr7q/HPlDcakQ8I9qLtooPSf3YrETTFbFjZsQo9a3eP3XNGnV c+AA== 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=C24r9+7xFY76vv0xEMFFBjdertNUwY0/5sUwUC6qot0=; fh=9PWVySHAdMbaJwSH2JIu1yvgc2H4HTtBBPJgaJCWFJQ=; b=zPunrM/Fbd49HOgqf0gNc4z+JppW9w+igQJmLgspN49JUepSyA45KT233Wy+dZV/Ty 6bvxxBXEvJnuDxAapMLGTDvXqymfHjTthyLGFus9PF/0we6AkhDzixnuDdjDnRPJA+Av s+v2QSZfArwbOjJngPYe9X2yv9PCR1z8rGkY2m2MeQy7sU5Gf94kp8QdwmihFXwtWWRb 3o2iHhxN/hWhFkJFCKf69deAzdmHV8k/DxoXZRE1e72d9DVZBppC30jH+T9559gPBP1Y 0AiNYpMuCkzsr5vL24nmZpsRBf+bVGcHVcgiXs2IZ3JZwQyVRAsd8LqDfZ4Shk5xdz6s p55Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jxzsvTIU; 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 b20-20020a0cf054000000b0063cebcc91eesi6208802qvl.529.2023.08.08.14.11.07 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:11:07 -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=jxzsvTIU; 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 1qTTwy-0007hJ-Bw; Tue, 08 Aug 2023 17:09:08 -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 1qTTww-0007gT-Hw for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:06 -0400 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwu-0005wH-Oz for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:06 -0400 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1bbc87ded50so40538045ad.1 for ; Tue, 08 Aug 2023 14:09:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528943; x=1692133743; 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=C24r9+7xFY76vv0xEMFFBjdertNUwY0/5sUwUC6qot0=; b=jxzsvTIUwdON3X3T34UwouL5ohKkmleedvZlccK4emcjw97hFXYJvaCl3Xq3YF8i3N 8oJpjo//9ckuQWNNkK8lUAoMEUUCWgUqPwS8RmZ19VRW4Iimy6xQASewuiUulPYPqRx1 ZhiB7hc/XCioHtV9x0LzFpdXVmm2QzyFW0vgJpEqyEbUtQRi1Kg7u+vZtcHbt3qYG/ik 9lJ1eO+SaiY5MeyfIbpx97FyaMnbUTQoVwNHqbWtX8tIIGk0UipurdwRxLEwwIw0RiUv 2K+cMe4W+hmxoIviYUAQsYX29fwYKnc4GIFz3V1Y0ax2/f2ABdJsOwZ6DV5w5Kw1wR2k gExQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528943; x=1692133743; 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=C24r9+7xFY76vv0xEMFFBjdertNUwY0/5sUwUC6qot0=; b=hoqkRwip/tC62NIDwIKnAmY3rdIetTuzSJ+ocKCu07f0tcVXz2XRH3+V9MTJA+woat 1h6kmKHbEaOEwAYiZo1aaVgI3Dk4dzN6ySaWBk+nNrZQfYKCqjPsMATO0+DLCDLN0CWz 39KtfswwZ8MlZfpVUddnw3OkNqQy4wjF/OAsPNrEQJ4MSKiSqM+kPhCSAyJOVQqaWVMG 1Bq1oaQ6fatJUFlfNTbtuLqT5uwWbiByj87uU1g50+dmIsZgkvZlcGj+Pzg9I1SrEYAc xYUCxHr+pKQMSUt44UHapyMod87QclOL2e8E/282f4H++dDTglhTY9OZSOLqCbjuZpjx gANw== X-Gm-Message-State: AOJu0YyusU9S7PK5WXMStSKb2p0E41Q1eKIJpkbMiJnd9W4T9q/nMPcY P4m4WVfjSZamdX3KdT9c8Esl9eAxuxkGP+/WHns= X-Received: by 2002:a17:90a:c7c4:b0:268:f987:305d with SMTP id gf4-20020a17090ac7c400b00268f987305dmr674460pjb.5.1691528943582; Tue, 08 Aug 2023 14:09:03 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:03 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 06/14] linux-user: Adjust initial brk when interpreter is close to executable Date: Tue, 8 Aug 2023 14:08:48 -0700 Message-Id: <20230808210856.95568-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::635; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x635.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 Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- 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 Tue Aug 8 21:08:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711571 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2881282ltf; Tue, 8 Aug 2023 14:09:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFKaG/5bVKPR8bZGpa5RqIMVYMvW2h5JVd8xdz7LRJr9w1COEUFCZeeq1WwDpyudAtlBRv+ X-Received: by 2002:a05:6358:9392:b0:139:a45c:32b2 with SMTP id h18-20020a056358939200b00139a45c32b2mr687037rwb.11.1691528963891; Tue, 08 Aug 2023 14:09:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691528963; cv=none; d=google.com; s=arc-20160816; b=O+/A1tZ9k4GrHkaq5NacH7oxy73nlrLw5Lylnr7z+gsUY4o3VuwY2SCokZWywPapjq xd6i8l1S2zujvw7jAYp4pTB58+0FBROCYxf5RLCOg5ltgvmCvTEtzLBL0PYCfPKOftgN Jofjr35RROUtCRuSLvR+XFSHbhGzWx4FwoMiqmjjbmyWq2UdCWPUGj/W1n5cqxVFNQsV Y5iiYjXx/VGV6LaazLvX1dls1HDAl/1UvZ+xrmNj+K/9daOtUKnut3e2uzM8FqWvPC2a fNmh+imugci+A43dTcSisyFicOk4UiSphxvbkj+i6PHSsi9/DgeTZv6b/K2Ukb+ZQ92y 6JQw== 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=wka4C0I9AHmla/9hz2xv6tr7JssuiUOCCkjNKLiy/aQ=; fh=9PWVySHAdMbaJwSH2JIu1yvgc2H4HTtBBPJgaJCWFJQ=; b=YV8zmvvvSLMU7Jfe+xrzLZHGG8xrAthxbh2XHHkUdAGtL4b5BQXqvt3IOAxNrdqrgc 9t47zE3OCq00K/njGEeiq+ZwVjlwVnUkrXnYv0B7qbljss5GrsptINA1NvZFlih5zu70 ZM/qNZS8lBHxyzZ/Oa2/JnahJB/DJLyoQt1pNImNkEcTetZ/+FBrt8yaSMSjpcJix3zy ylARcfaYhSRHHLYGtN420cisVgrmrWhXddA/+KB/6a2upwVE0fsTmethyF4sJeda3WeV g/Z5Tw4kbJQBGz/7GjnNmpTvKPOiGkLVVj2w7z685tRYTcka8CD4L29bmfd1HZokPV0p JEqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=UWCJnMRV; 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 t1-20020ac85881000000b0040fe389422fsi6982870qta.501.2023.08.08.14.09.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:09:23 -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=UWCJnMRV; 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 1qTTwz-0007hX-2l; Tue, 08 Aug 2023 17:09:09 -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 1qTTwx-0007gh-DY for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:07 -0400 Received: from mail-oo1-xc33.google.com ([2607:f8b0:4864:20::c33]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwv-0005wV-P5 for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:07 -0400 Received: by mail-oo1-xc33.google.com with SMTP id 006d021491bc7-56cb1e602e7so3906175eaf.1 for ; Tue, 08 Aug 2023 14:09:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528944; x=1692133744; 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=wka4C0I9AHmla/9hz2xv6tr7JssuiUOCCkjNKLiy/aQ=; b=UWCJnMRV0G1bfnfg6KblGOImkm5brVsttuwhIiv+Zu4lUj1sOsucJL7ijO0ll53edg uOHb7E1D11cCKvlLy6M1DaHMg+sCE/6Z7u/SJHixxYKsyBRvIsSrQWF37YlfuFYbgm0A osrow6ctzBf5F1LeczUIjtPAbyfhSvaLQeAQdK1y4fVn7Ylg2yN3ia0SU0WpgWITYI24 vjQGAOkL00c+jxghHA1OykNNFKuAH0blSrZer3iMENUI+ENekTTIbrzNlefUER4s8vjl /u4iTMZwkTYe+gCjsCyjWWGHPMaPM5AQH3GinWebkK5l9dT5Dm8fLDwBVwVEIGJoTb55 A/LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528944; x=1692133744; 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=wka4C0I9AHmla/9hz2xv6tr7JssuiUOCCkjNKLiy/aQ=; b=j0yuZAJvWPwlPYP2vaKqHjOSaR3ZErZBq80X2mHXN1v9/0I0WHJTlprf1lGZUcoaiG 3yyuRihFn8M84qcrzGQIhoxeRAv9uqIgTyf99ZeA0FffV9pd2nNCHGCN502MRfOHewWj PX6ttj0Ir8DdVhX65TpY4SPT/Nta+ue5lRatX5o5wty6TFpJO54qKnnsLcTzUlBrU0Lz TuAQk72q3WBufauuJtYX5VEPKKBms+ERokmYI3Aa8595tZOM8IxwgX7xSafVP6AYQa+0 SrNFFTz0lnw+732eaq438qJVbHxoscqrUOIJBJHiya/pUZLfIQGE9R7owX543LT3tXSf 2p9Q== X-Gm-Message-State: AOJu0YzrpbmmfTs7Z7EP5rOPnsVHyxaS1Sh/9sQgT5XH/mMcwt15pkSo 704DB/MU7pBE8l24wkfWOUHgAk6b52XYG54iZSw= X-Received: by 2002:a05:6808:aab:b0:3a4:ccf:6a63 with SMTP id r11-20020a0568080aab00b003a40ccf6a63mr851274oij.55.1691528944376; Tue, 08 Aug 2023 14:09:04 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:03 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 07/14] linux-user: Do not adjust image mapping for host page size Date: Tue, 8 Aug 2023 14:08:49 -0700 Message-Id: <20230808210856.95568-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::c33; envelope-from=richard.henderson@linaro.org; helo=mail-oo1-xc33.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 Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- 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 Tue Aug 8 21:08:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711573 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2881492ltf; Tue, 8 Aug 2023 14:09:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE/ezI13Cz2DCHfduIHJ1FkYmZPyAY6r2IqwPa4C9u/2y22Ng0pVJdE5pOqZcw7BmglITin X-Received: by 2002:a05:6214:2aa:b0:63c:ee13:4fed with SMTP id m10-20020a05621402aa00b0063cee134fedmr535646qvv.39.1691528987886; Tue, 08 Aug 2023 14:09:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691528987; cv=none; d=google.com; s=arc-20160816; b=fKslK82qabMzJsR9nXJvpkjpNDHdRsX2tAsu/QKUmFuQ39gOn7c5zrSz3iOogVe3v5 n4eeTgH04b8oVuCvJ2wCfgAnPRbv7lXWSzYGnReuWKawOM1qDI+PTYQVNRIQOX90pXW1 gzn6Kh4Um3V/m1dJmRXcsqCMr2RCpkn05VtF0OUxsuIiEGka3UqmDV60ZwaPF57Ih1wG VGMbeVxvYgD8wTK+JmBbCtzVcYmNgWqv4oyZTtcSOGY9S462+mmc5ks91BEApvaf/K9u XCvOMjppQB8dPraUKXaSiJiqANknbimkiLuqLhEA8TFBfAo+0QYmr5DY+Ndk0NL43SZC OL+A== 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=cSqlJaXSE2kMrCNQmu7hR6r1H+eIl/aIUEieOM1aKCY=; fh=e1NRMjxVMtV0F5wYJj2s+gr60lsrqkDmE/ulJ4pjdtQ=; b=h+3GoJ1Hv035ezTKV52QCW4JRRfGkuyxY8mv5FX78XgG5SkipvAZ/P9lrEgG/4JamH qP012bfjYi7l+HT/pC/nGlFZiJQaTl9zHOOW7bZnnU9hBhEj7oF6LOK3PYZlPklaGPIn oiIQncybSKxiSPzO3lqB6K3I0b5uYFU+6qEO1lgSxr+u4HWkm6LjEJ8R4G1XKlRgdG0U InsDdPGTsgEB48Iv7lckf/98rLmROEC4SiNKpb5tWb1A/YGM7konz/WLqSRHWse0HsKB CihgkxsNETVc9xo/r+D+6g8csDdyBe0EOGnCRLEPhc5PDEB+MhRnR2DF9Hxd/a9p6gso By+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jDpzr2Bu; 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 j22-20020a05620a411600b0076cd1fb2a93si7010532qko.763.2023.08.08.14.09.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:09:47 -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=jDpzr2Bu; 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 1qTTx0-0007iJ-Nu; Tue, 08 Aug 2023 17:09:10 -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 1qTTwy-0007hK-DM for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:08 -0400 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTww-0005wh-La for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:08 -0400 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2693640dfa2so2057220a91.3 for ; Tue, 08 Aug 2023 14:09:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528945; x=1692133745; 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=cSqlJaXSE2kMrCNQmu7hR6r1H+eIl/aIUEieOM1aKCY=; b=jDpzr2BuiFX/zR7wyE3kyMfnzy+78UMnhmdUFZu6yRECO4gvZPvcRug3D+f57HxNki xOVMYEf5Fybxg5MwCRsO0At0qbtQ7C09gCqzX5IjspDNQuSjBO6nW9Yrt5u3Gk/MKSXJ DPpddx/HPs5sUsOhka6HQPGckHc6CWpios23Ws6H8ufSVUFU9U3jKbknQdU1HbSXAob4 SEtS+WrWIbj68y4ac8fcFZFaBKZznOgUnGqKp8DuR9+nVrrGitv1zqzTsBINK5aNgMNY 7lzbhpMFegnlm+am+zrIPXRQVz1HurHhdDm0U0qjNO2SM/QhGSjRWdFE/ce8P3lG6hgi PFQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528945; x=1692133745; 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=cSqlJaXSE2kMrCNQmu7hR6r1H+eIl/aIUEieOM1aKCY=; b=Db60ncksprBOXvvmSckA5Fc9ZdDnVOWI0fAUGNUlsUJGIFf6fCnXGU44KRg6OX4YnR kXpPpSMTuT66wsss+lSLTJ6EquKPaxfY1OrecVs9M27Y4dZ/470BBDcB547CflvD2rQu PYjjTSj7gocpd/K80FKDysMl3WMSFoY78u0Q9wh9qryqiALRvy+GhqueklIoaS6Amq6P LSdhw883c2u3s4RVshvyrgx4qgRf9WojC2dIVcvnaq0ZJRDGY81W0di0oGwY3NLJE34L jorr9suGLvxpveudE8BT6Y2p/nYjv0ebu5O5Lj2UrjToY7+utmLkUgla0T/n7IK7xH9K m63g== X-Gm-Message-State: AOJu0YwQRu5M+9hoKOOFZmfFbJgwUBYo0Earm88rMTAYLPw9WQiWYOMb ZgKTrh2Bl7OZOH2e9ua0Fee5O+BOGl5CNWgNhBg= X-Received: by 2002:a17:90a:ea0a:b0:268:b66b:d9f6 with SMTP id w10-20020a17090aea0a00b00268b66bd9f6mr642944pjy.18.1691528945302; Tue, 08 Aug 2023 14:09:05 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:04 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki Subject: [PULL 08/14] linux-user: Do not adjust zero_bss for host page size Date: Tue, 8 Aug 2023 14:08:50 -0700 Message-Id: <20230808210856.95568-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102a; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102a.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 | 63 +++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 964b21f997..881fdeb157 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2211,47 +2211,37 @@ 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) +/** + * zero_bss: + * + * Map and zero the bss. We need to explicitly zero any fractional pages + * after the data section (i.e. bss). Return false on mapping failure. + */ +static bool 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); - } + return align_bss >= end_bss || + target_mmap(align_bss, end_bss - align_bss, prot, + MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) != -1; } #if defined(TARGET_ARM) @@ -3255,8 +3245,9 @@ static void load_elf_image(const char *image_name, int image_fd, /* * 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); + if (eppnt->p_filesz < eppnt->p_memsz && + !zero_bss(vaddr_ef, vaddr_em, elf_prot)) { + goto exit_mmap; } } else if (eppnt->p_memsz != 0) { vaddr_len = eppnt->p_memsz + vaddr_po; From patchwork Tue Aug 8 21:08:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711580 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882111ltf; Tue, 8 Aug 2023 14:10:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHIDXNwRDYrFH/UtmCoMfJU3RyZc80Fk2flZs+SnGzZ6ZWxijG8tM6WCtKnpUrVTySRRM9/ X-Received: by 2002:a05:622a:87:b0:403:66f7:ae66 with SMTP id o7-20020a05622a008700b0040366f7ae66mr1273273qtw.13.1691529059207; Tue, 08 Aug 2023 14:10:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529059; cv=none; d=google.com; s=arc-20160816; b=MkC1Pa3p3G8oCVUGrsMhdL0ooNl1a40IvpHyUrzlSsgJwZdI4bf6I2p8u66HIEzq1C nWAu0Tw8GVvdqErzNqMCovTlClSvuMfKte2NXOf8tMVjPxgHjt+y7iDXLVZuStn9m9DC zzsQgN+KzYyhtEZgl6e6z875Yac0vRP7DFKb8CdF5bOun7uogDQd5bFrtMsDteAY+dI2 eceP5duiDZJaaZ03+3gb/yptowbNa5FxlLx26uM3qHnSumX3zIfMFNB4qa8eJvksxSMe gZpZF372PPY1ZvRpc2tFQX/dAz5WJafedIi2fkTV6xSs+RD924rFIAdl00JuWysk66pf qgNw== 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=mVgimCTGuUjAfzzKq6Eqz3lfGX5YOiTLny3wMx0dHp4=; fh=9PWVySHAdMbaJwSH2JIu1yvgc2H4HTtBBPJgaJCWFJQ=; b=gYKy9v9tneE6Omjd+GmhlDChH7GjSObxTSCjKWCXLSkg/MUMCZGQRuwxqpyTOWEtb7 CZK7M5vb96Llle9gICz3Cn3oIA93/CT0sGBV46PI9W7Cs0OQAi1TeKVQw+x3VzidZWjQ CjIOtKtX7hHQC1hxtIF0I/Gso/CRxJkhr4IaFpn6xJsFlkdVlJmgHTOjLvbXi2PAZnKF v6P5/keW4VYF2FT6AOAr1Ic3dU75VHS5zMOfhjkfBLCnjx8dQbeRMtWUIhX1kpwY9ETM K667FGSrOPQ5AzzxdKnPMvyRE6K3OZtsiXqp0IoESyPwip+yod+iiAZ4fhGa4UOgivws krlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TnDVQbCc; 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 d20-20020a05622a05d400b00403b0f6661asi7303091qtb.94.2023.08.08.14.10.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:10:59 -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=TnDVQbCc; 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 1qTTx0-0007iC-9m; Tue, 08 Aug 2023 17:09:10 -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 1qTTwz-0007hZ-6R for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:09 -0400 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwx-0005ws-Bp for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:08 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1bc8a2f71eeso6595805ad.0 for ; Tue, 08 Aug 2023 14:09:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528946; x=1692133746; 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=mVgimCTGuUjAfzzKq6Eqz3lfGX5YOiTLny3wMx0dHp4=; b=TnDVQbCcwnxs52vKgqoQ3E9lHmxLc+ApFqOoz6XxH+pysU5kSNF2akLXTgw9d6Opxt l7boMNpddTX0+0BkRZ41RdTZsR8aW48Yj2Dqul9fVP2RNLGuZNyrJD4jtxiwdqy/CrSQ XmpQ/iRsYK/xHlv4/1//kHgSn1pA/9PT+kgXlm1f/Pb/YScQZFIKLih4hXupSfMXPiaP fE+g/RnPyk8wqjgHq1isFiIs/tT0ULHwsDONDucNupIZMngqSKRKDPMj5odeIZBWMqnU CFL54H23gH8RqPSb+l3glbGcKel4xAQ5LsDbHYqzs0xBAF7eEw6wISoCQptPWP/D5Wgh Nwyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528946; x=1692133746; 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=mVgimCTGuUjAfzzKq6Eqz3lfGX5YOiTLny3wMx0dHp4=; b=cdDI+NlicomLTJL2mh2AK5KLwLVebBJLOAQvcqqzZN0Fnqq8nlARhx5JrvtQxklu8d EBY+sueHPKEttnYbCpzdNJX2dQlYoxAHDjweH8RgNjYXHtS/9grQrsE9pioIFN/b1bjh Br71Oofr0I1wxyrpPnd8D/JDBJ/EVFbIOcC/PWyOCPEJf8CYk5gCHBNBoXXYYH4dW4Sa //Za75vqhhCPbl/S9iD9B9OPB0TYK7WVUDmH5G+QPIZ/mM6MqtJc4IjOz9LGADIabhyc BE3iZH5R92SOkV8dew9RfrBydPf6ZZh+WcZVbz5uDQClkbflQqIbe0ilqaoCO9tsyQ79 rXBA== X-Gm-Message-State: AOJu0YwcMASGe7x1kbYm6abwSRKt2WVV82t00VbfiDJH+wFf4cpiLYGy okpGGSDOLoFFtQYWR/KjautolxqhcT0SMYhaY+c= X-Received: by 2002:a17:902:d904:b0:1b8:3dec:48de with SMTP id c4-20020a170902d90400b001b83dec48demr758807plz.47.1691528946122; Tue, 08 Aug 2023 14:09:06 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:05 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 09/14] linux-user: Use zero_bss for PT_LOAD with no file contents too Date: Tue, 8 Aug 2023 14:08:51 -0700 Message-Id: <20230808210856.95568-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x631.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 Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- linux-user/elfload.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 881fdeb157..e72497c4b4 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3208,7 +3208,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) { @@ -3233,31 +3233,18 @@ 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)) { - goto exit_mmap; - } - } 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)) { + goto exit_mmap; } /* Find the full program boundaries. */ From patchwork Tue Aug 8 21:08: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: 711583 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882288ltf; Tue, 8 Aug 2023 14:11:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHh0fVpIQ4S0aAYLsWAUGg7BJnxTqDUzaW7+bsE/YjibxWOZky9EXuhk3y0Zxw7UbPbYjwK X-Received: by 2002:a05:6358:93a3:b0:135:57d0:d171 with SMTP id h35-20020a05635893a300b0013557d0d171mr620174rwb.15.1691529078129; Tue, 08 Aug 2023 14:11:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529078; cv=none; d=google.com; s=arc-20160816; b=TjNvIu2riSRI+xUavlWaT+PVM9hw4dhHcEWRMtw9pSsO70aQ76NCdjOQrDuvdNDIN2 L7MbRFiBP33d7RYBfnR/PQ4Rxqo6V/x2GPcaPtxNW1dX46xxxydsnUvR6PLAhkm3CdEh nYcD+P4snoQZxgMdWm4te5xQiCELLuCAk1QtD/twfNU66P/35FUHx9Z+9cVdtk7dtw8p Rr20Vpw4S9DGK67UTJesibnrQRABVvaaGO1VyYRsab/+3O+rYgNCfQ1Mnnni97hfkDRt 3VZjDSItT27IkPwu1cNGGOVq0j9EWWeT3vpvQEvAixAT3gCZOu4IS/ssVWiuUTlc3utm 7gjw== 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:to:from :dkim-signature; bh=62eS3zAvxSE4Zm7HV6kd3RVw1vjOHnDGUE6VLCjXmBI=; fh=oYAd+8Lj9FKlReeF9hgENQXa3P4LhBP5C4njRBbobDA=; b=VywWZnU1Kk78IhPVcmn2X9ttO5FxZtU/X9CZyDJz9LHFeEIbS5PkHNw4F9wUjwhfoy c0VEwog1GWExU4MRm8RBP6pHNBJLN/rABEYWd9Bkg67fjcBonfeK/loHv6EsWNrWzIBi WNICrvD7SkqoCFw3cfZFFWGFaybVeDA7KcdvAd0r2KJJ8J7+20ubVbR4vmb+/b1X7esE SlwBKrIthjHM8oHDFd9vMoMLcfsiYUZ/yZT87lu0bc7skyr7zu/vyYbq6mdoKfIPnahf 7vrkWq1yAz8Voc91ydCYSBfg0dfZ853w1kbaCc0VF4S7s3nwXCAoP/NbtVvhj0rF8qte hSDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MKqVdguX; 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 t6-20020a0ce2c6000000b0063d3dbf77ecsi6763079qvl.40.2023.08.08.14.11.17 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:11:18 -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=MKqVdguX; 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 1qTTx2-0007jE-5z; Tue, 08 Aug 2023 17:09: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 1qTTx0-0007ia-T1 for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:10 -0400 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwy-0005x6-Hr for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:10 -0400 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-564b8e60ce9so2885600a12.2 for ; Tue, 08 Aug 2023 14:09:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528947; x=1692133747; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=62eS3zAvxSE4Zm7HV6kd3RVw1vjOHnDGUE6VLCjXmBI=; b=MKqVdguXvGKG2SwHzvS9xXJG7KUd+xhRNwgXGALsP0jE+fr3EeDs483Y8x6P4C196W cPjDjVCTtm91TyjASvkouuU2Zpu/aKX2/v+zenNTwk5xNN/ZYYkDkWmXyOHTSCz37VIe XfnTdcuy493nzujkPqY2CD7Flx6kfSKVFI8lHjtFAd/P8Xup+PeED8zDhlKyzxBrQz3q xuTlwEKwGgximjvUsWrt9L3zxkjCo3tVGsvaIyVAQyoeuzn6/bC2Rh4fVXsxTTKE+x2T uxN64mk+cmekAI4CVbC6+0wxa5ZufqBPLur2jxu51yQGVVURrVvfSRDb44XoQGlB4AYO WHyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528947; x=1692133747; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=62eS3zAvxSE4Zm7HV6kd3RVw1vjOHnDGUE6VLCjXmBI=; b=HJq3NZsUWY/e5Ucr5zvJUR2J/vJA9fUCopni4FPv3QT8eqHAeA47Rm63muPnHTBIyA pTBtqFcWrdez/TKxLhYGeta3yPeYxi06GrGH+jyTO6t06Lvrklk3FuEf5/zHbbaPs/s/ 3fViAsCXh0mg4bAaXAUP/IlSX6eiR2/3oiZkAjn+zVhaNHH+jl36GmsI9UeYlSUF7K7F 1XhYRI7PfO0FoToFjS+NWAHk4SMnHnL4SnpsgistRCkyEl878/yf3fph/eoMQ5prz1P2 Wzq1YQQ1t6t5J+Pw5W0LXfgUIUwtze3PsM9WsWeF+0xXiATGxfZvsE5eb2SRW7ggb2LY 4lWA== X-Gm-Message-State: AOJu0YxdLwIcZ7eDu500NDgj1QSXfb/0pEDNvqHzOJPheIB8eZX4GFdU DiDKJzOMmfpKzD0yt8L83SDiEYfEScTiVdMEKaA= X-Received: by 2002:a17:90b:1647:b0:259:10a8:2389 with SMTP id il7-20020a17090b164700b0025910a82389mr588192pjb.35.1691528947048; Tue, 08 Aug 2023 14:09:07 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:06 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PULL 10/14] util/selfmap: Rewrite using qemu/interval-tree.h Date: Tue, 8 Aug 2023 14:08:52 -0700 Message-Id: <20230808210856.95568-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52a; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52a.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 --- include/qemu/selfmap.h | 22 ++++---- linux-user/elfload.c | 14 +++-- linux-user/syscall.c | 15 +++--- util/selfmap.c | 114 +++++++++++++++++++++++++---------------- 4 files changed, 97 insertions(+), 68 deletions(-) diff --git a/include/qemu/selfmap.h b/include/qemu/selfmap.h index 3479a2a618..7d938945cb 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. + * Read /proc/self/maps and return a tree 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 e72497c4b4..fb137345f6 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2620,7 +2620,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; @@ -2638,12 +2639,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 Tue Aug 8 21:08: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: 711582 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882263ltf; Tue, 8 Aug 2023 14:11:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHKzViJwONMKUfAr3pF4PnfWfDbpgT+tsiZaDQdaT5TE6s25ZNT+5KKiauNm+q3xAB0k5JJ X-Received: by 2002:ac8:7dc4:0:b0:3f6:af5f:29a6 with SMTP id c4-20020ac87dc4000000b003f6af5f29a6mr1276636qte.33.1691529075291; Tue, 08 Aug 2023 14:11:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529075; cv=none; d=google.com; s=arc-20160816; b=gl/uEmLY8a6HUvWCjcQ9iuhuv6WtsGD2DGm57AB6J3OKG1+Dc9gWIdlrbPrehEXNCY 5QHGX4QCQyHlDQFyGIuL0NAHNWl8Q+T8gs3JTfX6KVSYfifTqQMQEpHiVeEpWaU30maq Rxh6M+3a99uHcn0XeltXQUnNLDLfWhHXLlhlfcxKQPcYemvm8mnTci7dApo9klt4Kzob qAtA6MkgRV1+H2P8E8qR4JNJT6n8LIbC/RMiBjLhkeo/r2uXve8oqTn+HCiiBLnCtmY4 ccCIp16tuQbcx/HU4dxI78HdU3R0KoOepuzbSOmr1WcrGLJocMxWLc8koIYFcrSXVkXa 4jjA== 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=DA7hmKIihrFnBJ2BJ8RPoXnfnYlRYqVeJaGCMsToUv4=; fh=ZuLSLtzpwbOj79Lm6HQkAzCl7AYBeBRT4Q/1zeFcrQg=; b=BLZ+crIBJKMubBVwm8spR0jCosGN0HfBWYmjeZOycrU/FFrJGCtMFIwKWz7MWjn4vv 2DpaxUdQkOvT7i+pMMidxi6jf9fiMFljYgXkobJdo3EY+vxVH0mi2UsChugRuc7UdDrC ayng0LBUltG8bQcoHfw9DRs8EzyKjiwCzK89OxPpk2ZXbNafXA+EveraiblEYGPlmkxL s8VQQ5FxAuB9krpDmxubyJT7r/atzLoizwXNOkz/KIVD0RYTlQ84nzm8d01ACka/zSrv 59a5a3wzmezcyUoeHjA+o7hy+XnuhKsIKcAW7buC317gHfKjeZXILKy51U9iDFz0sh2D S6+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=lRkrmR24; 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 b13-20020ac85bcd000000b0040cb20e8fd3si6431428qtb.768.2023.08.08.14.11.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:11:15 -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=lRkrmR24; 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 1qTTx2-0007jl-RK; Tue, 08 Aug 2023 17:09: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 1qTTx0-0007ie-VV for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:10 -0400 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwz-0005xL-8X for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:10 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1bb7b8390e8so39599765ad.2 for ; Tue, 08 Aug 2023 14:09:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528948; x=1692133748; 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=DA7hmKIihrFnBJ2BJ8RPoXnfnYlRYqVeJaGCMsToUv4=; b=lRkrmR244c2OP8DOAY8VONJyqtTvR+JaD4rpMJFCLV8I8PaxMJ33J8TutC+GPJ3/Jb JHw4pgUg1FAV7NV9hmMQBxO4FE/3XgTes0pXrhqWqsxdXFTS3n6kgHLTFKsXvIKO9pVA pqCDDwEhtkVXCjXRyjXgPbBElXvOIbPUcAevFrfxTx1djhMU0Oiax3zjf2jDNcx85UZl ZrvDPMNxKHGR0yt4lLaiPlMtMUIpm2o40X8J/BlgwlIn6EUCM1XSPgb1tvZjPsSZsCjT lCobBAZ6+ZPzSUruy0zfq2gJXLj6PrYCSl83gsSuz95KmMEc5l5AdeDefQcFFNQcgqkh ks4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528948; x=1692133748; 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=DA7hmKIihrFnBJ2BJ8RPoXnfnYlRYqVeJaGCMsToUv4=; b=JJyj0Y9sfhdpO+3BeRQCXAEolanYgt/RNRABDyyaEotIpLiwXYvKYdmuXG+z+MSedH jQ5S4GfBLJev9dL8ongpuGy5TOCC6lBystGO8rlh7Va1s3bRelD7iJvhiAWvncYvlCPr Si9RIlvA9b0NxdvPoUQ85zmVqXrg7eAPvaTU0G/zMUVvbkf3c3Xus6LIoHeLmcpsZYCE uzEDAznppdLrrzfkIWh561L9xzoFFq9SHGVuIuZvxjXwTFIVWvWCJsqAreJ3ruruuffn YkcIuzwfvR8uAO9Mz2E/K43e2vN/ACjlazA+Qvfgu0uapupX/RmKs+3uoEQEU64zX8Tr B2xw== X-Gm-Message-State: AOJu0YxYNiedZ9Qm20KgGgq/GnywODDddUDJMO2y5AVToGImoxwz+IgB EPwdqcg9FcSIOErqxVqc7Th2L0I8cRnEORre97M= X-Received: by 2002:a17:902:6b49:b0:1b9:f7dc:b4f6 with SMTP id g9-20020a1709026b4900b001b9f7dcb4f6mr760277plt.44.1691528947949; Tue, 08 Aug 2023 14:09:07 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:07 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 11/14] linux-user: Remove duplicate CPU_LOG_PAGE from probe_guest_base Date: Tue, 8 Aug 2023 14:08:53 -0700 Message-Id: <20230808210856.95568-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x631.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. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- linux-user/elfload.c | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index fb137345f6..c14139a5fc 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2560,9 +2560,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); } /** @@ -2605,9 +2602,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; } } @@ -2689,13 +2683,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; } @@ -2747,9 +2734,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) @@ -2807,9 +2791,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 Tue Aug 8 21:08:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711577 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2881918ltf; Tue, 8 Aug 2023 14:10:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGmh9RaSxzUJkuChlYD++i/ji7srllyEQ30uvcFBzYW3yXFwCoiKm260xIRQP+f+wf3zcYP X-Received: by 2002:ac8:5b41:0:b0:403:b4bd:8052 with SMTP id n1-20020ac85b41000000b00403b4bd8052mr15827500qtw.15.1691529038881; Tue, 08 Aug 2023 14:10:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529038; cv=none; d=google.com; s=arc-20160816; b=t5BaNai7fEuGNbVlDKpUVu0Y/FephAxmudvKCT1unGkValPxZ4+/ZikOasgzKyO1VH brQgbHgWu6hNZLwnqKS2bSDYVhsRX083f0zm4Mm3hC8+lVvbddZuzfjOYCGwLWmnxHjn G7T3OsUweznWK9eetFX77BQNvsu/bbyVtuf45QqXzbea06IMrIjghCIENSRtbIUbrCzr 8x7DnJFAsSs4oKIMfoiXJHdB3ePMs/SFwvKfJbKFAEfJSm5pjoeNRfR7DPVJR3IIt+hJ gSS4ZM5p0qO0mUFHgsa5IKQ4EzvVPTcJQK2bzsGZ3nDOv47Nx1j8BfgeNDMVtzAXcy5Q jwGg== 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=1TQ+ng9YK2iHiUzqlLMSJnh2uowz6Q550xji3/gr0BY=; fh=ZuLSLtzpwbOj79Lm6HQkAzCl7AYBeBRT4Q/1zeFcrQg=; b=ex1i21ju+t7LIkyMbNTf0LbZjVObI9AqhUzYpg5vRYS1NM2Z3VYYuvQG43bxtkaK5b WFWlJ0XmndCVBXg5brFvjHGyCfpcBL7Kg6jINwjQk5Oj8UET+RzoOFvIpFZjH2dBkiiL maIBi1PQvGUKDxTwfPTKaPS00TkrVsPQ3Me1tXWs9ljcawV0Unk92PUve69IhKSIBIeL HAzocdcQhwWzuohX5nGstSftAoHUsLXAnqd9FTU/0LazScmcS4AjY99gFJWMQ4oQbycP GcYFcbqlQp2NJW5qAY8UXeO8wjDt59FE3X6gNSYHXHGsE/WcsKgtJYkXM214NdCw244G SUGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eEWbJJCd; 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 19-20020ac85713000000b0040354027d63si6879190qtw.91.2023.08.08.14.10.38 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:10:38 -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=eEWbJJCd; 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 1qTTx3-0007jw-DE; Tue, 08 Aug 2023 17:09:13 -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 1qTTx2-0007jN-8C for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:12 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTx0-0005xU-9m for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:11 -0400 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2681223aaacso212544a91.0 for ; Tue, 08 Aug 2023 14:09:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528949; x=1692133749; 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=1TQ+ng9YK2iHiUzqlLMSJnh2uowz6Q550xji3/gr0BY=; b=eEWbJJCdoaCUkmRIhkYdrZjrWCm1uUf6acv38Rml9ApkhCqDcPWr2Sh0fDqO3HtC5F KLHz28TiSXtlcSY+/3ZZjZ/8gRx/DoFEaCnJh3tik4UXSAlSpYH9DNilITb9dCkn62wq COUAyIKGIWLWBuoSLN5QzIf21lkM63mht5v6Me6LSp9xd83gOxf3cXmLd4yktlOBSjIx UdvJo0FX35q1YdX0XzBr9ssbVyPgAsYbYFlRSGS4kriJHGxvcw0uq76S1Hb39KiteNBa PgaxZJgfKtguGylHId7CkgRwQwPfYt91AbtMxuePAhIgVK/3usVH1lUrPcHTIa25WXb6 cqDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528949; x=1692133749; 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=1TQ+ng9YK2iHiUzqlLMSJnh2uowz6Q550xji3/gr0BY=; b=aBLu18lhNvCDqO79C9nzbFQYP9XnB5rns76Dgifh65Q1t7X0lh44nEQg+jlaxNWcEV QlR6KQox5EnoDyELUJwaYuPd0WUaaV+OQK3i1kRPpTLBcGSxgGoLNbCOf5FJ0sN+fR1c /W+SmSC0UU3b3rqRfkCZSFtt7Z0BWvjs+LZ87QzL47WmbLlNTQhf4D5NL314p6gmkvBJ 4HPwIHEsGtlt6vE+N0tPlcKyKjrMrnIWGCK3kkTJyIZvgJWOXg4uaYWA99pRE1WAsAf/ eH2zRc/lPzGyjHxk/tqFDvPULkIHmH+apWJv/61umhWxuUqTTSlqrZ2dnlMtqQt42fi7 3HfA== X-Gm-Message-State: AOJu0Yzu40I5eAR/A0uNpIfmLv2u/nChCU06xo5v5OfWZdu9lh+WG7bT TzTm1kEd7zr9Xw6QdNZpxTEk/ohzsvUprC34OKA= X-Received: by 2002:a17:90b:1990:b0:268:8ff2:5c0d with SMTP id mv16-20020a17090b199000b002688ff25c0dmr1110601pjb.17.1691528948854; Tue, 08 Aug 2023 14:09:08 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 12/14] linux-user: Consolidate guest bounds check in probe_guest_base Date: Tue, 8 Aug 2023 14:08:54 -0700 Message-Id: <20230808210856.95568-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102f.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. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- 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 c14139a5fc..06d81f83b1 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2525,25 +2525,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. @@ -2694,13 +2675,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) { /* @@ -2766,13 +2740,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); @@ -2799,6 +2766,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 Tue Aug 8 21:08:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711578 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882033ltf; Tue, 8 Aug 2023 14:10:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZq+afy7By/G8QeXVHngCnvw/JwtdSKm8R2tUSdvb3PJ45wH9maqSuwRb8/zzyPFuaqZQM X-Received: by 2002:a05:620a:2182:b0:76c:cd44:bba6 with SMTP id g2-20020a05620a218200b0076ccd44bba6mr820566qka.78.1691529049604; Tue, 08 Aug 2023 14:10:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529049; cv=none; d=google.com; s=arc-20160816; b=gOSXx2yOfX8967fe+jcSuuyfD+WOtsmrRZjdbACfj17cglW5c0SzP4S3larbLVXsYk sG5HaB/RYB/+7M9HFzwld56SL3JsqWBp9lALBA4YO95rfOtOQNJPzq9UEFamhxyamqSp o/mJZZvJklCTGoWZzGvN5r+Zej0eXHYJ+7cnpENRp6OjItwHi8zvN+QAPVH8nlsu6PkS zhY6YAD295EDo7T59gTeuJDjLDIUHfXWNXGLmRCvwIdvbdm9QzE9vs5Ca26Jnm+jXpTM iy6wFeSaU1h6Srf4h75DV5vOQ16Zunec6R8UvgcHs+42AwmeUUqrlUTf6/fKe4yEYkSv VFtQ== 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=XWVD9ChkStM4MlCzbWF7GaEM5UjeZi9WpfIZ1Gpn9gU=; fh=ZuLSLtzpwbOj79Lm6HQkAzCl7AYBeBRT4Q/1zeFcrQg=; b=zOcfuX0I9m7Ln8xDi+4Mx9aa4qTJhsnfPsn9Ojj9ANgSDxE2zIZE5iPi8K9+DgeVgP 5EKl/FIej+XaeS9NM2St5FLLrUdSbTixo33/titJ6w1YDfp+yYWEoq6MKTR/6/J6dcic 72M3vdttzm9wIEA+l4ci7VDJhIfy0hY4TZxvO+7lQMvcFIrIncbQsPkfbFEfFGqDPh+/ yUXYYsskoxBfi2Pq31RcN7WQnn8jhGTJVug0yhbKMf3ZX9jIvEQZ8EY/OQTmKY9rNadt bWosSpt9T8NssoWgAahxippeslrhKI1TIYwDy2hxbI0QAKiWYIf6aQ0vXSZYy+gI/Oix unJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="xRQ/pWmy"; 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 w13-20020a05620a424d00b00766f9fc0433si6639451qko.523.2023.08.08.14.10.49 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:10:49 -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="xRQ/pWmy"; 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 1qTTx4-0007kI-6I; Tue, 08 Aug 2023 17:09:14 -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 1qTTx2-0007jn-US for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:12 -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 1qTTx1-0005xd-17 for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:12 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1bb84194bf3so40473955ad.3 for ; Tue, 08 Aug 2023 14:09:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528950; x=1692133750; 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=XWVD9ChkStM4MlCzbWF7GaEM5UjeZi9WpfIZ1Gpn9gU=; b=xRQ/pWmylwqPQAdvAUM3tOnrGeDcpeQcEZxr1yQ9BkO0ER+nEfxtwIpCq0up0gzzem kFX8nUugi97y7EeEhgX3e0/h6LiBVaIUaO/YbXpEAPAUOW+O9u1tN6NK5YOyBtM0GoSI zhslAW+Aa5rvUE9IXG/qeTFXXSV5ClCCgk/Q3arNpWgCgQHXHs7LKq0MWoPXIPfaJxyG Ar+frlrH84kAcMzNboDV2EcEDsHKJltWDpK27oFG2jIgO26TXkUXmBK4uMCsgkuq80YN idMGSM6bNXS4gx/iH/FuHXRIr7inUrS9/w0N3mtPRo0ndKlB1Yen88z78YIz4EKD3G81 dt9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528950; x=1692133750; 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=XWVD9ChkStM4MlCzbWF7GaEM5UjeZi9WpfIZ1Gpn9gU=; b=PysElt9nO3ZfEIoBYQfO8kct4BbMDYb8NrCp3yPqUcW+9JG/+0bDymiV9QLy3EW7VC pgr/WbyNKQFREPZapz3xky26EgZAdXVpatAj7Yw5CZvNgxkEw69FfqS79avRF/gBoIcQ +V+MuR8w+f9Rcmpv8RquuK1ZtKtkVRBlU+6vqmb7Ww2nx3E+fS8Q17cl0lRtjOBS5WPX LQ9LsGlmr8QQCFyX4jw3aAX6GRQbpwJzP0+0IGJUi+7i2o5tTjM5oqM5RcrhgYGKZ3Qh SH8RalroTdWcVhDT7R33PqcmaZiJNpWYFQXBwxj2bHNmDWnSzFHSYsroRNVO4FtZCbgF P1qA== X-Gm-Message-State: AOJu0YylnqejLS8iCNUrfSz6D4JeYoZSTvwfnjIH/5DdE0KRa2qFMNX4 Dg0bcvAVI9lTMK6ijxj6tPZ47X2ITZCGZ9NZ9x4= X-Received: by 2002:a17:902:7795:b0:1b3:b3c5:1d1f with SMTP id o21-20020a170902779500b001b3b3c51d1fmr821569pll.8.1691528949724; Tue, 08 Aug 2023 14:09:09 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:09 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 13/14] linux-user: Rewrite fixed probe_guest_base Date: Tue, 8 Aug 2023 14:08:55 -0700 Message-Id: <20230808210856.95568-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-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 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. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- linux-user/elfload.c | 188 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 161 insertions(+), 27 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 06d81f83b1..a5f9dd5b31 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2504,6 +2504,157 @@ 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 + * @addr_last: host last address + * @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; +} 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 " @@ -2512,33 +2663,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); } } @@ -2784,7 +2923,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) { @@ -2795,13 +2934,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 Tue Aug 8 21:08:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711585 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882316ltf; Tue, 8 Aug 2023 14:11:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH2ABBxK1Y2xiVh2ZnGbJwyFd6RKOiW19buoJocc0bXk//WIyO/y/KA07oWNzDL3A9He6yM X-Received: by 2002:a05:6358:341d:b0:137:89ac:20d4 with SMTP id h29-20020a056358341d00b0013789ac20d4mr620696rwd.18.1691529079687; Tue, 08 Aug 2023 14:11:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529079; cv=none; d=google.com; s=arc-20160816; b=iBd9jeVBIB35c7xD1zXw67wMx2NEq3Xq31CyzFVfsfqFspM/u3+zOU6CPFJb77ZDsZ 7N+xDOXg1P07RbbXOEuuBuujToyhPydem1h9JMYO6sRzbu3UZEFcMjeICHxJ5ex5M6dL besWPQlX6aFBtrdu2oq1cn+rZalJjWrPXkvmIhKVWcuhahD2p/C0qy2wfuVPkUuqGcXB 3TjzOWftf57umxuWCZ+av/veKceMV6AekI/2JJCbMRs46mgHZhnRumBEoOTq5AjbVPq0 7vYSmd6eam1wcB4W30pTbKsWpsBlpLTa3mURHDJV9OLLzPDrj04M7sMCfR2WRj+i4lPA YiDA== 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=u2nTqtcYhBLl6XhGtj8nvtR1cExXc+DDI9B9XR2Hu/Y=; fh=ZuLSLtzpwbOj79Lm6HQkAzCl7AYBeBRT4Q/1zeFcrQg=; b=JWFAibN6I/I7hn4fDDqJLSNQLqoMCfjc0lXa7GOvHnQQPH+9JfFC7sAEStx/vqbAGU SPpdKeTYPDftFqAumuDbPwjFMbSCq5EAMkVwnvn1w6BuEGyHGGlqv48q6mg82m875i5B j1j/TjGVFMsiwTOD3g/1fopul3wJzYe+YMHaPPpuwcVBfDShMR6tfBrp2uRQ1jgx2s0h NgZjOwumeegKxOJuIYyeHmZ7RvewKntnnK1IbX2gbDpooRAPKQCkkOEmFtetyQU4BUSY 8ms3ZZI4tA/8aKjDXV3i2BgN+nPw+w8kzOZ6K0NdTAQMarHA3bLy48PQ2r0tPNHR03i+ ElMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J9Q9fhQf; 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 c21-20020ac85a95000000b004039e75fe53si6858944qtc.250.2023.08.08.14.11.19 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:11: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=J9Q9fhQf; 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 1qTTx5-0007ki-BG; Tue, 08 Aug 2023 17:09:15 -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 1qTTx4-0007kJ-66 for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09: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 1qTTx1-0005xy-Vi for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:13 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1bc02bd4eafso52337615ad.1 for ; Tue, 08 Aug 2023 14:09:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528951; x=1692133751; 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=u2nTqtcYhBLl6XhGtj8nvtR1cExXc+DDI9B9XR2Hu/Y=; b=J9Q9fhQfLebBFCuIEtC/cEZCSJPNvEbGbtoJvjE1kzh/TVRaQ6A0q2QaV4jlJqAdWC ZvhUDGZDmJxalxxKBqkCXrA1WVMCZzeLnvn2IK9BVqq/iGXg96eg5OzFDQ0sNqthLGNY hTzQrLnqNwM+U1UClmQwv+oIfqmEbfPNbboaAONp6h/RBUvl6rKblFOrcD9SO0bBlz+6 VvtuxMJj+NAmZoXIxYvJB3iulG5n8ZCIuflOuSUhsgd6QDpZIY86UUOqEKRIDaUldaYQ D0aNNdC817E0fUX4ygQQ3n62gyS/l5pQGH3y6JB4Y7z9KASVUUusdKBSnCeTkcbxoUcL jIpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528951; x=1692133751; 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=u2nTqtcYhBLl6XhGtj8nvtR1cExXc+DDI9B9XR2Hu/Y=; b=dazUSnN+jLtRRF8OwvVGi/8mJjYwh3Lx+9De9jtUUP1Z6uubYyiQdUCc7bEtLuDO9H tqVy36ZJbtno7JXdRkbQCS+RoIGVEyfPrd9Z42Oly+rU85lCdoQhVSww2OHR/najUOeV r8Pp7lte/nRpd43A6UOxd27bf4751EdC1AfOHKOMp1EoPlQdFQMNpJGE/ATO9Dzci53I w9zb9x98dn63pSQBv79lqHvW+8ccyL0hyaX2jaxiPei6vh+/WuW4NWlc6OfT8VKBT354 aiIFgtue38SSnj8UbmVlLmchcIVOMCp6onV5pIjsE20yIJsPmFHg7jV52dxBzubA4Aau shRw== X-Gm-Message-State: AOJu0Yw50gil53v9YcKovaqDO4bnIh8cwFaC0mfHpsWtpr++W+6/9NDt Q3BbWa35WYfZ9ZI1h0E2pvu/uPepzDpKfsZZFMk= X-Received: by 2002:a17:903:32c2:b0:1bb:9b29:20d9 with SMTP id i2-20020a17090332c200b001bb9b2920d9mr897575plr.20.1691528950647; Tue, 08 Aug 2023 14:09:10 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:10 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 14/14] linux-user: Rewrite non-fixed probe_guest_base Date: Tue, 8 Aug 2023 14:08:56 -0700 Message-Id: <20230808210856.95568-15-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-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 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). Tested-by: Alex Bennée Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- 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 a5f9dd5b31..ac03beb01b 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2683,220 +2683,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 mmap_min_addr. */ + return mmap_min_addr - 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, @@ -2924,12 +2847,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. */