1 #include <unistd.h>
2 #include <sys/mman.h>
3 #include <errno.h>
4 #include <stdint.h>
5 #include <limits.h>
6 #include "syscall.h"
7
dummy(void)8 static void dummy(void) { }
9 weak_alias(dummy, __vm_wait);
10
11 #define UNIT SYSCALL_MMAP2_UNIT
12 #define OFF_MASK ((-0x2000ULL << (8*sizeof(syscall_arg_t)-1)) | (UNIT-1))
13
__mmap(void * start,size_t len,int prot,int flags,int fd,off_t off)14 void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off)
15 {
16 long ret;
17 if (off & OFF_MASK) {
18 errno = EINVAL;
19 return MAP_FAILED;
20 }
21 if (len >= PTRDIFF_MAX) {
22 errno = ENOMEM;
23 return MAP_FAILED;
24 }
25 if (len == 0) {
26 errno = EINVAL;
27 return MAP_FAILED;
28 }
29 if (flags & MAP_FIXED) {
30 __vm_wait();
31 }
32 #ifdef SYS_mmap2
33 ret = __syscall(SYS_mmap2, start, len, prot, flags, fd, off/UNIT);
34 #else
35 ret = __syscall(SYS_mmap, start, len, prot, flags, fd, off);
36 #endif
37 /* Fixup incorrect EPERM from kernel. */
38 if (ret == -EPERM && !start && (flags&MAP_ANON) && !(flags&MAP_FIXED))
39 ret = -ENOMEM;
40 return (void *)__syscall_ret(ret);
41 }
42
43
44 #ifdef HOOK_ENABLE
45 void* __libc_mmap(void*, size_t, int, int, int, off_t);
46 weak_alias(__mmap, __libc_mmap);
47 weak_alias(__libc_mmap, mmap64);
48 #else
49 weak_alias(__mmap, mmap);
50 weak_alias(mmap, mmap64);
51 #endif // HOOK_ENABLE
52
53 /*
54 Adapter function for lldb to not deal with 64-bit arguments on 32-bit systems
55 Issue I661Z1
56 Test: Build and boot devices.
57 */
__lldb_mmap(void * start,size_t len,int prot,int flags,int fd,intptr_t off)58 void *__lldb_mmap(void *start, size_t len, int prot, int flags, int fd, intptr_t off) {
59 return mmap(start, len, prot, flags, fd, off);
60 }
61