From patchwork Fri Sep 1 02:23:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 719466 Delivered-To: patch@linaro.org Received: by 2002:ab3:1981:0:b0:23f:8cfb:5ad7 with SMTP id 1csp16692lti; Thu, 31 Aug 2023 19:24:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGntJEYhHpMLqHz781F2buzE3gXRXMoBkV0xcthnFTzasalxx09MDPsqQEPAwpZBrfCUjVj X-Received: by 2002:a0c:f743:0:b0:64f:4dbf:b21a with SMTP id e3-20020a0cf743000000b0064f4dbfb21amr1044104qvo.29.1693535069263; Thu, 31 Aug 2023 19:24:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693535069; cv=none; d=google.com; s=arc-20160816; b=DVoik/qVg77+EUoXATKUnNWMy/Ay9NkLKiOpcsMp+D0bF9I8IU3mSytVs3IsfCSIkR +mJbxXMJeRyUoNTZJcmCZM7IUB1JrdP2bJvUqy6fthO4UVuSjGjp0cXtDDNE34mYWGoT p3Mwj7Iw40p2D4qMV0OTCeuzUugPKj9NU59kGUKqnZTa29lOcgc83meH5p0Wz3ufzobs AYzf3QPpmz0KT2GoYvxVMIRID07fRERfdR6F3CDrF8mdREDS3Uv6qFnzcBz/M1Y9v6EF kaokGelM1axbErseCmeSoh4wgA0S5B+VGOSKv1alkcPfdPiuipidfqY6dqZQG16v6ugr +Wqw== 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=4bucLpoT0vquWNBuZLTOreK2BOdVySJCazC6PuHo6Rk=; fh=r1cPj2GlTGSV4V8jQu3jfCKsZwpJ5ktYEUy+P7GD/+s=; b=eyg4BlA9LFGHXvCrw4f/f3MkFjVCRYC4CIUiRt9ByoNBmBENmzSNu0cuBJlPNjWRYq seWUXvkPbMMSzsWHiQX2VYs692504/FCPyFqVdRPZexANi6njYXqogxLXSQXts+UIbkW Da6DQY8dfeSf+Xrr49IWF07w5irWJ1Qaktk2hRPgjuyDpxDjziIGt2IsfXnvYRJz9wgM vFyll5C1huN3xncsT9GOFXSJPJ/OoY4qZ7YEG9pQLBPPrT7Tp9bybLl4lX5/vwu/nZn+ O9d43jjNcWXzaGuE4hX0s6ca6r5KGWE+ZF6H0/ZBpsFKggufX+EvfoLcIBY/wJs6RUJn PWEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SUj97dq8; 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 w9-20020a0cb549000000b006408e52cad5si1764291qvd.485.2023.08.31.19.24.29 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 31 Aug 2023 19:24:29 -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=SUj97dq8; 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 1qbtpM-0006ot-KN; Thu, 31 Aug 2023 22:24:04 -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 1qbtpJ-0006f2-4o for qemu-devel@nongnu.org; Thu, 31 Aug 2023 22:24:01 -0400 Received: from mail-oi1-x22d.google.com ([2607:f8b0:4864:20::22d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbtpA-0001Rx-B0 for qemu-devel@nongnu.org; Thu, 31 Aug 2023 22:23:54 -0400 Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3a8506f5b73so952307b6e.0 for ; Thu, 31 Aug 2023 19:23:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693535031; x=1694139831; darn=nongnu.org; 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=4bucLpoT0vquWNBuZLTOreK2BOdVySJCazC6PuHo6Rk=; b=SUj97dq8IhESm8KahCOejlOkTXlZyRvymOS405PszQ7fQQIlLgwrhEUcyRbTBI2y1a XK4JcfvPOkqqW6a9YJdzhJcm7oXixj81B/aPsix85ewa1dN08by4myyuONnSQFYd8F2W hT5MeMBEJ/YVu0686K8RJ/uSxaDI0bzK9Q4JTSmYkYtPpiKhGpccfQiOKhDDQa46QGXW hHpQ9qTrVxuFO3oDTydTlmCaLvpdpHSq6ufg7TXYoyx0IktYuHnXqZGgCj0IaZIGBdBW fI0qJdOHXxtpG6bUEIUv+/APhBDLK8GgBQe38ycKpvZBMp9fWdwOEHA/h5uYkmbqtNPE HZdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693535031; x=1694139831; 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=4bucLpoT0vquWNBuZLTOreK2BOdVySJCazC6PuHo6Rk=; b=fw5JlcqVB+/3AXPKtGRgb47arpkRkA0v4TN0LS09c/Bi9kan4UsWG/k5p2XuKX2sRI 8p6Z+OgVNlazsSMk758u3jatOIX8T63J2MLCbaGueguUpsud1vhm90tuFbmAPNfqQYoQ aEA7rtIYmu3xgwNg3fTowt1gGB3DS7NsrA9EBZHwf17Li0JW8BoUc5OM8Q6NCNYaMp/S P/GZQJothSA9LbWC49kelGK0MVCEeF9u/hHEgjU1TghkOQsvY7y8b3si2IcK91GCqUA5 VGgZ9cpBwhRGLrJm+U5iHVqDdBdRZ3hhsteBU99Brb1RVZCc31pE7RQMgQxcLeIyKp5E dPSw== X-Gm-Message-State: AOJu0YzLvqsnLi2L4ve73Bd6ci399ZJORAvVYq7xMfFWdVSwaMv8hf3D cGA1GAaLlRFkvMBgj1pYf1rMENKqczuZcYJ0Lfs= X-Received: by 2002:a54:4e81:0:b0:3a7:2570:dcfc with SMTP id c1-20020a544e81000000b003a72570dcfcmr1394444oiy.43.1693535030830; Thu, 31 Aug 2023 19:23:50 -0700 (PDT) Received: from stoup.. ([71.212.131.115]) by smtp.gmail.com with ESMTPSA id c25-20020aa781d9000000b0068a54866ca8sm1906977pfn.134.2023.08.31.19.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 19:23:50 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, alex.bennee@linaro.org Subject: [PATCH v2 22/33] linux-user: Split out mmap_h_lt_g Date: Thu, 31 Aug 2023 19:23:20 -0700 Message-Id: <20230901022331.115247-23-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230901022331.115247-1-richard.henderson@linaro.org> References: <20230901022331.115247-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::22d; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x22d.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 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 --- 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 7a0c0c1f35..ed82b4bb75 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -507,6 +507,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) @@ -551,37 +673,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))) {