chiark / gitweb /
volume_id: provide libvolume_id.a file
[elogind.git] / klibc / klibc / mmap.c
1 /*
2  * mmap.c
3  */
4
5 #include <stdint.h>
6 #include <errno.h>
7 #include <sys/syscall.h>
8 #include <sys/mman.h>
9 #include <unistd.h>
10 #include <asm/page.h>           /* For PAGE_SHIFT */
11 #include <bitsize.h>
12
13 /*
14  * MMAP2_SHIFT is definitely *NOT* equal to getpageshift() for
15  * many 32-bit architectures.  Supposedly this is fixed to 12
16  * for all 32-bit architectures.  CHECK THIS!!!
17  */
18 # define MMAP2_SHIFT    12      /* Fixed by syscall definition */
19
20 /*
21  * Set in SYSCALLS whether or not we should use an unadorned mmap() system
22  * call (typical on 64-bit architectures).
23  */
24 #if (_BITSIZE == 32 && defined(__NR_mmap2)) || (_BITSIZE == 64 && !defined(__NR_mmap))
25
26 /* This architecture uses mmap2(). The Linux mmap2() system call takes
27    a page offset as the offset argument.  We need to make sure we have
28    the proper conversion in place. */
29
30 extern void *__mmap2(void *, size_t, int, int, int, size_t);
31
32 void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
33 {
34   const int mmap2_shift = MMAP2_SHIFT;
35   const unsigned long mmap2_mask = (1UL << mmap2_shift) - 1;
36
37   if ( offset & mmap2_mask ) {
38     errno = EINVAL;
39     return MAP_FAILED;
40   }
41
42   return __mmap2(start, length, prot, flags, fd, (size_t)offset >> mmap2_shift);
43 }
44
45 #endif
46
47
48     
49