Skip to content

Commit

Permalink
bsd-user: Implement do_obreak function
Browse files Browse the repository at this point in the history
Match linux-user, by manually applying the following commits, in order:

d28b3c9   linux-user: Make sure initial brk(0) is page-aligned
15ad985   linux-user: Fix qemu brk() to not zero bytes on current page
dfe4986   linux-user: Prohibit brk() to to shrink below initial heap address
eac78a4   linux-user: Fix signed math overflow in brk() syscall
c6cc059   linux-user: Do not call get_errno() in do_brk()
e69e032   linux-user: Use MAP_FIXED_NOREPLACE for do_brk()
cb9d5d1   linux-user: Do nothing if too small brk is specified
2aea137   linux-user: Do not align brk with host page size

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20230925182709.4834-19-kariem.taha2.7@gmail.com>
  • Loading branch information
staceyson authored and bsdimp committed Oct 3, 2023
1 parent 83b045a commit a99d740
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
45 changes: 45 additions & 0 deletions bsd-user/bsd-mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,49 @@ static inline abi_long do_bsd_mincore(abi_ulong target_addr, abi_ulong len,
return ret;
}

/* do_brk() must return target values and target errnos. */
static inline abi_long do_obreak(abi_ulong brk_val)
{
abi_long mapped_addr;
abi_ulong new_brk;
abi_ulong old_brk;

/* brk pointers are always untagged */

/* do not allow to shrink below initial brk value */
if (brk_val < initial_target_brk) {
return target_brk;
}

new_brk = TARGET_PAGE_ALIGN(brk_val);
old_brk = TARGET_PAGE_ALIGN(target_brk);

/* new and old target_brk might be on the same page */
if (new_brk == old_brk) {
target_brk = brk_val;
return target_brk;
}

/* Release heap if necesary */
if (new_brk < old_brk) {
target_munmap(new_brk, old_brk - new_brk);

target_brk = brk_val;
return target_brk;
}

mapped_addr = target_mmap(old_brk, new_brk - old_brk,
PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_EXCL | MAP_ANON | MAP_PRIVATE,
-1, 0);

if (mapped_addr == old_brk) {
target_brk = brk_val;
return target_brk;
}

/* For everything else, return the previous break. */
return target_brk;
}

#endif /* BSD_USER_BSD_MEM_H */
7 changes: 7 additions & 0 deletions bsd-user/freebsd/os-syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,13 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
break;
#endif

/*
* Misc
*/
case TARGET_FREEBSD_NR_break:
ret = do_obreak(arg1);
break;

/*
* sys{ctl, arch, call}
*/
Expand Down

0 comments on commit a99d740

Please sign in to comment.