1 #include <unistd.h> 2 #include <stdio.h> 3 #include <errno.h> 4 #include <sys/mman.h> 5 6 /* Derived from development/ndk/platforms/android-3/include/asm-generic/mman.h */ 7 #define PROT_READ_PORTABLE 0x1 8 #define PROT_WRITE_PORTABLE 0x2 9 #define PROT_EXEC_PORTABLE 0x4 10 #define PROT_SEM_PORTABLE 0x8 11 #define PROT_NONE_PORTABLE 0x0 12 #define PROT_GROWSDOWN_PORTABLE 0x01000000 13 #define PROT_GROWSUP_PORTABLE 0x02000000 14 15 #define MAP_SHARED_PORTABLE 0x01 16 #define MAP_PRIVATE_PORTABLE 0x02 17 #define MAP_TYPE_PORTABLE 0x0f 18 #define MAP_FIXED_PORTABLE 0x10 19 #define MAP_ANONYMOUS_PORTABLE 0x20 20 21 #define MS_ASYNC_PORTABLE 1 22 #define MS_INVALIDATE_PORTABLE 2 23 #define MS_SYNC_PORTABLE 4 24 25 #define MADV_NORMAL_PORTABLE 0 26 #define MADV_RANDOM_PORTABLE 1 27 #define MADV_SEQUENTIAL_PORTABLE 2 28 #define MADV_WILLNEED_PORTABLE 3 29 #define MADV_DONTNEED_PORTABLE 4 30 31 #define MADV_REMOVE_PORTABLE 9 32 #define MADV_DONTFORK_PORTABLE 10 33 #define MADV_DOFORK_PORTABLE 11 34 35 #define MAP_ANON_PORTABLE MAP_ANONYMOUS_PORTABLE 36 #define MAP_FILE_PORTABLE 0 37 38 /* Derived from development/ndk/platforms/android-3/include/asm-generic/mman.h */ 39 #define MAP_GROWSDOWN_PORTABLE 0x0100 40 #define MAP_DENYWRITE_PORTABLE 0x0800 41 #define MAP_EXECUTABLE_PORTABLE 0x1000 42 #define MAP_LOCKED_PORTABLE 0x2000 43 #define MAP_NORESERVE_PORTABLE 0x4000 44 #define MAP_POPULATE_PORTABLE 0x8000 45 #define MAP_NONBLOCK_PORTABLE 0x10000 46 47 #define MCL_CURRENT_PORTABLE 1 48 #define MCL_FUTURE_PORTABLE 2 49 50 51 #if MAP_ANONYMOUS_PORTABLE==MAP_ANONYMOUS 52 #error Bad build environment 53 #endif 54 55 static inline int mips_change_prot(int prot) 56 { 57 /* Only PROT_SEM is different */ 58 if (prot & PROT_SEM_PORTABLE) { 59 prot &= ~PROT_SEM_PORTABLE; 60 prot |= PROT_SEM; 61 } 62 63 return prot; 64 } 65 66 static inline int mips_change_flags(int flags) 67 { 68 int mipsflags = 0; 69 /* These are the documented flags for mmap */ 70 if (flags & MAP_SHARED_PORTABLE) 71 mipsflags |= MAP_SHARED; 72 if (flags & MAP_PRIVATE_PORTABLE) 73 mipsflags |= MAP_PRIVATE; 74 #if defined(MAP_32BIT_PORTABLE) && defined(MAP_32BIT) 75 if (flags & MAP_32BIT_PORTABLE) 76 mipsflags |= MAP_32BIT; 77 #endif 78 if (flags & MAP_ANONYMOUS_PORTABLE) 79 mipsflags |= MAP_ANONYMOUS; 80 if (flags & MAP_FIXED_PORTABLE) 81 mipsflags |= MAP_FIXED; 82 if (flags & MAP_GROWSDOWN_PORTABLE) 83 mipsflags |= MAP_GROWSDOWN; 84 #if defined(MAP_HUGETLB_PORTABLE) && defined(MAP_HUGETLB) 85 if (flags & MAP_HUGETLB_PORTABLE) 86 mipsflags |= MAP_HUGETLB; 87 #endif 88 if (flags & MAP_LOCKED_PORTABLE) 89 mipsflags |= MAP_LOCKED; 90 if (flags & MAP_NONBLOCK_PORTABLE) 91 mipsflags |= MAP_NONBLOCK; 92 if (flags & MAP_NORESERVE_PORTABLE) 93 mipsflags |= MAP_NORESERVE; 94 if (flags & MAP_POPULATE_PORTABLE) 95 mipsflags |= MAP_POPULATE; 96 #if defined(MAP_STACK_PORTABLE) && defined(MAP_STACK) 97 if (flags & MAP_STACK_PORTABLE) 98 mipsflags |= MAP_STACK; 99 #endif 100 101 return mipsflags; 102 } 103 104 #define MMAP2_SHIFT 12 105 extern void *__mmap2(void *, size_t, int, int, int, size_t); 106 void *mmap(void *addr, size_t size, int prot, int flags, int fd, long offset) 107 { 108 if ( offset & ((1UL << MMAP2_SHIFT)-1) ) { 109 errno = EINVAL; 110 return MAP_FAILED; 111 } 112 113 return __mmap2(addr, size, mips_change_prot(prot), mips_change_flags(flags), 114 fd, (size_t)offset >> MMAP2_SHIFT); 115 } 116