diff mbox series

[15/22] Implement mlock(2), munlock(2), mlockall(2), munlockall(2), madvise(2), minherit(2)

Message ID 20230819094806.14965-16-kariem.taha2.7@gmail.com
State New
Headers show
Series Implement the mmap system call for FreeBSD. | expand

Commit Message

Karim Taha Aug. 19, 2023, 9:47 a.m. UTC
From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-mem.h            | 44 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 24 +++++++++++++++++++
 2 files changed, 68 insertions(+)

Comments

Warner Losh Aug. 20, 2023, 4:37 a.m. UTC | #1
On Sat, Aug 19, 2023 at 3:49 AM Karim Taha <kariem.taha2.7@gmail.com> wrote:

> From: Stacey Son <sson@FreeBSD.org>
>
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>  bsd-user/bsd-mem.h            | 44 +++++++++++++++++++++++++++++++++++
>  bsd-user/freebsd/os-syscall.c | 24 +++++++++++++++++++
>  2 files changed, 68 insertions(+)
>
> diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
> index 68d79ac080..f76881519c 100644
> --- a/bsd-user/bsd-mem.h
> +++ b/bsd-user/bsd-mem.h
> @@ -100,4 +100,48 @@ static inline abi_long do_bsd_msync(abi_long addr,
> abi_long len, abi_long flags)
>      return get_errno(msync(g2h_untagged(addr), len, flags));
>  }
>
> +/* mlock(2) */
> +static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2)
> +{
> +    return get_errno(mlock(g2h_untagged(arg1), arg2));
> +}
> +
> +/* munlock(2) */
> +static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2)
> +{
> +    return get_errno(munlock(g2h_untagged(arg1), arg2));
> +}
> +
> +/* mlockall(2) */
> +static inline abi_long do_bsd_mlockall(abi_long arg1)
> +{
> +    return get_errno(mlockall(arg1));
> +}
> +
> +/* munlockall(2) */
> +static inline abi_long do_bsd_munlockall(void)
> +{
> +    return get_errno(munlockall());
> +}
> +
> +/* madvise(2) */
> +static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2,
> +        abi_long arg3)
> +{
> +    /*
> +     * A straight passthrough may not be safe because qemu sometimes
> +     * turns private file-backed mapping into anonymous mappings. This
> +     * will break MADV_DONTNEED.  This is a hint, so ignoring and returing
> +     * success is ok.
> +     */
> +    return get_errno(0);
>

This looks like it was copied from an early linux-user implementation, and
that seems to have been fixed to no longer cause problems. Can someone
that knows about the linux-user history here comment?

Warner


> +}
> +
> +/* minherit(2) */
> +static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
> +        abi_long inherit)
> +{
> +    return get_errno(minherit(g2h_untagged(addr), len, inherit));
> +}
> +
>  #endif /* BSD_USER_BSD_MEM_H */
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index 3871b15309..96469f6a63 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -503,6 +503,30 @@ static abi_long freebsd_syscall(void *cpu_env, int
> num, abi_long arg1,
>          ret = do_bsd_msync(arg1, arg2, arg3);
>          break;
>
> +    case TARGET_FREEBSD_NR_mlock: /* mlock(2) */
> +        ret = do_bsd_mlock(arg1, arg2);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_munlock: /* munlock(2) */
> +        ret = do_bsd_munlock(arg1, arg2);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_mlockall: /* mlockall(2) */
> +        ret = do_bsd_mlockall(arg1);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_munlockall: /* munlockall(2) */
> +        ret = do_bsd_munlockall();
> +        break;
> +
> +    case TARGET_FREEBSD_NR_madvise: /* madvise(2) */
> +        ret = do_bsd_madvise(arg1, arg2, arg3);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_minherit: /* minherit(2) */
> +        ret = do_bsd_minherit(arg1, arg2, arg3);
> +        break;
> +
>  #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
>      case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
>          ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
> --
> 2.40.0
>
>
Richard Henderson Aug. 20, 2023, 2:42 p.m. UTC | #2
On 8/19/23 21:37, Warner Losh wrote:
>     +/* madvise(2) */
>     +static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2,
>     +        abi_long arg3)
>     +{
>     +    /*
>     +     * A straight passthrough may not be safe because qemu sometimes
>     +     * turns private file-backed mapping into anonymous mappings. This
>     +     * will break MADV_DONTNEED.  This is a hint, so ignoring and returing
>     +     * success is ok.
>     +     */
>     +    return get_errno(0);
> 
> 
> This looks like it was copied from an early linux-user implementation, and
> that seems to have been fixed to no longer cause problems. Can someone
> that knows about the linux-user history here comment?

We now track pages that are "passthrough" and ok for DONTNEED etc.


     case MADV_DONTNEED:
         if (page_check_range(start, len, PAGE_PASSTHROUGH)) {
             ret = get_errno(madvise(g2h_untagged(start), len, advice));
             if ((advice == MADV_DONTNEED) && (ret == 0)) {
                 page_reset_target_data(start, start + len - 1);
             }
         }


It's still not ideal, but it's something.


r~
Richard Henderson Aug. 20, 2023, 2:43 p.m. UTC | #3
On 8/19/23 02:47, Karim Taha wrote:
> +static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2)
> +{
> +    return get_errno(mlock(g2h_untagged(arg1), arg2));
> +}
> +
> +/* munlock(2) */
> +static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2)
> +{
> +    return get_errno(munlock(g2h_untagged(arg1), arg2));
> +}

I think these two need guest_range_valid_untagged.

Otherwise,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~
diff mbox series

Patch

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 68d79ac080..f76881519c 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -100,4 +100,48 @@  static inline abi_long do_bsd_msync(abi_long addr, abi_long len, abi_long flags)
     return get_errno(msync(g2h_untagged(addr), len, flags));
 }
 
+/* mlock(2) */
+static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2)
+{
+    return get_errno(mlock(g2h_untagged(arg1), arg2));
+}
+
+/* munlock(2) */
+static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2)
+{
+    return get_errno(munlock(g2h_untagged(arg1), arg2));
+}
+
+/* mlockall(2) */
+static inline abi_long do_bsd_mlockall(abi_long arg1)
+{
+    return get_errno(mlockall(arg1));
+}
+
+/* munlockall(2) */
+static inline abi_long do_bsd_munlockall(void)
+{
+    return get_errno(munlockall());
+}
+
+/* madvise(2) */
+static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2,
+        abi_long arg3)
+{
+    /*
+     * A straight passthrough may not be safe because qemu sometimes
+     * turns private file-backed mapping into anonymous mappings. This
+     * will break MADV_DONTNEED.  This is a hint, so ignoring and returing
+     * success is ok.
+     */
+    return get_errno(0);
+}
+
+/* minherit(2) */
+static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
+        abi_long inherit)
+{
+    return get_errno(minherit(g2h_untagged(addr), len, inherit));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 3871b15309..96469f6a63 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -503,6 +503,30 @@  static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_msync(arg1, arg2, arg3);
         break;
 
+    case TARGET_FREEBSD_NR_mlock: /* mlock(2) */
+        ret = do_bsd_mlock(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_munlock: /* munlock(2) */
+        ret = do_bsd_munlock(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_mlockall: /* mlockall(2) */
+        ret = do_bsd_mlockall(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_munlockall: /* munlockall(2) */
+        ret = do_bsd_munlockall();
+        break;
+
+    case TARGET_FREEBSD_NR_madvise: /* madvise(2) */
+        ret = do_bsd_madvise(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_minherit: /* minherit(2) */
+        ret = do_bsd_minherit(arg1, arg2, arg3);
+        break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
     case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
         ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);