From 50a9569b591a83071052614f2ff497ff2f2aa2ef Mon Sep 17 00:00:00 2001
From: balrog <balrog@c046a42c-6fe2-441c-8c8c-71466251a162>
Date: Wed, 12 Dec 2007 01:16:23 +0000
Subject: [PATCH] Mark host pages as reserved (Magnus Damm).

git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3813 c046a42c-6fe2-441c-8c8c-71466251a162
---
 cpu-all.h         |  1 +
 exec.c            | 21 +++++++++++++++++++++
 linux-user/mmap.c |  2 +-
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/cpu-all.h b/cpu-all.h
index 6b78435093..7876a470e6 100644
--- a/cpu-all.h
+++ b/cpu-all.h
@@ -716,6 +716,7 @@ extern unsigned long qemu_host_page_mask;
 /* original state of the write flag (used when tracking self-modifying
    code */
 #define PAGE_WRITE_ORG 0x0010
+#define PAGE_RESERVED  0x0020
 
 void page_dump(FILE *f);
 int page_get_flags(target_ulong address);
diff --git a/exec.c b/exec.c
index fd08ec9278..07d5de04ae 100644
--- a/exec.c
+++ b/exec.c
@@ -209,6 +209,27 @@ static void page_init(void)
     qemu_host_page_mask = ~(qemu_host_page_size - 1);
     l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
     memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
+
+#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
+    {
+        long long startaddr, endaddr;
+        FILE *f;
+        int n;
+
+        f = fopen("/proc/self/maps", "r");
+        if (f) {
+            do {
+                n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
+                if (n == 2) {
+                    page_set_flags(TARGET_PAGE_ALIGN(startaddr),
+                                   TARGET_PAGE_ALIGN(endaddr),
+                                   PAGE_RESERVED); 
+                }
+            } while (!feof(f));
+            fclose(f);
+        }
+    }
+#endif
 }
 
 static inline PageDesc *page_find_alloc(unsigned int index)
diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index 3f2647f304..6292826b43 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -162,7 +162,7 @@ static abi_ulong mmap_next_start = 0x40000000;
    'start'. If 'start' == 0, then a default start address is used.
    Return -1 if error.
 */
-/* XXX: should mark pages used by the host as reserved to be sure not
+/* page_init() marks pages used by the host as reserved to be sure not
    to use them. */
 static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
 {
-- 
2.30.2