* [PATCH v2 0/2] linux-user: Adding support for a group of 4 time64 syscalls @ 2020-08-24 22:30 Filip Bozuta 2020-08-24 22:30 ` [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() Filip Bozuta 2020-08-24 22:30 ` [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() Filip Bozuta 0 siblings, 2 replies; 11+ messages in thread From: Filip Bozuta @ 2020-08-24 22:30 UTC (permalink / raw) To: qemu-devel; +Cc: Laurent Vivier, Filip Bozuta This two patch series introduces functionality for following Year 2038 safe syscalls: --Introduced in first patch-- *ppoll_time64() *pselect6_time64() --Introduced in second patch-- *utimensat_time64() *semtimedop_time64() Testing notes: The implementations of these time64 syscalls was tested using tests from the LTP test suite which was built inside a chroot. v2: -Moved code of 'ppoll()' and 'pselect6()' to a separate function -Changed 'time64' from 'int' to 'bool' Filip Bozuta (2): linux-user: Add support for ppoll_time64() and pselect6_time64() linux-user: Add support for utimensat_time64() and semtimedop_time64() linux-user/syscall.c | 520 +++++++++++++++++++++++++------------------ 1 file changed, 300 insertions(+), 220 deletions(-) -- 2.25.1 ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() 2020-08-24 22:30 [PATCH v2 0/2] linux-user: Adding support for a group of 4 time64 syscalls Filip Bozuta @ 2020-08-24 22:30 ` Filip Bozuta 2020-08-25 7:00 ` Laurent Vivier 2020-08-25 7:17 ` Laurent Vivier 2020-08-24 22:30 ` [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() Filip Bozuta 1 sibling, 2 replies; 11+ messages in thread From: Filip Bozuta @ 2020-08-24 22:30 UTC (permalink / raw) To: qemu-devel; +Cc: Laurent Vivier, Filip Bozuta This patch introduces functionality for following time64 syscalls: *ppoll_time64 This is a year 2038 safe variant of: int poll(struct pollfd *fds, nfds_t nfds, int timeout) -- wait for some event on a file descriptor -- man page: https://man7.org/linux/man-pages/man2/ppoll.2.html *pselect6_time64 This is a year 2038 safe variant of: int pselect6(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask); -- synchronous I/O multiplexing -- man page: https://man7.org/linux/man-pages/man2/pselect6.2.html Implementation notes: Year 2038 safe syscalls in this patch were implemented with the same code as their regular variants (ppoll() and pselect()). This code was moved to new functions ('do_ppoll()' and 'do_pselect6()') that take a 'bool time64' from which a right 'struct timespec' converting function is called. (target_to_host/host_to_target_timespec() for regular and target_to_host/host_to_target_timespec64() for time64 variants) Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> --- linux-user/syscall.c | 462 +++++++++++++++++++++++-------------------- 1 file changed, 251 insertions(+), 211 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 1211e759c2..fc6a6e32e4 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -397,7 +397,7 @@ static int sys_getcwd1(char *buf, size_t size) return strlen(buf)+1; } -#ifdef TARGET_NR_utimensat +#if defined(TARGET_NR_utimensat) #if defined(__NR_utimensat) #define __NR_sys_utimensat __NR_utimensat _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, @@ -763,11 +763,11 @@ safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ int, options, struct rusage *, rusage) safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ - defined(TARGET_NR_pselect6) + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ fd_set *, exceptfds, struct timespec *, timeout, void *, sig) #endif -#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll) +#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64) safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, struct timespec *, tsp, const sigset_t *, sigmask, size_t, sigsetsize) @@ -984,7 +984,7 @@ abi_long do_brk(abi_ulong new_brk) } #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ - defined(TARGET_NR_pselect6) + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) static inline abi_long copy_from_user_fdset(fd_set *fds, abi_ulong target_fds_addr, int n) @@ -1252,7 +1252,8 @@ 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_pselect6_time64) || defined(TARGET_NR_ppoll_time64) static inline abi_long target_to_host_timespec64(struct timespec *host_ts, abi_ulong target_addr) { @@ -1458,6 +1459,237 @@ static abi_long do_old_select(abi_ulong arg1) #endif #endif +#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) +static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, + abi_long arg4, abi_long arg5, abi_long arg6, + bool time64) +{ + abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; + fd_set rfds, wfds, efds; + fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; + struct timespec ts, *ts_ptr; + abi_long ret; + + /* + * The 6th arg is actually two args smashed together, + * so we cannot use the C library. + */ + sigset_t set; + struct { + sigset_t *set; + size_t size; + } sig, *sig_ptr; + + abi_ulong arg_sigset, arg_sigsize, *arg7; + target_sigset_t *target_sigset; + + n = arg1; + rfd_addr = arg2; + wfd_addr = arg3; + efd_addr = arg4; + ts_addr = arg5; + + ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); + if (ret) { + return ret; + } + ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); + if (ret) { + return ret; + } + ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); + if (ret) { + return ret; + } + + /* + * This takes a timespec, and not a timeval, so we cannot + * use the do_select() helper ... + */ + if (ts_addr) { + if (time64) { + if (target_to_host_timespec64(&ts, ts_addr)) { + return -TARGET_EFAULT; + } + } else { + if (target_to_host_timespec(&ts, ts_addr)) { + return -TARGET_EFAULT; + } + } + ts_ptr = &ts; + } else { + ts_ptr = NULL; + } + + /* Extract the two packed args for the sigset */ + if (arg6) { + sig_ptr = &sig; + sig.size = SIGSET_T_SIZE; + + arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); + if (!arg7) { + return -TARGET_EFAULT; + } + arg_sigset = tswapal(arg7[0]); + arg_sigsize = tswapal(arg7[1]); + unlock_user(arg7, arg6, 0); + + if (arg_sigset) { + sig.set = &set; + if (arg_sigsize != sizeof(*target_sigset)) { + /* Like the kernel, we enforce correct size sigsets */ + return -TARGET_EINVAL; + } + target_sigset = lock_user(VERIFY_READ, arg_sigset, + sizeof(*target_sigset), 1); + if (!target_sigset) { + return -TARGET_EFAULT; + } + target_to_host_sigset(&set, target_sigset); + unlock_user(target_sigset, arg_sigset, 0); + } else { + sig.set = NULL; + } + } else { + sig_ptr = NULL; + } + + ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, + ts_ptr, sig_ptr)); + + if (!is_error(ret)) { + if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) { + return -TARGET_EFAULT; + } + if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) { + return -TARGET_EFAULT; + } + if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) { + return -TARGET_EFAULT; + } + if (time64) { + if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { + return -TARGET_EFAULT; + } + } else { + if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { + return -TARGET_EFAULT; + } + } + } + return ret; +} +#endif + +#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \ + defined(TARGET_NR_ppoll_time64) +static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3, + abi_long arg4, abi_long arg5, bool ppoll, bool time64) +{ + struct target_pollfd *target_pfd; + unsigned int nfds = arg2; + struct pollfd *pfd; + unsigned int i; + abi_long ret; + + pfd = NULL; + target_pfd = NULL; + if (nfds) { + if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { + return -TARGET_EINVAL; + } + target_pfd = lock_user(VERIFY_WRITE, arg1, + sizeof(struct target_pollfd) * nfds, 1); + if (!target_pfd) { + return -TARGET_EFAULT; + } + + pfd = alloca(sizeof(struct pollfd) * nfds); + for (i = 0; i < nfds; i++) { + pfd[i].fd = tswap32(target_pfd[i].fd); + pfd[i].events = tswap16(target_pfd[i].events); + } + } + if (ppoll) { + struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; + target_sigset_t *target_set; + sigset_t _set, *set = &_set; + + if (arg3) { + if (time64) { + if (target_to_host_timespec64(timeout_ts, arg3)) { + unlock_user(target_pfd, arg1, 0); + return -TARGET_EFAULT; + } + } else { + if (target_to_host_timespec(timeout_ts, arg3)) { + unlock_user(target_pfd, arg1, 0); + return -TARGET_EFAULT; + } + } + } else { + timeout_ts = NULL; + } + + if (arg4) { + if (arg5 != sizeof(target_sigset_t)) { + unlock_user(target_pfd, arg1, 0); + return -TARGET_EINVAL; + } + + target_set = lock_user(VERIFY_READ, arg4, + sizeof(target_sigset_t), 1); + if (!target_set) { + unlock_user(target_pfd, arg1, 0); + return -TARGET_EFAULT; + } + target_to_host_sigset(set, target_set); + } else { + set = NULL; + } + + ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, + set, SIGSET_T_SIZE)); + + if (!is_error(ret) && arg3) { + if (time64) { + if (host_to_target_timespec64(arg3, timeout_ts)) { + return -TARGET_EFAULT; + } + } else { + if (host_to_target_timespec(arg3, timeout_ts)) { + return -TARGET_EFAULT; + } + } + } + if (arg4) { + unlock_user(target_set, arg4, 0); + } + } else { + struct timespec ts, *pts; + + if (arg3 >= 0) { + /* Convert ms to secs, ns */ + ts.tv_sec = arg3 / 1000; + ts.tv_nsec = (arg3 % 1000) * 1000000LL; + pts = &ts; + } else { + /* -ve poll() timeout means "infinite" */ + pts = NULL; + } + ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); + } + + if (!is_error(ret)) { + for (i = 0; i < nfds; i++) { + target_pfd[i].revents = tswap16(pfd[i].revents); + } + } + unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); + return ret; +} +#endif + static abi_long do_pipe2(int host_pipe[], int flags) { #ifdef CONFIG_PIPE2 @@ -9045,106 +9277,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, #endif #ifdef TARGET_NR_pselect6 case TARGET_NR_pselect6: - { - abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; - fd_set rfds, wfds, efds; - fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; - struct timespec ts, *ts_ptr; - - /* - * The 6th arg is actually two args smashed together, - * so we cannot use the C library. - */ - sigset_t set; - struct { - sigset_t *set; - size_t size; - } sig, *sig_ptr; - - abi_ulong arg_sigset, arg_sigsize, *arg7; - target_sigset_t *target_sigset; - - n = arg1; - rfd_addr = arg2; - wfd_addr = arg3; - efd_addr = arg4; - ts_addr = arg5; - - ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); - if (ret) { - return ret; - } - ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); - if (ret) { - return ret; - } - ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); - if (ret) { - return ret; - } - - /* - * This takes a timespec, and not a timeval, so we cannot - * use the do_select() helper ... - */ - if (ts_addr) { - if (target_to_host_timespec(&ts, ts_addr)) { - return -TARGET_EFAULT; - } - ts_ptr = &ts; - } else { - ts_ptr = NULL; - } - - /* Extract the two packed args for the sigset */ - if (arg6) { - sig_ptr = &sig; - sig.size = SIGSET_T_SIZE; - - arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); - if (!arg7) { - return -TARGET_EFAULT; - } - arg_sigset = tswapal(arg7[0]); - arg_sigsize = tswapal(arg7[1]); - unlock_user(arg7, arg6, 0); - - if (arg_sigset) { - sig.set = &set; - if (arg_sigsize != sizeof(*target_sigset)) { - /* Like the kernel, we enforce correct size sigsets */ - return -TARGET_EINVAL; - } - target_sigset = lock_user(VERIFY_READ, arg_sigset, - sizeof(*target_sigset), 1); - if (!target_sigset) { - return -TARGET_EFAULT; - } - target_to_host_sigset(&set, target_sigset); - unlock_user(target_sigset, arg_sigset, 0); - } else { - sig.set = NULL; - } - } else { - sig_ptr = NULL; - } - - ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, - ts_ptr, sig_ptr)); - - if (!is_error(ret)) { - if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) - return -TARGET_EFAULT; - if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) - return -TARGET_EFAULT; - if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) - return -TARGET_EFAULT; - - if (ts_addr && host_to_target_timespec(ts_addr, &ts)) - return -TARGET_EFAULT; - } - } - return ret; + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false); +#endif +#ifdef TARGET_NR_pselect6_time64 + case TARGET_NR_pselect6_time64: + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true); #endif #ifdef TARGET_NR_symlink case TARGET_NR_symlink: @@ -10076,114 +10213,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, case TARGET_NR__newselect: return do_select(arg1, arg2, arg3, arg4, arg5); #endif -#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) -# ifdef TARGET_NR_poll +#ifdef TARGET_NR_poll case TARGET_NR_poll: -# endif -# ifdef TARGET_NR_ppoll + return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false); +#endif +#ifdef TARGET_NR_ppoll case TARGET_NR_ppoll: -# endif - { - struct target_pollfd *target_pfd; - unsigned int nfds = arg2; - struct pollfd *pfd; - unsigned int i; - - pfd = NULL; - target_pfd = NULL; - if (nfds) { - if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { - return -TARGET_EINVAL; - } - - target_pfd = lock_user(VERIFY_WRITE, arg1, - sizeof(struct target_pollfd) * nfds, 1); - if (!target_pfd) { - return -TARGET_EFAULT; - } - - pfd = alloca(sizeof(struct pollfd) * nfds); - for (i = 0; i < nfds; i++) { - pfd[i].fd = tswap32(target_pfd[i].fd); - pfd[i].events = tswap16(target_pfd[i].events); - } - } - - switch (num) { -# ifdef TARGET_NR_ppoll - case TARGET_NR_ppoll: - { - struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; - target_sigset_t *target_set; - sigset_t _set, *set = &_set; - - if (arg3) { - if (target_to_host_timespec(timeout_ts, arg3)) { - unlock_user(target_pfd, arg1, 0); - return -TARGET_EFAULT; - } - } else { - timeout_ts = NULL; - } - - if (arg4) { - if (arg5 != sizeof(target_sigset_t)) { - unlock_user(target_pfd, arg1, 0); - return -TARGET_EINVAL; - } - - target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1); - if (!target_set) { - unlock_user(target_pfd, arg1, 0); - return -TARGET_EFAULT; - } - target_to_host_sigset(set, target_set); - } else { - set = NULL; - } - - ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, - set, SIGSET_T_SIZE)); - - if (!is_error(ret) && arg3) { - host_to_target_timespec(arg3, timeout_ts); - } - if (arg4) { - unlock_user(target_set, arg4, 0); - } - break; - } -# endif -# ifdef TARGET_NR_poll - case TARGET_NR_poll: - { - struct timespec ts, *pts; - - if (arg3 >= 0) { - /* Convert ms to secs, ns */ - ts.tv_sec = arg3 / 1000; - ts.tv_nsec = (arg3 % 1000) * 1000000LL; - pts = &ts; - } else { - /* -ve poll() timeout means "infinite" */ - pts = NULL; - } - ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); - break; - } -# endif - default: - g_assert_not_reached(); - } - - if (!is_error(ret)) { - for(i = 0; i < nfds; i++) { - target_pfd[i].revents = tswap16(pfd[i].revents); - } - } - unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); - } - return ret; + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false); +#endif +#ifdef TARGET_NR_ppoll_time64 + case TARGET_NR_ppoll_time64: + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true); #endif case TARGET_NR_flock: /* NOTE: the flock constant seems to be the same for every -- 2.25.1 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() 2020-08-24 22:30 ` [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() Filip Bozuta @ 2020-08-25 7:00 ` Laurent Vivier 2020-08-25 7:17 ` Laurent Vivier 1 sibling, 0 replies; 11+ messages in thread From: Laurent Vivier @ 2020-08-25 7:00 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 25/08/2020 à 00:30, Filip Bozuta a écrit : > This patch introduces functionality for following time64 syscalls: > > *ppoll_time64 > > This is a year 2038 safe variant of: > > int poll(struct pollfd *fds, nfds_t nfds, int timeout) > -- wait for some event on a file descriptor -- > man page: https://man7.org/linux/man-pages/man2/ppoll.2.html > > *pselect6_time64 > > This is a year 2038 safe variant of: > > int pselect6(int nfds, fd_set *readfds, fd_set *writefds, > fd_set *exceptfds, const struct timespec *timeout, > const sigset_t *sigmask); > -- synchronous I/O multiplexing -- > man page: https://man7.org/linux/man-pages/man2/pselect6.2.html > > Implementation notes: > > Year 2038 safe syscalls in this patch were implemented > with the same code as their regular variants (ppoll() and pselect()). > This code was moved to new functions ('do_ppoll()' and 'do_pselect6()') > that take a 'bool time64' from which a right 'struct timespec' converting > function is called. > (target_to_host/host_to_target_timespec() for regular and > target_to_host/host_to_target_timespec64() for time64 variants) > > Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> > --- > linux-user/syscall.c | 462 +++++++++++++++++++++++-------------------- > 1 file changed, 251 insertions(+), 211 deletions(-) > > diff --git a/linux-user/syscall.c b/linux-user/syscall.c > index 1211e759c2..fc6a6e32e4 100644 > --- a/linux-user/syscall.c > +++ b/linux-user/syscall.c > @@ -397,7 +397,7 @@ static int sys_getcwd1(char *buf, size_t size) > return strlen(buf)+1; > } > > -#ifdef TARGET_NR_utimensat > +#if defined(TARGET_NR_utimensat) > #if defined(__NR_utimensat) > #define __NR_sys_utimensat __NR_utimensat > _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, > @@ -763,11 +763,11 @@ safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ > int, options, struct rusage *, rusage) > safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) > #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ > - defined(TARGET_NR_pselect6) > + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) > safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ > fd_set *, exceptfds, struct timespec *, timeout, void *, sig) > #endif > -#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll) > +#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64) > safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, > struct timespec *, tsp, const sigset_t *, sigmask, > size_t, sigsetsize) > @@ -984,7 +984,7 @@ abi_long do_brk(abi_ulong new_brk) > } > > #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ > - defined(TARGET_NR_pselect6) > + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) > static inline abi_long copy_from_user_fdset(fd_set *fds, > abi_ulong target_fds_addr, > int n) > @@ -1252,7 +1252,8 @@ 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_pselect6_time64) || defined(TARGET_NR_ppoll_time64) > static inline abi_long target_to_host_timespec64(struct timespec *host_ts, > abi_ulong target_addr) > { > @@ -1458,6 +1459,237 @@ static abi_long do_old_select(abi_ulong arg1) > #endif > #endif > > +#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) > +static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, > + abi_long arg4, abi_long arg5, abi_long arg6, > + bool time64) > +{ > + abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; > + fd_set rfds, wfds, efds; > + fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; > + struct timespec ts, *ts_ptr; > + abi_long ret; > + > + /* > + * The 6th arg is actually two args smashed together, > + * so we cannot use the C library. > + */ > + sigset_t set; > + struct { > + sigset_t *set; > + size_t size; > + } sig, *sig_ptr; > + > + abi_ulong arg_sigset, arg_sigsize, *arg7; > + target_sigset_t *target_sigset; > + > + n = arg1; > + rfd_addr = arg2; > + wfd_addr = arg3; > + efd_addr = arg4; > + ts_addr = arg5; > + > + ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); > + if (ret) { > + return ret; > + } > + ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); > + if (ret) { > + return ret; > + } > + ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); > + if (ret) { > + return ret; > + } > + > + /* > + * This takes a timespec, and not a timeval, so we cannot > + * use the do_select() helper ... > + */ > + if (ts_addr) { > + if (time64) { > + if (target_to_host_timespec64(&ts, ts_addr)) { > + return -TARGET_EFAULT; > + } > + } else { > + if (target_to_host_timespec(&ts, ts_addr)) { > + return -TARGET_EFAULT; > + } > + } > + ts_ptr = &ts; > + } else { > + ts_ptr = NULL; > + } > + > + /* Extract the two packed args for the sigset */ > + if (arg6) { > + sig_ptr = &sig; > + sig.size = SIGSET_T_SIZE; > + > + arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); > + if (!arg7) { > + return -TARGET_EFAULT; > + } > + arg_sigset = tswapal(arg7[0]); > + arg_sigsize = tswapal(arg7[1]); > + unlock_user(arg7, arg6, 0); > + > + if (arg_sigset) { > + sig.set = &set; > + if (arg_sigsize != sizeof(*target_sigset)) { > + /* Like the kernel, we enforce correct size sigsets */ > + return -TARGET_EINVAL; > + } > + target_sigset = lock_user(VERIFY_READ, arg_sigset, > + sizeof(*target_sigset), 1); > + if (!target_sigset) { > + return -TARGET_EFAULT; > + } > + target_to_host_sigset(&set, target_sigset); > + unlock_user(target_sigset, arg_sigset, 0); > + } else { > + sig.set = NULL; > + } > + } else { > + sig_ptr = NULL; > + } > + > + ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, > + ts_ptr, sig_ptr)); > + > + if (!is_error(ret)) { > + if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) { > + return -TARGET_EFAULT; > + } > + if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) { > + return -TARGET_EFAULT; > + } > + if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) { > + return -TARGET_EFAULT; > + } > + if (time64) { > + if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { > + return -TARGET_EFAULT; > + } > + } else { > + if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { > + return -TARGET_EFAULT; > + } > + } > + } > + return ret; > +} > +#endif > + > +#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \ > + defined(TARGET_NR_ppoll_time64) > +static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3, > + abi_long arg4, abi_long arg5, bool ppoll, bool time64) > +{ > + struct target_pollfd *target_pfd; > + unsigned int nfds = arg2; > + struct pollfd *pfd; > + unsigned int i; > + abi_long ret; > + > + pfd = NULL; > + target_pfd = NULL; > + if (nfds) { > + if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { > + return -TARGET_EINVAL; > + } > + target_pfd = lock_user(VERIFY_WRITE, arg1, > + sizeof(struct target_pollfd) * nfds, 1); > + if (!target_pfd) { > + return -TARGET_EFAULT; > + } > + > + pfd = alloca(sizeof(struct pollfd) * nfds); > + for (i = 0; i < nfds; i++) { > + pfd[i].fd = tswap32(target_pfd[i].fd); > + pfd[i].events = tswap16(target_pfd[i].events); > + } > + } > + if (ppoll) { > + struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; > + target_sigset_t *target_set; > + sigset_t _set, *set = &_set; > + > + if (arg3) { > + if (time64) { > + if (target_to_host_timespec64(timeout_ts, arg3)) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EFAULT; > + } > + } else { > + if (target_to_host_timespec(timeout_ts, arg3)) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EFAULT; > + } > + } > + } else { > + timeout_ts = NULL; > + } > + > + if (arg4) { > + if (arg5 != sizeof(target_sigset_t)) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EINVAL; > + } > + > + target_set = lock_user(VERIFY_READ, arg4, > + sizeof(target_sigset_t), 1); > + if (!target_set) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EFAULT; > + } > + target_to_host_sigset(set, target_set); > + } else { > + set = NULL; > + } > + > + ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, > + set, SIGSET_T_SIZE)); > + > + if (!is_error(ret) && arg3) { > + if (time64) { > + if (host_to_target_timespec64(arg3, timeout_ts)) { > + return -TARGET_EFAULT; > + } > + } else { > + if (host_to_target_timespec(arg3, timeout_ts)) { > + return -TARGET_EFAULT; > + } > + } > + } > + if (arg4) { > + unlock_user(target_set, arg4, 0); > + } > + } else { > + struct timespec ts, *pts; > + > + if (arg3 >= 0) { > + /* Convert ms to secs, ns */ > + ts.tv_sec = arg3 / 1000; > + ts.tv_nsec = (arg3 % 1000) * 1000000LL; > + pts = &ts; > + } else { > + /* -ve poll() timeout means "infinite" */ > + pts = NULL; > + } > + ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); > + } > + > + if (!is_error(ret)) { > + for (i = 0; i < nfds; i++) { > + target_pfd[i].revents = tswap16(pfd[i].revents); > + } > + } > + unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); > + return ret; > +} > +#endif > + > static abi_long do_pipe2(int host_pipe[], int flags) > { > #ifdef CONFIG_PIPE2 > @@ -9045,106 +9277,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, > #endif > #ifdef TARGET_NR_pselect6 > case TARGET_NR_pselect6: > - { > - abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; > - fd_set rfds, wfds, efds; > - fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; > - struct timespec ts, *ts_ptr; > - > - /* > - * The 6th arg is actually two args smashed together, > - * so we cannot use the C library. > - */ > - sigset_t set; > - struct { > - sigset_t *set; > - size_t size; > - } sig, *sig_ptr; > - > - abi_ulong arg_sigset, arg_sigsize, *arg7; > - target_sigset_t *target_sigset; > - > - n = arg1; > - rfd_addr = arg2; > - wfd_addr = arg3; > - efd_addr = arg4; > - ts_addr = arg5; > - > - ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); > - if (ret) { > - return ret; > - } > - ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); > - if (ret) { > - return ret; > - } > - ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); > - if (ret) { > - return ret; > - } > - > - /* > - * This takes a timespec, and not a timeval, so we cannot > - * use the do_select() helper ... > - */ > - if (ts_addr) { > - if (target_to_host_timespec(&ts, ts_addr)) { > - return -TARGET_EFAULT; > - } > - ts_ptr = &ts; > - } else { > - ts_ptr = NULL; > - } > - > - /* Extract the two packed args for the sigset */ > - if (arg6) { > - sig_ptr = &sig; > - sig.size = SIGSET_T_SIZE; > - > - arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); > - if (!arg7) { > - return -TARGET_EFAULT; > - } > - arg_sigset = tswapal(arg7[0]); > - arg_sigsize = tswapal(arg7[1]); > - unlock_user(arg7, arg6, 0); > - > - if (arg_sigset) { > - sig.set = &set; > - if (arg_sigsize != sizeof(*target_sigset)) { > - /* Like the kernel, we enforce correct size sigsets */ > - return -TARGET_EINVAL; > - } > - target_sigset = lock_user(VERIFY_READ, arg_sigset, > - sizeof(*target_sigset), 1); > - if (!target_sigset) { > - return -TARGET_EFAULT; > - } > - target_to_host_sigset(&set, target_sigset); > - unlock_user(target_sigset, arg_sigset, 0); > - } else { > - sig.set = NULL; > - } > - } else { > - sig_ptr = NULL; > - } > - > - ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, > - ts_ptr, sig_ptr)); > - > - if (!is_error(ret)) { > - if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) > - return -TARGET_EFAULT; > - if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) > - return -TARGET_EFAULT; > - if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) > - return -TARGET_EFAULT; > - > - if (ts_addr && host_to_target_timespec(ts_addr, &ts)) > - return -TARGET_EFAULT; > - } > - } > - return ret; > + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false); > +#endif > +#ifdef TARGET_NR_pselect6_time64 > + case TARGET_NR_pselect6_time64: > + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true); > #endif > #ifdef TARGET_NR_symlink > case TARGET_NR_symlink: > @@ -10076,114 +10213,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, > case TARGET_NR__newselect: > return do_select(arg1, arg2, arg3, arg4, arg5); > #endif > -#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) > -# ifdef TARGET_NR_poll > +#ifdef TARGET_NR_poll > case TARGET_NR_poll: > -# endif > -# ifdef TARGET_NR_ppoll > + return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false); > +#endif > +#ifdef TARGET_NR_ppoll > case TARGET_NR_ppoll: > -# endif > - { > - struct target_pollfd *target_pfd; > - unsigned int nfds = arg2; > - struct pollfd *pfd; > - unsigned int i; > - > - pfd = NULL; > - target_pfd = NULL; > - if (nfds) { > - if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { > - return -TARGET_EINVAL; > - } > - > - target_pfd = lock_user(VERIFY_WRITE, arg1, > - sizeof(struct target_pollfd) * nfds, 1); > - if (!target_pfd) { > - return -TARGET_EFAULT; > - } > - > - pfd = alloca(sizeof(struct pollfd) * nfds); > - for (i = 0; i < nfds; i++) { > - pfd[i].fd = tswap32(target_pfd[i].fd); > - pfd[i].events = tswap16(target_pfd[i].events); > - } > - } > - > - switch (num) { > -# ifdef TARGET_NR_ppoll > - case TARGET_NR_ppoll: > - { > - struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; > - target_sigset_t *target_set; > - sigset_t _set, *set = &_set; > - > - if (arg3) { > - if (target_to_host_timespec(timeout_ts, arg3)) { > - unlock_user(target_pfd, arg1, 0); > - return -TARGET_EFAULT; > - } > - } else { > - timeout_ts = NULL; > - } > - > - if (arg4) { > - if (arg5 != sizeof(target_sigset_t)) { > - unlock_user(target_pfd, arg1, 0); > - return -TARGET_EINVAL; > - } > - > - target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1); > - if (!target_set) { > - unlock_user(target_pfd, arg1, 0); > - return -TARGET_EFAULT; > - } > - target_to_host_sigset(set, target_set); > - } else { > - set = NULL; > - } > - > - ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, > - set, SIGSET_T_SIZE)); > - > - if (!is_error(ret) && arg3) { > - host_to_target_timespec(arg3, timeout_ts); > - } > - if (arg4) { > - unlock_user(target_set, arg4, 0); > - } > - break; > - } > -# endif > -# ifdef TARGET_NR_poll > - case TARGET_NR_poll: > - { > - struct timespec ts, *pts; > - > - if (arg3 >= 0) { > - /* Convert ms to secs, ns */ > - ts.tv_sec = arg3 / 1000; > - ts.tv_nsec = (arg3 % 1000) * 1000000LL; > - pts = &ts; > - } else { > - /* -ve poll() timeout means "infinite" */ > - pts = NULL; > - } > - ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); > - break; > - } > -# endif > - default: > - g_assert_not_reached(); > - } > - > - if (!is_error(ret)) { > - for(i = 0; i < nfds; i++) { > - target_pfd[i].revents = tswap16(pfd[i].revents); > - } > - } > - unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); > - } > - return ret; > + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false); > +#endif > +#ifdef TARGET_NR_ppoll_time64 > + case TARGET_NR_ppoll_time64: > + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true); > #endif > case TARGET_NR_flock: > /* NOTE: the flock constant seems to be the same for every > Reviewed-by: Laurent Vivier <laurent@vivier.eu> ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() 2020-08-24 22:30 ` [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() Filip Bozuta 2020-08-25 7:00 ` Laurent Vivier @ 2020-08-25 7:17 ` Laurent Vivier 2020-08-25 15:23 ` Laurent Vivier 1 sibling, 1 reply; 11+ messages in thread From: Laurent Vivier @ 2020-08-25 7:17 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 25/08/2020 à 00:30, Filip Bozuta a écrit : > This patch introduces functionality for following time64 syscalls: > > *ppoll_time64 > > This is a year 2038 safe variant of: > > int poll(struct pollfd *fds, nfds_t nfds, int timeout) > -- wait for some event on a file descriptor -- > man page: https://man7.org/linux/man-pages/man2/ppoll.2.html > > *pselect6_time64 > > This is a year 2038 safe variant of: > > int pselect6(int nfds, fd_set *readfds, fd_set *writefds, > fd_set *exceptfds, const struct timespec *timeout, > const sigset_t *sigmask); > -- synchronous I/O multiplexing -- > man page: https://man7.org/linux/man-pages/man2/pselect6.2.html > > Implementation notes: > > Year 2038 safe syscalls in this patch were implemented > with the same code as their regular variants (ppoll() and pselect()). > This code was moved to new functions ('do_ppoll()' and 'do_pselect6()') > that take a 'bool time64' from which a right 'struct timespec' converting > function is called. > (target_to_host/host_to_target_timespec() for regular and > target_to_host/host_to_target_timespec64() for time64 variants) > > Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> > --- > linux-user/syscall.c | 462 +++++++++++++++++++++++-------------------- > 1 file changed, 251 insertions(+), 211 deletions(-) > > diff --git a/linux-user/syscall.c b/linux-user/syscall.c > index 1211e759c2..fc6a6e32e4 100644 > --- a/linux-user/syscall.c > +++ b/linux-user/syscall.c > @@ -397,7 +397,7 @@ static int sys_getcwd1(char *buf, size_t size) > return strlen(buf)+1; > } > > -#ifdef TARGET_NR_utimensat > +#if defined(TARGET_NR_utimensat) > #if defined(__NR_utimensat) > #define __NR_sys_utimensat __NR_utimensat > _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, > @@ -763,11 +763,11 @@ safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ > int, options, struct rusage *, rusage) > safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) > #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ > - defined(TARGET_NR_pselect6) > + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) > safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ > fd_set *, exceptfds, struct timespec *, timeout, void *, sig) > #endif > -#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll) > +#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64) > safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, > struct timespec *, tsp, const sigset_t *, sigmask, > size_t, sigsetsize) > @@ -984,7 +984,7 @@ abi_long do_brk(abi_ulong new_brk) > } > > #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ > - defined(TARGET_NR_pselect6) > + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) > static inline abi_long copy_from_user_fdset(fd_set *fds, > abi_ulong target_fds_addr, > int n) > @@ -1252,7 +1252,8 @@ 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_pselect6_time64) || defined(TARGET_NR_ppoll_time64) > static inline abi_long target_to_host_timespec64(struct timespec *host_ts, > abi_ulong target_addr) > { > @@ -1458,6 +1459,237 @@ static abi_long do_old_select(abi_ulong arg1) > #endif > #endif > > +#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) > +static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, > + abi_long arg4, abi_long arg5, abi_long arg6, > + bool time64) > +{ > + abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; > + fd_set rfds, wfds, efds; > + fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; > + struct timespec ts, *ts_ptr; > + abi_long ret; > + > + /* > + * The 6th arg is actually two args smashed together, > + * so we cannot use the C library. > + */ > + sigset_t set; > + struct { > + sigset_t *set; > + size_t size; > + } sig, *sig_ptr; > + > + abi_ulong arg_sigset, arg_sigsize, *arg7; > + target_sigset_t *target_sigset; > + > + n = arg1; > + rfd_addr = arg2; > + wfd_addr = arg3; > + efd_addr = arg4; > + ts_addr = arg5; > + > + ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); > + if (ret) { > + return ret; > + } > + ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); > + if (ret) { > + return ret; > + } > + ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); > + if (ret) { > + return ret; > + } > + > + /* > + * This takes a timespec, and not a timeval, so we cannot > + * use the do_select() helper ... > + */ > + if (ts_addr) { > + if (time64) { > + if (target_to_host_timespec64(&ts, ts_addr)) { > + return -TARGET_EFAULT; > + } > + } else { > + if (target_to_host_timespec(&ts, ts_addr)) { > + return -TARGET_EFAULT; > + } > + } > + ts_ptr = &ts; > + } else { > + ts_ptr = NULL; > + } > + > + /* Extract the two packed args for the sigset */ > + if (arg6) { > + sig_ptr = &sig; > + sig.size = SIGSET_T_SIZE; > + > + arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); > + if (!arg7) { > + return -TARGET_EFAULT; > + } > + arg_sigset = tswapal(arg7[0]); > + arg_sigsize = tswapal(arg7[1]); > + unlock_user(arg7, arg6, 0); > + > + if (arg_sigset) { > + sig.set = &set; > + if (arg_sigsize != sizeof(*target_sigset)) { > + /* Like the kernel, we enforce correct size sigsets */ > + return -TARGET_EINVAL; > + } > + target_sigset = lock_user(VERIFY_READ, arg_sigset, > + sizeof(*target_sigset), 1); > + if (!target_sigset) { > + return -TARGET_EFAULT; > + } > + target_to_host_sigset(&set, target_sigset); > + unlock_user(target_sigset, arg_sigset, 0); > + } else { > + sig.set = NULL; > + } > + } else { > + sig_ptr = NULL; > + } > + > + ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, > + ts_ptr, sig_ptr)); > + > + if (!is_error(ret)) { > + if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) { > + return -TARGET_EFAULT; > + } > + if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) { > + return -TARGET_EFAULT; > + } > + if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) { > + return -TARGET_EFAULT; > + } > + if (time64) { > + if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { > + return -TARGET_EFAULT; > + } > + } else { > + if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { > + return -TARGET_EFAULT; > + } > + } > + } > + return ret; > +} > +#endif > + > +#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \ > + defined(TARGET_NR_ppoll_time64) > +static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3, > + abi_long arg4, abi_long arg5, bool ppoll, bool time64) > +{ > + struct target_pollfd *target_pfd; > + unsigned int nfds = arg2; > + struct pollfd *pfd; > + unsigned int i; > + abi_long ret; > + > + pfd = NULL; > + target_pfd = NULL; > + if (nfds) { > + if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { > + return -TARGET_EINVAL; > + } > + target_pfd = lock_user(VERIFY_WRITE, arg1, > + sizeof(struct target_pollfd) * nfds, 1); > + if (!target_pfd) { > + return -TARGET_EFAULT; > + } > + > + pfd = alloca(sizeof(struct pollfd) * nfds); > + for (i = 0; i < nfds; i++) { > + pfd[i].fd = tswap32(target_pfd[i].fd); > + pfd[i].events = tswap16(target_pfd[i].events); > + } > + } > + if (ppoll) { > + struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; > + target_sigset_t *target_set; > + sigset_t _set, *set = &_set; > + > + if (arg3) { > + if (time64) { > + if (target_to_host_timespec64(timeout_ts, arg3)) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EFAULT; > + } > + } else { > + if (target_to_host_timespec(timeout_ts, arg3)) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EFAULT; > + } > + } > + } else { > + timeout_ts = NULL; > + } > + > + if (arg4) { > + if (arg5 != sizeof(target_sigset_t)) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EINVAL; > + } > + > + target_set = lock_user(VERIFY_READ, arg4, > + sizeof(target_sigset_t), 1); > + if (!target_set) { > + unlock_user(target_pfd, arg1, 0); > + return -TARGET_EFAULT; > + } > + target_to_host_sigset(set, target_set); > + } else { > + set = NULL; > + } > + > + ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, > + set, SIGSET_T_SIZE)); > + > + if (!is_error(ret) && arg3) { > + if (time64) { > + if (host_to_target_timespec64(arg3, timeout_ts)) { > + return -TARGET_EFAULT; > + } > + } else { > + if (host_to_target_timespec(arg3, timeout_ts)) { > + return -TARGET_EFAULT; > + } > + } > + } > + if (arg4) { > + unlock_user(target_set, arg4, 0); > + } > + } else { > + struct timespec ts, *pts; > + > + if (arg3 >= 0) { > + /* Convert ms to secs, ns */ > + ts.tv_sec = arg3 / 1000; > + ts.tv_nsec = (arg3 % 1000) * 1000000LL; > + pts = &ts; > + } else { > + /* -ve poll() timeout means "infinite" */ > + pts = NULL; > + } > + ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); > + } > + > + if (!is_error(ret)) { > + for (i = 0; i < nfds; i++) { > + target_pfd[i].revents = tswap16(pfd[i].revents); > + } > + } > + unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); > + return ret; > +} > +#endif > + > static abi_long do_pipe2(int host_pipe[], int flags) > { > #ifdef CONFIG_PIPE2 > @@ -9045,106 +9277,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, > #endif > #ifdef TARGET_NR_pselect6 > case TARGET_NR_pselect6: > - { > - abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; > - fd_set rfds, wfds, efds; > - fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; > - struct timespec ts, *ts_ptr; > - > - /* > - * The 6th arg is actually two args smashed together, > - * so we cannot use the C library. > - */ > - sigset_t set; > - struct { > - sigset_t *set; > - size_t size; > - } sig, *sig_ptr; > - > - abi_ulong arg_sigset, arg_sigsize, *arg7; > - target_sigset_t *target_sigset; > - > - n = arg1; > - rfd_addr = arg2; > - wfd_addr = arg3; > - efd_addr = arg4; > - ts_addr = arg5; > - > - ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); > - if (ret) { > - return ret; > - } > - ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); > - if (ret) { > - return ret; > - } > - ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); > - if (ret) { > - return ret; > - } > - > - /* > - * This takes a timespec, and not a timeval, so we cannot > - * use the do_select() helper ... > - */ > - if (ts_addr) { > - if (target_to_host_timespec(&ts, ts_addr)) { > - return -TARGET_EFAULT; > - } > - ts_ptr = &ts; > - } else { > - ts_ptr = NULL; > - } > - > - /* Extract the two packed args for the sigset */ > - if (arg6) { > - sig_ptr = &sig; > - sig.size = SIGSET_T_SIZE; > - > - arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); > - if (!arg7) { > - return -TARGET_EFAULT; > - } > - arg_sigset = tswapal(arg7[0]); > - arg_sigsize = tswapal(arg7[1]); > - unlock_user(arg7, arg6, 0); > - > - if (arg_sigset) { > - sig.set = &set; > - if (arg_sigsize != sizeof(*target_sigset)) { > - /* Like the kernel, we enforce correct size sigsets */ > - return -TARGET_EINVAL; > - } > - target_sigset = lock_user(VERIFY_READ, arg_sigset, > - sizeof(*target_sigset), 1); > - if (!target_sigset) { > - return -TARGET_EFAULT; > - } > - target_to_host_sigset(&set, target_sigset); > - unlock_user(target_sigset, arg_sigset, 0); > - } else { > - sig.set = NULL; > - } > - } else { > - sig_ptr = NULL; > - } > - > - ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, > - ts_ptr, sig_ptr)); > - > - if (!is_error(ret)) { > - if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) > - return -TARGET_EFAULT; > - if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) > - return -TARGET_EFAULT; > - if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) > - return -TARGET_EFAULT; > - > - if (ts_addr && host_to_target_timespec(ts_addr, &ts)) > - return -TARGET_EFAULT; > - } > - } > - return ret; > + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false); > +#endif > +#ifdef TARGET_NR_pselect6_time64 > + case TARGET_NR_pselect6_time64: > + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true); > #endif > #ifdef TARGET_NR_symlink > case TARGET_NR_symlink: > @@ -10076,114 +10213,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, > case TARGET_NR__newselect: > return do_select(arg1, arg2, arg3, arg4, arg5); > #endif > -#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) > -# ifdef TARGET_NR_poll > +#ifdef TARGET_NR_poll > case TARGET_NR_poll: > -# endif > -# ifdef TARGET_NR_ppoll > + return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false); > +#endif > +#ifdef TARGET_NR_ppoll > case TARGET_NR_ppoll: > -# endif > - { > - struct target_pollfd *target_pfd; > - unsigned int nfds = arg2; > - struct pollfd *pfd; > - unsigned int i; > - > - pfd = NULL; > - target_pfd = NULL; > - if (nfds) { > - if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { > - return -TARGET_EINVAL; > - } > - > - target_pfd = lock_user(VERIFY_WRITE, arg1, > - sizeof(struct target_pollfd) * nfds, 1); > - if (!target_pfd) { > - return -TARGET_EFAULT; > - } > - > - pfd = alloca(sizeof(struct pollfd) * nfds); > - for (i = 0; i < nfds; i++) { > - pfd[i].fd = tswap32(target_pfd[i].fd); > - pfd[i].events = tswap16(target_pfd[i].events); > - } > - } > - > - switch (num) { > -# ifdef TARGET_NR_ppoll > - case TARGET_NR_ppoll: > - { > - struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; > - target_sigset_t *target_set; > - sigset_t _set, *set = &_set; > - > - if (arg3) { > - if (target_to_host_timespec(timeout_ts, arg3)) { > - unlock_user(target_pfd, arg1, 0); > - return -TARGET_EFAULT; > - } > - } else { > - timeout_ts = NULL; > - } > - > - if (arg4) { > - if (arg5 != sizeof(target_sigset_t)) { > - unlock_user(target_pfd, arg1, 0); > - return -TARGET_EINVAL; > - } > - > - target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1); > - if (!target_set) { > - unlock_user(target_pfd, arg1, 0); > - return -TARGET_EFAULT; > - } > - target_to_host_sigset(set, target_set); > - } else { > - set = NULL; > - } > - > - ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, > - set, SIGSET_T_SIZE)); > - > - if (!is_error(ret) && arg3) { > - host_to_target_timespec(arg3, timeout_ts); > - } > - if (arg4) { > - unlock_user(target_set, arg4, 0); > - } > - break; > - } > -# endif > -# ifdef TARGET_NR_poll > - case TARGET_NR_poll: > - { > - struct timespec ts, *pts; > - > - if (arg3 >= 0) { > - /* Convert ms to secs, ns */ > - ts.tv_sec = arg3 / 1000; > - ts.tv_nsec = (arg3 % 1000) * 1000000LL; > - pts = &ts; > - } else { > - /* -ve poll() timeout means "infinite" */ > - pts = NULL; > - } > - ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); > - break; > - } > -# endif > - default: > - g_assert_not_reached(); > - } > - > - if (!is_error(ret)) { > - for(i = 0; i < nfds; i++) { > - target_pfd[i].revents = tswap16(pfd[i].revents); > - } > - } > - unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); > - } > - return ret; > + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false); > +#endif > +#ifdef TARGET_NR_ppoll_time64 > + case TARGET_NR_ppoll_time64: > + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true); > #endif > case TARGET_NR_flock: > /* NOTE: the flock constant seems to be the same for every > Applied to my linux-user-for-5.2 branch. Thanks, Laurent ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() 2020-08-25 7:17 ` Laurent Vivier @ 2020-08-25 15:23 ` Laurent Vivier 2020-09-05 20:41 ` Laurent Vivier 0 siblings, 1 reply; 11+ messages in thread From: Laurent Vivier @ 2020-08-25 15:23 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 25/08/2020 à 09:17, Laurent Vivier a écrit : > Le 25/08/2020 à 00:30, Filip Bozuta a écrit : >> This patch introduces functionality for following time64 syscalls: >> >> *ppoll_time64 >> >> This is a year 2038 safe variant of: >> >> int poll(struct pollfd *fds, nfds_t nfds, int timeout) >> -- wait for some event on a file descriptor -- >> man page: https://man7.org/linux/man-pages/man2/ppoll.2.html >> >> *pselect6_time64 >> >> This is a year 2038 safe variant of: >> >> int pselect6(int nfds, fd_set *readfds, fd_set *writefds, >> fd_set *exceptfds, const struct timespec *timeout, >> const sigset_t *sigmask); >> -- synchronous I/O multiplexing -- >> man page: https://man7.org/linux/man-pages/man2/pselect6.2.html >> >> Implementation notes: >> >> Year 2038 safe syscalls in this patch were implemented >> with the same code as their regular variants (ppoll() and pselect()). >> This code was moved to new functions ('do_ppoll()' and 'do_pselect6()') >> that take a 'bool time64' from which a right 'struct timespec' converting >> function is called. >> (target_to_host/host_to_target_timespec() for regular and >> target_to_host/host_to_target_timespec64() for time64 variants) >> >> Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> >> --- >> linux-user/syscall.c | 462 +++++++++++++++++++++++-------------------- >> 1 file changed, 251 insertions(+), 211 deletions(-) >> >> diff --git a/linux-user/syscall.c b/linux-user/syscall.c >> index 1211e759c2..fc6a6e32e4 100644 >> --- a/linux-user/syscall.c >> +++ b/linux-user/syscall.c >> @@ -397,7 +397,7 @@ static int sys_getcwd1(char *buf, size_t size) >> return strlen(buf)+1; >> } >> >> -#ifdef TARGET_NR_utimensat >> +#if defined(TARGET_NR_utimensat) >> #if defined(__NR_utimensat) >> #define __NR_sys_utimensat __NR_utimensat >> _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, >> @@ -763,11 +763,11 @@ safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ >> int, options, struct rusage *, rusage) >> safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) >> #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ >> - defined(TARGET_NR_pselect6) >> + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) >> safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ >> fd_set *, exceptfds, struct timespec *, timeout, void *, sig) >> #endif >> -#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll) >> +#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64) >> safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, >> struct timespec *, tsp, const sigset_t *, sigmask, >> size_t, sigsetsize) >> @@ -984,7 +984,7 @@ abi_long do_brk(abi_ulong new_brk) >> } >> >> #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ >> - defined(TARGET_NR_pselect6) >> + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) >> static inline abi_long copy_from_user_fdset(fd_set *fds, >> abi_ulong target_fds_addr, >> int n) >> @@ -1252,7 +1252,8 @@ 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_pselect6_time64) || defined(TARGET_NR_ppoll_time64) >> static inline abi_long target_to_host_timespec64(struct timespec *host_ts, >> abi_ulong target_addr) >> { >> @@ -1458,6 +1459,237 @@ static abi_long do_old_select(abi_ulong arg1) >> #endif >> #endif >> >> +#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) >> +static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, >> + abi_long arg4, abi_long arg5, abi_long arg6, >> + bool time64) >> +{ >> + abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; >> + fd_set rfds, wfds, efds; >> + fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; >> + struct timespec ts, *ts_ptr; >> + abi_long ret; >> + >> + /* >> + * The 6th arg is actually two args smashed together, >> + * so we cannot use the C library. >> + */ >> + sigset_t set; >> + struct { >> + sigset_t *set; >> + size_t size; >> + } sig, *sig_ptr; >> + >> + abi_ulong arg_sigset, arg_sigsize, *arg7; >> + target_sigset_t *target_sigset; >> + >> + n = arg1; >> + rfd_addr = arg2; >> + wfd_addr = arg3; >> + efd_addr = arg4; >> + ts_addr = arg5; >> + >> + ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); >> + if (ret) { >> + return ret; >> + } >> + ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); >> + if (ret) { >> + return ret; >> + } >> + ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); >> + if (ret) { >> + return ret; >> + } >> + >> + /* >> + * This takes a timespec, and not a timeval, so we cannot >> + * use the do_select() helper ... >> + */ >> + if (ts_addr) { >> + if (time64) { >> + if (target_to_host_timespec64(&ts, ts_addr)) { >> + return -TARGET_EFAULT; >> + } >> + } else { >> + if (target_to_host_timespec(&ts, ts_addr)) { >> + return -TARGET_EFAULT; >> + } >> + } >> + ts_ptr = &ts; >> + } else { >> + ts_ptr = NULL; >> + } >> + >> + /* Extract the two packed args for the sigset */ >> + if (arg6) { >> + sig_ptr = &sig; >> + sig.size = SIGSET_T_SIZE; >> + >> + arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); >> + if (!arg7) { >> + return -TARGET_EFAULT; >> + } >> + arg_sigset = tswapal(arg7[0]); >> + arg_sigsize = tswapal(arg7[1]); >> + unlock_user(arg7, arg6, 0); >> + >> + if (arg_sigset) { >> + sig.set = &set; >> + if (arg_sigsize != sizeof(*target_sigset)) { >> + /* Like the kernel, we enforce correct size sigsets */ >> + return -TARGET_EINVAL; >> + } >> + target_sigset = lock_user(VERIFY_READ, arg_sigset, >> + sizeof(*target_sigset), 1); >> + if (!target_sigset) { >> + return -TARGET_EFAULT; >> + } >> + target_to_host_sigset(&set, target_sigset); >> + unlock_user(target_sigset, arg_sigset, 0); >> + } else { >> + sig.set = NULL; >> + } >> + } else { >> + sig_ptr = NULL; >> + } >> + >> + ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, >> + ts_ptr, sig_ptr)); >> + >> + if (!is_error(ret)) { >> + if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) { >> + return -TARGET_EFAULT; >> + } >> + if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) { >> + return -TARGET_EFAULT; >> + } >> + if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) { >> + return -TARGET_EFAULT; >> + } >> + if (time64) { >> + if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { >> + return -TARGET_EFAULT; >> + } >> + } else { >> + if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { >> + return -TARGET_EFAULT; >> + } >> + } >> + } >> + return ret; >> +} >> +#endif >> + >> +#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \ >> + defined(TARGET_NR_ppoll_time64) >> +static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3, >> + abi_long arg4, abi_long arg5, bool ppoll, bool time64) >> +{ >> + struct target_pollfd *target_pfd; >> + unsigned int nfds = arg2; >> + struct pollfd *pfd; >> + unsigned int i; >> + abi_long ret; >> + >> + pfd = NULL; >> + target_pfd = NULL; >> + if (nfds) { >> + if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { >> + return -TARGET_EINVAL; >> + } >> + target_pfd = lock_user(VERIFY_WRITE, arg1, >> + sizeof(struct target_pollfd) * nfds, 1); >> + if (!target_pfd) { >> + return -TARGET_EFAULT; >> + } >> + >> + pfd = alloca(sizeof(struct pollfd) * nfds); >> + for (i = 0; i < nfds; i++) { >> + pfd[i].fd = tswap32(target_pfd[i].fd); >> + pfd[i].events = tswap16(target_pfd[i].events); >> + } >> + } >> + if (ppoll) { >> + struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; >> + target_sigset_t *target_set; >> + sigset_t _set, *set = &_set; >> + >> + if (arg3) { >> + if (time64) { >> + if (target_to_host_timespec64(timeout_ts, arg3)) { >> + unlock_user(target_pfd, arg1, 0); >> + return -TARGET_EFAULT; >> + } >> + } else { >> + if (target_to_host_timespec(timeout_ts, arg3)) { >> + unlock_user(target_pfd, arg1, 0); >> + return -TARGET_EFAULT; >> + } >> + } >> + } else { >> + timeout_ts = NULL; >> + } >> + >> + if (arg4) { >> + if (arg5 != sizeof(target_sigset_t)) { >> + unlock_user(target_pfd, arg1, 0); >> + return -TARGET_EINVAL; >> + } >> + >> + target_set = lock_user(VERIFY_READ, arg4, >> + sizeof(target_sigset_t), 1); >> + if (!target_set) { >> + unlock_user(target_pfd, arg1, 0); >> + return -TARGET_EFAULT; >> + } >> + target_to_host_sigset(set, target_set); >> + } else { >> + set = NULL; >> + } >> + >> + ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, >> + set, SIGSET_T_SIZE)); >> + >> + if (!is_error(ret) && arg3) { >> + if (time64) { >> + if (host_to_target_timespec64(arg3, timeout_ts)) { >> + return -TARGET_EFAULT; >> + } >> + } else { >> + if (host_to_target_timespec(arg3, timeout_ts)) { >> + return -TARGET_EFAULT; >> + } >> + } >> + } >> + if (arg4) { >> + unlock_user(target_set, arg4, 0); >> + } >> + } else { >> + struct timespec ts, *pts; >> + >> + if (arg3 >= 0) { >> + /* Convert ms to secs, ns */ >> + ts.tv_sec = arg3 / 1000; >> + ts.tv_nsec = (arg3 % 1000) * 1000000LL; >> + pts = &ts; >> + } else { >> + /* -ve poll() timeout means "infinite" */ >> + pts = NULL; >> + } >> + ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); >> + } >> + >> + if (!is_error(ret)) { >> + for (i = 0; i < nfds; i++) { >> + target_pfd[i].revents = tswap16(pfd[i].revents); >> + } >> + } >> + unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); >> + return ret; >> +} >> +#endif >> + >> static abi_long do_pipe2(int host_pipe[], int flags) >> { >> #ifdef CONFIG_PIPE2 >> @@ -9045,106 +9277,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >> #endif >> #ifdef TARGET_NR_pselect6 >> case TARGET_NR_pselect6: >> - { >> - abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; >> - fd_set rfds, wfds, efds; >> - fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; >> - struct timespec ts, *ts_ptr; >> - >> - /* >> - * The 6th arg is actually two args smashed together, >> - * so we cannot use the C library. >> - */ >> - sigset_t set; >> - struct { >> - sigset_t *set; >> - size_t size; >> - } sig, *sig_ptr; >> - >> - abi_ulong arg_sigset, arg_sigsize, *arg7; >> - target_sigset_t *target_sigset; >> - >> - n = arg1; >> - rfd_addr = arg2; >> - wfd_addr = arg3; >> - efd_addr = arg4; >> - ts_addr = arg5; >> - >> - ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); >> - if (ret) { >> - return ret; >> - } >> - ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); >> - if (ret) { >> - return ret; >> - } >> - ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); >> - if (ret) { >> - return ret; >> - } >> - >> - /* >> - * This takes a timespec, and not a timeval, so we cannot >> - * use the do_select() helper ... >> - */ >> - if (ts_addr) { >> - if (target_to_host_timespec(&ts, ts_addr)) { >> - return -TARGET_EFAULT; >> - } >> - ts_ptr = &ts; >> - } else { >> - ts_ptr = NULL; >> - } >> - >> - /* Extract the two packed args for the sigset */ >> - if (arg6) { >> - sig_ptr = &sig; >> - sig.size = SIGSET_T_SIZE; >> - >> - arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); >> - if (!arg7) { >> - return -TARGET_EFAULT; >> - } >> - arg_sigset = tswapal(arg7[0]); >> - arg_sigsize = tswapal(arg7[1]); >> - unlock_user(arg7, arg6, 0); >> - >> - if (arg_sigset) { >> - sig.set = &set; >> - if (arg_sigsize != sizeof(*target_sigset)) { >> - /* Like the kernel, we enforce correct size sigsets */ >> - return -TARGET_EINVAL; >> - } >> - target_sigset = lock_user(VERIFY_READ, arg_sigset, >> - sizeof(*target_sigset), 1); >> - if (!target_sigset) { >> - return -TARGET_EFAULT; >> - } >> - target_to_host_sigset(&set, target_sigset); >> - unlock_user(target_sigset, arg_sigset, 0); >> - } else { >> - sig.set = NULL; >> - } >> - } else { >> - sig_ptr = NULL; >> - } >> - >> - ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, >> - ts_ptr, sig_ptr)); >> - >> - if (!is_error(ret)) { >> - if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) >> - return -TARGET_EFAULT; >> - if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) >> - return -TARGET_EFAULT; >> - if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) >> - return -TARGET_EFAULT; >> - >> - if (ts_addr && host_to_target_timespec(ts_addr, &ts)) >> - return -TARGET_EFAULT; >> - } >> - } >> - return ret; >> + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false); >> +#endif >> +#ifdef TARGET_NR_pselect6_time64 >> + case TARGET_NR_pselect6_time64: >> + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true); >> #endif >> #ifdef TARGET_NR_symlink >> case TARGET_NR_symlink: >> @@ -10076,114 +10213,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >> case TARGET_NR__newselect: >> return do_select(arg1, arg2, arg3, arg4, arg5); >> #endif >> -#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) >> -# ifdef TARGET_NR_poll >> +#ifdef TARGET_NR_poll >> case TARGET_NR_poll: >> -# endif >> -# ifdef TARGET_NR_ppoll >> + return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false); >> +#endif >> +#ifdef TARGET_NR_ppoll >> case TARGET_NR_ppoll: >> -# endif >> - { >> - struct target_pollfd *target_pfd; >> - unsigned int nfds = arg2; >> - struct pollfd *pfd; >> - unsigned int i; >> - >> - pfd = NULL; >> - target_pfd = NULL; >> - if (nfds) { >> - if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { >> - return -TARGET_EINVAL; >> - } >> - >> - target_pfd = lock_user(VERIFY_WRITE, arg1, >> - sizeof(struct target_pollfd) * nfds, 1); >> - if (!target_pfd) { >> - return -TARGET_EFAULT; >> - } >> - >> - pfd = alloca(sizeof(struct pollfd) * nfds); >> - for (i = 0; i < nfds; i++) { >> - pfd[i].fd = tswap32(target_pfd[i].fd); >> - pfd[i].events = tswap16(target_pfd[i].events); >> - } >> - } >> - >> - switch (num) { >> -# ifdef TARGET_NR_ppoll >> - case TARGET_NR_ppoll: >> - { >> - struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; >> - target_sigset_t *target_set; >> - sigset_t _set, *set = &_set; >> - >> - if (arg3) { >> - if (target_to_host_timespec(timeout_ts, arg3)) { >> - unlock_user(target_pfd, arg1, 0); >> - return -TARGET_EFAULT; >> - } >> - } else { >> - timeout_ts = NULL; >> - } >> - >> - if (arg4) { >> - if (arg5 != sizeof(target_sigset_t)) { >> - unlock_user(target_pfd, arg1, 0); >> - return -TARGET_EINVAL; >> - } >> - >> - target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1); >> - if (!target_set) { >> - unlock_user(target_pfd, arg1, 0); >> - return -TARGET_EFAULT; >> - } >> - target_to_host_sigset(set, target_set); >> - } else { >> - set = NULL; >> - } >> - >> - ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, >> - set, SIGSET_T_SIZE)); >> - >> - if (!is_error(ret) && arg3) { >> - host_to_target_timespec(arg3, timeout_ts); >> - } >> - if (arg4) { >> - unlock_user(target_set, arg4, 0); >> - } >> - break; >> - } >> -# endif >> -# ifdef TARGET_NR_poll >> - case TARGET_NR_poll: >> - { >> - struct timespec ts, *pts; >> - >> - if (arg3 >= 0) { >> - /* Convert ms to secs, ns */ >> - ts.tv_sec = arg3 / 1000; >> - ts.tv_nsec = (arg3 % 1000) * 1000000LL; >> - pts = &ts; >> - } else { >> - /* -ve poll() timeout means "infinite" */ >> - pts = NULL; >> - } >> - ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); >> - break; >> - } >> -# endif >> - default: >> - g_assert_not_reached(); >> - } >> - >> - if (!is_error(ret)) { >> - for(i = 0; i < nfds; i++) { >> - target_pfd[i].revents = tswap16(pfd[i].revents); >> - } >> - } >> - unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); >> - } >> - return ret; >> + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false); >> +#endif >> +#ifdef TARGET_NR_ppoll_time64 >> + case TARGET_NR_ppoll_time64: >> + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true); >> #endif >> case TARGET_NR_flock: >> /* NOTE: the flock constant seems to be the same for every >> > > Applied to my linux-user-for-5.2 branch. I have removed it from my branch because this patch breaks "go run hello.go" on armhf/bionic: # cat /tmp/hello.go package main import "fmt" func main() { fmt.Println("Hello World!") } # go run /tmp/hello.go fatal error: unexpected signal during runtime execution fatal error: unexpected signal during runtime execution panic during panic [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x0] ... Thanks, Laurent ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() 2020-08-25 15:23 ` Laurent Vivier @ 2020-09-05 20:41 ` Laurent Vivier 0 siblings, 0 replies; 11+ messages in thread From: Laurent Vivier @ 2020-09-05 20:41 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 25/08/2020 à 17:23, Laurent Vivier a écrit : > Le 25/08/2020 à 09:17, Laurent Vivier a écrit : >> Le 25/08/2020 à 00:30, Filip Bozuta a écrit : >>> This patch introduces functionality for following time64 syscalls: >>> >>> *ppoll_time64 >>> >>> This is a year 2038 safe variant of: >>> >>> int poll(struct pollfd *fds, nfds_t nfds, int timeout) >>> -- wait for some event on a file descriptor -- >>> man page: https://man7.org/linux/man-pages/man2/ppoll.2.html >>> >>> *pselect6_time64 >>> >>> This is a year 2038 safe variant of: >>> >>> int pselect6(int nfds, fd_set *readfds, fd_set *writefds, >>> fd_set *exceptfds, const struct timespec *timeout, >>> const sigset_t *sigmask); >>> -- synchronous I/O multiplexing -- >>> man page: https://man7.org/linux/man-pages/man2/pselect6.2.html >>> >>> Implementation notes: >>> >>> Year 2038 safe syscalls in this patch were implemented >>> with the same code as their regular variants (ppoll() and pselect()). >>> This code was moved to new functions ('do_ppoll()' and 'do_pselect6()') >>> that take a 'bool time64' from which a right 'struct timespec' converting >>> function is called. >>> (target_to_host/host_to_target_timespec() for regular and >>> target_to_host/host_to_target_timespec64() for time64 variants) >>> >>> Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> >>> --- >>> linux-user/syscall.c | 462 +++++++++++++++++++++++-------------------- >>> 1 file changed, 251 insertions(+), 211 deletions(-) >>> >>> diff --git a/linux-user/syscall.c b/linux-user/syscall.c >>> index 1211e759c2..fc6a6e32e4 100644 >>> --- a/linux-user/syscall.c >>> +++ b/linux-user/syscall.c >>> @@ -397,7 +397,7 @@ static int sys_getcwd1(char *buf, size_t size) >>> return strlen(buf)+1; >>> } >>> >>> -#ifdef TARGET_NR_utimensat >>> +#if defined(TARGET_NR_utimensat) >>> #if defined(__NR_utimensat) >>> #define __NR_sys_utimensat __NR_utimensat >>> _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, >>> @@ -763,11 +763,11 @@ safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \ >>> int, options, struct rusage *, rusage) >>> safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp) >>> #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ >>> - defined(TARGET_NR_pselect6) >>> + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) >>> safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \ >>> fd_set *, exceptfds, struct timespec *, timeout, void *, sig) >>> #endif >>> -#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_poll) >>> +#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64) >>> safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds, >>> struct timespec *, tsp, const sigset_t *, sigmask, >>> size_t, sigsetsize) >>> @@ -984,7 +984,7 @@ abi_long do_brk(abi_ulong new_brk) >>> } >>> >>> #if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \ >>> - defined(TARGET_NR_pselect6) >>> + defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) >>> static inline abi_long copy_from_user_fdset(fd_set *fds, >>> abi_ulong target_fds_addr, >>> int n) >>> @@ -1252,7 +1252,8 @@ 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_pselect6_time64) || defined(TARGET_NR_ppoll_time64) >>> static inline abi_long target_to_host_timespec64(struct timespec *host_ts, >>> abi_ulong target_addr) >>> { >>> @@ -1458,6 +1459,237 @@ static abi_long do_old_select(abi_ulong arg1) >>> #endif >>> #endif >>> >>> +#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64) >>> +static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, >>> + abi_long arg4, abi_long arg5, abi_long arg6, >>> + bool time64) >>> +{ >>> + abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; >>> + fd_set rfds, wfds, efds; >>> + fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; >>> + struct timespec ts, *ts_ptr; >>> + abi_long ret; >>> + >>> + /* >>> + * The 6th arg is actually two args smashed together, >>> + * so we cannot use the C library. >>> + */ >>> + sigset_t set; >>> + struct { >>> + sigset_t *set; >>> + size_t size; >>> + } sig, *sig_ptr; >>> + >>> + abi_ulong arg_sigset, arg_sigsize, *arg7; >>> + target_sigset_t *target_sigset; >>> + >>> + n = arg1; >>> + rfd_addr = arg2; >>> + wfd_addr = arg3; >>> + efd_addr = arg4; >>> + ts_addr = arg5; >>> + >>> + ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); >>> + if (ret) { >>> + return ret; >>> + } >>> + ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); >>> + if (ret) { >>> + return ret; >>> + } >>> + ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); >>> + if (ret) { >>> + return ret; >>> + } >>> + >>> + /* >>> + * This takes a timespec, and not a timeval, so we cannot >>> + * use the do_select() helper ... >>> + */ >>> + if (ts_addr) { >>> + if (time64) { >>> + if (target_to_host_timespec64(&ts, ts_addr)) { >>> + return -TARGET_EFAULT; >>> + } >>> + } else { >>> + if (target_to_host_timespec(&ts, ts_addr)) { >>> + return -TARGET_EFAULT; >>> + } >>> + } >>> + ts_ptr = &ts; >>> + } else { >>> + ts_ptr = NULL; >>> + } >>> + >>> + /* Extract the two packed args for the sigset */ >>> + if (arg6) { >>> + sig_ptr = &sig; >>> + sig.size = SIGSET_T_SIZE; >>> + >>> + arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); >>> + if (!arg7) { >>> + return -TARGET_EFAULT; >>> + } >>> + arg_sigset = tswapal(arg7[0]); >>> + arg_sigsize = tswapal(arg7[1]); >>> + unlock_user(arg7, arg6, 0); >>> + >>> + if (arg_sigset) { >>> + sig.set = &set; >>> + if (arg_sigsize != sizeof(*target_sigset)) { >>> + /* Like the kernel, we enforce correct size sigsets */ >>> + return -TARGET_EINVAL; >>> + } >>> + target_sigset = lock_user(VERIFY_READ, arg_sigset, >>> + sizeof(*target_sigset), 1); >>> + if (!target_sigset) { >>> + return -TARGET_EFAULT; >>> + } >>> + target_to_host_sigset(&set, target_sigset); >>> + unlock_user(target_sigset, arg_sigset, 0); >>> + } else { >>> + sig.set = NULL; >>> + } >>> + } else { >>> + sig_ptr = NULL; >>> + } >>> + >>> + ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, >>> + ts_ptr, sig_ptr)); >>> + >>> + if (!is_error(ret)) { >>> + if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) { >>> + return -TARGET_EFAULT; >>> + } >>> + if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) { >>> + return -TARGET_EFAULT; >>> + } >>> + if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) { >>> + return -TARGET_EFAULT; >>> + } >>> + if (time64) { >>> + if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { >>> + return -TARGET_EFAULT; >>> + } >>> + } else { >>> + if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { >>> + return -TARGET_EFAULT; >>> + } >>> + } >>> + } >>> + return ret; >>> +} >>> +#endif >>> + >>> +#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \ >>> + defined(TARGET_NR_ppoll_time64) >>> +static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3, >>> + abi_long arg4, abi_long arg5, bool ppoll, bool time64) >>> +{ >>> + struct target_pollfd *target_pfd; >>> + unsigned int nfds = arg2; >>> + struct pollfd *pfd; >>> + unsigned int i; >>> + abi_long ret; >>> + >>> + pfd = NULL; >>> + target_pfd = NULL; >>> + if (nfds) { >>> + if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { >>> + return -TARGET_EINVAL; >>> + } >>> + target_pfd = lock_user(VERIFY_WRITE, arg1, >>> + sizeof(struct target_pollfd) * nfds, 1); >>> + if (!target_pfd) { >>> + return -TARGET_EFAULT; >>> + } >>> + >>> + pfd = alloca(sizeof(struct pollfd) * nfds); >>> + for (i = 0; i < nfds; i++) { >>> + pfd[i].fd = tswap32(target_pfd[i].fd); >>> + pfd[i].events = tswap16(target_pfd[i].events); >>> + } >>> + } >>> + if (ppoll) { >>> + struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; >>> + target_sigset_t *target_set; >>> + sigset_t _set, *set = &_set; >>> + >>> + if (arg3) { >>> + if (time64) { >>> + if (target_to_host_timespec64(timeout_ts, arg3)) { >>> + unlock_user(target_pfd, arg1, 0); >>> + return -TARGET_EFAULT; >>> + } >>> + } else { >>> + if (target_to_host_timespec(timeout_ts, arg3)) { >>> + unlock_user(target_pfd, arg1, 0); >>> + return -TARGET_EFAULT; >>> + } >>> + } >>> + } else { >>> + timeout_ts = NULL; >>> + } >>> + >>> + if (arg4) { >>> + if (arg5 != sizeof(target_sigset_t)) { >>> + unlock_user(target_pfd, arg1, 0); >>> + return -TARGET_EINVAL; >>> + } >>> + >>> + target_set = lock_user(VERIFY_READ, arg4, >>> + sizeof(target_sigset_t), 1); >>> + if (!target_set) { >>> + unlock_user(target_pfd, arg1, 0); >>> + return -TARGET_EFAULT; >>> + } >>> + target_to_host_sigset(set, target_set); >>> + } else { >>> + set = NULL; >>> + } >>> + >>> + ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, >>> + set, SIGSET_T_SIZE)); >>> + >>> + if (!is_error(ret) && arg3) { >>> + if (time64) { >>> + if (host_to_target_timespec64(arg3, timeout_ts)) { >>> + return -TARGET_EFAULT; >>> + } >>> + } else { >>> + if (host_to_target_timespec(arg3, timeout_ts)) { >>> + return -TARGET_EFAULT; >>> + } >>> + } >>> + } >>> + if (arg4) { >>> + unlock_user(target_set, arg4, 0); >>> + } >>> + } else { >>> + struct timespec ts, *pts; >>> + >>> + if (arg3 >= 0) { >>> + /* Convert ms to secs, ns */ >>> + ts.tv_sec = arg3 / 1000; >>> + ts.tv_nsec = (arg3 % 1000) * 1000000LL; >>> + pts = &ts; >>> + } else { >>> + /* -ve poll() timeout means "infinite" */ >>> + pts = NULL; >>> + } >>> + ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); >>> + } >>> + >>> + if (!is_error(ret)) { >>> + for (i = 0; i < nfds; i++) { >>> + target_pfd[i].revents = tswap16(pfd[i].revents); >>> + } >>> + } >>> + unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); >>> + return ret; >>> +} >>> +#endif >>> + >>> static abi_long do_pipe2(int host_pipe[], int flags) >>> { >>> #ifdef CONFIG_PIPE2 >>> @@ -9045,106 +9277,11 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >>> #endif >>> #ifdef TARGET_NR_pselect6 >>> case TARGET_NR_pselect6: >>> - { >>> - abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr; >>> - fd_set rfds, wfds, efds; >>> - fd_set *rfds_ptr, *wfds_ptr, *efds_ptr; >>> - struct timespec ts, *ts_ptr; >>> - >>> - /* >>> - * The 6th arg is actually two args smashed together, >>> - * so we cannot use the C library. >>> - */ >>> - sigset_t set; >>> - struct { >>> - sigset_t *set; >>> - size_t size; >>> - } sig, *sig_ptr; >>> - >>> - abi_ulong arg_sigset, arg_sigsize, *arg7; >>> - target_sigset_t *target_sigset; >>> - >>> - n = arg1; >>> - rfd_addr = arg2; >>> - wfd_addr = arg3; >>> - efd_addr = arg4; >>> - ts_addr = arg5; >>> - >>> - ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n); >>> - if (ret) { >>> - return ret; >>> - } >>> - ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n); >>> - if (ret) { >>> - return ret; >>> - } >>> - ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n); >>> - if (ret) { >>> - return ret; >>> - } >>> - >>> - /* >>> - * This takes a timespec, and not a timeval, so we cannot >>> - * use the do_select() helper ... >>> - */ >>> - if (ts_addr) { >>> - if (target_to_host_timespec(&ts, ts_addr)) { >>> - return -TARGET_EFAULT; >>> - } >>> - ts_ptr = &ts; >>> - } else { >>> - ts_ptr = NULL; >>> - } >>> - >>> - /* Extract the two packed args for the sigset */ >>> - if (arg6) { >>> - sig_ptr = &sig; >>> - sig.size = SIGSET_T_SIZE; >>> - >>> - arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1); >>> - if (!arg7) { >>> - return -TARGET_EFAULT; >>> - } >>> - arg_sigset = tswapal(arg7[0]); >>> - arg_sigsize = tswapal(arg7[1]); >>> - unlock_user(arg7, arg6, 0); >>> - >>> - if (arg_sigset) { >>> - sig.set = &set; >>> - if (arg_sigsize != sizeof(*target_sigset)) { >>> - /* Like the kernel, we enforce correct size sigsets */ >>> - return -TARGET_EINVAL; >>> - } >>> - target_sigset = lock_user(VERIFY_READ, arg_sigset, >>> - sizeof(*target_sigset), 1); >>> - if (!target_sigset) { >>> - return -TARGET_EFAULT; >>> - } >>> - target_to_host_sigset(&set, target_sigset); >>> - unlock_user(target_sigset, arg_sigset, 0); >>> - } else { >>> - sig.set = NULL; >>> - } >>> - } else { >>> - sig_ptr = NULL; >>> - } >>> - >>> - ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr, >>> - ts_ptr, sig_ptr)); >>> - >>> - if (!is_error(ret)) { >>> - if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) >>> - return -TARGET_EFAULT; >>> - if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) >>> - return -TARGET_EFAULT; >>> - if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) >>> - return -TARGET_EFAULT; >>> - >>> - if (ts_addr && host_to_target_timespec(ts_addr, &ts)) >>> - return -TARGET_EFAULT; >>> - } >>> - } >>> - return ret; >>> + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false); >>> +#endif >>> +#ifdef TARGET_NR_pselect6_time64 >>> + case TARGET_NR_pselect6_time64: >>> + return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true); >>> #endif >>> #ifdef TARGET_NR_symlink >>> case TARGET_NR_symlink: >>> @@ -10076,114 +10213,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >>> case TARGET_NR__newselect: >>> return do_select(arg1, arg2, arg3, arg4, arg5); >>> #endif >>> -#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) >>> -# ifdef TARGET_NR_poll >>> +#ifdef TARGET_NR_poll >>> case TARGET_NR_poll: >>> -# endif >>> -# ifdef TARGET_NR_ppoll >>> + return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false); >>> +#endif >>> +#ifdef TARGET_NR_ppoll >>> case TARGET_NR_ppoll: >>> -# endif >>> - { >>> - struct target_pollfd *target_pfd; >>> - unsigned int nfds = arg2; >>> - struct pollfd *pfd; >>> - unsigned int i; >>> - >>> - pfd = NULL; >>> - target_pfd = NULL; >>> - if (nfds) { >>> - if (nfds > (INT_MAX / sizeof(struct target_pollfd))) { >>> - return -TARGET_EINVAL; >>> - } >>> - >>> - target_pfd = lock_user(VERIFY_WRITE, arg1, >>> - sizeof(struct target_pollfd) * nfds, 1); >>> - if (!target_pfd) { >>> - return -TARGET_EFAULT; >>> - } >>> - >>> - pfd = alloca(sizeof(struct pollfd) * nfds); >>> - for (i = 0; i < nfds; i++) { >>> - pfd[i].fd = tswap32(target_pfd[i].fd); >>> - pfd[i].events = tswap16(target_pfd[i].events); >>> - } >>> - } >>> - >>> - switch (num) { >>> -# ifdef TARGET_NR_ppoll >>> - case TARGET_NR_ppoll: >>> - { >>> - struct timespec _timeout_ts, *timeout_ts = &_timeout_ts; >>> - target_sigset_t *target_set; >>> - sigset_t _set, *set = &_set; >>> - >>> - if (arg3) { >>> - if (target_to_host_timespec(timeout_ts, arg3)) { >>> - unlock_user(target_pfd, arg1, 0); >>> - return -TARGET_EFAULT; >>> - } >>> - } else { >>> - timeout_ts = NULL; >>> - } >>> - >>> - if (arg4) { >>> - if (arg5 != sizeof(target_sigset_t)) { >>> - unlock_user(target_pfd, arg1, 0); >>> - return -TARGET_EINVAL; >>> - } >>> - >>> - target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1); >>> - if (!target_set) { >>> - unlock_user(target_pfd, arg1, 0); >>> - return -TARGET_EFAULT; >>> - } >>> - target_to_host_sigset(set, target_set); >>> - } else { >>> - set = NULL; >>> - } >>> - >>> - ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts, >>> - set, SIGSET_T_SIZE)); >>> - >>> - if (!is_error(ret) && arg3) { >>> - host_to_target_timespec(arg3, timeout_ts); >>> - } >>> - if (arg4) { >>> - unlock_user(target_set, arg4, 0); >>> - } >>> - break; >>> - } >>> -# endif >>> -# ifdef TARGET_NR_poll >>> - case TARGET_NR_poll: >>> - { >>> - struct timespec ts, *pts; >>> - >>> - if (arg3 >= 0) { >>> - /* Convert ms to secs, ns */ >>> - ts.tv_sec = arg3 / 1000; >>> - ts.tv_nsec = (arg3 % 1000) * 1000000LL; >>> - pts = &ts; >>> - } else { >>> - /* -ve poll() timeout means "infinite" */ >>> - pts = NULL; >>> - } >>> - ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0)); >>> - break; >>> - } >>> -# endif >>> - default: >>> - g_assert_not_reached(); >>> - } >>> - >>> - if (!is_error(ret)) { >>> - for(i = 0; i < nfds; i++) { >>> - target_pfd[i].revents = tswap16(pfd[i].revents); >>> - } >>> - } >>> - unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds); >>> - } >>> - return ret; >>> + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false); >>> +#endif >>> +#ifdef TARGET_NR_ppoll_time64 >>> + case TARGET_NR_ppoll_time64: >>> + return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true); >>> #endif >>> case TARGET_NR_flock: >>> /* NOTE: the flock constant seems to be the same for every >>> >> >> Applied to my linux-user-for-5.2 branch. > > > I have removed it from my branch because this patch breaks "go run > hello.go" on armhf/bionic: > > # cat /tmp/hello.go > package main > > import "fmt" > > func main() { > fmt.Println("Hello World!") > } > > # go run /tmp/hello.go > fatal error: unexpected signal during runtime execution > fatal error: unexpected signal during runtime execution > panic during panic > [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x0] > ... > I've re-applied this patch to linux-user-for-5.2 with this change that fixes the problem above: --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -1568,11 +1568,11 @@ static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3, return -TARGET_EFAULT; } if (time64) { - if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { + if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { return -TARGET_EFAULT; } } else { - if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) { + if (ts_addr && host_to_target_timespec(ts_addr, &ts)) { return -TARGET_EFAULT; } } Thanks, Laurent ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() 2020-08-24 22:30 [PATCH v2 0/2] linux-user: Adding support for a group of 4 time64 syscalls Filip Bozuta 2020-08-24 22:30 ` [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() Filip Bozuta @ 2020-08-24 22:30 ` Filip Bozuta 2020-08-25 7:18 ` Laurent Vivier 1 sibling, 1 reply; 11+ messages in thread From: Filip Bozuta @ 2020-08-24 22:30 UTC (permalink / raw) To: qemu-devel; +Cc: Laurent Vivier, Filip Bozuta This patch introduces functionality for following time64 syscalls: *utimensat_time64() int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags); -- change file timestamps with nanosecond precision -- man page: https://man7.org/linux/man-pages/man2/utimensat.2.html *semtimedop_time64() int semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timespec *timeout); -- System V semaphore operations -- man page: https://www.man7.org/linux/man-pages/man2/semtimedop.2.html Implementation notes: Syscall 'utimensat_time64()' is implemented in similar way as its regular variants only difference being that time64 converting function is used to convert values of 'struct timespec' between host and target ('target_to_host_timespec64()'). For syscall 'semtimedop_time64()' and additional argument is added in function 'do_semtimedop()' through which the aproppriate 'struct timespec' converting function is called (false for regular target_to_host_timespec() and true for target_to_host_timespec64()). For 'do_ipc()' a check was added as that additional argument: 'TARGET_ABI_BITS == 64'. Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> Reviewed-by: Laurent Vivier <laurent@vivier.eu> --- linux-user/syscall.c | 60 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index fc6a6e32e4..4d460af744 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -1253,7 +1253,8 @@ static inline abi_long target_to_host_timespec(struct timespec *host_ts, #endif #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \ - defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) + defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) || \ + defined(TARGET_NR_utimensat_time64) || defined(TARGET_NR_semtimedop_time64) static inline abi_long target_to_host_timespec64(struct timespec *host_ts, abi_ulong target_addr) { @@ -4117,7 +4118,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, } #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ - defined(TARGET_NR_semtimedop) + defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64) /* * This macro is required to handle the s390 variants, which passes the @@ -4134,7 +4135,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, static inline abi_long do_semtimedop(int semid, abi_long ptr, unsigned nsops, - abi_long timeout) + abi_long timeout, bool time64) { struct sembuf sops[nsops]; struct timespec ts, *pts = NULL; @@ -4142,8 +4143,14 @@ static inline abi_long do_semtimedop(int semid, if (timeout) { pts = &ts; - if (target_to_host_timespec(pts, timeout)) { - return -TARGET_EFAULT; + if (time64) { + if (target_to_host_timespec64(pts, timeout)) { + return -TARGET_EFAULT; + } + } else { + if (target_to_host_timespec(pts, timeout)) { + return -TARGET_EFAULT; + } } } @@ -4657,7 +4664,7 @@ static abi_long do_ipc(CPUArchState *cpu_env, switch (call) { case IPCOP_semop: - ret = do_semtimedop(first, ptr, second, 0); + ret = do_semtimedop(first, ptr, second, 0, false); break; case IPCOP_semtimedop: /* @@ -4667,9 +4674,9 @@ static abi_long do_ipc(CPUArchState *cpu_env, * to a struct timespec where the generic variant uses fifth parameter. */ #if defined(TARGET_S390X) - ret = do_semtimedop(first, ptr, second, third); + ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64); #else - ret = do_semtimedop(first, ptr, second, fifth); + ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64); #endif break; @@ -9887,11 +9894,15 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, #endif #ifdef TARGET_NR_semop case TARGET_NR_semop: - return do_semtimedop(arg1, arg2, arg3, 0); + return do_semtimedop(arg1, arg2, arg3, 0, false); #endif #ifdef TARGET_NR_semtimedop case TARGET_NR_semtimedop: - return do_semtimedop(arg1, arg2, arg3, arg4); + return do_semtimedop(arg1, arg2, arg3, arg4, false); +#endif +#ifdef TARGET_NR_semtimedop_time64 + case TARGET_NR_semtimedop_time64: + return do_semtimedop(arg1, arg2, arg3, arg4, true); #endif #ifdef TARGET_NR_semctl case TARGET_NR_semctl: @@ -11938,6 +11949,35 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, } return ret; #endif +#ifdef TARGET_NR_utimensat_time64 + case TARGET_NR_utimensat_time64: + { + struct timespec *tsp, ts[2]; + if (!arg3) { + tsp = NULL; + } else { + if (target_to_host_timespec64(ts, arg3)) { + return -TARGET_EFAULT; + } + if (target_to_host_timespec64(ts + 1, arg3 + + sizeof(struct target__kernel_timespec))) { + return -TARGET_EFAULT; + } + tsp = ts; + } + if (!arg2) + ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); + else { + p = lock_user_string(arg2); + if (!p) { + return -TARGET_EFAULT; + } + ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); + unlock_user(p, arg2, 0); + } + } + return ret; +#endif #ifdef TARGET_NR_futex case TARGET_NR_futex: return do_futex(arg1, arg2, arg3, arg4, arg5, arg6); -- 2.25.1 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() 2020-08-24 22:30 ` [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() Filip Bozuta @ 2020-08-25 7:18 ` Laurent Vivier 2020-08-25 14:23 ` Laurent Vivier 0 siblings, 1 reply; 11+ messages in thread From: Laurent Vivier @ 2020-08-25 7:18 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 25/08/2020 à 00:30, Filip Bozuta a écrit : > This patch introduces functionality for following time64 syscalls: > > *utimensat_time64() > > int utimensat(int dirfd, const char *pathname, > const struct timespec times[2], int flags); > -- change file timestamps with nanosecond precision -- > man page: https://man7.org/linux/man-pages/man2/utimensat.2.html > > *semtimedop_time64() > > int semtimedop(int semid, struct sembuf *sops, size_t nsops, > const struct timespec *timeout); > -- System V semaphore operations -- > man page: https://www.man7.org/linux/man-pages/man2/semtimedop.2.html > > Implementation notes: > > Syscall 'utimensat_time64()' is implemented in similar way as its > regular variants only difference being that time64 converting function > is used to convert values of 'struct timespec' between host and target > ('target_to_host_timespec64()'). > > For syscall 'semtimedop_time64()' and additional argument is added > in function 'do_semtimedop()' through which the aproppriate 'struct timespec' > converting function is called (false for regular target_to_host_timespec() > and true for target_to_host_timespec64()). For 'do_ipc()' a > check was added as that additional argument: 'TARGET_ABI_BITS == 64'. > > Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> > Reviewed-by: Laurent Vivier <laurent@vivier.eu> > --- > linux-user/syscall.c | 60 ++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 50 insertions(+), 10 deletions(-) > > diff --git a/linux-user/syscall.c b/linux-user/syscall.c > index fc6a6e32e4..4d460af744 100644 > --- a/linux-user/syscall.c > +++ b/linux-user/syscall.c > @@ -1253,7 +1253,8 @@ static inline abi_long target_to_host_timespec(struct timespec *host_ts, > #endif > > #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \ > - defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) > + defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) || \ > + defined(TARGET_NR_utimensat_time64) || defined(TARGET_NR_semtimedop_time64) > static inline abi_long target_to_host_timespec64(struct timespec *host_ts, > abi_ulong target_addr) > { > @@ -4117,7 +4118,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, > } > > #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ > - defined(TARGET_NR_semtimedop) > + defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64) > > /* > * This macro is required to handle the s390 variants, which passes the > @@ -4134,7 +4135,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, > static inline abi_long do_semtimedop(int semid, > abi_long ptr, > unsigned nsops, > - abi_long timeout) > + abi_long timeout, bool time64) > { > struct sembuf sops[nsops]; > struct timespec ts, *pts = NULL; > @@ -4142,8 +4143,14 @@ static inline abi_long do_semtimedop(int semid, > > if (timeout) { > pts = &ts; > - if (target_to_host_timespec(pts, timeout)) { > - return -TARGET_EFAULT; > + if (time64) { > + if (target_to_host_timespec64(pts, timeout)) { > + return -TARGET_EFAULT; > + } > + } else { > + if (target_to_host_timespec(pts, timeout)) { > + return -TARGET_EFAULT; > + } > } > } > > @@ -4657,7 +4664,7 @@ static abi_long do_ipc(CPUArchState *cpu_env, > > switch (call) { > case IPCOP_semop: > - ret = do_semtimedop(first, ptr, second, 0); > + ret = do_semtimedop(first, ptr, second, 0, false); > break; > case IPCOP_semtimedop: > /* > @@ -4667,9 +4674,9 @@ static abi_long do_ipc(CPUArchState *cpu_env, > * to a struct timespec where the generic variant uses fifth parameter. > */ > #if defined(TARGET_S390X) > - ret = do_semtimedop(first, ptr, second, third); > + ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64); > #else > - ret = do_semtimedop(first, ptr, second, fifth); > + ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64); > #endif > break; > > @@ -9887,11 +9894,15 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, > #endif > #ifdef TARGET_NR_semop > case TARGET_NR_semop: > - return do_semtimedop(arg1, arg2, arg3, 0); > + return do_semtimedop(arg1, arg2, arg3, 0, false); > #endif > #ifdef TARGET_NR_semtimedop > case TARGET_NR_semtimedop: > - return do_semtimedop(arg1, arg2, arg3, arg4); > + return do_semtimedop(arg1, arg2, arg3, arg4, false); > +#endif > +#ifdef TARGET_NR_semtimedop_time64 > + case TARGET_NR_semtimedop_time64: > + return do_semtimedop(arg1, arg2, arg3, arg4, true); > #endif > #ifdef TARGET_NR_semctl > case TARGET_NR_semctl: > @@ -11938,6 +11949,35 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, > } > return ret; > #endif > +#ifdef TARGET_NR_utimensat_time64 > + case TARGET_NR_utimensat_time64: > + { > + struct timespec *tsp, ts[2]; > + if (!arg3) { > + tsp = NULL; > + } else { > + if (target_to_host_timespec64(ts, arg3)) { > + return -TARGET_EFAULT; > + } > + if (target_to_host_timespec64(ts + 1, arg3 + > + sizeof(struct target__kernel_timespec))) { > + return -TARGET_EFAULT; > + } > + tsp = ts; > + } > + if (!arg2) > + ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); > + else { > + p = lock_user_string(arg2); > + if (!p) { > + return -TARGET_EFAULT; > + } > + ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); > + unlock_user(p, arg2, 0); > + } > + } > + return ret; > +#endif > #ifdef TARGET_NR_futex > case TARGET_NR_futex: > return do_futex(arg1, arg2, arg3, arg4, arg5, arg6); > Applied to my linux-user-for-5.2 branch. Thanks, Laurent ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() 2020-08-25 7:18 ` Laurent Vivier @ 2020-08-25 14:23 ` Laurent Vivier 2020-08-26 13:58 ` Laurent Vivier 0 siblings, 1 reply; 11+ messages in thread From: Laurent Vivier @ 2020-08-25 14:23 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 25/08/2020 à 09:18, Laurent Vivier a écrit : > Le 25/08/2020 à 00:30, Filip Bozuta a écrit : >> This patch introduces functionality for following time64 syscalls: >> >> *utimensat_time64() >> >> int utimensat(int dirfd, const char *pathname, >> const struct timespec times[2], int flags); >> -- change file timestamps with nanosecond precision -- >> man page: https://man7.org/linux/man-pages/man2/utimensat.2.html >> >> *semtimedop_time64() >> >> int semtimedop(int semid, struct sembuf *sops, size_t nsops, >> const struct timespec *timeout); >> -- System V semaphore operations -- >> man page: https://www.man7.org/linux/man-pages/man2/semtimedop.2.html >> >> Implementation notes: >> >> Syscall 'utimensat_time64()' is implemented in similar way as its >> regular variants only difference being that time64 converting function >> is used to convert values of 'struct timespec' between host and target >> ('target_to_host_timespec64()'). >> >> For syscall 'semtimedop_time64()' and additional argument is added >> in function 'do_semtimedop()' through which the aproppriate 'struct timespec' >> converting function is called (false for regular target_to_host_timespec() >> and true for target_to_host_timespec64()). For 'do_ipc()' a >> check was added as that additional argument: 'TARGET_ABI_BITS == 64'. >> >> Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> >> Reviewed-by: Laurent Vivier <laurent@vivier.eu> >> --- >> linux-user/syscall.c | 60 ++++++++++++++++++++++++++++++++++++-------- >> 1 file changed, 50 insertions(+), 10 deletions(-) >> >> diff --git a/linux-user/syscall.c b/linux-user/syscall.c >> index fc6a6e32e4..4d460af744 100644 >> --- a/linux-user/syscall.c >> +++ b/linux-user/syscall.c >> @@ -1253,7 +1253,8 @@ static inline abi_long target_to_host_timespec(struct timespec *host_ts, >> #endif >> >> #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \ >> - defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) >> + defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) || \ >> + defined(TARGET_NR_utimensat_time64) || defined(TARGET_NR_semtimedop_time64) >> static inline abi_long target_to_host_timespec64(struct timespec *host_ts, >> abi_ulong target_addr) >> { >> @@ -4117,7 +4118,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, >> } >> >> #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ >> - defined(TARGET_NR_semtimedop) >> + defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64) >> >> /* >> * This macro is required to handle the s390 variants, which passes the >> @@ -4134,7 +4135,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, >> static inline abi_long do_semtimedop(int semid, >> abi_long ptr, >> unsigned nsops, >> - abi_long timeout) >> + abi_long timeout, bool time64) >> { >> struct sembuf sops[nsops]; >> struct timespec ts, *pts = NULL; >> @@ -4142,8 +4143,14 @@ static inline abi_long do_semtimedop(int semid, >> >> if (timeout) { >> pts = &ts; >> - if (target_to_host_timespec(pts, timeout)) { >> - return -TARGET_EFAULT; >> + if (time64) { >> + if (target_to_host_timespec64(pts, timeout)) { >> + return -TARGET_EFAULT; >> + } >> + } else { >> + if (target_to_host_timespec(pts, timeout)) { >> + return -TARGET_EFAULT; >> + } >> } >> } >> >> @@ -4657,7 +4664,7 @@ static abi_long do_ipc(CPUArchState *cpu_env, >> >> switch (call) { >> case IPCOP_semop: >> - ret = do_semtimedop(first, ptr, second, 0); >> + ret = do_semtimedop(first, ptr, second, 0, false); >> break; >> case IPCOP_semtimedop: >> /* >> @@ -4667,9 +4674,9 @@ static abi_long do_ipc(CPUArchState *cpu_env, >> * to a struct timespec where the generic variant uses fifth parameter. >> */ >> #if defined(TARGET_S390X) >> - ret = do_semtimedop(first, ptr, second, third); >> + ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64); >> #else >> - ret = do_semtimedop(first, ptr, second, fifth); >> + ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64); >> #endif >> break; >> >> @@ -9887,11 +9894,15 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >> #endif >> #ifdef TARGET_NR_semop >> case TARGET_NR_semop: >> - return do_semtimedop(arg1, arg2, arg3, 0); >> + return do_semtimedop(arg1, arg2, arg3, 0, false); >> #endif >> #ifdef TARGET_NR_semtimedop >> case TARGET_NR_semtimedop: >> - return do_semtimedop(arg1, arg2, arg3, arg4); >> + return do_semtimedop(arg1, arg2, arg3, arg4, false); >> +#endif >> +#ifdef TARGET_NR_semtimedop_time64 >> + case TARGET_NR_semtimedop_time64: >> + return do_semtimedop(arg1, arg2, arg3, arg4, true); >> #endif >> #ifdef TARGET_NR_semctl >> case TARGET_NR_semctl: >> @@ -11938,6 +11949,35 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >> } >> return ret; >> #endif >> +#ifdef TARGET_NR_utimensat_time64 >> + case TARGET_NR_utimensat_time64: >> + { >> + struct timespec *tsp, ts[2]; >> + if (!arg3) { >> + tsp = NULL; >> + } else { >> + if (target_to_host_timespec64(ts, arg3)) { >> + return -TARGET_EFAULT; >> + } >> + if (target_to_host_timespec64(ts + 1, arg3 + >> + sizeof(struct target__kernel_timespec))) { >> + return -TARGET_EFAULT; >> + } >> + tsp = ts; >> + } >> + if (!arg2) >> + ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); >> + else { >> + p = lock_user_string(arg2); >> + if (!p) { >> + return -TARGET_EFAULT; >> + } >> + ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); >> + unlock_user(p, arg2, 0); >> + } >> + } >> + return ret; >> +#endif >> #ifdef TARGET_NR_futex >> case TARGET_NR_futex: >> return do_futex(arg1, arg2, arg3, arg4, arg5, arg6); >> > > Applied to my linux-user-for-5.2 branch. I have removed it from my queue because the "TARGET_NR_utimensat_time64" part breaks something (at least with sh4/sid on x86_64): $ touch a $ cp -p a b /usr/bin/cp: preserving times for 'b': Invalid argument Thanks, Laurent ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() 2020-08-25 14:23 ` Laurent Vivier @ 2020-08-26 13:58 ` Laurent Vivier 2020-08-26 22:50 ` Laurent Vivier 0 siblings, 1 reply; 11+ messages in thread From: Laurent Vivier @ 2020-08-26 13:58 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 25/08/2020 à 16:23, Laurent Vivier a écrit : > Le 25/08/2020 à 09:18, Laurent Vivier a écrit : >> Le 25/08/2020 à 00:30, Filip Bozuta a écrit : >>> This patch introduces functionality for following time64 syscalls: >>> >>> *utimensat_time64() >>> >>> int utimensat(int dirfd, const char *pathname, >>> const struct timespec times[2], int flags); >>> -- change file timestamps with nanosecond precision -- >>> man page: https://man7.org/linux/man-pages/man2/utimensat.2.html >>> >>> *semtimedop_time64() >>> >>> int semtimedop(int semid, struct sembuf *sops, size_t nsops, >>> const struct timespec *timeout); >>> -- System V semaphore operations -- >>> man page: https://www.man7.org/linux/man-pages/man2/semtimedop.2.html >>> >>> Implementation notes: >>> >>> Syscall 'utimensat_time64()' is implemented in similar way as its >>> regular variants only difference being that time64 converting function >>> is used to convert values of 'struct timespec' between host and target >>> ('target_to_host_timespec64()'). >>> >>> For syscall 'semtimedop_time64()' and additional argument is added >>> in function 'do_semtimedop()' through which the aproppriate 'struct timespec' >>> converting function is called (false for regular target_to_host_timespec() >>> and true for target_to_host_timespec64()). For 'do_ipc()' a >>> check was added as that additional argument: 'TARGET_ABI_BITS == 64'. >>> >>> Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> >>> Reviewed-by: Laurent Vivier <laurent@vivier.eu> >>> --- >>> linux-user/syscall.c | 60 ++++++++++++++++++++++++++++++++++++-------- >>> 1 file changed, 50 insertions(+), 10 deletions(-) >>> >>> diff --git a/linux-user/syscall.c b/linux-user/syscall.c >>> index fc6a6e32e4..4d460af744 100644 >>> --- a/linux-user/syscall.c >>> +++ b/linux-user/syscall.c >>> @@ -1253,7 +1253,8 @@ static inline abi_long target_to_host_timespec(struct timespec *host_ts, >>> #endif >>> >>> #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \ >>> - defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) >>> + defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) || \ >>> + defined(TARGET_NR_utimensat_time64) || defined(TARGET_NR_semtimedop_time64) >>> static inline abi_long target_to_host_timespec64(struct timespec *host_ts, >>> abi_ulong target_addr) >>> { >>> @@ -4117,7 +4118,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, >>> } >>> >>> #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ >>> - defined(TARGET_NR_semtimedop) >>> + defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64) >>> >>> /* >>> * This macro is required to handle the s390 variants, which passes the >>> @@ -4134,7 +4135,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, >>> static inline abi_long do_semtimedop(int semid, >>> abi_long ptr, >>> unsigned nsops, >>> - abi_long timeout) >>> + abi_long timeout, bool time64) >>> { >>> struct sembuf sops[nsops]; >>> struct timespec ts, *pts = NULL; >>> @@ -4142,8 +4143,14 @@ static inline abi_long do_semtimedop(int semid, >>> >>> if (timeout) { >>> pts = &ts; >>> - if (target_to_host_timespec(pts, timeout)) { >>> - return -TARGET_EFAULT; >>> + if (time64) { >>> + if (target_to_host_timespec64(pts, timeout)) { >>> + return -TARGET_EFAULT; >>> + } >>> + } else { >>> + if (target_to_host_timespec(pts, timeout)) { >>> + return -TARGET_EFAULT; >>> + } >>> } >>> } >>> >>> @@ -4657,7 +4664,7 @@ static abi_long do_ipc(CPUArchState *cpu_env, >>> >>> switch (call) { >>> case IPCOP_semop: >>> - ret = do_semtimedop(first, ptr, second, 0); >>> + ret = do_semtimedop(first, ptr, second, 0, false); >>> break; >>> case IPCOP_semtimedop: >>> /* >>> @@ -4667,9 +4674,9 @@ static abi_long do_ipc(CPUArchState *cpu_env, >>> * to a struct timespec where the generic variant uses fifth parameter. >>> */ >>> #if defined(TARGET_S390X) >>> - ret = do_semtimedop(first, ptr, second, third); >>> + ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64); >>> #else >>> - ret = do_semtimedop(first, ptr, second, fifth); >>> + ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64); >>> #endif >>> break; >>> >>> @@ -9887,11 +9894,15 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >>> #endif >>> #ifdef TARGET_NR_semop >>> case TARGET_NR_semop: >>> - return do_semtimedop(arg1, arg2, arg3, 0); >>> + return do_semtimedop(arg1, arg2, arg3, 0, false); >>> #endif >>> #ifdef TARGET_NR_semtimedop >>> case TARGET_NR_semtimedop: >>> - return do_semtimedop(arg1, arg2, arg3, arg4); >>> + return do_semtimedop(arg1, arg2, arg3, arg4, false); >>> +#endif >>> +#ifdef TARGET_NR_semtimedop_time64 >>> + case TARGET_NR_semtimedop_time64: >>> + return do_semtimedop(arg1, arg2, arg3, arg4, true); >>> #endif >>> #ifdef TARGET_NR_semctl >>> case TARGET_NR_semctl: >>> @@ -11938,6 +11949,35 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >>> } >>> return ret; >>> #endif >>> +#ifdef TARGET_NR_utimensat_time64 >>> + case TARGET_NR_utimensat_time64: >>> + { >>> + struct timespec *tsp, ts[2]; >>> + if (!arg3) { >>> + tsp = NULL; >>> + } else { >>> + if (target_to_host_timespec64(ts, arg3)) { >>> + return -TARGET_EFAULT; >>> + } >>> + if (target_to_host_timespec64(ts + 1, arg3 + >>> + sizeof(struct target__kernel_timespec))) { >>> + return -TARGET_EFAULT; >>> + } >>> + tsp = ts; >>> + } >>> + if (!arg2) >>> + ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); >>> + else { >>> + p = lock_user_string(arg2); >>> + if (!p) { >>> + return -TARGET_EFAULT; >>> + } >>> + ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); >>> + unlock_user(p, arg2, 0); >>> + } >>> + } >>> + return ret; >>> +#endif >>> #ifdef TARGET_NR_futex >>> case TARGET_NR_futex: >>> return do_futex(arg1, arg2, arg3, arg4, arg5, arg6); >>> >> >> Applied to my linux-user-for-5.2 branch. > > I have removed it from my queue because the "TARGET_NR_utimensat_time64" > part breaks something (at least with sh4/sid on x86_64): > > $ touch a > $ cp -p a b > /usr/bin/cp: preserving times for 'b': Invalid argument In fact, on sh4, he timespec64 conversion is broken for all syscalls. tv_sec is ok but tv_nsec is totally corrupted (as it would be only a 32bit value). Thanks, Laurent ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() 2020-08-26 13:58 ` Laurent Vivier @ 2020-08-26 22:50 ` Laurent Vivier 0 siblings, 0 replies; 11+ messages in thread From: Laurent Vivier @ 2020-08-26 22:50 UTC (permalink / raw) To: Filip Bozuta, qemu-devel Le 26/08/2020 à 15:58, Laurent Vivier a écrit : > Le 25/08/2020 à 16:23, Laurent Vivier a écrit : >> Le 25/08/2020 à 09:18, Laurent Vivier a écrit : >>> Le 25/08/2020 à 00:30, Filip Bozuta a écrit : >>>> This patch introduces functionality for following time64 syscalls: >>>> >>>> *utimensat_time64() >>>> >>>> int utimensat(int dirfd, const char *pathname, >>>> const struct timespec times[2], int flags); >>>> -- change file timestamps with nanosecond precision -- >>>> man page: https://man7.org/linux/man-pages/man2/utimensat.2.html >>>> >>>> *semtimedop_time64() >>>> >>>> int semtimedop(int semid, struct sembuf *sops, size_t nsops, >>>> const struct timespec *timeout); >>>> -- System V semaphore operations -- >>>> man page: https://www.man7.org/linux/man-pages/man2/semtimedop.2.html >>>> >>>> Implementation notes: >>>> >>>> Syscall 'utimensat_time64()' is implemented in similar way as its >>>> regular variants only difference being that time64 converting function >>>> is used to convert values of 'struct timespec' between host and target >>>> ('target_to_host_timespec64()'). >>>> >>>> For syscall 'semtimedop_time64()' and additional argument is added >>>> in function 'do_semtimedop()' through which the aproppriate 'struct timespec' >>>> converting function is called (false for regular target_to_host_timespec() >>>> and true for target_to_host_timespec64()). For 'do_ipc()' a >>>> check was added as that additional argument: 'TARGET_ABI_BITS == 64'. >>>> >>>> Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com> >>>> Reviewed-by: Laurent Vivier <laurent@vivier.eu> >>>> --- >>>> linux-user/syscall.c | 60 ++++++++++++++++++++++++++++++++++++-------- >>>> 1 file changed, 50 insertions(+), 10 deletions(-) >>>> >>>> diff --git a/linux-user/syscall.c b/linux-user/syscall.c >>>> index fc6a6e32e4..4d460af744 100644 >>>> --- a/linux-user/syscall.c >>>> +++ b/linux-user/syscall.c >>>> @@ -1253,7 +1253,8 @@ static inline abi_long target_to_host_timespec(struct timespec *host_ts, >>>> #endif >>>> >>>> #if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \ >>>> - defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) >>>> + defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64) || \ >>>> + defined(TARGET_NR_utimensat_time64) || defined(TARGET_NR_semtimedop_time64) >>>> static inline abi_long target_to_host_timespec64(struct timespec *host_ts, >>>> abi_ulong target_addr) >>>> { >>>> @@ -4117,7 +4118,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, >>>> } >>>> >>>> #if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \ >>>> - defined(TARGET_NR_semtimedop) >>>> + defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64) >>>> >>>> /* >>>> * This macro is required to handle the s390 variants, which passes the >>>> @@ -4134,7 +4135,7 @@ static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf, >>>> static inline abi_long do_semtimedop(int semid, >>>> abi_long ptr, >>>> unsigned nsops, >>>> - abi_long timeout) >>>> + abi_long timeout, bool time64) >>>> { >>>> struct sembuf sops[nsops]; >>>> struct timespec ts, *pts = NULL; >>>> @@ -4142,8 +4143,14 @@ static inline abi_long do_semtimedop(int semid, >>>> >>>> if (timeout) { >>>> pts = &ts; >>>> - if (target_to_host_timespec(pts, timeout)) { >>>> - return -TARGET_EFAULT; >>>> + if (time64) { >>>> + if (target_to_host_timespec64(pts, timeout)) { >>>> + return -TARGET_EFAULT; >>>> + } >>>> + } else { >>>> + if (target_to_host_timespec(pts, timeout)) { >>>> + return -TARGET_EFAULT; >>>> + } >>>> } >>>> } >>>> >>>> @@ -4657,7 +4664,7 @@ static abi_long do_ipc(CPUArchState *cpu_env, >>>> >>>> switch (call) { >>>> case IPCOP_semop: >>>> - ret = do_semtimedop(first, ptr, second, 0); >>>> + ret = do_semtimedop(first, ptr, second, 0, false); >>>> break; >>>> case IPCOP_semtimedop: >>>> /* >>>> @@ -4667,9 +4674,9 @@ static abi_long do_ipc(CPUArchState *cpu_env, >>>> * to a struct timespec where the generic variant uses fifth parameter. >>>> */ >>>> #if defined(TARGET_S390X) >>>> - ret = do_semtimedop(first, ptr, second, third); >>>> + ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64); >>>> #else >>>> - ret = do_semtimedop(first, ptr, second, fifth); >>>> + ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64); >>>> #endif >>>> break; >>>> >>>> @@ -9887,11 +9894,15 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >>>> #endif >>>> #ifdef TARGET_NR_semop >>>> case TARGET_NR_semop: >>>> - return do_semtimedop(arg1, arg2, arg3, 0); >>>> + return do_semtimedop(arg1, arg2, arg3, 0, false); >>>> #endif >>>> #ifdef TARGET_NR_semtimedop >>>> case TARGET_NR_semtimedop: >>>> - return do_semtimedop(arg1, arg2, arg3, arg4); >>>> + return do_semtimedop(arg1, arg2, arg3, arg4, false); >>>> +#endif >>>> +#ifdef TARGET_NR_semtimedop_time64 >>>> + case TARGET_NR_semtimedop_time64: >>>> + return do_semtimedop(arg1, arg2, arg3, arg4, true); >>>> #endif >>>> #ifdef TARGET_NR_semctl >>>> case TARGET_NR_semctl: >>>> @@ -11938,6 +11949,35 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, >>>> } >>>> return ret; >>>> #endif >>>> +#ifdef TARGET_NR_utimensat_time64 >>>> + case TARGET_NR_utimensat_time64: >>>> + { >>>> + struct timespec *tsp, ts[2]; >>>> + if (!arg3) { >>>> + tsp = NULL; >>>> + } else { >>>> + if (target_to_host_timespec64(ts, arg3)) { >>>> + return -TARGET_EFAULT; >>>> + } >>>> + if (target_to_host_timespec64(ts + 1, arg3 + >>>> + sizeof(struct target__kernel_timespec))) { >>>> + return -TARGET_EFAULT; >>>> + } >>>> + tsp = ts; >>>> + } >>>> + if (!arg2) >>>> + ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4)); >>>> + else { >>>> + p = lock_user_string(arg2); >>>> + if (!p) { >>>> + return -TARGET_EFAULT; >>>> + } >>>> + ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4)); >>>> + unlock_user(p, arg2, 0); >>>> + } >>>> + } >>>> + return ret; >>>> +#endif >>>> #ifdef TARGET_NR_futex >>>> case TARGET_NR_futex: >>>> return do_futex(arg1, arg2, arg3, arg4, arg5, arg6); >>>> >>> >>> Applied to my linux-user-for-5.2 branch. >> >> I have removed it from my queue because the "TARGET_NR_utimensat_time64" >> part breaks something (at least with sh4/sid on x86_64): >> >> $ touch a >> $ cp -p a b >> /usr/bin/cp: preserving times for 'b': Invalid argument > > In fact, on sh4, he timespec64 conversion is broken for all syscalls. > tv_sec is ok but tv_nsec is totally corrupted (as it would be only a > 32bit value). In kernel, in get_timespec64(), we have: /* In 32-bit mode, this drops the padding */ ts->tv_nsec = kts.tv_nsec; and ts is "struct timespec64" (kts is __kernel_timespec): struct timespec64 { time64_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; So I think we need something like: diff --git a/linux-user/syscall.c b/linux-user/syscall.c index c82b73e03234..5f53d5d65eb6 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -1253,6 +1253,8 @@ static inline abi_long target_to_host_timespec64(struct timespec *host_ts, } __get_user(host_ts->tv_sec, &target_ts->tv_sec); __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); + /* in 32bit mode this drops the padding */ + host_ts->tv_nsec = (abi_long)host_ts->tv_nsec; unlock_user_struct(target_ts, target_addr, 0); return 0; } Thanks, Laurent ^ permalink raw reply related [flat|nested] 11+ messages in thread
end of thread, other threads:[~2020-09-05 20:42 UTC | newest] Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-08-24 22:30 [PATCH v2 0/2] linux-user: Adding support for a group of 4 time64 syscalls Filip Bozuta 2020-08-24 22:30 ` [PATCH v2 1/2] linux-user: Add support for ppoll_time64() and pselect6_time64() Filip Bozuta 2020-08-25 7:00 ` Laurent Vivier 2020-08-25 7:17 ` Laurent Vivier 2020-08-25 15:23 ` Laurent Vivier 2020-09-05 20:41 ` Laurent Vivier 2020-08-24 22:30 ` [PATCH v2 2/2] linux-user: Add support for utimensat_time64() and semtimedop_time64() Filip Bozuta 2020-08-25 7:18 ` Laurent Vivier 2020-08-25 14:23 ` Laurent Vivier 2020-08-26 13:58 ` Laurent Vivier 2020-08-26 22:50 ` Laurent Vivier
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).