[PATCH] mmap: provide implementation using mmap2()

Enrico Scholz <[email protected]> Sun, 20 Feb 2011 16:16:37 +0100
Newsgroups gmane.linux.lib.dietlibc
Message-ID <1298215004-28546-1-git-send-email-enrico.scholz@informatik.tu-chemnitz.de>
The mmap(2) syscall is obsoleted on some platforms (e.g. ARM-EABI,
Blackfin, FRV, Microblaze) and must be emulated by a call to mmap2(2).

Patch provides such a generic implementation.

Signed-off-by: Enrico Scholz <[email protected]>
---
 lib/__mmap.c      |   20 ++++++++++++++++++++
 syscalls.s/mmap.S |    4 ++++
 2 files changed, 24 insertions(+), 0 deletions(-)
 create mode 100644 lib/__mmap.c

diff --git a/lib/__mmap.c b/lib/__mmap.c
new file mode 100644
index 0000000..1b035c3
--- /dev/null
+++ b/lib/__mmap.c
@@ -0,0 +1,20 @@
+#include <errno.h>
+#include <sys/mman.h>
+#include <syscalls.h>
+
+#ifndef __NR_mmap
+void*__mmap2(void*start,size_t length,int prot,int flags,int fd,off_t pgoffset);
+void *mmap(void *addr, size_t length, int prot, int flags, int fd,
+	   off_t offset)
+{
+  size_t pgsz = 4096;		 /* TODO: fix for dynamic PAGESIZEs needed? */
+  void	*res;
+
+  if (__unlikely(offset & (pgsz - 1))) {
+    errno = -EINVAL;
+    res = MAP_FAILED;
+  } else
+    res = __mmap2(addr, length, prot, flags, fd, offset / pgsz);
+  return res;
+}
+#endif
diff --git a/syscalls.s/mmap.S b/syscalls.s/mmap.S
index 2e57fbb..486b531 100644
--- a/syscalls.s/mmap.S
+++ b/syscalls.s/mmap.S
@@ -1,3 +1,7 @@
 #include "syscalls.h"
 
+#ifdef __NR_mmap
+
 syscall(mmap,mmap)
+
+#endif
-- 
1.7.4