From ed319bda0b7f7b6bb4a946d24682897e7f465c23 Mon Sep 17 00:00:00 2001
From: Nguyen Anh Quynh <aquynh@gmail.com>
Date: Thu, 24 Dec 2015 09:51:17 +0800
Subject: [PATCH] x86: identity map guest address to host address. this fixes
 issue #300

---
 qemu/softmmu_template.h   | 3 +--
 qemu/target-i386/helper.c | 8 ++++++++
 tests/unit/Makefile       | 2 ++
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/qemu/softmmu_template.h b/qemu/softmmu_template.h
index 2818bde0..3b2a6295 100644
--- a/qemu/softmmu_template.h
+++ b/qemu/softmmu_template.h
@@ -238,8 +238,7 @@ WORD_TYPE helper_le_ld_name(CPUArchState *env, target_ulong addr, int mmu_idx,
                                        uc, UC_MEM_READ_PROT, addr, DATA_SIZE, 0,
                                        uc->hook_callbacks[uc->hook_mem_read_prot_idx].user_data)) {
             env->invalid_error = UC_ERR_OK;
-        }
-        else {
+        } else {
             env->invalid_addr = addr;
             env->invalid_error = UC_ERR_READ_PROT;
             // printf("***** Invalid memory read (non-readable) at " TARGET_FMT_lx "\n", addr);
diff --git a/qemu/target-i386/helper.c b/qemu/target-i386/helper.c
index ca8af555..b8ffbc28 100644
--- a/qemu/target-i386/helper.c
+++ b/qemu/target-i386/helper.c
@@ -764,6 +764,8 @@ do_check_protect_pse36:
         }
     }
  do_mapping:
+
+#if 0
     pte = pte & env->a20_mask;
 
     /* align to page_size */
@@ -774,6 +776,12 @@ do_check_protect_pse36:
     vaddr = addr & TARGET_PAGE_MASK;
     page_offset = vaddr & (page_size - 1);
     paddr = pte + page_offset;
+#endif
+
+    // Unicorn: indentity map guest virtual address to host virtual address
+    vaddr = addr & TARGET_PAGE_MASK;
+    paddr = vaddr;
+    //printf(">>> map address %"PRIx64" to %"PRIx64"\n", vaddr, paddr);
 
     tlb_set_page(cs, vaddr, paddr, prot, mmu_idx, page_size);
     return 0;
diff --git a/tests/unit/Makefile b/tests/unit/Makefile
index 2b53e8e8..5a908e1d 100644
--- a/tests/unit/Makefile
+++ b/tests/unit/Makefile
@@ -19,6 +19,8 @@ test: ${ALL_TESTS}
 	./test_sanity
 	./test_x86
 	./test_mem_map
+	./test_mem_map_ptr
+	./test_mem_high
 
 test_sanity: test_sanity.c
 test_x86: test_x86.c