linux-user: Split out target_to_host_prot

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>
This commit is contained in:
Richard Henderson 2023-07-07 21:40:40 +01:00
parent 037986053b
commit 0dd558121c

View File

@ -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);
@ -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;
}
@ -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);
}
@ -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;
}
@ -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;
}
@ -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;
@ -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 */
@ -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);
@ -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);
@ -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;
@ -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
@ -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;
}
@ -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);
}
@ -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;
}
@ -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;
@ -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;
}