mirror of https://github.com/xemu-project/xemu.git
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:
parent
037986053b
commit
0dd558121c
|
@ -69,24 +69,11 @@ void mmap_fork_end(int child)
|
||||||
* Return 0 if the target prot bitmask is invalid, otherwise
|
* Return 0 if the target prot bitmask is invalid, otherwise
|
||||||
* the internal qemu page_flags (which will include PAGE_VALID).
|
* 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 valid = PROT_READ | PROT_WRITE | PROT_EXEC | TARGET_PROT_SEM;
|
||||||
int page_flags = (prot & PAGE_BITS) | PAGE_VALID;
|
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
|
#ifdef TARGET_AARCH64
|
||||||
{
|
{
|
||||||
ARMCPU *cpu = ARM_CPU(thread_cpu);
|
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;
|
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. */
|
/* NOTE: all the constants are the HOST ones, but addresses are target. */
|
||||||
int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
|
int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
|
||||||
{
|
{
|
||||||
abi_ulong end, host_start, host_end, addr;
|
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);
|
trace_target_mprotect(start, len, target_prot);
|
||||||
|
|
||||||
if ((start & ~TARGET_PAGE_MASK) != 0) {
|
if ((start & ~TARGET_PAGE_MASK) != 0) {
|
||||||
return -TARGET_EINVAL;
|
return -TARGET_EINVAL;
|
||||||
}
|
}
|
||||||
page_flags = validate_prot_to_pageflags(&host_prot, target_prot);
|
page_flags = validate_prot_to_pageflags(target_prot);
|
||||||
if (!page_flags) {
|
if (!page_flags) {
|
||||||
return -TARGET_EINVAL;
|
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);
|
host_end = HOST_PAGE_ALIGN(end);
|
||||||
if (start > host_start) {
|
if (start > host_start) {
|
||||||
/* handle host page containing start */
|
/* handle host page containing start */
|
||||||
prot1 = host_prot;
|
prot1 = target_prot;
|
||||||
for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
|
||||||
prot1 |= page_get_flags(addr);
|
prot1 |= page_get_flags(addr);
|
||||||
}
|
}
|
||||||
|
@ -154,19 +157,19 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
|
||||||
end = host_end;
|
end = host_end;
|
||||||
}
|
}
|
||||||
ret = mprotect(g2h_untagged(host_start), qemu_host_page_size,
|
ret = mprotect(g2h_untagged(host_start), qemu_host_page_size,
|
||||||
prot1 & PAGE_BITS);
|
target_to_host_prot(prot1));
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
host_start += qemu_host_page_size;
|
host_start += qemu_host_page_size;
|
||||||
}
|
}
|
||||||
if (end < host_end) {
|
if (end < host_end) {
|
||||||
prot1 = host_prot;
|
prot1 = target_prot;
|
||||||
for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
|
||||||
prot1 |= page_get_flags(addr);
|
prot1 |= page_get_flags(addr);
|
||||||
}
|
}
|
||||||
ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
|
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) {
|
if (ret != 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
@ -175,8 +178,8 @@ int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
|
||||||
|
|
||||||
/* handle the pages in the middle */
|
/* handle the pages in the middle */
|
||||||
if (host_start < host_end) {
|
if (host_start < host_end) {
|
||||||
ret = mprotect(g2h_untagged(host_start),
|
ret = mprotect(g2h_untagged(host_start), host_end - host_start,
|
||||||
host_end - host_start, host_prot);
|
target_to_host_prot(target_prot));
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
@ -212,7 +215,8 @@ static int mmap_frag(abi_ulong real_start,
|
||||||
|
|
||||||
if (prot1 == 0) {
|
if (prot1 == 0) {
|
||||||
/* no page was there, so we allocate one */
|
/* 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);
|
flags | MAP_ANONYMOUS, -1, 0);
|
||||||
if (p == MAP_FAILED) {
|
if (p == MAP_FAILED) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -233,7 +237,8 @@ static int mmap_frag(abi_ulong real_start,
|
||||||
|
|
||||||
/* adjust protection to be able to read */
|
/* adjust protection to be able to read */
|
||||||
if (!(prot1 & PROT_WRITE)) {
|
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 */
|
/* read the corresponding file data */
|
||||||
|
@ -243,11 +248,13 @@ static int mmap_frag(abi_ulong real_start,
|
||||||
|
|
||||||
/* put final protection */
|
/* put final protection */
|
||||||
if (prot_new != (prot1 | PROT_WRITE)) {
|
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 {
|
} else {
|
||||||
if (prot_new != prot1) {
|
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) {
|
if (prot_new & PROT_WRITE) {
|
||||||
memset(g2h_untagged(start), 0, end - start);
|
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,
|
abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len,
|
||||||
passthrough_start = -1, passthrough_end = -1;
|
passthrough_start = -1, passthrough_end = -1;
|
||||||
int page_flags, host_prot;
|
int page_flags;
|
||||||
|
|
||||||
mmap_lock();
|
mmap_lock();
|
||||||
trace_target_mmap(start, len, target_prot, flags, fd, offset);
|
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;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
page_flags = validate_prot_to_pageflags(&host_prot, target_prot);
|
page_flags = validate_prot_to_pageflags(target_prot);
|
||||||
if (!page_flags) {
|
if (!page_flags) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
goto fail;
|
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))) {
|
if (!(flags & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
|
||||||
unsigned long host_start;
|
unsigned long host_start;
|
||||||
|
int host_prot;
|
||||||
void *p;
|
void *p;
|
||||||
|
|
||||||
host_len = len + offset - host_offset;
|
host_len = len + offset - host_offset;
|
||||||
host_len = HOST_PAGE_ALIGN(host_len);
|
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
|
* 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
|
* msync() won't work here, so we return an error if write is
|
||||||
* possible while it is a shared mapping
|
* 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;
|
errno = EINVAL;
|
||||||
goto fail;
|
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) {
|
if (pread(fd, g2h_untagged(start), len, offset) == -1) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
if (!(host_prot & PROT_WRITE)) {
|
if (!(target_prot & PROT_WRITE)) {
|
||||||
ret = target_mprotect(start, len, target_prot);
|
ret = target_mprotect(start, len, target_prot);
|
||||||
assert(ret == 0);
|
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) {
|
if (real_end == real_start + qemu_host_page_size) {
|
||||||
/* one single host page */
|
/* one single host page */
|
||||||
ret = mmap_frag(real_start, start, end,
|
ret = mmap_frag(real_start, start, end,
|
||||||
host_prot, flags, fd, offset);
|
target_prot, flags, fd, offset);
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
goto the_end1;
|
goto the_end1;
|
||||||
}
|
}
|
||||||
ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
|
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) {
|
if (ret == -1) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
@ -660,7 +670,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
|
||||||
if (end < real_end) {
|
if (end < real_end) {
|
||||||
ret = mmap_frag(real_end - qemu_host_page_size,
|
ret = mmap_frag(real_end - qemu_host_page_size,
|
||||||
real_end - qemu_host_page_size, end,
|
real_end - qemu_host_page_size, end,
|
||||||
host_prot, flags, fd,
|
target_prot, flags, fd,
|
||||||
offset + real_end - qemu_host_page_size - start);
|
offset + real_end - qemu_host_page_size - start);
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
goto fail;
|
goto fail;
|
||||||
|
@ -678,7 +688,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
|
||||||
offset1 = offset + real_start - start;
|
offset1 = offset + real_start - start;
|
||||||
}
|
}
|
||||||
p = mmap(g2h_untagged(real_start), real_end - real_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) {
|
if (p == MAP_FAILED) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue