Add clock_getres_time64, timer_gettime64, timer_settime64,

timerfd_gettime64, timerfd_settime64
 Some fixes (page protection, print_fdset, timerspec, itimerspec)
 -----BEGIN PGP SIGNATURE-----
 
 iQJGBAABCAAwFiEEzS913cjjpNwuT1Fz8ww4vT8vvjwFAl9ChC0SHGxhdXJlbnRA
 dml2aWVyLmV1AAoJEPMMOL0/L748DLwP/2xnxFVehyqQwT4rsmnHE1Zz8FljG9k/
 SQVV3gqCPtush+wpf+48HBS8LgWQHRT92cuweRrKfe/9T4hjmqCHxULVnpXeKskE
 O07V/b8oeZFgNP1RWoEDmpXEAxR4SmD8sHXmFmAOgK46lj0Ece5w/bakix74hs1L
 XPIguAwRYocEbdbjvGeq4PwbwS/C5ISWylIoK8pwjGm6oRAEKsSXDT1cxKNXhA/D
 F/do/cvo0S1SlNQXzlcFBarw2kpr7djL7PwUqzcA6knoax300YgMA6BF5FaeVPP4
 Yrmud7dBBApsvChny4KnvWbpFyvabW/ldyLJn7lrKiEKDHamIM3I5/xFV4KZ00bq
 Zjyc/YwrerGB7qnmxSvKfmtOcmxGqcQF5qyPJGx4HRbdVQD5mvoxyieaTXjWoXxu
 sEePfz237XcsCoBre1r9eZuYFdGik354CF75LaT7jId7+mWt7nG5P022tXuW8Ohb
 /eDRCIVh5pbSMZDrDTVq5sw08PYAKSkv/7cm7t289Y1yMRfQ4YCphErgoduRc1yB
 uZgi5paqdoA3h/2oikMGxijJr65IL/+2CXLcaY32E+JdRhQ45Gal7R6U10iiDdGb
 Nq3CWSjMWEGbbbFRwczf6HianroMtbj57W7Brhi623ZmRvoZqVQmBnRvCoJs9xSe
 B/ubdy6gZfTD
 =O/cs
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/vivier2/tags/linux-user-for-5.2-pull-request' into staging

Add clock_getres_time64, timer_gettime64, timer_settime64,
    timerfd_gettime64, timerfd_settime64
Some fixes (page protection, print_fdset, timerspec, itimerspec)

# gpg: Signature made Sun 23 Aug 2020 15:58:53 BST
# gpg:                using RSA key CD2F75DDC8E3A4DC2E4F5173F30C38BD3F2FBE3C
# gpg:                issuer "laurent@vivier.eu"
# gpg: Good signature from "Laurent Vivier <lvivier@redhat.com>" [full]
# gpg:                 aka "Laurent Vivier <laurent@vivier.eu>" [full]
# gpg:                 aka "Laurent Vivier (Red Hat) <lvivier@redhat.com>" [full]
# Primary key fingerprint: CD2F 75DD C8E3 A4DC 2E4F  5173 F30C 38BD 3F2F BE3C

* remotes/vivier2/tags/linux-user-for-5.2-pull-request:
  linux-user: Fix 'utimensat()' implementation
  linux-user: Add support for a group of 2038 safe syscalls
  linux-user: Modify 'target_to_host/host_to_target_itimerspec()'
  linux-user: Adjust guest page protection for the host
  linux-user: Validate mmap/mprotect prot value
  linux-user: Fix "print_fdset()" in "strace.c" to not print ", " after last value

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell 2020-08-23 16:34:43 +01:00
commit 8367a77c4d
4 changed files with 249 additions and 62 deletions

View File

