On 8/19/23 02:48, Karim Taha wrote:
From: Stacey Son <s...@freebsd.org>
Co-authored-by: Mikaël Urankar <mikael.uran...@gmail.com>
Signed-off-by: Mikaël Urankar <mikael.uran...@gmail.com>
Signed-off-by: Stacey Son <s...@freebsd.org>
Signed-off-by: Karim Taha <kariem.taha...@gmail.com>
---
bsd-user/bsd-mem.h | 85 +++++++++++++++++++++++++++++++++++
bsd-user/freebsd/os-syscall.c | 7 +++
2 files changed, 92 insertions(+)
diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index edbccd3111..6f33148eb7 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -167,4 +167,89 @@ static inline abi_long do_bsd_mincore(abi_ulong
target_addr, abi_ulong len,
return ret;
}
+#ifdef DO_DEBUG
+#define DEBUGF_BRK(message, args...) \
+ do { fprintf(stderr, (message), ## args); } while (0)
+#else
+#define DEBUGF_BRK(message, args...)
+#endif
We are trying to get rid of all of this old-style debugging.
We are replacing them with tracepoints.
+
+/* do_brk() must return target values and target errnos. */
+static inline abi_long do_obreak(abi_ulong new_brk)
+{
+ abi_long mapped_addr;
+ int new_alloc_size;
+
+ DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
+
+ if (!new_brk) {
+ DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", bsd_target_brk);
+ return bsd_target_brk;
+ }
+ if (new_brk < bsd_target_original_brk) {
+ DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < bsd_target_original_brk)\n",
+ bsd_target_brk);
+ return bsd_target_brk;
+ }
These two cases may be combined, since original_brk will never be 0.
+
+ /*
+ * If the new brk is less than the highest page reserved to the target heap
+ * allocation, set it and we're almost done...
+ */
+ if (new_brk <= brk_page) {
+ /*
+ * Heap contents are initialized to zero, as for anonymous mapped
pages.
+ */
+ if (new_brk > bsd_target_brk) {
+ memset(g2h_untagged(bsd_target_brk), 0, new_brk - bsd_target_brk);
+ }
+ bsd_target_brk = new_brk;
+ DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n",
+ bsd_target_brk);
+ return bsd_target_brk;
+ }
+
+ /*
+ * We need to allocate more memory after the brk... Note that we don't use
+ * MAP_FIXED because that will map over the top of any existing mapping
+ * (like the one with the host libc or qemu itself); instead we treat
+ * "mapped but at wrong address" as a failure and unmap again.
+ */
+ new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
We removed all HOST_PAGE_ALIGN from brk in linux-user this cycle.
+ mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
+ PROT_READ | PROT_WRITE,
+ MAP_ANON | MAP_PRIVATE, -1, 0));
+
+ if (mapped_addr == brk_page) {
+ /*
+ * Heap contents are initialized to zero, as for anonymous mapped
pages.
+ * Technically the new pages are already initialized to zero since they
+ * *are* anonymous mapped pages, however we have to take care with the
+ * contents that come from the remaining part of the previous page: it
+ * may contains garbage data due to a previous heap usage (grown then
+ * shrunken).
+ */
+ memset(g2h_untagged(bsd_target_brk), 0, brk_page - bsd_target_brk);
+
+ bsd_target_brk = new_brk;
+ brk_page = HOST_PAGE_ALIGN(bsd_target_brk);
+ DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
+ bsd_target_brk);
+ return bsd_target_brk;
+ } else if (mapped_addr != -1) {
+ /*
+ * Mapped but at wrong address, meaning there wasn't actually enough
+ * space for this brk.
+ */
+ target_munmap(mapped_addr, new_alloc_size);
+ mapped_addr = -1;
+ DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", bsd_target_brk);
+ } else {
+ DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", bsd_target_brk);
+ }
It may require additional work in target_mmap, but you probably want to use MAP_FIXED |
MAP_EXCL here. On the linux-user side we changed to use MAP_FIXED_NOREPLACE, which is
approximately the same thing.
r~