summaryrefslogtreecommitdiffstats
path: root/usr/klibc/mmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/klibc/mmap.c')
-rw-r--r--usr/klibc/mmap.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/usr/klibc/mmap.c b/usr/klibc/mmap.c
new file mode 100644
index 0000000..2c427ca
--- /dev/null
+++ b/usr/klibc/mmap.c
@@ -0,0 +1,40 @@
+/*
+ * mmap.c
+ */
+
+#include <stdint.h>
+#include <errno.h>
+#include <sys/syscall.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <bitsize.h>
+#include <klibc/sysconfig.h>
+
+/*
+ * Set in SYSCALLS whether or not we should use an unadorned mmap() system
+ * call (typical on 64-bit architectures).
+ */
+#if _KLIBC_USE_MMAP2
+
+/* This architecture uses mmap2(). The Linux mmap2() system call takes
+ a page offset as the offset argument. We need to make sure we have
+ the proper conversion in place. */
+
+extern void *__mmap2(void *, size_t, int, int, int, size_t);
+
+void *mmap(void *start, size_t length, int prot, int flags, int fd,
+ off_t offset)
+{
+ const int mmap2_shift = _KLIBC_MMAP2_SHIFT;
+ const off_t mmap2_mask = ((off_t) 1 << mmap2_shift) - 1;
+
+ if (offset & mmap2_mask) {
+ errno = EINVAL;
+ return MAP_FAILED;
+ }
+
+ return __mmap2(start, length, prot, flags, fd,
+ (size_t) offset >> mmap2_shift);
+}
+
+#endif