diff --git a/exec.c b/exec.c
index b0b6056..9552366 100644
--- a/exec.c
+++ b/exec.c
@@ -2733,11 +2733,30 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size)
                                 PROT_EXEC|PROT_READ|PROT_WRITE,
                                 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
 #else
-        new_block->host = qemu_vmalloc(size);
+#if defined(__linux__) && defined(__x86_64__)
+#define MAX_TRANSPARENT_HUGEPAGE_SIZE (2*1024*1024)
+	if (size >= MAX_TRANSPARENT_HUGEPAGE_SIZE)
+		/*
+		 * Align on the max transparent hugepage size so that
+		 * "(gfn ^ pfn) & (HPAGE_SIZE-1) == 0" to allow KVM to
+		 * take advantage of hugepages with NPT/EPP or to
+		 * ensure the first 2M of the guest physical ram will
+		 * be mapped by the same hugetlb for QEMU (it is worth
+		 * it even without NPT/EPT).
+		 */
+		new_block->host = qemu_memalign(MAX_TRANSPARENT_HUGEPAGE_SIZE,
+						size);
+	else
+#undef MAX_TRANSPARENT_HUGEPAGE_SIZE
+#endif 
+		new_block->host = qemu_vmalloc(size);
 #endif
 #ifdef MADV_MERGEABLE
         madvise(new_block->host, size, MADV_MERGEABLE);
 #endif
+#ifdef MADV_HUGEPAGE
+        madvise(new_block->host, size, MADV_HUGEPAGE);
+#endif
     }
     new_block->offset = last_ram_offset;
     new_block->length = size;
