From patchwork Tue Jan 2 01:57:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 759343 Delivered-To: patch@linaro.org Received: by 2002:a5d:67c6:0:b0:336:6142:bf13 with SMTP id n6csp5856652wrw; Mon, 1 Jan 2024 18:00:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPrFNyqS8qxwZuyesIuZtRDwBO64wgEBmqHH7XW5LUfVb1RKEl6UhPpcQTnFoZjh1zDQrm X-Received: by 2002:a05:620a:4903:b0:781:b736:f03d with SMTP id vy3-20020a05620a490300b00781b736f03dmr3559405qkn.101.1704160856924; Mon, 01 Jan 2024 18:00:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704160856; cv=none; d=google.com; s=arc-20160816; b=MeEv8MPoxRRGezUmTYS4jwb7VBm9OGeKL+R8KTsUiCgGiT2j3oAK83zhkyBplB9uIg d5E76eZzphdx4YNruU2M3GTgEoVlGfTUkqby/iw+AP0haHmlwJAiY+rY+wipfxF347Uf 1rolix9rDWwdr3FUtjAifzjKnCFOF7vApdOLcff2ZJvoO1/x0QpqRXVMSW/O0DNQ+gPy KNl/FSwlozhBaMwpJQ0pjoHfeZbiOVufzhs6MJevFXfAJoD2OiCALRvtQImAkba7VsAq udRRx70p7SbbncpSglBOpxcJSqwBdBvnya4l9+Aqond11xhqYlxVJKf6PARat8zvknVc PTYw== 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=qA2a/PTKZLcZg5cbX228JZnmG9F2Eb0mEU1DFgp6lOI=; fh=PnYt+qEB9tAfMKoqBm2xjKOFpYyFFGPudh5cVIoieJM=; b=lDnVLWhMJdCRTv40QNWMKeIwLkU4JbeuVAb1xuD3MdITc3uN6y/+1XvxV3FMvNOiRS Zkz/Gbu7VRRGkcMsVEmyDdBc2/ZPtMNJFTB0GwqxX64se8Uo2stJ38SgsTW/xyihrG6Q T1p+OPwoENmXX0Cj5zh1EcKV72FRa2NP8L9q3TFAIKrjo7gu35TpcTZD3ejw7wRKPJg1 pIrH+4wtQ4SBYhQiJADhfMn6txGnsKhrcws9LEvuV9rRFPXortTvRTAuFXfXMu5GGyIV vpLTNzNFyPISWilbxsVlX7YvjGfqJLDWfnc8FPeE61ZUdm84vitkf+YMmpfdg17BgtyV 6A3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tXc2OJRC; 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 b26-20020a05620a089a00b0078154cbc43dsi14786840qka.652.2024.01.01.18.00.56 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 01 Jan 2024 18:00:56 -0800 (PST) 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=tXc2OJRC; 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 1rKU3l-0003kr-Ow; Mon, 01 Jan 2024 20:59:13 -0500 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 1rKU3Z-0003eP-Gl for qemu-devel@nongnu.org; Mon, 01 Jan 2024 20:59:02 -0500 Received: from mail-ot1-x331.google.com ([2607:f8b0:4864:20::331]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rKU3X-00074b-NB for qemu-devel@nongnu.org; Mon, 01 Jan 2024 20:59:01 -0500 Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-6dc018228b4so3062009a34.0 for ; Mon, 01 Jan 2024 17:58:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1704160738; x=1704765538; darn=nongnu.org; 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=qA2a/PTKZLcZg5cbX228JZnmG9F2Eb0mEU1DFgp6lOI=; b=tXc2OJRCBlQorvRvX/hn7/nUSOktcroMzwn7shw9Ag9QDegfsFDZL9vh7pP7BDY7c9 eWkvxNHJwGNUEmNHcnsg4HTkQBsYMDYnuxj8ejlLAkdhyyCkfE3+5R+fLg7PU0kthK9p pHHI+CgX5sJa/qSONH7mzWCpXkX9p/l+m3iqCE0JhSwx3fLDAMkMUJhey0DN8o19n/8K 75rGkVfBNqQwBT/r5xU+3GqNKjmr+vMoryFe5OKqDCMzPFuwGE30M50MqoedJkdAcpxg JJMKTATANwZ0BS4mTmZdS2HFQSwAagMwdzKkweq5Y28ERjRG5PnfTsRjSWnDpodXjr+6 Szfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704160738; x=1704765538; 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=qA2a/PTKZLcZg5cbX228JZnmG9F2Eb0mEU1DFgp6lOI=; b=ltEru97ggWBl4JCiDGqeucl2+tdK3q1ukBpAR26kyLnI9k0VlFycTOPYi6jQrhTXm5 zr24f47eCJbGYiIdb3BonG9BdYcF5+ktMfTsIN2CZ92daCnzgeHol9QDu6pq3kZLyMoQ iM3Cpa3RS+mULl1MwgcW7N1Mpwxf2c6IidGoxldjzm4BoQoo1yBrmQBj1ukfP06CJoxh mxff6yj122w6Sdutg1mCqsTG7ERwOGn6Z8yYCEvACUhC4k/EzEG9/F6JUXYo2ODvlam6 H3aMMakjoOb76QsolGhJFniqfJcfVdSjmhRDudqjGJfAWljWCcW5k4j6fagw/xt6GX/H ZmYw== X-Gm-Message-State: AOJu0YwAi/PH4bFFRHXunPr82S96hvvz7zpHYlE3oP6ZxiJqK5OQftqK pZFJXQgvtJ1kHggVN++dugSGn3rMRpSqs0qiExkgMa4moe0= X-Received: by 2002:a05:6808:238e:b0:3bb:e1bb:3546 with SMTP id bp14-20020a056808238e00b003bbe1bb3546mr6643355oib.44.1704160738363; Mon, 01 Jan 2024 17:58:58 -0800 (PST) Received: from stoup.. (124-149-254-207.tpgi.com.au. [124.149.254.207]) by smtp.gmail.com with ESMTPSA id g33-20020a635221000000b0058ee60f8e4dsm19561891pgb.34.2024.01.01.17.58.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jan 2024 17:58:57 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v3 22/33] linux-user: Split out mmap_h_lt_g Date: Tue, 2 Jan 2024 12:57:57 +1100 Message-Id: <20240102015808.132373-23-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240102015808.132373-1-richard.henderson@linaro.org> References: <20240102015808.132373-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::331; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x331.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 Work much harder to get alignment and mapping beyond the end of the file correct. Both of which are excercised by our test-mmap for alpha (8k pages) on any 4k page host. Signed-off-by: Richard Henderson Reviewed-by: Pierrick Bouvier --- linux-user/mmap.c | 156 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 125 insertions(+), 31 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 00003b8329..8b0a26e50d 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -552,6 +552,128 @@ static abi_long mmap_h_eq_g(abi_ulong start, abi_ulong len, return mmap_end(start, last, start, last, flags, page_flags); } +/* + * Special case host page size < target page size. + * + * The two special cases are increased guest alignment, and mapping + * past the end of a file. + * + * When mapping files into a memory area larger than the file, + * accesses to pages beyond the file size will cause a SIGBUS. + * + * For example, if mmaping a file of 100 bytes on a host with 4K + * pages emulating a target with 8K pages, the target expects to + * be able to access the first 8K. But the host will trap us on + * any access beyond 4K. + * + * When emulating a target with a larger page-size than the hosts, + * we may need to truncate file maps at EOF and add extra anonymous + * pages up to the targets page boundary. + * + * This workaround only works for files that do not change. + * If the file is later extended (e.g. ftruncate), the SIGBUS + * vanishes and the proper behaviour is that changes within the + * anon page should be reflected in the file. + * + * However, this case is rather common with executable images, + * so the workaround is important for even trivial tests, whereas + * the mmap of of a file being extended is less common. + */ +static abi_long mmap_h_lt_g(abi_ulong start, abi_ulong len, int host_prot, + int mmap_flags, int page_flags, int fd, + off_t offset, int host_page_size) +{ + void *p, *want_p = g2h_untagged(start); + off_t fileend_adj = 0; + int flags = mmap_flags; + abi_ulong last, pass_last; + + if (!(flags & MAP_ANONYMOUS)) { + struct stat sb; + + if (fstat(fd, &sb) == -1) { + return -1; + } + if (offset >= sb.st_size) { + /* + * The entire map is beyond the end of the file. + * Transform it to an anonymous mapping. + */ + flags |= MAP_ANONYMOUS; + fd = -1; + offset = 0; + } else if (offset + len > sb.st_size) { + /* + * A portion of the map is beyond the end of the file. + * Truncate the file portion of the allocation. + */ + fileend_adj = offset + len - sb.st_size; + } + } + + if (flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)) { + if (fileend_adj) { + p = mmap(want_p, len, host_prot, flags | MAP_ANONYMOUS, -1, 0); + } else { + p = mmap(want_p, len, host_prot, flags, fd, offset); + } + if (p != want_p) { + if (p != MAP_FAILED) { + munmap(p, len); + errno = EEXIST; + } + return -1; + } + + if (fileend_adj) { + void *t = mmap(p, len - fileend_adj, host_prot, + (flags & ~MAP_FIXED_NOREPLACE) | MAP_FIXED, + fd, offset); + assert(t != MAP_FAILED); + } + } else { + size_t host_len, part_len; + + /* + * Take care to align the host memory. Perform a larger anonymous + * allocation and extract the aligned portion. Remap the file on + * top of that. + */ + host_len = len + TARGET_PAGE_SIZE - host_page_size; + p = mmap(want_p, host_len, host_prot, flags | MAP_ANONYMOUS, -1, 0); + if (p == MAP_FAILED) { + return -1; + } + + part_len = (uintptr_t)p & (TARGET_PAGE_SIZE - 1); + if (part_len) { + part_len = TARGET_PAGE_SIZE - part_len; + munmap(p, part_len); + p += part_len; + host_len -= part_len; + } + if (len < host_len) { + munmap(p + len, host_len - len); + } + + if (!(flags & MAP_ANONYMOUS)) { + void *t = mmap(p, len - fileend_adj, host_prot, + flags | MAP_FIXED, fd, offset); + assert(t != MAP_FAILED); + } + + start = h2g(p); + } + + last = start + len - 1; + if (fileend_adj) { + pass_last = ROUND_UP(last - fileend_adj, host_page_size) - 1; + } else { + pass_last = last; + } + return mmap_end(start, last, start, pass_last, mmap_flags, page_flags); +} + static abi_long target_mmap__locked(abi_ulong start, abi_ulong len, int target_prot, int flags, int page_flags, int fd, off_t offset) @@ -596,37 +718,9 @@ static abi_long target_mmap__locked(abi_ulong start, abi_ulong len, if (host_page_size == TARGET_PAGE_SIZE) { return mmap_h_eq_g(start, len, host_prot, flags, page_flags, fd, offset); - } - - /* - * When mapping files into a memory area larger than the file, accesses - * to pages beyond the file size will cause a SIGBUS. - * - * For example, if mmaping a file of 100 bytes on a host with 4K pages - * emulating a target with 8K pages, the target expects to be able to - * access the first 8K. But the host will trap us on any access beyond - * 4K. - * - * When emulating a target with a larger page-size than the hosts, we - * may need to truncate file maps at EOF and add extra anonymous pages - * up to the targets page boundary. - */ - if (host_page_size < TARGET_PAGE_SIZE && !(flags & MAP_ANONYMOUS)) { - struct stat sb; - - if (fstat(fd, &sb) == -1) { - return -1; - } - - /* Are we trying to create a map beyond EOF?. */ - if (offset + len > sb.st_size) { - /* - * If so, truncate the file map at eof aligned with - * the hosts real pagesize. Additional anonymous maps - * will be created beyond EOF. - */ - len = ROUND_UP(sb.st_size - offset, host_page_size); - } + } else if (host_page_size < TARGET_PAGE_SIZE) { + return mmap_h_lt_g(start, len, host_prot, flags, + page_flags, fd, offset, host_page_size); } if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {