Skip to content

Commit

Permalink
linux-user: Split out target_to_host_prot
Browse files Browse the repository at this point in the history
Split out from validate_prot_to_pageflags, as there is not
one single host_prot for the entire range.  We need to adjust
prot for every host page that overlaps multiple guest pages.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20230707204054.8792-13-richard.henderson@linaro.org>
  • Loading branch information
rth7680 committed Jul 15, 2023
1 parent 0379860 commit 0dd5581
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions linux-user/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,11 @@ void mmap_fork_end(int child)
* Return 0 if the target prot bitmask is invalid, otherwise
* the internal qemu page_flags (which will include PAGE_VALID).
*/
static int validate_prot_to_pageflags(int *host_prot, int prot)
static int validate_prot_to_pageflags(int prot)
{
int valid = PROT_READ | PROT_WRITE | PROT_EXEC | TARGET_PROT_SEM;
int page_flags = (prot & PAGE_BITS) | PAGE_VALID;

/*
* For the host, we need not pass anything except read/write/exec.
* While PROT_SEM is allowed by all hosts, it is also ignored, so
* don't bother transforming guest bit to host bit. Any other
* target-specific prot bits will not be understood by the host
* and will need to be encoded into page_flags for qemu emulation.
*
* Pages that are executable by the guest will never be executed
* by the host, but the host will need to be able to read them.
*/
*host_prot = (prot & (PROT_READ | PROT_WRITE))
| (prot & PROT_EXEC ? PROT_READ : 0);

#ifdef TARGET_AARCH64
{
ARMCPU *cpu = ARM_CPU(thread_cpu);
Expand Down Expand Up @@ -114,18 +101,34 @@ static int validate_prot_to_pageflags(int *host_prot, int prot)
return prot & ~valid ? 0 : page_flags;
}

/*
* For the host, we need not pass anything except read/write/exec.
* While PROT_SEM is allowed by all hosts, it is also ignored, so
* don't bother transforming guest bit to host bit. Any other
* target-specific prot bits will not be understood by the host
* and will need to be encoded into page_flags for qemu emulation.
*
* Pages that are executable by the guest will never be executed
* by the host, but the host will need to be able to read them.
*/
static int target_to_host_prot(int prot)
{
return (prot & (PROT_READ | PROT_WRITE)) |
(prot & PROT_EXEC ? PROT_READ : 0);
}

/* NOTE: all the constants are the HOST ones, but addresses are target. */
int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
{
abi_ulong end, host_start, host_end, addr;
int prot1, ret, page_flags, host_prot;
int prot1, ret, page_flags;

trace_target_mprotect(start, len, target_prot);

if ((start & ~TARGET_PAGE_MASK) != 0) {
return -TARGET_EINVAL;
}
page_flags = validate_prot_to_pageflags(&host_prot, target_prot);
page_flags = validate_prot_to_pageflags(target_prot);
if (!page_flags) {
return -TARGET_EINVAL;
}
Expand All @@ -143,7 +146,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
host_end = HOST_PAGE_ALIGN(end);
if (start > host_start) {
/* handle host page containing start */
prot1 = host_prot;
prot1 = target_prot;
for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
prot1 |= page_get_flags(addr);
}
Expand All @@ -154,19 +157,19 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
end = host_end;
}
ret = mprotect(g2h_untagged(host_start), qemu_host_page_size,
prot1 & PAGE_BITS);
target_to_host_prot(prot1));
if (ret != 0) {
goto error;
}
host_start += qemu_host_page_size;
}
if (end < host_end) {
prot1 = host_prot;
prot1 = target_prot;
for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
prot1 |= page_get_flags(addr);
}
ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
qemu_host_page_size, prot1 & PAGE_BITS);
qemu_host_page_size, target_to_host_prot(prot1));
if (ret != 0) {
goto error;
}
Expand All @@ -175,8 +178,8 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)

/* handle the pages in the middle */
if (host_start < host_end) {
ret = mprotect(g2h_untagged(host_start),
host_end - host_start, host_prot);
ret = mprotect(g2h_untagged(host_start), host_end - host_start,
target_to_host_prot(target_prot));
if (ret != 0) {
goto error;
}
Expand Down Expand Up @@ -212,7 +215,8 @@ static int mmap_frag(abi_ulong real_start,

if (prot1 == 0) {
/* no page was there, so we allocate one */
void *p = mmap(host_start, qemu_host_page_size, prot,
void *p = mmap(host_start, qemu_host_page_size,
target_to_host_prot(prot),
flags | MAP_ANONYMOUS, -1, 0);
if (p == MAP_FAILED) {
return -1;
Expand All @@ -233,7 +237,8 @@ static int mmap_frag(abi_ulong real_start,

/* adjust protection to be able to read */
if (!(prot1 & PROT_WRITE)) {
mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
mprotect(host_start, qemu_host_page_size,
target_to_host_prot(prot1) | PROT_WRITE);
}

/* read the corresponding file data */
Expand All @@ -243,11 +248,13 @@ static int mmap_frag(abi_ulong real_start,

/* put final protection */
if (prot_new != (prot1 | PROT_WRITE)) {
mprotect(host_start, qemu_host_page_size, prot_new);
mprotect(host_start, qemu_host_page_size,
target_to_host_prot(prot_new));
}
} else {
if (prot_new != prot1) {
mprotect(host_start, qemu_host_page_size, prot_new);
mprotect(host_start, qemu_host_page_size,
target_to_host_prot(prot_new));
}
if (prot_new & PROT_WRITE) {
memset(g2h_untagged(start), 0, end - start);
Expand Down Expand Up @@ -460,7 +467,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
{
abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len,
passthrough_start = -1, passthrough_end = -1;
int page_flags, host_prot;
int page_flags;

mmap_lock();
trace_target_mmap(start, len, target_prot, flags, fd, offset);
Expand All @@ -470,7 +477,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
goto fail;
}

page_flags = validate_prot_to_pageflags(&host_prot, target_prot);
page_flags = validate_prot_to_pageflags(target_prot);
if (!page_flags) {
errno = EINVAL;
goto fail;
Expand Down Expand Up @@ -553,10 +560,12 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,

if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
unsigned long host_start;
int host_prot;
void *p;

host_len = len + offset - host_offset;
host_len = HOST_PAGE_ALIGN(host_len);
host_prot = target_to_host_prot(target_prot);

/*
* Note: we prefer to control the mapping address. It is
Expand Down Expand Up @@ -617,7 +626,8 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
* msync() won't work here, so we return an error if write is
* possible while it is a shared mapping
*/
if ((flags & MAP_TYPE) == MAP_SHARED && (host_prot & PROT_WRITE)) {
if ((flags & MAP_TYPE) == MAP_SHARED
&& (target_prot & PROT_WRITE)) {
errno = EINVAL;
goto fail;
}
Expand All @@ -631,7 +641,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
if (pread(fd, g2h_untagged(start), len, offset) == -1) {
goto fail;
}
if (!(host_prot & PROT_WRITE)) {
if (!(target_prot & PROT_WRITE)) {
ret = target_mprotect(start, len, target_prot);
assert(ret == 0);
}
Expand All @@ -643,14 +653,14 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
if (real_end == real_start + qemu_host_page_size) {
/* one single host page */
ret = mmap_frag(real_start, start, end,
host_prot, flags, fd, offset);
target_prot, flags, fd, offset);
if (ret == -1) {
goto fail;
}
goto the_end1;
}
ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
host_prot, flags, fd, offset);
target_prot, flags, fd, offset);
if (ret == -1) {
goto fail;
}
Expand All @@ -660,7 +670,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
if (end < real_end) {
ret = mmap_frag(real_end - qemu_host_page_size,
real_end - qemu_host_page_size, end,
host_prot, flags, fd,
target_prot, flags, fd,
offset + real_end - qemu_host_page_size - start);
if (ret == -1) {
goto fail;
Expand All @@ -678,7 +688,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
offset1 = offset + real_start - start;
}
p = mmap(g2h_untagged(real_start), real_end - real_start,
host_prot, flags, fd, offset1);
target_to_host_prot(target_prot), flags, fd, offset1);
if (p == MAP_FAILED) {
goto fail;
}
Expand Down

0 comments on commit 0dd5581

Please sign in to comment.