From patchwork Sat Jul 15 13:52:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 703209 Delivered-To: patch@linaro.org Received: by 2002:a5d:4e4d:0:b0:314:25da:8dc4 with SMTP id r13csp156666wrt; Sat, 15 Jul 2023 06:54:14 -0700 (PDT) X-Google-Smtp-Source: APBJJlF5UXpmcAQQMZomatE5WN3M8RR67BBXhfYwe+ZZ//GCebVTaq1wJ3FJvIHdbCkq6DyW8ea+ X-Received: by 2002:a05:620a:29d3:b0:763:d32b:507f with SMTP id s19-20020a05620a29d300b00763d32b507fmr10890399qkp.48.1689429254022; Sat, 15 Jul 2023 06:54:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689429254; cv=none; d=google.com; s=arc-20160816; b=oZw9NjxlNmlH0gbBJSiKZmVBomQiVvM/XOQ20kov5xrKqL74UNem10q2Uzc9JKLOhR cg2HCcLRW7NubxCyAWhwf89TCe1peeMO4CWX9QIFPZe5ZXcoy53p/Rf3oDyvGz0+5hDU bDpnrc36YatzUrQI37Ok1N5LrfgFWZbZcdwtC8hvRBdtM9pbSxu8c1fElQ19FeHFBxtU 9WPSXjznRx+OsYSmtGB6HX7TnlwJKXm41x25VkkBTBgS8aZiAfgEmj0/78sBZAf4B86u bAREFdnBdthwFgHYoPaPeu1fLeC/Ncz9LrrU5yIEQrnmeju7JvzyW5tnwcPN7xZX47hA TbDA== 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=7S8fJjWsfM8/RKnCcasDShb0DLhg94fNTjj77rs37DE=; fh=ZuLSLtzpwbOj79Lm6HQkAzCl7AYBeBRT4Q/1zeFcrQg=; b=bkjGhkzph9OQ1gl92dttsgzEKCxN3kryHUtSdMbURkP7jSnJ5TAnDzfuY1L+qXhj+f fcJNUVpwWnrldotQX3qPQ3dO4RTDb8eAsw6SlMoYCn4zyo+juegpwAWt+eNMCoUXfrzb sh6f6zpDgQJsT9Uf1/AK4awa6jAjxrUWviD3T5XRUBaD7+sJzcJ11DBRPH63/KSEpg6d bsmARfu30qogbjIHb6+/ggcam8O6KVcmQ6JvGHLXAWOIeo7f10K0IeXy/Nce0t6iYPgU QWQFgNFDjoBIe4nkvqFvZ386O69IuFflELEAgYGZA8FZSdGedCHB8045UiAxRPO2pXDx oVhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=adaWzhfJ; 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 16-20020a05620a049000b00767ee53f0absi4499243qkr.340.2023.07.15.06.54.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 15 Jul 2023 06:54:14 -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=adaWzhfJ; 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 1qKfir-0004EZ-Tg; Sat, 15 Jul 2023 09:54: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 1qKfia-00048U-11 for qemu-devel@nongnu.org; Sat, 15 Jul 2023 09:53:55 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qKfiS-0001JI-DW for qemu-devel@nongnu.org; Sat, 15 Jul 2023 09:53:50 -0400 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3fbc6ab5ff5so25921965e9.1 for ; Sat, 15 Jul 2023 06:53:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1689429222; x=1692021222; 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=7S8fJjWsfM8/RKnCcasDShb0DLhg94fNTjj77rs37DE=; b=adaWzhfJF68woo4+Ou6nQ8GniG7eOI2oqpAo541kTPodiLeBQdzXXThSJFxkClIIwn NEH/+nRihFFVkaiWvdCbUXxtjHMdXyIjVtA/dnKotNd4z5kMp1ssDfSCmC68FZmQqB5c TPFstxkoQSXgUaPsN27n149DiBH3v3e5rKzGfPQPIcb/ZjFgpd3YFVtfISi6KATBYV2a 9AKdmCReILDEEjUCxhZITwCyD2WTRX0Qp+8rXNWB+PM7T4Gl6+tosvb1ELJrf83qDxWK i22EmlgF1/xczTPJ5CPkcHpiqyHX2vAXN0PhFPvGVudsxmw6ai0TozIsFbpEzB6LKptk p54A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689429222; x=1692021222; 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=7S8fJjWsfM8/RKnCcasDShb0DLhg94fNTjj77rs37DE=; b=ebtcNmF0KRRaYKvM286XfEFknp7eFJqyQUOCyjepSjS1lO7puzagoB5UJu0bU5bFqk lrntvY7EdovyhVkrss7E//2TuS9C7RFOM4Ky68202An9jQzkz68O1h6auuKHfuBopdYX Cm65JxN13HKSW4B0Z4CW2NoHF9472bIIVQpxFiBCcKIHCXIfJLeWV1EJChr53a1ka13C xQ5LwH8PuKd1ji5z0+7DRNs6wP9gUMNY5WeCECxdmWTvuSJfEitRa7HYfS01QWA8y7S+ Ay8E6bdOmqwk7YgYbVFHkKOcvoGWLgObmAjba6CXAtB1AXdXAFchZZomS9Dt+AHdOdvX ew9A== X-Gm-Message-State: ABy/qLZheDBIBtpClnGzFQyyEvvuQsn9X7+Ux6k04Kphu6M1PFYT5PP0 GnKkf2sYQJGynDug/Tdh8Ih9J22+cOkaMKQrrlV0mw== X-Received: by 2002:adf:ea4a:0:b0:316:e422:38e8 with SMTP id j10-20020adfea4a000000b00316e42238e8mr7550246wrn.66.1689429222400; Sat, 15 Jul 2023 06:53:42 -0700 (PDT) Received: from stoup.lan ([51.219.12.49]) by smtp.gmail.com with ESMTPSA id k10-20020a5d66ca000000b00313f9085119sm13838014wrw.113.2023.07.15.06.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Jul 2023 06:53:42 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 29/47] linux-user: Split out target_to_host_prot Date: Sat, 15 Jul 2023 14:52:59 +0100 Message-Id: <20230715135317.7219-30-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230715135317.7219-1-richard.henderson@linaro.org> References: <20230715135317.7219-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x334.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 Split out from validate_prot_to_pageflags, as there is not one single host_prot for the entire range. We need to adjust prot for every host page that overlaps multiple guest pages. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson Message-Id: <20230707204054.8792-13-richard.henderson@linaro.org> --- linux-user/mmap.c | 78 ++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 9dc34fc29d..12b1308a83 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -69,24 +69,11 @@ void mmap_fork_end(int child) * Return 0 if the target prot bitmask is invalid, otherwise * the internal qemu page_flags (which will include PAGE_VALID). */ -static int validate_prot_to_pageflags(int *host_prot, int prot) +static int validate_prot_to_pageflags(int prot) { int valid = PROT_READ | PROT_WRITE | PROT_EXEC | TARGET_PROT_SEM; int page_flags = (prot & PAGE_BITS) | PAGE_VALID; - /* - * For the host, we need not pass anything except read/write/exec. - * While PROT_SEM is allowed by all hosts, it is also ignored, so - * don't bother transforming guest bit to host bit. Any other - * target-specific prot bits will not be understood by the host - * and will need to be encoded into page_flags for qemu emulation. - * - * Pages that are executable by the guest will never be executed - * by the host, but the host will need to be able to read them. - */ - *host_prot = (prot & (PROT_READ | PROT_WRITE)) - | (prot & PROT_EXEC ? PROT_READ : 0); - #ifdef TARGET_AARCH64 { ARMCPU *cpu = ARM_CPU(thread_cpu); @@ -114,18 +101,34 @@ static int validate_prot_to_pageflags(int *host_prot, int prot) return prot & ~valid ? 0 : page_flags; } +/* + * For the host, we need not pass anything except read/write/exec. + * While PROT_SEM is allowed by all hosts, it is also ignored, so + * don't bother transforming guest bit to host bit. Any other + * target-specific prot bits will not be understood by the host + * and will need to be encoded into page_flags for qemu emulation. + * + * Pages that are executable by the guest will never be executed + * by the host, but the host will need to be able to read them. + */ +static int target_to_host_prot(int prot) +{ + return (prot & (PROT_READ | PROT_WRITE)) | + (prot & PROT_EXEC ? PROT_READ : 0); +} + /* NOTE: all the constants are the HOST ones, but addresses are target. */ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) { abi_ulong end, host_start, host_end, addr; - int prot1, ret, page_flags, host_prot; + int prot1, ret, page_flags; trace_target_mprotect(start, len, target_prot); if ((start & ~TARGET_PAGE_MASK) != 0) { return -TARGET_EINVAL; } - page_flags = validate_prot_to_pageflags(&host_prot, target_prot); + page_flags = validate_prot_to_pageflags(target_prot); if (!page_flags) { return -TARGET_EINVAL; } @@ -143,7 +146,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) host_end = HOST_PAGE_ALIGN(end); if (start > host_start) { /* handle host page containing start */ - prot1 = host_prot; + prot1 = target_prot; for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) { prot1 |= page_get_flags(addr); } @@ -154,19 +157,19 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) end = host_end; } ret = mprotect(g2h_untagged(host_start), qemu_host_page_size, - prot1 & PAGE_BITS); + target_to_host_prot(prot1)); if (ret != 0) { goto error; } host_start += qemu_host_page_size; } if (end < host_end) { - prot1 = host_prot; + prot1 = target_prot; for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) { prot1 |= page_get_flags(addr); } ret = mprotect(g2h_untagged(host_end - qemu_host_page_size), - qemu_host_page_size, prot1 & PAGE_BITS); + qemu_host_page_size, target_to_host_prot(prot1)); if (ret != 0) { goto error; } @@ -175,8 +178,8 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot) /* handle the pages in the middle */ if (host_start < host_end) { - ret = mprotect(g2h_untagged(host_start), - host_end - host_start, host_prot); + ret = mprotect(g2h_untagged(host_start), host_end - host_start, + target_to_host_prot(target_prot)); if (ret != 0) { goto error; } @@ -212,7 +215,8 @@ static int mmap_frag(abi_ulong real_start, if (prot1 == 0) { /* no page was there, so we allocate one */ - void *p = mmap(host_start, qemu_host_page_size, prot, + void *p = mmap(host_start, qemu_host_page_size, + target_to_host_prot(prot), flags | MAP_ANONYMOUS, -1, 0); if (p == MAP_FAILED) { return -1; @@ -233,7 +237,8 @@ static int mmap_frag(abi_ulong real_start, /* adjust protection to be able to read */ if (!(prot1 & PROT_WRITE)) { - mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); + mprotect(host_start, qemu_host_page_size, + target_to_host_prot(prot1) | PROT_WRITE); } /* read the corresponding file data */ @@ -243,11 +248,13 @@ static int mmap_frag(abi_ulong real_start, /* put final protection */ if (prot_new != (prot1 | PROT_WRITE)) { - mprotect(host_start, qemu_host_page_size, prot_new); + mprotect(host_start, qemu_host_page_size, + target_to_host_prot(prot_new)); } } else { if (prot_new != prot1) { - mprotect(host_start, qemu_host_page_size, prot_new); + mprotect(host_start, qemu_host_page_size, + target_to_host_prot(prot_new)); } if (prot_new & PROT_WRITE) { memset(g2h_untagged(start), 0, end - start); @@ -460,7 +467,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, { abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len, passthrough_start = -1, passthrough_end = -1; - int page_flags, host_prot; + int page_flags; mmap_lock(); trace_target_mmap(start, len, target_prot, flags, fd, offset); @@ -470,7 +477,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, goto fail; } - page_flags = validate_prot_to_pageflags(&host_prot, target_prot); + page_flags = validate_prot_to_pageflags(target_prot); if (!page_flags) { errno = EINVAL; goto fail; @@ -553,10 +560,12 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) { unsigned long host_start; + int host_prot; void *p; host_len = len + offset - host_offset; host_len = HOST_PAGE_ALIGN(host_len); + host_prot = target_to_host_prot(target_prot); /* * Note: we prefer to control the mapping address. It is @@ -617,7 +626,8 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, * msync() won't work here, so we return an error if write is * possible while it is a shared mapping */ - if ((flags & MAP_TYPE) == MAP_SHARED && (host_prot & PROT_WRITE)) { + if ((flags & MAP_TYPE) == MAP_SHARED + && (target_prot & PROT_WRITE)) { errno = EINVAL; goto fail; } @@ -631,7 +641,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, if (pread(fd, g2h_untagged(start), len, offset) == -1) { goto fail; } - if (!(host_prot & PROT_WRITE)) { + if (!(target_prot & PROT_WRITE)) { ret = target_mprotect(start, len, target_prot); assert(ret == 0); } @@ -643,14 +653,14 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, if (real_end == real_start + qemu_host_page_size) { /* one single host page */ ret = mmap_frag(real_start, start, end, - host_prot, flags, fd, offset); + target_prot, flags, fd, offset); if (ret == -1) { goto fail; } goto the_end1; } ret = mmap_frag(real_start, start, real_start + qemu_host_page_size, - host_prot, flags, fd, offset); + target_prot, flags, fd, offset); if (ret == -1) { goto fail; } @@ -660,7 +670,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, if (end < real_end) { ret = mmap_frag(real_end - qemu_host_page_size, real_end - qemu_host_page_size, end, - host_prot, flags, fd, + target_prot, flags, fd, offset + real_end - qemu_host_page_size - start); if (ret == -1) { goto fail; @@ -678,7 +688,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, offset1 = offset + real_start - start; } p = mmap(g2h_untagged(real_start), real_end - real_start, - host_prot, flags, fd, offset1); + target_to_host_prot(target_prot), flags, fd, offset1); if (p == MAP_FAILED) { goto fail; }