[llvm-commits] CVS: reopt/lib/TraceCache/VirtualMem.cpp

Brian Gaeke gaeke at cs.uiuc.edu
Tue Oct 12 13:22:18 PDT 2004



Changes in directory reopt/lib/TraceCache:

VirtualMem.cpp updated: 1.20 -> 1.21
---
Log message:

Chris noticed that nowhere did I actually call rwxPages.insert(anything), and
that I had two mprotect calls.  I fixed that by factoring the mprotect
calls out into a new static function ensurePageIsWritable(). Thanks!


---
Diffs of the changes:  (+15 -9)

Index: reopt/lib/TraceCache/VirtualMem.cpp
diff -u reopt/lib/TraceCache/VirtualMem.cpp:1.20 reopt/lib/TraceCache/VirtualMem.cpp:1.21
--- reopt/lib/TraceCache/VirtualMem.cpp:1.20	Tue Oct 12 14:47:17 2004
+++ reopt/lib/TraceCache/VirtualMem.cpp	Tue Oct 12 15:22:08 2004
@@ -46,16 +46,21 @@
 
 std::set<uint64_t> rwxPages;
 
+static inline void ensurePageIsWritable (uint64_t destAddr) {
+  if (rwxPages.find (destAddr &~ 0x1fff) == rwxPages.end ()) {
+    int rc = mprotect ((caddr_t)(destAddr & ~0x1fff), 0x2000,
+                       PROT_READ|PROT_WRITE|PROT_EXEC);
+    assert (rc == 0);
+    rwxPages.insert (destAddr &~ 0x1fff);
+  }
+}
+
 /// writeInstToVM - Emit the word newInstr to memory at address destAddr.
 ///
 void VirtualMem::writeInstToVM (uint64_t destAddr, unsigned int newInstr) {
   DEBUG (std::cerr << "writeInstToVM (" << std::hex << destAddr << ", "
                    << newInstr << ");\n" << std::dec);
-  if (rwxPages.find (destAddr &~ 0x7fff) == rwxPages.end ()) {
-    int rc = mprotect ((caddr_t)(destAddr &~ 0x7fff), 0x8000,
-                       PROT_READ|PROT_WRITE|PROT_EXEC);
-    assert (rc == 0);
-  }
+  ensurePageIsWritable (destAddr);
   *((unsigned int *)destAddr) = newInstr;
 }
 
@@ -66,10 +71,11 @@
                                  std::vector<unsigned int> &newInstrs) {
   DEBUG (std::cerr << "writeTraceToVM ("<<std::hex << destAddr << ", ["
                    << std::dec << newInstrs.size() << " instrs]);\n");
-  int rc = mprotect ((caddr_t)(destAddr &~ 0x1fff),
-                     ((newInstrs.size() * sizeof(unsigned int)) + 0x2000)
-                      &~ 0x1fff, PROT_READ|PROT_WRITE|PROT_EXEC);
-  assert (rc == 0);
+  uint64_t firstPage = destAddr & ~0x1fff,
+           lastPage = (destAddr + (newInstrs.size() * sizeof(unsigned int))
+                       + 0x2000) & ~0x1fff;
+  for (uint64_t addr = firstPage; addr <= lastPage; addr += 0x2000)
+	ensurePageIsWritable (addr);
   memcpy ((unsigned int *) destAddr, &newInstrs[0],
           newInstrs.size () * sizeof (unsigned int));
 }






More information about the llvm-commits mailing list