[Qemu-devel] [PATCH v2] linux-user: fix preadv/pwritev offsets

Max Filippov posted 1 patch 6 years ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20180405014135.3235-1-jcmvbkbc@gmail.com
Test checkpatch passed
Test docker-build@min-glib passed
Test docker-mingw@fedora passed
Test s390x passed
There is a newer version of this series
linux-user/syscall.c | 27 +++++++++++++++++++++++++--
1 file changed, 25 insertions(+), 2 deletions(-)
[Qemu-devel] [PATCH v2] linux-user: fix preadv/pwritev offsets
Posted by Max Filippov 6 years ago
preadv/pwritev accept low and high parts of file offset in two separate
parameters. When host bitness doesn't match guest bitness these parts
must be appropriately recombined.
Introduce target_low_high_to_host_low_high that does this recombination
and use it in preadv/pwritev syscalls.

This fixes glibc testsuite test misc/tst-preadvwritev64.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
---
Changes v1->v2:
- fix host high computation in TARGET_LONG_BITS > HOST_LONG_BITS case

 linux-user/syscall.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 5ef517613577..7e014066260a 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -3386,6 +3386,23 @@ static abi_long do_getsockopt(int sockfd, int level, int optname,
     return ret;
 }
 
+static void target_low_high_to_host_low_high(abi_ulong tlow,
+                                             abi_ulong thigh,
+                                             unsigned long *hlow,
+                                             unsigned long *hhigh)
+{
+#if TARGET_LONG_BITS == HOST_LONG_BITS
+        *hlow = tlow;
+        *hhigh = thigh;
+#elif TARGET_LONG_BITS < HOST_LONG_BITS
+        *hlow = tlow | (unsigned long)thigh << TARGET_LONG_BITS;
+        *hhigh = 0;
+#else
+        *hlow = (unsigned long)tlow;
+        *hhigh = (unsigned long)(tlow >> HOST_LONG_BITS);
+#endif
+}
+
 static struct iovec *lock_iovec(int type, abi_ulong target_addr,
                                 abi_ulong count, int copy)
 {
@@ -10449,7 +10466,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         {
             struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
             if (vec != NULL) {
-                ret = get_errno(safe_preadv(arg1, vec, arg3, arg4, arg5));
+                unsigned long low, high;
+
+                target_low_high_to_host_low_high(arg4, arg5, &low, &high);
+                ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
                 unlock_iovec(vec, arg2, arg3, 1);
             } else {
                 ret = -host_to_target_errno(errno);
@@ -10462,7 +10482,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         {
             struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
             if (vec != NULL) {
-                ret = get_errno(safe_pwritev(arg1, vec, arg3, arg4, arg5));
+                unsigned long low, high;
+
+                target_low_high_to_host_low_high(arg4, arg5, &low, &high);
+                ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
                 unlock_iovec(vec, arg2, arg3, 0);
             } else {
                 ret = -host_to_target_errno(errno);
-- 
2.11.0


Re: [Qemu-devel] [PATCH v2] linux-user: fix preadv/pwritev offsets
Posted by Richard Henderson 6 years ago
On 04/05/2018 11:41 AM, Max Filippov wrote:
> +static void target_low_high_to_host_low_high(abi_ulong tlow,
> +                                             abi_ulong thigh,
> +                                             unsigned long *hlow,
> +                                             unsigned long *hhigh)
> +{
> +#if TARGET_LONG_BITS == HOST_LONG_BITS
> +        *hlow = tlow;
> +        *hhigh = thigh;
> +#elif TARGET_LONG_BITS < HOST_LONG_BITS
> +        *hlow = tlow | (unsigned long)thigh << TARGET_LONG_BITS;
> +        *hhigh = 0;
> +#else
> +        *hlow = (unsigned long)tlow;
> +        *hhigh = (unsigned long)(tlow >> HOST_LONG_BITS);
> +#endif

It might still be worth a check for HOST_LONG_BITS >= 2 * TARGET_LONG_BITS and
#error otherwise.  Or explicit checks vs 32 & 64 if you like.


r~

Re: [Qemu-devel] [PATCH v2] linux-user: fix preadv/pwritev offsets
Posted by Max Filippov 6 years ago
On Wed, Apr 4, 2018 at 8:43 PM, Richard Henderson
<richard.henderson@linaro.org> wrote:
> On 04/05/2018 11:41 AM, Max Filippov wrote:
>> +static void target_low_high_to_host_low_high(abi_ulong tlow,
>> +                                             abi_ulong thigh,
>> +                                             unsigned long *hlow,
>> +                                             unsigned long *hhigh)
>> +{
>> +#if TARGET_LONG_BITS == HOST_LONG_BITS
>> +        *hlow = tlow;
>> +        *hhigh = thigh;
>> +#elif TARGET_LONG_BITS < HOST_LONG_BITS
>> +        *hlow = tlow | (unsigned long)thigh << TARGET_LONG_BITS;
>> +        *hhigh = 0;
>> +#else
>> +        *hlow = (unsigned long)tlow;
>> +        *hhigh = (unsigned long)(tlow >> HOST_LONG_BITS);
>> +#endif
>
> It might still be worth a check for HOST_LONG_BITS >= 2 * TARGET_LONG_BITS and
> #error otherwise.  Or explicit checks vs 32 & 64 if you like.

Ok, will do in v3.

-- 
Thanks.
-- Max

Re: [Qemu-devel] [PATCH v2] linux-user: fix preadv/pwritev offsets
Posted by Laurent Vivier 6 years ago
Le 05/04/2018 à 03:41, Max Filippov a écrit :
> preadv/pwritev accept low and high parts of file offset in two separate
> parameters. When host bitness doesn't match guest bitness these parts
> must be appropriately recombined.
> Introduce target_low_high_to_host_low_high that does this recombination
> and use it in preadv/pwritev syscalls.
> 
> This fixes glibc testsuite test misc/tst-preadvwritev64.
> 
> Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
> ---
> Changes v1->v2:
> - fix host high computation in TARGET_LONG_BITS > HOST_LONG_BITS case
> 
>  linux-user/syscall.c | 27 +++++++++++++++++++++++++--
>  1 file changed, 25 insertions(+), 2 deletions(-)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 5ef517613577..7e014066260a 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -3386,6 +3386,23 @@ static abi_long do_getsockopt(int sockfd, int level, int optname,
>      return ret;
>  }
>  
> +static void target_low_high_to_host_low_high(abi_ulong tlow,
> +                                             abi_ulong thigh,
> +                                             unsigned long *hlow,
> +                                             unsigned long *hhigh)
> +{
> +#if TARGET_LONG_BITS == HOST_LONG_BITS
> +        *hlow = tlow;
> +        *hhigh = thigh;
> +#elif TARGET_LONG_BITS < HOST_LONG_BITS
> +        *hlow = tlow | (unsigned long)thigh << TARGET_LONG_BITS;
> +        *hhigh = 0;
> +#else
> +        *hlow = (unsigned long)tlow;
> +        *hhigh = (unsigned long)(tlow >> HOST_LONG_BITS);
> +#endif
> +}
> +
>  static struct iovec *lock_iovec(int type, abi_ulong target_addr,
>                                  abi_ulong count, int copy)
>  {
> @@ -10449,7 +10466,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
>          {
>              struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
>              if (vec != NULL) {
> -                ret = get_errno(safe_preadv(arg1, vec, arg3, arg4, arg5));
> +                unsigned long low, high;
> +
> +                target_low_high_to_host_low_high(arg4, arg5, &low, &high);
> +                ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
>                  unlock_iovec(vec, arg2, arg3, 1);
>              } else {
>                  ret = -host_to_target_errno(errno);
> @@ -10462,7 +10482,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
>          {
>              struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
>              if (vec != NULL) {
> -                ret = get_errno(safe_pwritev(arg1, vec, arg3, arg4, arg5));
> +                unsigned long low, high;
> +
> +                target_low_high_to_host_low_high(arg4, arg5, &low, &high);
> +                ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
>                  unlock_iovec(vec, arg2, arg3, 0);
>              } else {
>                  ret = -host_to_target_errno(errno);
> 

Did you try to use the regpairs_aligned() and target_offset64()
functions as it is done for pread64(), pwrite64(), fadvise64(),... ?

Thanks,
Laurent

Re: [Qemu-devel] [PATCH v2] linux-user: fix preadv/pwritev offsets
Posted by Max Filippov 6 years ago
On Thu, Apr 5, 2018 at 2:03 AM, Laurent Vivier <laurent@vivier.eu> wrote:
> Did you try to use the regpairs_aligned() and target_offset64()
> functions as it is done for pread64(), pwrite64(), fadvise64(),... ?

My understanding is that if the syscalls had single parameter that took
two registers then it'd be a place to use regpairs_aligned. By the syscalls
are defined as follows:

SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
                unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)

SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
                unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)

so these are normal unsigned long arguments, low part, then the high part.
target_offset64 treats arguments differently depending on the target
endianness, which is not needed for preadv/pwritev.

-- 
Thanks.
-- Max