[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 17/24] linux-user: Use 'last' instead of 'end' in target_mmap
From: |
Richard Henderson |
Subject: |
[PATCH 17/24] linux-user: Use 'last' instead of 'end' in target_mmap |
Date: |
Fri, 30 Jun 2023 15:21:52 +0200 |
Complete the transition within the mmap functions to a formulation
that does not overflow at the end of the address space.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
linux-user/mmap.c | 45 +++++++++++++++++++++++----------------------
1 file changed, 23 insertions(+), 22 deletions(-)
diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index 6ecdf9e56d..67a117823f 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -455,8 +455,8 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size,
abi_ulong align)
abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
int flags, int fd, off_t offset)
{
- abi_ulong ret, end, real_start, real_end, retaddr, host_len,
- passthrough_start = -1, passthrough_end = -1;
+ abi_ulong ret, last, real_start, real_last, retaddr, host_len;
+ abi_ulong passthrough_start = -1, passthrough_last = 0;
int page_flags;
off_t host_offset;
@@ -580,29 +580,30 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int
target_prot,
host_start += offset - host_offset;
}
start = h2g(host_start);
+ last = start + len - 1;
passthrough_start = start;
- passthrough_end = start + len;
+ passthrough_last = last;
} else {
if (start & ~TARGET_PAGE_MASK) {
errno = EINVAL;
goto fail;
}
- end = start + len;
- real_end = HOST_PAGE_ALIGN(end);
+ last = start + len - 1;
+ real_last = HOST_PAGE_ALIGN(last) - 1;
/*
* Test if requested memory area fits target address space
* It can fail only on 64-bit host with 32-bit target.
* On any other target/host host mmap() handles this error correctly.
*/
- if (end < start || !guest_range_valid_untagged(start, len)) {
+ if (last < start || !guest_range_valid_untagged(start, len)) {
errno = ENOMEM;
goto fail;
}
/* Validate that the chosen range is empty. */
if ((flags & MAP_FIXED_NOREPLACE)
- && !page_check_range_empty(start, end - 1)) {
+ && !page_check_range_empty(start, last)) {
errno = EEXIST;
goto fail;
}
@@ -642,9 +643,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int
target_prot,
/* handle the start of the mapping */
if (start > real_start) {
- if (real_end == real_start + qemu_host_page_size) {
+ if (real_last == real_start + qemu_host_page_size - 1) {
/* one single host page */
- if (!mmap_frag(real_start, start, end - 1,
+ if (!mmap_frag(real_start, start, last,
target_prot, flags, fd, offset)) {
goto fail;
}
@@ -658,18 +659,18 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int
target_prot,
real_start += qemu_host_page_size;
}
/* handle the end of the mapping */
- if (end < real_end) {
- if (!mmap_frag(real_end - qemu_host_page_size,
- real_end - qemu_host_page_size, end - 1,
+ if (last < real_last) {
+ abi_ulong real_page = real_last - qemu_host_page_size + 1;
+ if (!mmap_frag(real_page, real_page, last,
target_prot, flags, fd,
- offset + real_end - qemu_host_page_size - start)) {
+ offset + real_page - start)) {
goto fail;
}
- real_end -= qemu_host_page_size;
+ real_last -= qemu_host_page_size;
}
/* map the middle (easier) */
- if (real_start < real_end) {
+ if (real_start < real_last) {
void *p;
off_t offset1;
@@ -678,13 +679,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int
target_prot,
} else {
offset1 = offset + real_start - start;
}
- p = mmap(g2h_untagged(real_start), real_end - real_start,
+ p = mmap(g2h_untagged(real_start), real_last - real_start + 1,
target_to_host_prot(target_prot), flags, fd, offset1);
if (p == MAP_FAILED) {
goto fail;
}
passthrough_start = real_start;
- passthrough_end = real_end;
+ passthrough_last = real_last;
}
}
the_end1:
@@ -692,16 +693,16 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int
target_prot,
page_flags |= PAGE_ANON;
}
page_flags |= PAGE_RESET;
- if (passthrough_start == passthrough_end) {
- page_set_flags(start, start + len - 1, page_flags);
+ if (passthrough_start > passthrough_last) {
+ page_set_flags(start, last, page_flags);
} else {
if (start < passthrough_start) {
page_set_flags(start, passthrough_start - 1, page_flags);
}
- page_set_flags(passthrough_start, passthrough_end - 1,
+ page_set_flags(passthrough_start, passthrough_last,
page_flags | PAGE_PASSTHROUGH);
- if (passthrough_end < start + len) {
- page_set_flags(passthrough_end, start + len - 1, page_flags);
+ if (passthrough_last < last) {
+ page_set_flags(passthrough_last + 1, last, page_flags);
}
}
the_end:
--
2.34.1
- [PATCH 08/24] bsd-user: Use page_check_range_empty for MAP_EXCL, (continued)
- [PATCH 08/24] bsd-user: Use page_check_range_empty for MAP_EXCL, Richard Henderson, 2023/06/30
- [PATCH 11/24] linux-user: Widen target_mmap offset argument to off_t, Richard Henderson, 2023/06/30
- [PATCH 12/24] linux-user: Rewrite target_mprotect, Richard Henderson, 2023/06/30
- [PATCH 09/24] linux-user: Implement MAP_FIXED_NOREPLACE, Richard Henderson, 2023/06/30
- [PATCH 10/24] linux-user: Split out target_to_host_prot, Richard Henderson, 2023/06/30
- [PATCH 14/24] accel/tcg: Introduce page_find_range_empty, Richard Henderson, 2023/06/30
- [PATCH 13/24] linux-user: Rewrite mmap_frag, Richard Henderson, 2023/06/30
- [PATCH 18/24] linux-user: Rewrite mmap_reserve, Richard Henderson, 2023/06/30
- [PATCH 15/24] bsd-user: Use page_find_range_empty for mmap_find_vma_reserved, Richard Henderson, 2023/06/30
- [PATCH 16/24] linux-user: Use page_find_range_empty for mmap_find_vma_reserved, Richard Henderson, 2023/06/30
- [PATCH 17/24] linux-user: Use 'last' instead of 'end' in target_mmap,
Richard Henderson <=
- [PATCH 20/24] linux-user: Simplify target_munmap, Richard Henderson, 2023/06/30
- [PATCH 21/24] accel/tcg: Accept more page flags in page_check_range, Richard Henderson, 2023/06/30
- [PATCH 19/24] linux-user: Rename mmap_reserve to mmap_reserve_or_unmap, Richard Henderson, 2023/06/30
- [PATCH 22/24] accel/tcg: Return bool from page_check_range, Richard Henderson, 2023/06/30
- [PATCH 23/24] linux-user: Remove can_passthrough_madvise, Richard Henderson, 2023/06/30
- [PATCH 24/24] linux-user: Simplify target_madvise, Richard Henderson, 2023/06/30