diff mbox series

[Xen-devel,v2,03/16] xen/arm: Extend copy_to_guest to support copying from guest VA and use it

Message ID 20171212190212.5535-4-julien.grall@linaro.org
State Accepted
Commit b324c511906694ee27f7f06bfe99f343e8509f64
Headers show
Series xen/arm: Stage-2 handling cleanup | expand

Commit Message

Julien Grall Dec. 12, 2017, 7:01 p.m. UTC
The only differences between copy_to_guest (formerly called
raw_copy_to_guest_helper) and raw_copy_from_guest is:
    - The direction of the memcpy
    - The permission use for translating the address

Extend copy_to_guest to support copying from guest VA by adding using a
bit in the flags to tell the direction of the copy.

Lastly, reimplement raw_copy_from_guest using copy_to_guest.

Signed-off-by: Julien Grall <julien.grall@linaro.org>

---
    Changes in v2:
        - Use vaddr_t
---
 xen/arch/arm/guestcopy.c | 46 +++++++++++++---------------------------------
 1 file changed, 13 insertions(+), 33 deletions(-)

Comments

Stefano Stabellini Dec. 12, 2017, 7:54 p.m. UTC | #1
On Tue, 12 Dec 2017, Julien Grall wrote:
> The only differences between copy_to_guest (formerly called
> raw_copy_to_guest_helper) and raw_copy_from_guest is:
>     - The direction of the memcpy
>     - The permission use for translating the address
> 
> Extend copy_to_guest to support copying from guest VA by adding using a
> bit in the flags to tell the direction of the copy.
> 
> Lastly, reimplement raw_copy_from_guest using copy_to_guest.
> 
> Signed-off-by: Julien Grall <julien.grall@linaro.org>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>


> ---
>     Changes in v2:
>         - Use vaddr_t
> ---
>  xen/arch/arm/guestcopy.c | 46 +++++++++++++---------------------------------
>  1 file changed, 13 insertions(+), 33 deletions(-)
> 
> diff --git a/xen/arch/arm/guestcopy.c b/xen/arch/arm/guestcopy.c
> index 08d0fa0a83..12fb03dd19 100644
> --- a/xen/arch/arm/guestcopy.c
> +++ b/xen/arch/arm/guestcopy.c
> @@ -6,6 +6,8 @@
>  #include <asm/guest_access.h>
>  
>  #define COPY_flush_dcache   (1U << 0)
> +#define COPY_from_guest     (0U << 1)
> +#define COPY_to_guest       (1U << 1)
>  
>  static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
>                                  unsigned int flags)
> @@ -21,13 +23,18 @@ static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
>          unsigned size = min(len, (unsigned)PAGE_SIZE - offset);
>          struct page_info *page;
>  
> -        page = get_page_from_gva(current, addr, GV2M_WRITE);
> +        page = get_page_from_gva(current, addr,
> +                                 (flags & COPY_to_guest) ? GV2M_WRITE : GV2M_READ);
>          if ( page == NULL )
>              return len;
>  
>          p = __map_domain_page(page);
>          p += offset;
> -        memcpy(p, buf, size);
> +        if ( flags & COPY_to_guest )
> +            memcpy(p, buf, size);
> +        else
> +            memcpy(buf, p, size);
> +
>          if ( flags & COPY_flush_dcache )
>              clean_dcache_va_range(p, size);
>  
> @@ -48,13 +55,14 @@ static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
>  
>  unsigned long raw_copy_to_guest(void *to, const void *from, unsigned len)
>  {
> -    return copy_guest((void *)from, (vaddr_t)to, len, 0);
> +    return copy_guest((void *)from, (vaddr_t)to, len, COPY_to_guest);
>  }
>  
>  unsigned long raw_copy_to_guest_flush_dcache(void *to, const void *from,
>                                               unsigned len)
>  {
> -    return copy_guest((void *)from, (vaddr_t)to, len, COPY_flush_dcache);
> +    return copy_guest((void *)from, (vaddr_t)to, len,
> +                      COPY_to_guest | COPY_flush_dcache);
>  }
>  
>  unsigned long raw_clear_guest(void *to, unsigned len)
> @@ -92,35 +100,7 @@ unsigned long raw_clear_guest(void *to, unsigned len)
>  
>  unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned len)
>  {
> -    unsigned offset = (vaddr_t)from & ~PAGE_MASK;
> -
> -    while ( len )
> -    {
> -        void *p;
> -        unsigned size = min(len, (unsigned)(PAGE_SIZE - offset));
> -        struct page_info *page;
> -
> -        page = get_page_from_gva(current, (vaddr_t) from, GV2M_READ);
> -        if ( page == NULL )
> -            return len;
> -
> -        p = __map_domain_page(page);
> -        p += ((vaddr_t)from & (~PAGE_MASK));
> -
> -        memcpy(to, p, size);
> -
> -        unmap_domain_page(p);
> -        put_page(page);
> -        len -= size;
> -        from += size;
> -        to += size;
> -        /*
> -         * After the first iteration, guest virtual address is correctly
> -         * aligned to PAGE_SIZE.
> -         */
> -        offset = 0;
> -    }
> -    return 0;
> +    return copy_guest(to, (vaddr_t)from, len, COPY_from_guest);
>  }
>  
>  /*
> -- 
> 2.11.0
>
diff mbox series

Patch

diff --git a/xen/arch/arm/guestcopy.c b/xen/arch/arm/guestcopy.c
index 08d0fa0a83..12fb03dd19 100644
--- a/xen/arch/arm/guestcopy.c
+++ b/xen/arch/arm/guestcopy.c
@@ -6,6 +6,8 @@ 
 #include <asm/guest_access.h>
 
 #define COPY_flush_dcache   (1U << 0)
+#define COPY_from_guest     (0U << 1)
+#define COPY_to_guest       (1U << 1)
 
 static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
                                 unsigned int flags)
@@ -21,13 +23,18 @@  static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
         unsigned size = min(len, (unsigned)PAGE_SIZE - offset);
         struct page_info *page;
 
-        page = get_page_from_gva(current, addr, GV2M_WRITE);
+        page = get_page_from_gva(current, addr,
+                                 (flags & COPY_to_guest) ? GV2M_WRITE : GV2M_READ);
         if ( page == NULL )
             return len;
 
         p = __map_domain_page(page);
         p += offset;
-        memcpy(p, buf, size);
+        if ( flags & COPY_to_guest )
+            memcpy(p, buf, size);
+        else
+            memcpy(buf, p, size);
+
         if ( flags & COPY_flush_dcache )
             clean_dcache_va_range(p, size);
 
@@ -48,13 +55,14 @@  static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
 
 unsigned long raw_copy_to_guest(void *to, const void *from, unsigned len)
 {
-    return copy_guest((void *)from, (vaddr_t)to, len, 0);
+    return copy_guest((void *)from, (vaddr_t)to, len, COPY_to_guest);
 }
 
 unsigned long raw_copy_to_guest_flush_dcache(void *to, const void *from,
                                              unsigned len)
 {
-    return copy_guest((void *)from, (vaddr_t)to, len, COPY_flush_dcache);
+    return copy_guest((void *)from, (vaddr_t)to, len,
+                      COPY_to_guest | COPY_flush_dcache);
 }
 
 unsigned long raw_clear_guest(void *to, unsigned len)
@@ -92,35 +100,7 @@  unsigned long raw_clear_guest(void *to, unsigned len)
 
 unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned len)
 {
-    unsigned offset = (vaddr_t)from & ~PAGE_MASK;
-
-    while ( len )
-    {
-        void *p;
-        unsigned size = min(len, (unsigned)(PAGE_SIZE - offset));
-        struct page_info *page;
-
-        page = get_page_from_gva(current, (vaddr_t) from, GV2M_READ);
-        if ( page == NULL )
-            return len;
-
-        p = __map_domain_page(page);
-        p += ((vaddr_t)from & (~PAGE_MASK));
-
-        memcpy(to, p, size);
-
-        unmap_domain_page(p);
-        put_page(page);
-        len -= size;
-        from += size;
-        to += size;
-        /*
-         * After the first iteration, guest virtual address is correctly
-         * aligned to PAGE_SIZE.
-         */
-        offset = 0;
-    }
-    return 0;
+    return copy_guest(to, (vaddr_t)from, len, COPY_from_guest);
 }
 
 /*