Index: xen-4.4.4-testing/tools/libxc/xc_dom.h
===================================================================
--- xen-4.4.4-testing.orig/tools/libxc/xc_dom.h
+++ xen-4.4.4-testing/tools/libxc/xc_dom.h
@@ -223,7 +223,6 @@ int xc_dom_mem_init(struct xc_dom_image
 int xc_dom_kernel_check_size(struct xc_dom_image *dom, size_t sz);
 int xc_dom_kernel_max_size(struct xc_dom_image *dom, size_t sz);
 
-int xc_dom_ramdisk_check_size(struct xc_dom_image *dom, size_t sz);
 int xc_dom_ramdisk_max_size(struct xc_dom_image *dom, size_t sz);
 
 int xc_dom_devicetree_max_size(struct xc_dom_image *dom, size_t sz);
Index: xen-4.4.4-testing/tools/libxc/xc_dom_core.c
===================================================================
--- xen-4.4.4-testing.orig/tools/libxc/xc_dom_core.c
+++ xen-4.4.4-testing/tools/libxc/xc_dom_core.c
@@ -281,7 +281,8 @@ int xc_dom_kernel_check_size(struct xc_d
     return 0;
 }
 
-int xc_dom_ramdisk_check_size(struct xc_dom_image *dom, size_t sz)
+static int xc_dom_ramdisk_check_size(struct xc_dom_image *dom, size_t sz,
+                                     size_t raw)
 {
     /* No limit */
     if ( !dom->max_ramdisk_size )
@@ -289,8 +290,9 @@ int xc_dom_ramdisk_check_size(struct xc_
 
     if ( sz > dom->max_ramdisk_size )
     {
-        xc_dom_panic(dom->xch, XC_INVALID_KERNEL,
-                     "ramdisk image too large");
+        if ( raw > dom->max_ramdisk_size )
+            xc_dom_panic(dom->xch, XC_INVALID_KERNEL,
+                         "ramdisk image too large");
         return 1;
     }
 
@@ -959,13 +961,13 @@ int xc_dom_build_image(struct xc_dom_ima
         {
             unziplen = xc_dom_check_gzip(dom->xch,
                                          dom->ramdisk_blob, dom->ramdisk_size);
-            if ( xc_dom_ramdisk_check_size(dom, unziplen) != 0 )
+            if ( xc_dom_ramdisk_check_size(dom, unziplen, dom->ramdisk_size) != 0 )
                 unziplen = 0;
         }
         else
             unziplen = 0;
 
-        ramdisklen = unziplen ? unziplen : dom->ramdisk_size;
+        ramdisklen = max(unziplen, dom->ramdisk_size);
 
         if ( xc_dom_alloc_segment(dom, &dom->ramdisk_seg, "ramdisk",
                                   dom->ramdisk_seg.vstart,
@@ -978,15 +980,15 @@ int xc_dom_build_image(struct xc_dom_ima
                       __FUNCTION__);
             goto err;
         }
-        if ( unziplen )
+        if ( !unziplen ||
+             xc_dom_do_gunzip(dom->xch, dom->ramdisk_blob, dom->ramdisk_size,
+                              ramdiskmap, unziplen) == -1 )
         {
-            if ( xc_dom_do_gunzip(dom->xch,
-                                  dom->ramdisk_blob, dom->ramdisk_size,
-                                  ramdiskmap, ramdisklen) == -1 )
-                goto err;
-        }
-        else
             memcpy(ramdiskmap, dom->ramdisk_blob, dom->ramdisk_size);
+            if ( unziplen > dom->ramdisk_size )
+                memset(ramdiskmap + dom->ramdisk_size, 0,
+                       unziplen - dom->ramdisk_size);
+        }
     }
 
     /* load devicetree */
Index: xen-4.4.4-testing/tools/libxc/xc_private.h
===================================================================
--- xen-4.4.4-testing.orig/tools/libxc/xc_private.h
+++ xen-4.4.4-testing/tools/libxc/xc_private.h
@@ -329,6 +329,22 @@ int xc_ffs16(uint16_t x);
 int xc_ffs32(uint32_t x);
 int xc_ffs64(uint64_t x);
 
+#define min(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x < _y) ? _x : _y; })
+#define max(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x > _y) ? _x : _y; })
+
+#define min_t(type,x,y) \
+        ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
+#define max_t(type,x,y) \
+        ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
+
 #define DOMPRINTF(fmt, args...) xc_dom_printf(dom->xch, fmt, ## args)
 #define DOMPRINTF_CALLED(xch) xc_dom_printf((xch), "%s: called", __FUNCTION__)
 
Index: xen-4.4.4-testing/tools/libxc/xc_dom_decompress_unsafe_xz.c
===================================================================
--- xen-4.4.4-testing.orig/tools/libxc/xc_dom_decompress_unsafe_xz.c
+++ xen-4.4.4-testing/tools/libxc/xc_dom_decompress_unsafe_xz.c
@@ -34,17 +34,6 @@ static inline u32 le32_to_cpup(const u32
 	return cpu_to_le32(*p);
 }
 
-#define min(x,y) ({ \
-        const typeof(x) _x = (x);       \
-        const typeof(y) _y = (y);       \
-        (void) (&_x == &_y);            \
-        _x < _y ? _x : _y; })
-
-#define min_t(type,x,y) \
-        ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
-#define max_t(type,x,y) \
-        ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
-
 #define __force
 #define always_inline
 
