[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 44/51] bsd-user: Implment madvise(2) to match the linux-user imple
From: |
Warner Losh |
Subject: |
[PULL 44/51] bsd-user: Implment madvise(2) to match the linux-user implementation. |
Date: |
Tue, 3 Oct 2023 17:32:08 -0600 |
From: Karim Taha <kariem.taha2.7@gmail.com>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20230925182709.4834-17-kariem.taha2.7@gmail.com>
---
bsd-user/bsd-mem.h | 53 +++++++++++++++++++++++++++++++++++
bsd-user/freebsd/os-syscall.c | 4 +++
bsd-user/syscall_defs.h | 2 ++
3 files changed, 59 insertions(+)
diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 16c22593bfd..b00ab3aed8e 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -129,6 +129,59 @@ 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)
+{
+ abi_ulong len;
+ int ret = 0;
+ abi_long start = arg1;
+ abi_long len_in = arg2;
+ abi_long advice = arg3;
+
+ if (start & ~TARGET_PAGE_MASK) {
+ return -TARGET_EINVAL;
+ }
+ if (len_in == 0) {
+ return 0;
+ }
+ len = TARGET_PAGE_ALIGN(len_in);
+ if (len == 0 || !guest_range_valid_untagged(start, len)) {
+ return -TARGET_EINVAL;
+ }
+
+ /*
+ * Most advice values are hints, so ignoring and returning success is ok.
+ *
+ * However, some advice values such as MADV_DONTNEED, are not hints and
+ * need to be emulated.
+ *
+ * A straight passthrough for those may not be safe because qemu sometimes
+ * turns private file-backed mappings into anonymous mappings.
+ * If all guest pages have PAGE_PASSTHROUGH set, mappings have the
+ * same semantics for the host as for the guest.
+ *
+ * MADV_DONTNEED is passed through, if possible.
+ * If passthrough isn't possible, we nevertheless (wrongly!) return
+ * success, which is broken but some userspace programs fail to work
+ * otherwise. Completely implementing such emulation is quite complicated
+ * though.
+ */
+ mmap_lock();
+ switch (advice) {
+ case MADV_DONTNEED:
+ if (page_check_range(start, len, PAGE_PASSTHROUGH)) {
+ ret = get_errno(madvise(g2h_untagged(start), len, advice));
+ if (ret == 0) {
+ page_reset_target_data(start, start + len - 1);
+ }
+ }
+ }
+ mmap_unlock();
+
+ return ret;
+}
+
/* minherit(2) */
static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
abi_long inherit)
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7a7ae26793f..b8c44cea0ff 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -831,6 +831,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num,
abi_long arg1,
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;
diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index ff692814333..52f84d5dd17 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -95,6 +95,8 @@ struct bsd_shm_regions {
/*
* sys/mman.h
*/
+#define TARGET_MADV_DONTNEED 4 /* dont need these pages */
+
#define TARGET_FREEBSD_MAP_RESERVED0080 0x0080 /* previously misimplemented */
/* MAP_INHERIT */
#define TARGET_FREEBSD_MAP_RESERVED0100 0x0100 /* previously unimplemented */
--
2.41.0
- [PULL 32/51] bsd-user: Introduce freebsd/os-misc.h to the source tree, (continued)
- [PULL 32/51] bsd-user: Introduce freebsd/os-misc.h to the source tree, Warner Losh, 2023/10/03
- [PULL 35/51] bsd-user: Add bsd-mem.c to meson.build, Warner Losh, 2023/10/03
- [PULL 42/51] bsd-user: Implement msync(2), Warner Losh, 2023/10/03
- [PULL 41/51] bsd-user: Implement mprotect(2), Warner Losh, 2023/10/03
- [PULL 43/51] bsd-user: Implement mlock(2), munlock(2), mlockall(2), munlockall(2), minherit(2), Warner Losh, 2023/10/03
- [PULL 38/51] bsd-user: Implement shmid_ds conversion between host and target., Warner Losh, 2023/10/03
- [PULL 45/51] bsd-user: Implement mincore(2), Warner Losh, 2023/10/03
- [PULL 49/51] bsd-user: Implement shmctl(2), Warner Losh, 2023/10/03
- [PULL 48/51] bsd-user: Implement shm_unlink(2) and shmget(2), Warner Losh, 2023/10/03
- [PULL 46/51] bsd-user: Implement do_obreak function, Warner Losh, 2023/10/03
- [PULL 44/51] bsd-user: Implment madvise(2) to match the linux-user implementation.,
Warner Losh <=
- [PULL 51/51] bsd-user: Add stubs for vadvise(), sbrk() and sstk(), Warner Losh, 2023/10/03
- [PULL 50/51] bsd-user: Implement shmat(2) and shmdt(2), Warner Losh, 2023/10/03
- [PULL 47/51] bsd-user: Implement shm_open(2), Warner Losh, 2023/10/03
- Re: [PULL 00/51] Bsd user mmap patches, Stefan Hajnoczi, 2023/10/05