[PATCH] user: Extract common MMAP API to 'user/mmap.h'

Philippe Mathieu-Daudé posted 1 patch 2 weeks, 4 days ago
bsd-user/qemu.h        | 12 +-----------
include/user/mmap.h    | 33 +++++++++++++++++++++++++++++++++
linux-user/user-mmap.h | 19 ++-----------------
3 files changed, 36 insertions(+), 28 deletions(-)
create mode 100644 include/user/mmap.h
[PATCH] user: Extract common MMAP API to 'user/mmap.h'
Posted by Philippe Mathieu-Daudé 2 weeks, 4 days ago
Keep common MMAP-related declarations in a single place.

Note, this disable ThreadSafetyAnalysis on Linux for:
- mmap_fork_start()
- mmap_fork_end().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 bsd-user/qemu.h        | 12 +-----------
 include/user/mmap.h    | 33 +++++++++++++++++++++++++++++++++
 linux-user/user-mmap.h | 19 ++-----------------
 3 files changed, 36 insertions(+), 28 deletions(-)
 create mode 100644 include/user/mmap.h

diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 3eaa14f3f56..a451d80a4db 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -32,6 +32,7 @@
 extern char **environ;
 
 #include "user/thunk.h"
+#include "user/mmap.h"
 #include "target_arch.h"
 #include "syscall_defs.h"
 #include "target_syscall.h"
@@ -234,19 +235,8 @@ void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
 extern int do_strace;
 
 /* mmap.c */
-int target_mprotect(abi_ulong start, abi_ulong len, int prot);
-abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
-                     int flags, int fd, off_t offset);
-int target_munmap(abi_ulong start, abi_ulong len);
-abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
-                       abi_ulong new_size, unsigned long flags,
-                       abi_ulong new_addr);
 int target_msync(abi_ulong start, abi_ulong len, int flags);
-extern abi_ulong mmap_next_start;
-abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
 void mmap_reserve(abi_ulong start, abi_ulong size);
-void TSA_NO_TSA mmap_fork_start(void);
-void TSA_NO_TSA mmap_fork_end(int child);
 
 /* main.c */
 extern char qemu_proc_pathname[];
diff --git a/include/user/mmap.h b/include/user/mmap.h
new file mode 100644
index 00000000000..93476a159d0
--- /dev/null
+++ b/include/user/mmap.h
@@ -0,0 +1,33 @@
+/*
+ * MMAP declarations for QEMU user emulation
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef USER_MMAP_H
+#define USER_MMAP_H
+
+#include "qemu/clang-tsa.h"
+#include "exec/user/abitypes.h"
+
+/*
+ * mmap_next_start: The base address for the next mmap without hint,
+ * increased after each successful map, starting at task_unmapped_base.
+ * This is an optimization within QEMU and not part of ADDR_COMPAT_LAYOUT.
+ */
+extern abi_ulong mmap_next_start;
+
+int target_mprotect(abi_ulong start, abi_ulong len, int prot);
+
+abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
+                     int flags, int fd, off_t offset);
+int target_munmap(abi_ulong start, abi_ulong len);
+abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
+                       abi_ulong new_size, unsigned long flags,
+                       abi_ulong new_addr);
+
+abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong);
+
+void TSA_NO_TSA mmap_fork_start(void);
+void TSA_NO_TSA mmap_fork_end(int child);
+
+#endif
diff --git a/linux-user/user-mmap.h b/linux-user/user-mmap.h
index b94bcdcf83c..dfc4477a720 100644
--- a/linux-user/user-mmap.h
+++ b/linux-user/user-mmap.h
@@ -18,6 +18,8 @@
 #ifndef LINUX_USER_USER_MMAP_H
 #define LINUX_USER_USER_MMAP_H
 
+#include "user/mmap.h"
+
 /*
  * Guest parameters for the ADDR_COMPAT_LAYOUT personality
  * (at present this is the only layout supported by QEMU).
@@ -39,24 +41,7 @@
 extern abi_ulong task_unmapped_base;
 extern abi_ulong elf_et_dyn_base;
 
-/*
- * mmap_next_start: The base address for the next mmap without hint,
- * increased after each successful map, starting at task_unmapped_base.
- * This is an optimization within QEMU and not part of ADDR_COMPAT_LAYOUT.
- */
-extern abi_ulong mmap_next_start;
-
-int target_mprotect(abi_ulong start, abi_ulong len, int prot);
-abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
-                     int flags, int fd, off_t offset);
-int target_munmap(abi_ulong start, abi_ulong len);
-abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
-                       abi_ulong new_size, unsigned long flags,
-                       abi_ulong new_addr);
 abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice);
-abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong);
-void mmap_fork_start(void);
-void mmap_fork_end(int child);
 
 abi_ulong target_shmat(CPUArchState *cpu_env, int shmid,
                        abi_ulong shmaddr, int shmflg);
-- 
2.47.1


Re: [PATCH] user: Extract common MMAP API to 'user/mmap.h'
Posted by Warner Losh 2 weeks, 3 days ago
On Fri, Jan 17, 2025 at 10:13 AM Philippe Mathieu-Daudé <philmd@linaro.org>
wrote:

