qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>
Subject: [PULL 29/47] linux-user: Split out target_to_host_prot
Date: Sat, 15 Jul 2023 14:52:59 +0100	[thread overview]
Message-ID: <20230715135317.7219-30-richard.henderson@linaro.org> (raw)
In-Reply-To: <20230715135317.7219-1-richard.henderson@linaro.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 <alex.bennee@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
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;
             }
-- 
2.34.1



  parent reply	other threads:[~2023-07-15 13:54 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-15 13:52 [PULL 00/47] tcg + linux-user patch queue Richard Henderson
2023-07-15 13:52 ` [PULL 01/47] linux-user: Reformat syscall_defs.h Richard Henderson
2023-07-15 13:52 ` [PULL 02/47] linux-user: Remove #if 0 block in syscall_defs.h Richard Henderson
2023-07-15 13:52 ` [PULL 03/47] linux-user: Use abi_uint not uint32_t " Richard Henderson
2023-07-15 13:52 ` [PULL 04/47] linux-user: Use abi_int not int32_t " Richard Henderson
2023-07-15 13:52 ` [PULL 05/47] linux-user: Use abi_ullong not uint64_t " Richard Henderson
2023-07-15 13:52 ` [PULL 06/47] linux-user: Use abi_llong not int64_t " Richard Henderson
2023-07-15 13:52 ` [PULL 07/47] linux-user: Use abi_uint not unsigned int " Richard Henderson
2023-07-15 13:52 ` [PULL 08/47] linux-user: Use abi_ullong not unsigned long long " Richard Henderson
2023-07-15 13:52 ` [PULL 09/47] linux-user: Use abi_llong not " Richard Henderson
2023-07-15 13:52 ` [PULL 10/47] linux-user: Use abi_int not int " Richard Henderson
2023-07-15 13:52 ` [PULL 11/47] linux-user: Use abi_ushort not unsigned short " Richard Henderson
2023-07-15 13:52 ` [PULL 12/47] linux-user: Use abi_short not " Richard Henderson
2023-07-15 13:52 ` [PULL 13/47] linux-user: Use abi_uint not unsigned " Richard Henderson
2023-07-15 13:52 ` [PULL 14/47] include/exec/user: Set ABI_LLONG_ALIGNMENT to 4 for microblaze Richard Henderson
2023-07-15 13:52 ` [PULL 15/47] include/exec/user: Set ABI_LLONG_ALIGNMENT to 4 for nios2 Richard Henderson
2023-08-08  7:19   ` Michael Tokarev
2023-07-15 13:52 ` [PULL 16/47] linux-user/syscall: Implement execve without execveat Richard Henderson
2023-07-15 13:52 ` [PULL 17/47] linux-user: Fix do_shmat type errors Richard Henderson
2023-07-15 13:52 ` [PULL 18/47] accel/tcg: Split out cpu_exec_longjmp_cleanup Richard Henderson
2023-07-15 13:52 ` [PULL 19/47] tcg: Fix info_in_idx increment in layout_arg_by_ref Richard Henderson
2023-07-15 13:52 ` [PULL 20/47] linux-user: Make sure initial brk(0) is page-aligned Richard Henderson
2023-07-16 18:15   ` Michael Tokarev
2023-07-17 14:42     ` Richard Henderson
2023-07-17 14:57       ` Michael Tokarev
2023-07-18 11:51       ` Michael Tokarev
2023-07-15 13:52 ` [PULL 21/47] linux-user: Fix formatting of mmap.c Richard Henderson
2023-07-15 13:52 ` [PULL 22/47] linux-user/strace: Expand struct flags to hold a mask Richard Henderson
2023-07-15 13:52 ` [PULL 23/47] linux-user: Split TARGET_MAP_* out of syscall_defs.h Richard Henderson
2023-07-15 13:52 ` [PULL 24/47] linux-user: Split TARGET_PROT_* " Richard Henderson
2023-07-15 13:52 ` [PULL 25/47] linux-user: Populate more bits in mmap_flags_tbl Richard Henderson
2023-07-15 13:52 ` [PULL 26/47] accel/tcg: Introduce page_check_range_empty Richard Henderson
2023-07-15 13:52 ` [PULL 27/47] bsd-user: Use page_check_range_empty for MAP_EXCL Richard Henderson
2023-07-15 13:52 ` [PULL 28/47] linux-user: Implement MAP_FIXED_NOREPLACE Richard Henderson
2023-07-15 13:52 ` Richard Henderson [this message]
2023-07-15 13:53 ` [PULL 30/47] linux-user: Widen target_mmap offset argument to off_t Richard Henderson
2023-07-15 13:53 ` [PULL 31/47] linux-user: Rewrite target_mprotect Richard Henderson
2023-07-15 13:53 ` [PULL 32/47] linux-user: Rewrite mmap_frag Richard Henderson
2023-07-15 13:53 ` [PULL 33/47] accel/tcg: Introduce page_find_range_empty Richard Henderson
2023-07-15 13:53 ` [PULL 34/47] bsd-user: Use page_find_range_empty for mmap_find_vma_reserved Richard Henderson
2023-07-15 13:53 ` [PULL 35/47] linux-user: " Richard Henderson
2023-07-18  9:07   ` Laurent Vivier
2023-07-15 13:53 ` [PULL 36/47] linux-user: Use 'last' instead of 'end' in target_mmap Richard Henderson
2023-07-15 13:53 ` [PULL 37/47] linux-user: Rewrite mmap_reserve Richard Henderson
2023-07-15 13:53 ` [PULL 38/47] linux-user: Rename mmap_reserve to mmap_reserve_or_unmap Richard Henderson
2023-09-18  8:35   ` Andreas Schwab
2023-10-03 21:03     ` Richard Henderson
2023-07-15 13:53 ` [PULL 39/47] linux-user: Simplify target_munmap Richard Henderson
2023-07-15 13:53 ` [PULL 40/47] accel/tcg: Accept more page flags in page_check_range Richard Henderson
2023-07-15 13:53 ` [PULL 41/47] accel/tcg: Return bool from page_check_range Richard Henderson
2023-07-15 13:53 ` [PULL 42/47] linux-user: Remove can_passthrough_madvise Richard Henderson
2023-07-15 13:53 ` [PULL 43/47] linux-user: Simplify target_madvise Richard Henderson
2023-07-15 13:53 ` [PULL 44/47] linux-user: Drop uint and ulong Richard Henderson
2023-07-15 13:53 ` [PULL 45/47] linux-user/arm: Do not allocate a commpage at all for M-profile CPUs Richard Henderson
2023-07-16 18:13   ` Michael Tokarev
2023-07-17 14:42     ` Richard Henderson
2023-07-15 13:53 ` [PULL 46/47] accel/tcg: Always lock pages before translation Richard Henderson
2023-07-15 13:53 ` [PULL 47/47] tcg: Use HAVE_CMPXCHG128 instead of CONFIG_CMPXCHG128 Richard Henderson
2023-07-16 16:49 ` [PULL 00/47] tcg + linux-user patch queue Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230715135317.7219-30-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).