@ -59,64 +59,100 @@ void mmap_fork_end(int child)
pthread_mutex_unlock(&mmap_mutex);
}
/*
* Validate target prot bitmask.
* Return the prot bitmask for the host in *HOST_PROT.
* 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)
{
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);
return prot & ~valid ? 0 : page_flags;
}
/* NOTE: all the constants are the HOST ones, but addresses are target. */
int target_mprotect(abi_ulong start, abi_ulong len, int prot)
int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
{
abi_ulong end, host_start, host_end, addr;
int prot1, ret;
int prot1, ret, page_flags, host_prot;
trace_target_mprotect(start, len, prot);
trace_target_mprotect(start, len, target_prot);
if ((start & ~TARGET_PAGE_MASK) != 0)
if ((start & ~TARGET_PAGE_MASK) != 0) {
return -TARGET_EINVAL;
}
page_flags = validate_prot_to_pageflags(&host_prot, target_prot);
if (!page_flags) {
return -TARGET_EINVAL;
}
len = TARGET_PAGE_ALIGN(len);
end = start + len;
if (!guest_range_valid(start, len)) {
return -TARGET_ENOMEM;
}
prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
if (len == 0)
if (len == 0) {
return 0;
}
mmap_lock();
host_start = start & qemu_host_page_mask;
host_end = HOST_PAGE_ALIGN(end);
if (start > host_start) {
/* handle host page containing start */
prot1 = prot;
for(addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
prot1 = host_prot;
for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
prot1 |= page_get_flags(addr);
}
if (host_end == host_start + qemu_host_page_size) {
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);
}
end = host_end;
}
ret = mprotect(g2h(host_start), qemu_host_page_size, prot1 & PAGE_BITS);
if (ret != 0)
ret = mprotect(g2h(host_start), qemu_host_page_size,
prot1 & PAGE_BITS);
if (ret != 0) {
goto error;
}
host_start += qemu_host_page_size;
}
if (end < host_end) {
prot1 = prot;
for(addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
prot1 = host_prot;
for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
prot1 |= page_get_flags(addr);
}
ret = mprotect(g2h(host_end - qemu_host_page_size), qemu_host_page_size,
prot1 & PAGE_BITS);
if (ret != 0)
ret = mprotect(g2h(host_end - qemu_host_page_size),
qemu_host_page_size, prot1 & PAGE_BITS);
if (ret != 0) {
goto error;
}
host_end -= qemu_host_page_size;
}
/* handle the pages in the middle */
if (host_start < host_end) {
ret = mprotect(g2h(host_start), host_end - host_start, prot);
if (ret != 0)
ret = mprotect(g2h(host_start), host_end - host_start, host_prot);
if (ret != 0) {
goto error;
}
}
page_set_flags(start, start + len, prot | PAGE_VALID);
page_set_flags(start, start + len, page_flags);
mmap_unlock();
return 0;
error:
@ -360,19 +396,26 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align)
}
/* NOTE: all the constants are the HOST ones */
abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
int flags, int fd, abi_ulong offset)
{
abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
int page_flags, host_prot;
mmap_lock();
trace_target_mmap(start, len, prot, flags, fd, offset);
trace_target_mmap(start, len, target_prot, flags, fd, offset);
if (!len) {
errno = EINVAL;
goto fail;
}
page_flags = validate_prot_to_pageflags(&host_prot, target_prot);
if (!page_flags) {
errno = EINVAL;
goto fail;
}
/* Also check for overflows... */
len = TARGET_PAGE_ALIGN(len);
if (!len) {
@ -438,14 +481,15 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
/* Note: we prefer to control the mapping address. It is
especially important if qemu_host_page_size >
qemu_real_host_page_size */
p = mmap(g2h(start), host_len, prot,
p = mmap(g2h(start), host_len, host_prot,
flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (p == MAP_FAILED)
if (p == MAP_FAILED) {
goto fail;
}
/* update start so that it points to the file position at 'offset' */
host_start = (unsigned long)p;
if (!(flags & MAP_ANONYMOUS)) {
p = mmap(g2h(start), len, prot,
p = mmap(g2h(start), len, host_prot,
flags | MAP_FIXED, fd, host_offset);
if (p == MAP_FAILED) {
munmap(g2h(start), host_len);
@ -479,19 +523,19 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int 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 &&
(prot & PROT_WRITE)) {
(host_prot & PROT_WRITE)) {
errno = EINVAL;
goto fail;
}
retaddr = target_mmap(start, len, prot | PROT_WRITE,
retaddr = target_mmap(start, len, target_prot | PROT_WRITE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if (retaddr == -1)
goto fail;
if (pread(fd, g2h(start), len, offset) == -1)
goto fail;
if (!(prot & PROT_WRITE)) {
ret = target_mprotect(start, len, prot);
if (!(host_prot & PROT_WRITE)) {
ret = target_mprotect(start, len, target_prot);
assert(ret == 0);
}
goto the_end;
@ -502,13 +546,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
if (real_end == real_start + qemu_host_page_size) {
/* one single host page */
ret = mmap_frag(real_start, start, end,
prot, flags, fd, offset);
host_prot, flags, fd, offset);
if (ret == -1)
goto fail;
goto the_end1;
}
ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
prot, flags, fd, offset);
host_prot, flags, fd, offset);
if (ret == -1)
goto fail;
real_start += qemu_host_page_size;
@ -517,7 +561,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
if (end < real_end) {
ret = mmap_frag(real_end - qemu_host_page_size,
real_end - qemu_host_page_size, end,
prot, flags, fd,
host_prot, flags, fd,
offset + real_end - qemu_host_page_size - start);
if (ret == -1)
goto fail;
@ -533,13 +577,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
else
offset1 = offset + real_start - start;
p = mmap(g2h(real_start), real_end - real_start,
prot, flags, fd, offset1);
host_prot, flags, fd, offset1);
if (p == MAP_FAILED)
goto fail;
}
}
the_end1:
page_set_flags(start, start + len, prot | PAGE_VALID);
page_set_flags(start, start + len, page_flags);
the_end:
trace_target_mmap_complete(start);
if (qemu_loglevel_mask(CPU_LOG_PAGE)) {

View File

@ -541,6 +541,7 @@ static void
print_fdset(int n, abi_ulong target_fds_addr)
{
int i;
int first = 1;
qemu_log("[");
if( target_fds_addr ) {
@ -555,9 +556,12 @@ print_fdset(int n, abi_ulong target_fds_addr)
return;
for (i=n; i>=0; i--) {
if ((tswapal(target_fds[i / TARGET_ABI_BITS]) >> (i & (TARGET_ABI_BITS - 1))) & 1)
qemu_log("%d,", i);
if ((tswapal(target_fds[i / TARGET_ABI_BITS]) >>
(i & (TARGET_ABI_BITS - 1))) & 1) {
qemu_log("%s%d", get_comma(first), i);
first = 0;
}
}
unlock_user(target_fds, target_fds_addr, 0);
}
qemu_log("]");

View File

@ -1229,7 +1229,9 @@ static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop)
defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
defined(TARGET_NR_timer_settime) || \
(defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
static inline abi_long target_to_host_timespec(struct timespec *host_ts,
abi_ulong target_addr)
{
@ -1245,7 +1247,9 @@ static inline abi_long target_to_host_timespec(struct timespec *host_ts,
}
#endif
#if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64)
#if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
defined(TARGET_NR_timer_settime64) || \
(defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
abi_ulong target_addr)
{
@ -6783,46 +6787,74 @@ static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
#if defined(TARGET_NR_timer_settime) || \
(defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
abi_ulong target_addr)
{
struct target_itimerspec *target_itspec;
if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
if (target_to_host_timespec(&host_its->it_interval, target_addr +
offsetof(struct target_itimerspec,
it_interval)) ||
target_to_host_timespec(&host_its->it_value, target_addr +
offsetof(struct target_itimerspec,
it_value))) {
return -TARGET_EFAULT;
}
host_itspec->it_interval.tv_sec =
tswapal(target_itspec->it_interval.tv_sec);
host_itspec->it_interval.tv_nsec =
tswapal(target_itspec->it_interval.tv_nsec);
host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
return 0;
}
#endif
#if defined(TARGET_NR_timer_settime64) || \
(defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
abi_ulong target_addr)
{
if (target_to_host_timespec64(&host_its->it_interval, target_addr +
offsetof(struct target__kernel_itimerspec,
it_interval)) ||
target_to_host_timespec64(&host_its->it_value, target_addr +
offsetof(struct target__kernel_itimerspec,
it_value))) {
return -TARGET_EFAULT;
}
unlock_user_struct(target_itspec, target_addr, 1);
return 0;
}
#endif
#if ((defined(TARGET_NR_timerfd_gettime) || \
defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
struct itimerspec *host_its)
struct itimerspec *host_its)
{
struct target_itimerspec *target_itspec;
if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
it_interval),
&host_its->it_interval) ||
host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
it_value),
&host_its->it_value)) {
return -TARGET_EFAULT;
}
return 0;
}
#endif
target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
unlock_user_struct(target_itspec, target_addr, 0);
#if ((defined(TARGET_NR_timerfd_gettime64) || \
defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
struct itimerspec *host_its)
{
if (host_to_target_timespec64(target_addr +
offsetof(struct target__kernel_itimerspec,
it_interval),
&host_its->it_interval) ||
host_to_target_timespec64(target_addr +
offsetof(struct target__kernel_itimerspec,
it_value),
&host_its->it_value)) {
return -TARGET_EFAULT;
}
return 0;
}
#endif
@ -11819,6 +11851,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
return ret;
}
#endif
#ifdef TARGET_NR_clock_getres_time64
case TARGET_NR_clock_getres_time64:
{
struct timespec ts;
ret = get_errno(clock_getres(arg1, &ts));
if (!is_error(ret)) {
host_to_target_timespec64(arg2, &ts);
}
return ret;
}
#endif
#ifdef TARGET_NR_clock_nanosleep
case TARGET_NR_clock_nanosleep:
{
@ -11876,8 +11919,13 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
if (!arg3) {
tsp = NULL;
} else {
target_to_host_timespec(ts, arg3);
target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
if (target_to_host_timespec(ts, arg3)) {
return -TARGET_EFAULT;
}
if (target_to_host_timespec(ts + 1, arg3 +
sizeof(struct target_timespec))) {
return -TARGET_EFAULT;
}
tsp = ts;
}
if (!arg2)
@ -12413,6 +12461,32 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
}
#endif
#ifdef TARGET_NR_timer_settime64
case TARGET_NR_timer_settime64:
{
target_timer_t timerid = get_timer_id(arg1);
if (timerid < 0) {
ret = timerid;
} else if (arg3 == 0) {
ret = -TARGET_EINVAL;
} else {
timer_t htimer = g_posix_timers[timerid];
struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
if (target_to_host_itimerspec64(&hspec_new, arg3)) {
return -TARGET_EFAULT;
}
ret = get_errno(
timer_settime(htimer, arg2, &hspec_new, &hspec_old));
if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
return -TARGET_EFAULT;
}
}
return ret;
}
#endif
#ifdef TARGET_NR_timer_gettime
case TARGET_NR_timer_gettime:
{
@ -12436,6 +12510,29 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
}
#endif
#ifdef TARGET_NR_timer_gettime64
case TARGET_NR_timer_gettime64:
{
/* args: timer_t timerid, struct itimerspec64 *curr_value */
target_timer_t timerid = get_timer_id(arg1);
if (timerid < 0) {
ret = timerid;
} else if (!arg2) {
ret = -TARGET_EFAULT;
} else {
timer_t htimer = g_posix_timers[timerid];
struct itimerspec hspec;
ret = get_errno(timer_gettime(htimer, &hspec));
if (host_to_target_itimerspec64(arg2, &hspec)) {
ret = -TARGET_EFAULT;
}
}
return ret;
}
#endif
#ifdef TARGET_NR_timer_getoverrun
case TARGET_NR_timer_getoverrun:
{
@ -12489,6 +12586,20 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
return ret;
#endif
#if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
case TARGET_NR_timerfd_gettime64:
{
struct itimerspec its_curr;
ret = get_errno(timerfd_gettime(arg1, &its_curr));
if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
return -TARGET_EFAULT;
}
}
return ret;
#endif
#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
case TARGET_NR_timerfd_settime:
{
@ -12512,6 +12623,29 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
return ret;
#endif
#if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
case TARGET_NR_timerfd_settime64:
{
struct itimerspec its_new, its_old, *p_new;
if (arg3) {
if (target_to_host_itimerspec64(&its_new, arg3)) {
return -TARGET_EFAULT;
}
p_new = &its_new;
} else {
p_new = NULL;
}
ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
return -TARGET_EFAULT;
}
}
return ret;
#endif
#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
case TARGET_NR_ioprio_get:
return get_errno(ioprio_get(arg1, arg2));

View File

@ -259,6 +259,11 @@ struct target_itimerspec {
struct target_timespec it_value;
};
struct target__kernel_itimerspec {
struct target__kernel_timespec it_interval;
struct target__kernel_timespec it_value;
};
struct target_timex {
abi_uint modes; /* Mode selector */
abi_long offset; /* Time offset */