From b01a54444fdf6c758355d99225798f926fb451c6 Mon Sep 17 00:00:00 2001
From: Pekka Enberg <penberg@cloudius-systems.com>
Date: Wed, 15 Jan 2014 10:44:07 +0200
Subject: [PATCH] mmu: procfs support
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add procfs_maps() function to core/mmu.cc that returns all the VMAs
formatted for Linux compatible "/proc/<pid>/maps" file.

This will be called by the procfs filesystem.

Limitations:

  * Shared mappings are not identified as such.
  * File-backed mmap offset, device, inode, and pathname are not
    reported.
  * Special region names such as [heap] and [stack] are not reported.

Reviewed-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
---
 core/mmu.cc    | 16 ++++++++++++++++
 include/mmu.hh |  2 ++
 2 files changed, 18 insertions(+)

diff --git a/core/mmu.cc b/core/mmu.cc
index 3cac8fb5c..d7d54c673 100644
--- a/core/mmu.cc
+++ b/core/mmu.cc
@@ -1262,4 +1262,20 @@ error mincore(void *addr, size_t length, unsigned char *vec)
     return no_error();
 }
 
+std::string procfs_maps()
+{
+    std::ostringstream os;
+    WITH_LOCK(vma_list_mutex) {
+        for (auto& vma : vma_list) {
+            char read    = vma.perm() & perm_read  ? 'r' : '-';
+            char write   = vma.perm() & perm_write ? 'w' : '-';
+            char execute = vma.perm() & perm_exec  ? 'x' : '-';
+            char priv    = 'p';
+            osv::fprintf(os, "%x-%x %c%c%c%c 00000000 00:00 0\n",
+                vma.start(), vma.end(), read, write, execute, priv);
+        }
+    }
+    return os.str();
+}
+
 }
diff --git a/include/mmu.hh b/include/mmu.hh
index b6e001a04..2c257a4f4 100644
--- a/include/mmu.hh
+++ b/include/mmu.hh
@@ -194,6 +194,8 @@ void vdepopulate(void* addr, size_t size);
 
 void vm_fault(uintptr_t addr, exception_frame* ef);
 
+std::string procfs_maps();
+
 }
 
 #endif
-- 
GitLab