summaryrefslogtreecommitdiff
path: root/klibc/klibc/mmap.c
blob: 89cf3a6a8f1b28120c336548586e3c2a878dbe84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * mmap.c
 */

#include <stdint.h>
#include <errno.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <asm/page.h>		/* For PAGE_SHIFT */

#if defined(__sparc__)
# define MMAP2_SHIFT	12	/* Fixed by syscall definition */
#else
# define MMAP2_SHIFT	PAGE_SHIFT
#endif
#define MMAP2_MASK	((1UL << MMAP2_SHIFT)-1)

/*
 * Prefer mmap2() over mmap(), except on the architectures listed
 */

#if defined(__NR_mmap2) && !defined(__sparc__) && !defined(__ia64__) && !defined(__powerpc__) && !defined(__powerpc64__)

/* This architecture uses mmap2() */

static inline _syscall6(void *,mmap2,void *,start,size_t,length,int,prot,int,flags,int,fd,off_t,offset);

/* 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. */

void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
{
  if ( offset & MMAP2_MASK ) {
    errno = EINVAL;
    return MAP_FAILED;
  }

  return mmap2(start, length, prot, flags, fd, (size_t)offset >> MMAP2_SHIFT);
}

#else

/* This architecture uses a plain mmap() system call */
/* Only use this for architectures where mmap() is a real 6-argument system call! */

_syscall6(void *,mmap,void *,start,size_t,length,int,prot,int,flags,int,fd,off_t,offset)

#endif