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

Brian Gaeke gaeke at cs.uiuc.edu
Tue Oct 12 12:47:27 PDT 2004



Changes in directory reopt/lib/TraceCache:

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

Instead of using /proc/<pid>/as and read/write/lseek system calls,
we can use mprotect() on page ranges along with direct reads and writes, 
thereby saving system calls.


---
Diffs of the changes:  (+18 -16)

Index: reopt/lib/TraceCache/VirtualMem.cpp
diff -u reopt/lib/TraceCache/VirtualMem.cpp:1.19 reopt/lib/TraceCache/VirtualMem.cpp:1.20
--- reopt/lib/TraceCache/VirtualMem.cpp:1.19	Thu Sep  2 11:55:44 2004
+++ reopt/lib/TraceCache/VirtualMem.cpp	Tue Oct 12 14:47:17 2004
@@ -13,25 +13,17 @@
 #include <cassert>
 #include <string>
 #include <cstdio>
+#include <set>
+#include <sys/types.h>
+#include <sys/mman.h>
 
 using namespace llvm;
 
 VirtualMem::VirtualMem() {
-  // I/O into the process's address space is accomplished by reading
-  // and writing the /proc/<pid>/as file exported by SVR4 procfs.
-  // Get a file descriptor open to that file now.
-  char ctr[25];
-  sprintf(ctr, "/proc/%d/as", (int) getpid ());
-  fp = open(ctr, O_RDWR);
-  if (fp < 0)
-    throw std::string ("Error opening address space file in procfs.");
 }
 
 unsigned int VirtualMem::readInstrFrmVm(uint64_t frm){
-  lseek(fp, frm, SEEK_SET);
-  unsigned int instr=0;
-  read (fp, &instr, sizeof (unsigned int));
-  return instr;
+  return *((unsigned int *)frm);
 }
 
 unsigned int VirtualMem::readInstrFrmVm(uint64_t frm, TraceCache *tr){
@@ -52,13 +44,19 @@
   return readInstrFrmVm (frm);
 }
 
+std::set<uint64_t> rwxPages;
+
 /// 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);
-  lseek (fp, destAddr, SEEK_SET);
-  write (fp, &newInstr, sizeof (unsigned int));
+  if (rwxPages.find (destAddr &~ 0x7fff) == rwxPages.end ()) {
+    int rc = mprotect ((caddr_t)(destAddr &~ 0x7fff), 0x8000,
+                       PROT_READ|PROT_WRITE|PROT_EXEC);
+    assert (rc == 0);
+  }
+  *((unsigned int *)destAddr) = newInstr;
 }
 
 /// writeTraceToVM - Emit the words in newInstrs to memory starting at
@@ -68,8 +66,12 @@
                                  std::vector<unsigned int> &newInstrs) {
   DEBUG (std::cerr << "writeTraceToVM ("<<std::hex << destAddr << ", ["
                    << std::dec << newInstrs.size() << " instrs]);\n");
-  lseek (fp, destAddr, SEEK_SET);
-  write (fp, &newInstrs[0], newInstrs.size () * sizeof (unsigned int));
+  int rc = mprotect ((caddr_t)(destAddr &~ 0x1fff),
+                     ((newInstrs.size() * sizeof(unsigned int)) + 0x2000)
+                      &~ 0x1fff, PROT_READ|PROT_WRITE|PROT_EXEC);
+  assert (rc == 0);
+  memcpy ((unsigned int *) destAddr, &newInstrs[0],
+          newInstrs.size () * sizeof (unsigned int));
 }
 
 /// changeBranchTarget - Handles two kinds of branches for now, namely,






More information about the llvm-commits mailing list