diff mbox series

[v2,1/2] linux-user: Modify 'target_to_host/host_to_target_itimerspec()'

Message ID 20200722153421.295411-2-Filip.Bozuta@syrmia.com
State New
Headers show
Series This two patch series introduces functionality for a group | expand

Commit Message

Filip Bozuta July 22, 2020, 3:34 p.m. UTC
Functions 'target_to_host_itimerspec()' and 'host_to_target_itimerspec()'
are used to convert values of type 'struct itimerspec' between target and
host. This type has 'struct timespec' as its fields. That is the reason
why this patch introduces a little modification to the converting functions
to be implemented using already existing functions that convert 'struct timespec':
'target_to_host_timespec()' and 'host_to_target_timespec()'. This makes the
code of 'target_to_host_itimerspec()' and 'host_to_target_itimerspec()' more
clean and readable.

Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com>
---
 linux-user/syscall.c | 46 ++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 27 deletions(-)

Comments

Laurent Vivier Aug. 7, 2020, 9:56 a.m. UTC | #1
Le 22/07/2020 à 17:34, Filip Bozuta a écrit :
> Functions 'target_to_host_itimerspec()' and 'host_to_target_itimerspec()'
> are used to convert values of type 'struct itimerspec' between target and
> host. This type has 'struct timespec' as its fields. That is the reason
> why this patch introduces a little modification to the converting functions
> to be implemented using already existing functions that convert 'struct timespec':
> 'target_to_host_timespec()' and 'host_to_target_timespec()'. This makes the
> code of 'target_to_host_itimerspec()' and 'host_to_target_itimerspec()' more
> clean and readable.
> 
> Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com>
> ---
>  linux-user/syscall.c | 46 ++++++++++++++++++--------------------------
>  1 file changed, 19 insertions(+), 27 deletions(-)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 1211e759c2..b1baed346c 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -1236,7 +1236,9 @@ static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
>      defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
>      defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
>      defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
> -    defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop)
> +    defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
> +    defined(TARGET_NR_timer_settime) || \
> +    (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
>  static inline abi_long target_to_host_timespec(struct timespec *host_ts,
>                                                 abi_ulong target_addr)
>  {
> @@ -6790,46 +6792,36 @@ static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
>  
>  #if defined(TARGET_NR_timer_settime) || \
>      (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
> -static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
> +static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
>                                                   abi_ulong target_addr)
>  {
> -    struct target_itimerspec *target_itspec;
> -
> -    if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
> +    if (target_to_host_timespec(&host_its->it_interval, target_addr +
> +                                offsetof(struct target_itimerspec,
> +                                         it_interval)) ||
> +        target_to_host_timespec(&host_its->it_value, target_addr +
> +                                offsetof(struct target_itimerspec,
> +                                         it_value))) {
>          return -TARGET_EFAULT;
>      }
>  
> -    host_itspec->it_interval.tv_sec =
> -                            tswapal(target_itspec->it_interval.tv_sec);
> -    host_itspec->it_interval.tv_nsec =
> -                            tswapal(target_itspec->it_interval.tv_nsec);
> -    host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
> -    host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
> -
> -    unlock_user_struct(target_itspec, target_addr, 1);
>      return 0;
>  }
>  #endif
>  
>  #if ((defined(TARGET_NR_timerfd_gettime) || \
>        defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
> -    defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
> +      defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
>  static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
> -                                               struct itimerspec *host_its)
> -{
> -    struct target_itimerspec *target_itspec;
> -
> -    if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
> +                                                 struct itimerspec *host_its)
> +{
> +    if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
> +                                                       it_interval),
> +                                &host_its->it_interval) ||
> +        host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
> +                                                       it_value),
> +                                &host_its->it_value)) {
>          return -TARGET_EFAULT;
>      }
> -
> -    target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
> -    target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
> -
> -    target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
> -    target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
> -
> -    unlock_user_struct(target_itspec, target_addr, 0);
>      return 0;
>  }
>  #endif
> 