> Keep common MMAP-related declarations in a single place.
>
> Note, this disable ThreadSafetyAnalysis on Linux for:
> - mmap_fork_start()
> - mmap_fork_end().
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  bsd-user/qemu.h        | 12 +-----------
>  include/user/mmap.h    | 33 +++++++++++++++++++++++++++++++++
>  linux-user/user-mmap.h | 19 ++-----------------
>  3 files changed, 36 insertions(+), 28 deletions(-)
>  create mode 100644 include/user/mmap.h
>

Reviewed-by: Warner Losh <imp@bsdimp.com>

There's actually rather a lot of things like this since the code lineages
are rather
common and co-evolved between bsd-user and linux-user.

Warner


> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
> index 3eaa14f3f56..a451d80a4db 100644
> --- a/bsd-user/qemu.h
> +++ b/bsd-user/qemu.h
> @@ -32,6 +32,7 @@
>  extern char **environ;
>
>  #include "user/thunk.h"
> +#include "user/mmap.h"
>  #include "target_arch.h"
>  #include "syscall_defs.h"
>  #include "target_syscall.h"
> @@ -234,19 +235,8 @@ void print_taken_signal(int target_signum, const
> target_siginfo_t *tinfo);
>  extern int do_strace;
>
>  /* mmap.c */
> -int target_mprotect(abi_ulong start, abi_ulong len, int prot);
> -abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
> -                     int flags, int fd, off_t offset);
> -int target_munmap(abi_ulong start, abi_ulong len);
> -abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
> -                       abi_ulong new_size, unsigned long flags,
> -                       abi_ulong new_addr);
>  int target_msync(abi_ulong start, abi_ulong len, int flags);
> -extern abi_ulong mmap_next_start;
> -abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
>  void mmap_reserve(abi_ulong start, abi_ulong size);
> -void TSA_NO_TSA mmap_fork_start(void);
> -void TSA_NO_TSA mmap_fork_end(int child);
>
>  /* main.c */
>  extern char qemu_proc_pathname[];
> diff --git a/include/user/mmap.h b/include/user/mmap.h
> new file mode 100644
> index 00000000000..93476a159d0
> --- /dev/null
> +++ b/include/user/mmap.h
> @@ -0,0 +1,33 @@
> +/*
> + * MMAP declarations for QEMU user emulation
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +#ifndef USER_MMAP_H
> +#define USER_MMAP_H
> +
> +#include "qemu/clang-tsa.h"
> +#include "exec/user/abitypes.h"
> +
> +/*
> + * mmap_next_start: The base address for the next mmap without hint,
> + * increased after each successful map, starting at task_unmapped_base.
> + * This is an optimization within QEMU and not part of ADDR_COMPAT_LAYOUT.
> + */
> +extern abi_ulong mmap_next_start;
> +
> +int target_mprotect(abi_ulong start, abi_ulong len, int prot);
> +
> +abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
> +                     int flags, int fd, off_t offset);
> +int target_munmap(abi_ulong start, abi_ulong len);
> +abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
> +                       abi_ulong new_size, unsigned long flags,
> +                       abi_ulong new_addr);
> +
> +abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong);
> +
> +void TSA_NO_TSA mmap_fork_start(void);
> +void TSA_NO_TSA mmap_fork_end(int child);
> +
> +#endif
> diff --git a/linux-user/user-mmap.h b/linux-user/user-mmap.h
> index b94bcdcf83c..dfc4477a720 100644
> --- a/linux-user/user-mmap.h
> +++ b/linux-user/user-mmap.h
> @@ -18,6 +18,8 @@
>  #ifndef LINUX_USER_USER_MMAP_H
>  #define LINUX_USER_USER_MMAP_H
>
> +#include "user/mmap.h"
> +
>  /*
>   * Guest parameters for the ADDR_COMPAT_LAYOUT personality
>   * (at present this is the only layout supported by QEMU).
> @@ -39,24 +41,7 @@
>  extern abi_ulong task_unmapped_base;
>  extern abi_ulong elf_et_dyn_base;
>
> -/*
> - * mmap_next_start: The base address for the next mmap without hint,
> - * increased after each successful map, starting at task_unmapped_base.
> - * This is an optimization within QEMU and not part of ADDR_COMPAT_LAYOUT.
> - */
> -extern abi_ulong mmap_next_start;
> -
> -int target_mprotect(abi_ulong start, abi_ulong len, int prot);
> -abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
> -                     int flags, int fd, off_t offset);
> -int target_munmap(abi_ulong start, abi_ulong len);
> -abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
> -                       abi_ulong new_size, unsigned long flags,
> -                       abi_ulong new_addr);
>  abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice);
> -abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong);
> -void mmap_fork_start(void);
> -void mmap_fork_end(int child);
>
>  abi_ulong target_shmat(CPUArchState *cpu_env, int shmid,
>                         abi_ulong shmaddr, int shmflg);
> --
> 2.47.1
>
>