1 /*
2  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  */
6 /* Massivly hacked up for uClibc by Erik Andersen */
7 
8 #include <_lfs_64.h>
9 #include <stdint.h>
10 #include <sys/syscall.h>
11 #include <errno.h>
12 #include <sys/mman.h>
13 
14 #ifndef __NR_mmap2
15 
16 /*
17  * This version is a stub that just chops off everything at the mmap 32 bit
18  * mmap() address space...  You will probably need to add in an arch specific
19  * implementation to override this as there is not a generic way for me to
20  * implement this particular syscall if your arch lacks _syscall6...
21  *
22  */
23 
mmap64(void * addr,size_t len,int prot,int flags,int fd,__off64_t offset)24 void *mmap64(void *addr, size_t len, int prot, int flags, int fd, __off64_t offset)
25 {
26 	if (offset != (off_t) offset ||
27 	    (offset + len) != (off_t) (offset + len)) {
28 		__set_errno(EINVAL);
29 		return MAP_FAILED;
30 	}
31 
32 	return mmap(addr, len, prot, flags, fd, (off_t) offset);
33 }
34 
35 #else
36 # include <bits/uClibc_page.h>
37 
38 /* Some architectures always use 12 as page shift for mmap2() even though the
39  * real PAGE_SHIFT != 12.  Other architectures use the same value as
40  * PAGE_SHIFT...
41  */
42 # ifndef MMAP2_PAGE_SHIFT
43 #  define MMAP2_PAGE_SHIFT 12
44 # endif
45 
mmap64(void * addr,size_t len,int prot,int flags,int fd,__off64_t offset)46 void *mmap64(void *addr, size_t len, int prot, int flags, int fd, __off64_t offset)
47 {
48 	/*
49 	 * Some arches check the size in INLINE_SYSCALL() and barf if it's
50 	 * too big (i.e. a 64bit value getting truncated to 32bit).
51 	 */
52 # if __WORDSIZE == 32
53 	uint32_t sysoff;
54 # else
55 	uint64_t sysoff;
56 # endif
57 
58 	if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1)) {
59 		__set_errno(EINVAL);
60 		return MAP_FAILED;
61 	}
62 
63 	/*
64 	 * We know __off64_t is always a signed 64-bit type, but need things
65 	 * to be unsigned before doing the shift.  If it isn't, we might
66 	 * sign extend things and pass in the wrong value.  So cast it to
67 	 * an unsigned 64-bit value before doing the shift.
68 	 */
69 	sysoff = (uint64_t)offset >> MMAP2_PAGE_SHIFT;
70 
71 	return (void*) INLINE_SYSCALL(mmap2, 6, addr, len, prot, flags, fd, sysoff);
72 }
73 
74 #endif
75