Reviewed-by: Laurent Vivier <laurent@vivier.eu>
Laurent Vivier Aug. 7, 2020, 11:16 a.m. UTC | #2
Le 22/07/2020 à 17:34, Filip Bozuta a écrit :
> Functions 'target_to_host_itimerspec()' and 'host_to_target_itimerspec()'
> are used to convert values of type 'struct itimerspec' between target and
> host. This type has 'struct timespec' as its fields. That is the reason
> why this patch introduces a little modification to the converting functions
> to be implemented using already existing functions that convert 'struct timespec':
> 'target_to_host_timespec()' and 'host_to_target_timespec()'. This makes the
> code of 'target_to_host_itimerspec()' and 'host_to_target_itimerspec()' more
> clean and readable.
> 
> Signed-off-by: Filip Bozuta <Filip.Bozuta@syrmia.com>
> ---
>  linux-user/syscall.c | 46 ++++++++++++++++++--------------------------
>  1 file changed, 19 insertions(+), 27 deletions(-)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 1211e759c2..b1baed346c 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -1236,7 +1236,9 @@ static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
>      defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
>      defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
>      defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
> -    defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop)
> +    defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
> +    defined(TARGET_NR_timer_settime) || \
> +    (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
>  static inline abi_long target_to_host_timespec(struct timespec *host_ts,
>                                                 abi_ulong target_addr)
>  {
> @@ -6790,46 +6792,36 @@ static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
>  
>  #if defined(TARGET_NR_timer_settime) || \
>      (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
> -static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
> +static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
>                                                   abi_ulong target_addr)
>  {
> -    struct target_itimerspec *target_itspec;
> -
> -    if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
> +    if (target_to_host_timespec(&host_its->it_interval, target_addr +
> +                                offsetof(struct target_itimerspec,
> +                                         it_interval)) ||
> +        target_to_host_timespec(&host_its->it_value, target_addr +
> +                                offsetof(struct target_itimerspec,
> +                                         it_value))) {
>          return -TARGET_EFAULT;
>      }
>  
> -    host_itspec->it_interval.tv_sec =
> -                            tswapal(target_itspec->it_interval.tv_sec);
> -    host_itspec->it_interval.tv_nsec =
> -                            tswapal(target_itspec->it_interval.tv_nsec);
> -    host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
> -    host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
> -
> -    unlock_user_struct(target_itspec, target_addr, 1);
>      return 0;
>  }
>  #endif
>  
>  #if ((defined(TARGET_NR_timerfd_gettime) || \
>        defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
> -    defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
> +      defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
>  static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
> -                                               struct itimerspec *host_its)
> -{
> -    struct target_itimerspec *target_itspec;
> -
> -    if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
> +                                                 struct itimerspec *host_its)
> +{
> +    if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
> +                                                       it_interval),
> +                                &host_its->it_interval) ||
> +        host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
> +                                                       it_value),
> +                                &host_its->it_value)) {
>          return -TARGET_EFAULT;
>      }
> -
> -    target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
> -    target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
> -
> -    target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
> -    target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
> -
> -    unlock_user_struct(target_itspec, target_addr, 0);
>      return 0;
>  }
>  #endif
> 

Applied to my linux-user-for-5.2 branch.

Thanks,
Laurent
diff mbox series

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 1211e759c2..b1baed346c 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -1236,7 +1236,9 @@  static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
     defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
     defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
     defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
-    defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop)
+    defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
+    defined(TARGET_NR_timer_settime) || \
+    (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
                                                abi_ulong target_addr)
 {
@@ -6790,46 +6792,36 @@  static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
 
 #if defined(TARGET_NR_timer_settime) || \
     (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
-static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
+static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
                                                  abi_ulong target_addr)
 {
-    struct target_itimerspec *target_itspec;
-
-    if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
+    if (target_to_host_timespec(&host_its->it_interval, target_addr +
+                                offsetof(struct target_itimerspec,
+                                         it_interval)) ||
+        target_to_host_timespec(&host_its->it_value, target_addr +
+                                offsetof(struct target_itimerspec,
+                                         it_value))) {
         return -TARGET_EFAULT;
     }
 
-    host_itspec->it_interval.tv_sec =
-                            tswapal(target_itspec->it_interval.tv_sec);
-    host_itspec->it_interval.tv_nsec =
-                            tswapal(target_itspec->it_interval.tv_nsec);
-    host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
-    host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
-
-    unlock_user_struct(target_itspec, target_addr, 1);
     return 0;
 }
 #endif
 
 #if ((defined(TARGET_NR_timerfd_gettime) || \
       defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
-    defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
+      defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
 static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
-                                               struct itimerspec *host_its)
-{
-    struct target_itimerspec *target_itspec;
-
-    if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
+                                                 struct itimerspec *host_its)
+{
+    if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
+                                                       it_interval),
+                                &host_its->it_interval) ||
+        host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
+                                                       it_value),
+                                &host_its->it_value)) {
         return -TARGET_EFAULT;
     }
-
-    target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
-    target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
-
-    target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
-    target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
-
-    unlock_user_struct(target_itspec, target_addr, 0);
     return 0;
 }
 #endif