From patchwork Sun Jul 9 18:29:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 700893 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp4829110wrs; Sun, 9 Jul 2023 11:38:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlEJ4KV4duKjtUqS3RqWRDuWgMGk9JLpMf5ZDgKavHOt/jOUSRai4PnW7PNLjw4Mmb1aZ1Kz X-Received: by 2002:a0c:c403:0:b0:635:e0dd:db42 with SMTP id r3-20020a0cc403000000b00635e0dddb42mr8373173qvi.27.1688927924474; Sun, 09 Jul 2023 11:38:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688927924; cv=none; d=google.com; s=arc-20160816; b=CdtoMrSpazlD3322HPE813V6ibupFaCvLR4GHZJxtUrFd/0M7210Ck5pm7Dj7jnZ30 OxGCpInrK4rzHdGf4ghMc/asDyH1aQMxInn3ZxsQjQ/fWQQGzOm18uwo9b2NRfoWglGU wnesZcl0UgShnJ48DvHhRvFWQ79QlXGxFVnwc2aXcrSBKbuhIQlB6MdpKr1j8Xcmoau6 QTyG+wjxOv0exwbnkKmmi9kd/bH9nH2KQGdnoNmFjiJ2VmLq+D5ceDK/z7nTcrA0kD5P +EvbHQwivK8TeT1ZzIQZbe7KcTKW1I3WSkteQuMrJq+U5gtwaAPekuRK5oZL9L2UshWX cJ2g== 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=jfvLV6T2p9fP56/v/Xia6iQ7yK2dQ6zBkNiykDOKhfw=; fh=3IEiF/YPZ00TE94rna3Orj/L+TRFhpuNf7ElRJz3ubE=; b=JQUGKgA95gaiS2x61QVNRYSEcPdUeTQBbhSh1HtmCCkzMqJeCTvrq+1A3vjeLNSEGi DhfatwyZzKG15PRdZjDWPc0l4N8kxfczxFis9PzSbEeRYtUeMBP6tfAfvgfIMS7hwt/a /wMlkyOBFQD52XuDIaLDydJuZ73U/e1hTc71YS6YY5XSUTmqyZBcTaFWOVNPTeqRa5zE KAB4OxW8hT/zRW0i8vegD1FC0TWJlboKKTtiJ1+DWti6QKG8w+BE8+9bdJ/lnnOJcvRb pTjgC8751LpfyRLkJUO2KBJcfOoKT2Vwiac7WQj57qVg8NUbFQHq/A5r72ldnynnDB9+ YZxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Iu769+Ce; 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 p27-20020a05620a15fb00b0076776a86721si5055245qkm.297.2023.07.09.11.38.44 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 09 Jul 2023 11:38: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=Iu769+Ce; 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 1qIZEC-0004ms-09; Sun, 09 Jul 2023 14:33:48 -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 1qIZE8-0004KA-1u for qemu-devel@nongnu.org; Sun, 09 Jul 2023 14:33:44 -0400 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qIZE3-0004Iz-85 for qemu-devel@nongnu.org; Sun, 09 Jul 2023 14:33:40 -0400 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-3128fcd58f3so4042883f8f.1 for ; Sun, 09 Jul 2023 11:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688927618; x=1691519618; 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=jfvLV6T2p9fP56/v/Xia6iQ7yK2dQ6zBkNiykDOKhfw=; b=Iu769+CeDmbhxeaP4f3c/BUQzMW00to6eQPH8l9S4Twt9ml4jNywDOIozg16e/cLuj Y3gdayKKPxma+e1VR2wileGpd3hvnI3CfJ/9DLff9lKI4RqfEeJN26+QXnGmDM27ZXw2 S5/Ho/+5Hcrf/bZ9BZvfhdh7pYXPRhf15NAFjwYvbDvKM+BW64HZ3fY3MVhsf3tJstOH b5NDev7Rog5h/QEcxgFn9Z0Gtc3x0r/HDs2L565wle4XFLOKZSNPPT4++0k8fYJuIrrh 2guCMO283HZ/eYb+OQwodWXdRmAXOqevFVJp49tC8ph6hRIV2E5gq39Y7cvtlt9S6cyg uM5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688927618; x=1691519618; 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=jfvLV6T2p9fP56/v/Xia6iQ7yK2dQ6zBkNiykDOKhfw=; b=kil+BwgTdJViItSq0KorjLGF0t4d9Kf9wkSY3SH2hbPxKzQLMvvOL3zqkGwcP1RCgT Z9ZxVeZLAxfWorLXW20h0uZc5Ys/16p8BLNJw2Dgh4OBRTkUsmOXoVt7iV4DV57DpBHv CZIMu/rxMOBRhZIa87q2aVde+EAfQRGempa/OdoqiE5n/7hp//Rr25t47qd8AzAlqJcF +peylS+FrH4hH0/c5TYOQLYH6vEUYIxU4uhG+Af2WuonrAy1BmIcVGhVhaWRfHvcg012 eb6BMdtsvnV5Hvn1V5K+uv4ZKiGD1Imcd7iAx/csCdyyjxSmWm6yBWnbwmoqsACCMS4u ajaA== X-Gm-Message-State: ABy/qLaKyT7RSxORhzwlXYdJjCzoMnv9mjTcri2JMrVlifJsXqG3CpXH i00xPwUxjClI1k/VWzudnZI3ttqnx+0tPvmmS5Ly0A== X-Received: by 2002:adf:ed05:0:b0:313:f0d7:a43 with SMTP id a5-20020adfed05000000b00313f0d70a43mr9922790wro.23.1688927617982; Sun, 09 Jul 2023 11:33:37 -0700 (PDT) Received: from stoup.. ([148.252.133.210]) by smtp.gmail.com with ESMTPSA id e17-20020a5d4e91000000b0031433760a92sm9635256wru.115.2023.07.09.11.33.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jul 2023 11:33:37 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH 36/45] linux-user: Use 'last' instead of 'end' in target_mmap Date: Sun, 9 Jul 2023 19:29:24 +0100 Message-Id: <20230709182934.309468-73-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230709182934.309468-1-richard.henderson@linaro.org> References: <20230709182934.309468-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::433; envelope-from=richard.henderson@linaro.org; helo=mail-wr1-x433.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Complete the transition within the mmap functions to a formulation that does not overflow at the end of the address space. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20230707204054.8792-20-richard.henderson@linaro.org> --- linux-user/mmap.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 738b9b797d..bb9cbe52cd 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -456,8 +456,8 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, int flags, int fd, off_t offset) { - abi_ulong ret, end, real_start, real_end, retaddr, host_len, - passthrough_start = -1, passthrough_end = -1; + abi_ulong ret, last, real_start, real_last, retaddr, host_len; + abi_ulong passthrough_start = -1, passthrough_last = 0; int page_flags; off_t host_offset; @@ -581,29 +581,30 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, host_start += offset - host_offset; } start = h2g(host_start); + last = start + len - 1; passthrough_start = start; - passthrough_end = start + len; + passthrough_last = last; } else { if (start & ~TARGET_PAGE_MASK) { errno = EINVAL; goto fail; } - end = start + len; - real_end = HOST_PAGE_ALIGN(end); + last = start + len - 1; + real_last = HOST_PAGE_ALIGN(last) - 1; /* * Test if requested memory area fits target address space * It can fail only on 64-bit host with 32-bit target. * On any other target/host host mmap() handles this error correctly. */ - if (end < start || !guest_range_valid_untagged(start, len)) { + if (last < start || !guest_range_valid_untagged(start, len)) { errno = ENOMEM; goto fail; } /* Validate that the chosen range is empty. */ if ((flags & MAP_FIXED_NOREPLACE) - && !page_check_range_empty(start, end - 1)) { + && !page_check_range_empty(start, last)) { errno = EEXIST; goto fail; } @@ -642,9 +643,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, /* handle the start of the mapping */ if (start > real_start) { - if (real_end == real_start + qemu_host_page_size) { + if (real_last == real_start + qemu_host_page_size - 1) { /* one single host page */ - if (!mmap_frag(real_start, start, end - 1, + if (!mmap_frag(real_start, start, last, target_prot, flags, fd, offset)) { goto fail; } @@ -658,18 +659,18 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, real_start += qemu_host_page_size; } /* handle the end of the mapping */ - if (end < real_end) { - if (!mmap_frag(real_end - qemu_host_page_size, - real_end - qemu_host_page_size, end - 1, + if (last < real_last) { + abi_ulong real_page = real_last - qemu_host_page_size + 1; + if (!mmap_frag(real_page, real_page, last, target_prot, flags, fd, - offset + real_end - qemu_host_page_size - start)) { + offset + real_page - start)) { goto fail; } - real_end -= qemu_host_page_size; + real_last -= qemu_host_page_size; } /* map the middle (easier) */ - if (real_start < real_end) { + if (real_start < real_last) { void *p; off_t offset1; @@ -678,13 +679,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, } else { offset1 = offset + real_start - start; } - p = mmap(g2h_untagged(real_start), real_end - real_start, + p = mmap(g2h_untagged(real_start), real_last - real_start + 1, target_to_host_prot(target_prot), flags, fd, offset1); if (p == MAP_FAILED) { goto fail; } passthrough_start = real_start; - passthrough_end = real_end; + passthrough_last = real_last; } } the_end1: @@ -692,16 +693,16 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, page_flags |= PAGE_ANON; } page_flags |= PAGE_RESET; - if (passthrough_start == passthrough_end) { - page_set_flags(start, start + len - 1, page_flags); + if (passthrough_start > passthrough_last) { + page_set_flags(start, last, page_flags); } else { if (start < passthrough_start) { page_set_flags(start, passthrough_start - 1, page_flags); } - page_set_flags(passthrough_start, passthrough_end - 1, + page_set_flags(passthrough_start, passthrough_last, page_flags | PAGE_PASSTHROUGH); - if (passthrough_end < start + len) { - page_set_flags(passthrough_end, start + len - 1, page_flags); + if (passthrough_last < last) { + page_set_flags(passthrough_last + 1, last, page_flags); } } the_end: