diff mbox series

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

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

Commit Message

Julien Grall Dec. 12, 2017, 7:02 p.m. UTC
The function copy_to_guest can easily be extended to support zeroing
guest VA. To avoid using a new bit, it is considered that a NULL buffer
(i.e buf == NULL) means the guest memory will be zeroed.

Lastly, reimplement raw_clear_guest using copy_to_guest.

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

---
    Changes in v3:
        - Use vaddr_t
---
 xen/arch/arm/guestcopy.c | 41 +++++++++++------------------------------
 1 file changed, 11 insertions(+), 30 deletions(-)

Comments

Stefano Stabellini Dec. 12, 2017, 7:57 p.m. UTC | #1
On Tue, 12 Dec 2017, Julien Grall wrote:
> The function copy_to_guest can easily be extended to support zeroing
> guest VA. To avoid using a new bit, it is considered that a NULL buffer
> (i.e buf == NULL) means the guest memory will be zeroed.
> 
> Lastly, reimplement raw_clear_guest using copy_to_guest.
> 
> Signed-off-by: Julien Grall <julien.grall@linaro.org>

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


> ---
>     Changes in v3:
>         - Use vaddr_t
> ---
>  xen/arch/arm/guestcopy.c | 41 +++++++++++------------------------------
>  1 file changed, 11 insertions(+), 30 deletions(-)
> 
> diff --git a/xen/arch/arm/guestcopy.c b/xen/arch/arm/guestcopy.c
> index 12fb03dd19..ff7d15380f 100644
> --- a/xen/arch/arm/guestcopy.c
> +++ b/xen/arch/arm/guestcopy.c
> @@ -31,7 +31,16 @@ static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
>          p = __map_domain_page(page);
>          p += offset;
>          if ( flags & COPY_to_guest )
> -            memcpy(p, buf, size);
> +        {
> +            /*
> +             * buf will be NULL when the caller request to zero the
> +             * guest memory.
> +             */
> +            if ( buf )
> +                memcpy(p, buf, size);
> +            else
> +                memset(p, 0, size);
> +        }
>          else
>              memcpy(buf, p, size);
>  
> @@ -67,35 +76,7 @@ unsigned long raw_copy_to_guest_flush_dcache(void *to, const void *from,
>  
>  unsigned long raw_clear_guest(void *to, unsigned len)
>  {
> -    /* XXX needs to handle faults */
> -    unsigned offset = (vaddr_t)to & ~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) to, GV2M_WRITE);
> -        if ( page == NULL )
> -            return len;
> -
> -        p = __map_domain_page(page);
> -        p += offset;
> -        memset(p, 0x00, size);
> -
> -        unmap_domain_page(p - offset);
> -        put_page(page);
> -        len -= size;
> -        to += size;
> -        /*
> -         * After the first iteration, guest virtual address is correctly
> -         * aligned to PAGE_SIZE.
> -         */
> -        offset = 0;
> -    }
> -
> -    return 0;
> +    return copy_guest(NULL, (vaddr_t)to, len, COPY_to_guest);
>  }
>  
>  unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned len)
> -- 
> 2.11.0
>
diff mbox series

Patch

diff --git a/xen/arch/arm/guestcopy.c b/xen/arch/arm/guestcopy.c
index 12fb03dd19..ff7d15380f 100644
--- a/xen/arch/arm/guestcopy.c
+++ b/xen/arch/arm/guestcopy.c
@@ -31,7 +31,16 @@  static unsigned long copy_guest(void *buf, uint64_t addr, unsigned int len,
         p = __map_domain_page(page);
         p += offset;
         if ( flags & COPY_to_guest )
-            memcpy(p, buf, size);
+        {
+            /*
+             * buf will be NULL when the caller request to zero the
+             * guest memory.
+             */
+            if ( buf )
+                memcpy(p, buf, size);
+            else
+                memset(p, 0, size);
+        }
         else
             memcpy(buf, p, size);
 
@@ -67,35 +76,7 @@  unsigned long raw_copy_to_guest_flush_dcache(void *to, const void *from,
 
 unsigned long raw_clear_guest(void *to, unsigned len)
 {
-    /* XXX needs to handle faults */
-    unsigned offset = (vaddr_t)to & ~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) to, GV2M_WRITE);
-        if ( page == NULL )
-            return len;
-
-        p = __map_domain_page(page);
-        p += offset;
-        memset(p, 0x00, size);
-
-        unmap_domain_page(p - offset);
-        put_page(page);
-        len -= size;
-        to += size;
-        /*
-         * After the first iteration, guest virtual address is correctly
-         * aligned to PAGE_SIZE.
-         */
-        offset = 0;
-    }
-
-    return 0;
+    return copy_guest(NULL, (vaddr_t)to, len, COPY_to_guest);
 }
 
 unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned len)